org.el 805 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433
  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. (defun org-version (&optional here)
  197. "Show the org-mode version in the echo area.
  198. With prefix arg HERE, insert it at point."
  199. (interactive "P")
  200. (let* ((origin default-directory)
  201. (version org-version)
  202. (git-version)
  203. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  204. (when (and (file-exists-p (expand-file-name ".git" dir))
  205. (executable-find "git"))
  206. (unwind-protect
  207. (progn
  208. (cd dir)
  209. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  210. (with-current-buffer "*Shell Command Output*"
  211. (goto-char (point-min))
  212. (setq git-version (buffer-substring (point) (point-at-eol))))
  213. (subst-char-in-string ?- ?. git-version t)
  214. (when (string-match "\\S-"
  215. (shell-command-to-string
  216. "git diff-index --name-only HEAD --"))
  217. (setq git-version (concat git-version ".dirty")))
  218. (setq version (concat version " (" git-version ")"))))
  219. (cd origin)))
  220. (setq version (format "Org-mode version %s" version))
  221. (if here (insert version))
  222. (message version)))
  223. ;;; Compatibility constants
  224. ;;; The custom variables
  225. (defgroup org nil
  226. "Outline-based notes management and organizer."
  227. :tag "Org"
  228. :group 'outlines
  229. :group 'calendar)
  230. (defcustom org-mode-hook nil
  231. "Mode hook for Org-mode, run after the mode was turned on."
  232. :group 'org
  233. :type 'hook)
  234. (defcustom org-load-hook nil
  235. "Hook that is run after org.el has been loaded."
  236. :group 'org
  237. :type 'hook)
  238. (defcustom org-log-buffer-setup-hook nil
  239. "Hook that is run after an Org log buffer is created."
  240. :group 'org
  241. :version "24.1"
  242. :type 'hook)
  243. (defvar org-modules) ; defined below
  244. (defvar org-modules-loaded nil
  245. "Have the modules been loaded already?")
  246. (defun org-load-modules-maybe (&optional force)
  247. "Load all extensions listed in `org-modules'."
  248. (when (or force (not org-modules-loaded))
  249. (mapc (lambda (ext)
  250. (condition-case nil (require ext)
  251. (error (message "Problems while trying to load feature `%s'" ext))))
  252. org-modules)
  253. (setq org-modules-loaded t)))
  254. (defun org-set-modules (var value)
  255. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  256. (set var value)
  257. (when (featurep 'org)
  258. (org-load-modules-maybe 'force)))
  259. (when (org-bound-and-true-p org-modules)
  260. (let ((a (member 'org-infojs org-modules)))
  261. (and a (setcar a 'org-jsinfo))))
  262. (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)
  263. "Modules that should always be loaded together with org.el.
  264. If a description starts with <C>, the file is not part of Emacs
  265. and loading it will require that you have downloaded and properly installed
  266. the org-mode distribution.
  267. You can also use this system to load external packages (i.e. neither Org
  268. core modules, nor modules from the CONTRIB directory). Just add symbols
  269. to the end of the list. If the package is called org-xyz.el, then you need
  270. to add the symbol `xyz', and the package must have a call to
  271. (provide 'org-xyz)"
  272. :group 'org
  273. :set 'org-set-modules
  274. :type
  275. '(set :greedy t
  276. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  277. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  278. (const :tag " crypt: Encryption of subtrees" org-crypt)
  279. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  280. (const :tag " docview: Links to doc-view buffers" org-docview)
  281. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  282. (const :tag " id: Global IDs for identifying entries" org-id)
  283. (const :tag " info: Links to Info nodes" org-info)
  284. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  285. (const :tag " habit: Track your consistency with habits" org-habit)
  286. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  287. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  288. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  289. (const :tag " mew Links to Mew folders/messages" org-mew)
  290. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  291. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  292. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  293. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  294. (const :tag " vm: Links to VM folders/messages" org-vm)
  295. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  296. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  297. (const :tag " mouse: Additional mouse support" org-mouse)
  298. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  299. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  300. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  301. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  302. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  303. (const :tag "C collector: Collect properties into tables" org-collector)
  304. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  305. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  306. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  307. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  308. (const :tag "C eval: Include command output as text" org-eval)
  309. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  310. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  311. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  312. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  313. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  314. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  315. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  316. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  317. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  318. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  319. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  320. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  321. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  322. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  323. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  324. (const :tag "C registry: A registry for Org-mode links" org-registry)
  325. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  326. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  327. (const :tag "C secretary: Team management with org-mode" org-secretary)
  328. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  329. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  330. (const :tag "C track: Keep up with Org-mode development" org-track)
  331. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  332. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  333. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  334. (defcustom org-support-shift-select nil
  335. "Non-nil means make shift-cursor commands select text when possible.
  336. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  337. start selecting a region, or enlarge regions started in this way.
  338. In Org-mode, in special contexts, these same keys are used for
  339. other purposes, important enough to compete with shift selection.
  340. Org tries to balance these needs by supporting `shift-select-mode'
  341. outside these special contexts, under control of this variable.
  342. The default of this variable is nil, to avoid confusing behavior. Shifted
  343. cursor keys will then execute Org commands in the following contexts:
  344. - on a headline, changing TODO state (left/right) and priority (up/down)
  345. - on a time stamp, changing the time
  346. - in a plain list item, changing the bullet type
  347. - in a property definition line, switching between allowed values
  348. - in the BEGIN line of a clock table (changing the time block).
  349. Outside these contexts, the commands will throw an error.
  350. When this variable is t and the cursor is not in a special
  351. context, Org-mode will support shift-selection for making and
  352. enlarging regions. To make this more effective, the bullet
  353. cycling will no longer happen anywhere in an item line, but only
  354. if the cursor is exactly on the bullet.
  355. If you set this variable to the symbol `always', then the keys
  356. will not be special in headlines, property lines, and item lines,
  357. to make shift selection work there as well. If this is what you
  358. want, you can use the following alternative commands: `C-c C-t'
  359. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  360. can be used to switch TODO sets, `C-c -' to cycle item bullet
  361. types, and properties can be edited by hand or in column view.
  362. However, when the cursor is on a timestamp, shift-cursor commands
  363. will still edit the time stamp - this is just too good to give up.
  364. XEmacs user should have this variable set to nil, because
  365. `shift-select-mode' is in Emacs 23 or later only."
  366. :group 'org
  367. :type '(choice
  368. (const :tag "Never" nil)
  369. (const :tag "When outside special context" t)
  370. (const :tag "Everywhere except timestamps" always)))
  371. (defcustom org-loop-over-headlines-in-active-region nil
  372. "Shall some commands act upon headlines in the active region?
  373. When set to `t', some commands will be performed in all headlines
  374. within the active region.
  375. When set to `start-level', some commands will be performed in all
  376. headlines within the active region, provided that these headlines
  377. are of the same level than the first one.
  378. When set to a string, those commands will be performed on the
  379. matching headlines within the active region. Such string must be
  380. a tags/property/todo match as it is used in the agenda tags view.
  381. The list of commands is: `org-schedule', `org-deadline',
  382. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  383. `org-archive-to-archive-sibling'. The archiving commands skip
  384. already archived entries."
  385. :type '(choice (const :tag "Don't loop" nil)
  386. (const :tag "All headlines in active region" t)
  387. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  388. (string :tag "Tags/Property/Todo matcher"))
  389. :version "24.1"
  390. :group 'org-todo
  391. :group 'org-archive)
  392. (defgroup org-startup nil
  393. "Options concerning startup of Org-mode."
  394. :tag "Org Startup"
  395. :group 'org)
  396. (defcustom org-startup-folded t
  397. "Non-nil means entering Org-mode will switch to OVERVIEW.
  398. This can also be configured on a per-file basis by adding one of
  399. the following lines anywhere in the buffer:
  400. #+STARTUP: fold (or `overview', this is equivalent)
  401. #+STARTUP: nofold (or `showall', this is equivalent)
  402. #+STARTUP: content
  403. #+STARTUP: showeverything"
  404. :group 'org-startup
  405. :type '(choice
  406. (const :tag "nofold: show all" nil)
  407. (const :tag "fold: overview" t)
  408. (const :tag "content: all headlines" content)
  409. (const :tag "show everything, even drawers" showeverything)))
  410. (defcustom org-startup-truncated t
  411. "Non-nil means entering Org-mode will set `truncate-lines'.
  412. This is useful since some lines containing links can be very long and
  413. uninteresting. Also tables look terrible when wrapped."
  414. :group 'org-startup
  415. :type 'boolean)
  416. (defcustom org-startup-indented nil
  417. "Non-nil means turn on `org-indent-mode' on startup.
  418. This can also be configured on a per-file basis by adding one of
  419. the following lines anywhere in the buffer:
  420. #+STARTUP: indent
  421. #+STARTUP: noindent"
  422. :group 'org-structure
  423. :type '(choice
  424. (const :tag "Not" nil)
  425. (const :tag "Globally (slow on startup in large files)" t)))
  426. (defcustom org-use-sub-superscripts t
  427. "Non-nil means interpret \"_\" and \"^\" for export.
  428. When this option is turned on, you can use TeX-like syntax for sub- and
  429. superscripts. Several characters after \"_\" or \"^\" will be
  430. considered as a single item - so grouping with {} is normally not
  431. needed. For example, the following things will be parsed as single
  432. sub- or superscripts.
  433. 10^24 or 10^tau several digits will be considered 1 item.
  434. 10^-12 or 10^-tau a leading sign with digits or a word
  435. x^2-y^3 will be read as x^2 - y^3, because items are
  436. terminated by almost any nonword/nondigit char.
  437. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  438. Still, ambiguity is possible - so when in doubt use {} to enclose the
  439. sub/superscript. If you set this variable to the symbol `{}',
  440. the braces are *required* in order to trigger interpretations as
  441. sub/superscript. This can be helpful in documents that need \"_\"
  442. frequently in plain text.
  443. Not all export backends support this, but HTML does.
  444. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  445. :group 'org-startup
  446. :group 'org-export-translation
  447. :version "24.1"
  448. :type '(choice
  449. (const :tag "Always interpret" t)
  450. (const :tag "Only with braces" {})
  451. (const :tag "Never interpret" nil)))
  452. (if (fboundp 'defvaralias)
  453. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  454. (defcustom org-startup-with-beamer-mode nil
  455. "Non-nil means turn on `org-beamer-mode' on startup.
  456. This can also be configured on a per-file basis by adding one of
  457. the following lines anywhere in the buffer:
  458. #+STARTUP: beamer"
  459. :group 'org-startup
  460. :version "24.1"
  461. :type 'boolean)
  462. (defcustom org-startup-align-all-tables nil
  463. "Non-nil means align all tables when visiting a file.
  464. This is useful when the column width in tables is forced with <N> cookies
  465. in table fields. Such tables will look correct only after the first re-align.
  466. This can also be configured on a per-file basis by adding one of
  467. the following lines anywhere in the buffer:
  468. #+STARTUP: align
  469. #+STARTUP: noalign"
  470. :group 'org-startup
  471. :type 'boolean)
  472. (defcustom org-startup-with-inline-images nil
  473. "Non-nil means show inline images when loading a new Org file.
  474. This can also be configured on a per-file basis by adding one of
  475. the following lines anywhere in the buffer:
  476. #+STARTUP: inlineimages
  477. #+STARTUP: noinlineimages"
  478. :group 'org-startup
  479. :version "24.1"
  480. :type 'boolean)
  481. (defcustom org-insert-mode-line-in-empty-file nil
  482. "Non-nil means insert the first line setting Org-mode in empty files.
  483. When the function `org-mode' is called interactively in an empty file, this
  484. normally means that the file name does not automatically trigger Org-mode.
  485. To ensure that the file will always be in Org-mode in the future, a
  486. line enforcing Org-mode will be inserted into the buffer, if this option
  487. has been set."
  488. :group 'org-startup
  489. :type 'boolean)
  490. (defcustom org-replace-disputed-keys nil
  491. "Non-nil means use alternative key bindings for some keys.
  492. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  493. These keys are also used by other packages like shift-selection-mode'
  494. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  495. If you want to use Org-mode together with one of these other modes,
  496. or more generally if you would like to move some Org-mode commands to
  497. other keys, set this variable and configure the keys with the variable
  498. `org-disputed-keys'.
  499. This option is only relevant at load-time of Org-mode, and must be set
  500. *before* org.el is loaded. Changing it requires a restart of Emacs to
  501. become effective."
  502. :group 'org-startup
  503. :type 'boolean)
  504. (defcustom org-use-extra-keys nil
  505. "Non-nil means use extra key sequence definitions for certain commands.
  506. This happens automatically if you run XEmacs or if `window-system'
  507. is nil. This variable lets you do the same manually. You must
  508. set it before loading org.
  509. Example: on Carbon Emacs 22 running graphically, with an external
  510. keyboard on a Powerbook, the default way of setting M-left might
  511. not work for either Alt or ESC. Setting this variable will make
  512. it work for ESC."
  513. :group 'org-startup
  514. :type 'boolean)
  515. (if (fboundp 'defvaralias)
  516. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  517. (defcustom org-disputed-keys
  518. '(([(shift up)] . [(meta p)])
  519. ([(shift down)] . [(meta n)])
  520. ([(shift left)] . [(meta -)])
  521. ([(shift right)] . [(meta +)])
  522. ([(control shift right)] . [(meta shift +)])
  523. ([(control shift left)] . [(meta shift -)]))
  524. "Keys for which Org-mode and other modes compete.
  525. This is an alist, cars are the default keys, second element specifies
  526. the alternative to use when `org-replace-disputed-keys' is t.
  527. Keys can be specified in any syntax supported by `define-key'.
  528. The value of this option takes effect only at Org-mode's startup,
  529. therefore you'll have to restart Emacs to apply it after changing."
  530. :group 'org-startup
  531. :type 'alist)
  532. (defun org-key (key)
  533. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  534. Or return the original if not disputed.
  535. Also apply the translations defined in `org-xemacs-key-equivalents'."
  536. (when org-replace-disputed-keys
  537. (let* ((nkey (key-description key))
  538. (x (org-find-if (lambda (x)
  539. (equal (key-description (car x)) nkey))
  540. org-disputed-keys)))
  541. (setq key (if x (cdr x) key))))
  542. (when (featurep 'xemacs)
  543. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  544. key)
  545. (defun org-find-if (predicate seq)
  546. (catch 'exit
  547. (while seq
  548. (if (funcall predicate (car seq))
  549. (throw 'exit (car seq))
  550. (pop seq)))))
  551. (defun org-defkey (keymap key def)
  552. "Define a key, possibly translated, as returned by `org-key'."
  553. (define-key keymap (org-key key) def))
  554. (defcustom org-ellipsis nil
  555. "The ellipsis to use in the Org-mode outline.
  556. When nil, just use the standard three dots. When a string, use that instead,
  557. When a face, use the standard 3 dots, but with the specified face.
  558. The change affects only Org-mode (which will then use its own display table).
  559. Changing this requires executing `M-x org-mode' in a buffer to become
  560. effective."
  561. :group 'org-startup
  562. :type '(choice (const :tag "Default" nil)
  563. (face :tag "Face" :value org-warning)
  564. (string :tag "String" :value "...#")))
  565. (defvar org-display-table nil
  566. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  567. (defgroup org-keywords nil
  568. "Keywords in Org-mode."
  569. :tag "Org Keywords"
  570. :group 'org)
  571. (defcustom org-deadline-string "DEADLINE:"
  572. "String to mark deadline entries.
  573. A deadline is this string, followed by a time stamp. Should be a word,
  574. terminated by a colon. You can insert a schedule keyword and
  575. a timestamp with \\[org-deadline].
  576. Changes become only effective after restarting Emacs."
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-scheduled-string "SCHEDULED:"
  580. "String to mark scheduled TODO entries.
  581. A schedule is this string, followed by a time stamp. Should be a word,
  582. terminated by a colon. You can insert a schedule keyword and
  583. a timestamp with \\[org-schedule].
  584. Changes become only effective after restarting Emacs."
  585. :group 'org-keywords
  586. :type 'string)
  587. (defcustom org-closed-string "CLOSED:"
  588. "String used as the prefix for timestamps logging closing a TODO entry."
  589. :group 'org-keywords
  590. :type 'string)
  591. (defcustom org-clock-string "CLOCK:"
  592. "String used as prefix for timestamps clocking work hours on an item."
  593. :group 'org-keywords
  594. :type 'string)
  595. (defcustom org-comment-string "COMMENT"
  596. "Entries starting with this keyword will never be exported.
  597. An entry can be toggled between COMMENT and normal with
  598. \\[org-toggle-comment].
  599. Changes become only effective after restarting Emacs."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defcustom org-quote-string "QUOTE"
  603. "Entries starting with this keyword will be exported in fixed-width font.
  604. Quoting applies only to the text in the entry following the headline, and does
  605. not extend beyond the next headline, even if that is lower level.
  606. An entry can be toggled between QUOTE and normal with
  607. \\[org-toggle-fixed-width-section]."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defconst org-repeat-re
  611. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  612. "Regular expression for specifying repeated events.
  613. After a match, group 1 contains the repeat expression.")
  614. (defgroup org-structure nil
  615. "Options concerning the general structure of Org-mode files."
  616. :tag "Org Structure"
  617. :group 'org)
  618. (defgroup org-reveal-location nil
  619. "Options about how to make context of a location visible."
  620. :tag "Org Reveal Location"
  621. :group 'org-structure)
  622. (defconst org-context-choice
  623. '(choice
  624. (const :tag "Always" t)
  625. (const :tag "Never" nil)
  626. (repeat :greedy t :tag "Individual contexts"
  627. (cons
  628. (choice :tag "Context"
  629. (const agenda)
  630. (const org-goto)
  631. (const occur-tree)
  632. (const tags-tree)
  633. (const link-search)
  634. (const mark-goto)
  635. (const bookmark-jump)
  636. (const isearch)
  637. (const default))
  638. (boolean))))
  639. "Contexts for the reveal options.")
  640. (defcustom org-show-hierarchy-above '((default . t))
  641. "Non-nil means show full hierarchy when revealing a location.
  642. Org-mode often shows locations in an org-mode file which might have
  643. been invisible before. When this is set, the hierarchy of headings
  644. above the exposed location is shown.
  645. Turning this off for example for sparse trees makes them very compact.
  646. Instead of t, this can also be an alist specifying this option for different
  647. contexts. Valid contexts are
  648. agenda when exposing an entry from the agenda
  649. org-goto when using the command `org-goto' on key C-c C-j
  650. occur-tree when using the command `org-occur' on key C-c /
  651. tags-tree when constructing a sparse tree based on tags matches
  652. link-search when exposing search matches associated with a link
  653. mark-goto when exposing the jump goal of a mark
  654. bookmark-jump when exposing a bookmark location
  655. isearch when exiting from an incremental search
  656. default default for all contexts not set explicitly"
  657. :group 'org-reveal-location
  658. :type org-context-choice)
  659. (defcustom org-show-following-heading '((default . nil))
  660. "Non-nil means show following heading when revealing a location.
  661. Org-mode often shows locations in an org-mode file which might have
  662. been invisible before. When this is set, the heading following the
  663. match is shown.
  664. Turning this off for example for sparse trees makes them very compact,
  665. but makes it harder to edit the location of the match. In such a case,
  666. use the command \\[org-reveal] to show more context.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. See `org-show-hierarchy-above' for valid contexts."
  669. :group 'org-reveal-location
  670. :type org-context-choice)
  671. (defcustom org-show-siblings '((default . nil) (isearch t))
  672. "Non-nil means show all sibling heading when revealing a location.
  673. Org-mode often shows locations in an org-mode file which might have
  674. been invisible before. When this is set, the sibling of the current entry
  675. heading are all made visible. If `org-show-hierarchy-above' is t,
  676. the same happens on each level of the hierarchy above the current entry.
  677. By default this is on for the isearch context, off for all other contexts.
  678. Turning this off for example for sparse trees makes them very compact,
  679. but makes it harder to edit the location of the match. In such a case,
  680. use the command \\[org-reveal] to show more context.
  681. Instead of t, this can also be an alist specifying this option for different
  682. contexts. See `org-show-hierarchy-above' for valid contexts."
  683. :group 'org-reveal-location
  684. :type org-context-choice)
  685. (defcustom org-show-entry-below '((default . nil))
  686. "Non-nil means show the entry below a headline when revealing a location.
  687. Org-mode often shows locations in an org-mode file which might have
  688. been invisible before. When this is set, the text below the headline that is
  689. exposed is also shown.
  690. By default this is off for all contexts.
  691. Instead of t, this can also be an alist specifying this option for different
  692. contexts. See `org-show-hierarchy-above' for valid contexts."
  693. :group 'org-reveal-location
  694. :type org-context-choice)
  695. (defcustom org-indirect-buffer-display 'other-window
  696. "How should indirect tree buffers be displayed?
  697. This applies to indirect buffers created with the commands
  698. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  699. Valid values are:
  700. current-window Display in the current window
  701. other-window Just display in another window.
  702. dedicated-frame Create one new frame, and re-use it each time.
  703. new-frame Make a new frame each time. Note that in this case
  704. previously-made indirect buffers are kept, and you need to
  705. kill these buffers yourself."
  706. :group 'org-structure
  707. :group 'org-agenda-windows
  708. :type '(choice
  709. (const :tag "In current window" current-window)
  710. (const :tag "In current frame, other window" other-window)
  711. (const :tag "Each time a new frame" new-frame)
  712. (const :tag "One dedicated frame" dedicated-frame)))
  713. (defcustom org-use-speed-commands nil
  714. "Non-nil means activate single letter commands at beginning of a headline.
  715. This may also be a function to test for appropriate locations where speed
  716. commands should be active."
  717. :group 'org-structure
  718. :type '(choice
  719. (const :tag "Never" nil)
  720. (const :tag "At beginning of headline stars" t)
  721. (function)))
  722. (defcustom org-speed-commands-user nil
  723. "Alist of additional speed commands.
  724. This list will be checked before `org-speed-commands-default'
  725. when the variable `org-use-speed-commands' is non-nil
  726. and when the cursor is at the beginning of a headline.
  727. The car if each entry is a string with a single letter, which must
  728. be assigned to `self-insert-command' in the global map.
  729. The cdr is either a command to be called interactively, a function
  730. to be called, or a form to be evaluated.
  731. An entry that is just a list with a single string will be interpreted
  732. as a descriptive headline that will be added when listing the speed
  733. commands in the Help buffer using the `?' speed command."
  734. :group 'org-structure
  735. :type '(repeat :value ("k" . ignore)
  736. (choice :value ("k" . ignore)
  737. (list :tag "Descriptive Headline" (string :tag "Headline"))
  738. (cons :tag "Letter and Command"
  739. (string :tag "Command letter")
  740. (choice
  741. (function)
  742. (sexp))))))
  743. (defgroup org-cycle nil
  744. "Options concerning visibility cycling in Org-mode."
  745. :tag "Org Cycle"
  746. :group 'org-structure)
  747. (defcustom org-cycle-skip-children-state-if-no-children t
  748. "Non-nil means skip CHILDREN state in entries that don't have any."
  749. :group 'org-cycle
  750. :type 'boolean)
  751. (defcustom org-cycle-max-level nil
  752. "Maximum level which should still be subject to visibility cycling.
  753. Levels higher than this will, for cycling, be treated as text, not a headline.
  754. When `org-odd-levels-only' is set, a value of N in this variable actually
  755. means 2N-1 stars as the limiting headline.
  756. When nil, cycle all levels.
  757. Note that the limiting level of cycling is also influenced by
  758. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  759. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  760. than its value."
  761. :group 'org-cycle
  762. :type '(choice
  763. (const :tag "No limit" nil)
  764. (integer :tag "Maximum level")))
  765. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  766. "Names of drawers. Drawers are not opened by cycling on the headline above.
  767. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  768. this:
  769. :DRAWERNAME:
  770. .....
  771. :END:
  772. The drawer \"PROPERTIES\" is special for capturing properties through
  773. the property API.
  774. Drawers can be defined on the per-file basis with a line like:
  775. #+DRAWERS: HIDDEN STATE PROPERTIES"
  776. :group 'org-structure
  777. :group 'org-cycle
  778. :type '(repeat (string :tag "Drawer Name")))
  779. (defcustom org-hide-block-startup nil
  780. "Non-nil means entering Org-mode will fold all blocks.
  781. This can also be set in on a per-file basis with
  782. #+STARTUP: hideblocks
  783. #+STARTUP: showblocks"
  784. :group 'org-startup
  785. :group 'org-cycle
  786. :type 'boolean)
  787. (defcustom org-cycle-global-at-bob nil
  788. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  789. This makes it possible to do global cycling without having to use S-TAB or
  790. \\[universal-argument] TAB. For this special case to work, the first line \
  791. of the buffer
  792. must not be a headline - it may be empty or some other text. When used in
  793. this way, `org-cycle-hook' is disables temporarily, to make sure the
  794. cursor stays at the beginning of the buffer.
  795. When this option is nil, don't do anything special at the beginning
  796. of the buffer."
  797. :group 'org-cycle
  798. :type 'boolean)
  799. (defcustom org-cycle-level-after-item/entry-creation t
  800. "Non-nil means cycle entry level or item indentation in new empty entries.
  801. When the cursor is at the end of an empty headline, i.e with only stars
  802. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  803. and then all possible ancestor states, before returning to the original state.
  804. This makes data entry extremely fast: M-RET to create a new headline,
  805. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  806. When the cursor is at the end of an empty plain list item, one TAB will
  807. make it a subitem, two or more tabs will back up to make this an item
  808. higher up in the item hierarchy."
  809. :group 'org-cycle
  810. :type 'boolean)
  811. (defcustom org-cycle-emulate-tab t
  812. "Where should `org-cycle' emulate TAB.
  813. nil Never
  814. white Only in completely white lines
  815. whitestart Only at the beginning of lines, before the first non-white char
  816. t Everywhere except in headlines
  817. exc-hl-bol Everywhere except at the start of a headline
  818. If TAB is used in a place where it does not emulate TAB, the current subtree
  819. visibility is cycled."
  820. :group 'org-cycle
  821. :type '(choice (const :tag "Never" nil)
  822. (const :tag "Only in completely white lines" white)
  823. (const :tag "Before first char in a line" whitestart)
  824. (const :tag "Everywhere except in headlines" t)
  825. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  826. ))
  827. (defcustom org-cycle-separator-lines 2
  828. "Number of empty lines needed to keep an empty line between collapsed trees.
  829. If you leave an empty line between the end of a subtree and the following
  830. headline, this empty line is hidden when the subtree is folded.
  831. Org-mode will leave (exactly) one empty line visible if the number of
  832. empty lines is equal or larger to the number given in this variable.
  833. So the default 2 means at least 2 empty lines after the end of a subtree
  834. are needed to produce free space between a collapsed subtree and the
  835. following headline.
  836. If the number is negative, and the number of empty lines is at least -N,
  837. all empty lines are shown.
  838. Special case: when 0, never leave empty lines in collapsed view."
  839. :group 'org-cycle
  840. :type 'integer)
  841. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  842. (defcustom org-pre-cycle-hook nil
  843. "Hook that is run before visibility cycling is happening.
  844. The function(s) in this hook must accept a single argument which indicates
  845. the new state that will be set right after running this hook. The
  846. argument is a symbol. Before a global state change, it can have the values
  847. `overview', `content', or `all'. Before a local state change, it can have
  848. the values `folded', `children', or `subtree'."
  849. :group 'org-cycle
  850. :type 'hook)
  851. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  852. org-cycle-hide-drawers
  853. org-cycle-show-empty-lines
  854. org-optimize-window-after-visibility-change)
  855. "Hook that is run after `org-cycle' has changed the buffer visibility.
  856. The function(s) in this hook must accept a single argument which indicates
  857. the new state that was set by the most recent `org-cycle' command. The
  858. argument is a symbol. After a global state change, it can have the values
  859. `overview', `content', or `all'. After a local state change, it can have
  860. the values `folded', `children', or `subtree'."
  861. :group 'org-cycle
  862. :type 'hook)
  863. (defgroup org-edit-structure nil
  864. "Options concerning structure editing in Org-mode."
  865. :tag "Org Edit Structure"
  866. :group 'org-structure)
  867. (defcustom org-odd-levels-only nil
  868. "Non-nil means skip even levels and only use odd levels for the outline.
  869. This has the effect that two stars are being added/taken away in
  870. promotion/demotion commands. It also influences how levels are
  871. handled by the exporters.
  872. Changing it requires restart of `font-lock-mode' to become effective
  873. for fontification also in regions already fontified.
  874. You may also set this on a per-file basis by adding one of the following
  875. lines to the buffer:
  876. #+STARTUP: odd
  877. #+STARTUP: oddeven"
  878. :group 'org-edit-structure
  879. :group 'org-appearance
  880. :type 'boolean)
  881. (defcustom org-adapt-indentation t
  882. "Non-nil means adapt indentation to outline node level.
  883. When this variable is set, Org assumes that you write outlines by
  884. indenting text in each node to align with the headline (after the stars).
  885. The following issues are influenced by this variable:
  886. - When this is set and the *entire* text in an entry is indented, the
  887. indentation is increased by one space in a demotion command, and
  888. decreased by one in a promotion command. If any line in the entry
  889. body starts with text at column 0, indentation is not changed at all.
  890. - Property drawers and planning information is inserted indented when
  891. this variable s set. When nil, they will not be indented.
  892. - TAB indents a line relative to context. The lines below a headline
  893. will be indented when this variable is set.
  894. Note that this is all about true indentation, by adding and removing
  895. space characters. See also `org-indent.el' which does level-dependent
  896. indentation in a virtual way, i.e. at display time in Emacs."
  897. :group 'org-edit-structure
  898. :type 'boolean)
  899. (defcustom org-special-ctrl-a/e nil
  900. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  901. When t, `C-a' will bring back the cursor to the beginning of the
  902. headline text, i.e. after the stars and after a possible TODO
  903. keyword. In an item, this will be the position after bullet and
  904. check-box, if any. When the cursor is already at that position,
  905. another `C-a' will bring it to the beginning of the line.
  906. `C-e' will jump to the end of the headline, ignoring the presence
  907. of tags in the headline. A second `C-e' will then jump to the
  908. true end of the line, after any tags. This also means that, when
  909. this variable is non-nil, `C-e' also will never jump beyond the
  910. end of the heading of a folded section, i.e. not after the
  911. ellipses.
  912. When set to the symbol `reversed', the first `C-a' or `C-e' works
  913. normally, going to the true line boundary first. Only a directly
  914. following, identical keypress will bring the cursor to the
  915. special positions.
  916. This may also be a cons cell where the behavior for `C-a' and
  917. `C-e' is set separately."
  918. :group 'org-edit-structure
  919. :type '(choice
  920. (const :tag "off" nil)
  921. (const :tag "on: after stars/bullet and before tags first" t)
  922. (const :tag "reversed: true line boundary first" reversed)
  923. (cons :tag "Set C-a and C-e separately"
  924. (choice :tag "Special C-a"
  925. (const :tag "off" nil)
  926. (const :tag "on: after stars/bullet first" t)
  927. (const :tag "reversed: before stars/bullet first" reversed))
  928. (choice :tag "Special C-e"
  929. (const :tag "off" nil)
  930. (const :tag "on: before tags first" t)
  931. (const :tag "reversed: after tags first" reversed)))))
  932. (if (fboundp 'defvaralias)
  933. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  934. (defcustom org-special-ctrl-k nil
  935. "Non-nil means `C-k' will behave specially in headlines.
  936. When nil, `C-k' will call the default `kill-line' command.
  937. When t, the following will happen while the cursor is in the headline:
  938. - When the cursor is at the beginning of a headline, kill the entire
  939. line and possible the folded subtree below the line.
  940. - When in the middle of the headline text, kill the headline up to the tags.
  941. - When after the headline text, kill the tags."
  942. :group 'org-edit-structure
  943. :type 'boolean)
  944. (defcustom org-ctrl-k-protect-subtree nil
  945. "Non-nil means, do not delete a hidden subtree with C-k.
  946. When set to the symbol `error', simply throw an error when C-k is
  947. used to kill (part-of) a headline that has hidden text behind it.
  948. Any other non-nil value will result in a query to the user, if it is
  949. OK to kill that hidden subtree. When nil, kill without remorse."
  950. :group 'org-edit-structure
  951. :version "24.1"
  952. :type '(choice
  953. (const :tag "Do not protect hidden subtrees" nil)
  954. (const :tag "Protect hidden subtrees with a security query" t)
  955. (const :tag "Never kill a hidden subtree with C-k" error)))
  956. (defcustom org-catch-invisible-edits nil
  957. "Check if in invisible region before inserting or deleting a character.
  958. Valid values are:
  959. nil Do not check, so just do invisible edits.
  960. error Throw an error and do nothing.
  961. show Make point visible, and do the requested edit.
  962. show-and-error Make point visible, then throw an error and abort the edit.
  963. smart Make point visible, and do insertion/deletion if it is
  964. adjacent to visible text and the change feels predictable.
  965. Never delete a previously invisible character or add in the
  966. middle or right after an invisible region. Basically, this
  967. allows insertion and backward-delete right before ellipses.
  968. FIXME: maybe in this case we should not even show?"
  969. :group 'org-edit-structure
  970. :version "24.1"
  971. :type '(choice
  972. (const :tag "Do not check" nil)
  973. (const :tag "Throw error when trying to edit" error)
  974. (const :tag "Unhide, but do not do the edit" show-and-error)
  975. (const :tag "Show invisible part and do the edit" show)
  976. (const :tag "Be smart and do the right thing" smart)))
  977. (defcustom org-yank-folded-subtrees t
  978. "Non-nil means when yanking subtrees, fold them.
  979. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  980. it starts with a heading and all other headings in it are either children
  981. or siblings, then fold all the subtrees. However, do this only if no
  982. text after the yank would be swallowed into a folded tree by this action."
  983. :group 'org-edit-structure
  984. :type 'boolean)
  985. (defcustom org-yank-adjusted-subtrees nil
  986. "Non-nil means when yanking subtrees, adjust the level.
  987. With this setting, `org-paste-subtree' is used to insert the subtree, see
  988. this function for details."
  989. :group 'org-edit-structure
  990. :type 'boolean)
  991. (defcustom org-M-RET-may-split-line '((default . t))
  992. "Non-nil means M-RET will split the line at the cursor position.
  993. When nil, it will go to the end of the line before making a
  994. new line.
  995. You may also set this option in a different way for different
  996. contexts. Valid contexts are:
  997. headline when creating a new headline
  998. item when creating a new item
  999. table in a table field
  1000. default the value to be used for all contexts not explicitly
  1001. customized"
  1002. :group 'org-structure
  1003. :group 'org-table
  1004. :type '(choice
  1005. (const :tag "Always" t)
  1006. (const :tag "Never" nil)
  1007. (repeat :greedy t :tag "Individual contexts"
  1008. (cons
  1009. (choice :tag "Context"
  1010. (const headline)
  1011. (const item)
  1012. (const table)
  1013. (const default))
  1014. (boolean)))))
  1015. (defcustom org-insert-heading-respect-content nil
  1016. "Non-nil means insert new headings after the current subtree.
  1017. When nil, the new heading is created directly after the current line.
  1018. The commands \\[org-insert-heading-respect-content] and
  1019. \\[org-insert-todo-heading-respect-content] turn this variable on
  1020. for the duration of the command."
  1021. :group 'org-structure
  1022. :type 'boolean)
  1023. (defcustom org-blank-before-new-entry '((heading . auto)
  1024. (plain-list-item . auto))
  1025. "Should `org-insert-heading' leave a blank line before new heading/item?
  1026. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1027. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1028. which case Org will look at the surrounding headings/items and try to
  1029. make an intelligent decision whether to insert a blank line or not.
  1030. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1031. set, the setting here is ignored and no empty line is inserted, to avoid
  1032. breaking the list structure."
  1033. :group 'org-edit-structure
  1034. :type '(list
  1035. (cons (const heading)
  1036. (choice (const :tag "Never" nil)
  1037. (const :tag "Always" t)
  1038. (const :tag "Auto" auto)))
  1039. (cons (const plain-list-item)
  1040. (choice (const :tag "Never" nil)
  1041. (const :tag "Always" t)
  1042. (const :tag "Auto" auto)))))
  1043. (defcustom org-insert-heading-hook nil
  1044. "Hook being run after inserting a new heading."
  1045. :group 'org-edit-structure
  1046. :type 'hook)
  1047. (defcustom org-enable-fixed-width-editor t
  1048. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1049. This currently only means they are never auto-wrapped.
  1050. When nil, such lines will be treated like ordinary lines.
  1051. See also the QUOTE keyword."
  1052. :group 'org-edit-structure
  1053. :type 'boolean)
  1054. (defcustom org-goto-auto-isearch t
  1055. "Non-nil means typing characters in `org-goto' starts incremental search."
  1056. :group 'org-edit-structure
  1057. :type 'boolean)
  1058. (defgroup org-sparse-trees nil
  1059. "Options concerning sparse trees in Org-mode."
  1060. :tag "Org Sparse Trees"
  1061. :group 'org-structure)
  1062. (defcustom org-highlight-sparse-tree-matches t
  1063. "Non-nil means highlight all matches that define a sparse tree.
  1064. The highlights will automatically disappear the next time the buffer is
  1065. changed by an edit command."
  1066. :group 'org-sparse-trees
  1067. :type 'boolean)
  1068. (defcustom org-remove-highlights-with-change t
  1069. "Non-nil means any change to the buffer will remove temporary highlights.
  1070. Such highlights are created by `org-occur' and `org-clock-display'.
  1071. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1072. The highlights created by `org-preview-latex-fragment' always need
  1073. `C-c C-c' to be removed."
  1074. :group 'org-sparse-trees
  1075. :group 'org-time
  1076. :type 'boolean)
  1077. (defcustom org-occur-hook '(org-first-headline-recenter)
  1078. "Hook that is run after `org-occur' has constructed a sparse tree.
  1079. This can be used to recenter the window to show as much of the structure
  1080. as possible."
  1081. :group 'org-sparse-trees
  1082. :type 'hook)
  1083. (defgroup org-imenu-and-speedbar nil
  1084. "Options concerning imenu and speedbar in Org-mode."
  1085. :tag "Org Imenu and Speedbar"
  1086. :group 'org-structure)
  1087. (defcustom org-imenu-depth 2
  1088. "The maximum level for Imenu access to Org-mode headlines.
  1089. This also applied for speedbar access."
  1090. :group 'org-imenu-and-speedbar
  1091. :type 'integer)
  1092. (defgroup org-table nil
  1093. "Options concerning tables in Org-mode."
  1094. :tag "Org Table"
  1095. :group 'org)
  1096. (defcustom org-enable-table-editor 'optimized
  1097. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1098. When nil, such lines will be treated like ordinary lines.
  1099. When equal to the symbol `optimized', the table editor will be optimized to
  1100. do the following:
  1101. - Automatic overwrite mode in front of whitespace in table fields.
  1102. This makes the structure of the table stay in tact as long as the edited
  1103. field does not exceed the column width.
  1104. - Minimize the number of realigns. Normally, the table is aligned each time
  1105. TAB or RET are pressed to move to another field. With optimization this
  1106. happens only if changes to a field might have changed the column width.
  1107. Optimization requires replacing the functions `self-insert-command',
  1108. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1109. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1110. very good at guessing when a re-align will be necessary, but you can always
  1111. force one with \\[org-ctrl-c-ctrl-c].
  1112. If you would like to use the optimized version in Org-mode, but the
  1113. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1114. This variable can be used to turn on and off the table editor during a session,
  1115. but in order to toggle optimization, a restart is required.
  1116. See also the variable `org-table-auto-blank-field'."
  1117. :group 'org-table
  1118. :type '(choice
  1119. (const :tag "off" nil)
  1120. (const :tag "on" t)
  1121. (const :tag "on, optimized" optimized)))
  1122. (defcustom org-self-insert-cluster-for-undo t
  1123. "Non-nil means cluster self-insert commands for undo when possible.
  1124. If this is set, then, like in the Emacs command loop, 20 consecutive
  1125. characters will be undone together.
  1126. This is configurable, because there is some impact on typing performance."
  1127. :group 'org-table
  1128. :type 'boolean)
  1129. (defcustom org-table-tab-recognizes-table.el t
  1130. "Non-nil means TAB will automatically notice a table.el table.
  1131. When it sees such a table, it moves point into it and - if necessary -
  1132. calls `table-recognize-table'."
  1133. :group 'org-table-editing
  1134. :type 'boolean)
  1135. (defgroup org-link nil
  1136. "Options concerning links in Org-mode."
  1137. :tag "Org Link"
  1138. :group 'org)
  1139. (defvar org-link-abbrev-alist-local nil
  1140. "Buffer-local version of `org-link-abbrev-alist', which see.
  1141. The value of this is taken from the #+LINK lines.")
  1142. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1143. (defcustom org-link-abbrev-alist nil
  1144. "Alist of link abbreviations.
  1145. The car of each element is a string, to be replaced at the start of a link.
  1146. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1147. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1148. [[linkkey:tag][description]]
  1149. The 'linkkey' must be a word word, starting with a letter, followed
  1150. by letters, numbers, '-' or '_'.
  1151. If REPLACE is a string, the tag will simply be appended to create the link.
  1152. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1153. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1154. be inserted at that point (see the function `url-hexify-string').
  1155. REPLACE may also be a function that will be called with the tag as the
  1156. only argument to create the link, which should be returned as a string.
  1157. See the manual for examples."
  1158. :group 'org-link
  1159. :type '(repeat
  1160. (cons
  1161. (string :tag "Protocol")
  1162. (choice
  1163. (string :tag "Format")
  1164. (function)))))
  1165. (defcustom org-descriptive-links t
  1166. "Non-nil means Org will display descriptive links.
  1167. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1168. \"Org Website\", hiding the link itself and just displaying its
  1169. description. When set to `nil', Org will display the full links
  1170. literally.
  1171. You can interactively set the value of this variable by calling
  1172. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1173. :group 'org-link
  1174. :type 'boolean)
  1175. (defcustom org-link-file-path-type 'adaptive
  1176. "How the path name in file links should be stored.
  1177. Valid values are:
  1178. relative Relative to the current directory, i.e. the directory of the file
  1179. into which the link is being inserted.
  1180. absolute Absolute path, if possible with ~ for home directory.
  1181. noabbrev Absolute path, no abbreviation of home directory.
  1182. adaptive Use relative path for files in the current directory and sub-
  1183. directories of it. For other files, use an absolute path."
  1184. :group 'org-link
  1185. :type '(choice
  1186. (const relative)
  1187. (const absolute)
  1188. (const noabbrev)
  1189. (const adaptive)))
  1190. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1191. "Types of links that should be activated in Org-mode files.
  1192. This is a list of symbols, each leading to the activation of a certain link
  1193. type. In principle, it does not hurt to turn on most link types - there may
  1194. be a small gain when turning off unused link types. The types are:
  1195. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1196. angle Links in angular brackets that may contain whitespace like
  1197. <bbdb:Carsten Dominik>.
  1198. plain Plain links in normal text, no whitespace, like http://google.com.
  1199. radio Text that is matched by a radio target, see manual for details.
  1200. tag Tag settings in a headline (link to tag search).
  1201. date Time stamps (link to calendar).
  1202. footnote Footnote labels.
  1203. Changing this variable requires a restart of Emacs to become effective."
  1204. :group 'org-link
  1205. :type '(set :greedy t
  1206. (const :tag "Double bracket links" bracket)
  1207. (const :tag "Angular bracket links" angle)
  1208. (const :tag "Plain text links" plain)
  1209. (const :tag "Radio target matches" radio)
  1210. (const :tag "Tags" tag)
  1211. (const :tag "Timestamps" date)
  1212. (const :tag "Footnotes" footnote)))
  1213. (defcustom org-make-link-description-function nil
  1214. "Function to use to generate link descriptions from links.
  1215. If nil the link location will be used. This function must take
  1216. two parameters; the first is the link and the second the
  1217. description `org-insert-link' has generated, and should return the
  1218. description to use."
  1219. :group 'org-link
  1220. :type 'function)
  1221. (defgroup org-link-store nil
  1222. "Options concerning storing links in Org-mode."
  1223. :tag "Org Store Link"
  1224. :group 'org-link)
  1225. (defcustom org-email-link-description-format "Email %c: %.30s"
  1226. "Format of the description part of a link to an email or usenet message.
  1227. The following %-escapes will be replaced by corresponding information:
  1228. %F full \"From\" field
  1229. %f name, taken from \"From\" field, address if no name
  1230. %T full \"To\" field
  1231. %t first name in \"To\" field, address if no name
  1232. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1233. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1234. %s subject
  1235. %d date
  1236. %m message-id.
  1237. You may use normal field width specification between the % and the letter.
  1238. This is for example useful to limit the length of the subject.
  1239. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1240. :group 'org-link-store
  1241. :type 'string)
  1242. (defcustom org-from-is-user-regexp
  1243. (let (r1 r2)
  1244. (when (and user-mail-address (not (string= user-mail-address "")))
  1245. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1246. (when (and user-full-name (not (string= user-full-name "")))
  1247. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1248. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1249. "Regexp matched against the \"From:\" header of an email or usenet message.
  1250. It should match if the message is from the user him/herself."
  1251. :group 'org-link-store
  1252. :type 'regexp)
  1253. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1254. "Non-nil means storing a link to an Org file will use entry IDs.
  1255. Note that before this variable is even considered, org-id must be loaded,
  1256. so please customize `org-modules' and turn it on.
  1257. The variable can have the following values:
  1258. t Create an ID if needed to make a link to the current entry.
  1259. create-if-interactive
  1260. If `org-store-link' is called directly (interactively, as a user
  1261. command), do create an ID to support the link. But when doing the
  1262. job for remember, only use the ID if it already exists. The
  1263. purpose of this setting is to avoid proliferation of unwanted
  1264. IDs, just because you happen to be in an Org file when you
  1265. call `org-remember' that automatically and preemptively
  1266. creates a link. If you do want to get an ID link in a remember
  1267. template to an entry not having an ID, create it first by
  1268. explicitly creating a link to it, using `C-c C-l' first.
  1269. create-if-interactive-and-no-custom-id
  1270. Like create-if-interactive, but do not create an ID if there is
  1271. a CUSTOM_ID property defined in the entry. This is the default.
  1272. use-existing
  1273. Use existing ID, do not create one.
  1274. nil Never use an ID to make a link, instead link using a text search for
  1275. the headline text."
  1276. :group 'org-link-store
  1277. :type '(choice
  1278. (const :tag "Create ID to make link" t)
  1279. (const :tag "Create if storing link interactively"
  1280. create-if-interactive)
  1281. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1282. create-if-interactive-and-no-custom-id)
  1283. (const :tag "Only use existing" use-existing)
  1284. (const :tag "Do not use ID to create link" nil)))
  1285. (defcustom org-context-in-file-links t
  1286. "Non-nil means file links from `org-store-link' contain context.
  1287. A search string will be added to the file name with :: as separator and
  1288. used to find the context when the link is activated by the command
  1289. `org-open-at-point'. When this option is t, the entire active region
  1290. will be placed in the search string of the file link. If set to a
  1291. positive integer, only the first n lines of context will be stored.
  1292. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1293. negates this setting for the duration of the command."
  1294. :group 'org-link-store
  1295. :type '(choice boolean integer))
  1296. (defcustom org-keep-stored-link-after-insertion nil
  1297. "Non-nil means keep link in list for entire session.
  1298. The command `org-store-link' adds a link pointing to the current
  1299. location to an internal list. These links accumulate during a session.
  1300. The command `org-insert-link' can be used to insert links into any
  1301. Org-mode file (offering completion for all stored links). When this
  1302. option is nil, every link which has been inserted once using \\[org-insert-link]
  1303. will be removed from the list, to make completing the unused links
  1304. more efficient."
  1305. :group 'org-link-store
  1306. :type 'boolean)
  1307. (defgroup org-link-follow nil
  1308. "Options concerning following links in Org-mode."
  1309. :tag "Org Follow Link"
  1310. :group 'org-link)
  1311. (defcustom org-link-translation-function nil
  1312. "Function to translate links with different syntax to Org syntax.
  1313. This can be used to translate links created for example by the Planner
  1314. or emacs-wiki packages to Org syntax.
  1315. The function must accept two parameters, a TYPE containing the link
  1316. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1317. which is everything after the link protocol. It should return a cons
  1318. with possibly modified values of type and path.
  1319. Org contains a function for this, so if you set this variable to
  1320. `org-translate-link-from-planner', you should be able follow many
  1321. links created by planner."
  1322. :group 'org-link-follow
  1323. :type 'function)
  1324. (defcustom org-follow-link-hook nil
  1325. "Hook that is run after a link has been followed."
  1326. :group 'org-link-follow
  1327. :type 'hook)
  1328. (defcustom org-tab-follows-link nil
  1329. "Non-nil means on links TAB will follow the link.
  1330. Needs to be set before org.el is loaded.
  1331. This really should not be used, it does not make sense, and the
  1332. implementation is bad."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-return-follows-link nil
  1336. "Non-nil means on links RET will follow the link."
  1337. :group 'org-link-follow
  1338. :type 'boolean)
  1339. (defcustom org-mouse-1-follows-link
  1340. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1341. "Non-nil means mouse-1 on a link will follow the link.
  1342. A longer mouse click will still set point. Does not work on XEmacs.
  1343. Needs to be set before org.el is loaded."
  1344. :group 'org-link-follow
  1345. :type 'boolean)
  1346. (defcustom org-mark-ring-length 4
  1347. "Number of different positions to be recorded in the ring.
  1348. Changing this requires a restart of Emacs to work correctly."
  1349. :group 'org-link-follow
  1350. :type 'integer)
  1351. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1352. "Non-nil means internal links in Org files must exactly match a headline.
  1353. When nil, the link search tries to match a phrase with all words
  1354. in the search text."
  1355. :group 'org-link-follow
  1356. :version "24.1"
  1357. :type '(choice
  1358. (const :tag "Use fuzzy text search" nil)
  1359. (const :tag "Match only exact headline" t)
  1360. (const :tag "Match exact headline or query to create it"
  1361. query-to-create)))
  1362. (defcustom org-link-frame-setup
  1363. '((vm . vm-visit-folder-other-frame)
  1364. (vm-imap . vm-visit-imap-folder-other-frame)
  1365. (gnus . org-gnus-no-new-news)
  1366. (file . find-file-other-window)
  1367. (wl . wl-other-frame))
  1368. "Setup the frame configuration for following links.
  1369. When following a link with Emacs, it may often be useful to display
  1370. this link in another window or frame. This variable can be used to
  1371. set this up for the different types of links.
  1372. For VM, use any of
  1373. `vm-visit-folder'
  1374. `vm-visit-folder-other-window'
  1375. `vm-visit-folder-other-frame'
  1376. For Gnus, use any of
  1377. `gnus'
  1378. `gnus-other-frame'
  1379. `org-gnus-no-new-news'
  1380. For FILE, use any of
  1381. `find-file'
  1382. `find-file-other-window'
  1383. `find-file-other-frame'
  1384. For Wanderlust use any of
  1385. `wl'
  1386. `wl-other-frame'
  1387. For the calendar, use the variable `calendar-setup'.
  1388. For BBDB, it is currently only possible to display the matches in
  1389. another window."
  1390. :group 'org-link-follow
  1391. :type '(list
  1392. (cons (const vm)
  1393. (choice
  1394. (const vm-visit-folder)
  1395. (const vm-visit-folder-other-window)
  1396. (const vm-visit-folder-other-frame)))
  1397. (cons (const gnus)
  1398. (choice
  1399. (const gnus)
  1400. (const gnus-other-frame)
  1401. (const org-gnus-no-new-news)))
  1402. (cons (const file)
  1403. (choice
  1404. (const find-file)
  1405. (const find-file-other-window)
  1406. (const find-file-other-frame)))
  1407. (cons (const wl)
  1408. (choice
  1409. (const wl)
  1410. (const wl-other-frame)))))
  1411. (defcustom org-display-internal-link-with-indirect-buffer nil
  1412. "Non-nil means use indirect buffer to display infile links.
  1413. Activating internal links (from one location in a file to another location
  1414. in the same file) normally just jumps to the location. When the link is
  1415. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1416. is displayed in
  1417. another window. When this option is set, the other window actually displays
  1418. an indirect buffer clone of the current buffer, to avoid any visibility
  1419. changes to the current buffer."
  1420. :group 'org-link-follow
  1421. :type 'boolean)
  1422. (defcustom org-open-non-existing-files nil
  1423. "Non-nil means `org-open-file' will open non-existing files.
  1424. When nil, an error will be generated.
  1425. This variable applies only to external applications because they
  1426. might choke on non-existing files. If the link is to a file that
  1427. will be opened in Emacs, the variable is ignored."
  1428. :group 'org-link-follow
  1429. :type 'boolean)
  1430. (defcustom org-open-directory-means-index-dot-org nil
  1431. "Non-nil means a link to a directory really means to index.org.
  1432. When nil, following a directory link will run dired or open a finder/explorer
  1433. window on that directory."
  1434. :group 'org-link-follow
  1435. :type 'boolean)
  1436. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1437. "Function and arguments to call for following mailto links.
  1438. This is a list with the first element being a Lisp function, and the
  1439. remaining elements being arguments to the function. In string arguments,
  1440. %a will be replaced by the address, and %s will be replaced by the subject
  1441. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1442. :group 'org-link-follow
  1443. :type '(choice
  1444. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1445. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1446. (const :tag "message-mail" (message-mail "%a" "%s"))
  1447. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1448. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1449. "Non-nil means ask for confirmation before executing shell links.
  1450. Shell links can be dangerous: just think about a link
  1451. [[shell:rm -rf ~/*][Google Search]]
  1452. This link would show up in your Org-mode document as \"Google Search\",
  1453. but really it would remove your entire home directory.
  1454. Therefore we advise against setting this variable to nil.
  1455. Just change it to `y-or-n-p' if you want to confirm with a
  1456. single keystroke rather than having to type \"yes\"."
  1457. :group 'org-link-follow
  1458. :type '(choice
  1459. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1460. (const :tag "with y-or-n (faster)" y-or-n-p)
  1461. (const :tag "no confirmation (dangerous)" nil)))
  1462. (put 'org-confirm-shell-link-function
  1463. 'safe-local-variable
  1464. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1465. (defcustom org-confirm-shell-link-not-regexp ""
  1466. "A regexp to skip confirmation for shell links."
  1467. :group 'org-link-follow
  1468. :version "24.1"
  1469. :type 'regexp)
  1470. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1471. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1472. Elisp links can be dangerous: just think about a link
  1473. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1474. This link would show up in your Org-mode document as \"Google Search\",
  1475. but really it would remove your entire home directory.
  1476. Therefore we advise against setting this variable to nil.
  1477. Just change it to `y-or-n-p' if you want to confirm with a
  1478. single keystroke rather than having to type \"yes\"."
  1479. :group 'org-link-follow
  1480. :type '(choice
  1481. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1482. (const :tag "with y-or-n (faster)" y-or-n-p)
  1483. (const :tag "no confirmation (dangerous)" nil)))
  1484. (put 'org-confirm-shell-link-function
  1485. 'safe-local-variable
  1486. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1487. (defcustom org-confirm-elisp-link-not-regexp ""
  1488. "A regexp to skip confirmation for Elisp links."
  1489. :group 'org-link-follow
  1490. :version "24.1"
  1491. :type 'regexp)
  1492. (defconst org-file-apps-defaults-gnu
  1493. '((remote . emacs)
  1494. (system . mailcap)
  1495. (t . mailcap))
  1496. "Default file applications on a UNIX or GNU/Linux system.
  1497. See `org-file-apps'.")
  1498. (defconst org-file-apps-defaults-macosx
  1499. '((remote . emacs)
  1500. (t . "open %s")
  1501. (system . "open %s")
  1502. ("ps.gz" . "gv %s")
  1503. ("eps.gz" . "gv %s")
  1504. ("dvi" . "xdvi %s")
  1505. ("fig" . "xfig %s"))
  1506. "Default file applications on a MacOS X system.
  1507. The system \"open\" is known as a default, but we use X11 applications
  1508. for some files for which the OS does not have a good default.
  1509. See `org-file-apps'.")
  1510. (defconst org-file-apps-defaults-windowsnt
  1511. (list
  1512. '(remote . emacs)
  1513. (cons t
  1514. (list (if (featurep 'xemacs)
  1515. 'mswindows-shell-execute
  1516. 'w32-shell-execute)
  1517. "open" 'file))
  1518. (cons 'system
  1519. (list (if (featurep 'xemacs)
  1520. 'mswindows-shell-execute
  1521. 'w32-shell-execute)
  1522. "open" 'file)))
  1523. "Default file applications on a Windows NT system.
  1524. The system \"open\" is used for most files.
  1525. See `org-file-apps'.")
  1526. (defcustom org-file-apps
  1527. '(
  1528. (auto-mode . emacs)
  1529. ("\\.mm\\'" . default)
  1530. ("\\.x?html?\\'" . default)
  1531. ("\\.pdf\\'" . default)
  1532. )
  1533. "External applications for opening `file:path' items in a document.
  1534. Org-mode uses system defaults for different file types, but
  1535. you can use this variable to set the application for a given file
  1536. extension. The entries in this list are cons cells where the car identifies
  1537. files and the cdr the corresponding command. Possible values for the
  1538. file identifier are
  1539. \"string\" A string as a file identifier can be interpreted in different
  1540. ways, depending on its contents:
  1541. - Alphanumeric characters only:
  1542. Match links with this file extension.
  1543. Example: (\"pdf\" . \"evince %s\")
  1544. to open PDFs with evince.
  1545. - Regular expression: Match links where the
  1546. filename matches the regexp. If you want to
  1547. use groups here, use shy groups.
  1548. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1549. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1550. to open *.html and *.xhtml with firefox.
  1551. - Regular expression which contains (non-shy) groups:
  1552. Match links where the whole link, including \"::\", and
  1553. anything after that, matches the regexp.
  1554. In a custom command string, %1, %2, etc. are replaced with
  1555. the parts of the link that were matched by the groups.
  1556. For backwards compatibility, if a command string is given
  1557. that does not use any of the group matches, this case is
  1558. handled identically to the second one (i.e. match against
  1559. file name only).
  1560. In a custom lisp form, you can access the group matches with
  1561. (match-string n link).
  1562. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1563. to open [[file:document.pdf::5]] with evince at page 5.
  1564. `directory' Matches a directory
  1565. `remote' Matches a remote file, accessible through tramp or efs.
  1566. Remote files most likely should be visited through Emacs
  1567. because external applications cannot handle such paths.
  1568. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1569. so all files Emacs knows how to handle. Using this with
  1570. command `emacs' will open most files in Emacs. Beware that this
  1571. will also open html files inside Emacs, unless you add
  1572. (\"html\" . default) to the list as well.
  1573. t Default for files not matched by any of the other options.
  1574. `system' The system command to open files, like `open' on Windows
  1575. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1576. that will be selected if you call `C-c C-o' with a double
  1577. \\[universal-argument] \\[universal-argument] prefix.
  1578. Possible values for the command are:
  1579. `emacs' The file will be visited by the current Emacs process.
  1580. `default' Use the default application for this file type, which is the
  1581. association for t in the list, most likely in the system-specific
  1582. part.
  1583. This can be used to overrule an unwanted setting in the
  1584. system-specific variable.
  1585. `system' Use the system command for opening files, like \"open\".
  1586. This command is specified by the entry whose car is `system'.
  1587. Most likely, the system-specific version of this variable
  1588. does define this command, but you can overrule/replace it
  1589. here.
  1590. string A command to be executed by a shell; %s will be replaced
  1591. by the path to the file.
  1592. sexp A Lisp form which will be evaluated. The file path will
  1593. be available in the Lisp variable `file'.
  1594. For more examples, see the system specific constants
  1595. `org-file-apps-defaults-macosx'
  1596. `org-file-apps-defaults-windowsnt'
  1597. `org-file-apps-defaults-gnu'."
  1598. :group 'org-link-follow
  1599. :type '(repeat
  1600. (cons (choice :value ""
  1601. (string :tag "Extension")
  1602. (const :tag "System command to open files" system)
  1603. (const :tag "Default for unrecognized files" t)
  1604. (const :tag "Remote file" remote)
  1605. (const :tag "Links to a directory" directory)
  1606. (const :tag "Any files that have Emacs modes"
  1607. auto-mode))
  1608. (choice :value ""
  1609. (const :tag "Visit with Emacs" emacs)
  1610. (const :tag "Use default" default)
  1611. (const :tag "Use the system command" system)
  1612. (string :tag "Command")
  1613. (sexp :tag "Lisp form")))))
  1614. (defgroup org-refile nil
  1615. "Options concerning refiling entries in Org-mode."
  1616. :tag "Org Refile"
  1617. :group 'org)
  1618. (defcustom org-directory "~/org"
  1619. "Directory with org files.
  1620. This is just a default location to look for Org files. There is no need
  1621. at all to put your files into this directory. It is only used in the
  1622. following situations:
  1623. 1. When a remember template specifies a target file that is not an
  1624. absolute path. The path will then be interpreted relative to
  1625. `org-directory'
  1626. 2. When a remember note is filed away in an interactive way (when exiting the
  1627. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1628. with `org-directory' as the default path."
  1629. :group 'org-refile
  1630. :group 'org-remember
  1631. :type 'directory)
  1632. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1633. "Default target for storing notes.
  1634. Used as a fall back file for org-remember.el and org-capture.el, for
  1635. templates that do not specify a target file."
  1636. :group 'org-refile
  1637. :group 'org-remember
  1638. :type '(choice
  1639. (const :tag "Default from remember-data-file" nil)
  1640. file))
  1641. (defcustom org-goto-interface 'outline
  1642. "The default interface to be used for `org-goto'.
  1643. Allowed values are:
  1644. outline The interface shows an outline of the relevant file
  1645. and the correct heading is found by moving through
  1646. the outline or by searching with incremental search.
  1647. outline-path-completion Headlines in the current buffer are offered via
  1648. completion. This is the interface also used by
  1649. the refile command."
  1650. :group 'org-refile
  1651. :type '(choice
  1652. (const :tag "Outline" outline)
  1653. (const :tag "Outline-path-completion" outline-path-completion)))
  1654. (defcustom org-goto-max-level 5
  1655. "Maximum target level when running `org-goto' with refile interface."
  1656. :group 'org-refile
  1657. :type 'integer)
  1658. (defcustom org-reverse-note-order nil
  1659. "Non-nil means store new notes at the beginning of a file or entry.
  1660. When nil, new notes will be filed to the end of a file or entry.
  1661. This can also be a list with cons cells of regular expressions that
  1662. are matched against file names, and values."
  1663. :group 'org-remember
  1664. :group 'org-refile
  1665. :type '(choice
  1666. (const :tag "Reverse always" t)
  1667. (const :tag "Reverse never" nil)
  1668. (repeat :tag "By file name regexp"
  1669. (cons regexp boolean))))
  1670. (defcustom org-log-refile nil
  1671. "Information to record when a task is refiled.
  1672. Possible values are:
  1673. nil Don't add anything
  1674. time Add a time stamp to the task
  1675. note Prompt for a note and add it with template `org-log-note-headings'
  1676. This option can also be set with on a per-file-basis with
  1677. #+STARTUP: nologrefile
  1678. #+STARTUP: logrefile
  1679. #+STARTUP: lognoterefile
  1680. You can have local logging settings for a subtree by setting the LOGGING
  1681. property to one or more of these keywords.
  1682. When bulk-refiling from the agenda, the value `note' is forbidden and
  1683. will temporarily be changed to `time'."
  1684. :group 'org-refile
  1685. :group 'org-progress
  1686. :version "24.1"
  1687. :type '(choice
  1688. (const :tag "No logging" nil)
  1689. (const :tag "Record timestamp" time)
  1690. (const :tag "Record timestamp with note." note)))
  1691. (defcustom org-refile-targets nil
  1692. "Targets for refiling entries with \\[org-refile].
  1693. This is a list of cons cells. Each cell contains:
  1694. - a specification of the files to be considered, either a list of files,
  1695. or a symbol whose function or variable value will be used to retrieve
  1696. a file name or a list of file names. If you use `org-agenda-files' for
  1697. that, all agenda files will be scanned for targets. Nil means consider
  1698. headings in the current buffer.
  1699. - A specification of how to find candidate refile targets. This may be
  1700. any of:
  1701. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1702. This tag has to be present in all target headlines, inheritance will
  1703. not be considered.
  1704. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1705. todo keyword.
  1706. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1707. headlines that are refiling targets.
  1708. - a cons cell (:level . N). Any headline of level N is considered a target.
  1709. Note that, when `org-odd-levels-only' is set, level corresponds to
  1710. order in hierarchy, not to the number of stars.
  1711. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1712. Note that, when `org-odd-levels-only' is set, level corresponds to
  1713. order in hierarchy, not to the number of stars.
  1714. Each element of this list generates a set of possible targets.
  1715. The union of these sets is presented (with completion) to
  1716. the user by `org-refile'.
  1717. You can set the variable `org-refile-target-verify-function' to a function
  1718. to verify each headline found by the simple criteria above.
  1719. When this variable is nil, all top-level headlines in the current buffer
  1720. are used, equivalent to the value `((nil . (:level . 1))'."
  1721. :group 'org-refile
  1722. :type '(repeat
  1723. (cons
  1724. (choice :value org-agenda-files
  1725. (const :tag "All agenda files" org-agenda-files)
  1726. (const :tag "Current buffer" nil)
  1727. (function) (variable) (file))
  1728. (choice :tag "Identify target headline by"
  1729. (cons :tag "Specific tag" (const :value :tag) (string))
  1730. (cons :tag "TODO keyword" (const :value :todo) (string))
  1731. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1732. (cons :tag "Level number" (const :value :level) (integer))
  1733. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1734. (defcustom org-refile-target-verify-function nil
  1735. "Function to verify if the headline at point should be a refile target.
  1736. The function will be called without arguments, with point at the
  1737. beginning of the headline. It should return t and leave point
  1738. where it is if the headline is a valid target for refiling.
  1739. If the target should not be selected, the function must return nil.
  1740. In addition to this, it may move point to a place from where the search
  1741. should be continued. For example, the function may decide that the entire
  1742. subtree of the current entry should be excluded and move point to the end
  1743. of the subtree."
  1744. :group 'org-refile
  1745. :type 'function)
  1746. (defcustom org-refile-use-cache nil
  1747. "Non-nil means cache refile targets to speed up the process.
  1748. The cache for a particular file will be updated automatically when
  1749. the buffer has been killed, or when any of the marker used for flagging
  1750. refile targets no longer points at a live buffer.
  1751. If you have added new entries to a buffer that might themselves be targets,
  1752. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1753. find that easier, `C-u C-u C-u C-c C-w'."
  1754. :group 'org-refile
  1755. :version "24.1"
  1756. :type 'boolean)
  1757. (defcustom org-refile-use-outline-path nil
  1758. "Non-nil means provide refile targets as paths.
  1759. So a level 3 headline will be available as level1/level2/level3.
  1760. When the value is `file', also include the file name (without directory)
  1761. into the path. In this case, you can also stop the completion after
  1762. the file name, to get entries inserted as top level in the file.
  1763. When `full-file-path', include the full file path."
  1764. :group 'org-refile
  1765. :type '(choice
  1766. (const :tag "Not" nil)
  1767. (const :tag "Yes" t)
  1768. (const :tag "Start with file name" file)
  1769. (const :tag "Start with full file path" full-file-path)))
  1770. (defcustom org-outline-path-complete-in-steps t
  1771. "Non-nil means complete the outline path in hierarchical steps.
  1772. When Org-mode uses the refile interface to select an outline path
  1773. \(see variable `org-refile-use-outline-path'), the completion of
  1774. the path can be done is a single go, or if can be done in steps down
  1775. the headline hierarchy. Going in steps is probably the best if you
  1776. do not use a special completion package like `ido' or `icicles'.
  1777. However, when using these packages, going in one step can be very
  1778. fast, while still showing the whole path to the entry."
  1779. :group 'org-refile
  1780. :type 'boolean)
  1781. (defcustom org-refile-allow-creating-parent-nodes nil
  1782. "Non-nil means allow to create new nodes as refile targets.
  1783. New nodes are then created by adding \"/new node name\" to the completion
  1784. of an existing node. When the value of this variable is `confirm',
  1785. new node creation must be confirmed by the user (recommended)
  1786. When nil, the completion must match an existing entry.
  1787. Note that, if the new heading is not seen by the criteria
  1788. listed in `org-refile-targets', multiple instances of the same
  1789. heading would be created by trying again to file under the new
  1790. heading."
  1791. :group 'org-refile
  1792. :type '(choice
  1793. (const :tag "Never" nil)
  1794. (const :tag "Always" t)
  1795. (const :tag "Prompt for confirmation" confirm)))
  1796. (defcustom org-refile-active-region-within-subtree nil
  1797. "Non-nil means also refile active region within a subtree.
  1798. By default `org-refile' doesn't allow refiling regions if they
  1799. don't contain a set of subtrees, but it might be convenient to
  1800. do so sometimes: in that case, the first line of the region is
  1801. converted to a headline before refiling."
  1802. :group 'org-refile
  1803. :version "24.1"
  1804. :type 'boolean)
  1805. (defgroup org-todo nil
  1806. "Options concerning TODO items in Org-mode."
  1807. :tag "Org TODO"
  1808. :group 'org)
  1809. (defgroup org-progress nil
  1810. "Options concerning Progress logging in Org-mode."
  1811. :tag "Org Progress"
  1812. :group 'org-time)
  1813. (defvar org-todo-interpretation-widgets
  1814. '((:tag "Sequence (cycling hits every state)" sequence)
  1815. (:tag "Type (cycling directly to DONE)" type))
  1816. "The available interpretation symbols for customizing `org-todo-keywords'.
  1817. Interested libraries should add to this list.")
  1818. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1819. "List of TODO entry keyword sequences and their interpretation.
  1820. \\<org-mode-map>This is a list of sequences.
  1821. Each sequence starts with a symbol, either `sequence' or `type',
  1822. indicating if the keywords should be interpreted as a sequence of
  1823. action steps, or as different types of TODO items. The first
  1824. keywords are states requiring action - these states will select a headline
  1825. for inclusion into the global TODO list Org-mode produces. If one of
  1826. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1827. signify that no further action is necessary. If \"|\" is not found,
  1828. the last keyword is treated as the only DONE state of the sequence.
  1829. The command \\[org-todo] cycles an entry through these states, and one
  1830. additional state where no keyword is present. For details about this
  1831. cycling, see the manual.
  1832. TODO keywords and interpretation can also be set on a per-file basis with
  1833. the special #+SEQ_TODO and #+TYP_TODO lines.
  1834. Each keyword can optionally specify a character for fast state selection
  1835. \(in combination with the variable `org-use-fast-todo-selection')
  1836. and specifiers for state change logging, using the same syntax
  1837. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1838. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1839. indicates to record a time stamp each time this state is selected.
  1840. Each keyword may also specify if a timestamp or a note should be
  1841. recorded when entering or leaving the state, by adding additional
  1842. characters in the parenthesis after the keyword. This looks like this:
  1843. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1844. record only the time of the state change. With X and Y being either
  1845. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1846. Y when leaving the state if and only if the *target* state does not
  1847. define X. You may omit any of the fast-selection key or X or /Y,
  1848. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1849. For backward compatibility, this variable may also be just a list
  1850. of keywords - in this case the interpretation (sequence or type) will be
  1851. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1852. :group 'org-todo
  1853. :group 'org-keywords
  1854. :type '(choice
  1855. (repeat :tag "Old syntax, just keywords"
  1856. (string :tag "Keyword"))
  1857. (repeat :tag "New syntax"
  1858. (cons
  1859. (choice
  1860. :tag "Interpretation"
  1861. ;;Quick and dirty way to see
  1862. ;;`org-todo-interpretations'. This takes the
  1863. ;;place of item arguments
  1864. :convert-widget
  1865. (lambda (widget)
  1866. (widget-put widget
  1867. :args (mapcar
  1868. #'(lambda (x)
  1869. (widget-convert
  1870. (cons 'const x)))
  1871. org-todo-interpretation-widgets))
  1872. widget))
  1873. (repeat
  1874. (string :tag "Keyword"))))))
  1875. (defvar org-todo-keywords-1 nil
  1876. "All TODO and DONE keywords active in a buffer.")
  1877. (make-variable-buffer-local 'org-todo-keywords-1)
  1878. (defvar org-todo-keywords-for-agenda nil)
  1879. (defvar org-done-keywords-for-agenda nil)
  1880. (defvar org-drawers-for-agenda nil)
  1881. (defvar org-todo-keyword-alist-for-agenda nil)
  1882. (defvar org-tag-alist-for-agenda nil)
  1883. (defvar org-agenda-contributing-files nil)
  1884. (defvar org-not-done-keywords nil)
  1885. (make-variable-buffer-local 'org-not-done-keywords)
  1886. (defvar org-done-keywords nil)
  1887. (make-variable-buffer-local 'org-done-keywords)
  1888. (defvar org-todo-heads nil)
  1889. (make-variable-buffer-local 'org-todo-heads)
  1890. (defvar org-todo-sets nil)
  1891. (make-variable-buffer-local 'org-todo-sets)
  1892. (defvar org-todo-log-states nil)
  1893. (make-variable-buffer-local 'org-todo-log-states)
  1894. (defvar org-todo-kwd-alist nil)
  1895. (make-variable-buffer-local 'org-todo-kwd-alist)
  1896. (defvar org-todo-key-alist nil)
  1897. (make-variable-buffer-local 'org-todo-key-alist)
  1898. (defvar org-todo-key-trigger nil)
  1899. (make-variable-buffer-local 'org-todo-key-trigger)
  1900. (defcustom org-todo-interpretation 'sequence
  1901. "Controls how TODO keywords are interpreted.
  1902. This variable is in principle obsolete and is only used for
  1903. backward compatibility, if the interpretation of todo keywords is
  1904. not given already in `org-todo-keywords'. See that variable for
  1905. more information."
  1906. :group 'org-todo
  1907. :group 'org-keywords
  1908. :type '(choice (const sequence)
  1909. (const type)))
  1910. (defcustom org-use-fast-todo-selection t
  1911. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1912. This variable describes if and under what circumstances the cycling
  1913. mechanism for TODO keywords will be replaced by a single-key, direct
  1914. selection scheme.
  1915. When nil, fast selection is never used.
  1916. When the symbol `prefix', it will be used when `org-todo' is called with
  1917. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1918. in an agenda buffer.
  1919. When t, fast selection is used by default. In this case, the prefix
  1920. argument forces cycling instead.
  1921. In all cases, the special interface is only used if access keys have actually
  1922. been assigned by the user, i.e. if keywords in the configuration are followed
  1923. by a letter in parenthesis, like TODO(t)."
  1924. :group 'org-todo
  1925. :type '(choice
  1926. (const :tag "Never" nil)
  1927. (const :tag "By default" t)
  1928. (const :tag "Only with C-u C-c C-t" prefix)))
  1929. (defcustom org-provide-todo-statistics t
  1930. "Non-nil means update todo statistics after insert and toggle.
  1931. ALL-HEADLINES means update todo statistics by including headlines
  1932. with no TODO keyword as well, counting them as not done.
  1933. A list of TODO keywords means the same, but skip keywords that are
  1934. not in this list.
  1935. When this is set, todo statistics is updated in the parent of the
  1936. current entry each time a todo state is changed."
  1937. :group 'org-todo
  1938. :type '(choice
  1939. (const :tag "Yes, only for TODO entries" t)
  1940. (const :tag "Yes, including all entries" 'all-headlines)
  1941. (repeat :tag "Yes, for TODOs in this list"
  1942. (string :tag "TODO keyword"))
  1943. (other :tag "No TODO statistics" nil)))
  1944. (defcustom org-hierarchical-todo-statistics t
  1945. "Non-nil means TODO statistics covers just direct children.
  1946. When nil, all entries in the subtree are considered.
  1947. This has only an effect if `org-provide-todo-statistics' is set.
  1948. To set this to nil for only a single subtree, use a COOKIE_DATA
  1949. property and include the word \"recursive\" into the value."
  1950. :group 'org-todo
  1951. :type 'boolean)
  1952. (defcustom org-after-todo-state-change-hook nil
  1953. "Hook which is run after the state of a TODO item was changed.
  1954. The new state (a string with a TODO keyword, or nil) is available in the
  1955. Lisp variable `state'."
  1956. :group 'org-todo
  1957. :type 'hook)
  1958. (defvar org-blocker-hook nil
  1959. "Hook for functions that are allowed to block a state change.
  1960. Each function gets as its single argument a property list, see
  1961. `org-trigger-hook' for more information about this list.
  1962. If any of the functions in this hook returns nil, the state change
  1963. is blocked.")
  1964. (defvar org-trigger-hook nil
  1965. "Hook for functions that are triggered by a state change.
  1966. Each function gets as its single argument a property list with at least
  1967. the following elements:
  1968. (:type type-of-change :position pos-at-entry-start
  1969. :from old-state :to new-state)
  1970. Depending on the type, more properties may be present.
  1971. This mechanism is currently implemented for:
  1972. TODO state changes
  1973. ------------------
  1974. :type todo-state-change
  1975. :from previous state (keyword as a string), or nil, or a symbol
  1976. 'todo' or 'done', to indicate the general type of state.
  1977. :to new state, like in :from")
  1978. (defcustom org-enforce-todo-dependencies nil
  1979. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1980. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1981. be blocked if any prior sibling is not yet done.
  1982. Finally, if the parent is blocked because of ordered siblings of its own,
  1983. the child will also be blocked.
  1984. This variable needs to be set before org.el is loaded, and you need to
  1985. restart Emacs after a change to make the change effective. The only way
  1986. to change is while Emacs is running is through the customize interface."
  1987. :set (lambda (var val)
  1988. (set var val)
  1989. (if val
  1990. (add-hook 'org-blocker-hook
  1991. 'org-block-todo-from-children-or-siblings-or-parent)
  1992. (remove-hook 'org-blocker-hook
  1993. 'org-block-todo-from-children-or-siblings-or-parent)))
  1994. :group 'org-todo
  1995. :type 'boolean)
  1996. (defcustom org-enforce-todo-checkbox-dependencies nil
  1997. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1998. When this is nil, checkboxes have no influence on switching TODO states.
  1999. When non-nil, you first need to check off all check boxes before the TODO
  2000. entry can be switched to DONE.
  2001. This variable needs to be set before org.el is loaded, and you need to
  2002. restart Emacs after a change to make the change effective. The only way
  2003. to change is while Emacs is running is through the customize interface."
  2004. :set (lambda (var val)
  2005. (set var val)
  2006. (if val
  2007. (add-hook 'org-blocker-hook
  2008. 'org-block-todo-from-checkboxes)
  2009. (remove-hook 'org-blocker-hook
  2010. 'org-block-todo-from-checkboxes)))
  2011. :group 'org-todo
  2012. :type 'boolean)
  2013. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2014. "Non-nil means inserting a TODO heading is treated as state change.
  2015. So when the command \\[org-insert-todo-heading] is used, state change
  2016. logging will apply if appropriate. When nil, the new TODO item will
  2017. be inserted directly, and no logging will take place."
  2018. :group 'org-todo
  2019. :type 'boolean)
  2020. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2021. "Non-nil means switching TODO states with S-cursor counts as state change.
  2022. This is the default behavior. However, setting this to nil allows a
  2023. convenient way to select a TODO state and bypass any logging associated
  2024. with that."
  2025. :group 'org-todo
  2026. :type 'boolean)
  2027. (defcustom org-todo-state-tags-triggers nil
  2028. "Tag changes that should be triggered by TODO state changes.
  2029. This is a list. Each entry is
  2030. (state-change (tag . flag) .......)
  2031. State-change can be a string with a state, and empty string to indicate the
  2032. state that has no TODO keyword, or it can be one of the symbols `todo'
  2033. or `done', meaning any not-done or done state, respectively."
  2034. :group 'org-todo
  2035. :group 'org-tags
  2036. :type '(repeat
  2037. (cons (choice :tag "When changing to"
  2038. (const :tag "Not-done state" todo)
  2039. (const :tag "Done state" done)
  2040. (string :tag "State"))
  2041. (repeat
  2042. (cons :tag "Tag action"
  2043. (string :tag "Tag")
  2044. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2045. (defcustom org-log-done nil
  2046. "Information to record when a task moves to the DONE state.
  2047. Possible values are:
  2048. nil Don't add anything, just change the keyword
  2049. time Add a time stamp to the task
  2050. note Prompt for a note and add it with template `org-log-note-headings'
  2051. This option can also be set with on a per-file-basis with
  2052. #+STARTUP: nologdone
  2053. #+STARTUP: logdone
  2054. #+STARTUP: lognotedone
  2055. You can have local logging settings for a subtree by setting the LOGGING
  2056. property to one or more of these keywords."
  2057. :group 'org-todo
  2058. :group 'org-progress
  2059. :type '(choice
  2060. (const :tag "No logging" nil)
  2061. (const :tag "Record CLOSED timestamp" time)
  2062. (const :tag "Record CLOSED timestamp with note." note)))
  2063. ;; Normalize old uses of org-log-done.
  2064. (cond
  2065. ((eq org-log-done t) (setq org-log-done 'time))
  2066. ((and (listp org-log-done) (memq 'done org-log-done))
  2067. (setq org-log-done 'note)))
  2068. (defcustom org-log-reschedule nil
  2069. "Information to record when the scheduling date of a tasks is modified.
  2070. Possible values are:
  2071. nil Don't add anything, just change the date
  2072. time Add a time stamp to the task
  2073. note Prompt for a note and add it with template `org-log-note-headings'
  2074. This option can also be set with on a per-file-basis with
  2075. #+STARTUP: nologreschedule
  2076. #+STARTUP: logreschedule
  2077. #+STARTUP: lognotereschedule"
  2078. :group 'org-todo
  2079. :group 'org-progress
  2080. :type '(choice
  2081. (const :tag "No logging" nil)
  2082. (const :tag "Record timestamp" time)
  2083. (const :tag "Record timestamp with note." note)))
  2084. (defcustom org-log-redeadline nil
  2085. "Information to record when the deadline date of a tasks is modified.
  2086. Possible values are:
  2087. nil Don't add anything, just change the date
  2088. time Add a time stamp to the task
  2089. note Prompt for a note and add it with template `org-log-note-headings'
  2090. This option can also be set with on a per-file-basis with
  2091. #+STARTUP: nologredeadline
  2092. #+STARTUP: logredeadline
  2093. #+STARTUP: lognoteredeadline
  2094. You can have local logging settings for a subtree by setting the LOGGING
  2095. property to one or more of these keywords."
  2096. :group 'org-todo
  2097. :group 'org-progress
  2098. :type '(choice
  2099. (const :tag "No logging" nil)
  2100. (const :tag "Record timestamp" time)
  2101. (const :tag "Record timestamp with note." note)))
  2102. (defcustom org-log-note-clock-out nil
  2103. "Non-nil means record a note when clocking out of an item.
  2104. This can also be configured on a per-file basis by adding one of
  2105. the following lines anywhere in the buffer:
  2106. #+STARTUP: lognoteclock-out
  2107. #+STARTUP: nolognoteclock-out"
  2108. :group 'org-todo
  2109. :group 'org-progress
  2110. :type 'boolean)
  2111. (defcustom org-log-done-with-time t
  2112. "Non-nil means the CLOSED time stamp will contain date and time.
  2113. When nil, only the date will be recorded."
  2114. :group 'org-progress
  2115. :type 'boolean)
  2116. (defcustom org-log-note-headings
  2117. '((done . "CLOSING NOTE %t")
  2118. (state . "State %-12s from %-12S %t")
  2119. (note . "Note taken on %t")
  2120. (reschedule . "Rescheduled from %S on %t")
  2121. (delschedule . "Not scheduled, was %S on %t")
  2122. (redeadline . "New deadline from %S on %t")
  2123. (deldeadline . "Removed deadline, was %S on %t")
  2124. (refile . "Refiled on %t")
  2125. (clock-out . ""))
  2126. "Headings for notes added to entries.
  2127. The value is an alist, with the car being a symbol indicating the note
  2128. context, and the cdr is the heading to be used. The heading may also be the
  2129. empty string.
  2130. %t in the heading will be replaced by a time stamp.
  2131. %T will be an active time stamp instead the default inactive one
  2132. %d will be replaced by a short-format time stamp.
  2133. %D will be replaced by an active short-format time stamp.
  2134. %s will be replaced by the new TODO state, in double quotes.
  2135. %S will be replaced by the old TODO state, in double quotes.
  2136. %u will be replaced by the user name.
  2137. %U will be replaced by the full user name.
  2138. In fact, it is not a good idea to change the `state' entry, because
  2139. agenda log mode depends on the format of these entries."
  2140. :group 'org-todo
  2141. :group 'org-progress
  2142. :type '(list :greedy t
  2143. (cons (const :tag "Heading when closing an item" done) string)
  2144. (cons (const :tag
  2145. "Heading when changing todo state (todo sequence only)"
  2146. state) string)
  2147. (cons (const :tag "Heading when just taking a note" note) string)
  2148. (cons (const :tag "Heading when clocking out" clock-out) string)
  2149. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2150. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2151. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2152. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2153. (cons (const :tag "Heading when refiling" refile) string)))
  2154. (unless (assq 'note org-log-note-headings)
  2155. (push '(note . "%t") org-log-note-headings))
  2156. (defcustom org-log-into-drawer nil
  2157. "Non-nil means insert state change notes and time stamps into a drawer.
  2158. When nil, state changes notes will be inserted after the headline and
  2159. any scheduling and clock lines, but not inside a drawer.
  2160. The value of this variable should be the name of the drawer to use.
  2161. LOGBOOK is proposed as the default drawer for this purpose, you can
  2162. also set this to a string to define the drawer of your choice.
  2163. A value of t is also allowed, representing \"LOGBOOK\".
  2164. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2165. will be ignored.
  2166. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2167. a subtree."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type '(choice
  2171. (const :tag "Not into a drawer" nil)
  2172. (const :tag "LOGBOOK" t)
  2173. (string :tag "Other")))
  2174. (if (fboundp 'defvaralias)
  2175. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2176. (defun org-log-into-drawer ()
  2177. "Return the value of `org-log-into-drawer', but let properties overrule.
  2178. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2179. used instead of the default value."
  2180. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2181. (cond
  2182. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2183. ((equal p "t") "LOGBOOK")
  2184. (t p))))
  2185. (defcustom org-log-state-notes-insert-after-drawers nil
  2186. "Non-nil means insert state change notes after any drawers in entry.
  2187. Only the drawers that *immediately* follow the headline and the
  2188. deadline/scheduled line are skipped.
  2189. When nil, insert notes right after the heading and perhaps the line
  2190. with deadline/scheduling if present.
  2191. This variable will have no effect if `org-log-into-drawer' is
  2192. set."
  2193. :group 'org-todo
  2194. :group 'org-progress
  2195. :type 'boolean)
  2196. (defcustom org-log-states-order-reversed t
  2197. "Non-nil means the latest state note will be directly after heading.
  2198. When nil, the state change notes will be ordered according to time."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type 'boolean)
  2202. (defcustom org-todo-repeat-to-state nil
  2203. "The TODO state to which a repeater should return the repeating task.
  2204. By default this is the first task in a TODO sequence, or the previous state
  2205. in a TODO_TYP set. But you can specify another task here.
  2206. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2207. :group 'org-todo
  2208. :version "24.1"
  2209. :type '(choice (const :tag "Head of sequence" nil)
  2210. (string :tag "Specific state")))
  2211. (defcustom org-log-repeat 'time
  2212. "Non-nil means record moving through the DONE state when triggering repeat.
  2213. An auto-repeating task is immediately switched back to TODO when
  2214. marked DONE. If you are not logging state changes (by adding \"@\"
  2215. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2216. record a closing note, there will be no record of the task moving
  2217. through DONE. This variable forces taking a note anyway.
  2218. nil Don't force a record
  2219. time Record a time stamp
  2220. note Record a note
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: logrepeat
  2223. #+STARTUP: lognoterepeat
  2224. #+STARTUP: nologrepeat
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "Don't force a record" nil)
  2231. (const :tag "Force recording the DONE state" time)
  2232. (const :tag "Force recording a note with the DONE state" note)))
  2233. (defgroup org-priorities nil
  2234. "Priorities in Org-mode."
  2235. :tag "Org Priorities"
  2236. :group 'org-todo)
  2237. (defcustom org-enable-priority-commands t
  2238. "Non-nil means priority commands are active.
  2239. When nil, these commands will be disabled, so that you never accidentally
  2240. set a priority."
  2241. :group 'org-priorities
  2242. :type 'boolean)
  2243. (defcustom org-highest-priority ?A
  2244. "The highest priority of TODO items. A character like ?A, ?B etc.
  2245. Must have a smaller ASCII number than `org-lowest-priority'."
  2246. :group 'org-priorities
  2247. :type 'character)
  2248. (defcustom org-lowest-priority ?C
  2249. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2250. Must have a larger ASCII number than `org-highest-priority'."
  2251. :group 'org-priorities
  2252. :type 'character)
  2253. (defcustom org-default-priority ?B
  2254. "The default priority of TODO items.
  2255. This is the priority an item gets if no explicit priority is given.
  2256. When starting to cycle on an empty priority the first step in the cycle
  2257. depends on `org-priority-start-cycle-with-default'. The resulting first
  2258. step priority must not exceed the range from `org-highest-priority' to
  2259. `org-lowest-priority' which means that `org-default-priority' has to be
  2260. in this range exclusive or inclusive the range boundaries. Else the
  2261. first step refuses to set the default and the second will fall back
  2262. to (depending on the command used) the highest or lowest priority."
  2263. :group 'org-priorities
  2264. :type 'character)
  2265. (defcustom org-priority-start-cycle-with-default t
  2266. "Non-nil means start with default priority when starting to cycle.
  2267. When this is nil, the first step in the cycle will be (depending on the
  2268. command used) one higher or lower than the default priority.
  2269. See also `org-default-priority'."
  2270. :group 'org-priorities
  2271. :type 'boolean)
  2272. (defcustom org-get-priority-function nil
  2273. "Function to extract the priority from a string.
  2274. The string is normally the headline. If this is nil Org computes the
  2275. priority from the priority cookie like [#A] in the headline. It returns
  2276. an integer, increasing by 1000 for each priority level.
  2277. The user can set a different function here, which should take a string
  2278. as an argument and return the numeric priority."
  2279. :group 'org-priorities
  2280. :version "24.1"
  2281. :type 'function)
  2282. (defgroup org-time nil
  2283. "Options concerning time stamps and deadlines in Org-mode."
  2284. :tag "Org Time"
  2285. :group 'org)
  2286. (defcustom org-insert-labeled-timestamps-at-point nil
  2287. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2288. When nil, these labeled time stamps are forces into the second line of an
  2289. entry, just after the headline. When scheduling from the global TODO list,
  2290. the time stamp will always be forced into the second line."
  2291. :group 'org-time
  2292. :type 'boolean)
  2293. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2294. "Formats for `format-time-string' which are used for time stamps.
  2295. It is not recommended to change this constant.")
  2296. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2297. "Number of minutes to round time stamps to.
  2298. These are two values, the first applies when first creating a time stamp.
  2299. The second applies when changing it with the commands `S-up' and `S-down'.
  2300. When changing the time stamp, this means that it will change in steps
  2301. of N minutes, as given by the second value.
  2302. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2303. numbers should be factors of 60, so for example 5, 10, 15.
  2304. When this is larger than 1, you can still force an exact time stamp by using
  2305. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2306. and by using a prefix arg to `S-up/down' to specify the exact number
  2307. of minutes to shift."
  2308. :group 'org-time
  2309. :get #'(lambda (var) ; Make sure both elements are there
  2310. (if (integerp (default-value var))
  2311. (list (default-value var) 5)
  2312. (default-value var)))
  2313. :type '(list
  2314. (integer :tag "when inserting times")
  2315. (integer :tag "when modifying times")))
  2316. ;; Normalize old customizations of this variable.
  2317. (when (integerp org-time-stamp-rounding-minutes)
  2318. (setq org-time-stamp-rounding-minutes
  2319. (list org-time-stamp-rounding-minutes
  2320. org-time-stamp-rounding-minutes)))
  2321. (defcustom org-display-custom-times nil
  2322. "Non-nil means overlay custom formats over all time stamps.
  2323. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2324. To turn this on on a per-file basis, insert anywhere in the file:
  2325. #+STARTUP: customtime"
  2326. :group 'org-time
  2327. :set 'set-default
  2328. :type 'sexp)
  2329. (make-variable-buffer-local 'org-display-custom-times)
  2330. (defcustom org-time-stamp-custom-formats
  2331. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2332. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2333. These are overlaid over the default ISO format if the variable
  2334. `org-display-custom-times' is set. Time like %H:%M should be at the
  2335. end of the second format. The custom formats are also honored by export
  2336. commands, if custom time display is turned on at the time of export."
  2337. :group 'org-time
  2338. :type 'sexp)
  2339. (defun org-time-stamp-format (&optional long inactive)
  2340. "Get the right format for a time string."
  2341. (let ((f (if long (cdr org-time-stamp-formats)
  2342. (car org-time-stamp-formats))))
  2343. (if inactive
  2344. (concat "[" (substring f 1 -1) "]")
  2345. f)))
  2346. (defcustom org-time-clocksum-format "%d:%02d"
  2347. "The format string used when creating CLOCKSUM lines.
  2348. This is also used when org-mode generates a time duration."
  2349. :group 'org-time
  2350. :type 'string)
  2351. (defcustom org-time-clocksum-use-fractional nil
  2352. "If non-nil, \\[org-clock-display] uses fractional times.
  2353. org-mode generates a time duration."
  2354. :group 'org-time
  2355. :type 'boolean)
  2356. (defcustom org-time-clocksum-fractional-format "%.2f"
  2357. "The format string used when creating CLOCKSUM lines, or when
  2358. org-mode generates a time duration."
  2359. :group 'org-time
  2360. :type 'string)
  2361. (defcustom org-deadline-warning-days 14
  2362. "No. of days before expiration during which a deadline becomes active.
  2363. This variable governs the display in sparse trees and in the agenda.
  2364. When 0 or negative, it means use this number (the absolute value of it)
  2365. even if a deadline has a different individual lead time specified.
  2366. Custom commands can set this variable in the options section."
  2367. :group 'org-time
  2368. :group 'org-agenda-daily/weekly
  2369. :type 'integer)
  2370. (defcustom org-read-date-prefer-future t
  2371. "Non-nil means assume future for incomplete date input from user.
  2372. This affects the following situations:
  2373. 1. The user gives a month but not a year.
  2374. For example, if it is April and you enter \"feb 2\", this will be read
  2375. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2376. 2. The user gives a day, but no month.
  2377. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2378. read this as the third of *next* month. However, if you enter \"17\",
  2379. it will be considered as *this* month.
  2380. If you set this variable to the symbol `time', then also the following
  2381. will work:
  2382. 3. If the user gives a time, but no day. If the time is before now,
  2383. to will be interpreted as tomorrow.
  2384. Currently none of this works for ISO week specifications.
  2385. When this option is nil, the current day, month and year will always be
  2386. used as defaults.
  2387. See also `org-agenda-jump-prefer-future'."
  2388. :group 'org-time
  2389. :type '(choice
  2390. (const :tag "Never" nil)
  2391. (const :tag "Check month and day" t)
  2392. (const :tag "Check month, day, and time" time)))
  2393. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2394. "Should the agenda jump command prefer the future for incomplete dates?
  2395. The default is to do the same as configured in `org-read-date-prefer-future'.
  2396. But you can also set a deviating value here.
  2397. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2398. :group 'org-agenda
  2399. :group 'org-time
  2400. :version "24.1"
  2401. :type '(choice
  2402. (const :tag "Use org-read-date-prefer-future"
  2403. org-read-date-prefer-future)
  2404. (const :tag "Never" nil)
  2405. (const :tag "Always" t)))
  2406. (defcustom org-read-date-force-compatible-dates t
  2407. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2408. Depending on the system Emacs is running on, certain dates cannot
  2409. be represented with the type used internally to represent time.
  2410. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2411. correctly. Some systems allow for earlier dates, some for later,
  2412. some for both. One way to find out it to insert any date into an
  2413. Org buffer, putting the cursor on the year and hitting S-up and
  2414. S-down to test the range.
  2415. When this variable is set to t, the date/time prompt will not let
  2416. you specify dates outside the 1970-2037 range, so it is certain that
  2417. these dates will work in whatever version of Emacs you are
  2418. running, and also that you can move a file from one Emacs implementation
  2419. to another. WHenever Org is forcing the year for you, it will display
  2420. a message and beep.
  2421. When this variable is nil, Org will check if the date is
  2422. representable in the specific Emacs implementation you are using.
  2423. If not, it will force a year, usually the current year, and beep
  2424. to remind you. Currently this setting is not recommended because
  2425. the likelihood that you will open your Org files in an Emacs that
  2426. has limited date range is not negligible.
  2427. A workaround for this problem is to use diary sexp dates for time
  2428. stamps outside of this range."
  2429. :group 'org-time
  2430. :version "24.1"
  2431. :type 'boolean)
  2432. (defcustom org-read-date-display-live t
  2433. "Non-nil means display current interpretation of date prompt live.
  2434. This display will be in an overlay, in the minibuffer."
  2435. :group 'org-time
  2436. :type 'boolean)
  2437. (defcustom org-read-date-popup-calendar t
  2438. "Non-nil means pop up a calendar when prompting for a date.
  2439. In the calendar, the date can be selected with mouse-1. However, the
  2440. minibuffer will also be active, and you can simply enter the date as well.
  2441. When nil, only the minibuffer will be available."
  2442. :group 'org-time
  2443. :type 'boolean)
  2444. (if (fboundp 'defvaralias)
  2445. (defvaralias 'org-popup-calendar-for-date-prompt
  2446. 'org-read-date-popup-calendar))
  2447. (defcustom org-read-date-minibuffer-setup-hook nil
  2448. "Hook to be used to set up keys for the date/time interface.
  2449. Add key definitions to `minibuffer-local-map', which will be a temporary
  2450. copy."
  2451. :group 'org-time
  2452. :type 'hook)
  2453. (defcustom org-extend-today-until 0
  2454. "The hour when your day really ends. Must be an integer.
  2455. This has influence for the following applications:
  2456. - When switching the agenda to \"today\". It it is still earlier than
  2457. the time given here, the day recognized as TODAY is actually yesterday.
  2458. - When a date is read from the user and it is still before the time given
  2459. here, the current date and time will be assumed to be yesterday, 23:59.
  2460. Also, timestamps inserted in remember templates follow this rule.
  2461. IMPORTANT: This is a feature whose implementation is and likely will
  2462. remain incomplete. Really, it is only here because past midnight seems to
  2463. be the favorite working time of John Wiegley :-)"
  2464. :group 'org-time
  2465. :type 'integer)
  2466. (defcustom org-use-effective-time nil
  2467. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2468. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2469. \"effective time\" of any timestamps between midnight and 8am will be
  2470. 23:59 of the previous day."
  2471. :group 'boolean
  2472. :version "24.1"
  2473. :type 'integer)
  2474. (defcustom org-edit-timestamp-down-means-later nil
  2475. "Non-nil means S-down will increase the time in a time stamp.
  2476. When nil, S-up will increase."
  2477. :group 'org-time
  2478. :type 'boolean)
  2479. (defcustom org-calendar-follow-timestamp-change t
  2480. "Non-nil means make the calendar window follow timestamp changes.
  2481. When a timestamp is modified and the calendar window is visible, it will be
  2482. moved to the new date."
  2483. :group 'org-time
  2484. :type 'boolean)
  2485. (defgroup org-tags nil
  2486. "Options concerning tags in Org-mode."
  2487. :tag "Org Tags"
  2488. :group 'org)
  2489. (defcustom org-tag-alist nil
  2490. "List of tags allowed in Org-mode files.
  2491. When this list is nil, Org-mode will base TAG input on what is already in the
  2492. buffer.
  2493. The value of this variable is an alist, the car of each entry must be a
  2494. keyword as a string, the cdr may be a character that is used to select
  2495. that tag through the fast-tag-selection interface.
  2496. See the manual for details."
  2497. :group 'org-tags
  2498. :type '(repeat
  2499. (choice
  2500. (cons (string :tag "Tag name")
  2501. (character :tag "Access char"))
  2502. (list :tag "Start radio group"
  2503. (const :startgroup)
  2504. (option (string :tag "Group description")))
  2505. (list :tag "End radio group"
  2506. (const :endgroup)
  2507. (option (string :tag "Group description")))
  2508. (const :tag "New line" (:newline)))))
  2509. (defcustom org-tag-persistent-alist nil
  2510. "List of tags that will always appear in all Org-mode files.
  2511. This is in addition to any in buffer settings or customizations
  2512. of `org-tag-alist'.
  2513. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2514. The value of this variable is an alist, the car of each entry must be a
  2515. keyword as a string, the cdr may be a character that is used to select
  2516. that tag through the fast-tag-selection interface.
  2517. See the manual for details.
  2518. To disable these tags on a per-file basis, insert anywhere in the file:
  2519. #+STARTUP: noptag"
  2520. :group 'org-tags
  2521. :type '(repeat
  2522. (choice
  2523. (cons (string :tag "Tag name")
  2524. (character :tag "Access char"))
  2525. (const :tag "Start radio group" (:startgroup))
  2526. (const :tag "End radio group" (:endgroup))
  2527. (const :tag "New line" (:newline)))))
  2528. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2529. "If non-nil, always offer completion for all tags of all agenda files.
  2530. Instead of customizing this variable directly, you might want to
  2531. set it locally for capture buffers, because there no list of
  2532. tags in that file can be created dynamically (there are none).
  2533. (add-hook 'org-capture-mode-hook
  2534. (lambda ()
  2535. (set (make-local-variable
  2536. 'org-complete-tags-always-offer-all-agenda-tags)
  2537. t)))"
  2538. :group 'org-tags
  2539. :version "24.1"
  2540. :type 'boolean)
  2541. (defvar org-file-tags nil
  2542. "List of tags that can be inherited by all entries in the file.
  2543. The tags will be inherited if the variable `org-use-tag-inheritance'
  2544. says they should be.
  2545. This variable is populated from #+FILETAGS lines.")
  2546. (defcustom org-use-fast-tag-selection 'auto
  2547. "Non-nil means use fast tag selection scheme.
  2548. This is a special interface to select and deselect tags with single keys.
  2549. When nil, fast selection is never used.
  2550. When the symbol `auto', fast selection is used if and only if selection
  2551. characters for tags have been configured, either through the variable
  2552. `org-tag-alist' or through a #+TAGS line in the buffer.
  2553. When t, fast selection is always used and selection keys are assigned
  2554. automatically if necessary."
  2555. :group 'org-tags
  2556. :type '(choice
  2557. (const :tag "Always" t)
  2558. (const :tag "Never" nil)
  2559. (const :tag "When selection characters are configured" 'auto)))
  2560. (defcustom org-fast-tag-selection-single-key nil
  2561. "Non-nil means fast tag selection exits after first change.
  2562. When nil, you have to press RET to exit it.
  2563. During fast tag selection, you can toggle this flag with `C-c'.
  2564. This variable can also have the value `expert'. In this case, the window
  2565. displaying the tags menu is not even shown, until you press C-c again."
  2566. :group 'org-tags
  2567. :type '(choice
  2568. (const :tag "No" nil)
  2569. (const :tag "Yes" t)
  2570. (const :tag "Expert" expert)))
  2571. (defvar org-fast-tag-selection-include-todo nil
  2572. "Non-nil means fast tags selection interface will also offer TODO states.
  2573. This is an undocumented feature, you should not rely on it.")
  2574. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2575. "The column to which tags should be indented in a headline.
  2576. If this number is positive, it specifies the column. If it is negative,
  2577. it means that the tags should be flushright to that column. For example,
  2578. -80 works well for a normal 80 character screen.
  2579. When 0, place tags directly after headline text, with only one space in
  2580. between."
  2581. :group 'org-tags
  2582. :type 'integer)
  2583. (defcustom org-auto-align-tags t
  2584. "Non-nil keeps tags aligned when modifying headlines.
  2585. Some operations (i.e. demoting) change the length of a headline and
  2586. therefore shift the tags around. With this option turned on, after
  2587. each such operation the tags are again aligned to `org-tags-column'."
  2588. :group 'org-tags
  2589. :type 'boolean)
  2590. (defcustom org-use-tag-inheritance t
  2591. "Non-nil means tags in levels apply also for sublevels.
  2592. When nil, only the tags directly given in a specific line apply there.
  2593. This may also be a list of tags that should be inherited, or a regexp that
  2594. matches tags that should be inherited. Additional control is possible
  2595. with the variable `org-tags-exclude-from-inheritance' which gives an
  2596. explicit list of tags to be excluded from inheritance., even if the value of
  2597. `org-use-tag-inheritance' would select it for inheritance.
  2598. If this option is t, a match early-on in a tree can lead to a large
  2599. number of matches in the subtree when constructing the agenda or creating
  2600. a sparse tree. If you only want to see the first match in a tree during
  2601. a search, check out the variable `org-tags-match-list-sublevels'."
  2602. :group 'org-tags
  2603. :type '(choice
  2604. (const :tag "Not" nil)
  2605. (const :tag "Always" t)
  2606. (repeat :tag "Specific tags" (string :tag "Tag"))
  2607. (regexp :tag "Tags matched by regexp")))
  2608. (defcustom org-tags-exclude-from-inheritance nil
  2609. "List of tags that should never be inherited.
  2610. This is a way to exclude a few tags from inheritance. For way to do
  2611. the opposite, to actively allow inheritance for selected tags,
  2612. see the variable `org-use-tag-inheritance'."
  2613. :group 'org-tags
  2614. :type '(repeat (string :tag "Tag")))
  2615. (defun org-tag-inherit-p (tag)
  2616. "Check if TAG is one that should be inherited."
  2617. (cond
  2618. ((member tag org-tags-exclude-from-inheritance) nil)
  2619. ((eq org-use-tag-inheritance t) t)
  2620. ((not org-use-tag-inheritance) nil)
  2621. ((stringp org-use-tag-inheritance)
  2622. (string-match org-use-tag-inheritance tag))
  2623. ((listp org-use-tag-inheritance)
  2624. (member tag org-use-tag-inheritance))
  2625. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2626. (defcustom org-tags-match-list-sublevels t
  2627. "Non-nil means list also sublevels of headlines matching a search.
  2628. This variable applies to tags/property searches, and also to stuck
  2629. projects because this search is based on a tags match as well.
  2630. When set to the symbol `indented', sublevels are indented with
  2631. leading dots.
  2632. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2633. the sublevels of a headline matching a tag search often also match
  2634. the same search. Listing all of them can create very long lists.
  2635. Setting this variable to nil causes subtrees of a match to be skipped.
  2636. This variable is semi-obsolete and probably should always be true. It
  2637. is better to limit inheritance to certain tags using the variables
  2638. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2639. :group 'org-tags
  2640. :type '(choice
  2641. (const :tag "No, don't list them" nil)
  2642. (const :tag "Yes, do list them" t)
  2643. (const :tag "List them, indented with leading dots" indented)))
  2644. (defcustom org-tags-sort-function nil
  2645. "When set, tags are sorted using this function as a comparator."
  2646. :group 'org-tags
  2647. :type '(choice
  2648. (const :tag "No sorting" nil)
  2649. (const :tag "Alphabetical" string<)
  2650. (const :tag "Reverse alphabetical" string>)
  2651. (function :tag "Custom function" nil)))
  2652. (defvar org-tags-history nil
  2653. "History of minibuffer reads for tags.")
  2654. (defvar org-last-tags-completion-table nil
  2655. "The last used completion table for tags.")
  2656. (defvar org-after-tags-change-hook nil
  2657. "Hook that is run after the tags in a line have changed.")
  2658. (defgroup org-properties nil
  2659. "Options concerning properties in Org-mode."
  2660. :tag "Org Properties"
  2661. :group 'org)
  2662. (defcustom org-property-format "%-10s %s"
  2663. "How property key/value pairs should be formatted by `indent-line'.
  2664. When `indent-line' hits a property definition, it will format the line
  2665. according to this format, mainly to make sure that the values are
  2666. lined-up with respect to each other."
  2667. :group 'org-properties
  2668. :type 'string)
  2669. (defcustom org-properties-postprocess-alist nil
  2670. "Alist of properties and functions to adjust inserted values.
  2671. Elements of this alist must be of the form
  2672. ([string] [function])
  2673. where [string] must be a property name and [function] must be a
  2674. lambda expression: this lambda expression must take one argument,
  2675. the value to adjust, and return the new value as a string.
  2676. For example, this element will allow the property \"Remaining\"
  2677. to be updated wrt the relation between the \"Effort\" property
  2678. and the clock summary:
  2679. ((\"Remaining\" (lambda(value)
  2680. (let ((clocksum (org-clock-sum-current-item))
  2681. (effort (org-duration-string-to-minutes
  2682. (org-entry-get (point) \"Effort\"))))
  2683. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2684. :group 'org-properties
  2685. :version "24.1"
  2686. :type 'alist)
  2687. (defcustom org-use-property-inheritance nil
  2688. "Non-nil means properties apply also for sublevels.
  2689. This setting is chiefly used during property searches. Turning it on can
  2690. cause significant overhead when doing a search, which is why it is not
  2691. on by default.
  2692. When nil, only the properties directly given in the current entry count.
  2693. When t, every property is inherited. The value may also be a list of
  2694. properties that should have inheritance, or a regular expression matching
  2695. properties that should be inherited.
  2696. However, note that some special properties use inheritance under special
  2697. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2698. and the properties ending in \"_ALL\" when they are used as descriptor
  2699. for valid values of a property.
  2700. Note for programmers:
  2701. When querying an entry with `org-entry-get', you can control if inheritance
  2702. should be used. By default, `org-entry-get' looks only at the local
  2703. properties. You can request inheritance by setting the inherit argument
  2704. to t (to force inheritance) or to `selective' (to respect the setting
  2705. in this variable)."
  2706. :group 'org-properties
  2707. :type '(choice
  2708. (const :tag "Not" nil)
  2709. (const :tag "Always" t)
  2710. (repeat :tag "Specific properties" (string :tag "Property"))
  2711. (regexp :tag "Properties matched by regexp")))
  2712. (defun org-property-inherit-p (property)
  2713. "Check if PROPERTY is one that should be inherited."
  2714. (cond
  2715. ((eq org-use-property-inheritance t) t)
  2716. ((not org-use-property-inheritance) nil)
  2717. ((stringp org-use-property-inheritance)
  2718. (string-match org-use-property-inheritance property))
  2719. ((listp org-use-property-inheritance)
  2720. (member property org-use-property-inheritance))
  2721. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2722. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2723. "The default column format, if no other format has been defined.
  2724. This variable can be set on the per-file basis by inserting a line
  2725. #+COLUMNS: %25ITEM ....."
  2726. :group 'org-properties
  2727. :type 'string)
  2728. (defcustom org-columns-ellipses ".."
  2729. "The ellipses to be used when a field in column view is truncated.
  2730. When this is the empty string, as many characters as possible are shown,
  2731. but then there will be no visual indication that the field has been truncated.
  2732. When this is a string of length N, the last N characters of a truncated
  2733. field are replaced by this string. If the column is narrower than the
  2734. ellipses string, only part of the ellipses string will be shown."
  2735. :group 'org-properties
  2736. :type 'string)
  2737. (defcustom org-columns-modify-value-for-display-function nil
  2738. "Function that modifies values for display in column view.
  2739. For example, it can be used to cut out a certain part from a time stamp.
  2740. The function must take 2 arguments:
  2741. column-title The title of the column (*not* the property name)
  2742. value The value that should be modified.
  2743. The function should return the value that should be displayed,
  2744. or nil if the normal value should be used."
  2745. :group 'org-properties
  2746. :type 'function)
  2747. (defcustom org-effort-property "Effort"
  2748. "The property that is being used to keep track of effort estimates.
  2749. Effort estimates given in this property need to have the format H:MM."
  2750. :group 'org-properties
  2751. :group 'org-progress
  2752. :type '(string :tag "Property"))
  2753. (defconst org-global-properties-fixed
  2754. '(("VISIBILITY_ALL" . "folded children content all")
  2755. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2756. "List of property/value pairs that can be inherited by any entry.
  2757. These are fixed values, for the preset properties. The user variable
  2758. that can be used to add to this list is `org-global-properties'.
  2759. The entries in this list are cons cells where the car is a property
  2760. name and cdr is a string with the value. If the value represents
  2761. multiple items like an \"_ALL\" property, separate the items by
  2762. spaces.")
  2763. (defcustom org-global-properties nil
  2764. "List of property/value pairs that can be inherited by any entry.
  2765. This list will be combined with the constant `org-global-properties-fixed'.
  2766. The entries in this list are cons cells where the car is a property
  2767. name and cdr is a string with the value.
  2768. You can set buffer-local values for the same purpose in the variable
  2769. `org-file-properties' this by adding lines like
  2770. #+PROPERTY: NAME VALUE"
  2771. :group 'org-properties
  2772. :type '(repeat
  2773. (cons (string :tag "Property")
  2774. (string :tag "Value"))))
  2775. (defvar org-file-properties nil
  2776. "List of property/value pairs that can be inherited by any entry.
  2777. Valid for the current buffer.
  2778. This variable is populated from #+PROPERTY lines.")
  2779. (make-variable-buffer-local 'org-file-properties)
  2780. (defgroup org-agenda nil
  2781. "Options concerning agenda views in Org-mode."
  2782. :tag "Org Agenda"
  2783. :group 'org)
  2784. (defvar org-category nil
  2785. "Variable used by org files to set a category for agenda display.
  2786. Such files should use a file variable to set it, for example
  2787. # -*- mode: org; org-category: \"ELisp\"
  2788. or contain a special line
  2789. #+CATEGORY: ELisp
  2790. If the file does not specify a category, then file's base name
  2791. is used instead.")
  2792. (make-variable-buffer-local 'org-category)
  2793. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2794. (defcustom org-agenda-files nil
  2795. "The files to be used for agenda display.
  2796. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2797. \\[org-remove-file]. You can also use customize to edit the list.
  2798. If an entry is a directory, all files in that directory that are matched by
  2799. `org-agenda-file-regexp' will be part of the file list.
  2800. If the value of the variable is not a list but a single file name, then
  2801. the list of agenda files is actually stored and maintained in that file, one
  2802. agenda file per line. In this file paths can be given relative to
  2803. `org-directory'. Tilde expansion and environment variable substitution
  2804. are also made."
  2805. :group 'org-agenda
  2806. :type '(choice
  2807. (repeat :tag "List of files and directories" file)
  2808. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2809. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2810. "Regular expression to match files for `org-agenda-files'.
  2811. If any element in the list in that variable contains a directory instead
  2812. of a normal file, all files in that directory that are matched by this
  2813. regular expression will be included."
  2814. :group 'org-agenda
  2815. :type 'regexp)
  2816. (defcustom org-agenda-text-search-extra-files nil
  2817. "List of extra files to be searched by text search commands.
  2818. These files will be search in addition to the agenda files by the
  2819. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2820. Note that these files will only be searched for text search commands,
  2821. not for the other agenda views like todo lists, tag searches or the weekly
  2822. agenda. This variable is intended to list notes and possibly archive files
  2823. that should also be searched by these two commands.
  2824. In fact, if the first element in the list is the symbol `agenda-archives',
  2825. than all archive files of all agenda files will be added to the search
  2826. scope."
  2827. :group 'org-agenda
  2828. :type '(set :greedy t
  2829. (const :tag "Agenda Archives" agenda-archives)
  2830. (repeat :inline t (file))))
  2831. (if (fboundp 'defvaralias)
  2832. (defvaralias 'org-agenda-multi-occur-extra-files
  2833. 'org-agenda-text-search-extra-files))
  2834. (defcustom org-agenda-skip-unavailable-files nil
  2835. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2836. A nil value means to remove them, after a query, from the list."
  2837. :group 'org-agenda
  2838. :type 'boolean)
  2839. (defcustom org-calendar-to-agenda-key [?c]
  2840. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2841. The command `org-calendar-goto-agenda' will be bound to this key. The
  2842. default is the character `c' because then `c' can be used to switch back and
  2843. forth between agenda and calendar."
  2844. :group 'org-agenda
  2845. :type 'sexp)
  2846. (defcustom org-calendar-agenda-action-key [?k]
  2847. "The key to be installed in `calendar-mode-map' for agenda-action.
  2848. The command `org-agenda-action' will be bound to this key. The
  2849. default is the character `k' because we use the same key in the agenda."
  2850. :group 'org-agenda
  2851. :type 'sexp)
  2852. (defcustom org-calendar-insert-diary-entry-key [?i]
  2853. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2854. This option is irrelevant until `org-agenda-diary-file' has been configured
  2855. to point to an Org-mode file. When that is the case, the command
  2856. `org-agenda-diary-entry' will be bound to the key given here, by default
  2857. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2858. if you want to continue doing this, you need to change this to a different
  2859. key."
  2860. :group 'org-agenda
  2861. :type 'sexp)
  2862. (defcustom org-agenda-diary-file 'diary-file
  2863. "File to which to add new entries with the `i' key in agenda and calendar.
  2864. When this is the symbol `diary-file', the functionality in the Emacs
  2865. calendar will be used to add entries to the `diary-file'. But when this
  2866. points to a file, `org-agenda-diary-entry' will be used instead."
  2867. :group 'org-agenda
  2868. :type '(choice
  2869. (const :tag "The standard Emacs diary file" diary-file)
  2870. (file :tag "Special Org file diary entries")))
  2871. (eval-after-load "calendar"
  2872. '(progn
  2873. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2874. 'org-calendar-goto-agenda)
  2875. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2876. 'org-agenda-action)
  2877. (add-hook 'calendar-mode-hook
  2878. (lambda ()
  2879. (unless (eq org-agenda-diary-file 'diary-file)
  2880. (define-key calendar-mode-map
  2881. org-calendar-insert-diary-entry-key
  2882. 'org-agenda-diary-entry))))))
  2883. (defgroup org-latex nil
  2884. "Options for embedding LaTeX code into Org-mode."
  2885. :tag "Org LaTeX"
  2886. :group 'org)
  2887. (defcustom org-format-latex-options
  2888. '(:foreground default :background default :scale 1.0
  2889. :html-foreground "Black" :html-background "Transparent"
  2890. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2891. "Options for creating images from LaTeX fragments.
  2892. This is a property list with the following properties:
  2893. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2894. `default' means use the foreground of the default face.
  2895. :background the background color, or \"Transparent\".
  2896. `default' means use the background of the default face.
  2897. :scale a scaling factor for the size of the images, to get more pixels
  2898. :html-foreground, :html-background, :html-scale
  2899. the same numbers for HTML export.
  2900. :matchers a list indicating which matchers should be used to
  2901. find LaTeX fragments. Valid members of this list are:
  2902. \"begin\" find environments
  2903. \"$1\" find single characters surrounded by $.$
  2904. \"$\" find math expressions surrounded by $...$
  2905. \"$$\" find math expressions surrounded by $$....$$
  2906. \"\\(\" find math expressions surrounded by \\(...\\)
  2907. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2908. :group 'org-latex
  2909. :type 'plist)
  2910. (defcustom org-format-latex-signal-error t
  2911. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2912. When nil, just push out a message."
  2913. :group 'org-latex
  2914. :version "24.1"
  2915. :type 'boolean)
  2916. (defcustom org-latex-to-mathml-jar-file nil
  2917. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2918. Use this to specify additional executable file say a jar file.
  2919. When using MathToWeb as the converter, specify the full-path to
  2920. your mathtoweb.jar file."
  2921. :group 'org-latex
  2922. :version "24.1"
  2923. :type '(choice
  2924. (const :tag "None" nil)
  2925. (file :tag "JAR file" :must-match t)))
  2926. (defcustom org-latex-to-mathml-convert-command nil
  2927. "Command to convert LaTeX fragments to MathML.
  2928. Replace format-specifiers in the command as noted below and use
  2929. `shell-command' to convert LaTeX to MathML.
  2930. %j: Executable file in fully expanded form as specified by
  2931. `org-latex-to-mathml-jar-file'.
  2932. %I: Input LaTeX file in fully expanded form
  2933. %o: Output MathML file
  2934. This command is used by `org-create-math-formula'.
  2935. When using MathToWeb as the converter, set this to
  2936. \"java -jar %j -unicode -force -df %o %I\"."
  2937. :group 'org-latex
  2938. :version "24.1"
  2939. :type '(choice
  2940. (const :tag "None" nil)
  2941. (string :tag "\nShell command")))
  2942. (defun org-format-latex-mathml-available-p ()
  2943. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2944. (save-match-data
  2945. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2946. org-latex-to-mathml-convert-command)
  2947. (let ((executable (car (split-string
  2948. org-latex-to-mathml-convert-command))))
  2949. (when (executable-find executable)
  2950. (if (string-match
  2951. "%j" org-latex-to-mathml-convert-command)
  2952. (file-readable-p org-latex-to-mathml-jar-file)
  2953. t))))))
  2954. (defcustom org-format-latex-header "\\documentclass{article}
  2955. \\usepackage[usenames]{color}
  2956. \\usepackage{amsmath}
  2957. \\usepackage[mathscr]{eucal}
  2958. \\pagestyle{empty} % do not remove
  2959. \[PACKAGES]
  2960. \[DEFAULT-PACKAGES]
  2961. % The settings below are copied from fullpage.sty
  2962. \\setlength{\\textwidth}{\\paperwidth}
  2963. \\addtolength{\\textwidth}{-3cm}
  2964. \\setlength{\\oddsidemargin}{1.5cm}
  2965. \\addtolength{\\oddsidemargin}{-2.54cm}
  2966. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2967. \\setlength{\\textheight}{\\paperheight}
  2968. \\addtolength{\\textheight}{-\\headheight}
  2969. \\addtolength{\\textheight}{-\\headsep}
  2970. \\addtolength{\\textheight}{-\\footskip}
  2971. \\addtolength{\\textheight}{-3cm}
  2972. \\setlength{\\topmargin}{1.5cm}
  2973. \\addtolength{\\topmargin}{-2.54cm}"
  2974. "The document header used for processing LaTeX fragments.
  2975. It is imperative that this header make sure that no page number
  2976. appears on the page. The package defined in the variables
  2977. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2978. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2979. will be appended."
  2980. :group 'org-latex
  2981. :type 'string)
  2982. (defvar org-format-latex-header-extra nil)
  2983. (defun org-set-packages-alist (var val)
  2984. "Set the packages alist and make sure it has 3 elements per entry."
  2985. (set var (mapcar (lambda (x)
  2986. (if (and (consp x) (= (length x) 2))
  2987. (list (car x) (nth 1 x) t)
  2988. x))
  2989. val)))
  2990. (defun org-get-packages-alist (var)
  2991. "Get the packages alist and make sure it has 3 elements per entry."
  2992. (mapcar (lambda (x)
  2993. (if (and (consp x) (= (length x) 2))
  2994. (list (car x) (nth 1 x) t)
  2995. x))
  2996. (default-value var)))
  2997. ;; The following variables are defined here because is it also used
  2998. ;; when formatting latex fragments. Originally it was part of the
  2999. ;; LaTeX exporter, which is why the name includes "export".
  3000. (defcustom org-export-latex-default-packages-alist
  3001. '(("AUTO" "inputenc" t)
  3002. ("T1" "fontenc" t)
  3003. ("" "fixltx2e" nil)
  3004. ("" "graphicx" t)
  3005. ("" "longtable" nil)
  3006. ("" "float" nil)
  3007. ("" "wrapfig" nil)
  3008. ("" "soul" t)
  3009. ("" "textcomp" t)
  3010. ("" "marvosym" t)
  3011. ("" "wasysym" t)
  3012. ("" "latexsym" t)
  3013. ("" "amssymb" t)
  3014. ("" "hyperref" nil)
  3015. "\\tolerance=1000"
  3016. )
  3017. "Alist of default packages to be inserted in the header.
  3018. Change this only if one of the packages here causes an incompatibility
  3019. with another package you are using.
  3020. The packages in this list are needed by one part or another of Org-mode
  3021. to function properly.
  3022. - inputenc, fontenc: for basic font and character selection
  3023. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3024. for interpreting the entities in `org-entities'. You can skip some of these
  3025. packages if you don't use any of the symbols in it.
  3026. - graphicx: for including images
  3027. - float, wrapfig: for figure placement
  3028. - longtable: for long tables
  3029. - hyperref: for cross references
  3030. Therefore you should not modify this variable unless you know what you
  3031. are doing. The one reason to change it anyway is that you might be loading
  3032. some other package that conflicts with one of the default packages.
  3033. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3034. If SNIPPET-FLAG is t, the package also needs to be included when
  3035. compiling LaTeX snippets into images for inclusion into HTML."
  3036. :group 'org-export-latex
  3037. :set 'org-set-packages-alist
  3038. :get 'org-get-packages-alist
  3039. :version "24.1"
  3040. :type '(repeat
  3041. (choice
  3042. (list :tag "options/package pair"
  3043. (string :tag "options")
  3044. (string :tag "package")
  3045. (boolean :tag "Snippet"))
  3046. (string :tag "A line of LaTeX"))))
  3047. (defcustom org-export-latex-packages-alist nil
  3048. "Alist of packages to be inserted in every LaTeX header.
  3049. These will be inserted after `org-export-latex-default-packages-alist'.
  3050. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3051. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3052. turning LaTeX snippets into images for inclusion into HTML.
  3053. Make sure that you only list packages here which:
  3054. - you want in every file
  3055. - do not conflict with the default packages in
  3056. `org-export-latex-default-packages-alist'
  3057. - do not conflict with the setup in `org-format-latex-header'."
  3058. :group 'org-export-latex
  3059. :set 'org-set-packages-alist
  3060. :get 'org-get-packages-alist
  3061. :type '(repeat
  3062. (choice
  3063. (list :tag "options/package pair"
  3064. (string :tag "options")
  3065. (string :tag "package")
  3066. (boolean :tag "Snippet"))
  3067. (string :tag "A line of LaTeX"))))
  3068. (defgroup org-appearance nil
  3069. "Settings for Org-mode appearance."
  3070. :tag "Org Appearance"
  3071. :group 'org)
  3072. (defcustom org-level-color-stars-only nil
  3073. "Non-nil means fontify only the stars in each headline.
  3074. When nil, the entire headline is fontified.
  3075. Changing it requires restart of `font-lock-mode' to become effective
  3076. also in regions already fontified."
  3077. :group 'org-appearance
  3078. :type 'boolean)
  3079. (defcustom org-hide-leading-stars nil
  3080. "Non-nil means hide the first N-1 stars in a headline.
  3081. This works by using the face `org-hide' for these stars. This
  3082. face is white for a light background, and black for a dark
  3083. background. You may have to customize the face `org-hide' to
  3084. make this work.
  3085. Changing it requires restart of `font-lock-mode' to become effective
  3086. also in regions already fontified.
  3087. You may also set this on a per-file basis by adding one of the following
  3088. lines to the buffer:
  3089. #+STARTUP: hidestars
  3090. #+STARTUP: showstars"
  3091. :group 'org-appearance
  3092. :type 'boolean)
  3093. (defcustom org-hidden-keywords nil
  3094. "List of symbols corresponding to keywords to be hidden the org buffer.
  3095. For example, a value '(title) for this list will make the document's title
  3096. appear in the buffer without the initial #+TITLE: keyword."
  3097. :group 'org-appearance
  3098. :version "24.1"
  3099. :type '(set (const :tag "#+AUTHOR" author)
  3100. (const :tag "#+DATE" date)
  3101. (const :tag "#+EMAIL" email)
  3102. (const :tag "#+TITLE" title)))
  3103. (defcustom org-fontify-done-headline nil
  3104. "Non-nil means change the face of a headline if it is marked DONE.
  3105. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3106. When this is non-nil, the headline after the keyword is set to the
  3107. `org-headline-done' as an additional indication."
  3108. :group 'org-appearance
  3109. :type 'boolean)
  3110. (defcustom org-fontify-emphasized-text t
  3111. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3112. Changing this variable requires a restart of Emacs to take effect."
  3113. :group 'org-appearance
  3114. :type 'boolean)
  3115. (defcustom org-fontify-whole-heading-line nil
  3116. "Non-nil means fontify the whole line for headings.
  3117. This is useful when setting a background color for the
  3118. org-level-* faces."
  3119. :group 'org-appearance
  3120. :type 'boolean)
  3121. (defcustom org-highlight-latex-fragments-and-specials nil
  3122. "Non-nil means fontify what is treated specially by the exporters."
  3123. :group 'org-appearance
  3124. :type 'boolean)
  3125. (defcustom org-hide-emphasis-markers nil
  3126. "Non-nil mean font-lock should hide the emphasis marker characters."
  3127. :group 'org-appearance
  3128. :type 'boolean)
  3129. (defcustom org-pretty-entities nil
  3130. "Non-nil means show entities as UTF8 characters.
  3131. When nil, the \\name form remains in the buffer."
  3132. :group 'org-appearance
  3133. :version "24.1"
  3134. :type 'boolean)
  3135. (defcustom org-pretty-entities-include-sub-superscripts t
  3136. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3137. :group 'org-appearance
  3138. :version "24.1"
  3139. :type 'boolean)
  3140. (defvar org-emph-re nil
  3141. "Regular expression for matching emphasis.
  3142. After a match, the match groups contain these elements:
  3143. 0 The match of the full regular expression, including the characters
  3144. before and after the proper match
  3145. 1 The character before the proper match, or empty at beginning of line
  3146. 2 The proper match, including the leading and trailing markers
  3147. 3 The leading marker like * or /, indicating the type of highlighting
  3148. 4 The text between the emphasis markers, not including the markers
  3149. 5 The character after the match, empty at the end of a line")
  3150. (defvar org-verbatim-re nil
  3151. "Regular expression for matching verbatim text.")
  3152. (defvar org-emphasis-regexp-components) ; defined just below
  3153. (defvar org-emphasis-alist) ; defined just below
  3154. (defun org-set-emph-re (var val)
  3155. "Set variable and compute the emphasis regular expression."
  3156. (set var val)
  3157. (when (and (boundp 'org-emphasis-alist)
  3158. (boundp 'org-emphasis-regexp-components)
  3159. org-emphasis-alist org-emphasis-regexp-components)
  3160. (let* ((e org-emphasis-regexp-components)
  3161. (pre (car e))
  3162. (post (nth 1 e))
  3163. (border (nth 2 e))
  3164. (body (nth 3 e))
  3165. (nl (nth 4 e))
  3166. (body1 (concat body "*?"))
  3167. (markers (mapconcat 'car org-emphasis-alist ""))
  3168. (vmarkers (mapconcat
  3169. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3170. org-emphasis-alist "")))
  3171. ;; make sure special characters appear at the right position in the class
  3172. (if (string-match "\\^" markers)
  3173. (setq markers (concat (replace-match "" t t markers) "^")))
  3174. (if (string-match "-" markers)
  3175. (setq markers (concat (replace-match "" t t markers) "-")))
  3176. (if (string-match "\\^" vmarkers)
  3177. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3178. (if (string-match "-" vmarkers)
  3179. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3180. (if (> nl 0)
  3181. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3182. (int-to-string nl) "\\}")))
  3183. ;; Make the regexp
  3184. (setq org-emph-re
  3185. (concat "\\([" pre "]\\|^\\)"
  3186. "\\("
  3187. "\\([" markers "]\\)"
  3188. "\\("
  3189. "[^" border "]\\|"
  3190. "[^" border "]"
  3191. body1
  3192. "[^" border "]"
  3193. "\\)"
  3194. "\\3\\)"
  3195. "\\([" post "]\\|$\\)"))
  3196. (setq org-verbatim-re
  3197. (concat "\\([" pre "]\\|^\\)"
  3198. "\\("
  3199. "\\([" vmarkers "]\\)"
  3200. "\\("
  3201. "[^" border "]\\|"
  3202. "[^" border "]"
  3203. body1
  3204. "[^" border "]"
  3205. "\\)"
  3206. "\\3\\)"
  3207. "\\([" post "]\\|$\\)")))))
  3208. (defcustom org-emphasis-regexp-components
  3209. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3210. "Components used to build the regular expression for emphasis.
  3211. This is a list with five entries. Terminology: In an emphasis string
  3212. like \" *strong word* \", we call the initial space PREMATCH, the final
  3213. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3214. and \"trong wor\" is the body. The different components in this variable
  3215. specify what is allowed/forbidden in each part:
  3216. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3217. post Chars allowed as postmatch. End of line will be allowed too.
  3218. border The chars *forbidden* as border characters.
  3219. body-regexp A regexp like \".\" to match a body character. Don't use
  3220. non-shy groups here, and don't allow newline here.
  3221. newline The maximum number of newlines allowed in an emphasis exp.
  3222. Use customize to modify this, or restart Emacs after changing it."
  3223. :group 'org-appearance
  3224. :set 'org-set-emph-re
  3225. :type '(list
  3226. (sexp :tag "Allowed chars in pre ")
  3227. (sexp :tag "Allowed chars in post ")
  3228. (sexp :tag "Forbidden chars in border ")
  3229. (sexp :tag "Regexp for body ")
  3230. (integer :tag "number of newlines allowed")
  3231. (option (boolean :tag "Please ignore this button"))))
  3232. (defcustom org-emphasis-alist
  3233. `(("*" bold "<b>" "</b>")
  3234. ("/" italic "<i>" "</i>")
  3235. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3236. ("=" org-code "<code>" "</code>" verbatim)
  3237. ("~" org-verbatim "<code>" "</code>" verbatim)
  3238. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3239. "<del>" "</del>")
  3240. )
  3241. "Special syntax for emphasized text.
  3242. Text starting and ending with a special character will be emphasized, for
  3243. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3244. characters, the face to be used by font-lock for highlighting in Org-mode
  3245. Emacs buffers, and the HTML tags to be used for this.
  3246. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3247. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3248. Use customize to modify this, or restart Emacs after changing it."
  3249. :group 'org-appearance
  3250. :set 'org-set-emph-re
  3251. :type '(repeat
  3252. (list
  3253. (string :tag "Marker character")
  3254. (choice
  3255. (face :tag "Font-lock-face")
  3256. (plist :tag "Face property list"))
  3257. (string :tag "HTML start tag")
  3258. (string :tag "HTML end tag")
  3259. (option (const verbatim)))))
  3260. (defvar org-protecting-blocks
  3261. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3262. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3263. This is needed for font-lock setup.")
  3264. ;;; Miscellaneous options
  3265. (defgroup org-completion nil
  3266. "Completion in Org-mode."
  3267. :tag "Org Completion"
  3268. :group 'org)
  3269. (defcustom org-completion-use-ido nil
  3270. "Non-nil means use ido completion wherever possible.
  3271. Note that `ido-mode' must be active for this variable to be relevant.
  3272. If you decide to turn this variable on, you might well want to turn off
  3273. `org-outline-path-complete-in-steps'.
  3274. See also `org-completion-use-iswitchb'."
  3275. :group 'org-completion
  3276. :type 'boolean)
  3277. (defcustom org-completion-use-iswitchb nil
  3278. "Non-nil means use iswitchb completion wherever possible.
  3279. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3280. If you decide to turn this variable on, you might well want to turn off
  3281. `org-outline-path-complete-in-steps'.
  3282. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3283. :group 'org-completion
  3284. :type 'boolean)
  3285. (defcustom org-completion-fallback-command 'hippie-expand
  3286. "The expansion command called by \\[pcomplete] in normal context.
  3287. Normal means, no org-mode-specific context."
  3288. :group 'org-completion
  3289. :type 'function)
  3290. ;;; Functions and variables from their packages
  3291. ;; Declared here to avoid compiler warnings
  3292. ;; XEmacs only
  3293. (defvar outline-mode-menu-heading)
  3294. (defvar outline-mode-menu-show)
  3295. (defvar outline-mode-menu-hide)
  3296. (defvar zmacs-regions) ; XEmacs regions
  3297. ;; Emacs only
  3298. (defvar mark-active)
  3299. ;; Various packages
  3300. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3301. (declare-function calendar-forward-day "cal-move" (arg))
  3302. (declare-function calendar-goto-date "cal-move" (date))
  3303. (declare-function calendar-goto-today "cal-move" ())
  3304. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3305. (defvar calc-embedded-close-formula)
  3306. (defvar calc-embedded-open-formula)
  3307. (declare-function cdlatex-tab "ext:cdlatex" ())
  3308. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3309. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3310. (defvar font-lock-unfontify-region-function)
  3311. (declare-function iswitchb-read-buffer "iswitchb"
  3312. (prompt &optional default require-match start matches-set))
  3313. (defvar iswitchb-temp-buflist)
  3314. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3315. (defvar org-agenda-tags-todo-honor-ignore-options)
  3316. (declare-function org-agenda-skip "org-agenda" ())
  3317. (declare-function
  3318. org-agenda-format-item "org-agenda"
  3319. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3321. (declare-function org-agenda-change-all-lines "org-agenda"
  3322. (newhead hdmarker &optional fixface just-this))
  3323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3326. (beg end))
  3327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3329. "org-agenda" (&optional end))
  3330. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3331. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3332. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3333. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3334. (declare-function org-indent-mode "org-indent" (&optional arg))
  3335. (declare-function parse-time-string "parse-time" (string))
  3336. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3337. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3338. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3339. (defvar remember-data-file)
  3340. (defvar texmathp-why)
  3341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3343. (defvar w3m-current-url)
  3344. (defvar w3m-current-title)
  3345. (defvar org-latex-regexps)
  3346. ;;; Autoload and prepare some org modules
  3347. ;; Some table stuff that needs to be defined here, because it is used
  3348. ;; by the functions setting up org-mode or checking for table context.
  3349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3350. "Detect an org-type or table-type table.")
  3351. (defconst org-table-line-regexp "^[ \t]*|"
  3352. "Detect an org-type table line.")
  3353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3354. "Detect an org-type table line.")
  3355. (defconst org-table-hline-regexp "^[ \t]*|-"
  3356. "Detect an org-type table hline.")
  3357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3358. "Detect a table-type table hline.")
  3359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3360. "Detect the first line outside a table when searching from within it.
  3361. This works for both table types.")
  3362. ;; Autoload the functions in org-table.el that are needed by functions here.
  3363. (eval-and-compile
  3364. (org-autoload "org-table"
  3365. '(org-table-align org-table-begin org-table-blank-field
  3366. org-table-convert org-table-convert-region org-table-copy-down
  3367. org-table-copy-region org-table-create
  3368. org-table-create-or-convert-from-region
  3369. org-table-create-with-table.el org-table-current-dline
  3370. org-table-cut-region org-table-delete-column org-table-edit-field
  3371. org-table-edit-formulas org-table-end org-table-eval-formula
  3372. org-table-export org-table-field-info
  3373. org-table-get-stored-formulas org-table-goto-column
  3374. org-table-hline-and-move org-table-import org-table-insert-column
  3375. org-table-insert-hline org-table-insert-row org-table-iterate
  3376. org-table-justify-field-maybe org-table-kill-row
  3377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3378. org-table-move-column org-table-move-column-left
  3379. org-table-move-column-right org-table-move-row
  3380. org-table-move-row-down org-table-move-row-up
  3381. org-table-next-field org-table-next-row org-table-paste-rectangle
  3382. org-table-previous-field org-table-recalculate
  3383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3384. org-table-toggle-coordinate-overlays
  3385. org-table-toggle-formula-debugger org-table-wrap-region
  3386. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3387. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3388. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3389. (defun org-at-table-p (&optional table-type)
  3390. "Return t if the cursor is inside an org-type table.
  3391. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3392. (if org-enable-table-editor
  3393. (save-excursion
  3394. (beginning-of-line 1)
  3395. (looking-at (if table-type org-table-any-line-regexp
  3396. org-table-line-regexp)))
  3397. nil))
  3398. (defsubst org-table-p () (org-at-table-p))
  3399. (defun org-at-table.el-p ()
  3400. "Return t if and only if we are at a table.el table."
  3401. (and (org-at-table-p 'any)
  3402. (save-excursion
  3403. (goto-char (org-table-begin 'any))
  3404. (looking-at org-table1-hline-regexp))))
  3405. (defun org-table-recognize-table.el ()
  3406. "If there is a table.el table nearby, recognize it and move into it."
  3407. (if org-table-tab-recognizes-table.el
  3408. (if (org-at-table.el-p)
  3409. (progn
  3410. (beginning-of-line 1)
  3411. (if (looking-at org-table-dataline-regexp)
  3412. nil
  3413. (if (looking-at org-table1-hline-regexp)
  3414. (progn
  3415. (beginning-of-line 2)
  3416. (if (looking-at org-table-any-border-regexp)
  3417. (beginning-of-line -1)))))
  3418. (if (re-search-forward "|" (org-table-end t) t)
  3419. (progn
  3420. (require 'table)
  3421. (if (table--at-cell-p (point))
  3422. t
  3423. (message "recognizing table.el table...")
  3424. (table-recognize-table)
  3425. (message "recognizing table.el table...done")))
  3426. (error "This should not happen"))
  3427. t)
  3428. nil)
  3429. nil))
  3430. (defun org-at-table-hline-p ()
  3431. "Return t if the cursor is inside a hline in a table."
  3432. (if org-enable-table-editor
  3433. (save-excursion
  3434. (beginning-of-line 1)
  3435. (looking-at org-table-hline-regexp))
  3436. nil))
  3437. (defvar org-table-clean-did-remove-column nil)
  3438. (defun org-table-map-tables (function &optional quietly)
  3439. "Apply FUNCTION to the start of all tables in the buffer."
  3440. (save-excursion
  3441. (save-restriction
  3442. (widen)
  3443. (goto-char (point-min))
  3444. (while (re-search-forward org-table-any-line-regexp nil t)
  3445. (unless quietly
  3446. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3447. (beginning-of-line 1)
  3448. (when (looking-at org-table-line-regexp)
  3449. (save-excursion (funcall function))
  3450. (or (looking-at org-table-line-regexp)
  3451. (forward-char 1)))
  3452. (re-search-forward org-table-any-border-regexp nil 1))))
  3453. (unless quietly (message "Mapping tables: done")))
  3454. ;; Declare and autoload functions from org-exp.el & Co
  3455. (declare-function org-default-export-plist "org-exp")
  3456. (declare-function org-infile-export-plist "org-exp")
  3457. (declare-function org-get-current-options "org-exp")
  3458. (eval-and-compile
  3459. (org-autoload "org-exp"
  3460. '(org-export org-export-visible
  3461. org-insert-export-options-template
  3462. org-table-clean-before-export))
  3463. (org-autoload "org-ascii"
  3464. '(org-export-as-ascii org-export-ascii-preprocess
  3465. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3466. org-export-region-as-ascii))
  3467. (org-autoload "org-latex"
  3468. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3469. org-replace-region-by-latex org-export-region-as-latex
  3470. org-export-as-latex org-export-as-pdf
  3471. org-export-as-pdf-and-open))
  3472. (org-autoload "org-html"
  3473. '(org-export-as-html-and-open
  3474. org-export-as-html-batch org-export-as-html-to-buffer
  3475. org-replace-region-by-html org-export-region-as-html
  3476. org-export-as-html))
  3477. (org-autoload "org-docbook"
  3478. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3479. org-replace-region-by-docbook org-export-region-as-docbook
  3480. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3481. org-export-as-docbook))
  3482. (org-autoload "org-icalendar"
  3483. '(org-export-icalendar-this-file
  3484. org-export-icalendar-all-agenda-files
  3485. org-export-icalendar-combine-agenda-files))
  3486. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3487. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3488. ;; Declare and autoload functions from org-agenda.el
  3489. (eval-and-compile
  3490. (org-autoload "org-agenda"
  3491. '(org-agenda org-agenda-list org-search-view
  3492. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3493. org-diary org-agenda-to-appt
  3494. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3495. ;; Autoload org-remember
  3496. (eval-and-compile
  3497. (org-autoload "org-remember"
  3498. '(org-remember-insinuate org-remember-annotation
  3499. org-remember-apply-template org-remember org-remember-handler)))
  3500. (eval-and-compile
  3501. (org-autoload "org-capture"
  3502. '(org-capture org-capture-insert-template-here
  3503. org-capture-import-remember-templates)))
  3504. ;; Autoload org-clock.el
  3505. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3506. (beg end))
  3507. (declare-function org-clock-update-mode-line "org-clock" ())
  3508. (declare-function org-resolve-clocks "org-clock"
  3509. (&optional also-non-dangling-p prompt last-valid))
  3510. (defvar org-clock-start-time)
  3511. (defvar org-clock-marker (make-marker)
  3512. "Marker recording the last clock-in.")
  3513. (defvar org-clock-hd-marker (make-marker)
  3514. "Marker recording the last clock-in, but the headline position.")
  3515. (defvar org-clock-heading ""
  3516. "The heading of the current clock entry.")
  3517. (defun org-clock-is-active ()
  3518. "Return non-nil if clock is currently running.
  3519. The return value is actually the clock marker."
  3520. (marker-buffer org-clock-marker))
  3521. (eval-and-compile
  3522. (org-autoload
  3523. "org-clock"
  3524. '(org-clock-in org-clock-out org-clock-cancel
  3525. org-clock-goto org-clock-sum org-clock-display
  3526. org-clock-remove-overlays org-clock-report
  3527. org-clocktable-shift org-dblock-write:clocktable
  3528. org-get-clocktable org-resolve-clocks)))
  3529. (defun org-clock-update-time-maybe ()
  3530. "If this is a CLOCK line, update it and return t.
  3531. Otherwise, return nil."
  3532. (interactive)
  3533. (save-excursion
  3534. (beginning-of-line 1)
  3535. (skip-chars-forward " \t")
  3536. (when (looking-at org-clock-string)
  3537. (let ((re (concat "[ \t]*" org-clock-string
  3538. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3539. "\\([ \t]*=>.*\\)?\\)?"))
  3540. ts te h m s neg)
  3541. (cond
  3542. ((not (looking-at re))
  3543. nil)
  3544. ((not (match-end 2))
  3545. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3546. (> org-clock-marker (point))
  3547. (<= org-clock-marker (point-at-eol)))
  3548. ;; The clock is running here
  3549. (setq org-clock-start-time
  3550. (apply 'encode-time
  3551. (org-parse-time-string (match-string 1))))
  3552. (org-clock-update-mode-line)))
  3553. (t
  3554. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3555. (end-of-line 1)
  3556. (setq ts (match-string 1)
  3557. te (match-string 3))
  3558. (setq s (- (org-float-time
  3559. (apply 'encode-time (org-parse-time-string te)))
  3560. (org-float-time
  3561. (apply 'encode-time (org-parse-time-string ts))))
  3562. neg (< s 0)
  3563. s (abs s)
  3564. h (floor (/ s 3600))
  3565. s (- s (* 3600 h))
  3566. m (floor (/ s 60))
  3567. s (- s (* 60 s)))
  3568. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3569. t))))))
  3570. (defun org-check-running-clock ()
  3571. "Check if the current buffer contains the running clock.
  3572. If yes, offer to stop it and to save the buffer with the changes."
  3573. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3574. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3575. (buffer-name))))
  3576. (org-clock-out)
  3577. (when (y-or-n-p "Save changed buffer?")
  3578. (save-buffer))))
  3579. (defun org-clocktable-try-shift (dir n)
  3580. "Check if this line starts a clock table, if yes, shift the time block."
  3581. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3582. (org-clocktable-shift dir n)))
  3583. ;; Autoload org-timer.el
  3584. (eval-and-compile
  3585. (org-autoload
  3586. "org-timer"
  3587. '(org-timer-start org-timer org-timer-item
  3588. org-timer-change-times-in-region
  3589. org-timer-set-timer
  3590. org-timer-reset-timers
  3591. org-timer-show-remaining-time)))
  3592. ;; Autoload org-feed.el
  3593. (eval-and-compile
  3594. (org-autoload
  3595. "org-feed"
  3596. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3597. ;; Autoload org-indent.el
  3598. ;; Define the variable already here, to make sure we have it.
  3599. (defvar org-indent-mode nil
  3600. "Non-nil if Org-Indent mode is enabled.
  3601. Use the command `org-indent-mode' to change this variable.")
  3602. (eval-and-compile
  3603. (org-autoload
  3604. "org-indent"
  3605. '(org-indent-mode)))
  3606. ;; Autoload org-mobile.el
  3607. (eval-and-compile
  3608. (org-autoload
  3609. "org-mobile"
  3610. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3611. ;; Autoload archiving code
  3612. ;; The stuff that is needed for cycling and tags has to be defined here.
  3613. (defgroup org-archive nil
  3614. "Options concerning archiving in Org-mode."
  3615. :tag "Org Archive"
  3616. :group 'org-structure)
  3617. (defcustom org-archive-location "%s_archive::"
  3618. "The location where subtrees should be archived.
  3619. The value of this variable is a string, consisting of two parts,
  3620. separated by a double-colon. The first part is a filename and
  3621. the second part is a headline.
  3622. When the filename is omitted, archiving happens in the same file.
  3623. %s in the filename will be replaced by the current file
  3624. name (without the directory part). Archiving to a different file
  3625. is useful to keep archived entries from contributing to the
  3626. Org-mode Agenda.
  3627. The archived entries will be filed as subtrees of the specified
  3628. headline. When the headline is omitted, the subtrees are simply
  3629. filed away at the end of the file, as top-level entries. Also in
  3630. the heading you can use %s to represent the file name, this can be
  3631. useful when using the same archive for a number of different files.
  3632. Here are a few examples:
  3633. \"%s_archive::\"
  3634. If the current file is Projects.org, archive in file
  3635. Projects.org_archive, as top-level trees. This is the default.
  3636. \"::* Archived Tasks\"
  3637. Archive in the current file, under the top-level headline
  3638. \"* Archived Tasks\".
  3639. \"~/org/archive.org::\"
  3640. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3641. \"~/org/archive.org::* From %s\"
  3642. Archive in file ~/org/archive.org (absolute path), under headlines
  3643. \"From FILENAME\" where file name is the current file name.
  3644. \"basement::** Finished Tasks\"
  3645. Archive in file ./basement (relative path), as level 3 trees
  3646. below the level 2 heading \"** Finished Tasks\".
  3647. You may set this option on a per-file basis by adding to the buffer a
  3648. line like
  3649. #+ARCHIVE: basement::** Finished Tasks
  3650. You may also define it locally for a subtree by setting an ARCHIVE property
  3651. in the entry. If such a property is found in an entry, or anywhere up
  3652. the hierarchy, it will be used."
  3653. :group 'org-archive
  3654. :type 'string)
  3655. (defcustom org-archive-tag "ARCHIVE"
  3656. "The tag that marks a subtree as archived.
  3657. An archived subtree does not open during visibility cycling, and does
  3658. not contribute to the agenda listings.
  3659. After changing this, font-lock must be restarted in the relevant buffers to
  3660. get the proper fontification."
  3661. :group 'org-archive
  3662. :group 'org-keywords
  3663. :type 'string)
  3664. (defcustom org-agenda-skip-archived-trees t
  3665. "Non-nil means the agenda will skip any items located in archived trees.
  3666. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3667. variable is no longer recommended, you should leave it at the value t.
  3668. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3669. :group 'org-archive
  3670. :group 'org-agenda-skip
  3671. :type 'boolean)
  3672. (defcustom org-columns-skip-archived-trees t
  3673. "Non-nil means ignore archived trees when creating column view."
  3674. :group 'org-archive
  3675. :group 'org-properties
  3676. :type 'boolean)
  3677. (defcustom org-cycle-open-archived-trees nil
  3678. "Non-nil means `org-cycle' will open archived trees.
  3679. An archived tree is a tree marked with the tag ARCHIVE.
  3680. When nil, archived trees will stay folded. You can still open them with
  3681. normal outline commands like `show-all', but not with the cycling commands."
  3682. :group 'org-archive
  3683. :group 'org-cycle
  3684. :type 'boolean)
  3685. (defcustom org-sparse-tree-open-archived-trees nil
  3686. "Non-nil means sparse tree construction shows matches in archived trees.
  3687. When nil, matches in these trees are highlighted, but the trees are kept in
  3688. collapsed state."
  3689. :group 'org-archive
  3690. :group 'org-sparse-trees
  3691. :type 'boolean)
  3692. (defun org-cycle-hide-archived-subtrees (state)
  3693. "Re-hide all archived subtrees after a visibility state change."
  3694. (when (and (not org-cycle-open-archived-trees)
  3695. (not (memq state '(overview folded))))
  3696. (save-excursion
  3697. (let* ((globalp (memq state '(contents all)))
  3698. (beg (if globalp (point-min) (point)))
  3699. (end (if globalp (point-max) (org-end-of-subtree t))))
  3700. (org-hide-archived-subtrees beg end)
  3701. (goto-char beg)
  3702. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3703. (message "%s" (substitute-command-keys
  3704. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3705. (defun org-force-cycle-archived ()
  3706. "Cycle subtree even if it is archived."
  3707. (interactive)
  3708. (setq this-command 'org-cycle)
  3709. (let ((org-cycle-open-archived-trees t))
  3710. (call-interactively 'org-cycle)))
  3711. (defun org-hide-archived-subtrees (beg end)
  3712. "Re-hide all archived subtrees after a visibility state change."
  3713. (save-excursion
  3714. (let* ((re (concat ":" org-archive-tag ":")))
  3715. (goto-char beg)
  3716. (while (re-search-forward re end t)
  3717. (when (org-at-heading-p)
  3718. (org-flag-subtree t)
  3719. (org-end-of-subtree t))))))
  3720. (defun org-flag-subtree (flag)
  3721. (save-excursion
  3722. (org-back-to-heading t)
  3723. (outline-end-of-heading)
  3724. (outline-flag-region (point)
  3725. (progn (org-end-of-subtree t) (point))
  3726. flag)))
  3727. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3728. (eval-and-compile
  3729. (org-autoload "org-archive"
  3730. '(org-add-archive-files org-archive-subtree
  3731. org-archive-to-archive-sibling org-toggle-archive-tag
  3732. org-archive-subtree-default
  3733. org-archive-subtree-default-with-confirmation)))
  3734. ;; Autoload Column View Code
  3735. (declare-function org-columns-number-to-string "org-colview")
  3736. (declare-function org-columns-get-format-and-top-level "org-colview")
  3737. (declare-function org-columns-compute "org-colview")
  3738. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3739. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3740. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3741. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3742. ;; Autoload ID code
  3743. (declare-function org-id-store-link "org-id")
  3744. (declare-function org-id-locations-load "org-id")
  3745. (declare-function org-id-locations-save "org-id")
  3746. (defvar org-id-track-globally)
  3747. (org-autoload "org-id"
  3748. '(org-id-get-create org-id-new org-id-copy org-id-get
  3749. org-id-get-with-outline-path-completion
  3750. org-id-get-with-outline-drilling org-id-store-link
  3751. org-id-goto org-id-find org-id-store-link))
  3752. ;; Autoload Plotting Code
  3753. (org-autoload "org-plot"
  3754. '(org-plot/gnuplot))
  3755. ;;; Variables for pre-computed regular expressions, all buffer local
  3756. (defvar org-drawer-regexp nil
  3757. "Matches first line of a hidden block.")
  3758. (make-variable-buffer-local 'org-drawer-regexp)
  3759. (defvar org-todo-regexp nil
  3760. "Matches any of the TODO state keywords.")
  3761. (make-variable-buffer-local 'org-todo-regexp)
  3762. (defvar org-not-done-regexp nil
  3763. "Matches any of the TODO state keywords except the last one.")
  3764. (make-variable-buffer-local 'org-not-done-regexp)
  3765. (defvar org-not-done-heading-regexp nil
  3766. "Matches a TODO headline that is not done.")
  3767. (make-variable-buffer-local 'org-not-done-regexp)
  3768. (defvar org-todo-line-regexp nil
  3769. "Matches a headline and puts TODO state into group 2 if present.")
  3770. (make-variable-buffer-local 'org-todo-line-regexp)
  3771. (defvar org-complex-heading-regexp nil
  3772. "Matches a headline and puts everything into groups:
  3773. group 1: the stars
  3774. group 2: The todo keyword, maybe
  3775. group 3: Priority cookie
  3776. group 4: True headline
  3777. group 5: Tags")
  3778. (make-variable-buffer-local 'org-complex-heading-regexp)
  3779. (defvar org-complex-heading-regexp-format nil
  3780. "Printf format to make regexp to match an exact headline.
  3781. This regexp will match the headline of any node which has the
  3782. exact headline text that is put into the format, but may have any
  3783. TODO state, priority and tags.")
  3784. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3785. (defvar org-todo-line-tags-regexp nil
  3786. "Matches a headline and puts TODO state into group 2 if present.
  3787. Also put tags into group 4 if tags are present.")
  3788. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3789. (defvar org-ds-keyword-length 12
  3790. "Maximum length of the Deadline and SCHEDULED keywords.")
  3791. (make-variable-buffer-local 'org-ds-keyword-length)
  3792. (defvar org-deadline-regexp nil
  3793. "Matches the DEADLINE keyword.")
  3794. (make-variable-buffer-local 'org-deadline-regexp)
  3795. (defvar org-deadline-time-regexp nil
  3796. "Matches the DEADLINE keyword together with a time stamp.")
  3797. (make-variable-buffer-local 'org-deadline-time-regexp)
  3798. (defvar org-deadline-line-regexp nil
  3799. "Matches the DEADLINE keyword and the rest of the line.")
  3800. (make-variable-buffer-local 'org-deadline-line-regexp)
  3801. (defvar org-scheduled-regexp nil
  3802. "Matches the SCHEDULED keyword.")
  3803. (make-variable-buffer-local 'org-scheduled-regexp)
  3804. (defvar org-scheduled-time-regexp nil
  3805. "Matches the SCHEDULED keyword together with a time stamp.")
  3806. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3807. (defvar org-closed-time-regexp nil
  3808. "Matches the CLOSED keyword together with a time stamp.")
  3809. (make-variable-buffer-local 'org-closed-time-regexp)
  3810. (defvar org-keyword-time-regexp nil
  3811. "Matches any of the 4 keywords, together with the time stamp.")
  3812. (make-variable-buffer-local 'org-keyword-time-regexp)
  3813. (defvar org-keyword-time-not-clock-regexp nil
  3814. "Matches any of the 3 keywords, together with the time stamp.")
  3815. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3816. (defvar org-maybe-keyword-time-regexp nil
  3817. "Matches a timestamp, possibly preceded by a keyword.")
  3818. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3819. (defvar org-planning-or-clock-line-re nil
  3820. "Matches a line with planning or clock info.")
  3821. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3822. (defvar org-all-time-keywords nil
  3823. "List of time keywords.")
  3824. (make-variable-buffer-local 'org-all-time-keywords)
  3825. (defconst org-plain-time-of-day-regexp
  3826. (concat
  3827. "\\(\\<[012]?[0-9]"
  3828. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3829. "\\(--?"
  3830. "\\(\\<[012]?[0-9]"
  3831. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3832. "\\)?")
  3833. "Regular expression to match a plain time or time range.
  3834. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3835. groups carry important information:
  3836. 0 the full match
  3837. 1 the first time, range or not
  3838. 8 the second time, if it is a range.")
  3839. (defconst org-plain-time-extension-regexp
  3840. (concat
  3841. "\\(\\<[012]?[0-9]"
  3842. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3843. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3844. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3845. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3846. groups carry important information:
  3847. 0 the full match
  3848. 7 hours of duration
  3849. 9 minutes of duration")
  3850. (defconst org-stamp-time-of-day-regexp
  3851. (concat
  3852. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3853. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3854. "\\(--?"
  3855. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3856. "Regular expression to match a timestamp time or time range.
  3857. After a match, the following groups carry important information:
  3858. 0 the full match
  3859. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3860. 2 the first time, range or not
  3861. 4 the second time, if it is a range.")
  3862. (defconst org-startup-options
  3863. '(("fold" org-startup-folded t)
  3864. ("overview" org-startup-folded t)
  3865. ("nofold" org-startup-folded nil)
  3866. ("showall" org-startup-folded nil)
  3867. ("showeverything" org-startup-folded showeverything)
  3868. ("content" org-startup-folded content)
  3869. ("indent" org-startup-indented t)
  3870. ("noindent" org-startup-indented nil)
  3871. ("hidestars" org-hide-leading-stars t)
  3872. ("showstars" org-hide-leading-stars nil)
  3873. ("odd" org-odd-levels-only t)
  3874. ("oddeven" org-odd-levels-only nil)
  3875. ("align" org-startup-align-all-tables t)
  3876. ("noalign" org-startup-align-all-tables nil)
  3877. ("inlineimages" org-startup-with-inline-images t)
  3878. ("noinlineimages" org-startup-with-inline-images nil)
  3879. ("customtime" org-display-custom-times t)
  3880. ("logdone" org-log-done time)
  3881. ("lognotedone" org-log-done note)
  3882. ("nologdone" org-log-done nil)
  3883. ("lognoteclock-out" org-log-note-clock-out t)
  3884. ("nolognoteclock-out" org-log-note-clock-out nil)
  3885. ("logrepeat" org-log-repeat state)
  3886. ("lognoterepeat" org-log-repeat note)
  3887. ("nologrepeat" org-log-repeat nil)
  3888. ("logreschedule" org-log-reschedule time)
  3889. ("lognotereschedule" org-log-reschedule note)
  3890. ("nologreschedule" org-log-reschedule nil)
  3891. ("logredeadline" org-log-redeadline time)
  3892. ("lognoteredeadline" org-log-redeadline note)
  3893. ("nologredeadline" org-log-redeadline nil)
  3894. ("logrefile" org-log-refile time)
  3895. ("lognoterefile" org-log-refile note)
  3896. ("nologrefile" org-log-refile nil)
  3897. ("fninline" org-footnote-define-inline t)
  3898. ("nofninline" org-footnote-define-inline nil)
  3899. ("fnlocal" org-footnote-section nil)
  3900. ("fnauto" org-footnote-auto-label t)
  3901. ("fnprompt" org-footnote-auto-label nil)
  3902. ("fnconfirm" org-footnote-auto-label confirm)
  3903. ("fnplain" org-footnote-auto-label plain)
  3904. ("fnadjust" org-footnote-auto-adjust t)
  3905. ("nofnadjust" org-footnote-auto-adjust nil)
  3906. ("constcgs" constants-unit-system cgs)
  3907. ("constSI" constants-unit-system SI)
  3908. ("noptag" org-tag-persistent-alist nil)
  3909. ("hideblocks" org-hide-block-startup t)
  3910. ("nohideblocks" org-hide-block-startup nil)
  3911. ("beamer" org-startup-with-beamer-mode t)
  3912. ("entitiespretty" org-pretty-entities t)
  3913. ("entitiesplain" org-pretty-entities nil))
  3914. "Variable associated with STARTUP options for org-mode.
  3915. Each element is a list of three items: The startup options as written
  3916. in the #+STARTUP line, the corresponding variable, and the value to
  3917. set this variable to if the option is found. An optional forth element PUSH
  3918. means to push this value onto the list in the variable.")
  3919. (defun org-update-property-plist (key val props)
  3920. "Update PROPS with KEY and VAL."
  3921. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3922. (key (if appending (substring key 0 (- (length key) 1)) key))
  3923. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3924. (previous (cdr (assoc key props))))
  3925. (if appending
  3926. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3927. (cons (cons key val) remainder))))
  3928. (defconst org-block-regexp
  3929. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3930. "Regular expression for hiding blocks.")
  3931. (defconst org-heading-keyword-regexp-format
  3932. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3933. "Printf format for a regexp matching an headline with some keyword.
  3934. This regexp will match the headline of any node which has the
  3935. exact keyword that is put into the format. The keyword isn't in
  3936. any group by default, but the stars and the body are.")
  3937. (defconst org-heading-keyword-maybe-regexp-format
  3938. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3939. "Printf format for a regexp matching an headline, possibly with some keyword.
  3940. This regexp can match any headline with the specified keyword, or
  3941. without a keyword. The keyword isn't in any group by default,
  3942. but the stars and the body are.")
  3943. (defun org-set-regexps-and-options ()
  3944. "Precompute regular expressions for current buffer."
  3945. (when (eq major-mode 'org-mode)
  3946. (org-set-local 'org-todo-kwd-alist nil)
  3947. (org-set-local 'org-todo-key-alist nil)
  3948. (org-set-local 'org-todo-key-trigger nil)
  3949. (org-set-local 'org-todo-keywords-1 nil)
  3950. (org-set-local 'org-done-keywords nil)
  3951. (org-set-local 'org-todo-heads nil)
  3952. (org-set-local 'org-todo-sets nil)
  3953. (org-set-local 'org-todo-log-states nil)
  3954. (org-set-local 'org-file-properties nil)
  3955. (org-set-local 'org-file-tags nil)
  3956. (let ((re (org-make-options-regexp
  3957. '("CATEGORY" "TODO" "COLUMNS"
  3958. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3959. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3960. "OPTIONS")
  3961. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3962. (splitre "[ \t]+")
  3963. (scripts org-use-sub-superscripts)
  3964. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3965. tail sep kws1 prio props ftags drawers beamer-p
  3966. ext-setup-or-nil setup-contents (start 0))
  3967. (save-excursion
  3968. (save-restriction
  3969. (widen)
  3970. (goto-char (point-min))
  3971. (while (or (and ext-setup-or-nil
  3972. (string-match re ext-setup-or-nil start)
  3973. (setq start (match-end 0)))
  3974. (and (setq ext-setup-or-nil nil start 0)
  3975. (re-search-forward re nil t)))
  3976. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3977. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3978. (if (stringp value) (setq value (org-trim value)))
  3979. (cond
  3980. ((equal key "CATEGORY")
  3981. (setq cat value))
  3982. ((member key '("SEQ_TODO" "TODO"))
  3983. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3984. ((equal key "TYP_TODO")
  3985. (push (cons 'type (org-split-string value splitre)) kwds))
  3986. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3987. ;; general TODO-like setup
  3988. (push (cons (intern (downcase (match-string 1 key)))
  3989. (org-split-string value splitre)) kwds))
  3990. ((equal key "TAGS")
  3991. (setq tags (append tags (if tags '("\\n") nil)
  3992. (org-split-string value splitre))))
  3993. ((equal key "COLUMNS")
  3994. (org-set-local 'org-columns-default-format value))
  3995. ((equal key "LINK")
  3996. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3997. (push (cons (match-string 1 value)
  3998. (org-trim (match-string 2 value)))
  3999. links)))
  4000. ((equal key "PRIORITIES")
  4001. (setq prio (org-split-string value " +")))
  4002. ((equal key "PROPERTY")
  4003. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4004. (setq props (org-update-property-plist (match-string 1 value)
  4005. (match-string 2 value)
  4006. props))))
  4007. ((equal key "FILETAGS")
  4008. (when (string-match "\\S-" value)
  4009. (setq ftags
  4010. (append
  4011. ftags
  4012. (apply 'append
  4013. (mapcar (lambda (x) (org-split-string x ":"))
  4014. (org-split-string value)))))))
  4015. ((equal key "DRAWERS")
  4016. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4017. ((equal key "CONSTANTS")
  4018. (setq const (append const (org-split-string value splitre))))
  4019. ((equal key "STARTUP")
  4020. (let ((opts (org-split-string value splitre))
  4021. l var val)
  4022. (while (setq l (pop opts))
  4023. (when (setq l (assoc l org-startup-options))
  4024. (setq var (nth 1 l) val (nth 2 l))
  4025. (if (not (nth 3 l))
  4026. (set (make-local-variable var) val)
  4027. (if (not (listp (symbol-value var)))
  4028. (set (make-local-variable var) nil))
  4029. (set (make-local-variable var) (symbol-value var))
  4030. (add-to-list var val))))))
  4031. ((equal key "ARCHIVE")
  4032. (setq arch value)
  4033. (remove-text-properties 0 (length arch)
  4034. '(face t fontified t) arch))
  4035. ((equal key "LATEX_CLASS")
  4036. (setq beamer-p (equal value "beamer")))
  4037. ((equal key "OPTIONS")
  4038. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4039. (setq scripts (read (match-string 2 value)))))
  4040. ((equal key "SETUPFILE")
  4041. (setq setup-contents (org-file-contents
  4042. (expand-file-name
  4043. (org-remove-double-quotes value))
  4044. 'noerror))
  4045. (if (not ext-setup-or-nil)
  4046. (setq ext-setup-or-nil setup-contents start 0)
  4047. (setq ext-setup-or-nil
  4048. (concat (substring ext-setup-or-nil 0 start)
  4049. "\n" setup-contents "\n"
  4050. (substring ext-setup-or-nil start)))))))
  4051. ;; search for property blocks
  4052. (goto-char (point-min))
  4053. (while (re-search-forward org-block-regexp nil t)
  4054. (when (equal "PROPERTY" (upcase (match-string 1)))
  4055. (setq value (replace-regexp-in-string
  4056. "[\n\r]" " " (match-string 4)))
  4057. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4058. (setq props (org-update-property-plist (match-string 1 value)
  4059. (match-string 2 value)
  4060. props)))))))
  4061. (org-set-local 'org-use-sub-superscripts scripts)
  4062. (when cat
  4063. (org-set-local 'org-category (intern cat))
  4064. (push (cons "CATEGORY" cat) props))
  4065. (when prio
  4066. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4067. (setq prio (mapcar 'string-to-char prio))
  4068. (org-set-local 'org-highest-priority (nth 0 prio))
  4069. (org-set-local 'org-lowest-priority (nth 1 prio))
  4070. (org-set-local 'org-default-priority (nth 2 prio)))
  4071. (and props (org-set-local 'org-file-properties (nreverse props)))
  4072. (and ftags (org-set-local 'org-file-tags
  4073. (mapcar 'org-add-prop-inherited ftags)))
  4074. (and drawers (org-set-local 'org-drawers drawers))
  4075. (and arch (org-set-local 'org-archive-location arch))
  4076. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4077. ;; Process the TODO keywords
  4078. (unless kwds
  4079. ;; Use the global values as if they had been given locally.
  4080. (setq kwds (default-value 'org-todo-keywords))
  4081. (if (stringp (car kwds))
  4082. (setq kwds (list (cons org-todo-interpretation
  4083. (default-value 'org-todo-keywords)))))
  4084. (setq kwds (reverse kwds)))
  4085. (setq kwds (nreverse kwds))
  4086. (let (inter kws kw)
  4087. (while (setq kws (pop kwds))
  4088. (let ((kws (or
  4089. (run-hook-with-args-until-success
  4090. 'org-todo-setup-filter-hook kws)
  4091. kws)))
  4092. (setq inter (pop kws) sep (member "|" kws)
  4093. kws0 (delete "|" (copy-sequence kws))
  4094. kwsa nil
  4095. kws1 (mapcar
  4096. (lambda (x)
  4097. ;; 1 2
  4098. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4099. (progn
  4100. (setq kw (match-string 1 x)
  4101. key (and (match-end 2) (match-string 2 x))
  4102. log (org-extract-log-state-settings x))
  4103. (push (cons kw (and key (string-to-char key))) kwsa)
  4104. (and log (push log org-todo-log-states))
  4105. kw)
  4106. (error "Invalid TODO keyword %s" x)))
  4107. kws0)
  4108. kwsa (if kwsa (append '((:startgroup))
  4109. (nreverse kwsa)
  4110. '((:endgroup))))
  4111. hw (car kws1)
  4112. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4113. tail (list inter hw (car dws) (org-last dws))))
  4114. (add-to-list 'org-todo-heads hw 'append)
  4115. (push kws1 org-todo-sets)
  4116. (setq org-done-keywords (append org-done-keywords dws nil))
  4117. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4118. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4119. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4120. (setq org-todo-sets (nreverse org-todo-sets)
  4121. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4122. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4123. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4124. ;; Process the constants
  4125. (when const
  4126. (let (e cst)
  4127. (while (setq e (pop const))
  4128. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4129. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4130. (setq org-table-formula-constants-local cst)))
  4131. ;; Process the tags.
  4132. (when tags
  4133. (let (e tgs)
  4134. (while (setq e (pop tags))
  4135. (cond
  4136. ((equal e "{") (push '(:startgroup) tgs))
  4137. ((equal e "}") (push '(:endgroup) tgs))
  4138. ((equal e "\\n") (push '(:newline) tgs))
  4139. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4140. (push (cons (match-string 1 e)
  4141. (string-to-char (match-string 2 e)))
  4142. tgs))
  4143. (t (push (list e) tgs))))
  4144. (org-set-local 'org-tag-alist nil)
  4145. (while (setq e (pop tgs))
  4146. (or (and (stringp (car e))
  4147. (assoc (car e) org-tag-alist))
  4148. (push e org-tag-alist)))))
  4149. ;; Compute the regular expressions and other local variables.
  4150. ;; Using `org-outline-regexp-bol' would complicate them much,
  4151. ;; because of the fixed white space at the end of that string.
  4152. (if (not org-done-keywords)
  4153. (setq org-done-keywords (and org-todo-keywords-1
  4154. (list (org-last org-todo-keywords-1)))))
  4155. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4156. (length org-scheduled-string)
  4157. (length org-clock-string)
  4158. (length org-closed-string)))
  4159. org-drawer-regexp
  4160. (concat "^[ \t]*:\\("
  4161. (mapconcat 'regexp-quote org-drawers "\\|")
  4162. "\\):[ \t]*$")
  4163. org-not-done-keywords
  4164. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4165. org-todo-regexp
  4166. (concat "\\("
  4167. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4168. "\\)")
  4169. org-not-done-regexp
  4170. (concat "\\("
  4171. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4172. "\\)")
  4173. org-not-done-heading-regexp
  4174. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4175. org-todo-line-regexp
  4176. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4177. org-complex-heading-regexp
  4178. (concat "^\\(\\*+\\)"
  4179. "\\(?: +" org-todo-regexp "\\)?"
  4180. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4181. "\\(?: +\\(.*?\\)\\)?"
  4182. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4183. "[ \t]*$")
  4184. org-complex-heading-regexp-format
  4185. (concat "^\\(\\*+\\)"
  4186. "\\(?: +" org-todo-regexp "\\)?"
  4187. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4188. "\\(?: +"
  4189. ;; Stats cookies can be stuck to body.
  4190. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4191. "\\(%s\\)"
  4192. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4193. "\\)"
  4194. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4195. "[ \t]*$")
  4196. org-todo-line-tags-regexp
  4197. (concat "^\\(\\*+\\)"
  4198. "\\(?: +" org-todo-regexp "\\)?"
  4199. "\\(?: +\\(.*?\\)\\)?"
  4200. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4201. "[ \t]*$")
  4202. org-deadline-regexp (concat "\\<" org-deadline-string)
  4203. org-deadline-time-regexp
  4204. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4205. org-deadline-line-regexp
  4206. (concat "\\<\\(" org-deadline-string "\\).*")
  4207. org-scheduled-regexp
  4208. (concat "\\<" org-scheduled-string)
  4209. org-scheduled-time-regexp
  4210. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4211. org-closed-time-regexp
  4212. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4213. org-keyword-time-regexp
  4214. (concat "\\<\\(" org-scheduled-string
  4215. "\\|" org-deadline-string
  4216. "\\|" org-closed-string
  4217. "\\|" org-clock-string "\\)"
  4218. " *[[<]\\([^]>]+\\)[]>]")
  4219. org-keyword-time-not-clock-regexp
  4220. (concat "\\<\\(" org-scheduled-string
  4221. "\\|" org-deadline-string
  4222. "\\|" org-closed-string
  4223. "\\)"
  4224. " *[[<]\\([^]>]+\\)[]>]")
  4225. org-maybe-keyword-time-regexp
  4226. (concat "\\(\\<\\(" org-scheduled-string
  4227. "\\|" org-deadline-string
  4228. "\\|" org-closed-string
  4229. "\\|" org-clock-string "\\)\\)?"
  4230. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4231. org-planning-or-clock-line-re
  4232. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4233. "\\|" org-deadline-string
  4234. "\\|" org-closed-string "\\|" org-clock-string
  4235. "\\)\\>\\)")
  4236. org-all-time-keywords
  4237. (mapcar (lambda (w) (substring w 0 -1))
  4238. (list org-scheduled-string org-deadline-string
  4239. org-clock-string org-closed-string))
  4240. )
  4241. (org-compute-latex-and-specials-regexp)
  4242. (org-set-font-lock-defaults))))
  4243. (defun org-file-contents (file &optional noerror)
  4244. "Return the contents of FILE, as a string."
  4245. (if (or (not file)
  4246. (not (file-readable-p file)))
  4247. (if noerror
  4248. (progn
  4249. (message "Cannot read file \"%s\"" file)
  4250. (ding) (sit-for 2)
  4251. "")
  4252. (error "Cannot read file \"%s\"" file))
  4253. (with-temp-buffer
  4254. (insert-file-contents file)
  4255. (buffer-string))))
  4256. (defun org-extract-log-state-settings (x)
  4257. "Extract the log state setting from a TODO keyword string.
  4258. This will extract info from a string like \"WAIT(w@/!)\"."
  4259. (let (kw key log1 log2)
  4260. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4261. (setq kw (match-string 1 x)
  4262. key (and (match-end 2) (match-string 2 x))
  4263. log1 (and (match-end 3) (match-string 3 x))
  4264. log2 (and (match-end 4) (match-string 4 x)))
  4265. (and (or log1 log2)
  4266. (list kw
  4267. (and log1 (if (equal log1 "!") 'time 'note))
  4268. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4269. (defun org-remove-keyword-keys (list)
  4270. "Remove a pair of parenthesis at the end of each string in LIST."
  4271. (mapcar (lambda (x)
  4272. (if (string-match "(.*)$" x)
  4273. (substring x 0 (match-beginning 0))
  4274. x))
  4275. list))
  4276. (defun org-assign-fast-keys (alist)
  4277. "Assign fast keys to a keyword-key alist.
  4278. Respect keys that are already there."
  4279. (let (new e (alt ?0))
  4280. (while (setq e (pop alist))
  4281. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4282. (cdr e)) ;; Key already assigned.
  4283. (push e new)
  4284. (let ((clist (string-to-list (downcase (car e))))
  4285. (used (append new alist)))
  4286. (when (= (car clist) ?@)
  4287. (pop clist))
  4288. (while (and clist (rassoc (car clist) used))
  4289. (pop clist))
  4290. (unless clist
  4291. (while (rassoc alt used)
  4292. (incf alt)))
  4293. (push (cons (car e) (or (car clist) alt)) new))))
  4294. (nreverse new)))
  4295. ;;; Some variables used in various places
  4296. (defvar org-window-configuration nil
  4297. "Used in various places to store a window configuration.")
  4298. (defvar org-selected-window nil
  4299. "Used in various places to store a window configuration.")
  4300. (defvar org-finish-function nil
  4301. "Function to be called when `C-c C-c' is used.
  4302. This is for getting out of special buffers like remember.")
  4303. ;; FIXME: Occasionally check by commenting these, to make sure
  4304. ;; no other functions uses these, forgetting to let-bind them.
  4305. (defvar entry)
  4306. (defvar last-state)
  4307. (defvar date)
  4308. ;; Defined somewhere in this file, but used before definition.
  4309. (defvar org-entities) ;; defined in org-entities.el
  4310. (defvar org-struct-menu)
  4311. (defvar org-org-menu)
  4312. (defvar org-tbl-menu)
  4313. ;;;; Define the Org-mode
  4314. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4315. (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"))
  4316. ;; We use a before-change function to check if a table might need
  4317. ;; an update.
  4318. (defvar org-table-may-need-update t
  4319. "Indicates that a table might need an update.
  4320. This variable is set by `org-before-change-function'.
  4321. `org-table-align' sets it back to nil.")
  4322. (defun org-before-change-function (beg end)
  4323. "Every change indicates that a table might need an update."
  4324. (setq org-table-may-need-update t))
  4325. (defvar org-mode-map)
  4326. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4327. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4328. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4329. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4330. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4331. (defvar org-table-buffer-is-an nil)
  4332. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4333. ;; some places -- e.g. see the macro org-with-limited-levels.
  4334. ;;
  4335. ;; In Org buffers, the value of `outline-regexp' is that of
  4336. ;; `org-outline-regexp'. The only function still directly relying on
  4337. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4338. ;; job when `orgstruct-mode' is active.
  4339. (defvar org-outline-regexp "\\*+ "
  4340. "Regexp to match Org headlines.")
  4341. (defconst org-outline-regexp-bol "^\\*+ "
  4342. "Regexp to match Org headlines.
  4343. This is similar to `org-outline-regexp' but additionally makes
  4344. sure that we are at the beginning of the line.")
  4345. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4346. "Matches an headline, putting stars and text into groups.
  4347. Stars are put in group 1 and the trimmed body in group 2.")
  4348. ;;;###autoload
  4349. (define-derived-mode org-mode outline-mode "Org"
  4350. "Outline-based notes management and organizer, alias
  4351. \"Carsten's outline-mode for keeping track of everything.\"
  4352. Org-mode develops organizational tasks around a NOTES file which
  4353. contains information about projects as plain text. Org-mode is
  4354. implemented on top of outline-mode, which is ideal to keep the content
  4355. of large files well structured. It supports ToDo items, deadlines and
  4356. time stamps, which magically appear in the diary listing of the Emacs
  4357. calendar. Tables are easily created with a built-in table editor.
  4358. Plain text URL-like links connect to websites, emails (VM), Usenet
  4359. messages (Gnus), BBDB entries, and any files related to the project.
  4360. For printing and sharing of notes, an Org-mode file (or a part of it)
  4361. can be exported as a structured ASCII or HTML file.
  4362. The following commands are available:
  4363. \\{org-mode-map}"
  4364. ;; Get rid of Outline menus, they are not needed
  4365. ;; Need to do this here because define-derived-mode sets up
  4366. ;; the keymap so late. Still, it is a waste to call this each time
  4367. ;; we switch another buffer into org-mode.
  4368. (if (featurep 'xemacs)
  4369. (when (boundp 'outline-mode-menu-heading)
  4370. ;; Assume this is Greg's port, it uses easymenu
  4371. (easy-menu-remove outline-mode-menu-heading)
  4372. (easy-menu-remove outline-mode-menu-show)
  4373. (easy-menu-remove outline-mode-menu-hide))
  4374. (define-key org-mode-map [menu-bar headings] 'undefined)
  4375. (define-key org-mode-map [menu-bar hide] 'undefined)
  4376. (define-key org-mode-map [menu-bar show] 'undefined))
  4377. (org-load-modules-maybe)
  4378. (easy-menu-add org-org-menu)
  4379. (easy-menu-add org-tbl-menu)
  4380. (org-install-agenda-files-menu)
  4381. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4382. (add-to-invisibility-spec '(org-cwidth))
  4383. (add-to-invisibility-spec '(org-hide-block . t))
  4384. (when (featurep 'xemacs)
  4385. (org-set-local 'line-move-ignore-invisible t))
  4386. (org-set-local 'outline-regexp org-outline-regexp)
  4387. (org-set-local 'outline-level 'org-outline-level)
  4388. (setq bidi-paragraph-direction 'left-to-right)
  4389. (when (and org-ellipsis
  4390. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4391. (fboundp 'make-glyph-code))
  4392. (unless org-display-table
  4393. (setq org-display-table (make-display-table)))
  4394. (set-display-table-slot
  4395. org-display-table 4
  4396. (vconcat (mapcar
  4397. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4398. org-ellipsis)))
  4399. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4400. (setq buffer-display-table org-display-table))
  4401. (org-set-regexps-and-options)
  4402. (when (and org-tag-faces (not org-tags-special-faces-re))
  4403. ;; tag faces set outside customize.... force initialization.
  4404. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4405. ;; Calc embedded
  4406. (org-set-local 'calc-embedded-open-mode "# ")
  4407. (modify-syntax-entry ?@ "w")
  4408. (if org-startup-truncated (setq truncate-lines t))
  4409. (org-set-local 'font-lock-unfontify-region-function
  4410. 'org-unfontify-region)
  4411. ;; Activate before-change-function
  4412. (org-set-local 'org-table-may-need-update t)
  4413. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4414. 'local)
  4415. ;; Check for running clock before killing a buffer
  4416. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4417. ;; Paragraphs and auto-filling
  4418. (org-set-autofill-regexps)
  4419. (setq indent-line-function 'org-indent-line-function)
  4420. (org-update-radio-target-regexp)
  4421. ;; Beginning/end of defun
  4422. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4423. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4424. ;; Next error for sparse trees
  4425. (org-set-local 'next-error-function 'org-occur-next-match)
  4426. ;; Make sure dependence stuff works reliably, even for users who set it
  4427. ;; too late :-(
  4428. (if org-enforce-todo-dependencies
  4429. (add-hook 'org-blocker-hook
  4430. 'org-block-todo-from-children-or-siblings-or-parent)
  4431. (remove-hook 'org-blocker-hook
  4432. 'org-block-todo-from-children-or-siblings-or-parent))
  4433. (if org-enforce-todo-checkbox-dependencies
  4434. (add-hook 'org-blocker-hook
  4435. 'org-block-todo-from-checkboxes)
  4436. (remove-hook 'org-blocker-hook
  4437. 'org-block-todo-from-checkboxes))
  4438. ;; Comment characters
  4439. (org-set-local 'comment-start "#")
  4440. (org-set-local 'comment-padding " ")
  4441. ;; Align options lines
  4442. (org-set-local
  4443. 'align-mode-rules-list
  4444. '((org-in-buffer-settings
  4445. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4446. (modes . '(org-mode)))))
  4447. ;; Imenu
  4448. (org-set-local 'imenu-create-index-function
  4449. 'org-imenu-get-tree)
  4450. ;; Make isearch reveal context
  4451. (if (or (featurep 'xemacs)
  4452. (not (boundp 'outline-isearch-open-invisible-function)))
  4453. ;; Emacs 21 and XEmacs make use of the hook
  4454. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4455. ;; Emacs 22 deals with this through a special variable
  4456. (org-set-local 'outline-isearch-open-invisible-function
  4457. (lambda (&rest ignore) (org-show-context 'isearch))))
  4458. ;; Turn on org-beamer-mode?
  4459. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4460. ;; Setup the pcomplete hooks
  4461. (set (make-local-variable 'pcomplete-command-completion-function)
  4462. 'org-pcomplete-initial)
  4463. (set (make-local-variable 'pcomplete-command-name-function)
  4464. 'org-command-at-point)
  4465. (set (make-local-variable 'pcomplete-default-completion-function)
  4466. 'ignore)
  4467. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4468. 'org-parse-arguments)
  4469. (set (make-local-variable 'pcomplete-termination-string) "")
  4470. (set (make-local-variable 'face-remapping-alist)
  4471. '((default org-default)))
  4472. ;; If empty file that did not turn on org-mode automatically, make it to.
  4473. (if (and org-insert-mode-line-in-empty-file
  4474. (org-called-interactively-p 'any)
  4475. (= (point-min) (point-max)))
  4476. (insert "# -*- mode: org -*-\n\n"))
  4477. (unless org-inhibit-startup
  4478. (when org-startup-align-all-tables
  4479. (let ((bmp (buffer-modified-p)))
  4480. (org-table-map-tables 'org-table-align 'quietly)
  4481. (set-buffer-modified-p bmp)))
  4482. (when org-startup-with-inline-images
  4483. (org-display-inline-images))
  4484. (when org-startup-indented
  4485. (require 'org-indent)
  4486. (org-indent-mode 1))
  4487. (unless org-inhibit-startup-visibility-stuff
  4488. (org-set-startup-visibility))))
  4489. (when (fboundp 'abbrev-table-put)
  4490. (abbrev-table-put org-mode-abbrev-table
  4491. :parents (list text-mode-abbrev-table)))
  4492. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4493. (defun org-current-time ()
  4494. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4495. (if (> (car org-time-stamp-rounding-minutes) 1)
  4496. (let ((r (car org-time-stamp-rounding-minutes))
  4497. (time (decode-time)))
  4498. (apply 'encode-time
  4499. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4500. (nthcdr 2 time))))
  4501. (current-time)))
  4502. (defun org-today ()
  4503. "Return today date, considering `org-extend-today-until'."
  4504. (time-to-days
  4505. (time-subtract (current-time)
  4506. (list 0 (* 3600 org-extend-today-until) 0))))
  4507. ;;;; Font-Lock stuff, including the activators
  4508. (defvar org-mouse-map (make-sparse-keymap))
  4509. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4510. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4511. (when org-mouse-1-follows-link
  4512. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4513. (when org-tab-follows-link
  4514. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4515. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4516. (require 'font-lock)
  4517. (defconst org-non-link-chars "]\t\n\r<>")
  4518. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4519. "shell" "elisp" "doi" "message"))
  4520. (defvar org-link-types-re nil
  4521. "Matches a link that has a url-like prefix like \"http:\"")
  4522. (defvar org-link-re-with-space nil
  4523. "Matches a link with spaces, optional angular brackets around it.")
  4524. (defvar org-link-re-with-space2 nil
  4525. "Matches a link with spaces, optional angular brackets around it.")
  4526. (defvar org-link-re-with-space3 nil
  4527. "Matches a link with spaces, only for internal part in bracket links.")
  4528. (defvar org-angle-link-re nil
  4529. "Matches link with angular brackets, spaces are allowed.")
  4530. (defvar org-plain-link-re nil
  4531. "Matches plain link, without spaces.")
  4532. (defvar org-bracket-link-regexp nil
  4533. "Matches a link in double brackets.")
  4534. (defvar org-bracket-link-analytic-regexp nil
  4535. "Regular expression used to analyze links.
  4536. Here is what the match groups contain after a match:
  4537. 1: http:
  4538. 2: http
  4539. 3: path
  4540. 4: [desc]
  4541. 5: desc")
  4542. (defvar org-bracket-link-analytic-regexp++ nil
  4543. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4544. (defvar org-any-link-re nil
  4545. "Regular expression matching any link.")
  4546. (defcustom org-match-sexp-depth 3
  4547. "Number of stacked braces for sub/superscript matching.
  4548. This has to be set before loading org.el to be effective."
  4549. :group 'org-export-translation ; ??????????????????????????/
  4550. :type 'integer)
  4551. (defun org-create-multibrace-regexp (left right n)
  4552. "Create a regular expression which will match a balanced sexp.
  4553. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4554. as single character strings.
  4555. The regexp returned will match the entire expression including the
  4556. delimiters. It will also define a single group which contains the
  4557. match except for the outermost delimiters. The maximum depth of
  4558. stacked delimiters is N. Escaping delimiters is not possible."
  4559. (let* ((nothing (concat "[^" left right "]*?"))
  4560. (or "\\|")
  4561. (re nothing)
  4562. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4563. (while (> n 1)
  4564. (setq n (1- n)
  4565. re (concat re or next)
  4566. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4567. (concat left "\\(" re "\\)" right)))
  4568. (defvar org-match-substring-regexp
  4569. (concat
  4570. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4571. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4572. "\\|"
  4573. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4574. "\\|"
  4575. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4576. "The regular expression matching a sub- or superscript.")
  4577. (defvar org-match-substring-with-braces-regexp
  4578. (concat
  4579. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4580. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4581. "\\)")
  4582. "The regular expression matching a sub- or superscript, forcing braces.")
  4583. (defun org-make-link-regexps ()
  4584. "Update the link regular expressions.
  4585. This should be called after the variable `org-link-types' has changed."
  4586. (setq org-link-types-re
  4587. (concat
  4588. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4589. org-link-re-with-space
  4590. (concat
  4591. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4592. "\\([^" org-non-link-chars " ]"
  4593. "[^" org-non-link-chars "]*"
  4594. "[^" org-non-link-chars " ]\\)>?")
  4595. org-link-re-with-space2
  4596. (concat
  4597. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4598. "\\([^" org-non-link-chars " ]"
  4599. "[^\t\n\r]*"
  4600. "[^" org-non-link-chars " ]\\)>?")
  4601. org-link-re-with-space3
  4602. (concat
  4603. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4604. "\\([^" org-non-link-chars " ]"
  4605. "[^\t\n\r]*\\)")
  4606. org-angle-link-re
  4607. (concat
  4608. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4609. "\\([^" org-non-link-chars " ]"
  4610. "[^" org-non-link-chars "]*"
  4611. "\\)>")
  4612. org-plain-link-re
  4613. (concat
  4614. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4615. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4616. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4617. org-bracket-link-regexp
  4618. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4619. org-bracket-link-analytic-regexp
  4620. (concat
  4621. "\\[\\["
  4622. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4623. "\\([^]]+\\)"
  4624. "\\]"
  4625. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4626. "\\]")
  4627. org-bracket-link-analytic-regexp++
  4628. (concat
  4629. "\\[\\["
  4630. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4631. "\\([^]]+\\)"
  4632. "\\]"
  4633. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4634. "\\]")
  4635. org-any-link-re
  4636. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4637. org-angle-link-re "\\)\\|\\("
  4638. org-plain-link-re "\\)")))
  4639. (org-make-link-regexps)
  4640. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4641. "Regular expression for fast time stamp matching.")
  4642. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4643. "Regular expression for fast time stamp matching.")
  4644. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4645. "Regular expression matching time strings for analysis.
  4646. This one does not require the space after the date, so it can be used
  4647. on a string that terminates immediately after the date.")
  4648. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4649. "Regular expression matching time strings for analysis.")
  4650. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4651. "Regular expression matching time stamps, with groups.")
  4652. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4653. "Regular expression matching time stamps (also [..]), with groups.")
  4654. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4655. "Regular expression matching a time stamp range.")
  4656. (defconst org-tr-regexp-both
  4657. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4658. "Regular expression matching a time stamp range.")
  4659. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4660. org-ts-regexp "\\)?")
  4661. "Regular expression matching a time stamp or time stamp range.")
  4662. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4663. org-ts-regexp-both "\\)?")
  4664. "Regular expression matching a time stamp or time stamp range.
  4665. The time stamps may be either active or inactive.")
  4666. (defvar org-emph-face nil)
  4667. (defun org-do-emphasis-faces (limit)
  4668. "Run through the buffer and add overlays to emphasized strings."
  4669. (let (rtn a)
  4670. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4671. (if (not (= (char-after (match-beginning 3))
  4672. (char-after (match-beginning 4))))
  4673. (progn
  4674. (setq rtn t)
  4675. (setq a (assoc (match-string 3) org-emphasis-alist))
  4676. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4677. 'face
  4678. (nth 1 a))
  4679. (and (nth 4 a)
  4680. (org-remove-flyspell-overlays-in
  4681. (match-beginning 0) (match-end 0)))
  4682. (add-text-properties (match-beginning 2) (match-end 2)
  4683. '(font-lock-multiline t org-emphasis t))
  4684. (when org-hide-emphasis-markers
  4685. (add-text-properties (match-end 4) (match-beginning 5)
  4686. '(invisible org-link))
  4687. (add-text-properties (match-beginning 3) (match-end 3)
  4688. '(invisible org-link)))))
  4689. (backward-char 1))
  4690. rtn))
  4691. (defun org-emphasize (&optional char)
  4692. "Insert or change an emphasis, i.e. a font like bold or italic.
  4693. If there is an active region, change that region to a new emphasis.
  4694. If there is no region, just insert the marker characters and position
  4695. the cursor between them.
  4696. CHAR should be either the marker character, or the first character of the
  4697. HTML tag associated with that emphasis. If CHAR is a space, the means
  4698. to remove the emphasis of the selected region.
  4699. If char is not given (for example in an interactive call) it
  4700. will be prompted for."
  4701. (interactive)
  4702. (let ((eal org-emphasis-alist) e det
  4703. (erc org-emphasis-regexp-components)
  4704. (prompt "")
  4705. (string "") beg end move tag c s)
  4706. (if (org-region-active-p)
  4707. (setq beg (region-beginning) end (region-end)
  4708. string (buffer-substring beg end))
  4709. (setq move t))
  4710. (while (setq e (pop eal))
  4711. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4712. c (aref tag 0))
  4713. (push (cons c (string-to-char (car e))) det)
  4714. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4715. (substring tag 1)))))
  4716. (setq det (nreverse det))
  4717. (unless char
  4718. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4719. (setq char (read-char-exclusive)))
  4720. (setq char (or (cdr (assoc char det)) char))
  4721. (if (equal char ?\ )
  4722. (setq s "" move nil)
  4723. (unless (assoc (char-to-string char) org-emphasis-alist)
  4724. (error "No such emphasis marker: \"%c\"" char))
  4725. (setq s (char-to-string char)))
  4726. (while (and (> (length string) 1)
  4727. (equal (substring string 0 1) (substring string -1))
  4728. (assoc (substring string 0 1) org-emphasis-alist))
  4729. (setq string (substring string 1 -1)))
  4730. (setq string (concat s string s))
  4731. (if beg (delete-region beg end))
  4732. (unless (or (bolp)
  4733. (string-match (concat "[" (nth 0 erc) "\n]")
  4734. (char-to-string (char-before (point)))))
  4735. (insert " "))
  4736. (unless (or (eobp)
  4737. (string-match (concat "[" (nth 1 erc) "\n]")
  4738. (char-to-string (char-after (point)))))
  4739. (insert " ") (backward-char 1))
  4740. (insert string)
  4741. (and move (backward-char 1))))
  4742. (defconst org-nonsticky-props
  4743. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4744. (defsubst org-rear-nonsticky-at (pos)
  4745. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4746. (defun org-activate-plain-links (limit)
  4747. "Run through the buffer and add overlays to links."
  4748. (catch 'exit
  4749. (let (f)
  4750. (if (re-search-forward org-plain-link-re limit t)
  4751. (progn
  4752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4753. (setq f (get-text-property (match-beginning 0) 'face))
  4754. (if (or (eq f 'org-tag)
  4755. (and (listp f) (memq 'org-tag f)))
  4756. nil
  4757. (add-text-properties (match-beginning 0) (match-end 0)
  4758. (list 'mouse-face 'highlight
  4759. 'face 'org-link
  4760. 'keymap org-mouse-map))
  4761. (org-rear-nonsticky-at (match-end 0)))
  4762. t)))))
  4763. (defun org-activate-code (limit)
  4764. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4765. (progn
  4766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4767. (remove-text-properties (match-beginning 0) (match-end 0)
  4768. '(display t invisible t intangible t))
  4769. t)))
  4770. (defcustom org-src-fontify-natively nil
  4771. "When non-nil, fontify code in code blocks."
  4772. :type 'boolean
  4773. :version "24.1"
  4774. :group 'org-appearance
  4775. :group 'org-babel)
  4776. (defun org-fontify-meta-lines-and-blocks (limit)
  4777. (condition-case nil
  4778. (org-fontify-meta-lines-and-blocks-1 limit)
  4779. (error (message "org-mode fontification error"))))
  4780. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4781. "Fontify #+ lines and blocks, in the correct ways."
  4782. (let ((case-fold-search t))
  4783. (if (re-search-forward
  4784. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4785. limit t)
  4786. (let ((beg (match-beginning 0))
  4787. (block-start (match-end 0))
  4788. (block-end nil)
  4789. (lang (match-string 7))
  4790. (beg1 (line-beginning-position 2))
  4791. (dc1 (downcase (match-string 2)))
  4792. (dc3 (downcase (match-string 3)))
  4793. end end1 quoting block-type ovl)
  4794. (cond
  4795. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4796. ;; a single line of backend-specific content
  4797. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4798. (remove-text-properties (match-beginning 0) (match-end 0)
  4799. '(display t invisible t intangible t))
  4800. (add-text-properties (match-beginning 1) (match-end 3)
  4801. '(font-lock-fontified t face org-meta-line))
  4802. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4803. '(font-lock-fontified t face org-block))
  4804. ; for backend-specific code
  4805. t)
  4806. ((and (match-end 4) (equal dc3 "begin"))
  4807. ;; Truly a block
  4808. (setq block-type (downcase (match-string 5))
  4809. quoting (member block-type org-protecting-blocks))
  4810. (when (re-search-forward
  4811. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4812. nil t) ;; on purpose, we look further than LIMIT
  4813. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4814. (setq block-end (match-beginning 0))
  4815. (when quoting
  4816. (remove-text-properties beg end
  4817. '(display t invisible t intangible t)))
  4818. (add-text-properties
  4819. beg end
  4820. '(font-lock-fontified t font-lock-multiline t))
  4821. (add-text-properties beg beg1 '(face org-meta-line))
  4822. (add-text-properties end1 (min (point-max) (1+ end))
  4823. '(face org-meta-line)) ; for end_src
  4824. (cond
  4825. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4826. (org-src-font-lock-fontify-block lang block-start block-end)
  4827. ;; remove old background overlays
  4828. (mapc (lambda (ov)
  4829. (if (eq (overlay-get ov 'face) 'org-block-background)
  4830. (delete-overlay ov)))
  4831. (overlays-at (/ (+ beg1 block-end) 2)))
  4832. ;; add a background overlay
  4833. (setq ovl (make-overlay beg1 block-end))
  4834. (overlay-put ovl 'face 'org-block-background)
  4835. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4836. (quoting
  4837. (add-text-properties beg1 (min (point-max) (1+ end1))
  4838. '(face org-block))) ; end of source block
  4839. ((not org-fontify-quote-and-verse-blocks))
  4840. ((string= block-type "quote")
  4841. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4842. ((string= block-type "verse")
  4843. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4844. (add-text-properties beg beg1 '(face org-block-begin-line))
  4845. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4846. t))
  4847. ((member dc1 '("title:" "author:" "email:" "date:"))
  4848. (add-text-properties
  4849. beg (match-end 3)
  4850. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4851. '(font-lock-fontified t invisible t)
  4852. '(font-lock-fontified t face org-document-info-keyword)))
  4853. (add-text-properties
  4854. (match-beginning 6) (match-end 6)
  4855. (if (string-equal dc1 "title:")
  4856. '(font-lock-fontified t face org-document-title)
  4857. '(font-lock-fontified t face org-document-info))))
  4858. ((not (member (char-after beg) '(?\ ?\t)))
  4859. ;; just any other in-buffer setting, but not indented
  4860. (add-text-properties
  4861. beg (1+ (match-end 0))
  4862. '(font-lock-fontified t face org-meta-line))
  4863. t)
  4864. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4865. "orgtbl:" "tblfm:" "tblname:" "results:"
  4866. "call:" "header:" "headers:" "name:"))
  4867. (and (match-end 4) (equal dc3 "attr")))
  4868. (add-text-properties
  4869. beg (match-end 0)
  4870. '(font-lock-fontified t face org-meta-line))
  4871. t)
  4872. ((member dc3 '(" " ""))
  4873. (add-text-properties
  4874. beg (match-end 0)
  4875. '(font-lock-fontified t face font-lock-comment-face)))
  4876. (t nil))))))
  4877. (defun org-strip-protective-commas (beg end)
  4878. "Strip protective commas between BEG and END in the current buffer."
  4879. (interactive "r")
  4880. (save-excursion
  4881. (save-match-data
  4882. (goto-char beg)
  4883. (let ((front-line (save-excursion
  4884. (re-search-forward
  4885. "[^[:space:]]" end t)
  4886. (goto-char (match-beginning 0))
  4887. (current-column))))
  4888. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4889. (goto-char (match-beginning 1))
  4890. (when (= (current-column) front-line)
  4891. (replace-match "" nil nil nil 1)))))))
  4892. (defun org-activate-angle-links (limit)
  4893. "Run through the buffer and add overlays to links."
  4894. (if (re-search-forward org-angle-link-re limit t)
  4895. (progn
  4896. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4897. (add-text-properties (match-beginning 0) (match-end 0)
  4898. (list 'mouse-face 'highlight
  4899. 'keymap org-mouse-map))
  4900. (org-rear-nonsticky-at (match-end 0))
  4901. t)))
  4902. (defun org-activate-footnote-links (limit)
  4903. "Run through the buffer and add overlays to footnotes."
  4904. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4905. (when fn
  4906. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4907. (org-remove-flyspell-overlays-in beg end)
  4908. (add-text-properties beg end
  4909. (list 'mouse-face 'highlight
  4910. 'keymap org-mouse-map
  4911. 'help-echo
  4912. (if (= (point-at-bol) beg)
  4913. "Footnote definition"
  4914. "Footnote reference")
  4915. 'font-lock-fontified t
  4916. 'font-lock-multiline t
  4917. 'face 'org-footnote))))))
  4918. (defun org-activate-bracket-links (limit)
  4919. "Run through the buffer and add overlays to bracketed links."
  4920. (if (re-search-forward org-bracket-link-regexp limit t)
  4921. (let* ((help (concat "LINK: "
  4922. (org-match-string-no-properties 1)))
  4923. ;; FIXME: above we should remove the escapes.
  4924. ;; but that requires another match, protecting match data,
  4925. ;; a lot of overhead for font-lock.
  4926. (ip (org-maybe-intangible
  4927. (list 'invisible 'org-link
  4928. 'keymap org-mouse-map 'mouse-face 'highlight
  4929. 'font-lock-multiline t 'help-echo help)))
  4930. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4931. 'font-lock-multiline t 'help-echo help)))
  4932. ;; We need to remove the invisible property here. Table narrowing
  4933. ;; may have made some of this invisible.
  4934. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4935. (remove-text-properties (match-beginning 0) (match-end 0)
  4936. '(invisible nil))
  4937. (if (match-end 3)
  4938. (progn
  4939. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4940. (org-rear-nonsticky-at (match-beginning 3))
  4941. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4942. (org-rear-nonsticky-at (match-end 3))
  4943. (add-text-properties (match-end 3) (match-end 0) ip)
  4944. (org-rear-nonsticky-at (match-end 0)))
  4945. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4946. (org-rear-nonsticky-at (match-beginning 1))
  4947. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4948. (org-rear-nonsticky-at (match-end 1))
  4949. (add-text-properties (match-end 1) (match-end 0) ip)
  4950. (org-rear-nonsticky-at (match-end 0)))
  4951. t)))
  4952. (defun org-activate-dates (limit)
  4953. "Run through the buffer and add overlays to dates."
  4954. (if (re-search-forward org-tsr-regexp-both limit t)
  4955. (progn
  4956. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4957. (add-text-properties (match-beginning 0) (match-end 0)
  4958. (list 'mouse-face 'highlight
  4959. 'keymap org-mouse-map))
  4960. (org-rear-nonsticky-at (match-end 0))
  4961. (when org-display-custom-times
  4962. (if (match-end 3)
  4963. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4964. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4965. t)))
  4966. (defvar org-target-link-regexp nil
  4967. "Regular expression matching radio targets in plain text.")
  4968. (make-variable-buffer-local 'org-target-link-regexp)
  4969. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4970. "Regular expression matching a link target.")
  4971. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4972. "Regular expression matching a radio target.")
  4973. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4974. "Regular expression matching any target.")
  4975. (defun org-activate-target-links (limit)
  4976. "Run through the buffer and add overlays to target matches."
  4977. (when org-target-link-regexp
  4978. (let ((case-fold-search t))
  4979. (if (re-search-forward org-target-link-regexp limit t)
  4980. (progn
  4981. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4982. (add-text-properties (match-beginning 0) (match-end 0)
  4983. (list 'mouse-face 'highlight
  4984. 'keymap org-mouse-map
  4985. 'help-echo "Radio target link"
  4986. 'org-linked-text t))
  4987. (org-rear-nonsticky-at (match-end 0))
  4988. t)))))
  4989. (defun org-update-radio-target-regexp ()
  4990. "Find all radio targets in this file and update the regular expression."
  4991. (interactive)
  4992. (when (memq 'radio org-activate-links)
  4993. (setq org-target-link-regexp
  4994. (org-make-target-link-regexp (org-all-targets 'radio)))
  4995. (org-restart-font-lock)))
  4996. (defun org-hide-wide-columns (limit)
  4997. (let (s e)
  4998. (setq s (text-property-any (point) (or limit (point-max))
  4999. 'org-cwidth t))
  5000. (when s
  5001. (setq e (next-single-property-change s 'org-cwidth))
  5002. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5003. (goto-char e)
  5004. t)))
  5005. (defvar org-latex-and-specials-regexp nil
  5006. "Regular expression for highlighting export special stuff.")
  5007. (defvar org-match-substring-regexp)
  5008. (defvar org-match-substring-with-braces-regexp)
  5009. ;; This should be with the exporter code, but we also use if for font-locking
  5010. (defconst org-export-html-special-string-regexps
  5011. '(("\\\\-" . "&shy;")
  5012. ("---\\([^-]\\)" . "&mdash;\\1")
  5013. ("--\\([^-]\\)" . "&ndash;\\1")
  5014. ("\\.\\.\\." . "&hellip;"))
  5015. "Regular expressions for special string conversion.")
  5016. (defun org-compute-latex-and-specials-regexp ()
  5017. "Compute regular expression for stuff treated specially by exporters."
  5018. (if (not org-highlight-latex-fragments-and-specials)
  5019. (org-set-local 'org-latex-and-specials-regexp nil)
  5020. (require 'org-exp)
  5021. (let*
  5022. ((matchers (plist-get org-format-latex-options :matchers))
  5023. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5024. org-latex-regexps)))
  5025. (org-export-allow-BIND nil)
  5026. (options (org-combine-plists (org-default-export-plist)
  5027. (org-infile-export-plist)))
  5028. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5029. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5030. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5031. (org-export-html-expand (plist-get options :expand-quoted-html))
  5032. (org-export-with-special-strings (plist-get options :special-strings))
  5033. (re-sub
  5034. (cond
  5035. ((equal org-export-with-sub-superscripts '{})
  5036. (list org-match-substring-with-braces-regexp))
  5037. (org-export-with-sub-superscripts
  5038. (list org-match-substring-regexp))
  5039. (t nil)))
  5040. (re-latex
  5041. (if org-export-with-LaTeX-fragments
  5042. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5043. (re-macros
  5044. (if org-export-with-TeX-macros
  5045. (list (concat "\\\\"
  5046. (regexp-opt
  5047. (append
  5048. (delq nil
  5049. (mapcar 'car-safe
  5050. (append org-entities-user
  5051. org-entities)))
  5052. (if (boundp 'org-latex-entities)
  5053. (mapcar (lambda (x)
  5054. (or (car-safe x) x))
  5055. org-latex-entities)
  5056. nil))
  5057. 'words))) ; FIXME
  5058. ))
  5059. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5060. (re-special (if org-export-with-special-strings
  5061. (mapcar (lambda (x) (car x))
  5062. org-export-html-special-string-regexps)))
  5063. (re-rest
  5064. (delq nil
  5065. (list
  5066. (if org-export-html-expand "@<[^>\n]+>")
  5067. ))))
  5068. (org-set-local
  5069. 'org-latex-and-specials-regexp
  5070. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5071. re-rest) "\\|")))))
  5072. (defun org-do-latex-and-special-faces (limit)
  5073. "Run through the buffer and add overlays to links."
  5074. (when org-latex-and-specials-regexp
  5075. (let (rtn d)
  5076. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5077. limit t))
  5078. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5079. 'face))
  5080. '(org-code org-verbatim underline)))
  5081. (progn
  5082. (setq rtn t
  5083. d (cond ((member (char-after (1+ (match-beginning 0)))
  5084. '(?_ ?^)) 1)
  5085. (t 0)))
  5086. (font-lock-prepend-text-property
  5087. (+ d (match-beginning 0)) (match-end 0)
  5088. 'face 'org-latex-and-export-specials)
  5089. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5090. '(font-lock-multiline t)))))
  5091. rtn)))
  5092. (defun org-restart-font-lock ()
  5093. "Restart `font-lock-mode', to force refontification."
  5094. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5095. (font-lock-mode -1)
  5096. (font-lock-mode 1)))
  5097. (defun org-all-targets (&optional radio)
  5098. "Return a list of all targets in this file.
  5099. With optional argument RADIO, only find radio targets."
  5100. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5101. rtn)
  5102. (save-excursion
  5103. (goto-char (point-min))
  5104. (while (re-search-forward re nil t)
  5105. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5106. rtn)))
  5107. (defun org-make-target-link-regexp (targets)
  5108. "Make regular expression matching all strings in TARGETS.
  5109. The regular expression finds the targets also if there is a line break
  5110. between words."
  5111. (and targets
  5112. (concat
  5113. "\\<\\("
  5114. (mapconcat
  5115. (lambda (x)
  5116. (setq x (regexp-quote x))
  5117. (while (string-match " +" x)
  5118. (setq x (replace-match "\\s-+" t t x)))
  5119. x)
  5120. targets
  5121. "\\|")
  5122. "\\)\\>")))
  5123. (defun org-activate-tags (limit)
  5124. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5125. (progn
  5126. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5127. (add-text-properties (match-beginning 1) (match-end 1)
  5128. (list 'mouse-face 'highlight
  5129. 'keymap org-mouse-map))
  5130. (org-rear-nonsticky-at (match-end 1))
  5131. t)))
  5132. (defun org-outline-level ()
  5133. "Compute the outline level of the heading at point.
  5134. This function assumes that the cursor is at the beginning of a line matched
  5135. by `outline-regexp'. Otherwise it returns garbage.
  5136. If this is called at a normal headline, the level is the number of stars.
  5137. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5138. (save-excursion
  5139. (looking-at org-outline-regexp)
  5140. (1- (- (match-end 0) (match-beginning 0)))))
  5141. (defvar org-font-lock-keywords nil)
  5142. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5143. "Regular expression matching a property line.")
  5144. (defvar org-font-lock-hook nil
  5145. "Functions to be called for special font lock stuff.")
  5146. (defvar org-font-lock-set-keywords-hook nil
  5147. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5148. This is called after `org-font-lock-extra-keywords' is defined, but before
  5149. it is installed to be used by font lock. This can be useful if something
  5150. needs to be inserted at a specific position in the font-lock sequence.")
  5151. (defun org-font-lock-hook (limit)
  5152. (run-hook-with-args 'org-font-lock-hook limit))
  5153. (defun org-set-font-lock-defaults ()
  5154. (let* ((em org-fontify-emphasized-text)
  5155. (lk org-activate-links)
  5156. (org-font-lock-extra-keywords
  5157. (list
  5158. ;; Call the hook
  5159. '(org-font-lock-hook)
  5160. ;; Headlines
  5161. `(,(if org-fontify-whole-heading-line
  5162. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5163. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5164. (1 (org-get-level-face 1))
  5165. (2 (org-get-level-face 2))
  5166. (3 (org-get-level-face 3)))
  5167. ;; Table lines
  5168. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5169. (1 'org-table t))
  5170. ;; Table internals
  5171. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5172. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5173. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5174. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5175. ;; Drawers
  5176. (list org-drawer-regexp '(0 'org-special-keyword t))
  5177. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5178. ;; Properties
  5179. (list org-property-re
  5180. '(1 'org-special-keyword t)
  5181. '(3 'org-property-value t))
  5182. ;; Links
  5183. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5184. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5185. (if (memq 'plain lk) '(org-activate-plain-links))
  5186. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5187. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5188. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5189. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5190. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5191. '(org-hide-wide-columns (0 nil append))
  5192. ;; TODO keyword
  5193. (list (format org-heading-keyword-regexp-format
  5194. org-todo-regexp)
  5195. '(2 (org-get-todo-face 2) t))
  5196. ;; DONE
  5197. (if org-fontify-done-headline
  5198. (list (format org-heading-keyword-regexp-format
  5199. (concat
  5200. "\\(?:"
  5201. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5202. "\\)"))
  5203. '(2 'org-headline-done t))
  5204. nil)
  5205. ;; Priorities
  5206. '(org-font-lock-add-priority-faces)
  5207. ;; Tags
  5208. '(org-font-lock-add-tag-faces)
  5209. ;; Special keywords
  5210. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5211. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5212. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5213. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5214. ;; Emphasis
  5215. (if em
  5216. (if (featurep 'xemacs)
  5217. '(org-do-emphasis-faces (0 nil append))
  5218. '(org-do-emphasis-faces)))
  5219. ;; Checkboxes
  5220. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5221. 1 'org-checkbox prepend)
  5222. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5223. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5224. (0 (org-get-checkbox-statistics-face) t)))
  5225. ;; Description list items
  5226. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5227. 1 'bold prepend)
  5228. ;; ARCHIVEd headings
  5229. (list (concat
  5230. org-outline-regexp-bol
  5231. "\\(.*:" org-archive-tag ":.*\\)")
  5232. '(1 'org-archived prepend))
  5233. ;; Specials
  5234. '(org-do-latex-and-special-faces)
  5235. '(org-fontify-entities)
  5236. '(org-raise-scripts)
  5237. ;; Code
  5238. '(org-activate-code (1 'org-code t))
  5239. ;; COMMENT
  5240. (list (format org-heading-keyword-regexp-format
  5241. (concat "\\("
  5242. org-comment-string "\\|" org-quote-string
  5243. "\\)"))
  5244. '(2 'org-special-keyword t))
  5245. '("^#.*" (0 'font-lock-comment-face t))
  5246. ;; Blocks and meta lines
  5247. '(org-fontify-meta-lines-and-blocks)
  5248. )))
  5249. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5250. (run-hooks 'org-font-lock-set-keywords-hook)
  5251. ;; Now set the full font-lock-keywords
  5252. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5253. (org-set-local 'font-lock-defaults
  5254. '(org-font-lock-keywords t nil nil backward-paragraph))
  5255. (kill-local-variable 'font-lock-keywords) nil))
  5256. (defun org-toggle-pretty-entities ()
  5257. "Toggle the composition display of entities as UTF8 characters."
  5258. (interactive)
  5259. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5260. (org-restart-font-lock)
  5261. (if org-pretty-entities
  5262. (message "Entities are displayed as UTF8 characters")
  5263. (save-restriction
  5264. (widen)
  5265. (org-decompose-region (point-min) (point-max))
  5266. (message "Entities are displayed plain"))))
  5267. (defun org-fontify-entities (limit)
  5268. "Find an entity to fontify."
  5269. (let (ee)
  5270. (when org-pretty-entities
  5271. (catch 'match
  5272. (while (re-search-forward
  5273. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5274. limit t)
  5275. (if (and (not (org-in-indented-comment-line))
  5276. (setq ee (org-entity-get (match-string 1)))
  5277. (= (length (nth 6 ee)) 1))
  5278. (progn
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 1)
  5281. (list 'font-lock-fontified t))
  5282. (compose-region (match-beginning 0) (match-end 1)
  5283. (nth 6 ee) nil)
  5284. (backward-char 1)
  5285. (throw 'match t))))
  5286. nil))))
  5287. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5288. "Fontify string S like in Org-mode."
  5289. (with-temp-buffer
  5290. (insert s)
  5291. (let ((org-odd-levels-only odd-levels))
  5292. (org-mode)
  5293. (font-lock-fontify-buffer)
  5294. (buffer-string))))
  5295. (defvar org-m nil)
  5296. (defvar org-l nil)
  5297. (defvar org-f nil)
  5298. (defun org-get-level-face (n)
  5299. "Get the right face for match N in font-lock matching of headlines."
  5300. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5301. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5302. (if org-cycle-level-faces
  5303. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5304. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5305. (cond
  5306. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5307. ((eq n 2) org-f)
  5308. (t (if org-level-color-stars-only nil org-f))))
  5309. (defun org-get-todo-face (kwd)
  5310. "Get the right face for a TODO keyword KWD.
  5311. If KWD is a number, get the corresponding match group."
  5312. (if (numberp kwd) (setq kwd (match-string kwd)))
  5313. (or (org-face-from-face-or-color
  5314. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5315. (and (member kwd org-done-keywords) 'org-done)
  5316. 'org-todo))
  5317. (defun org-face-from-face-or-color (context inherit face-or-color)
  5318. "Create a face list that inherits INHERIT, but sets the foreground color.
  5319. When FACE-OR-COLOR is not a string, just return it."
  5320. (if (stringp face-or-color)
  5321. (list :inherit inherit
  5322. (cdr (assoc context org-faces-easy-properties))
  5323. face-or-color)
  5324. face-or-color))
  5325. (defun org-font-lock-add-tag-faces (limit)
  5326. "Add the special tag faces."
  5327. (when (and org-tag-faces org-tags-special-faces-re)
  5328. (while (re-search-forward org-tags-special-faces-re limit t)
  5329. (add-text-properties (match-beginning 1) (match-end 1)
  5330. (list 'face (org-get-tag-face 1)
  5331. 'font-lock-fontified t))
  5332. (backward-char 1))))
  5333. (defun org-font-lock-add-priority-faces (limit)
  5334. "Add the special priority faces."
  5335. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5336. (add-text-properties
  5337. (match-beginning 0) (match-end 0)
  5338. (list 'face (or (org-face-from-face-or-color
  5339. 'priority 'org-special-keyword
  5340. (cdr (assoc (char-after (match-beginning 1))
  5341. org-priority-faces)))
  5342. 'org-special-keyword)
  5343. 'font-lock-fontified t))))
  5344. (defun org-get-tag-face (kwd)
  5345. "Get the right face for a TODO keyword KWD.
  5346. If KWD is a number, get the corresponding match group."
  5347. (if (numberp kwd) (setq kwd (match-string kwd)))
  5348. (or (org-face-from-face-or-color
  5349. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5350. 'org-tag))
  5351. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5352. "Remove fontification and activation overlays from links."
  5353. (font-lock-default-unfontify-region beg end)
  5354. (let* ((buffer-undo-list t)
  5355. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5356. (inhibit-modification-hooks t)
  5357. deactivate-mark buffer-file-name buffer-file-truename)
  5358. (org-decompose-region beg end)
  5359. (remove-text-properties beg end
  5360. '(mouse-face t keymap t org-linked-text t
  5361. invisible t intangible t
  5362. org-no-flyspell t org-emphasis t))
  5363. (org-remove-font-lock-display-properties beg end)))
  5364. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5365. ((raise 0.3) (height 0.7))
  5366. ((raise -0.5))
  5367. ((raise 0.5)))
  5368. "Display properties for showing superscripts and subscripts.")
  5369. (defun org-remove-font-lock-display-properties (beg end)
  5370. "Remove specific display properties that have been added by font lock.
  5371. The will remove the raise properties that are used to show superscripts
  5372. and subscripts."
  5373. (let (next prop)
  5374. (while (< beg end)
  5375. (setq next (next-single-property-change beg 'display nil end)
  5376. prop (get-text-property beg 'display))
  5377. (if (member prop org-script-display)
  5378. (put-text-property beg next 'display nil))
  5379. (setq beg next))))
  5380. (defun org-raise-scripts (limit)
  5381. "Add raise properties to sub/superscripts."
  5382. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5383. (if (re-search-forward
  5384. (if (eq org-use-sub-superscripts t)
  5385. org-match-substring-regexp
  5386. org-match-substring-with-braces-regexp)
  5387. limit t)
  5388. (let* ((pos (point)) table-p comment-p
  5389. (mpos (match-beginning 3))
  5390. (emph-p (get-text-property mpos 'org-emphasis))
  5391. (link-p (get-text-property mpos 'mouse-face))
  5392. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5393. (goto-char (point-at-bol))
  5394. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5395. comment-p (org-looking-at-p "[ \t]*#"))
  5396. (goto-char pos)
  5397. ;; FIXME: Should we go back one character here, for a_b^c
  5398. ;; (goto-char (1- pos)) ;????????????????????
  5399. (if (or comment-p emph-p link-p keyw-p)
  5400. t
  5401. (put-text-property (match-beginning 3) (match-end 0)
  5402. 'display
  5403. (if (equal (char-after (match-beginning 2)) ?^)
  5404. (nth (if table-p 3 1) org-script-display)
  5405. (nth (if table-p 2 0) org-script-display)))
  5406. (add-text-properties (match-beginning 2) (match-end 2)
  5407. (list 'invisible t
  5408. 'org-dwidth t 'org-dwidth-n 1))
  5409. (if (and (eq (char-after (match-beginning 3)) ?{)
  5410. (eq (char-before (match-end 3)) ?}))
  5411. (progn
  5412. (add-text-properties
  5413. (match-beginning 3) (1+ (match-beginning 3))
  5414. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5415. (add-text-properties
  5416. (1- (match-end 3)) (match-end 3)
  5417. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5418. t)))))
  5419. ;;;; Visibility cycling, including org-goto and indirect buffer
  5420. ;;; Cycling
  5421. (defvar org-cycle-global-status nil)
  5422. (make-variable-buffer-local 'org-cycle-global-status)
  5423. (defvar org-cycle-subtree-status nil)
  5424. (make-variable-buffer-local 'org-cycle-subtree-status)
  5425. ;;;###autoload
  5426. (defvar org-inlinetask-min-level)
  5427. (defun org-cycle (&optional arg)
  5428. "TAB-action and visibility cycling for Org-mode.
  5429. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5430. is outline visibility cycling, but it also invokes other actions
  5431. in special contexts.
  5432. - When this function is called with a prefix argument, rotate the entire
  5433. buffer through 3 states (global cycling)
  5434. 1. OVERVIEW: Show only top-level headlines.
  5435. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5436. 3. SHOW ALL: Show everything.
  5437. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5438. determined by the variable `org-startup-folded', and by any VISIBILITY
  5439. properties in the buffer.
  5440. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5441. including any drawers.
  5442. - When inside a table, re-align the table and move to the next field.
  5443. - When point is at the beginning of a headline, rotate the subtree started
  5444. by this line through 3 different states (local cycling)
  5445. 1. FOLDED: Only the main headline is shown.
  5446. 2. CHILDREN: The main headline and the direct children are shown.
  5447. From this state, you can move to one of the children
  5448. and zoom in further.
  5449. 3. SUBTREE: Show the entire subtree, including body text.
  5450. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5451. - When point is at the beginning of an empty headline and the variable
  5452. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5453. of the headline by demoting and promoting it to likely levels. This
  5454. speeds up creation document structure by pressing TAB once or several
  5455. times right after creating a new headline.
  5456. - When there is a numeric prefix, go up to a heading with level ARG, do
  5457. a `show-subtree' and return to the previous cursor position. If ARG
  5458. is negative, go up that many levels.
  5459. - When point is not at the beginning of a headline, execute the global
  5460. binding for TAB, which is re-indenting the line. See the option
  5461. `org-cycle-emulate-tab' for details.
  5462. - Special case: if point is at the beginning of the buffer and there is
  5463. no headline in line 1, this function will act as if called with prefix arg
  5464. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5465. But only if also the variable `org-cycle-global-at-bob' is t."
  5466. (interactive "P")
  5467. (org-load-modules-maybe)
  5468. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5469. (and org-cycle-level-after-item/entry-creation
  5470. (or (org-cycle-level)
  5471. (org-cycle-item-indentation))))
  5472. (let* ((limit-level
  5473. (or org-cycle-max-level
  5474. (and (boundp 'org-inlinetask-min-level)
  5475. org-inlinetask-min-level
  5476. (1- org-inlinetask-min-level))))
  5477. (nstars (and limit-level
  5478. (if org-odd-levels-only
  5479. (and limit-level (1- (* limit-level 2)))
  5480. limit-level)))
  5481. (org-outline-regexp
  5482. (if (not (eq major-mode 'org-mode))
  5483. outline-regexp
  5484. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5485. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5486. (not (looking-at org-outline-regexp))))
  5487. (org-cycle-hook
  5488. (if bob-special
  5489. (delq 'org-optimize-window-after-visibility-change
  5490. (copy-sequence org-cycle-hook))
  5491. org-cycle-hook))
  5492. (pos (point)))
  5493. (if (or bob-special (equal arg '(4)))
  5494. ;; special case: use global cycling
  5495. (setq arg t))
  5496. (cond
  5497. ((equal arg '(16))
  5498. (setq last-command 'dummy)
  5499. (org-set-startup-visibility)
  5500. (message "Startup visibility, plus VISIBILITY properties"))
  5501. ((equal arg '(64))
  5502. (show-all)
  5503. (message "Entire buffer visible, including drawers"))
  5504. ;; Table: enter it or move to the next field.
  5505. ((org-at-table-p 'any)
  5506. (if (org-at-table.el-p)
  5507. (message "Use C-c ' to edit table.el tables")
  5508. (if arg (org-table-edit-field t)
  5509. (org-table-justify-field-maybe)
  5510. (call-interactively 'org-table-next-field))))
  5511. ((run-hook-with-args-until-success
  5512. 'org-tab-after-check-for-table-hook))
  5513. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5514. ((eq arg t) (org-cycle-internal-global))
  5515. ;; Drawers: delegate to `org-flag-drawer'.
  5516. ((and org-drawers org-drawer-regexp
  5517. (save-excursion
  5518. (beginning-of-line 1)
  5519. (looking-at org-drawer-regexp)))
  5520. (org-flag-drawer ; toggle block visibility
  5521. (not (get-char-property (match-end 0) 'invisible))))
  5522. ;; Show-subtree, ARG levels up from here.
  5523. ((integerp arg)
  5524. (save-excursion
  5525. (org-back-to-heading)
  5526. (outline-up-heading (if (< arg 0) (- arg)
  5527. (- (funcall outline-level) arg)))
  5528. (org-show-subtree)))
  5529. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5530. ((and (featurep 'org-inlinetask)
  5531. (org-inlinetask-at-task-p)
  5532. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5533. (org-inlinetask-toggle-visibility))
  5534. ((org-try-cdlatex-tab))
  5535. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5536. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5537. (save-excursion (beginning-of-line 1)
  5538. (looking-at org-outline-regexp)))
  5539. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5540. (org-cycle-internal-local))
  5541. ;; From there: TAB emulation and template completion.
  5542. (buffer-read-only (org-back-to-heading))
  5543. ((run-hook-with-args-until-success
  5544. 'org-tab-after-check-for-cycling-hook))
  5545. ((org-try-structure-completion))
  5546. ((run-hook-with-args-until-success
  5547. 'org-tab-before-tab-emulation-hook))
  5548. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5549. (or (not (bolp))
  5550. (not (looking-at org-outline-regexp))))
  5551. (call-interactively (global-key-binding "\t")))
  5552. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5553. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5554. (or (and (eq org-cycle-emulate-tab 'white)
  5555. (= (match-end 0) (point-at-eol)))
  5556. (and (eq org-cycle-emulate-tab 'whitestart)
  5557. (>= (match-end 0) pos))))
  5558. t
  5559. (eq org-cycle-emulate-tab t))
  5560. (call-interactively (global-key-binding "\t")))
  5561. (t (save-excursion
  5562. (org-back-to-heading)
  5563. (org-cycle)))))))
  5564. (defun org-cycle-internal-global ()
  5565. "Do the global cycling action."
  5566. ;; Hack to avoid display of messages for .org attachments in Gnus
  5567. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5568. (cond
  5569. ((and (eq last-command this-command)
  5570. (eq org-cycle-global-status 'overview))
  5571. ;; We just created the overview - now do table of contents
  5572. ;; This can be slow in very large buffers, so indicate action
  5573. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5574. (unless ga (message "CONTENTS..."))
  5575. (org-content)
  5576. (unless ga (message "CONTENTS...done"))
  5577. (setq org-cycle-global-status 'contents)
  5578. (run-hook-with-args 'org-cycle-hook 'contents))
  5579. ((and (eq last-command this-command)
  5580. (eq org-cycle-global-status 'contents))
  5581. ;; We just showed the table of contents - now show everything
  5582. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5583. (show-all)
  5584. (unless ga (message "SHOW ALL"))
  5585. (setq org-cycle-global-status 'all)
  5586. (run-hook-with-args 'org-cycle-hook 'all))
  5587. (t
  5588. ;; Default action: go to overview
  5589. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5590. (org-overview)
  5591. (unless ga (message "OVERVIEW"))
  5592. (setq org-cycle-global-status 'overview)
  5593. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5594. (defun org-cycle-internal-local ()
  5595. "Do the local cycling action."
  5596. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5597. ;; First, determine end of headline (EOH), end of subtree or item
  5598. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5599. (save-excursion
  5600. (if (org-at-item-p)
  5601. (progn
  5602. (beginning-of-line)
  5603. (setq struct (org-list-struct))
  5604. (setq eoh (point-at-eol))
  5605. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5606. (setq has-children (org-list-has-child-p (point) struct)))
  5607. (org-back-to-heading)
  5608. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5609. (setq eos (save-excursion
  5610. (org-end-of-subtree t)
  5611. (unless (eobp)
  5612. (skip-chars-forward " \t\n"))
  5613. (if (eobp) (point) (1- (point)))))
  5614. (setq has-children
  5615. (or (save-excursion
  5616. (let ((level (funcall outline-level)))
  5617. (outline-next-heading)
  5618. (and (org-at-heading-p t)
  5619. (> (funcall outline-level) level))))
  5620. (save-excursion
  5621. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5622. ;; Determine end invisible part of buffer (EOL)
  5623. (beginning-of-line 2)
  5624. ;; XEmacs doesn't have `next-single-char-property-change'
  5625. (if (featurep 'xemacs)
  5626. (while (and (not (eobp)) ;; this is like `next-line'
  5627. (get-char-property (1- (point)) 'invisible))
  5628. (beginning-of-line 2))
  5629. (while (and (not (eobp)) ;; this is like `next-line'
  5630. (get-char-property (1- (point)) 'invisible))
  5631. (goto-char (next-single-char-property-change (point) 'invisible))
  5632. (and (eolp) (beginning-of-line 2))))
  5633. (setq eol (point)))
  5634. ;; Find out what to do next and set `this-command'
  5635. (cond
  5636. ((= eos eoh)
  5637. ;; Nothing is hidden behind this heading
  5638. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5639. (message "EMPTY ENTRY")
  5640. (setq org-cycle-subtree-status nil)
  5641. (save-excursion
  5642. (goto-char eos)
  5643. (outline-next-heading)
  5644. (if (outline-invisible-p) (org-flag-heading nil))))
  5645. ((and (or (>= eol eos)
  5646. (not (string-match "\\S-" (buffer-substring eol eos))))
  5647. (or has-children
  5648. (not (setq children-skipped
  5649. org-cycle-skip-children-state-if-no-children))))
  5650. ;; Entire subtree is hidden in one line: children view
  5651. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5652. (if (org-at-item-p)
  5653. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5654. (org-show-entry)
  5655. (org-with-limited-levels (show-children))
  5656. (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5657. (org-cycle-hide-drawers 'subtree))
  5658. ;; Fold every list in subtree to top-level items.
  5659. (when (eq org-cycle-include-plain-lists 'integrate)
  5660. (save-excursion
  5661. (org-back-to-heading)
  5662. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5663. (beginning-of-line 1)
  5664. (let* ((struct (org-list-struct))
  5665. (prevs (org-list-prevs-alist struct))
  5666. (end (org-list-get-bottom-point struct)))
  5667. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5668. (org-list-get-all-items (point) struct prevs))
  5669. (goto-char end))))))
  5670. (message "CHILDREN")
  5671. (save-excursion
  5672. (goto-char eos)
  5673. (outline-next-heading)
  5674. (if (outline-invisible-p) (org-flag-heading nil)))
  5675. (setq org-cycle-subtree-status 'children)
  5676. (run-hook-with-args 'org-cycle-hook 'children))
  5677. ((or children-skipped
  5678. (and (eq last-command this-command)
  5679. (eq org-cycle-subtree-status 'children)))
  5680. ;; We just showed the children, or no children are there,
  5681. ;; now show everything.
  5682. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5683. (outline-flag-region eoh eos nil)
  5684. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5685. (setq org-cycle-subtree-status 'subtree)
  5686. (run-hook-with-args 'org-cycle-hook 'subtree))
  5687. (t
  5688. ;; Default action: hide the subtree.
  5689. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5690. (outline-flag-region eoh eos t)
  5691. (message "FOLDED")
  5692. (setq org-cycle-subtree-status 'folded)
  5693. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5694. ;;;###autoload
  5695. (defun org-global-cycle (&optional arg)
  5696. "Cycle the global visibility. For details see `org-cycle'.
  5697. With \\[universal-argument] prefix arg, switch to startup visibility.
  5698. With a numeric prefix, show all headlines up to that level."
  5699. (interactive "P")
  5700. (let ((org-cycle-include-plain-lists
  5701. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5702. (cond
  5703. ((integerp arg)
  5704. (show-all)
  5705. (hide-sublevels arg)
  5706. (setq org-cycle-global-status 'contents))
  5707. ((equal arg '(4))
  5708. (org-set-startup-visibility)
  5709. (message "Startup visibility, plus VISIBILITY properties."))
  5710. (t
  5711. (org-cycle '(4))))))
  5712. (defun org-set-startup-visibility ()
  5713. "Set the visibility required by startup options and properties."
  5714. (cond
  5715. ((eq org-startup-folded t)
  5716. (org-cycle '(4)))
  5717. ((eq org-startup-folded 'content)
  5718. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5719. (org-cycle '(4)) (org-cycle '(4)))))
  5720. (unless (eq org-startup-folded 'showeverything)
  5721. (if org-hide-block-startup (org-hide-block-all))
  5722. (org-set-visibility-according-to-property 'no-cleanup)
  5723. (org-cycle-hide-archived-subtrees 'all)
  5724. (org-cycle-hide-drawers 'all)
  5725. (org-cycle-show-empty-lines t)))
  5726. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5727. "Switch subtree visibilities according to :VISIBILITY: property."
  5728. (interactive)
  5729. (let (org-show-entry-below state)
  5730. (save-excursion
  5731. (goto-char (point-min))
  5732. (while (re-search-forward
  5733. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5734. nil t)
  5735. (setq state (match-string 1))
  5736. (save-excursion
  5737. (org-back-to-heading t)
  5738. (hide-subtree)
  5739. (org-reveal)
  5740. (cond
  5741. ((equal state '("fold" "folded"))
  5742. (hide-subtree))
  5743. ((equal state "children")
  5744. (org-show-hidden-entry)
  5745. (show-children))
  5746. ((equal state "content")
  5747. (save-excursion
  5748. (save-restriction
  5749. (org-narrow-to-subtree)
  5750. (org-content))))
  5751. ((member state '("all" "showall"))
  5752. (show-subtree)))))
  5753. (unless no-cleanup
  5754. (org-cycle-hide-archived-subtrees 'all)
  5755. (org-cycle-hide-drawers 'all)
  5756. (org-cycle-show-empty-lines 'all)))))
  5757. ;; This function uses outline-regexp instead of the more fundamental
  5758. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5759. ;; buffers, where outline-regexp is needed.
  5760. (defun org-overview ()
  5761. "Switch to overview mode, showing only top-level headlines.
  5762. Really, this shows all headlines with level equal or greater than the level
  5763. of the first headline in the buffer. This is important, because if the
  5764. first headline is not level one, then (hide-sublevels 1) gives confusing
  5765. results."
  5766. (interactive)
  5767. (let ((level (save-excursion
  5768. (goto-char (point-min))
  5769. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5770. (progn
  5771. (goto-char (match-beginning 0))
  5772. (funcall outline-level))))))
  5773. (and level (hide-sublevels level))))
  5774. (defun org-content (&optional arg)
  5775. "Show all headlines in the buffer, like a table of contents.
  5776. With numerical argument N, show content up to level N."
  5777. (interactive "P")
  5778. (save-excursion
  5779. ;; Visit all headings and show their offspring
  5780. (and (integerp arg) (org-overview))
  5781. (goto-char (point-max))
  5782. (catch 'exit
  5783. (while (and (progn (condition-case nil
  5784. (outline-previous-visible-heading 1)
  5785. (error (goto-char (point-min))))
  5786. t)
  5787. (looking-at org-outline-regexp))
  5788. (if (integerp arg)
  5789. (show-children (1- arg))
  5790. (show-branches))
  5791. (if (bobp) (throw 'exit nil))))))
  5792. (defun org-optimize-window-after-visibility-change (state)
  5793. "Adjust the window after a change in outline visibility.
  5794. This function is the default value of the hook `org-cycle-hook'."
  5795. (when (get-buffer-window (current-buffer))
  5796. (cond
  5797. ((eq state 'content) nil)
  5798. ((eq state 'all) nil)
  5799. ((eq state 'folded) nil)
  5800. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5801. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5802. (defun org-remove-empty-overlays-at (pos)
  5803. "Remove outline overlays that do not contain non-white stuff."
  5804. (mapc
  5805. (lambda (o)
  5806. (and (eq 'outline (overlay-get o 'invisible))
  5807. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5808. (overlay-end o))))
  5809. (delete-overlay o)))
  5810. (overlays-at pos)))
  5811. (defun org-clean-visibility-after-subtree-move ()
  5812. "Fix visibility issues after moving a subtree."
  5813. ;; First, find a reasonable region to look at:
  5814. ;; Start two siblings above, end three below
  5815. (let* ((beg (save-excursion
  5816. (and (org-get-last-sibling)
  5817. (org-get-last-sibling))
  5818. (point)))
  5819. (end (save-excursion
  5820. (and (org-get-next-sibling)
  5821. (org-get-next-sibling)
  5822. (org-get-next-sibling))
  5823. (if (org-at-heading-p)
  5824. (point-at-eol)
  5825. (point))))
  5826. (level (looking-at "\\*+"))
  5827. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5828. (save-excursion
  5829. (save-restriction
  5830. (narrow-to-region beg end)
  5831. (when re
  5832. ;; Properly fold already folded siblings
  5833. (goto-char (point-min))
  5834. (while (re-search-forward re nil t)
  5835. (if (and (not (outline-invisible-p))
  5836. (save-excursion
  5837. (goto-char (point-at-eol)) (outline-invisible-p)))
  5838. (hide-entry))))
  5839. (org-cycle-show-empty-lines 'overview)
  5840. (org-cycle-hide-drawers 'overview)))))
  5841. (defun org-cycle-show-empty-lines (state)
  5842. "Show empty lines above all visible headlines.
  5843. The region to be covered depends on STATE when called through
  5844. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5845. entire buffer covered. Note that an empty line is only shown if there
  5846. are at least `org-cycle-separator-lines' empty lines before the headline."
  5847. (when (not (= org-cycle-separator-lines 0))
  5848. (save-excursion
  5849. (let* ((n (abs org-cycle-separator-lines))
  5850. (re (cond
  5851. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5852. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5853. (t (let ((ns (number-to-string (- n 2))))
  5854. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5855. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5856. beg end b e)
  5857. (cond
  5858. ((memq state '(overview contents t))
  5859. (setq beg (point-min) end (point-max)))
  5860. ((memq state '(children folded))
  5861. (setq beg (point) end (progn (org-end-of-subtree t t)
  5862. (beginning-of-line 2)
  5863. (point)))))
  5864. (when beg
  5865. (goto-char beg)
  5866. (while (re-search-forward re end t)
  5867. (unless (get-char-property (match-end 1) 'invisible)
  5868. (setq e (match-end 1))
  5869. (if (< org-cycle-separator-lines 0)
  5870. (setq b (save-excursion
  5871. (goto-char (match-beginning 0))
  5872. (org-back-over-empty-lines)
  5873. (if (save-excursion
  5874. (goto-char (max (point-min) (1- (point))))
  5875. (org-at-heading-p))
  5876. (1- (point))
  5877. (point))))
  5878. (setq b (match-beginning 1)))
  5879. (outline-flag-region b e nil)))))))
  5880. ;; Never hide empty lines at the end of the file.
  5881. (save-excursion
  5882. (goto-char (point-max))
  5883. (outline-previous-heading)
  5884. (outline-end-of-heading)
  5885. (if (and (looking-at "[ \t\n]+")
  5886. (= (match-end 0) (point-max)))
  5887. (outline-flag-region (point) (match-end 0) nil))))
  5888. (defun org-show-empty-lines-in-parent ()
  5889. "Move to the parent and re-show empty lines before visible headlines."
  5890. (save-excursion
  5891. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5892. (org-cycle-show-empty-lines context))))
  5893. (defun org-files-list ()
  5894. "Return `org-agenda-files' list, plus all open org-mode files.
  5895. This is useful for operations that need to scan all of a user's
  5896. open and agenda-wise Org files."
  5897. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5898. (dolist (buf (buffer-list))
  5899. (with-current-buffer buf
  5900. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5901. (let ((file (expand-file-name (buffer-file-name))))
  5902. (unless (member file files)
  5903. (push file files))))))
  5904. files))
  5905. (defsubst org-entry-beginning-position ()
  5906. "Return the beginning position of the current entry."
  5907. (save-excursion (outline-back-to-heading t) (point)))
  5908. (defsubst org-entry-end-position ()
  5909. "Return the end position of the current entry."
  5910. (save-excursion (outline-next-heading) (point)))
  5911. (defun org-cycle-hide-drawers (state)
  5912. "Re-hide all drawers after a visibility state change."
  5913. (when (and (eq major-mode 'org-mode)
  5914. (not (memq state '(overview folded contents))))
  5915. (save-excursion
  5916. (let* ((globalp (memq state '(contents all)))
  5917. (beg (if globalp (point-min) (point)))
  5918. (end (if globalp (point-max)
  5919. (if (eq state 'children)
  5920. (save-excursion (outline-next-heading) (point))
  5921. (org-end-of-subtree t)))))
  5922. (goto-char beg)
  5923. (while (re-search-forward org-drawer-regexp end t)
  5924. (org-flag-drawer t))))))
  5925. (defun org-flag-drawer (flag)
  5926. (save-excursion
  5927. (beginning-of-line 1)
  5928. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5929. (let ((b (match-end 0)))
  5930. (if (re-search-forward
  5931. "^[ \t]*:END:"
  5932. (save-excursion (outline-next-heading) (point)) t)
  5933. (outline-flag-region b (point-at-eol) flag)
  5934. (error ":END: line missing at position %s" b))))))
  5935. (defun org-subtree-end-visible-p ()
  5936. "Is the end of the current subtree visible?"
  5937. (pos-visible-in-window-p
  5938. (save-excursion (org-end-of-subtree t) (point))))
  5939. (defun org-first-headline-recenter (&optional N)
  5940. "Move cursor to the first headline and recenter the headline.
  5941. Optional argument N means put the headline into the Nth line of the window."
  5942. (goto-char (point-min))
  5943. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5944. (beginning-of-line)
  5945. (recenter (prefix-numeric-value N))))
  5946. ;;; Saving and restoring visibility
  5947. (defun org-outline-overlay-data (&optional use-markers)
  5948. "Return a list of the locations of all outline overlays.
  5949. These are overlays with the `invisible' property value `outline'.
  5950. The return value is a list of cons cells, with start and stop
  5951. positions for each overlay.
  5952. If USE-MARKERS is set, return the positions as markers."
  5953. (let (beg end)
  5954. (save-excursion
  5955. (save-restriction
  5956. (widen)
  5957. (delq nil
  5958. (mapcar (lambda (o)
  5959. (when (eq (overlay-get o 'invisible) 'outline)
  5960. (setq beg (overlay-start o)
  5961. end (overlay-end o))
  5962. (and beg end (> end beg)
  5963. (if use-markers
  5964. (cons (move-marker (make-marker) beg)
  5965. (move-marker (make-marker) end))
  5966. (cons beg end)))))
  5967. (overlays-in (point-min) (point-max))))))))
  5968. (defun org-set-outline-overlay-data (data)
  5969. "Create visibility overlays for all positions in DATA.
  5970. DATA should have been made by `org-outline-overlay-data'."
  5971. (let (o)
  5972. (save-excursion
  5973. (save-restriction
  5974. (widen)
  5975. (show-all)
  5976. (mapc (lambda (c)
  5977. (outline-flag-region (car c) (cdr c) t))
  5978. data)))))
  5979. ;;; Folding of blocks
  5980. (defvar org-hide-block-overlays nil
  5981. "Overlays hiding blocks.")
  5982. (make-variable-buffer-local 'org-hide-block-overlays)
  5983. (defun org-block-map (function &optional start end)
  5984. "Call FUNCTION at the head of all source blocks in the current buffer.
  5985. Optional arguments START and END can be used to limit the range."
  5986. (let ((start (or start (point-min)))
  5987. (end (or end (point-max))))
  5988. (save-excursion
  5989. (goto-char start)
  5990. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5991. (save-excursion
  5992. (save-match-data
  5993. (goto-char (match-beginning 0))
  5994. (funcall function)))))))
  5995. (defun org-hide-block-toggle-all ()
  5996. "Toggle the visibility of all blocks in the current buffer."
  5997. (org-block-map #'org-hide-block-toggle))
  5998. (defun org-hide-block-all ()
  5999. "Fold all blocks in the current buffer."
  6000. (interactive)
  6001. (org-show-block-all)
  6002. (org-block-map #'org-hide-block-toggle-maybe))
  6003. (defun org-show-block-all ()
  6004. "Unfold all blocks in the current buffer."
  6005. (interactive)
  6006. (mapc 'delete-overlay org-hide-block-overlays)
  6007. (setq org-hide-block-overlays nil))
  6008. (defun org-hide-block-toggle-maybe ()
  6009. "Toggle visibility of block at point."
  6010. (interactive)
  6011. (let ((case-fold-search t))
  6012. (if (save-excursion
  6013. (beginning-of-line 1)
  6014. (looking-at org-block-regexp))
  6015. (progn (org-hide-block-toggle)
  6016. t) ;; to signal that we took action
  6017. nil))) ;; to signal that we did not
  6018. (defun org-hide-block-toggle (&optional force)
  6019. "Toggle the visibility of the current block."
  6020. (interactive)
  6021. (save-excursion
  6022. (beginning-of-line)
  6023. (if (re-search-forward org-block-regexp nil t)
  6024. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6025. (end (match-end 0)) ;; end of entire body
  6026. ov)
  6027. (if (memq t (mapcar (lambda (overlay)
  6028. (eq (overlay-get overlay 'invisible)
  6029. 'org-hide-block))
  6030. (overlays-at start)))
  6031. (if (or (not force) (eq force 'off))
  6032. (mapc (lambda (ov)
  6033. (when (member ov org-hide-block-overlays)
  6034. (setq org-hide-block-overlays
  6035. (delq ov org-hide-block-overlays)))
  6036. (when (eq (overlay-get ov 'invisible)
  6037. 'org-hide-block)
  6038. (delete-overlay ov)))
  6039. (overlays-at start)))
  6040. (setq ov (make-overlay start end))
  6041. (overlay-put ov 'invisible 'org-hide-block)
  6042. ;; make the block accessible to isearch
  6043. (overlay-put
  6044. ov 'isearch-open-invisible
  6045. (lambda (ov)
  6046. (when (member ov org-hide-block-overlays)
  6047. (setq org-hide-block-overlays
  6048. (delq ov org-hide-block-overlays)))
  6049. (when (eq (overlay-get ov 'invisible)
  6050. 'org-hide-block)
  6051. (delete-overlay ov))))
  6052. (push ov org-hide-block-overlays)))
  6053. (error "Not looking at a source block"))))
  6054. ;; org-tab-after-check-for-cycling-hook
  6055. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6056. ;; Remove overlays when changing major mode
  6057. (add-hook 'org-mode-hook
  6058. (lambda () (org-add-hook 'change-major-mode-hook
  6059. 'org-show-block-all 'append 'local)))
  6060. ;;; Org-goto
  6061. (defvar org-goto-window-configuration nil)
  6062. (defvar org-goto-marker nil)
  6063. (defvar org-goto-map
  6064. (let ((map (make-sparse-keymap)))
  6065. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6066. (while (setq cmd (pop cmds))
  6067. (substitute-key-definition cmd cmd map global-map)))
  6068. (suppress-keymap map)
  6069. (org-defkey map "\C-m" 'org-goto-ret)
  6070. (org-defkey map [(return)] 'org-goto-ret)
  6071. (org-defkey map [(left)] 'org-goto-left)
  6072. (org-defkey map [(right)] 'org-goto-right)
  6073. (org-defkey map [(control ?g)] 'org-goto-quit)
  6074. (org-defkey map "\C-i" 'org-cycle)
  6075. (org-defkey map [(tab)] 'org-cycle)
  6076. (org-defkey map [(down)] 'outline-next-visible-heading)
  6077. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6078. (if org-goto-auto-isearch
  6079. (if (fboundp 'define-key-after)
  6080. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6081. nil)
  6082. (org-defkey map "q" 'org-goto-quit)
  6083. (org-defkey map "n" 'outline-next-visible-heading)
  6084. (org-defkey map "p" 'outline-previous-visible-heading)
  6085. (org-defkey map "f" 'outline-forward-same-level)
  6086. (org-defkey map "b" 'outline-backward-same-level)
  6087. (org-defkey map "u" 'outline-up-heading))
  6088. (org-defkey map "/" 'org-occur)
  6089. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6090. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6091. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6092. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6093. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6094. map))
  6095. (defconst org-goto-help
  6096. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6097. RET=jump to location [Q]uit and return to previous location
  6098. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6099. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6100. ;; FIXME: Docstring does not mention both interfaces
  6101. (defun org-goto (&optional alternative-interface)
  6102. "Look up a different location in the current file, keeping current visibility.
  6103. When you want look-up or go to a different location in a document, the
  6104. fastest way is often to fold the entire buffer and then dive into the tree.
  6105. This method has the disadvantage, that the previous location will be folded,
  6106. which may not be what you want.
  6107. This command works around this by showing a copy of the current buffer
  6108. in an indirect buffer, in overview mode. You can dive into the tree in
  6109. that copy, use org-occur and incremental search to find a location.
  6110. When pressing RET or `Q', the command returns to the original buffer in
  6111. which the visibility is still unchanged. After RET is will also jump to
  6112. the location selected in the indirect buffer and expose the headline
  6113. hierarchy above."
  6114. (interactive "P")
  6115. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6116. (org-refile-use-outline-path t)
  6117. (org-refile-target-verify-function nil)
  6118. (interface
  6119. (if (not alternative-interface)
  6120. org-goto-interface
  6121. (if (eq org-goto-interface 'outline)
  6122. 'outline-path-completion
  6123. 'outline)))
  6124. (org-goto-start-pos (point))
  6125. (selected-point
  6126. (if (eq interface 'outline)
  6127. (car (org-get-location (current-buffer) org-goto-help))
  6128. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6129. (org-refile-check-position pa)
  6130. (nth 3 pa)))))
  6131. (if selected-point
  6132. (progn
  6133. (org-mark-ring-push org-goto-start-pos)
  6134. (goto-char selected-point)
  6135. (if (or (outline-invisible-p) (org-invisible-p2))
  6136. (org-show-context 'org-goto)))
  6137. (message "Quit"))))
  6138. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6139. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6140. (defvar org-goto-local-auto-isearch-map) ; defined below
  6141. (defun org-get-location (buf help)
  6142. "Let the user select a location in the Org-mode buffer BUF.
  6143. This function uses a recursive edit. It returns the selected position
  6144. or nil."
  6145. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6146. (isearch-hide-immediately nil)
  6147. (isearch-search-fun-function
  6148. (lambda () 'org-goto-local-search-headings))
  6149. (org-goto-selected-point org-goto-exit-command)
  6150. (pop-up-frames nil)
  6151. (special-display-buffer-names nil)
  6152. (special-display-regexps nil)
  6153. (special-display-function nil))
  6154. (save-excursion
  6155. (save-window-excursion
  6156. (delete-other-windows)
  6157. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6158. (org-pop-to-buffer-same-window
  6159. (condition-case nil
  6160. (make-indirect-buffer (current-buffer) "*org-goto*")
  6161. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6162. (with-output-to-temp-buffer "*Help*"
  6163. (princ help))
  6164. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6165. (setq buffer-read-only nil)
  6166. (let ((org-startup-truncated t)
  6167. (org-startup-folded nil)
  6168. (org-startup-align-all-tables nil))
  6169. (org-mode)
  6170. (org-overview))
  6171. (setq buffer-read-only t)
  6172. (if (and (boundp 'org-goto-start-pos)
  6173. (integer-or-marker-p org-goto-start-pos))
  6174. (let ((org-show-hierarchy-above t)
  6175. (org-show-siblings t)
  6176. (org-show-following-heading t))
  6177. (goto-char org-goto-start-pos)
  6178. (and (outline-invisible-p) (org-show-context)))
  6179. (goto-char (point-min)))
  6180. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6181. (message "Select location and press RET")
  6182. (use-local-map org-goto-map)
  6183. (recursive-edit)
  6184. ))
  6185. (kill-buffer "*org-goto*")
  6186. (cons org-goto-selected-point org-goto-exit-command)))
  6187. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6188. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6189. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6190. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6191. (defun org-goto-local-search-headings (string bound noerror)
  6192. "Search and make sure that any matches are in headlines."
  6193. (catch 'return
  6194. (while (if isearch-forward
  6195. (search-forward string bound noerror)
  6196. (search-backward string bound noerror))
  6197. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6198. (and (member :headline context)
  6199. (not (member :tags context))))
  6200. (throw 'return (point))))))
  6201. (defun org-goto-local-auto-isearch ()
  6202. "Start isearch."
  6203. (interactive)
  6204. (goto-char (point-min))
  6205. (let ((keys (this-command-keys)))
  6206. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6207. (isearch-mode t)
  6208. (isearch-process-search-char (string-to-char keys)))))
  6209. (defun org-goto-ret (&optional arg)
  6210. "Finish `org-goto' by going to the new location."
  6211. (interactive "P")
  6212. (setq org-goto-selected-point (point)
  6213. org-goto-exit-command 'return)
  6214. (throw 'exit nil))
  6215. (defun org-goto-left ()
  6216. "Finish `org-goto' by going to the new location."
  6217. (interactive)
  6218. (if (org-at-heading-p)
  6219. (progn
  6220. (beginning-of-line 1)
  6221. (setq org-goto-selected-point (point)
  6222. org-goto-exit-command 'left)
  6223. (throw 'exit nil))
  6224. (error "Not on a heading")))
  6225. (defun org-goto-right ()
  6226. "Finish `org-goto' by going to the new location."
  6227. (interactive)
  6228. (if (org-at-heading-p)
  6229. (progn
  6230. (setq org-goto-selected-point (point)
  6231. org-goto-exit-command 'right)
  6232. (throw 'exit nil))
  6233. (error "Not on a heading")))
  6234. (defun org-goto-quit ()
  6235. "Finish `org-goto' without cursor motion."
  6236. (interactive)
  6237. (setq org-goto-selected-point nil)
  6238. (setq org-goto-exit-command 'quit)
  6239. (throw 'exit nil))
  6240. ;;; Indirect buffer display of subtrees
  6241. (defvar org-indirect-dedicated-frame nil
  6242. "This is the frame being used for indirect tree display.")
  6243. (defvar org-last-indirect-buffer nil)
  6244. (defun org-tree-to-indirect-buffer (&optional arg)
  6245. "Create indirect buffer and narrow it to current subtree.
  6246. With numerical prefix ARG, go up to this level and then take that tree.
  6247. If ARG is negative, go up that many levels.
  6248. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6249. indirect buffer previously made with this command, to avoid proliferation of
  6250. indirect buffers. However, when you call the command with a \
  6251. \\[universal-argument] prefix, or
  6252. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6253. is kept so that you can work with several indirect buffers at the same time.
  6254. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6255. \\[universal-argument] prefix also
  6256. requests that a new frame be made for the new buffer, so that the dedicated
  6257. frame is not changed."
  6258. (interactive "P")
  6259. (let ((cbuf (current-buffer))
  6260. (cwin (selected-window))
  6261. (pos (point))
  6262. beg end level heading ibuf)
  6263. (save-excursion
  6264. (org-back-to-heading t)
  6265. (when (numberp arg)
  6266. (setq level (org-outline-level))
  6267. (if (< arg 0) (setq arg (+ level arg)))
  6268. (while (> (setq level (org-outline-level)) arg)
  6269. (outline-up-heading 1 t)))
  6270. (setq beg (point)
  6271. heading (org-get-heading))
  6272. (org-end-of-subtree t t)
  6273. (if (org-at-heading-p) (backward-char 1))
  6274. (setq end (point)))
  6275. (if (and (buffer-live-p org-last-indirect-buffer)
  6276. (not (eq org-indirect-buffer-display 'new-frame))
  6277. (not arg))
  6278. (kill-buffer org-last-indirect-buffer))
  6279. (setq ibuf (org-get-indirect-buffer cbuf)
  6280. org-last-indirect-buffer ibuf)
  6281. (cond
  6282. ((or (eq org-indirect-buffer-display 'new-frame)
  6283. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6284. (select-frame (make-frame))
  6285. (delete-other-windows)
  6286. (org-pop-to-buffer-same-window ibuf)
  6287. (org-set-frame-title heading))
  6288. ((eq org-indirect-buffer-display 'dedicated-frame)
  6289. (raise-frame
  6290. (select-frame (or (and org-indirect-dedicated-frame
  6291. (frame-live-p org-indirect-dedicated-frame)
  6292. org-indirect-dedicated-frame)
  6293. (setq org-indirect-dedicated-frame (make-frame)))))
  6294. (delete-other-windows)
  6295. (org-pop-to-buffer-same-window ibuf)
  6296. (org-set-frame-title (concat "Indirect: " heading)))
  6297. ((eq org-indirect-buffer-display 'current-window)
  6298. (org-pop-to-buffer-same-window ibuf))
  6299. ((eq org-indirect-buffer-display 'other-window)
  6300. (pop-to-buffer ibuf))
  6301. (t (error "Invalid value")))
  6302. (if (featurep 'xemacs)
  6303. (save-excursion (org-mode) (turn-on-font-lock)))
  6304. (narrow-to-region beg end)
  6305. (show-all)
  6306. (goto-char pos)
  6307. (run-hook-with-args 'org-cycle-hook 'all)
  6308. (and (window-live-p cwin) (select-window cwin))))
  6309. (defun org-get-indirect-buffer (&optional buffer)
  6310. (setq buffer (or buffer (current-buffer)))
  6311. (let ((n 1) (base (buffer-name buffer)) bname)
  6312. (while (buffer-live-p
  6313. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6314. (setq n (1+ n)))
  6315. (condition-case nil
  6316. (make-indirect-buffer buffer bname 'clone)
  6317. (error (make-indirect-buffer buffer bname)))))
  6318. (defun org-set-frame-title (title)
  6319. "Set the title of the current frame to the string TITLE."
  6320. ;; FIXME: how to name a single frame in XEmacs???
  6321. (unless (featurep 'xemacs)
  6322. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6323. ;;;; Structure editing
  6324. ;;; Inserting headlines
  6325. (defun org-previous-line-empty-p ()
  6326. (save-excursion
  6327. (and (not (bobp))
  6328. (or (beginning-of-line 0) t)
  6329. (save-match-data
  6330. (looking-at "[ \t]*$")))))
  6331. (defun org-insert-heading (&optional force-heading invisible-ok)
  6332. "Insert a new heading or item with same depth at point.
  6333. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6334. If point is at the beginning of a headline, insert a sibling before the
  6335. current headline. If point is not at the beginning, split the line,
  6336. create the new headline with the text in the current line after point
  6337. \(but see also the variable `org-M-RET-may-split-line').
  6338. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6339. This is important for non-interactive uses of the command."
  6340. (interactive "P")
  6341. (if (or (= (buffer-size) 0)
  6342. (and (not (save-excursion
  6343. (and (ignore-errors (org-back-to-heading invisible-ok))
  6344. (org-at-heading-p))))
  6345. (or force-heading (not (org-in-item-p)))))
  6346. (progn
  6347. (insert "\n* ")
  6348. (run-hooks 'org-insert-heading-hook))
  6349. (when (or force-heading (not (org-insert-item)))
  6350. (let* ((empty-line-p nil)
  6351. (level nil)
  6352. (on-heading (org-at-heading-p))
  6353. (head (save-excursion
  6354. (condition-case nil
  6355. (progn
  6356. (org-back-to-heading invisible-ok)
  6357. (when (and (not on-heading)
  6358. (featurep 'org-inlinetask)
  6359. (integerp org-inlinetask-min-level)
  6360. (>= (length (match-string 0))
  6361. org-inlinetask-min-level))
  6362. ;; Find a heading level before the inline task
  6363. (while (and (setq level (org-up-heading-safe))
  6364. (>= level org-inlinetask-min-level)))
  6365. (if (org-at-heading-p)
  6366. (org-back-to-heading invisible-ok)
  6367. (error "This should not happen")))
  6368. (setq empty-line-p (org-previous-line-empty-p))
  6369. (match-string 0))
  6370. (error "*"))))
  6371. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6372. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6373. pos hide-previous previous-pos)
  6374. (cond
  6375. ((and (org-at-heading-p) (bolp)
  6376. (or (bobp)
  6377. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6378. ;; insert before the current line
  6379. (open-line (if blank 2 1)))
  6380. ((and (bolp)
  6381. (not org-insert-heading-respect-content)
  6382. (or (bobp)
  6383. (save-excursion
  6384. (backward-char 1) (not (outline-invisible-p)))))
  6385. ;; insert right here
  6386. nil)
  6387. (t
  6388. ;; somewhere in the line
  6389. (save-excursion
  6390. (setq previous-pos (point-at-bol))
  6391. (end-of-line)
  6392. (setq hide-previous (outline-invisible-p)))
  6393. (and org-insert-heading-respect-content (org-show-subtree))
  6394. (let ((split
  6395. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6396. (save-excursion
  6397. (let ((p (point)))
  6398. (goto-char (point-at-bol))
  6399. (and (looking-at org-complex-heading-regexp)
  6400. (match-beginning 4)
  6401. (> p (match-beginning 4)))))))
  6402. tags pos)
  6403. (cond
  6404. (org-insert-heading-respect-content
  6405. (org-end-of-subtree nil t)
  6406. (when (featurep 'org-inlinetask)
  6407. (while (and (not (eobp))
  6408. (looking-at "\\(\\*+\\)[ \t]+")
  6409. (>= (length (match-string 1))
  6410. org-inlinetask-min-level))
  6411. (org-end-of-subtree nil t)))
  6412. (or (bolp) (newline))
  6413. (or (org-previous-line-empty-p)
  6414. (and blank (newline)))
  6415. (open-line 1))
  6416. ((org-at-heading-p)
  6417. (when hide-previous
  6418. (show-children)
  6419. (org-show-entry))
  6420. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6421. (setq tags (and (match-end 2) (match-string 2)))
  6422. (and (match-end 1)
  6423. (delete-region (match-beginning 1) (match-end 1)))
  6424. (setq pos (point-at-bol))
  6425. (or split (end-of-line 1))
  6426. (delete-horizontal-space)
  6427. (if (string-match "\\`\\*+\\'"
  6428. (buffer-substring (point-at-bol) (point)))
  6429. (insert " "))
  6430. (newline (if blank 2 1))
  6431. (when tags
  6432. (save-excursion
  6433. (goto-char pos)
  6434. (end-of-line 1)
  6435. (insert " " tags)
  6436. (org-set-tags nil 'align))))
  6437. (t
  6438. (or split (end-of-line 1))
  6439. (newline (if blank 2 1)))))))
  6440. (insert head) (just-one-space)
  6441. (setq pos (point))
  6442. (end-of-line 1)
  6443. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6444. (when (and org-insert-heading-respect-content hide-previous)
  6445. (save-excursion
  6446. (goto-char previous-pos)
  6447. (hide-subtree)))
  6448. (run-hooks 'org-insert-heading-hook)))))
  6449. (defun org-get-heading (&optional no-tags no-todo)
  6450. "Return the heading of the current entry, without the stars.
  6451. When NO-TAGS is non-nil, don't include tags.
  6452. When NO-TODO is non-nil, don't include TODO keywords."
  6453. (save-excursion
  6454. (org-back-to-heading t)
  6455. (cond
  6456. ((and no-tags no-todo)
  6457. (looking-at org-complex-heading-regexp)
  6458. (match-string 4))
  6459. (no-tags
  6460. (looking-at (concat org-outline-regexp
  6461. "\\(.*?\\)"
  6462. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6463. (match-string 1))
  6464. (no-todo
  6465. (looking-at org-todo-line-regexp)
  6466. (match-string 3))
  6467. (t (looking-at org-heading-regexp)
  6468. (match-string 2)))))
  6469. (defun org-heading-components ()
  6470. "Return the components of the current heading.
  6471. This is a list with the following elements:
  6472. - the level as an integer
  6473. - the reduced level, different if `org-odd-levels-only' is set.
  6474. - the TODO keyword, or nil
  6475. - the priority character, like ?A, or nil if no priority is given
  6476. - the headline text itself, or the tags string if no headline text
  6477. - the tags string, or nil."
  6478. (save-excursion
  6479. (org-back-to-heading t)
  6480. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6481. (list (length (match-string 1))
  6482. (org-reduced-level (length (match-string 1)))
  6483. (org-match-string-no-properties 2)
  6484. (and (match-end 3) (aref (match-string 3) 2))
  6485. (org-match-string-no-properties 4)
  6486. (org-match-string-no-properties 5)))))
  6487. (defun org-get-entry ()
  6488. "Get the entry text, after heading, entire subtree."
  6489. (save-excursion
  6490. (org-back-to-heading t)
  6491. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6492. (defun org-insert-heading-after-current ()
  6493. "Insert a new heading with same level as current, after current subtree."
  6494. (interactive)
  6495. (org-back-to-heading)
  6496. (org-insert-heading)
  6497. (org-move-subtree-down)
  6498. (end-of-line 1))
  6499. (defun org-insert-heading-respect-content ()
  6500. (interactive)
  6501. (let ((org-insert-heading-respect-content t))
  6502. (org-insert-heading t)))
  6503. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6504. (interactive "P")
  6505. (let ((org-insert-heading-respect-content t))
  6506. (org-insert-todo-heading force-state t)))
  6507. (defun org-insert-todo-heading (arg &optional force-heading)
  6508. "Insert a new heading with the same level and TODO state as current heading.
  6509. If the heading has no TODO state, or if the state is DONE, use the first
  6510. state (TODO by default). Also with prefix arg, force first state."
  6511. (interactive "P")
  6512. (when (or force-heading (not (org-insert-item 'checkbox)))
  6513. (org-insert-heading force-heading)
  6514. (save-excursion
  6515. (org-back-to-heading)
  6516. (outline-previous-heading)
  6517. (looking-at org-todo-line-regexp))
  6518. (let*
  6519. ((new-mark-x
  6520. (if (or arg
  6521. (not (match-beginning 2))
  6522. (member (match-string 2) org-done-keywords))
  6523. (car org-todo-keywords-1)
  6524. (match-string 2)))
  6525. (new-mark
  6526. (or
  6527. (run-hook-with-args-until-success
  6528. 'org-todo-get-default-hook new-mark-x nil)
  6529. new-mark-x)))
  6530. (beginning-of-line 1)
  6531. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6532. (if org-treat-insert-todo-heading-as-state-change
  6533. (org-todo new-mark)
  6534. (insert new-mark " "))))
  6535. (when org-provide-todo-statistics
  6536. (org-update-parent-todo-statistics))))
  6537. (defun org-insert-subheading (arg)
  6538. "Insert a new subheading and demote it.
  6539. Works for outline headings and for plain lists alike."
  6540. (interactive "P")
  6541. (org-insert-heading arg)
  6542. (cond
  6543. ((org-at-heading-p) (org-do-demote))
  6544. ((org-at-item-p) (org-indent-item))))
  6545. (defun org-insert-todo-subheading (arg)
  6546. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6547. Works for outline headings and for plain lists alike."
  6548. (interactive "P")
  6549. (org-insert-todo-heading arg)
  6550. (cond
  6551. ((org-at-heading-p) (org-do-demote))
  6552. ((org-at-item-p) (org-indent-item))))
  6553. ;;; Promotion and Demotion
  6554. (defvar org-after-demote-entry-hook nil
  6555. "Hook run after an entry has been demoted.
  6556. The cursor will be at the beginning of the entry.
  6557. When a subtree is being demoted, the hook will be called for each node.")
  6558. (defvar org-after-promote-entry-hook nil
  6559. "Hook run after an entry has been promoted.
  6560. The cursor will be at the beginning of the entry.
  6561. When a subtree is being promoted, the hook will be called for each node.")
  6562. (defun org-promote-subtree ()
  6563. "Promote the entire subtree.
  6564. See also `org-promote'."
  6565. (interactive)
  6566. (save-excursion
  6567. (org-with-limited-levels (org-map-tree 'org-promote)))
  6568. (org-fix-position-after-promote))
  6569. (defun org-demote-subtree ()
  6570. "Demote the entire subtree. See `org-demote'.
  6571. See also `org-promote'."
  6572. (interactive)
  6573. (save-excursion
  6574. (org-with-limited-levels (org-map-tree 'org-demote)))
  6575. (org-fix-position-after-promote))
  6576. (defun org-do-promote ()
  6577. "Promote the current heading higher up the tree.
  6578. If the region is active in `transient-mark-mode', promote all headings
  6579. in the region."
  6580. (interactive)
  6581. (save-excursion
  6582. (if (org-region-active-p)
  6583. (org-map-region 'org-promote (region-beginning) (region-end))
  6584. (org-promote)))
  6585. (org-fix-position-after-promote))
  6586. (defun org-do-demote ()
  6587. "Demote the current heading lower down the tree.
  6588. If the region is active in `transient-mark-mode', demote all headings
  6589. in the region."
  6590. (interactive)
  6591. (save-excursion
  6592. (if (org-region-active-p)
  6593. (org-map-region 'org-demote (region-beginning) (region-end))
  6594. (org-demote)))
  6595. (org-fix-position-after-promote))
  6596. (defun org-fix-position-after-promote ()
  6597. "Make sure that after pro/demotion cursor position is right."
  6598. (let ((pos (point)))
  6599. (when (save-excursion
  6600. (beginning-of-line 1)
  6601. (looking-at org-todo-line-regexp)
  6602. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6603. (cond ((eobp) (insert " "))
  6604. ((eolp) (insert " "))
  6605. ((equal (char-after) ?\ ) (forward-char 1))))))
  6606. (defun org-current-level ()
  6607. "Return the level of the current entry, or nil if before the first headline.
  6608. The level is the number of stars at the beginning of the headline."
  6609. (save-excursion
  6610. (org-with-limited-levels
  6611. (if (ignore-errors (org-back-to-heading t))
  6612. (funcall outline-level)))))
  6613. (defun org-get-previous-line-level ()
  6614. "Return the outline depth of the last headline before the current line.
  6615. Returns 0 for the first headline in the buffer, and nil if before the
  6616. first headline."
  6617. (let ((current-level (org-current-level))
  6618. (prev-level (when (> (line-number-at-pos) 1)
  6619. (save-excursion
  6620. (beginning-of-line 0)
  6621. (org-current-level)))))
  6622. (cond ((null current-level) nil) ; Before first headline
  6623. ((null prev-level) 0) ; At first headline
  6624. (prev-level))))
  6625. (defun org-reduced-level (l)
  6626. "Compute the effective level of a heading.
  6627. This takes into account the setting of `org-odd-levels-only'."
  6628. (cond
  6629. ((zerop l) 0)
  6630. (org-odd-levels-only (1+ (floor (/ l 2))))
  6631. (t l)))
  6632. (defun org-level-increment ()
  6633. "Return the number of stars that will be added or removed at a
  6634. time to headlines when structure editing, based on the value of
  6635. `org-odd-levels-only'."
  6636. (if org-odd-levels-only 2 1))
  6637. (defun org-get-valid-level (level &optional change)
  6638. "Rectify a level change under the influence of `org-odd-levels-only'
  6639. LEVEL is a current level, CHANGE is by how much the level should be
  6640. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6641. even level numbers will become the next higher odd number."
  6642. (if org-odd-levels-only
  6643. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6644. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6645. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6646. (max 1 (+ level (or change 0)))))
  6647. (if (boundp 'define-obsolete-function-alias)
  6648. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6649. (define-obsolete-function-alias 'org-get-legal-level
  6650. 'org-get-valid-level)
  6651. (define-obsolete-function-alias 'org-get-legal-level
  6652. 'org-get-valid-level "23.1")))
  6653. (defun org-promote ()
  6654. "Promote the current heading higher up the tree.
  6655. If the region is active in `transient-mark-mode', promote all headings
  6656. in the region."
  6657. (org-back-to-heading t)
  6658. (let* ((level (save-match-data (funcall outline-level)))
  6659. (after-change-functions (remove 'flyspell-after-change-function
  6660. after-change-functions))
  6661. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6662. (diff (abs (- level (length up-head) -1))))
  6663. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6664. (replace-match up-head nil t)
  6665. ;; Fixup tag positioning
  6666. (and org-auto-align-tags (org-set-tags nil t))
  6667. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6668. (run-hooks 'org-after-promote-entry-hook)))
  6669. (defun org-demote ()
  6670. "Demote the current heading lower down the tree.
  6671. If the region is active in `transient-mark-mode', demote all headings
  6672. in the region."
  6673. (org-back-to-heading t)
  6674. (let* ((level (save-match-data (funcall outline-level)))
  6675. (after-change-functions (remove 'flyspell-after-change-function
  6676. after-change-functions))
  6677. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6678. (diff (abs (- level (length down-head) -1))))
  6679. (replace-match down-head nil t)
  6680. ;; Fixup tag positioning
  6681. (and org-auto-align-tags (org-set-tags nil t))
  6682. (if org-adapt-indentation (org-fixup-indentation diff))
  6683. (run-hooks 'org-after-demote-entry-hook)))
  6684. (defun org-cycle-level ()
  6685. "Cycle the level of an empty headline through possible states.
  6686. This goes first to child, then to parent, level, then up the hierarchy.
  6687. After top level, it switches back to sibling level."
  6688. (interactive)
  6689. (let ((org-adapt-indentation nil))
  6690. (when (org-point-at-end-of-empty-headline)
  6691. (setq this-command 'org-cycle-level) ; Only needed for caching
  6692. (let ((cur-level (org-current-level))
  6693. (prev-level (org-get-previous-line-level)))
  6694. (cond
  6695. ;; If first headline in file, promote to top-level.
  6696. ((= prev-level 0)
  6697. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6698. do (org-do-promote)))
  6699. ;; If same level as prev, demote one.
  6700. ((= prev-level cur-level)
  6701. (org-do-demote))
  6702. ;; If parent is top-level, promote to top level if not already.
  6703. ((= prev-level 1)
  6704. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6705. do (org-do-promote)))
  6706. ;; If top-level, return to prev-level.
  6707. ((= cur-level 1)
  6708. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6709. do (org-do-demote)))
  6710. ;; If less than prev-level, promote one.
  6711. ((< cur-level prev-level)
  6712. (org-do-promote))
  6713. ;; If deeper than prev-level, promote until higher than
  6714. ;; prev-level.
  6715. ((> cur-level prev-level)
  6716. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6717. do (org-do-promote))))
  6718. t))))
  6719. (defun org-map-tree (fun)
  6720. "Call FUN for every heading underneath the current one."
  6721. (org-back-to-heading)
  6722. (let ((level (funcall outline-level)))
  6723. (save-excursion
  6724. (funcall fun)
  6725. (while (and (progn
  6726. (outline-next-heading)
  6727. (> (funcall outline-level) level))
  6728. (not (eobp)))
  6729. (funcall fun)))))
  6730. (defun org-map-region (fun beg end)
  6731. "Call FUN for every heading between BEG and END."
  6732. (let ((org-ignore-region t))
  6733. (save-excursion
  6734. (setq end (copy-marker end))
  6735. (goto-char beg)
  6736. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6737. (< (point) end))
  6738. (funcall fun))
  6739. (while (and (progn
  6740. (outline-next-heading)
  6741. (< (point) end))
  6742. (not (eobp)))
  6743. (funcall fun)))))
  6744. (defvar org-property-end-re) ; silence byte-compiler
  6745. (defun org-fixup-indentation (diff)
  6746. "Change the indentation in the current entry by DIFF.
  6747. However, if any line in the current entry has no indentation, or if it
  6748. would end up with no indentation after the change, nothing at all is done."
  6749. (save-excursion
  6750. (let ((end (save-excursion (outline-next-heading)
  6751. (point-marker)))
  6752. (prohibit (if (> diff 0)
  6753. "^\\S-"
  6754. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6755. col)
  6756. (unless (save-excursion (end-of-line 1)
  6757. (re-search-forward prohibit end t))
  6758. (while (and (< (point) end)
  6759. (re-search-forward "^[ \t]+" end t))
  6760. (goto-char (match-end 0))
  6761. (setq col (current-column))
  6762. (if (< diff 0) (replace-match ""))
  6763. (org-indent-to-column (+ diff col))))
  6764. (move-marker end nil))))
  6765. (defun org-convert-to-odd-levels ()
  6766. "Convert an org-mode file with all levels allowed to one with odd levels.
  6767. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6768. level 5 etc."
  6769. (interactive)
  6770. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6771. (let ((outline-level 'org-outline-level)
  6772. (org-odd-levels-only nil) n)
  6773. (save-excursion
  6774. (goto-char (point-min))
  6775. (while (re-search-forward "^\\*\\*+ " nil t)
  6776. (setq n (- (length (match-string 0)) 2))
  6777. (while (>= (setq n (1- n)) 0)
  6778. (org-demote))
  6779. (end-of-line 1))))))
  6780. (defun org-convert-to-oddeven-levels ()
  6781. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6782. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6783. file contains a section with an even level, conversion would
  6784. destroy the structure of the file. An error is signaled in this
  6785. case."
  6786. (interactive)
  6787. (goto-char (point-min))
  6788. ;; First check if there are no even levels
  6789. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6790. (org-show-context t)
  6791. (error "Not all levels are odd in this file. Conversion not possible"))
  6792. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6793. (let ((outline-regexp org-outline-regexp)
  6794. (outline-level 'org-outline-level)
  6795. (org-odd-levels-only nil) n)
  6796. (save-excursion
  6797. (goto-char (point-min))
  6798. (while (re-search-forward "^\\*\\*+ " nil t)
  6799. (setq n (/ (1- (length (match-string 0))) 2))
  6800. (while (>= (setq n (1- n)) 0)
  6801. (org-promote))
  6802. (end-of-line 1))))))
  6803. (defun org-tr-level (n)
  6804. "Make N odd if required."
  6805. (if org-odd-levels-only (1+ (/ n 2)) n))
  6806. ;;; Vertical tree motion, cutting and pasting of subtrees
  6807. (defun org-move-subtree-up (&optional arg)
  6808. "Move the current subtree up past ARG headlines of the same level."
  6809. (interactive "p")
  6810. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6811. (defun org-move-subtree-down (&optional arg)
  6812. "Move the current subtree down past ARG headlines of the same level."
  6813. (interactive "p")
  6814. (setq arg (prefix-numeric-value arg))
  6815. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6816. 'org-get-last-sibling))
  6817. (ins-point (make-marker))
  6818. (cnt (abs arg))
  6819. (col (current-column))
  6820. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6821. ;; Select the tree
  6822. (org-back-to-heading)
  6823. (setq beg0 (point))
  6824. (save-excursion
  6825. (setq ne-beg (org-back-over-empty-lines))
  6826. (setq beg (point)))
  6827. (save-match-data
  6828. (save-excursion (outline-end-of-heading)
  6829. (setq folded (outline-invisible-p)))
  6830. (outline-end-of-subtree))
  6831. (outline-next-heading)
  6832. (setq ne-end (org-back-over-empty-lines))
  6833. (setq end (point))
  6834. (goto-char beg0)
  6835. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6836. ;; include less whitespace
  6837. (save-excursion
  6838. (goto-char beg)
  6839. (forward-line (- ne-beg ne-end))
  6840. (setq beg (point))))
  6841. ;; Find insertion point, with error handling
  6842. (while (> cnt 0)
  6843. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6844. (progn (goto-char beg0)
  6845. (error "Cannot move past superior level or buffer limit")))
  6846. (setq cnt (1- cnt)))
  6847. (if (> arg 0)
  6848. ;; Moving forward - still need to move over subtree
  6849. (progn (org-end-of-subtree t t)
  6850. (save-excursion
  6851. (org-back-over-empty-lines)
  6852. (or (bolp) (newline)))))
  6853. (setq ne-ins (org-back-over-empty-lines))
  6854. (move-marker ins-point (point))
  6855. (setq txt (buffer-substring beg end))
  6856. (org-save-markers-in-region beg end)
  6857. (delete-region beg end)
  6858. (org-remove-empty-overlays-at beg)
  6859. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6860. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6861. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6862. (let ((bbb (point)))
  6863. (insert-before-markers txt)
  6864. (org-reinstall-markers-in-region bbb)
  6865. (move-marker ins-point bbb))
  6866. (or (bolp) (insert "\n"))
  6867. (setq ins-end (point))
  6868. (goto-char ins-point)
  6869. (org-skip-whitespace)
  6870. (when (and (< arg 0)
  6871. (org-first-sibling-p)
  6872. (> ne-ins ne-beg))
  6873. ;; Move whitespace back to beginning
  6874. (save-excursion
  6875. (goto-char ins-end)
  6876. (let ((kill-whole-line t))
  6877. (kill-line (- ne-ins ne-beg)) (point)))
  6878. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6879. (move-marker ins-point nil)
  6880. (if folded
  6881. (hide-subtree)
  6882. (org-show-entry)
  6883. (show-children)
  6884. (org-cycle-hide-drawers 'children))
  6885. (org-clean-visibility-after-subtree-move)
  6886. ;; move back to the initial column we were at
  6887. (move-to-column col)))
  6888. (defvar org-subtree-clip ""
  6889. "Clipboard for cut and paste of subtrees.
  6890. This is actually only a copy of the kill, because we use the normal kill
  6891. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6892. (defvar org-subtree-clip-folded nil
  6893. "Was the last copied subtree folded?
  6894. This is used to fold the tree back after pasting.")
  6895. (defun org-cut-subtree (&optional n)
  6896. "Cut the current subtree into the clipboard.
  6897. With prefix arg N, cut this many sequential subtrees.
  6898. This is a short-hand for marking the subtree and then cutting it."
  6899. (interactive "p")
  6900. (org-copy-subtree n 'cut))
  6901. (defun org-copy-subtree (&optional n cut force-store-markers)
  6902. "Cut the current subtree into the clipboard.
  6903. With prefix arg N, cut this many sequential subtrees.
  6904. This is a short-hand for marking the subtree and then copying it.
  6905. If CUT is non-nil, actually cut the subtree.
  6906. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6907. of some markers in the region, even if CUT is non-nil. This is
  6908. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6909. (interactive "p")
  6910. (let (beg end folded (beg0 (point)))
  6911. (if (org-called-interactively-p 'any)
  6912. (org-back-to-heading nil) ; take what looks like a subtree
  6913. (org-back-to-heading t)) ; take what is really there
  6914. (org-back-over-empty-lines)
  6915. (setq beg (point))
  6916. (skip-chars-forward " \t\r\n")
  6917. (save-match-data
  6918. (save-excursion (outline-end-of-heading)
  6919. (setq folded (outline-invisible-p)))
  6920. (condition-case nil
  6921. (org-forward-same-level (1- n) t)
  6922. (error nil))
  6923. (org-end-of-subtree t t))
  6924. (org-back-over-empty-lines)
  6925. (setq end (point))
  6926. (goto-char beg0)
  6927. (when (> end beg)
  6928. (setq org-subtree-clip-folded folded)
  6929. (when (or cut force-store-markers)
  6930. (org-save-markers-in-region beg end))
  6931. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6932. (setq org-subtree-clip (current-kill 0))
  6933. (message "%s: Subtree(s) with %d characters"
  6934. (if cut "Cut" "Copied")
  6935. (length org-subtree-clip)))))
  6936. (defun org-paste-subtree (&optional level tree for-yank)
  6937. "Paste the clipboard as a subtree, with modification of headline level.
  6938. The entire subtree is promoted or demoted in order to match a new headline
  6939. level.
  6940. If the cursor is at the beginning of a headline, the same level as
  6941. that headline is used to paste the tree
  6942. If not, the new level is derived from the *visible* headings
  6943. before and after the insertion point, and taken to be the inferior headline
  6944. level of the two. So if the previous visible heading is level 3 and the
  6945. next is level 4 (or vice versa), level 4 will be used for insertion.
  6946. This makes sure that the subtree remains an independent subtree and does
  6947. not swallow low level entries.
  6948. You can also force a different level, either by using a numeric prefix
  6949. argument, or by inserting the heading marker by hand. For example, if the
  6950. cursor is after \"*****\", then the tree will be shifted to level 5.
  6951. If optional TREE is given, use this text instead of the kill ring.
  6952. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6953. move back over whitespace before inserting, and move point to the end of
  6954. the inserted text when done."
  6955. (interactive "P")
  6956. (setq tree (or tree (and kill-ring (current-kill 0))))
  6957. (unless (org-kill-is-subtree-p tree)
  6958. (error "%s"
  6959. (substitute-command-keys
  6960. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6961. (org-with-limited-levels
  6962. (let* ((visp (not (outline-invisible-p)))
  6963. (txt tree)
  6964. (^re_ "\\(\\*+\\)[ \t]*")
  6965. (old-level (if (string-match org-outline-regexp-bol txt)
  6966. (- (match-end 0) (match-beginning 0) 1)
  6967. -1))
  6968. (force-level (cond (level (prefix-numeric-value level))
  6969. ((and (looking-at "[ \t]*$")
  6970. (string-match
  6971. "^\\*+$" (buffer-substring
  6972. (point-at-bol) (point))))
  6973. (- (match-end 1) (match-beginning 1)))
  6974. ((and (bolp)
  6975. (looking-at org-outline-regexp))
  6976. (- (match-end 0) (point) 1))
  6977. (t nil)))
  6978. (previous-level (save-excursion
  6979. (condition-case nil
  6980. (progn
  6981. (outline-previous-visible-heading 1)
  6982. (if (looking-at ^re_)
  6983. (- (match-end 0) (match-beginning 0) 1)
  6984. 1))
  6985. (error 1))))
  6986. (next-level (save-excursion
  6987. (condition-case nil
  6988. (progn
  6989. (or (looking-at org-outline-regexp)
  6990. (outline-next-visible-heading 1))
  6991. (if (looking-at ^re_)
  6992. (- (match-end 0) (match-beginning 0) 1)
  6993. 1))
  6994. (error 1))))
  6995. (new-level (or force-level (max previous-level next-level)))
  6996. (shift (if (or (= old-level -1)
  6997. (= new-level -1)
  6998. (= old-level new-level))
  6999. 0
  7000. (- new-level old-level)))
  7001. (delta (if (> shift 0) -1 1))
  7002. (func (if (> shift 0) 'org-demote 'org-promote))
  7003. (org-odd-levels-only nil)
  7004. beg end newend)
  7005. ;; Remove the forced level indicator
  7006. (if force-level
  7007. (delete-region (point-at-bol) (point)))
  7008. ;; Paste
  7009. (beginning-of-line (if (bolp) 1 2))
  7010. (unless for-yank (org-back-over-empty-lines))
  7011. (setq beg (point))
  7012. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7013. (insert-before-markers txt)
  7014. (unless (string-match "\n\\'" txt) (insert "\n"))
  7015. (setq newend (point))
  7016. (org-reinstall-markers-in-region beg)
  7017. (setq end (point))
  7018. (goto-char beg)
  7019. (skip-chars-forward " \t\n\r")
  7020. (setq beg (point))
  7021. (if (and (outline-invisible-p) visp)
  7022. (save-excursion (outline-show-heading)))
  7023. ;; Shift if necessary
  7024. (unless (= shift 0)
  7025. (save-restriction
  7026. (narrow-to-region beg end)
  7027. (while (not (= shift 0))
  7028. (org-map-region func (point-min) (point-max))
  7029. (setq shift (+ delta shift)))
  7030. (goto-char (point-min))
  7031. (setq newend (point-max))))
  7032. (when (or (org-called-interactively-p 'interactive) for-yank)
  7033. (message "Clipboard pasted as level %d subtree" new-level))
  7034. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7035. kill-ring
  7036. (eq org-subtree-clip (current-kill 0))
  7037. org-subtree-clip-folded)
  7038. ;; The tree was folded before it was killed/copied
  7039. (hide-subtree))
  7040. (and for-yank (goto-char newend)))))
  7041. (defun org-kill-is-subtree-p (&optional txt)
  7042. "Check if the current kill is an outline subtree, or a set of trees.
  7043. Returns nil if kill does not start with a headline, or if the first
  7044. headline level is not the largest headline level in the tree.
  7045. So this will actually accept several entries of equal levels as well,
  7046. which is OK for `org-paste-subtree'.
  7047. If optional TXT is given, check this string instead of the current kill."
  7048. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7049. (re (org-get-limited-outline-regexp))
  7050. (^re (concat "^" re))
  7051. (start-level (and kill
  7052. (string-match
  7053. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7054. kill)
  7055. (- (match-end 2) (match-beginning 2) 1)))
  7056. (start (1+ (or (match-beginning 2) -1))))
  7057. (if (not start-level)
  7058. (progn
  7059. nil) ;; does not even start with a heading
  7060. (catch 'exit
  7061. (while (setq start (string-match ^re kill (1+ start)))
  7062. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7063. (throw 'exit nil)))
  7064. t))))
  7065. (defvar org-markers-to-move nil
  7066. "Markers that should be moved with a cut-and-paste operation.
  7067. Those markers are stored together with their positions relative to
  7068. the start of the region.")
  7069. (defun org-save-markers-in-region (beg end)
  7070. "Check markers in region.
  7071. If these markers are between BEG and END, record their position relative
  7072. to BEG, so that after moving the block of text, we can put the markers back
  7073. into place.
  7074. This function gets called just before an entry or tree gets cut from the
  7075. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7076. called immediately, to move the markers with the entries."
  7077. (setq org-markers-to-move nil)
  7078. (when (featurep 'org-clock)
  7079. (org-clock-save-markers-for-cut-and-paste beg end))
  7080. (when (featurep 'org-agenda)
  7081. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7082. (defun org-check-and-save-marker (marker beg end)
  7083. "Check if MARKER is between BEG and END.
  7084. If yes, remember the marker and the distance to BEG."
  7085. (when (and (marker-buffer marker)
  7086. (equal (marker-buffer marker) (current-buffer)))
  7087. (if (and (>= marker beg) (< marker end))
  7088. (push (cons marker (- marker beg)) org-markers-to-move))))
  7089. (defun org-reinstall-markers-in-region (beg)
  7090. "Move all remembered markers to their position relative to BEG."
  7091. (mapc (lambda (x)
  7092. (move-marker (car x) (+ beg (cdr x))))
  7093. org-markers-to-move)
  7094. (setq org-markers-to-move nil))
  7095. (defun org-narrow-to-subtree ()
  7096. "Narrow buffer to the current subtree."
  7097. (interactive)
  7098. (save-excursion
  7099. (save-match-data
  7100. (org-with-limited-levels
  7101. (narrow-to-region
  7102. (progn (org-back-to-heading t) (point))
  7103. (progn (org-end-of-subtree t t)
  7104. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7105. (point)))))))
  7106. (defun org-narrow-to-block ()
  7107. "Narrow buffer to the current block."
  7108. (interactive)
  7109. (let* ((case-fold-search t)
  7110. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7111. "^[ \t]*#\\+end_.*")))
  7112. (if blockp
  7113. (narrow-to-region (car blockp) (cdr blockp))
  7114. (error "Not in a block"))))
  7115. (eval-when-compile
  7116. (defvar org-property-drawer-re))
  7117. (defvar org-property-start-re) ;; defined below
  7118. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7119. "Clone the task (subtree) at point N times.
  7120. The clones will be inserted as siblings.
  7121. In interactive use, the user will be prompted for the number of
  7122. clones to be produced, and for a time SHIFT, which may be a
  7123. repeater as used in time stamps, for example `+3d'.
  7124. When a valid repeater is given and the entry contains any time
  7125. stamps, the clones will become a sequence in time, with time
  7126. stamps in the subtree shifted for each clone produced. If SHIFT
  7127. is nil or the empty string, time stamps will be left alone. The
  7128. ID property of the original subtree is removed.
  7129. If the original subtree did contain time stamps with a repeater,
  7130. the following will happen:
  7131. - the repeater will be removed in each clone
  7132. - an additional clone will be produced, with the current, unshifted
  7133. date(s) in the entry.
  7134. - the original entry will be placed *after* all the clones, with
  7135. repeater intact.
  7136. - the start days in the repeater in the original entry will be shifted
  7137. to past the last clone.
  7138. In this way you can spell out a number of instances of a repeating task,
  7139. and still retain the repeater to cover future instances of the task."
  7140. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7141. (let (beg end template task idprop
  7142. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7143. (drawer-re org-drawer-regexp))
  7144. (if (not (and (integerp n) (> n 0)))
  7145. (error "Invalid number of replications %s" n))
  7146. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7147. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7148. shift)))
  7149. (error "Invalid shift specification %s" shift))
  7150. (when doshift
  7151. (setq shift-n (string-to-number (match-string 1 shift))
  7152. shift-what (cdr (assoc (match-string 2 shift)
  7153. '(("d" . day) ("w" . week)
  7154. ("m" . month) ("y" . year))))))
  7155. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7156. (setq nmin 1 nmax n)
  7157. (org-back-to-heading t)
  7158. (setq beg (point))
  7159. (setq idprop (org-entry-get nil "ID"))
  7160. (org-end-of-subtree t t)
  7161. (or (bolp) (insert "\n"))
  7162. (setq end (point))
  7163. (setq template (buffer-substring beg end))
  7164. (when (and doshift
  7165. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
  7166. (delete-region beg end)
  7167. (setq end beg)
  7168. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7169. (goto-char end)
  7170. (loop for n from nmin to nmax do
  7171. ;; prepare clone
  7172. (with-temp-buffer
  7173. (insert template)
  7174. (org-mode)
  7175. (goto-char (point-min))
  7176. (org-show-subtree)
  7177. (and idprop (if org-clone-delete-id
  7178. (org-entry-delete nil "ID")
  7179. (org-id-get-create t)))
  7180. (unless (= n 0)
  7181. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7182. (kill-whole-line))
  7183. (goto-char (point-min))
  7184. (while (re-search-forward drawer-re nil t)
  7185. (mapc (lambda (d)
  7186. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7187. (goto-char (point-min))
  7188. (when doshift
  7189. (while (re-search-forward org-ts-regexp-both nil t)
  7190. (org-timestamp-change (* n shift-n) shift-what))
  7191. (unless (= n n-no-remove)
  7192. (goto-char (point-min))
  7193. (while (re-search-forward org-ts-regexp nil t)
  7194. (save-excursion
  7195. (goto-char (match-beginning 0))
  7196. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
  7197. (delete-region (match-beginning 1) (match-end 1)))))))
  7198. (setq task (buffer-string)))
  7199. (insert task))
  7200. (goto-char beg)))
  7201. ;;; Outline Sorting
  7202. (defun org-sort (with-case)
  7203. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7204. Optional argument WITH-CASE means sort case-sensitively."
  7205. (interactive "P")
  7206. (cond
  7207. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7208. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7209. (t
  7210. (org-call-with-arg 'org-sort-entries with-case))))
  7211. (defun org-sort-remove-invisible (s)
  7212. (remove-text-properties 0 (length s) org-rm-props s)
  7213. (while (string-match org-bracket-link-regexp s)
  7214. (setq s (replace-match (if (match-end 2)
  7215. (match-string 3 s)
  7216. (match-string 1 s)) t t s)))
  7217. s)
  7218. (defvar org-priority-regexp) ; defined later in the file
  7219. (defvar org-after-sorting-entries-or-items-hook nil
  7220. "Hook that is run after a bunch of entries or items have been sorted.
  7221. When children are sorted, the cursor is in the parent line when this
  7222. hook gets called. When a region or a plain list is sorted, the cursor
  7223. will be in the first entry of the sorted region/list.")
  7224. (defun org-sort-entries
  7225. (&optional with-case sorting-type getkey-func compare-func property)
  7226. "Sort entries on a certain level of an outline tree.
  7227. If there is an active region, the entries in the region are sorted.
  7228. Else, if the cursor is before the first entry, sort the top-level items.
  7229. Else, the children of the entry at point are sorted.
  7230. Sorting can be alphabetically, numerically, by date/time as given by
  7231. a time stamp, by a property or by priority.
  7232. The command prompts for the sorting type unless it has been given to the
  7233. function through the SORTING-TYPE argument, which needs to be a character,
  7234. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7235. precise meaning of each character:
  7236. n Numerically, by converting the beginning of the entry/item to a number.
  7237. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7238. t By date/time, either the first active time stamp in the entry, or, if
  7239. none exist, by the first inactive one.
  7240. s By the scheduled date/time.
  7241. d By deadline date/time.
  7242. c By creation time, which is assumed to be the first inactive time stamp
  7243. at the beginning of a line.
  7244. p By priority according to the cookie.
  7245. r By the value of a property.
  7246. Capital letters will reverse the sort order.
  7247. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7248. called with point at the beginning of the record. It must return either
  7249. a string or a number that should serve as the sorting key for that record.
  7250. Comparing entries ignores case by default. However, with an optional argument
  7251. WITH-CASE, the sorting considers case as well."
  7252. (interactive "P")
  7253. (let ((case-func (if with-case 'identity 'downcase))
  7254. start beg end stars re re2
  7255. txt what tmp)
  7256. ;; Find beginning and end of region to sort
  7257. (cond
  7258. ((org-region-active-p)
  7259. ;; we will sort the region
  7260. (setq end (region-end)
  7261. what "region")
  7262. (goto-char (region-beginning))
  7263. (if (not (org-at-heading-p)) (outline-next-heading))
  7264. (setq start (point)))
  7265. ((or (org-at-heading-p)
  7266. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7267. ;; we will sort the children of the current headline
  7268. (org-back-to-heading)
  7269. (setq start (point)
  7270. end (progn (org-end-of-subtree t t)
  7271. (or (bolp) (insert "\n"))
  7272. (org-back-over-empty-lines)
  7273. (point))
  7274. what "children")
  7275. (goto-char start)
  7276. (show-subtree)
  7277. (outline-next-heading))
  7278. (t
  7279. ;; we will sort the top-level entries in this file
  7280. (goto-char (point-min))
  7281. (or (org-at-heading-p) (outline-next-heading))
  7282. (setq start (point))
  7283. (goto-char (point-max))
  7284. (beginning-of-line 1)
  7285. (when (looking-at ".*?\\S-")
  7286. ;; File ends in a non-white line
  7287. (end-of-line 1)
  7288. (insert "\n"))
  7289. (setq end (point-max))
  7290. (setq what "top-level")
  7291. (goto-char start)
  7292. (show-all)))
  7293. (setq beg (point))
  7294. (if (>= beg end) (error "Nothing to sort"))
  7295. (looking-at "\\(\\*+\\)")
  7296. (setq stars (match-string 1)
  7297. re (concat "^" (regexp-quote stars) " +")
  7298. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7299. txt (buffer-substring beg end))
  7300. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7301. (if (and (not (equal stars "*")) (string-match re2 txt))
  7302. (error "Region to sort contains a level above the first entry"))
  7303. (unless sorting-type
  7304. (message
  7305. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7306. [t]ime [s]cheduled [d]eadline [c]reated
  7307. A/N/T/S/D/C/P/O/F means reversed:"
  7308. what)
  7309. (setq sorting-type (read-char-exclusive))
  7310. (and (= (downcase sorting-type) ?f)
  7311. (setq getkey-func
  7312. (org-icompleting-read "Sort using function: "
  7313. obarray 'fboundp t nil nil))
  7314. (setq getkey-func (intern getkey-func)))
  7315. (and (= (downcase sorting-type) ?r)
  7316. (setq property
  7317. (org-icompleting-read "Property: "
  7318. (mapcar 'list (org-buffer-property-keys t))
  7319. nil t))))
  7320. (message "Sorting entries...")
  7321. (save-restriction
  7322. (narrow-to-region start end)
  7323. (let ((dcst (downcase sorting-type))
  7324. (case-fold-search nil)
  7325. (now (current-time)))
  7326. (sort-subr
  7327. (/= dcst sorting-type)
  7328. ;; This function moves to the beginning character of the "record" to
  7329. ;; be sorted.
  7330. (lambda nil
  7331. (if (re-search-forward re nil t)
  7332. (goto-char (match-beginning 0))
  7333. (goto-char (point-max))))
  7334. ;; This function moves to the last character of the "record" being
  7335. ;; sorted.
  7336. (lambda nil
  7337. (save-match-data
  7338. (condition-case nil
  7339. (outline-forward-same-level 1)
  7340. (error
  7341. (goto-char (point-max))))))
  7342. ;; This function returns the value that gets sorted against.
  7343. (lambda nil
  7344. (cond
  7345. ((= dcst ?n)
  7346. (if (looking-at org-complex-heading-regexp)
  7347. (string-to-number (match-string 4))
  7348. nil))
  7349. ((= dcst ?a)
  7350. (if (looking-at org-complex-heading-regexp)
  7351. (funcall case-func (match-string 4))
  7352. nil))
  7353. ((= dcst ?t)
  7354. (let ((end (save-excursion (outline-next-heading) (point))))
  7355. (if (or (re-search-forward org-ts-regexp end t)
  7356. (re-search-forward org-ts-regexp-both end t))
  7357. (org-time-string-to-seconds (match-string 0))
  7358. (org-float-time now))))
  7359. ((= dcst ?c)
  7360. (let ((end (save-excursion (outline-next-heading) (point))))
  7361. (if (re-search-forward
  7362. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7363. end t)
  7364. (org-time-string-to-seconds (match-string 0))
  7365. (org-float-time now))))
  7366. ((= dcst ?s)
  7367. (let ((end (save-excursion (outline-next-heading) (point))))
  7368. (if (re-search-forward org-scheduled-time-regexp end t)
  7369. (org-time-string-to-seconds (match-string 1))
  7370. (org-float-time now))))
  7371. ((= dcst ?d)
  7372. (let ((end (save-excursion (outline-next-heading) (point))))
  7373. (if (re-search-forward org-deadline-time-regexp end t)
  7374. (org-time-string-to-seconds (match-string 1))
  7375. (org-float-time now))))
  7376. ((= dcst ?p)
  7377. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7378. (string-to-char (match-string 2))
  7379. org-default-priority))
  7380. ((= dcst ?r)
  7381. (or (org-entry-get nil property) ""))
  7382. ((= dcst ?o)
  7383. (if (looking-at org-complex-heading-regexp)
  7384. (- 9999 (length (member (match-string 2)
  7385. org-todo-keywords-1)))))
  7386. ((= dcst ?f)
  7387. (if getkey-func
  7388. (progn
  7389. (setq tmp (funcall getkey-func))
  7390. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7391. tmp)
  7392. (error "Invalid key function `%s'" getkey-func)))
  7393. (t (error "Invalid sorting type `%c'" sorting-type))))
  7394. nil
  7395. (cond
  7396. ((= dcst ?a) 'string<)
  7397. ((= dcst ?f) compare-func)
  7398. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7399. (t nil)))))
  7400. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7401. (message "Sorting entries...done")))
  7402. (defun org-do-sort (table what &optional with-case sorting-type)
  7403. "Sort TABLE of WHAT according to SORTING-TYPE.
  7404. The user will be prompted for the SORTING-TYPE if the call to this
  7405. function does not specify it. WHAT is only for the prompt, to indicate
  7406. what is being sorted. The sorting key will be extracted from
  7407. the car of the elements of the table.
  7408. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7409. (unless sorting-type
  7410. (message
  7411. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7412. what)
  7413. (setq sorting-type (read-char-exclusive)))
  7414. (let ((dcst (downcase sorting-type))
  7415. extractfun comparefun)
  7416. ;; Define the appropriate functions
  7417. (cond
  7418. ((= dcst ?n)
  7419. (setq extractfun 'string-to-number
  7420. comparefun (if (= dcst sorting-type) '< '>)))
  7421. ((= dcst ?a)
  7422. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7423. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7424. comparefun (if (= dcst sorting-type)
  7425. 'string<
  7426. (lambda (a b) (and (not (string< a b))
  7427. (not (string= a b)))))))
  7428. ((= dcst ?t)
  7429. (setq extractfun
  7430. (lambda (x)
  7431. (if (or (string-match org-ts-regexp x)
  7432. (string-match org-ts-regexp-both x))
  7433. (org-float-time
  7434. (org-time-string-to-time (match-string 0 x)))
  7435. 0))
  7436. comparefun (if (= dcst sorting-type) '< '>)))
  7437. (t (error "Invalid sorting type `%c'" sorting-type)))
  7438. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7439. table)
  7440. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7441. ;;; The orgstruct minor mode
  7442. ;; Define a minor mode which can be used in other modes in order to
  7443. ;; integrate the org-mode structure editing commands.
  7444. ;; This is really a hack, because the org-mode structure commands use
  7445. ;; keys which normally belong to the major mode. Here is how it
  7446. ;; works: The minor mode defines all the keys necessary to operate the
  7447. ;; structure commands, but wraps the commands into a function which
  7448. ;; tests if the cursor is currently at a headline or a plain list
  7449. ;; item. If that is the case, the structure command is used,
  7450. ;; temporarily setting many Org-mode variables like regular
  7451. ;; expressions for filling etc. However, when any of those keys is
  7452. ;; used at a different location, function uses `key-binding' to look
  7453. ;; up if the key has an associated command in another currently active
  7454. ;; keymap (minor modes, major mode, global), and executes that
  7455. ;; command. There might be problems if any of the keys is otherwise
  7456. ;; used as a prefix key.
  7457. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7458. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7459. ;; addresses this by checking explicitly for both bindings.
  7460. (defvar orgstruct-mode-map (make-sparse-keymap)
  7461. "Keymap for the minor `orgstruct-mode'.")
  7462. (defvar org-local-vars nil
  7463. "List of local variables, for use by `orgstruct-mode'.")
  7464. ;;;###autoload
  7465. (define-minor-mode orgstruct-mode
  7466. "Toggle the minor mode `orgstruct-mode'.
  7467. This mode is for using Org-mode structure commands in other
  7468. modes. The following keys behave as if Org-mode were active, if
  7469. the cursor is on a headline, or on a plain list item (both as
  7470. defined by Org-mode).
  7471. M-up Move entry/item up
  7472. M-down Move entry/item down
  7473. M-left Promote
  7474. M-right Demote
  7475. M-S-up Move entry/item up
  7476. M-S-down Move entry/item down
  7477. M-S-left Promote subtree
  7478. M-S-right Demote subtree
  7479. M-q Fill paragraph and items like in Org-mode
  7480. C-c ^ Sort entries
  7481. C-c - Cycle list bullet
  7482. TAB Cycle item visibility
  7483. M-RET Insert new heading/item
  7484. S-M-RET Insert new TODO heading / Checkbox item
  7485. C-c C-c Set tags / toggle checkbox"
  7486. nil " OrgStruct" nil
  7487. (org-load-modules-maybe)
  7488. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7489. ;;;###autoload
  7490. (defun turn-on-orgstruct ()
  7491. "Unconditionally turn on `orgstruct-mode'."
  7492. (orgstruct-mode 1))
  7493. (defun orgstruct++-mode (&optional arg)
  7494. "Toggle `orgstruct-mode', the enhanced version of it.
  7495. In addition to setting orgstruct-mode, this also exports all indentation
  7496. and autofilling variables from org-mode into the buffer. It will also
  7497. recognize item context in multiline items.
  7498. Note that turning off orgstruct-mode will *not* remove the
  7499. indentation/paragraph settings. This can only be done by refreshing the
  7500. major mode, for example with \\[normal-mode]."
  7501. (interactive "P")
  7502. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7503. (if (< arg 1)
  7504. (orgstruct-mode -1)
  7505. (orgstruct-mode 1)
  7506. (let (var val)
  7507. (mapc
  7508. (lambda (x)
  7509. (when (string-match
  7510. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7511. (symbol-name (car x)))
  7512. (setq var (car x) val (nth 1 x))
  7513. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7514. org-local-vars)
  7515. (org-set-local 'orgstruct-is-++ t))))
  7516. (defvar orgstruct-is-++ nil
  7517. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7518. (make-variable-buffer-local 'orgstruct-is-++)
  7519. ;;;###autoload
  7520. (defun turn-on-orgstruct++ ()
  7521. "Unconditionally turn on `orgstruct++-mode'."
  7522. (orgstruct++-mode 1))
  7523. (defun orgstruct-error ()
  7524. "Error when there is no default binding for a structure key."
  7525. (interactive)
  7526. (error "This key has no function outside structure elements"))
  7527. (defun orgstruct-setup ()
  7528. "Setup orgstruct keymaps."
  7529. (let ((nfunc 0)
  7530. (bindings
  7531. (list
  7532. '([(meta up)] org-metaup)
  7533. '([(meta down)] org-metadown)
  7534. '([(meta left)] org-metaleft)
  7535. '([(meta right)] org-metaright)
  7536. '([(meta shift up)] org-shiftmetaup)
  7537. '([(meta shift down)] org-shiftmetadown)
  7538. '([(meta shift left)] org-shiftmetaleft)
  7539. '([(meta shift right)] org-shiftmetaright)
  7540. '([?\e (up)] org-metaup)
  7541. '([?\e (down)] org-metadown)
  7542. '([?\e (left)] org-metaleft)
  7543. '([?\e (right)] org-metaright)
  7544. '([?\e (shift up)] org-shiftmetaup)
  7545. '([?\e (shift down)] org-shiftmetadown)
  7546. '([?\e (shift left)] org-shiftmetaleft)
  7547. '([?\e (shift right)] org-shiftmetaright)
  7548. '([(shift up)] org-shiftup)
  7549. '([(shift down)] org-shiftdown)
  7550. '([(shift left)] org-shiftleft)
  7551. '([(shift right)] org-shiftright)
  7552. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7553. '("\M-q" fill-paragraph)
  7554. '("\C-c^" org-sort)
  7555. '("\C-c-" org-cycle-list-bullet)))
  7556. elt key fun cmd)
  7557. (while (setq elt (pop bindings))
  7558. (setq nfunc (1+ nfunc))
  7559. (setq key (org-key (car elt))
  7560. fun (nth 1 elt)
  7561. cmd (orgstruct-make-binding fun nfunc key))
  7562. (org-defkey orgstruct-mode-map key cmd))
  7563. ;; Special treatment needed for TAB and RET
  7564. (org-defkey orgstruct-mode-map [(tab)]
  7565. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7566. (org-defkey orgstruct-mode-map "\C-i"
  7567. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7568. (org-defkey orgstruct-mode-map "\M-\C-m"
  7569. (orgstruct-make-binding 'org-insert-heading 105
  7570. "\M-\C-m" [(meta return)]))
  7571. (org-defkey orgstruct-mode-map [(meta return)]
  7572. (orgstruct-make-binding 'org-insert-heading 106
  7573. [(meta return)] "\M-\C-m"))
  7574. (org-defkey orgstruct-mode-map [(shift meta return)]
  7575. (orgstruct-make-binding 'org-insert-todo-heading 107
  7576. [(meta return)] "\M-\C-m"))
  7577. (org-defkey orgstruct-mode-map "\e\C-m"
  7578. (orgstruct-make-binding 'org-insert-heading 108
  7579. "\e\C-m" [?\e (return)]))
  7580. (org-defkey orgstruct-mode-map [?\e (return)]
  7581. (orgstruct-make-binding 'org-insert-heading 109
  7582. [?\e (return)] "\e\C-m"))
  7583. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7584. (orgstruct-make-binding 'org-insert-todo-heading 110
  7585. [?\e (return)] "\e\C-m"))
  7586. (unless org-local-vars
  7587. (setq org-local-vars (org-get-local-variables)))
  7588. t))
  7589. (defun orgstruct-make-binding (fun n &rest keys)
  7590. "Create a function for binding in the structure minor mode.
  7591. FUN is the command to call inside a table. N is used to create a unique
  7592. command name. KEYS are keys that should be checked in for a command
  7593. to execute outside of tables."
  7594. (eval
  7595. (list 'defun
  7596. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7597. '(arg)
  7598. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7599. "Outside of structure, run the binding of `"
  7600. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7601. "'.")
  7602. '(interactive "p")
  7603. (list 'if
  7604. `(org-context-p 'headline 'item
  7605. (and orgstruct-is-++
  7606. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7607. 'item-body))
  7608. (list 'org-run-like-in-org-mode (list 'quote fun))
  7609. (list 'let '(orgstruct-mode)
  7610. (list 'call-interactively
  7611. (append '(or)
  7612. (mapcar (lambda (k)
  7613. (list 'key-binding k))
  7614. keys)
  7615. '('orgstruct-error))))))))
  7616. (defun org-context-p (&rest contexts)
  7617. "Check if local context is any of CONTEXTS.
  7618. Possible values in the list of contexts are `table', `headline', and `item'."
  7619. (let ((pos (point)))
  7620. (goto-char (point-at-bol))
  7621. (prog1 (or (and (memq 'table contexts)
  7622. (looking-at "[ \t]*|"))
  7623. (and (memq 'headline contexts)
  7624. (looking-at org-outline-regexp))
  7625. (and (memq 'item contexts)
  7626. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7627. (and (memq 'item-body contexts)
  7628. (org-in-item-p)))
  7629. (goto-char pos))))
  7630. (defun org-get-local-variables ()
  7631. "Return a list of all local variables in an org-mode buffer."
  7632. (let (varlist)
  7633. (with-current-buffer (get-buffer-create "*Org tmp*")
  7634. (erase-buffer)
  7635. (org-mode)
  7636. (setq varlist (buffer-local-variables)))
  7637. (kill-buffer "*Org tmp*")
  7638. (delq nil
  7639. (mapcar
  7640. (lambda (x)
  7641. (setq x
  7642. (if (symbolp x)
  7643. (list x)
  7644. (list (car x) (list 'quote (cdr x)))))
  7645. (if (string-match
  7646. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7647. (symbol-name (car x)))
  7648. x nil))
  7649. varlist))))
  7650. (defun org-clone-local-variables (from-buffer &optional regexp)
  7651. "Clone local variables from FROM-BUFFER.
  7652. Optional argument REGEXP selects variables to clone."
  7653. (mapc
  7654. (lambda (pair)
  7655. (and (symbolp (car pair))
  7656. (or (null regexp)
  7657. (string-match regexp (symbol-name (car pair))))
  7658. (set (make-local-variable (car pair))
  7659. (cdr pair))))
  7660. (buffer-local-variables from-buffer)))
  7661. ;;;###autoload
  7662. (defun org-run-like-in-org-mode (cmd)
  7663. "Run a command, pretending that the current buffer is in Org-mode.
  7664. This will temporarily bind local variables that are typically bound in
  7665. Org-mode to the values they have in Org-mode, and then interactively
  7666. call CMD."
  7667. (org-load-modules-maybe)
  7668. (unless org-local-vars
  7669. (setq org-local-vars (org-get-local-variables)))
  7670. (eval (list 'let org-local-vars
  7671. (list 'call-interactively (list 'quote cmd)))))
  7672. ;;;; Archiving
  7673. (defun org-get-category (&optional pos force-refresh)
  7674. "Get the category applying to position POS."
  7675. (if force-refresh (org-refresh-category-properties))
  7676. (let ((pos (or pos (point))))
  7677. (or (get-text-property pos 'org-category)
  7678. (progn (org-refresh-category-properties)
  7679. (get-text-property pos 'org-category)))))
  7680. (defun org-refresh-category-properties ()
  7681. "Refresh category text properties in the buffer."
  7682. (let ((def-cat (cond
  7683. ((null org-category)
  7684. (if buffer-file-name
  7685. (file-name-sans-extension
  7686. (file-name-nondirectory buffer-file-name))
  7687. "???"))
  7688. ((symbolp org-category) (symbol-name org-category))
  7689. (t org-category)))
  7690. beg end cat pos optionp)
  7691. (org-unmodified
  7692. (save-excursion
  7693. (save-restriction
  7694. (widen)
  7695. (goto-char (point-min))
  7696. (put-text-property (point) (point-max) 'org-category def-cat)
  7697. (while (re-search-forward
  7698. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7699. (setq pos (match-end 0)
  7700. optionp (equal (char-after (match-beginning 0)) ?#)
  7701. cat (org-trim (match-string 2)))
  7702. (if optionp
  7703. (setq beg (point-at-bol) end (point-max))
  7704. (org-back-to-heading t)
  7705. (setq beg (point) end (org-end-of-subtree t t)))
  7706. (put-text-property beg end 'org-category cat)
  7707. (put-text-property beg end 'org-category-position beg)
  7708. (goto-char pos)))))))
  7709. ;;;; Link Stuff
  7710. ;;; Link abbreviations
  7711. (defun org-link-expand-abbrev (link)
  7712. "Apply replacements as defined in `org-link-abbrev-alist."
  7713. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7714. (let* ((key (match-string 1 link))
  7715. (as (or (assoc key org-link-abbrev-alist-local)
  7716. (assoc key org-link-abbrev-alist)))
  7717. (tag (and (match-end 2) (match-string 3 link)))
  7718. rpl)
  7719. (if (not as)
  7720. link
  7721. (setq rpl (cdr as))
  7722. (cond
  7723. ((symbolp rpl) (funcall rpl tag))
  7724. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7725. ((string-match "%h" rpl)
  7726. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7727. (t (concat rpl tag)))))
  7728. link))
  7729. ;;; Storing and inserting links
  7730. (defvar org-insert-link-history nil
  7731. "Minibuffer history for links inserted with `org-insert-link'.")
  7732. (defvar org-stored-links nil
  7733. "Contains the links stored with `org-store-link'.")
  7734. (defvar org-store-link-plist nil
  7735. "Plist with info about the most recently link created with `org-store-link'.")
  7736. (defvar org-link-protocols nil
  7737. "Link protocols added to Org-mode using `org-add-link-type'.")
  7738. (defvar org-store-link-functions nil
  7739. "List of functions that are called to create and store a link.
  7740. Each function will be called in turn until one returns a non-nil
  7741. value. Each function should check if it is responsible for creating
  7742. this link (for example by looking at the major mode).
  7743. If not, it must exit and return nil.
  7744. If yes, it should return a non-nil value after a calling
  7745. `org-store-link-props' with a list of properties and values.
  7746. Special properties are:
  7747. :type The link prefix, like \"http\". This must be given.
  7748. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7749. This is obligatory as well.
  7750. :description Optional default description for the second pair
  7751. of brackets in an Org-mode link. The user can still change
  7752. this when inserting this link into an Org-mode buffer.
  7753. In addition to these, any additional properties can be specified
  7754. and then used in remember templates.")
  7755. (defun org-add-link-type (type &optional follow export)
  7756. "Add TYPE to the list of `org-link-types'.
  7757. Re-compute all regular expressions depending on `org-link-types'
  7758. FOLLOW and EXPORT are two functions.
  7759. FOLLOW should take the link path as the single argument and do whatever
  7760. is necessary to follow the link, for example find a file or display
  7761. a mail message.
  7762. EXPORT should format the link path for export to one of the export formats.
  7763. It should be a function accepting three arguments:
  7764. path the path of the link, the text after the prefix (like \"http:\")
  7765. desc the description of the link, if any, or a description added by
  7766. org-export-normalize-links if there is none
  7767. format the export format, a symbol like `html' or `latex' or `ascii'..
  7768. The function may use the FORMAT information to return different values
  7769. depending on the format. The return value will be put literally into
  7770. the exported file. If the return value is nil, this means Org should
  7771. do what it normally does with links which do not have EXPORT defined.
  7772. Org-mode has a built-in default for exporting links. If you are happy with
  7773. this default, there is no need to define an export function for the link
  7774. type. For a simple example of an export function, see `org-bbdb.el'."
  7775. (add-to-list 'org-link-types type t)
  7776. (org-make-link-regexps)
  7777. (if (assoc type org-link-protocols)
  7778. (setcdr (assoc type org-link-protocols) (list follow export))
  7779. (push (list type follow export) org-link-protocols)))
  7780. (defvar org-agenda-buffer-name)
  7781. ;;;###autoload
  7782. (defun org-store-link (arg)
  7783. "\\<org-mode-map>Store an org-link to the current location.
  7784. This link is added to `org-stored-links' and can later be inserted
  7785. into an org-buffer with \\[org-insert-link].
  7786. For some link types, a prefix arg is interpreted:
  7787. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7788. For file links, arg negates `org-context-in-file-links'."
  7789. (interactive "P")
  7790. (org-load-modules-maybe)
  7791. (setq org-store-link-plist nil) ; reset
  7792. (org-with-limited-levels
  7793. (let (link cpltxt desc description search txt custom-id agenda-link)
  7794. (cond
  7795. ((run-hook-with-args-until-success 'org-store-link-functions)
  7796. (setq link (plist-get org-store-link-plist :link)
  7797. desc (or (plist-get org-store-link-plist :description) link)))
  7798. ((equal (buffer-name) "*Org Edit Src Example*")
  7799. (let (label gc)
  7800. (while (or (not label)
  7801. (save-excursion
  7802. (save-restriction
  7803. (widen)
  7804. (goto-char (point-min))
  7805. (re-search-forward
  7806. (regexp-quote (format org-coderef-label-format label))
  7807. nil t))))
  7808. (when label (message "Label exists already") (sit-for 2))
  7809. (setq label (read-string "Code line label: " label)))
  7810. (end-of-line 1)
  7811. (setq link (format org-coderef-label-format label))
  7812. (setq gc (- 79 (length link)))
  7813. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7814. (insert link)
  7815. (setq link (concat "(" label ")") desc nil)))
  7816. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7817. ;; We are in the agenda, link to referenced location
  7818. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7819. (get-text-property (point) 'org-marker))))
  7820. (when m
  7821. (org-with-point-at m
  7822. (setq agenda-link
  7823. (if (org-called-interactively-p 'any)
  7824. (call-interactively 'org-store-link)
  7825. (org-store-link nil)))))))
  7826. ((eq major-mode 'calendar-mode)
  7827. (let ((cd (calendar-cursor-to-date)))
  7828. (setq link
  7829. (format-time-string
  7830. (car org-time-stamp-formats)
  7831. (apply 'encode-time
  7832. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7833. nil nil nil))))
  7834. (org-store-link-props :type "calendar" :date cd)))
  7835. ((eq major-mode 'w3-mode)
  7836. (setq cpltxt (if (and (buffer-name)
  7837. (not (string-match "Untitled" (buffer-name))))
  7838. (buffer-name)
  7839. (url-view-url t))
  7840. link (org-make-link (url-view-url t)))
  7841. (org-store-link-props :type "w3" :url (url-view-url t)))
  7842. ((eq major-mode 'w3m-mode)
  7843. (setq cpltxt (or w3m-current-title w3m-current-url)
  7844. link (org-make-link w3m-current-url))
  7845. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7846. ((setq search (run-hook-with-args-until-success
  7847. 'org-create-file-search-functions))
  7848. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7849. "::" search))
  7850. (setq cpltxt (or description link)))
  7851. ((eq major-mode 'image-mode)
  7852. (setq cpltxt (concat "file:"
  7853. (abbreviate-file-name buffer-file-name))
  7854. link (org-make-link cpltxt))
  7855. (org-store-link-props :type "image" :file buffer-file-name))
  7856. ((eq major-mode 'dired-mode)
  7857. ;; link to the file in the current line
  7858. (let ((file (dired-get-filename nil t)))
  7859. (setq file (if file
  7860. (abbreviate-file-name
  7861. (expand-file-name (dired-get-filename nil t)))
  7862. ;; otherwise, no file so use current directory.
  7863. default-directory))
  7864. (setq cpltxt (concat "file:" file)
  7865. link (org-make-link cpltxt))))
  7866. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7867. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7868. (cond
  7869. ((org-in-regexp "<<\\(.*?\\)>>")
  7870. (setq cpltxt
  7871. (concat "file:"
  7872. (abbreviate-file-name
  7873. (buffer-file-name (buffer-base-buffer)))
  7874. "::" (match-string 1))
  7875. link (org-make-link cpltxt)))
  7876. ((and (featurep 'org-id)
  7877. (or (eq org-link-to-org-use-id t)
  7878. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7879. (org-called-interactively-p 'any))
  7880. (and (eq org-link-to-org-use-id
  7881. 'create-if-interactive-and-no-custom-id)
  7882. (org-called-interactively-p 'any)
  7883. (not custom-id))
  7884. (and org-link-to-org-use-id
  7885. (org-entry-get nil "ID"))))
  7886. ;; We can make a link using the ID.
  7887. (setq link (condition-case nil
  7888. (prog1 (org-id-store-link)
  7889. (setq desc (plist-get org-store-link-plist
  7890. :description)))
  7891. (error
  7892. ;; probably before first headline, link to file only
  7893. (concat "file:"
  7894. (abbreviate-file-name
  7895. (buffer-file-name (buffer-base-buffer))))))))
  7896. (t
  7897. ;; Just link to current headline
  7898. (setq cpltxt (concat "file:"
  7899. (abbreviate-file-name
  7900. (buffer-file-name (buffer-base-buffer)))))
  7901. ;; Add a context search string
  7902. (when (org-xor org-context-in-file-links arg)
  7903. (setq txt (cond
  7904. ((org-at-heading-p) nil)
  7905. ((org-region-active-p)
  7906. (buffer-substring (region-beginning) (region-end)))
  7907. (t nil)))
  7908. (when (or (null txt) (string-match "\\S-" txt))
  7909. (setq cpltxt
  7910. (concat cpltxt "::"
  7911. (condition-case nil
  7912. (org-make-org-heading-search-string txt)
  7913. (error "")))
  7914. desc (or (nth 4 (ignore-errors
  7915. (org-heading-components))) "NONE"))))
  7916. (if (string-match "::\\'" cpltxt)
  7917. (setq cpltxt (substring cpltxt 0 -2)))
  7918. (setq link (org-make-link cpltxt)))))
  7919. ((buffer-file-name (buffer-base-buffer))
  7920. ;; Just link to this file here.
  7921. (setq cpltxt (concat "file:"
  7922. (abbreviate-file-name
  7923. (buffer-file-name (buffer-base-buffer)))))
  7924. ;; Add a context string
  7925. (when (org-xor org-context-in-file-links arg)
  7926. (setq txt (if (org-region-active-p)
  7927. (buffer-substring (region-beginning) (region-end))
  7928. (buffer-substring (point-at-bol) (point-at-eol))))
  7929. ;; Only use search option if there is some text.
  7930. (when (string-match "\\S-" txt)
  7931. (setq cpltxt
  7932. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7933. desc "NONE")))
  7934. (setq link (org-make-link cpltxt)))
  7935. ((org-called-interactively-p 'interactive)
  7936. (error "Cannot link to a buffer which is not visiting a file"))
  7937. (t (setq link nil)))
  7938. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7939. (setq link (or link cpltxt)
  7940. desc (or desc cpltxt))
  7941. (if (equal desc "NONE") (setq desc nil))
  7942. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7943. (progn
  7944. (setq org-stored-links
  7945. (cons (list link desc) org-stored-links))
  7946. (message "Stored: %s" (or desc link))
  7947. (when custom-id
  7948. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7949. "::#" custom-id))
  7950. (setq org-stored-links
  7951. (cons (list link desc) org-stored-links))))
  7952. (or agenda-link (and link (org-make-link-string link desc)))))))
  7953. (defun org-store-link-props (&rest plist)
  7954. "Store link properties, extract names and addresses."
  7955. (let (x adr)
  7956. (when (setq x (plist-get plist :from))
  7957. (setq adr (mail-extract-address-components x))
  7958. (setq plist (plist-put plist :fromname (car adr)))
  7959. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7960. (when (setq x (plist-get plist :to))
  7961. (setq adr (mail-extract-address-components x))
  7962. (setq plist (plist-put plist :toname (car adr)))
  7963. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7964. (let ((from (plist-get plist :from))
  7965. (to (plist-get plist :to)))
  7966. (when (and from to org-from-is-user-regexp)
  7967. (setq plist
  7968. (plist-put plist :fromto
  7969. (if (string-match org-from-is-user-regexp from)
  7970. (concat "to %t")
  7971. (concat "from %f"))))))
  7972. (setq org-store-link-plist plist))
  7973. (defun org-add-link-props (&rest plist)
  7974. "Add these properties to the link property list."
  7975. (let (key value)
  7976. (while plist
  7977. (setq key (pop plist) value (pop plist))
  7978. (setq org-store-link-plist
  7979. (plist-put org-store-link-plist key value)))))
  7980. (defun org-email-link-description (&optional fmt)
  7981. "Return the description part of an email link.
  7982. This takes information from `org-store-link-plist' and formats it
  7983. according to FMT (default from `org-email-link-description-format')."
  7984. (setq fmt (or fmt org-email-link-description-format))
  7985. (let* ((p org-store-link-plist)
  7986. (to (plist-get p :toaddress))
  7987. (from (plist-get p :fromaddress))
  7988. (table
  7989. (list
  7990. (cons "%c" (plist-get p :fromto))
  7991. (cons "%F" (plist-get p :from))
  7992. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7993. (cons "%T" (plist-get p :to))
  7994. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7995. (cons "%s" (plist-get p :subject))
  7996. (cons "%d" (plist-get p :date))
  7997. (cons "%m" (plist-get p :message-id)))))
  7998. (when (string-match "%c" fmt)
  7999. ;; Check if the user wrote this message
  8000. (if (and org-from-is-user-regexp from to
  8001. (save-match-data (string-match org-from-is-user-regexp from)))
  8002. (setq fmt (replace-match "to %t" t t fmt))
  8003. (setq fmt (replace-match "from %f" t t fmt))))
  8004. (org-replace-escapes fmt table)))
  8005. (defun org-make-org-heading-search-string (&optional string heading)
  8006. "Make search string for STRING or current headline."
  8007. (interactive)
  8008. (let ((s (or string (org-get-heading)))
  8009. (lines org-context-in-file-links))
  8010. (unless (and string (not heading))
  8011. ;; We are using a headline, clean up garbage in there.
  8012. (if (string-match org-todo-regexp s)
  8013. (setq s (replace-match "" t t s)))
  8014. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8015. (setq s (replace-match "" t t s)))
  8016. (setq s (org-trim s))
  8017. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8018. org-comment-string "\\)") s)
  8019. (setq s (replace-match "" t t s)))
  8020. (while (string-match org-ts-regexp s)
  8021. (setq s (replace-match "" t t s))))
  8022. (or string (setq s (concat "*" s))) ; Add * for headlines
  8023. (when (and string (integerp lines) (> lines 0))
  8024. (let ((slines (org-split-string s "\n")))
  8025. (when (< lines (length slines))
  8026. (setq s (mapconcat
  8027. 'identity
  8028. (reverse (nthcdr (- (length slines) lines)
  8029. (reverse slines))) "\n")))))
  8030. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8031. (defun org-make-link (&rest strings)
  8032. "Concatenate STRINGS."
  8033. (apply 'concat strings))
  8034. (defun org-make-link-string (link &optional description)
  8035. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8036. (unless (string-match "\\S-" link)
  8037. (error "Empty link"))
  8038. (when (and description
  8039. (stringp description)
  8040. (not (string-match "\\S-" description)))
  8041. (setq description nil))
  8042. (when (stringp description)
  8043. ;; Remove brackets from the description, they are fatal.
  8044. (while (string-match "\\[" description)
  8045. (setq description (replace-match "{" t t description)))
  8046. (while (string-match "\\]" description)
  8047. (setq description (replace-match "}" t t description))))
  8048. (when (equal link description)
  8049. ;; No description needed, it is identical
  8050. (setq description nil))
  8051. (when (and (not description)
  8052. (not (string-match (org-image-file-name-regexp) link))
  8053. (not (equal link (org-link-escape link))))
  8054. (setq description (org-extract-attributes link)))
  8055. (setq link
  8056. (cond ((string-match (org-image-file-name-regexp) link) link)
  8057. ((string-match org-link-types-re link)
  8058. (concat (match-string 1 link)
  8059. (org-link-escape (substring link (match-end 1)))))
  8060. (t (org-link-escape link))))
  8061. (concat "[[" link "]"
  8062. (if description (concat "[" description "]") "")
  8063. "]"))
  8064. (defconst org-link-escape-chars
  8065. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8066. "List of characters that should be escaped in link.
  8067. This is the list that is used for internal purposes.")
  8068. (defvar org-url-encoding-use-url-hexify nil)
  8069. (defconst org-link-escape-chars-browser
  8070. '(?\ )
  8071. "List of escapes for characters that are problematic in links.
  8072. This is the list that is used before handing over to the browser.")
  8073. (defun org-link-escape (text &optional table merge)
  8074. "Return percent escaped representation of TEXT.
  8075. TEXT is a string with the text to escape.
  8076. Optional argument TABLE is a list with characters that should be
  8077. escaped. When nil, `org-link-escape-chars' is used.
  8078. If optional argument MERGE is set, merge TABLE into
  8079. `org-link-escape-chars'."
  8080. (if (and org-url-encoding-use-url-hexify (not table))
  8081. (url-hexify-string text)
  8082. (cond
  8083. ((and table merge)
  8084. (mapc (lambda (defchr)
  8085. (unless (member defchr table)
  8086. (setq table (cons defchr table)))) org-link-escape-chars))
  8087. ((null table)
  8088. (setq table org-link-escape-chars)))
  8089. (mapconcat
  8090. (lambda (char)
  8091. (if (or (member char table)
  8092. (< char 32) (= char 37) (> char 126))
  8093. (mapconcat (lambda (sequence-element)
  8094. (format "%%%.2X" sequence-element))
  8095. (or (encode-coding-char char 'utf-8)
  8096. (error "Unable to percent escape character: %s"
  8097. (char-to-string char))) "")
  8098. (char-to-string char))) text "")))
  8099. (defun org-link-unescape (str)
  8100. "Unhex hexified Unicode strings as returned from the JavaScript function
  8101. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8102. (unless (and (null str) (string= "" str))
  8103. (let ((pos 0) (case-fold-search t) unhexed)
  8104. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8105. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8106. (setq str (replace-match unhexed t t str))
  8107. (setq pos (+ pos (length unhexed))))))
  8108. str)
  8109. (defun org-link-unescape-compound (hex)
  8110. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8111. Note: this function also decodes single byte encodings like
  8112. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8113. (save-match-data
  8114. (let* ((bytes (cdr (split-string hex "%")))
  8115. (ret "")
  8116. (eat 0)
  8117. (sum 0))
  8118. (while bytes
  8119. (let* ((val (string-to-number (pop bytes) 16))
  8120. (shift-xor
  8121. (if (= 0 eat)
  8122. (cond
  8123. ((>= val 252) (cons 6 252))
  8124. ((>= val 248) (cons 5 248))
  8125. ((>= val 240) (cons 4 240))
  8126. ((>= val 224) (cons 3 224))
  8127. ((>= val 192) (cons 2 192))
  8128. (t (cons 0 0)))
  8129. (cons 6 128))))
  8130. (if (>= val 192) (setq eat (car shift-xor)))
  8131. (setq val (logxor val (cdr shift-xor)))
  8132. (setq sum (+ (lsh sum (car shift-xor)) val))
  8133. (if (> eat 0) (setq eat (- eat 1)))
  8134. (cond
  8135. ((= 0 eat) ;multi byte
  8136. (setq ret (concat ret (org-char-to-string sum)))
  8137. (setq sum 0))
  8138. ((not bytes) ; single byte(s)
  8139. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8140. )) ;; end (while bytes
  8141. ret )))
  8142. (defun org-link-unescape-single-byte-sequence (hex)
  8143. "Unhexify hex-encoded single byte character sequences."
  8144. (mapconcat (lambda (byte)
  8145. (char-to-string (string-to-number byte 16)))
  8146. (cdr (split-string hex "%")) ""))
  8147. (defun org-xor (a b)
  8148. "Exclusive or."
  8149. (if a (not b) b))
  8150. (defun org-fixup-message-id-for-http (s)
  8151. "Replace special characters in a message id, so it can be used in an http query."
  8152. (when (string-match "%" s)
  8153. (setq s (mapconcat (lambda (c)
  8154. (if (eq c ?%)
  8155. "%25"
  8156. (char-to-string c)))
  8157. s "")))
  8158. (while (string-match "<" s)
  8159. (setq s (replace-match "%3C" t t s)))
  8160. (while (string-match ">" s)
  8161. (setq s (replace-match "%3E" t t s)))
  8162. (while (string-match "@" s)
  8163. (setq s (replace-match "%40" t t s)))
  8164. s)
  8165. ;;;###autoload
  8166. (defun org-insert-link-global ()
  8167. "Insert a link like Org-mode does.
  8168. This command can be called in any mode to insert a link in Org-mode syntax."
  8169. (interactive)
  8170. (org-load-modules-maybe)
  8171. (org-run-like-in-org-mode 'org-insert-link))
  8172. (defun org-insert-link (&optional complete-file link-location default-description)
  8173. "Insert a link. At the prompt, enter the link.
  8174. Completion can be used to insert any of the link protocol prefixes like
  8175. http or ftp in use.
  8176. The history can be used to select a link previously stored with
  8177. `org-store-link'. When the empty string is entered (i.e. if you just
  8178. press RET at the prompt), the link defaults to the most recently
  8179. stored link. As SPC triggers completion in the minibuffer, you need to
  8180. use M-SPC or C-q SPC to force the insertion of a space character.
  8181. You will also be prompted for a description, and if one is given, it will
  8182. be displayed in the buffer instead of the link.
  8183. If there is already a link at point, this command will allow you to edit link
  8184. and description parts.
  8185. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8186. be selected using completion. The path to the file will be relative to the
  8187. current directory if the file is in the current directory or a subdirectory.
  8188. Otherwise, the link will be the absolute path as completed in the minibuffer
  8189. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8190. option `org-link-file-path-type'.
  8191. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8192. the current directory or below.
  8193. With three \\[universal-argument] prefixes, negate the meaning of
  8194. `org-keep-stored-link-after-insertion'.
  8195. If `org-make-link-description-function' is non-nil, this function will be
  8196. called with the link target, and the result will be the default
  8197. link description.
  8198. If the LINK-LOCATION parameter is non-nil, this value will be
  8199. used as the link location instead of reading one interactively.
  8200. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8201. be used as the default description."
  8202. (interactive "P")
  8203. (let* ((wcf (current-window-configuration))
  8204. (region (if (org-region-active-p)
  8205. (buffer-substring (region-beginning) (region-end))))
  8206. (remove (and region (list (region-beginning) (region-end))))
  8207. (desc region)
  8208. tmphist ; byte-compile incorrectly complains about this
  8209. (link link-location)
  8210. entry file all-prefixes)
  8211. (cond
  8212. (link-location) ; specified by arg, just use it.
  8213. ((org-in-regexp org-bracket-link-regexp 1)
  8214. ;; We do have a link at point, and we are going to edit it.
  8215. (setq remove (list (match-beginning 0) (match-end 0)))
  8216. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8217. (setq link (read-string "Link: "
  8218. (org-link-unescape
  8219. (org-match-string-no-properties 1)))))
  8220. ((or (org-in-regexp org-angle-link-re)
  8221. (org-in-regexp org-plain-link-re))
  8222. ;; Convert to bracket link
  8223. (setq remove (list (match-beginning 0) (match-end 0))
  8224. link (read-string "Link: "
  8225. (org-remove-angle-brackets (match-string 0)))))
  8226. ((member complete-file '((4) (16)))
  8227. ;; Completing read for file names.
  8228. (setq link (org-file-complete-link complete-file)))
  8229. (t
  8230. ;; Read link, with completion for stored links.
  8231. (with-output-to-temp-buffer "*Org Links*"
  8232. (princ "Insert a link.
  8233. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8234. (when org-stored-links
  8235. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8236. (princ (mapconcat
  8237. (lambda (x)
  8238. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8239. (reverse org-stored-links) "\n"))))
  8240. (let ((cw (selected-window)))
  8241. (select-window (get-buffer-window "*Org Links*" 'visible))
  8242. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8243. (unless (pos-visible-in-window-p (point-max))
  8244. (org-fit-window-to-buffer))
  8245. (and (window-live-p cw) (select-window cw)))
  8246. ;; Fake a link history, containing the stored links.
  8247. (setq tmphist (append (mapcar 'car org-stored-links)
  8248. org-insert-link-history))
  8249. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8250. (mapcar 'car org-link-abbrev-alist)
  8251. org-link-types))
  8252. (unwind-protect
  8253. (progn
  8254. (setq link
  8255. (let ((org-completion-use-ido nil)
  8256. (org-completion-use-iswitchb nil))
  8257. (org-completing-read
  8258. "Link: "
  8259. (append
  8260. (mapcar (lambda (x) (list (concat x ":")))
  8261. all-prefixes)
  8262. (mapcar 'car org-stored-links))
  8263. nil nil nil
  8264. 'tmphist
  8265. (car (car org-stored-links)))))
  8266. (if (not (string-match "\\S-" link))
  8267. (error "No link selected"))
  8268. (if (or (member link all-prefixes)
  8269. (and (equal ":" (substring link -1))
  8270. (member (substring link 0 -1) all-prefixes)
  8271. (setq link (substring link 0 -1))))
  8272. (setq link (org-link-try-special-completion link))))
  8273. (set-window-configuration wcf)
  8274. (kill-buffer "*Org Links*"))
  8275. (setq entry (assoc link org-stored-links))
  8276. (or entry (push link org-insert-link-history))
  8277. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8278. (not org-keep-stored-link-after-insertion))
  8279. (setq org-stored-links (delq (assoc link org-stored-links)
  8280. org-stored-links)))
  8281. (setq desc (or desc (nth 1 entry)))))
  8282. (if (string-match org-plain-link-re link)
  8283. ;; URL-like link, normalize the use of angular brackets.
  8284. (setq link (org-make-link (org-remove-angle-brackets link))))
  8285. ;; Check if we are linking to the current file with a search option
  8286. ;; If yes, simplify the link by using only the search option.
  8287. (when (and buffer-file-name
  8288. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8289. (let* ((path (match-string 1 link))
  8290. (case-fold-search nil)
  8291. (search (match-string 2 link)))
  8292. (save-match-data
  8293. (if (equal (file-truename buffer-file-name) (file-truename path))
  8294. ;; We are linking to this same file, with a search option
  8295. (setq link search)))))
  8296. ;; Check if we can/should use a relative path. If yes, simplify the link
  8297. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8298. (let* ((type (match-string 1 link))
  8299. (path (match-string 2 link))
  8300. (origpath path)
  8301. (case-fold-search nil))
  8302. (cond
  8303. ((or (eq org-link-file-path-type 'absolute)
  8304. (equal complete-file '(16)))
  8305. (setq path (abbreviate-file-name (expand-file-name path))))
  8306. ((eq org-link-file-path-type 'noabbrev)
  8307. (setq path (expand-file-name path)))
  8308. ((eq org-link-file-path-type 'relative)
  8309. (setq path (file-relative-name path)))
  8310. (t
  8311. (save-match-data
  8312. (if (string-match (concat "^" (regexp-quote
  8313. (expand-file-name
  8314. (file-name-as-directory
  8315. default-directory))))
  8316. (expand-file-name path))
  8317. ;; We are linking a file with relative path name.
  8318. (setq path (substring (expand-file-name path)
  8319. (match-end 0)))
  8320. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8321. (setq link (concat type path))
  8322. (if (equal desc origpath)
  8323. (setq desc path))))
  8324. (if org-make-link-description-function
  8325. (setq desc (funcall org-make-link-description-function link desc))
  8326. (if default-description (setq desc default-description)))
  8327. (setq desc (read-string "Description: " desc))
  8328. (unless (string-match "\\S-" desc) (setq desc nil))
  8329. (if remove (apply 'delete-region remove))
  8330. (insert (org-make-link-string link desc))))
  8331. (defun org-link-try-special-completion (type)
  8332. "If there is completion support for link type TYPE, offer it."
  8333. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8334. (if (functionp fun)
  8335. (funcall fun)
  8336. (read-string "Link (no completion support): " (concat type ":")))))
  8337. (defun org-file-complete-link (&optional arg)
  8338. "Create a file link using completion."
  8339. (let (file link)
  8340. (setq file (read-file-name "File: "))
  8341. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8342. (pwd1 (file-name-as-directory (abbreviate-file-name
  8343. (expand-file-name ".")))))
  8344. (cond
  8345. ((equal arg '(16))
  8346. (setq link (org-make-link
  8347. "file:"
  8348. (abbreviate-file-name (expand-file-name file)))))
  8349. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8350. (setq link (org-make-link "file:" (match-string 1 file))))
  8351. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8352. (expand-file-name file))
  8353. (setq link (org-make-link
  8354. "file:" (match-string 1 (expand-file-name file)))))
  8355. (t (setq link (org-make-link "file:" file)))))
  8356. link))
  8357. (defun org-completing-read (&rest args)
  8358. "Completing-read with SPACE being a normal character."
  8359. (let ((enable-recursive-minibuffers t)
  8360. (minibuffer-local-completion-map
  8361. (copy-keymap minibuffer-local-completion-map)))
  8362. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8363. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8364. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8365. (apply 'org-icompleting-read args)))
  8366. (defun org-completing-read-no-i (&rest args)
  8367. (let (org-completion-use-ido org-completion-use-iswitchb)
  8368. (apply 'org-completing-read args)))
  8369. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8370. "Use iswitch as a completing-read replacement to choose from choices.
  8371. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8372. from."
  8373. (let* ((iswitchb-use-virtual-buffers nil)
  8374. (iswitchb-make-buflist-hook
  8375. (lambda ()
  8376. (setq iswitchb-temp-buflist choices))))
  8377. (iswitchb-read-buffer prompt)))
  8378. (defun org-icompleting-read (&rest args)
  8379. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8380. (org-without-partial-completion
  8381. (if (and org-completion-use-ido
  8382. (fboundp 'ido-completing-read)
  8383. (boundp 'ido-mode) ido-mode
  8384. (listp (second args)))
  8385. (let ((ido-enter-matching-directory nil))
  8386. (apply 'ido-completing-read (concat (car args))
  8387. (if (consp (car (nth 1 args)))
  8388. (mapcar 'car (nth 1 args))
  8389. (nth 1 args))
  8390. (cddr args)))
  8391. (if (and org-completion-use-iswitchb
  8392. (boundp 'iswitchb-mode) iswitchb-mode
  8393. (listp (second args)))
  8394. (apply 'org-iswitchb-completing-read (concat (car args))
  8395. (if (consp (car (nth 1 args)))
  8396. (mapcar 'car (nth 1 args))
  8397. (nth 1 args))
  8398. (cddr args))
  8399. (apply 'completing-read args)))))
  8400. (defun org-extract-attributes (s)
  8401. "Extract the attributes cookie from a string and set as text property."
  8402. (let (a attr (start 0) key value)
  8403. (save-match-data
  8404. (when (string-match "{{\\([^}]+\\)}}$" s)
  8405. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8406. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8407. (setq key (match-string 1 a) value (match-string 2 a)
  8408. start (match-end 0)
  8409. attr (plist-put attr (intern key) value))))
  8410. (org-add-props s nil 'org-attr attr))
  8411. s))
  8412. (defun org-extract-attributes-from-string (tag)
  8413. (let (key value attr)
  8414. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8415. (setq key (match-string 1 tag) value (match-string 2 tag)
  8416. tag (replace-match "" t t tag)
  8417. attr (plist-put attr (intern key) value)))
  8418. (cons tag attr)))
  8419. (defun org-attributes-to-string (plist)
  8420. "Format a property list into an HTML attribute list."
  8421. (let ((s "") key value)
  8422. (while plist
  8423. (setq key (pop plist) value (pop plist))
  8424. (and value
  8425. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8426. s))
  8427. ;;; Opening/following a link
  8428. (defvar org-link-search-failed nil)
  8429. (defvar org-open-link-functions nil
  8430. "Hook for functions finding a plain text link.
  8431. These functions must take a single argument, the link content.
  8432. They will be called for links that look like [[link text][description]]
  8433. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8434. like a filename (e.g. \"./blue.png\").
  8435. These functions will be called *before* Org attempts to resolve the
  8436. link by doing text searches in the current buffer - so if you want a
  8437. link \"[[target]]\" to still find \"<<target>>\", your function should
  8438. handle this as a special case.
  8439. When the function does handle the link, it must return a non-nil value.
  8440. If it decides that it is not responsible for this link, it must return
  8441. nil to indicate that that Org-mode can continue with other options
  8442. like exact and fuzzy text search.")
  8443. (defun org-next-link ()
  8444. "Move forward to the next link.
  8445. If the link is in hidden text, expose it."
  8446. (interactive)
  8447. (when (and org-link-search-failed (eq this-command last-command))
  8448. (goto-char (point-min))
  8449. (message "Link search wrapped back to beginning of buffer"))
  8450. (setq org-link-search-failed nil)
  8451. (let* ((pos (point))
  8452. (ct (org-context))
  8453. (a (assoc :link ct)))
  8454. (if a (goto-char (nth 2 a)))
  8455. (if (re-search-forward org-any-link-re nil t)
  8456. (progn
  8457. (goto-char (match-beginning 0))
  8458. (if (outline-invisible-p) (org-show-context)))
  8459. (goto-char pos)
  8460. (setq org-link-search-failed t)
  8461. (error "No further link found"))))
  8462. (defun org-previous-link ()
  8463. "Move backward to the previous link.
  8464. If the link is in hidden text, expose it."
  8465. (interactive)
  8466. (when (and org-link-search-failed (eq this-command last-command))
  8467. (goto-char (point-max))
  8468. (message "Link search wrapped back to end of buffer"))
  8469. (setq org-link-search-failed nil)
  8470. (let* ((pos (point))
  8471. (ct (org-context))
  8472. (a (assoc :link ct)))
  8473. (if a (goto-char (nth 1 a)))
  8474. (if (re-search-backward org-any-link-re nil t)
  8475. (progn
  8476. (goto-char (match-beginning 0))
  8477. (if (outline-invisible-p) (org-show-context)))
  8478. (goto-char pos)
  8479. (setq org-link-search-failed t)
  8480. (error "No further link found"))))
  8481. (defun org-translate-link (s)
  8482. "Translate a link string if a translation function has been defined."
  8483. (if (and org-link-translation-function
  8484. (fboundp org-link-translation-function)
  8485. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8486. (progn
  8487. (setq s (funcall org-link-translation-function
  8488. (match-string 1) (match-string 2)))
  8489. (concat (car s) ":" (cdr s)))
  8490. s))
  8491. (defun org-translate-link-from-planner (type path)
  8492. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8493. This is still an experimental function, your mileage may vary."
  8494. (cond
  8495. ((member type '("http" "https" "news" "ftp"))
  8496. ;; standard Internet links are the same.
  8497. nil)
  8498. ((and (equal type "irc") (string-match "^//" path))
  8499. ;; Planner has two / at the beginning of an irc link, we have 1.
  8500. ;; We should have zero, actually....
  8501. (setq path (substring path 1)))
  8502. ((and (equal type "lisp") (string-match "^/" path))
  8503. ;; Planner has a slash, we do not.
  8504. (setq type "elisp" path (substring path 1)))
  8505. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8506. ;; A typical message link. Planner has the id after the final slash,
  8507. ;; we separate it with a hash mark
  8508. (setq path (concat (match-string 1 path) "#"
  8509. (org-remove-angle-brackets (match-string 2 path)))))
  8510. )
  8511. (cons type path))
  8512. (defun org-find-file-at-mouse (ev)
  8513. "Open file link or URL at mouse."
  8514. (interactive "e")
  8515. (mouse-set-point ev)
  8516. (org-open-at-point 'in-emacs))
  8517. (defun org-open-at-mouse (ev)
  8518. "Open file link or URL at mouse.
  8519. See the docstring of `org-open-file' for details."
  8520. (interactive "e")
  8521. (mouse-set-point ev)
  8522. (if (eq major-mode 'org-agenda-mode)
  8523. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8524. (org-open-at-point))
  8525. (defvar org-window-config-before-follow-link nil
  8526. "The window configuration before following a link.
  8527. This is saved in case the need arises to restore it.")
  8528. (defvar org-open-link-marker (make-marker)
  8529. "Marker pointing to the location where `org-open-at-point; was called.")
  8530. ;;;###autoload
  8531. (defun org-open-at-point-global ()
  8532. "Follow a link like Org-mode does.
  8533. This command can be called in any mode to follow a link that has
  8534. Org-mode syntax."
  8535. (interactive)
  8536. (org-run-like-in-org-mode 'org-open-at-point))
  8537. ;;;###autoload
  8538. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8539. "Open a link in the string S, as if it was in Org-mode."
  8540. (interactive "sLink: \nP")
  8541. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8542. (with-temp-buffer
  8543. (let ((org-inhibit-startup (not reference-buffer)))
  8544. (org-mode)
  8545. (insert s)
  8546. (goto-char (point-min))
  8547. (when reference-buffer
  8548. (setq org-link-abbrev-alist-local
  8549. (with-current-buffer reference-buffer
  8550. org-link-abbrev-alist-local)))
  8551. (org-open-at-point arg reference-buffer)))))
  8552. (defvar org-open-at-point-functions nil
  8553. "Hook that is run when following a link at point.
  8554. Functions in this hook must return t if they identify and follow
  8555. a link at point. If they don't find anything interesting at point,
  8556. they must return nil.")
  8557. (defun org-open-at-point (&optional arg reference-buffer)
  8558. "Open link at or after point.
  8559. If there is no link at point, this function will search forward up to
  8560. the end of the current line.
  8561. Normally, files will be opened by an appropriate application. If the
  8562. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8563. With a double prefix argument, try to open outside of Emacs, in the
  8564. application the system uses for this file type."
  8565. (interactive "P")
  8566. ;; if in a code block, then open the block's results
  8567. (unless (call-interactively #'org-babel-open-src-block-result)
  8568. (org-load-modules-maybe)
  8569. (move-marker org-open-link-marker (point))
  8570. (setq org-window-config-before-follow-link (current-window-configuration))
  8571. (org-remove-occur-highlights nil nil t)
  8572. (cond
  8573. ((and (org-at-heading-p)
  8574. (not (org-in-regexp
  8575. (concat org-plain-link-re "\\|"
  8576. org-bracket-link-regexp "\\|"
  8577. org-angle-link-re "\\|"
  8578. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8579. (not (get-text-property (point) 'org-linked-text)))
  8580. (or (org-offer-links-in-entry arg)
  8581. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8582. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8583. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8584. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8585. (not (org-in-regexp org-bracket-link-regexp)))
  8586. (org-footnote-action))
  8587. (t
  8588. (let (type path link line search (pos (point)))
  8589. (catch 'match
  8590. (save-excursion
  8591. (skip-chars-forward "^]\n\r")
  8592. (when (org-in-regexp org-bracket-link-regexp 1)
  8593. (setq link (org-extract-attributes
  8594. (org-link-unescape (org-match-string-no-properties 1))))
  8595. (while (string-match " *\n *" link)
  8596. (setq link (replace-match " " t t link)))
  8597. (setq link (org-link-expand-abbrev link))
  8598. (cond
  8599. ((or (file-name-absolute-p link)
  8600. (string-match "^\\.\\.?/" link))
  8601. (setq type "file" path link))
  8602. ((string-match org-link-re-with-space3 link)
  8603. (setq type (match-string 1 link) path (match-string 2 link)))
  8604. (t (setq type "thisfile" path link)))
  8605. (throw 'match t)))
  8606. (when (get-text-property (point) 'org-linked-text)
  8607. (setq type "thisfile"
  8608. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8609. (1+ (point)) (point))
  8610. path (buffer-substring
  8611. (or (previous-single-property-change pos 'org-linked-text)
  8612. (point-min))
  8613. (or (next-single-property-change pos 'org-linked-text)
  8614. (point-max))))
  8615. (throw 'match t))
  8616. (save-excursion
  8617. (when (or (org-in-regexp org-angle-link-re)
  8618. (org-in-regexp org-plain-link-re))
  8619. (setq type (match-string 1)
  8620. path (org-link-unescape (match-string 2)))
  8621. (throw 'match t)))
  8622. (save-excursion
  8623. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8624. (setq type "tags"
  8625. path (match-string 1))
  8626. (while (string-match ":" path)
  8627. (setq path (replace-match "+" t t path)))
  8628. (throw 'match t)))
  8629. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8630. (setq type "tree-match"
  8631. path (match-string 1))
  8632. (throw 'match t)))
  8633. (unless path
  8634. (error "No link found"))
  8635. ;; switch back to reference buffer
  8636. ;; needed when if called in a temporary buffer through
  8637. ;; org-open-link-from-string
  8638. (with-current-buffer (or reference-buffer (current-buffer))
  8639. ;; Remove any trailing spaces in path
  8640. (if (string-match " +\\'" path)
  8641. (setq path (replace-match "" t t path)))
  8642. (if (and org-link-translation-function
  8643. (fboundp org-link-translation-function))
  8644. ;; Check if we need to translate the link
  8645. (let ((tmp (funcall org-link-translation-function type path)))
  8646. (setq type (car tmp) path (cdr tmp))))
  8647. (cond
  8648. ((assoc type org-link-protocols)
  8649. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8650. ((equal type "mailto")
  8651. (let ((cmd (car org-link-mailto-program))
  8652. (args (cdr org-link-mailto-program)) args1
  8653. (address path) (subject "") a)
  8654. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8655. (setq address (match-string 1 path)
  8656. subject (org-link-escape (match-string 2 path))))
  8657. (while args
  8658. (cond
  8659. ((not (stringp (car args))) (push (pop args) args1))
  8660. (t (setq a (pop args))
  8661. (if (string-match "%a" a)
  8662. (setq a (replace-match address t t a)))
  8663. (if (string-match "%s" a)
  8664. (setq a (replace-match subject t t a)))
  8665. (push a args1))))
  8666. (apply cmd (nreverse args1))))
  8667. ((member type '("http" "https" "ftp" "news"))
  8668. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8669. (org-link-escape
  8670. path org-link-escape-chars-browser)
  8671. path))))
  8672. ((string= type "doi")
  8673. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8674. (org-link-escape
  8675. path org-link-escape-chars-browser)
  8676. path))))
  8677. ((member type '("message"))
  8678. (browse-url (concat type ":" path)))
  8679. ((string= type "tags")
  8680. (org-tags-view arg path))
  8681. ((string= type "tree-match")
  8682. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8683. ((string= type "file")
  8684. (if (string-match "::\\([0-9]+\\)\\'" path)
  8685. (setq line (string-to-number (match-string 1 path))
  8686. path (substring path 0 (match-beginning 0)))
  8687. (if (string-match "::\\(.+\\)\\'" path)
  8688. (setq search (match-string 1 path)
  8689. path (substring path 0 (match-beginning 0)))))
  8690. (if (string-match "[*?{]" (file-name-nondirectory path))
  8691. (dired path)
  8692. (org-open-file path arg line search)))
  8693. ((string= type "shell")
  8694. (let ((cmd path))
  8695. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8696. (string-match org-confirm-shell-link-not-regexp cmd))
  8697. (not org-confirm-shell-link-function)
  8698. (funcall org-confirm-shell-link-function
  8699. (format "Execute \"%s\" in shell? "
  8700. (org-add-props cmd nil
  8701. 'face 'org-warning))))
  8702. (progn
  8703. (message "Executing %s" cmd)
  8704. (shell-command cmd))
  8705. (error "Abort"))))
  8706. ((string= type "elisp")
  8707. (let ((cmd path))
  8708. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8709. (string-match org-confirm-elisp-link-not-regexp cmd))
  8710. (not org-confirm-elisp-link-function)
  8711. (funcall org-confirm-elisp-link-function
  8712. (format "Execute \"%s\" as elisp? "
  8713. (org-add-props cmd nil
  8714. 'face 'org-warning))))
  8715. (message "%s => %s" cmd
  8716. (if (equal (string-to-char cmd) ?\()
  8717. (eval (read cmd))
  8718. (call-interactively (read cmd))))
  8719. (error "Abort"))))
  8720. ((and (string= type "thisfile")
  8721. (run-hook-with-args-until-success
  8722. 'org-open-link-functions path)))
  8723. ((string= type "thisfile")
  8724. (if arg
  8725. (switch-to-buffer-other-window
  8726. (org-get-buffer-for-internal-link (current-buffer)))
  8727. (org-mark-ring-push))
  8728. (let ((cmd `(org-link-search
  8729. ,path
  8730. ,(cond ((equal arg '(4)) ''occur)
  8731. ((equal arg '(16)) ''org-occur)
  8732. (t nil))
  8733. ,pos)))
  8734. (condition-case nil (let ((org-link-search-inhibit-query t))
  8735. (eval cmd))
  8736. (error (progn (widen) (eval cmd))))))
  8737. (t
  8738. (browse-url-at-point)))))))
  8739. (move-marker org-open-link-marker nil)
  8740. (run-hook-with-args 'org-follow-link-hook)))
  8741. (defun org-offer-links-in-entry (&optional nth zero)
  8742. "Offer links in the current entry and follow the selected link.
  8743. If there is only one link, follow it immediately as well.
  8744. If NTH is an integer, immediately pick the NTH link found.
  8745. If ZERO is a string, check also this string for a link, and if
  8746. there is one, offer it as link number zero."
  8747. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8748. "\\(" org-angle-link-re "\\)\\|"
  8749. "\\(" org-plain-link-re "\\)"))
  8750. (cnt ?0)
  8751. (in-emacs (if (integerp nth) nil nth))
  8752. have-zero end links link c)
  8753. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8754. (push (match-string 0 zero) links)
  8755. (setq cnt (1- cnt) have-zero t))
  8756. (save-excursion
  8757. (org-back-to-heading t)
  8758. (setq end (save-excursion (outline-next-heading) (point)))
  8759. (while (re-search-forward re end t)
  8760. (push (match-string 0) links))
  8761. (setq links (org-uniquify (reverse links))))
  8762. (cond
  8763. ((null links)
  8764. (message "No links"))
  8765. ((equal (length links) 1)
  8766. (setq link (list (car links))))
  8767. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8768. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8769. (t ; we have to select a link
  8770. (save-excursion
  8771. (save-window-excursion
  8772. (delete-other-windows)
  8773. (with-output-to-temp-buffer "*Select Link*"
  8774. (mapc (lambda (l)
  8775. (if (not (string-match org-bracket-link-regexp l))
  8776. (princ (format "[%c] %s\n" (incf cnt)
  8777. (org-remove-angle-brackets l)))
  8778. (if (match-end 3)
  8779. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8780. (match-string 3 l) (match-string 1 l)))
  8781. (princ (format "[%c] %s\n" (incf cnt)
  8782. (match-string 1 l))))))
  8783. links))
  8784. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8785. (message "Select link to open, RET to open all:")
  8786. (setq c (read-char-exclusive))
  8787. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8788. (when (equal c ?q) (error "Abort"))
  8789. (if (equal c ?\C-m)
  8790. (setq link links)
  8791. (setq nth (- c ?0))
  8792. (if have-zero (setq nth (1+ nth)))
  8793. (unless (and (integerp nth) (>= (length links) nth))
  8794. (error "Invalid link selection"))
  8795. (setq link (list (nth (1- nth) links))))))
  8796. (if link
  8797. (let ((buf (current-buffer)))
  8798. (dolist (l link)
  8799. (org-open-link-from-string l in-emacs buf))
  8800. t)
  8801. nil)))
  8802. ;; Add special file links that specify the way of opening
  8803. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8804. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8805. (defun org-open-file-with-system (path)
  8806. "Open file at PATH using the system way of opening it."
  8807. (org-open-file path 'system))
  8808. (defun org-open-file-with-emacs (path)
  8809. "Open file at PATH in Emacs."
  8810. (org-open-file path 'emacs))
  8811. (defun org-remove-file-link-modifiers ()
  8812. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8813. (goto-char (point-min))
  8814. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8815. (org-if-unprotected
  8816. (replace-match "file:" t t))))
  8817. (eval-after-load "org-exp"
  8818. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8819. 'org-remove-file-link-modifiers))
  8820. ;;;; Time estimates
  8821. (defun org-get-effort (&optional pom)
  8822. "Get the effort estimate for the current entry."
  8823. (org-entry-get pom org-effort-property))
  8824. ;;; File search
  8825. (defvar org-create-file-search-functions nil
  8826. "List of functions to construct the right search string for a file link.
  8827. These functions are called in turn with point at the location to
  8828. which the link should point.
  8829. A function in the hook should first test if it would like to
  8830. handle this file type, for example by checking the `major-mode'
  8831. or the file extension. If it decides not to handle this file, it
  8832. should just return nil to give other functions a chance. If it
  8833. does handle the file, it must return the search string to be used
  8834. when following the link. The search string will be part of the
  8835. file link, given after a double colon, and `org-open-at-point'
  8836. will automatically search for it. If special measures must be
  8837. taken to make the search successful, another function should be
  8838. added to the companion hook `org-execute-file-search-functions',
  8839. which see.
  8840. A function in this hook may also use `setq' to set the variable
  8841. `description' to provide a suggestion for the descriptive text to
  8842. be used for this link when it gets inserted into an Org-mode
  8843. buffer with \\[org-insert-link].")
  8844. (defvar org-execute-file-search-functions nil
  8845. "List of functions to execute a file search triggered by a link.
  8846. Functions added to this hook must accept a single argument, the
  8847. search string that was part of the file link, the part after the
  8848. double colon. The function must first check if it would like to
  8849. handle this search, for example by checking the `major-mode' or
  8850. the file extension. If it decides not to handle this search, it
  8851. should just return nil to give other functions a chance. If it
  8852. does handle the search, it must return a non-nil value to keep
  8853. other functions from trying.
  8854. Each function can access the current prefix argument through the
  8855. variable `current-prefix-argument'. Note that a single prefix is
  8856. used to force opening a link in Emacs, so it may be good to only
  8857. use a numeric or double prefix to guide the search function.
  8858. In case this is needed, a function in this hook can also restore
  8859. the window configuration before `org-open-at-point' was called using:
  8860. (set-window-configuration org-window-config-before-follow-link)")
  8861. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8862. (defun org-link-search (s &optional type avoid-pos stealth)
  8863. "Search for a link search option.
  8864. If S is surrounded by forward slashes, it is interpreted as a
  8865. regular expression. In org-mode files, this will create an `org-occur'
  8866. sparse tree. In ordinary files, `occur' will be used to list matches.
  8867. If the current buffer is in `dired-mode', grep will be used to search
  8868. in all files. If AVOID-POS is given, ignore matches near that position.
  8869. When optional argument STEALTH is non-nil, do not modify
  8870. visibility around point, thus ignoring
  8871. `org-show-hierarchy-above', `org-show-following-heading' and
  8872. `org-show-siblings' variables."
  8873. (let ((case-fold-search t)
  8874. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8875. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8876. (append '(("") (" ") ("\t") ("\n"))
  8877. org-emphasis-alist)
  8878. "\\|") "\\)"))
  8879. (pos (point))
  8880. (pre nil) (post nil)
  8881. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8882. (cond
  8883. ;; First check if there are any special search functions
  8884. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8885. ;; Now try the builtin stuff
  8886. ((and (equal (string-to-char s0) ?#)
  8887. (> (length s0) 1)
  8888. (save-excursion
  8889. (goto-char (point-min))
  8890. (and
  8891. (re-search-forward
  8892. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8893. (setq type 'dedicated
  8894. pos (match-beginning 0))))
  8895. ;; There is an exact target for this
  8896. (goto-char pos)
  8897. (org-back-to-heading t)))
  8898. ((save-excursion
  8899. (goto-char (point-min))
  8900. (and
  8901. (re-search-forward
  8902. (concat "<<" (regexp-quote s0) ">>") nil t)
  8903. (setq type 'dedicated
  8904. pos (match-beginning 0))))
  8905. ;; There is an exact target for this
  8906. (goto-char pos))
  8907. ((save-excursion
  8908. (goto-char (point-min))
  8909. (and
  8910. (re-search-forward
  8911. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8912. (setq type 'dedicated pos (match-beginning 0))))
  8913. ;; Found an invisible target.
  8914. (goto-char pos))
  8915. ((save-excursion
  8916. (goto-char (point-min))
  8917. (and
  8918. (re-search-forward
  8919. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8920. (setq type 'dedicated pos (match-beginning 0))))
  8921. ;; Found an element with a matching #+name affiliated keyword.
  8922. (goto-char pos))
  8923. ((and (string-match "^(\\(.*\\))$" s0)
  8924. (save-excursion
  8925. (goto-char (point-min))
  8926. (and
  8927. (re-search-forward
  8928. (concat "[^[]" (regexp-quote
  8929. (format org-coderef-label-format
  8930. (match-string 1 s0))))
  8931. nil t)
  8932. (setq type 'dedicated
  8933. pos (1+ (match-beginning 0))))))
  8934. ;; There is a coderef target for this
  8935. (goto-char pos))
  8936. ((string-match "^/\\(.*\\)/$" s)
  8937. ;; A regular expression
  8938. (cond
  8939. ((eq major-mode 'org-mode)
  8940. (org-occur (match-string 1 s)))
  8941. ;;((eq major-mode 'dired-mode)
  8942. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8943. (t (org-do-occur (match-string 1 s)))))
  8944. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8945. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8946. (goto-char (point-min))
  8947. (cond
  8948. ((let (case-fold-search)
  8949. (re-search-forward (format org-complex-heading-regexp-format
  8950. (regexp-quote s))
  8951. nil t))
  8952. ;; OK, found a match
  8953. (setq type 'dedicated)
  8954. (goto-char (match-beginning 0)))
  8955. ((and (not org-link-search-inhibit-query)
  8956. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8957. (y-or-n-p "No match - create this as a new heading? "))
  8958. (goto-char (point-max))
  8959. (or (bolp) (newline))
  8960. (insert "* " s "\n")
  8961. (beginning-of-line 0))
  8962. (t
  8963. (goto-char pos)
  8964. (error "No match"))))
  8965. (t
  8966. ;; A normal search string
  8967. (when (equal (string-to-char s) ?*)
  8968. ;; Anchor on headlines, post may include tags.
  8969. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8970. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8971. s (substring s 1)))
  8972. (remove-text-properties
  8973. 0 (length s)
  8974. '(face nil mouse-face nil keymap nil fontified nil) s)
  8975. ;; Make a series of regular expressions to find a match
  8976. (setq words (org-split-string s "[ \n\r\t]+")
  8977. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8978. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8979. "\\)" markers)
  8980. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8981. re2a (concat "[ \t\r\n]" re2a_)
  8982. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8983. re4 (concat "[^a-zA-Z_]" re4_)
  8984. re1 (concat pre re2 post)
  8985. re3 (concat pre (if pre re4_ re4) post)
  8986. re5 (concat pre ".*" re4)
  8987. re2 (concat pre re2)
  8988. re2a (concat pre (if pre re2a_ re2a))
  8989. re4 (concat pre (if pre re4_ re4))
  8990. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8991. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8992. re5 "\\)"
  8993. ))
  8994. (cond
  8995. ((eq type 'org-occur) (org-occur reall))
  8996. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8997. (t (goto-char (point-min))
  8998. (setq type 'fuzzy)
  8999. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9000. (org-search-not-self 1 re1 nil t)
  9001. (org-search-not-self 1 re2 nil t)
  9002. (org-search-not-self 1 re2a nil t)
  9003. (org-search-not-self 1 re3 nil t)
  9004. (org-search-not-self 1 re4 nil t)
  9005. (org-search-not-self 1 re5 nil t)
  9006. )
  9007. (goto-char (match-beginning 1))
  9008. (goto-char pos)
  9009. (error "No match"))))))
  9010. (and (eq major-mode 'org-mode)
  9011. (not stealth)
  9012. (org-show-context 'link-search))
  9013. type))
  9014. (defun org-search-not-self (group &rest args)
  9015. "Execute `re-search-forward', but only accept matches that do not
  9016. enclose the position of `org-open-link-marker'."
  9017. (let ((m org-open-link-marker))
  9018. (catch 'exit
  9019. (while (apply 're-search-forward args)
  9020. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9021. (goto-char (match-end group))
  9022. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9023. (> (match-beginning 0) (marker-position m))
  9024. (< (match-end 0) (marker-position m)))
  9025. (save-match-data
  9026. (or (not (org-in-regexp
  9027. org-bracket-link-analytic-regexp 1))
  9028. (not (match-end 4)) ; no description
  9029. (and (<= (match-beginning 4) (point))
  9030. (>= (match-end 4) (point))))))
  9031. (throw 'exit (point))))))))
  9032. (defun org-get-buffer-for-internal-link (buffer)
  9033. "Return a buffer to be used for displaying the link target of internal links."
  9034. (cond
  9035. ((not org-display-internal-link-with-indirect-buffer)
  9036. buffer)
  9037. ((string-match "(Clone)$" (buffer-name buffer))
  9038. (message "Buffer is already a clone, not making another one")
  9039. ;; we also do not modify visibility in this case
  9040. buffer)
  9041. (t ; make a new indirect buffer for displaying the link
  9042. (let* ((bn (buffer-name buffer))
  9043. (ibn (concat bn "(Clone)"))
  9044. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9045. (with-current-buffer ib (org-overview))
  9046. ib))))
  9047. (defun org-do-occur (regexp &optional cleanup)
  9048. "Call the Emacs command `occur'.
  9049. If CLEANUP is non-nil, remove the printout of the regular expression
  9050. in the *Occur* buffer. This is useful if the regex is long and not useful
  9051. to read."
  9052. (occur regexp)
  9053. (when cleanup
  9054. (let ((cwin (selected-window)) win beg end)
  9055. (when (setq win (get-buffer-window "*Occur*"))
  9056. (select-window win))
  9057. (goto-char (point-min))
  9058. (when (re-search-forward "match[a-z]+" nil t)
  9059. (setq beg (match-end 0))
  9060. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9061. (setq end (1- (match-beginning 0)))))
  9062. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9063. (goto-char (point-min))
  9064. (select-window cwin))))
  9065. ;;; The mark ring for links jumps
  9066. (defvar org-mark-ring nil
  9067. "Mark ring for positions before jumps in Org-mode.")
  9068. (defvar org-mark-ring-last-goto nil
  9069. "Last position in the mark ring used to go back.")
  9070. ;; Fill and close the ring
  9071. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9072. (loop for i from 1 to org-mark-ring-length do
  9073. (push (make-marker) org-mark-ring))
  9074. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9075. org-mark-ring)
  9076. (defun org-mark-ring-push (&optional pos buffer)
  9077. "Put the current position or POS into the mark ring and rotate it."
  9078. (interactive)
  9079. (setq pos (or pos (point)))
  9080. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9081. (move-marker (car org-mark-ring)
  9082. (or pos (point))
  9083. (or buffer (current-buffer)))
  9084. (message "%s"
  9085. (substitute-command-keys
  9086. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9087. (defun org-mark-ring-goto (&optional n)
  9088. "Jump to the previous position in the mark ring.
  9089. With prefix arg N, jump back that many stored positions. When
  9090. called several times in succession, walk through the entire ring.
  9091. Org-mode commands jumping to a different position in the current file,
  9092. or to another Org-mode file, automatically push the old position
  9093. onto the ring."
  9094. (interactive "p")
  9095. (let (p m)
  9096. (if (eq last-command this-command)
  9097. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9098. (setq p org-mark-ring))
  9099. (setq org-mark-ring-last-goto p)
  9100. (setq m (car p))
  9101. (org-pop-to-buffer-same-window (marker-buffer m))
  9102. (goto-char m)
  9103. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9104. (defun org-remove-angle-brackets (s)
  9105. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9106. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9107. s)
  9108. (defun org-add-angle-brackets (s)
  9109. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9110. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9111. s)
  9112. (defun org-remove-double-quotes (s)
  9113. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9114. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9115. s)
  9116. ;;; Following specific links
  9117. (defun org-follow-timestamp-link ()
  9118. (cond
  9119. ((org-at-date-range-p t)
  9120. (let ((org-agenda-start-on-weekday)
  9121. (t1 (match-string 1))
  9122. (t2 (match-string 2)))
  9123. (setq t1 (time-to-days (org-time-string-to-time t1))
  9124. t2 (time-to-days (org-time-string-to-time t2)))
  9125. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9126. ((org-at-timestamp-p t)
  9127. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9128. (substring (match-string 1) 0 10)))
  9129. 1))
  9130. (t (error "This should not happen"))))
  9131. ;;; Following file links
  9132. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9133. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9134. (declare-function mailcap-mime-info
  9135. "mailcap" (string &optional request no-decode))
  9136. (defvar org-wait nil)
  9137. (defun org-open-file (path &optional in-emacs line search)
  9138. "Open the file at PATH.
  9139. First, this expands any special file name abbreviations. Then the
  9140. configuration variable `org-file-apps' is checked if it contains an
  9141. entry for this file type, and if yes, the corresponding command is launched.
  9142. If no application is found, Emacs simply visits the file.
  9143. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9144. With a double \\[universal-argument] \\[universal-argument] \
  9145. prefix arg, Org tries to avoid opening in Emacs
  9146. and to use an external application to visit the file.
  9147. Optional LINE specifies a line to go to, optional SEARCH a string
  9148. to search for. If LINE or SEARCH is given, the file will be
  9149. opened in Emacs, unless an entry from org-file-apps that makes
  9150. use of groups in a regexp matches.
  9151. If you want to change the way frames are used when following a
  9152. link, please customize `org-link-frame-setup'.
  9153. If the file does not exist, an error is thrown."
  9154. (let* ((file (if (equal path "")
  9155. buffer-file-name
  9156. (substitute-in-file-name (expand-file-name path))))
  9157. (file-apps (append org-file-apps (org-default-apps)))
  9158. (apps (org-remove-if
  9159. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9160. (apps-dlink (org-remove-if-not
  9161. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9162. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9163. (dirp (if remp nil (file-directory-p file)))
  9164. (file (if (and dirp org-open-directory-means-index-dot-org)
  9165. (concat (file-name-as-directory file) "index.org")
  9166. file))
  9167. (a-m-a-p (assq 'auto-mode apps))
  9168. (dfile (downcase file))
  9169. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9170. (link (cond ((and (eq line nil)
  9171. (eq search nil))
  9172. file)
  9173. (line
  9174. (concat file "::" (number-to-string line)))
  9175. (search
  9176. (concat file "::" search))))
  9177. (dlink (downcase link))
  9178. (old-buffer (current-buffer))
  9179. (old-pos (point))
  9180. (old-mode major-mode)
  9181. ext cmd link-match-data)
  9182. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9183. (setq ext (match-string 1 dfile))
  9184. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9185. (setq ext (match-string 1 dfile))))
  9186. (cond
  9187. ((member in-emacs '((16) system))
  9188. (setq cmd (cdr (assoc 'system apps))))
  9189. (in-emacs (setq cmd 'emacs))
  9190. (t
  9191. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9192. (and dirp (cdr (assoc 'directory apps)))
  9193. ; first, try matching against apps-dlink
  9194. ; if we get a match here, store the match data for later
  9195. (let ((match (assoc-default dlink apps-dlink
  9196. 'string-match)))
  9197. (if match
  9198. (progn (setq link-match-data (match-data))
  9199. match)
  9200. (progn (setq in-emacs (or in-emacs line search))
  9201. nil))) ; if we have no match in apps-dlink,
  9202. ; always open the file in emacs if line or search
  9203. ; is given (for backwards compatibility)
  9204. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9205. 'string-match)
  9206. (cdr (assoc ext apps))
  9207. (cdr (assoc t apps))))))
  9208. (when (eq cmd 'system)
  9209. (setq cmd (cdr (assoc 'system apps))))
  9210. (when (eq cmd 'default)
  9211. (setq cmd (cdr (assoc t apps))))
  9212. (when (eq cmd 'mailcap)
  9213. (require 'mailcap)
  9214. (mailcap-parse-mailcaps)
  9215. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9216. (command (mailcap-mime-info mime-type)))
  9217. (if (stringp command)
  9218. (setq cmd command)
  9219. (setq cmd 'emacs))))
  9220. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9221. (not (file-exists-p file))
  9222. (not org-open-non-existing-files))
  9223. (error "No such file: %s" file))
  9224. (cond
  9225. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9226. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9227. (while (string-match "['\"]%s['\"]" cmd)
  9228. (setq cmd (replace-match "%s" t t cmd)))
  9229. (while (string-match "%s" cmd)
  9230. (setq cmd (replace-match
  9231. (save-match-data
  9232. (shell-quote-argument
  9233. (convert-standard-filename file)))
  9234. t t cmd)))
  9235. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9236. (save-match-data
  9237. (let ((match-index 1)
  9238. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9239. (set-match-data link-match-data)
  9240. (while (<= match-index number-of-groups)
  9241. (let ((regex (concat "%" (number-to-string match-index)))
  9242. (replace-with (match-string match-index dlink)))
  9243. (while (string-match regex cmd)
  9244. (setq cmd (replace-match replace-with t t cmd))))
  9245. (setq match-index (+ match-index 1)))))
  9246. (save-window-excursion
  9247. (start-process-shell-command cmd nil cmd)
  9248. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9249. ))
  9250. ((or (stringp cmd)
  9251. (eq cmd 'emacs))
  9252. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9253. (widen)
  9254. (if line (org-goto-line line)
  9255. (if search (org-link-search search))))
  9256. ((consp cmd)
  9257. (let ((file (convert-standard-filename file)))
  9258. (save-match-data
  9259. (set-match-data link-match-data)
  9260. (eval cmd))))
  9261. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9262. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9263. (or (not (equal old-buffer (current-buffer)))
  9264. (not (equal old-pos (point))))
  9265. (org-mark-ring-push old-pos old-buffer))))
  9266. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9267. "This function returns non-nil if `entry' uses a regular
  9268. expression which should be matched against the whole link by
  9269. org-open-file.
  9270. It assumes that is the case when the entry uses a regular
  9271. expression which has at least one grouping construct and the
  9272. action is either a lisp form or a command string containing
  9273. '%1', i.e. using at least one subexpression match as a
  9274. parameter."
  9275. (let ((selector (car entry))
  9276. (action (cdr entry)))
  9277. (if (stringp selector)
  9278. (and (> (regexp-opt-depth selector) 0)
  9279. (or (and (stringp action)
  9280. (string-match "%[0-9]" action))
  9281. (consp action)))
  9282. nil)))
  9283. (defun org-default-apps ()
  9284. "Return the default applications for this operating system."
  9285. (cond
  9286. ((eq system-type 'darwin)
  9287. org-file-apps-defaults-macosx)
  9288. ((eq system-type 'windows-nt)
  9289. org-file-apps-defaults-windowsnt)
  9290. (t org-file-apps-defaults-gnu)))
  9291. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9292. "Convert extensions to regular expressions in the cars of LIST.
  9293. Also, weed out any non-string entries, because the return value is used
  9294. only for regexp matching.
  9295. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9296. point to the symbol `emacs', indicating that the file should
  9297. be opened in Emacs."
  9298. (append
  9299. (delq nil
  9300. (mapcar (lambda (x)
  9301. (if (not (stringp (car x)))
  9302. nil
  9303. (if (string-match "\\W" (car x))
  9304. x
  9305. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9306. list))
  9307. (if add-auto-mode
  9308. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9309. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9310. (defun org-file-remote-p (file)
  9311. "Test whether FILE specifies a location on a remote system.
  9312. Return non-nil if the location is indeed remote.
  9313. For example, the filename \"/user@host:/foo\" specifies a location
  9314. on the system \"/user@host:\"."
  9315. (cond ((fboundp 'file-remote-p)
  9316. (file-remote-p file))
  9317. ((fboundp 'tramp-handle-file-remote-p)
  9318. (tramp-handle-file-remote-p file))
  9319. ((and (boundp 'ange-ftp-name-format)
  9320. (string-match (car ange-ftp-name-format) file))
  9321. t)
  9322. (t nil)))
  9323. ;;;; Refiling
  9324. (defun org-get-org-file ()
  9325. "Read a filename, with default directory `org-directory'."
  9326. (let ((default (or org-default-notes-file remember-data-file)))
  9327. (read-file-name (format "File name [%s]: " default)
  9328. (file-name-as-directory org-directory)
  9329. default)))
  9330. (defun org-notes-order-reversed-p ()
  9331. "Check if the current file should receive notes in reversed order."
  9332. (cond
  9333. ((not org-reverse-note-order) nil)
  9334. ((eq t org-reverse-note-order) t)
  9335. ((not (listp org-reverse-note-order)) nil)
  9336. (t (catch 'exit
  9337. (let ((all org-reverse-note-order)
  9338. entry)
  9339. (while (setq entry (pop all))
  9340. (if (string-match (car entry) buffer-file-name)
  9341. (throw 'exit (cdr entry))))
  9342. nil)))))
  9343. (defvar org-refile-target-table nil
  9344. "The list of refile targets, created by `org-refile'.")
  9345. (defvar org-agenda-new-buffers nil
  9346. "Buffers created to visit agenda files.")
  9347. (defvar org-refile-cache nil
  9348. "Cache for refile targets.")
  9349. (defvar org-refile-markers nil
  9350. "All the markers used for caching refile locations.")
  9351. (defun org-refile-marker (pos)
  9352. "Get a new refile marker, but only if caching is in use."
  9353. (if (not org-refile-use-cache)
  9354. pos
  9355. (let ((m (make-marker)))
  9356. (move-marker m pos)
  9357. (push m org-refile-markers)
  9358. m)))
  9359. (defun org-refile-cache-clear ()
  9360. "Clear the refile cache and disable all the markers."
  9361. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9362. (setq org-refile-markers nil)
  9363. (setq org-refile-cache nil)
  9364. (message "Refile cache has been cleared"))
  9365. (defun org-refile-cache-check-set (set)
  9366. "Check if all the markers in the cache still have live buffers."
  9367. (let (marker)
  9368. (catch 'exit
  9369. (while (and set (setq marker (nth 3 (pop set))))
  9370. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9371. (when (and marker (null (marker-buffer marker)))
  9372. (message "not found") (sit-for 3)
  9373. (throw 'exit nil)))
  9374. t)))
  9375. (defun org-refile-cache-put (set &rest identifiers)
  9376. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9377. (let* ((key (sha1 (prin1-to-string identifiers)))
  9378. (entry (assoc key org-refile-cache)))
  9379. (if entry
  9380. (setcdr entry set)
  9381. (push (cons key set) org-refile-cache))))
  9382. (defun org-refile-cache-get (&rest identifiers)
  9383. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9384. (cond
  9385. ((not org-refile-cache) nil)
  9386. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9387. (t
  9388. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9389. org-refile-cache))))
  9390. (and set (org-refile-cache-check-set set) set)))))
  9391. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9392. "Produce a table with refile targets."
  9393. (let ((case-fold-search nil)
  9394. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9395. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9396. targets tgs txt re files f desc descre fast-path-p level pos0)
  9397. (message "Getting targets...")
  9398. (with-current-buffer (or default-buffer (current-buffer))
  9399. (while (setq entry (pop entries))
  9400. (setq files (car entry) desc (cdr entry))
  9401. (setq fast-path-p nil)
  9402. (cond
  9403. ((null files) (setq files (list (current-buffer))))
  9404. ((eq files 'org-agenda-files)
  9405. (setq files (org-agenda-files 'unrestricted)))
  9406. ((and (symbolp files) (fboundp files))
  9407. (setq files (funcall files)))
  9408. ((and (symbolp files) (boundp files))
  9409. (setq files (symbol-value files))))
  9410. (if (stringp files) (setq files (list files)))
  9411. (cond
  9412. ((eq (car desc) :tag)
  9413. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9414. ((eq (car desc) :todo)
  9415. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9416. ((eq (car desc) :regexp)
  9417. (setq descre (cdr desc)))
  9418. ((eq (car desc) :level)
  9419. (setq descre (concat "^\\*\\{" (number-to-string
  9420. (if org-odd-levels-only
  9421. (1- (* 2 (cdr desc)))
  9422. (cdr desc)))
  9423. "\\}[ \t]")))
  9424. ((eq (car desc) :maxlevel)
  9425. (setq fast-path-p t)
  9426. (setq descre (concat "^\\*\\{1," (number-to-string
  9427. (if org-odd-levels-only
  9428. (1- (* 2 (cdr desc)))
  9429. (cdr desc)))
  9430. "\\}[ \t]")))
  9431. (t (error "Bad refiling target description %s" desc)))
  9432. (while (setq f (pop files))
  9433. (with-current-buffer
  9434. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9435. (or
  9436. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9437. (progn
  9438. (if (bufferp f) (setq f (buffer-file-name
  9439. (buffer-base-buffer f))))
  9440. (setq f (and f (expand-file-name f)))
  9441. (if (eq org-refile-use-outline-path 'file)
  9442. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9443. (save-excursion
  9444. (save-restriction
  9445. (widen)
  9446. (goto-char (point-min))
  9447. (while (re-search-forward descre nil t)
  9448. (goto-char (setq pos0 (point-at-bol)))
  9449. (catch 'next
  9450. (when org-refile-target-verify-function
  9451. (save-match-data
  9452. (or (funcall org-refile-target-verify-function)
  9453. (throw 'next t))))
  9454. (when (and (looking-at org-complex-heading-regexp)
  9455. (not (member (match-string 4) excluded-entries))
  9456. (match-string 4))
  9457. (setq level (org-reduced-level
  9458. (- (match-end 1) (match-beginning 1)))
  9459. txt (org-link-display-format (match-string 4))
  9460. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9461. re (format org-complex-heading-regexp-format
  9462. (regexp-quote (match-string 4))))
  9463. (when org-refile-use-outline-path
  9464. (setq txt (mapconcat
  9465. 'org-protect-slash
  9466. (append
  9467. (if (eq org-refile-use-outline-path
  9468. 'file)
  9469. (list (file-name-nondirectory
  9470. (buffer-file-name
  9471. (buffer-base-buffer))))
  9472. (if (eq org-refile-use-outline-path
  9473. 'full-file-path)
  9474. (list (buffer-file-name
  9475. (buffer-base-buffer)))))
  9476. (org-get-outline-path fast-path-p
  9477. level txt)
  9478. (list txt))
  9479. "/")))
  9480. (push (list txt f re (org-refile-marker (point)))
  9481. tgs)))
  9482. (when (= (point) pos0)
  9483. ;; verification function has not moved point
  9484. (goto-char (point-at-eol))))))))
  9485. (when org-refile-use-cache
  9486. (org-refile-cache-put tgs (buffer-file-name) descre))
  9487. (setq targets (append tgs targets))
  9488. ))))
  9489. (message "Getting targets...done")
  9490. (nreverse targets)))
  9491. (defun org-protect-slash (s)
  9492. (while (string-match "/" s)
  9493. (setq s (replace-match "\\" t t s)))
  9494. s)
  9495. (defvar org-olpa (make-vector 20 nil))
  9496. (defun org-get-outline-path (&optional fastp level heading)
  9497. "Return the outline path to the current entry, as a list.
  9498. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9499. routine which makes outline path derivations for an entire file,
  9500. avoiding backtracing. Refile target collection makes use of that."
  9501. (if fastp
  9502. (progn
  9503. (if (> level 19)
  9504. (error "Outline path failure, more than 19 levels"))
  9505. (loop for i from level upto 19 do
  9506. (aset org-olpa i nil))
  9507. (prog1
  9508. (delq nil (append org-olpa nil))
  9509. (aset org-olpa level heading)))
  9510. (let (rtn case-fold-search)
  9511. (save-excursion
  9512. (save-restriction
  9513. (widen)
  9514. (while (org-up-heading-safe)
  9515. (when (looking-at org-complex-heading-regexp)
  9516. (push (org-match-string-no-properties 4) rtn)))
  9517. rtn)))))
  9518. (defun org-format-outline-path (path &optional width prefix)
  9519. "Format the outline path PATH for display.
  9520. Width is the maximum number of characters that is available.
  9521. Prefix is a prefix to be included in the returned string,
  9522. such as the file name."
  9523. (setq width (or width 79))
  9524. (if prefix (setq width (- width (length prefix))))
  9525. (if (not path)
  9526. (or prefix "")
  9527. (let* ((nsteps (length path))
  9528. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9529. (maxwidth (if (<= total-width width)
  9530. 10000 ;; everything fits
  9531. ;; we need to shorten the level headings
  9532. (/ (- width nsteps) nsteps)))
  9533. (org-odd-levels-only nil)
  9534. (n 0)
  9535. (total (1+ (length prefix))))
  9536. (setq maxwidth (max maxwidth 10))
  9537. (concat prefix
  9538. (mapconcat
  9539. (lambda (h)
  9540. (setq n (1+ n))
  9541. (if (and (= n nsteps) (< maxwidth 10000))
  9542. (setq maxwidth (- total-width total)))
  9543. (if (< (length h) maxwidth)
  9544. (progn (setq total (+ total (length h) 1)) h)
  9545. (setq h (substring h 0 (- maxwidth 2))
  9546. total (+ total maxwidth 1))
  9547. (if (string-match "[ \t]+\\'" h)
  9548. (setq h (substring h 0 (match-beginning 0))))
  9549. (setq h (concat h "..")))
  9550. (org-add-props h nil 'face
  9551. (nth (% (1- n) org-n-level-faces)
  9552. org-level-faces))
  9553. h)
  9554. path "/")))))
  9555. (defun org-display-outline-path (&optional file current)
  9556. "Display the current outline path in the echo area."
  9557. (interactive "P")
  9558. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9559. (case-fold-search nil)
  9560. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9561. (if current (setq path (append path
  9562. (save-excursion
  9563. (org-back-to-heading t)
  9564. (if (looking-at org-complex-heading-regexp)
  9565. (list (match-string 4)))))))
  9566. (message "%s"
  9567. (org-format-outline-path
  9568. path
  9569. (1- (frame-width))
  9570. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9571. (defvar org-refile-history nil
  9572. "History for refiling operations.")
  9573. (defvar org-after-refile-insert-hook nil
  9574. "Hook run after `org-refile' has inserted its stuff at the new location.
  9575. Note that this is still *before* the stuff will be removed from
  9576. the *old* location.")
  9577. (defvar org-capture-last-stored-marker)
  9578. (defun org-refile (&optional goto default-buffer rfloc)
  9579. "Move the entry or entries at point to another heading.
  9580. The list of target headings is compiled using the information in
  9581. `org-refile-targets', which see.
  9582. At the target location, the entry is filed as a subitem of the target
  9583. heading. Depending on `org-reverse-note-order', the new subitem will
  9584. either be the first or the last subitem.
  9585. If there is an active region, all entries in that region will be moved.
  9586. However, the region must fulfill the requirement that the first heading
  9587. is the first one sets the top-level of the moved text - at most siblings
  9588. below it are allowed.
  9589. With prefix arg GOTO, the command will only visit the target location
  9590. and not actually move anything.
  9591. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9592. go to the location where the last refiling operation has put the subtree.
  9593. With a prefix argument of `2', refile to the running clock.
  9594. RFLOC can be a refile location obtained in a different way.
  9595. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9596. If you are using target caching (see `org-refile-use-cache'),
  9597. You have to clear the target cache in order to find new targets.
  9598. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9599. prefix argument (`C-u C-u C-u C-c C-w')."
  9600. (interactive "P")
  9601. (if (member goto '(0 (64)))
  9602. (org-refile-cache-clear)
  9603. (let* ((cbuf (current-buffer))
  9604. (regionp (org-region-active-p))
  9605. (region-start (and regionp (region-beginning)))
  9606. (region-end (and regionp (region-end)))
  9607. (region-length (and regionp (- region-end region-start)))
  9608. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9609. pos it nbuf file re level reversed)
  9610. (setq last-command nil)
  9611. (when regionp
  9612. (goto-char region-start)
  9613. (or (bolp) (goto-char (point-at-bol)))
  9614. (setq region-start (point))
  9615. (unless (or (org-kill-is-subtree-p
  9616. (buffer-substring region-start region-end))
  9617. (prog1 org-refile-active-region-within-subtree
  9618. (org-toggle-heading)))
  9619. (error "The region is not a (sequence of) subtree(s)")))
  9620. (if (equal goto '(16))
  9621. (org-refile-goto-last-stored)
  9622. (when (or
  9623. (and (equal goto 2)
  9624. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9625. (prog1
  9626. (setq it (list (or org-clock-heading "running clock")
  9627. (buffer-file-name
  9628. (marker-buffer org-clock-hd-marker))
  9629. ""
  9630. (marker-position org-clock-hd-marker)))
  9631. (setq goto nil)))
  9632. (setq it (or rfloc
  9633. (let (heading-text)
  9634. (save-excursion
  9635. (unless goto
  9636. (org-back-to-heading t)
  9637. (setq heading-text
  9638. (nth 4 (org-heading-components))))
  9639. (org-refile-get-location
  9640. (cond (goto "Goto")
  9641. (regionp "Refile region to")
  9642. (t (concat "Refile subtree \""
  9643. heading-text "\" to")))
  9644. default-buffer
  9645. org-refile-allow-creating-parent-nodes
  9646. goto))))))
  9647. (setq file (nth 1 it)
  9648. re (nth 2 it)
  9649. pos (nth 3 it))
  9650. (if (and (not goto)
  9651. pos
  9652. (equal (buffer-file-name) file)
  9653. (if regionp
  9654. (and (>= pos region-start)
  9655. (<= pos region-end))
  9656. (and (>= pos (point))
  9657. (< pos (save-excursion
  9658. (org-end-of-subtree t t))))))
  9659. (error "Cannot refile to position inside the tree or region"))
  9660. (setq nbuf (or (find-buffer-visiting file)
  9661. (find-file-noselect file)))
  9662. (if goto
  9663. (progn
  9664. (org-pop-to-buffer-same-window nbuf)
  9665. (goto-char pos)
  9666. (org-show-context 'org-goto))
  9667. (if regionp
  9668. (progn
  9669. (org-kill-new (buffer-substring region-start region-end))
  9670. (org-save-markers-in-region region-start region-end))
  9671. (org-copy-subtree 1 nil t))
  9672. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9673. (find-file-noselect file)))
  9674. (setq reversed (org-notes-order-reversed-p))
  9675. (save-excursion
  9676. (save-restriction
  9677. (widen)
  9678. (if pos
  9679. (progn
  9680. (goto-char pos)
  9681. (looking-at org-outline-regexp)
  9682. (setq level (org-get-valid-level (funcall outline-level) 1))
  9683. (goto-char
  9684. (if reversed
  9685. (or (outline-next-heading) (point-max))
  9686. (or (save-excursion (org-get-next-sibling))
  9687. (org-end-of-subtree t t)
  9688. (point-max)))))
  9689. (setq level 1)
  9690. (if (not reversed)
  9691. (goto-char (point-max))
  9692. (goto-char (point-min))
  9693. (or (outline-next-heading) (goto-char (point-max)))))
  9694. (if (not (bolp)) (newline))
  9695. (org-paste-subtree level)
  9696. (when org-log-refile
  9697. (org-add-log-setup 'refile nil nil 'findpos
  9698. org-log-refile)
  9699. (unless (eq org-log-refile 'note)
  9700. (save-excursion (org-add-log-note))))
  9701. (and org-auto-align-tags (org-set-tags nil t))
  9702. (bookmark-set "org-refile-last-stored")
  9703. ;; If we are refiling for capture, make sure that the
  9704. ;; last-capture pointers point here
  9705. (when (org-bound-and-true-p org-refile-for-capture)
  9706. (bookmark-set "org-capture-last-stored-marker")
  9707. (move-marker org-capture-last-stored-marker (point)))
  9708. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9709. (run-hooks 'org-after-refile-insert-hook))))
  9710. (if regionp
  9711. (delete-region (point) (+ (point) region-length))
  9712. (org-cut-subtree))
  9713. (when (featurep 'org-inlinetask)
  9714. (org-inlinetask-remove-END-maybe))
  9715. (setq org-markers-to-move nil)
  9716. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9717. (defun org-refile-goto-last-stored ()
  9718. "Go to the location where the last refile was stored."
  9719. (interactive)
  9720. (bookmark-jump "org-refile-last-stored")
  9721. (message "This is the location of the last refile"))
  9722. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9723. no-exclude)
  9724. "Prompt the user for a refile location, using PROMPT.
  9725. PROMPT should not be suffixed with a colon and a space, because
  9726. this function appends the default value from
  9727. `org-refile-history' automatically, if that is not empty.
  9728. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9729. this is used for the GOTO interface."
  9730. (let ((org-refile-targets org-refile-targets)
  9731. (org-refile-use-outline-path org-refile-use-outline-path)
  9732. excluded-entries)
  9733. (when (and (eq major-mode 'org-mode)
  9734. (not org-refile-use-cache)
  9735. (not no-exclude))
  9736. (org-map-tree
  9737. (lambda()
  9738. (setq excluded-entries
  9739. (append excluded-entries (list (org-get-heading t t)))))))
  9740. (setq org-refile-target-table
  9741. (org-refile-get-targets default-buffer excluded-entries)))
  9742. (unless org-refile-target-table
  9743. (error "No refile targets"))
  9744. (let* ((prompt (concat prompt
  9745. (and (car org-refile-history)
  9746. (concat " (default " (car org-refile-history) ")"))
  9747. ": "))
  9748. (cbuf (current-buffer))
  9749. (partial-completion-mode nil)
  9750. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9751. (cfunc (if (and org-refile-use-outline-path
  9752. org-outline-path-complete-in-steps)
  9753. 'org-olpath-completing-read
  9754. 'org-icompleting-read))
  9755. (extra (if org-refile-use-outline-path "/" ""))
  9756. (filename (and cfn (expand-file-name cfn)))
  9757. (tbl (mapcar
  9758. (lambda (x)
  9759. (if (and (not (member org-refile-use-outline-path
  9760. '(file full-file-path)))
  9761. (not (equal filename (nth 1 x))))
  9762. (cons (concat (car x) extra " ("
  9763. (file-name-nondirectory (nth 1 x)) ")")
  9764. (cdr x))
  9765. (cons (concat (car x) extra) (cdr x))))
  9766. org-refile-target-table))
  9767. (completion-ignore-case t)
  9768. pa answ parent-target child parent old-hist)
  9769. (setq old-hist org-refile-history)
  9770. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9771. nil 'org-refile-history (car org-refile-history)))
  9772. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9773. (org-refile-check-position pa)
  9774. (if pa
  9775. (progn
  9776. (when (or (not org-refile-history)
  9777. (not (eq old-hist org-refile-history))
  9778. (not (equal (car pa) (car org-refile-history))))
  9779. (setq org-refile-history
  9780. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9781. org-refile-history
  9782. (cdr org-refile-history))))
  9783. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9784. (pop org-refile-history)))
  9785. pa)
  9786. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9787. (progn
  9788. (setq parent (match-string 1 answ)
  9789. child (match-string 2 answ))
  9790. (setq parent-target (or (assoc parent tbl)
  9791. (assoc (concat parent "/") tbl)))
  9792. (when (and parent-target
  9793. (or (eq new-nodes t)
  9794. (and (eq new-nodes 'confirm)
  9795. (y-or-n-p (format "Create new node \"%s\"? "
  9796. child)))))
  9797. (org-refile-new-child parent-target child)))
  9798. (error "Invalid target location")))))
  9799. (defun org-refile-check-position (refile-pointer)
  9800. "Check if the refile pointer matches the readline to which it points."
  9801. (let* ((file (nth 1 refile-pointer))
  9802. (re (nth 2 refile-pointer))
  9803. (pos (nth 3 refile-pointer))
  9804. buffer)
  9805. (when (org-string-nw-p re)
  9806. (setq buffer (if (markerp pos)
  9807. (marker-buffer pos)
  9808. (or (find-buffer-visiting file)
  9809. (find-file-noselect file))))
  9810. (with-current-buffer buffer
  9811. (save-excursion
  9812. (save-restriction
  9813. (widen)
  9814. (goto-char pos)
  9815. (beginning-of-line 1)
  9816. (unless (org-looking-at-p re)
  9817. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9818. (defun org-refile-new-child (parent-target child)
  9819. "Use refile target PARENT-TARGET to add new CHILD below it."
  9820. (unless parent-target
  9821. (error "Cannot find parent for new node"))
  9822. (let ((file (nth 1 parent-target))
  9823. (pos (nth 3 parent-target))
  9824. level)
  9825. (with-current-buffer (or (find-buffer-visiting file)
  9826. (find-file-noselect file))
  9827. (save-excursion
  9828. (save-restriction
  9829. (widen)
  9830. (if pos
  9831. (goto-char pos)
  9832. (goto-char (point-max))
  9833. (if (not (bolp)) (newline)))
  9834. (when (looking-at org-outline-regexp)
  9835. (setq level (funcall outline-level))
  9836. (org-end-of-subtree t t))
  9837. (org-back-over-empty-lines)
  9838. (insert "\n" (make-string
  9839. (if pos (org-get-valid-level level 1) 1) ?*)
  9840. " " child "\n")
  9841. (beginning-of-line 0)
  9842. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9843. (defun org-olpath-completing-read (prompt collection &rest args)
  9844. "Read an outline path like a file name."
  9845. (let ((thetable collection)
  9846. (org-completion-use-ido nil) ; does not work with ido.
  9847. (org-completion-use-iswitchb nil)) ; or iswitchb
  9848. (apply
  9849. 'org-icompleting-read prompt
  9850. (lambda (string predicate &optional flag)
  9851. (let (rtn r f (l (length string)))
  9852. (cond
  9853. ((eq flag nil)
  9854. ;; try completion
  9855. (try-completion string thetable))
  9856. ((eq flag t)
  9857. ;; all-completions
  9858. (setq rtn (all-completions string thetable predicate))
  9859. (mapcar
  9860. (lambda (x)
  9861. (setq r (substring x l))
  9862. (if (string-match " ([^)]*)$" x)
  9863. (setq f (match-string 0 x))
  9864. (setq f ""))
  9865. (if (string-match "/" r)
  9866. (concat string (substring r 0 (match-end 0)) f)
  9867. x))
  9868. rtn))
  9869. ((eq flag 'lambda)
  9870. ;; exact match?
  9871. (assoc string thetable)))
  9872. ))
  9873. args)))
  9874. ;;;; Dynamic blocks
  9875. (defun org-find-dblock (name)
  9876. "Find the first dynamic block with name NAME in the buffer.
  9877. If not found, stay at current position and return nil."
  9878. (let (pos)
  9879. (save-excursion
  9880. (goto-char (point-min))
  9881. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9882. nil t)
  9883. (match-beginning 0))))
  9884. (if pos (goto-char pos))
  9885. pos))
  9886. (defconst org-dblock-start-re
  9887. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9888. "Matches the start line of a dynamic block, with parameters.")
  9889. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9890. "Matches the end of a dynamic block.")
  9891. (defun org-create-dblock (plist)
  9892. "Create a dynamic block section, with parameters taken from PLIST.
  9893. PLIST must contain a :name entry which is used as name of the block."
  9894. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9895. (end-of-line 1)
  9896. (newline))
  9897. (let ((col (current-column))
  9898. (name (plist-get plist :name)))
  9899. (insert "#+BEGIN: " name)
  9900. (while plist
  9901. (if (eq (car plist) :name)
  9902. (setq plist (cddr plist))
  9903. (insert " " (prin1-to-string (pop plist)))))
  9904. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9905. (beginning-of-line -2)))
  9906. (defun org-prepare-dblock ()
  9907. "Prepare dynamic block for refresh.
  9908. This empties the block, puts the cursor at the insert position and returns
  9909. the property list including an extra property :name with the block name."
  9910. (unless (looking-at org-dblock-start-re)
  9911. (error "Not at a dynamic block"))
  9912. (let* ((begdel (1+ (match-end 0)))
  9913. (name (org-no-properties (match-string 1)))
  9914. (params (append (list :name name)
  9915. (read (concat "(" (match-string 3) ")")))))
  9916. (save-excursion
  9917. (beginning-of-line 1)
  9918. (skip-chars-forward " \t")
  9919. (setq params (plist-put params :indentation-column (current-column))))
  9920. (unless (re-search-forward org-dblock-end-re nil t)
  9921. (error "Dynamic block not terminated"))
  9922. (setq params
  9923. (append params
  9924. (list :content (buffer-substring
  9925. begdel (match-beginning 0)))))
  9926. (delete-region begdel (match-beginning 0))
  9927. (goto-char begdel)
  9928. (open-line 1)
  9929. params))
  9930. (defun org-map-dblocks (&optional command)
  9931. "Apply COMMAND to all dynamic blocks in the current buffer.
  9932. If COMMAND is not given, use `org-update-dblock'."
  9933. (let ((cmd (or command 'org-update-dblock)))
  9934. (save-excursion
  9935. (goto-char (point-min))
  9936. (while (re-search-forward org-dblock-start-re nil t)
  9937. (goto-char (match-beginning 0))
  9938. (save-excursion
  9939. (condition-case nil
  9940. (funcall cmd)
  9941. (error (message "Error during update of dynamic block"))))
  9942. (unless (re-search-forward org-dblock-end-re nil t)
  9943. (error "Dynamic block not terminated"))))))
  9944. (defun org-dblock-update (&optional arg)
  9945. "User command for updating dynamic blocks.
  9946. Update the dynamic block at point. With prefix ARG, update all dynamic
  9947. blocks in the buffer."
  9948. (interactive "P")
  9949. (if arg
  9950. (org-update-all-dblocks)
  9951. (or (looking-at org-dblock-start-re)
  9952. (org-beginning-of-dblock))
  9953. (org-update-dblock)))
  9954. (defun org-update-dblock ()
  9955. "Update the dynamic block at point.
  9956. This means to empty the block, parse for parameters and then call
  9957. the correct writing function."
  9958. (interactive)
  9959. (save-window-excursion
  9960. (let* ((pos (point))
  9961. (line (org-current-line))
  9962. (params (org-prepare-dblock))
  9963. (name (plist-get params :name))
  9964. (indent (plist-get params :indentation-column))
  9965. (cmd (intern (concat "org-dblock-write:" name))))
  9966. (message "Updating dynamic block `%s' at line %d..." name line)
  9967. (funcall cmd params)
  9968. (message "Updating dynamic block `%s' at line %d...done" name line)
  9969. (goto-char pos)
  9970. (when (and indent (> indent 0))
  9971. (setq indent (make-string indent ?\ ))
  9972. (save-excursion
  9973. (org-beginning-of-dblock)
  9974. (forward-line 1)
  9975. (while (not (looking-at org-dblock-end-re))
  9976. (insert indent)
  9977. (beginning-of-line 2))
  9978. (when (looking-at org-dblock-end-re)
  9979. (and (looking-at "[ \t]+")
  9980. (replace-match ""))
  9981. (insert indent)))))))
  9982. (defun org-beginning-of-dblock ()
  9983. "Find the beginning of the dynamic block at point.
  9984. Error if there is no such block at point."
  9985. (let ((pos (point))
  9986. beg)
  9987. (end-of-line 1)
  9988. (if (and (re-search-backward org-dblock-start-re nil t)
  9989. (setq beg (match-beginning 0))
  9990. (re-search-forward org-dblock-end-re nil t)
  9991. (> (match-end 0) pos))
  9992. (goto-char beg)
  9993. (goto-char pos)
  9994. (error "Not in a dynamic block"))))
  9995. (defun org-update-all-dblocks ()
  9996. "Update all dynamic blocks in the buffer.
  9997. This function can be used in a hook."
  9998. (interactive)
  9999. (when (eq major-mode 'org-mode)
  10000. (org-map-dblocks 'org-update-dblock)))
  10001. ;;;; Completion
  10002. (defconst org-additional-option-like-keywords
  10003. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10004. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10005. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10006. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10007. "BEGIN:" "END:"
  10008. "ORGTBL" "TBLFM:" "TBLNAME:"
  10009. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10010. "BEGIN_QUOTE" "END_QUOTE"
  10011. "BEGIN_VERSE" "END_VERSE"
  10012. "BEGIN_CENTER" "END_CENTER"
  10013. "BEGIN_SRC" "END_SRC"
  10014. "BEGIN_RESULT" "END_RESULT"
  10015. "NAME:" "RESULTS:"
  10016. "HEADER:" "HEADERS:"
  10017. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10018. "CAPTION:" "LABEL:"
  10019. "SETUPFILE:"
  10020. "INCLUDE:"
  10021. "BIND:"
  10022. "MACRO:"))
  10023. (defcustom org-structure-template-alist
  10024. '(
  10025. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10026. "<src lang=\"?\">\n\n</src>")
  10027. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10028. "<example>\n?\n</example>")
  10029. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10030. "<quote>\n?\n</quote>")
  10031. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10032. "<verse>\n?\n</verse>")
  10033. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10034. "<center>\n?\n</center>")
  10035. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10036. "<literal style=\"latex\">\n?\n</literal>")
  10037. ("L" "#+LaTeX: "
  10038. "<literal style=\"latex\">?</literal>")
  10039. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10040. "<literal style=\"html\">\n?\n</literal>")
  10041. ("H" "#+HTML: "
  10042. "<literal style=\"html\">?</literal>")
  10043. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10044. ("A" "#+ASCII: ")
  10045. ("i" "#+INDEX: ?"
  10046. "#+INDEX: ?")
  10047. ("I" "#+INCLUDE %file ?"
  10048. "<include file=%file markup=\"?\">")
  10049. )
  10050. "Structure completion elements.
  10051. This is a list of abbreviation keys and values. The value gets inserted
  10052. if you type `<' followed by the key and then press the completion key,
  10053. usually `M-TAB'. %file will be replaced by a file name after prompting
  10054. for the file using completion. The cursor will be placed at the position
  10055. of the `?` in the template.
  10056. There are two templates for each key, the first uses the original Org syntax,
  10057. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10058. the default when the /org-mtags.el/ module has been loaded. See also the
  10059. variable `org-mtags-prefer-muse-templates'."
  10060. :group 'org-completion
  10061. :type '(repeat
  10062. (string :tag "Key")
  10063. (string :tag "Template")
  10064. (string :tag "Muse Template")))
  10065. (defun org-try-structure-completion ()
  10066. "Try to complete a structure template before point.
  10067. This looks for strings like \"<e\" on an otherwise empty line and
  10068. expands them."
  10069. (let ((l (buffer-substring (point-at-bol) (point)))
  10070. a)
  10071. (when (and (looking-at "[ \t]*$")
  10072. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10073. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10074. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10075. (match-beginning 1)) a)
  10076. t)))
  10077. (defun org-complete-expand-structure-template (start cell)
  10078. "Expand a structure template."
  10079. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10080. (rpl (nth (if musep 2 1) cell))
  10081. (ind ""))
  10082. (delete-region start (point))
  10083. (when (string-match "\\`#\\+" rpl)
  10084. (cond
  10085. ((bolp))
  10086. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10087. (setq ind (buffer-substring (point-at-bol) (point))))
  10088. (t (newline))))
  10089. (setq start (point))
  10090. (if (string-match "%file" rpl)
  10091. (setq rpl (replace-match
  10092. (concat
  10093. "\""
  10094. (save-match-data
  10095. (abbreviate-file-name (read-file-name "Include file: ")))
  10096. "\"")
  10097. t t rpl)))
  10098. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10099. (concat "\n" ind)))
  10100. (insert rpl)
  10101. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10102. ;;;; TODO, DEADLINE, Comments
  10103. (defun org-toggle-comment ()
  10104. "Change the COMMENT state of an entry."
  10105. (interactive)
  10106. (save-excursion
  10107. (org-back-to-heading)
  10108. (let (case-fold-search)
  10109. (cond
  10110. ((looking-at (format org-heading-keyword-regexp-format
  10111. org-comment-string))
  10112. (goto-char (match-end 1))
  10113. (looking-at (concat " +" org-comment-string))
  10114. (replace-match "" t t)
  10115. (when (eolp) (insert " ")))
  10116. ((looking-at org-outline-regexp)
  10117. (goto-char (match-end 0))
  10118. (insert org-comment-string " "))))))
  10119. (defvar org-last-todo-state-is-todo nil
  10120. "This is non-nil when the last TODO state change led to a TODO state.
  10121. If the last change removed the TODO tag or switched to DONE, then
  10122. this is nil.")
  10123. (defvar org-setting-tags nil) ; dynamically skipped
  10124. (defvar org-todo-setup-filter-hook nil
  10125. "Hook for functions that pre-filter todo specs.
  10126. Each function takes a todo spec and returns either nil or the spec
  10127. transformed into canonical form." )
  10128. (defvar org-todo-get-default-hook nil
  10129. "Hook for functions that get a default item for todo.
  10130. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10131. nil or a string to be used for the todo mark." )
  10132. (defvar org-agenda-headline-snapshot-before-repeat)
  10133. (defun org-current-effective-time ()
  10134. "Return current time adjusted for `org-extend-today-until' variable"
  10135. (let* ((ct (org-current-time))
  10136. (dct (decode-time ct))
  10137. (ct1
  10138. (if (and org-use-effective-time
  10139. (< (nth 2 dct) org-extend-today-until))
  10140. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10141. ct)))
  10142. ct1))
  10143. (defun org-todo-yesterday (&optional arg)
  10144. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10145. (interactive "P")
  10146. (if (eq major-mode 'org-agenda-mode)
  10147. (apply 'org-agenda-todo-yesterday arg)
  10148. (let* ((hour (third (decode-time
  10149. (org-current-time))))
  10150. (org-extend-today-until (1+ hour)))
  10151. (org-todo arg))))
  10152. (defun org-todo (&optional arg)
  10153. "Change the TODO state of an item.
  10154. The state of an item is given by a keyword at the start of the heading,
  10155. like
  10156. *** TODO Write paper
  10157. *** DONE Call mom
  10158. The different keywords are specified in the variable `org-todo-keywords'.
  10159. By default the available states are \"TODO\" and \"DONE\".
  10160. So for this example: when the item starts with TODO, it is changed to DONE.
  10161. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10162. DONE are present, add TODO at the beginning of the heading.
  10163. With \\[universal-argument] prefix arg, use completion to determine the new \
  10164. state.
  10165. With numeric prefix arg, switch to that state.
  10166. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10167. keywords (nextset).
  10168. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10169. With a numeric prefix arg of 0, inhibit note taking for the change.
  10170. For calling through lisp, arg is also interpreted in the following way:
  10171. 'none -> empty state
  10172. \"\"(empty string) -> switch to empty state
  10173. 'done -> switch to DONE
  10174. 'nextset -> switch to the next set of keywords
  10175. 'previousset -> switch to the previous set of keywords
  10176. \"WAITING\" -> switch to the specified keyword, but only if it
  10177. really is a member of `org-todo-keywords'."
  10178. (interactive "P")
  10179. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10180. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10181. 'region-start-level 'region))
  10182. org-loop-over-headlines-in-active-region)
  10183. (org-map-entries
  10184. `(org-todo ,arg)
  10185. org-loop-over-headlines-in-active-region
  10186. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10187. (if (equal arg '(16)) (setq arg 'nextset))
  10188. (let ((org-blocker-hook org-blocker-hook)
  10189. (case-fold-search nil))
  10190. (when (equal arg '(64))
  10191. (setq arg nil org-blocker-hook nil))
  10192. (when (and org-blocker-hook
  10193. (or org-inhibit-blocking
  10194. (org-entry-get nil "NOBLOCKING")))
  10195. (setq org-blocker-hook nil))
  10196. (save-excursion
  10197. (catch 'exit
  10198. (org-back-to-heading t)
  10199. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10200. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10201. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10202. (let* ((match-data (match-data))
  10203. (startpos (point-at-bol))
  10204. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10205. (org-log-done org-log-done)
  10206. (org-log-repeat org-log-repeat)
  10207. (org-todo-log-states org-todo-log-states)
  10208. (org-inhibit-logging
  10209. (if (equal arg 0)
  10210. (progn (setq arg nil) 'note) org-inhibit-logging))
  10211. (this (match-string 1))
  10212. (hl-pos (match-beginning 0))
  10213. (head (org-get-todo-sequence-head this))
  10214. (ass (assoc head org-todo-kwd-alist))
  10215. (interpret (nth 1 ass))
  10216. (done-word (nth 3 ass))
  10217. (final-done-word (nth 4 ass))
  10218. (last-state (or this ""))
  10219. (completion-ignore-case t)
  10220. (member (member this org-todo-keywords-1))
  10221. (tail (cdr member))
  10222. (state (cond
  10223. ((and org-todo-key-trigger
  10224. (or (and (equal arg '(4))
  10225. (eq org-use-fast-todo-selection 'prefix))
  10226. (and (not arg) org-use-fast-todo-selection
  10227. (not (eq org-use-fast-todo-selection
  10228. 'prefix)))))
  10229. ;; Use fast selection
  10230. (org-fast-todo-selection))
  10231. ((and (equal arg '(4))
  10232. (or (not org-use-fast-todo-selection)
  10233. (not org-todo-key-trigger)))
  10234. ;; Read a state with completion
  10235. (org-icompleting-read
  10236. "State: " (mapcar (lambda(x) (list x))
  10237. org-todo-keywords-1)
  10238. nil t))
  10239. ((eq arg 'right)
  10240. (if this
  10241. (if tail (car tail) nil)
  10242. (car org-todo-keywords-1)))
  10243. ((eq arg 'left)
  10244. (if (equal member org-todo-keywords-1)
  10245. nil
  10246. (if this
  10247. (nth (- (length org-todo-keywords-1)
  10248. (length tail) 2)
  10249. org-todo-keywords-1)
  10250. (org-last org-todo-keywords-1))))
  10251. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10252. (setq arg nil))) ; hack to fall back to cycling
  10253. (arg
  10254. ;; user or caller requests a specific state
  10255. (cond
  10256. ((equal arg "") nil)
  10257. ((eq arg 'none) nil)
  10258. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10259. ((eq arg 'nextset)
  10260. (or (car (cdr (member head org-todo-heads)))
  10261. (car org-todo-heads)))
  10262. ((eq arg 'previousset)
  10263. (let ((org-todo-heads (reverse org-todo-heads)))
  10264. (or (car (cdr (member head org-todo-heads)))
  10265. (car org-todo-heads))))
  10266. ((car (member arg org-todo-keywords-1)))
  10267. ((stringp arg)
  10268. (error "State `%s' not valid in this file" arg))
  10269. ((nth (1- (prefix-numeric-value arg))
  10270. org-todo-keywords-1))))
  10271. ((null member) (or head (car org-todo-keywords-1)))
  10272. ((equal this final-done-word) nil) ;; -> make empty
  10273. ((null tail) nil) ;; -> first entry
  10274. ((memq interpret '(type priority))
  10275. (if (eq this-command last-command)
  10276. (car tail)
  10277. (if (> (length tail) 0)
  10278. (or done-word (car org-done-keywords))
  10279. nil)))
  10280. (t
  10281. (car tail))))
  10282. (state (or
  10283. (run-hook-with-args-until-success
  10284. 'org-todo-get-default-hook state last-state)
  10285. state))
  10286. (next (if state (concat " " state " ") " "))
  10287. (change-plist (list :type 'todo-state-change :from this :to state
  10288. :position startpos))
  10289. dolog now-done-p)
  10290. (when org-blocker-hook
  10291. (setq org-last-todo-state-is-todo
  10292. (not (member this org-done-keywords)))
  10293. (unless (save-excursion
  10294. (save-match-data
  10295. (org-with-wide-buffer
  10296. (run-hook-with-args-until-failure
  10297. 'org-blocker-hook change-plist))))
  10298. (if (org-called-interactively-p 'interactive)
  10299. (error "TODO state change from %s to %s blocked" this state)
  10300. ;; fail silently
  10301. (message "TODO state change from %s to %s blocked" this state)
  10302. (throw 'exit nil))))
  10303. (store-match-data match-data)
  10304. (replace-match next t t)
  10305. (unless (pos-visible-in-window-p hl-pos)
  10306. (message "TODO state changed to %s" (org-trim next)))
  10307. (unless head
  10308. (setq head (org-get-todo-sequence-head state)
  10309. ass (assoc head org-todo-kwd-alist)
  10310. interpret (nth 1 ass)
  10311. done-word (nth 3 ass)
  10312. final-done-word (nth 4 ass)))
  10313. (when (memq arg '(nextset previousset))
  10314. (message "Keyword-Set %d/%d: %s"
  10315. (- (length org-todo-sets) -1
  10316. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10317. (length org-todo-sets)
  10318. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10319. (setq org-last-todo-state-is-todo
  10320. (not (member state org-done-keywords)))
  10321. (setq now-done-p (and (member state org-done-keywords)
  10322. (not (member this org-done-keywords))))
  10323. (and logging (org-local-logging logging))
  10324. (when (and (or org-todo-log-states org-log-done)
  10325. (not (eq org-inhibit-logging t))
  10326. (not (memq arg '(nextset previousset))))
  10327. ;; we need to look at recording a time and note
  10328. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10329. (nth 2 (assoc this org-todo-log-states))))
  10330. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10331. (setq dolog 'time))
  10332. (when (and state
  10333. (member state org-not-done-keywords)
  10334. (not (member this org-not-done-keywords)))
  10335. ;; This is now a todo state and was not one before
  10336. ;; If there was a CLOSED time stamp, get rid of it.
  10337. (org-add-planning-info nil nil 'closed))
  10338. (when (and now-done-p org-log-done)
  10339. ;; It is now done, and it was not done before
  10340. (org-add-planning-info 'closed (org-current-effective-time))
  10341. (if (and (not dolog) (eq 'note org-log-done))
  10342. (org-add-log-setup 'done state this 'findpos 'note)))
  10343. (when (and state dolog)
  10344. ;; This is a non-nil state, and we need to log it
  10345. (org-add-log-setup 'state state this 'findpos dolog)))
  10346. ;; Fixup tag positioning
  10347. (org-todo-trigger-tag-changes state)
  10348. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10349. (when org-provide-todo-statistics
  10350. (org-update-parent-todo-statistics))
  10351. (run-hooks 'org-after-todo-state-change-hook)
  10352. (if (and arg (not (member state org-done-keywords)))
  10353. (setq head (org-get-todo-sequence-head state)))
  10354. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10355. ;; Do we need to trigger a repeat?
  10356. (when now-done-p
  10357. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10358. ;; This is for the agenda, take a snapshot of the headline.
  10359. (save-match-data
  10360. (setq org-agenda-headline-snapshot-before-repeat
  10361. (org-get-heading))))
  10362. (org-auto-repeat-maybe state))
  10363. ;; Fixup cursor location if close to the keyword
  10364. (if (and (outline-on-heading-p)
  10365. (not (bolp))
  10366. (save-excursion (beginning-of-line 1)
  10367. (looking-at org-todo-line-regexp))
  10368. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10369. (progn
  10370. (goto-char (or (match-end 2) (match-end 1)))
  10371. (and (looking-at " ") (just-one-space))))
  10372. (when org-trigger-hook
  10373. (save-excursion
  10374. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10375. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10376. "Block turning an entry into a TODO, using the hierarchy.
  10377. This checks whether the current task should be blocked from state
  10378. changes. Such blocking occurs when:
  10379. 1. The task has children which are not all in a completed state.
  10380. 2. A task has a parent with the property :ORDERED:, and there
  10381. are siblings prior to the current task with incomplete
  10382. status.
  10383. 3. The parent of the task is blocked because it has siblings that should
  10384. be done first, or is child of a block grandparent TODO entry."
  10385. (if (not org-enforce-todo-dependencies)
  10386. t ; if locally turned off don't block
  10387. (catch 'dont-block
  10388. ;; If this is not a todo state change, or if this entry is already DONE,
  10389. ;; do not block
  10390. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10391. (member (plist-get change-plist :from)
  10392. (cons 'done org-done-keywords))
  10393. (member (plist-get change-plist :to)
  10394. (cons 'todo org-not-done-keywords))
  10395. (not (plist-get change-plist :to)))
  10396. (throw 'dont-block t))
  10397. ;; If this task has children, and any are undone, it's blocked
  10398. (save-excursion
  10399. (org-back-to-heading t)
  10400. (let ((this-level (funcall outline-level)))
  10401. (outline-next-heading)
  10402. (let ((child-level (funcall outline-level)))
  10403. (while (and (not (eobp))
  10404. (> child-level this-level))
  10405. ;; this todo has children, check whether they are all
  10406. ;; completed
  10407. (if (and (not (org-entry-is-done-p))
  10408. (org-entry-is-todo-p))
  10409. (throw 'dont-block nil))
  10410. (outline-next-heading)
  10411. (setq child-level (funcall outline-level))))))
  10412. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10413. ;; any previous siblings are undone, it's blocked
  10414. (save-excursion
  10415. (org-back-to-heading t)
  10416. (let* ((pos (point))
  10417. (parent-pos (and (org-up-heading-safe) (point))))
  10418. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10419. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10420. (forward-line 1)
  10421. (re-search-forward org-not-done-heading-regexp pos t))
  10422. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10423. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10424. (while t
  10425. (goto-char parent-pos)
  10426. (if (not (looking-at org-not-done-heading-regexp))
  10427. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10428. (setq pos (point))
  10429. (setq parent-pos (and (org-up-heading-safe) (point)))
  10430. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10431. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10432. (forward-line 1)
  10433. (re-search-forward org-not-done-heading-regexp pos t))
  10434. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10435. (defcustom org-track-ordered-property-with-tag nil
  10436. "Should the ORDERED property also be shown as a tag?
  10437. The ORDERED property decides if an entry should require subtasks to be
  10438. completed in sequence. Since a property is not very visible, setting
  10439. this option means that toggling the ORDERED property with the command
  10440. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10441. not relevant for the behavior, but it makes things more visible.
  10442. Note that toggling the tag with tags commands will not change the property
  10443. and therefore not influence behavior!
  10444. This can be t, meaning the tag ORDERED should be used, It can also be a
  10445. string to select a different tag for this task."
  10446. :group 'org-todo
  10447. :type '(choice
  10448. (const :tag "No tracking" nil)
  10449. (const :tag "Track with ORDERED tag" t)
  10450. (string :tag "Use other tag")))
  10451. (defun org-toggle-ordered-property ()
  10452. "Toggle the ORDERED property of the current entry.
  10453. For better visibility, you can track the value of this property with a tag.
  10454. See variable `org-track-ordered-property-with-tag'."
  10455. (interactive)
  10456. (let* ((t1 org-track-ordered-property-with-tag)
  10457. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10458. (save-excursion
  10459. (org-back-to-heading)
  10460. (if (org-entry-get nil "ORDERED")
  10461. (progn
  10462. (org-delete-property "ORDERED")
  10463. (and tag (org-toggle-tag tag 'off))
  10464. (message "Subtasks can be completed in arbitrary order"))
  10465. (org-entry-put nil "ORDERED" "t")
  10466. (and tag (org-toggle-tag tag 'on))
  10467. (message "Subtasks must be completed in sequence")))))
  10468. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10469. (defun org-block-todo-from-checkboxes (change-plist)
  10470. "Block turning an entry into a TODO, using checkboxes.
  10471. This checks whether the current task should be blocked from state
  10472. changes because there are unchecked boxes in this entry."
  10473. (if (not org-enforce-todo-checkbox-dependencies)
  10474. t ; if locally turned off don't block
  10475. (catch 'dont-block
  10476. ;; If this is not a todo state change, or if this entry is already DONE,
  10477. ;; do not block
  10478. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10479. (member (plist-get change-plist :from)
  10480. (cons 'done org-done-keywords))
  10481. (member (plist-get change-plist :to)
  10482. (cons 'todo org-not-done-keywords))
  10483. (not (plist-get change-plist :to)))
  10484. (throw 'dont-block t))
  10485. ;; If this task has checkboxes that are not checked, it's blocked
  10486. (save-excursion
  10487. (org-back-to-heading t)
  10488. (let ((beg (point)) end)
  10489. (outline-next-heading)
  10490. (setq end (point))
  10491. (goto-char beg)
  10492. (if (org-list-search-forward
  10493. (concat (org-item-beginning-re)
  10494. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10495. "\\[[- ]\\]")
  10496. end t)
  10497. (progn
  10498. (if (boundp 'org-blocked-by-checkboxes)
  10499. (setq org-blocked-by-checkboxes t))
  10500. (throw 'dont-block nil)))))
  10501. t))) ; do not block
  10502. (defun org-entry-blocked-p ()
  10503. "Is the current entry blocked?"
  10504. (if (org-entry-get nil "NOBLOCKING")
  10505. nil ;; Never block this entry
  10506. (not
  10507. (run-hook-with-args-until-failure
  10508. 'org-blocker-hook
  10509. (list :type 'todo-state-change
  10510. :position (point)
  10511. :from 'todo
  10512. :to 'done)))))
  10513. (defun org-update-statistics-cookies (all)
  10514. "Update the statistics cookie, either from TODO or from checkboxes.
  10515. This should be called with the cursor in a line with a statistics cookie."
  10516. (interactive "P")
  10517. (if all
  10518. (progn
  10519. (org-update-checkbox-count 'all)
  10520. (org-map-entries 'org-update-parent-todo-statistics))
  10521. (if (not (org-at-heading-p))
  10522. (org-update-checkbox-count)
  10523. (let ((pos (move-marker (make-marker) (point)))
  10524. end l1 l2)
  10525. (ignore-errors (org-back-to-heading t))
  10526. (if (not (org-at-heading-p))
  10527. (org-update-checkbox-count)
  10528. (setq l1 (org-outline-level))
  10529. (setq end (save-excursion
  10530. (outline-next-heading)
  10531. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10532. (point)))
  10533. (if (and (save-excursion
  10534. (re-search-forward
  10535. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10536. (not (save-excursion (re-search-forward
  10537. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10538. (org-update-checkbox-count)
  10539. (if (and l2 (> l2 l1))
  10540. (progn
  10541. (goto-char end)
  10542. (org-update-parent-todo-statistics))
  10543. (goto-char pos)
  10544. (beginning-of-line 1)
  10545. (while (re-search-forward
  10546. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10547. (point-at-eol) t)
  10548. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10549. (goto-char pos)
  10550. (move-marker pos nil)))))
  10551. (defvar org-entry-property-inherited-from) ;; defined below
  10552. (defun org-update-parent-todo-statistics ()
  10553. "Update any statistics cookie in the parent of the current headline.
  10554. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10555. the entire subtree and this will travel up the hierarchy and update
  10556. statistics everywhere."
  10557. (let* ((prop (save-excursion (org-up-heading-safe)
  10558. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10559. (recursive (or (not org-hierarchical-todo-statistics)
  10560. (and prop (string-match "\\<recursive\\>" prop))))
  10561. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10562. 0))
  10563. (first t)
  10564. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10565. level ltoggle l1 new ndel
  10566. (cnt-all 0) (cnt-done 0) is-percent kwd
  10567. checkbox-beg ov ovs ove cookie-present)
  10568. (catch 'exit
  10569. (save-excursion
  10570. (beginning-of-line 1)
  10571. (setq ltoggle (funcall outline-level))
  10572. ;; Three situations are to consider:
  10573. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10574. ;; to the top-level ancestor on the headline;
  10575. ;; 2. If parent has "recursive" property, repeat up to the
  10576. ;; headline setting that property, taking inheritance into
  10577. ;; account;
  10578. ;; 3. Else, move up to direct parent and proceed only once.
  10579. (while (and (setq level (org-up-heading-safe))
  10580. (or recursive first)
  10581. (>= (point) lim))
  10582. (setq first nil cookie-present nil)
  10583. (unless (and level
  10584. (not (string-match
  10585. "\\<checkbox\\>"
  10586. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10587. "")))))
  10588. (throw 'exit nil))
  10589. (while (re-search-forward box-re (point-at-eol) t)
  10590. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10591. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10592. (save-match-data
  10593. (unless (outline-next-heading) (throw 'exit nil))
  10594. (while (and (looking-at org-complex-heading-regexp)
  10595. (> (setq l1 (length (match-string 1))) level))
  10596. (setq kwd (and (or recursive (= l1 ltoggle))
  10597. (match-string 2)))
  10598. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10599. (and (listp org-provide-todo-statistics)
  10600. (or (member kwd org-provide-todo-statistics)
  10601. (member kwd org-done-keywords))))
  10602. (setq cnt-all (1+ cnt-all))
  10603. (if (eq org-provide-todo-statistics t)
  10604. (and kwd (setq cnt-all (1+ cnt-all)))))
  10605. (and (member kwd org-done-keywords)
  10606. (setq cnt-done (1+ cnt-done)))
  10607. (outline-next-heading)))
  10608. (setq new
  10609. (if is-percent
  10610. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10611. (format "[%d/%d]" cnt-done cnt-all))
  10612. ndel (- (match-end 0) checkbox-beg))
  10613. ;; handle overlays when updating cookie from column view
  10614. (when (setq ov (car (overlays-at checkbox-beg)))
  10615. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10616. (delete-overlay ov))
  10617. (goto-char checkbox-beg)
  10618. (insert new)
  10619. (delete-region (point) (+ (point) ndel))
  10620. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10621. (when ov (move-overlay ov ovs ove)))
  10622. (when cookie-present
  10623. (run-hook-with-args 'org-after-todo-statistics-hook
  10624. cnt-done (- cnt-all cnt-done))))))
  10625. (run-hooks 'org-todo-statistics-hook)))
  10626. (defvar org-after-todo-statistics-hook nil
  10627. "Hook that is called after a TODO statistics cookie has been updated.
  10628. Each function is called with two arguments: the number of not-done entries
  10629. and the number of done entries.
  10630. For example, the following function, when added to this hook, will switch
  10631. an entry to DONE when all children are done, and back to TODO when new
  10632. entries are set to a TODO status. Note that this hook is only called
  10633. when there is a statistics cookie in the headline!
  10634. (defun org-summary-todo (n-done n-not-done)
  10635. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10636. (let (org-log-done org-log-states) ; turn off logging
  10637. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10638. ")
  10639. (defvar org-todo-statistics-hook nil
  10640. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10641. This hook runs even if there is no statistics cookie present, in which case
  10642. `org-after-todo-statistics-hook' would not run.")
  10643. (defun org-todo-trigger-tag-changes (state)
  10644. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10645. (let ((l org-todo-state-tags-triggers)
  10646. changes)
  10647. (when (or (not state) (equal state ""))
  10648. (setq changes (append changes (cdr (assoc "" l)))))
  10649. (when (and (stringp state) (> (length state) 0))
  10650. (setq changes (append changes (cdr (assoc state l)))))
  10651. (when (member state org-not-done-keywords)
  10652. (setq changes (append changes (cdr (assoc 'todo l)))))
  10653. (when (member state org-done-keywords)
  10654. (setq changes (append changes (cdr (assoc 'done l)))))
  10655. (dolist (c changes)
  10656. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10657. (defun org-local-logging (value)
  10658. "Get logging settings from a property VALUE."
  10659. (let* (words w a)
  10660. ;; directly set the variables, they are already local.
  10661. (setq org-log-done nil
  10662. org-log-repeat nil
  10663. org-todo-log-states nil)
  10664. (setq words (org-split-string value))
  10665. (while (setq w (pop words))
  10666. (cond
  10667. ((setq a (assoc w org-startup-options))
  10668. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10669. (set (nth 1 a) (nth 2 a))))
  10670. ((setq a (org-extract-log-state-settings w))
  10671. (and (member (car a) org-todo-keywords-1)
  10672. (push a org-todo-log-states)))))))
  10673. (defun org-get-todo-sequence-head (kwd)
  10674. "Return the head of the TODO sequence to which KWD belongs.
  10675. If KWD is not set, check if there is a text property remembering the
  10676. right sequence."
  10677. (let (p)
  10678. (cond
  10679. ((not kwd)
  10680. (or (get-text-property (point-at-bol) 'org-todo-head)
  10681. (progn
  10682. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10683. nil (point-at-eol)))
  10684. (get-text-property p 'org-todo-head))))
  10685. ((not (member kwd org-todo-keywords-1))
  10686. (car org-todo-keywords-1))
  10687. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10688. (defun org-fast-todo-selection ()
  10689. "Fast TODO keyword selection with single keys.
  10690. Returns the new TODO keyword, or nil if no state change should occur."
  10691. (let* ((fulltable org-todo-key-alist)
  10692. (done-keywords org-done-keywords) ;; needed for the faces.
  10693. (maxlen (apply 'max (mapcar
  10694. (lambda (x)
  10695. (if (stringp (car x)) (string-width (car x)) 0))
  10696. fulltable)))
  10697. (expert nil)
  10698. (fwidth (+ maxlen 3 1 3))
  10699. (ncol (/ (- (window-width) 4) fwidth))
  10700. tg cnt e c tbl
  10701. groups ingroup)
  10702. (save-excursion
  10703. (save-window-excursion
  10704. (if expert
  10705. (set-buffer (get-buffer-create " *Org todo*"))
  10706. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10707. (erase-buffer)
  10708. (org-set-local 'org-done-keywords done-keywords)
  10709. (setq tbl fulltable cnt 0)
  10710. (while (setq e (pop tbl))
  10711. (cond
  10712. ((equal e '(:startgroup))
  10713. (push '() groups) (setq ingroup t)
  10714. (when (not (= cnt 0))
  10715. (setq cnt 0)
  10716. (insert "\n"))
  10717. (insert "{ "))
  10718. ((equal e '(:endgroup))
  10719. (setq ingroup nil cnt 0)
  10720. (insert "}\n"))
  10721. ((equal e '(:newline))
  10722. (when (not (= cnt 0))
  10723. (setq cnt 0)
  10724. (insert "\n")
  10725. (setq e (car tbl))
  10726. (while (equal (car tbl) '(:newline))
  10727. (insert "\n")
  10728. (setq tbl (cdr tbl)))))
  10729. (t
  10730. (setq tg (car e) c (cdr e))
  10731. (if ingroup (push tg (car groups)))
  10732. (setq tg (org-add-props tg nil 'face
  10733. (org-get-todo-face tg)))
  10734. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10735. (insert "[" c "] " tg (make-string
  10736. (- fwidth 4 (length tg)) ?\ ))
  10737. (when (= (setq cnt (1+ cnt)) ncol)
  10738. (insert "\n")
  10739. (if ingroup (insert " "))
  10740. (setq cnt 0)))))
  10741. (insert "\n")
  10742. (goto-char (point-min))
  10743. (if (not expert) (org-fit-window-to-buffer))
  10744. (message "[a-z..]:Set [SPC]:clear")
  10745. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10746. (cond
  10747. ((or (= c ?\C-g)
  10748. (and (= c ?q) (not (rassoc c fulltable))))
  10749. (setq quit-flag t))
  10750. ((= c ?\ ) nil)
  10751. ((setq e (rassoc c fulltable) tg (car e))
  10752. tg)
  10753. (t (setq quit-flag t)))))))
  10754. (defun org-entry-is-todo-p ()
  10755. (member (org-get-todo-state) org-not-done-keywords))
  10756. (defun org-entry-is-done-p ()
  10757. (member (org-get-todo-state) org-done-keywords))
  10758. (defun org-get-todo-state ()
  10759. (save-excursion
  10760. (org-back-to-heading t)
  10761. (and (looking-at org-todo-line-regexp)
  10762. (match-end 2)
  10763. (match-string 2))))
  10764. (defun org-at-date-range-p (&optional inactive-ok)
  10765. "Is the cursor inside a date range?"
  10766. (interactive)
  10767. (save-excursion
  10768. (catch 'exit
  10769. (let ((pos (point)))
  10770. (skip-chars-backward "^[<\r\n")
  10771. (skip-chars-backward "<[")
  10772. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10773. (>= (match-end 0) pos)
  10774. (throw 'exit t))
  10775. (skip-chars-backward "^<[\r\n")
  10776. (skip-chars-backward "<[")
  10777. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10778. (>= (match-end 0) pos)
  10779. (throw 'exit t)))
  10780. nil)))
  10781. (defun org-get-repeat (&optional tagline)
  10782. "Check if there is a deadline/schedule with repeater in this entry."
  10783. (save-match-data
  10784. (save-excursion
  10785. (org-back-to-heading t)
  10786. (and (re-search-forward (if tagline
  10787. (concat tagline "\\s-*" org-repeat-re)
  10788. org-repeat-re)
  10789. (org-entry-end-position) t)
  10790. (match-string-no-properties 1)))))
  10791. (defvar org-last-changed-timestamp)
  10792. (defvar org-last-inserted-timestamp)
  10793. (defvar org-log-post-message)
  10794. (defvar org-log-note-purpose)
  10795. (defvar org-log-note-how)
  10796. (defvar org-log-note-extra)
  10797. (defun org-auto-repeat-maybe (done-word)
  10798. "Check if the current headline contains a repeated deadline/schedule.
  10799. If yes, set TODO state back to what it was and change the base date
  10800. of repeating deadline/scheduled time stamps to new date.
  10801. This function is run automatically after each state change to a DONE state."
  10802. ;; last-state is dynamically scoped into this function
  10803. (let* ((repeat (org-get-repeat))
  10804. (aa (assoc last-state org-todo-kwd-alist))
  10805. (interpret (nth 1 aa))
  10806. (head (nth 2 aa))
  10807. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10808. (msg "Entry repeats: ")
  10809. (org-log-done nil)
  10810. (org-todo-log-states nil)
  10811. re type n what ts time to-state)
  10812. (when repeat
  10813. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10814. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10815. org-todo-repeat-to-state))
  10816. (unless (and to-state (member to-state org-todo-keywords-1))
  10817. (setq to-state (if (eq interpret 'type) last-state head)))
  10818. (org-todo to-state)
  10819. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10820. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10821. (org-time-stamp-format t t))))
  10822. (when org-log-repeat
  10823. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10824. (memq 'org-add-log-note post-command-hook))
  10825. ;; OK, we are already setup for some record
  10826. (if (eq org-log-repeat 'note)
  10827. ;; make sure we take a note, not only a time stamp
  10828. (setq org-log-note-how 'note))
  10829. ;; Set up for taking a record
  10830. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10831. last-state
  10832. 'findpos org-log-repeat)))
  10833. (org-back-to-heading t)
  10834. (org-add-planning-info nil nil 'closed)
  10835. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10836. org-deadline-time-regexp "\\)\\|\\("
  10837. org-ts-regexp "\\)"))
  10838. (while (re-search-forward
  10839. re (save-excursion (outline-next-heading) (point)) t)
  10840. (setq type (if (match-end 1) org-scheduled-string
  10841. (if (match-end 3) org-deadline-string "Plain:"))
  10842. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10843. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10844. (setq n (string-to-number (match-string 2 ts))
  10845. what (match-string 3 ts))
  10846. (if (equal what "w") (setq n (* n 7) what "d"))
  10847. ;; Preparation, see if we need to modify the start date for the change
  10848. (when (match-end 1)
  10849. (setq time (save-match-data (org-time-string-to-time ts)))
  10850. (cond
  10851. ((equal (match-string 1 ts) ".")
  10852. ;; Shift starting date to today
  10853. (org-timestamp-change
  10854. (- (org-today) (time-to-days time))
  10855. 'day))
  10856. ((equal (match-string 1 ts) "+")
  10857. (let ((nshiftmax 10) (nshift 0))
  10858. (while (or (= nshift 0)
  10859. (<= (time-to-days time)
  10860. (time-to-days (current-time))))
  10861. (when (= (incf nshift) nshiftmax)
  10862. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10863. (error "Abort")))
  10864. (org-timestamp-change n (cdr (assoc what whata)))
  10865. (org-at-timestamp-p t)
  10866. (setq ts (match-string 1))
  10867. (setq time (save-match-data (org-time-string-to-time ts)))))
  10868. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10869. ;; rematch, so that we have everything in place for the real shift
  10870. (org-at-timestamp-p t)
  10871. (setq ts (match-string 1))
  10872. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10873. (org-timestamp-change n (cdr (assoc what whata)))
  10874. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10875. (setq org-log-post-message msg)
  10876. (message "%s" msg))))
  10877. (defun org-show-todo-tree (arg)
  10878. "Make a compact tree which shows all headlines marked with TODO.
  10879. The tree will show the lines where the regexp matches, and all higher
  10880. headlines above the match.
  10881. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10882. With a numeric prefix N, construct a sparse tree for the Nth element
  10883. of `org-todo-keywords-1'."
  10884. (interactive "P")
  10885. (let ((case-fold-search nil)
  10886. (kwd-re
  10887. (cond ((null arg) org-not-done-regexp)
  10888. ((equal arg '(4))
  10889. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10890. (mapcar 'list org-todo-keywords-1))))
  10891. (concat "\\("
  10892. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10893. "\\)\\>")))
  10894. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10895. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10896. org-todo-keywords-1)))
  10897. (t (error "Invalid prefix argument: %s" arg)))))
  10898. (message "%d TODO entries found"
  10899. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10900. (defun org-deadline (&optional remove time)
  10901. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10902. With argument REMOVE, remove any deadline from the item.
  10903. With argument TIME, set the deadline at the corresponding date. TIME
  10904. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10905. (interactive "P")
  10906. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10907. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10908. 'region-start-level 'region))
  10909. org-loop-over-headlines-in-active-region)
  10910. (org-map-entries
  10911. `(org-deadline ',remove ,time)
  10912. org-loop-over-headlines-in-active-region
  10913. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10914. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10915. (repeater (and old-date
  10916. (string-match
  10917. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10918. old-date)
  10919. (match-string 1 old-date))))
  10920. (if remove
  10921. (progn
  10922. (when (and old-date org-log-redeadline)
  10923. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10924. org-log-redeadline))
  10925. (org-remove-timestamp-with-keyword org-deadline-string)
  10926. (message "Item no longer has a deadline."))
  10927. (org-add-planning-info 'deadline time 'closed)
  10928. (when (and old-date org-log-redeadline
  10929. (not (equal old-date
  10930. (substring org-last-inserted-timestamp 1 -1))))
  10931. (org-add-log-setup 'redeadline nil old-date 'findpos
  10932. org-log-redeadline))
  10933. (when repeater
  10934. (save-excursion
  10935. (org-back-to-heading t)
  10936. (when (re-search-forward (concat org-deadline-string " "
  10937. org-last-inserted-timestamp)
  10938. (save-excursion
  10939. (outline-next-heading) (point)) t)
  10940. (goto-char (1- (match-end 0)))
  10941. (insert " " repeater)
  10942. (setq org-last-inserted-timestamp
  10943. (concat (substring org-last-inserted-timestamp 0 -1)
  10944. " " repeater
  10945. (substring org-last-inserted-timestamp -1))))))
  10946. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10947. (defun org-schedule (&optional remove time)
  10948. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10949. With argument REMOVE, remove any scheduling date from the item.
  10950. With argument TIME, scheduled at the corresponding date. TIME can
  10951. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10952. (interactive "P")
  10953. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10954. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10955. 'region-start-level 'region))
  10956. org-loop-over-headlines-in-active-region)
  10957. (org-map-entries
  10958. `(org-schedule ',remove ,time)
  10959. org-loop-over-headlines-in-active-region
  10960. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10961. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10962. (repeater (and old-date
  10963. (string-match
  10964. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10965. old-date)
  10966. (match-string 1 old-date))))
  10967. (if remove
  10968. (progn
  10969. (when (and old-date org-log-reschedule)
  10970. (org-add-log-setup 'delschedule nil old-date 'findpos
  10971. org-log-reschedule))
  10972. (org-remove-timestamp-with-keyword org-scheduled-string)
  10973. (message "Item is no longer scheduled."))
  10974. (org-add-planning-info 'scheduled time 'closed)
  10975. (when (and old-date org-log-reschedule
  10976. (not (equal old-date
  10977. (substring org-last-inserted-timestamp 1 -1))))
  10978. (org-add-log-setup 'reschedule nil old-date 'findpos
  10979. org-log-reschedule))
  10980. (when repeater
  10981. (save-excursion
  10982. (org-back-to-heading t)
  10983. (when (re-search-forward (concat org-scheduled-string " "
  10984. org-last-inserted-timestamp)
  10985. (save-excursion
  10986. (outline-next-heading) (point)) t)
  10987. (goto-char (1- (match-end 0)))
  10988. (insert " " repeater)
  10989. (setq org-last-inserted-timestamp
  10990. (concat (substring org-last-inserted-timestamp 0 -1)
  10991. " " repeater
  10992. (substring org-last-inserted-timestamp -1))))))
  10993. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10994. (defun org-get-scheduled-time (pom &optional inherit)
  10995. "Get the scheduled time as a time tuple, of a format suitable
  10996. for calling org-schedule with, or if there is no scheduling,
  10997. returns nil."
  10998. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10999. (when time
  11000. (apply 'encode-time (org-parse-time-string time)))))
  11001. (defun org-get-deadline-time (pom &optional inherit)
  11002. "Get the deadline as a time tuple, of a format suitable for
  11003. calling org-deadline with, or if there is no scheduling, returns
  11004. nil."
  11005. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11006. (when time
  11007. (apply 'encode-time (org-parse-time-string time)))))
  11008. (defun org-remove-timestamp-with-keyword (keyword)
  11009. "Remove all time stamps with KEYWORD in the current entry."
  11010. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11011. beg)
  11012. (save-excursion
  11013. (org-back-to-heading t)
  11014. (setq beg (point))
  11015. (outline-next-heading)
  11016. (while (re-search-backward re beg t)
  11017. (replace-match "")
  11018. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11019. (equal (char-before) ?\ ))
  11020. (backward-delete-char 1)
  11021. (if (string-match "^[ \t]*$" (buffer-substring
  11022. (point-at-bol) (point-at-eol)))
  11023. (delete-region (point-at-bol)
  11024. (min (point-max) (1+ (point-at-eol))))))))))
  11025. (defun org-add-planning-info (what &optional time &rest remove)
  11026. "Insert new timestamp with keyword in the line directly after the headline.
  11027. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11028. If non is given, the user is prompted for a date.
  11029. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11030. be removed."
  11031. (interactive)
  11032. (let (org-time-was-given org-end-time-was-given ts
  11033. end default-time default-input)
  11034. (catch 'exit
  11035. (when (and (memq what '(scheduled deadline))
  11036. (or (not time)
  11037. (and (stringp time)
  11038. (string-match "^[-+]+[0-9]" time))))
  11039. ;; Try to get a default date/time from existing timestamp
  11040. (save-excursion
  11041. (org-back-to-heading t)
  11042. (setq end (save-excursion (outline-next-heading) (point)))
  11043. (when (re-search-forward (if (eq what 'scheduled)
  11044. org-scheduled-time-regexp
  11045. org-deadline-time-regexp)
  11046. end t)
  11047. (setq ts (match-string 1)
  11048. default-time
  11049. (apply 'encode-time (org-parse-time-string ts))
  11050. default-input (and ts (org-get-compact-tod ts))))))
  11051. (when what
  11052. (setq time
  11053. (if (stringp time)
  11054. ;; This is a string (relative or absolute), set proper date
  11055. (apply 'encode-time
  11056. (org-read-date-analyze
  11057. time default-time (decode-time default-time)))
  11058. ;; If necessary, get the time from the user
  11059. (or time (org-read-date nil 'to-time nil nil
  11060. default-time default-input)))))
  11061. (when (and org-insert-labeled-timestamps-at-point
  11062. (member what '(scheduled deadline)))
  11063. (insert
  11064. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11065. (org-insert-time-stamp time org-time-was-given
  11066. nil nil nil (list org-end-time-was-given))
  11067. (setq what nil))
  11068. (save-excursion
  11069. (save-restriction
  11070. (let (col list elt ts buffer-invisibility-spec)
  11071. (org-back-to-heading t)
  11072. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11073. (goto-char (match-end 1))
  11074. (setq col (current-column))
  11075. (goto-char (match-end 0))
  11076. (if (eobp) (insert "\n") (forward-char 1))
  11077. (when (and (not what)
  11078. (not (looking-at
  11079. (concat "[ \t]*"
  11080. org-keyword-time-not-clock-regexp))))
  11081. ;; Nothing to add, nothing to remove...... :-)
  11082. (throw 'exit nil))
  11083. (if (and (not (looking-at org-outline-regexp))
  11084. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11085. "[^\r\n]*"))
  11086. (not (equal (match-string 1) org-clock-string)))
  11087. (narrow-to-region (match-beginning 0) (match-end 0))
  11088. (insert-before-markers "\n")
  11089. (backward-char 1)
  11090. (narrow-to-region (point) (point))
  11091. (and org-adapt-indentation (org-indent-to-column col)))
  11092. ;; Check if we have to remove something.
  11093. (setq list (cons what remove))
  11094. (while list
  11095. (setq elt (pop list))
  11096. (when (or (and (eq elt 'scheduled)
  11097. (re-search-forward org-scheduled-time-regexp nil t))
  11098. (and (eq elt 'deadline)
  11099. (re-search-forward org-deadline-time-regexp nil t))
  11100. (and (eq elt 'closed)
  11101. (re-search-forward org-closed-time-regexp nil t)))
  11102. (replace-match "")
  11103. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11104. (and (looking-at "[ \t]+") (replace-match ""))
  11105. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11106. (when what
  11107. (insert
  11108. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11109. (cond ((eq what 'scheduled) org-scheduled-string)
  11110. ((eq what 'deadline) org-deadline-string)
  11111. ((eq what 'closed) org-closed-string))
  11112. " ")
  11113. (setq ts (org-insert-time-stamp
  11114. time
  11115. (or org-time-was-given
  11116. (and (eq what 'closed) org-log-done-with-time))
  11117. (eq what 'closed)
  11118. nil nil (list org-end-time-was-given)))
  11119. (insert
  11120. (if (not (or (bolp) (eq (char-before) ?\ )
  11121. (memq (char-after) '(32 10))
  11122. (eobp))) " " ""))
  11123. (end-of-line 1))
  11124. (goto-char (point-min))
  11125. (widen)
  11126. (if (and (looking-at "[ \t]*\n")
  11127. (equal (char-before) ?\n))
  11128. (delete-region (1- (point)) (point-at-eol)))
  11129. ts))))))
  11130. (defvar org-log-note-marker (make-marker))
  11131. (defvar org-log-note-purpose nil)
  11132. (defvar org-log-note-state nil)
  11133. (defvar org-log-note-previous-state nil)
  11134. (defvar org-log-note-how nil)
  11135. (defvar org-log-note-extra nil)
  11136. (defvar org-log-note-window-configuration nil)
  11137. (defvar org-log-note-return-to (make-marker))
  11138. (defvar org-log-note-effective-time nil
  11139. "Remembered current time so that dynamically scoped
  11140. `org-extend-today-until' affects tha timestamps in state change
  11141. log")
  11142. (defvar org-log-post-message nil
  11143. "Message to be displayed after a log note has been stored.
  11144. The auto-repeater uses this.")
  11145. (defun org-add-note ()
  11146. "Add a note to the current entry.
  11147. This is done in the same way as adding a state change note."
  11148. (interactive)
  11149. (org-add-log-setup 'note nil nil 'findpos nil))
  11150. (defvar org-property-end-re)
  11151. (defun org-add-log-setup (&optional purpose state prev-state
  11152. findpos how extra)
  11153. "Set up the post command hook to take a note.
  11154. If this is about to TODO state change, the new state is expected in STATE.
  11155. When FINDPOS is non-nil, find the correct position for the note in
  11156. the current entry. If not, assume that it can be inserted at point.
  11157. HOW is an indicator what kind of note should be created.
  11158. EXTRA is additional text that will be inserted into the notes buffer."
  11159. (let* ((org-log-into-drawer (org-log-into-drawer))
  11160. (drawer (cond ((stringp org-log-into-drawer)
  11161. org-log-into-drawer)
  11162. (org-log-into-drawer "LOGBOOK")
  11163. (t nil))))
  11164. (save-restriction
  11165. (save-excursion
  11166. (when findpos
  11167. (org-back-to-heading t)
  11168. (narrow-to-region (point) (save-excursion
  11169. (outline-next-heading) (point)))
  11170. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11171. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11172. "[^\r\n]*\\)?"))
  11173. (goto-char (match-end 0))
  11174. (cond
  11175. (drawer
  11176. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11177. nil t)
  11178. (progn
  11179. (goto-char (match-end 0))
  11180. (or org-log-states-order-reversed
  11181. (and (re-search-forward org-property-end-re nil t)
  11182. (goto-char (1- (match-beginning 0))))))
  11183. (insert "\n:" drawer ":\n:END:")
  11184. (beginning-of-line 0)
  11185. (org-indent-line-function)
  11186. (beginning-of-line 2)
  11187. (org-indent-line-function)
  11188. (end-of-line 0)))
  11189. ((and org-log-state-notes-insert-after-drawers
  11190. (save-excursion
  11191. (forward-line) (looking-at org-drawer-regexp)))
  11192. (forward-line)
  11193. (while (looking-at org-drawer-regexp)
  11194. (goto-char (match-end 0))
  11195. (re-search-forward org-property-end-re (point-max) t)
  11196. (forward-line))
  11197. (forward-line -1)))
  11198. (unless org-log-states-order-reversed
  11199. (and (= (char-after) ?\n) (forward-char 1))
  11200. (org-skip-over-state-notes)
  11201. (skip-chars-backward " \t\n\r")))
  11202. (move-marker org-log-note-marker (point))
  11203. (setq org-log-note-purpose purpose
  11204. org-log-note-state state
  11205. org-log-note-previous-state prev-state
  11206. org-log-note-how how
  11207. org-log-note-extra extra
  11208. org-log-note-effective-time (org-current-effective-time))
  11209. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11210. (defun org-skip-over-state-notes ()
  11211. "Skip past the list of State notes in an entry."
  11212. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11213. (when (ignore-errors (goto-char (org-in-item-p)))
  11214. (let* ((struct (org-list-struct))
  11215. (prevs (org-list-prevs-alist struct)))
  11216. (while (looking-at "[ \t]*- State")
  11217. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11218. (org-list-get-item-end (point) struct)))))))
  11219. (defun org-add-log-note (&optional purpose)
  11220. "Pop up a window for taking a note, and add this note later at point."
  11221. (remove-hook 'post-command-hook 'org-add-log-note)
  11222. (setq org-log-note-window-configuration (current-window-configuration))
  11223. (delete-other-windows)
  11224. (move-marker org-log-note-return-to (point))
  11225. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11226. (goto-char org-log-note-marker)
  11227. (org-switch-to-buffer-other-window "*Org Note*")
  11228. (erase-buffer)
  11229. (if (memq org-log-note-how '(time state))
  11230. (let (current-prefix-arg) (org-store-log-note))
  11231. (let ((org-inhibit-startup t)) (org-mode))
  11232. (insert (format "# Insert note for %s.
  11233. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11234. (cond
  11235. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11236. ((eq org-log-note-purpose 'done) "closed todo item")
  11237. ((eq org-log-note-purpose 'state)
  11238. (format "state change from \"%s\" to \"%s\""
  11239. (or org-log-note-previous-state "")
  11240. (or org-log-note-state "")))
  11241. ((eq org-log-note-purpose 'reschedule)
  11242. "rescheduling")
  11243. ((eq org-log-note-purpose 'delschedule)
  11244. "no longer scheduled")
  11245. ((eq org-log-note-purpose 'redeadline)
  11246. "changing deadline")
  11247. ((eq org-log-note-purpose 'deldeadline)
  11248. "removing deadline")
  11249. ((eq org-log-note-purpose 'refile)
  11250. "refiling")
  11251. ((eq org-log-note-purpose 'note)
  11252. "this entry")
  11253. (t (error "This should not happen")))))
  11254. (if org-log-note-extra (insert org-log-note-extra))
  11255. (org-set-local 'org-finish-function 'org-store-log-note)
  11256. (run-hooks 'org-log-buffer-setup-hook)))
  11257. (defvar org-note-abort nil) ; dynamically scoped
  11258. (defun org-store-log-note ()
  11259. "Finish taking a log note, and insert it to where it belongs."
  11260. (let ((txt (buffer-string))
  11261. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11262. lines ind bul)
  11263. (kill-buffer (current-buffer))
  11264. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11265. (setq txt (replace-match "" t t txt)))
  11266. (if (string-match "\\s-+\\'" txt)
  11267. (setq txt (replace-match "" t t txt)))
  11268. (setq lines (org-split-string txt "\n"))
  11269. (when (and note (string-match "\\S-" note))
  11270. (setq note
  11271. (org-replace-escapes
  11272. note
  11273. (list (cons "%u" (user-login-name))
  11274. (cons "%U" user-full-name)
  11275. (cons "%t" (format-time-string
  11276. (org-time-stamp-format 'long 'inactive)
  11277. org-log-note-effective-time))
  11278. (cons "%T" (format-time-string
  11279. (org-time-stamp-format 'long nil)
  11280. org-log-note-effective-time))
  11281. (cons "%d" (format-time-string
  11282. (org-time-stamp-format nil 'inactive)
  11283. org-log-note-effective-time))
  11284. (cons "%D" (format-time-string
  11285. (org-time-stamp-format nil nil)
  11286. org-log-note-effective-time))
  11287. (cons "%s" (if org-log-note-state
  11288. (concat "\"" org-log-note-state "\"")
  11289. ""))
  11290. (cons "%S" (if org-log-note-previous-state
  11291. (concat "\"" org-log-note-previous-state "\"")
  11292. "\"\"")))))
  11293. (if lines (setq note (concat note " \\\\")))
  11294. (push note lines))
  11295. (when (or current-prefix-arg org-note-abort)
  11296. (when org-log-into-drawer
  11297. (org-remove-empty-drawer-at
  11298. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11299. org-log-note-marker))
  11300. (setq lines nil))
  11301. (when lines
  11302. (with-current-buffer (marker-buffer org-log-note-marker)
  11303. (save-excursion
  11304. (goto-char org-log-note-marker)
  11305. (move-marker org-log-note-marker nil)
  11306. (end-of-line 1)
  11307. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11308. (setq ind (save-excursion
  11309. (if (ignore-errors (goto-char (org-in-item-p)))
  11310. (let ((struct (org-list-struct)))
  11311. (org-list-get-ind
  11312. (org-list-get-top-point struct) struct))
  11313. (skip-chars-backward " \r\t\n")
  11314. (cond
  11315. ((and (org-at-heading-p)
  11316. org-adapt-indentation)
  11317. (1+ (org-current-level)))
  11318. ((org-at-heading-p) 0)
  11319. (t (org-get-indentation))))))
  11320. (setq bul (org-list-bullet-string "-"))
  11321. (org-indent-line-to ind)
  11322. (insert bul (pop lines))
  11323. (let ((ind-body (+ (length bul) ind)))
  11324. (while lines
  11325. (insert "\n")
  11326. (org-indent-line-to ind-body)
  11327. (insert (pop lines))))
  11328. (message "Note stored")
  11329. (org-back-to-heading t)
  11330. (org-cycle-hide-drawers 'children)))))
  11331. (set-window-configuration org-log-note-window-configuration)
  11332. (with-current-buffer (marker-buffer org-log-note-return-to)
  11333. (goto-char org-log-note-return-to))
  11334. (move-marker org-log-note-return-to nil)
  11335. (and org-log-post-message (message "%s" org-log-post-message)))
  11336. (defun org-remove-empty-drawer-at (drawer pos)
  11337. "Remove an empty drawer DRAWER at position POS.
  11338. POS may also be a marker."
  11339. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11340. (save-excursion
  11341. (save-restriction
  11342. (widen)
  11343. (goto-char pos)
  11344. (if (org-in-regexp
  11345. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11346. (replace-match ""))))))
  11347. (defun org-sparse-tree (&optional arg)
  11348. "Create a sparse tree, prompt for the details.
  11349. This command can create sparse trees. You first need to select the type
  11350. of match used to create the tree:
  11351. t Show all TODO entries.
  11352. T Show entries with a specific TODO keyword.
  11353. m Show entries selected by a tags/property match.
  11354. p Enter a property name and its value (both with completion on existing
  11355. names/values) and show entries with that property.
  11356. r Show entries matching a regular expression (`/' can be used as well)
  11357. d Show deadlines due within `org-deadline-warning-days'.
  11358. b Show deadlines and scheduled items before a date.
  11359. a Show deadlines and scheduled items after a date."
  11360. (interactive "P")
  11361. (let (ans kwd value)
  11362. (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")
  11363. (setq ans (read-char-exclusive))
  11364. (cond
  11365. ((equal ans ?d)
  11366. (call-interactively 'org-check-deadlines))
  11367. ((equal ans ?b)
  11368. (call-interactively 'org-check-before-date))
  11369. ((equal ans ?a)
  11370. (call-interactively 'org-check-after-date))
  11371. ((equal ans ?D)
  11372. (call-interactively 'org-check-dates-range))
  11373. ((equal ans ?t)
  11374. (org-show-todo-tree nil))
  11375. ((equal ans ?T)
  11376. (org-show-todo-tree '(4)))
  11377. ((member ans '(?T ?m))
  11378. (call-interactively 'org-match-sparse-tree))
  11379. ((member ans '(?p ?P))
  11380. (setq kwd (org-icompleting-read "Property: "
  11381. (mapcar 'list (org-buffer-property-keys))))
  11382. (setq value (org-icompleting-read "Value: "
  11383. (mapcar 'list (org-property-values kwd))))
  11384. (unless (string-match "\\`{.*}\\'" value)
  11385. (setq value (concat "\"" value "\"")))
  11386. (org-match-sparse-tree arg (concat kwd "=" value)))
  11387. ((member ans '(?r ?R ?/))
  11388. (call-interactively 'org-occur))
  11389. (t (error "No such sparse tree command \"%c\"" ans)))))
  11390. (defvar org-occur-highlights nil
  11391. "List of overlays used for occur matches.")
  11392. (make-variable-buffer-local 'org-occur-highlights)
  11393. (defvar org-occur-parameters nil
  11394. "Parameters of the active org-occur calls.
  11395. This is a list, each call to org-occur pushes as cons cell,
  11396. containing the regular expression and the callback, onto the list.
  11397. The list can contain several entries if `org-occur' has been called
  11398. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11399. will only contain one set of parameters. When the highlights are
  11400. removed (for example with `C-c C-c', or with the next edit (depending
  11401. on `org-remove-highlights-with-change'), this variable is emptied
  11402. as well.")
  11403. (make-variable-buffer-local 'org-occur-parameters)
  11404. (defun org-occur (regexp &optional keep-previous callback)
  11405. "Make a compact tree which shows all matches of REGEXP.
  11406. The tree will show the lines where the regexp matches, and all higher
  11407. headlines above the match. It will also show the heading after the match,
  11408. to make sure editing the matching entry is easy.
  11409. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11410. call to `org-occur' will be kept, to allow stacking of calls to this
  11411. command.
  11412. If CALLBACK is non-nil, it is a function which is called to confirm
  11413. that the match should indeed be shown."
  11414. (interactive "sRegexp: \nP")
  11415. (when (equal regexp "")
  11416. (error "Regexp cannot be empty"))
  11417. (unless keep-previous
  11418. (org-remove-occur-highlights nil nil t))
  11419. (push (cons regexp callback) org-occur-parameters)
  11420. (let ((cnt 0))
  11421. (save-excursion
  11422. (goto-char (point-min))
  11423. (if (or (not keep-previous) ; do not want to keep
  11424. (not org-occur-highlights)) ; no previous matches
  11425. ;; hide everything
  11426. (org-overview))
  11427. (while (re-search-forward regexp nil t)
  11428. (when (or (not callback)
  11429. (save-match-data (funcall callback)))
  11430. (setq cnt (1+ cnt))
  11431. (when org-highlight-sparse-tree-matches
  11432. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11433. (org-show-context 'occur-tree))))
  11434. (when org-remove-highlights-with-change
  11435. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11436. nil 'local))
  11437. (unless org-sparse-tree-open-archived-trees
  11438. (org-hide-archived-subtrees (point-min) (point-max)))
  11439. (run-hooks 'org-occur-hook)
  11440. (if (org-called-interactively-p 'interactive)
  11441. (message "%d match(es) for regexp %s" cnt regexp))
  11442. cnt))
  11443. (defun org-occur-next-match (&optional n reset)
  11444. "Function for `next-error-function' to find sparse tree matches.
  11445. N is the number of matches to move, when negative move backwards.
  11446. RESET is entirely ignored - this function always goes back to the
  11447. starting point when no match is found."
  11448. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11449. (search-func (if (< n 0)
  11450. 'previous-single-char-property-change
  11451. 'next-single-char-property-change))
  11452. (n (abs n))
  11453. (pos (point))
  11454. p1)
  11455. (catch 'exit
  11456. (while (setq p1 (funcall search-func (point) 'org-type))
  11457. (when (equal p1 limit)
  11458. (goto-char pos)
  11459. (error "No more matches"))
  11460. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11461. (setq n (1- n))
  11462. (when (= n 0)
  11463. (goto-char p1)
  11464. (throw 'exit (point))))
  11465. (goto-char p1))
  11466. (goto-char p1)
  11467. (error "No more matches"))))
  11468. (defun org-show-context (&optional key)
  11469. "Make sure point and context are visible.
  11470. How much context is shown depends upon the variables
  11471. `org-show-hierarchy-above', `org-show-following-heading',
  11472. `org-show-entry-below' and `org-show-siblings'."
  11473. (let ((heading-p (org-at-heading-p t))
  11474. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11475. (following-p (org-get-alist-option org-show-following-heading key))
  11476. (entry-p (org-get-alist-option org-show-entry-below key))
  11477. (siblings-p (org-get-alist-option org-show-siblings key)))
  11478. (catch 'exit
  11479. ;; Show heading or entry text
  11480. (if (and heading-p (not entry-p))
  11481. (org-flag-heading nil) ; only show the heading
  11482. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11483. (org-show-hidden-entry))) ; show entire entry
  11484. (when following-p
  11485. ;; Show next sibling, or heading below text
  11486. (save-excursion
  11487. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11488. (org-flag-heading nil))))
  11489. (when siblings-p (org-show-siblings))
  11490. (when hierarchy-p
  11491. ;; show all higher headings, possibly with siblings
  11492. (save-excursion
  11493. (while (and (condition-case nil
  11494. (progn (org-up-heading-all 1) t)
  11495. (error nil))
  11496. (not (bobp)))
  11497. (org-flag-heading nil)
  11498. (when siblings-p (org-show-siblings))))))))
  11499. (defvar org-reveal-start-hook nil
  11500. "Hook run before revealing a location.")
  11501. (defun org-reveal (&optional siblings)
  11502. "Show current entry, hierarchy above it, and the following headline.
  11503. This can be used to show a consistent set of context around locations
  11504. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11505. not t for the search context.
  11506. With optional argument SIBLINGS, on each level of the hierarchy all
  11507. siblings are shown. This repairs the tree structure to what it would
  11508. look like when opened with hierarchical calls to `org-cycle'.
  11509. With double optional argument \\[universal-argument] \\[universal-argument], \
  11510. go to the parent and show the
  11511. entire tree."
  11512. (interactive "P")
  11513. (run-hooks 'org-reveal-start-hook)
  11514. (let ((org-show-hierarchy-above t)
  11515. (org-show-following-heading t)
  11516. (org-show-siblings (if siblings t org-show-siblings)))
  11517. (org-show-context nil))
  11518. (when (equal siblings '(16))
  11519. (save-excursion
  11520. (when (org-up-heading-safe)
  11521. (org-show-subtree)
  11522. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11523. (defun org-highlight-new-match (beg end)
  11524. "Highlight from BEG to END and mark the highlight is an occur headline."
  11525. (let ((ov (make-overlay beg end)))
  11526. (overlay-put ov 'face 'secondary-selection)
  11527. (overlay-put ov 'org-type 'org-occur)
  11528. (push ov org-occur-highlights)))
  11529. (defun org-remove-occur-highlights (&optional beg end noremove)
  11530. "Remove the occur highlights from the buffer.
  11531. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11532. from the `before-change-functions' in the current buffer."
  11533. (interactive)
  11534. (unless org-inhibit-highlight-removal
  11535. (mapc 'delete-overlay org-occur-highlights)
  11536. (setq org-occur-highlights nil)
  11537. (setq org-occur-parameters nil)
  11538. (unless noremove
  11539. (remove-hook 'before-change-functions
  11540. 'org-remove-occur-highlights 'local))))
  11541. ;;;; Priorities
  11542. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11543. "Regular expression matching the priority indicator.")
  11544. (defvar org-remove-priority-next-time nil)
  11545. (defun org-priority-up ()
  11546. "Increase the priority of the current item."
  11547. (interactive)
  11548. (org-priority 'up))
  11549. (defun org-priority-down ()
  11550. "Decrease the priority of the current item."
  11551. (interactive)
  11552. (org-priority 'down))
  11553. (defun org-priority (&optional action)
  11554. "Change the priority of an item by ARG.
  11555. ACTION can be `set', `up', `down', or a character."
  11556. (interactive)
  11557. (unless org-enable-priority-commands
  11558. (error "Priority commands are disabled"))
  11559. (setq action (or action 'set))
  11560. (let (current new news have remove)
  11561. (save-excursion
  11562. (org-back-to-heading t)
  11563. (if (looking-at org-priority-regexp)
  11564. (setq current (string-to-char (match-string 2))
  11565. have t))
  11566. (cond
  11567. ((eq action 'remove)
  11568. (setq remove t new ?\ ))
  11569. ((or (eq action 'set)
  11570. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11571. (if (not (eq action 'set))
  11572. (setq new action)
  11573. (message "Priority %c-%c, SPC to remove: "
  11574. org-highest-priority org-lowest-priority)
  11575. (save-match-data
  11576. (setq new (read-char-exclusive))))
  11577. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11578. (= (upcase org-lowest-priority) org-lowest-priority))
  11579. (setq new (upcase new)))
  11580. (cond ((equal new ?\ ) (setq remove t))
  11581. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11582. (error "Priority must be between `%c' and `%c'"
  11583. org-highest-priority org-lowest-priority))))
  11584. ((eq action 'up)
  11585. (setq new (if have
  11586. (1- current) ; normal cycling
  11587. ;; last priority was empty
  11588. (if (eq last-command this-command)
  11589. org-lowest-priority ; wrap around empty to lowest
  11590. ;; default
  11591. (if org-priority-start-cycle-with-default
  11592. org-default-priority
  11593. (1- org-default-priority))))))
  11594. ((eq action 'down)
  11595. (setq new (if have
  11596. (1+ current) ; normal cycling
  11597. ;; last priority was empty
  11598. (if (eq last-command this-command)
  11599. org-highest-priority ; wrap around empty to highest
  11600. ;; default
  11601. (if org-priority-start-cycle-with-default
  11602. org-default-priority
  11603. (1+ org-default-priority))))))
  11604. (t (error "Invalid action")))
  11605. (if (or (< (upcase new) org-highest-priority)
  11606. (> (upcase new) org-lowest-priority))
  11607. (if (and (memq action '(up down))
  11608. (not have) (not (eq last-command this-command)))
  11609. ;; `new' is from default priority
  11610. (error
  11611. "The default can not be set, see `org-default-priority' why")
  11612. ;; normal cycling: `new' is beyond highest/lowest priority
  11613. ;; and is wrapped around to the empty priority
  11614. (setq remove t)))
  11615. (setq news (format "%c" new))
  11616. (if have
  11617. (if remove
  11618. (replace-match "" t t nil 1)
  11619. (replace-match news t t nil 2))
  11620. (if remove
  11621. (error "No priority cookie found in line")
  11622. (let ((case-fold-search nil))
  11623. (looking-at org-todo-line-regexp))
  11624. (if (match-end 2)
  11625. (progn
  11626. (goto-char (match-end 2))
  11627. (insert " [#" news "]"))
  11628. (goto-char (match-beginning 3))
  11629. (insert "[#" news "] "))))
  11630. (org-preserve-lc (org-set-tags nil 'align)))
  11631. (if remove
  11632. (message "Priority removed")
  11633. (message "Priority of current item set to %s" news))))
  11634. (defun org-get-priority (s)
  11635. "Find priority cookie and return priority."
  11636. (if (functionp org-get-priority-function)
  11637. (funcall org-get-priority-function)
  11638. (save-match-data
  11639. (if (not (string-match org-priority-regexp s))
  11640. (* 1000 (- org-lowest-priority org-default-priority))
  11641. (* 1000 (- org-lowest-priority
  11642. (string-to-char (match-string 2 s))))))))
  11643. ;;;; Tags
  11644. (defvar org-agenda-archives-mode)
  11645. (defvar org-map-continue-from nil
  11646. "Position from where mapping should continue.
  11647. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11648. (defvar org-scanner-tags nil
  11649. "The current tag list while the tags scanner is running.")
  11650. (defvar org-trust-scanner-tags nil
  11651. "Should `org-get-tags-at' use the tags for the scanner.
  11652. This is for internal dynamical scoping only.
  11653. When this is non-nil, the function `org-get-tags-at' will return the value
  11654. of `org-scanner-tags' instead of building the list by itself. This
  11655. can lead to large speed-ups when the tags scanner is used in a file with
  11656. many entries, and when the list of tags is retrieved, for example to
  11657. obtain a list of properties. Building the tags list for each entry in such
  11658. a file becomes an N^2 operation - but with this variable set, it scales
  11659. as N.")
  11660. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11661. "Scan headline tags with inheritance and produce output ACTION.
  11662. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11663. or `agenda' to produce an entry list for an agenda view. It can also be
  11664. a Lisp form or a function that should be called at each matched headline, in
  11665. this case the return value is a list of all return values from these calls.
  11666. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11667. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11668. only lines with a TODO keyword are included in the output.
  11669. START-LEVEL can be a string with asterisks, reducing the scope to
  11670. headlines matching this string."
  11671. (require 'org-agenda)
  11672. (let* ((re (concat "^"
  11673. (if start-level
  11674. ;; Get the correct level to match
  11675. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11676. org-outline-regexp)
  11677. " *\\(\\<\\("
  11678. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11679. (org-re
  11680. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11681. (props (list 'face 'default
  11682. 'done-face 'org-agenda-done
  11683. 'undone-face 'default
  11684. 'mouse-face 'highlight
  11685. 'org-not-done-regexp org-not-done-regexp
  11686. 'org-todo-regexp org-todo-regexp
  11687. 'org-complex-heading-regexp org-complex-heading-regexp
  11688. 'help-echo
  11689. (format "mouse-2 or RET jump to org file %s"
  11690. (abbreviate-file-name
  11691. (or (buffer-file-name (buffer-base-buffer))
  11692. (buffer-name (buffer-base-buffer)))))))
  11693. (case-fold-search nil)
  11694. (org-map-continue-from nil)
  11695. lspos tags
  11696. (tags-alist (list (cons 0 org-file-tags)))
  11697. (llast 0) rtn rtn1 level category i txt
  11698. todo marker entry priority)
  11699. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11700. (setq action (list 'lambda nil action)))
  11701. (save-excursion
  11702. (goto-char (point-min))
  11703. (when (eq action 'sparse-tree)
  11704. (org-overview)
  11705. (org-remove-occur-highlights))
  11706. (while (re-search-forward re nil t)
  11707. (setq org-map-continue-from nil)
  11708. (catch :skip
  11709. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11710. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11711. (goto-char (setq lspos (match-beginning 0)))
  11712. (setq level (org-reduced-level (funcall outline-level))
  11713. category (org-get-category))
  11714. (setq i llast llast level)
  11715. ;; remove tag lists from same and sublevels
  11716. (while (>= i level)
  11717. (when (setq entry (assoc i tags-alist))
  11718. (setq tags-alist (delete entry tags-alist)))
  11719. (setq i (1- i)))
  11720. ;; add the next tags
  11721. (when tags
  11722. (setq tags (org-split-string tags ":")
  11723. tags-alist
  11724. (cons (cons level tags) tags-alist)))
  11725. ;; compile tags for current headline
  11726. (setq tags-list
  11727. (if org-use-tag-inheritance
  11728. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11729. tags)
  11730. org-scanner-tags tags-list)
  11731. (when org-use-tag-inheritance
  11732. (setcdr (car tags-alist)
  11733. (mapcar (lambda (x)
  11734. (setq x (copy-sequence x))
  11735. (org-add-prop-inherited x))
  11736. (cdar tags-alist))))
  11737. (when (and tags org-use-tag-inheritance
  11738. (or (not (eq t org-use-tag-inheritance))
  11739. org-tags-exclude-from-inheritance))
  11740. ;; selective inheritance, remove uninherited ones
  11741. (setcdr (car tags-alist)
  11742. (org-remove-uninherited-tags (cdar tags-alist))))
  11743. (when (and
  11744. ;; eval matcher only when the todo condition is OK
  11745. (and (or (not todo-only) (member todo org-not-done-keywords))
  11746. (let ((case-fold-search t)) (eval matcher)))
  11747. ;; Call the skipper, but return t if it does not skip,
  11748. ;; so that the `and' form continues evaluating
  11749. (progn
  11750. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11751. t)
  11752. ;; Check if timestamps are deselecting this entry
  11753. (or (not todo-only)
  11754. (and (member todo org-not-done-keywords)
  11755. (or (not org-agenda-tags-todo-honor-ignore-options)
  11756. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11757. ;; Extra check for the archive tag
  11758. ;; FIXME: Does the skipper already do this????
  11759. (or
  11760. (not (member org-archive-tag tags-list))
  11761. ;; we have an archive tag, should we use this anyway?
  11762. (or (not org-agenda-skip-archived-trees)
  11763. (and (eq action 'agenda) org-agenda-archives-mode))))
  11764. ;; select this headline
  11765. (cond
  11766. ((eq action 'sparse-tree)
  11767. (and org-highlight-sparse-tree-matches
  11768. (org-get-heading) (match-end 0)
  11769. (org-highlight-new-match
  11770. (match-beginning 1) (match-end 1)))
  11771. (org-show-context 'tags-tree))
  11772. ((eq action 'agenda)
  11773. (setq txt (org-agenda-format-item
  11774. ""
  11775. (concat
  11776. (if (eq org-tags-match-list-sublevels 'indented)
  11777. (make-string (1- level) ?.) "")
  11778. (org-get-heading))
  11779. category
  11780. tags-list)
  11781. priority (org-get-priority txt))
  11782. (goto-char lspos)
  11783. (setq marker (org-agenda-new-marker))
  11784. (org-add-props txt props
  11785. 'org-marker marker 'org-hd-marker marker 'org-category category
  11786. 'todo-state todo
  11787. 'priority priority 'type "tagsmatch")
  11788. (push txt rtn))
  11789. ((functionp action)
  11790. (setq org-map-continue-from nil)
  11791. (save-excursion
  11792. (setq rtn1 (funcall action))
  11793. (push rtn1 rtn)))
  11794. (t (error "Invalid action")))
  11795. ;; if we are to skip sublevels, jump to end of subtree
  11796. (unless org-tags-match-list-sublevels
  11797. (org-end-of-subtree t)
  11798. (backward-char 1))))
  11799. ;; Get the correct position from where to continue
  11800. (if org-map-continue-from
  11801. (goto-char org-map-continue-from)
  11802. (and (= (point) lspos) (end-of-line 1)))))
  11803. (when (and (eq action 'sparse-tree)
  11804. (not org-sparse-tree-open-archived-trees))
  11805. (org-hide-archived-subtrees (point-min) (point-max)))
  11806. (nreverse rtn)))
  11807. (defun org-remove-uninherited-tags (tags)
  11808. "Remove all tags that are not inherited from the list TAGS."
  11809. (cond
  11810. ((eq org-use-tag-inheritance t)
  11811. (if org-tags-exclude-from-inheritance
  11812. (org-delete-all org-tags-exclude-from-inheritance tags)
  11813. tags))
  11814. ((not org-use-tag-inheritance) nil)
  11815. ((stringp org-use-tag-inheritance)
  11816. (delq nil (mapcar
  11817. (lambda (x)
  11818. (if (and (string-match org-use-tag-inheritance x)
  11819. (not (member x org-tags-exclude-from-inheritance)))
  11820. x nil))
  11821. tags)))
  11822. ((listp org-use-tag-inheritance)
  11823. (delq nil (mapcar
  11824. (lambda (x)
  11825. (if (member x org-use-tag-inheritance) x nil))
  11826. tags)))))
  11827. (defvar todo-only) ;; dynamically scoped
  11828. (defun org-match-sparse-tree (&optional todo-only match)
  11829. "Create a sparse tree according to tags string MATCH.
  11830. MATCH can contain positive and negative selection of tags, like
  11831. \"+WORK+URGENT-WITHBOSS\".
  11832. If optional argument TODO-ONLY is non-nil, only select lines that are
  11833. also TODO lines."
  11834. (interactive "P")
  11835. (org-prepare-agenda-buffers (list (current-buffer)))
  11836. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11837. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11838. (defvar org-cached-props nil)
  11839. (defun org-cached-entry-get (pom property)
  11840. (if (or (eq t org-use-property-inheritance)
  11841. (and (stringp org-use-property-inheritance)
  11842. (string-match org-use-property-inheritance property))
  11843. (and (listp org-use-property-inheritance)
  11844. (member property org-use-property-inheritance)))
  11845. ;; Caching is not possible, check it directly
  11846. (org-entry-get pom property 'inherit)
  11847. ;; Get all properties, so that we can do complicated checks easily
  11848. (cdr (assoc property (or org-cached-props
  11849. (setq org-cached-props
  11850. (org-entry-properties pom)))))))
  11851. (defun org-global-tags-completion-table (&optional files)
  11852. "Return the list of all tags in all agenda buffer/files.
  11853. Optional FILES argument is a list of files to which can be used
  11854. instead of the agenda files."
  11855. (save-excursion
  11856. (org-uniquify
  11857. (delq nil
  11858. (apply 'append
  11859. (mapcar
  11860. (lambda (file)
  11861. (set-buffer (find-file-noselect file))
  11862. (append (org-get-buffer-tags)
  11863. (mapcar (lambda (x) (if (stringp (car-safe x))
  11864. (list (car-safe x)) nil))
  11865. org-tag-alist)))
  11866. (if (and files (car files))
  11867. files
  11868. (org-agenda-files))))))))
  11869. (defun org-make-tags-matcher (match)
  11870. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11871. ;; todo-only is scoped dynamically into this function, and the function
  11872. ;; may change it if the matcher asks for it.
  11873. (unless match
  11874. ;; Get a new match request, with completion
  11875. (let ((org-last-tags-completion-table
  11876. (org-global-tags-completion-table)))
  11877. (setq match (org-completing-read-no-i
  11878. "Match: " 'org-tags-completion-function nil nil nil
  11879. 'org-tags-history))))
  11880. ;; Parse the string and create a lisp form
  11881. (let ((match0 match)
  11882. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11883. minus tag mm
  11884. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11885. orterms term orlist re-p str-p level-p level-op time-p
  11886. prop-p pn pv po gv rest)
  11887. (if (string-match "/+" match)
  11888. ;; match contains also a todo-matching request
  11889. (progn
  11890. (setq tagsmatch (substring match 0 (match-beginning 0))
  11891. todomatch (substring match (match-end 0)))
  11892. (if (string-match "^!" todomatch)
  11893. (setq todo-only t todomatch (substring todomatch 1)))
  11894. (if (string-match "^\\s-*$" todomatch)
  11895. (setq todomatch nil)))
  11896. ;; only matching tags
  11897. (setq tagsmatch match todomatch nil))
  11898. ;; Make the tags matcher
  11899. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11900. (setq tagsmatcher t)
  11901. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11902. (while (setq term (pop orterms))
  11903. (while (and (equal (substring term -1) "\\") orterms)
  11904. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11905. (while (string-match re term)
  11906. (setq rest (substring term (match-end 0))
  11907. minus (and (match-end 1)
  11908. (equal (match-string 1 term) "-"))
  11909. tag (save-match-data (replace-regexp-in-string
  11910. "\\\\-" "-"
  11911. (match-string 2 term)))
  11912. re-p (equal (string-to-char tag) ?{)
  11913. level-p (match-end 4)
  11914. prop-p (match-end 5)
  11915. mm (cond
  11916. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11917. (level-p
  11918. (setq level-op (org-op-to-function (match-string 3 term)))
  11919. `(,level-op level ,(string-to-number
  11920. (match-string 4 term))))
  11921. (prop-p
  11922. (setq pn (match-string 5 term)
  11923. po (match-string 6 term)
  11924. pv (match-string 7 term)
  11925. re-p (equal (string-to-char pv) ?{)
  11926. str-p (equal (string-to-char pv) ?\")
  11927. time-p (save-match-data
  11928. (string-match "^\"[[<].*[]>]\"$" pv))
  11929. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11930. (if time-p (setq pv (org-matcher-time pv)))
  11931. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11932. (cond
  11933. ((equal pn "CATEGORY")
  11934. (setq gv '(get-text-property (point) 'org-category)))
  11935. ((equal pn "TODO")
  11936. (setq gv 'todo))
  11937. (t
  11938. (setq gv `(org-cached-entry-get nil ,pn))))
  11939. (if re-p
  11940. (if (eq po 'org<>)
  11941. `(not (string-match ,pv (or ,gv "")))
  11942. `(string-match ,pv (or ,gv "")))
  11943. (if str-p
  11944. `(,po (or ,gv "") ,pv)
  11945. `(,po (string-to-number (or ,gv ""))
  11946. ,(string-to-number pv) ))))
  11947. (t `(member ,tag tags-list)))
  11948. mm (if minus (list 'not mm) mm)
  11949. term rest)
  11950. (push mm tagsmatcher))
  11951. (push (if (> (length tagsmatcher) 1)
  11952. (cons 'and tagsmatcher)
  11953. (car tagsmatcher))
  11954. orlist)
  11955. (setq tagsmatcher nil))
  11956. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11957. (setq tagsmatcher
  11958. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11959. ;; Make the todo matcher
  11960. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11961. (setq todomatcher t)
  11962. (setq orterms (org-split-string todomatch "|") orlist nil)
  11963. (while (setq term (pop orterms))
  11964. (while (string-match re term)
  11965. (setq minus (and (match-end 1)
  11966. (equal (match-string 1 term) "-"))
  11967. kwd (match-string 2 term)
  11968. re-p (equal (string-to-char kwd) ?{)
  11969. term (substring term (match-end 0))
  11970. mm (if re-p
  11971. `(string-match ,(substring kwd 1 -1) todo)
  11972. (list 'equal 'todo kwd))
  11973. mm (if minus (list 'not mm) mm))
  11974. (push mm todomatcher))
  11975. (push (if (> (length todomatcher) 1)
  11976. (cons 'and todomatcher)
  11977. (car todomatcher))
  11978. orlist)
  11979. (setq todomatcher nil))
  11980. (setq todomatcher (if (> (length orlist) 1)
  11981. (cons 'or orlist) (car orlist))))
  11982. ;; Return the string and lisp forms of the matcher
  11983. (setq matcher (if todomatcher
  11984. (list 'and tagsmatcher todomatcher)
  11985. tagsmatcher))
  11986. (cons match0 matcher)))
  11987. (defun org-op-to-function (op &optional stringp)
  11988. "Turn an operator into the appropriate function."
  11989. (setq op
  11990. (cond
  11991. ((equal op "<" ) '(< string< org-time<))
  11992. ((equal op ">" ) '(> org-string> org-time>))
  11993. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11994. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11995. ((member op '("=" "==")) '(= string= org-time=))
  11996. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11997. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11998. (defun org<> (a b) (not (= a b)))
  11999. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12000. (defun org-string>= (a b) (not (string< a b)))
  12001. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12002. (defun org-string<> (a b) (not (string= a b)))
  12003. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12004. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< 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) (org<> a b)))
  12009. (defun org-2ft (s)
  12010. "Convert S to a floating point time.
  12011. If S is already a number, just return it. If it is a string, parse
  12012. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12013. (cond
  12014. ((numberp s) s)
  12015. ((stringp s)
  12016. (condition-case nil
  12017. (float-time (apply 'encode-time (org-parse-time-string s)))
  12018. (error 0.)))
  12019. (t 0.)))
  12020. (defun org-time-today ()
  12021. "Time in seconds today at 0:00.
  12022. Returns the float number of seconds since the beginning of the
  12023. epoch to the beginning of today (00:00)."
  12024. (float-time (apply 'encode-time
  12025. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12026. (defun org-matcher-time (s)
  12027. "Interpret a time comparison value."
  12028. (save-match-data
  12029. (cond
  12030. ((string= s "<now>") (float-time))
  12031. ((string= s "<today>") (org-time-today))
  12032. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12033. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12034. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  12035. (+ (org-time-today)
  12036. (* (string-to-number (match-string 1 s))
  12037. (cdr (assoc (match-string 2 s)
  12038. '(("d" . 86400.0) ("w" . 604800.0)
  12039. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12040. (t (org-2ft s)))))
  12041. (defun org-match-any-p (re list)
  12042. "Does re match any element of list?"
  12043. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12044. (delq nil list))
  12045. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12046. (defvar org-tags-overlay (make-overlay 1 1))
  12047. (org-detach-overlay org-tags-overlay)
  12048. (defun org-get-local-tags-at (&optional pos)
  12049. "Get a list of tags defined in the current headline."
  12050. (org-get-tags-at pos 'local))
  12051. (defun org-get-local-tags ()
  12052. "Get a list of tags defined in the current headline."
  12053. (org-get-tags-at nil 'local))
  12054. (defun org-get-tags-at (&optional pos local)
  12055. "Get a list of all headline tags applicable at POS.
  12056. POS defaults to point. If tags are inherited, the list contains
  12057. the targets in the same sequence as the headlines appear, i.e.
  12058. the tags of the current headline come last.
  12059. When LOCAL is non-nil, only return tags from the current headline,
  12060. ignore inherited ones."
  12061. (interactive)
  12062. (if (and org-trust-scanner-tags
  12063. (or (not pos) (equal pos (point)))
  12064. (not local))
  12065. org-scanner-tags
  12066. (let (tags ltags lastpos parent)
  12067. (save-excursion
  12068. (save-restriction
  12069. (widen)
  12070. (goto-char (or pos (point)))
  12071. (save-match-data
  12072. (catch 'done
  12073. (condition-case nil
  12074. (progn
  12075. (org-back-to-heading t)
  12076. (while (not (equal lastpos (point)))
  12077. (setq lastpos (point))
  12078. (when (looking-at
  12079. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12080. (setq ltags (org-split-string
  12081. (org-match-string-no-properties 1) ":"))
  12082. (when parent
  12083. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12084. (setq tags (append
  12085. (if parent
  12086. (org-remove-uninherited-tags ltags)
  12087. ltags)
  12088. tags)))
  12089. (or org-use-tag-inheritance (throw 'done t))
  12090. (if local (throw 'done t))
  12091. (or (org-up-heading-safe) (error nil))
  12092. (setq parent t)))
  12093. (error nil)))))
  12094. (if local
  12095. tags
  12096. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12097. (defun org-add-prop-inherited (s)
  12098. (add-text-properties 0 (length s) '(inherited t) s)
  12099. s)
  12100. (defun org-toggle-tag (tag &optional onoff)
  12101. "Toggle the tag TAG for the current line.
  12102. If ONOFF is `on' or `off', don't toggle but set to this state."
  12103. (let (res current)
  12104. (save-excursion
  12105. (org-back-to-heading t)
  12106. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12107. (point-at-eol) t)
  12108. (progn
  12109. (setq current (match-string 1))
  12110. (replace-match ""))
  12111. (setq current ""))
  12112. (setq current (nreverse (org-split-string current ":")))
  12113. (cond
  12114. ((eq onoff 'on)
  12115. (setq res t)
  12116. (or (member tag current) (push tag current)))
  12117. ((eq onoff 'off)
  12118. (or (not (member tag current)) (setq current (delete tag current))))
  12119. (t (if (member tag current)
  12120. (setq current (delete tag current))
  12121. (setq res t)
  12122. (push tag current))))
  12123. (end-of-line 1)
  12124. (if current
  12125. (progn
  12126. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12127. (org-set-tags nil t))
  12128. (delete-horizontal-space))
  12129. (run-hooks 'org-after-tags-change-hook))
  12130. res))
  12131. (defun org-align-tags-here (to-col)
  12132. ;; Assumes that this is a headline
  12133. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12134. (beginning-of-line 1)
  12135. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12136. (< pos (match-beginning 2)))
  12137. (progn
  12138. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12139. (goto-char (match-beginning 1))
  12140. (insert " ")
  12141. (delete-region (point) (1+ (match-beginning 2)))
  12142. (setq ncol (max (current-column)
  12143. (1+ col)
  12144. (if (> to-col 0)
  12145. to-col
  12146. (- (abs to-col) tags-l))))
  12147. (setq p (point))
  12148. (insert (make-string (- ncol (current-column)) ?\ ))
  12149. (setq ncol (current-column))
  12150. (when indent-tabs-mode (tabify p (point-at-eol)))
  12151. (org-move-to-column (min ncol col) t))
  12152. (goto-char pos))))
  12153. (defun org-set-tags-command (&optional arg just-align)
  12154. "Call the set-tags command for the current entry."
  12155. (interactive "P")
  12156. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12157. (org-set-tags arg just-align)
  12158. (save-excursion
  12159. (org-back-to-heading t)
  12160. (org-set-tags arg just-align))))
  12161. (defun org-set-tags-to (data)
  12162. "Set the tags of the current entry to DATA, replacing the current tags.
  12163. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12164. If DATA is nil or the empty string, any tags will be removed."
  12165. (interactive "sTags: ")
  12166. (setq data
  12167. (cond
  12168. ((eq data nil) "")
  12169. ((equal data "") "")
  12170. ((stringp data)
  12171. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12172. ":"))
  12173. ((listp data)
  12174. (concat ":" (mapconcat 'identity data ":") ":"))
  12175. (t nil)))
  12176. (when data
  12177. (save-excursion
  12178. (org-back-to-heading t)
  12179. (when (looking-at org-complex-heading-regexp)
  12180. (if (match-end 5)
  12181. (progn
  12182. (goto-char (match-beginning 5))
  12183. (insert data)
  12184. (delete-region (point) (point-at-eol))
  12185. (org-set-tags nil 'align))
  12186. (goto-char (point-at-eol))
  12187. (insert " " data)
  12188. (org-set-tags nil 'align)))
  12189. (beginning-of-line 1)
  12190. (if (looking-at ".*?\\([ \t]+\\)$")
  12191. (delete-region (match-beginning 1) (match-end 1))))))
  12192. (defun org-align-all-tags ()
  12193. "Align the tags i all headings."
  12194. (interactive)
  12195. (save-excursion
  12196. (or (ignore-errors (org-back-to-heading t))
  12197. (outline-next-heading))
  12198. (if (org-at-heading-p)
  12199. (org-set-tags t)
  12200. (message "No headings"))))
  12201. (defvar org-indent-indentation-per-level)
  12202. (defun org-set-tags (&optional arg just-align)
  12203. "Set the tags for the current headline.
  12204. With prefix ARG, realign all tags in headings in the current buffer."
  12205. (interactive "P")
  12206. (let* ((re org-outline-regexp-bol)
  12207. (current (unless arg (org-get-tags-string)))
  12208. (col (current-column))
  12209. (org-setting-tags t)
  12210. table current-tags inherited-tags ; computed below when needed
  12211. tags p0 c0 c1 rpl di tc level)
  12212. (if arg
  12213. (save-excursion
  12214. (goto-char (point-min))
  12215. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12216. (while (re-search-forward re nil t)
  12217. (org-set-tags nil t)
  12218. (end-of-line 1)))
  12219. (message "All tags realigned to column %d" org-tags-column))
  12220. (if just-align
  12221. (setq tags current)
  12222. ;; Get a new set of tags from the user
  12223. (save-excursion
  12224. (setq table (append org-tag-persistent-alist
  12225. (or org-tag-alist (org-get-buffer-tags))
  12226. (and
  12227. org-complete-tags-always-offer-all-agenda-tags
  12228. (org-global-tags-completion-table
  12229. (org-agenda-files))))
  12230. org-last-tags-completion-table table
  12231. current-tags (org-split-string current ":")
  12232. inherited-tags (nreverse
  12233. (nthcdr (length current-tags)
  12234. (nreverse (org-get-tags-at))))
  12235. tags
  12236. (if (or (eq t org-use-fast-tag-selection)
  12237. (and org-use-fast-tag-selection
  12238. (delq nil (mapcar 'cdr table))))
  12239. (org-fast-tag-selection
  12240. current-tags inherited-tags table
  12241. (if org-fast-tag-selection-include-todo
  12242. org-todo-key-alist))
  12243. (let ((org-add-colon-after-tag-completion t))
  12244. (org-trim
  12245. (org-icompleting-read "Tags: "
  12246. 'org-tags-completion-function
  12247. nil nil current 'org-tags-history))))))
  12248. (while (string-match "[-+&]+" tags)
  12249. ;; No boolean logic, just a list
  12250. (setq tags (replace-match ":" t t tags))))
  12251. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12252. (if org-tags-sort-function
  12253. (setq tags (mapconcat 'identity
  12254. (sort (org-split-string
  12255. tags (org-re "[^[:alnum:]_@#%]+"))
  12256. org-tags-sort-function) ":")))
  12257. (if (string-match "\\`[\t ]*\\'" tags)
  12258. (setq tags "")
  12259. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12260. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12261. ;; Insert new tags at the correct column
  12262. (beginning-of-line 1)
  12263. (setq level (or (and (looking-at org-outline-regexp)
  12264. (- (match-end 0) (point) 1))
  12265. 1))
  12266. (cond
  12267. ((and (equal current "") (equal tags "")))
  12268. ((re-search-forward
  12269. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12270. (point-at-eol) t)
  12271. (if (equal tags "")
  12272. (setq rpl "")
  12273. (goto-char (match-beginning 0))
  12274. (setq c0 (current-column)
  12275. ;; compute offset for the case of org-indent-mode active
  12276. di (if org-indent-mode
  12277. (* (1- org-indent-indentation-per-level) (1- level))
  12278. 0)
  12279. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12280. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12281. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12282. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12283. (replace-match rpl t t)
  12284. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12285. tags)
  12286. (t (error "Tags alignment failed")))
  12287. (org-move-to-column col)
  12288. (unless just-align
  12289. (run-hooks 'org-after-tags-change-hook)))))
  12290. (defun org-change-tag-in-region (beg end tag off)
  12291. "Add or remove TAG for each entry in the region.
  12292. This works in the agenda, and also in an org-mode buffer."
  12293. (interactive
  12294. (list (region-beginning) (region-end)
  12295. (let ((org-last-tags-completion-table
  12296. (if (eq major-mode 'org-mode)
  12297. (org-get-buffer-tags)
  12298. (org-global-tags-completion-table))))
  12299. (org-icompleting-read
  12300. "Tag: " 'org-tags-completion-function nil nil nil
  12301. 'org-tags-history))
  12302. (progn
  12303. (message "[s]et or [r]emove? ")
  12304. (equal (read-char-exclusive) ?r))))
  12305. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12306. (let ((agendap (equal major-mode 'org-agenda-mode))
  12307. l1 l2 m buf pos newhead (cnt 0))
  12308. (goto-char end)
  12309. (setq l2 (1- (org-current-line)))
  12310. (goto-char beg)
  12311. (setq l1 (org-current-line))
  12312. (loop for l from l1 to l2 do
  12313. (org-goto-line l)
  12314. (setq m (get-text-property (point) 'org-hd-marker))
  12315. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12316. (and agendap m))
  12317. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12318. pos (if agendap m (point)))
  12319. (with-current-buffer buf
  12320. (save-excursion
  12321. (save-restriction
  12322. (goto-char pos)
  12323. (setq cnt (1+ cnt))
  12324. (org-toggle-tag tag (if off 'off 'on))
  12325. (setq newhead (org-get-heading)))))
  12326. (and agendap (org-agenda-change-all-lines newhead m))))
  12327. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12328. (defun org-tags-completion-function (string predicate &optional flag)
  12329. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12330. (confirm (lambda (x) (stringp (car x)))))
  12331. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12332. (setq s1 (match-string 1 string)
  12333. s2 (match-string 2 string))
  12334. (setq s1 "" s2 string))
  12335. (cond
  12336. ((eq flag nil)
  12337. ;; try completion
  12338. (setq rtn (try-completion s2 ctable confirm))
  12339. (if (stringp rtn)
  12340. (setq rtn
  12341. (concat s1 s2 (substring rtn (length s2))
  12342. (if (and org-add-colon-after-tag-completion
  12343. (assoc rtn ctable))
  12344. ":" ""))))
  12345. rtn)
  12346. ((eq flag t)
  12347. ;; all-completions
  12348. (all-completions s2 ctable confirm)
  12349. )
  12350. ((eq flag 'lambda)
  12351. ;; exact match?
  12352. (assoc s2 ctable)))
  12353. ))
  12354. (defun org-fast-tag-insert (kwd tags face &optional end)
  12355. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12356. (insert (format "%-12s" (concat kwd ":"))
  12357. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12358. (or end "")))
  12359. (defun org-fast-tag-show-exit (flag)
  12360. (save-excursion
  12361. (org-goto-line 3)
  12362. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12363. (replace-match ""))
  12364. (when flag
  12365. (end-of-line 1)
  12366. (org-move-to-column (- (window-width) 19) t)
  12367. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12368. (defun org-set-current-tags-overlay (current prefix)
  12369. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12370. (if (featurep 'xemacs)
  12371. (org-overlay-display org-tags-overlay (concat prefix s)
  12372. 'secondary-selection)
  12373. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12374. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12375. (defvar org-last-tag-selection-key nil)
  12376. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12377. "Fast tag selection with single keys.
  12378. CURRENT is the current list of tags in the headline, INHERITED is the
  12379. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12380. possibly with grouping information. TODO-TABLE is a similar table with
  12381. TODO keywords, should these have keys assigned to them.
  12382. If the keys are nil, a-z are automatically assigned.
  12383. Returns the new tags string, or nil to not change the current settings."
  12384. (let* ((fulltable (append table todo-table))
  12385. (maxlen (apply 'max (mapcar
  12386. (lambda (x)
  12387. (if (stringp (car x)) (string-width (car x)) 0))
  12388. fulltable)))
  12389. (buf (current-buffer))
  12390. (expert (eq org-fast-tag-selection-single-key 'expert))
  12391. (buffer-tags nil)
  12392. (fwidth (+ maxlen 3 1 3))
  12393. (ncol (/ (- (window-width) 4) fwidth))
  12394. (i-face 'org-done)
  12395. (c-face 'org-todo)
  12396. tg cnt e c char c1 c2 ntable tbl rtn
  12397. ov-start ov-end ov-prefix
  12398. (exit-after-next org-fast-tag-selection-single-key)
  12399. (done-keywords org-done-keywords)
  12400. groups ingroup)
  12401. (save-excursion
  12402. (beginning-of-line 1)
  12403. (if (looking-at
  12404. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12405. (setq ov-start (match-beginning 1)
  12406. ov-end (match-end 1)
  12407. ov-prefix "")
  12408. (setq ov-start (1- (point-at-eol))
  12409. ov-end (1+ ov-start))
  12410. (skip-chars-forward "^\n\r")
  12411. (setq ov-prefix
  12412. (concat
  12413. (buffer-substring (1- (point)) (point))
  12414. (if (> (current-column) org-tags-column)
  12415. " "
  12416. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12417. (move-overlay org-tags-overlay ov-start ov-end)
  12418. (save-window-excursion
  12419. (if expert
  12420. (set-buffer (get-buffer-create " *Org tags*"))
  12421. (delete-other-windows)
  12422. (split-window-vertically)
  12423. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12424. (erase-buffer)
  12425. (org-set-local 'org-done-keywords done-keywords)
  12426. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12427. (org-fast-tag-insert "Current" current c-face "\n\n")
  12428. (org-fast-tag-show-exit exit-after-next)
  12429. (org-set-current-tags-overlay current ov-prefix)
  12430. (setq tbl fulltable char ?a cnt 0)
  12431. (while (setq e (pop tbl))
  12432. (cond
  12433. ((equal (car e) :startgroup)
  12434. (push '() groups) (setq ingroup t)
  12435. (when (not (= cnt 0))
  12436. (setq cnt 0)
  12437. (insert "\n"))
  12438. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12439. ((equal (car e) :endgroup)
  12440. (setq ingroup nil cnt 0)
  12441. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12442. ((equal e '(:newline))
  12443. (when (not (= cnt 0))
  12444. (setq cnt 0)
  12445. (insert "\n")
  12446. (setq e (car tbl))
  12447. (while (equal (car tbl) '(:newline))
  12448. (insert "\n")
  12449. (setq tbl (cdr tbl)))))
  12450. (t
  12451. (setq tg (copy-sequence (car e)) c2 nil)
  12452. (if (cdr e)
  12453. (setq c (cdr e))
  12454. ;; automatically assign a character.
  12455. (setq c1 (string-to-char
  12456. (downcase (substring
  12457. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12458. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12459. (while (or (rassoc char ntable) (rassoc char table))
  12460. (setq char (1+ char)))
  12461. (setq c2 c1))
  12462. (setq c (or c2 char)))
  12463. (if ingroup (push tg (car groups)))
  12464. (setq tg (org-add-props tg nil 'face
  12465. (cond
  12466. ((not (assoc tg table))
  12467. (org-get-todo-face tg))
  12468. ((member tg current) c-face)
  12469. ((member tg inherited) i-face)
  12470. (t nil))))
  12471. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12472. (insert "[" c "] " tg (make-string
  12473. (- fwidth 4 (length tg)) ?\ ))
  12474. (push (cons tg c) ntable)
  12475. (when (= (setq cnt (1+ cnt)) ncol)
  12476. (insert "\n")
  12477. (if ingroup (insert " "))
  12478. (setq cnt 0)))))
  12479. (setq ntable (nreverse ntable))
  12480. (insert "\n")
  12481. (goto-char (point-min))
  12482. (if (not expert) (org-fit-window-to-buffer))
  12483. (setq rtn
  12484. (catch 'exit
  12485. (while t
  12486. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12487. (if (not groups) "no " "")
  12488. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12489. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12490. (setq org-last-tag-selection-key c)
  12491. (cond
  12492. ((= c ?\r) (throw 'exit t))
  12493. ((= c ?!)
  12494. (setq groups (not groups))
  12495. (goto-char (point-min))
  12496. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12497. ((= c ?\C-c)
  12498. (if (not expert)
  12499. (org-fast-tag-show-exit
  12500. (setq exit-after-next (not exit-after-next)))
  12501. (setq expert nil)
  12502. (delete-other-windows)
  12503. (set-window-buffer (split-window-vertically) " *Org tags*")
  12504. (org-switch-to-buffer-other-window " *Org tags*")
  12505. (org-fit-window-to-buffer)))
  12506. ((or (= c ?\C-g)
  12507. (and (= c ?q) (not (rassoc c ntable))))
  12508. (org-detach-overlay org-tags-overlay)
  12509. (setq quit-flag t))
  12510. ((= c ?\ )
  12511. (setq current nil)
  12512. (if exit-after-next (setq exit-after-next 'now)))
  12513. ((= c ?\t)
  12514. (condition-case nil
  12515. (setq tg (org-icompleting-read
  12516. "Tag: "
  12517. (or buffer-tags
  12518. (with-current-buffer buf
  12519. (org-get-buffer-tags)))))
  12520. (quit (setq tg "")))
  12521. (when (string-match "\\S-" tg)
  12522. (add-to-list 'buffer-tags (list tg))
  12523. (if (member tg current)
  12524. (setq current (delete tg current))
  12525. (push tg current)))
  12526. (if exit-after-next (setq exit-after-next 'now)))
  12527. ((setq e (rassoc c todo-table) tg (car e))
  12528. (with-current-buffer buf
  12529. (save-excursion (org-todo tg)))
  12530. (if exit-after-next (setq exit-after-next 'now)))
  12531. ((setq e (rassoc c ntable) tg (car e))
  12532. (if (member tg current)
  12533. (setq current (delete tg current))
  12534. (loop for g in groups do
  12535. (if (member tg g)
  12536. (mapc (lambda (x)
  12537. (setq current (delete x current)))
  12538. g)))
  12539. (push tg current))
  12540. (if exit-after-next (setq exit-after-next 'now))))
  12541. ;; Create a sorted list
  12542. (setq current
  12543. (sort current
  12544. (lambda (a b)
  12545. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12546. (if (eq exit-after-next 'now) (throw 'exit t))
  12547. (goto-char (point-min))
  12548. (beginning-of-line 2)
  12549. (delete-region (point) (point-at-eol))
  12550. (org-fast-tag-insert "Current" current c-face)
  12551. (org-set-current-tags-overlay current ov-prefix)
  12552. (while (re-search-forward
  12553. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12554. (setq tg (match-string 1))
  12555. (add-text-properties
  12556. (match-beginning 1) (match-end 1)
  12557. (list 'face
  12558. (cond
  12559. ((member tg current) c-face)
  12560. ((member tg inherited) i-face)
  12561. (t (get-text-property (match-beginning 1) 'face))))))
  12562. (goto-char (point-min)))))
  12563. (org-detach-overlay org-tags-overlay)
  12564. (if rtn
  12565. (mapconcat 'identity current ":")
  12566. nil))))
  12567. (defun org-get-tags-string ()
  12568. "Get the TAGS string in the current headline."
  12569. (unless (org-at-heading-p t)
  12570. (error "Not on a heading"))
  12571. (save-excursion
  12572. (beginning-of-line 1)
  12573. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12574. (org-match-string-no-properties 1)
  12575. "")))
  12576. (defun org-get-tags ()
  12577. "Get the list of tags specified in the current headline."
  12578. (org-split-string (org-get-tags-string) ":"))
  12579. (defun org-get-buffer-tags ()
  12580. "Get a table of all tags used in the buffer, for completion."
  12581. (let (tags)
  12582. (save-excursion
  12583. (goto-char (point-min))
  12584. (while (re-search-forward
  12585. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12586. (when (equal (char-after (point-at-bol 0)) ?*)
  12587. (mapc (lambda (x) (add-to-list 'tags x))
  12588. (org-split-string (org-match-string-no-properties 1) ":")))))
  12589. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12590. (mapcar 'list tags)))
  12591. ;;;; The mapping API
  12592. ;;;###autoload
  12593. (defun org-map-entries (func &optional match scope &rest skip)
  12594. "Call FUNC at each headline selected by MATCH in SCOPE.
  12595. FUNC is a function or a lisp form. The function will be called without
  12596. arguments, with the cursor positioned at the beginning of the headline.
  12597. The return values of all calls to the function will be collected and
  12598. returned as a list.
  12599. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12600. does not need to preserve point. After evaluation, the cursor will be
  12601. moved to the end of the line (presumably of the headline of the
  12602. processed entry) and search continues from there. Under some
  12603. circumstances, this may not produce the wanted results. For example,
  12604. if you have removed (e.g. archived) the current (sub)tree it could
  12605. mean that the next entry will be skipped entirely. In such cases, you
  12606. can specify the position from where search should continue by making
  12607. FUNC set the variable `org-map-continue-from' to the desired buffer
  12608. position.
  12609. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12610. Only headlines that are matched by this query will be considered during
  12611. the iteration. When MATCH is nil or t, all headlines will be
  12612. visited by the iteration.
  12613. SCOPE determines the scope of this command. It can be any of:
  12614. nil The current buffer, respecting the restriction if any
  12615. tree The subtree started with the entry at point
  12616. region The entries within the active region, if any
  12617. region-start-level
  12618. The entries within the active region, but only those at
  12619. the same level than the first one.
  12620. file The current buffer, without restriction
  12621. file-with-archives
  12622. The current buffer, and any archives associated with it
  12623. agenda All agenda files
  12624. agenda-with-archives
  12625. All agenda files with any archive files associated with them
  12626. \(file1 file2 ...)
  12627. If this is a list, all files in the list will be scanned
  12628. The remaining args are treated as settings for the skipping facilities of
  12629. the scanner. The following items can be given here:
  12630. archive skip trees with the archive tag.
  12631. comment skip trees with the COMMENT keyword
  12632. function or Emacs Lisp form:
  12633. will be used as value for `org-agenda-skip-function', so whenever
  12634. the function returns t, FUNC will not be called for that
  12635. entry and search will continue from the point where the
  12636. function leaves it.
  12637. If your function needs to retrieve the tags including inherited tags
  12638. at the *current* entry, you can use the value of the variable
  12639. `org-scanner-tags' which will be much faster than getting the value
  12640. with `org-get-tags-at'. If your function gets properties with
  12641. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12642. to t around the call to `org-entry-properties' to get the same speedup.
  12643. Note that if your function moves around to retrieve tags and properties at
  12644. a *different* entry, you cannot use these techniques."
  12645. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12646. (not (org-region-active-p)))
  12647. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12648. (org-agenda-skip-archived-trees (memq 'archive skip))
  12649. (org-agenda-skip-comment-trees (memq 'comment skip))
  12650. (org-agenda-skip-function
  12651. (car (org-delete-all '(comment archive) skip)))
  12652. (org-tags-match-list-sublevels t)
  12653. (start-level (eq scope 'region-start-level))
  12654. matcher file res
  12655. org-todo-keywords-for-agenda
  12656. org-done-keywords-for-agenda
  12657. org-todo-keyword-alist-for-agenda
  12658. org-drawers-for-agenda
  12659. org-tag-alist-for-agenda)
  12660. (cond
  12661. ((eq match t) (setq matcher t))
  12662. ((eq match nil) (setq matcher t))
  12663. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12664. (save-excursion
  12665. (save-restriction
  12666. (cond ((eq scope 'tree)
  12667. (org-back-to-heading t)
  12668. (org-narrow-to-subtree)
  12669. (setq scope nil))
  12670. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12671. (org-region-active-p))
  12672. ;; If needed, set start-level to a string like "2"
  12673. (when start-level
  12674. (save-excursion
  12675. (goto-char (region-beginning))
  12676. (unless (org-at-heading-p) (outline-next-heading))
  12677. (setq start-level (org-current-level))))
  12678. (narrow-to-region (region-beginning)
  12679. (save-excursion
  12680. (goto-char (region-end))
  12681. (unless (and (bolp) (org-at-heading-p))
  12682. (outline-next-heading))
  12683. (point)))
  12684. (setq scope nil)))
  12685. (if (not scope)
  12686. (progn
  12687. (org-prepare-agenda-buffers
  12688. (list (buffer-file-name (current-buffer))))
  12689. (setq res (org-scan-tags func matcher nil start-level)))
  12690. ;; Get the right scope
  12691. (cond
  12692. ((and scope (listp scope) (symbolp (car scope)))
  12693. (setq scope (eval scope)))
  12694. ((eq scope 'agenda)
  12695. (setq scope (org-agenda-files t)))
  12696. ((eq scope 'agenda-with-archives)
  12697. (setq scope (org-agenda-files t))
  12698. (setq scope (org-add-archive-files scope)))
  12699. ((eq scope 'file)
  12700. (setq scope (list (buffer-file-name))))
  12701. ((eq scope 'file-with-archives)
  12702. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12703. (org-prepare-agenda-buffers scope)
  12704. (while (setq file (pop scope))
  12705. (with-current-buffer (org-find-base-buffer-visiting file)
  12706. (save-excursion
  12707. (save-restriction
  12708. (widen)
  12709. (goto-char (point-min))
  12710. (setq res (append res (org-scan-tags func matcher))))))))))
  12711. res)))
  12712. ;;;; Properties
  12713. ;;; Setting and retrieving properties
  12714. (defconst org-special-properties
  12715. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12716. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12717. "The special properties valid in Org-mode.
  12718. These are properties that are not defined in the property drawer,
  12719. but in some other way.")
  12720. (defconst org-default-properties
  12721. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12722. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12723. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12724. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12725. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12726. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12727. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12728. "Some properties that are used by Org-mode for various purposes.
  12729. Being in this list makes sure that they are offered for completion.")
  12730. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12731. "Regular expression matching the first line of a property drawer.")
  12732. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12733. "Regular expression matching the last line of a property drawer.")
  12734. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12735. "Regular expression matching the first line of a property drawer.")
  12736. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12737. "Regular expression matching the first line of a property drawer.")
  12738. (defconst org-property-drawer-re
  12739. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12740. org-property-end-re "\\)\n?")
  12741. "Matches an entire property drawer.")
  12742. (defconst org-clock-drawer-re
  12743. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12744. org-property-end-re "\\)\n?")
  12745. "Matches an entire clock drawer.")
  12746. (defsubst org-re-property (property)
  12747. "Return a regexp matching PROPERTY.
  12748. Match group 1 will be set to the value "
  12749. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12750. (defun org-property-action ()
  12751. "Do an action on properties."
  12752. (interactive)
  12753. (let (c)
  12754. (org-at-property-p)
  12755. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12756. (setq c (read-char-exclusive))
  12757. (cond
  12758. ((equal c ?s)
  12759. (call-interactively 'org-set-property))
  12760. ((equal c ?d)
  12761. (call-interactively 'org-delete-property))
  12762. ((equal c ?D)
  12763. (call-interactively 'org-delete-property-globally))
  12764. ((equal c ?c)
  12765. (call-interactively 'org-compute-property-at-point))
  12766. (t (error "No such property action %c" c)))))
  12767. (defun org-set-effort (&optional value)
  12768. "Set the effort property of the current entry.
  12769. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12770. allowed value."
  12771. (interactive "P")
  12772. (if (equal value 0) (setq value 10))
  12773. (let* ((completion-ignore-case t)
  12774. (prop org-effort-property)
  12775. (cur (org-entry-get nil prop))
  12776. (allowed (org-property-get-allowed-values nil prop 'table))
  12777. (existing (mapcar 'list (org-property-values prop)))
  12778. rpl
  12779. (val (cond
  12780. ((stringp value) value)
  12781. ((and allowed (integerp value))
  12782. (or (car (nth (1- value) allowed))
  12783. (car (org-last allowed))))
  12784. (allowed
  12785. (message "Select 1-9,0, [RET%s]: %s"
  12786. (if cur (concat "=" cur) "")
  12787. (mapconcat 'car allowed " "))
  12788. (setq rpl (read-char-exclusive))
  12789. (if (equal rpl ?\r)
  12790. cur
  12791. (setq rpl (- rpl ?0))
  12792. (if (equal rpl 0) (setq rpl 10))
  12793. (if (and (> rpl 0) (<= rpl (length allowed)))
  12794. (car (nth (1- rpl) allowed))
  12795. (org-completing-read "Effort: " allowed nil))))
  12796. (t
  12797. (let (org-completion-use-ido org-completion-use-iswitchb)
  12798. (org-completing-read
  12799. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12800. (concat "[" cur "]") "")
  12801. ": ")
  12802. existing nil nil "" nil cur))))))
  12803. (unless (equal (org-entry-get nil prop) val)
  12804. (org-entry-put nil prop val))
  12805. (message "%s is now %s" prop val)))
  12806. (defun org-at-property-p ()
  12807. "Is cursor inside a property drawer?"
  12808. (save-excursion
  12809. (beginning-of-line 1)
  12810. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12811. (save-match-data ;; Used by calling procedures
  12812. (let ((p (point))
  12813. (range (unless (org-before-first-heading-p)
  12814. (org-get-property-block))))
  12815. (and range (<= (car range) p) (< p (cdr range))))))))
  12816. (defun org-get-property-block (&optional beg end force)
  12817. "Return the (beg . end) range of the body of the property drawer.
  12818. BEG and END can be beginning and end of subtree, if not given
  12819. they will be found.
  12820. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12821. (catch 'exit
  12822. (save-excursion
  12823. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12824. (end (or end (progn (outline-next-heading) (point)))))
  12825. (goto-char beg)
  12826. (if (re-search-forward org-property-start-re end t)
  12827. (setq beg (1+ (match-end 0)))
  12828. (if force
  12829. (save-excursion
  12830. (org-insert-property-drawer)
  12831. (setq end (progn (outline-next-heading) (point))))
  12832. (throw 'exit nil))
  12833. (goto-char beg)
  12834. (if (re-search-forward org-property-start-re end t)
  12835. (setq beg (1+ (match-end 0)))))
  12836. (if (re-search-forward org-property-end-re end t)
  12837. (setq end (match-beginning 0))
  12838. (or force (throw 'exit nil))
  12839. (goto-char beg)
  12840. (setq end beg)
  12841. (org-indent-line-function)
  12842. (insert ":END:\n"))
  12843. (cons beg end)))))
  12844. (defun org-entry-properties (&optional pom which specific)
  12845. "Get all properties of the entry at point-or-marker POM.
  12846. This includes the TODO keyword, the tags, time strings for deadline,
  12847. scheduled, and clocking, and any additional properties defined in the
  12848. entry. The return value is an alist, keys may occur multiple times
  12849. if the property key was used several times.
  12850. POM may also be nil, in which case the current entry is used.
  12851. If WHICH is nil or `all', get all properties. If WHICH is
  12852. `special' or `standard', only get that subclass. If WHICH
  12853. is a string only get exactly this property. SPECIFIC can be a string, the
  12854. specific property we are interested in. Specifying it can speed
  12855. things up because then unnecessary parsing is avoided."
  12856. (setq which (or which 'all))
  12857. (org-with-point-at pom
  12858. (let ((clockstr (substring org-clock-string 0 -1))
  12859. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12860. (case-fold-search nil)
  12861. beg end range props sum-props key key1 value string clocksum)
  12862. (save-excursion
  12863. (when (condition-case nil
  12864. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12865. (error nil))
  12866. (setq beg (point))
  12867. (setq sum-props (get-text-property (point) 'org-summaries))
  12868. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12869. (outline-next-heading)
  12870. (setq end (point))
  12871. (when (memq which '(all special))
  12872. ;; Get the special properties, like TODO and tags
  12873. (goto-char beg)
  12874. (when (and (or (not specific) (string= specific "TODO"))
  12875. (looking-at org-todo-line-regexp) (match-end 2))
  12876. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12877. (when (and (or (not specific) (string= specific "PRIORITY"))
  12878. (looking-at org-priority-regexp))
  12879. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12880. (when (or (not specific) (string= specific "FILE"))
  12881. (push (cons "FILE" buffer-file-name) props))
  12882. (when (and (or (not specific) (string= specific "TAGS"))
  12883. (setq value (org-get-tags-string))
  12884. (string-match "\\S-" value))
  12885. (push (cons "TAGS" value) props))
  12886. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12887. (setq value (org-get-tags-at)))
  12888. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12889. ":"))
  12890. props))
  12891. (when (or (not specific) (string= specific "BLOCKED"))
  12892. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12893. (when (or (not specific)
  12894. (member specific
  12895. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12896. "TIMESTAMP" "TIMESTAMP_IA")))
  12897. (catch 'match
  12898. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12899. (setq key (if (match-end 1)
  12900. (substring (org-match-string-no-properties 1)
  12901. 0 -1))
  12902. string (if (equal key clockstr)
  12903. (org-no-properties
  12904. (org-trim
  12905. (buffer-substring
  12906. (match-beginning 3) (goto-char
  12907. (point-at-eol)))))
  12908. (substring (org-match-string-no-properties 3)
  12909. 1 -1)))
  12910. ;; Get the correct property name from the key. This is
  12911. ;; necessary if the user has configured time keywords.
  12912. (setq key1 (concat key ":"))
  12913. (cond
  12914. ((not key)
  12915. (setq key
  12916. (if (= (char-after (match-beginning 3)) ?\[)
  12917. "TIMESTAMP_IA" "TIMESTAMP")))
  12918. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12919. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12920. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12921. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12922. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12923. (progn
  12924. (push (cons key string) props)
  12925. ;; no need to search further if match is found
  12926. (throw 'match t))
  12927. (when (or (equal key "CLOCK") (not (assoc key props)))
  12928. (push (cons key string) props))))))
  12929. )
  12930. (when (memq which '(all standard))
  12931. ;; Get the standard properties, like :PROP: ...
  12932. (setq range (org-get-property-block beg end))
  12933. (when range
  12934. (goto-char (car range))
  12935. (while (re-search-forward
  12936. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12937. (cdr range) t)
  12938. (setq key (org-match-string-no-properties 1)
  12939. value (org-trim (or (org-match-string-no-properties 2) "")))
  12940. (unless (member key excluded)
  12941. (push (cons key (or value "")) props)))))
  12942. (if clocksum
  12943. (push (cons "CLOCKSUM"
  12944. (org-columns-number-to-string (/ (float clocksum) 60.)
  12945. 'add_times))
  12946. props))
  12947. (unless (assoc "CATEGORY" props)
  12948. (push (cons "CATEGORY" (org-get-category)) props))
  12949. (append sum-props (nreverse props)))))))
  12950. (defun org-entry-get (pom property &optional inherit literal-nil)
  12951. "Get value of PROPERTY for entry at point-or-marker POM.
  12952. If INHERIT is non-nil and the entry does not have the property,
  12953. then also check higher levels of the hierarchy.
  12954. If INHERIT is the symbol `selective', use inheritance only if the setting
  12955. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12956. If the property is present but empty, the return value is the empty string.
  12957. If the property is not present at all, nil is returned.
  12958. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12959. do not interpret it as the list atom nil. This is used for inheritance
  12960. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12961. (org-with-point-at pom
  12962. (if (and inherit (if (eq inherit 'selective)
  12963. (org-property-inherit-p property)
  12964. t))
  12965. (org-entry-get-with-inheritance property literal-nil)
  12966. (if (member property org-special-properties)
  12967. ;; We need a special property. Use `org-entry-properties' to
  12968. ;; retrieve it, but specify the wanted property
  12969. (cdr (assoc property (org-entry-properties nil 'special property)))
  12970. (let ((range (unless (org-before-first-heading-p)
  12971. (org-get-property-block)))
  12972. (props (list (or (assoc property org-file-properties)
  12973. (assoc property org-global-properties)
  12974. (assoc property org-global-properties-fixed))))
  12975. val)
  12976. (flet ((ap (key)
  12977. (when (re-search-forward
  12978. (org-re-property key) (cdr range) t)
  12979. (setq props
  12980. (org-update-property-plist
  12981. key
  12982. (if (match-end 1)
  12983. (org-match-string-no-properties 1) "")
  12984. props)))))
  12985. (when (and range (goto-char (car range)))
  12986. (ap property)
  12987. (goto-char (car range))
  12988. (while (ap (concat property "+")))
  12989. (setq val (cdr (assoc property props)))
  12990. (when val (if literal-nil val (org-not-nil val))))))))))
  12991. (defun org-property-or-variable-value (var &optional inherit)
  12992. "Check if there is a property fixing the value of VAR.
  12993. If yes, return this value. If not, return the current value of the variable."
  12994. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12995. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12996. (read prop)
  12997. (symbol-value var))))
  12998. (defun org-entry-delete (pom property)
  12999. "Delete the property PROPERTY from entry at point-or-marker POM."
  13000. (org-with-point-at pom
  13001. (if (member property org-special-properties)
  13002. nil ; cannot delete these properties.
  13003. (let ((range (org-get-property-block)))
  13004. (if (and range
  13005. (goto-char (car range))
  13006. (re-search-forward
  13007. (org-re-property property)
  13008. (cdr range) t))
  13009. (progn
  13010. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13011. t)
  13012. nil)))))
  13013. ;; Multi-values properties are properties that contain multiple values
  13014. ;; These values are assumed to be single words, separated by whitespace.
  13015. (defun org-entry-add-to-multivalued-property (pom property value)
  13016. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13017. (let* ((old (org-entry-get pom property))
  13018. (values (and old (org-split-string old "[ \t]"))))
  13019. (setq value (org-entry-protect-space value))
  13020. (unless (member value values)
  13021. (setq values (cons value values))
  13022. (org-entry-put pom property
  13023. (mapconcat 'identity values " ")))))
  13024. (defun org-entry-remove-from-multivalued-property (pom property value)
  13025. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13026. (let* ((old (org-entry-get pom property))
  13027. (values (and old (org-split-string old "[ \t]"))))
  13028. (setq value (org-entry-protect-space value))
  13029. (when (member value values)
  13030. (setq values (delete value values))
  13031. (org-entry-put pom property
  13032. (mapconcat 'identity values " ")))))
  13033. (defun org-entry-member-in-multivalued-property (pom property value)
  13034. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13035. (let* ((old (org-entry-get pom property))
  13036. (values (and old (org-split-string old "[ \t]"))))
  13037. (setq value (org-entry-protect-space value))
  13038. (member value values)))
  13039. (defun org-entry-get-multivalued-property (pom property)
  13040. "Return a list of values in a multivalued property."
  13041. (let* ((value (org-entry-get pom property))
  13042. (values (and value (org-split-string value "[ \t]"))))
  13043. (mapcar 'org-entry-restore-space values)))
  13044. (defun org-entry-put-multivalued-property (pom property &rest values)
  13045. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13046. VALUES should be a list of strings. Spaces will be protected."
  13047. (org-entry-put pom property
  13048. (mapconcat 'org-entry-protect-space values " "))
  13049. (let* ((value (org-entry-get pom property))
  13050. (values (and value (org-split-string value "[ \t]"))))
  13051. (mapcar 'org-entry-restore-space values)))
  13052. (defun org-entry-protect-space (s)
  13053. "Protect spaces and newline in string S."
  13054. (while (string-match " " s)
  13055. (setq s (replace-match "%20" t t s)))
  13056. (while (string-match "\n" s)
  13057. (setq s (replace-match "%0A" t t s)))
  13058. s)
  13059. (defun org-entry-restore-space (s)
  13060. "Restore spaces and newline in string S."
  13061. (while (string-match "%20" s)
  13062. (setq s (replace-match " " t t s)))
  13063. (while (string-match "%0A" s)
  13064. (setq s (replace-match "\n" t t s)))
  13065. s)
  13066. (defvar org-entry-property-inherited-from (make-marker)
  13067. "Marker pointing to the entry from where a property was inherited.
  13068. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13069. location of the entry where the inheritance search matched. If there was
  13070. no match, the marker will point nowhere.
  13071. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13072. is set.")
  13073. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13074. "Get entry property, and search higher levels if not present.
  13075. The search will stop at the first ancestor which has the property defined.
  13076. If the value found is \"nil\", return nil to show that the property
  13077. should be considered as undefined (this is the meaning of nil here).
  13078. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13079. (move-marker org-entry-property-inherited-from nil)
  13080. (let (tmp)
  13081. (unless (org-before-first-heading-p)
  13082. (save-excursion
  13083. (save-restriction
  13084. (widen)
  13085. (catch 'ex
  13086. (while t
  13087. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13088. (org-back-to-heading t)
  13089. (move-marker org-entry-property-inherited-from (point))
  13090. (throw 'ex tmp))
  13091. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13092. (setq tmp (or tmp
  13093. (cdr (assoc property org-file-properties))
  13094. (cdr (assoc property org-global-properties))
  13095. (cdr (assoc property org-global-properties-fixed))))
  13096. (if literal-nil tmp (org-not-nil tmp))))
  13097. (defvar org-property-changed-functions nil
  13098. "Hook called when the value of a property has changed.
  13099. Each hook function should accept two arguments, the name of the property
  13100. and the new value.")
  13101. (defun org-entry-put (pom property value)
  13102. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13103. (org-with-point-at pom
  13104. (org-back-to-heading t)
  13105. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13106. range)
  13107. (cond
  13108. ((equal property "TODO")
  13109. (when (and (stringp value) (string-match "\\S-" value)
  13110. (not (member value org-todo-keywords-1)))
  13111. (error "\"%s\" is not a valid TODO state" value))
  13112. (if (or (not value)
  13113. (not (string-match "\\S-" value)))
  13114. (setq value 'none))
  13115. (org-todo value)
  13116. (org-set-tags nil 'align))
  13117. ((equal property "PRIORITY")
  13118. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13119. (string-to-char value) ?\ ))
  13120. (org-set-tags nil 'align))
  13121. ((equal property "SCHEDULED")
  13122. (if (re-search-forward org-scheduled-time-regexp end t)
  13123. (cond
  13124. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13125. ((eq value 'later) (org-timestamp-change 1 'day))
  13126. (t (call-interactively 'org-schedule)))
  13127. (call-interactively 'org-schedule)))
  13128. ((equal property "DEADLINE")
  13129. (if (re-search-forward org-deadline-time-regexp end t)
  13130. (cond
  13131. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13132. ((eq value 'later) (org-timestamp-change 1 'day))
  13133. (t (call-interactively 'org-deadline)))
  13134. (call-interactively 'org-deadline)))
  13135. ((member property org-special-properties)
  13136. (error "The %s property can not yet be set with `org-entry-put'"
  13137. property))
  13138. (t ; a non-special property
  13139. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13140. (setq range (org-get-property-block beg end 'force))
  13141. (goto-char (car range))
  13142. (if (re-search-forward
  13143. (org-re-property property) (cdr range) t)
  13144. (progn
  13145. (delete-region (match-beginning 0) (match-end 0))
  13146. (goto-char (match-beginning 0)))
  13147. (goto-char (cdr range))
  13148. (insert "\n")
  13149. (backward-char 1)
  13150. (org-indent-line-function))
  13151. (insert ":" property ":")
  13152. (and value (insert " " value))
  13153. (org-indent-line-function)))))
  13154. (run-hook-with-args 'org-property-changed-functions property value)))
  13155. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13156. "Get all property keys in the current buffer.
  13157. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13158. like tags and TODO state.
  13159. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13160. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13161. and others.
  13162. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13163. formats in the current buffer."
  13164. (let (rtn range cfmt s p)
  13165. (save-excursion
  13166. (save-restriction
  13167. (widen)
  13168. (goto-char (point-min))
  13169. (while (re-search-forward org-property-start-re nil t)
  13170. (setq range (org-get-property-block))
  13171. (goto-char (car range))
  13172. (while (re-search-forward
  13173. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13174. (cdr range) t)
  13175. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13176. (outline-next-heading))))
  13177. (when include-specials
  13178. (setq rtn (append org-special-properties rtn)))
  13179. (when include-defaults
  13180. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13181. (add-to-list 'rtn org-effort-property))
  13182. (when include-columns
  13183. (save-excursion
  13184. (save-restriction
  13185. (widen)
  13186. (goto-char (point-min))
  13187. (while (re-search-forward
  13188. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13189. nil t)
  13190. (setq cfmt (match-string 2) s 0)
  13191. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13192. cfmt s)
  13193. (setq s (match-end 0)
  13194. p (match-string 1 cfmt))
  13195. (unless (or (equal p "ITEM")
  13196. (member p org-special-properties))
  13197. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13198. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13199. (defun org-property-values (key)
  13200. "Return a list of all values of property KEY in the current buffer."
  13201. (save-excursion
  13202. (save-restriction
  13203. (widen)
  13204. (goto-char (point-min))
  13205. (let ((re (org-re-property key))
  13206. values)
  13207. (while (re-search-forward re nil t)
  13208. (add-to-list 'values (org-trim (match-string 1))))
  13209. (delete "" values)))))
  13210. (defun org-insert-property-drawer ()
  13211. "Insert a property drawer into the current entry."
  13212. (org-back-to-heading t)
  13213. (looking-at org-outline-regexp)
  13214. (let ((indent (if org-adapt-indentation
  13215. (- (match-end 0) (match-beginning 0))
  13216. 0))
  13217. (beg (point))
  13218. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13219. end hiddenp)
  13220. (outline-next-heading)
  13221. (setq end (point))
  13222. (goto-char beg)
  13223. (while (re-search-forward re end t))
  13224. (setq hiddenp (outline-invisible-p))
  13225. (end-of-line 1)
  13226. (and (equal (char-after) ?\n) (forward-char 1))
  13227. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13228. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13229. ;; just skip this line
  13230. (beginning-of-line 2)
  13231. ;; Drawer start, find the end
  13232. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13233. (beginning-of-line 1)))
  13234. (org-skip-over-state-notes)
  13235. (skip-chars-backward " \t\n\r")
  13236. (if (eq (char-before) ?*) (forward-char 1))
  13237. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13238. (beginning-of-line 0)
  13239. (org-indent-to-column indent)
  13240. (beginning-of-line 2)
  13241. (org-indent-to-column indent)
  13242. (beginning-of-line 0)
  13243. (if hiddenp
  13244. (save-excursion
  13245. (org-back-to-heading t)
  13246. (hide-entry))
  13247. (org-flag-drawer t))))
  13248. (defun org-insert-drawer (&optional arg drawer)
  13249. "Insert a drawer at point.
  13250. Optional argument DRAWER, when non-nil, is a string representing
  13251. drawer's name. Otherwise, the user is prompted for a name.
  13252. If a region is active, insert the drawer around that region
  13253. instead.
  13254. Point is left between drawer's boundaries."
  13255. (interactive "P")
  13256. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13257. "LOGBOOK"))
  13258. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13259. ;; internally by Org. They are meant to be inserted
  13260. ;; automatically.
  13261. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13262. ;; Remove system drawers from list. Note: For some reason,
  13263. ;; `org-completing-read' ignores the predicate while
  13264. ;; `completing-read' handles it fine.
  13265. (drawer (if arg "PROPERTIES"
  13266. (or drawer
  13267. (completing-read
  13268. "Drawer: " org-drawers
  13269. (lambda (d) (not (member d system-drawers))))))))
  13270. (cond
  13271. ;; With C-u, fall back on `org-insert-property-drawer'
  13272. (arg (org-insert-property-drawer))
  13273. ;; With an active region, insert a drawer at point.
  13274. ((not (org-region-active-p))
  13275. (progn
  13276. (unless (bolp) (insert "\n"))
  13277. (insert (format ":%s:\n\n:END:\n" drawer))
  13278. (forward-line -2)))
  13279. ;; Otherwise, insert the drawer at point
  13280. (t
  13281. (let ((rbeg (region-beginning))
  13282. (rend (copy-marker (region-end))))
  13283. (unwind-protect
  13284. (progn
  13285. (goto-char rbeg)
  13286. (beginning-of-line)
  13287. (when (save-excursion
  13288. (re-search-forward org-outline-regexp-bol rend t))
  13289. (error "Drawers cannot contain headlines"))
  13290. ;; Position point at the beginning of the first
  13291. ;; non-blank line in region. Insert drawer's opening
  13292. ;; there, then indent it.
  13293. (org-skip-whitespace)
  13294. (beginning-of-line)
  13295. (insert ":" drawer ":\n")
  13296. (forward-line -1)
  13297. (indent-for-tab-command)
  13298. ;; Move point to the beginning of the first blank line
  13299. ;; after the last non-blank line in region. Insert
  13300. ;; drawer's closing, then indent it.
  13301. (goto-char rend)
  13302. (skip-chars-backward " \r\t\n")
  13303. (insert "\n:END:")
  13304. (indent-for-tab-command)
  13305. (unless (eolp) (insert "\n")))
  13306. ;; Clear marker, whatever the outcome of insertion is.
  13307. (set-marker rend nil)))))))
  13308. (defvar org-property-set-functions-alist nil
  13309. "Property set function alist.
  13310. Each entry should have the following format:
  13311. (PROPERTY . READ-FUNCTION)
  13312. The read function will be called with the same argument as
  13313. `org-completing-read'.")
  13314. (defun org-set-property-function (property)
  13315. "Get the function that should be used to set PROPERTY.
  13316. This is computed according to `org-property-set-functions-alist'."
  13317. (or (cdr (assoc property org-property-set-functions-alist))
  13318. 'org-completing-read))
  13319. (defun org-read-property-value (property)
  13320. "Read PROPERTY value from user."
  13321. (let* ((completion-ignore-case t)
  13322. (allowed (org-property-get-allowed-values nil property 'table))
  13323. (cur (org-entry-get nil property))
  13324. (prompt (concat property " value"
  13325. (if (and cur (string-match "\\S-" cur))
  13326. (concat " [" cur "]") "") ": "))
  13327. (set-function (org-set-property-function property))
  13328. (val (if allowed
  13329. (funcall set-function prompt allowed nil
  13330. (not (get-text-property 0 'org-unrestricted
  13331. (caar allowed))))
  13332. (let (org-completion-use-ido org-completion-use-iswitchb)
  13333. (funcall set-function prompt
  13334. (mapcar 'list (org-property-values property))
  13335. nil nil "" nil cur)))))
  13336. (if (equal val "")
  13337. cur
  13338. val)))
  13339. (defvar org-last-set-property nil)
  13340. (defun org-read-property-name ()
  13341. "Read a property name."
  13342. (let* ((completion-ignore-case t)
  13343. (keys (org-buffer-property-keys nil t t))
  13344. (default-prop (or (save-excursion
  13345. (save-match-data
  13346. (beginning-of-line)
  13347. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13348. (null (string= (match-string 1) "END"))
  13349. (match-string 1))))
  13350. org-last-set-property))
  13351. (property (org-icompleting-read
  13352. (concat "Property"
  13353. (if default-prop (concat " [" default-prop "]") "")
  13354. ": ")
  13355. (mapcar 'list keys)
  13356. nil nil nil nil
  13357. default-prop
  13358. )))
  13359. (if (member property keys)
  13360. property
  13361. (or (cdr (assoc (downcase property)
  13362. (mapcar (lambda (x) (cons (downcase x) x))
  13363. keys)))
  13364. property))))
  13365. (defun org-set-property (property value)
  13366. "In the current entry, set PROPERTY to VALUE.
  13367. When called interactively, this will prompt for a property name, offering
  13368. completion on existing and default properties. And then it will prompt
  13369. for a value, offering completion either on allowed values (via an inherited
  13370. xxx_ALL property) or on existing values in other instances of this property
  13371. in the current file."
  13372. (interactive (list nil nil))
  13373. (let* ((property (or property (org-read-property-name)))
  13374. (value (or value (org-read-property-value property)))
  13375. (fn (assoc property org-properties-postprocess-alist)))
  13376. (setq org-last-set-property property)
  13377. ;; Possibly postprocess the inserted value:
  13378. (when fn (setq value (funcall (cadr fn) value)))
  13379. (unless (equal (org-entry-get nil property) value)
  13380. (org-entry-put nil property value))))
  13381. (defun org-delete-property (property)
  13382. "In the current entry, delete PROPERTY."
  13383. (interactive
  13384. (let* ((completion-ignore-case t)
  13385. (prop (org-icompleting-read "Property: "
  13386. (org-entry-properties nil 'standard))))
  13387. (list prop)))
  13388. (message "Property %s %s" property
  13389. (if (org-entry-delete nil property)
  13390. "deleted"
  13391. "was not present in the entry")))
  13392. (defun org-delete-property-globally (property)
  13393. "Remove PROPERTY globally, from all entries."
  13394. (interactive
  13395. (let* ((completion-ignore-case t)
  13396. (prop (org-icompleting-read
  13397. "Globally remove property: "
  13398. (mapcar 'list (org-buffer-property-keys)))))
  13399. (list prop)))
  13400. (save-excursion
  13401. (save-restriction
  13402. (widen)
  13403. (goto-char (point-min))
  13404. (let ((cnt 0))
  13405. (while (re-search-forward
  13406. (org-re-property property)
  13407. nil t)
  13408. (setq cnt (1+ cnt))
  13409. (replace-match ""))
  13410. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13411. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13412. (defun org-compute-property-at-point ()
  13413. "Compute the property at point.
  13414. This looks for an enclosing column format, extracts the operator and
  13415. then applies it to the property in the column format's scope."
  13416. (interactive)
  13417. (unless (org-at-property-p)
  13418. (error "Not at a property"))
  13419. (let ((prop (org-match-string-no-properties 2)))
  13420. (org-columns-get-format-and-top-level)
  13421. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13422. (error "No operator defined for property %s" prop))
  13423. (org-columns-compute prop)))
  13424. (defvar org-property-allowed-value-functions nil
  13425. "Hook for functions supplying allowed values for a specific property.
  13426. The functions must take a single argument, the name of the property, and
  13427. return a flat list of allowed values. If \":ETC\" is one of
  13428. the values, this means that these values are intended as defaults for
  13429. completion, but that other values should be allowed too.
  13430. The functions must return nil if they are not responsible for this
  13431. property.")
  13432. (defun org-property-get-allowed-values (pom property &optional table)
  13433. "Get allowed values for the property PROPERTY.
  13434. When TABLE is non-nil, return an alist that can directly be used for
  13435. completion."
  13436. (let (vals)
  13437. (cond
  13438. ((equal property "TODO")
  13439. (setq vals (org-with-point-at pom
  13440. (append org-todo-keywords-1 '("")))))
  13441. ((equal property "PRIORITY")
  13442. (let ((n org-lowest-priority))
  13443. (while (>= n org-highest-priority)
  13444. (push (char-to-string n) vals)
  13445. (setq n (1- n)))))
  13446. ((member property org-special-properties))
  13447. ((setq vals (run-hook-with-args-until-success
  13448. 'org-property-allowed-value-functions property)))
  13449. (t
  13450. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13451. (when (and vals (string-match "\\S-" vals))
  13452. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13453. (setq vals (mapcar (lambda (x)
  13454. (cond ((stringp x) x)
  13455. ((numberp x) (number-to-string x))
  13456. ((symbolp x) (symbol-name x))
  13457. (t "???")))
  13458. vals)))))
  13459. (when (member ":ETC" vals)
  13460. (setq vals (remove ":ETC" vals))
  13461. (org-add-props (car vals) '(org-unrestricted t)))
  13462. (if table (mapcar 'list vals) vals)))
  13463. (defun org-property-previous-allowed-value (&optional previous)
  13464. "Switch to the next allowed value for this property."
  13465. (interactive)
  13466. (org-property-next-allowed-value t))
  13467. (defun org-property-next-allowed-value (&optional previous)
  13468. "Switch to the next allowed value for this property."
  13469. (interactive)
  13470. (unless (org-at-property-p)
  13471. (error "Not at a property"))
  13472. (let* ((key (match-string 2))
  13473. (value (match-string 3))
  13474. (allowed (or (org-property-get-allowed-values (point) key)
  13475. (and (member value '("[ ]" "[-]" "[X]"))
  13476. '("[ ]" "[X]"))))
  13477. nval)
  13478. (unless allowed
  13479. (error "Allowed values for this property have not been defined"))
  13480. (if previous (setq allowed (reverse allowed)))
  13481. (if (member value allowed)
  13482. (setq nval (car (cdr (member value allowed)))))
  13483. (setq nval (or nval (car allowed)))
  13484. (if (equal nval value)
  13485. (error "Only one allowed value for this property"))
  13486. (org-at-property-p)
  13487. (replace-match (concat " :" key ": " nval) t t)
  13488. (org-indent-line-function)
  13489. (beginning-of-line 1)
  13490. (skip-chars-forward " \t")
  13491. (run-hook-with-args 'org-property-changed-functions key nval)))
  13492. (defun org-find-olp (path &optional this-buffer)
  13493. "Return a marker pointing to the entry at outline path OLP.
  13494. If anything goes wrong, throw an error.
  13495. You can wrap this call to catch the error like this:
  13496. (condition-case msg
  13497. (org-mobile-locate-entry (match-string 4))
  13498. (error (nth 1 msg)))
  13499. The return value will then be either a string with the error message,
  13500. or a marker if everything is OK.
  13501. If THIS-BUFFER is set, the outline path does not contain a file,
  13502. only headings."
  13503. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13504. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13505. (level 1)
  13506. (lmin 1)
  13507. (lmax 1)
  13508. limit re end found pos heading cnt flevel)
  13509. (unless buffer (error "File not found :%s" file))
  13510. (with-current-buffer buffer
  13511. (save-excursion
  13512. (save-restriction
  13513. (widen)
  13514. (setq limit (point-max))
  13515. (goto-char (point-min))
  13516. (while (setq heading (pop path))
  13517. (setq re (format org-complex-heading-regexp-format
  13518. (regexp-quote heading)))
  13519. (setq cnt 0 pos (point))
  13520. (while (re-search-forward re end t)
  13521. (setq level (- (match-end 1) (match-beginning 1)))
  13522. (if (and (>= level lmin) (<= level lmax))
  13523. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13524. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13525. lmax heading))
  13526. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13527. lmax heading))
  13528. (goto-char found)
  13529. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13530. (setq end (save-excursion (org-end-of-subtree t t))))
  13531. (when (org-at-heading-p)
  13532. (move-marker (make-marker) (point))))))))
  13533. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13534. "Find node HEADING in BUFFER.
  13535. Return a marker to the heading if it was found, or nil if not.
  13536. If POS-ONLY is set, return just the position instead of a marker.
  13537. The heading text must match exact, but it may have a TODO keyword,
  13538. a priority cookie and tags in the standard locations."
  13539. (with-current-buffer (or buffer (current-buffer))
  13540. (save-excursion
  13541. (save-restriction
  13542. (widen)
  13543. (goto-char (point-min))
  13544. (let (case-fold-search)
  13545. (if (re-search-forward
  13546. (format org-complex-heading-regexp-format
  13547. (regexp-quote heading)) nil t)
  13548. (if pos-only
  13549. (match-beginning 0)
  13550. (move-marker (make-marker) (match-beginning 0)))))))))
  13551. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13552. "Find Org node headline HEADING in all .org files in directory DIR.
  13553. When the target headline is found, return a marker to this location."
  13554. (let ((files (directory-files (or dir default-directory)
  13555. nil "\\`[^.#].*\\.org\\'"))
  13556. file visiting m buffer)
  13557. (catch 'found
  13558. (while (setq file (pop files))
  13559. (message "trying %s" file)
  13560. (setq visiting (org-find-base-buffer-visiting file))
  13561. (setq buffer (or visiting (find-file-noselect file)))
  13562. (setq m (org-find-exact-headline-in-buffer
  13563. heading buffer))
  13564. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13565. (and m (throw 'found m))))))
  13566. (defun org-find-entry-with-id (ident)
  13567. "Locate the entry that contains the ID property with exact value IDENT.
  13568. IDENT can be a string, a symbol or a number, this function will search for
  13569. the string representation of it.
  13570. Return the position where this entry starts, or nil if there is no such entry."
  13571. (interactive "sID: ")
  13572. (let ((id (cond
  13573. ((stringp ident) ident)
  13574. ((symbol-name ident) (symbol-name ident))
  13575. ((numberp ident) (number-to-string ident))
  13576. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13577. (case-fold-search nil))
  13578. (save-excursion
  13579. (save-restriction
  13580. (widen)
  13581. (goto-char (point-min))
  13582. (when (re-search-forward
  13583. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13584. nil t)
  13585. (org-back-to-heading t)
  13586. (point))))))
  13587. ;;;; Timestamps
  13588. (defvar org-last-changed-timestamp nil)
  13589. (defvar org-last-inserted-timestamp nil
  13590. "The last time stamp inserted with `org-insert-time-stamp'.")
  13591. (defvar org-time-was-given) ; dynamically scoped parameter
  13592. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13593. (defvar org-ts-what) ; dynamically scoped parameter
  13594. (defun org-time-stamp (arg &optional inactive)
  13595. "Prompt for a date/time and insert a time stamp.
  13596. If the user specifies a time like HH:MM, or if this command is called
  13597. with a prefix argument, the time stamp will contain date and time.
  13598. Otherwise, only the date will be included. All parts of a date not
  13599. specified by the user will be filled in from the current date/time.
  13600. So if you press just return without typing anything, the time stamp
  13601. will represent the current date/time. If there is already a timestamp
  13602. at the cursor, it will be modified."
  13603. (interactive "P")
  13604. (let* ((ts nil)
  13605. (default-time
  13606. ;; Default time is either today, or, when entering a range,
  13607. ;; the range start.
  13608. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13609. (save-excursion
  13610. (re-search-backward
  13611. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13612. (- (point) 20) t)))
  13613. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13614. (current-time)))
  13615. (default-input (and ts (org-get-compact-tod ts)))
  13616. (repeater (save-excursion
  13617. (save-match-data
  13618. (beginning-of-line)
  13619. (when (re-search-forward
  13620. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13621. (save-excursion (progn (end-of-line) (point))) t)
  13622. (match-string 0)))))
  13623. org-time-was-given org-end-time-was-given time)
  13624. (cond
  13625. ((and (org-at-timestamp-p t)
  13626. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13627. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13628. (insert "--")
  13629. (setq time (let ((this-command this-command))
  13630. (org-read-date arg 'totime nil nil
  13631. default-time default-input inactive)))
  13632. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13633. ((org-at-timestamp-p t)
  13634. (setq time (let ((this-command this-command))
  13635. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13636. (when (org-at-timestamp-p t) ; just to get the match data
  13637. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13638. (replace-match "")
  13639. (setq org-last-changed-timestamp
  13640. (org-insert-time-stamp
  13641. time (or org-time-was-given arg)
  13642. inactive nil nil (list org-end-time-was-given)))
  13643. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13644. (setq org-last-changed-timestamp
  13645. (concat (substring org-last-inserted-timestamp 0 -1)
  13646. " " repeater ">"))))
  13647. (message "Timestamp updated"))
  13648. (t
  13649. (setq time (let ((this-command this-command))
  13650. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13651. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13652. nil nil (list org-end-time-was-given))))))
  13653. ;; FIXME: can we use this for something else, like computing time differences?
  13654. (defun org-get-compact-tod (s)
  13655. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13656. (let* ((t1 (match-string 1 s))
  13657. (h1 (string-to-number (match-string 2 s)))
  13658. (m1 (string-to-number (match-string 3 s)))
  13659. (t2 (and (match-end 4) (match-string 5 s)))
  13660. (h2 (and t2 (string-to-number (match-string 6 s))))
  13661. (m2 (and t2 (string-to-number (match-string 7 s))))
  13662. dh dm)
  13663. (if (not t2)
  13664. t1
  13665. (setq dh (- h2 h1) dm (- m2 m1))
  13666. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13667. (concat t1 "+" (number-to-string dh)
  13668. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13669. (defun org-time-stamp-inactive (&optional arg)
  13670. "Insert an inactive time stamp.
  13671. An inactive time stamp is enclosed in square brackets instead of angle
  13672. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13673. does not link to the calendar and cannot be changed with the S-cursor keys.
  13674. So these are more for recording a certain time/date."
  13675. (interactive "P")
  13676. (org-time-stamp arg 'inactive))
  13677. (defvar org-date-ovl (make-overlay 1 1))
  13678. (overlay-put org-date-ovl 'face 'org-warning)
  13679. (org-detach-overlay org-date-ovl)
  13680. (defvar org-ans1) ; dynamically scoped parameter
  13681. (defvar org-ans2) ; dynamically scoped parameter
  13682. (defvar org-plain-time-of-day-regexp) ; defined below
  13683. (defvar org-overriding-default-time nil) ; dynamically scoped
  13684. (defvar org-read-date-overlay nil)
  13685. (defvar org-dcst nil) ; dynamically scoped
  13686. (defvar org-read-date-history nil)
  13687. (defvar org-read-date-final-answer nil)
  13688. (defvar org-read-date-analyze-futurep nil)
  13689. (defvar org-read-date-analyze-forced-year nil)
  13690. (defun org-read-date (&optional with-time to-time from-string prompt
  13691. default-time default-input inactive)
  13692. "Read a date, possibly a time, and make things smooth for the user.
  13693. The prompt will suggest to enter an ISO date, but you can also enter anything
  13694. which will at least partially be understood by `parse-time-string'.
  13695. Unrecognized parts of the date will default to the current day, month, year,
  13696. hour and minute. If this command is called to replace a timestamp at point,
  13697. or to enter the second timestamp of a range, the default time is taken
  13698. from the existing stamp. Furthermore, the command prefers the future,
  13699. so if you are giving a date where the year is not given, and the day-month
  13700. combination is already past in the current year, it will assume you
  13701. mean next year. For details, see the manual. A few examples:
  13702. 3-2-5 --> 2003-02-05
  13703. feb 15 --> currentyear-02-15
  13704. 2/15 --> currentyear-02-15
  13705. sep 12 9 --> 2009-09-12
  13706. 12:45 --> today 12:45
  13707. 22 sept 0:34 --> currentyear-09-22 0:34
  13708. 12 --> currentyear-currentmonth-12
  13709. Fri --> nearest Friday (today or later)
  13710. etc.
  13711. Furthermore you can specify a relative date by giving, as the *first* thing
  13712. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13713. change in days weeks, months, years.
  13714. With a single plus or minus, the date is relative to today. With a double
  13715. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13716. +4d --> four days from today
  13717. +4 --> same as above
  13718. +2w --> two weeks from today
  13719. ++5 --> five days from default date
  13720. The function understands only English month and weekday abbreviations,
  13721. but this can be configured with the variables `parse-time-months' and
  13722. `parse-time-weekdays'.
  13723. While prompting, a calendar is popped up - you can also select the
  13724. date with the mouse (button 1). The calendar shows a period of three
  13725. months. To scroll it to other months, use the keys `>' and `<'.
  13726. If you don't like the calendar, turn it off with
  13727. \(setq org-read-date-popup-calendar nil)
  13728. With optional argument TO-TIME, the date will immediately be converted
  13729. to an internal time.
  13730. With an optional argument WITH-TIME, the prompt will suggest to also
  13731. insert a time. Note that when WITH-TIME is not set, you can still
  13732. enter a time, and this function will inform the calling routine about
  13733. this change. The calling routine may then choose to change the format
  13734. used to insert the time stamp into the buffer to include the time.
  13735. With optional argument FROM-STRING, read from this string instead from
  13736. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13737. the time/date that is used for everything that is not specified by the
  13738. user."
  13739. (require 'parse-time)
  13740. (let* ((org-time-stamp-rounding-minutes
  13741. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13742. (org-dcst org-display-custom-times)
  13743. (ct (org-current-time))
  13744. (def (or org-overriding-default-time default-time ct))
  13745. (defdecode (decode-time def))
  13746. (dummy (progn
  13747. (when (< (nth 2 defdecode) org-extend-today-until)
  13748. (setcar (nthcdr 2 defdecode) -1)
  13749. (setcar (nthcdr 1 defdecode) 59)
  13750. (setq def (apply 'encode-time defdecode)
  13751. defdecode (decode-time def)))))
  13752. (calendar-frame-setup nil)
  13753. (calendar-setup nil)
  13754. (calendar-move-hook nil)
  13755. (calendar-view-diary-initially-flag nil)
  13756. (calendar-view-holidays-initially-flag nil)
  13757. (timestr (format-time-string
  13758. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13759. (prompt (concat (if prompt (concat prompt " ") "")
  13760. (format "Date+time [%s]: " timestr)))
  13761. ans (org-ans0 "") org-ans1 org-ans2 final)
  13762. (cond
  13763. (from-string (setq ans from-string))
  13764. (org-read-date-popup-calendar
  13765. (save-excursion
  13766. (save-window-excursion
  13767. (calendar)
  13768. (unwind-protect
  13769. (progn
  13770. (calendar-forward-day (- (time-to-days def)
  13771. (calendar-absolute-from-gregorian
  13772. (calendar-current-date))))
  13773. (org-eval-in-calendar nil t)
  13774. (let* ((old-map (current-local-map))
  13775. (map (copy-keymap calendar-mode-map))
  13776. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13777. (org-defkey map (kbd "RET") 'org-calendar-select)
  13778. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13779. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13780. (org-defkey minibuffer-local-map [(meta shift left)]
  13781. (lambda () (interactive)
  13782. (org-eval-in-calendar '(calendar-backward-month 1))))
  13783. (org-defkey minibuffer-local-map [(meta shift right)]
  13784. (lambda () (interactive)
  13785. (org-eval-in-calendar '(calendar-forward-month 1))))
  13786. (org-defkey minibuffer-local-map [(meta shift up)]
  13787. (lambda () (interactive)
  13788. (org-eval-in-calendar '(calendar-backward-year 1))))
  13789. (org-defkey minibuffer-local-map [(meta shift down)]
  13790. (lambda () (interactive)
  13791. (org-eval-in-calendar '(calendar-forward-year 1))))
  13792. (org-defkey minibuffer-local-map [?\e (shift left)]
  13793. (lambda () (interactive)
  13794. (org-eval-in-calendar '(calendar-backward-month 1))))
  13795. (org-defkey minibuffer-local-map [?\e (shift right)]
  13796. (lambda () (interactive)
  13797. (org-eval-in-calendar '(calendar-forward-month 1))))
  13798. (org-defkey minibuffer-local-map [?\e (shift up)]
  13799. (lambda () (interactive)
  13800. (org-eval-in-calendar '(calendar-backward-year 1))))
  13801. (org-defkey minibuffer-local-map [?\e (shift down)]
  13802. (lambda () (interactive)
  13803. (org-eval-in-calendar '(calendar-forward-year 1))))
  13804. (org-defkey minibuffer-local-map [(shift up)]
  13805. (lambda () (interactive)
  13806. (org-eval-in-calendar '(calendar-backward-week 1))))
  13807. (org-defkey minibuffer-local-map [(shift down)]
  13808. (lambda () (interactive)
  13809. (org-eval-in-calendar '(calendar-forward-week 1))))
  13810. (org-defkey minibuffer-local-map [(shift left)]
  13811. (lambda () (interactive)
  13812. (org-eval-in-calendar '(calendar-backward-day 1))))
  13813. (org-defkey minibuffer-local-map [(shift right)]
  13814. (lambda () (interactive)
  13815. (org-eval-in-calendar '(calendar-forward-day 1))))
  13816. (org-defkey minibuffer-local-map ">"
  13817. (lambda () (interactive)
  13818. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13819. (org-defkey minibuffer-local-map "<"
  13820. (lambda () (interactive)
  13821. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13822. (org-defkey minibuffer-local-map "\C-v"
  13823. (lambda () (interactive)
  13824. (org-eval-in-calendar
  13825. '(calendar-scroll-left-three-months 1))))
  13826. (org-defkey minibuffer-local-map "\M-v"
  13827. (lambda () (interactive)
  13828. (org-eval-in-calendar
  13829. '(calendar-scroll-right-three-months 1))))
  13830. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13831. (unwind-protect
  13832. (progn
  13833. (use-local-map map)
  13834. (setq org-read-date-inactive inactive)
  13835. (add-hook 'post-command-hook 'org-read-date-display)
  13836. (setq org-ans0 (read-string prompt default-input
  13837. 'org-read-date-history nil))
  13838. ;; org-ans0: from prompt
  13839. ;; org-ans1: from mouse click
  13840. ;; org-ans2: from calendar motion
  13841. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13842. (remove-hook 'post-command-hook 'org-read-date-display)
  13843. (use-local-map old-map)
  13844. (when org-read-date-overlay
  13845. (delete-overlay org-read-date-overlay)
  13846. (setq org-read-date-overlay nil)))))
  13847. (bury-buffer "*Calendar*")))))
  13848. (t ; Naked prompt only
  13849. (unwind-protect
  13850. (setq ans (read-string prompt default-input
  13851. 'org-read-date-history timestr))
  13852. (when org-read-date-overlay
  13853. (delete-overlay org-read-date-overlay)
  13854. (setq org-read-date-overlay nil)))))
  13855. (setq final (org-read-date-analyze ans def defdecode))
  13856. (when org-read-date-analyze-forced-year
  13857. (message "Year was forced into %s"
  13858. (if org-read-date-force-compatible-dates
  13859. "compatible range (1970-2037)"
  13860. "range representable on this machine"))
  13861. (ding))
  13862. ;; One round trip to get rid of 34th of August and stuff like that....
  13863. (setq final (decode-time (apply 'encode-time final)))
  13864. (setq org-read-date-final-answer ans)
  13865. (if to-time
  13866. (apply 'encode-time final)
  13867. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13868. (format "%04d-%02d-%02d %02d:%02d"
  13869. (nth 5 final) (nth 4 final) (nth 3 final)
  13870. (nth 2 final) (nth 1 final))
  13871. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13872. (defvar def)
  13873. (defvar defdecode)
  13874. (defvar with-time)
  13875. (defvar org-read-date-inactive)
  13876. (defun org-read-date-display ()
  13877. "Display the current date prompt interpretation in the minibuffer."
  13878. (when org-read-date-display-live
  13879. (when org-read-date-overlay
  13880. (delete-overlay org-read-date-overlay))
  13881. (let ((p (point)))
  13882. (end-of-line 1)
  13883. (while (not (equal (buffer-substring
  13884. (max (point-min) (- (point) 4)) (point))
  13885. " "))
  13886. (insert " "))
  13887. (goto-char p))
  13888. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13889. " " (or org-ans1 org-ans2)))
  13890. (org-end-time-was-given nil)
  13891. (f (org-read-date-analyze ans def defdecode))
  13892. (fmts (if org-dcst
  13893. org-time-stamp-custom-formats
  13894. org-time-stamp-formats))
  13895. (fmt (if (or with-time
  13896. (and (boundp 'org-time-was-given) org-time-was-given))
  13897. (cdr fmts)
  13898. (car fmts)))
  13899. (txt (format-time-string fmt (apply 'encode-time f)))
  13900. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13901. (txt (concat "=> " txt)))
  13902. (when (and org-end-time-was-given
  13903. (string-match org-plain-time-of-day-regexp txt))
  13904. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13905. org-end-time-was-given
  13906. (substring txt (match-end 0)))))
  13907. (when org-read-date-analyze-futurep
  13908. (setq txt (concat txt " (=>F)")))
  13909. (setq org-read-date-overlay
  13910. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13911. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13912. (defun org-read-date-analyze (ans def defdecode)
  13913. "Analyze the combined answer of the date prompt."
  13914. ;; FIXME: cleanup and comment
  13915. (let ((nowdecode (decode-time (current-time)))
  13916. delta deltan deltaw deltadef year month day
  13917. hour minute second wday pm h2 m2 tl wday1
  13918. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13919. (setq org-read-date-analyze-futurep nil
  13920. org-read-date-analyze-forced-year nil)
  13921. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13922. (setq ans "+0"))
  13923. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13924. (setq ans (replace-match "" t t ans)
  13925. deltan (car delta)
  13926. deltaw (nth 1 delta)
  13927. deltadef (nth 2 delta)))
  13928. ;; Check if there is an iso week date in there
  13929. ;; If yes, store the info and postpone interpreting it until the rest
  13930. ;; of the parsing is done
  13931. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13932. (setq iso-year (if (match-end 1)
  13933. (org-small-year-to-year
  13934. (string-to-number (match-string 1 ans))))
  13935. iso-weekday (if (match-end 3)
  13936. (string-to-number (match-string 3 ans)))
  13937. iso-week (string-to-number (match-string 2 ans)))
  13938. (setq ans (replace-match "" t t ans)))
  13939. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13940. (when (string-match
  13941. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13942. (setq year (if (match-end 2)
  13943. (string-to-number (match-string 2 ans))
  13944. (progn (setq kill-year t)
  13945. (string-to-number (format-time-string "%Y"))))
  13946. month (string-to-number (match-string 3 ans))
  13947. day (string-to-number (match-string 4 ans)))
  13948. (if (< year 100) (setq year (+ 2000 year)))
  13949. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13950. t nil ans)))
  13951. ;; Help matching dotted european dates
  13952. (when (string-match
  13953. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13954. (setq year (if (match-end 3)
  13955. (string-to-number (match-string 3 ans))
  13956. (progn (setq kill-year t)
  13957. (string-to-number (format-time-string "%Y"))))
  13958. day (string-to-number (match-string 1 ans))
  13959. month (string-to-number (match-string 2 ans))
  13960. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13961. t nil ans)))
  13962. ;; Help matching american dates, like 5/30 or 5/30/7
  13963. (when (string-match
  13964. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13965. (setq year (if (match-end 4)
  13966. (string-to-number (match-string 4 ans))
  13967. (progn (setq kill-year t)
  13968. (string-to-number (format-time-string "%Y"))))
  13969. month (string-to-number (match-string 1 ans))
  13970. day (string-to-number (match-string 2 ans)))
  13971. (if (< year 100) (setq year (+ 2000 year)))
  13972. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13973. t nil ans)))
  13974. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13975. ;; If there is a time with am/pm, and *no* time without it, we convert
  13976. ;; so that matching will be successful.
  13977. (loop for i from 1 to 2 do ; twice, for end time as well
  13978. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13979. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13980. (setq hour (string-to-number (match-string 1 ans))
  13981. minute (if (match-end 3)
  13982. (string-to-number (match-string 3 ans))
  13983. 0)
  13984. pm (equal ?p
  13985. (string-to-char (downcase (match-string 4 ans)))))
  13986. (if (and (= hour 12) (not pm))
  13987. (setq hour 0)
  13988. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13989. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13990. t t ans))))
  13991. ;; Check if a time range is given as a duration
  13992. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13993. (setq hour (string-to-number (match-string 1 ans))
  13994. h2 (+ hour (string-to-number (match-string 3 ans)))
  13995. minute (string-to-number (match-string 2 ans))
  13996. m2 (+ minute (if (match-end 5) (string-to-number
  13997. (match-string 5 ans))0)))
  13998. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13999. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14000. t t ans)))
  14001. ;; Check if there is a time range
  14002. (when (boundp 'org-end-time-was-given)
  14003. (setq org-time-was-given nil)
  14004. (when (and (string-match org-plain-time-of-day-regexp ans)
  14005. (match-end 8))
  14006. (setq org-end-time-was-given (match-string 8 ans))
  14007. (setq ans (concat (substring ans 0 (match-beginning 7))
  14008. (substring ans (match-end 7))))))
  14009. (setq tl (parse-time-string ans)
  14010. day (or (nth 3 tl) (nth 3 defdecode))
  14011. month (or (nth 4 tl)
  14012. (if (and org-read-date-prefer-future
  14013. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14014. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14015. (nth 4 defdecode)))
  14016. year (or (and (not kill-year) (nth 5 tl))
  14017. (if (and org-read-date-prefer-future
  14018. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14019. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14020. (nth 5 defdecode)))
  14021. hour (or (nth 2 tl) (nth 2 defdecode))
  14022. minute (or (nth 1 tl) (nth 1 defdecode))
  14023. second (or (nth 0 tl) 0)
  14024. wday (nth 6 tl))
  14025. (when (and (eq org-read-date-prefer-future 'time)
  14026. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14027. (equal day (nth 3 nowdecode))
  14028. (equal month (nth 4 nowdecode))
  14029. (equal year (nth 5 nowdecode))
  14030. (nth 2 tl)
  14031. (or (< (nth 2 tl) (nth 2 nowdecode))
  14032. (and (= (nth 2 tl) (nth 2 nowdecode))
  14033. (nth 1 tl)
  14034. (< (nth 1 tl) (nth 1 nowdecode)))))
  14035. (setq day (1+ day)
  14036. futurep t))
  14037. ;; Special date definitions below
  14038. (cond
  14039. (iso-week
  14040. ;; There was an iso week
  14041. (require 'cal-iso)
  14042. (setq futurep nil)
  14043. (setq year (or iso-year year)
  14044. day (or iso-weekday wday 1)
  14045. wday nil ; to make sure that the trigger below does not match
  14046. iso-date (calendar-gregorian-from-absolute
  14047. (calendar-absolute-from-iso
  14048. (list iso-week day year))))
  14049. ; FIXME: Should we also push ISO weeks into the future?
  14050. ; (when (and org-read-date-prefer-future
  14051. ; (not iso-year)
  14052. ; (< (calendar-absolute-from-gregorian iso-date)
  14053. ; (time-to-days (current-time))))
  14054. ; (setq year (1+ year)
  14055. ; iso-date (calendar-gregorian-from-absolute
  14056. ; (calendar-absolute-from-iso
  14057. ; (list iso-week day year)))))
  14058. (setq month (car iso-date)
  14059. year (nth 2 iso-date)
  14060. day (nth 1 iso-date)))
  14061. (deltan
  14062. (setq futurep nil)
  14063. (unless deltadef
  14064. (let ((now (decode-time (current-time))))
  14065. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14066. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14067. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14068. ((equal deltaw "m") (setq month (+ month deltan)))
  14069. ((equal deltaw "y") (setq year (+ year deltan)))))
  14070. ((and wday (not (nth 3 tl)))
  14071. (setq futurep nil)
  14072. ;; Weekday was given, but no day, so pick that day in the week
  14073. ;; on or after the derived date.
  14074. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14075. (unless (equal wday wday1)
  14076. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14077. (if (and (boundp 'org-time-was-given)
  14078. (nth 2 tl))
  14079. (setq org-time-was-given t))
  14080. (if (< year 100) (setq year (+ 2000 year)))
  14081. ;; Check of the date is representable
  14082. (if org-read-date-force-compatible-dates
  14083. (progn
  14084. (if (< year 1970)
  14085. (setq year 1970 org-read-date-analyze-forced-year t))
  14086. (if (> year 2037)
  14087. (setq year 2037 org-read-date-analyze-forced-year t)))
  14088. (condition-case nil
  14089. (ignore (encode-time second minute hour day month year))
  14090. (error
  14091. (setq year (nth 5 defdecode))
  14092. (setq org-read-date-analyze-forced-year t))))
  14093. (setq org-read-date-analyze-futurep futurep)
  14094. (list second minute hour day month year)))
  14095. (defvar parse-time-weekdays)
  14096. (defun org-read-date-get-relative (s today default)
  14097. "Check string S for special relative date string.
  14098. TODAY and DEFAULT are internal times, for today and for a default.
  14099. Return shift list (N what def-flag)
  14100. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14101. N is the number of WHATs to shift.
  14102. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14103. the DEFAULT date rather than TODAY."
  14104. (require 'parse-time)
  14105. (when (and
  14106. (string-match
  14107. (concat
  14108. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14109. "\\([0-9]+\\)?"
  14110. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14111. "\\([ \t]\\|$\\)") s)
  14112. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14113. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14114. (string-to-char (substring (match-string 1 s) -1))
  14115. ?+))
  14116. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14117. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14118. (what (if (match-end 3) (match-string 3 s) "d"))
  14119. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14120. (date (if rel default today))
  14121. (wday (nth 6 (decode-time date)))
  14122. delta)
  14123. (if wday1
  14124. (progn
  14125. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14126. (if (= dir ?-) (setq delta (- delta 7)))
  14127. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14128. (list delta "d" rel))
  14129. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14130. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14131. "Turn a user-specified date into the internal representation.
  14132. The internal representation needed by the calendar is (month day year).
  14133. This is a wrapper to handle the brain-dead convention in calendar that
  14134. user function argument order change dependent on argument order."
  14135. (if (boundp 'calendar-date-style)
  14136. (cond
  14137. ((eq calendar-date-style 'american)
  14138. (list arg1 arg2 arg3))
  14139. ((eq calendar-date-style 'european)
  14140. (list arg2 arg1 arg3))
  14141. ((eq calendar-date-style 'iso)
  14142. (list arg2 arg3 arg1)))
  14143. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14144. (if (org-bound-and-true-p european-calendar-style)
  14145. (list arg2 arg1 arg3)
  14146. (list arg1 arg2 arg3)))))
  14147. (defun org-eval-in-calendar (form &optional keepdate)
  14148. "Eval FORM in the calendar window and return to current window.
  14149. Also, store the cursor date in variable org-ans2."
  14150. (let ((sf (selected-frame))
  14151. (sw (selected-window)))
  14152. (select-window (get-buffer-window "*Calendar*" t))
  14153. (eval form)
  14154. (when (and (not keepdate) (calendar-cursor-to-date))
  14155. (let* ((date (calendar-cursor-to-date))
  14156. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14157. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14158. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14159. (select-window sw)
  14160. (org-select-frame-set-input-focus sf)))
  14161. (defun org-calendar-select ()
  14162. "Return to `org-read-date' with the date currently selected.
  14163. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14164. (interactive)
  14165. (when (calendar-cursor-to-date)
  14166. (let* ((date (calendar-cursor-to-date))
  14167. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14168. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14169. (if (active-minibuffer-window) (exit-minibuffer))))
  14170. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14171. "Insert a date stamp for the date given by the internal TIME.
  14172. WITH-HM means use the stamp format that includes the time of the day.
  14173. INACTIVE means use square brackets instead of angular ones, so that the
  14174. stamp will not contribute to the agenda.
  14175. PRE and POST are optional strings to be inserted before and after the
  14176. stamp.
  14177. The command returns the inserted time stamp."
  14178. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14179. stamp)
  14180. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14181. (insert-before-markers (or pre ""))
  14182. (when (listp extra)
  14183. (setq extra (car extra))
  14184. (if (and (stringp extra)
  14185. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14186. (setq extra (format "-%02d:%02d"
  14187. (string-to-number (match-string 1 extra))
  14188. (string-to-number (match-string 2 extra))))
  14189. (setq extra nil)))
  14190. (when extra
  14191. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14192. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14193. (insert-before-markers (or post ""))
  14194. (setq org-last-inserted-timestamp stamp)))
  14195. (defun org-toggle-time-stamp-overlays ()
  14196. "Toggle the use of custom time stamp formats."
  14197. (interactive)
  14198. (setq org-display-custom-times (not org-display-custom-times))
  14199. (unless org-display-custom-times
  14200. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14201. (while (setq p (next-single-property-change p 'display))
  14202. (if (and (get-text-property p 'display)
  14203. (eq (get-text-property p 'face) 'org-date))
  14204. (remove-text-properties
  14205. p (setq p (next-single-property-change p 'display))
  14206. '(display t))))
  14207. (set-buffer-modified-p bmp)))
  14208. (if (featurep 'xemacs)
  14209. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14210. (org-restart-font-lock)
  14211. (setq org-table-may-need-update t)
  14212. (if org-display-custom-times
  14213. (message "Time stamps are overlaid with custom format")
  14214. (message "Time stamp overlays removed")))
  14215. (defun org-display-custom-time (beg end)
  14216. "Overlay modified time stamp format over timestamp between BEG and END."
  14217. (let* ((ts (buffer-substring beg end))
  14218. t1 w1 with-hm tf time str w2 (off 0))
  14219. (save-match-data
  14220. (setq t1 (org-parse-time-string ts t))
  14221. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14222. (setq off (- (match-end 0) (match-beginning 0)))))
  14223. (setq end (- end off))
  14224. (setq w1 (- end beg)
  14225. with-hm (and (nth 1 t1) (nth 2 t1))
  14226. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14227. time (org-fix-decoded-time t1)
  14228. str (org-add-props
  14229. (format-time-string
  14230. (substring tf 1 -1) (apply 'encode-time time))
  14231. nil 'mouse-face 'highlight)
  14232. w2 (length str))
  14233. (if (not (= w2 w1))
  14234. (add-text-properties (1+ beg) (+ 2 beg)
  14235. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14236. (if (featurep 'xemacs)
  14237. (progn
  14238. (put-text-property beg end 'invisible t)
  14239. (put-text-property beg end 'end-glyph (make-glyph str)))
  14240. (put-text-property beg end 'display str))))
  14241. (defun org-translate-time (string)
  14242. "Translate all timestamps in STRING to custom format.
  14243. But do this only if the variable `org-display-custom-times' is set."
  14244. (when org-display-custom-times
  14245. (save-match-data
  14246. (let* ((start 0)
  14247. (re org-ts-regexp-both)
  14248. t1 with-hm inactive tf time str beg end)
  14249. (while (setq start (string-match re string start))
  14250. (setq beg (match-beginning 0)
  14251. end (match-end 0)
  14252. t1 (save-match-data
  14253. (org-parse-time-string (substring string beg end) t))
  14254. with-hm (and (nth 1 t1) (nth 2 t1))
  14255. inactive (equal (substring string beg (1+ beg)) "[")
  14256. tf (funcall (if with-hm 'cdr 'car)
  14257. org-time-stamp-custom-formats)
  14258. time (org-fix-decoded-time t1)
  14259. str (format-time-string
  14260. (concat
  14261. (if inactive "[" "<") (substring tf 1 -1)
  14262. (if inactive "]" ">"))
  14263. (apply 'encode-time time))
  14264. string (replace-match str t t string)
  14265. start (+ start (length str)))))))
  14266. string)
  14267. (defun org-fix-decoded-time (time)
  14268. "Set 0 instead of nil for the first 6 elements of time.
  14269. Don't touch the rest."
  14270. (let ((n 0))
  14271. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14272. (defun org-days-to-time (timestamp-string)
  14273. "Difference between TIMESTAMP-STRING and now in days."
  14274. (- (time-to-days (org-time-string-to-time timestamp-string))
  14275. (time-to-days (current-time))))
  14276. (defun org-deadline-close (timestamp-string &optional ndays)
  14277. "Is the time in TIMESTAMP-STRING close to the current date?"
  14278. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14279. (and (< (org-days-to-time timestamp-string) ndays)
  14280. (not (org-entry-is-done-p))))
  14281. (defun org-get-wdays (ts)
  14282. "Get the deadline lead time appropriate for timestring TS."
  14283. (cond
  14284. ((<= org-deadline-warning-days 0)
  14285. ;; 0 or negative, enforce this value no matter what
  14286. (- org-deadline-warning-days))
  14287. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14288. ;; lead time is specified.
  14289. (floor (* (string-to-number (match-string 1 ts))
  14290. (cdr (assoc (match-string 2 ts)
  14291. '(("d" . 1) ("w" . 7)
  14292. ("m" . 30.4) ("y" . 365.25)))))))
  14293. ;; go for the default.
  14294. (t org-deadline-warning-days)))
  14295. (defun org-calendar-select-mouse (ev)
  14296. "Return to `org-read-date' with the date currently selected.
  14297. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14298. (interactive "e")
  14299. (mouse-set-point ev)
  14300. (when (calendar-cursor-to-date)
  14301. (let* ((date (calendar-cursor-to-date))
  14302. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14303. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14304. (if (active-minibuffer-window) (exit-minibuffer))))
  14305. (defun org-check-deadlines (ndays)
  14306. "Check if there are any deadlines due or past due.
  14307. A deadline is considered due if it happens within `org-deadline-warning-days'
  14308. days from today's date. If the deadline appears in an entry marked DONE,
  14309. it is not shown. The prefix arg NDAYS can be used to test that many
  14310. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14311. (interactive "P")
  14312. (let* ((org-warn-days
  14313. (cond
  14314. ((equal ndays '(4)) 100000)
  14315. (ndays (prefix-numeric-value ndays))
  14316. (t (abs org-deadline-warning-days))))
  14317. (case-fold-search nil)
  14318. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14319. (callback
  14320. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14321. (message "%d deadlines past-due or due within %d days"
  14322. (org-occur regexp nil callback)
  14323. org-warn-days)))
  14324. (defun org-check-before-date (date)
  14325. "Check if there are deadlines or scheduled entries before DATE."
  14326. (interactive (list (org-read-date)))
  14327. (let ((case-fold-search nil)
  14328. (regexp (concat "\\<\\(" org-deadline-string
  14329. "\\|" org-scheduled-string
  14330. "\\) *<\\([^>]+\\)>"))
  14331. (callback
  14332. (lambda () (time-less-p
  14333. (org-time-string-to-time (match-string 2))
  14334. (org-time-string-to-time date)))))
  14335. (message "%d entries before %s"
  14336. (org-occur regexp nil callback) date)))
  14337. (defun org-check-after-date (date)
  14338. "Check if there are deadlines or scheduled entries after DATE."
  14339. (interactive (list (org-read-date)))
  14340. (let ((case-fold-search nil)
  14341. (regexp (concat "\\<\\(" org-deadline-string
  14342. "\\|" org-scheduled-string
  14343. "\\) *<\\([^>]+\\)>"))
  14344. (callback
  14345. (lambda () (not
  14346. (time-less-p
  14347. (org-time-string-to-time (match-string 2))
  14348. (org-time-string-to-time date))))))
  14349. (message "%d entries after %s"
  14350. (org-occur regexp nil callback) date)))
  14351. (defun org-check-dates-range (start-date end-date)
  14352. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14353. (interactive (list (org-read-date nil nil nil "Range starts")
  14354. (org-read-date nil nil nil "Range end")))
  14355. (let ((case-fold-search nil)
  14356. (regexp (concat "\\<\\(" org-deadline-string
  14357. "\\|" org-scheduled-string
  14358. "\\) *<\\([^>]+\\)>"))
  14359. (callback
  14360. (lambda ()
  14361. (let ((match (match-string 2)))
  14362. (and
  14363. (not (time-less-p
  14364. (org-time-string-to-time match)
  14365. (org-time-string-to-time start-date)))
  14366. (time-less-p
  14367. (org-time-string-to-time match)
  14368. (org-time-string-to-time end-date)))))))
  14369. (message "%d entries between %s and %s"
  14370. (org-occur regexp nil callback) start-date end-date)))
  14371. (defun org-evaluate-time-range (&optional to-buffer)
  14372. "Evaluate a time range by computing the difference between start and end.
  14373. Normally the result is just printed in the echo area, but with prefix arg
  14374. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14375. If the time range is actually in a table, the result is inserted into the
  14376. next column.
  14377. For time difference computation, a year is assumed to be exactly 365
  14378. days in order to avoid rounding problems."
  14379. (interactive "P")
  14380. (or
  14381. (org-clock-update-time-maybe)
  14382. (save-excursion
  14383. (unless (org-at-date-range-p t)
  14384. (goto-char (point-at-bol))
  14385. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14386. (if (not (org-at-date-range-p t))
  14387. (error "Not at a time-stamp range, and none found in current line")))
  14388. (let* ((ts1 (match-string 1))
  14389. (ts2 (match-string 2))
  14390. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14391. (match-end (match-end 0))
  14392. (time1 (org-time-string-to-time ts1))
  14393. (time2 (org-time-string-to-time ts2))
  14394. (t1 (org-float-time time1))
  14395. (t2 (org-float-time time2))
  14396. (diff (abs (- t2 t1)))
  14397. (negative (< (- t2 t1) 0))
  14398. ;; (ys (floor (* 365 24 60 60)))
  14399. (ds (* 24 60 60))
  14400. (hs (* 60 60))
  14401. (fy "%dy %dd %02d:%02d")
  14402. (fy1 "%dy %dd")
  14403. (fd "%dd %02d:%02d")
  14404. (fd1 "%dd")
  14405. (fh "%02d:%02d")
  14406. y d h m align)
  14407. (if havetime
  14408. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14409. y 0
  14410. d (floor (/ diff ds)) diff (mod diff ds)
  14411. h (floor (/ diff hs)) diff (mod diff hs)
  14412. m (floor (/ diff 60)))
  14413. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14414. y 0
  14415. d (floor (+ (/ diff ds) 0.5))
  14416. h 0 m 0))
  14417. (if (not to-buffer)
  14418. (message "%s" (org-make-tdiff-string y d h m))
  14419. (if (org-at-table-p)
  14420. (progn
  14421. (goto-char match-end)
  14422. (setq align t)
  14423. (and (looking-at " *|") (goto-char (match-end 0))))
  14424. (goto-char match-end))
  14425. (if (looking-at
  14426. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14427. (replace-match ""))
  14428. (if negative (insert " -"))
  14429. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14430. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14431. (insert " " (format fh h m))))
  14432. (if align (org-table-align))
  14433. (message "Time difference inserted")))))
  14434. (defun org-make-tdiff-string (y d h m)
  14435. (let ((fmt "")
  14436. (l nil))
  14437. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14438. l (push y l)))
  14439. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14440. l (push d l)))
  14441. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14442. l (push h l)))
  14443. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14444. l (push m l)))
  14445. (apply 'format fmt (nreverse l))))
  14446. (defun org-time-string-to-time (s &optional buffer pos)
  14447. (condition-case errdata
  14448. (apply 'encode-time (org-parse-time-string s))
  14449. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14450. s (if (not (and buffer pos))
  14451. ""
  14452. (format " at %d in buffer `%s'" pos buffer))
  14453. (cdr errdata)))))
  14454. (defun org-time-string-to-seconds (s)
  14455. (org-float-time (org-time-string-to-time s)))
  14456. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14457. "Convert a time stamp to an absolute day number.
  14458. If there is a specifier for a cyclic time stamp, get the closest date to
  14459. DAYNR.
  14460. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14461. The variable date is bound by the calendar when this is called."
  14462. (cond
  14463. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14464. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14465. daynr
  14466. (+ daynr 1000)))
  14467. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14468. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14469. (time-to-days (current-time))) (match-string 0 s)
  14470. prefer show-all))
  14471. (t (time-to-days
  14472. (condition-case errdata
  14473. (apply 'encode-time (org-parse-time-string s))
  14474. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14475. s (if (not (and buffer pos))
  14476. ""
  14477. (format " at %d in buffer `%s'" pos buffer))
  14478. (cdr errdata))))))))
  14479. (defun org-days-to-iso-week (days)
  14480. "Return the iso week number."
  14481. (require 'cal-iso)
  14482. (car (calendar-iso-from-absolute days)))
  14483. (defun org-small-year-to-year (year)
  14484. "Convert 2-digit years into 4-digit years.
  14485. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14486. The year 2000 cannot be abbreviated. Any year larger than 99
  14487. is returned unchanged."
  14488. (if (< year 38)
  14489. (setq year (+ 2000 year))
  14490. (if (< year 100)
  14491. (setq year (+ 1900 year))))
  14492. year)
  14493. (defun org-time-from-absolute (d)
  14494. "Return the time corresponding to date D.
  14495. D may be an absolute day number, or a calendar-type list (month day year)."
  14496. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14497. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14498. (defun org-calendar-holiday ()
  14499. "List of holidays, for Diary display in Org-mode."
  14500. (require 'holidays)
  14501. (let ((hl (funcall
  14502. (if (fboundp 'calendar-check-holidays)
  14503. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14504. (if hl (mapconcat 'identity hl "; "))))
  14505. (defun org-diary-sexp-entry (sexp entry date)
  14506. "Process a SEXP diary ENTRY for DATE."
  14507. (require 'diary-lib)
  14508. (let ((result (if calendar-debug-sexp
  14509. (let ((stack-trace-on-error t))
  14510. (eval (car (read-from-string sexp))))
  14511. (condition-case nil
  14512. (eval (car (read-from-string sexp)))
  14513. (error
  14514. (beep)
  14515. (message "Bad sexp at line %d in %s: %s"
  14516. (org-current-line)
  14517. (buffer-file-name) sexp)
  14518. (sleep-for 2))))))
  14519. (cond ((stringp result) (split-string result "; "))
  14520. ((and (consp result)
  14521. (not (consp (cdr result)))
  14522. (stringp (cdr result))) (cdr result))
  14523. ((and (consp result)
  14524. (stringp (car result))) result)
  14525. (result entry)
  14526. (t nil))))
  14527. (defun org-diary-to-ical-string (frombuf)
  14528. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14529. This uses the icalendar.el library."
  14530. (let* ((tmpdir (if (featurep 'xemacs)
  14531. (temp-directory)
  14532. temporary-file-directory))
  14533. (tmpfile (make-temp-name
  14534. (expand-file-name "orgics" tmpdir)))
  14535. buf rtn b e)
  14536. (with-current-buffer frombuf
  14537. (icalendar-export-region (point-min) (point-max) tmpfile)
  14538. (setq buf (find-buffer-visiting tmpfile))
  14539. (set-buffer buf)
  14540. (goto-char (point-min))
  14541. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14542. (setq b (match-beginning 0)))
  14543. (goto-char (point-max))
  14544. (if (re-search-backward "^END:VEVENT" nil t)
  14545. (setq e (match-end 0)))
  14546. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14547. (kill-buffer buf)
  14548. (delete-file tmpfile)
  14549. rtn))
  14550. (defun org-closest-date (start current change prefer show-all)
  14551. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14552. When PREFER is `past', return a date that is either CURRENT or past.
  14553. When PREFER is `future', return a date that is either CURRENT or future.
  14554. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14555. ;; Make the proper lists from the dates
  14556. (catch 'exit
  14557. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14558. dn dw sday cday n1 n2 n0
  14559. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14560. (setq start (org-date-to-gregorian start)
  14561. current (org-date-to-gregorian
  14562. (if show-all
  14563. current
  14564. (time-to-days (current-time))))
  14565. sday (calendar-absolute-from-gregorian start)
  14566. cday (calendar-absolute-from-gregorian current))
  14567. (if (<= cday sday) (throw 'exit sday))
  14568. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14569. (setq dn (string-to-number (match-string 1 change))
  14570. dw (cdr (assoc (match-string 2 change) a1)))
  14571. (error "Invalid change specifier: %s" change))
  14572. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14573. (cond
  14574. ((eq dw 'day)
  14575. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14576. n2 (+ n1 dn)))
  14577. ((eq dw 'year)
  14578. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14579. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14580. (setq date1 (list m d y1)
  14581. n1 (calendar-absolute-from-gregorian date1)
  14582. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14583. n2 (calendar-absolute-from-gregorian date2)))
  14584. ((eq dw 'month)
  14585. ;; approx number of month between the two dates
  14586. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14587. ;; How often does dn fit in there?
  14588. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14589. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14590. m (+ m nm)
  14591. ny (floor (/ m 12))
  14592. y (+ y ny)
  14593. m (- m (* ny 12)))
  14594. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14595. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14596. (setq m2 (+ m dn) y2 y)
  14597. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14598. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14599. (while (<= n2 cday)
  14600. (setq n1 n2 m m2 y y2)
  14601. (setq m2 (+ m dn) y2 y)
  14602. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14603. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14604. ;; Make sure n1 is the earlier date
  14605. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14606. (if show-all
  14607. (cond
  14608. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14609. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14610. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14611. (cond
  14612. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14613. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14614. (t (if (= cday n1) n1 n2)))))))
  14615. (defun org-date-to-gregorian (date)
  14616. "Turn any specification of DATE into a Gregorian date for the calendar."
  14617. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14618. ((and (listp date) (= (length date) 3)) date)
  14619. ((stringp date)
  14620. (setq date (org-parse-time-string date))
  14621. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14622. ((listp date)
  14623. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14624. (defun org-parse-time-string (s &optional nodefault)
  14625. "Parse the standard Org-mode time string.
  14626. This should be a lot faster than the normal `parse-time-string'.
  14627. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14628. hour and minute fields will be nil if not given."
  14629. (if (string-match org-ts-regexp0 s)
  14630. (list 0
  14631. (if (or (match-beginning 8) (not nodefault))
  14632. (string-to-number (or (match-string 8 s) "0")))
  14633. (if (or (match-beginning 7) (not nodefault))
  14634. (string-to-number (or (match-string 7 s) "0")))
  14635. (string-to-number (match-string 4 s))
  14636. (string-to-number (match-string 3 s))
  14637. (string-to-number (match-string 2 s))
  14638. nil nil nil)
  14639. (error "Not a standard Org-mode time string: %s" s)))
  14640. (defun org-timestamp-up (&optional arg)
  14641. "Increase the date item at the cursor by one.
  14642. If the cursor is on the year, change the year. If it is on the month,
  14643. the day or the time, change that.
  14644. With prefix ARG, change by that many units."
  14645. (interactive "p")
  14646. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14647. (defun org-timestamp-down (&optional arg)
  14648. "Decrease the date item at the cursor by one.
  14649. If the cursor is on the year, change the year. If it is on the month,
  14650. the day or the time, change that.
  14651. With prefix ARG, change by that many units."
  14652. (interactive "p")
  14653. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14654. (defun org-timestamp-up-day (&optional arg)
  14655. "Increase the date in the time stamp by one day.
  14656. With prefix ARG, change that many days."
  14657. (interactive "p")
  14658. (if (and (not (org-at-timestamp-p t))
  14659. (org-at-heading-p))
  14660. (org-todo 'up)
  14661. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14662. (defun org-timestamp-down-day (&optional arg)
  14663. "Decrease the date in the time stamp by one day.
  14664. With prefix ARG, change that many days."
  14665. (interactive "p")
  14666. (if (and (not (org-at-timestamp-p t))
  14667. (org-at-heading-p))
  14668. (org-todo 'down)
  14669. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14670. (defun org-at-timestamp-p (&optional inactive-ok)
  14671. "Determine if the cursor is in or at a timestamp."
  14672. (interactive)
  14673. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14674. (pos (point))
  14675. (ans (or (looking-at tsr)
  14676. (save-excursion
  14677. (skip-chars-backward "^[<\n\r\t")
  14678. (if (> (point) (point-min)) (backward-char 1))
  14679. (and (looking-at tsr)
  14680. (> (- (match-end 0) pos) -1))))))
  14681. (and ans
  14682. (boundp 'org-ts-what)
  14683. (setq org-ts-what
  14684. (cond
  14685. ((= pos (match-beginning 0)) 'bracket)
  14686. ;; Point is considered to be "on the bracket" whether
  14687. ;; it's really on it or right after it.
  14688. ((or (= pos (1- (match-end 0)))
  14689. (= pos (match-end 0))) 'bracket)
  14690. ((org-pos-in-match-range pos 2) 'year)
  14691. ((org-pos-in-match-range pos 3) 'month)
  14692. ((org-pos-in-match-range pos 7) 'hour)
  14693. ((org-pos-in-match-range pos 8) 'minute)
  14694. ((or (org-pos-in-match-range pos 4)
  14695. (org-pos-in-match-range pos 5)) 'day)
  14696. ((and (> pos (or (match-end 8) (match-end 5)))
  14697. (< pos (match-end 0)))
  14698. (- pos (or (match-end 8) (match-end 5))))
  14699. (t 'day))))
  14700. ans))
  14701. (defun org-toggle-timestamp-type ()
  14702. "Toggle the type (<active> or [inactive]) of a time stamp."
  14703. (interactive)
  14704. (when (org-at-timestamp-p t)
  14705. (let ((beg (match-beginning 0)) (end (match-end 0))
  14706. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14707. (save-excursion
  14708. (goto-char beg)
  14709. (while (re-search-forward "[][<>]" end t)
  14710. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14711. t t)))
  14712. (message "Timestamp is now %sactive"
  14713. (if (equal (char-after beg) ?<) "" "in")))))
  14714. (defun org-timestamp-change (n &optional what updown)
  14715. "Change the date in the time stamp at point.
  14716. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14717. `year', `minute', `second'. If WHAT is not given, the cursor position
  14718. in the timestamp determines what will be changed."
  14719. (let ((origin (point)) origin-cat
  14720. with-hm inactive
  14721. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14722. org-ts-what
  14723. extra rem
  14724. ts time time0)
  14725. (if (not (org-at-timestamp-p t))
  14726. (error "Not at a timestamp"))
  14727. (if (and (not what) (eq org-ts-what 'bracket))
  14728. (org-toggle-timestamp-type)
  14729. ;; Point isn't on brackets. Remember the part of the time-stamp
  14730. ;; the point was in. Indeed, size of time-stamps may change,
  14731. ;; but point must be kept in the same category nonetheless.
  14732. (setq origin-cat org-ts-what)
  14733. (if (and (not what) (not (eq org-ts-what 'day))
  14734. org-display-custom-times
  14735. (get-text-property (point) 'display)
  14736. (not (get-text-property (1- (point)) 'display)))
  14737. (setq org-ts-what 'day))
  14738. (setq org-ts-what (or what org-ts-what)
  14739. inactive (= (char-after (match-beginning 0)) ?\[)
  14740. ts (match-string 0))
  14741. (replace-match "")
  14742. (if (string-match
  14743. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14744. ts)
  14745. (setq extra (match-string 1 ts)))
  14746. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14747. (setq with-hm t))
  14748. (setq time0 (org-parse-time-string ts))
  14749. (when (and updown
  14750. (eq org-ts-what 'minute)
  14751. (not current-prefix-arg))
  14752. ;; This looks like s-up and s-down. Change by one rounding step.
  14753. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14754. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14755. (setcar (cdr time0) (+ (nth 1 time0)
  14756. (if (> n 0) (- rem) (- dm rem))))))
  14757. (setq time
  14758. (encode-time (or (car time0) 0)
  14759. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14760. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14761. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14762. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14763. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14764. (nthcdr 6 time0)))
  14765. (when (and (member org-ts-what '(hour minute))
  14766. extra
  14767. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14768. (setq extra (org-modify-ts-extra
  14769. extra
  14770. (if (eq org-ts-what 'hour) 2 5)
  14771. n dm)))
  14772. (when (integerp org-ts-what)
  14773. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14774. (if (eq what 'calendar)
  14775. (let ((cal-date (org-get-date-from-calendar)))
  14776. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14777. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14778. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14779. (setcar time0 (or (car time0) 0))
  14780. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14781. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14782. (setq time (apply 'encode-time time0))))
  14783. ;; Insert the new time-stamp, and ensure point stays in the same
  14784. ;; category as before (i.e. not after the last position in that
  14785. ;; category).
  14786. (let ((pos (point)))
  14787. ;; Stay before inserted string. `save-excursion' is of no use.
  14788. (setq org-last-changed-timestamp
  14789. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14790. (goto-char pos))
  14791. (save-match-data
  14792. (looking-at org-ts-regexp3)
  14793. (goto-char (cond
  14794. ;; `day' category ends before `hour' if any, or at
  14795. ;; the end of the day name.
  14796. ((eq origin-cat 'day)
  14797. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14798. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14799. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14800. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14801. ;; `year' and `month' have both fixed size: point
  14802. ;; couldn't have moved into another part.
  14803. (t origin))))
  14804. ;; Update clock if on a CLOCK line.
  14805. (org-clock-update-time-maybe)
  14806. ;; Try to recenter the calendar window, if any.
  14807. (if (and org-calendar-follow-timestamp-change
  14808. (get-buffer-window "*Calendar*" t)
  14809. (memq org-ts-what '(day month year)))
  14810. (org-recenter-calendar (time-to-days time))))))
  14811. (defun org-modify-ts-extra (s pos n dm)
  14812. "Change the different parts of the lead-time and repeat fields in timestamp."
  14813. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14814. ng h m new rem)
  14815. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14816. (cond
  14817. ((or (org-pos-in-match-range pos 2)
  14818. (org-pos-in-match-range pos 3))
  14819. (setq m (string-to-number (match-string 3 s))
  14820. h (string-to-number (match-string 2 s)))
  14821. (if (org-pos-in-match-range pos 2)
  14822. (setq h (+ h n))
  14823. (setq n (* dm (org-no-warnings (signum n))))
  14824. (when (not (= 0 (setq rem (% m dm))))
  14825. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14826. (setq m (+ m n)))
  14827. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14828. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14829. (setq h (min 24 (max 0 h)))
  14830. (setq ng 1 new (format "-%02d:%02d" h m)))
  14831. ((org-pos-in-match-range pos 6)
  14832. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14833. ((org-pos-in-match-range pos 5)
  14834. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14835. ((org-pos-in-match-range pos 9)
  14836. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14837. ((org-pos-in-match-range pos 8)
  14838. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14839. (when ng
  14840. (setq s (concat
  14841. (substring s 0 (match-beginning ng))
  14842. new
  14843. (substring s (match-end ng))))))
  14844. s))
  14845. (defun org-recenter-calendar (date)
  14846. "If the calendar is visible, recenter it to DATE."
  14847. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14848. (when cwin
  14849. (let ((calendar-move-hook nil))
  14850. (with-selected-window cwin
  14851. (calendar-goto-date (if (listp date) date
  14852. (calendar-gregorian-from-absolute date))))))))
  14853. (defun org-goto-calendar (&optional arg)
  14854. "Go to the Emacs calendar at the current date.
  14855. If there is a time stamp in the current line, go to that date.
  14856. A prefix ARG can be used to force the current date."
  14857. (interactive "P")
  14858. (let ((tsr org-ts-regexp) diff
  14859. (calendar-move-hook nil)
  14860. (calendar-view-holidays-initially-flag nil)
  14861. (calendar-view-diary-initially-flag nil))
  14862. (if (or (org-at-timestamp-p)
  14863. (save-excursion
  14864. (beginning-of-line 1)
  14865. (looking-at (concat ".*" tsr))))
  14866. (let ((d1 (time-to-days (current-time)))
  14867. (d2 (time-to-days
  14868. (org-time-string-to-time (match-string 1)))))
  14869. (setq diff (- d2 d1))))
  14870. (calendar)
  14871. (calendar-goto-today)
  14872. (if (and diff (not arg)) (calendar-forward-day diff))))
  14873. (defun org-get-date-from-calendar ()
  14874. "Return a list (month day year) of date at point in calendar."
  14875. (with-current-buffer "*Calendar*"
  14876. (save-match-data
  14877. (calendar-cursor-to-date))))
  14878. (defun org-date-from-calendar ()
  14879. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14880. If there is already a time stamp at the cursor position, update it."
  14881. (interactive)
  14882. (if (org-at-timestamp-p t)
  14883. (org-timestamp-change 0 'calendar)
  14884. (let ((cal-date (org-get-date-from-calendar)))
  14885. (org-insert-time-stamp
  14886. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14887. (defun org-minutes-to-hh:mm-string (m)
  14888. "Compute H:MM from a number of minutes."
  14889. (let ((h (/ m 60)))
  14890. (setq m (- m (* 60 h)))
  14891. (format org-time-clocksum-format h m)))
  14892. (defun org-hh:mm-string-to-minutes (s)
  14893. "Convert a string H:MM to a number of minutes.
  14894. If the string is just a number, interpret it as minutes.
  14895. In fact, the first hh:mm or number in the string will be taken,
  14896. there can be extra stuff in the string.
  14897. If no number is found, the return value is 0."
  14898. (cond
  14899. ((integerp s) s)
  14900. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14901. (+ (* (string-to-number (match-string 1 s)) 60)
  14902. (string-to-number (match-string 2 s))))
  14903. ((string-match "\\([0-9]+\\)" s)
  14904. (string-to-number (match-string 1 s)))
  14905. (t 0)))
  14906. (defcustom org-effort-durations
  14907. `(("h" . 60)
  14908. ("d" . ,(* 60 8))
  14909. ("w" . ,(* 60 8 5))
  14910. ("m" . ,(* 60 8 5 4))
  14911. ("y" . ,(* 60 8 5 40)))
  14912. "Conversion factor to minutes for an effort modifier.
  14913. Each entry has the form (MODIFIER . MINUTES).
  14914. In an effort string, a number followed by MODIFIER is multiplied
  14915. by the specified number of MINUTES to obtain an effort in
  14916. minutes.
  14917. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14918. effort string \"2hours\" is equivalent to 120 minutes."
  14919. :group 'org-agenda
  14920. :version "24.1"
  14921. :type '(alist :key-type (string :tag "Modifier")
  14922. :value-type (number :tag "Minutes")))
  14923. (defun org-duration-string-to-minutes (s)
  14924. "Convert a duration string S to minutes.
  14925. A bare number is interpreted as minutes, modifiers can be set by
  14926. customizing `org-effort-durations' (which see).
  14927. Entries containing a colon are interpreted as H:MM by
  14928. `org-hh:mm-string-to-minutes'."
  14929. (let ((result 0)
  14930. (re (concat "\\([0-9]+\\) *\\("
  14931. (regexp-opt (mapcar 'car org-effort-durations))
  14932. "\\)")))
  14933. (while (string-match re s)
  14934. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14935. (string-to-number (match-string 1 s))))
  14936. (setq s (replace-match "" nil t s)))
  14937. (incf result (org-hh:mm-string-to-minutes s))
  14938. result))
  14939. ;;;; Files
  14940. (defun org-save-all-org-buffers ()
  14941. "Save all Org-mode buffers without user confirmation."
  14942. (interactive)
  14943. (message "Saving all Org-mode buffers...")
  14944. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14945. (when (featurep 'org-id) (org-id-locations-save))
  14946. (message "Saving all Org-mode buffers... done"))
  14947. (defun org-revert-all-org-buffers ()
  14948. "Revert all Org-mode buffers.
  14949. Prompt for confirmation when there are unsaved changes.
  14950. Be sure you know what you are doing before letting this function
  14951. overwrite your changes.
  14952. This function is useful in a setup where one tracks org files
  14953. with a version control system, to revert on one machine after pulling
  14954. changes from another. I believe the procedure must be like this:
  14955. 1. M-x org-save-all-org-buffers
  14956. 2. Pull changes from the other machine, resolve conflicts
  14957. 3. M-x org-revert-all-org-buffers"
  14958. (interactive)
  14959. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14960. (error "Abort"))
  14961. (save-excursion
  14962. (save-window-excursion
  14963. (mapc
  14964. (lambda (b)
  14965. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14966. (with-current-buffer b buffer-file-name))
  14967. (org-pop-to-buffer-same-window b)
  14968. (revert-buffer t 'no-confirm)))
  14969. (buffer-list))
  14970. (when (and (featurep 'org-id) org-id-track-globally)
  14971. (org-id-locations-load)))))
  14972. ;;;; Agenda files
  14973. ;;;###autoload
  14974. (defun org-switchb (&optional arg)
  14975. "Switch between Org buffers.
  14976. With one prefix argument, restrict available buffers to files.
  14977. With two prefix arguments, restrict available buffers to agenda files.
  14978. Defaults to `iswitchb' for buffer name completion.
  14979. Set `org-completion-use-ido' to make it use ido instead."
  14980. (interactive "P")
  14981. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14982. ((equal arg '(16)) (org-buffer-list 'agenda))
  14983. (t (org-buffer-list))))
  14984. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14985. (org-completion-use-ido org-completion-use-ido))
  14986. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14987. (setq org-completion-use-iswitchb t))
  14988. (org-pop-to-buffer-same-window
  14989. (org-icompleting-read "Org buffer: "
  14990. (mapcar 'list (mapcar 'buffer-name blist))
  14991. nil t))))
  14992. ;;; Define some older names previously used for this functionality
  14993. ;;;###autoload
  14994. (defalias 'org-ido-switchb 'org-switchb)
  14995. ;;;###autoload
  14996. (defalias 'org-iswitchb 'org-switchb)
  14997. (defun org-buffer-list (&optional predicate exclude-tmp)
  14998. "Return a list of Org buffers.
  14999. PREDICATE can be `export', `files' or `agenda'.
  15000. export restrict the list to Export buffers.
  15001. files restrict the list to buffers visiting Org files.
  15002. agenda restrict the list to buffers visiting agenda files.
  15003. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15004. (let* ((bfn nil)
  15005. (agenda-files (and (eq predicate 'agenda)
  15006. (mapcar 'file-truename (org-agenda-files t))))
  15007. (filter
  15008. (cond
  15009. ((eq predicate 'files)
  15010. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  15011. ((eq predicate 'export)
  15012. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15013. ((eq predicate 'agenda)
  15014. (lambda (b)
  15015. (with-current-buffer b
  15016. (and (eq major-mode 'org-mode)
  15017. (setq bfn (buffer-file-name b))
  15018. (member (file-truename bfn) agenda-files)))))
  15019. (t (lambda (b) (with-current-buffer b
  15020. (or (eq major-mode 'org-mode)
  15021. (string-match "\*Org .*Export"
  15022. (buffer-name b)))))))))
  15023. (delq nil
  15024. (mapcar
  15025. (lambda(b)
  15026. (if (and (funcall filter b)
  15027. (or (not exclude-tmp)
  15028. (not (string-match "tmp" (buffer-name b)))))
  15029. b
  15030. nil))
  15031. (buffer-list)))))
  15032. (defun org-agenda-files (&optional unrestricted archives)
  15033. "Get the list of agenda files.
  15034. Optional UNRESTRICTED means return the full list even if a restriction
  15035. is currently in place.
  15036. When ARCHIVES is t, include all archive files that are really being
  15037. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15038. `org-agenda-archives-mode' is t."
  15039. (let ((files
  15040. (cond
  15041. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15042. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15043. ((listp org-agenda-files) org-agenda-files)
  15044. (t (error "Invalid value of `org-agenda-files'")))))
  15045. (setq files (apply 'append
  15046. (mapcar (lambda (f)
  15047. (if (file-directory-p f)
  15048. (directory-files
  15049. f t org-agenda-file-regexp)
  15050. (list f)))
  15051. files)))
  15052. (when org-agenda-skip-unavailable-files
  15053. (setq files (delq nil
  15054. (mapcar (function
  15055. (lambda (file)
  15056. (and (file-readable-p file) file)))
  15057. files))))
  15058. (when (or (eq archives t)
  15059. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15060. (setq files (org-add-archive-files files)))
  15061. files))
  15062. (defun org-agenda-file-p (&optional file)
  15063. "Return non-nil, if FILE is an agenda file.
  15064. If FILE is omitted, use the file associated with the current
  15065. buffer."
  15066. (member (or file (buffer-file-name))
  15067. (org-agenda-files t)))
  15068. (defun org-edit-agenda-file-list ()
  15069. "Edit the list of agenda files.
  15070. Depending on setup, this either uses customize to edit the variable
  15071. `org-agenda-files', or it visits the file that is holding the list. In the
  15072. latter case, the buffer is set up in a way that saving it automatically kills
  15073. the buffer and restores the previous window configuration."
  15074. (interactive)
  15075. (if (stringp org-agenda-files)
  15076. (let ((cw (current-window-configuration)))
  15077. (find-file org-agenda-files)
  15078. (org-set-local 'org-window-configuration cw)
  15079. (org-add-hook 'after-save-hook
  15080. (lambda ()
  15081. (set-window-configuration
  15082. (prog1 org-window-configuration
  15083. (kill-buffer (current-buffer))))
  15084. (org-install-agenda-files-menu)
  15085. (message "New agenda file list installed"))
  15086. nil 'local)
  15087. (message "%s" (substitute-command-keys
  15088. "Edit list and finish with \\[save-buffer]")))
  15089. (customize-variable 'org-agenda-files)))
  15090. (defun org-store-new-agenda-file-list (list)
  15091. "Set new value for the agenda file list and save it correctly."
  15092. (if (stringp org-agenda-files)
  15093. (let ((fe (org-read-agenda-file-list t)) b u)
  15094. (while (setq b (find-buffer-visiting org-agenda-files))
  15095. (kill-buffer b))
  15096. (with-temp-file org-agenda-files
  15097. (insert
  15098. (mapconcat
  15099. (lambda (f) ;; Keep un-expanded entries.
  15100. (if (setq u (assoc f fe))
  15101. (cdr u)
  15102. f))
  15103. list "\n")
  15104. "\n")))
  15105. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15106. (org-insert-mode-line-in-empty-file nil))
  15107. (setq org-agenda-files list)
  15108. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15109. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15110. "Read the list of agenda files from a file.
  15111. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15112. filenames, used by `org-store-new-agenda-file-list' to write back
  15113. un-expanded file names."
  15114. (when (file-directory-p org-agenda-files)
  15115. (error "`org-agenda-files' cannot be a single directory"))
  15116. (when (stringp org-agenda-files)
  15117. (with-temp-buffer
  15118. (insert-file-contents org-agenda-files)
  15119. (mapcar
  15120. (lambda (f)
  15121. (let ((e (expand-file-name (substitute-in-file-name f)
  15122. org-directory)))
  15123. (if pair-with-expansion
  15124. (cons e f)
  15125. e)))
  15126. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15127. ;;;###autoload
  15128. (defun org-cycle-agenda-files ()
  15129. "Cycle through the files in `org-agenda-files'.
  15130. If the current buffer visits an agenda file, find the next one in the list.
  15131. If the current buffer does not, find the first agenda file."
  15132. (interactive)
  15133. (let* ((fs (org-agenda-files t))
  15134. (files (append fs (list (car fs))))
  15135. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15136. file)
  15137. (unless files (error "No agenda files"))
  15138. (catch 'exit
  15139. (while (setq file (pop files))
  15140. (if (equal (file-truename file) tcf)
  15141. (when (car files)
  15142. (find-file (car files))
  15143. (throw 'exit t))))
  15144. (find-file (car fs)))
  15145. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15146. (defun org-agenda-file-to-front (&optional to-end)
  15147. "Move/add the current file to the top of the agenda file list.
  15148. If the file is not present in the list, it is added to the front. If it is
  15149. present, it is moved there. With optional argument TO-END, add/move to the
  15150. end of the list."
  15151. (interactive "P")
  15152. (let ((org-agenda-skip-unavailable-files nil)
  15153. (file-alist (mapcar (lambda (x)
  15154. (cons (file-truename x) x))
  15155. (org-agenda-files t)))
  15156. (ctf (file-truename buffer-file-name))
  15157. x had)
  15158. (setq x (assoc ctf file-alist) had x)
  15159. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15160. (if to-end
  15161. (setq file-alist (append (delq x file-alist) (list x)))
  15162. (setq file-alist (cons x (delq x file-alist))))
  15163. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15164. (org-install-agenda-files-menu)
  15165. (message "File %s to %s of agenda file list"
  15166. (if had "moved" "added") (if to-end "end" "front"))))
  15167. (defun org-remove-file (&optional file)
  15168. "Remove current file from the list of files in variable `org-agenda-files'.
  15169. These are the files which are being checked for agenda entries.
  15170. Optional argument FILE means use this file instead of the current."
  15171. (interactive)
  15172. (let* ((org-agenda-skip-unavailable-files nil)
  15173. (file (or file buffer-file-name))
  15174. (true-file (file-truename file))
  15175. (afile (abbreviate-file-name file))
  15176. (files (delq nil (mapcar
  15177. (lambda (x)
  15178. (if (equal true-file
  15179. (file-truename x))
  15180. nil x))
  15181. (org-agenda-files t)))))
  15182. (if (not (= (length files) (length (org-agenda-files t))))
  15183. (progn
  15184. (org-store-new-agenda-file-list files)
  15185. (org-install-agenda-files-menu)
  15186. (message "Removed file: %s" afile))
  15187. (message "File was not in list: %s (not removed)" afile))))
  15188. (defun org-file-menu-entry (file)
  15189. (vector file (list 'find-file file) t))
  15190. (defun org-check-agenda-file (file)
  15191. "Make sure FILE exists. If not, ask user what to do."
  15192. (when (not (file-exists-p file))
  15193. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15194. (abbreviate-file-name file))
  15195. (let ((r (downcase (read-char-exclusive))))
  15196. (cond
  15197. ((equal r ?r)
  15198. (org-remove-file file)
  15199. (throw 'nextfile t))
  15200. (t (error "Abort"))))))
  15201. (defun org-get-agenda-file-buffer (file)
  15202. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15203. it to the list of buffers which might be released later."
  15204. (let ((buf (org-find-base-buffer-visiting file)))
  15205. (if buf
  15206. buf ; just return it
  15207. ;; Make a new buffer and remember it
  15208. (setq buf (find-file-noselect file))
  15209. (if buf (push buf org-agenda-new-buffers))
  15210. buf)))
  15211. (defun org-release-buffers (blist)
  15212. "Release all buffers in list, asking the user for confirmation when needed.
  15213. When a buffer is unmodified, it is just killed. When modified, it is saved
  15214. \(if the user agrees) and then killed."
  15215. (let (buf file)
  15216. (while (setq buf (pop blist))
  15217. (setq file (buffer-file-name buf))
  15218. (when (and (buffer-modified-p buf)
  15219. file
  15220. (y-or-n-p (format "Save file %s? " file)))
  15221. (with-current-buffer buf (save-buffer)))
  15222. (kill-buffer buf))))
  15223. (defun org-prepare-agenda-buffers (files)
  15224. "Create buffers for all agenda files, protect archived trees and comments."
  15225. (interactive)
  15226. (let ((pa '(:org-archived t))
  15227. (pc '(:org-comment t))
  15228. (pall '(:org-archived t :org-comment t))
  15229. (inhibit-read-only t)
  15230. (rea (concat ":" org-archive-tag ":"))
  15231. bmp file re)
  15232. (save-excursion
  15233. (save-restriction
  15234. (while (setq file (pop files))
  15235. (catch 'nextfile
  15236. (if (bufferp file)
  15237. (set-buffer file)
  15238. (org-check-agenda-file file)
  15239. (set-buffer (org-get-agenda-file-buffer file)))
  15240. (widen)
  15241. (setq bmp (buffer-modified-p))
  15242. (org-refresh-category-properties)
  15243. (setq org-todo-keywords-for-agenda
  15244. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15245. (setq org-done-keywords-for-agenda
  15246. (append org-done-keywords-for-agenda org-done-keywords))
  15247. (setq org-todo-keyword-alist-for-agenda
  15248. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15249. (setq org-drawers-for-agenda
  15250. (append org-drawers-for-agenda org-drawers))
  15251. (setq org-tag-alist-for-agenda
  15252. (append org-tag-alist-for-agenda org-tag-alist))
  15253. (save-excursion
  15254. (remove-text-properties (point-min) (point-max) pall)
  15255. (when org-agenda-skip-archived-trees
  15256. (goto-char (point-min))
  15257. (while (re-search-forward rea nil t)
  15258. (if (org-at-heading-p t)
  15259. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15260. (goto-char (point-min))
  15261. (setq re (format org-heading-keyword-regexp-format
  15262. org-comment-string))
  15263. (while (re-search-forward re nil t)
  15264. (add-text-properties
  15265. (match-beginning 0) (org-end-of-subtree t) pc)))
  15266. (set-buffer-modified-p bmp)))))
  15267. (setq org-todo-keywords-for-agenda
  15268. (org-uniquify org-todo-keywords-for-agenda))
  15269. (setq org-todo-keyword-alist-for-agenda
  15270. (org-uniquify org-todo-keyword-alist-for-agenda)
  15271. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15272. ;;;; Embedded LaTeX
  15273. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15274. "Keymap for the minor `org-cdlatex-mode'.")
  15275. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15276. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15277. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15278. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15279. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15280. (defvar org-cdlatex-texmathp-advice-is-done nil
  15281. "Flag remembering if we have applied the advice to texmathp already.")
  15282. (define-minor-mode org-cdlatex-mode
  15283. "Toggle the minor `org-cdlatex-mode'.
  15284. This mode supports entering LaTeX environment and math in LaTeX fragments
  15285. in Org-mode.
  15286. \\{org-cdlatex-mode-map}"
  15287. nil " OCDL" nil
  15288. (when org-cdlatex-mode
  15289. (require 'cdlatex)
  15290. (run-hooks 'cdlatex-mode-hook)
  15291. (cdlatex-compute-tables))
  15292. (unless org-cdlatex-texmathp-advice-is-done
  15293. (setq org-cdlatex-texmathp-advice-is-done t)
  15294. (defadvice texmathp (around org-math-always-on activate)
  15295. "Always return t in org-mode buffers.
  15296. This is because we want to insert math symbols without dollars even outside
  15297. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15298. an embedded LaTeX fragment, let texmathp do its job.
  15299. \\[org-cdlatex-mode-map]"
  15300. (interactive)
  15301. (let (p)
  15302. (cond
  15303. ((not (eq major-mode 'org-mode)) ad-do-it)
  15304. ((eq this-command 'cdlatex-math-symbol)
  15305. (setq ad-return-value t
  15306. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15307. (t
  15308. (let ((p (org-inside-LaTeX-fragment-p)))
  15309. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15310. (setq ad-return-value t
  15311. texmathp-why '("Org-mode embedded math" . 0))
  15312. (if p ad-do-it)))))))))
  15313. (defun turn-on-org-cdlatex ()
  15314. "Unconditionally turn on `org-cdlatex-mode'."
  15315. (org-cdlatex-mode 1))
  15316. (defun org-inside-LaTeX-fragment-p ()
  15317. "Test if point is inside a LaTeX fragment.
  15318. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15319. sequence appearing also before point.
  15320. Even though the matchers for math are configurable, this function assumes
  15321. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15322. delimiters are skipped when they have been removed by customization.
  15323. The return value is nil, or a cons cell with the delimiter and the
  15324. position of this delimiter.
  15325. This function does a reasonably good job, but can locally be fooled by
  15326. for example currency specifications. For example it will assume being in
  15327. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15328. fragments that are properly closed, but during editing, we have to live
  15329. with the uncertainty caused by missing closing delimiters. This function
  15330. looks only before point, not after."
  15331. (catch 'exit
  15332. (let ((pos (point))
  15333. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15334. (lim (progn
  15335. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15336. (point)))
  15337. dd-on str (start 0) m re)
  15338. (goto-char pos)
  15339. (when dodollar
  15340. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15341. re (nth 1 (assoc "$" org-latex-regexps)))
  15342. (while (string-match re str start)
  15343. (cond
  15344. ((= (match-end 0) (length str))
  15345. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15346. ((= (match-end 0) (- (length str) 5))
  15347. (throw 'exit nil))
  15348. (t (setq start (match-end 0))))))
  15349. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15350. (goto-char pos)
  15351. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15352. (and (match-beginning 2) (throw 'exit nil))
  15353. ;; count $$
  15354. (while (re-search-backward "\\$\\$" lim t)
  15355. (setq dd-on (not dd-on)))
  15356. (goto-char pos)
  15357. (if dd-on (cons "$$" m))))))
  15358. (defun org-inside-latex-macro-p ()
  15359. "Is point inside a LaTeX macro or its arguments?"
  15360. (save-match-data
  15361. (org-in-regexp
  15362. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15363. (defun org-try-cdlatex-tab ()
  15364. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15365. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15366. - inside a LaTeX fragment, or
  15367. - after the first word in a line, where an abbreviation expansion could
  15368. insert a LaTeX environment."
  15369. (when org-cdlatex-mode
  15370. (cond
  15371. ;; Before any word on the line: No expansion possible.
  15372. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15373. ;; Just after first word on the line: Expand it. Make sure it
  15374. ;; cannot happen on headlines, though.
  15375. ((save-excursion
  15376. (skip-chars-backward "a-zA-Z0-9*")
  15377. (skip-chars-backward " \t")
  15378. (and (bolp) (not (org-at-heading-p))))
  15379. (cdlatex-tab) t)
  15380. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15381. (defun org-cdlatex-underscore-caret (&optional arg)
  15382. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15383. Revert to the normal definition outside of these fragments."
  15384. (interactive "P")
  15385. (if (org-inside-LaTeX-fragment-p)
  15386. (call-interactively 'cdlatex-sub-superscript)
  15387. (let (org-cdlatex-mode)
  15388. (call-interactively (key-binding (vector last-input-event))))))
  15389. (defun org-cdlatex-math-modify (&optional arg)
  15390. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15391. Revert to the normal definition outside of these fragments."
  15392. (interactive "P")
  15393. (if (org-inside-LaTeX-fragment-p)
  15394. (call-interactively 'cdlatex-math-modify)
  15395. (let (org-cdlatex-mode)
  15396. (call-interactively (key-binding (vector last-input-event))))))
  15397. (defvar org-latex-fragment-image-overlays nil
  15398. "List of overlays carrying the images of latex fragments.")
  15399. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15400. (defun org-remove-latex-fragment-image-overlays ()
  15401. "Remove all overlays with LaTeX fragment images in current buffer."
  15402. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15403. (setq org-latex-fragment-image-overlays nil))
  15404. (defun org-preview-latex-fragment (&optional subtree)
  15405. "Preview the LaTeX fragment at point, or all locally or globally.
  15406. If the cursor is in a LaTeX fragment, create the image and overlay
  15407. it over the source code. If there is no fragment at point, display
  15408. all fragments in the current text, from one headline to the next. With
  15409. prefix SUBTREE, display all fragments in the current subtree. With a
  15410. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15411. the cursor is before the first headline,
  15412. display all fragments in the buffer.
  15413. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15414. (interactive "P")
  15415. (unless buffer-file-name
  15416. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15417. (org-remove-latex-fragment-image-overlays)
  15418. (save-excursion
  15419. (save-restriction
  15420. (let (beg end at msg)
  15421. (cond
  15422. ((or (equal subtree '(16))
  15423. (not (save-excursion
  15424. (re-search-backward org-outline-regexp-bol nil t))))
  15425. (setq beg (point-min) end (point-max)
  15426. msg "Creating images for buffer...%s"))
  15427. ((equal subtree '(4))
  15428. (org-back-to-heading)
  15429. (setq beg (point) end (org-end-of-subtree t)
  15430. msg "Creating images for subtree...%s"))
  15431. (t
  15432. (if (setq at (org-inside-LaTeX-fragment-p))
  15433. (goto-char (max (point-min) (- (cdr at) 2)))
  15434. (org-back-to-heading))
  15435. (setq beg (point) end (progn (outline-next-heading) (point))
  15436. msg (if at "Creating image...%s"
  15437. "Creating images for entry...%s"))))
  15438. (message msg "")
  15439. (narrow-to-region beg end)
  15440. (goto-char beg)
  15441. (org-format-latex
  15442. (concat "ltxpng/" (file-name-sans-extension
  15443. (file-name-nondirectory
  15444. buffer-file-name)))
  15445. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15446. (message msg "done. Use `C-c C-c' to remove images.")))))
  15447. (defvar org-latex-regexps
  15448. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15449. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15450. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15451. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15452. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15453. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15454. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15455. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15456. "Regular expressions for matching embedded LaTeX.")
  15457. (defvar org-export-have-math nil) ;; dynamic scoping
  15458. (defun org-format-latex (prefix &optional dir overlays msg at
  15459. forbuffer processing-type)
  15460. "Replace LaTeX fragments with links to an image, and produce images.
  15461. Some of the options can be changed using the variable
  15462. `org-format-latex-options'."
  15463. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15464. (let* ((prefixnodir (file-name-nondirectory prefix))
  15465. (absprefix (expand-file-name prefix dir))
  15466. (todir (file-name-directory absprefix))
  15467. (opt org-format-latex-options)
  15468. (matchers (plist-get opt :matchers))
  15469. (re-list org-latex-regexps)
  15470. (org-format-latex-header-extra
  15471. (plist-get (org-infile-export-plist) :latex-header-extra))
  15472. (cnt 0) txt hash link beg end re e checkdir
  15473. executables-checked string
  15474. m n block-type block linkfile movefile ov)
  15475. ;; Check the different regular expressions
  15476. (while (setq e (pop re-list))
  15477. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15478. block (if block-type "\n\n" ""))
  15479. (when (member m matchers)
  15480. (goto-char (point-min))
  15481. (while (re-search-forward re nil t)
  15482. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15483. (not (get-text-property (match-beginning n)
  15484. 'org-protected))
  15485. (or (not overlays)
  15486. (not (eq (get-char-property (match-beginning n)
  15487. 'org-overlay-type)
  15488. 'org-latex-overlay))))
  15489. (setq org-export-have-math t)
  15490. (cond
  15491. ((eq processing-type 'verbatim)
  15492. ;; Leave the text verbatim, just protect it
  15493. (add-text-properties (match-beginning n) (match-end n)
  15494. '(org-protected t)))
  15495. ((eq processing-type 'mathjax)
  15496. ;; Prepare for MathJax processing
  15497. (setq string (match-string n))
  15498. (if (member m '("$" "$1"))
  15499. (save-excursion
  15500. (delete-region (match-beginning n) (match-end n))
  15501. (goto-char (match-beginning n))
  15502. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15503. "\\)")
  15504. '(org-protected t))))
  15505. (add-text-properties (match-beginning n) (match-end n)
  15506. '(org-protected t))))
  15507. ((eq processing-type 'dvipng)
  15508. ;; Process to an image
  15509. (setq txt (match-string n)
  15510. beg (match-beginning n) end (match-end n)
  15511. cnt (1+ cnt))
  15512. (let (print-length print-level) ; make sure full list is printed
  15513. (setq hash (sha1 (prin1-to-string
  15514. (list org-format-latex-header
  15515. org-format-latex-header-extra
  15516. org-export-latex-default-packages-alist
  15517. org-export-latex-packages-alist
  15518. org-format-latex-options
  15519. forbuffer txt)))
  15520. linkfile (format "%s_%s.png" prefix hash)
  15521. movefile (format "%s_%s.png" absprefix hash)))
  15522. (setq link (concat block "[[file:" linkfile "]]" block))
  15523. (if msg (message msg cnt))
  15524. (goto-char beg)
  15525. (unless checkdir ; make sure the directory exists
  15526. (setq checkdir t)
  15527. (or (file-directory-p todir) (make-directory todir t)))
  15528. (unless executables-checked
  15529. (org-check-external-command
  15530. "latex" "needed to convert LaTeX fragments to images")
  15531. (org-check-external-command
  15532. "dvipng" "needed to convert LaTeX fragments to images")
  15533. (setq executables-checked t))
  15534. (unless (file-exists-p movefile)
  15535. (org-create-formula-image
  15536. txt movefile opt forbuffer))
  15537. (if overlays
  15538. (progn
  15539. (mapc (lambda (o)
  15540. (if (eq (overlay-get o 'org-overlay-type)
  15541. 'org-latex-overlay)
  15542. (delete-overlay o)))
  15543. (overlays-in beg end))
  15544. (setq ov (make-overlay beg end))
  15545. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15546. (if (featurep 'xemacs)
  15547. (progn
  15548. (overlay-put ov 'invisible t)
  15549. (overlay-put
  15550. ov 'end-glyph
  15551. (make-glyph (vector 'png :file movefile))))
  15552. (overlay-put
  15553. ov 'display
  15554. (list 'image :type 'png :file movefile :ascent 'center)))
  15555. (push ov org-latex-fragment-image-overlays)
  15556. (goto-char end))
  15557. (delete-region beg end)
  15558. (insert (org-add-props link
  15559. (list 'org-latex-src
  15560. (replace-regexp-in-string
  15561. "\"" "" txt)
  15562. 'org-latex-src-embed-type
  15563. (if block-type 'paragraph 'character))))))
  15564. ((eq processing-type 'mathml)
  15565. ;; Process to MathML
  15566. (unless executables-checked
  15567. (unless (save-match-data (org-format-latex-mathml-available-p))
  15568. (error "LaTeX to MathML converter not configured"))
  15569. (setq executables-checked t))
  15570. (setq txt (match-string n)
  15571. beg (match-beginning n) end (match-end n)
  15572. cnt (1+ cnt))
  15573. (if msg (message msg cnt))
  15574. (goto-char beg)
  15575. (delete-region beg end)
  15576. (insert (org-format-latex-as-mathml
  15577. txt block-type prefix dir)))
  15578. (t
  15579. (error "Unknown conversion type %s for latex fragments"
  15580. processing-type)))))))))
  15581. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15582. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15583. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15584. sucessful, return the portion between \"<math...> </math>\"
  15585. elements otherwise return nil. When MATHML-FILE is specified,
  15586. write the results in to that file. When invoked as an
  15587. interactive command, prompt for LATEX-FRAG, with initial value
  15588. set to the current active region and echo the results for user
  15589. inspection."
  15590. (interactive (list (let ((frag (when (region-active-p)
  15591. (buffer-substring-no-properties
  15592. (region-beginning) (region-end)))))
  15593. (read-string "LaTeX Fragment: " frag nil frag))))
  15594. (unless latex-frag (error "Invalid latex-frag"))
  15595. (let* ((tmp-in-file (file-relative-name
  15596. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15597. (ignore (write-region latex-frag nil tmp-in-file))
  15598. (tmp-out-file (file-relative-name
  15599. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15600. (cmd (format-spec
  15601. org-latex-to-mathml-convert-command
  15602. `((?j . ,(shell-quote-argument
  15603. (expand-file-name org-latex-to-mathml-jar-file)))
  15604. (?I . ,(shell-quote-argument tmp-in-file))
  15605. (?o . ,(shell-quote-argument tmp-out-file)))))
  15606. mathml shell-command-output)
  15607. (when (org-called-interactively-p 'any)
  15608. (unless (org-format-latex-mathml-available-p)
  15609. (error "LaTeX to MathML converter not configured")))
  15610. (message "Running %s" cmd)
  15611. (setq shell-command-output (shell-command-to-string cmd))
  15612. (setq mathml
  15613. (when (file-readable-p tmp-out-file)
  15614. (with-current-buffer (find-file-noselect tmp-out-file t)
  15615. (goto-char (point-min))
  15616. (when (re-search-forward
  15617. (concat
  15618. (regexp-quote
  15619. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15620. "\\(.\\|\n\\)*"
  15621. (regexp-quote "</math>")) nil t)
  15622. (prog1 (match-string 0) (kill-buffer))))))
  15623. (cond
  15624. (mathml
  15625. (setq mathml
  15626. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15627. (when mathml-file
  15628. (write-region mathml nil mathml-file))
  15629. (when (org-called-interactively-p 'any)
  15630. (message mathml)))
  15631. ((message "LaTeX to MathML conversion failed")
  15632. (message shell-command-output)))
  15633. (delete-file tmp-in-file)
  15634. (when (file-exists-p tmp-out-file)
  15635. (delete-file tmp-out-file))
  15636. mathml))
  15637. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15638. prefix &optional dir)
  15639. "Use `org-create-math-formula' but check local cache first."
  15640. (let* ((absprefix (expand-file-name prefix dir))
  15641. (print-length nil) (print-level nil)
  15642. (formula-id (concat
  15643. "formula-"
  15644. (sha1
  15645. (prin1-to-string
  15646. (list latex-frag
  15647. org-latex-to-mathml-convert-command)))))
  15648. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15649. (formula-cache-dir (file-name-directory formula-cache)))
  15650. (unless (file-directory-p formula-cache-dir)
  15651. (make-directory formula-cache-dir t))
  15652. (unless (file-exists-p formula-cache)
  15653. (org-create-math-formula latex-frag formula-cache))
  15654. (if (file-exists-p formula-cache)
  15655. ;; Successful conversion. Return the link to MathML file.
  15656. (org-add-props
  15657. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15658. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15659. 'org-latex-src-embed-type (if latex-frag-type
  15660. 'paragraph 'character)))
  15661. ;; Failed conversion. Return the LaTeX fragment verbatim
  15662. (add-text-properties
  15663. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15664. latex-frag)))
  15665. ;; This function borrows from Ganesh Swami's latex2png.el
  15666. (defun org-create-formula-image (string tofile options buffer)
  15667. "This calls dvipng."
  15668. (require 'org-latex)
  15669. (let* ((tmpdir (if (featurep 'xemacs)
  15670. (temp-directory)
  15671. temporary-file-directory))
  15672. (texfilebase (make-temp-name
  15673. (expand-file-name "orgtex" tmpdir)))
  15674. (texfile (concat texfilebase ".tex"))
  15675. (dvifile (concat texfilebase ".dvi"))
  15676. (pngfile (concat texfilebase ".png"))
  15677. (fnh (if (featurep 'xemacs)
  15678. (font-height (get-face-font 'default))
  15679. (face-attribute 'default :height nil)))
  15680. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15681. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15682. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15683. "Black"))
  15684. (bg (or (plist-get options (if buffer :background :html-background))
  15685. "Transparent")))
  15686. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15687. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15688. (with-temp-file texfile
  15689. (insert (org-splice-latex-header
  15690. org-format-latex-header
  15691. org-export-latex-default-packages-alist
  15692. org-export-latex-packages-alist t
  15693. org-format-latex-header-extra))
  15694. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15695. (require 'org-latex)
  15696. (org-export-latex-fix-inputenc))
  15697. (let ((dir default-directory))
  15698. (condition-case nil
  15699. (progn
  15700. (cd tmpdir)
  15701. (call-process "latex" nil nil nil texfile))
  15702. (error nil))
  15703. (cd dir))
  15704. (if (not (file-exists-p dvifile))
  15705. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15706. (condition-case nil
  15707. (call-process "dvipng" nil nil nil
  15708. "-fg" fg "-bg" bg
  15709. "-D" dpi
  15710. ;;"-x" scale "-y" scale
  15711. "-T" "tight"
  15712. "-o" pngfile
  15713. dvifile)
  15714. (error nil))
  15715. (if (not (file-exists-p pngfile))
  15716. (if org-format-latex-signal-error
  15717. (error "Failed to create png file from %s" texfile)
  15718. (message "Failed to create png file from %s" texfile)
  15719. nil)
  15720. ;; Use the requested file name and clean up
  15721. (copy-file pngfile tofile 'replace)
  15722. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15723. (delete-file (concat texfilebase e)))
  15724. pngfile))))
  15725. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15726. "Fill a LaTeX header template TPL.
  15727. In the template, the following place holders will be recognized:
  15728. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15729. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15730. [PACKAGES] \\usepackage statements for PKG
  15731. [NO-PACKAGES] do not include PKG
  15732. [EXTRA] the string EXTRA
  15733. [NO-EXTRA] do not include EXTRA
  15734. For backward compatibility, if both the positive and the negative place
  15735. holder is missing, the positive one (without the \"NO-\") will be
  15736. assumed to be present at the end of the template.
  15737. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15738. EXTRA is a string.
  15739. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15740. (let (rpl (end ""))
  15741. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15742. (setq rpl (if (or (match-end 1) (not def-pkg))
  15743. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15744. tpl (replace-match rpl t t tpl))
  15745. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15746. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15747. (setq rpl (if (or (match-end 1) (not pkg))
  15748. "" (org-latex-packages-to-string pkg snippets-p t))
  15749. tpl (replace-match rpl t t tpl))
  15750. (if pkg (setq end
  15751. (concat end "\n"
  15752. (org-latex-packages-to-string pkg snippets-p)))))
  15753. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15754. (setq rpl (if (or (match-end 1) (not extra))
  15755. "" (concat extra "\n"))
  15756. tpl (replace-match rpl t t tpl))
  15757. (if (and extra (string-match "\\S-" extra))
  15758. (setq end (concat end "\n" extra))))
  15759. (if (string-match "\\S-" end)
  15760. (concat tpl "\n" end)
  15761. tpl)))
  15762. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15763. "Turn an alist of packages into a string with the \\usepackage macros."
  15764. (setq pkg (mapconcat (lambda(p)
  15765. (cond
  15766. ((stringp p) p)
  15767. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15768. (format "%% Package %s omitted" (cadr p)))
  15769. ((equal "" (car p))
  15770. (format "\\usepackage{%s}" (cadr p)))
  15771. (t
  15772. (format "\\usepackage[%s]{%s}"
  15773. (car p) (cadr p)))))
  15774. pkg
  15775. "\n"))
  15776. (if newline (concat pkg "\n") pkg))
  15777. (defun org-dvipng-color (attr)
  15778. "Return an rgb color specification for dvipng."
  15779. (apply 'format "rgb %s %s %s"
  15780. (mapcar 'org-normalize-color
  15781. (color-values (face-attribute 'default attr nil)))))
  15782. (defun org-normalize-color (value)
  15783. "Return string to be used as color value for an RGB component."
  15784. (format "%g" (/ value 65535.0)))
  15785. ;; Image display
  15786. (defvar org-inline-image-overlays nil)
  15787. (make-variable-buffer-local 'org-inline-image-overlays)
  15788. (defun org-toggle-inline-images (&optional include-linked)
  15789. "Toggle the display of inline images.
  15790. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15791. (interactive "P")
  15792. (if org-inline-image-overlays
  15793. (progn
  15794. (org-remove-inline-images)
  15795. (message "Inline image display turned off"))
  15796. (org-display-inline-images include-linked)
  15797. (if org-inline-image-overlays
  15798. (message "%d images displayed inline"
  15799. (length org-inline-image-overlays))
  15800. (message "No images to display inline"))))
  15801. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15802. "Display inline images.
  15803. Normally only links without a description part are inlined, because this
  15804. is how it will work for export. When INCLUDE-LINKED is set, also links
  15805. with a description part will be inlined. This can be nice for a quick
  15806. look at those images, but it does not reflect what exported files will look
  15807. like.
  15808. When REFRESH is set, refresh existing images between BEG and END.
  15809. This will create new image displays only if necessary.
  15810. BEG and END default to the buffer boundaries."
  15811. (interactive "P")
  15812. (unless refresh
  15813. (org-remove-inline-images)
  15814. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15815. (save-excursion
  15816. (save-restriction
  15817. (widen)
  15818. (setq beg (or beg (point-min)) end (or end (point-max)))
  15819. (goto-char (point-min))
  15820. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15821. (substring (org-image-file-name-regexp) 0 -2)
  15822. "\\)\\]" (if include-linked "" "\\]")))
  15823. old file ov img)
  15824. (while (re-search-forward re end t)
  15825. (setq old (get-char-property-and-overlay (match-beginning 1)
  15826. 'org-image-overlay))
  15827. (setq file (expand-file-name
  15828. (concat (or (match-string 3) "") (match-string 4))))
  15829. (when (file-exists-p file)
  15830. (if (and (car-safe old) refresh)
  15831. (image-refresh (overlay-get (cdr old) 'display))
  15832. (setq img (save-match-data (create-image file)))
  15833. (when img
  15834. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15835. (overlay-put ov 'display img)
  15836. (overlay-put ov 'face 'default)
  15837. (overlay-put ov 'org-image-overlay t)
  15838. (overlay-put ov 'modification-hooks
  15839. (list 'org-display-inline-modification-hook))
  15840. (push ov org-inline-image-overlays)))))))))
  15841. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15842. "Remove inline-display overlay if a corresponding region is modified."
  15843. (let ((inhibit-modification-hooks t))
  15844. (when (and ov after)
  15845. (delete ov org-inline-image-overlays)
  15846. (delete-overlay ov))))
  15847. (defun org-remove-inline-images ()
  15848. "Remove inline display of images."
  15849. (interactive)
  15850. (mapc 'delete-overlay org-inline-image-overlays)
  15851. (setq org-inline-image-overlays nil))
  15852. ;;;; Key bindings
  15853. ;; Outline functions from `outline-mode-prefix-map'
  15854. ;; that can be remapped in Org:
  15855. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15856. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15857. (define-key org-mode-map [remap outline-forward-same-level]
  15858. 'org-forward-same-level)
  15859. (define-key org-mode-map [remap outline-backward-same-level]
  15860. 'org-backward-same-level)
  15861. (define-key org-mode-map [remap show-branches]
  15862. 'org-kill-note-or-show-branches)
  15863. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15864. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15865. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15866. ;; Outline functions from `outline-mode-prefix-map'
  15867. ;; that can not be remapped in Org:
  15868. ;; - the column "key binding" shows whether the Outline function is still
  15869. ;; available in Org mode on the same key that it has been bound to in
  15870. ;; Outline mode:
  15871. ;; - "overridden": key used for a different functionality in Org mode
  15872. ;; - else: key still bound to the same Outline function in Org mode
  15873. ;; | Outline function | key binding | Org replacement |
  15874. ;; |------------------------------------+-------------+-----------------------|
  15875. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15876. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15877. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15878. ;; | `hide-subtree' | overridden | visibility cycling |
  15879. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15880. ;; | `hide-body' | overridden | no replacement |
  15881. ;; | `show-all' | overridden | no replacement |
  15882. ;; | `hide-entry' | overridden | visibility cycling |
  15883. ;; | `show-entry' | overridden | no replacement |
  15884. ;; | `hide-leaves' | overridden | no replacement |
  15885. ;; | `hide-sublevels' | overridden | no replacement |
  15886. ;; | `hide-other' | overridden | no replacement |
  15887. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15888. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15889. ;; Make `C-c C-x' a prefix key
  15890. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15891. ;; TAB key with modifiers
  15892. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15893. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15894. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15895. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15896. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15897. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15898. ;; The following line is necessary under Suse GNU/Linux
  15899. (unless (featurep 'xemacs)
  15900. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15901. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15902. (define-key org-mode-map [backtab] 'org-shifttab)
  15903. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15904. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15905. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15906. ;; Cursor keys with modifiers
  15907. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15908. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15909. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15910. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15911. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15912. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15913. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15914. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15915. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15916. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15917. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15918. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15919. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15920. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15921. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15922. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15923. ;; Babel keys
  15924. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15925. (mapc (lambda (pair)
  15926. (define-key org-babel-map (car pair) (cdr pair)))
  15927. org-babel-key-bindings)
  15928. ;;; Extra keys for tty access.
  15929. ;; We only set them when really needed because otherwise the
  15930. ;; menus don't show the simple keys
  15931. (when (or org-use-extra-keys
  15932. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15933. (not window-system))
  15934. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15935. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15936. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15937. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15938. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15939. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15940. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15941. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15942. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15943. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15944. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15945. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15946. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15947. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15948. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15949. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15950. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15951. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15952. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15953. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15954. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15955. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15956. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15957. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15958. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15959. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15960. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15961. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15962. ;; All the other keys
  15963. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15964. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15965. (if (boundp 'narrow-map)
  15966. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15967. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15968. (if (boundp 'narrow-map)
  15969. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15970. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15971. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15972. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15973. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15974. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15975. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15976. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15977. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15978. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15979. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15980. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15981. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15982. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15983. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15984. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15985. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15986. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15987. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15988. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15989. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15990. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15991. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15992. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15993. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15994. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15995. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15996. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15997. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15998. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15999. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16000. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16001. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16002. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16003. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16004. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16005. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16006. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16007. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16008. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16009. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16010. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16011. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16012. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16013. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16014. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16015. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16016. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16017. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16018. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16019. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16020. (org-defkey org-mode-map "\C-m" 'org-return)
  16021. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16022. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16023. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16024. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16025. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16026. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16027. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16028. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16029. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16030. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16031. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16032. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16033. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16034. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16035. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16036. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16037. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16038. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16039. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16040. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16041. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16042. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16043. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16044. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16045. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16046. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16047. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16048. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16049. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16050. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16051. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16052. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16053. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16054. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16055. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16056. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16057. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16058. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16059. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16060. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16061. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16062. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16063. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16064. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16065. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16066. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16067. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16068. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16069. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16070. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16071. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16072. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16073. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16074. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16075. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16076. (when (featurep 'xemacs)
  16077. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16078. (defconst org-speed-commands-default
  16079. '(
  16080. ("Outline Navigation")
  16081. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16082. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16083. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16084. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16085. ("u" . (org-speed-move-safe 'outline-up-heading))
  16086. ("j" . org-goto)
  16087. ("g" . (org-refile t))
  16088. ("Outline Visibility")
  16089. ("c" . org-cycle)
  16090. ("C" . org-shifttab)
  16091. (" " . org-display-outline-path)
  16092. ("Outline Structure Editing")
  16093. ("U" . org-shiftmetaup)
  16094. ("D" . org-shiftmetadown)
  16095. ("r" . org-metaright)
  16096. ("l" . org-metaleft)
  16097. ("R" . org-shiftmetaright)
  16098. ("L" . org-shiftmetaleft)
  16099. ("i" . (progn (forward-char 1) (call-interactively
  16100. 'org-insert-heading-respect-content)))
  16101. ("^" . org-sort)
  16102. ("w" . org-refile)
  16103. ("a" . org-archive-subtree-default-with-confirmation)
  16104. ("." . org-mark-subtree)
  16105. ("Clock Commands")
  16106. ("I" . org-clock-in)
  16107. ("O" . org-clock-out)
  16108. ("Meta Data Editing")
  16109. ("t" . org-todo)
  16110. ("0" . (org-priority ?\ ))
  16111. ("1" . (org-priority ?A))
  16112. ("2" . (org-priority ?B))
  16113. ("3" . (org-priority ?C))
  16114. (";" . org-set-tags-command)
  16115. ("e" . org-set-effort)
  16116. ("Agenda Views etc")
  16117. ("v" . org-agenda)
  16118. ("/" . org-sparse-tree)
  16119. ("Misc")
  16120. ("o" . org-open-at-point)
  16121. ("?" . org-speed-command-help)
  16122. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16123. (">" . (org-agenda-remove-restriction-lock))
  16124. )
  16125. "The default speed commands.")
  16126. (defun org-print-speed-command (e)
  16127. (if (> (length (car e)) 1)
  16128. (progn
  16129. (princ "\n")
  16130. (princ (car e))
  16131. (princ "\n")
  16132. (princ (make-string (length (car e)) ?-))
  16133. (princ "\n"))
  16134. (princ (car e))
  16135. (princ " ")
  16136. (if (symbolp (cdr e))
  16137. (princ (symbol-name (cdr e)))
  16138. (prin1 (cdr e)))
  16139. (princ "\n")))
  16140. (defun org-speed-command-help ()
  16141. "Show the available speed commands."
  16142. (interactive)
  16143. (if (not org-use-speed-commands)
  16144. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16145. (with-output-to-temp-buffer "*Help*"
  16146. (princ "User-defined Speed commands\n===========================\n")
  16147. (mapc 'org-print-speed-command org-speed-commands-user)
  16148. (princ "\n")
  16149. (princ "Built-in Speed commands\n=======================\n")
  16150. (mapc 'org-print-speed-command org-speed-commands-default))
  16151. (with-current-buffer "*Help*"
  16152. (setq truncate-lines t))))
  16153. (defun org-speed-move-safe (cmd)
  16154. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16155. If not, return to the original position and throw an error."
  16156. (interactive)
  16157. (let ((pos (point)))
  16158. (call-interactively cmd)
  16159. (unless (and (bolp) (org-at-heading-p))
  16160. (goto-char pos)
  16161. (error "Boundary reached while executing %s" cmd))))
  16162. (defvar org-self-insert-command-undo-counter 0)
  16163. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16164. (defvar org-speed-command nil)
  16165. (defun org-speed-command-default-hook (keys)
  16166. "Hook for activating single-letter speed commands.
  16167. `org-speed-commands-default' specifies a minimal command set.
  16168. Use `org-speed-commands-user' for further customization."
  16169. (when (or (and (bolp) (looking-at org-outline-regexp))
  16170. (and (functionp org-use-speed-commands)
  16171. (funcall org-use-speed-commands)))
  16172. (cdr (assoc keys (append org-speed-commands-user
  16173. org-speed-commands-default)))))
  16174. (defun org-babel-speed-command-hook (keys)
  16175. "Hook for activating single-letter code block commands."
  16176. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16177. (cdr (assoc keys org-babel-key-bindings))))
  16178. (defcustom org-speed-command-hook
  16179. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16180. "Hook for activating speed commands at strategic locations.
  16181. Hook functions are called in sequence until a valid handler is
  16182. found.
  16183. Each hook takes a single argument, a user-pressed command key
  16184. which is also a `self-insert-command' from the global map.
  16185. Within the hook, examine the cursor position and the command key
  16186. and return nil or a valid handler as appropriate. Handler could
  16187. be one of an interactive command, a function, or a form.
  16188. Set `org-use-speed-commands' to non-nil value to enable this
  16189. hook. The default setting is `org-speed-command-default-hook'."
  16190. :group 'org-structure
  16191. :version "24.1"
  16192. :type 'hook)
  16193. (defun org-self-insert-command (N)
  16194. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16195. If the cursor is in a table looking at whitespace, the whitespace is
  16196. overwritten, and the table is not marked as requiring realignment."
  16197. (interactive "p")
  16198. (org-check-before-invisible-edit 'insert)
  16199. (cond
  16200. ((and org-use-speed-commands
  16201. (setq org-speed-command
  16202. (run-hook-with-args-until-success
  16203. 'org-speed-command-hook (this-command-keys))))
  16204. (cond
  16205. ((commandp org-speed-command)
  16206. (setq this-command org-speed-command)
  16207. (call-interactively org-speed-command))
  16208. ((functionp org-speed-command)
  16209. (funcall org-speed-command))
  16210. ((and org-speed-command (listp org-speed-command))
  16211. (eval org-speed-command))
  16212. (t (let (org-use-speed-commands)
  16213. (call-interactively 'org-self-insert-command)))))
  16214. ((and
  16215. (org-table-p)
  16216. (progn
  16217. ;; check if we blank the field, and if that triggers align
  16218. (and (featurep 'org-table) org-table-auto-blank-field
  16219. (member last-command
  16220. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16221. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16222. ;; got extra space, this field does not determine column width
  16223. (let (org-table-may-need-update) (org-table-blank-field))
  16224. ;; no extra space, this field may determine column width
  16225. (org-table-blank-field)))
  16226. t)
  16227. (eq N 1)
  16228. (looking-at "[^|\n]* |"))
  16229. (let (org-table-may-need-update)
  16230. (goto-char (1- (match-end 0)))
  16231. (delete-char -1)
  16232. (goto-char (match-beginning 0))
  16233. (self-insert-command N)))
  16234. (t
  16235. (setq org-table-may-need-update t)
  16236. (self-insert-command N)
  16237. (org-fix-tags-on-the-fly)
  16238. (if org-self-insert-cluster-for-undo
  16239. (if (not (eq last-command 'org-self-insert-command))
  16240. (setq org-self-insert-command-undo-counter 1)
  16241. (if (>= org-self-insert-command-undo-counter 20)
  16242. (setq org-self-insert-command-undo-counter 1)
  16243. (and (> org-self-insert-command-undo-counter 0)
  16244. buffer-undo-list (listp buffer-undo-list)
  16245. (not (cadr buffer-undo-list)) ; remove nil entry
  16246. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16247. (setq org-self-insert-command-undo-counter
  16248. (1+ org-self-insert-command-undo-counter))))))))
  16249. (defun org-check-before-invisible-edit (kind)
  16250. "Check is editing if kind KIND would be dangerous with invisible text around.
  16251. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16252. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16253. (if (and org-catch-invisible-edits
  16254. (or (not (boundp 'visible-mode)) (not visible-mode))
  16255. (or (get-char-property (point) 'invisible)
  16256. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16257. ;; OK, we need to take a closer look
  16258. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16259. (invisible-before-point (if (bobp) nil (get-char-property
  16260. (1- (point)) 'invisible)))
  16261. (border-and-ok-direction
  16262. (or
  16263. ;; Check if we are acting predictably before invisible text
  16264. (and invisible-at-point (not invisible-before-point)
  16265. (memq kind '(insert delete-backward)))
  16266. ;; Check if we are acting predictably after invisible text
  16267. ;; This works not well, and I have turned it off. It seems
  16268. ;; better to always show and stop after invisible text.
  16269. ;; (and (not invisible-at-point) invisible-before-point
  16270. ;; (memq kind '(insert delete)))
  16271. )))
  16272. (when (or (memq invisible-at-point '(outline org-hide-block))
  16273. (memq invisible-before-point '(outline org-hide-block)))
  16274. (if (eq org-catch-invisible-edits 'error)
  16275. (error "Editing in invisible areas is prohibited - make visible first"))
  16276. ;; Make the area visible
  16277. (save-excursion
  16278. (if invisible-before-point
  16279. (goto-char (previous-single-char-property-change
  16280. (point) 'invisible)))
  16281. (org-cycle))
  16282. (cond
  16283. ((eq org-catch-invisible-edits 'show)
  16284. ;; That's it, we do the edit after showing
  16285. (message
  16286. "Unfolding invisible region around point before editing")
  16287. (sit-for 1))
  16288. ((and (eq org-catch-invisible-edits 'smart)
  16289. border-and-ok-direction)
  16290. (message "Unfolding invisible region around point before editing"))
  16291. (t
  16292. ;; Don't do the edit, make the user repeat it in full visibility
  16293. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16294. (defun org-fix-tags-on-the-fly ()
  16295. (when (and (equal (char-after (point-at-bol)) ?*)
  16296. (org-at-heading-p))
  16297. (org-align-tags-here org-tags-column)))
  16298. (defun org-delete-backward-char (N)
  16299. "Like `delete-backward-char', insert whitespace at field end in tables.
  16300. When deleting backwards, in tables this function will insert whitespace in
  16301. front of the next \"|\" separator, to keep the table aligned. The table will
  16302. still be marked for re-alignment if the field did fill the entire column,
  16303. because, in this case the deletion might narrow the column."
  16304. (interactive "p")
  16305. (org-check-before-invisible-edit 'delete-backward)
  16306. (if (and (org-table-p)
  16307. (eq N 1)
  16308. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16309. (looking-at ".*?|"))
  16310. (let ((pos (point))
  16311. (noalign (looking-at "[^|\n\r]* |"))
  16312. (c org-table-may-need-update))
  16313. (backward-delete-char N)
  16314. (if (not overwrite-mode)
  16315. (progn
  16316. (skip-chars-forward "^|")
  16317. (insert " ")
  16318. (goto-char (1- pos))))
  16319. ;; noalign: if there were two spaces at the end, this field
  16320. ;; does not determine the width of the column.
  16321. (if noalign (setq org-table-may-need-update c)))
  16322. (backward-delete-char N)
  16323. (org-fix-tags-on-the-fly)))
  16324. (defun org-delete-char (N)
  16325. "Like `delete-char', but insert whitespace at field end in tables.
  16326. When deleting characters, in tables this function will insert whitespace in
  16327. front of the next \"|\" separator, to keep the table aligned. The table will
  16328. still be marked for re-alignment if the field did fill the entire column,
  16329. because, in this case the deletion might narrow the column."
  16330. (interactive "p")
  16331. (org-check-before-invisible-edit 'delete)
  16332. (if (and (org-table-p)
  16333. (not (bolp))
  16334. (not (= (char-after) ?|))
  16335. (eq N 1))
  16336. (if (looking-at ".*?|")
  16337. (let ((pos (point))
  16338. (noalign (looking-at "[^|\n\r]* |"))
  16339. (c org-table-may-need-update))
  16340. (replace-match (concat
  16341. (substring (match-string 0) 1 -1)
  16342. " |"))
  16343. (goto-char pos)
  16344. ;; noalign: if there were two spaces at the end, this field
  16345. ;; does not determine the width of the column.
  16346. (if noalign (setq org-table-may-need-update c)))
  16347. (delete-char N))
  16348. (delete-char N)
  16349. (org-fix-tags-on-the-fly)))
  16350. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16351. (put 'org-self-insert-command 'delete-selection t)
  16352. (put 'orgtbl-self-insert-command 'delete-selection t)
  16353. (put 'org-delete-char 'delete-selection 'supersede)
  16354. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16355. (put 'org-yank 'delete-selection 'yank)
  16356. ;; Make `flyspell-mode' delay after some commands
  16357. (put 'org-self-insert-command 'flyspell-delayed t)
  16358. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16359. (put 'org-delete-char 'flyspell-delayed t)
  16360. (put 'org-delete-backward-char 'flyspell-delayed t)
  16361. ;; Make pabbrev-mode expand after org-mode commands
  16362. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16363. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16364. ;; How to do this: Measure non-white length of current string
  16365. ;; If equal to column width, we should realign.
  16366. (defun org-remap (map &rest commands)
  16367. "In MAP, remap the functions given in COMMANDS.
  16368. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16369. (let (new old)
  16370. (while commands
  16371. (setq old (pop commands) new (pop commands))
  16372. (if (fboundp 'command-remapping)
  16373. (org-defkey map (vector 'remap old) new)
  16374. (substitute-key-definition old new map global-map)))))
  16375. (when (eq org-enable-table-editor 'optimized)
  16376. ;; If the user wants maximum table support, we need to hijack
  16377. ;; some standard editing functions
  16378. (org-remap org-mode-map
  16379. 'self-insert-command 'org-self-insert-command
  16380. 'delete-char 'org-delete-char
  16381. 'delete-backward-char 'org-delete-backward-char)
  16382. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16383. (defvar org-ctrl-c-ctrl-c-hook nil
  16384. "Hook for functions attaching themselves to `C-c C-c'.
  16385. This can be used to add additional functionality to the C-c C-c
  16386. key which executes context-dependent commands. This hook is run
  16387. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16388. run after the last test.
  16389. Each function will be called with no arguments. The function
  16390. must check if the context is appropriate for it to act. If yes,
  16391. it should do its thing and then return a non-nil value. If the
  16392. context is wrong, just do nothing and return nil.")
  16393. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16394. "Hook for functions attaching themselves to `C-c C-c'.
  16395. This can be used to add additional functionality to the C-c C-c
  16396. key which executes context-dependent commands. This hook is run
  16397. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16398. before the first test.
  16399. Each function will be called with no arguments. The function
  16400. must check if the context is appropriate for it to act. If yes,
  16401. it should do its thing and then return a non-nil value. If the
  16402. context is wrong, just do nothing and return nil.")
  16403. (defvar org-tab-first-hook nil
  16404. "Hook for functions to attach themselves to TAB.
  16405. See `org-ctrl-c-ctrl-c-hook' for more information.
  16406. This hook runs as the first action when TAB is pressed, even before
  16407. `org-cycle' messes around with the `outline-regexp' to cater for
  16408. inline tasks and plain list item folding.
  16409. If any function in this hook returns t, any other actions that
  16410. would have been caused by TAB (such as table field motion or visibility
  16411. cycling) will not occur.")
  16412. (defvar org-tab-after-check-for-table-hook nil
  16413. "Hook for functions to attach themselves to TAB.
  16414. See `org-ctrl-c-ctrl-c-hook' for more information.
  16415. This hook runs after it has been established that the cursor is not in a
  16416. table, but before checking if the cursor is in a headline or if global cycling
  16417. should be done.
  16418. If any function in this hook returns t, not other actions like visibility
  16419. cycling will be done.")
  16420. (defvar org-tab-after-check-for-cycling-hook nil
  16421. "Hook for functions to attach themselves to TAB.
  16422. See `org-ctrl-c-ctrl-c-hook' for more information.
  16423. This hook runs after it has been established that not table field motion and
  16424. not visibility should be done because of current context. This is probably
  16425. the place where a package like yasnippets can hook in.")
  16426. (defvar org-tab-before-tab-emulation-hook nil
  16427. "Hook for functions to attach themselves to TAB.
  16428. See `org-ctrl-c-ctrl-c-hook' for more information.
  16429. This hook runs after every other options for TAB have been exhausted, but
  16430. before indentation and \t insertion takes place.")
  16431. (defvar org-metaleft-hook nil
  16432. "Hook for functions attaching themselves to `M-left'.
  16433. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16434. (defvar org-metaright-hook nil
  16435. "Hook for functions attaching themselves to `M-right'.
  16436. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16437. (defvar org-metaup-hook nil
  16438. "Hook for functions attaching themselves to `M-up'.
  16439. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16440. (defvar org-metadown-hook nil
  16441. "Hook for functions attaching themselves to `M-down'.
  16442. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16443. (defvar org-shiftmetaleft-hook nil
  16444. "Hook for functions attaching themselves to `M-S-left'.
  16445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16446. (defvar org-shiftmetaright-hook nil
  16447. "Hook for functions attaching themselves to `M-S-right'.
  16448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16449. (defvar org-shiftmetaup-hook nil
  16450. "Hook for functions attaching themselves to `M-S-up'.
  16451. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16452. (defvar org-shiftmetadown-hook nil
  16453. "Hook for functions attaching themselves to `M-S-down'.
  16454. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16455. (defvar org-metareturn-hook nil
  16456. "Hook for functions attaching themselves to `M-RET'.
  16457. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16458. (defvar org-shiftup-hook nil
  16459. "Hook for functions attaching themselves to `S-up'.
  16460. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16461. (defvar org-shiftup-final-hook nil
  16462. "Hook for functions attaching themselves to `S-up'.
  16463. This one runs after all other options except shift-select have been excluded.
  16464. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16465. (defvar org-shiftdown-hook nil
  16466. "Hook for functions attaching themselves to `S-down'.
  16467. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16468. (defvar org-shiftdown-final-hook nil
  16469. "Hook for functions attaching themselves to `S-down'.
  16470. This one runs after all other options except shift-select have been excluded.
  16471. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16472. (defvar org-shiftleft-hook nil
  16473. "Hook for functions attaching themselves to `S-left'.
  16474. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16475. (defvar org-shiftleft-final-hook nil
  16476. "Hook for functions attaching themselves to `S-left'.
  16477. This one runs after all other options except shift-select have been excluded.
  16478. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16479. (defvar org-shiftright-hook nil
  16480. "Hook for functions attaching themselves to `S-right'.
  16481. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16482. (defvar org-shiftright-final-hook nil
  16483. "Hook for functions attaching themselves to `S-right'.
  16484. This one runs after all other options except shift-select have been excluded.
  16485. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16486. (defun org-modifier-cursor-error ()
  16487. "Throw an error, a modified cursor command was applied in wrong context."
  16488. (error "This command is active in special context like tables, headlines or items"))
  16489. (defun org-shiftselect-error ()
  16490. "Throw an error because Shift-Cursor command was applied in wrong context."
  16491. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16492. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16493. (error "This command works only in special context like headlines or timestamps")))
  16494. (defun org-call-for-shift-select (cmd)
  16495. (let ((this-command-keys-shift-translated t))
  16496. (call-interactively cmd)))
  16497. (defun org-shifttab (&optional arg)
  16498. "Global visibility cycling or move to previous table field.
  16499. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16500. on context.
  16501. See the individual commands for more information."
  16502. (interactive "P")
  16503. (cond
  16504. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16505. ((integerp arg)
  16506. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16507. (message "Content view to level: %d" arg)
  16508. (org-content (prefix-numeric-value arg2))
  16509. (setq org-cycle-global-status 'overview)))
  16510. (t (call-interactively 'org-global-cycle))))
  16511. (defun org-shiftmetaleft ()
  16512. "Promote subtree or delete table column.
  16513. Calls `org-promote-subtree', `org-outdent-item',
  16514. or `org-table-delete-column', depending on context.
  16515. See the individual commands for more information."
  16516. (interactive)
  16517. (cond
  16518. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16519. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16520. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16521. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16522. (t (org-modifier-cursor-error))))
  16523. (defun org-shiftmetaright ()
  16524. "Demote subtree or insert table column.
  16525. Calls `org-demote-subtree', `org-indent-item',
  16526. or `org-table-insert-column', depending on context.
  16527. See the individual commands for more information."
  16528. (interactive)
  16529. (cond
  16530. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16531. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16532. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16533. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16534. (t (org-modifier-cursor-error))))
  16535. (defun org-shiftmetaup (&optional arg)
  16536. "Move subtree up or kill table row.
  16537. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16538. `org-move-item-up' depending on context. See the individual commands
  16539. for more information."
  16540. (interactive "P")
  16541. (cond
  16542. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16543. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16544. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16545. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16546. (t (org-modifier-cursor-error))))
  16547. (defun org-shiftmetadown (&optional arg)
  16548. "Move subtree down or insert table row.
  16549. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16550. `org-move-item-down', depending on context. See the individual
  16551. commands for more information."
  16552. (interactive "P")
  16553. (cond
  16554. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16555. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16556. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16557. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16558. (t (org-modifier-cursor-error))))
  16559. (defsubst org-hidden-tree-error ()
  16560. (error
  16561. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16562. (defun org-metaleft (&optional arg)
  16563. "Promote heading or move table column to left.
  16564. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16565. With no specific context, calls the Emacs default `backward-word'.
  16566. See the individual commands for more information."
  16567. (interactive "P")
  16568. (cond
  16569. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16570. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16571. ((org-with-limited-levels
  16572. (or (org-at-heading-p)
  16573. (and (org-region-active-p)
  16574. (save-excursion
  16575. (goto-char (region-beginning))
  16576. (org-at-heading-p)))))
  16577. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16578. (call-interactively 'org-do-promote))
  16579. ;; At an inline task.
  16580. ((org-at-heading-p)
  16581. (call-interactively 'org-inlinetask-promote))
  16582. ((or (org-at-item-p)
  16583. (and (org-region-active-p)
  16584. (save-excursion
  16585. (goto-char (region-beginning))
  16586. (org-at-item-p))))
  16587. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16588. (call-interactively 'org-outdent-item))
  16589. (t (call-interactively 'backward-word))))
  16590. (defun org-metaright (&optional arg)
  16591. "Demote a subtree, a list item or move table column to right.
  16592. In front of a drawer or a block keyword, indent it correctly.
  16593. With no specific context, calls the Emacs default `forward-word'.
  16594. See the individual commands for more information."
  16595. (interactive "P")
  16596. (cond
  16597. ((run-hook-with-args-until-success 'org-metaright-hook))
  16598. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16599. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16600. ((org-at-block-p) (call-interactively 'org-indent-block))
  16601. ((org-with-limited-levels
  16602. (or (org-at-heading-p)
  16603. (and (org-region-active-p)
  16604. (save-excursion
  16605. (goto-char (region-beginning))
  16606. (org-at-heading-p)))))
  16607. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16608. (call-interactively 'org-do-demote))
  16609. ;; At an inline task.
  16610. ((org-at-heading-p)
  16611. (call-interactively 'org-inlinetask-demote))
  16612. ((or (org-at-item-p)
  16613. (and (org-region-active-p)
  16614. (save-excursion
  16615. (goto-char (region-beginning))
  16616. (org-at-item-p))))
  16617. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16618. (call-interactively 'org-indent-item))
  16619. (t (call-interactively 'forward-word))))
  16620. (defun org-check-for-hidden (what)
  16621. "Check if there are hidden headlines/items in the current visual line.
  16622. WHAT can be either `headlines' or `items'. If the current line is
  16623. an outline or item heading and it has a folded subtree below it,
  16624. this function returns t, nil otherwise."
  16625. (let ((re (cond
  16626. ((eq what 'headlines) org-outline-regexp-bol)
  16627. ((eq what 'items) (org-item-beginning-re))
  16628. (t (error "This should not happen"))))
  16629. beg end)
  16630. (save-excursion
  16631. (catch 'exit
  16632. (unless (org-region-active-p)
  16633. (setq beg (point-at-bol))
  16634. (beginning-of-line 2)
  16635. (while (and (not (eobp)) ;; this is like `next-line'
  16636. (get-char-property (1- (point)) 'invisible))
  16637. (beginning-of-line 2))
  16638. (setq end (point))
  16639. (goto-char beg)
  16640. (goto-char (point-at-eol))
  16641. (setq end (max end (point)))
  16642. (while (re-search-forward re end t)
  16643. (if (get-char-property (match-beginning 0) 'invisible)
  16644. (throw 'exit t))))
  16645. nil))))
  16646. (defun org-metaup (&optional arg)
  16647. "Move subtree up or move table row up.
  16648. Calls `org-move-subtree-up' or `org-table-move-row' or
  16649. `org-move-item-up', depending on context. See the individual commands
  16650. for more information."
  16651. (interactive "P")
  16652. (cond
  16653. ((run-hook-with-args-until-success 'org-metaup-hook))
  16654. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16655. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16656. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16657. (t (transpose-lines 1) (beginning-of-line -1))))
  16658. (defun org-metadown (&optional arg)
  16659. "Move subtree down or move table row down.
  16660. Calls `org-move-subtree-down' or `org-table-move-row' or
  16661. `org-move-item-down', depending on context. See the individual
  16662. commands for more information."
  16663. (interactive "P")
  16664. (cond
  16665. ((run-hook-with-args-until-success 'org-metadown-hook))
  16666. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16667. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16668. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16669. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16670. (defun org-shiftup (&optional arg)
  16671. "Increase item in timestamp or increase priority of current headline.
  16672. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16673. depending on context. See the individual commands for more information."
  16674. (interactive "P")
  16675. (cond
  16676. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16677. ((and org-support-shift-select (org-region-active-p))
  16678. (org-call-for-shift-select 'previous-line))
  16679. ((org-at-timestamp-p t)
  16680. (call-interactively (if org-edit-timestamp-down-means-later
  16681. 'org-timestamp-down 'org-timestamp-up)))
  16682. ((and (not (eq org-support-shift-select 'always))
  16683. org-enable-priority-commands
  16684. (org-at-heading-p))
  16685. (call-interactively 'org-priority-up))
  16686. ((and (not org-support-shift-select) (org-at-item-p))
  16687. (call-interactively 'org-previous-item))
  16688. ((org-clocktable-try-shift 'up arg))
  16689. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16690. (org-support-shift-select
  16691. (org-call-for-shift-select 'previous-line))
  16692. (t (org-shiftselect-error))))
  16693. (defun org-shiftdown (&optional arg)
  16694. "Decrease item in timestamp or decrease priority of current headline.
  16695. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16696. depending on context. See the individual commands for more information."
  16697. (interactive "P")
  16698. (cond
  16699. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16700. ((and org-support-shift-select (org-region-active-p))
  16701. (org-call-for-shift-select 'next-line))
  16702. ((org-at-timestamp-p t)
  16703. (call-interactively (if org-edit-timestamp-down-means-later
  16704. 'org-timestamp-up 'org-timestamp-down)))
  16705. ((and (not (eq org-support-shift-select 'always))
  16706. org-enable-priority-commands
  16707. (org-at-heading-p))
  16708. (call-interactively 'org-priority-down))
  16709. ((and (not org-support-shift-select) (org-at-item-p))
  16710. (call-interactively 'org-next-item))
  16711. ((org-clocktable-try-shift 'down arg))
  16712. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16713. (org-support-shift-select
  16714. (org-call-for-shift-select 'next-line))
  16715. (t (org-shiftselect-error))))
  16716. (defun org-shiftright (&optional arg)
  16717. "Cycle the thing at point or in the current line, depending on context.
  16718. Depending on context, this does one of the following:
  16719. - switch a timestamp at point one day into the future
  16720. - on a headline, switch to the next TODO keyword.
  16721. - on an item, switch entire list to the next bullet type
  16722. - on a property line, switch to the next allowed value
  16723. - on a clocktable definition line, move time block into the future"
  16724. (interactive "P")
  16725. (cond
  16726. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16727. ((and org-support-shift-select (org-region-active-p))
  16728. (org-call-for-shift-select 'forward-char))
  16729. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16730. ((and (not (eq org-support-shift-select 'always))
  16731. (org-at-heading-p))
  16732. (let ((org-inhibit-logging
  16733. (not org-treat-S-cursor-todo-selection-as-state-change))
  16734. (org-inhibit-blocking
  16735. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16736. (org-call-with-arg 'org-todo 'right)))
  16737. ((or (and org-support-shift-select
  16738. (not (eq org-support-shift-select 'always))
  16739. (org-at-item-bullet-p))
  16740. (and (not org-support-shift-select) (org-at-item-p)))
  16741. (org-call-with-arg 'org-cycle-list-bullet nil))
  16742. ((and (not (eq org-support-shift-select 'always))
  16743. (org-at-property-p))
  16744. (call-interactively 'org-property-next-allowed-value))
  16745. ((org-clocktable-try-shift 'right arg))
  16746. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16747. (org-support-shift-select
  16748. (org-call-for-shift-select 'forward-char))
  16749. (t (org-shiftselect-error))))
  16750. (defun org-shiftleft (&optional arg)
  16751. "Cycle the thing at point or in the current line, depending on context.
  16752. Depending on context, this does one of the following:
  16753. - switch a timestamp at point one day into the past
  16754. - on a headline, switch to the previous TODO keyword.
  16755. - on an item, switch entire list to the previous bullet type
  16756. - on a property line, switch to the previous allowed value
  16757. - on a clocktable definition line, move time block into the past"
  16758. (interactive "P")
  16759. (cond
  16760. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16761. ((and org-support-shift-select (org-region-active-p))
  16762. (org-call-for-shift-select 'backward-char))
  16763. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16764. ((and (not (eq org-support-shift-select 'always))
  16765. (org-at-heading-p))
  16766. (let ((org-inhibit-logging
  16767. (not org-treat-S-cursor-todo-selection-as-state-change))
  16768. (org-inhibit-blocking
  16769. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16770. (org-call-with-arg 'org-todo 'left)))
  16771. ((or (and org-support-shift-select
  16772. (not (eq org-support-shift-select 'always))
  16773. (org-at-item-bullet-p))
  16774. (and (not org-support-shift-select) (org-at-item-p)))
  16775. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16776. ((and (not (eq org-support-shift-select 'always))
  16777. (org-at-property-p))
  16778. (call-interactively 'org-property-previous-allowed-value))
  16779. ((org-clocktable-try-shift 'left arg))
  16780. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16781. (org-support-shift-select
  16782. (org-call-for-shift-select 'backward-char))
  16783. (t (org-shiftselect-error))))
  16784. (defun org-shiftcontrolright ()
  16785. "Switch to next TODO set."
  16786. (interactive)
  16787. (cond
  16788. ((and org-support-shift-select (org-region-active-p))
  16789. (org-call-for-shift-select 'forward-word))
  16790. ((and (not (eq org-support-shift-select 'always))
  16791. (org-at-heading-p))
  16792. (org-call-with-arg 'org-todo 'nextset))
  16793. (org-support-shift-select
  16794. (org-call-for-shift-select 'forward-word))
  16795. (t (org-shiftselect-error))))
  16796. (defun org-shiftcontrolleft ()
  16797. "Switch to previous TODO set."
  16798. (interactive)
  16799. (cond
  16800. ((and org-support-shift-select (org-region-active-p))
  16801. (org-call-for-shift-select 'backward-word))
  16802. ((and (not (eq org-support-shift-select 'always))
  16803. (org-at-heading-p))
  16804. (org-call-with-arg 'org-todo 'previousset))
  16805. (org-support-shift-select
  16806. (org-call-for-shift-select 'backward-word))
  16807. (t (org-shiftselect-error))))
  16808. (defun org-shiftcontrolup ()
  16809. "Change timestamps synchronously up in CLOCK log lines."
  16810. (interactive)
  16811. (cond ((and (not org-support-shift-select)
  16812. (org-at-clock-log-p)
  16813. (org-at-timestamp-p t))
  16814. (org-clock-timestamps-up))
  16815. (t (org-shiftselect-error))))
  16816. (defun org-shiftcontroldown ()
  16817. "Change timestamps synchronously down in CLOCK log lines."
  16818. (interactive)
  16819. (cond ((and (not org-support-shift-select)
  16820. (org-at-clock-log-p)
  16821. (org-at-timestamp-p t))
  16822. (org-clock-timestamps-down))
  16823. (t (org-shiftselect-error))))
  16824. (defun org-ctrl-c-ret ()
  16825. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16826. (interactive)
  16827. (cond
  16828. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16829. (t (call-interactively 'org-insert-heading))))
  16830. (defun org-find-visible ()
  16831. (let ((s (point)))
  16832. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16833. (get-char-property s 'invisible)))
  16834. s))
  16835. (defun org-find-invisible ()
  16836. (let ((s (point)))
  16837. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16838. (not (get-char-property s 'invisible))))
  16839. s))
  16840. (defun org-copy-visible (beg end)
  16841. "Copy the visible parts of the region."
  16842. (interactive "r")
  16843. (let (snippets s)
  16844. (save-excursion
  16845. (save-restriction
  16846. (narrow-to-region beg end)
  16847. (setq s (goto-char (point-min)))
  16848. (while (not (= (point) (point-max)))
  16849. (goto-char (org-find-invisible))
  16850. (push (buffer-substring s (point)) snippets)
  16851. (setq s (goto-char (org-find-visible))))))
  16852. (kill-new (apply 'concat (nreverse snippets)))))
  16853. (defun org-copy-special ()
  16854. "Copy region in table or copy current subtree.
  16855. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16856. See the individual commands for more information."
  16857. (interactive)
  16858. (call-interactively
  16859. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16860. (defun org-cut-special ()
  16861. "Cut region in table or cut current subtree.
  16862. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16863. See the individual commands for more information."
  16864. (interactive)
  16865. (call-interactively
  16866. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16867. (defun org-paste-special (arg)
  16868. "Paste rectangular region into table, or past subtree relative to level.
  16869. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16870. See the individual commands for more information."
  16871. (interactive "P")
  16872. (if (org-at-table-p)
  16873. (org-table-paste-rectangle)
  16874. (org-paste-subtree arg)))
  16875. (defun org-edit-special (&optional arg)
  16876. "Call a special editor for the stuff at point.
  16877. When at a table, call the formula editor with `org-table-edit-formulas'.
  16878. When at the first line of an src example, call `org-edit-src-code'.
  16879. When in an #+include line, visit the include file. Otherwise call
  16880. `ffap' to visit the file at point."
  16881. (interactive)
  16882. ;; possibly prep session before editing source
  16883. (when arg
  16884. (let* ((info (org-babel-get-src-block-info))
  16885. (lang (nth 0 info))
  16886. (params (nth 2 info))
  16887. (session (cdr (assoc :session params))))
  16888. (when (and info session) ;; we are in a source-code block with a session
  16889. (funcall
  16890. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16891. (cond ;; proceed with `org-edit-special'
  16892. ((save-excursion
  16893. (beginning-of-line 1)
  16894. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16895. (find-file (org-trim (match-string 1))))
  16896. ((org-edit-src-code))
  16897. ((org-edit-fixed-width-region))
  16898. ((org-at-table.el-p)
  16899. (org-edit-src-code))
  16900. ((or (org-at-table-p)
  16901. (save-excursion
  16902. (beginning-of-line 1)
  16903. (looking-at "[ \t]*#\\+TBLFM:")))
  16904. (call-interactively 'org-table-edit-formulas))
  16905. (t (call-interactively 'ffap))))
  16906. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16907. (defun org-ctrl-c-ctrl-c (&optional arg)
  16908. "Set tags in headline, or update according to changed information at point.
  16909. This command does many different things, depending on context:
  16910. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16911. this is what we do.
  16912. - If the cursor is on a statistics cookie, update it.
  16913. - If the cursor is in a headline, prompt for tags and insert them
  16914. into the current line, aligned to `org-tags-column'. When called
  16915. with prefix arg, realign all tags in the current buffer.
  16916. - If the cursor is in one of the special #+KEYWORD lines, this
  16917. triggers scanning the buffer for these lines and updating the
  16918. information.
  16919. - If the cursor is inside a table, realign the table. This command
  16920. works even if the automatic table editor has been turned off.
  16921. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16922. the entire table.
  16923. - If the cursor is at a footnote reference or definition, jump to
  16924. the corresponding definition or references, respectively.
  16925. - If the cursor is a the beginning of a dynamic block, update it.
  16926. - If the current buffer is a capture buffer, close note and file it.
  16927. - If the cursor is on a <<<target>>>, update radio targets and
  16928. corresponding links in this buffer.
  16929. - If the cursor is on a numbered item in a plain list, renumber the
  16930. ordered list.
  16931. - If the cursor is on a checkbox, toggle it.
  16932. - If the cursor is on a code block, evaluate it. The variable
  16933. `org-confirm-babel-evaluate' can be used to control prompting
  16934. before code block evaluation, by default every code block
  16935. evaluation requires confirmation. Code block evaluation can be
  16936. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16937. (interactive "P")
  16938. (let ((org-enable-table-editor t))
  16939. (cond
  16940. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16941. org-occur-highlights
  16942. org-latex-fragment-image-overlays)
  16943. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16944. (org-remove-occur-highlights)
  16945. (org-remove-latex-fragment-image-overlays)
  16946. (message "Temporary highlights/overlays removed from current buffer"))
  16947. ((and (local-variable-p 'org-finish-function (current-buffer))
  16948. (fboundp org-finish-function))
  16949. (funcall org-finish-function))
  16950. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16951. ((org-in-regexp org-ts-regexp-both)
  16952. (org-timestamp-change 0 'day))
  16953. ((or (looking-at org-property-start-re)
  16954. (org-at-property-p))
  16955. (call-interactively 'org-property-action))
  16956. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16957. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16958. (or (org-at-heading-p) (org-at-item-p)))
  16959. (call-interactively 'org-update-statistics-cookies))
  16960. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16961. ((org-at-table.el-p)
  16962. (message "Use C-c ' to edit table.el tables"))
  16963. ((org-at-table-p)
  16964. (org-table-maybe-eval-formula)
  16965. (if arg
  16966. (call-interactively 'org-table-recalculate)
  16967. (org-table-maybe-recalculate-line))
  16968. (call-interactively 'org-table-align)
  16969. (orgtbl-send-table 'maybe))
  16970. ((or (org-footnote-at-reference-p)
  16971. (org-footnote-at-definition-p))
  16972. (call-interactively 'org-footnote-action))
  16973. ((org-at-item-checkbox-p)
  16974. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16975. ;; list only if at top item.
  16976. (let* ((cbox (match-string 1))
  16977. (struct (org-list-struct))
  16978. (old-struct (copy-tree struct))
  16979. (parents (org-list-parents-alist struct))
  16980. (orderedp (org-entry-get nil "ORDERED"))
  16981. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16982. block-item)
  16983. ;; Use a light version of `org-toggle-checkbox' to avoid
  16984. ;; computing list structure twice.
  16985. (let ((new-box (cond
  16986. ((equal arg '(16)) "[-]")
  16987. ((equal arg '(4)) nil)
  16988. ((equal "[X]" cbox) "[ ]")
  16989. (t "[X]"))))
  16990. (if (and firstp arg)
  16991. ;; If at first item of sub-list, remove check-box from
  16992. ;; every item at the same level.
  16993. (mapc
  16994. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16995. (org-list-get-all-items
  16996. (point-at-bol) struct (org-list-prevs-alist struct)))
  16997. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16998. ;; Replicate `org-list-write-struct', while grabbing a return
  16999. ;; value from `org-list-struct-fix-box'.
  17000. (org-list-struct-fix-ind struct parents 2)
  17001. (org-list-struct-fix-item-end struct)
  17002. (let ((prevs (org-list-prevs-alist struct)))
  17003. (org-list-struct-fix-bul struct prevs)
  17004. (org-list-struct-fix-ind struct parents)
  17005. (setq block-item
  17006. (org-list-struct-fix-box struct parents prevs orderedp)))
  17007. (org-list-struct-apply-struct struct old-struct)
  17008. (org-update-checkbox-count-maybe)
  17009. (when block-item
  17010. (message
  17011. "Checkboxes were removed due to unchecked box at line %d"
  17012. (org-current-line block-item)))
  17013. (when firstp (org-list-send-list 'maybe))))
  17014. ((org-at-item-p)
  17015. ;; Cursor at an item: repair list. Do checkbox related actions
  17016. ;; only if function was called with an argument. Send list only
  17017. ;; if at top item.
  17018. (let* ((struct (org-list-struct))
  17019. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17020. old-struct)
  17021. (when arg
  17022. (setq old-struct (copy-tree struct))
  17023. (if firstp
  17024. ;; If at first item of sub-list, add check-box to every
  17025. ;; item at the same level.
  17026. (mapc
  17027. (lambda (pos)
  17028. (unless (org-list-get-checkbox pos struct)
  17029. (org-list-set-checkbox pos struct "[ ]")))
  17030. (org-list-get-all-items
  17031. (point-at-bol) struct (org-list-prevs-alist struct)))
  17032. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17033. (org-list-write-struct
  17034. struct (org-list-parents-alist struct) old-struct)
  17035. (when arg (org-update-checkbox-count-maybe))
  17036. (when firstp (org-list-send-list 'maybe))))
  17037. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17038. ;; Dynamic block
  17039. (beginning-of-line 1)
  17040. (save-excursion (org-update-dblock)))
  17041. ((save-excursion
  17042. (beginning-of-line 1)
  17043. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17044. (cond
  17045. ((equal (match-string 1) "TBLFM")
  17046. ;; Recalculate the table before this line
  17047. (save-excursion
  17048. (beginning-of-line 1)
  17049. (skip-chars-backward " \r\n\t")
  17050. (if (org-at-table-p)
  17051. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17052. (t
  17053. (let ((org-inhibit-startup-visibility-stuff t)
  17054. (org-startup-align-all-tables nil))
  17055. (when (boundp 'org-table-coordinate-overlays)
  17056. (mapc 'delete-overlay org-table-coordinate-overlays)
  17057. (setq org-table-coordinate-overlays nil))
  17058. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17059. (message "Local setup has been refreshed"))))
  17060. ((org-clock-update-time-maybe))
  17061. (t
  17062. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17063. (error "C-c C-c can do nothing useful at this location"))))))
  17064. (defun org-mode-restart ()
  17065. "Restart Org-mode, to scan again for special lines.
  17066. Also updates the keyword regular expressions."
  17067. (interactive)
  17068. (org-mode)
  17069. (message "Org-mode restarted"))
  17070. (defun org-kill-note-or-show-branches ()
  17071. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17072. (interactive)
  17073. (if (not org-finish-function)
  17074. (progn
  17075. (hide-subtree)
  17076. (call-interactively 'show-branches))
  17077. (let ((org-note-abort t))
  17078. (funcall org-finish-function))))
  17079. (defun org-return (&optional indent)
  17080. "Goto next table row or insert a newline.
  17081. Calls `org-table-next-row' or `newline', depending on context.
  17082. See the individual commands for more information."
  17083. (interactive)
  17084. (cond
  17085. ((or (bobp) (org-in-src-block-p))
  17086. (if indent (newline-and-indent) (newline)))
  17087. ((org-at-table-p)
  17088. (org-table-justify-field-maybe)
  17089. (call-interactively 'org-table-next-row))
  17090. ;; when `newline-and-indent' is called within a list, make sure
  17091. ;; text moved stays inside the item.
  17092. ((and (org-in-item-p) indent)
  17093. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17094. (progn
  17095. (save-match-data (newline))
  17096. (org-indent-line-to (length (match-string 0))))
  17097. (let ((ind (org-get-indentation)))
  17098. (newline)
  17099. (if (org-looking-back org-list-end-re)
  17100. (org-indent-line-function)
  17101. (org-indent-line-to ind)))))
  17102. ((and org-return-follows-link
  17103. (let ((tprop (get-text-property (point) 'face)))
  17104. (or (eq tprop 'org-link)
  17105. (and (listp tprop) (memq 'org-link tprop)))))
  17106. (call-interactively 'org-open-at-point))
  17107. ((and (org-at-heading-p)
  17108. (looking-at
  17109. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17110. (org-show-entry)
  17111. (end-of-line 1)
  17112. (newline))
  17113. (t (if indent (newline-and-indent) (newline)))))
  17114. (defun org-return-indent ()
  17115. "Goto next table row or insert a newline and indent.
  17116. Calls `org-table-next-row' or `newline-and-indent', depending on
  17117. context. See the individual commands for more information."
  17118. (interactive)
  17119. (org-return t))
  17120. (defun org-ctrl-c-star ()
  17121. "Compute table, or change heading status of lines.
  17122. Calls `org-table-recalculate' or `org-toggle-heading',
  17123. depending on context."
  17124. (interactive)
  17125. (cond
  17126. ((org-at-table-p)
  17127. (call-interactively 'org-table-recalculate))
  17128. (t
  17129. ;; Convert all lines in region to list items
  17130. (call-interactively 'org-toggle-heading))))
  17131. (defun org-ctrl-c-minus ()
  17132. "Insert separator line in table or modify bullet status of line.
  17133. Also turns a plain line or a region of lines into list items.
  17134. Calls `org-table-insert-hline', `org-toggle-item', or
  17135. `org-cycle-list-bullet', depending on context."
  17136. (interactive)
  17137. (cond
  17138. ((org-at-table-p)
  17139. (call-interactively 'org-table-insert-hline))
  17140. ((org-region-active-p)
  17141. (call-interactively 'org-toggle-item))
  17142. ((org-in-item-p)
  17143. (call-interactively 'org-cycle-list-bullet))
  17144. (t
  17145. (call-interactively 'org-toggle-item))))
  17146. (defun org-toggle-item (arg)
  17147. "Convert headings or normal lines to items, items to normal lines.
  17148. If there is no active region, only the current line is considered.
  17149. If the first non blank line in the region is an headline, convert
  17150. all headlines to items, shifting text accordingly.
  17151. If it is an item, convert all items to normal lines.
  17152. If it is normal text, change region into an item. With a prefix
  17153. argument ARG, change each line in region into an item."
  17154. (interactive "P")
  17155. (let ((shift-text
  17156. (function
  17157. ;; Shift text in current section to IND, from point to END.
  17158. ;; The function leaves point to END line.
  17159. (lambda (ind end)
  17160. (let ((min-i 1000) (end (copy-marker end)))
  17161. ;; First determine the minimum indentation (MIN-I) of
  17162. ;; the text.
  17163. (save-excursion
  17164. (catch 'exit
  17165. (while (< (point) end)
  17166. (let ((i (org-get-indentation)))
  17167. (cond
  17168. ;; Skip blank lines and inline tasks.
  17169. ((looking-at "^[ \t]*$"))
  17170. ((looking-at org-outline-regexp-bol))
  17171. ;; We can't find less than 0 indentation.
  17172. ((zerop i) (throw 'exit (setq min-i 0)))
  17173. ((< i min-i) (setq min-i i))))
  17174. (forward-line))))
  17175. ;; Then indent each line so that a line indented to
  17176. ;; MIN-I becomes indented to IND. Ignore blank lines
  17177. ;; and inline tasks in the process.
  17178. (let ((delta (- ind min-i)))
  17179. (while (< (point) end)
  17180. (unless (or (looking-at "^[ \t]*$")
  17181. (looking-at org-outline-regexp-bol))
  17182. (org-indent-line-to (+ (org-get-indentation) delta)))
  17183. (forward-line)))))))
  17184. (skip-blanks
  17185. (function
  17186. ;; Return beginning of first non-blank line, starting from
  17187. ;; line at POS.
  17188. (lambda (pos)
  17189. (save-excursion
  17190. (goto-char pos)
  17191. (skip-chars-forward " \r\t\n")
  17192. (point-at-bol)))))
  17193. beg end)
  17194. ;; Determine boundaries of changes.
  17195. (if (org-region-active-p)
  17196. (setq beg (funcall skip-blanks (region-beginning))
  17197. end (copy-marker (region-end)))
  17198. (setq beg (funcall skip-blanks (point-at-bol))
  17199. end (copy-marker (point-at-eol))))
  17200. ;; Depending on the starting line, choose an action on the text
  17201. ;; between BEG and END.
  17202. (org-with-limited-levels
  17203. (save-excursion
  17204. (goto-char beg)
  17205. (cond
  17206. ;; Case 1. Start at an item: de-itemize. Note that it only
  17207. ;; happens when a region is active: `org-ctrl-c-minus'
  17208. ;; would call `org-cycle-list-bullet' otherwise.
  17209. ((org-at-item-p)
  17210. (while (< (point) end)
  17211. (when (org-at-item-p)
  17212. (skip-chars-forward " \t")
  17213. (delete-region (point) (match-end 0)))
  17214. (forward-line)))
  17215. ;; Case 2. Start at an heading: convert to items.
  17216. ((org-at-heading-p)
  17217. (let* ((bul (org-list-bullet-string "-"))
  17218. (bul-len (length bul))
  17219. ;; Indentation of the first heading. It should be
  17220. ;; relative to the indentation of its parent, if any.
  17221. (start-ind (save-excursion
  17222. (cond
  17223. ((not org-adapt-indentation) 0)
  17224. ((not (outline-previous-heading)) 0)
  17225. (t (length (match-string 0))))))
  17226. ;; Level of first heading. Further headings will be
  17227. ;; compared to it to determine hierarchy in the list.
  17228. (ref-level (org-reduced-level (org-outline-level))))
  17229. (while (< (point) end)
  17230. (let* ((level (org-reduced-level (org-outline-level)))
  17231. (delta (max 0 (- level ref-level))))
  17232. ;; If current headline is less indented than the first
  17233. ;; one, set it as reference, in order to preserve
  17234. ;; subtrees.
  17235. (when (< level ref-level) (setq ref-level level))
  17236. (replace-match bul t t)
  17237. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17238. ;; Ensure all text down to END (or SECTION-END) belongs
  17239. ;; to the newly created item.
  17240. (let ((section-end (save-excursion
  17241. (or (outline-next-heading) (point)))))
  17242. (forward-line)
  17243. (funcall shift-text
  17244. (+ start-ind (* (1+ delta) bul-len))
  17245. (min end section-end)))))))
  17246. ;; Case 3. Normal line with ARG: turn each non-item line into
  17247. ;; an item.
  17248. (arg
  17249. (while (< (point) end)
  17250. (unless (or (org-at-heading-p) (org-at-item-p))
  17251. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17252. (replace-match
  17253. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17254. (forward-line)))
  17255. ;; Case 4. Normal line without ARG: make the first line of
  17256. ;; region an item, and shift indentation of others
  17257. ;; lines to set them as item's body.
  17258. (t (let* ((bul (org-list-bullet-string "-"))
  17259. (bul-len (length bul))
  17260. (ref-ind (org-get-indentation)))
  17261. (skip-chars-forward " \t")
  17262. (insert bul)
  17263. (forward-line)
  17264. (while (< (point) end)
  17265. ;; Ensure that lines less indented than first one
  17266. ;; still get included in item body.
  17267. (funcall shift-text
  17268. (+ ref-ind bul-len)
  17269. (min end (save-excursion (or (outline-next-heading)
  17270. (point)))))
  17271. (forward-line)))))))))
  17272. (defun org-toggle-heading (&optional nstars)
  17273. "Convert headings to normal text, or items or text to headings.
  17274. If there is no active region, only the current line is considered.
  17275. If the first non blank line is an headline, remove the stars from
  17276. all headlines in the region.
  17277. If it is a plain list item, turn all plain list items into headings.
  17278. If it is a normal line, turn each and every normal line (i.e. not
  17279. an heading or an item) in the region into a heading.
  17280. When converting a line into a heading, the number of stars is chosen
  17281. such that the lines become children of the current entry. However,
  17282. when a prefix argument is given, its value determines the number of
  17283. stars to add."
  17284. (interactive "P")
  17285. (let ((skip-blanks
  17286. (function
  17287. ;; Return beginning of first non-blank line, starting from
  17288. ;; line at POS.
  17289. (lambda (pos)
  17290. (save-excursion
  17291. (goto-char pos)
  17292. (skip-chars-forward " \r\t\n")
  17293. (point-at-bol)))))
  17294. beg end)
  17295. ;; Determine boundaries of changes. If region ends at a bol, do
  17296. ;; not consider the last line to be in the region.
  17297. (if (org-region-active-p)
  17298. (setq beg (funcall skip-blanks (region-beginning))
  17299. end (copy-marker (save-excursion
  17300. (goto-char (region-end))
  17301. (if (bolp) (point) (point-at-eol)))))
  17302. (setq beg (funcall skip-blanks (point-at-bol))
  17303. end (copy-marker (point-at-eol))))
  17304. ;; Ensure inline tasks don't count as headings.
  17305. (org-with-limited-levels
  17306. (save-excursion
  17307. (goto-char beg)
  17308. (cond
  17309. ;; Case 1. Started at an heading: de-star headings.
  17310. ((org-at-heading-p)
  17311. (while (< (point) end)
  17312. (when (org-at-heading-p t)
  17313. (looking-at org-outline-regexp) (replace-match ""))
  17314. (forward-line)))
  17315. ;; Case 2. Started at an item: change items into headlines.
  17316. ;; One star will be added by `org-list-to-subtree'.
  17317. ((org-at-item-p)
  17318. (let* ((stars (make-string
  17319. (if nstars
  17320. ;; subtract the star that will be added again by
  17321. ;; `org-list-to-subtree'
  17322. (1- (prefix-numeric-value current-prefix-arg))
  17323. (or (org-current-level) 0))
  17324. ?*))
  17325. (add-stars
  17326. (cond (nstars "") ; stars from prefix only
  17327. ((equal stars "") "") ; before first heading
  17328. (org-odd-levels-only "*") ; inside heading, odd
  17329. (t "")))) ; inside heading, oddeven
  17330. (while (< (point) end)
  17331. (when (org-at-item-p)
  17332. ;; Pay attention to cases when region ends before list.
  17333. (let* ((struct (org-list-struct))
  17334. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17335. (save-restriction
  17336. (narrow-to-region (point) list-end)
  17337. (insert
  17338. (org-list-to-subtree
  17339. (org-list-parse-list t)
  17340. '(:istart (concat stars add-stars (funcall get-stars depth))
  17341. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17342. (forward-line))))
  17343. ;; Case 3. Started at normal text: make every line an heading,
  17344. ;; skipping headlines and items.
  17345. (t (let* ((stars (make-string
  17346. (if nstars
  17347. (prefix-numeric-value current-prefix-arg)
  17348. (or (org-current-level) 0))
  17349. ?*))
  17350. (add-stars
  17351. (cond (nstars "") ; stars from prefix only
  17352. ((equal stars "") "*") ; before first heading
  17353. (org-odd-levels-only "**") ; inside heading, odd
  17354. (t "*"))) ; inside heading, oddeven
  17355. (rpl (concat stars add-stars " ")))
  17356. (while (< (point) end)
  17357. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17358. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17359. (replace-match (concat rpl (match-string 2))))
  17360. (forward-line)))))))))
  17361. (defun org-meta-return (&optional arg)
  17362. "Insert a new heading or wrap a region in a table.
  17363. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17364. See the individual commands for more information."
  17365. (interactive "P")
  17366. (cond
  17367. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17368. ((or (org-at-drawer-p) (org-at-property-p))
  17369. (newline-and-indent))
  17370. ((org-at-table-p)
  17371. (call-interactively 'org-table-wrap-region))
  17372. (t (call-interactively 'org-insert-heading))))
  17373. ;;; Menu entries
  17374. ;; Define the Org-mode menus
  17375. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17376. '("Tbl"
  17377. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17378. ["Next Field" org-cycle (org-at-table-p)]
  17379. ["Previous Field" org-shifttab (org-at-table-p)]
  17380. ["Next Row" org-return (org-at-table-p)]
  17381. "--"
  17382. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17383. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17384. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17385. "--"
  17386. ("Column"
  17387. ["Move Column Left" org-metaleft (org-at-table-p)]
  17388. ["Move Column Right" org-metaright (org-at-table-p)]
  17389. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17390. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17391. ("Row"
  17392. ["Move Row Up" org-metaup (org-at-table-p)]
  17393. ["Move Row Down" org-metadown (org-at-table-p)]
  17394. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17395. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17396. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17397. "--"
  17398. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17399. ("Rectangle"
  17400. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17401. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17402. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17403. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17404. "--"
  17405. ("Calculate"
  17406. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17407. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17408. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17409. "--"
  17410. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17411. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17412. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17413. "--"
  17414. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17415. "--"
  17416. ["Sum Column/Rectangle" org-table-sum
  17417. (or (org-at-table-p) (org-region-active-p))]
  17418. ["Which Column?" org-table-current-column (org-at-table-p)])
  17419. ["Debug Formulas"
  17420. org-table-toggle-formula-debugger
  17421. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17422. ["Show Col/Row Numbers"
  17423. org-table-toggle-coordinate-overlays
  17424. :style toggle
  17425. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17426. "--"
  17427. ["Create" org-table-create (and (not (org-at-table-p))
  17428. org-enable-table-editor)]
  17429. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17430. ["Import from File" org-table-import (not (org-at-table-p))]
  17431. ["Export to File" org-table-export (org-at-table-p)]
  17432. "--"
  17433. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17434. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17435. '("Org"
  17436. ("Show/Hide"
  17437. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17438. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17439. ["Sparse Tree..." org-sparse-tree t]
  17440. ["Reveal Context" org-reveal t]
  17441. ["Show All" show-all t]
  17442. "--"
  17443. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17444. "--"
  17445. ["New Heading" org-insert-heading t]
  17446. ("Navigate Headings"
  17447. ["Up" outline-up-heading t]
  17448. ["Next" outline-next-visible-heading t]
  17449. ["Previous" outline-previous-visible-heading t]
  17450. ["Next Same Level" outline-forward-same-level t]
  17451. ["Previous Same Level" outline-backward-same-level t]
  17452. "--"
  17453. ["Jump" org-goto t])
  17454. ("Edit Structure"
  17455. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17456. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17457. "--"
  17458. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17459. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17460. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17461. "--"
  17462. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17463. "--"
  17464. ["Copy visible text" org-copy-visible t]
  17465. "--"
  17466. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17467. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17468. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17469. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17470. "--"
  17471. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17472. "--"
  17473. ["Convert to odd levels" org-convert-to-odd-levels t]
  17474. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17475. ("Editing"
  17476. ["Emphasis..." org-emphasize t]
  17477. ["Edit Source Example" org-edit-special t]
  17478. "--"
  17479. ["Footnote new/jump" org-footnote-action t]
  17480. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17481. ("Archive"
  17482. ["Archive (default method)" org-archive-subtree-default t]
  17483. "--"
  17484. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17485. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17486. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17487. )
  17488. "--"
  17489. ("Hyperlinks"
  17490. ["Store Link (Global)" org-store-link t]
  17491. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17492. ["Insert Link" org-insert-link t]
  17493. ["Follow Link" org-open-at-point t]
  17494. "--"
  17495. ["Next link" org-next-link t]
  17496. ["Previous link" org-previous-link t]
  17497. "--"
  17498. ["Descriptive Links"
  17499. org-toggle-link-display
  17500. :style radio
  17501. :selected org-descriptive-links
  17502. ]
  17503. ["Literal Links"
  17504. org-toggle-link-display
  17505. :style radio
  17506. :selected (not org-descriptive-links)])
  17507. "--"
  17508. ("TODO Lists"
  17509. ["TODO/DONE/-" org-todo t]
  17510. ("Select keyword"
  17511. ["Next keyword" org-shiftright (org-at-heading-p)]
  17512. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17513. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17514. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17515. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17516. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17517. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17518. "--"
  17519. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17520. :selected org-enforce-todo-dependencies :style toggle :active t]
  17521. "Settings for tree at point"
  17522. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17523. :selected (org-entry-get nil "ORDERED")
  17524. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17525. ["Do Children parallel" org-toggle-ordered-property :style radio
  17526. :selected (not (org-entry-get nil "ORDERED"))
  17527. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17528. "--"
  17529. ["Set Priority" org-priority t]
  17530. ["Priority Up" org-shiftup t]
  17531. ["Priority Down" org-shiftdown t]
  17532. "--"
  17533. ["Get news from all feeds" org-feed-update-all t]
  17534. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17535. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17536. ("TAGS and Properties"
  17537. ["Set Tags" org-set-tags-command t]
  17538. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17539. "--"
  17540. ["Set property" org-set-property t]
  17541. ["Column view of properties" org-columns t]
  17542. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17543. ("Dates and Scheduling"
  17544. ["Timestamp" org-time-stamp t]
  17545. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17546. ("Change Date"
  17547. ["1 Day Later" org-shiftright t]
  17548. ["1 Day Earlier" org-shiftleft t]
  17549. ["1 ... Later" org-shiftup t]
  17550. ["1 ... Earlier" org-shiftdown t])
  17551. ["Compute Time Range" org-evaluate-time-range t]
  17552. ["Schedule Item" org-schedule t]
  17553. ["Deadline" org-deadline t]
  17554. "--"
  17555. ["Custom time format" org-toggle-time-stamp-overlays
  17556. :style radio :selected org-display-custom-times]
  17557. "--"
  17558. ["Goto Calendar" org-goto-calendar t]
  17559. ["Date from Calendar" org-date-from-calendar t]
  17560. "--"
  17561. ["Start/Restart Timer" org-timer-start t]
  17562. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17563. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17564. ["Insert Timer String" org-timer t]
  17565. ["Insert Timer Item" org-timer-item t])
  17566. ("Logging work"
  17567. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17568. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17569. ["Clock out" org-clock-out t]
  17570. ["Clock cancel" org-clock-cancel t]
  17571. "--"
  17572. ["Mark as default task" org-clock-mark-default-task t]
  17573. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17574. ["Goto running clock" org-clock-goto t]
  17575. "--"
  17576. ["Display times" org-clock-display t]
  17577. ["Create clock table" org-clock-report t]
  17578. "--"
  17579. ["Record DONE time"
  17580. (progn (setq org-log-done (not org-log-done))
  17581. (message "Switching to %s will %s record a timestamp"
  17582. (car org-done-keywords)
  17583. (if org-log-done "automatically" "not")))
  17584. :style toggle :selected org-log-done])
  17585. "--"
  17586. ["Agenda Command..." org-agenda t]
  17587. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17588. ("File List for Agenda")
  17589. ("Special views current file"
  17590. ["TODO Tree" org-show-todo-tree t]
  17591. ["Check Deadlines" org-check-deadlines t]
  17592. ["Timeline" org-timeline t]
  17593. ["Tags/Property tree" org-match-sparse-tree t])
  17594. "--"
  17595. ["Export/Publish..." org-export t]
  17596. ("LaTeX"
  17597. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17598. :selected org-cdlatex-mode]
  17599. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17600. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17601. ["Modify math symbol" org-cdlatex-math-modify
  17602. (org-inside-LaTeX-fragment-p)]
  17603. ["Insert citation" org-reftex-citation t]
  17604. "--"
  17605. ["Template for BEAMER" org-insert-beamer-options-template t])
  17606. "--"
  17607. ("MobileOrg"
  17608. ["Push Files and Views" org-mobile-push t]
  17609. ["Get Captured and Flagged" org-mobile-pull t]
  17610. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17611. "--"
  17612. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17613. "--"
  17614. ("Documentation"
  17615. ["Show Version" org-version t]
  17616. ["Info Documentation" org-info t])
  17617. ("Customize"
  17618. ["Browse Org Group" org-customize t]
  17619. "--"
  17620. ["Expand This Menu" org-create-customize-menu
  17621. (fboundp 'customize-menu-create)])
  17622. ["Send bug report" org-submit-bug-report t]
  17623. "--"
  17624. ("Refresh/Reload"
  17625. ["Refresh setup current buffer" org-mode-restart t]
  17626. ["Reload Org (after update)" org-reload t]
  17627. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17628. ))
  17629. (defun org-info (&optional node)
  17630. "Read documentation for Org-mode in the info system.
  17631. With optional NODE, go directly to that node."
  17632. (interactive)
  17633. (info (format "(org)%s" (or node ""))))
  17634. ;;;###autoload
  17635. (defun org-submit-bug-report ()
  17636. "Submit a bug report on Org-mode via mail.
  17637. Don't hesitate to report any problems or inaccurate documentation.
  17638. If you don't have setup sending mail from (X)Emacs, please copy the
  17639. output buffer into your mail program, as it gives us important
  17640. information about your Org-mode version and configuration."
  17641. (interactive)
  17642. (require 'reporter)
  17643. (org-load-modules-maybe)
  17644. (org-require-autoloaded-modules)
  17645. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17646. (reporter-submit-bug-report
  17647. "emacs-orgmode@gnu.org"
  17648. (org-version)
  17649. (let (list)
  17650. (save-window-excursion
  17651. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17652. (delete-other-windows)
  17653. (erase-buffer)
  17654. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17655. We would like to add your full Org-mode and Outline configuration to the
  17656. bug report. This greatly simplifies the work of the maintainer and
  17657. other experts on the mailing list.
  17658. HOWEVER, some variables you have customized may contain private
  17659. information. The names of customers, colleagues, or friends, might
  17660. appear in the form of file names, tags, todo states, or search strings.
  17661. If you answer yes to the prompt, you might want to check and remove
  17662. such private information before sending the email.")
  17663. (add-text-properties (point-min) (point-max) '(face org-warning))
  17664. (when (yes-or-no-p "Include your Org-mode configuration ")
  17665. (mapatoms
  17666. (lambda (v)
  17667. (and (boundp v)
  17668. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17669. (or (and (symbol-value v)
  17670. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17671. (and
  17672. (get v 'custom-type) (get v 'standard-value)
  17673. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17674. (push v list)))))
  17675. (kill-buffer (get-buffer "*Warn about privacy*"))
  17676. list))
  17677. nil nil
  17678. "Remember to cover the basics, that is, what you expected to happen and
  17679. what in fact did happen. You don't know how to make a good report? See
  17680. http://orgmode.org/manual/Feedback.html#Feedback
  17681. Your bug report will be posted to the Org-mode mailing list.
  17682. ------------------------------------------------------------------------")
  17683. (save-excursion
  17684. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17685. (replace-match "\\1Bug: \\3 [\\2]")))))
  17686. (defun org-install-agenda-files-menu ()
  17687. (let ((bl (buffer-list)))
  17688. (save-excursion
  17689. (while bl
  17690. (set-buffer (pop bl))
  17691. (if (eq major-mode 'org-mode) (setq bl nil)))
  17692. (when (eq major-mode 'org-mode)
  17693. (easy-menu-change
  17694. '("Org") "File List for Agenda"
  17695. (append
  17696. (list
  17697. ["Edit File List" (org-edit-agenda-file-list) t]
  17698. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17699. ["Remove Current File from List" org-remove-file t]
  17700. ["Cycle through agenda files" org-cycle-agenda-files t]
  17701. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17702. "--")
  17703. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17704. ;;;; Documentation
  17705. ;;;###autoload
  17706. (defun org-require-autoloaded-modules ()
  17707. (interactive)
  17708. (mapc 'require
  17709. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17710. org-docbook org-exp org-html org-icalendar
  17711. org-id org-latex
  17712. org-publish org-remember org-table
  17713. org-timer org-xoxo)))
  17714. ;;;###autoload
  17715. (defun org-reload (&optional uncompiled)
  17716. "Reload all org lisp files.
  17717. With prefix arg UNCOMPILED, load the uncompiled versions."
  17718. (interactive "P")
  17719. (require 'find-func)
  17720. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17721. (dir-org (file-name-directory (org-find-library-name "org")))
  17722. (dir-org-contrib (ignore-errors
  17723. (file-name-directory
  17724. (org-find-library-name "org-contribdir"))))
  17725. (babel-files
  17726. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17727. (append (list nil "comint" "eval" "exp" "keys"
  17728. "lob" "ref" "table" "tangle")
  17729. (delq nil
  17730. (mapcar
  17731. (lambda (lang)
  17732. (when (cdr lang) (symbol-name (car lang))))
  17733. org-babel-load-languages)))))
  17734. (files
  17735. (append (directory-files dir-org t file-re)
  17736. babel-files
  17737. (and dir-org-contrib
  17738. (directory-files dir-org-contrib t file-re))))
  17739. (remove-re (concat (if (featurep 'xemacs)
  17740. "org-colview" "org-colview-xemacs")
  17741. "\\'")))
  17742. (setq files (mapcar 'file-name-sans-extension files))
  17743. (setq files (mapcar
  17744. (lambda (x) (if (string-match remove-re x) nil x))
  17745. files))
  17746. (setq files (delq nil files))
  17747. (mapc
  17748. (lambda (f)
  17749. (when (featurep (intern (file-name-nondirectory f)))
  17750. (if (and (not uncompiled)
  17751. (file-exists-p (concat f ".elc")))
  17752. (load (concat f ".elc") nil nil t)
  17753. (load (concat f ".el") nil nil t))))
  17754. files))
  17755. (org-version))
  17756. ;;;###autoload
  17757. (defun org-customize ()
  17758. "Call the customize function with org as argument."
  17759. (interactive)
  17760. (org-load-modules-maybe)
  17761. (org-require-autoloaded-modules)
  17762. (customize-browse 'org))
  17763. (defun org-create-customize-menu ()
  17764. "Create a full customization menu for Org-mode, insert it into the menu."
  17765. (interactive)
  17766. (org-load-modules-maybe)
  17767. (org-require-autoloaded-modules)
  17768. (if (fboundp 'customize-menu-create)
  17769. (progn
  17770. (easy-menu-change
  17771. '("Org") "Customize"
  17772. `(["Browse Org group" org-customize t]
  17773. "--"
  17774. ,(customize-menu-create 'org)
  17775. ["Set" Custom-set t]
  17776. ["Save" Custom-save t]
  17777. ["Reset to Current" Custom-reset-current t]
  17778. ["Reset to Saved" Custom-reset-saved t]
  17779. ["Reset to Standard Settings" Custom-reset-standard t]))
  17780. (message "\"Org\"-menu now contains full customization menu"))
  17781. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17782. ;;;; Miscellaneous stuff
  17783. ;;; Generally useful functions
  17784. (defun org-get-at-bol (property)
  17785. "Get text property PROPERTY at beginning of line."
  17786. (get-text-property (point-at-bol) property))
  17787. (defun org-find-text-property-in-string (prop s)
  17788. "Return the first non-nil value of property PROP in string S."
  17789. (or (get-text-property 0 prop s)
  17790. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17791. prop s)))
  17792. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17793. "Display the given MESSAGE as a warning."
  17794. (if (fboundp 'display-warning)
  17795. (display-warning 'org message
  17796. (if (featurep 'xemacs) 'warning :warning))
  17797. (let ((buf (get-buffer-create "*Org warnings*")))
  17798. (with-current-buffer buf
  17799. (goto-char (point-max))
  17800. (insert "Warning (Org): " message)
  17801. (unless (bolp)
  17802. (newline)))
  17803. (display-buffer buf)
  17804. (sit-for 0))))
  17805. (defun org-eval (form)
  17806. "Eval FORM and return result."
  17807. (condition-case error
  17808. (eval form)
  17809. (error (format "%%![Error: %s]" error))))
  17810. (defun org-in-clocktable-p ()
  17811. "Check if the cursor is in a clocktable."
  17812. (let ((pos (point)) start)
  17813. (save-excursion
  17814. (end-of-line 1)
  17815. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  17816. (setq start (match-beginning 0))
  17817. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  17818. (>= (match-end 0) pos)
  17819. start))))
  17820. (defun org-in-commented-line ()
  17821. "Is point in a line starting with `#'?"
  17822. (equal (char-after (point-at-bol)) ?#))
  17823. (defun org-in-indented-comment-line ()
  17824. "Is point in a line starting with `#' after some white space?"
  17825. (save-excursion
  17826. (save-match-data
  17827. (goto-char (point-at-bol))
  17828. (looking-at "[ \t]*#"))))
  17829. (defun org-in-verbatim-emphasis ()
  17830. (save-match-data
  17831. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17832. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17833. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17834. (if (and marker (marker-buffer marker)
  17835. (buffer-live-p (marker-buffer marker)))
  17836. (progn
  17837. (org-pop-to-buffer-same-window (marker-buffer marker))
  17838. (if (or (> marker (point-max)) (< marker (point-min)))
  17839. (widen))
  17840. (goto-char marker)
  17841. (org-show-context 'org-goto))
  17842. (if bookmark
  17843. (bookmark-jump bookmark)
  17844. (error "Cannot find location"))))
  17845. (defun org-quote-csv-field (s)
  17846. "Quote field for inclusion in CSV material."
  17847. (if (string-match "[\",]" s)
  17848. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17849. s))
  17850. (defun org-force-self-insert (N)
  17851. "Needed to enforce self-insert under remapping."
  17852. (interactive "p")
  17853. (self-insert-command N))
  17854. (defun org-string-width (s)
  17855. "Compute width of string, ignoring invisible characters.
  17856. This ignores character with invisibility property `org-link', and also
  17857. characters with property `org-cwidth', because these will become invisible
  17858. upon the next fontification round."
  17859. (let (b l)
  17860. (when (or (eq t buffer-invisibility-spec)
  17861. (assq 'org-link buffer-invisibility-spec))
  17862. (while (setq b (text-property-any 0 (length s)
  17863. 'invisible 'org-link s))
  17864. (setq s (concat (substring s 0 b)
  17865. (substring s (or (next-single-property-change
  17866. b 'invisible s) (length s)))))))
  17867. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17868. (setq s (concat (substring s 0 b)
  17869. (substring s (or (next-single-property-change
  17870. b 'org-cwidth s) (length s))))))
  17871. (setq l (string-width s) b -1)
  17872. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17873. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17874. l))
  17875. (defun org-shorten-string (s maxlength)
  17876. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17877. If the string is shorter or has length MAXLENGTH, just return the
  17878. original string. If it is longer, the functions finds a space in the
  17879. string, breaks this string off at that locations and adds three dots
  17880. as ellipsis. Including the ellipsis, the string will not be longer
  17881. than MAXLENGTH. If finding a good breaking point in the string does
  17882. not work, the string is just chopped off in the middle of a word
  17883. if necessary."
  17884. (if (<= (length s) maxlength)
  17885. s
  17886. (let* ((n (max (- maxlength 4) 1))
  17887. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17888. (if (string-match re s)
  17889. (concat (match-string 1 s) "...")
  17890. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17891. (defun org-get-indentation (&optional line)
  17892. "Get the indentation of the current line, interpreting tabs.
  17893. When LINE is given, assume it represents a line and compute its indentation."
  17894. (if line
  17895. (if (string-match "^ *" (org-remove-tabs line))
  17896. (match-end 0))
  17897. (save-excursion
  17898. (beginning-of-line 1)
  17899. (skip-chars-forward " \t")
  17900. (current-column))))
  17901. (defun org-get-string-indentation (s)
  17902. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17903. (let ((n -1) (i 0) (w tab-width) c)
  17904. (catch 'exit
  17905. (while (< (setq n (1+ n)) (length s))
  17906. (setq c (aref s n))
  17907. (cond ((= c ?\ ) (setq i (1+ i)))
  17908. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17909. (t (throw 'exit t)))))
  17910. i))
  17911. (defun org-remove-tabs (s &optional width)
  17912. "Replace tabulators in S with spaces.
  17913. Assumes that s is a single line, starting in column 0."
  17914. (setq width (or width tab-width))
  17915. (while (string-match "\t" s)
  17916. (setq s (replace-match
  17917. (make-string
  17918. (- (* width (/ (+ (match-beginning 0) width) width))
  17919. (match-beginning 0)) ?\ )
  17920. t t s)))
  17921. s)
  17922. (defun org-fix-indentation (line ind)
  17923. "Fix indentation in LINE.
  17924. IND is a cons cell with target and minimum indentation.
  17925. If the current indentation in LINE is smaller than the minimum,
  17926. leave it alone. If it is larger than ind, set it to the target."
  17927. (let* ((l (org-remove-tabs line))
  17928. (i (org-get-indentation l))
  17929. (i1 (car ind)) (i2 (cdr ind)))
  17930. (if (>= i i2) (setq l (substring line i2)))
  17931. (if (> i1 0)
  17932. (concat (make-string i1 ?\ ) l)
  17933. l)))
  17934. (defun org-remove-indentation (code &optional n)
  17935. "Remove the maximum common indentation from the lines in CODE.
  17936. N may optionally be the number of spaces to remove."
  17937. (with-temp-buffer
  17938. (insert code)
  17939. (org-do-remove-indentation n)
  17940. (buffer-string)))
  17941. (defun org-do-remove-indentation (&optional n)
  17942. "Remove the maximum common indentation from the buffer."
  17943. (untabify (point-min) (point-max))
  17944. (let ((min 10000) re)
  17945. (if n
  17946. (setq min n)
  17947. (goto-char (point-min))
  17948. (while (re-search-forward "^ *[^ \n]" nil t)
  17949. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17950. (unless (or (= min 0) (= min 10000))
  17951. (setq re (format "^ \\{%d\\}" min))
  17952. (goto-char (point-min))
  17953. (while (re-search-forward re nil t)
  17954. (replace-match "")
  17955. (end-of-line 1))
  17956. min)))
  17957. (defun org-fill-template (template alist)
  17958. "Find each %key of ALIST in TEMPLATE and replace it."
  17959. (let ((case-fold-search nil)
  17960. entry key value)
  17961. (setq alist (sort (copy-sequence alist)
  17962. (lambda (a b) (< (length (car a)) (length (car b))))))
  17963. (while (setq entry (pop alist))
  17964. (setq template
  17965. (replace-regexp-in-string
  17966. (concat "%" (regexp-quote (car entry)))
  17967. (cdr entry) template t t)))
  17968. template))
  17969. (defun org-base-buffer (buffer)
  17970. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17971. (if (not buffer)
  17972. buffer
  17973. (or (buffer-base-buffer buffer)
  17974. buffer)))
  17975. (defun org-trim (s)
  17976. "Remove whitespace at beginning and end of string."
  17977. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17978. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17979. s)
  17980. (defun org-wrap (string &optional width lines)
  17981. "Wrap string to either a number of lines, or a width in characters.
  17982. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17983. that costs. If there is a word longer than WIDTH, the text is actually
  17984. wrapped to the length of that word.
  17985. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17986. many lines, whatever width that takes.
  17987. The return value is a list of lines, without newlines at the end."
  17988. (let* ((words (org-split-string string "[ \t\n]+"))
  17989. (maxword (apply 'max (mapcar 'org-string-width words)))
  17990. w ll)
  17991. (cond (width
  17992. (org-do-wrap words (max maxword width)))
  17993. (lines
  17994. (setq w maxword)
  17995. (setq ll (org-do-wrap words maxword))
  17996. (if (<= (length ll) lines)
  17997. ll
  17998. (setq ll words)
  17999. (while (> (length ll) lines)
  18000. (setq w (1+ w))
  18001. (setq ll (org-do-wrap words w)))
  18002. ll))
  18003. (t (error "Cannot wrap this")))))
  18004. (defun org-do-wrap (words width)
  18005. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18006. (let (lines line)
  18007. (while words
  18008. (setq line (pop words))
  18009. (while (and words (< (+ (length line) (length (car words))) width))
  18010. (setq line (concat line " " (pop words))))
  18011. (setq lines (push line lines)))
  18012. (nreverse lines)))
  18013. (defun org-split-string (string &optional separators)
  18014. "Splits STRING into substrings at SEPARATORS.
  18015. No empty strings are returned if there are matches at the beginning
  18016. and end of string."
  18017. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18018. (start 0)
  18019. notfirst
  18020. (list nil))
  18021. (while (and (string-match rexp string
  18022. (if (and notfirst
  18023. (= start (match-beginning 0))
  18024. (< start (length string)))
  18025. (1+ start) start))
  18026. (< (match-beginning 0) (length string)))
  18027. (setq notfirst t)
  18028. (or (eq (match-beginning 0) 0)
  18029. (and (eq (match-beginning 0) (match-end 0))
  18030. (eq (match-beginning 0) start))
  18031. (setq list
  18032. (cons (substring string start (match-beginning 0))
  18033. list)))
  18034. (setq start (match-end 0)))
  18035. (or (eq start (length string))
  18036. (setq list
  18037. (cons (substring string start)
  18038. list)))
  18039. (nreverse list)))
  18040. (defun org-quote-vert (s)
  18041. "Replace \"|\" with \"\\vert\"."
  18042. (while (string-match "|" s)
  18043. (setq s (replace-match "\\vert" t t s)))
  18044. s)
  18045. (defun org-uuidgen-p (s)
  18046. "Is S an ID created by UUIDGEN?"
  18047. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18048. (defun org-in-src-block-p nil
  18049. "Whether point is in a code source block."
  18050. (let (ov)
  18051. (when (setq ov (overlays-at (point)))
  18052. (memq 'org-block-background
  18053. (overlay-properties
  18054. (car ov))))))
  18055. (defun org-context ()
  18056. "Return a list of contexts of the current cursor position.
  18057. If several contexts apply, all are returned.
  18058. Each context entry is a list with a symbol naming the context, and
  18059. two positions indicating start and end of the context. Possible
  18060. contexts are:
  18061. :headline anywhere in a headline
  18062. :headline-stars on the leading stars in a headline
  18063. :todo-keyword on a TODO keyword (including DONE) in a headline
  18064. :tags on the TAGS in a headline
  18065. :priority on the priority cookie in a headline
  18066. :item on the first line of a plain list item
  18067. :item-bullet on the bullet/number of a plain list item
  18068. :checkbox on the checkbox in a plain list item
  18069. :table in an org-mode table
  18070. :table-special on a special filed in a table
  18071. :table-table in a table.el table
  18072. :clocktable in a clocktable
  18073. :src-block in a source block
  18074. :link on a hyperlink
  18075. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18076. :target on a <<target>>
  18077. :radio-target on a <<<radio-target>>>
  18078. :latex-fragment on a LaTeX fragment
  18079. :latex-preview on a LaTeX fragment with overlaid preview image
  18080. This function expects the position to be visible because it uses font-lock
  18081. faces as a help to recognize the following contexts: :table-special, :link,
  18082. and :keyword."
  18083. (let* ((f (get-text-property (point) 'face))
  18084. (faces (if (listp f) f (list f)))
  18085. (case-fold-search t)
  18086. (p (point)) clist o)
  18087. ;; First the large context
  18088. (cond
  18089. ((org-at-heading-p t)
  18090. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18091. (when (progn
  18092. (beginning-of-line 1)
  18093. (looking-at org-todo-line-tags-regexp))
  18094. (push (org-point-in-group p 1 :headline-stars) clist)
  18095. (push (org-point-in-group p 2 :todo-keyword) clist)
  18096. (push (org-point-in-group p 4 :tags) clist))
  18097. (goto-char p)
  18098. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18099. (if (looking-at "\\[#[A-Z0-9]\\]")
  18100. (push (org-point-in-group p 0 :priority) clist)))
  18101. ((org-at-item-p)
  18102. (push (org-point-in-group p 2 :item-bullet) clist)
  18103. (push (list :item (point-at-bol)
  18104. (save-excursion (org-end-of-item) (point)))
  18105. clist)
  18106. (and (org-at-item-checkbox-p)
  18107. (push (org-point-in-group p 0 :checkbox) clist)))
  18108. ((org-at-table-p)
  18109. (push (list :table (org-table-begin) (org-table-end)) clist)
  18110. (if (memq 'org-formula faces)
  18111. (push (list :table-special
  18112. (previous-single-property-change p 'face)
  18113. (next-single-property-change p 'face)) clist)))
  18114. ((org-at-table-p 'any)
  18115. (push (list :table-table) clist)))
  18116. (goto-char p)
  18117. ;; New the "medium" contexts: clocktables, source blocks
  18118. (cond ((org-in-clocktable-p)
  18119. (push (list :clocktable
  18120. (and (or (looking-at "#\\+BEGIN: clocktable")
  18121. (search-backward "#+BEGIN: clocktable" nil t))
  18122. (match-beginning 0))
  18123. (and (re-search-forward "#\\+END:?" nil t)
  18124. (match-end 0))) clist))
  18125. ((org-in-src-block-p)
  18126. (push (list :src-block
  18127. (and (or (looking-at "#\\+BEGIN_SRC")
  18128. (search-backward "#+BEGIN_SRC" nil t))
  18129. (match-beginning 0))
  18130. (and (search-forward "#+END_SRC" nil t)
  18131. (match-beginning 0))) clist)))
  18132. (goto-char p)
  18133. ;; Now the small context
  18134. (cond
  18135. ((org-at-timestamp-p)
  18136. (push (org-point-in-group p 0 :timestamp) clist))
  18137. ((memq 'org-link faces)
  18138. (push (list :link
  18139. (previous-single-property-change p 'face)
  18140. (next-single-property-change p 'face)) clist))
  18141. ((memq 'org-special-keyword faces)
  18142. (push (list :keyword
  18143. (previous-single-property-change p 'face)
  18144. (next-single-property-change p 'face)) clist))
  18145. ((org-at-target-p)
  18146. (push (org-point-in-group p 0 :target) clist)
  18147. (goto-char (1- (match-beginning 0)))
  18148. (if (looking-at org-radio-target-regexp)
  18149. (push (org-point-in-group p 0 :radio-target) clist))
  18150. (goto-char p))
  18151. ((setq o (car (delq nil
  18152. (mapcar
  18153. (lambda (x)
  18154. (if (memq x org-latex-fragment-image-overlays) x))
  18155. (overlays-at (point))))))
  18156. (push (list :latex-fragment
  18157. (overlay-start o) (overlay-end o)) clist)
  18158. (push (list :latex-preview
  18159. (overlay-start o) (overlay-end o)) clist))
  18160. ((org-inside-LaTeX-fragment-p)
  18161. ;; FIXME: positions wrong.
  18162. (push (list :latex-fragment (point) (point)) clist)))
  18163. (setq clist (nreverse (delq nil clist)))
  18164. clist))
  18165. ;; FIXME: Compare with at-regexp-p Do we need both?
  18166. (defun org-in-regexp (re &optional nlines visually)
  18167. "Check if point is inside a match of regexp.
  18168. Normally only the current line is checked, but you can include NLINES extra
  18169. lines both before and after point into the search.
  18170. If VISUALLY is set, require that the cursor is not after the match but
  18171. really on, so that the block visually is on the match."
  18172. (catch 'exit
  18173. (let ((pos (point))
  18174. (eol (point-at-eol (+ 1 (or nlines 0))))
  18175. (inc (if visually 1 0)))
  18176. (save-excursion
  18177. (beginning-of-line (- 1 (or nlines 0)))
  18178. (while (re-search-forward re eol t)
  18179. (if (and (<= (match-beginning 0) pos)
  18180. (>= (+ inc (match-end 0)) pos))
  18181. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18182. (defun org-at-regexp-p (regexp)
  18183. "Is point inside a match of REGEXP in the current line?"
  18184. (catch 'exit
  18185. (save-excursion
  18186. (let ((pos (point)) (end (point-at-eol)))
  18187. (beginning-of-line 1)
  18188. (while (re-search-forward regexp end t)
  18189. (if (and (<= (match-beginning 0) pos)
  18190. (>= (match-end 0) pos))
  18191. (throw 'exit t)))
  18192. nil))))
  18193. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18194. "Non-nil when point is between matches of START-RE and END-RE.
  18195. Also return a non-nil value when point is on one of the matches.
  18196. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18197. buffer positions. Default values are the positions of headlines
  18198. surrounding the point.
  18199. The functions returns a cons cell whose car (resp. cdr) is the
  18200. position before START-RE (resp. after END-RE)."
  18201. (save-match-data
  18202. (let ((pos (point))
  18203. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18204. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18205. beg end)
  18206. (save-excursion
  18207. ;; Point is on a block when on START-RE or if START-RE can be
  18208. ;; found before it...
  18209. (and (or (org-at-regexp-p start-re)
  18210. (re-search-backward start-re limit-up t))
  18211. (setq beg (match-beginning 0))
  18212. ;; ... and END-RE after it...
  18213. (goto-char (match-end 0))
  18214. (re-search-forward end-re limit-down t)
  18215. (> (setq end (match-end 0)) pos)
  18216. ;; ... without another START-RE in-between.
  18217. (goto-char (match-beginning 0))
  18218. (not (re-search-backward start-re (1+ beg) t))
  18219. ;; Return value.
  18220. (cons beg end))))))
  18221. (defun org-in-block-p (names)
  18222. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18223. NAMES is a list of strings containing names of blocks.
  18224. Return first block name matched, or nil. Beware that in case of
  18225. nested blocks, the returned name may not belong to the closest
  18226. block from point."
  18227. (save-match-data
  18228. (catch 'exit
  18229. (let ((case-fold-search t)
  18230. (lim-up (save-excursion (outline-previous-heading)))
  18231. (lim-down (save-excursion (outline-next-heading))))
  18232. (mapc (lambda (name)
  18233. (let ((n (regexp-quote name)))
  18234. (when (org-between-regexps-p
  18235. (concat "^[ \t]*#\\+begin_" n)
  18236. (concat "^[ \t]*#\\+end_" n)
  18237. lim-up lim-down)
  18238. (throw 'exit n))))
  18239. names))
  18240. nil)))
  18241. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18242. "Call `multi-occur' with buffers for all agenda files."
  18243. (interactive "sOrg-files matching: \np")
  18244. (let* ((files (org-agenda-files))
  18245. (tnames (mapcar 'file-truename files))
  18246. (extra org-agenda-text-search-extra-files)
  18247. f)
  18248. (when (eq (car extra) 'agenda-archives)
  18249. (setq extra (cdr extra))
  18250. (setq files (org-add-archive-files files)))
  18251. (while (setq f (pop extra))
  18252. (unless (member (file-truename f) tnames)
  18253. (add-to-list 'files f 'append)
  18254. (add-to-list 'tnames (file-truename f) 'append)))
  18255. (multi-occur
  18256. (mapcar (lambda (x)
  18257. (with-current-buffer
  18258. (or (get-file-buffer x) (find-file-noselect x))
  18259. (widen)
  18260. (current-buffer)))
  18261. files)
  18262. regexp)))
  18263. (if (boundp 'occur-mode-find-occurrence-hook)
  18264. ;; Emacs 23
  18265. (add-hook 'occur-mode-find-occurrence-hook
  18266. (lambda ()
  18267. (when (eq major-mode 'org-mode)
  18268. (org-reveal))))
  18269. ;; Emacs 22
  18270. (defadvice occur-mode-goto-occurrence
  18271. (after org-occur-reveal activate)
  18272. (and (eq major-mode 'org-mode) (org-reveal)))
  18273. (defadvice occur-mode-goto-occurrence-other-window
  18274. (after org-occur-reveal activate)
  18275. (and (eq major-mode 'org-mode) (org-reveal)))
  18276. (defadvice occur-mode-display-occurrence
  18277. (after org-occur-reveal activate)
  18278. (when (eq major-mode 'org-mode)
  18279. (let ((pos (occur-mode-find-occurrence)))
  18280. (with-current-buffer (marker-buffer pos)
  18281. (save-excursion
  18282. (goto-char pos)
  18283. (org-reveal)))))))
  18284. (defun org-occur-link-in-agenda-files ()
  18285. "Create a link and search for it in the agendas.
  18286. The link is not stored in `org-stored-links', it is just created
  18287. for the search purpose."
  18288. (interactive)
  18289. (let ((link (condition-case nil
  18290. (org-store-link nil)
  18291. (error "Unable to create a link to here"))))
  18292. (org-occur-in-agenda-files (regexp-quote link))))
  18293. (defun org-uniquify (list)
  18294. "Remove duplicate elements from LIST."
  18295. (let (res)
  18296. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18297. res))
  18298. (defun org-delete-all (elts list)
  18299. "Remove all elements in ELTS from LIST."
  18300. (while elts
  18301. (setq list (delete (pop elts) list)))
  18302. list)
  18303. (defun org-count (cl-item cl-seq)
  18304. "Count the number of occurrences of ITEM in SEQ.
  18305. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18306. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18307. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18308. (while (< cl-start cl-end)
  18309. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18310. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18311. (setq cl-start (1+ cl-start)))
  18312. cl-count))
  18313. (defun org-remove-if (predicate seq)
  18314. "Remove everything from SEQ that fulfills PREDICATE."
  18315. (let (res e)
  18316. (while seq
  18317. (setq e (pop seq))
  18318. (if (not (funcall predicate e)) (push e res)))
  18319. (nreverse res)))
  18320. (defun org-remove-if-not (predicate seq)
  18321. "Remove everything from SEQ that does not fulfill PREDICATE."
  18322. (let (res e)
  18323. (while seq
  18324. (setq e (pop seq))
  18325. (if (funcall predicate e) (push e res)))
  18326. (nreverse res)))
  18327. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18328. "Reduce two-argument FUNCTION across SEQ.
  18329. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18330. \":initial-value\" removed."
  18331. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18332. (cadr (memq :initial-value cl-keys)))
  18333. (cl-seq (pop cl-seq))
  18334. (t (funcall cl-func)))))
  18335. (while cl-seq
  18336. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18337. cl-accum))
  18338. (defun org-back-over-empty-lines ()
  18339. "Move backwards over whitespace, to the beginning of the first empty line.
  18340. Returns the number of empty lines passed."
  18341. (let ((pos (point)))
  18342. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18343. (skip-chars-backward " \t\n\r")
  18344. (unless (eobp)
  18345. (forward-line -1)))
  18346. (beginning-of-line 2)
  18347. (goto-char (min (point) pos))
  18348. (count-lines (point) pos)))
  18349. (defun org-skip-whitespace ()
  18350. (skip-chars-forward " \t\n\r"))
  18351. (defun org-point-in-group (point group &optional context)
  18352. "Check if POINT is in match-group GROUP.
  18353. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18354. match. If the match group does not exist or point is not inside it,
  18355. return nil."
  18356. (and (match-beginning group)
  18357. (>= point (match-beginning group))
  18358. (<= point (match-end group))
  18359. (if context
  18360. (list context (match-beginning group) (match-end group))
  18361. t)))
  18362. (defun org-switch-to-buffer-other-window (&rest args)
  18363. "Switch to buffer in a second window on the current frame.
  18364. In particular, do not allow pop-up frames.
  18365. Returns the newly created buffer."
  18366. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18367. special-display-function)
  18368. (apply 'switch-to-buffer-other-window args)))
  18369. (defun org-combine-plists (&rest plists)
  18370. "Create a single property list from all plists in PLISTS.
  18371. The process starts by copying the first list, and then setting properties
  18372. from the other lists. Settings in the last list are the most significant
  18373. ones and overrule settings in the other lists."
  18374. (let ((rtn (copy-sequence (pop plists)))
  18375. p v ls)
  18376. (while plists
  18377. (setq ls (pop plists))
  18378. (while ls
  18379. (setq p (pop ls) v (pop ls))
  18380. (setq rtn (plist-put rtn p v))))
  18381. rtn))
  18382. (defun org-move-line-down (arg)
  18383. "Move the current line down. With prefix argument, move it past ARG lines."
  18384. (interactive "p")
  18385. (let ((col (current-column))
  18386. beg end pos)
  18387. (beginning-of-line 1) (setq beg (point))
  18388. (beginning-of-line 2) (setq end (point))
  18389. (beginning-of-line (+ 1 arg))
  18390. (setq pos (move-marker (make-marker) (point)))
  18391. (insert (delete-and-extract-region beg end))
  18392. (goto-char pos)
  18393. (org-move-to-column col)))
  18394. (defun org-move-line-up (arg)
  18395. "Move the current line up. With prefix argument, move it past ARG lines."
  18396. (interactive "p")
  18397. (let ((col (current-column))
  18398. beg end pos)
  18399. (beginning-of-line 1) (setq beg (point))
  18400. (beginning-of-line 2) (setq end (point))
  18401. (beginning-of-line (- arg))
  18402. (setq pos (move-marker (make-marker) (point)))
  18403. (insert (delete-and-extract-region beg end))
  18404. (goto-char pos)
  18405. (org-move-to-column col)))
  18406. (defun org-replace-escapes (string table)
  18407. "Replace %-escapes in STRING with values in TABLE.
  18408. TABLE is an association list with keys like \"%a\" and string values.
  18409. The sequences in STRING may contain normal field width and padding information,
  18410. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18411. so values can contain further %-escapes if they are define later in TABLE."
  18412. (let ((tbl (copy-alist table))
  18413. (case-fold-search nil)
  18414. (pchg 0)
  18415. e re rpl)
  18416. (while (setq e (pop tbl))
  18417. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18418. (when (and (cdr e) (string-match re (cdr e)))
  18419. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18420. (safe "SREF"))
  18421. (add-text-properties 0 3 (list 'sref sref) safe)
  18422. (setcdr e (replace-match safe t t (cdr e)))))
  18423. (while (string-match re string)
  18424. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18425. (cdr e)))
  18426. (setq string (replace-match rpl t t string))))
  18427. (while (setq pchg (next-property-change pchg string))
  18428. (let ((sref (get-text-property pchg 'sref string)))
  18429. (when (and sref (string-match "SREF" string pchg))
  18430. (setq string (replace-match sref t t string)))))
  18431. string))
  18432. (defun org-sublist (list start end)
  18433. "Return a section of LIST, from START to END.
  18434. Counting starts at 1."
  18435. (let (rtn (c start))
  18436. (setq list (nthcdr (1- start) list))
  18437. (while (and list (<= c end))
  18438. (push (pop list) rtn)
  18439. (setq c (1+ c)))
  18440. (nreverse rtn)))
  18441. (defun org-find-base-buffer-visiting (file)
  18442. "Like `find-buffer-visiting' but always return the base buffer and
  18443. not an indirect buffer."
  18444. (let ((buf (or (get-file-buffer file)
  18445. (find-buffer-visiting file))))
  18446. (if buf
  18447. (or (buffer-base-buffer buf) buf)
  18448. nil)))
  18449. (defun org-image-file-name-regexp (&optional extensions)
  18450. "Return regexp matching the file names of images.
  18451. If EXTENSIONS is given, only match these."
  18452. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18453. (image-file-name-regexp)
  18454. (let ((image-file-name-extensions
  18455. (or extensions
  18456. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18457. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18458. (concat "\\."
  18459. (regexp-opt (nconc (mapcar 'upcase
  18460. image-file-name-extensions)
  18461. image-file-name-extensions)
  18462. t)
  18463. "\\'"))))
  18464. (defun org-file-image-p (file &optional extensions)
  18465. "Return non-nil if FILE is an image."
  18466. (save-match-data
  18467. (string-match (org-image-file-name-regexp extensions) file)))
  18468. (defun org-get-cursor-date ()
  18469. "Return the date at cursor in as a time.
  18470. This works in the calendar and in the agenda, anywhere else it just
  18471. returns the current time."
  18472. (let (date day defd)
  18473. (cond
  18474. ((eq major-mode 'calendar-mode)
  18475. (setq date (calendar-cursor-to-date)
  18476. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18477. ((eq major-mode 'org-agenda-mode)
  18478. (setq day (get-text-property (point) 'day))
  18479. (if day
  18480. (setq date (calendar-gregorian-from-absolute day)
  18481. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18482. (nth 2 date))))))
  18483. (or defd (current-time))))
  18484. (defvar org-agenda-action-marker (make-marker)
  18485. "Marker pointing to the entry for the next agenda action.")
  18486. (defun org-mark-entry-for-agenda-action ()
  18487. "Mark the current entry as target of an agenda action.
  18488. Agenda actions are actions executed from the agenda with the key `k',
  18489. which make use of the date at the cursor."
  18490. (interactive)
  18491. (move-marker org-agenda-action-marker
  18492. (save-excursion (org-back-to-heading t) (point))
  18493. (current-buffer))
  18494. (message
  18495. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18496. (defun org-mark-subtree ()
  18497. "Mark the current subtree.
  18498. This puts point at the start of the current subtree, and mark at the end.
  18499. If point is in an inline task, mark that task instead."
  18500. (interactive)
  18501. (let ((inline-task-p
  18502. (and (featurep 'org-inlinetask)
  18503. (org-inlinetask-in-task-p)))
  18504. (beg))
  18505. ;; Get beginning of subtree
  18506. (cond
  18507. (inline-task-p (org-inlinetask-goto-beginning))
  18508. ((org-at-heading-p) (beginning-of-line))
  18509. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18510. (setq beg (point))
  18511. ;; Get end of it
  18512. (if inline-task-p
  18513. (org-inlinetask-goto-end)
  18514. (org-end-of-subtree))
  18515. ;; Mark zone
  18516. (push-mark (point) nil t)
  18517. (goto-char beg)))
  18518. ;;; Paragraph filling stuff.
  18519. ;; We want this to be just right, so use the full arsenal.
  18520. (defun org-indent-line-function ()
  18521. "Indent line depending on context."
  18522. (interactive)
  18523. (let* ((pos (point))
  18524. (itemp (org-at-item-p))
  18525. (case-fold-search t)
  18526. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18527. (inline-task-p (and (featurep 'org-inlinetask)
  18528. (org-inlinetask-in-task-p)))
  18529. (inline-re (and inline-task-p
  18530. (org-inlinetask-outline-regexp)))
  18531. column)
  18532. (beginning-of-line 1)
  18533. (cond
  18534. ;; Comments
  18535. ((looking-at "# ") (setq column 0))
  18536. ;; Headings
  18537. ((looking-at org-outline-regexp) (setq column 0))
  18538. ;; Included files
  18539. ((looking-at "#\\+include:") (setq column 0))
  18540. ;; Footnote definition
  18541. ((looking-at org-footnote-definition-re) (setq column 0))
  18542. ;; Literal examples
  18543. ((looking-at "[ \t]*:\\( \\|$\\)")
  18544. (setq column (org-get-indentation))) ; do nothing
  18545. ;; Lists
  18546. ((ignore-errors (goto-char (org-in-item-p)))
  18547. (setq column (if itemp
  18548. (org-get-indentation)
  18549. (org-list-item-body-column (point))))
  18550. (goto-char pos))
  18551. ;; Drawers
  18552. ((and (looking-at "[ \t]*:END:")
  18553. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18554. (save-excursion
  18555. (goto-char (1- (match-beginning 1)))
  18556. (setq column (current-column))))
  18557. ;; Special blocks
  18558. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18559. (save-excursion
  18560. (re-search-backward
  18561. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18562. (setq column (org-get-indentation (match-string 0))))
  18563. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18564. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18565. (save-excursion
  18566. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18567. (setq column
  18568. (cond ((equal (downcase (match-string 1)) "src")
  18569. ;; src blocks: let `org-edit-src-exit' handle them
  18570. (org-get-indentation))
  18571. ((equal (downcase (match-string 1)) "example")
  18572. (max (org-get-indentation)
  18573. (org-get-indentation (match-string 0))))
  18574. (t
  18575. (org-get-indentation (match-string 0))))))
  18576. ;; This line has nothing special, look at the previous relevant
  18577. ;; line to compute indentation
  18578. (t
  18579. (beginning-of-line 0)
  18580. (while (and (not (bobp))
  18581. (not (looking-at org-drawer-regexp))
  18582. ;; When point started in an inline task, do not move
  18583. ;; above task starting line.
  18584. (not (and inline-task-p (looking-at inline-re)))
  18585. ;; Skip drawers, blocks, empty lines, verbatim,
  18586. ;; comments, tables, footnotes definitions, lists,
  18587. ;; inline tasks.
  18588. (or (and (looking-at "[ \t]*:END:")
  18589. (re-search-backward org-drawer-regexp nil t))
  18590. (and (looking-at "[ \t]*#\\+end_")
  18591. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18592. (looking-at "[ \t]*[\n:#|]")
  18593. (looking-at org-footnote-definition-re)
  18594. (and (ignore-errors (goto-char (org-in-item-p)))
  18595. (goto-char
  18596. (org-list-get-top-point (org-list-struct))))
  18597. (and (not inline-task-p)
  18598. (featurep 'org-inlinetask)
  18599. (org-inlinetask-in-task-p)
  18600. (or (org-inlinetask-goto-beginning) t))))
  18601. (beginning-of-line 0))
  18602. (cond
  18603. ;; There was an heading above.
  18604. ((looking-at "\\*+[ \t]+")
  18605. (if (not org-adapt-indentation)
  18606. (setq column 0)
  18607. (goto-char (match-end 0))
  18608. (setq column (current-column))))
  18609. ;; A drawer had started and is unfinished
  18610. ((looking-at org-drawer-regexp)
  18611. (goto-char (1- (match-beginning 1)))
  18612. (setq column (current-column)))
  18613. ;; Else, nothing noticeable found: get indentation and go on.
  18614. (t (setq column (org-get-indentation))))))
  18615. ;; Now apply indentation and move cursor accordingly
  18616. (goto-char pos)
  18617. (if (<= (current-column) (current-indentation))
  18618. (org-indent-line-to column)
  18619. (save-excursion (org-indent-line-to column)))
  18620. ;; Special polishing for properties, see `org-property-format'
  18621. (setq column (current-column))
  18622. (beginning-of-line 1)
  18623. (if (looking-at
  18624. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18625. (replace-match (concat (match-string 1)
  18626. (format org-property-format
  18627. (match-string 2) (match-string 3)))
  18628. t t))
  18629. (org-move-to-column column)))
  18630. (defun org-indent-drawer ()
  18631. "Indent the drawer at point."
  18632. (interactive)
  18633. (let ((p (point))
  18634. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18635. (match-end 0)))
  18636. (folded
  18637. (save-excursion
  18638. (end-of-line)
  18639. (when (overlays-at (point))
  18640. (member 'invisible (overlay-properties
  18641. (car (overlays-at (point)))))))))
  18642. (when folded (org-cycle))
  18643. (indent-for-tab-command)
  18644. (while (and (move-beginning-of-line 2) (< (point) e))
  18645. (indent-for-tab-command))
  18646. (goto-char p)
  18647. (when folded (org-cycle)))
  18648. (message "Drawer at point indented"))
  18649. (defun org-indent-block ()
  18650. "Indent the block at point."
  18651. (interactive)
  18652. (let ((p (point))
  18653. (case-fold-search t)
  18654. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18655. (match-end 0)))
  18656. (folded
  18657. (save-excursion
  18658. (end-of-line)
  18659. (when (overlays-at (point))
  18660. (member 'invisible (overlay-properties
  18661. (car (overlays-at (point)))))))))
  18662. (when folded (org-cycle))
  18663. (indent-for-tab-command)
  18664. (while (and (move-beginning-of-line 2) (< (point) e))
  18665. (indent-for-tab-command))
  18666. (goto-char p)
  18667. (when folded (org-cycle)))
  18668. (message "Block at point indented"))
  18669. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18670. "Variable to store copy of `adaptive-fill-regexp'.
  18671. Since `adaptive-fill-regexp' is set to never match, we need to
  18672. store a backup of its value before entering `org-mode' so that
  18673. the functionality can be provided as a fall-back.")
  18674. (defun org-set-autofill-regexps ()
  18675. (interactive)
  18676. ;; In the paragraph separator we include headlines, because filling
  18677. ;; text in a line directly attached to a headline would otherwise
  18678. ;; fill the headline as well.
  18679. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18680. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18681. ;; The paragraph starter includes hand-formatted lists.
  18682. (org-set-local
  18683. 'paragraph-start
  18684. (concat
  18685. "\f" "\\|"
  18686. "[ ]*$" "\\|"
  18687. org-outline-regexp "\\|"
  18688. "[ \t]*#" "\\|"
  18689. (org-item-re) "\\|"
  18690. "[ \t]*[:|]" "\\|"
  18691. "\\$\\$" "\\|"
  18692. "\\\\\\(begin\\|end\\|[][]\\)"))
  18693. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18694. ;; But only if the user has not turned off tables or fixed-width regions
  18695. (org-set-local
  18696. 'auto-fill-inhibit-regexp
  18697. (concat org-outline-regexp
  18698. "\\|#\\+"
  18699. "\\|[ \t]*" org-keyword-time-regexp
  18700. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18701. (concat
  18702. "\\|[ \t]*["
  18703. (if org-enable-table-editor "|" "")
  18704. (if org-enable-fixed-width-editor ":" "")
  18705. "]"))))
  18706. ;; We use our own fill-paragraph function, to make sure that tables
  18707. ;; and fixed-width regions are not wrapped. That function will pass
  18708. ;; through to `fill-paragraph' when appropriate.
  18709. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18710. ;; Prevent auto-fill from inserting unwanted new items.
  18711. (if (boundp 'fill-nobreak-predicate)
  18712. (org-set-local
  18713. 'fill-nobreak-predicate
  18714. (org-uniquify
  18715. (append fill-nobreak-predicate
  18716. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18717. ;; Adaptive filling: To get full control, first make sure that
  18718. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18719. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18720. (org-set-local 'org-adaptive-fill-regexp-backup
  18721. adaptive-fill-regexp))
  18722. (org-set-local 'adaptive-fill-regexp "\000")
  18723. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18724. (org-set-local 'adaptive-fill-function
  18725. 'org-adaptive-fill-function)
  18726. (org-set-local
  18727. 'align-mode-rules-list
  18728. '((org-in-buffer-settings
  18729. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18730. (modes . '(org-mode))))))
  18731. (defun org-fill-item-nobreak-p ()
  18732. "Non-nil when a line break at point would insert a new item."
  18733. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18734. (defun org-fill-line-break-nobreak-p ()
  18735. "Non-nil when a line break at point would create an Org line break."
  18736. (save-excursion
  18737. (skip-chars-backward "[ \t]")
  18738. (skip-chars-backward "\\\\")
  18739. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18740. (defun org-fill-paragraph (&optional justify)
  18741. "Re-align a table, pass through to fill-paragraph if no table."
  18742. (let ((table-p (org-at-table-p))
  18743. (table.el-p (org-at-table.el-p))
  18744. (itemp (org-in-item-p)))
  18745. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18746. (save-excursion (goto-char (point-at-bol))
  18747. (looking-at org-outline-regexp)))
  18748. t) ; skip headlines
  18749. (table.el-p t) ; skip table.el tables
  18750. (table-p (org-table-align) t) ; align Org tables
  18751. (itemp ; align text in items
  18752. (let* ((struct (save-excursion (goto-char itemp)
  18753. (org-list-struct)))
  18754. (parents (org-list-parents-alist struct))
  18755. (children (org-list-get-children itemp struct parents))
  18756. beg end prev next prefix)
  18757. ;; Determine in which part of item point is: before
  18758. ;; first child, after last child, between two
  18759. ;; sub-lists, or simply in item if there's no child.
  18760. (cond
  18761. ((not children)
  18762. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18763. beg itemp
  18764. end (org-list-get-item-end itemp struct)))
  18765. ((< (point) (setq next (car children)))
  18766. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18767. beg itemp
  18768. end next))
  18769. ((> (point) (setq prev (car (last children))))
  18770. (setq beg (org-list-get-item-end prev struct)
  18771. end (org-list-get-item-end itemp struct)
  18772. prefix (save-excursion
  18773. (goto-char beg)
  18774. (skip-chars-forward " \t")
  18775. (make-string (current-column) ?\ ))))
  18776. (t (catch 'exit
  18777. (while (setq next (pop children))
  18778. (if (> (point) next)
  18779. (setq prev next)
  18780. (setq beg (org-list-get-item-end prev struct)
  18781. end next
  18782. prefix (save-excursion
  18783. (goto-char beg)
  18784. (skip-chars-forward " \t")
  18785. (make-string (current-column) ?\ )))
  18786. (throw 'exit nil))))))
  18787. ;; Use `fill-paragraph' with buffer narrowed to item
  18788. ;; without any child, and with our computed PREFIX.
  18789. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18790. (save-restriction
  18791. (narrow-to-region beg end)
  18792. (save-excursion (fill-paragraph justify)))) t))
  18793. ;; Special case where point is not in a list but is on
  18794. ;; a paragraph adjacent to a list: make sure this paragraph
  18795. ;; doesn't get merged with the end of the list by narrowing
  18796. ;; buffer first.
  18797. ((save-excursion (forward-paragraph -1)
  18798. (setq itemp (org-in-item-p)))
  18799. (let ((struct (save-excursion (goto-char itemp)
  18800. (org-list-struct))))
  18801. (save-restriction
  18802. (narrow-to-region (org-list-get-bottom-point struct)
  18803. (save-excursion (forward-paragraph 1)
  18804. (point)))
  18805. (fill-paragraph justify) t)))
  18806. ;; Else simply call `fill-paragraph'.
  18807. (t nil))))
  18808. ;; For reference, this is the default value of adaptive-fill-regexp
  18809. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18810. (defun org-adaptive-fill-function ()
  18811. "Return a fill prefix for org-mode files."
  18812. (let (itemp)
  18813. (save-excursion
  18814. (cond
  18815. ;; Comment line
  18816. ((looking-at "#[ \t]+")
  18817. (match-string-no-properties 0))
  18818. ;; Plain list item
  18819. ((org-at-item-p)
  18820. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18821. ;; Point is in a list after `backward-paragraph': original
  18822. ;; point wasn't in the list, or filling would have been taken
  18823. ;; care of by `org-auto-fill-function', but the list and the
  18824. ;; real paragraph are not separated by a blank line. Thus, move
  18825. ;; point after the list to go back to real paragraph and
  18826. ;; determine fill-prefix.
  18827. ((setq itemp (org-in-item-p))
  18828. (goto-char itemp)
  18829. (let* ((struct (org-list-struct))
  18830. (bottom (org-list-get-bottom-point struct)))
  18831. (goto-char bottom)
  18832. (make-string (org-get-indentation) ?\ )))
  18833. ;; Other text
  18834. ((looking-at org-adaptive-fill-regexp-backup)
  18835. (match-string-no-properties 0))))))
  18836. (defun org-auto-fill-function ()
  18837. "Auto-fill function."
  18838. (let (itemp prefix)
  18839. ;; When in a list, compute an appropriate fill-prefix and make
  18840. ;; sure it will be used by `do-auto-fill'.
  18841. (if (setq itemp (org-in-item-p))
  18842. (progn
  18843. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18844. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18845. (do-auto-fill)))
  18846. ;; Else just use `do-auto-fill'.
  18847. (do-auto-fill))))
  18848. ;;; Other stuff.
  18849. (defun org-toggle-fixed-width-section (arg)
  18850. "Toggle the fixed-width export.
  18851. If there is no active region, the QUOTE keyword at the current headline is
  18852. inserted or removed. When present, it causes the text between this headline
  18853. and the next to be exported as fixed-width text, and unmodified.
  18854. If there is an active region, this command adds or removes a colon as the
  18855. first character of this line. If the first character of a line is a colon,
  18856. this line is also exported in fixed-width font."
  18857. (interactive "P")
  18858. (let* ((cc 0)
  18859. (regionp (org-region-active-p))
  18860. (beg (if regionp (region-beginning) (point)))
  18861. (end (if regionp (region-end)))
  18862. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18863. (case-fold-search nil)
  18864. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18865. off)
  18866. (if regionp
  18867. (save-excursion
  18868. (goto-char beg)
  18869. (setq cc (current-column))
  18870. (beginning-of-line 1)
  18871. (setq off (looking-at re))
  18872. (while (> nlines 0)
  18873. (setq nlines (1- nlines))
  18874. (beginning-of-line 1)
  18875. (cond
  18876. (arg
  18877. (org-move-to-column cc t)
  18878. (insert ": \n")
  18879. (forward-line -1))
  18880. ((and off (looking-at re))
  18881. (replace-match "" t t nil 1))
  18882. ((not off) (org-move-to-column cc t) (insert ": ")))
  18883. (forward-line 1)))
  18884. (save-excursion
  18885. (org-back-to-heading)
  18886. (cond
  18887. ((looking-at (format org-heading-keyword-regexp-format
  18888. org-quote-string))
  18889. (goto-char (match-end 1))
  18890. (looking-at (concat " +" org-quote-string))
  18891. (replace-match "" t t)
  18892. (when (eolp) (insert " ")))
  18893. ((looking-at org-outline-regexp)
  18894. (goto-char (match-end 0))
  18895. (insert org-quote-string " ")))))))
  18896. (defun org-reftex-citation ()
  18897. "Use reftex-citation to insert a citation into the buffer.
  18898. This looks for a line like
  18899. #+BIBLIOGRAPHY: foo plain option:-d
  18900. and derives from it that foo.bib is the bibliography file relevant
  18901. for this document. It then installs the necessary environment for RefTeX
  18902. to work in this buffer and calls `reftex-citation' to insert a citation
  18903. into the buffer.
  18904. Export of such citations to both LaTeX and HTML is handled by the contributed
  18905. package org-exp-bibtex by Taru Karttunen."
  18906. (interactive)
  18907. (let ((reftex-docstruct-symbol 'rds)
  18908. (reftex-cite-format "\\cite{%l}")
  18909. rds bib)
  18910. (save-excursion
  18911. (save-restriction
  18912. (widen)
  18913. (let ((case-fold-search t)
  18914. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18915. (if (not (save-excursion
  18916. (or (re-search-forward re nil t)
  18917. (re-search-backward re nil t))))
  18918. (error "No bibliography defined in file")
  18919. (setq bib (concat (match-string 1) ".bib")
  18920. rds (list (list 'bib bib)))))))
  18921. (call-interactively 'reftex-citation)))
  18922. ;;;; Functions extending outline functionality
  18923. (defun org-beginning-of-line (&optional arg)
  18924. "Go to the beginning of the current line. If that is invisible, continue
  18925. to a visible line beginning. This makes the function of C-a more intuitive.
  18926. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18927. first attempt, and only move to after the tags when the cursor is already
  18928. beyond the end of the headline."
  18929. (interactive "P")
  18930. (let ((pos (point))
  18931. (special (if (consp org-special-ctrl-a/e)
  18932. (car org-special-ctrl-a/e)
  18933. org-special-ctrl-a/e))
  18934. refpos)
  18935. (if (org-bound-and-true-p line-move-visual)
  18936. (beginning-of-visual-line 1)
  18937. (beginning-of-line 1))
  18938. (if (and arg (fboundp 'move-beginning-of-line))
  18939. (call-interactively 'move-beginning-of-line)
  18940. (if (bobp)
  18941. nil
  18942. (backward-char 1)
  18943. (if (org-truely-invisible-p)
  18944. (while (and (not (bobp)) (org-truely-invisible-p))
  18945. (backward-char 1)
  18946. (beginning-of-line 1))
  18947. (forward-char 1))))
  18948. (when special
  18949. (cond
  18950. ((and (looking-at org-complex-heading-regexp)
  18951. (= (char-after (match-end 1)) ?\ ))
  18952. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18953. (point-at-eol)))
  18954. (goto-char
  18955. (if (eq special t)
  18956. (cond ((> pos refpos) refpos)
  18957. ((= pos (point)) refpos)
  18958. (t (point)))
  18959. (cond ((> pos (point)) (point))
  18960. ((not (eq last-command this-command)) (point))
  18961. (t refpos)))))
  18962. ((org-at-item-p)
  18963. ;; Being at an item and not looking at an the item means point
  18964. ;; was previously moved to beginning of a visual line, whiche
  18965. ;; doesn't contain the item. Therefore, do nothing special,
  18966. ;; just stay here.
  18967. (when (looking-at org-list-full-item-re)
  18968. ;; Set special position at first white space character after
  18969. ;; bullet, and check-box, if any.
  18970. (let ((after-bullet
  18971. (let ((box (match-end 3)))
  18972. (if (not box) (match-end 1)
  18973. (let ((after (char-after box)))
  18974. (if (and after (= after ? )) (1+ box) box))))))
  18975. ;; Special case: Move point to special position when
  18976. ;; currently after it or at beginning of line.
  18977. (if (eq special t)
  18978. (when (or (> pos after-bullet) (= (point) pos))
  18979. (goto-char after-bullet))
  18980. ;; Reversed case: Move point to special position when
  18981. ;; point was already at beginning of line and command is
  18982. ;; repeated.
  18983. (when (and (= (point) pos) (eq last-command this-command))
  18984. (goto-char after-bullet))))))))
  18985. (org-no-warnings
  18986. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18987. (defun org-end-of-line (&optional arg)
  18988. "Go to the end of the line.
  18989. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18990. first attempt, and only move to after the tags when the cursor is already
  18991. beyond the end of the headline."
  18992. (interactive "P")
  18993. (let ((special (if (consp org-special-ctrl-a/e)
  18994. (cdr org-special-ctrl-a/e)
  18995. org-special-ctrl-a/e)))
  18996. (cond
  18997. ((or (not special) arg
  18998. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18999. (call-interactively
  19000. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19001. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19002. (t 'end-of-line))))
  19003. ((org-at-heading-p)
  19004. (let ((pos (point)))
  19005. (beginning-of-line 1)
  19006. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19007. (if (eq special t)
  19008. (if (or (< pos (match-beginning 1))
  19009. (= pos (match-end 0)))
  19010. (goto-char (match-beginning 1))
  19011. (goto-char (match-end 0)))
  19012. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19013. (goto-char (match-end 0))
  19014. (goto-char (match-beginning 1))))
  19015. (call-interactively (if (fboundp 'move-end-of-line)
  19016. 'move-end-of-line
  19017. 'end-of-line)))))
  19018. ((org-at-drawer-p)
  19019. (move-end-of-line 1)
  19020. (when (overlays-at (1- (point))) (backward-char 1)))
  19021. ;; At an item: Move before any hidden text.
  19022. (t (call-interactively
  19023. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19024. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19025. (t 'end-of-line)))))
  19026. (org-no-warnings
  19027. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19028. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19029. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19030. (defun org-backward-sentence (&optional arg)
  19031. "Go to beginning of sentence, or beginning of table field.
  19032. This will call `backward-sentence' or `org-table-beginning-of-field',
  19033. depending on context."
  19034. (interactive "P")
  19035. (cond
  19036. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19037. (t (call-interactively 'backward-sentence))))
  19038. (defun org-forward-sentence (&optional arg)
  19039. "Go to end of sentence, or end of table field.
  19040. This will call `forward-sentence' or `org-table-end-of-field',
  19041. depending on context."
  19042. (interactive "P")
  19043. (cond
  19044. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19045. (t (call-interactively 'forward-sentence))))
  19046. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19047. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19048. (defun org-kill-line (&optional arg)
  19049. "Kill line, to tags or end of line."
  19050. (interactive "P")
  19051. (cond
  19052. ((or (not org-special-ctrl-k)
  19053. (bolp)
  19054. (not (org-at-heading-p)))
  19055. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19056. org-ctrl-k-protect-subtree)
  19057. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19058. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19059. (error "C-k aborted - would kill hidden subtree")))
  19060. (call-interactively
  19061. (if visual-line-mode 'kill-visual-line 'kill-line)))
  19062. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19063. (kill-region (point) (match-beginning 1))
  19064. (org-set-tags nil t))
  19065. (t (kill-region (point) (point-at-eol)))))
  19066. (define-key org-mode-map "\C-k" 'org-kill-line)
  19067. (defun org-yank (&optional arg)
  19068. "Yank. If the kill is a subtree, treat it specially.
  19069. This command will look at the current kill and check if is a single
  19070. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19071. cursor is at the beginning of a line or after the stars of a currently
  19072. empty headline, then the yank is handled specially. How exactly depends
  19073. on the value of the following variables, both set by default.
  19074. org-yank-folded-subtrees
  19075. When set, the subtree(s) will be folded after insertion, but only
  19076. if doing so would now swallow text after the yanked text.
  19077. org-yank-adjusted-subtrees
  19078. When set, the subtree will be promoted or demoted in order to
  19079. fit into the local outline tree structure, which means that the level
  19080. will be adjusted so that it becomes the smaller one of the two
  19081. *visible* surrounding headings.
  19082. Any prefix to this command will cause `yank' to be called directly with
  19083. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19084. will just
  19085. plainly yank the text as it is.
  19086. \[1] The test checks if the first non-white line is a heading
  19087. and if there are no other headings with fewer stars."
  19088. (interactive "P")
  19089. (org-yank-generic 'yank arg))
  19090. (defun org-yank-generic (command arg)
  19091. "Perform some yank-like command.
  19092. This function implements the behavior described in the `org-yank'
  19093. documentation. However, it has been generalized to work for any
  19094. interactive command with similar behavior."
  19095. ;; pretend to be command COMMAND
  19096. (setq this-command command)
  19097. (if arg
  19098. (call-interactively command)
  19099. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19100. (and (org-kill-is-subtree-p)
  19101. (or (bolp)
  19102. (and (looking-at "[ \t]*$")
  19103. (string-match
  19104. "\\`\\*+\\'"
  19105. (buffer-substring (point-at-bol) (point)))))))
  19106. swallowp)
  19107. (cond
  19108. ((and subtreep org-yank-folded-subtrees)
  19109. (let ((beg (point))
  19110. end)
  19111. (if (and subtreep org-yank-adjusted-subtrees)
  19112. (org-paste-subtree nil nil 'for-yank)
  19113. (call-interactively command))
  19114. (setq end (point))
  19115. (goto-char beg)
  19116. (when (and (bolp) subtreep
  19117. (not (setq swallowp
  19118. (org-yank-folding-would-swallow-text beg end))))
  19119. (org-with-limited-levels
  19120. (or (looking-at org-outline-regexp)
  19121. (re-search-forward org-outline-regexp-bol end t))
  19122. (while (and (< (point) end) (looking-at org-outline-regexp))
  19123. (hide-subtree)
  19124. (org-cycle-show-empty-lines 'folded)
  19125. (condition-case nil
  19126. (outline-forward-same-level 1)
  19127. (error (goto-char end))))))
  19128. (when swallowp
  19129. (message
  19130. "Inserted text not folded because that would swallow text"))
  19131. (goto-char end)
  19132. (skip-chars-forward " \t\n\r")
  19133. (beginning-of-line 1)
  19134. (push-mark beg 'nomsg)))
  19135. ((and subtreep org-yank-adjusted-subtrees)
  19136. (let ((beg (point-at-bol)))
  19137. (org-paste-subtree nil nil 'for-yank)
  19138. (push-mark beg 'nomsg)))
  19139. (t
  19140. (call-interactively command))))))
  19141. (defun org-yank-folding-would-swallow-text (beg end)
  19142. "Would hide-subtree at BEG swallow any text after END?"
  19143. (let (level)
  19144. (org-with-limited-levels
  19145. (save-excursion
  19146. (goto-char beg)
  19147. (when (or (looking-at org-outline-regexp)
  19148. (re-search-forward org-outline-regexp-bol end t))
  19149. (setq level (org-outline-level)))
  19150. (goto-char end)
  19151. (skip-chars-forward " \t\r\n\v\f")
  19152. (if (or (eobp)
  19153. (and (bolp) (looking-at org-outline-regexp)
  19154. (<= (org-outline-level) level)))
  19155. nil ; Nothing would be swallowed
  19156. t))))) ; something would swallow
  19157. (define-key org-mode-map "\C-y" 'org-yank)
  19158. (defun org-truely-invisible-p ()
  19159. "Check if point is at a character currently not visible.
  19160. This version does not only check the character property, but also
  19161. `visible-mode'."
  19162. ;; Early versions of noutline don't have `outline-invisible-p'.
  19163. (if (org-bound-and-true-p visible-mode)
  19164. nil
  19165. (outline-invisible-p)))
  19166. (defun org-invisible-p2 ()
  19167. "Check if point is at a character currently not visible."
  19168. (save-excursion
  19169. (if (and (eolp) (not (bobp))) (backward-char 1))
  19170. ;; Early versions of noutline don't have `outline-invisible-p'.
  19171. (outline-invisible-p)))
  19172. (defun org-back-to-heading (&optional invisible-ok)
  19173. "Call `outline-back-to-heading', but provide a better error message."
  19174. (condition-case nil
  19175. (outline-back-to-heading invisible-ok)
  19176. (error (error "Before first headline at position %d in buffer %s"
  19177. (point) (current-buffer)))))
  19178. (defun org-beginning-of-defun ()
  19179. "Go to the beginning of the subtree, i.e. back to the heading."
  19180. (org-back-to-heading))
  19181. (defun org-end-of-defun ()
  19182. "Go to the end of the subtree."
  19183. (org-end-of-subtree nil t))
  19184. (defun org-before-first-heading-p ()
  19185. "Before first heading?"
  19186. (save-excursion
  19187. (end-of-line)
  19188. (null (re-search-backward org-outline-regexp-bol nil t))))
  19189. (defun org-at-heading-p (&optional ignored)
  19190. (outline-on-heading-p t))
  19191. ;; Compatibility alias with Org versions < 7.8.03
  19192. (defalias 'org-on-heading-p 'org-at-heading-p)
  19193. (defun org-at-drawer-p nil
  19194. "Is cursor at a drawer keyword?"
  19195. (save-excursion
  19196. (move-beginning-of-line 1)
  19197. (looking-at org-drawer-regexp)))
  19198. (defun org-at-block-p nil
  19199. "Is cursor at a block keyword?"
  19200. (save-excursion
  19201. (move-beginning-of-line 1)
  19202. (looking-at org-block-regexp)))
  19203. (defun org-point-at-end-of-empty-headline ()
  19204. "If point is at the end of an empty headline, return t, else nil.
  19205. If the heading only contains a TODO keyword, it is still still considered
  19206. empty."
  19207. (and (looking-at "[ \t]*$")
  19208. (save-excursion
  19209. (beginning-of-line 1)
  19210. (let ((case-fold-search nil))
  19211. (looking-at org-todo-line-regexp)))
  19212. (string= (match-string 3) "")))
  19213. (defun org-at-heading-or-item-p ()
  19214. (or (org-at-heading-p) (org-at-item-p)))
  19215. (defun org-at-target-p ()
  19216. (or (org-in-regexp org-radio-target-regexp)
  19217. (org-in-regexp org-target-regexp)))
  19218. ;; Compatibility alias with Org versions < 7.8.03
  19219. (defalias 'org-on-target-p 'org-at-target-p)
  19220. (defun org-up-heading-all (arg)
  19221. "Move to the heading line of which the present line is a subheading.
  19222. This function considers both visible and invisible heading lines.
  19223. With argument, move up ARG levels."
  19224. (if (fboundp 'outline-up-heading-all)
  19225. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19226. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19227. (defun org-up-heading-safe ()
  19228. "Move to the heading line of which the present line is a subheading.
  19229. This version will not throw an error. It will return the level of the
  19230. headline found, or nil if no higher level is found.
  19231. Also, this function will be a lot faster than `outline-up-heading',
  19232. because it relies on stars being the outline starters. This can really
  19233. make a significant difference in outlines with very many siblings."
  19234. (let (start-level re)
  19235. (org-back-to-heading t)
  19236. (setq start-level (funcall outline-level))
  19237. (if (equal start-level 1)
  19238. nil
  19239. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19240. (if (re-search-backward re nil t)
  19241. (funcall outline-level)))))
  19242. (defun org-first-sibling-p ()
  19243. "Is this heading the first child of its parents?"
  19244. (interactive)
  19245. (let ((re org-outline-regexp-bol)
  19246. level l)
  19247. (unless (org-at-heading-p t)
  19248. (error "Not at a heading"))
  19249. (setq level (funcall outline-level))
  19250. (save-excursion
  19251. (if (not (re-search-backward re nil t))
  19252. t
  19253. (setq l (funcall outline-level))
  19254. (< l level)))))
  19255. (defun org-goto-sibling (&optional previous)
  19256. "Goto the next sibling, even if it is invisible.
  19257. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19258. when a sibling was found. When none is found, return nil and don't
  19259. move point."
  19260. (let ((fun (if previous 're-search-backward 're-search-forward))
  19261. (pos (point))
  19262. (re org-outline-regexp-bol)
  19263. level l)
  19264. (when (condition-case nil (org-back-to-heading t) (error nil))
  19265. (setq level (funcall outline-level))
  19266. (catch 'exit
  19267. (or previous (forward-char 1))
  19268. (while (funcall fun re nil t)
  19269. (setq l (funcall outline-level))
  19270. (when (< l level) (goto-char pos) (throw 'exit nil))
  19271. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19272. (goto-char pos)
  19273. nil))))
  19274. (defun org-show-siblings ()
  19275. "Show all siblings of the current headline."
  19276. (save-excursion
  19277. (while (org-goto-sibling) (org-flag-heading nil)))
  19278. (save-excursion
  19279. (while (org-goto-sibling 'previous)
  19280. (org-flag-heading nil))))
  19281. (defun org-goto-first-child ()
  19282. "Goto the first child, even if it is invisible.
  19283. Return t when a child was found. Otherwise don't move point and
  19284. return nil."
  19285. (let (level (pos (point)) (re org-outline-regexp-bol))
  19286. (when (condition-case nil (org-back-to-heading t) (error nil))
  19287. (setq level (outline-level))
  19288. (forward-char 1)
  19289. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19290. (progn (goto-char (match-beginning 0)) t)
  19291. (goto-char pos) nil))))
  19292. (defun org-show-hidden-entry ()
  19293. "Show an entry where even the heading is hidden."
  19294. (save-excursion
  19295. (org-show-entry)))
  19296. (defun org-flag-heading (flag &optional entry)
  19297. "Flag the current heading. FLAG non-nil means make invisible.
  19298. When ENTRY is non-nil, show the entire entry."
  19299. (save-excursion
  19300. (org-back-to-heading t)
  19301. ;; Check if we should show the entire entry
  19302. (if entry
  19303. (progn
  19304. (org-show-entry)
  19305. (save-excursion
  19306. (and (outline-next-heading)
  19307. (org-flag-heading nil))))
  19308. (outline-flag-region (max (point-min) (1- (point)))
  19309. (save-excursion (outline-end-of-heading) (point))
  19310. flag))))
  19311. (defun org-get-next-sibling ()
  19312. "Move to next heading of the same level, and return point.
  19313. If there is no such heading, return nil.
  19314. This is like outline-next-sibling, but invisible headings are ok."
  19315. (let ((level (funcall outline-level)))
  19316. (outline-next-heading)
  19317. (while (and (not (eobp)) (> (funcall outline-level) level))
  19318. (outline-next-heading))
  19319. (if (or (eobp) (< (funcall outline-level) level))
  19320. nil
  19321. (point))))
  19322. (defun org-get-last-sibling ()
  19323. "Move to previous heading of the same level, and return point.
  19324. If there is no such heading, return nil."
  19325. (let ((opoint (point))
  19326. (level (funcall outline-level)))
  19327. (outline-previous-heading)
  19328. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19329. (while (and (> (funcall outline-level) level)
  19330. (not (bobp)))
  19331. (outline-previous-heading))
  19332. (if (< (funcall outline-level) level)
  19333. nil
  19334. (point)))))
  19335. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19336. ;; This contains an exact copy of the original function, but it uses
  19337. ;; `org-back-to-heading', to make it work also in invisible
  19338. ;; trees. And is uses an invisible-OK argument.
  19339. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19340. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19341. ;; with many children and grandchildren etc, this can be much faster
  19342. ;; than the outline version.
  19343. (org-back-to-heading invisible-OK)
  19344. (let ((first t)
  19345. (level (funcall outline-level)))
  19346. (if (and (eq major-mode 'org-mode) (< level 1000))
  19347. ;; A true heading (not a plain list item), in Org-mode
  19348. ;; This means we can easily find the end by looking
  19349. ;; only for the right number of stars. Using a regexp to do
  19350. ;; this is so much faster than using a Lisp loop.
  19351. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19352. (forward-char 1)
  19353. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19354. ;; something else, do it the slow way
  19355. (while (and (not (eobp))
  19356. (or first (> (funcall outline-level) level)))
  19357. (setq first nil)
  19358. (outline-next-heading)))
  19359. (unless to-heading
  19360. (if (memq (preceding-char) '(?\n ?\^M))
  19361. (progn
  19362. ;; Go to end of line before heading
  19363. (forward-char -1)
  19364. (if (memq (preceding-char) '(?\n ?\^M))
  19365. ;; leave blank line before heading
  19366. (forward-char -1))))))
  19367. (point))
  19368. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19369. "Use Org version in org-mode, for dramatic speed-up."
  19370. (if (eq major-mode 'org-mode)
  19371. (progn
  19372. (org-end-of-subtree nil t)
  19373. (unless (eobp) (backward-char 1)))
  19374. ad-do-it))
  19375. (defun org-end-of-meta-data-and-drawers ()
  19376. "Jump to the first text after meta data and drawers in the current entry.
  19377. This will move over empty lines, lines with planning time stamps,
  19378. clocking lines, and drawers."
  19379. (org-back-to-heading t)
  19380. (let ((end (save-excursion (outline-next-heading) (point)))
  19381. (re (concat "\\(" org-drawer-regexp "\\)"
  19382. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19383. (forward-line 1)
  19384. (while (re-search-forward re end t)
  19385. (if (not (match-end 1))
  19386. ;; empty or planning line
  19387. (forward-line 1)
  19388. ;; a drawer, find the end
  19389. (re-search-forward "^[ \t]*:END:" end 'move)
  19390. (forward-line 1)))
  19391. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19392. (point)))
  19393. (defun org-forward-same-level (arg &optional invisible-ok)
  19394. "Move forward to the arg'th subheading at same level as this one.
  19395. Stop at the first and last subheadings of a superior heading.
  19396. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19397. it wil also look at invisible ones."
  19398. (interactive "p")
  19399. (org-back-to-heading invisible-ok)
  19400. (org-at-heading-p)
  19401. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19402. (re (format "^\\*\\{1,%d\\} " level))
  19403. l)
  19404. (forward-char 1)
  19405. (while (> arg 0)
  19406. (while (and (re-search-forward re nil 'move)
  19407. (setq l (- (match-end 0) (match-beginning 0) 1))
  19408. (= l level)
  19409. (not invisible-ok)
  19410. (progn (backward-char 1) (outline-invisible-p)))
  19411. (if (< l level) (setq arg 1)))
  19412. (setq arg (1- arg)))
  19413. (beginning-of-line 1)))
  19414. (defun org-backward-same-level (arg &optional invisible-ok)
  19415. "Move backward to the arg'th subheading at same level as this one.
  19416. Stop at the first and last subheadings of a superior heading."
  19417. (interactive "p")
  19418. (org-back-to-heading)
  19419. (org-at-heading-p)
  19420. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19421. (re (format "^\\*\\{1,%d\\} " level))
  19422. l)
  19423. (while (> arg 0)
  19424. (while (and (re-search-backward re nil 'move)
  19425. (setq l (- (match-end 0) (match-beginning 0) 1))
  19426. (= l level)
  19427. (not invisible-ok)
  19428. (outline-invisible-p))
  19429. (if (< l level) (setq arg 1)))
  19430. (setq arg (1- arg)))))
  19431. (defun org-show-subtree ()
  19432. "Show everything after this heading at deeper levels."
  19433. (outline-flag-region
  19434. (point)
  19435. (save-excursion
  19436. (org-end-of-subtree t t))
  19437. nil))
  19438. (defun org-show-entry ()
  19439. "Show the body directly following this heading.
  19440. Show the heading too, if it is currently invisible."
  19441. (interactive)
  19442. (save-excursion
  19443. (condition-case nil
  19444. (progn
  19445. (org-back-to-heading t)
  19446. (outline-flag-region
  19447. (max (point-min) (1- (point)))
  19448. (save-excursion
  19449. (if (re-search-forward
  19450. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19451. (match-beginning 1)
  19452. (point-max)))
  19453. nil)
  19454. (org-cycle-hide-drawers 'children))
  19455. (error nil))))
  19456. (defun org-make-options-regexp (kwds &optional extra)
  19457. "Make a regular expression for keyword lines."
  19458. (concat
  19459. "^"
  19460. "#?[ \t]*\\+\\("
  19461. (mapconcat 'regexp-quote kwds "\\|")
  19462. (if extra (concat "\\|" extra))
  19463. "\\):[ \t]*"
  19464. "\\(.*\\)"))
  19465. ;; Make isearch reveal the necessary context
  19466. (defun org-isearch-end ()
  19467. "Reveal context after isearch exits."
  19468. (when isearch-success ; only if search was successful
  19469. (if (featurep 'xemacs)
  19470. ;; Under XEmacs, the hook is run in the correct place,
  19471. ;; we directly show the context.
  19472. (org-show-context 'isearch)
  19473. ;; In Emacs the hook runs *before* restoring the overlays.
  19474. ;; So we have to use a one-time post-command-hook to do this.
  19475. ;; (Emacs 22 has a special variable, see function `org-mode')
  19476. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19477. isearch-mode-end-hook-quit)
  19478. ;; Only when the isearch was not quitted.
  19479. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19480. 'append 'local)))))
  19481. (defun org-isearch-post-command ()
  19482. "Remove self from hook, and show context."
  19483. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19484. (org-show-context 'isearch))
  19485. ;;;; Integration with and fixes for other packages
  19486. ;;; Imenu support
  19487. (defvar org-imenu-markers nil
  19488. "All markers currently used by Imenu.")
  19489. (make-variable-buffer-local 'org-imenu-markers)
  19490. (defun org-imenu-new-marker (&optional pos)
  19491. "Return a new marker for use by Imenu, and remember the marker."
  19492. (let ((m (make-marker)))
  19493. (move-marker m (or pos (point)))
  19494. (push m org-imenu-markers)
  19495. m))
  19496. (defun org-imenu-get-tree ()
  19497. "Produce the index for Imenu."
  19498. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19499. (setq org-imenu-markers nil)
  19500. (let* ((n org-imenu-depth)
  19501. (re (concat "^" (org-get-limited-outline-regexp)))
  19502. (subs (make-vector (1+ n) nil))
  19503. (last-level 0)
  19504. m level head)
  19505. (save-excursion
  19506. (save-restriction
  19507. (widen)
  19508. (goto-char (point-max))
  19509. (while (re-search-backward re nil t)
  19510. (setq level (org-reduced-level (funcall outline-level)))
  19511. (when (<= level n)
  19512. (looking-at org-complex-heading-regexp)
  19513. (setq head (org-link-display-format
  19514. (org-match-string-no-properties 4))
  19515. m (org-imenu-new-marker))
  19516. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19517. (if (>= level last-level)
  19518. (push (cons head m) (aref subs level))
  19519. (push (cons head (aref subs (1+ level))) (aref subs level))
  19520. (loop for i from (1+ level) to n do (aset subs i nil)))
  19521. (setq last-level level)))))
  19522. (aref subs 1)))
  19523. (eval-after-load "imenu"
  19524. '(progn
  19525. (add-hook 'imenu-after-jump-hook
  19526. (lambda ()
  19527. (if (eq major-mode 'org-mode)
  19528. (org-show-context 'org-goto))))))
  19529. (defun org-link-display-format (link)
  19530. "Replace a link with either the description, or the link target
  19531. if no description is present"
  19532. (save-match-data
  19533. (if (string-match org-bracket-link-analytic-regexp link)
  19534. (replace-match (if (match-end 5)
  19535. (match-string 5 link)
  19536. (concat (match-string 1 link)
  19537. (match-string 3 link)))
  19538. nil t link)
  19539. link)))
  19540. (defun org-toggle-link-display ()
  19541. "Toggle the literal or descriptive display of links."
  19542. (interactive)
  19543. (if org-descriptive-links
  19544. (progn (org-remove-from-invisibility-spec '(org-link))
  19545. (org-restart-font-lock)
  19546. (setq org-descriptive-links nil))
  19547. (progn (add-to-invisibility-spec '(org-link))
  19548. (org-restart-font-lock)
  19549. (setq org-descriptive-links t))))
  19550. ;; Speedbar support
  19551. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19552. "Overlay marking the agenda restriction line in speedbar.")
  19553. (overlay-put org-speedbar-restriction-lock-overlay
  19554. 'face 'org-agenda-restriction-lock)
  19555. (overlay-put org-speedbar-restriction-lock-overlay
  19556. 'help-echo "Agendas are currently limited to this item.")
  19557. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19558. (defun org-speedbar-set-agenda-restriction ()
  19559. "Restrict future agenda commands to the location at point in speedbar.
  19560. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19561. (interactive)
  19562. (require 'org-agenda)
  19563. (let (p m tp np dir txt)
  19564. (cond
  19565. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19566. 'org-imenu t))
  19567. (setq m (get-text-property p 'org-imenu-marker))
  19568. (with-current-buffer (marker-buffer m)
  19569. (goto-char m)
  19570. (org-agenda-set-restriction-lock 'subtree)))
  19571. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19572. 'speedbar-function 'speedbar-find-file))
  19573. (setq tp (previous-single-property-change
  19574. (1+ p) 'speedbar-function)
  19575. np (next-single-property-change
  19576. tp 'speedbar-function)
  19577. dir (speedbar-line-directory)
  19578. txt (buffer-substring-no-properties (or tp (point-min))
  19579. (or np (point-max))))
  19580. (with-current-buffer (find-file-noselect
  19581. (let ((default-directory dir))
  19582. (expand-file-name txt)))
  19583. (unless (eq major-mode 'org-mode)
  19584. (error "Cannot restrict to non-Org-mode file"))
  19585. (org-agenda-set-restriction-lock 'file)))
  19586. (t (error "Don't know how to restrict Org-mode's agenda")))
  19587. (move-overlay org-speedbar-restriction-lock-overlay
  19588. (point-at-bol) (point-at-eol))
  19589. (setq current-prefix-arg nil)
  19590. (org-agenda-maybe-redo)))
  19591. (eval-after-load "speedbar"
  19592. '(progn
  19593. (speedbar-add-supported-extension ".org")
  19594. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19595. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19596. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19597. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19598. (add-hook 'speedbar-visiting-tag-hook
  19599. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19600. ;;; Fixes and Hacks for problems with other packages
  19601. ;; Make flyspell not check words in links, to not mess up our keymap
  19602. (defun org-mode-flyspell-verify ()
  19603. "Don't let flyspell put overlays at active buttons, or on
  19604. {todo,all-time,additional-option-like}-keywords."
  19605. (let ((pos (max (1- (point)) (point-min)))
  19606. (word (thing-at-point 'word)))
  19607. (and (not (get-text-property pos 'keymap))
  19608. (not (get-text-property pos 'org-no-flyspell))
  19609. (not (member word org-todo-keywords-1))
  19610. (not (member word org-all-time-keywords))
  19611. (not (member word org-additional-option-like-keywords)))))
  19612. (defun org-remove-flyspell-overlays-in (beg end)
  19613. "Remove flyspell overlays in region."
  19614. (and (org-bound-and-true-p flyspell-mode)
  19615. (fboundp 'flyspell-delete-region-overlays)
  19616. (flyspell-delete-region-overlays beg end))
  19617. (add-text-properties beg end '(org-no-flyspell t)))
  19618. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19619. (eval-after-load "bookmark"
  19620. '(if (boundp 'bookmark-after-jump-hook)
  19621. ;; We can use the hook
  19622. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19623. ;; Hook not available, use advice
  19624. (defadvice bookmark-jump (after org-make-visible activate)
  19625. "Make the position visible."
  19626. (org-bookmark-jump-unhide))))
  19627. ;; Make sure saveplace shows the location if it was hidden
  19628. (eval-after-load "saveplace"
  19629. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19630. "Make the position visible."
  19631. (org-bookmark-jump-unhide)))
  19632. ;; Make sure ecb shows the location if it was hidden
  19633. (eval-after-load "ecb"
  19634. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19635. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19636. (if (eq major-mode 'org-mode)
  19637. (org-show-context))))
  19638. (defun org-bookmark-jump-unhide ()
  19639. "Unhide the current position, to show the bookmark location."
  19640. (and (eq major-mode 'org-mode)
  19641. (or (outline-invisible-p)
  19642. (save-excursion (goto-char (max (point-min) (1- (point))))
  19643. (outline-invisible-p)))
  19644. (org-show-context 'bookmark-jump)))
  19645. ;; Make session.el ignore our circular variable
  19646. (eval-after-load "session"
  19647. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19648. ;;;; Experimental code
  19649. (defun org-closed-in-range ()
  19650. "Sparse tree of items closed in a certain time range.
  19651. Still experimental, may disappear in the future."
  19652. (interactive)
  19653. ;; Get the time interval from the user.
  19654. (let* ((time1 (org-float-time
  19655. (org-read-date nil 'to-time nil "Starting date: ")))
  19656. (time2 (org-float-time
  19657. (org-read-date nil 'to-time nil "End date:")))
  19658. ;; callback function
  19659. (callback (lambda ()
  19660. (let ((time
  19661. (org-float-time
  19662. (apply 'encode-time
  19663. (org-parse-time-string
  19664. (match-string 1))))))
  19665. ;; check if time in interval
  19666. (and (>= time time1) (<= time time2))))))
  19667. ;; make tree, check each match with the callback
  19668. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19669. ;;;; Finish up
  19670. (provide 'org)
  19671. (run-hooks 'org-load-hook)
  19672. ;;; org.el ends here