org.el 816 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700
  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. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :version "24.1"
  143. :type '(alist :tag "Babel Languages"
  144. :key-type
  145. (choice
  146. (const :tag "Awk" awk)
  147. (const :tag "C" C)
  148. (const :tag "R" R)
  149. (const :tag "Asymptote" asymptote)
  150. (const :tag "Calc" calc)
  151. (const :tag "Clojure" clojure)
  152. (const :tag "CSS" css)
  153. (const :tag "Ditaa" ditaa)
  154. (const :tag "Dot" dot)
  155. (const :tag "Emacs Lisp" emacs-lisp)
  156. (const :tag "Fortran" fortran)
  157. (const :tag "Gnuplot" gnuplot)
  158. (const :tag "Haskell" haskell)
  159. (const :tag "IO" io)
  160. (const :tag "Java" java)
  161. (const :tag "Javascript" js)
  162. (const :tag "LaTeX" latex)
  163. (const :tag "Ledger" ledger)
  164. (const :tag "Lilypond" lilypond)
  165. (const :tag "Maxima" maxima)
  166. (const :tag "Matlab" matlab)
  167. (const :tag "Mscgen" mscgen)
  168. (const :tag "Ocaml" ocaml)
  169. (const :tag "Octave" octave)
  170. (const :tag "Org" org)
  171. (const :tag "Perl" perl)
  172. (const :tag "Pico Lisp" picolisp)
  173. (const :tag "PlantUML" plantuml)
  174. (const :tag "Python" python)
  175. (const :tag "Ruby" ruby)
  176. (const :tag "Sass" sass)
  177. (const :tag "Scala" scala)
  178. (const :tag "Scheme" scheme)
  179. (const :tag "Screen" screen)
  180. (const :tag "Shell Script" sh)
  181. (const :tag "Shen" shen)
  182. (const :tag "Sql" sql)
  183. (const :tag "Sqlite" sqlite))
  184. :value-type (boolean :tag "Activate" :value t)))
  185. ;;;; Customization variables
  186. (defcustom org-clone-delete-id nil
  187. "Remove ID property of clones of a subtree.
  188. When non-nil, clones of a subtree don't inherit the ID property.
  189. Otherwise they inherit the ID property with a new unique
  190. identifier."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-id)
  194. ;;; Version
  195. (defvaralias 'org-version 'org-release)
  196. ;;;###autoload
  197. (defun org-version (&optional here)
  198. "Show the org-mode version in the echo area.
  199. With prefix arg HERE, insert it at point."
  200. (interactive "P")
  201. (let* ((origin default-directory)
  202. (version (if (boundp 'org-release) org-release "N/A"))
  203. (git-version (if (boundp 'org-git-version) org-git-version "N/A"))
  204. (org-install (ignore-errors (find-library-name "org-install")))
  205. (version_ (format "Org-mode version %s (%s @ %s)"
  206. version
  207. git-version
  208. (if org-install org-install "org-install.el can not be found!"))))
  209. (if (org-called-interactively-p 'interactive)
  210. (if here (insert version_)
  211. (message version_))
  212. version)))
  213. ;;; Compatibility constants
  214. ;;; The custom variables
  215. (defgroup org nil
  216. "Outline-based notes management and organizer."
  217. :tag "Org"
  218. :group 'outlines
  219. :group 'calendar)
  220. (defcustom org-mode-hook nil
  221. "Mode hook for Org-mode, run after the mode was turned on."
  222. :group 'org
  223. :type 'hook)
  224. (defcustom org-load-hook nil
  225. "Hook that is run after org.el has been loaded."
  226. :group 'org
  227. :type 'hook)
  228. (defcustom org-log-buffer-setup-hook nil
  229. "Hook that is run after an Org log buffer is created."
  230. :group 'org
  231. :version "24.1"
  232. :type 'hook)
  233. (defvar org-modules) ; defined below
  234. (defvar org-modules-loaded nil
  235. "Have the modules been loaded already?")
  236. (defun org-load-modules-maybe (&optional force)
  237. "Load all extensions listed in `org-modules'."
  238. (when (or force (not org-modules-loaded))
  239. (mapc (lambda (ext)
  240. (condition-case nil (require ext)
  241. (error (message "Problems while trying to load feature `%s'" ext))))
  242. org-modules)
  243. (setq org-modules-loaded t)))
  244. (defun org-set-modules (var value)
  245. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  246. (set var value)
  247. (when (featurep 'org)
  248. (org-load-modules-maybe 'force)))
  249. (when (org-bound-and-true-p org-modules)
  250. (let ((a (member 'org-infojs org-modules)))
  251. (and a (setcar a 'org-jsinfo))))
  252. (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)
  253. "Modules that should always be loaded together with org.el.
  254. If a description starts with <C>, the file is not part of Emacs
  255. and loading it will require that you have downloaded and properly installed
  256. the org-mode distribution.
  257. You can also use this system to load external packages (i.e. neither Org
  258. core modules, nor modules from the CONTRIB directory). Just add symbols
  259. to the end of the list. If the package is called org-xyz.el, then you need
  260. to add the symbol `xyz', and the package must have a call to
  261. (provide 'org-xyz)"
  262. :group 'org
  263. :set 'org-set-modules
  264. :type
  265. '(set :greedy t
  266. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  267. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  268. (const :tag " crypt: Encryption of subtrees" org-crypt)
  269. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  270. (const :tag " docview: Links to doc-view buffers" org-docview)
  271. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  272. (const :tag " id: Global IDs for identifying entries" org-id)
  273. (const :tag " info: Links to Info nodes" org-info)
  274. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  275. (const :tag " habit: Track your consistency with habits" org-habit)
  276. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  277. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  278. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  279. (const :tag " mew Links to Mew folders/messages" org-mew)
  280. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  281. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  282. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  283. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  284. (const :tag " vm: Links to VM folders/messages" org-vm)
  285. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  286. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  287. (const :tag " mouse: Additional mouse support" org-mouse)
  288. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  289. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  290. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  291. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  292. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  293. (const :tag "C collector: Collect properties into tables" org-collector)
  294. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  295. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  296. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  297. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  298. (const :tag "C eval: Include command output as text" org-eval)
  299. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  300. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  301. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  302. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  303. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  304. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  305. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  306. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  307. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  308. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  309. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  310. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  311. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  312. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  313. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  314. (const :tag "C registry: A registry for Org-mode links" org-registry)
  315. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  316. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  317. (const :tag "C secretary: Team management with org-mode" org-secretary)
  318. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  319. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  320. (const :tag "C track: Keep up with Org-mode development" org-track)
  321. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  322. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  323. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  324. (defcustom org-support-shift-select nil
  325. "Non-nil means make shift-cursor commands select text when possible.
  326. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  327. start selecting a region, or enlarge regions started in this way.
  328. In Org-mode, in special contexts, these same keys are used for
  329. other purposes, important enough to compete with shift selection.
  330. Org tries to balance these needs by supporting `shift-select-mode'
  331. outside these special contexts, under control of this variable.
  332. The default of this variable is nil, to avoid confusing behavior. Shifted
  333. cursor keys will then execute Org commands in the following contexts:
  334. - on a headline, changing TODO state (left/right) and priority (up/down)
  335. - on a time stamp, changing the time
  336. - in a plain list item, changing the bullet type
  337. - in a property definition line, switching between allowed values
  338. - in the BEGIN line of a clock table (changing the time block).
  339. Outside these contexts, the commands will throw an error.
  340. When this variable is t and the cursor is not in a special
  341. context, Org-mode will support shift-selection for making and
  342. enlarging regions. To make this more effective, the bullet
  343. cycling will no longer happen anywhere in an item line, but only
  344. if the cursor is exactly on the bullet.
  345. If you set this variable to the symbol `always', then the keys
  346. will not be special in headlines, property lines, and item lines,
  347. to make shift selection work there as well. If this is what you
  348. want, you can use the following alternative commands: `C-c C-t'
  349. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  350. can be used to switch TODO sets, `C-c -' to cycle item bullet
  351. types, and properties can be edited by hand or in column view.
  352. However, when the cursor is on a timestamp, shift-cursor commands
  353. will still edit the time stamp - this is just too good to give up.
  354. XEmacs user should have this variable set to nil, because
  355. `shift-select-mode' is in Emacs 23 or later only."
  356. :group 'org
  357. :type '(choice
  358. (const :tag "Never" nil)
  359. (const :tag "When outside special context" t)
  360. (const :tag "Everywhere except timestamps" always)))
  361. (defcustom org-loop-over-headlines-in-active-region nil
  362. "Shall some commands act upon headlines in the active region?
  363. When set to `t', some commands will be performed in all headlines
  364. within the active region.
  365. When set to `start-level', some commands will be performed in all
  366. headlines within the active region, provided that these headlines
  367. are of the same level than the first one.
  368. When set to a string, those commands will be performed on the
  369. matching headlines within the active region. Such string must be
  370. a tags/property/todo match as it is used in the agenda tags view.
  371. The list of commands is: `org-schedule', `org-deadline',
  372. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  373. `org-archive-to-archive-sibling'. The archiving commands skip
  374. already archived entries."
  375. :type '(choice (const :tag "Don't loop" nil)
  376. (const :tag "All headlines in active region" t)
  377. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  378. (string :tag "Tags/Property/Todo matcher"))
  379. :version "24.1"
  380. :group 'org-todo
  381. :group 'org-archive)
  382. (defgroup org-startup nil
  383. "Options concerning startup of Org-mode."
  384. :tag "Org Startup"
  385. :group 'org)
  386. (defcustom org-startup-folded t
  387. "Non-nil means entering Org-mode will switch to OVERVIEW.
  388. This can also be configured on a per-file basis by adding one of
  389. the following lines anywhere in the buffer:
  390. #+STARTUP: fold (or `overview', this is equivalent)
  391. #+STARTUP: nofold (or `showall', this is equivalent)
  392. #+STARTUP: content
  393. #+STARTUP: showeverything"
  394. :group 'org-startup
  395. :type '(choice
  396. (const :tag "nofold: show all" nil)
  397. (const :tag "fold: overview" t)
  398. (const :tag "content: all headlines" content)
  399. (const :tag "show everything, even drawers" showeverything)))
  400. (defcustom org-startup-truncated t
  401. "Non-nil means entering Org-mode will set `truncate-lines'.
  402. This is useful since some lines containing links can be very long and
  403. uninteresting. Also tables look terrible when wrapped."
  404. :group 'org-startup
  405. :type 'boolean)
  406. (defcustom org-startup-indented nil
  407. "Non-nil means turn on `org-indent-mode' on startup.
  408. This can also be configured on a per-file basis by adding one of
  409. the following lines anywhere in the buffer:
  410. #+STARTUP: indent
  411. #+STARTUP: noindent"
  412. :group 'org-structure
  413. :type '(choice
  414. (const :tag "Not" nil)
  415. (const :tag "Globally (slow on startup in large files)" t)))
  416. (defcustom org-use-sub-superscripts t
  417. "Non-nil means interpret \"_\" and \"^\" for export.
  418. When this option is turned on, you can use TeX-like syntax for sub- and
  419. superscripts. Several characters after \"_\" or \"^\" will be
  420. considered as a single item - so grouping with {} is normally not
  421. needed. For example, the following things will be parsed as single
  422. sub- or superscripts.
  423. 10^24 or 10^tau several digits will be considered 1 item.
  424. 10^-12 or 10^-tau a leading sign with digits or a word
  425. x^2-y^3 will be read as x^2 - y^3, because items are
  426. terminated by almost any nonword/nondigit char.
  427. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  428. Still, ambiguity is possible - so when in doubt use {} to enclose the
  429. sub/superscript. If you set this variable to the symbol `{}',
  430. the braces are *required* in order to trigger interpretations as
  431. sub/superscript. This can be helpful in documents that need \"_\"
  432. frequently in plain text.
  433. Not all export backends support this, but HTML does.
  434. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  435. :group 'org-startup
  436. :group 'org-export-translation
  437. :version "24.1"
  438. :type '(choice
  439. (const :tag "Always interpret" t)
  440. (const :tag "Only with braces" {})
  441. (const :tag "Never interpret" nil)))
  442. (if (fboundp 'defvaralias)
  443. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  444. (defcustom org-startup-with-beamer-mode nil
  445. "Non-nil means turn on `org-beamer-mode' on startup.
  446. This can also be configured on a per-file basis by adding one of
  447. the following lines anywhere in the buffer:
  448. #+STARTUP: beamer"
  449. :group 'org-startup
  450. :version "24.1"
  451. :type 'boolean)
  452. (defcustom org-startup-align-all-tables nil
  453. "Non-nil means align all tables when visiting a file.
  454. This is useful when the column width in tables is forced with <N> cookies
  455. in table fields. Such tables will look correct only after the first re-align.
  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: align
  459. #+STARTUP: noalign"
  460. :group 'org-startup
  461. :type 'boolean)
  462. (defcustom org-startup-with-inline-images nil
  463. "Non-nil means show inline images when loading a new Org file.
  464. This can also be configured on a per-file basis by adding one of
  465. the following lines anywhere in the buffer:
  466. #+STARTUP: inlineimages
  467. #+STARTUP: noinlineimages"
  468. :group 'org-startup
  469. :version "24.1"
  470. :type 'boolean)
  471. (defcustom org-insert-mode-line-in-empty-file nil
  472. "Non-nil means insert the first line setting Org-mode in empty files.
  473. When the function `org-mode' is called interactively in an empty file, this
  474. normally means that the file name does not automatically trigger Org-mode.
  475. To ensure that the file will always be in Org-mode in the future, a
  476. line enforcing Org-mode will be inserted into the buffer, if this option
  477. has been set."
  478. :group 'org-startup
  479. :type 'boolean)
  480. (defcustom org-replace-disputed-keys nil
  481. "Non-nil means use alternative key bindings for some keys.
  482. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  483. These keys are also used by other packages like shift-selection-mode'
  484. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  485. If you want to use Org-mode together with one of these other modes,
  486. or more generally if you would like to move some Org-mode commands to
  487. other keys, set this variable and configure the keys with the variable
  488. `org-disputed-keys'.
  489. This option is only relevant at load-time of Org-mode, and must be set
  490. *before* org.el is loaded. Changing it requires a restart of Emacs to
  491. become effective."
  492. :group 'org-startup
  493. :type 'boolean)
  494. (defcustom org-use-extra-keys nil
  495. "Non-nil means use extra key sequence definitions for certain commands.
  496. This happens automatically if you run XEmacs or if `window-system'
  497. is nil. This variable lets you do the same manually. You must
  498. set it before loading org.
  499. Example: on Carbon Emacs 22 running graphically, with an external
  500. keyboard on a Powerbook, the default way of setting M-left might
  501. not work for either Alt or ESC. Setting this variable will make
  502. it work for ESC."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (if (fboundp 'defvaralias)
  506. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  507. (defcustom org-disputed-keys
  508. '(([(shift up)] . [(meta p)])
  509. ([(shift down)] . [(meta n)])
  510. ([(shift left)] . [(meta -)])
  511. ([(shift right)] . [(meta +)])
  512. ([(control shift right)] . [(meta shift +)])
  513. ([(control shift left)] . [(meta shift -)]))
  514. "Keys for which Org-mode and other modes compete.
  515. This is an alist, cars are the default keys, second element specifies
  516. the alternative to use when `org-replace-disputed-keys' is t.
  517. Keys can be specified in any syntax supported by `define-key'.
  518. The value of this option takes effect only at Org-mode's startup,
  519. therefore you'll have to restart Emacs to apply it after changing."
  520. :group 'org-startup
  521. :type 'alist)
  522. (defun org-key (key)
  523. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  524. Or return the original if not disputed.
  525. Also apply the translations defined in `org-xemacs-key-equivalents'."
  526. (when org-replace-disputed-keys
  527. (let* ((nkey (key-description key))
  528. (x (org-find-if (lambda (x)
  529. (equal (key-description (car x)) nkey))
  530. org-disputed-keys)))
  531. (setq key (if x (cdr x) key))))
  532. (when (featurep 'xemacs)
  533. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  534. key)
  535. (defun org-find-if (predicate seq)
  536. (catch 'exit
  537. (while seq
  538. (if (funcall predicate (car seq))
  539. (throw 'exit (car seq))
  540. (pop seq)))))
  541. (defun org-defkey (keymap key def)
  542. "Define a key, possibly translated, as returned by `org-key'."
  543. (define-key keymap (org-key key) def))
  544. (defcustom org-ellipsis nil
  545. "The ellipsis to use in the Org-mode outline.
  546. When nil, just use the standard three dots. When a string, use that instead,
  547. When a face, use the standard 3 dots, but with the specified face.
  548. The change affects only Org-mode (which will then use its own display table).
  549. Changing this requires executing `M-x org-mode' in a buffer to become
  550. effective."
  551. :group 'org-startup
  552. :type '(choice (const :tag "Default" nil)
  553. (face :tag "Face" :value org-warning)
  554. (string :tag "String" :value "...#")))
  555. (defvar org-display-table nil
  556. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  557. (defgroup org-keywords nil
  558. "Keywords in Org-mode."
  559. :tag "Org Keywords"
  560. :group 'org)
  561. (defcustom org-deadline-string "DEADLINE:"
  562. "String to mark deadline entries.
  563. A deadline is this string, followed by a time stamp. Should be a word,
  564. terminated by a colon. You can insert a schedule keyword and
  565. a timestamp with \\[org-deadline].
  566. Changes become only effective after restarting Emacs."
  567. :group 'org-keywords
  568. :type 'string)
  569. (defcustom org-scheduled-string "SCHEDULED:"
  570. "String to mark scheduled TODO entries.
  571. A schedule is this string, followed by a time stamp. Should be a word,
  572. terminated by a colon. You can insert a schedule keyword and
  573. a timestamp with \\[org-schedule].
  574. Changes become only effective after restarting Emacs."
  575. :group 'org-keywords
  576. :type 'string)
  577. (defcustom org-closed-string "CLOSED:"
  578. "String used as the prefix for timestamps logging closing a TODO entry."
  579. :group 'org-keywords
  580. :type 'string)
  581. (defcustom org-clock-string "CLOCK:"
  582. "String used as prefix for timestamps clocking work hours on an item."
  583. :group 'org-keywords
  584. :type 'string)
  585. (defcustom org-comment-string "COMMENT"
  586. "Entries starting with this keyword will never be exported.
  587. An entry can be toggled between COMMENT and normal with
  588. \\[org-toggle-comment].
  589. Changes become only effective after restarting Emacs."
  590. :group 'org-keywords
  591. :type 'string)
  592. (defcustom org-quote-string "QUOTE"
  593. "Entries starting with this keyword will be exported in fixed-width font.
  594. Quoting applies only to the text in the entry following the headline, and does
  595. not extend beyond the next headline, even if that is lower level.
  596. An entry can be toggled between QUOTE and normal with
  597. \\[org-toggle-fixed-width-section]."
  598. :group 'org-keywords
  599. :type 'string)
  600. (defconst org-repeat-re
  601. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  602. "Regular expression for specifying repeated events.
  603. After a match, group 1 contains the repeat expression.")
  604. (defgroup org-structure nil
  605. "Options concerning the general structure of Org-mode files."
  606. :tag "Org Structure"
  607. :group 'org)
  608. (defgroup org-reveal-location nil
  609. "Options about how to make context of a location visible."
  610. :tag "Org Reveal Location"
  611. :group 'org-structure)
  612. (defconst org-context-choice
  613. '(choice
  614. (const :tag "Always" t)
  615. (const :tag "Never" nil)
  616. (repeat :greedy t :tag "Individual contexts"
  617. (cons
  618. (choice :tag "Context"
  619. (const agenda)
  620. (const org-goto)
  621. (const occur-tree)
  622. (const tags-tree)
  623. (const link-search)
  624. (const mark-goto)
  625. (const bookmark-jump)
  626. (const isearch)
  627. (const default))
  628. (boolean))))
  629. "Contexts for the reveal options.")
  630. (defcustom org-show-hierarchy-above '((default . t))
  631. "Non-nil means show full hierarchy when revealing a location.
  632. Org-mode often shows locations in an org-mode file which might have
  633. been invisible before. When this is set, the hierarchy of headings
  634. above the exposed location is shown.
  635. Turning this off for example for sparse trees makes them very compact.
  636. Instead of t, this can also be an alist specifying this option for different
  637. contexts. Valid contexts are
  638. agenda when exposing an entry from the agenda
  639. org-goto when using the command `org-goto' on key C-c C-j
  640. occur-tree when using the command `org-occur' on key C-c /
  641. tags-tree when constructing a sparse tree based on tags matches
  642. link-search when exposing search matches associated with a link
  643. mark-goto when exposing the jump goal of a mark
  644. bookmark-jump when exposing a bookmark location
  645. isearch when exiting from an incremental search
  646. default default for all contexts not set explicitly"
  647. :group 'org-reveal-location
  648. :type org-context-choice)
  649. (defcustom org-show-following-heading '((default . nil))
  650. "Non-nil means show following heading when revealing a location.
  651. Org-mode often shows locations in an org-mode file which might have
  652. been invisible before. When this is set, the heading following the
  653. match is shown.
  654. Turning this off for example for sparse trees makes them very compact,
  655. but makes it harder to edit the location of the match. In such a case,
  656. use the command \\[org-reveal] to show more context.
  657. Instead of t, this can also be an alist specifying this option for different
  658. contexts. See `org-show-hierarchy-above' for valid contexts."
  659. :group 'org-reveal-location
  660. :type org-context-choice)
  661. (defcustom org-show-siblings '((default . nil) (isearch t))
  662. "Non-nil means show all sibling heading when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the sibling of the current entry
  665. heading are all made visible. If `org-show-hierarchy-above' is t,
  666. the same happens on each level of the hierarchy above the current entry.
  667. By default this is on for the isearch context, off for all other contexts.
  668. Turning this off for example for sparse trees makes them very compact,
  669. but makes it harder to edit the location of the match. In such a case,
  670. use the command \\[org-reveal] to show more context.
  671. Instead of t, this can also be an alist specifying this option for different
  672. contexts. See `org-show-hierarchy-above' for valid contexts."
  673. :group 'org-reveal-location
  674. :type org-context-choice)
  675. (defcustom org-show-entry-below '((default . nil))
  676. "Non-nil means show the entry below a headline when revealing a location.
  677. Org-mode often shows locations in an org-mode file which might have
  678. been invisible before. When this is set, the text below the headline that is
  679. exposed is also shown.
  680. By default this is off for all contexts.
  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-indirect-buffer-display 'other-window
  686. "How should indirect tree buffers be displayed?
  687. This applies to indirect buffers created with the commands
  688. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  689. Valid values are:
  690. current-window Display in the current window
  691. other-window Just display in another window.
  692. dedicated-frame Create one new frame, and re-use it each time.
  693. new-frame Make a new frame each time. Note that in this case
  694. previously-made indirect buffers are kept, and you need to
  695. kill these buffers yourself."
  696. :group 'org-structure
  697. :group 'org-agenda-windows
  698. :type '(choice
  699. (const :tag "In current window" current-window)
  700. (const :tag "In current frame, other window" other-window)
  701. (const :tag "Each time a new frame" new-frame)
  702. (const :tag "One dedicated frame" dedicated-frame)))
  703. (defcustom org-use-speed-commands nil
  704. "Non-nil means activate single letter commands at beginning of a headline.
  705. This may also be a function to test for appropriate locations where speed
  706. commands should be active."
  707. :group 'org-structure
  708. :type '(choice
  709. (const :tag "Never" nil)
  710. (const :tag "At beginning of headline stars" t)
  711. (function)))
  712. (defcustom org-speed-commands-user nil
  713. "Alist of additional speed commands.
  714. This list will be checked before `org-speed-commands-default'
  715. when the variable `org-use-speed-commands' is non-nil
  716. and when the cursor is at the beginning of a headline.
  717. The car if each entry is a string with a single letter, which must
  718. be assigned to `self-insert-command' in the global map.
  719. The cdr is either a command to be called interactively, a function
  720. to be called, or a form to be evaluated.
  721. An entry that is just a list with a single string will be interpreted
  722. as a descriptive headline that will be added when listing the speed
  723. commands in the Help buffer using the `?' speed command."
  724. :group 'org-structure
  725. :type '(repeat :value ("k" . ignore)
  726. (choice :value ("k" . ignore)
  727. (list :tag "Descriptive Headline" (string :tag "Headline"))
  728. (cons :tag "Letter and Command"
  729. (string :tag "Command letter")
  730. (choice
  731. (function)
  732. (sexp))))))
  733. (defgroup org-cycle nil
  734. "Options concerning visibility cycling in Org-mode."
  735. :tag "Org Cycle"
  736. :group 'org-structure)
  737. (defcustom org-cycle-skip-children-state-if-no-children t
  738. "Non-nil means skip CHILDREN state in entries that don't have any."
  739. :group 'org-cycle
  740. :type 'boolean)
  741. (defcustom org-cycle-max-level nil
  742. "Maximum level which should still be subject to visibility cycling.
  743. Levels higher than this will, for cycling, be treated as text, not a headline.
  744. When `org-odd-levels-only' is set, a value of N in this variable actually
  745. means 2N-1 stars as the limiting headline.
  746. When nil, cycle all levels.
  747. Note that the limiting level of cycling is also influenced by
  748. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  749. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  750. than its value."
  751. :group 'org-cycle
  752. :type '(choice
  753. (const :tag "No limit" nil)
  754. (integer :tag "Maximum level")))
  755. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  756. "Names of drawers. Drawers are not opened by cycling on the headline above.
  757. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  758. this:
  759. :DRAWERNAME:
  760. .....
  761. :END:
  762. The drawer \"PROPERTIES\" is special for capturing properties through
  763. the property API.
  764. Drawers can be defined on the per-file basis with a line like:
  765. #+DRAWERS: HIDDEN STATE PROPERTIES"
  766. :group 'org-structure
  767. :group 'org-cycle
  768. :type '(repeat (string :tag "Drawer Name")))
  769. (defcustom org-hide-block-startup nil
  770. "Non-nil means entering Org-mode will fold all blocks.
  771. This can also be set in on a per-file basis with
  772. #+STARTUP: hideblocks
  773. #+STARTUP: showblocks"
  774. :group 'org-startup
  775. :group 'org-cycle
  776. :type 'boolean)
  777. (defcustom org-cycle-global-at-bob nil
  778. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  779. This makes it possible to do global cycling without having to use S-TAB or
  780. \\[universal-argument] TAB. For this special case to work, the first line \
  781. of the buffer
  782. must not be a headline - it may be empty or some other text. When used in
  783. this way, `org-cycle-hook' is disables temporarily, to make sure the
  784. cursor stays at the beginning of the buffer.
  785. When this option is nil, don't do anything special at the beginning
  786. of the buffer."
  787. :group 'org-cycle
  788. :type 'boolean)
  789. (defcustom org-cycle-level-after-item/entry-creation t
  790. "Non-nil means cycle entry level or item indentation in new empty entries.
  791. When the cursor is at the end of an empty headline, i.e with only stars
  792. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  793. and then all possible ancestor states, before returning to the original state.
  794. This makes data entry extremely fast: M-RET to create a new headline,
  795. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  796. When the cursor is at the end of an empty plain list item, one TAB will
  797. make it a subitem, two or more tabs will back up to make this an item
  798. higher up in the item hierarchy."
  799. :group 'org-cycle
  800. :type 'boolean)
  801. (defcustom org-cycle-emulate-tab t
  802. "Where should `org-cycle' emulate TAB.
  803. nil Never
  804. white Only in completely white lines
  805. whitestart Only at the beginning of lines, before the first non-white char
  806. t Everywhere except in headlines
  807. exc-hl-bol Everywhere except at the start of a headline
  808. If TAB is used in a place where it does not emulate TAB, the current subtree
  809. visibility is cycled."
  810. :group 'org-cycle
  811. :type '(choice (const :tag "Never" nil)
  812. (const :tag "Only in completely white lines" white)
  813. (const :tag "Before first char in a line" whitestart)
  814. (const :tag "Everywhere except in headlines" t)
  815. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  816. ))
  817. (defcustom org-cycle-separator-lines 2
  818. "Number of empty lines needed to keep an empty line between collapsed trees.
  819. If you leave an empty line between the end of a subtree and the following
  820. headline, this empty line is hidden when the subtree is folded.
  821. Org-mode will leave (exactly) one empty line visible if the number of
  822. empty lines is equal or larger to the number given in this variable.
  823. So the default 2 means at least 2 empty lines after the end of a subtree
  824. are needed to produce free space between a collapsed subtree and the
  825. following headline.
  826. If the number is negative, and the number of empty lines is at least -N,
  827. all empty lines are shown.
  828. Special case: when 0, never leave empty lines in collapsed view."
  829. :group 'org-cycle
  830. :type 'integer)
  831. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  832. (defcustom org-pre-cycle-hook nil
  833. "Hook that is run before visibility cycling is happening.
  834. The function(s) in this hook must accept a single argument which indicates
  835. the new state that will be set right after running this hook. The
  836. argument is a symbol. Before a global state change, it can have the values
  837. `overview', `content', or `all'. Before a local state change, it can have
  838. the values `folded', `children', or `subtree'."
  839. :group 'org-cycle
  840. :type 'hook)
  841. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  842. org-cycle-hide-drawers
  843. org-cycle-show-empty-lines
  844. org-optimize-window-after-visibility-change)
  845. "Hook that is run after `org-cycle' has changed the buffer visibility.
  846. The function(s) in this hook must accept a single argument which indicates
  847. the new state that was set by the most recent `org-cycle' command. The
  848. argument is a symbol. After a global state change, it can have the values
  849. `overview', `content', or `all'. After a local state change, it can have
  850. the values `folded', `children', or `subtree'."
  851. :group 'org-cycle
  852. :type 'hook)
  853. (defgroup org-edit-structure nil
  854. "Options concerning structure editing in Org-mode."
  855. :tag "Org Edit Structure"
  856. :group 'org-structure)
  857. (defcustom org-odd-levels-only nil
  858. "Non-nil means skip even levels and only use odd levels for the outline.
  859. This has the effect that two stars are being added/taken away in
  860. promotion/demotion commands. It also influences how levels are
  861. handled by the exporters.
  862. Changing it requires restart of `font-lock-mode' to become effective
  863. for fontification also in regions already fontified.
  864. You may also set this on a per-file basis by adding one of the following
  865. lines to the buffer:
  866. #+STARTUP: odd
  867. #+STARTUP: oddeven"
  868. :group 'org-edit-structure
  869. :group 'org-appearance
  870. :type 'boolean)
  871. (defcustom org-adapt-indentation t
  872. "Non-nil means adapt indentation to outline node level.
  873. When this variable is set, Org assumes that you write outlines by
  874. indenting text in each node to align with the headline (after the stars).
  875. The following issues are influenced by this variable:
  876. - When this is set and the *entire* text in an entry is indented, the
  877. indentation is increased by one space in a demotion command, and
  878. decreased by one in a promotion command. If any line in the entry
  879. body starts with text at column 0, indentation is not changed at all.
  880. - Property drawers and planning information is inserted indented when
  881. this variable s set. When nil, they will not be indented.
  882. - TAB indents a line relative to context. The lines below a headline
  883. will be indented when this variable is set.
  884. Note that this is all about true indentation, by adding and removing
  885. space characters. See also `org-indent.el' which does level-dependent
  886. indentation in a virtual way, i.e. at display time in Emacs."
  887. :group 'org-edit-structure
  888. :type 'boolean)
  889. (defcustom org-special-ctrl-a/e nil
  890. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  891. When t, `C-a' will bring back the cursor to the beginning of the
  892. headline text, i.e. after the stars and after a possible TODO
  893. keyword. In an item, this will be the position after bullet and
  894. check-box, if any. When the cursor is already at that position,
  895. another `C-a' will bring it to the beginning of the line.
  896. `C-e' will jump to the end of the headline, ignoring the presence
  897. of tags in the headline. A second `C-e' will then jump to the
  898. true end of the line, after any tags. This also means that, when
  899. this variable is non-nil, `C-e' also will never jump beyond the
  900. end of the heading of a folded section, i.e. not after the
  901. ellipses.
  902. When set to the symbol `reversed', the first `C-a' or `C-e' works
  903. normally, going to the true line boundary first. Only a directly
  904. following, identical keypress will bring the cursor to the
  905. special positions.
  906. This may also be a cons cell where the behavior for `C-a' and
  907. `C-e' is set separately."
  908. :group 'org-edit-structure
  909. :type '(choice
  910. (const :tag "off" nil)
  911. (const :tag "on: after stars/bullet and before tags first" t)
  912. (const :tag "reversed: true line boundary first" reversed)
  913. (cons :tag "Set C-a and C-e separately"
  914. (choice :tag "Special C-a"
  915. (const :tag "off" nil)
  916. (const :tag "on: after stars/bullet first" t)
  917. (const :tag "reversed: before stars/bullet first" reversed))
  918. (choice :tag "Special C-e"
  919. (const :tag "off" nil)
  920. (const :tag "on: before tags first" t)
  921. (const :tag "reversed: after tags first" reversed)))))
  922. (if (fboundp 'defvaralias)
  923. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  924. (defcustom org-special-ctrl-k nil
  925. "Non-nil means `C-k' will behave specially in headlines.
  926. When nil, `C-k' will call the default `kill-line' command.
  927. When t, the following will happen while the cursor is in the headline:
  928. - When the cursor is at the beginning of a headline, kill the entire
  929. line and possible the folded subtree below the line.
  930. - When in the middle of the headline text, kill the headline up to the tags.
  931. - When after the headline text, kill the tags."
  932. :group 'org-edit-structure
  933. :type 'boolean)
  934. (defcustom org-ctrl-k-protect-subtree nil
  935. "Non-nil means, do not delete a hidden subtree with C-k.
  936. When set to the symbol `error', simply throw an error when C-k is
  937. used to kill (part-of) a headline that has hidden text behind it.
  938. Any other non-nil value will result in a query to the user, if it is
  939. OK to kill that hidden subtree. When nil, kill without remorse."
  940. :group 'org-edit-structure
  941. :version "24.1"
  942. :type '(choice
  943. (const :tag "Do not protect hidden subtrees" nil)
  944. (const :tag "Protect hidden subtrees with a security query" t)
  945. (const :tag "Never kill a hidden subtree with C-k" error)))
  946. (defcustom org-catch-invisible-edits nil
  947. "Check if in invisible region before inserting or deleting a character.
  948. Valid values are:
  949. nil Do not check, so just do invisible edits.
  950. error Throw an error and do nothing.
  951. show Make point visible, and do the requested edit.
  952. show-and-error Make point visible, then throw an error and abort the edit.
  953. smart Make point visible, and do insertion/deletion if it is
  954. adjacent to visible text and the change feels predictable.
  955. Never delete a previously invisible character or add in the
  956. middle or right after an invisible region. Basically, this
  957. allows insertion and backward-delete right before ellipses.
  958. FIXME: maybe in this case we should not even show?"
  959. :group 'org-edit-structure
  960. :version "24.1"
  961. :type '(choice
  962. (const :tag "Do not check" nil)
  963. (const :tag "Throw error when trying to edit" error)
  964. (const :tag "Unhide, but do not do the edit" show-and-error)
  965. (const :tag "Show invisible part and do the edit" show)
  966. (const :tag "Be smart and do the right thing" smart)))
  967. (defcustom org-yank-folded-subtrees t
  968. "Non-nil means when yanking subtrees, fold them.
  969. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  970. it starts with a heading and all other headings in it are either children
  971. or siblings, then fold all the subtrees. However, do this only if no
  972. text after the yank would be swallowed into a folded tree by this action."
  973. :group 'org-edit-structure
  974. :type 'boolean)
  975. (defcustom org-yank-adjusted-subtrees nil
  976. "Non-nil means when yanking subtrees, adjust the level.
  977. With this setting, `org-paste-subtree' is used to insert the subtree, see
  978. this function for details."
  979. :group 'org-edit-structure
  980. :type 'boolean)
  981. (defcustom org-M-RET-may-split-line '((default . t))
  982. "Non-nil means M-RET will split the line at the cursor position.
  983. When nil, it will go to the end of the line before making a
  984. new line.
  985. You may also set this option in a different way for different
  986. contexts. Valid contexts are:
  987. headline when creating a new headline
  988. item when creating a new item
  989. table in a table field
  990. default the value to be used for all contexts not explicitly
  991. customized"
  992. :group 'org-structure
  993. :group 'org-table
  994. :type '(choice
  995. (const :tag "Always" t)
  996. (const :tag "Never" nil)
  997. (repeat :greedy t :tag "Individual contexts"
  998. (cons
  999. (choice :tag "Context"
  1000. (const headline)
  1001. (const item)
  1002. (const table)
  1003. (const default))
  1004. (boolean)))))
  1005. (defcustom org-insert-heading-respect-content nil
  1006. "Non-nil means insert new headings after the current subtree.
  1007. When nil, the new heading is created directly after the current line.
  1008. The commands \\[org-insert-heading-respect-content] and
  1009. \\[org-insert-todo-heading-respect-content] turn this variable on
  1010. for the duration of the command."
  1011. :group 'org-structure
  1012. :type 'boolean)
  1013. (defcustom org-blank-before-new-entry '((heading . auto)
  1014. (plain-list-item . auto))
  1015. "Should `org-insert-heading' leave a blank line before new heading/item?
  1016. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1017. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1018. which case Org will look at the surrounding headings/items and try to
  1019. make an intelligent decision whether to insert a blank line or not.
  1020. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1021. set, the setting here is ignored and no empty line is inserted, to avoid
  1022. breaking the list structure."
  1023. :group 'org-edit-structure
  1024. :type '(list
  1025. (cons (const heading)
  1026. (choice (const :tag "Never" nil)
  1027. (const :tag "Always" t)
  1028. (const :tag "Auto" auto)))
  1029. (cons (const plain-list-item)
  1030. (choice (const :tag "Never" nil)
  1031. (const :tag "Always" t)
  1032. (const :tag "Auto" auto)))))
  1033. (defcustom org-insert-heading-hook nil
  1034. "Hook being run after inserting a new heading."
  1035. :group 'org-edit-structure
  1036. :type 'hook)
  1037. (defcustom org-enable-fixed-width-editor t
  1038. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1039. This currently only means they are never auto-wrapped.
  1040. When nil, such lines will be treated like ordinary lines.
  1041. See also the QUOTE keyword."
  1042. :group 'org-edit-structure
  1043. :type 'boolean)
  1044. (defcustom org-goto-auto-isearch t
  1045. "Non-nil means typing characters in `org-goto' starts incremental search."
  1046. :group 'org-edit-structure
  1047. :type 'boolean)
  1048. (defgroup org-sparse-trees nil
  1049. "Options concerning sparse trees in Org-mode."
  1050. :tag "Org Sparse Trees"
  1051. :group 'org-structure)
  1052. (defcustom org-highlight-sparse-tree-matches t
  1053. "Non-nil means highlight all matches that define a sparse tree.
  1054. The highlights will automatically disappear the next time the buffer is
  1055. changed by an edit command."
  1056. :group 'org-sparse-trees
  1057. :type 'boolean)
  1058. (defcustom org-remove-highlights-with-change t
  1059. "Non-nil means any change to the buffer will remove temporary highlights.
  1060. Such highlights are created by `org-occur' and `org-clock-display'.
  1061. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1062. The highlights created by `org-preview-latex-fragment' always need
  1063. `C-c C-c' to be removed."
  1064. :group 'org-sparse-trees
  1065. :group 'org-time
  1066. :type 'boolean)
  1067. (defcustom org-occur-hook '(org-first-headline-recenter)
  1068. "Hook that is run after `org-occur' has constructed a sparse tree.
  1069. This can be used to recenter the window to show as much of the structure
  1070. as possible."
  1071. :group 'org-sparse-trees
  1072. :type 'hook)
  1073. (defgroup org-imenu-and-speedbar nil
  1074. "Options concerning imenu and speedbar in Org-mode."
  1075. :tag "Org Imenu and Speedbar"
  1076. :group 'org-structure)
  1077. (defcustom org-imenu-depth 2
  1078. "The maximum level for Imenu access to Org-mode headlines.
  1079. This also applied for speedbar access."
  1080. :group 'org-imenu-and-speedbar
  1081. :type 'integer)
  1082. (defgroup org-table nil
  1083. "Options concerning tables in Org-mode."
  1084. :tag "Org Table"
  1085. :group 'org)
  1086. (defcustom org-enable-table-editor 'optimized
  1087. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. When equal to the symbol `optimized', the table editor will be optimized to
  1090. do the following:
  1091. - Automatic overwrite mode in front of whitespace in table fields.
  1092. This makes the structure of the table stay in tact as long as the edited
  1093. field does not exceed the column width.
  1094. - Minimize the number of realigns. Normally, the table is aligned each time
  1095. TAB or RET are pressed to move to another field. With optimization this
  1096. happens only if changes to a field might have changed the column width.
  1097. Optimization requires replacing the functions `self-insert-command',
  1098. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1099. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1100. very good at guessing when a re-align will be necessary, but you can always
  1101. force one with \\[org-ctrl-c-ctrl-c].
  1102. If you would like to use the optimized version in Org-mode, but the
  1103. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1104. This variable can be used to turn on and off the table editor during a session,
  1105. but in order to toggle optimization, a restart is required.
  1106. See also the variable `org-table-auto-blank-field'."
  1107. :group 'org-table
  1108. :type '(choice
  1109. (const :tag "off" nil)
  1110. (const :tag "on" t)
  1111. (const :tag "on, optimized" optimized)))
  1112. (defcustom org-self-insert-cluster-for-undo t
  1113. "Non-nil means cluster self-insert commands for undo when possible.
  1114. If this is set, then, like in the Emacs command loop, 20 consecutive
  1115. characters will be undone together.
  1116. This is configurable, because there is some impact on typing performance."
  1117. :group 'org-table
  1118. :type 'boolean)
  1119. (defcustom org-table-tab-recognizes-table.el t
  1120. "Non-nil means TAB will automatically notice a table.el table.
  1121. When it sees such a table, it moves point into it and - if necessary -
  1122. calls `table-recognize-table'."
  1123. :group 'org-table-editing
  1124. :type 'boolean)
  1125. (defgroup org-link nil
  1126. "Options concerning links in Org-mode."
  1127. :tag "Org Link"
  1128. :group 'org)
  1129. (defvar org-link-abbrev-alist-local nil
  1130. "Buffer-local version of `org-link-abbrev-alist', which see.
  1131. The value of this is taken from the #+LINK lines.")
  1132. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1133. (defcustom org-link-abbrev-alist nil
  1134. "Alist of link abbreviations.
  1135. The car of each element is a string, to be replaced at the start of a link.
  1136. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1137. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1138. [[linkkey:tag][description]]
  1139. The 'linkkey' must be a word word, starting with a letter, followed
  1140. by letters, numbers, '-' or '_'.
  1141. If REPLACE is a string, the tag will simply be appended to create the link.
  1142. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1143. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1144. be inserted at that point (see the function `url-hexify-string').
  1145. REPLACE may also be a function that will be called with the tag as the
  1146. only argument to create the link, which should be returned as a string.
  1147. See the manual for examples."
  1148. :group 'org-link
  1149. :type '(repeat
  1150. (cons
  1151. (string :tag "Protocol")
  1152. (choice
  1153. (string :tag "Format")
  1154. (function)))))
  1155. (defcustom org-descriptive-links t
  1156. "Non-nil means Org will display descriptive links.
  1157. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1158. \"Org Website\", hiding the link itself and just displaying its
  1159. description. When set to `nil', Org will display the full links
  1160. literally.
  1161. You can interactively set the value of this variable by calling
  1162. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1163. :group 'org-link
  1164. :type 'boolean)
  1165. (defcustom org-link-file-path-type 'adaptive
  1166. "How the path name in file links should be stored.
  1167. Valid values are:
  1168. relative Relative to the current directory, i.e. the directory of the file
  1169. into which the link is being inserted.
  1170. absolute Absolute path, if possible with ~ for home directory.
  1171. noabbrev Absolute path, no abbreviation of home directory.
  1172. adaptive Use relative path for files in the current directory and sub-
  1173. directories of it. For other files, use an absolute path."
  1174. :group 'org-link
  1175. :type '(choice
  1176. (const relative)
  1177. (const absolute)
  1178. (const noabbrev)
  1179. (const adaptive)))
  1180. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1181. "Types of links that should be activated in Org-mode files.
  1182. This is a list of symbols, each leading to the activation of a certain link
  1183. type. In principle, it does not hurt to turn on most link types - there may
  1184. be a small gain when turning off unused link types. The types are:
  1185. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1186. angle Links in angular brackets that may contain whitespace like
  1187. <bbdb:Carsten Dominik>.
  1188. plain Plain links in normal text, no whitespace, like http://google.com.
  1189. radio Text that is matched by a radio target, see manual for details.
  1190. tag Tag settings in a headline (link to tag search).
  1191. date Time stamps (link to calendar).
  1192. footnote Footnote labels.
  1193. Changing this variable requires a restart of Emacs to become effective."
  1194. :group 'org-link
  1195. :type '(set :greedy t
  1196. (const :tag "Double bracket links" bracket)
  1197. (const :tag "Angular bracket links" angle)
  1198. (const :tag "Plain text links" plain)
  1199. (const :tag "Radio target matches" radio)
  1200. (const :tag "Tags" tag)
  1201. (const :tag "Timestamps" date)
  1202. (const :tag "Footnotes" footnote)))
  1203. (defcustom org-make-link-description-function nil
  1204. "Function to use to generate link descriptions from links.
  1205. If nil the link location will be used. This function must take
  1206. two parameters; the first is the link and the second the
  1207. description `org-insert-link' has generated, and should return the
  1208. description to use."
  1209. :group 'org-link
  1210. :type 'function)
  1211. (defgroup org-link-store nil
  1212. "Options concerning storing links in Org-mode."
  1213. :tag "Org Store Link"
  1214. :group 'org-link)
  1215. (defcustom org-email-link-description-format "Email %c: %.30s"
  1216. "Format of the description part of a link to an email or usenet message.
  1217. The following %-escapes will be replaced by corresponding information:
  1218. %F full \"From\" field
  1219. %f name, taken from \"From\" field, address if no name
  1220. %T full \"To\" field
  1221. %t first name in \"To\" field, address if no name
  1222. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1223. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1224. %s subject
  1225. %d date
  1226. %m message-id.
  1227. You may use normal field width specification between the % and the letter.
  1228. This is for example useful to limit the length of the subject.
  1229. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1230. :group 'org-link-store
  1231. :type 'string)
  1232. (defcustom org-from-is-user-regexp
  1233. (let (r1 r2)
  1234. (when (and user-mail-address (not (string= user-mail-address "")))
  1235. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1236. (when (and user-full-name (not (string= user-full-name "")))
  1237. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1238. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1239. "Regexp matched against the \"From:\" header of an email or usenet message.
  1240. It should match if the message is from the user him/herself."
  1241. :group 'org-link-store
  1242. :type 'regexp)
  1243. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1244. "Non-nil means storing a link to an Org file will use entry IDs.
  1245. Note that before this variable is even considered, org-id must be loaded,
  1246. so please customize `org-modules' and turn it on.
  1247. The variable can have the following values:
  1248. t Create an ID if needed to make a link to the current entry.
  1249. create-if-interactive
  1250. If `org-store-link' is called directly (interactively, as a user
  1251. command), do create an ID to support the link. But when doing the
  1252. job for remember, only use the ID if it already exists. The
  1253. purpose of this setting is to avoid proliferation of unwanted
  1254. IDs, just because you happen to be in an Org file when you
  1255. call `org-remember' that automatically and preemptively
  1256. creates a link. If you do want to get an ID link in a remember
  1257. template to an entry not having an ID, create it first by
  1258. explicitly creating a link to it, using `C-c C-l' first.
  1259. create-if-interactive-and-no-custom-id
  1260. Like create-if-interactive, but do not create an ID if there is
  1261. a CUSTOM_ID property defined in the entry. This is the default.
  1262. use-existing
  1263. Use existing ID, do not create one.
  1264. nil Never use an ID to make a link, instead link using a text search for
  1265. the headline text."
  1266. :group 'org-link-store
  1267. :type '(choice
  1268. (const :tag "Create ID to make link" t)
  1269. (const :tag "Create if storing link interactively"
  1270. create-if-interactive)
  1271. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1272. create-if-interactive-and-no-custom-id)
  1273. (const :tag "Only use existing" use-existing)
  1274. (const :tag "Do not use ID to create link" nil)))
  1275. (defcustom org-context-in-file-links t
  1276. "Non-nil means file links from `org-store-link' contain context.
  1277. A search string will be added to the file name with :: as separator and
  1278. used to find the context when the link is activated by the command
  1279. `org-open-at-point'. When this option is t, the entire active region
  1280. will be placed in the search string of the file link. If set to a
  1281. positive integer, only the first n lines of context will be stored.
  1282. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1283. negates this setting for the duration of the command."
  1284. :group 'org-link-store
  1285. :type '(choice boolean integer))
  1286. (defcustom org-keep-stored-link-after-insertion nil
  1287. "Non-nil means keep link in list for entire session.
  1288. The command `org-store-link' adds a link pointing to the current
  1289. location to an internal list. These links accumulate during a session.
  1290. The command `org-insert-link' can be used to insert links into any
  1291. Org-mode file (offering completion for all stored links). When this
  1292. option is nil, every link which has been inserted once using \\[org-insert-link]
  1293. will be removed from the list, to make completing the unused links
  1294. more efficient."
  1295. :group 'org-link-store
  1296. :type 'boolean)
  1297. (defgroup org-link-follow nil
  1298. "Options concerning following links in Org-mode."
  1299. :tag "Org Follow Link"
  1300. :group 'org-link)
  1301. (defcustom org-link-translation-function nil
  1302. "Function to translate links with different syntax to Org syntax.
  1303. This can be used to translate links created for example by the Planner
  1304. or emacs-wiki packages to Org syntax.
  1305. The function must accept two parameters, a TYPE containing the link
  1306. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1307. which is everything after the link protocol. It should return a cons
  1308. with possibly modified values of type and path.
  1309. Org contains a function for this, so if you set this variable to
  1310. `org-translate-link-from-planner', you should be able follow many
  1311. links created by planner."
  1312. :group 'org-link-follow
  1313. :type 'function)
  1314. (defcustom org-follow-link-hook nil
  1315. "Hook that is run after a link has been followed."
  1316. :group 'org-link-follow
  1317. :type 'hook)
  1318. (defcustom org-tab-follows-link nil
  1319. "Non-nil means on links TAB will follow the link.
  1320. Needs to be set before org.el is loaded.
  1321. This really should not be used, it does not make sense, and the
  1322. implementation is bad."
  1323. :group 'org-link-follow
  1324. :type 'boolean)
  1325. (defcustom org-return-follows-link nil
  1326. "Non-nil means on links RET will follow the link."
  1327. :group 'org-link-follow
  1328. :type 'boolean)
  1329. (defcustom org-mouse-1-follows-link
  1330. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1331. "Non-nil means mouse-1 on a link will follow the link.
  1332. A longer mouse click will still set point. Does not work on XEmacs.
  1333. Needs to be set before org.el is loaded."
  1334. :group 'org-link-follow
  1335. :type 'boolean)
  1336. (defcustom org-mark-ring-length 4
  1337. "Number of different positions to be recorded in the ring.
  1338. Changing this requires a restart of Emacs to work correctly."
  1339. :group 'org-link-follow
  1340. :type 'integer)
  1341. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1342. "Non-nil means internal links in Org files must exactly match a headline.
  1343. When nil, the link search tries to match a phrase with all words
  1344. in the search text."
  1345. :group 'org-link-follow
  1346. :version "24.1"
  1347. :type '(choice
  1348. (const :tag "Use fuzzy text search" nil)
  1349. (const :tag "Match only exact headline" t)
  1350. (const :tag "Match exact headline or query to create it"
  1351. query-to-create)))
  1352. (defcustom org-link-frame-setup
  1353. '((vm . vm-visit-folder-other-frame)
  1354. (vm-imap . vm-visit-imap-folder-other-frame)
  1355. (gnus . org-gnus-no-new-news)
  1356. (file . find-file-other-window)
  1357. (wl . wl-other-frame))
  1358. "Setup the frame configuration for following links.
  1359. When following a link with Emacs, it may often be useful to display
  1360. this link in another window or frame. This variable can be used to
  1361. set this up for the different types of links.
  1362. For VM, use any of
  1363. `vm-visit-folder'
  1364. `vm-visit-folder-other-window'
  1365. `vm-visit-folder-other-frame'
  1366. For Gnus, use any of
  1367. `gnus'
  1368. `gnus-other-frame'
  1369. `org-gnus-no-new-news'
  1370. For FILE, use any of
  1371. `find-file'
  1372. `find-file-other-window'
  1373. `find-file-other-frame'
  1374. For Wanderlust use any of
  1375. `wl'
  1376. `wl-other-frame'
  1377. For the calendar, use the variable `calendar-setup'.
  1378. For BBDB, it is currently only possible to display the matches in
  1379. another window."
  1380. :group 'org-link-follow
  1381. :type '(list
  1382. (cons (const vm)
  1383. (choice
  1384. (const vm-visit-folder)
  1385. (const vm-visit-folder-other-window)
  1386. (const vm-visit-folder-other-frame)))
  1387. (cons (const gnus)
  1388. (choice
  1389. (const gnus)
  1390. (const gnus-other-frame)
  1391. (const org-gnus-no-new-news)))
  1392. (cons (const file)
  1393. (choice
  1394. (const find-file)
  1395. (const find-file-other-window)
  1396. (const find-file-other-frame)))
  1397. (cons (const wl)
  1398. (choice
  1399. (const wl)
  1400. (const wl-other-frame)))))
  1401. (defcustom org-display-internal-link-with-indirect-buffer nil
  1402. "Non-nil means use indirect buffer to display infile links.
  1403. Activating internal links (from one location in a file to another location
  1404. in the same file) normally just jumps to the location. When the link is
  1405. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1406. is displayed in
  1407. another window. When this option is set, the other window actually displays
  1408. an indirect buffer clone of the current buffer, to avoid any visibility
  1409. changes to the current buffer."
  1410. :group 'org-link-follow
  1411. :type 'boolean)
  1412. (defcustom org-open-non-existing-files nil
  1413. "Non-nil means `org-open-file' will open non-existing files.
  1414. When nil, an error will be generated.
  1415. This variable applies only to external applications because they
  1416. might choke on non-existing files. If the link is to a file that
  1417. will be opened in Emacs, the variable is ignored."
  1418. :group 'org-link-follow
  1419. :type 'boolean)
  1420. (defcustom org-open-directory-means-index-dot-org nil
  1421. "Non-nil means a link to a directory really means to index.org.
  1422. When nil, following a directory link will run dired or open a finder/explorer
  1423. window on that directory."
  1424. :group 'org-link-follow
  1425. :type 'boolean)
  1426. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1427. "Function and arguments to call for following mailto links.
  1428. This is a list with the first element being a Lisp function, and the
  1429. remaining elements being arguments to the function. In string arguments,
  1430. %a will be replaced by the address, and %s will be replaced by the subject
  1431. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1432. :group 'org-link-follow
  1433. :type '(choice
  1434. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1435. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1436. (const :tag "message-mail" (message-mail "%a" "%s"))
  1437. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1438. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1439. "Non-nil means ask for confirmation before executing shell links.
  1440. Shell links can be dangerous: just think about a link
  1441. [[shell:rm -rf ~/*][Google Search]]
  1442. This link would show up in your Org-mode document as \"Google Search\",
  1443. but really it would remove your entire home directory.
  1444. Therefore we advise against setting this variable to nil.
  1445. Just change it to `y-or-n-p' if you want to confirm with a
  1446. single keystroke rather than having to type \"yes\"."
  1447. :group 'org-link-follow
  1448. :type '(choice
  1449. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1450. (const :tag "with y-or-n (faster)" y-or-n-p)
  1451. (const :tag "no confirmation (dangerous)" nil)))
  1452. (put 'org-confirm-shell-link-function
  1453. 'safe-local-variable
  1454. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1455. (defcustom org-confirm-shell-link-not-regexp ""
  1456. "A regexp to skip confirmation for shell links."
  1457. :group 'org-link-follow
  1458. :version "24.1"
  1459. :type 'regexp)
  1460. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1461. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1462. Elisp links can be dangerous: just think about a link
  1463. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1464. This link would show up in your Org-mode document as \"Google Search\",
  1465. but really it would remove your entire home directory.
  1466. Therefore we advise against setting this variable to nil.
  1467. Just change it to `y-or-n-p' if you want to confirm with a
  1468. single keystroke rather than having to type \"yes\"."
  1469. :group 'org-link-follow
  1470. :type '(choice
  1471. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1472. (const :tag "with y-or-n (faster)" y-or-n-p)
  1473. (const :tag "no confirmation (dangerous)" nil)))
  1474. (put 'org-confirm-shell-link-function
  1475. 'safe-local-variable
  1476. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1477. (defcustom org-confirm-elisp-link-not-regexp ""
  1478. "A regexp to skip confirmation for Elisp links."
  1479. :group 'org-link-follow
  1480. :version "24.1"
  1481. :type 'regexp)
  1482. (defconst org-file-apps-defaults-gnu
  1483. '((remote . emacs)
  1484. (system . mailcap)
  1485. (t . mailcap))
  1486. "Default file applications on a UNIX or GNU/Linux system.
  1487. See `org-file-apps'.")
  1488. (defconst org-file-apps-defaults-macosx
  1489. '((remote . emacs)
  1490. (t . "open %s")
  1491. (system . "open %s")
  1492. ("ps.gz" . "gv %s")
  1493. ("eps.gz" . "gv %s")
  1494. ("dvi" . "xdvi %s")
  1495. ("fig" . "xfig %s"))
  1496. "Default file applications on a MacOS X system.
  1497. The system \"open\" is known as a default, but we use X11 applications
  1498. for some files for which the OS does not have a good default.
  1499. See `org-file-apps'.")
  1500. (defconst org-file-apps-defaults-windowsnt
  1501. (list
  1502. '(remote . emacs)
  1503. (cons t
  1504. (list (if (featurep 'xemacs)
  1505. 'mswindows-shell-execute
  1506. 'w32-shell-execute)
  1507. "open" 'file))
  1508. (cons 'system
  1509. (list (if (featurep 'xemacs)
  1510. 'mswindows-shell-execute
  1511. 'w32-shell-execute)
  1512. "open" 'file)))
  1513. "Default file applications on a Windows NT system.
  1514. The system \"open\" is used for most files.
  1515. See `org-file-apps'.")
  1516. (defcustom org-file-apps
  1517. '(
  1518. (auto-mode . emacs)
  1519. ("\\.mm\\'" . default)
  1520. ("\\.x?html?\\'" . default)
  1521. ("\\.pdf\\'" . default)
  1522. )
  1523. "External applications for opening `file:path' items in a document.
  1524. Org-mode uses system defaults for different file types, but
  1525. you can use this variable to set the application for a given file
  1526. extension. The entries in this list are cons cells where the car identifies
  1527. files and the cdr the corresponding command. Possible values for the
  1528. file identifier are
  1529. \"string\" A string as a file identifier can be interpreted in different
  1530. ways, depending on its contents:
  1531. - Alphanumeric characters only:
  1532. Match links with this file extension.
  1533. Example: (\"pdf\" . \"evince %s\")
  1534. to open PDFs with evince.
  1535. - Regular expression: Match links where the
  1536. filename matches the regexp. If you want to
  1537. use groups here, use shy groups.
  1538. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1539. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1540. to open *.html and *.xhtml with firefox.
  1541. - Regular expression which contains (non-shy) groups:
  1542. Match links where the whole link, including \"::\", and
  1543. anything after that, matches the regexp.
  1544. In a custom command string, %1, %2, etc. are replaced with
  1545. the parts of the link that were matched by the groups.
  1546. For backwards compatibility, if a command string is given
  1547. that does not use any of the group matches, this case is
  1548. handled identically to the second one (i.e. match against
  1549. file name only).
  1550. In a custom lisp form, you can access the group matches with
  1551. (match-string n link).
  1552. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1553. to open [[file:document.pdf::5]] with evince at page 5.
  1554. `directory' Matches a directory
  1555. `remote' Matches a remote file, accessible through tramp or efs.
  1556. Remote files most likely should be visited through Emacs
  1557. because external applications cannot handle such paths.
  1558. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1559. so all files Emacs knows how to handle. Using this with
  1560. command `emacs' will open most files in Emacs. Beware that this
  1561. will also open html files inside Emacs, unless you add
  1562. (\"html\" . default) to the list as well.
  1563. t Default for files not matched by any of the other options.
  1564. `system' The system command to open files, like `open' on Windows
  1565. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1566. that will be selected if you call `C-c C-o' with a double
  1567. \\[universal-argument] \\[universal-argument] prefix.
  1568. Possible values for the command are:
  1569. `emacs' The file will be visited by the current Emacs process.
  1570. `default' Use the default application for this file type, which is the
  1571. association for t in the list, most likely in the system-specific
  1572. part.
  1573. This can be used to overrule an unwanted setting in the
  1574. system-specific variable.
  1575. `system' Use the system command for opening files, like \"open\".
  1576. This command is specified by the entry whose car is `system'.
  1577. Most likely, the system-specific version of this variable
  1578. does define this command, but you can overrule/replace it
  1579. here.
  1580. string A command to be executed by a shell; %s will be replaced
  1581. by the path to the file.
  1582. sexp A Lisp form which will be evaluated. The file path will
  1583. be available in the Lisp variable `file'.
  1584. For more examples, see the system specific constants
  1585. `org-file-apps-defaults-macosx'
  1586. `org-file-apps-defaults-windowsnt'
  1587. `org-file-apps-defaults-gnu'."
  1588. :group 'org-link-follow
  1589. :type '(repeat
  1590. (cons (choice :value ""
  1591. (string :tag "Extension")
  1592. (const :tag "System command to open files" system)
  1593. (const :tag "Default for unrecognized files" t)
  1594. (const :tag "Remote file" remote)
  1595. (const :tag "Links to a directory" directory)
  1596. (const :tag "Any files that have Emacs modes"
  1597. auto-mode))
  1598. (choice :value ""
  1599. (const :tag "Visit with Emacs" emacs)
  1600. (const :tag "Use default" default)
  1601. (const :tag "Use the system command" system)
  1602. (string :tag "Command")
  1603. (sexp :tag "Lisp form")))))
  1604. (defgroup org-refile nil
  1605. "Options concerning refiling entries in Org-mode."
  1606. :tag "Org Refile"
  1607. :group 'org)
  1608. (defcustom org-directory "~/org"
  1609. "Directory with org files.
  1610. This is just a default location to look for Org files. There is no need
  1611. at all to put your files into this directory. It is only used in the
  1612. following situations:
  1613. 1. When a remember template specifies a target file that is not an
  1614. absolute path. The path will then be interpreted relative to
  1615. `org-directory'
  1616. 2. When a remember note is filed away in an interactive way (when exiting the
  1617. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1618. with `org-directory' as the default path."
  1619. :group 'org-refile
  1620. :group 'org-remember
  1621. :type 'directory)
  1622. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1623. "Default target for storing notes.
  1624. Used as a fall back file for org-remember.el and org-capture.el, for
  1625. templates that do not specify a target file."
  1626. :group 'org-refile
  1627. :group 'org-remember
  1628. :type '(choice
  1629. (const :tag "Default from remember-data-file" nil)
  1630. file))
  1631. (defcustom org-goto-interface 'outline
  1632. "The default interface to be used for `org-goto'.
  1633. Allowed values are:
  1634. outline The interface shows an outline of the relevant file
  1635. and the correct heading is found by moving through
  1636. the outline or by searching with incremental search.
  1637. outline-path-completion Headlines in the current buffer are offered via
  1638. completion. This is the interface also used by
  1639. the refile command."
  1640. :group 'org-refile
  1641. :type '(choice
  1642. (const :tag "Outline" outline)
  1643. (const :tag "Outline-path-completion" outline-path-completion)))
  1644. (defcustom org-goto-max-level 5
  1645. "Maximum target level when running `org-goto' with refile interface."
  1646. :group 'org-refile
  1647. :type 'integer)
  1648. (defcustom org-reverse-note-order nil
  1649. "Non-nil means store new notes at the beginning of a file or entry.
  1650. When nil, new notes will be filed to the end of a file or entry.
  1651. This can also be a list with cons cells of regular expressions that
  1652. are matched against file names, and values."
  1653. :group 'org-remember
  1654. :group 'org-refile
  1655. :type '(choice
  1656. (const :tag "Reverse always" t)
  1657. (const :tag "Reverse never" nil)
  1658. (repeat :tag "By file name regexp"
  1659. (cons regexp boolean))))
  1660. (defcustom org-log-refile nil
  1661. "Information to record when a task is refiled.
  1662. Possible values are:
  1663. nil Don't add anything
  1664. time Add a time stamp to the task
  1665. note Prompt for a note and add it with template `org-log-note-headings'
  1666. This option can also be set with on a per-file-basis with
  1667. #+STARTUP: nologrefile
  1668. #+STARTUP: logrefile
  1669. #+STARTUP: lognoterefile
  1670. You can have local logging settings for a subtree by setting the LOGGING
  1671. property to one or more of these keywords.
  1672. When bulk-refiling from the agenda, the value `note' is forbidden and
  1673. will temporarily be changed to `time'."
  1674. :group 'org-refile
  1675. :group 'org-progress
  1676. :version "24.1"
  1677. :type '(choice
  1678. (const :tag "No logging" nil)
  1679. (const :tag "Record timestamp" time)
  1680. (const :tag "Record timestamp with note." note)))
  1681. (defcustom org-refile-targets nil
  1682. "Targets for refiling entries with \\[org-refile].
  1683. This is a list of cons cells. Each cell contains:
  1684. - a specification of the files to be considered, either a list of files,
  1685. or a symbol whose function or variable value will be used to retrieve
  1686. a file name or a list of file names. If you use `org-agenda-files' for
  1687. that, all agenda files will be scanned for targets. Nil means consider
  1688. headings in the current buffer.
  1689. - A specification of how to find candidate refile targets. This may be
  1690. any of:
  1691. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1692. This tag has to be present in all target headlines, inheritance will
  1693. not be considered.
  1694. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1695. todo keyword.
  1696. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1697. headlines that are refiling targets.
  1698. - a cons cell (:level . N). Any headline of level N is considered a target.
  1699. Note that, when `org-odd-levels-only' is set, level corresponds to
  1700. order in hierarchy, not to the number of stars.
  1701. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1702. Note that, when `org-odd-levels-only' is set, level corresponds to
  1703. order in hierarchy, not to the number of stars.
  1704. Each element of this list generates a set of possible targets.
  1705. The union of these sets is presented (with completion) to
  1706. the user by `org-refile'.
  1707. You can set the variable `org-refile-target-verify-function' to a function
  1708. to verify each headline found by the simple criteria above.
  1709. When this variable is nil, all top-level headlines in the current buffer
  1710. are used, equivalent to the value `((nil . (:level . 1))'."
  1711. :group 'org-refile
  1712. :type '(repeat
  1713. (cons
  1714. (choice :value org-agenda-files
  1715. (const :tag "All agenda files" org-agenda-files)
  1716. (const :tag "Current buffer" nil)
  1717. (function) (variable) (file))
  1718. (choice :tag "Identify target headline by"
  1719. (cons :tag "Specific tag" (const :value :tag) (string))
  1720. (cons :tag "TODO keyword" (const :value :todo) (string))
  1721. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1722. (cons :tag "Level number" (const :value :level) (integer))
  1723. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1724. (defcustom org-refile-target-verify-function nil
  1725. "Function to verify if the headline at point should be a refile target.
  1726. The function will be called without arguments, with point at the
  1727. beginning of the headline. It should return t and leave point
  1728. where it is if the headline is a valid target for refiling.
  1729. If the target should not be selected, the function must return nil.
  1730. In addition to this, it may move point to a place from where the search
  1731. should be continued. For example, the function may decide that the entire
  1732. subtree of the current entry should be excluded and move point to the end
  1733. of the subtree."
  1734. :group 'org-refile
  1735. :type 'function)
  1736. (defcustom org-refile-use-cache nil
  1737. "Non-nil means cache refile targets to speed up the process.
  1738. The cache for a particular file will be updated automatically when
  1739. the buffer has been killed, or when any of the marker used for flagging
  1740. refile targets no longer points at a live buffer.
  1741. If you have added new entries to a buffer that might themselves be targets,
  1742. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1743. find that easier, `C-u C-u C-u C-c C-w'."
  1744. :group 'org-refile
  1745. :version "24.1"
  1746. :type 'boolean)
  1747. (defcustom org-refile-use-outline-path nil
  1748. "Non-nil means provide refile targets as paths.
  1749. So a level 3 headline will be available as level1/level2/level3.
  1750. When the value is `file', also include the file name (without directory)
  1751. into the path. In this case, you can also stop the completion after
  1752. the file name, to get entries inserted as top level in the file.
  1753. When `full-file-path', include the full file path."
  1754. :group 'org-refile
  1755. :type '(choice
  1756. (const :tag "Not" nil)
  1757. (const :tag "Yes" t)
  1758. (const :tag "Start with file name" file)
  1759. (const :tag "Start with full file path" full-file-path)))
  1760. (defcustom org-outline-path-complete-in-steps t
  1761. "Non-nil means complete the outline path in hierarchical steps.
  1762. When Org-mode uses the refile interface to select an outline path
  1763. \(see variable `org-refile-use-outline-path'), the completion of
  1764. the path can be done is a single go, or if can be done in steps down
  1765. the headline hierarchy. Going in steps is probably the best if you
  1766. do not use a special completion package like `ido' or `icicles'.
  1767. However, when using these packages, going in one step can be very
  1768. fast, while still showing the whole path to the entry."
  1769. :group 'org-refile
  1770. :type 'boolean)
  1771. (defcustom org-refile-allow-creating-parent-nodes nil
  1772. "Non-nil means allow to create new nodes as refile targets.
  1773. New nodes are then created by adding \"/new node name\" to the completion
  1774. of an existing node. When the value of this variable is `confirm',
  1775. new node creation must be confirmed by the user (recommended)
  1776. When nil, the completion must match an existing entry.
  1777. Note that, if the new heading is not seen by the criteria
  1778. listed in `org-refile-targets', multiple instances of the same
  1779. heading would be created by trying again to file under the new
  1780. heading."
  1781. :group 'org-refile
  1782. :type '(choice
  1783. (const :tag "Never" nil)
  1784. (const :tag "Always" t)
  1785. (const :tag "Prompt for confirmation" confirm)))
  1786. (defcustom org-refile-active-region-within-subtree nil
  1787. "Non-nil means also refile active region within a subtree.
  1788. By default `org-refile' doesn't allow refiling regions if they
  1789. don't contain a set of subtrees, but it might be convenient to
  1790. do so sometimes: in that case, the first line of the region is
  1791. converted to a headline before refiling."
  1792. :group 'org-refile
  1793. :version "24.1"
  1794. :type 'boolean)
  1795. (defgroup org-todo nil
  1796. "Options concerning TODO items in Org-mode."
  1797. :tag "Org TODO"
  1798. :group 'org)
  1799. (defgroup org-progress nil
  1800. "Options concerning Progress logging in Org-mode."
  1801. :tag "Org Progress"
  1802. :group 'org-time)
  1803. (defvar org-todo-interpretation-widgets
  1804. '((:tag "Sequence (cycling hits every state)" sequence)
  1805. (:tag "Type (cycling directly to DONE)" type))
  1806. "The available interpretation symbols for customizing `org-todo-keywords'.
  1807. Interested libraries should add to this list.")
  1808. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1809. "List of TODO entry keyword sequences and their interpretation.
  1810. \\<org-mode-map>This is a list of sequences.
  1811. Each sequence starts with a symbol, either `sequence' or `type',
  1812. indicating if the keywords should be interpreted as a sequence of
  1813. action steps, or as different types of TODO items. The first
  1814. keywords are states requiring action - these states will select a headline
  1815. for inclusion into the global TODO list Org-mode produces. If one of
  1816. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1817. signify that no further action is necessary. If \"|\" is not found,
  1818. the last keyword is treated as the only DONE state of the sequence.
  1819. The command \\[org-todo] cycles an entry through these states, and one
  1820. additional state where no keyword is present. For details about this
  1821. cycling, see the manual.
  1822. TODO keywords and interpretation can also be set on a per-file basis with
  1823. the special #+SEQ_TODO and #+TYP_TODO lines.
  1824. Each keyword can optionally specify a character for fast state selection
  1825. \(in combination with the variable `org-use-fast-todo-selection')
  1826. and specifiers for state change logging, using the same syntax
  1827. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1828. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1829. indicates to record a time stamp each time this state is selected.
  1830. Each keyword may also specify if a timestamp or a note should be
  1831. recorded when entering or leaving the state, by adding additional
  1832. characters in the parenthesis after the keyword. This looks like this:
  1833. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1834. record only the time of the state change. With X and Y being either
  1835. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1836. Y when leaving the state if and only if the *target* state does not
  1837. define X. You may omit any of the fast-selection key or X or /Y,
  1838. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1839. For backward compatibility, this variable may also be just a list
  1840. of keywords - in this case the interpretation (sequence or type) will be
  1841. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1842. :group 'org-todo
  1843. :group 'org-keywords
  1844. :type '(choice
  1845. (repeat :tag "Old syntax, just keywords"
  1846. (string :tag "Keyword"))
  1847. (repeat :tag "New syntax"
  1848. (cons
  1849. (choice
  1850. :tag "Interpretation"
  1851. ;;Quick and dirty way to see
  1852. ;;`org-todo-interpretations'. This takes the
  1853. ;;place of item arguments
  1854. :convert-widget
  1855. (lambda (widget)
  1856. (widget-put widget
  1857. :args (mapcar
  1858. #'(lambda (x)
  1859. (widget-convert
  1860. (cons 'const x)))
  1861. org-todo-interpretation-widgets))
  1862. widget))
  1863. (repeat
  1864. (string :tag "Keyword"))))))
  1865. (defvar org-todo-keywords-1 nil
  1866. "All TODO and DONE keywords active in a buffer.")
  1867. (make-variable-buffer-local 'org-todo-keywords-1)
  1868. (defvar org-todo-keywords-for-agenda nil)
  1869. (defvar org-done-keywords-for-agenda nil)
  1870. (defvar org-drawers-for-agenda nil)
  1871. (defvar org-todo-keyword-alist-for-agenda nil)
  1872. (defvar org-tag-alist-for-agenda nil)
  1873. (defvar org-agenda-contributing-files nil)
  1874. (defvar org-not-done-keywords nil)
  1875. (make-variable-buffer-local 'org-not-done-keywords)
  1876. (defvar org-done-keywords nil)
  1877. (make-variable-buffer-local 'org-done-keywords)
  1878. (defvar org-todo-heads nil)
  1879. (make-variable-buffer-local 'org-todo-heads)
  1880. (defvar org-todo-sets nil)
  1881. (make-variable-buffer-local 'org-todo-sets)
  1882. (defvar org-todo-log-states nil)
  1883. (make-variable-buffer-local 'org-todo-log-states)
  1884. (defvar org-todo-kwd-alist nil)
  1885. (make-variable-buffer-local 'org-todo-kwd-alist)
  1886. (defvar org-todo-key-alist nil)
  1887. (make-variable-buffer-local 'org-todo-key-alist)
  1888. (defvar org-todo-key-trigger nil)
  1889. (make-variable-buffer-local 'org-todo-key-trigger)
  1890. (defcustom org-todo-interpretation 'sequence
  1891. "Controls how TODO keywords are interpreted.
  1892. This variable is in principle obsolete and is only used for
  1893. backward compatibility, if the interpretation of todo keywords is
  1894. not given already in `org-todo-keywords'. See that variable for
  1895. more information."
  1896. :group 'org-todo
  1897. :group 'org-keywords
  1898. :type '(choice (const sequence)
  1899. (const type)))
  1900. (defcustom org-use-fast-todo-selection t
  1901. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1902. This variable describes if and under what circumstances the cycling
  1903. mechanism for TODO keywords will be replaced by a single-key, direct
  1904. selection scheme.
  1905. When nil, fast selection is never used.
  1906. When the symbol `prefix', it will be used when `org-todo' is called with
  1907. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1908. in an agenda buffer.
  1909. When t, fast selection is used by default. In this case, the prefix
  1910. argument forces cycling instead.
  1911. In all cases, the special interface is only used if access keys have actually
  1912. been assigned by the user, i.e. if keywords in the configuration are followed
  1913. by a letter in parenthesis, like TODO(t)."
  1914. :group 'org-todo
  1915. :type '(choice
  1916. (const :tag "Never" nil)
  1917. (const :tag "By default" t)
  1918. (const :tag "Only with C-u C-c C-t" prefix)))
  1919. (defcustom org-provide-todo-statistics t
  1920. "Non-nil means update todo statistics after insert and toggle.
  1921. ALL-HEADLINES means update todo statistics by including headlines
  1922. with no TODO keyword as well, counting them as not done.
  1923. A list of TODO keywords means the same, but skip keywords that are
  1924. not in this list.
  1925. When this is set, todo statistics is updated in the parent of the
  1926. current entry each time a todo state is changed."
  1927. :group 'org-todo
  1928. :type '(choice
  1929. (const :tag "Yes, only for TODO entries" t)
  1930. (const :tag "Yes, including all entries" 'all-headlines)
  1931. (repeat :tag "Yes, for TODOs in this list"
  1932. (string :tag "TODO keyword"))
  1933. (other :tag "No TODO statistics" nil)))
  1934. (defcustom org-hierarchical-todo-statistics t
  1935. "Non-nil means TODO statistics covers just direct children.
  1936. When nil, all entries in the subtree are considered.
  1937. This has only an effect if `org-provide-todo-statistics' is set.
  1938. To set this to nil for only a single subtree, use a COOKIE_DATA
  1939. property and include the word \"recursive\" into the value."
  1940. :group 'org-todo
  1941. :type 'boolean)
  1942. (defcustom org-after-todo-state-change-hook nil
  1943. "Hook which is run after the state of a TODO item was changed.
  1944. The new state (a string with a TODO keyword, or nil) is available in the
  1945. Lisp variable `org-state'."
  1946. :group 'org-todo
  1947. :type 'hook)
  1948. (defvar org-blocker-hook nil
  1949. "Hook for functions that are allowed to block a state change.
  1950. Each function gets as its single argument a property list, see
  1951. `org-trigger-hook' for more information about this list.
  1952. If any of the functions in this hook returns nil, the state change
  1953. is blocked.")
  1954. (defvar org-trigger-hook nil
  1955. "Hook for functions that are triggered by a state change.
  1956. Each function gets as its single argument a property list with at least
  1957. the following elements:
  1958. (:type type-of-change :position pos-at-entry-start
  1959. :from old-state :to new-state)
  1960. Depending on the type, more properties may be present.
  1961. This mechanism is currently implemented for:
  1962. TODO state changes
  1963. ------------------
  1964. :type todo-state-change
  1965. :from previous state (keyword as a string), or nil, or a symbol
  1966. 'todo' or 'done', to indicate the general type of state.
  1967. :to new state, like in :from")
  1968. (defcustom org-enforce-todo-dependencies nil
  1969. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1970. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1971. be blocked if any prior sibling is not yet done.
  1972. Finally, if the parent is blocked because of ordered siblings of its own,
  1973. the child will also be blocked."
  1974. :set (lambda (var val)
  1975. (set var val)
  1976. (if val
  1977. (add-hook 'org-blocker-hook
  1978. 'org-block-todo-from-children-or-siblings-or-parent)
  1979. (remove-hook 'org-blocker-hook
  1980. 'org-block-todo-from-children-or-siblings-or-parent)))
  1981. :group 'org-todo
  1982. :type 'boolean)
  1983. (defcustom org-enforce-todo-checkbox-dependencies nil
  1984. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1985. When this is nil, checkboxes have no influence on switching TODO states.
  1986. When non-nil, you first need to check off all check boxes before the TODO
  1987. entry can be switched to DONE.
  1988. This variable needs to be set before org.el is loaded, and you need to
  1989. restart Emacs after a change to make the change effective. The only way
  1990. to change is while Emacs is running is through the customize interface."
  1991. :set (lambda (var val)
  1992. (set var val)
  1993. (if val
  1994. (add-hook 'org-blocker-hook
  1995. 'org-block-todo-from-checkboxes)
  1996. (remove-hook 'org-blocker-hook
  1997. 'org-block-todo-from-checkboxes)))
  1998. :group 'org-todo
  1999. :type 'boolean)
  2000. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2001. "Non-nil means inserting a TODO heading is treated as state change.
  2002. So when the command \\[org-insert-todo-heading] is used, state change
  2003. logging will apply if appropriate. When nil, the new TODO item will
  2004. be inserted directly, and no logging will take place."
  2005. :group 'org-todo
  2006. :type 'boolean)
  2007. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2008. "Non-nil means switching TODO states with S-cursor counts as state change.
  2009. This is the default behavior. However, setting this to nil allows a
  2010. convenient way to select a TODO state and bypass any logging associated
  2011. with that."
  2012. :group 'org-todo
  2013. :type 'boolean)
  2014. (defcustom org-todo-state-tags-triggers nil
  2015. "Tag changes that should be triggered by TODO state changes.
  2016. This is a list. Each entry is
  2017. (state-change (tag . flag) .......)
  2018. State-change can be a string with a state, and empty string to indicate the
  2019. state that has no TODO keyword, or it can be one of the symbols `todo'
  2020. or `done', meaning any not-done or done state, respectively."
  2021. :group 'org-todo
  2022. :group 'org-tags
  2023. :type '(repeat
  2024. (cons (choice :tag "When changing to"
  2025. (const :tag "Not-done state" todo)
  2026. (const :tag "Done state" done)
  2027. (string :tag "State"))
  2028. (repeat
  2029. (cons :tag "Tag action"
  2030. (string :tag "Tag")
  2031. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2032. (defcustom org-log-done nil
  2033. "Information to record when a task moves to the DONE state.
  2034. Possible values are:
  2035. nil Don't add anything, just change the keyword
  2036. time Add a time stamp to the task
  2037. note Prompt for a note and add it with template `org-log-note-headings'
  2038. This option can also be set with on a per-file-basis with
  2039. #+STARTUP: nologdone
  2040. #+STARTUP: logdone
  2041. #+STARTUP: lognotedone
  2042. You can have local logging settings for a subtree by setting the LOGGING
  2043. property to one or more of these keywords."
  2044. :group 'org-todo
  2045. :group 'org-progress
  2046. :type '(choice
  2047. (const :tag "No logging" nil)
  2048. (const :tag "Record CLOSED timestamp" time)
  2049. (const :tag "Record CLOSED timestamp with note." note)))
  2050. ;; Normalize old uses of org-log-done.
  2051. (cond
  2052. ((eq org-log-done t) (setq org-log-done 'time))
  2053. ((and (listp org-log-done) (memq 'done org-log-done))
  2054. (setq org-log-done 'note)))
  2055. (defcustom org-log-reschedule nil
  2056. "Information to record when the scheduling date of a tasks is modified.
  2057. Possible values are:
  2058. nil Don't add anything, just change the date
  2059. time Add a time stamp to the task
  2060. note Prompt for a note and add it with template `org-log-note-headings'
  2061. This option can also be set with on a per-file-basis with
  2062. #+STARTUP: nologreschedule
  2063. #+STARTUP: logreschedule
  2064. #+STARTUP: lognotereschedule"
  2065. :group 'org-todo
  2066. :group 'org-progress
  2067. :type '(choice
  2068. (const :tag "No logging" nil)
  2069. (const :tag "Record timestamp" time)
  2070. (const :tag "Record timestamp with note." note)))
  2071. (defcustom org-log-redeadline nil
  2072. "Information to record when the deadline date of a tasks is modified.
  2073. Possible values are:
  2074. nil Don't add anything, just change the date
  2075. time Add a time stamp to the task
  2076. note Prompt for a note and add it with template `org-log-note-headings'
  2077. This option can also be set with on a per-file-basis with
  2078. #+STARTUP: nologredeadline
  2079. #+STARTUP: logredeadline
  2080. #+STARTUP: lognoteredeadline
  2081. You can have local logging settings for a subtree by setting the LOGGING
  2082. property to one or more of these keywords."
  2083. :group 'org-todo
  2084. :group 'org-progress
  2085. :type '(choice
  2086. (const :tag "No logging" nil)
  2087. (const :tag "Record timestamp" time)
  2088. (const :tag "Record timestamp with note." note)))
  2089. (defcustom org-log-note-clock-out nil
  2090. "Non-nil means record a note when clocking out of an item.
  2091. This can also be configured on a per-file basis by adding one of
  2092. the following lines anywhere in the buffer:
  2093. #+STARTUP: lognoteclock-out
  2094. #+STARTUP: nolognoteclock-out"
  2095. :group 'org-todo
  2096. :group 'org-progress
  2097. :type 'boolean)
  2098. (defcustom org-log-done-with-time t
  2099. "Non-nil means the CLOSED time stamp will contain date and time.
  2100. When nil, only the date will be recorded."
  2101. :group 'org-progress
  2102. :type 'boolean)
  2103. (defcustom org-log-note-headings
  2104. '((done . "CLOSING NOTE %t")
  2105. (state . "State %-12s from %-12S %t")
  2106. (note . "Note taken on %t")
  2107. (reschedule . "Rescheduled from %S on %t")
  2108. (delschedule . "Not scheduled, was %S on %t")
  2109. (redeadline . "New deadline from %S on %t")
  2110. (deldeadline . "Removed deadline, was %S on %t")
  2111. (refile . "Refiled on %t")
  2112. (clock-out . ""))
  2113. "Headings for notes added to entries.
  2114. The value is an alist, with the car being a symbol indicating the note
  2115. context, and the cdr is the heading to be used. The heading may also be the
  2116. empty string.
  2117. %t in the heading will be replaced by a time stamp.
  2118. %T will be an active time stamp instead the default inactive one
  2119. %d will be replaced by a short-format time stamp.
  2120. %D will be replaced by an active short-format time stamp.
  2121. %s will be replaced by the new TODO state, in double quotes.
  2122. %S will be replaced by the old TODO state, in double quotes.
  2123. %u will be replaced by the user name.
  2124. %U will be replaced by the full user name.
  2125. In fact, it is not a good idea to change the `state' entry, because
  2126. agenda log mode depends on the format of these entries."
  2127. :group 'org-todo
  2128. :group 'org-progress
  2129. :type '(list :greedy t
  2130. (cons (const :tag "Heading when closing an item" done) string)
  2131. (cons (const :tag
  2132. "Heading when changing todo state (todo sequence only)"
  2133. state) string)
  2134. (cons (const :tag "Heading when just taking a note" note) string)
  2135. (cons (const :tag "Heading when clocking out" clock-out) string)
  2136. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2137. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2138. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2139. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2140. (cons (const :tag "Heading when refiling" refile) string)))
  2141. (unless (assq 'note org-log-note-headings)
  2142. (push '(note . "%t") org-log-note-headings))
  2143. (defcustom org-log-into-drawer nil
  2144. "Non-nil means insert state change notes and time stamps into a drawer.
  2145. When nil, state changes notes will be inserted after the headline and
  2146. any scheduling and clock lines, but not inside a drawer.
  2147. The value of this variable should be the name of the drawer to use.
  2148. LOGBOOK is proposed as the default drawer for this purpose, you can
  2149. also set this to a string to define the drawer of your choice.
  2150. A value of t is also allowed, representing \"LOGBOOK\".
  2151. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2152. will be ignored.
  2153. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2154. a subtree."
  2155. :group 'org-todo
  2156. :group 'org-progress
  2157. :type '(choice
  2158. (const :tag "Not into a drawer" nil)
  2159. (const :tag "LOGBOOK" t)
  2160. (string :tag "Other")))
  2161. (if (fboundp 'defvaralias)
  2162. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2163. (defun org-log-into-drawer ()
  2164. "Return the value of `org-log-into-drawer', but let properties overrule.
  2165. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2166. used instead of the default value."
  2167. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2168. (cond
  2169. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2170. ((equal p "t") "LOGBOOK")
  2171. (t p))))
  2172. (defcustom org-log-state-notes-insert-after-drawers nil
  2173. "Non-nil means insert state change notes after any drawers in entry.
  2174. Only the drawers that *immediately* follow the headline and the
  2175. deadline/scheduled line are skipped.
  2176. When nil, insert notes right after the heading and perhaps the line
  2177. with deadline/scheduling if present.
  2178. This variable will have no effect if `org-log-into-drawer' is
  2179. set."
  2180. :group 'org-todo
  2181. :group 'org-progress
  2182. :type 'boolean)
  2183. (defcustom org-log-states-order-reversed t
  2184. "Non-nil means the latest state note will be directly after heading.
  2185. When nil, the state change notes will be ordered according to time."
  2186. :group 'org-todo
  2187. :group 'org-progress
  2188. :type 'boolean)
  2189. (defcustom org-todo-repeat-to-state nil
  2190. "The TODO state to which a repeater should return the repeating task.
  2191. By default this is the first task in a TODO sequence, or the previous state
  2192. in a TODO_TYP set. But you can specify another task here.
  2193. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2194. :group 'org-todo
  2195. :version "24.1"
  2196. :type '(choice (const :tag "Head of sequence" nil)
  2197. (string :tag "Specific state")))
  2198. (defcustom org-log-repeat 'time
  2199. "Non-nil means record moving through the DONE state when triggering repeat.
  2200. An auto-repeating task is immediately switched back to TODO when
  2201. marked DONE. If you are not logging state changes (by adding \"@\"
  2202. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2203. record a closing note, there will be no record of the task moving
  2204. through DONE. This variable forces taking a note anyway.
  2205. nil Don't force a record
  2206. time Record a time stamp
  2207. note Record a note
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: logrepeat
  2210. #+STARTUP: lognoterepeat
  2211. #+STARTUP: nologrepeat
  2212. You can have local logging settings for a subtree by setting the LOGGING
  2213. property to one or more of these keywords."
  2214. :group 'org-todo
  2215. :group 'org-progress
  2216. :type '(choice
  2217. (const :tag "Don't force a record" nil)
  2218. (const :tag "Force recording the DONE state" time)
  2219. (const :tag "Force recording a note with the DONE state" note)))
  2220. (defgroup org-priorities nil
  2221. "Priorities in Org-mode."
  2222. :tag "Org Priorities"
  2223. :group 'org-todo)
  2224. (defcustom org-enable-priority-commands t
  2225. "Non-nil means priority commands are active.
  2226. When nil, these commands will be disabled, so that you never accidentally
  2227. set a priority."
  2228. :group 'org-priorities
  2229. :type 'boolean)
  2230. (defcustom org-highest-priority ?A
  2231. "The highest priority of TODO items. A character like ?A, ?B etc.
  2232. Must have a smaller ASCII number than `org-lowest-priority'."
  2233. :group 'org-priorities
  2234. :type 'character)
  2235. (defcustom org-lowest-priority ?C
  2236. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2237. Must have a larger ASCII number than `org-highest-priority'."
  2238. :group 'org-priorities
  2239. :type 'character)
  2240. (defcustom org-default-priority ?B
  2241. "The default priority of TODO items.
  2242. This is the priority an item gets if no explicit priority is given.
  2243. When starting to cycle on an empty priority the first step in the cycle
  2244. depends on `org-priority-start-cycle-with-default'. The resulting first
  2245. step priority must not exceed the range from `org-highest-priority' to
  2246. `org-lowest-priority' which means that `org-default-priority' has to be
  2247. in this range exclusive or inclusive the range boundaries. Else the
  2248. first step refuses to set the default and the second will fall back
  2249. to (depending on the command used) the highest or lowest priority."
  2250. :group 'org-priorities
  2251. :type 'character)
  2252. (defcustom org-priority-start-cycle-with-default t
  2253. "Non-nil means start with default priority when starting to cycle.
  2254. When this is nil, the first step in the cycle will be (depending on the
  2255. command used) one higher or lower than the default priority.
  2256. See also `org-default-priority'."
  2257. :group 'org-priorities
  2258. :type 'boolean)
  2259. (defcustom org-get-priority-function nil
  2260. "Function to extract the priority from a string.
  2261. The string is normally the headline. If this is nil Org computes the
  2262. priority from the priority cookie like [#A] in the headline. It returns
  2263. an integer, increasing by 1000 for each priority level.
  2264. The user can set a different function here, which should take a string
  2265. as an argument and return the numeric priority."
  2266. :group 'org-priorities
  2267. :version "24.1"
  2268. :type 'function)
  2269. (defgroup org-time nil
  2270. "Options concerning time stamps and deadlines in Org-mode."
  2271. :tag "Org Time"
  2272. :group 'org)
  2273. (defcustom org-insert-labeled-timestamps-at-point nil
  2274. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2275. When nil, these labeled time stamps are forces into the second line of an
  2276. entry, just after the headline. When scheduling from the global TODO list,
  2277. the time stamp will always be forced into the second line."
  2278. :group 'org-time
  2279. :type 'boolean)
  2280. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2281. "Formats for `format-time-string' which are used for time stamps.
  2282. It is not recommended to change this constant.")
  2283. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2284. "Number of minutes to round time stamps to.
  2285. These are two values, the first applies when first creating a time stamp.
  2286. The second applies when changing it with the commands `S-up' and `S-down'.
  2287. When changing the time stamp, this means that it will change in steps
  2288. of N minutes, as given by the second value.
  2289. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2290. numbers should be factors of 60, so for example 5, 10, 15.
  2291. When this is larger than 1, you can still force an exact time stamp by using
  2292. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2293. and by using a prefix arg to `S-up/down' to specify the exact number
  2294. of minutes to shift."
  2295. :group 'org-time
  2296. :get #'(lambda (var) ; Make sure both elements are there
  2297. (if (integerp (default-value var))
  2298. (list (default-value var) 5)
  2299. (default-value var)))
  2300. :type '(list
  2301. (integer :tag "when inserting times")
  2302. (integer :tag "when modifying times")))
  2303. ;; Normalize old customizations of this variable.
  2304. (when (integerp org-time-stamp-rounding-minutes)
  2305. (setq org-time-stamp-rounding-minutes
  2306. (list org-time-stamp-rounding-minutes
  2307. org-time-stamp-rounding-minutes)))
  2308. (defcustom org-display-custom-times nil
  2309. "Non-nil means overlay custom formats over all time stamps.
  2310. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2311. To turn this on on a per-file basis, insert anywhere in the file:
  2312. #+STARTUP: customtime"
  2313. :group 'org-time
  2314. :set 'set-default
  2315. :type 'sexp)
  2316. (make-variable-buffer-local 'org-display-custom-times)
  2317. (defcustom org-time-stamp-custom-formats
  2318. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2319. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2320. These are overlaid over the default ISO format if the variable
  2321. `org-display-custom-times' is set. Time like %H:%M should be at the
  2322. end of the second format. The custom formats are also honored by export
  2323. commands, if custom time display is turned on at the time of export."
  2324. :group 'org-time
  2325. :type 'sexp)
  2326. (defun org-time-stamp-format (&optional long inactive)
  2327. "Get the right format for a time string."
  2328. (let ((f (if long (cdr org-time-stamp-formats)
  2329. (car org-time-stamp-formats))))
  2330. (if inactive
  2331. (concat "[" (substring f 1 -1) "]")
  2332. f)))
  2333. (defcustom org-time-clocksum-format "%d:%02d"
  2334. "The format string used when creating CLOCKSUM lines.
  2335. This is also used when org-mode generates a time duration."
  2336. :group 'org-time
  2337. :type 'string)
  2338. (defcustom org-time-clocksum-use-fractional nil
  2339. "If non-nil, \\[org-clock-display] uses fractional times.
  2340. org-mode generates a time duration."
  2341. :group 'org-time
  2342. :type 'boolean)
  2343. (defcustom org-time-clocksum-fractional-format "%.2f"
  2344. "The format string used when creating CLOCKSUM lines, or when
  2345. org-mode generates a time duration."
  2346. :group 'org-time
  2347. :type 'string)
  2348. (defcustom org-deadline-warning-days 14
  2349. "No. of days before expiration during which a deadline becomes active.
  2350. This variable governs the display in sparse trees and in the agenda.
  2351. When 0 or negative, it means use this number (the absolute value of it)
  2352. even if a deadline has a different individual lead time specified.
  2353. Custom commands can set this variable in the options section."
  2354. :group 'org-time
  2355. :group 'org-agenda-daily/weekly
  2356. :type 'integer)
  2357. (defcustom org-read-date-prefer-future t
  2358. "Non-nil means assume future for incomplete date input from user.
  2359. This affects the following situations:
  2360. 1. The user gives a month but not a year.
  2361. For example, if it is April and you enter \"feb 2\", this will be read
  2362. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2363. 2. The user gives a day, but no month.
  2364. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2365. read this as the third of *next* month. However, if you enter \"17\",
  2366. it will be considered as *this* month.
  2367. If you set this variable to the symbol `time', then also the following
  2368. will work:
  2369. 3. If the user gives a time, but no day. If the time is before now,
  2370. to will be interpreted as tomorrow.
  2371. Currently none of this works for ISO week specifications.
  2372. When this option is nil, the current day, month and year will always be
  2373. used as defaults.
  2374. See also `org-agenda-jump-prefer-future'."
  2375. :group 'org-time
  2376. :type '(choice
  2377. (const :tag "Never" nil)
  2378. (const :tag "Check month and day" t)
  2379. (const :tag "Check month, day, and time" time)))
  2380. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2381. "Should the agenda jump command prefer the future for incomplete dates?
  2382. The default is to do the same as configured in `org-read-date-prefer-future'.
  2383. But you can also set a deviating value here.
  2384. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2385. :group 'org-agenda
  2386. :group 'org-time
  2387. :version "24.1"
  2388. :type '(choice
  2389. (const :tag "Use org-read-date-prefer-future"
  2390. org-read-date-prefer-future)
  2391. (const :tag "Never" nil)
  2392. (const :tag "Always" t)))
  2393. (defcustom org-read-date-force-compatible-dates t
  2394. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2395. Depending on the system Emacs is running on, certain dates cannot
  2396. be represented with the type used internally to represent time.
  2397. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2398. correctly. Some systems allow for earlier dates, some for later,
  2399. some for both. One way to find out it to insert any date into an
  2400. Org buffer, putting the cursor on the year and hitting S-up and
  2401. S-down to test the range.
  2402. When this variable is set to t, the date/time prompt will not let
  2403. you specify dates outside the 1970-2037 range, so it is certain that
  2404. these dates will work in whatever version of Emacs you are
  2405. running, and also that you can move a file from one Emacs implementation
  2406. to another. WHenever Org is forcing the year for you, it will display
  2407. a message and beep.
  2408. When this variable is nil, Org will check if the date is
  2409. representable in the specific Emacs implementation you are using.
  2410. If not, it will force a year, usually the current year, and beep
  2411. to remind you. Currently this setting is not recommended because
  2412. the likelihood that you will open your Org files in an Emacs that
  2413. has limited date range is not negligible.
  2414. A workaround for this problem is to use diary sexp dates for time
  2415. stamps outside of this range."
  2416. :group 'org-time
  2417. :version "24.1"
  2418. :type 'boolean)
  2419. (defcustom org-read-date-display-live t
  2420. "Non-nil means display current interpretation of date prompt live.
  2421. This display will be in an overlay, in the minibuffer."
  2422. :group 'org-time
  2423. :type 'boolean)
  2424. (defcustom org-read-date-popup-calendar t
  2425. "Non-nil means pop up a calendar when prompting for a date.
  2426. In the calendar, the date can be selected with mouse-1. However, the
  2427. minibuffer will also be active, and you can simply enter the date as well.
  2428. When nil, only the minibuffer will be available."
  2429. :group 'org-time
  2430. :type 'boolean)
  2431. (if (fboundp 'defvaralias)
  2432. (defvaralias 'org-popup-calendar-for-date-prompt
  2433. 'org-read-date-popup-calendar))
  2434. (defcustom org-read-date-minibuffer-setup-hook nil
  2435. "Hook to be used to set up keys for the date/time interface.
  2436. Add key definitions to `minibuffer-local-map', which will be a temporary
  2437. copy."
  2438. :group 'org-time
  2439. :type 'hook)
  2440. (defcustom org-extend-today-until 0
  2441. "The hour when your day really ends. Must be an integer.
  2442. This has influence for the following applications:
  2443. - When switching the agenda to \"today\". It it is still earlier than
  2444. the time given here, the day recognized as TODAY is actually yesterday.
  2445. - When a date is read from the user and it is still before the time given
  2446. here, the current date and time will be assumed to be yesterday, 23:59.
  2447. Also, timestamps inserted in remember templates follow this rule.
  2448. IMPORTANT: This is a feature whose implementation is and likely will
  2449. remain incomplete. Really, it is only here because past midnight seems to
  2450. be the favorite working time of John Wiegley :-)"
  2451. :group 'org-time
  2452. :type 'integer)
  2453. (defcustom org-use-effective-time nil
  2454. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2455. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2456. \"effective time\" of any timestamps between midnight and 8am will be
  2457. 23:59 of the previous day."
  2458. :group 'org-time
  2459. :version "24.1"
  2460. :type 'boolean)
  2461. (defcustom org-edit-timestamp-down-means-later nil
  2462. "Non-nil means S-down will increase the time in a time stamp.
  2463. When nil, S-up will increase."
  2464. :group 'org-time
  2465. :type 'boolean)
  2466. (defcustom org-calendar-follow-timestamp-change t
  2467. "Non-nil means make the calendar window follow timestamp changes.
  2468. When a timestamp is modified and the calendar window is visible, it will be
  2469. moved to the new date."
  2470. :group 'org-time
  2471. :type 'boolean)
  2472. (defgroup org-tags nil
  2473. "Options concerning tags in Org-mode."
  2474. :tag "Org Tags"
  2475. :group 'org)
  2476. (defcustom org-tag-alist nil
  2477. "List of tags allowed in Org-mode files.
  2478. When this list is nil, Org-mode will base TAG input on what is already in the
  2479. buffer.
  2480. The value of this variable is an alist, the car of each entry must be a
  2481. keyword as a string, the cdr may be a character that is used to select
  2482. that tag through the fast-tag-selection interface.
  2483. See the manual for details."
  2484. :group 'org-tags
  2485. :type '(repeat
  2486. (choice
  2487. (cons (string :tag "Tag name")
  2488. (character :tag "Access char"))
  2489. (list :tag "Start radio group"
  2490. (const :startgroup)
  2491. (option (string :tag "Group description")))
  2492. (list :tag "End radio group"
  2493. (const :endgroup)
  2494. (option (string :tag "Group description")))
  2495. (const :tag "New line" (:newline)))))
  2496. (defcustom org-tag-persistent-alist nil
  2497. "List of tags that will always appear in all Org-mode files.
  2498. This is in addition to any in buffer settings or customizations
  2499. of `org-tag-alist'.
  2500. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2501. The value of this variable is an alist, the car of each entry must be a
  2502. keyword as a string, the cdr may be a character that is used to select
  2503. that tag through the fast-tag-selection interface.
  2504. See the manual for details.
  2505. To disable these tags on a per-file basis, insert anywhere in the file:
  2506. #+STARTUP: noptag"
  2507. :group 'org-tags
  2508. :type '(repeat
  2509. (choice
  2510. (cons (string :tag "Tag name")
  2511. (character :tag "Access char"))
  2512. (const :tag "Start radio group" (:startgroup))
  2513. (const :tag "End radio group" (:endgroup))
  2514. (const :tag "New line" (:newline)))))
  2515. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2516. "If non-nil, always offer completion for all tags of all agenda files.
  2517. Instead of customizing this variable directly, you might want to
  2518. set it locally for capture buffers, because there no list of
  2519. tags in that file can be created dynamically (there are none).
  2520. (add-hook 'org-capture-mode-hook
  2521. (lambda ()
  2522. (set (make-local-variable
  2523. 'org-complete-tags-always-offer-all-agenda-tags)
  2524. t)))"
  2525. :group 'org-tags
  2526. :version "24.1"
  2527. :type 'boolean)
  2528. (defvar org-file-tags nil
  2529. "List of tags that can be inherited by all entries in the file.
  2530. The tags will be inherited if the variable `org-use-tag-inheritance'
  2531. says they should be.
  2532. This variable is populated from #+FILETAGS lines.")
  2533. (defcustom org-use-fast-tag-selection 'auto
  2534. "Non-nil means use fast tag selection scheme.
  2535. This is a special interface to select and deselect tags with single keys.
  2536. When nil, fast selection is never used.
  2537. When the symbol `auto', fast selection is used if and only if selection
  2538. characters for tags have been configured, either through the variable
  2539. `org-tag-alist' or through a #+TAGS line in the buffer.
  2540. When t, fast selection is always used and selection keys are assigned
  2541. automatically if necessary."
  2542. :group 'org-tags
  2543. :type '(choice
  2544. (const :tag "Always" t)
  2545. (const :tag "Never" nil)
  2546. (const :tag "When selection characters are configured" 'auto)))
  2547. (defcustom org-fast-tag-selection-single-key nil
  2548. "Non-nil means fast tag selection exits after first change.
  2549. When nil, you have to press RET to exit it.
  2550. During fast tag selection, you can toggle this flag with `C-c'.
  2551. This variable can also have the value `expert'. In this case, the window
  2552. displaying the tags menu is not even shown, until you press C-c again."
  2553. :group 'org-tags
  2554. :type '(choice
  2555. (const :tag "No" nil)
  2556. (const :tag "Yes" t)
  2557. (const :tag "Expert" expert)))
  2558. (defvar org-fast-tag-selection-include-todo nil
  2559. "Non-nil means fast tags selection interface will also offer TODO states.
  2560. This is an undocumented feature, you should not rely on it.")
  2561. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2562. "The column to which tags should be indented in a headline.
  2563. If this number is positive, it specifies the column. If it is negative,
  2564. it means that the tags should be flushright to that column. For example,
  2565. -80 works well for a normal 80 character screen.
  2566. When 0, place tags directly after headline text, with only one space in
  2567. between."
  2568. :group 'org-tags
  2569. :type 'integer)
  2570. (defcustom org-auto-align-tags t
  2571. "Non-nil keeps tags aligned when modifying headlines.
  2572. Some operations (i.e. demoting) change the length of a headline and
  2573. therefore shift the tags around. With this option turned on, after
  2574. each such operation the tags are again aligned to `org-tags-column'."
  2575. :group 'org-tags
  2576. :type 'boolean)
  2577. (defcustom org-use-tag-inheritance t
  2578. "Non-nil means tags in levels apply also for sublevels.
  2579. When nil, only the tags directly given in a specific line apply there.
  2580. This may also be a list of tags that should be inherited, or a regexp that
  2581. matches tags that should be inherited. Additional control is possible
  2582. with the variable `org-tags-exclude-from-inheritance' which gives an
  2583. explicit list of tags to be excluded from inheritance., even if the value of
  2584. `org-use-tag-inheritance' would select it for inheritance.
  2585. If this option is t, a match early-on in a tree can lead to a large
  2586. number of matches in the subtree when constructing the agenda or creating
  2587. a sparse tree. If you only want to see the first match in a tree during
  2588. a search, check out the variable `org-tags-match-list-sublevels'."
  2589. :group 'org-tags
  2590. :type '(choice
  2591. (const :tag "Not" nil)
  2592. (const :tag "Always" t)
  2593. (repeat :tag "Specific tags" (string :tag "Tag"))
  2594. (regexp :tag "Tags matched by regexp")))
  2595. (defcustom org-tags-exclude-from-inheritance nil
  2596. "List of tags that should never be inherited.
  2597. This is a way to exclude a few tags from inheritance. For way to do
  2598. the opposite, to actively allow inheritance for selected tags,
  2599. see the variable `org-use-tag-inheritance'."
  2600. :group 'org-tags
  2601. :type '(repeat (string :tag "Tag")))
  2602. (defun org-tag-inherit-p (tag)
  2603. "Check if TAG is one that should be inherited."
  2604. (cond
  2605. ((member tag org-tags-exclude-from-inheritance) nil)
  2606. ((eq org-use-tag-inheritance t) t)
  2607. ((not org-use-tag-inheritance) nil)
  2608. ((stringp org-use-tag-inheritance)
  2609. (string-match org-use-tag-inheritance tag))
  2610. ((listp org-use-tag-inheritance)
  2611. (member tag org-use-tag-inheritance))
  2612. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2613. (defcustom org-tags-match-list-sublevels t
  2614. "Non-nil means list also sublevels of headlines matching a search.
  2615. This variable applies to tags/property searches, and also to stuck
  2616. projects because this search is based on a tags match as well.
  2617. When set to the symbol `indented', sublevels are indented with
  2618. leading dots.
  2619. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2620. the sublevels of a headline matching a tag search often also match
  2621. the same search. Listing all of them can create very long lists.
  2622. Setting this variable to nil causes subtrees of a match to be skipped.
  2623. This variable is semi-obsolete and probably should always be true. It
  2624. is better to limit inheritance to certain tags using the variables
  2625. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2626. :group 'org-tags
  2627. :type '(choice
  2628. (const :tag "No, don't list them" nil)
  2629. (const :tag "Yes, do list them" t)
  2630. (const :tag "List them, indented with leading dots" indented)))
  2631. (defcustom org-tags-sort-function nil
  2632. "When set, tags are sorted using this function as a comparator."
  2633. :group 'org-tags
  2634. :type '(choice
  2635. (const :tag "No sorting" nil)
  2636. (const :tag "Alphabetical" string<)
  2637. (const :tag "Reverse alphabetical" string>)
  2638. (function :tag "Custom function" nil)))
  2639. (defvar org-tags-history nil
  2640. "History of minibuffer reads for tags.")
  2641. (defvar org-last-tags-completion-table nil
  2642. "The last used completion table for tags.")
  2643. (defvar org-after-tags-change-hook nil
  2644. "Hook that is run after the tags in a line have changed.")
  2645. (defgroup org-properties nil
  2646. "Options concerning properties in Org-mode."
  2647. :tag "Org Properties"
  2648. :group 'org)
  2649. (defcustom org-property-format "%-10s %s"
  2650. "How property key/value pairs should be formatted by `indent-line'.
  2651. When `indent-line' hits a property definition, it will format the line
  2652. according to this format, mainly to make sure that the values are
  2653. lined-up with respect to each other."
  2654. :group 'org-properties
  2655. :type 'string)
  2656. (defcustom org-properties-postprocess-alist nil
  2657. "Alist of properties and functions to adjust inserted values.
  2658. Elements of this alist must be of the form
  2659. ([string] [function])
  2660. where [string] must be a property name and [function] must be a
  2661. lambda expression: this lambda expression must take one argument,
  2662. the value to adjust, and return the new value as a string.
  2663. For example, this element will allow the property \"Remaining\"
  2664. to be updated wrt the relation between the \"Effort\" property
  2665. and the clock summary:
  2666. ((\"Remaining\" (lambda(value)
  2667. (let ((clocksum (org-clock-sum-current-item))
  2668. (effort (org-duration-string-to-minutes
  2669. (org-entry-get (point) \"Effort\"))))
  2670. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2671. :group 'org-properties
  2672. :version "24.1"
  2673. :type '(alist :key-type (string :tag "Property")
  2674. :value-type (function :tag "Function")))
  2675. (defcustom org-use-property-inheritance nil
  2676. "Non-nil means properties apply also for sublevels.
  2677. This setting is chiefly used during property searches. Turning it on can
  2678. cause significant overhead when doing a search, which is why it is not
  2679. on by default.
  2680. When nil, only the properties directly given in the current entry count.
  2681. When t, every property is inherited. The value may also be a list of
  2682. properties that should have inheritance, or a regular expression matching
  2683. properties that should be inherited.
  2684. However, note that some special properties use inheritance under special
  2685. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2686. and the properties ending in \"_ALL\" when they are used as descriptor
  2687. for valid values of a property.
  2688. Note for programmers:
  2689. When querying an entry with `org-entry-get', you can control if inheritance
  2690. should be used. By default, `org-entry-get' looks only at the local
  2691. properties. You can request inheritance by setting the inherit argument
  2692. to t (to force inheritance) or to `selective' (to respect the setting
  2693. in this variable)."
  2694. :group 'org-properties
  2695. :type '(choice
  2696. (const :tag "Not" nil)
  2697. (const :tag "Always" t)
  2698. (repeat :tag "Specific properties" (string :tag "Property"))
  2699. (regexp :tag "Properties matched by regexp")))
  2700. (defun org-property-inherit-p (property)
  2701. "Check if PROPERTY is one that should be inherited."
  2702. (cond
  2703. ((eq org-use-property-inheritance t) t)
  2704. ((not org-use-property-inheritance) nil)
  2705. ((stringp org-use-property-inheritance)
  2706. (string-match org-use-property-inheritance property))
  2707. ((listp org-use-property-inheritance)
  2708. (member property org-use-property-inheritance))
  2709. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2710. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2711. "The default column format, if no other format has been defined.
  2712. This variable can be set on the per-file basis by inserting a line
  2713. #+COLUMNS: %25ITEM ....."
  2714. :group 'org-properties
  2715. :type 'string)
  2716. (defcustom org-columns-ellipses ".."
  2717. "The ellipses to be used when a field in column view is truncated.
  2718. When this is the empty string, as many characters as possible are shown,
  2719. but then there will be no visual indication that the field has been truncated.
  2720. When this is a string of length N, the last N characters of a truncated
  2721. field are replaced by this string. If the column is narrower than the
  2722. ellipses string, only part of the ellipses string will be shown."
  2723. :group 'org-properties
  2724. :type 'string)
  2725. (defcustom org-columns-modify-value-for-display-function nil
  2726. "Function that modifies values for display in column view.
  2727. For example, it can be used to cut out a certain part from a time stamp.
  2728. The function must take 2 arguments:
  2729. column-title The title of the column (*not* the property name)
  2730. value The value that should be modified.
  2731. The function should return the value that should be displayed,
  2732. or nil if the normal value should be used."
  2733. :group 'org-properties
  2734. :type 'function)
  2735. (defcustom org-effort-property "Effort"
  2736. "The property that is being used to keep track of effort estimates.
  2737. Effort estimates given in this property need to have the format H:MM."
  2738. :group 'org-properties
  2739. :group 'org-progress
  2740. :type '(string :tag "Property"))
  2741. (defconst org-global-properties-fixed
  2742. '(("VISIBILITY_ALL" . "folded children content all")
  2743. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2744. "List of property/value pairs that can be inherited by any entry.
  2745. These are fixed values, for the preset properties. The user variable
  2746. that can be used to add to this list is `org-global-properties'.
  2747. The entries in this list are cons cells where the car is a property
  2748. name and cdr is a string with the value. If the value represents
  2749. multiple items like an \"_ALL\" property, separate the items by
  2750. spaces.")
  2751. (defcustom org-global-properties nil
  2752. "List of property/value pairs that can be inherited by any entry.
  2753. This list will be combined with the constant `org-global-properties-fixed'.
  2754. The entries in this list are cons cells where the car is a property
  2755. name and cdr is a string with the value.
  2756. You can set buffer-local values for the same purpose in the variable
  2757. `org-file-properties' this by adding lines like
  2758. #+PROPERTY: NAME VALUE"
  2759. :group 'org-properties
  2760. :type '(repeat
  2761. (cons (string :tag "Property")
  2762. (string :tag "Value"))))
  2763. (defvar org-file-properties nil
  2764. "List of property/value pairs that can be inherited by any entry.
  2765. Valid for the current buffer.
  2766. This variable is populated from #+PROPERTY lines.")
  2767. (make-variable-buffer-local 'org-file-properties)
  2768. (defgroup org-agenda nil
  2769. "Options concerning agenda views in Org-mode."
  2770. :tag "Org Agenda"
  2771. :group 'org)
  2772. (defvar org-category nil
  2773. "Variable used by org files to set a category for agenda display.
  2774. Such files should use a file variable to set it, for example
  2775. # -*- mode: org; org-category: \"ELisp\"
  2776. or contain a special line
  2777. #+CATEGORY: ELisp
  2778. If the file does not specify a category, then file's base name
  2779. is used instead.")
  2780. (make-variable-buffer-local 'org-category)
  2781. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2782. (defcustom org-agenda-files nil
  2783. "The files to be used for agenda display.
  2784. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2785. \\[org-remove-file]. You can also use customize to edit the list.
  2786. If an entry is a directory, all files in that directory that are matched by
  2787. `org-agenda-file-regexp' will be part of the file list.
  2788. If the value of the variable is not a list but a single file name, then
  2789. the list of agenda files is actually stored and maintained in that file, one
  2790. agenda file per line. In this file paths can be given relative to
  2791. `org-directory'. Tilde expansion and environment variable substitution
  2792. are also made."
  2793. :group 'org-agenda
  2794. :type '(choice
  2795. (repeat :tag "List of files and directories" file)
  2796. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2797. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2798. "Regular expression to match files for `org-agenda-files'.
  2799. If any element in the list in that variable contains a directory instead
  2800. of a normal file, all files in that directory that are matched by this
  2801. regular expression will be included."
  2802. :group 'org-agenda
  2803. :type 'regexp)
  2804. (defcustom org-agenda-text-search-extra-files nil
  2805. "List of extra files to be searched by text search commands.
  2806. These files will be search in addition to the agenda files by the
  2807. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2808. Note that these files will only be searched for text search commands,
  2809. not for the other agenda views like todo lists, tag searches or the weekly
  2810. agenda. This variable is intended to list notes and possibly archive files
  2811. that should also be searched by these two commands.
  2812. In fact, if the first element in the list is the symbol `agenda-archives',
  2813. than all archive files of all agenda files will be added to the search
  2814. scope."
  2815. :group 'org-agenda
  2816. :type '(set :greedy t
  2817. (const :tag "Agenda Archives" agenda-archives)
  2818. (repeat :inline t (file))))
  2819. (if (fboundp 'defvaralias)
  2820. (defvaralias 'org-agenda-multi-occur-extra-files
  2821. 'org-agenda-text-search-extra-files))
  2822. (defcustom org-agenda-skip-unavailable-files nil
  2823. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2824. A nil value means to remove them, after a query, from the list."
  2825. :group 'org-agenda
  2826. :type 'boolean)
  2827. (defcustom org-calendar-to-agenda-key [?c]
  2828. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2829. The command `org-calendar-goto-agenda' will be bound to this key. The
  2830. default is the character `c' because then `c' can be used to switch back and
  2831. forth between agenda and calendar."
  2832. :group 'org-agenda
  2833. :type 'sexp)
  2834. (defcustom org-calendar-agenda-action-key [?k]
  2835. "The key to be installed in `calendar-mode-map' for agenda-action.
  2836. The command `org-agenda-action' will be bound to this key. The
  2837. default is the character `k' because we use the same key in the agenda."
  2838. :group 'org-agenda
  2839. :type 'sexp)
  2840. (defcustom org-calendar-insert-diary-entry-key [?i]
  2841. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2842. This option is irrelevant until `org-agenda-diary-file' has been configured
  2843. to point to an Org-mode file. When that is the case, the command
  2844. `org-agenda-diary-entry' will be bound to the key given here, by default
  2845. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2846. if you want to continue doing this, you need to change this to a different
  2847. key."
  2848. :group 'org-agenda
  2849. :type 'sexp)
  2850. (defcustom org-agenda-diary-file 'diary-file
  2851. "File to which to add new entries with the `i' key in agenda and calendar.
  2852. When this is the symbol `diary-file', the functionality in the Emacs
  2853. calendar will be used to add entries to the `diary-file'. But when this
  2854. points to a file, `org-agenda-diary-entry' will be used instead."
  2855. :group 'org-agenda
  2856. :type '(choice
  2857. (const :tag "The standard Emacs diary file" diary-file)
  2858. (file :tag "Special Org file diary entries")))
  2859. (eval-after-load "calendar"
  2860. '(progn
  2861. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2862. 'org-calendar-goto-agenda)
  2863. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2864. 'org-agenda-action)
  2865. (add-hook 'calendar-mode-hook
  2866. (lambda ()
  2867. (unless (eq org-agenda-diary-file 'diary-file)
  2868. (define-key calendar-mode-map
  2869. org-calendar-insert-diary-entry-key
  2870. 'org-agenda-diary-entry))))))
  2871. (defgroup org-latex nil
  2872. "Options for embedding LaTeX code into Org-mode."
  2873. :tag "Org LaTeX"
  2874. :group 'org)
  2875. (defcustom org-format-latex-options
  2876. '(:foreground default :background default :scale 1.0
  2877. :html-foreground "Black" :html-background "Transparent"
  2878. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2879. "Options for creating images from LaTeX fragments.
  2880. This is a property list with the following properties:
  2881. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2882. `default' means use the foreground of the default face.
  2883. :background the background color, or \"Transparent\".
  2884. `default' means use the background of the default face.
  2885. :scale a scaling factor for the size of the images, to get more pixels
  2886. :html-foreground, :html-background, :html-scale
  2887. the same numbers for HTML export.
  2888. :matchers a list indicating which matchers should be used to
  2889. find LaTeX fragments. Valid members of this list are:
  2890. \"begin\" find environments
  2891. \"$1\" find single characters surrounded by $.$
  2892. \"$\" find math expressions surrounded by $...$
  2893. \"$$\" find math expressions surrounded by $$....$$
  2894. \"\\(\" find math expressions surrounded by \\(...\\)
  2895. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2896. :group 'org-latex
  2897. :type 'plist)
  2898. (defcustom org-format-latex-signal-error t
  2899. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2900. When nil, just push out a message."
  2901. :group 'org-latex
  2902. :version "24.1"
  2903. :type 'boolean)
  2904. (defcustom org-latex-to-mathml-jar-file nil
  2905. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2906. Use this to specify additional executable file say a jar file.
  2907. When using MathToWeb as the converter, specify the full-path to
  2908. your mathtoweb.jar file."
  2909. :group 'org-latex
  2910. :version "24.1"
  2911. :type '(choice
  2912. (const :tag "None" nil)
  2913. (file :tag "JAR file" :must-match t)))
  2914. (defcustom org-latex-to-mathml-convert-command nil
  2915. "Command to convert LaTeX fragments to MathML.
  2916. Replace format-specifiers in the command as noted below and use
  2917. `shell-command' to convert LaTeX to MathML.
  2918. %j: Executable file in fully expanded form as specified by
  2919. `org-latex-to-mathml-jar-file'.
  2920. %I: Input LaTeX file in fully expanded form
  2921. %o: Output MathML file
  2922. This command is used by `org-create-math-formula'.
  2923. When using MathToWeb as the converter, set this to
  2924. \"java -jar %j -unicode -force -df %o %I\"."
  2925. :group 'org-latex
  2926. :version "24.1"
  2927. :type '(choice
  2928. (const :tag "None" nil)
  2929. (string :tag "\nShell command")))
  2930. (defcustom org-latex-create-formula-image-program 'dvipng
  2931. "Program to convert LaTeX fragments with.
  2932. dvipng Process the LaTeX fragments to dvi file, then convert
  2933. dvi files to png files using dvipng.
  2934. This will also include processing of non-math environments.
  2935. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2936. to convert pdf files to png files"
  2937. :group 'org-latex
  2938. :version "24.1"
  2939. :type '(choice
  2940. (const :tag "dvipng" dvipng)
  2941. (const :tag "imagemagick" imagemagick)))
  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 (and (looking-at org-table-line-regexp)
  3449. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3450. (not (org-in-block-p '("src" "example"))))
  3451. (save-excursion (funcall function))
  3452. (or (looking-at org-table-line-regexp)
  3453. (forward-char 1)))
  3454. (re-search-forward org-table-any-border-regexp nil 1))))
  3455. (unless quietly (message "Mapping tables: done")))
  3456. ;; Declare and autoload functions from org-exp.el & Co
  3457. (declare-function org-default-export-plist "org-exp")
  3458. (declare-function org-infile-export-plist "org-exp")
  3459. (declare-function org-get-current-options "org-exp")
  3460. (eval-and-compile
  3461. (org-autoload "org-exp"
  3462. '(org-export org-export-visible
  3463. org-insert-export-options-template
  3464. org-table-clean-before-export))
  3465. (org-autoload "org-ascii"
  3466. '(org-export-as-ascii org-export-ascii-preprocess
  3467. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3468. org-export-region-as-ascii))
  3469. (org-autoload "org-latex"
  3470. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3471. org-replace-region-by-latex org-export-region-as-latex
  3472. org-export-as-latex org-export-as-pdf
  3473. org-export-as-pdf-and-open))
  3474. (org-autoload "org-html"
  3475. '(org-export-as-html-and-open
  3476. org-export-as-html-batch org-export-as-html-to-buffer
  3477. org-replace-region-by-html org-export-region-as-html
  3478. org-export-as-html))
  3479. (org-autoload "org-docbook"
  3480. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3481. org-replace-region-by-docbook org-export-region-as-docbook
  3482. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3483. org-export-as-docbook))
  3484. (org-autoload "org-icalendar"
  3485. '(org-export-icalendar-this-file
  3486. org-export-icalendar-all-agenda-files
  3487. org-export-icalendar-combine-agenda-files))
  3488. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3489. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3490. ;; Declare and autoload functions from org-agenda.el
  3491. (eval-and-compile
  3492. (org-autoload "org-agenda"
  3493. '(org-agenda org-agenda-list org-search-view
  3494. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3495. org-diary org-agenda-to-appt
  3496. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3497. ;; Autoload org-remember
  3498. (eval-and-compile
  3499. (org-autoload "org-remember"
  3500. '(org-remember-insinuate org-remember-annotation
  3501. org-remember-apply-template org-remember org-remember-handler)))
  3502. (eval-and-compile
  3503. (org-autoload "org-capture"
  3504. '(org-capture org-capture-insert-template-here
  3505. org-capture-import-remember-templates)))
  3506. ;; Autoload org-clock.el
  3507. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3508. (beg end))
  3509. (declare-function org-clock-update-mode-line "org-clock" ())
  3510. (declare-function org-resolve-clocks "org-clock"
  3511. (&optional also-non-dangling-p prompt last-valid))
  3512. (defvar org-clock-start-time)
  3513. (defvar org-clock-marker (make-marker)
  3514. "Marker recording the last clock-in.")
  3515. (defvar org-clock-hd-marker (make-marker)
  3516. "Marker recording the last clock-in, but the headline position.")
  3517. (defvar org-clock-heading ""
  3518. "The heading of the current clock entry.")
  3519. (defun org-clock-is-active ()
  3520. "Return non-nil if clock is currently running.
  3521. The return value is actually the clock marker."
  3522. (marker-buffer org-clock-marker))
  3523. (eval-and-compile
  3524. (org-autoload
  3525. "org-clock"
  3526. '(org-clock-in org-clock-out org-clock-cancel
  3527. org-clock-goto org-clock-sum org-clock-display
  3528. org-clock-remove-overlays org-clock-report
  3529. org-clocktable-shift org-dblock-write:clocktable
  3530. org-get-clocktable org-resolve-clocks)))
  3531. (defun org-clock-update-time-maybe ()
  3532. "If this is a CLOCK line, update it and return t.
  3533. Otherwise, return nil."
  3534. (interactive)
  3535. (save-excursion
  3536. (beginning-of-line 1)
  3537. (skip-chars-forward " \t")
  3538. (when (looking-at org-clock-string)
  3539. (let ((re (concat "[ \t]*" org-clock-string
  3540. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3541. "\\([ \t]*=>.*\\)?\\)?"))
  3542. ts te h m s neg)
  3543. (cond
  3544. ((not (looking-at re))
  3545. nil)
  3546. ((not (match-end 2))
  3547. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3548. (> org-clock-marker (point))
  3549. (<= org-clock-marker (point-at-eol)))
  3550. ;; The clock is running here
  3551. (setq org-clock-start-time
  3552. (apply 'encode-time
  3553. (org-parse-time-string (match-string 1))))
  3554. (org-clock-update-mode-line)))
  3555. (t
  3556. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3557. (end-of-line 1)
  3558. (setq ts (match-string 1)
  3559. te (match-string 3))
  3560. (setq s (- (org-float-time
  3561. (apply 'encode-time (org-parse-time-string te)))
  3562. (org-float-time
  3563. (apply 'encode-time (org-parse-time-string ts))))
  3564. neg (< s 0)
  3565. s (abs s)
  3566. h (floor (/ s 3600))
  3567. s (- s (* 3600 h))
  3568. m (floor (/ s 60))
  3569. s (- s (* 60 s)))
  3570. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3571. t))))))
  3572. (defun org-check-running-clock ()
  3573. "Check if the current buffer contains the running clock.
  3574. If yes, offer to stop it and to save the buffer with the changes."
  3575. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3576. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3577. (buffer-name))))
  3578. (org-clock-out)
  3579. (when (y-or-n-p "Save changed buffer?")
  3580. (save-buffer))))
  3581. (defun org-clocktable-try-shift (dir n)
  3582. "Check if this line starts a clock table, if yes, shift the time block."
  3583. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3584. (org-clocktable-shift dir n)))
  3585. ;; Autoload org-timer.el
  3586. (eval-and-compile
  3587. (org-autoload
  3588. "org-timer"
  3589. '(org-timer-start org-timer org-timer-item
  3590. org-timer-change-times-in-region
  3591. org-timer-set-timer
  3592. org-timer-reset-timers
  3593. org-timer-show-remaining-time)))
  3594. ;; Autoload org-feed.el
  3595. (eval-and-compile
  3596. (org-autoload
  3597. "org-feed"
  3598. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3599. ;; Autoload org-indent.el
  3600. ;; Define the variable already here, to make sure we have it.
  3601. (defvar org-indent-mode nil
  3602. "Non-nil if Org-Indent mode is enabled.
  3603. Use the command `org-indent-mode' to change this variable.")
  3604. (eval-and-compile
  3605. (org-autoload
  3606. "org-indent"
  3607. '(org-indent-mode)))
  3608. ;; Autoload org-mobile.el
  3609. (eval-and-compile
  3610. (org-autoload
  3611. "org-mobile"
  3612. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3613. ;; Autoload archiving code
  3614. ;; The stuff that is needed for cycling and tags has to be defined here.
  3615. (defgroup org-archive nil
  3616. "Options concerning archiving in Org-mode."
  3617. :tag "Org Archive"
  3618. :group 'org-structure)
  3619. (defcustom org-archive-location "%s_archive::"
  3620. "The location where subtrees should be archived.
  3621. The value of this variable is a string, consisting of two parts,
  3622. separated by a double-colon. The first part is a filename and
  3623. the second part is a headline.
  3624. When the filename is omitted, archiving happens in the same file.
  3625. %s in the filename will be replaced by the current file
  3626. name (without the directory part). Archiving to a different file
  3627. is useful to keep archived entries from contributing to the
  3628. Org-mode Agenda.
  3629. The archived entries will be filed as subtrees of the specified
  3630. headline. When the headline is omitted, the subtrees are simply
  3631. filed away at the end of the file, as top-level entries. Also in
  3632. the heading you can use %s to represent the file name, this can be
  3633. useful when using the same archive for a number of different files.
  3634. Here are a few examples:
  3635. \"%s_archive::\"
  3636. If the current file is Projects.org, archive in file
  3637. Projects.org_archive, as top-level trees. This is the default.
  3638. \"::* Archived Tasks\"
  3639. Archive in the current file, under the top-level headline
  3640. \"* Archived Tasks\".
  3641. \"~/org/archive.org::\"
  3642. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3643. \"~/org/archive.org::* From %s\"
  3644. Archive in file ~/org/archive.org (absolute path), under headlines
  3645. \"From FILENAME\" where file name is the current file name.
  3646. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3647. The \"datetree/\" string is special, signifying to archive
  3648. items to the datetree. Items are placed in either the CLOSED
  3649. date of the item, or the current date if there is no CLOSED date.
  3650. The heading will be a subentry to the current date. There doesn't
  3651. need to be a heading, but there always needs to be a slash after
  3652. datetree. For example, to store archived items directly in the
  3653. datetree, use \"~/org/datetree.org::datetree/\".
  3654. \"basement::** Finished Tasks\"
  3655. Archive in file ./basement (relative path), as level 3 trees
  3656. below the level 2 heading \"** Finished Tasks\".
  3657. You may set this option on a per-file basis by adding to the buffer a
  3658. line like
  3659. #+ARCHIVE: basement::** Finished Tasks
  3660. You may also define it locally for a subtree by setting an ARCHIVE property
  3661. in the entry. If such a property is found in an entry, or anywhere up
  3662. the hierarchy, it will be used."
  3663. :group 'org-archive
  3664. :type 'string)
  3665. (defcustom org-archive-tag "ARCHIVE"
  3666. "The tag that marks a subtree as archived.
  3667. An archived subtree does not open during visibility cycling, and does
  3668. not contribute to the agenda listings.
  3669. After changing this, font-lock must be restarted in the relevant buffers to
  3670. get the proper fontification."
  3671. :group 'org-archive
  3672. :group 'org-keywords
  3673. :type 'string)
  3674. (defcustom org-agenda-skip-archived-trees t
  3675. "Non-nil means the agenda will skip any items located in archived trees.
  3676. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3677. variable is no longer recommended, you should leave it at the value t.
  3678. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3679. :group 'org-archive
  3680. :group 'org-agenda-skip
  3681. :type 'boolean)
  3682. (defcustom org-columns-skip-archived-trees t
  3683. "Non-nil means ignore archived trees when creating column view."
  3684. :group 'org-archive
  3685. :group 'org-properties
  3686. :type 'boolean)
  3687. (defcustom org-cycle-open-archived-trees nil
  3688. "Non-nil means `org-cycle' will open archived trees.
  3689. An archived tree is a tree marked with the tag ARCHIVE.
  3690. When nil, archived trees will stay folded. You can still open them with
  3691. normal outline commands like `show-all', but not with the cycling commands."
  3692. :group 'org-archive
  3693. :group 'org-cycle
  3694. :type 'boolean)
  3695. (defcustom org-sparse-tree-open-archived-trees nil
  3696. "Non-nil means sparse tree construction shows matches in archived trees.
  3697. When nil, matches in these trees are highlighted, but the trees are kept in
  3698. collapsed state."
  3699. :group 'org-archive
  3700. :group 'org-sparse-trees
  3701. :type 'boolean)
  3702. (defun org-cycle-hide-archived-subtrees (state)
  3703. "Re-hide all archived subtrees after a visibility state change."
  3704. (when (and (not org-cycle-open-archived-trees)
  3705. (not (memq state '(overview folded))))
  3706. (save-excursion
  3707. (let* ((globalp (memq state '(contents all)))
  3708. (beg (if globalp (point-min) (point)))
  3709. (end (if globalp (point-max) (org-end-of-subtree t))))
  3710. (org-hide-archived-subtrees beg end)
  3711. (goto-char beg)
  3712. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3713. (message "%s" (substitute-command-keys
  3714. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3715. (defun org-force-cycle-archived ()
  3716. "Cycle subtree even if it is archived."
  3717. (interactive)
  3718. (setq this-command 'org-cycle)
  3719. (let ((org-cycle-open-archived-trees t))
  3720. (call-interactively 'org-cycle)))
  3721. (defun org-hide-archived-subtrees (beg end)
  3722. "Re-hide all archived subtrees after a visibility state change."
  3723. (save-excursion
  3724. (let* ((re (concat ":" org-archive-tag ":")))
  3725. (goto-char beg)
  3726. (while (re-search-forward re end t)
  3727. (when (org-at-heading-p)
  3728. (org-flag-subtree t)
  3729. (org-end-of-subtree t))))))
  3730. (defun org-flag-subtree (flag)
  3731. (save-excursion
  3732. (org-back-to-heading t)
  3733. (outline-end-of-heading)
  3734. (outline-flag-region (point)
  3735. (progn (org-end-of-subtree t) (point))
  3736. flag)))
  3737. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3738. (eval-and-compile
  3739. (org-autoload "org-archive"
  3740. '(org-add-archive-files org-archive-subtree
  3741. org-archive-to-archive-sibling org-toggle-archive-tag
  3742. org-archive-subtree-default
  3743. org-archive-subtree-default-with-confirmation)))
  3744. ;; Autoload Column View Code
  3745. (declare-function org-columns-number-to-string "org-colview")
  3746. (declare-function org-columns-get-format-and-top-level "org-colview")
  3747. (declare-function org-columns-compute "org-colview")
  3748. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3749. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3750. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3751. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3752. ;; Autoload ID code
  3753. (declare-function org-id-store-link "org-id")
  3754. (declare-function org-id-locations-load "org-id")
  3755. (declare-function org-id-locations-save "org-id")
  3756. (defvar org-id-track-globally)
  3757. (org-autoload "org-id"
  3758. '(org-id-get-create org-id-new org-id-copy org-id-get
  3759. org-id-get-with-outline-path-completion
  3760. org-id-get-with-outline-drilling org-id-store-link
  3761. org-id-goto org-id-find org-id-store-link))
  3762. ;; Autoload Plotting Code
  3763. (org-autoload "org-plot"
  3764. '(org-plot/gnuplot))
  3765. ;;; Variables for pre-computed regular expressions, all buffer local
  3766. (defvar org-drawer-regexp nil
  3767. "Matches first line of a hidden block.")
  3768. (make-variable-buffer-local 'org-drawer-regexp)
  3769. (defvar org-todo-regexp nil
  3770. "Matches any of the TODO state keywords.")
  3771. (make-variable-buffer-local 'org-todo-regexp)
  3772. (defvar org-not-done-regexp nil
  3773. "Matches any of the TODO state keywords except the last one.")
  3774. (make-variable-buffer-local 'org-not-done-regexp)
  3775. (defvar org-not-done-heading-regexp nil
  3776. "Matches a TODO headline that is not done.")
  3777. (make-variable-buffer-local 'org-not-done-regexp)
  3778. (defvar org-todo-line-regexp nil
  3779. "Matches a headline and puts TODO state into group 2 if present.")
  3780. (make-variable-buffer-local 'org-todo-line-regexp)
  3781. (defvar org-complex-heading-regexp nil
  3782. "Matches a headline and puts everything into groups:
  3783. group 1: the stars
  3784. group 2: The todo keyword, maybe
  3785. group 3: Priority cookie
  3786. group 4: True headline
  3787. group 5: Tags")
  3788. (make-variable-buffer-local 'org-complex-heading-regexp)
  3789. (defvar org-complex-heading-regexp-format nil
  3790. "Printf format to make regexp to match an exact headline.
  3791. This regexp will match the headline of any node which has the
  3792. exact headline text that is put into the format, but may have any
  3793. TODO state, priority and tags.")
  3794. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3795. (defvar org-todo-line-tags-regexp nil
  3796. "Matches a headline and puts TODO state into group 2 if present.
  3797. Also put tags into group 4 if tags are present.")
  3798. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3799. (defvar org-ds-keyword-length 12
  3800. "Maximum length of the Deadline and SCHEDULED keywords.")
  3801. (make-variable-buffer-local 'org-ds-keyword-length)
  3802. (defvar org-deadline-regexp nil
  3803. "Matches the DEADLINE keyword.")
  3804. (make-variable-buffer-local 'org-deadline-regexp)
  3805. (defvar org-deadline-time-regexp nil
  3806. "Matches the DEADLINE keyword together with a time stamp.")
  3807. (make-variable-buffer-local 'org-deadline-time-regexp)
  3808. (defvar org-deadline-line-regexp nil
  3809. "Matches the DEADLINE keyword and the rest of the line.")
  3810. (make-variable-buffer-local 'org-deadline-line-regexp)
  3811. (defvar org-scheduled-regexp nil
  3812. "Matches the SCHEDULED keyword.")
  3813. (make-variable-buffer-local 'org-scheduled-regexp)
  3814. (defvar org-scheduled-time-regexp nil
  3815. "Matches the SCHEDULED keyword together with a time stamp.")
  3816. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3817. (defvar org-closed-time-regexp nil
  3818. "Matches the CLOSED keyword together with a time stamp.")
  3819. (make-variable-buffer-local 'org-closed-time-regexp)
  3820. (defvar org-keyword-time-regexp nil
  3821. "Matches any of the 4 keywords, together with the time stamp.")
  3822. (make-variable-buffer-local 'org-keyword-time-regexp)
  3823. (defvar org-keyword-time-not-clock-regexp nil
  3824. "Matches any of the 3 keywords, together with the time stamp.")
  3825. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3826. (defvar org-maybe-keyword-time-regexp nil
  3827. "Matches a timestamp, possibly preceded by a keyword.")
  3828. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3829. (defvar org-planning-or-clock-line-re nil
  3830. "Matches a line with planning or clock info.")
  3831. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3832. (defvar org-all-time-keywords nil
  3833. "List of time keywords.")
  3834. (make-variable-buffer-local 'org-all-time-keywords)
  3835. (defconst org-plain-time-of-day-regexp
  3836. (concat
  3837. "\\(\\<[012]?[0-9]"
  3838. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3839. "\\(--?"
  3840. "\\(\\<[012]?[0-9]"
  3841. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3842. "\\)?")
  3843. "Regular expression to match a plain time or time range.
  3844. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3845. groups carry important information:
  3846. 0 the full match
  3847. 1 the first time, range or not
  3848. 8 the second time, if it is a range.")
  3849. (defconst org-plain-time-extension-regexp
  3850. (concat
  3851. "\\(\\<[012]?[0-9]"
  3852. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3853. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3854. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3855. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3856. groups carry important information:
  3857. 0 the full match
  3858. 7 hours of duration
  3859. 9 minutes of duration")
  3860. (defconst org-stamp-time-of-day-regexp
  3861. (concat
  3862. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3863. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3864. "\\(--?"
  3865. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3866. "Regular expression to match a timestamp time or time range.
  3867. After a match, the following groups carry important information:
  3868. 0 the full match
  3869. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3870. 2 the first time, range or not
  3871. 4 the second time, if it is a range.")
  3872. (defconst org-startup-options
  3873. '(("fold" org-startup-folded t)
  3874. ("overview" org-startup-folded t)
  3875. ("nofold" org-startup-folded nil)
  3876. ("showall" org-startup-folded nil)
  3877. ("showeverything" org-startup-folded showeverything)
  3878. ("content" org-startup-folded content)
  3879. ("indent" org-startup-indented t)
  3880. ("noindent" org-startup-indented nil)
  3881. ("hidestars" org-hide-leading-stars t)
  3882. ("showstars" org-hide-leading-stars nil)
  3883. ("odd" org-odd-levels-only t)
  3884. ("oddeven" org-odd-levels-only nil)
  3885. ("align" org-startup-align-all-tables t)
  3886. ("noalign" org-startup-align-all-tables nil)
  3887. ("inlineimages" org-startup-with-inline-images t)
  3888. ("noinlineimages" org-startup-with-inline-images nil)
  3889. ("customtime" org-display-custom-times t)
  3890. ("logdone" org-log-done time)
  3891. ("lognotedone" org-log-done note)
  3892. ("nologdone" org-log-done nil)
  3893. ("lognoteclock-out" org-log-note-clock-out t)
  3894. ("nolognoteclock-out" org-log-note-clock-out nil)
  3895. ("logrepeat" org-log-repeat state)
  3896. ("lognoterepeat" org-log-repeat note)
  3897. ("nologrepeat" org-log-repeat nil)
  3898. ("logreschedule" org-log-reschedule time)
  3899. ("lognotereschedule" org-log-reschedule note)
  3900. ("nologreschedule" org-log-reschedule nil)
  3901. ("logredeadline" org-log-redeadline time)
  3902. ("lognoteredeadline" org-log-redeadline note)
  3903. ("nologredeadline" org-log-redeadline nil)
  3904. ("logrefile" org-log-refile time)
  3905. ("lognoterefile" org-log-refile note)
  3906. ("nologrefile" org-log-refile nil)
  3907. ("fninline" org-footnote-define-inline t)
  3908. ("nofninline" org-footnote-define-inline nil)
  3909. ("fnlocal" org-footnote-section nil)
  3910. ("fnauto" org-footnote-auto-label t)
  3911. ("fnprompt" org-footnote-auto-label nil)
  3912. ("fnconfirm" org-footnote-auto-label confirm)
  3913. ("fnplain" org-footnote-auto-label plain)
  3914. ("fnadjust" org-footnote-auto-adjust t)
  3915. ("nofnadjust" org-footnote-auto-adjust nil)
  3916. ("constcgs" constants-unit-system cgs)
  3917. ("constSI" constants-unit-system SI)
  3918. ("noptag" org-tag-persistent-alist nil)
  3919. ("hideblocks" org-hide-block-startup t)
  3920. ("nohideblocks" org-hide-block-startup nil)
  3921. ("beamer" org-startup-with-beamer-mode t)
  3922. ("entitiespretty" org-pretty-entities t)
  3923. ("entitiesplain" org-pretty-entities nil))
  3924. "Variable associated with STARTUP options for org-mode.
  3925. Each element is a list of three items: The startup options as written
  3926. in the #+STARTUP line, the corresponding variable, and the value to
  3927. set this variable to if the option is found. An optional forth element PUSH
  3928. means to push this value onto the list in the variable.")
  3929. (defun org-update-property-plist (key val props)
  3930. "Update PROPS with KEY and VAL."
  3931. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3932. (key (if appending (substring key 0 (- (length key) 1)) key))
  3933. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3934. (previous (cdr (assoc key props))))
  3935. (if appending
  3936. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3937. (cons (cons key val) remainder))))
  3938. (defconst org-block-regexp
  3939. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3940. "Regular expression for hiding blocks.")
  3941. (defconst org-heading-keyword-regexp-format
  3942. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3943. "Printf format for a regexp matching an headline with some keyword.
  3944. This regexp will match the headline of any node which has the
  3945. exact keyword that is put into the format. The keyword isn't in
  3946. any group by default, but the stars and the body are.")
  3947. (defconst org-heading-keyword-maybe-regexp-format
  3948. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3949. "Printf format for a regexp matching an headline, possibly with some keyword.
  3950. This regexp can match any headline with the specified keyword, or
  3951. without a keyword. The keyword isn't in any group by default,
  3952. but the stars and the body are.")
  3953. (defun org-set-regexps-and-options ()
  3954. "Precompute regular expressions for current buffer."
  3955. (when (derived-mode-p 'org-mode)
  3956. (org-set-local 'org-todo-kwd-alist nil)
  3957. (org-set-local 'org-todo-key-alist nil)
  3958. (org-set-local 'org-todo-key-trigger nil)
  3959. (org-set-local 'org-todo-keywords-1 nil)
  3960. (org-set-local 'org-done-keywords nil)
  3961. (org-set-local 'org-todo-heads nil)
  3962. (org-set-local 'org-todo-sets nil)
  3963. (org-set-local 'org-todo-log-states nil)
  3964. (org-set-local 'org-file-properties nil)
  3965. (org-set-local 'org-file-tags nil)
  3966. (let ((re (org-make-options-regexp
  3967. '("CATEGORY" "TODO" "COLUMNS"
  3968. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3969. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3970. "OPTIONS")
  3971. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3972. (splitre "[ \t]+")
  3973. (scripts org-use-sub-superscripts)
  3974. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3975. tail sep kws1 prio props ftags drawers beamer-p
  3976. ext-setup-or-nil setup-contents (start 0))
  3977. (save-excursion
  3978. (save-restriction
  3979. (widen)
  3980. (goto-char (point-min))
  3981. (while (or (and ext-setup-or-nil
  3982. (string-match re ext-setup-or-nil start)
  3983. (setq start (match-end 0)))
  3984. (and (setq ext-setup-or-nil nil start 0)
  3985. (re-search-forward re nil t)))
  3986. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3987. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3988. (if (stringp value) (setq value (org-trim value)))
  3989. (cond
  3990. ((equal key "CATEGORY")
  3991. (setq cat value))
  3992. ((member key '("SEQ_TODO" "TODO"))
  3993. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3994. ((equal key "TYP_TODO")
  3995. (push (cons 'type (org-split-string value splitre)) kwds))
  3996. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3997. ;; general TODO-like setup
  3998. (push (cons (intern (downcase (match-string 1 key)))
  3999. (org-split-string value splitre)) kwds))
  4000. ((equal key "TAGS")
  4001. (setq tags (append tags (if tags '("\\n") nil)
  4002. (org-split-string value splitre))))
  4003. ((equal key "COLUMNS")
  4004. (org-set-local 'org-columns-default-format value))
  4005. ((equal key "LINK")
  4006. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4007. (push (cons (match-string 1 value)
  4008. (org-trim (match-string 2 value)))
  4009. links)))
  4010. ((equal key "PRIORITIES")
  4011. (setq prio (org-split-string value " +")))
  4012. ((equal key "PROPERTY")
  4013. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4014. (setq props (org-update-property-plist (match-string 1 value)
  4015. (match-string 2 value)
  4016. props))))
  4017. ((equal key "FILETAGS")
  4018. (when (string-match "\\S-" value)
  4019. (setq ftags
  4020. (append
  4021. ftags
  4022. (apply 'append
  4023. (mapcar (lambda (x) (org-split-string x ":"))
  4024. (org-split-string value)))))))
  4025. ((equal key "DRAWERS")
  4026. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4027. ((equal key "CONSTANTS")
  4028. (setq const (append const (org-split-string value splitre))))
  4029. ((equal key "STARTUP")
  4030. (let ((opts (org-split-string value splitre))
  4031. l var val)
  4032. (while (setq l (pop opts))
  4033. (when (setq l (assoc l org-startup-options))
  4034. (setq var (nth 1 l) val (nth 2 l))
  4035. (if (not (nth 3 l))
  4036. (set (make-local-variable var) val)
  4037. (if (not (listp (symbol-value var)))
  4038. (set (make-local-variable var) nil))
  4039. (set (make-local-variable var) (symbol-value var))
  4040. (add-to-list var val))))))
  4041. ((equal key "ARCHIVE")
  4042. (setq arch value)
  4043. (remove-text-properties 0 (length arch)
  4044. '(face t fontified t) arch))
  4045. ((equal key "LATEX_CLASS")
  4046. (setq beamer-p (equal value "beamer")))
  4047. ((equal key "OPTIONS")
  4048. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4049. (setq scripts (read (match-string 2 value)))))
  4050. ((equal key "SETUPFILE")
  4051. (setq setup-contents (org-file-contents
  4052. (expand-file-name
  4053. (org-remove-double-quotes value))
  4054. 'noerror))
  4055. (if (not ext-setup-or-nil)
  4056. (setq ext-setup-or-nil setup-contents start 0)
  4057. (setq ext-setup-or-nil
  4058. (concat (substring ext-setup-or-nil 0 start)
  4059. "\n" setup-contents "\n"
  4060. (substring ext-setup-or-nil start)))))))
  4061. ;; search for property blocks
  4062. (goto-char (point-min))
  4063. (while (re-search-forward org-block-regexp nil t)
  4064. (when (equal "PROPERTY" (upcase (match-string 1)))
  4065. (setq value (replace-regexp-in-string
  4066. "[\n\r]" " " (match-string 4)))
  4067. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4068. (setq props (org-update-property-plist (match-string 1 value)
  4069. (match-string 2 value)
  4070. props)))))))
  4071. (org-set-local 'org-use-sub-superscripts scripts)
  4072. (when cat
  4073. (org-set-local 'org-category (intern cat))
  4074. (push (cons "CATEGORY" cat) props))
  4075. (when prio
  4076. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4077. (setq prio (mapcar 'string-to-char prio))
  4078. (org-set-local 'org-highest-priority (nth 0 prio))
  4079. (org-set-local 'org-lowest-priority (nth 1 prio))
  4080. (org-set-local 'org-default-priority (nth 2 prio)))
  4081. (and props (org-set-local 'org-file-properties (nreverse props)))
  4082. (and ftags (org-set-local 'org-file-tags
  4083. (mapcar 'org-add-prop-inherited ftags)))
  4084. (and drawers (org-set-local 'org-drawers drawers))
  4085. (and arch (org-set-local 'org-archive-location arch))
  4086. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4087. ;; Process the TODO keywords
  4088. (unless kwds
  4089. ;; Use the global values as if they had been given locally.
  4090. (setq kwds (default-value 'org-todo-keywords))
  4091. (if (stringp (car kwds))
  4092. (setq kwds (list (cons org-todo-interpretation
  4093. (default-value 'org-todo-keywords)))))
  4094. (setq kwds (reverse kwds)))
  4095. (setq kwds (nreverse kwds))
  4096. (let (inter kws kw)
  4097. (while (setq kws (pop kwds))
  4098. (let ((kws (or
  4099. (run-hook-with-args-until-success
  4100. 'org-todo-setup-filter-hook kws)
  4101. kws)))
  4102. (setq inter (pop kws) sep (member "|" kws)
  4103. kws0 (delete "|" (copy-sequence kws))
  4104. kwsa nil
  4105. kws1 (mapcar
  4106. (lambda (x)
  4107. ;; 1 2
  4108. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4109. (progn
  4110. (setq kw (match-string 1 x)
  4111. key (and (match-end 2) (match-string 2 x))
  4112. log (org-extract-log-state-settings x))
  4113. (push (cons kw (and key (string-to-char key))) kwsa)
  4114. (and log (push log org-todo-log-states))
  4115. kw)
  4116. (error "Invalid TODO keyword %s" x)))
  4117. kws0)
  4118. kwsa (if kwsa (append '((:startgroup))
  4119. (nreverse kwsa)
  4120. '((:endgroup))))
  4121. hw (car kws1)
  4122. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4123. tail (list inter hw (car dws) (org-last dws))))
  4124. (add-to-list 'org-todo-heads hw 'append)
  4125. (push kws1 org-todo-sets)
  4126. (setq org-done-keywords (append org-done-keywords dws nil))
  4127. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4128. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4129. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4130. (setq org-todo-sets (nreverse org-todo-sets)
  4131. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4132. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4133. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4134. ;; Process the constants
  4135. (when const
  4136. (let (e cst)
  4137. (while (setq e (pop const))
  4138. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4139. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4140. (setq org-table-formula-constants-local cst)))
  4141. ;; Process the tags.
  4142. (when tags
  4143. (let (e tgs)
  4144. (while (setq e (pop tags))
  4145. (cond
  4146. ((equal e "{") (push '(:startgroup) tgs))
  4147. ((equal e "}") (push '(:endgroup) tgs))
  4148. ((equal e "\\n") (push '(:newline) tgs))
  4149. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4150. (push (cons (match-string 1 e)
  4151. (string-to-char (match-string 2 e)))
  4152. tgs))
  4153. (t (push (list e) tgs))))
  4154. (org-set-local 'org-tag-alist nil)
  4155. (while (setq e (pop tgs))
  4156. (or (and (stringp (car e))
  4157. (assoc (car e) org-tag-alist))
  4158. (push e org-tag-alist)))))
  4159. ;; Compute the regular expressions and other local variables.
  4160. ;; Using `org-outline-regexp-bol' would complicate them much,
  4161. ;; because of the fixed white space at the end of that string.
  4162. (if (not org-done-keywords)
  4163. (setq org-done-keywords (and org-todo-keywords-1
  4164. (list (org-last org-todo-keywords-1)))))
  4165. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4166. (length org-scheduled-string)
  4167. (length org-clock-string)
  4168. (length org-closed-string)))
  4169. org-drawer-regexp
  4170. (concat "^[ \t]*:\\("
  4171. (mapconcat 'regexp-quote org-drawers "\\|")
  4172. "\\):[ \t]*$")
  4173. org-not-done-keywords
  4174. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4175. org-todo-regexp
  4176. (concat "\\("
  4177. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4178. "\\)")
  4179. org-not-done-regexp
  4180. (concat "\\("
  4181. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4182. "\\)")
  4183. org-not-done-heading-regexp
  4184. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4185. org-todo-line-regexp
  4186. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4187. org-complex-heading-regexp
  4188. (concat "^\\(\\*+\\)"
  4189. "\\(?: +" org-todo-regexp "\\)?"
  4190. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4191. "\\(?: +\\(.*?\\)\\)?"
  4192. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4193. "[ \t]*$")
  4194. org-complex-heading-regexp-format
  4195. (concat "^\\(\\*+\\)"
  4196. "\\(?: +" org-todo-regexp "\\)?"
  4197. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4198. "\\(?: +"
  4199. ;; Stats cookies can be stuck to body.
  4200. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4201. "\\(%s\\)"
  4202. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4203. "\\)"
  4204. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4205. "[ \t]*$")
  4206. org-todo-line-tags-regexp
  4207. (concat "^\\(\\*+\\)"
  4208. "\\(?: +" org-todo-regexp "\\)?"
  4209. "\\(?: +\\(.*?\\)\\)?"
  4210. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4211. "[ \t]*$")
  4212. org-deadline-regexp (concat "\\<" org-deadline-string)
  4213. org-deadline-time-regexp
  4214. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4215. org-deadline-line-regexp
  4216. (concat "\\<\\(" org-deadline-string "\\).*")
  4217. org-scheduled-regexp
  4218. (concat "\\<" org-scheduled-string)
  4219. org-scheduled-time-regexp
  4220. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4221. org-closed-time-regexp
  4222. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4223. org-keyword-time-regexp
  4224. (concat "\\<\\(" org-scheduled-string
  4225. "\\|" org-deadline-string
  4226. "\\|" org-closed-string
  4227. "\\|" org-clock-string "\\)"
  4228. " *[[<]\\([^]>]+\\)[]>]")
  4229. org-keyword-time-not-clock-regexp
  4230. (concat "\\<\\(" org-scheduled-string
  4231. "\\|" org-deadline-string
  4232. "\\|" org-closed-string
  4233. "\\)"
  4234. " *[[<]\\([^]>]+\\)[]>]")
  4235. org-maybe-keyword-time-regexp
  4236. (concat "\\(\\<\\(" org-scheduled-string
  4237. "\\|" org-deadline-string
  4238. "\\|" org-closed-string
  4239. "\\|" org-clock-string "\\)\\)?"
  4240. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4241. org-planning-or-clock-line-re
  4242. (concat "^[ \t]*\\("
  4243. org-scheduled-string "\\|"
  4244. org-deadline-string "\\|"
  4245. org-closed-string "\\|"
  4246. org-clock-string "\\)")
  4247. org-all-time-keywords
  4248. (mapcar (lambda (w) (substring w 0 -1))
  4249. (list org-scheduled-string org-deadline-string
  4250. org-clock-string org-closed-string))
  4251. )
  4252. (org-compute-latex-and-specials-regexp)
  4253. (org-set-font-lock-defaults))))
  4254. (defun org-file-contents (file &optional noerror)
  4255. "Return the contents of FILE, as a string."
  4256. (if (or (not file)
  4257. (not (file-readable-p file)))
  4258. (if noerror
  4259. (progn
  4260. (message "Cannot read file \"%s\"" file)
  4261. (ding) (sit-for 2)
  4262. "")
  4263. (error "Cannot read file \"%s\"" file))
  4264. (with-temp-buffer
  4265. (insert-file-contents file)
  4266. (buffer-string))))
  4267. (defun org-extract-log-state-settings (x)
  4268. "Extract the log state setting from a TODO keyword string.
  4269. This will extract info from a string like \"WAIT(w@/!)\"."
  4270. (let (kw key log1 log2)
  4271. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4272. (setq kw (match-string 1 x)
  4273. key (and (match-end 2) (match-string 2 x))
  4274. log1 (and (match-end 3) (match-string 3 x))
  4275. log2 (and (match-end 4) (match-string 4 x)))
  4276. (and (or log1 log2)
  4277. (list kw
  4278. (and log1 (if (equal log1 "!") 'time 'note))
  4279. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4280. (defun org-remove-keyword-keys (list)
  4281. "Remove a pair of parenthesis at the end of each string in LIST."
  4282. (mapcar (lambda (x)
  4283. (if (string-match "(.*)$" x)
  4284. (substring x 0 (match-beginning 0))
  4285. x))
  4286. list))
  4287. (defun org-assign-fast-keys (alist)
  4288. "Assign fast keys to a keyword-key alist.
  4289. Respect keys that are already there."
  4290. (let (new e (alt ?0))
  4291. (while (setq e (pop alist))
  4292. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4293. (cdr e)) ;; Key already assigned.
  4294. (push e new)
  4295. (let ((clist (string-to-list (downcase (car e))))
  4296. (used (append new alist)))
  4297. (when (= (car clist) ?@)
  4298. (pop clist))
  4299. (while (and clist (rassoc (car clist) used))
  4300. (pop clist))
  4301. (unless clist
  4302. (while (rassoc alt used)
  4303. (incf alt)))
  4304. (push (cons (car e) (or (car clist) alt)) new))))
  4305. (nreverse new)))
  4306. ;;; Some variables used in various places
  4307. (defvar org-window-configuration nil
  4308. "Used in various places to store a window configuration.")
  4309. (defvar org-selected-window nil
  4310. "Used in various places to store a window configuration.")
  4311. (defvar org-finish-function nil
  4312. "Function to be called when `C-c C-c' is used.
  4313. This is for getting out of special buffers like remember.")
  4314. ;; FIXME: Occasionally check by commenting these, to make sure
  4315. ;; no other functions uses these, forgetting to let-bind them.
  4316. (defvar entry)
  4317. (defvar org-last-state)
  4318. (defvar date)
  4319. ;; Defined somewhere in this file, but used before definition.
  4320. (defvar org-entities) ;; defined in org-entities.el
  4321. (defvar org-struct-menu)
  4322. (defvar org-org-menu)
  4323. (defvar org-tbl-menu)
  4324. ;;;; Define the Org-mode
  4325. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4326. (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"))
  4327. ;; We use a before-change function to check if a table might need
  4328. ;; an update.
  4329. (defvar org-table-may-need-update t
  4330. "Indicates that a table might need an update.
  4331. This variable is set by `org-before-change-function'.
  4332. `org-table-align' sets it back to nil.")
  4333. (defun org-before-change-function (beg end)
  4334. "Every change indicates that a table might need an update."
  4335. (setq org-table-may-need-update t))
  4336. (defvar org-mode-map)
  4337. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4338. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4339. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4340. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4341. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4342. (defvar org-table-buffer-is-an nil)
  4343. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4344. ;; some places -- e.g. see the macro org-with-limited-levels.
  4345. ;;
  4346. ;; In Org buffers, the value of `outline-regexp' is that of
  4347. ;; `org-outline-regexp'. The only function still directly relying on
  4348. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4349. ;; job when `orgstruct-mode' is active.
  4350. (defvar org-outline-regexp "\\*+ "
  4351. "Regexp to match Org headlines.")
  4352. (defconst org-outline-regexp-bol "^\\*+ "
  4353. "Regexp to match Org headlines.
  4354. This is similar to `org-outline-regexp' but additionally makes
  4355. sure that we are at the beginning of the line.")
  4356. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4357. "Matches an headline, putting stars and text into groups.
  4358. Stars are put in group 1 and the trimmed body in group 2.")
  4359. (defvar bidi-paragraph-direction)
  4360. (defvar buffer-face-mode-face)
  4361. ;;;###autoload
  4362. (define-derived-mode org-mode outline-mode "Org"
  4363. "Outline-based notes management and organizer, alias
  4364. \"Carsten's outline-mode for keeping track of everything.\"
  4365. Org-mode develops organizational tasks around a NOTES file which
  4366. contains information about projects as plain text. Org-mode is
  4367. implemented on top of outline-mode, which is ideal to keep the content
  4368. of large files well structured. It supports ToDo items, deadlines and
  4369. time stamps, which magically appear in the diary listing of the Emacs
  4370. calendar. Tables are easily created with a built-in table editor.
  4371. Plain text URL-like links connect to websites, emails (VM), Usenet
  4372. messages (Gnus), BBDB entries, and any files related to the project.
  4373. For printing and sharing of notes, an Org-mode file (or a part of it)
  4374. can be exported as a structured ASCII or HTML file.
  4375. The following commands are available:
  4376. \\{org-mode-map}"
  4377. ;; Get rid of Outline menus, they are not needed
  4378. ;; Need to do this here because define-derived-mode sets up
  4379. ;; the keymap so late. Still, it is a waste to call this each time
  4380. ;; we switch another buffer into org-mode.
  4381. (if (featurep 'xemacs)
  4382. (when (boundp 'outline-mode-menu-heading)
  4383. ;; Assume this is Greg's port, it uses easymenu
  4384. (easy-menu-remove outline-mode-menu-heading)
  4385. (easy-menu-remove outline-mode-menu-show)
  4386. (easy-menu-remove outline-mode-menu-hide))
  4387. (define-key org-mode-map [menu-bar headings] 'undefined)
  4388. (define-key org-mode-map [menu-bar hide] 'undefined)
  4389. (define-key org-mode-map [menu-bar show] 'undefined))
  4390. (org-load-modules-maybe)
  4391. (easy-menu-add org-org-menu)
  4392. (easy-menu-add org-tbl-menu)
  4393. (org-install-agenda-files-menu)
  4394. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4395. (add-to-invisibility-spec '(org-cwidth))
  4396. (add-to-invisibility-spec '(org-hide-block . t))
  4397. (when (featurep 'xemacs)
  4398. (org-set-local 'line-move-ignore-invisible t))
  4399. (org-set-local 'outline-regexp org-outline-regexp)
  4400. (org-set-local 'outline-level 'org-outline-level)
  4401. (setq bidi-paragraph-direction 'left-to-right)
  4402. (when (and org-ellipsis
  4403. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4404. (fboundp 'make-glyph-code))
  4405. (unless org-display-table
  4406. (setq org-display-table (make-display-table)))
  4407. (set-display-table-slot
  4408. org-display-table 4
  4409. (vconcat (mapcar
  4410. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4411. org-ellipsis)))
  4412. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4413. (setq buffer-display-table org-display-table))
  4414. (org-set-regexps-and-options)
  4415. (when (and org-tag-faces (not org-tags-special-faces-re))
  4416. ;; tag faces set outside customize.... force initialization.
  4417. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4418. ;; Calc embedded
  4419. (org-set-local 'calc-embedded-open-mode "# ")
  4420. (modify-syntax-entry ?@ "w")
  4421. (if org-startup-truncated (setq truncate-lines t))
  4422. (org-set-local 'font-lock-unfontify-region-function
  4423. 'org-unfontify-region)
  4424. ;; Activate before-change-function
  4425. (org-set-local 'org-table-may-need-update t)
  4426. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4427. 'local)
  4428. ;; Check for running clock before killing a buffer
  4429. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4430. ;; Paragraphs and auto-filling
  4431. (org-set-autofill-regexps)
  4432. (setq indent-line-function 'org-indent-line-function)
  4433. (org-update-radio-target-regexp)
  4434. ;; Beginning/end of defun
  4435. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4436. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4437. ;; Next error for sparse trees
  4438. (org-set-local 'next-error-function 'org-occur-next-match)
  4439. ;; Make sure dependence stuff works reliably, even for users who set it
  4440. ;; too late :-(
  4441. (if org-enforce-todo-dependencies
  4442. (add-hook 'org-blocker-hook
  4443. 'org-block-todo-from-children-or-siblings-or-parent)
  4444. (remove-hook 'org-blocker-hook
  4445. 'org-block-todo-from-children-or-siblings-or-parent))
  4446. (if org-enforce-todo-checkbox-dependencies
  4447. (add-hook 'org-blocker-hook
  4448. 'org-block-todo-from-checkboxes)
  4449. (remove-hook 'org-blocker-hook
  4450. 'org-block-todo-from-checkboxes))
  4451. ;; Comment characters
  4452. (org-set-local 'comment-start "#")
  4453. (org-set-local 'comment-padding " ")
  4454. ;; Align options lines
  4455. (org-set-local
  4456. 'align-mode-rules-list
  4457. '((org-in-buffer-settings
  4458. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4459. (modes . '(org-mode)))))
  4460. ;; Imenu
  4461. (org-set-local 'imenu-create-index-function
  4462. 'org-imenu-get-tree)
  4463. ;; Make isearch reveal context
  4464. (if (or (featurep 'xemacs)
  4465. (not (boundp 'outline-isearch-open-invisible-function)))
  4466. ;; Emacs 21 and XEmacs make use of the hook
  4467. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4468. ;; Emacs 22 deals with this through a special variable
  4469. (org-set-local 'outline-isearch-open-invisible-function
  4470. (lambda (&rest ignore) (org-show-context 'isearch))))
  4471. ;; Turn on org-beamer-mode?
  4472. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4473. ;; Setup the pcomplete hooks
  4474. (set (make-local-variable 'pcomplete-command-completion-function)
  4475. 'org-pcomplete-initial)
  4476. (set (make-local-variable 'pcomplete-command-name-function)
  4477. 'org-command-at-point)
  4478. (set (make-local-variable 'pcomplete-default-completion-function)
  4479. 'ignore)
  4480. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4481. 'org-parse-arguments)
  4482. (set (make-local-variable 'pcomplete-termination-string) "")
  4483. (when (>= emacs-major-version 23)
  4484. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4485. ;; If empty file that did not turn on org-mode automatically, make it to.
  4486. (if (and org-insert-mode-line-in-empty-file
  4487. (org-called-interactively-p 'any)
  4488. (= (point-min) (point-max)))
  4489. (insert "# -*- mode: org -*-\n\n"))
  4490. (unless org-inhibit-startup
  4491. (when org-startup-align-all-tables
  4492. (let ((bmp (buffer-modified-p)))
  4493. (org-table-map-tables 'org-table-align 'quietly)
  4494. (set-buffer-modified-p bmp)))
  4495. (when org-startup-with-inline-images
  4496. (org-display-inline-images))
  4497. (when org-startup-indented
  4498. (require 'org-indent)
  4499. (org-indent-mode 1))
  4500. (unless org-inhibit-startup-visibility-stuff
  4501. (org-set-startup-visibility))))
  4502. (when (fboundp 'abbrev-table-put)
  4503. (abbrev-table-put org-mode-abbrev-table
  4504. :parents (list text-mode-abbrev-table)))
  4505. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4506. (defun org-current-time ()
  4507. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4508. (if (> (car org-time-stamp-rounding-minutes) 1)
  4509. (let ((r (car org-time-stamp-rounding-minutes))
  4510. (time (decode-time)))
  4511. (apply 'encode-time
  4512. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4513. (nthcdr 2 time))))
  4514. (current-time)))
  4515. (defun org-today ()
  4516. "Return today date, considering `org-extend-today-until'."
  4517. (time-to-days
  4518. (time-subtract (current-time)
  4519. (list 0 (* 3600 org-extend-today-until) 0))))
  4520. ;;;; Font-Lock stuff, including the activators
  4521. (defvar org-mouse-map (make-sparse-keymap))
  4522. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4523. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4524. (when org-mouse-1-follows-link
  4525. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4526. (when org-tab-follows-link
  4527. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4528. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4529. (require 'font-lock)
  4530. (defconst org-non-link-chars "]\t\n\r<>")
  4531. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4532. "shell" "elisp" "doi" "message"))
  4533. (defvar org-link-types-re nil
  4534. "Matches a link that has a url-like prefix like \"http:\"")
  4535. (defvar org-link-re-with-space nil
  4536. "Matches a link with spaces, optional angular brackets around it.")
  4537. (defvar org-link-re-with-space2 nil
  4538. "Matches a link with spaces, optional angular brackets around it.")
  4539. (defvar org-link-re-with-space3 nil
  4540. "Matches a link with spaces, only for internal part in bracket links.")
  4541. (defvar org-angle-link-re nil
  4542. "Matches link with angular brackets, spaces are allowed.")
  4543. (defvar org-plain-link-re nil
  4544. "Matches plain link, without spaces.")
  4545. (defvar org-bracket-link-regexp nil
  4546. "Matches a link in double brackets.")
  4547. (defvar org-bracket-link-analytic-regexp nil
  4548. "Regular expression used to analyze links.
  4549. Here is what the match groups contain after a match:
  4550. 1: http:
  4551. 2: http
  4552. 3: path
  4553. 4: [desc]
  4554. 5: desc")
  4555. (defvar org-bracket-link-analytic-regexp++ nil
  4556. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4557. (defvar org-any-link-re nil
  4558. "Regular expression matching any link.")
  4559. (defcustom org-match-sexp-depth 3
  4560. "Number of stacked braces for sub/superscript matching.
  4561. This has to be set before loading org.el to be effective."
  4562. :group 'org-export-translation ; ??????????????????????????/
  4563. :type 'integer)
  4564. (defun org-create-multibrace-regexp (left right n)
  4565. "Create a regular expression which will match a balanced sexp.
  4566. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4567. as single character strings.
  4568. The regexp returned will match the entire expression including the
  4569. delimiters. It will also define a single group which contains the
  4570. match except for the outermost delimiters. The maximum depth of
  4571. stacked delimiters is N. Escaping delimiters is not possible."
  4572. (let* ((nothing (concat "[^" left right "]*?"))
  4573. (or "\\|")
  4574. (re nothing)
  4575. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4576. (while (> n 1)
  4577. (setq n (1- n)
  4578. re (concat re or next)
  4579. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4580. (concat left "\\(" re "\\)" right)))
  4581. (defvar org-match-substring-regexp
  4582. (concat
  4583. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4584. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4585. "\\|"
  4586. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4587. "\\|"
  4588. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4589. "The regular expression matching a sub- or superscript.")
  4590. (defvar org-match-substring-with-braces-regexp
  4591. (concat
  4592. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4593. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4594. "\\)")
  4595. "The regular expression matching a sub- or superscript, forcing braces.")
  4596. (defun org-make-link-regexps ()
  4597. "Update the link regular expressions.
  4598. This should be called after the variable `org-link-types' has changed."
  4599. (setq org-link-types-re
  4600. (concat
  4601. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4602. org-link-re-with-space
  4603. (concat
  4604. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4605. "\\([^" org-non-link-chars " ]"
  4606. "[^" org-non-link-chars "]*"
  4607. "[^" org-non-link-chars " ]\\)>?")
  4608. org-link-re-with-space2
  4609. (concat
  4610. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4611. "\\([^" org-non-link-chars " ]"
  4612. "[^\t\n\r]*"
  4613. "[^" org-non-link-chars " ]\\)>?")
  4614. org-link-re-with-space3
  4615. (concat
  4616. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4617. "\\([^" org-non-link-chars " ]"
  4618. "[^\t\n\r]*\\)")
  4619. org-angle-link-re
  4620. (concat
  4621. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4622. "\\([^" org-non-link-chars " ]"
  4623. "[^" org-non-link-chars "]*"
  4624. "\\)>")
  4625. org-plain-link-re
  4626. (concat
  4627. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4628. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4629. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4630. org-bracket-link-regexp
  4631. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4632. org-bracket-link-analytic-regexp
  4633. (concat
  4634. "\\[\\["
  4635. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4636. "\\([^]]+\\)"
  4637. "\\]"
  4638. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4639. "\\]")
  4640. org-bracket-link-analytic-regexp++
  4641. (concat
  4642. "\\[\\["
  4643. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4644. "\\([^]]+\\)"
  4645. "\\]"
  4646. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4647. "\\]")
  4648. org-any-link-re
  4649. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4650. org-angle-link-re "\\)\\|\\("
  4651. org-plain-link-re "\\)")))
  4652. (org-make-link-regexps)
  4653. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4654. "Regular expression for fast time stamp matching.")
  4655. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4656. "Regular expression for fast time stamp matching.")
  4657. (defconst org-ts-regexp0
  4658. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4659. "Regular expression matching time strings for analysis.
  4660. This one does not require the space after the date, so it can be used
  4661. on a string that terminates immediately after the date.")
  4662. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4663. "Regular expression matching time strings for analysis.")
  4664. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4665. "Regular expression matching time stamps, with groups.")
  4666. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4667. "Regular expression matching time stamps (also [..]), with groups.")
  4668. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4669. "Regular expression matching a time stamp range.")
  4670. (defconst org-tr-regexp-both
  4671. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4672. "Regular expression matching a time stamp range.")
  4673. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4674. org-ts-regexp "\\)?")
  4675. "Regular expression matching a time stamp or time stamp range.")
  4676. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4677. org-ts-regexp-both "\\)?")
  4678. "Regular expression matching a time stamp or time stamp range.
  4679. The time stamps may be either active or inactive.")
  4680. (defvar org-emph-face nil)
  4681. (defun org-do-emphasis-faces (limit)
  4682. "Run through the buffer and add overlays to emphasized strings."
  4683. (let (rtn a)
  4684. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4685. (if (not (= (char-after (match-beginning 3))
  4686. (char-after (match-beginning 4))))
  4687. (progn
  4688. (setq rtn t)
  4689. (setq a (assoc (match-string 3) org-emphasis-alist))
  4690. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4691. 'face
  4692. (nth 1 a))
  4693. (and (nth 4 a)
  4694. (org-remove-flyspell-overlays-in
  4695. (match-beginning 0) (match-end 0)))
  4696. (add-text-properties (match-beginning 2) (match-end 2)
  4697. '(font-lock-multiline t org-emphasis t))
  4698. (when org-hide-emphasis-markers
  4699. (add-text-properties (match-end 4) (match-beginning 5)
  4700. '(invisible org-link))
  4701. (add-text-properties (match-beginning 3) (match-end 3)
  4702. '(invisible org-link)))))
  4703. (backward-char 1))
  4704. rtn))
  4705. (defun org-emphasize (&optional char)
  4706. "Insert or change an emphasis, i.e. a font like bold or italic.
  4707. If there is an active region, change that region to a new emphasis.
  4708. If there is no region, just insert the marker characters and position
  4709. the cursor between them.
  4710. CHAR should be either the marker character, or the first character of the
  4711. HTML tag associated with that emphasis. If CHAR is a space, the means
  4712. to remove the emphasis of the selected region.
  4713. If char is not given (for example in an interactive call) it
  4714. will be prompted for."
  4715. (interactive)
  4716. (let ((eal org-emphasis-alist) e det
  4717. (erc org-emphasis-regexp-components)
  4718. (prompt "")
  4719. (string "") beg end move tag c s)
  4720. (if (org-region-active-p)
  4721. (setq beg (region-beginning) end (region-end)
  4722. string (buffer-substring beg end))
  4723. (setq move t))
  4724. (while (setq e (pop eal))
  4725. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4726. c (aref tag 0))
  4727. (push (cons c (string-to-char (car e))) det)
  4728. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4729. (substring tag 1)))))
  4730. (setq det (nreverse det))
  4731. (unless char
  4732. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4733. (setq char (read-char-exclusive)))
  4734. (setq char (or (cdr (assoc char det)) char))
  4735. (if (equal char ?\ )
  4736. (setq s "" move nil)
  4737. (unless (assoc (char-to-string char) org-emphasis-alist)
  4738. (error "No such emphasis marker: \"%c\"" char))
  4739. (setq s (char-to-string char)))
  4740. (while (and (> (length string) 1)
  4741. (equal (substring string 0 1) (substring string -1))
  4742. (assoc (substring string 0 1) org-emphasis-alist))
  4743. (setq string (substring string 1 -1)))
  4744. (setq string (concat s string s))
  4745. (if beg (delete-region beg end))
  4746. (unless (or (bolp)
  4747. (string-match (concat "[" (nth 0 erc) "\n]")
  4748. (char-to-string (char-before (point)))))
  4749. (insert " "))
  4750. (unless (or (eobp)
  4751. (string-match (concat "[" (nth 1 erc) "\n]")
  4752. (char-to-string (char-after (point)))))
  4753. (insert " ") (backward-char 1))
  4754. (insert string)
  4755. (and move (backward-char 1))))
  4756. (defconst org-nonsticky-props
  4757. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4758. (defsubst org-rear-nonsticky-at (pos)
  4759. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4760. (defun org-activate-plain-links (limit)
  4761. "Run through the buffer and add overlays to links."
  4762. (catch 'exit
  4763. (let (f)
  4764. (if (re-search-forward org-plain-link-re limit t)
  4765. (progn
  4766. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4767. (setq f (get-text-property (match-beginning 0) 'face))
  4768. (if (or (eq f 'org-tag)
  4769. (and (listp f) (memq 'org-tag f)))
  4770. nil
  4771. (add-text-properties (match-beginning 0) (match-end 0)
  4772. (list 'mouse-face 'highlight
  4773. 'face 'org-link
  4774. 'keymap org-mouse-map))
  4775. (org-rear-nonsticky-at (match-end 0)))
  4776. t)))))
  4777. (defun org-activate-code (limit)
  4778. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4779. (progn
  4780. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4781. (remove-text-properties (match-beginning 0) (match-end 0)
  4782. '(display t invisible t intangible t))
  4783. t)))
  4784. (defcustom org-src-fontify-natively nil
  4785. "When non-nil, fontify code in code blocks."
  4786. :type 'boolean
  4787. :version "24.1"
  4788. :group 'org-appearance
  4789. :group 'org-babel)
  4790. (defcustom org-src-prevent-auto-filling nil
  4791. "When non-nil, prevent auto-filling in src blocks."
  4792. :type 'boolean
  4793. :version "24.1"
  4794. :group 'org-appearance
  4795. :group 'org-babel)
  4796. (defun org-fontify-meta-lines-and-blocks (limit)
  4797. (condition-case nil
  4798. (org-fontify-meta-lines-and-blocks-1 limit)
  4799. (error (message "org-mode fontification error"))))
  4800. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4801. "Fontify #+ lines and blocks, in the correct ways."
  4802. (let ((case-fold-search t))
  4803. (if (re-search-forward
  4804. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4805. limit t)
  4806. (let ((beg (match-beginning 0))
  4807. (block-start (match-end 0))
  4808. (block-end nil)
  4809. (lang (match-string 7))
  4810. (beg1 (line-beginning-position 2))
  4811. (dc1 (downcase (match-string 2)))
  4812. (dc3 (downcase (match-string 3)))
  4813. end end1 quoting block-type ovl)
  4814. (cond
  4815. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4816. ;; a single line of backend-specific content
  4817. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4818. (remove-text-properties (match-beginning 0) (match-end 0)
  4819. '(display t invisible t intangible t))
  4820. (add-text-properties (match-beginning 1) (match-end 3)
  4821. '(font-lock-fontified t face org-meta-line))
  4822. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4823. '(font-lock-fontified t face org-block))
  4824. ; for backend-specific code
  4825. t)
  4826. ((and (match-end 4) (equal dc3 "begin"))
  4827. ;; Truly a block
  4828. (setq block-type (downcase (match-string 5))
  4829. quoting (member block-type org-protecting-blocks))
  4830. (when (re-search-forward
  4831. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4832. nil t) ;; on purpose, we look further than LIMIT
  4833. (setq end (min (point-max) (match-end 0))
  4834. end1 (min (point-max) (1- (match-beginning 0))))
  4835. (setq block-end (match-beginning 0))
  4836. (when quoting
  4837. (remove-text-properties beg end
  4838. '(display t invisible t intangible t)))
  4839. (add-text-properties
  4840. beg end
  4841. '(font-lock-fontified t font-lock-multiline t))
  4842. (add-text-properties beg beg1 '(face org-meta-line))
  4843. (add-text-properties end1 (min (point-max) (1+ end))
  4844. '(face org-meta-line)) ; for end_src
  4845. (cond
  4846. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4847. (org-src-font-lock-fontify-block lang block-start block-end)
  4848. ;; remove old background overlays
  4849. (mapc (lambda (ov)
  4850. (if (eq (overlay-get ov 'face) 'org-block-background)
  4851. (delete-overlay ov)))
  4852. (overlays-at (/ (+ beg1 block-end) 2)))
  4853. ;; add a background overlay
  4854. (setq ovl (make-overlay beg1 block-end))
  4855. (overlay-put ovl 'face 'org-block-background)
  4856. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4857. (quoting
  4858. (add-text-properties beg1 (min (point-max) (1+ end1))
  4859. '(face org-block))) ; end of source block
  4860. ((not org-fontify-quote-and-verse-blocks))
  4861. ((string= block-type "quote")
  4862. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4863. ((string= block-type "verse")
  4864. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4865. (add-text-properties beg beg1 '(face org-block-begin-line))
  4866. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4867. '(face org-block-end-line))
  4868. t))
  4869. ((member dc1 '("title:" "author:" "email:" "date:"))
  4870. (add-text-properties
  4871. beg (match-end 3)
  4872. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4873. '(font-lock-fontified t invisible t)
  4874. '(font-lock-fontified t face org-document-info-keyword)))
  4875. (add-text-properties
  4876. (match-beginning 6) (match-end 6)
  4877. (if (string-equal dc1 "title:")
  4878. '(font-lock-fontified t face org-document-title)
  4879. '(font-lock-fontified t face org-document-info))))
  4880. ((not (member (char-after beg) '(?\ ?\t)))
  4881. ;; just any other in-buffer setting, but not indented
  4882. (add-text-properties
  4883. beg (match-end 0)
  4884. '(font-lock-fontified t face org-meta-line))
  4885. t)
  4886. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4887. "orgtbl:" "tblfm:" "tblname:" "results:"
  4888. "call:" "header:" "headers:" "name:"))
  4889. (and (match-end 4) (equal dc3 "attr")))
  4890. (add-text-properties
  4891. beg (match-end 0)
  4892. '(font-lock-fontified t face org-meta-line))
  4893. t)
  4894. ((member dc3 '(" " ""))
  4895. (add-text-properties
  4896. beg (match-end 0)
  4897. '(font-lock-fontified t face font-lock-comment-face)))
  4898. (t nil))))))
  4899. (defun org-strip-protective-commas (beg end)
  4900. "Strip protective commas between BEG and END in the current buffer."
  4901. (interactive "r")
  4902. (save-excursion
  4903. (save-match-data
  4904. (goto-char beg)
  4905. (let ((front-line (save-excursion
  4906. (re-search-forward
  4907. "[^[:space:]]" end t)
  4908. (goto-char (match-beginning 0))
  4909. (current-column))))
  4910. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4911. (goto-char (match-beginning 1))
  4912. (when (= (current-column) front-line)
  4913. (replace-match "" nil nil nil 1)))))))
  4914. (defun org-activate-angle-links (limit)
  4915. "Run through the buffer and add overlays to links."
  4916. (if (re-search-forward org-angle-link-re limit t)
  4917. (progn
  4918. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4919. (add-text-properties (match-beginning 0) (match-end 0)
  4920. (list 'mouse-face 'highlight
  4921. 'keymap org-mouse-map))
  4922. (org-rear-nonsticky-at (match-end 0))
  4923. t)))
  4924. (defun org-activate-footnote-links (limit)
  4925. "Run through the buffer and add overlays to footnotes."
  4926. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4927. (when fn
  4928. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4929. (org-remove-flyspell-overlays-in beg end)
  4930. (add-text-properties beg end
  4931. (list 'mouse-face 'highlight
  4932. 'keymap org-mouse-map
  4933. 'help-echo
  4934. (if (= (point-at-bol) beg)
  4935. "Footnote definition"
  4936. "Footnote reference")
  4937. 'font-lock-fontified t
  4938. 'font-lock-multiline t
  4939. 'face 'org-footnote))))))
  4940. (defun org-activate-bracket-links (limit)
  4941. "Run through the buffer and add overlays to bracketed links."
  4942. (if (re-search-forward org-bracket-link-regexp limit t)
  4943. (let* ((help (concat "LINK: "
  4944. (org-match-string-no-properties 1)))
  4945. ;; FIXME: above we should remove the escapes.
  4946. ;; but that requires another match, protecting match data,
  4947. ;; a lot of overhead for font-lock.
  4948. (ip (org-maybe-intangible
  4949. (list 'invisible 'org-link
  4950. 'keymap org-mouse-map 'mouse-face 'highlight
  4951. 'font-lock-multiline t 'help-echo help)))
  4952. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4953. 'font-lock-multiline t 'help-echo help)))
  4954. ;; We need to remove the invisible property here. Table narrowing
  4955. ;; may have made some of this invisible.
  4956. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4957. (remove-text-properties (match-beginning 0) (match-end 0)
  4958. '(invisible nil))
  4959. (if (match-end 3)
  4960. (progn
  4961. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4962. (org-rear-nonsticky-at (match-beginning 3))
  4963. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4964. (org-rear-nonsticky-at (match-end 3))
  4965. (add-text-properties (match-end 3) (match-end 0) ip)
  4966. (org-rear-nonsticky-at (match-end 0)))
  4967. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4968. (org-rear-nonsticky-at (match-beginning 1))
  4969. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4970. (org-rear-nonsticky-at (match-end 1))
  4971. (add-text-properties (match-end 1) (match-end 0) ip)
  4972. (org-rear-nonsticky-at (match-end 0)))
  4973. t)))
  4974. (defun org-activate-dates (limit)
  4975. "Run through the buffer and add overlays to dates."
  4976. (if (re-search-forward org-tsr-regexp-both limit t)
  4977. (progn
  4978. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4979. (add-text-properties (match-beginning 0) (match-end 0)
  4980. (list 'mouse-face 'highlight
  4981. 'keymap org-mouse-map))
  4982. (org-rear-nonsticky-at (match-end 0))
  4983. (when org-display-custom-times
  4984. (if (match-end 3)
  4985. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4986. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4987. t)))
  4988. (defvar org-target-link-regexp nil
  4989. "Regular expression matching radio targets in plain text.")
  4990. (make-variable-buffer-local 'org-target-link-regexp)
  4991. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4992. "Regular expression matching a link target.")
  4993. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4994. "Regular expression matching a radio target.")
  4995. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4996. "Regular expression matching any target.")
  4997. (defun org-activate-target-links (limit)
  4998. "Run through the buffer and add overlays to target matches."
  4999. (when org-target-link-regexp
  5000. (let ((case-fold-search t))
  5001. (if (re-search-forward org-target-link-regexp limit t)
  5002. (progn
  5003. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5004. (add-text-properties (match-beginning 0) (match-end 0)
  5005. (list 'mouse-face 'highlight
  5006. 'keymap org-mouse-map
  5007. 'help-echo "Radio target link"
  5008. 'org-linked-text t))
  5009. (org-rear-nonsticky-at (match-end 0))
  5010. t)))))
  5011. (defun org-update-radio-target-regexp ()
  5012. "Find all radio targets in this file and update the regular expression."
  5013. (interactive)
  5014. (when (memq 'radio org-activate-links)
  5015. (setq org-target-link-regexp
  5016. (org-make-target-link-regexp (org-all-targets 'radio)))
  5017. (org-restart-font-lock)))
  5018. (defun org-hide-wide-columns (limit)
  5019. (let (s e)
  5020. (setq s (text-property-any (point) (or limit (point-max))
  5021. 'org-cwidth t))
  5022. (when s
  5023. (setq e (next-single-property-change s 'org-cwidth))
  5024. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5025. (goto-char e)
  5026. t)))
  5027. (defvar org-latex-and-specials-regexp nil
  5028. "Regular expression for highlighting export special stuff.")
  5029. (defvar org-match-substring-regexp)
  5030. (defvar org-match-substring-with-braces-regexp)
  5031. ;; This should be with the exporter code, but we also use if for font-locking
  5032. (defconst org-export-html-special-string-regexps
  5033. '(("\\\\-" . "&shy;")
  5034. ("---\\([^-]\\)" . "&mdash;\\1")
  5035. ("--\\([^-]\\)" . "&ndash;\\1")
  5036. ("\\.\\.\\." . "&hellip;"))
  5037. "Regular expressions for special string conversion.")
  5038. (defun org-compute-latex-and-specials-regexp ()
  5039. "Compute regular expression for stuff treated specially by exporters."
  5040. (if (not org-highlight-latex-fragments-and-specials)
  5041. (org-set-local 'org-latex-and-specials-regexp nil)
  5042. (require 'org-exp)
  5043. (let*
  5044. ((matchers (plist-get org-format-latex-options :matchers))
  5045. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5046. org-latex-regexps)))
  5047. (org-export-allow-BIND nil)
  5048. (options (org-combine-plists (org-default-export-plist)
  5049. (org-infile-export-plist)))
  5050. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5051. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5052. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5053. (org-export-html-expand (plist-get options :expand-quoted-html))
  5054. (org-export-with-special-strings (plist-get options :special-strings))
  5055. (re-sub
  5056. (cond
  5057. ((equal org-export-with-sub-superscripts '{})
  5058. (list org-match-substring-with-braces-regexp))
  5059. (org-export-with-sub-superscripts
  5060. (list org-match-substring-regexp))
  5061. (t nil)))
  5062. (re-latex
  5063. (if org-export-with-LaTeX-fragments
  5064. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5065. (re-macros
  5066. (if org-export-with-TeX-macros
  5067. (list (concat "\\\\"
  5068. (regexp-opt
  5069. (append
  5070. (delq nil
  5071. (mapcar 'car-safe
  5072. (append org-entities-user
  5073. org-entities)))
  5074. (if (boundp 'org-latex-entities)
  5075. (mapcar (lambda (x)
  5076. (or (car-safe x) x))
  5077. org-latex-entities)
  5078. nil))
  5079. 'words))) ; FIXME
  5080. ))
  5081. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5082. (re-special (if org-export-with-special-strings
  5083. (mapcar (lambda (x) (car x))
  5084. org-export-html-special-string-regexps)))
  5085. (re-rest
  5086. (delq nil
  5087. (list
  5088. (if org-export-html-expand "@<[^>\n]+>")
  5089. ))))
  5090. (org-set-local
  5091. 'org-latex-and-specials-regexp
  5092. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5093. re-rest) "\\|")))))
  5094. (defun org-do-latex-and-special-faces (limit)
  5095. "Run through the buffer and add overlays to links."
  5096. (when org-latex-and-specials-regexp
  5097. (let (rtn d)
  5098. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5099. limit t))
  5100. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5101. 'face))
  5102. '(org-code org-verbatim underline)))
  5103. (progn
  5104. (setq rtn t
  5105. d (cond ((member (char-after (1+ (match-beginning 0)))
  5106. '(?_ ?^)) 1)
  5107. (t 0)))
  5108. (font-lock-prepend-text-property
  5109. (+ d (match-beginning 0)) (match-end 0)
  5110. 'face 'org-latex-and-export-specials)
  5111. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5112. '(font-lock-multiline t)))))
  5113. rtn)))
  5114. (defun org-restart-font-lock ()
  5115. "Restart `font-lock-mode', to force refontification."
  5116. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5117. (font-lock-mode -1)
  5118. (font-lock-mode 1)))
  5119. (defun org-all-targets (&optional radio)
  5120. "Return a list of all targets in this file.
  5121. With optional argument RADIO, only find radio targets."
  5122. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5123. rtn)
  5124. (save-excursion
  5125. (goto-char (point-min))
  5126. (while (re-search-forward re nil t)
  5127. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5128. rtn)))
  5129. (defun org-make-target-link-regexp (targets)
  5130. "Make regular expression matching all strings in TARGETS.
  5131. The regular expression finds the targets also if there is a line break
  5132. between words."
  5133. (and targets
  5134. (concat
  5135. "\\<\\("
  5136. (mapconcat
  5137. (lambda (x)
  5138. (setq x (regexp-quote x))
  5139. (while (string-match " +" x)
  5140. (setq x (replace-match "\\s-+" t t x)))
  5141. x)
  5142. targets
  5143. "\\|")
  5144. "\\)\\>")))
  5145. (defun org-activate-tags (limit)
  5146. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5147. (progn
  5148. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5149. (add-text-properties (match-beginning 1) (match-end 1)
  5150. (list 'mouse-face 'highlight
  5151. 'keymap org-mouse-map))
  5152. (org-rear-nonsticky-at (match-end 1))
  5153. t)))
  5154. (defun org-outline-level ()
  5155. "Compute the outline level of the heading at point.
  5156. This function assumes that the cursor is at the beginning of a line matched
  5157. by `outline-regexp'. Otherwise it returns garbage.
  5158. If this is called at a normal headline, the level is the number of stars.
  5159. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5160. (save-excursion
  5161. (looking-at org-outline-regexp)
  5162. (1- (- (match-end 0) (match-beginning 0)))))
  5163. (defvar org-font-lock-keywords nil)
  5164. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5165. "Regular expression matching a property line.")
  5166. (defvar org-font-lock-hook nil
  5167. "Functions to be called for special font lock stuff.")
  5168. (defvar org-font-lock-set-keywords-hook nil
  5169. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5170. This is called after `org-font-lock-extra-keywords' is defined, but before
  5171. it is installed to be used by font lock. This can be useful if something
  5172. needs to be inserted at a specific position in the font-lock sequence.")
  5173. (defun org-font-lock-hook (limit)
  5174. (run-hook-with-args 'org-font-lock-hook limit))
  5175. (defun org-set-font-lock-defaults ()
  5176. (let* ((em org-fontify-emphasized-text)
  5177. (lk org-activate-links)
  5178. (org-font-lock-extra-keywords
  5179. (list
  5180. ;; Call the hook
  5181. '(org-font-lock-hook)
  5182. ;; Headlines
  5183. `(,(if org-fontify-whole-heading-line
  5184. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5185. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5186. (1 (org-get-level-face 1))
  5187. (2 (org-get-level-face 2))
  5188. (3 (org-get-level-face 3)))
  5189. ;; Table lines
  5190. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5191. (1 'org-table t))
  5192. ;; Table internals
  5193. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5194. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5195. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5196. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5197. ;; Drawers
  5198. (list org-drawer-regexp '(0 'org-special-keyword t))
  5199. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5200. ;; Properties
  5201. (list org-property-re
  5202. '(1 'org-special-keyword t)
  5203. '(3 'org-property-value t))
  5204. ;; Links
  5205. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5206. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5207. (if (memq 'plain lk) '(org-activate-plain-links))
  5208. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5209. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5210. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5211. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5212. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5213. '(org-hide-wide-columns (0 nil append))
  5214. ;; TODO keyword
  5215. (list (format org-heading-keyword-regexp-format
  5216. org-todo-regexp)
  5217. '(2 (org-get-todo-face 2) t))
  5218. ;; DONE
  5219. (if org-fontify-done-headline
  5220. (list (format org-heading-keyword-regexp-format
  5221. (concat
  5222. "\\(?:"
  5223. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5224. "\\)"))
  5225. '(2 'org-headline-done t))
  5226. nil)
  5227. ;; Priorities
  5228. '(org-font-lock-add-priority-faces)
  5229. ;; Tags
  5230. '(org-font-lock-add-tag-faces)
  5231. ;; Special keywords
  5232. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5233. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5234. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5235. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5236. ;; Emphasis
  5237. (if em
  5238. (if (featurep 'xemacs)
  5239. '(org-do-emphasis-faces (0 nil append))
  5240. '(org-do-emphasis-faces)))
  5241. ;; Checkboxes
  5242. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5243. 1 'org-checkbox prepend)
  5244. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5245. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5246. (0 (org-get-checkbox-statistics-face) t)))
  5247. ;; Description list items
  5248. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5249. 1 'org-list-dt prepend)
  5250. ;; ARCHIVEd headings
  5251. (list (concat
  5252. org-outline-regexp-bol
  5253. "\\(.*:" org-archive-tag ":.*\\)")
  5254. '(1 'org-archived prepend))
  5255. ;; Specials
  5256. '(org-do-latex-and-special-faces)
  5257. '(org-fontify-entities)
  5258. '(org-raise-scripts)
  5259. ;; Code
  5260. '(org-activate-code (1 'org-code t))
  5261. ;; COMMENT
  5262. (list (format org-heading-keyword-regexp-format
  5263. (concat "\\("
  5264. org-comment-string "\\|" org-quote-string
  5265. "\\)"))
  5266. '(2 'org-special-keyword t))
  5267. '("^#.*" (0 'font-lock-comment-face t))
  5268. ;; Blocks and meta lines
  5269. '(org-fontify-meta-lines-and-blocks)
  5270. )))
  5271. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5272. (run-hooks 'org-font-lock-set-keywords-hook)
  5273. ;; Now set the full font-lock-keywords
  5274. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5275. (org-set-local 'font-lock-defaults
  5276. '(org-font-lock-keywords t nil nil backward-paragraph))
  5277. (kill-local-variable 'font-lock-keywords) nil))
  5278. (defun org-toggle-pretty-entities ()
  5279. "Toggle the composition display of entities as UTF8 characters."
  5280. (interactive)
  5281. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5282. (org-restart-font-lock)
  5283. (if org-pretty-entities
  5284. (message "Entities are displayed as UTF8 characters")
  5285. (save-restriction
  5286. (widen)
  5287. (org-decompose-region (point-min) (point-max))
  5288. (message "Entities are displayed plain"))))
  5289. (defun org-fontify-entities (limit)
  5290. "Find an entity to fontify."
  5291. (let (ee)
  5292. (when org-pretty-entities
  5293. (catch 'match
  5294. (while (re-search-forward
  5295. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5296. limit t)
  5297. (if (and (not (org-in-indented-comment-line))
  5298. (setq ee (org-entity-get (match-string 1)))
  5299. (= (length (nth 6 ee)) 1))
  5300. (let*
  5301. ((end (if (equal (match-string 2) "{}")
  5302. (match-end 2)
  5303. (match-end 1))))
  5304. (add-text-properties
  5305. (match-beginning 0) end
  5306. (list 'font-lock-fontified t))
  5307. (compose-region (match-beginning 0) end
  5308. (nth 6 ee) nil)
  5309. (backward-char 1)
  5310. (throw 'match t))))
  5311. nil))))
  5312. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5313. "Fontify string S like in Org-mode."
  5314. (with-temp-buffer
  5315. (insert s)
  5316. (let ((org-odd-levels-only odd-levels))
  5317. (org-mode)
  5318. (font-lock-fontify-buffer)
  5319. (buffer-string))))
  5320. (defvar org-m nil)
  5321. (defvar org-l nil)
  5322. (defvar org-f nil)
  5323. (defun org-get-level-face (n)
  5324. "Get the right face for match N in font-lock matching of headlines."
  5325. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5326. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5327. (if org-cycle-level-faces
  5328. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5329. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5330. (cond
  5331. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5332. ((eq n 2) org-f)
  5333. (t (if org-level-color-stars-only nil org-f))))
  5334. (defun org-get-todo-face (kwd)
  5335. "Get the right face for a TODO keyword KWD.
  5336. If KWD is a number, get the corresponding match group."
  5337. (if (numberp kwd) (setq kwd (match-string kwd)))
  5338. (or (org-face-from-face-or-color
  5339. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5340. (and (member kwd org-done-keywords) 'org-done)
  5341. 'org-todo))
  5342. (defun org-face-from-face-or-color (context inherit face-or-color)
  5343. "Create a face list that inherits INHERIT, but sets the foreground color.
  5344. When FACE-OR-COLOR is not a string, just return it."
  5345. (if (stringp face-or-color)
  5346. (list :inherit inherit
  5347. (cdr (assoc context org-faces-easy-properties))
  5348. face-or-color)
  5349. face-or-color))
  5350. (defun org-font-lock-add-tag-faces (limit)
  5351. "Add the special tag faces."
  5352. (when (and org-tag-faces org-tags-special-faces-re)
  5353. (while (re-search-forward org-tags-special-faces-re limit t)
  5354. (add-text-properties (match-beginning 1) (match-end 1)
  5355. (list 'face (org-get-tag-face 1)
  5356. 'font-lock-fontified t))
  5357. (backward-char 1))))
  5358. (defun org-font-lock-add-priority-faces (limit)
  5359. "Add the special priority faces."
  5360. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5361. (add-text-properties
  5362. (match-beginning 0) (match-end 0)
  5363. (list 'face (or (org-face-from-face-or-color
  5364. 'priority 'org-special-keyword
  5365. (cdr (assoc (char-after (match-beginning 1))
  5366. org-priority-faces)))
  5367. 'org-special-keyword)
  5368. 'font-lock-fontified t))))
  5369. (defun org-get-tag-face (kwd)
  5370. "Get the right face for a TODO keyword KWD.
  5371. If KWD is a number, get the corresponding match group."
  5372. (if (numberp kwd) (setq kwd (match-string kwd)))
  5373. (or (org-face-from-face-or-color
  5374. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5375. 'org-tag))
  5376. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5377. "Remove fontification and activation overlays from links."
  5378. (font-lock-default-unfontify-region beg end)
  5379. (let* ((buffer-undo-list t)
  5380. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5381. (inhibit-modification-hooks t)
  5382. deactivate-mark buffer-file-name buffer-file-truename)
  5383. (org-decompose-region beg end)
  5384. (remove-text-properties beg end
  5385. '(mouse-face t keymap t org-linked-text t
  5386. invisible t intangible t
  5387. org-no-flyspell t org-emphasis t))
  5388. (org-remove-font-lock-display-properties beg end)))
  5389. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5390. ((raise 0.3) (height 0.7))
  5391. ((raise -0.5))
  5392. ((raise 0.5)))
  5393. "Display properties for showing superscripts and subscripts.")
  5394. (defun org-remove-font-lock-display-properties (beg end)
  5395. "Remove specific display properties that have been added by font lock.
  5396. The will remove the raise properties that are used to show superscripts
  5397. and subscripts."
  5398. (let (next prop)
  5399. (while (< beg end)
  5400. (setq next (next-single-property-change beg 'display nil end)
  5401. prop (get-text-property beg 'display))
  5402. (if (member prop org-script-display)
  5403. (put-text-property beg next 'display nil))
  5404. (setq beg next))))
  5405. (defun org-raise-scripts (limit)
  5406. "Add raise properties to sub/superscripts."
  5407. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5408. (if (re-search-forward
  5409. (if (eq org-use-sub-superscripts t)
  5410. org-match-substring-regexp
  5411. org-match-substring-with-braces-regexp)
  5412. limit t)
  5413. (let* ((pos (point)) table-p comment-p
  5414. (mpos (match-beginning 3))
  5415. (emph-p (get-text-property mpos 'org-emphasis))
  5416. (link-p (get-text-property mpos 'mouse-face))
  5417. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5418. (goto-char (point-at-bol))
  5419. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5420. comment-p (org-looking-at-p "[ \t]*#"))
  5421. (goto-char pos)
  5422. ;; FIXME: Should we go back one character here, for a_b^c
  5423. ;; (goto-char (1- pos)) ;????????????????????
  5424. (if (or comment-p emph-p link-p keyw-p)
  5425. t
  5426. (put-text-property (match-beginning 3) (match-end 0)
  5427. 'display
  5428. (if (equal (char-after (match-beginning 2)) ?^)
  5429. (nth (if table-p 3 1) org-script-display)
  5430. (nth (if table-p 2 0) org-script-display)))
  5431. (add-text-properties (match-beginning 2) (match-end 2)
  5432. (list 'invisible t
  5433. 'org-dwidth t 'org-dwidth-n 1))
  5434. (if (and (eq (char-after (match-beginning 3)) ?{)
  5435. (eq (char-before (match-end 3)) ?}))
  5436. (progn
  5437. (add-text-properties
  5438. (match-beginning 3) (1+ (match-beginning 3))
  5439. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5440. (add-text-properties
  5441. (1- (match-end 3)) (match-end 3)
  5442. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5443. t)))))
  5444. ;;;; Visibility cycling, including org-goto and indirect buffer
  5445. ;;; Cycling
  5446. (defvar org-cycle-global-status nil)
  5447. (make-variable-buffer-local 'org-cycle-global-status)
  5448. (defvar org-cycle-subtree-status nil)
  5449. (make-variable-buffer-local 'org-cycle-subtree-status)
  5450. ;;;###autoload
  5451. (defvar org-inlinetask-min-level)
  5452. (defun org-cycle (&optional arg)
  5453. "TAB-action and visibility cycling for Org-mode.
  5454. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5455. is outline visibility cycling, but it also invokes other actions
  5456. in special contexts.
  5457. - When this function is called with a prefix argument, rotate the entire
  5458. buffer through 3 states (global cycling)
  5459. 1. OVERVIEW: Show only top-level headlines.
  5460. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5461. 3. SHOW ALL: Show everything.
  5462. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5463. determined by the variable `org-startup-folded', and by any VISIBILITY
  5464. properties in the buffer.
  5465. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5466. including any drawers.
  5467. - When inside a table, re-align the table and move to the next field.
  5468. - When point is at the beginning of a headline, rotate the subtree started
  5469. by this line through 3 different states (local cycling)
  5470. 1. FOLDED: Only the main headline is shown.
  5471. 2. CHILDREN: The main headline and the direct children are shown.
  5472. From this state, you can move to one of the children
  5473. and zoom in further.
  5474. 3. SUBTREE: Show the entire subtree, including body text.
  5475. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5476. - When point is at the beginning of an empty headline and the variable
  5477. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5478. of the headline by demoting and promoting it to likely levels. This
  5479. speeds up creation document structure by pressing TAB once or several
  5480. times right after creating a new headline.
  5481. - When there is a numeric prefix, go up to a heading with level ARG, do
  5482. a `show-subtree' and return to the previous cursor position. If ARG
  5483. is negative, go up that many levels.
  5484. - When point is not at the beginning of a headline, execute the global
  5485. binding for TAB, which is re-indenting the line. See the option
  5486. `org-cycle-emulate-tab' for details.
  5487. - Special case: if point is at the beginning of the buffer and there is
  5488. no headline in line 1, this function will act as if called with prefix arg
  5489. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5490. But only if also the variable `org-cycle-global-at-bob' is t."
  5491. (interactive "P")
  5492. (org-load-modules-maybe)
  5493. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5494. (and org-cycle-level-after-item/entry-creation
  5495. (or (org-cycle-level)
  5496. (org-cycle-item-indentation))))
  5497. (let* ((limit-level
  5498. (or org-cycle-max-level
  5499. (and (boundp 'org-inlinetask-min-level)
  5500. org-inlinetask-min-level
  5501. (1- org-inlinetask-min-level))))
  5502. (nstars (and limit-level
  5503. (if org-odd-levels-only
  5504. (and limit-level (1- (* limit-level 2)))
  5505. limit-level)))
  5506. (org-outline-regexp
  5507. (if (not (derived-mode-p 'org-mode))
  5508. outline-regexp
  5509. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5510. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5511. (not (looking-at org-outline-regexp))))
  5512. (org-cycle-hook
  5513. (if bob-special
  5514. (delq 'org-optimize-window-after-visibility-change
  5515. (copy-sequence org-cycle-hook))
  5516. org-cycle-hook))
  5517. (pos (point)))
  5518. (if (or bob-special (equal arg '(4)))
  5519. ;; special case: use global cycling
  5520. (setq arg t))
  5521. (cond
  5522. ((equal arg '(16))
  5523. (setq last-command 'dummy)
  5524. (org-set-startup-visibility)
  5525. (message "Startup visibility, plus VISIBILITY properties"))
  5526. ((equal arg '(64))
  5527. (show-all)
  5528. (message "Entire buffer visible, including drawers"))
  5529. ;; Table: enter it or move to the next field.
  5530. ((org-at-table-p 'any)
  5531. (if (org-at-table.el-p)
  5532. (message "Use C-c ' to edit table.el tables")
  5533. (if arg (org-table-edit-field t)
  5534. (org-table-justify-field-maybe)
  5535. (call-interactively 'org-table-next-field))))
  5536. ((run-hook-with-args-until-success
  5537. 'org-tab-after-check-for-table-hook))
  5538. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5539. ((eq arg t) (org-cycle-internal-global))
  5540. ;; Drawers: delegate to `org-flag-drawer'.
  5541. ((and org-drawers org-drawer-regexp
  5542. (save-excursion
  5543. (beginning-of-line 1)
  5544. (looking-at org-drawer-regexp)))
  5545. (org-flag-drawer ; toggle block visibility
  5546. (not (get-char-property (match-end 0) 'invisible))))
  5547. ;; Show-subtree, ARG levels up from here.
  5548. ((integerp arg)
  5549. (save-excursion
  5550. (org-back-to-heading)
  5551. (outline-up-heading (if (< arg 0) (- arg)
  5552. (- (funcall outline-level) arg)))
  5553. (org-show-subtree)))
  5554. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5555. ((and (featurep 'org-inlinetask)
  5556. (org-inlinetask-at-task-p)
  5557. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5558. (org-inlinetask-toggle-visibility))
  5559. ((org-try-cdlatex-tab))
  5560. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5561. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5562. (save-excursion (beginning-of-line 1)
  5563. (looking-at org-outline-regexp)))
  5564. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5565. (org-cycle-internal-local))
  5566. ;; From there: TAB emulation and template completion.
  5567. (buffer-read-only (org-back-to-heading))
  5568. ((run-hook-with-args-until-success
  5569. 'org-tab-after-check-for-cycling-hook))
  5570. ((org-try-structure-completion))
  5571. ((run-hook-with-args-until-success
  5572. 'org-tab-before-tab-emulation-hook))
  5573. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5574. (or (not (bolp))
  5575. (not (looking-at org-outline-regexp))))
  5576. (call-interactively (global-key-binding "\t")))
  5577. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5578. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5579. (or (and (eq org-cycle-emulate-tab 'white)
  5580. (= (match-end 0) (point-at-eol)))
  5581. (and (eq org-cycle-emulate-tab 'whitestart)
  5582. (>= (match-end 0) pos))))
  5583. t
  5584. (eq org-cycle-emulate-tab t))
  5585. (call-interactively (global-key-binding "\t")))
  5586. (t (save-excursion
  5587. (org-back-to-heading)
  5588. (org-cycle)))))))
  5589. (defun org-cycle-internal-global ()
  5590. "Do the global cycling action."
  5591. ;; Hack to avoid display of messages for .org attachments in Gnus
  5592. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5593. (cond
  5594. ((and (eq last-command this-command)
  5595. (eq org-cycle-global-status 'overview))
  5596. ;; We just created the overview - now do table of contents
  5597. ;; This can be slow in very large buffers, so indicate action
  5598. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5599. (unless ga (message "CONTENTS..."))
  5600. (org-content)
  5601. (unless ga (message "CONTENTS...done"))
  5602. (setq org-cycle-global-status 'contents)
  5603. (run-hook-with-args 'org-cycle-hook 'contents))
  5604. ((and (eq last-command this-command)
  5605. (eq org-cycle-global-status 'contents))
  5606. ;; We just showed the table of contents - now show everything
  5607. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5608. (show-all)
  5609. (unless ga (message "SHOW ALL"))
  5610. (setq org-cycle-global-status 'all)
  5611. (run-hook-with-args 'org-cycle-hook 'all))
  5612. (t
  5613. ;; Default action: go to overview
  5614. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5615. (org-overview)
  5616. (unless ga (message "OVERVIEW"))
  5617. (setq org-cycle-global-status 'overview)
  5618. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5619. (defun org-cycle-internal-local ()
  5620. "Do the local cycling action."
  5621. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5622. ;; First, determine end of headline (EOH), end of subtree or item
  5623. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5624. (save-excursion
  5625. (if (org-at-item-p)
  5626. (progn
  5627. (beginning-of-line)
  5628. (setq struct (org-list-struct))
  5629. (setq eoh (point-at-eol))
  5630. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5631. (setq has-children (org-list-has-child-p (point) struct)))
  5632. (org-back-to-heading)
  5633. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5634. (setq eos (save-excursion
  5635. (org-end-of-subtree t)
  5636. (unless (eobp)
  5637. (skip-chars-forward " \t\n"))
  5638. (if (eobp) (point) (1- (point)))))
  5639. (setq has-children
  5640. (or (save-excursion
  5641. (let ((level (funcall outline-level)))
  5642. (outline-next-heading)
  5643. (and (org-at-heading-p t)
  5644. (> (funcall outline-level) level))))
  5645. (save-excursion
  5646. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5647. ;; Determine end invisible part of buffer (EOL)
  5648. (beginning-of-line 2)
  5649. ;; XEmacs doesn't have `next-single-char-property-change'
  5650. (if (featurep 'xemacs)
  5651. (while (and (not (eobp)) ;; this is like `next-line'
  5652. (get-char-property (1- (point)) 'invisible))
  5653. (beginning-of-line 2))
  5654. (while (and (not (eobp)) ;; this is like `next-line'
  5655. (get-char-property (1- (point)) 'invisible))
  5656. (goto-char (next-single-char-property-change (point) 'invisible))
  5657. (and (eolp) (beginning-of-line 2))))
  5658. (setq eol (point)))
  5659. ;; Find out what to do next and set `this-command'
  5660. (cond
  5661. ((= eos eoh)
  5662. ;; Nothing is hidden behind this heading
  5663. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5664. (message "EMPTY ENTRY")
  5665. (setq org-cycle-subtree-status nil)
  5666. (save-excursion
  5667. (goto-char eos)
  5668. (outline-next-heading)
  5669. (if (outline-invisible-p) (org-flag-heading nil))))
  5670. ((and (or (>= eol eos)
  5671. (not (string-match "\\S-" (buffer-substring eol eos))))
  5672. (or has-children
  5673. (not (setq children-skipped
  5674. org-cycle-skip-children-state-if-no-children))))
  5675. ;; Entire subtree is hidden in one line: children view
  5676. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5677. (if (org-at-item-p)
  5678. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5679. (org-show-entry)
  5680. (org-with-limited-levels (show-children))
  5681. ;; FIXME: This slows down the func way too much.
  5682. ;; How keep drawers hidden in subtree anyway?
  5683. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5684. ;; (org-cycle-hide-drawers 'subtree))
  5685. ;; Fold every list in subtree to top-level items.
  5686. (when (eq org-cycle-include-plain-lists 'integrate)
  5687. (save-excursion
  5688. (org-back-to-heading)
  5689. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5690. (beginning-of-line 1)
  5691. (let* ((struct (org-list-struct))
  5692. (prevs (org-list-prevs-alist struct))
  5693. (end (org-list-get-bottom-point struct)))
  5694. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5695. (org-list-get-all-items (point) struct prevs))
  5696. (goto-char end))))))
  5697. (message "CHILDREN")
  5698. (save-excursion
  5699. (goto-char eos)
  5700. (outline-next-heading)
  5701. (if (outline-invisible-p) (org-flag-heading nil)))
  5702. (setq org-cycle-subtree-status 'children)
  5703. (run-hook-with-args 'org-cycle-hook 'children))
  5704. ((or children-skipped
  5705. (and (eq last-command this-command)
  5706. (eq org-cycle-subtree-status 'children)))
  5707. ;; We just showed the children, or no children are there,
  5708. ;; now show everything.
  5709. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5710. (outline-flag-region eoh eos nil)
  5711. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5712. (setq org-cycle-subtree-status 'subtree)
  5713. (run-hook-with-args 'org-cycle-hook 'subtree))
  5714. (t
  5715. ;; Default action: hide the subtree.
  5716. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5717. (outline-flag-region eoh eos t)
  5718. (message "FOLDED")
  5719. (setq org-cycle-subtree-status 'folded)
  5720. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5721. ;;;###autoload
  5722. (defun org-global-cycle (&optional arg)
  5723. "Cycle the global visibility. For details see `org-cycle'.
  5724. With \\[universal-argument] prefix arg, switch to startup visibility.
  5725. With a numeric prefix, show all headlines up to that level."
  5726. (interactive "P")
  5727. (let ((org-cycle-include-plain-lists
  5728. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5729. (cond
  5730. ((integerp arg)
  5731. (show-all)
  5732. (hide-sublevels arg)
  5733. (setq org-cycle-global-status 'contents))
  5734. ((equal arg '(4))
  5735. (org-set-startup-visibility)
  5736. (message "Startup visibility, plus VISIBILITY properties."))
  5737. (t
  5738. (org-cycle '(4))))))
  5739. (defun org-set-startup-visibility ()
  5740. "Set the visibility required by startup options and properties."
  5741. (cond
  5742. ((eq org-startup-folded t)
  5743. (org-cycle '(4)))
  5744. ((eq org-startup-folded 'content)
  5745. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5746. (org-cycle '(4)) (org-cycle '(4)))))
  5747. (unless (eq org-startup-folded 'showeverything)
  5748. (if org-hide-block-startup (org-hide-block-all))
  5749. (org-set-visibility-according-to-property 'no-cleanup)
  5750. (org-cycle-hide-archived-subtrees 'all)
  5751. (org-cycle-hide-drawers 'all)
  5752. (org-cycle-show-empty-lines t)))
  5753. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5754. "Switch subtree visibilities according to :VISIBILITY: property."
  5755. (interactive)
  5756. (let (org-show-entry-below state)
  5757. (save-excursion
  5758. (goto-char (point-min))
  5759. (while (re-search-forward
  5760. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5761. nil t)
  5762. (setq state (match-string 1))
  5763. (save-excursion
  5764. (org-back-to-heading t)
  5765. (hide-subtree)
  5766. (org-reveal)
  5767. (cond
  5768. ((equal state '("fold" "folded"))
  5769. (hide-subtree))
  5770. ((equal state "children")
  5771. (org-show-hidden-entry)
  5772. (show-children))
  5773. ((equal state "content")
  5774. (save-excursion
  5775. (save-restriction
  5776. (org-narrow-to-subtree)
  5777. (org-content))))
  5778. ((member state '("all" "showall"))
  5779. (show-subtree)))))
  5780. (unless no-cleanup
  5781. (org-cycle-hide-archived-subtrees 'all)
  5782. (org-cycle-hide-drawers 'all)
  5783. (org-cycle-show-empty-lines 'all)))))
  5784. ;; This function uses outline-regexp instead of the more fundamental
  5785. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5786. ;; buffers, where outline-regexp is needed.
  5787. (defun org-overview ()
  5788. "Switch to overview mode, showing only top-level headlines.
  5789. Really, this shows all headlines with level equal or greater than the level
  5790. of the first headline in the buffer. This is important, because if the
  5791. first headline is not level one, then (hide-sublevels 1) gives confusing
  5792. results."
  5793. (interactive)
  5794. (let ((level (save-excursion
  5795. (goto-char (point-min))
  5796. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5797. (progn
  5798. (goto-char (match-beginning 0))
  5799. (funcall outline-level))))))
  5800. (and level (hide-sublevels level))))
  5801. (defun org-content (&optional arg)
  5802. "Show all headlines in the buffer, like a table of contents.
  5803. With numerical argument N, show content up to level N."
  5804. (interactive "P")
  5805. (save-excursion
  5806. ;; Visit all headings and show their offspring
  5807. (and (integerp arg) (org-overview))
  5808. (goto-char (point-max))
  5809. (catch 'exit
  5810. (while (and (progn (condition-case nil
  5811. (outline-previous-visible-heading 1)
  5812. (error (goto-char (point-min))))
  5813. t)
  5814. (looking-at org-outline-regexp))
  5815. (if (integerp arg)
  5816. (show-children (1- arg))
  5817. (show-branches))
  5818. (if (bobp) (throw 'exit nil))))))
  5819. (defun org-optimize-window-after-visibility-change (state)
  5820. "Adjust the window after a change in outline visibility.
  5821. This function is the default value of the hook `org-cycle-hook'."
  5822. (when (get-buffer-window (current-buffer))
  5823. (cond
  5824. ((eq state 'content) nil)
  5825. ((eq state 'all) nil)
  5826. ((eq state 'folded) nil)
  5827. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5828. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5829. (defun org-remove-empty-overlays-at (pos)
  5830. "Remove outline overlays that do not contain non-white stuff."
  5831. (mapc
  5832. (lambda (o)
  5833. (and (eq 'outline (overlay-get o 'invisible))
  5834. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5835. (overlay-end o))))
  5836. (delete-overlay o)))
  5837. (overlays-at pos)))
  5838. (defun org-clean-visibility-after-subtree-move ()
  5839. "Fix visibility issues after moving a subtree."
  5840. ;; First, find a reasonable region to look at:
  5841. ;; Start two siblings above, end three below
  5842. (let* ((beg (save-excursion
  5843. (and (org-get-last-sibling)
  5844. (org-get-last-sibling))
  5845. (point)))
  5846. (end (save-excursion
  5847. (and (org-get-next-sibling)
  5848. (org-get-next-sibling)
  5849. (org-get-next-sibling))
  5850. (if (org-at-heading-p)
  5851. (point-at-eol)
  5852. (point))))
  5853. (level (looking-at "\\*+"))
  5854. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5855. (save-excursion
  5856. (save-restriction
  5857. (narrow-to-region beg end)
  5858. (when re
  5859. ;; Properly fold already folded siblings
  5860. (goto-char (point-min))
  5861. (while (re-search-forward re nil t)
  5862. (if (and (not (outline-invisible-p))
  5863. (save-excursion
  5864. (goto-char (point-at-eol)) (outline-invisible-p)))
  5865. (hide-entry))))
  5866. (org-cycle-show-empty-lines 'overview)
  5867. (org-cycle-hide-drawers 'overview)))))
  5868. (defun org-cycle-show-empty-lines (state)
  5869. "Show empty lines above all visible headlines.
  5870. The region to be covered depends on STATE when called through
  5871. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5872. entire buffer covered. Note that an empty line is only shown if there
  5873. are at least `org-cycle-separator-lines' empty lines before the headline."
  5874. (when (not (= org-cycle-separator-lines 0))
  5875. (save-excursion
  5876. (let* ((n (abs org-cycle-separator-lines))
  5877. (re (cond
  5878. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5879. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5880. (t (let ((ns (number-to-string (- n 2))))
  5881. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5882. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5883. beg end b e)
  5884. (cond
  5885. ((memq state '(overview contents t))
  5886. (setq beg (point-min) end (point-max)))
  5887. ((memq state '(children folded))
  5888. (setq beg (point) end (progn (org-end-of-subtree t t)
  5889. (beginning-of-line 2)
  5890. (point)))))
  5891. (when beg
  5892. (goto-char beg)
  5893. (while (re-search-forward re end t)
  5894. (unless (get-char-property (match-end 1) 'invisible)
  5895. (setq e (match-end 1))
  5896. (if (< org-cycle-separator-lines 0)
  5897. (setq b (save-excursion
  5898. (goto-char (match-beginning 0))
  5899. (org-back-over-empty-lines)
  5900. (if (save-excursion
  5901. (goto-char (max (point-min) (1- (point))))
  5902. (org-at-heading-p))
  5903. (1- (point))
  5904. (point))))
  5905. (setq b (match-beginning 1)))
  5906. (outline-flag-region b e nil)))))))
  5907. ;; Never hide empty lines at the end of the file.
  5908. (save-excursion
  5909. (goto-char (point-max))
  5910. (outline-previous-heading)
  5911. (outline-end-of-heading)
  5912. (if (and (looking-at "[ \t\n]+")
  5913. (= (match-end 0) (point-max)))
  5914. (outline-flag-region (point) (match-end 0) nil))))
  5915. (defun org-show-empty-lines-in-parent ()
  5916. "Move to the parent and re-show empty lines before visible headlines."
  5917. (save-excursion
  5918. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5919. (org-cycle-show-empty-lines context))))
  5920. (defun org-files-list ()
  5921. "Return `org-agenda-files' list, plus all open org-mode files.
  5922. This is useful for operations that need to scan all of a user's
  5923. open and agenda-wise Org files."
  5924. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5925. (dolist (buf (buffer-list))
  5926. (with-current-buffer buf
  5927. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5928. (let ((file (expand-file-name (buffer-file-name))))
  5929. (unless (member file files)
  5930. (push file files))))))
  5931. files))
  5932. (defsubst org-entry-beginning-position ()
  5933. "Return the beginning position of the current entry."
  5934. (save-excursion (outline-back-to-heading t) (point)))
  5935. (defsubst org-entry-end-position ()
  5936. "Return the end position of the current entry."
  5937. (save-excursion (outline-next-heading) (point)))
  5938. (defun org-cycle-hide-drawers (state)
  5939. "Re-hide all drawers after a visibility state change."
  5940. (when (and (derived-mode-p 'org-mode)
  5941. (not (memq state '(overview folded contents))))
  5942. (save-excursion
  5943. (let* ((globalp (memq state '(contents all)))
  5944. (beg (if globalp (point-min) (point)))
  5945. (end (if globalp (point-max)
  5946. (if (eq state 'children)
  5947. (save-excursion (outline-next-heading) (point))
  5948. (org-end-of-subtree t)))))
  5949. (goto-char beg)
  5950. (while (re-search-forward org-drawer-regexp end t)
  5951. (org-flag-drawer t))))))
  5952. (defun org-flag-drawer (flag)
  5953. (save-excursion
  5954. (beginning-of-line 1)
  5955. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5956. (let ((b (match-end 0)))
  5957. (if (re-search-forward
  5958. "^[ \t]*:END:"
  5959. (save-excursion (outline-next-heading) (point)) t)
  5960. (outline-flag-region b (point-at-eol) flag)
  5961. (error ":END: line missing at position %s" b))))))
  5962. (defun org-subtree-end-visible-p ()
  5963. "Is the end of the current subtree visible?"
  5964. (pos-visible-in-window-p
  5965. (save-excursion (org-end-of-subtree t) (point))))
  5966. (defun org-first-headline-recenter (&optional N)
  5967. "Move cursor to the first headline and recenter the headline.
  5968. Optional argument N means put the headline into the Nth line of the window."
  5969. (goto-char (point-min))
  5970. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5971. (beginning-of-line)
  5972. (recenter (prefix-numeric-value N))))
  5973. ;;; Saving and restoring visibility
  5974. (defun org-outline-overlay-data (&optional use-markers)
  5975. "Return a list of the locations of all outline overlays.
  5976. These are overlays with the `invisible' property value `outline'.
  5977. The return value is a list of cons cells, with start and stop
  5978. positions for each overlay.
  5979. If USE-MARKERS is set, return the positions as markers."
  5980. (let (beg end)
  5981. (save-excursion
  5982. (save-restriction
  5983. (widen)
  5984. (delq nil
  5985. (mapcar (lambda (o)
  5986. (when (eq (overlay-get o 'invisible) 'outline)
  5987. (setq beg (overlay-start o)
  5988. end (overlay-end o))
  5989. (and beg end (> end beg)
  5990. (if use-markers
  5991. (cons (move-marker (make-marker) beg)
  5992. (move-marker (make-marker) end))
  5993. (cons beg end)))))
  5994. (overlays-in (point-min) (point-max))))))))
  5995. (defun org-set-outline-overlay-data (data)
  5996. "Create visibility overlays for all positions in DATA.
  5997. DATA should have been made by `org-outline-overlay-data'."
  5998. (let (o)
  5999. (save-excursion
  6000. (save-restriction
  6001. (widen)
  6002. (show-all)
  6003. (mapc (lambda (c)
  6004. (outline-flag-region (car c) (cdr c) t))
  6005. data)))))
  6006. ;;; Folding of blocks
  6007. (defvar org-hide-block-overlays nil
  6008. "Overlays hiding blocks.")
  6009. (make-variable-buffer-local 'org-hide-block-overlays)
  6010. (defun org-block-map (function &optional start end)
  6011. "Call FUNCTION at the head of all source blocks in the current buffer.
  6012. Optional arguments START and END can be used to limit the range."
  6013. (let ((start (or start (point-min)))
  6014. (end (or end (point-max))))
  6015. (save-excursion
  6016. (goto-char start)
  6017. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6018. (save-excursion
  6019. (save-match-data
  6020. (goto-char (match-beginning 0))
  6021. (funcall function)))))))
  6022. (defun org-hide-block-toggle-all ()
  6023. "Toggle the visibility of all blocks in the current buffer."
  6024. (org-block-map #'org-hide-block-toggle))
  6025. (defun org-hide-block-all ()
  6026. "Fold all blocks in the current buffer."
  6027. (interactive)
  6028. (org-show-block-all)
  6029. (org-block-map #'org-hide-block-toggle-maybe))
  6030. (defun org-show-block-all ()
  6031. "Unfold all blocks in the current buffer."
  6032. (interactive)
  6033. (mapc 'delete-overlay org-hide-block-overlays)
  6034. (setq org-hide-block-overlays nil))
  6035. (defun org-hide-block-toggle-maybe ()
  6036. "Toggle visibility of block at point."
  6037. (interactive)
  6038. (let ((case-fold-search t))
  6039. (if (save-excursion
  6040. (beginning-of-line 1)
  6041. (looking-at org-block-regexp))
  6042. (progn (org-hide-block-toggle)
  6043. t) ;; to signal that we took action
  6044. nil))) ;; to signal that we did not
  6045. (defun org-hide-block-toggle (&optional force)
  6046. "Toggle the visibility of the current block."
  6047. (interactive)
  6048. (save-excursion
  6049. (beginning-of-line)
  6050. (if (re-search-forward org-block-regexp nil t)
  6051. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6052. (end (match-end 0)) ;; end of entire body
  6053. ov)
  6054. (if (memq t (mapcar (lambda (overlay)
  6055. (eq (overlay-get overlay 'invisible)
  6056. 'org-hide-block))
  6057. (overlays-at start)))
  6058. (if (or (not force) (eq force 'off))
  6059. (mapc (lambda (ov)
  6060. (when (member ov org-hide-block-overlays)
  6061. (setq org-hide-block-overlays
  6062. (delq ov org-hide-block-overlays)))
  6063. (when (eq (overlay-get ov 'invisible)
  6064. 'org-hide-block)
  6065. (delete-overlay ov)))
  6066. (overlays-at start)))
  6067. (setq ov (make-overlay start end))
  6068. (overlay-put ov 'invisible 'org-hide-block)
  6069. ;; make the block accessible to isearch
  6070. (overlay-put
  6071. ov 'isearch-open-invisible
  6072. (lambda (ov)
  6073. (when (member ov org-hide-block-overlays)
  6074. (setq org-hide-block-overlays
  6075. (delq ov org-hide-block-overlays)))
  6076. (when (eq (overlay-get ov 'invisible)
  6077. 'org-hide-block)
  6078. (delete-overlay ov))))
  6079. (push ov org-hide-block-overlays)))
  6080. (error "Not looking at a source block"))))
  6081. ;; org-tab-after-check-for-cycling-hook
  6082. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6083. ;; Remove overlays when changing major mode
  6084. (add-hook 'org-mode-hook
  6085. (lambda () (org-add-hook 'change-major-mode-hook
  6086. 'org-show-block-all 'append 'local)))
  6087. ;;; Org-goto
  6088. (defvar org-goto-window-configuration nil)
  6089. (defvar org-goto-marker nil)
  6090. (defvar org-goto-map
  6091. (let ((map (make-sparse-keymap)))
  6092. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6093. (while (setq cmd (pop cmds))
  6094. (substitute-key-definition cmd cmd map global-map)))
  6095. (suppress-keymap map)
  6096. (org-defkey map "\C-m" 'org-goto-ret)
  6097. (org-defkey map [(return)] 'org-goto-ret)
  6098. (org-defkey map [(left)] 'org-goto-left)
  6099. (org-defkey map [(right)] 'org-goto-right)
  6100. (org-defkey map [(control ?g)] 'org-goto-quit)
  6101. (org-defkey map "\C-i" 'org-cycle)
  6102. (org-defkey map [(tab)] 'org-cycle)
  6103. (org-defkey map [(down)] 'outline-next-visible-heading)
  6104. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6105. (if org-goto-auto-isearch
  6106. (if (fboundp 'define-key-after)
  6107. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6108. nil)
  6109. (org-defkey map "q" 'org-goto-quit)
  6110. (org-defkey map "n" 'outline-next-visible-heading)
  6111. (org-defkey map "p" 'outline-previous-visible-heading)
  6112. (org-defkey map "f" 'outline-forward-same-level)
  6113. (org-defkey map "b" 'outline-backward-same-level)
  6114. (org-defkey map "u" 'outline-up-heading))
  6115. (org-defkey map "/" 'org-occur)
  6116. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6117. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6118. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6119. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6120. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6121. map))
  6122. (defconst org-goto-help
  6123. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6124. RET=jump to location [Q]uit and return to previous location
  6125. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6126. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6127. ;; FIXME: Docstring does not mention both interfaces
  6128. (defun org-goto (&optional alternative-interface)
  6129. "Look up a different location in the current file, keeping current visibility.
  6130. When you want look-up or go to a different location in a
  6131. document, the fastest way is often to fold the entire buffer and
  6132. then dive into the tree. This method has the disadvantage, that
  6133. the previous location will be folded, which may not be what you
  6134. want.
  6135. This command works around this by showing a copy of the current
  6136. buffer in an indirect buffer, in overview mode. You can dive
  6137. into the tree in that copy, use org-occur and incremental search
  6138. to find a location. When pressing RET or `Q', the command
  6139. returns to the original buffer in which the visibility is still
  6140. unchanged. After RET it will also jump to the location selected
  6141. in the indirect buffer and expose the headline hierarchy above.
  6142. With a prefix argument, use the alternative interface: e.g. if
  6143. `org-goto-interface' is 'outline use 'outline-path-completion."
  6144. (interactive "P")
  6145. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6146. (org-refile-use-outline-path t)
  6147. (org-refile-target-verify-function nil)
  6148. (interface
  6149. (if (not alternative-interface)
  6150. org-goto-interface
  6151. (if (eq org-goto-interface 'outline)
  6152. 'outline-path-completion
  6153. 'outline)))
  6154. (org-goto-start-pos (point))
  6155. (selected-point
  6156. (if (eq interface 'outline)
  6157. (car (org-get-location (current-buffer) org-goto-help))
  6158. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6159. (org-refile-check-position pa)
  6160. (nth 3 pa)))))
  6161. (if selected-point
  6162. (progn
  6163. (org-mark-ring-push org-goto-start-pos)
  6164. (goto-char selected-point)
  6165. (if (or (outline-invisible-p) (org-invisible-p2))
  6166. (org-show-context 'org-goto)))
  6167. (message "Quit"))))
  6168. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6169. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6170. (defvar org-goto-local-auto-isearch-map) ; defined below
  6171. (defun org-get-location (buf help)
  6172. "Let the user select a location in the Org-mode buffer BUF.
  6173. This function uses a recursive edit. It returns the selected position
  6174. or nil."
  6175. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6176. (isearch-hide-immediately nil)
  6177. (isearch-search-fun-function
  6178. (lambda () 'org-goto-local-search-headings))
  6179. (org-goto-selected-point org-goto-exit-command)
  6180. (pop-up-frames nil)
  6181. (special-display-buffer-names nil)
  6182. (special-display-regexps nil)
  6183. (special-display-function nil))
  6184. (save-excursion
  6185. (save-window-excursion
  6186. (delete-other-windows)
  6187. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6188. (org-pop-to-buffer-same-window
  6189. (condition-case nil
  6190. (make-indirect-buffer (current-buffer) "*org-goto*")
  6191. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6192. (with-output-to-temp-buffer "*Help*"
  6193. (princ help))
  6194. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6195. (setq buffer-read-only nil)
  6196. (let ((org-startup-truncated t)
  6197. (org-startup-folded nil)
  6198. (org-startup-align-all-tables nil))
  6199. (org-mode)
  6200. (org-overview))
  6201. (setq buffer-read-only t)
  6202. (if (and (boundp 'org-goto-start-pos)
  6203. (integer-or-marker-p org-goto-start-pos))
  6204. (let ((org-show-hierarchy-above t)
  6205. (org-show-siblings t)
  6206. (org-show-following-heading t))
  6207. (goto-char org-goto-start-pos)
  6208. (and (outline-invisible-p) (org-show-context)))
  6209. (goto-char (point-min)))
  6210. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6211. (message "Select location and press RET")
  6212. (use-local-map org-goto-map)
  6213. (recursive-edit)
  6214. ))
  6215. (kill-buffer "*org-goto*")
  6216. (cons org-goto-selected-point org-goto-exit-command)))
  6217. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6218. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6219. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6220. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6221. (defun org-goto-local-search-headings (string bound noerror)
  6222. "Search and make sure that any matches are in headlines."
  6223. (catch 'return
  6224. (while (if isearch-forward
  6225. (search-forward string bound noerror)
  6226. (search-backward string bound noerror))
  6227. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6228. (and (member :headline context)
  6229. (not (member :tags context))))
  6230. (throw 'return (point))))))
  6231. (defun org-goto-local-auto-isearch ()
  6232. "Start isearch."
  6233. (interactive)
  6234. (goto-char (point-min))
  6235. (let ((keys (this-command-keys)))
  6236. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6237. (isearch-mode t)
  6238. (isearch-process-search-char (string-to-char keys)))))
  6239. (defun org-goto-ret (&optional arg)
  6240. "Finish `org-goto' by going to the new location."
  6241. (interactive "P")
  6242. (setq org-goto-selected-point (point)
  6243. org-goto-exit-command 'return)
  6244. (throw 'exit nil))
  6245. (defun org-goto-left ()
  6246. "Finish `org-goto' by going to the new location."
  6247. (interactive)
  6248. (if (org-at-heading-p)
  6249. (progn
  6250. (beginning-of-line 1)
  6251. (setq org-goto-selected-point (point)
  6252. org-goto-exit-command 'left)
  6253. (throw 'exit nil))
  6254. (error "Not on a heading")))
  6255. (defun org-goto-right ()
  6256. "Finish `org-goto' by going to the new location."
  6257. (interactive)
  6258. (if (org-at-heading-p)
  6259. (progn
  6260. (setq org-goto-selected-point (point)
  6261. org-goto-exit-command 'right)
  6262. (throw 'exit nil))
  6263. (error "Not on a heading")))
  6264. (defun org-goto-quit ()
  6265. "Finish `org-goto' without cursor motion."
  6266. (interactive)
  6267. (setq org-goto-selected-point nil)
  6268. (setq org-goto-exit-command 'quit)
  6269. (throw 'exit nil))
  6270. ;;; Indirect buffer display of subtrees
  6271. (defvar org-indirect-dedicated-frame nil
  6272. "This is the frame being used for indirect tree display.")
  6273. (defvar org-last-indirect-buffer nil)
  6274. (defun org-tree-to-indirect-buffer (&optional arg)
  6275. "Create indirect buffer and narrow it to current subtree.
  6276. With numerical prefix ARG, go up to this level and then take that tree.
  6277. If ARG is negative, go up that many levels.
  6278. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6279. indirect buffer previously made with this command, to avoid proliferation of
  6280. indirect buffers. However, when you call the command with a \
  6281. \\[universal-argument] prefix, or
  6282. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6283. is kept so that you can work with several indirect buffers at the same time.
  6284. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6285. \\[universal-argument] prefix also
  6286. requests that a new frame be made for the new buffer, so that the dedicated
  6287. frame is not changed."
  6288. (interactive "P")
  6289. (let ((cbuf (current-buffer))
  6290. (cwin (selected-window))
  6291. (pos (point))
  6292. beg end level heading ibuf)
  6293. (save-excursion
  6294. (org-back-to-heading t)
  6295. (when (numberp arg)
  6296. (setq level (org-outline-level))
  6297. (if (< arg 0) (setq arg (+ level arg)))
  6298. (while (> (setq level (org-outline-level)) arg)
  6299. (outline-up-heading 1 t)))
  6300. (setq beg (point)
  6301. heading (org-get-heading))
  6302. (org-end-of-subtree t t)
  6303. (if (org-at-heading-p) (backward-char 1))
  6304. (setq end (point)))
  6305. (if (and (buffer-live-p org-last-indirect-buffer)
  6306. (not (eq org-indirect-buffer-display 'new-frame))
  6307. (not arg))
  6308. (kill-buffer org-last-indirect-buffer))
  6309. (setq ibuf (org-get-indirect-buffer cbuf)
  6310. org-last-indirect-buffer ibuf)
  6311. (cond
  6312. ((or (eq org-indirect-buffer-display 'new-frame)
  6313. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6314. (select-frame (make-frame))
  6315. (delete-other-windows)
  6316. (org-pop-to-buffer-same-window ibuf)
  6317. (org-set-frame-title heading))
  6318. ((eq org-indirect-buffer-display 'dedicated-frame)
  6319. (raise-frame
  6320. (select-frame (or (and org-indirect-dedicated-frame
  6321. (frame-live-p org-indirect-dedicated-frame)
  6322. org-indirect-dedicated-frame)
  6323. (setq org-indirect-dedicated-frame (make-frame)))))
  6324. (delete-other-windows)
  6325. (org-pop-to-buffer-same-window ibuf)
  6326. (org-set-frame-title (concat "Indirect: " heading)))
  6327. ((eq org-indirect-buffer-display 'current-window)
  6328. (org-pop-to-buffer-same-window ibuf))
  6329. ((eq org-indirect-buffer-display 'other-window)
  6330. (pop-to-buffer ibuf))
  6331. (t (error "Invalid value")))
  6332. (if (featurep 'xemacs)
  6333. (save-excursion (org-mode) (turn-on-font-lock)))
  6334. (narrow-to-region beg end)
  6335. (show-all)
  6336. (goto-char pos)
  6337. (run-hook-with-args 'org-cycle-hook 'all)
  6338. (and (window-live-p cwin) (select-window cwin))))
  6339. (defun org-get-indirect-buffer (&optional buffer)
  6340. (setq buffer (or buffer (current-buffer)))
  6341. (let ((n 1) (base (buffer-name buffer)) bname)
  6342. (while (buffer-live-p
  6343. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6344. (setq n (1+ n)))
  6345. (condition-case nil
  6346. (make-indirect-buffer buffer bname 'clone)
  6347. (error (make-indirect-buffer buffer bname)))))
  6348. (defun org-set-frame-title (title)
  6349. "Set the title of the current frame to the string TITLE."
  6350. ;; FIXME: how to name a single frame in XEmacs???
  6351. (unless (featurep 'xemacs)
  6352. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6353. ;;;; Structure editing
  6354. ;;; Inserting headlines
  6355. (defun org-previous-line-empty-p ()
  6356. (save-excursion
  6357. (and (not (bobp))
  6358. (or (beginning-of-line 0) t)
  6359. (save-match-data
  6360. (looking-at "[ \t]*$")))))
  6361. (defun org-insert-heading (&optional force-heading invisible-ok)
  6362. "Insert a new heading or item with same depth at point.
  6363. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6364. If point is at the beginning of a headline, insert a sibling before the
  6365. current headline. If point is not at the beginning, split the line,
  6366. create the new headline with the text in the current line after point
  6367. \(but see also the variable `org-M-RET-may-split-line').
  6368. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6369. This is important for non-interactive uses of the command."
  6370. (interactive "P")
  6371. (if (or (= (buffer-size) 0)
  6372. (and (not (save-excursion
  6373. (and (ignore-errors (org-back-to-heading invisible-ok))
  6374. (org-at-heading-p))))
  6375. (or force-heading (not (org-in-item-p)))))
  6376. (progn
  6377. (insert "\n* ")
  6378. (run-hooks 'org-insert-heading-hook))
  6379. (when (or force-heading (not (org-insert-item)))
  6380. (let* ((empty-line-p nil)
  6381. (level nil)
  6382. (on-heading (org-at-heading-p))
  6383. (head (save-excursion
  6384. (condition-case nil
  6385. (progn
  6386. (org-back-to-heading invisible-ok)
  6387. (when (and (not on-heading)
  6388. (featurep 'org-inlinetask)
  6389. (integerp org-inlinetask-min-level)
  6390. (>= (length (match-string 0))
  6391. org-inlinetask-min-level))
  6392. ;; Find a heading level before the inline task
  6393. (while (and (setq level (org-up-heading-safe))
  6394. (>= level org-inlinetask-min-level)))
  6395. (if (org-at-heading-p)
  6396. (org-back-to-heading invisible-ok)
  6397. (error "This should not happen")))
  6398. (setq empty-line-p (org-previous-line-empty-p))
  6399. (match-string 0))
  6400. (error "*"))))
  6401. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6402. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6403. pos hide-previous previous-pos)
  6404. (cond
  6405. ((and (org-at-heading-p) (bolp)
  6406. (or (bobp)
  6407. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6408. ;; insert before the current line
  6409. (open-line (if blank 2 1)))
  6410. ((and (bolp)
  6411. (not org-insert-heading-respect-content)
  6412. (or (bobp)
  6413. (save-excursion
  6414. (backward-char 1) (not (outline-invisible-p)))))
  6415. ;; insert right here
  6416. nil)
  6417. (t
  6418. ;; somewhere in the line
  6419. (save-excursion
  6420. (setq previous-pos (point-at-bol))
  6421. (end-of-line)
  6422. (setq hide-previous (outline-invisible-p)))
  6423. (and org-insert-heading-respect-content (org-show-subtree))
  6424. (let ((split
  6425. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6426. (save-excursion
  6427. (let ((p (point)))
  6428. (goto-char (point-at-bol))
  6429. (and (looking-at org-complex-heading-regexp)
  6430. (match-beginning 4)
  6431. (> p (match-beginning 4)))))))
  6432. tags pos)
  6433. (cond
  6434. (org-insert-heading-respect-content
  6435. (org-end-of-subtree nil t)
  6436. (when (featurep 'org-inlinetask)
  6437. (while (and (not (eobp))
  6438. (looking-at "\\(\\*+\\)[ \t]+")
  6439. (>= (length (match-string 1))
  6440. org-inlinetask-min-level))
  6441. (org-end-of-subtree nil t)))
  6442. (or (bolp) (newline))
  6443. (or (org-previous-line-empty-p)
  6444. (and blank (newline)))
  6445. (open-line 1))
  6446. ((org-at-heading-p)
  6447. (when hide-previous
  6448. (show-children)
  6449. (org-show-entry))
  6450. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6451. (setq tags (and (match-end 2) (match-string 2)))
  6452. (and (match-end 1)
  6453. (delete-region (match-beginning 1) (match-end 1)))
  6454. (setq pos (point-at-bol))
  6455. (or split (end-of-line 1))
  6456. (delete-horizontal-space)
  6457. (if (string-match "\\`\\*+\\'"
  6458. (buffer-substring (point-at-bol) (point)))
  6459. (insert " "))
  6460. (newline (if blank 2 1))
  6461. (when tags
  6462. (save-excursion
  6463. (goto-char pos)
  6464. (end-of-line 1)
  6465. (insert " " tags)
  6466. (org-set-tags nil 'align))))
  6467. (t
  6468. (or split (end-of-line 1))
  6469. (newline (if blank 2 1)))))))
  6470. (insert head) (just-one-space)
  6471. (setq pos (point))
  6472. (end-of-line 1)
  6473. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6474. (when (and org-insert-heading-respect-content hide-previous)
  6475. (save-excursion
  6476. (goto-char previous-pos)
  6477. (hide-subtree)))
  6478. (run-hooks 'org-insert-heading-hook)))))
  6479. (defun org-get-heading (&optional no-tags no-todo)
  6480. "Return the heading of the current entry, without the stars.
  6481. When NO-TAGS is non-nil, don't include tags.
  6482. When NO-TODO is non-nil, don't include TODO keywords."
  6483. (save-excursion
  6484. (org-back-to-heading t)
  6485. (cond
  6486. ((and no-tags no-todo)
  6487. (looking-at org-complex-heading-regexp)
  6488. (match-string 4))
  6489. (no-tags
  6490. (looking-at (concat org-outline-regexp
  6491. "\\(.*?\\)"
  6492. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6493. (match-string 1))
  6494. (no-todo
  6495. (looking-at org-todo-line-regexp)
  6496. (match-string 3))
  6497. (t (looking-at org-heading-regexp)
  6498. (match-string 2)))))
  6499. (defun org-heading-components ()
  6500. "Return the components of the current heading.
  6501. This is a list with the following elements:
  6502. - the level as an integer
  6503. - the reduced level, different if `org-odd-levels-only' is set.
  6504. - the TODO keyword, or nil
  6505. - the priority character, like ?A, or nil if no priority is given
  6506. - the headline text itself, or the tags string if no headline text
  6507. - the tags string, or nil."
  6508. (save-excursion
  6509. (org-back-to-heading t)
  6510. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6511. (list (length (match-string 1))
  6512. (org-reduced-level (length (match-string 1)))
  6513. (org-match-string-no-properties 2)
  6514. (and (match-end 3) (aref (match-string 3) 2))
  6515. (org-match-string-no-properties 4)
  6516. (org-match-string-no-properties 5)))))
  6517. (defun org-get-entry ()
  6518. "Get the entry text, after heading, entire subtree."
  6519. (save-excursion
  6520. (org-back-to-heading t)
  6521. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6522. (defun org-insert-heading-after-current ()
  6523. "Insert a new heading with same level as current, after current subtree."
  6524. (interactive)
  6525. (org-back-to-heading)
  6526. (org-insert-heading)
  6527. (org-move-subtree-down)
  6528. (end-of-line 1))
  6529. (defun org-insert-heading-respect-content ()
  6530. (interactive)
  6531. (let ((org-insert-heading-respect-content t))
  6532. (org-insert-heading t)))
  6533. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6534. (interactive "P")
  6535. (let ((org-insert-heading-respect-content t))
  6536. (org-insert-todo-heading force-state t)))
  6537. (defun org-insert-todo-heading (arg &optional force-heading)
  6538. "Insert a new heading with the same level and TODO state as current heading.
  6539. If the heading has no TODO state, or if the state is DONE, use the first
  6540. state (TODO by default). Also with prefix arg, force first state."
  6541. (interactive "P")
  6542. (when (or force-heading (not (org-insert-item 'checkbox)))
  6543. (org-insert-heading force-heading)
  6544. (save-excursion
  6545. (org-back-to-heading)
  6546. (outline-previous-heading)
  6547. (looking-at org-todo-line-regexp))
  6548. (let*
  6549. ((new-mark-x
  6550. (if (or arg
  6551. (not (match-beginning 2))
  6552. (member (match-string 2) org-done-keywords))
  6553. (car org-todo-keywords-1)
  6554. (match-string 2)))
  6555. (new-mark
  6556. (or
  6557. (run-hook-with-args-until-success
  6558. 'org-todo-get-default-hook new-mark-x nil)
  6559. new-mark-x)))
  6560. (beginning-of-line 1)
  6561. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6562. (if org-treat-insert-todo-heading-as-state-change
  6563. (org-todo new-mark)
  6564. (insert new-mark " "))))
  6565. (when org-provide-todo-statistics
  6566. (org-update-parent-todo-statistics))))
  6567. (defun org-insert-subheading (arg)
  6568. "Insert a new subheading and demote it.
  6569. Works for outline headings and for plain lists alike."
  6570. (interactive "P")
  6571. (org-insert-heading arg)
  6572. (cond
  6573. ((org-at-heading-p) (org-do-demote))
  6574. ((org-at-item-p) (org-indent-item))))
  6575. (defun org-insert-todo-subheading (arg)
  6576. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6577. Works for outline headings and for plain lists alike."
  6578. (interactive "P")
  6579. (org-insert-todo-heading arg)
  6580. (cond
  6581. ((org-at-heading-p) (org-do-demote))
  6582. ((org-at-item-p) (org-indent-item))))
  6583. ;;; Promotion and Demotion
  6584. (defvar org-after-demote-entry-hook nil
  6585. "Hook run after an entry has been demoted.
  6586. The cursor will be at the beginning of the entry.
  6587. When a subtree is being demoted, the hook will be called for each node.")
  6588. (defvar org-after-promote-entry-hook nil
  6589. "Hook run after an entry has been promoted.
  6590. The cursor will be at the beginning of the entry.
  6591. When a subtree is being promoted, the hook will be called for each node.")
  6592. (defun org-promote-subtree ()
  6593. "Promote the entire subtree.
  6594. See also `org-promote'."
  6595. (interactive)
  6596. (save-excursion
  6597. (org-with-limited-levels (org-map-tree 'org-promote)))
  6598. (org-fix-position-after-promote))
  6599. (defun org-demote-subtree ()
  6600. "Demote the entire subtree. See `org-demote'.
  6601. See also `org-promote'."
  6602. (interactive)
  6603. (save-excursion
  6604. (org-with-limited-levels (org-map-tree 'org-demote)))
  6605. (org-fix-position-after-promote))
  6606. (defun org-do-promote ()
  6607. "Promote the current heading higher up the tree.
  6608. If the region is active in `transient-mark-mode', promote all headings
  6609. in the region."
  6610. (interactive)
  6611. (save-excursion
  6612. (if (org-region-active-p)
  6613. (org-map-region 'org-promote (region-beginning) (region-end))
  6614. (org-promote)))
  6615. (org-fix-position-after-promote))
  6616. (defun org-do-demote ()
  6617. "Demote the current heading lower down the tree.
  6618. If the region is active in `transient-mark-mode', demote all headings
  6619. in the region."
  6620. (interactive)
  6621. (save-excursion
  6622. (if (org-region-active-p)
  6623. (org-map-region 'org-demote (region-beginning) (region-end))
  6624. (org-demote)))
  6625. (org-fix-position-after-promote))
  6626. (defun org-fix-position-after-promote ()
  6627. "Make sure that after pro/demotion cursor position is right."
  6628. (let ((pos (point)))
  6629. (when (save-excursion
  6630. (beginning-of-line 1)
  6631. (looking-at org-todo-line-regexp)
  6632. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6633. (cond ((eobp) (insert " "))
  6634. ((eolp) (insert " "))
  6635. ((equal (char-after) ?\ ) (forward-char 1))))))
  6636. (defun org-current-level ()
  6637. "Return the level of the current entry, or nil if before the first headline.
  6638. The level is the number of stars at the beginning of the headline."
  6639. (save-excursion
  6640. (org-with-limited-levels
  6641. (if (ignore-errors (org-back-to-heading t))
  6642. (funcall outline-level)))))
  6643. (defun org-get-previous-line-level ()
  6644. "Return the outline depth of the last headline before the current line.
  6645. Returns 0 for the first headline in the buffer, and nil if before the
  6646. first headline."
  6647. (let ((current-level (org-current-level))
  6648. (prev-level (when (> (line-number-at-pos) 1)
  6649. (save-excursion
  6650. (beginning-of-line 0)
  6651. (org-current-level)))))
  6652. (cond ((null current-level) nil) ; Before first headline
  6653. ((null prev-level) 0) ; At first headline
  6654. (prev-level))))
  6655. (defun org-reduced-level (l)
  6656. "Compute the effective level of a heading.
  6657. This takes into account the setting of `org-odd-levels-only'."
  6658. (cond
  6659. ((zerop l) 0)
  6660. (org-odd-levels-only (1+ (floor (/ l 2))))
  6661. (t l)))
  6662. (defun org-level-increment ()
  6663. "Return the number of stars that will be added or removed at a
  6664. time to headlines when structure editing, based on the value of
  6665. `org-odd-levels-only'."
  6666. (if org-odd-levels-only 2 1))
  6667. (defun org-get-valid-level (level &optional change)
  6668. "Rectify a level change under the influence of `org-odd-levels-only'
  6669. LEVEL is a current level, CHANGE is by how much the level should be
  6670. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6671. even level numbers will become the next higher odd number."
  6672. (if org-odd-levels-only
  6673. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6674. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6675. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6676. (max 1 (+ level (or change 0)))))
  6677. (if (boundp 'define-obsolete-function-alias)
  6678. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6679. (define-obsolete-function-alias 'org-get-legal-level
  6680. 'org-get-valid-level)
  6681. (define-obsolete-function-alias 'org-get-legal-level
  6682. 'org-get-valid-level "23.1")))
  6683. (defun org-promote ()
  6684. "Promote the current heading higher up the tree.
  6685. If the region is active in `transient-mark-mode', promote all headings
  6686. in the region."
  6687. (org-back-to-heading t)
  6688. (let* ((level (save-match-data (funcall outline-level)))
  6689. (after-change-functions (remove 'flyspell-after-change-function
  6690. after-change-functions))
  6691. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6692. (diff (abs (- level (length up-head) -1))))
  6693. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6694. (replace-match up-head nil t)
  6695. ;; Fixup tag positioning
  6696. (and org-auto-align-tags (org-set-tags nil t))
  6697. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6698. (run-hooks 'org-after-promote-entry-hook)))
  6699. (defun org-demote ()
  6700. "Demote the current heading lower down the tree.
  6701. If the region is active in `transient-mark-mode', demote all headings
  6702. in the region."
  6703. (org-back-to-heading t)
  6704. (let* ((level (save-match-data (funcall outline-level)))
  6705. (after-change-functions (remove 'flyspell-after-change-function
  6706. after-change-functions))
  6707. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6708. (diff (abs (- level (length down-head) -1))))
  6709. (replace-match down-head nil t)
  6710. ;; Fixup tag positioning
  6711. (and org-auto-align-tags (org-set-tags nil t))
  6712. (if org-adapt-indentation (org-fixup-indentation diff))
  6713. (run-hooks 'org-after-demote-entry-hook)))
  6714. (defun org-cycle-level ()
  6715. "Cycle the level of an empty headline through possible states.
  6716. This goes first to child, then to parent, level, then up the hierarchy.
  6717. After top level, it switches back to sibling level."
  6718. (interactive)
  6719. (let ((org-adapt-indentation nil))
  6720. (when (org-point-at-end-of-empty-headline)
  6721. (setq this-command 'org-cycle-level) ; Only needed for caching
  6722. (let ((cur-level (org-current-level))
  6723. (prev-level (org-get-previous-line-level)))
  6724. (cond
  6725. ;; If first headline in file, promote to top-level.
  6726. ((= prev-level 0)
  6727. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6728. do (org-do-promote)))
  6729. ;; If same level as prev, demote one.
  6730. ((= prev-level cur-level)
  6731. (org-do-demote))
  6732. ;; If parent is top-level, promote to top level if not already.
  6733. ((= prev-level 1)
  6734. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6735. do (org-do-promote)))
  6736. ;; If top-level, return to prev-level.
  6737. ((= cur-level 1)
  6738. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6739. do (org-do-demote)))
  6740. ;; If less than prev-level, promote one.
  6741. ((< cur-level prev-level)
  6742. (org-do-promote))
  6743. ;; If deeper than prev-level, promote until higher than
  6744. ;; prev-level.
  6745. ((> cur-level prev-level)
  6746. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6747. do (org-do-promote))))
  6748. t))))
  6749. (defun org-map-tree (fun)
  6750. "Call FUN for every heading underneath the current one."
  6751. (org-back-to-heading)
  6752. (let ((level (funcall outline-level)))
  6753. (save-excursion
  6754. (funcall fun)
  6755. (while (and (progn
  6756. (outline-next-heading)
  6757. (> (funcall outline-level) level))
  6758. (not (eobp)))
  6759. (funcall fun)))))
  6760. (defun org-map-region (fun beg end)
  6761. "Call FUN for every heading between BEG and END."
  6762. (let ((org-ignore-region t))
  6763. (save-excursion
  6764. (setq end (copy-marker end))
  6765. (goto-char beg)
  6766. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6767. (< (point) end))
  6768. (funcall fun))
  6769. (while (and (progn
  6770. (outline-next-heading)
  6771. (< (point) end))
  6772. (not (eobp)))
  6773. (funcall fun)))))
  6774. (defvar org-property-end-re) ; silence byte-compiler
  6775. (defun org-fixup-indentation (diff)
  6776. "Change the indentation in the current entry by DIFF.
  6777. However, if any line in the current entry has no indentation, or if it
  6778. would end up with no indentation after the change, nothing at all is done."
  6779. (save-excursion
  6780. (let ((end (save-excursion (outline-next-heading)
  6781. (point-marker)))
  6782. (prohibit (if (> diff 0)
  6783. "^\\S-"
  6784. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6785. col)
  6786. (unless (save-excursion (end-of-line 1)
  6787. (re-search-forward prohibit end t))
  6788. (while (and (< (point) end)
  6789. (re-search-forward "^[ \t]+" end t))
  6790. (goto-char (match-end 0))
  6791. (setq col (current-column))
  6792. (if (< diff 0) (replace-match ""))
  6793. (org-indent-to-column (+ diff col))))
  6794. (move-marker end nil))))
  6795. (defun org-convert-to-odd-levels ()
  6796. "Convert an org-mode file with all levels allowed to one with odd levels.
  6797. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6798. level 5 etc."
  6799. (interactive)
  6800. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6801. (let ((outline-level 'org-outline-level)
  6802. (org-odd-levels-only nil) n)
  6803. (save-excursion
  6804. (goto-char (point-min))
  6805. (while (re-search-forward "^\\*\\*+ " nil t)
  6806. (setq n (- (length (match-string 0)) 2))
  6807. (while (>= (setq n (1- n)) 0)
  6808. (org-demote))
  6809. (end-of-line 1))))))
  6810. (defun org-convert-to-oddeven-levels ()
  6811. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6812. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6813. file contains a section with an even level, conversion would
  6814. destroy the structure of the file. An error is signaled in this
  6815. case."
  6816. (interactive)
  6817. (goto-char (point-min))
  6818. ;; First check if there are no even levels
  6819. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6820. (org-show-context t)
  6821. (error "Not all levels are odd in this file. Conversion not possible"))
  6822. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6823. (let ((outline-regexp org-outline-regexp)
  6824. (outline-level 'org-outline-level)
  6825. (org-odd-levels-only nil) n)
  6826. (save-excursion
  6827. (goto-char (point-min))
  6828. (while (re-search-forward "^\\*\\*+ " nil t)
  6829. (setq n (/ (1- (length (match-string 0))) 2))
  6830. (while (>= (setq n (1- n)) 0)
  6831. (org-promote))
  6832. (end-of-line 1))))))
  6833. (defun org-tr-level (n)
  6834. "Make N odd if required."
  6835. (if org-odd-levels-only (1+ (/ n 2)) n))
  6836. ;;; Vertical tree motion, cutting and pasting of subtrees
  6837. (defun org-move-subtree-up (&optional arg)
  6838. "Move the current subtree up past ARG headlines of the same level."
  6839. (interactive "p")
  6840. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6841. (defun org-move-subtree-down (&optional arg)
  6842. "Move the current subtree down past ARG headlines of the same level."
  6843. (interactive "p")
  6844. (setq arg (prefix-numeric-value arg))
  6845. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6846. 'org-get-last-sibling))
  6847. (ins-point (make-marker))
  6848. (cnt (abs arg))
  6849. (col (current-column))
  6850. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6851. ;; Select the tree
  6852. (org-back-to-heading)
  6853. (setq beg0 (point))
  6854. (save-excursion
  6855. (setq ne-beg (org-back-over-empty-lines))
  6856. (setq beg (point)))
  6857. (save-match-data
  6858. (save-excursion (outline-end-of-heading)
  6859. (setq folded (outline-invisible-p)))
  6860. (outline-end-of-subtree))
  6861. (outline-next-heading)
  6862. (setq ne-end (org-back-over-empty-lines))
  6863. (setq end (point))
  6864. (goto-char beg0)
  6865. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6866. ;; include less whitespace
  6867. (save-excursion
  6868. (goto-char beg)
  6869. (forward-line (- ne-beg ne-end))
  6870. (setq beg (point))))
  6871. ;; Find insertion point, with error handling
  6872. (while (> cnt 0)
  6873. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6874. (progn (goto-char beg0)
  6875. (error "Cannot move past superior level or buffer limit")))
  6876. (setq cnt (1- cnt)))
  6877. (if (> arg 0)
  6878. ;; Moving forward - still need to move over subtree
  6879. (progn (org-end-of-subtree t t)
  6880. (save-excursion
  6881. (org-back-over-empty-lines)
  6882. (or (bolp) (newline)))))
  6883. (setq ne-ins (org-back-over-empty-lines))
  6884. (move-marker ins-point (point))
  6885. (setq txt (buffer-substring beg end))
  6886. (org-save-markers-in-region beg end)
  6887. (delete-region beg end)
  6888. (org-remove-empty-overlays-at beg)
  6889. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6890. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6891. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6892. (let ((bbb (point)))
  6893. (insert-before-markers txt)
  6894. (org-reinstall-markers-in-region bbb)
  6895. (move-marker ins-point bbb))
  6896. (or (bolp) (insert "\n"))
  6897. (setq ins-end (point))
  6898. (goto-char ins-point)
  6899. (org-skip-whitespace)
  6900. (when (and (< arg 0)
  6901. (org-first-sibling-p)
  6902. (> ne-ins ne-beg))
  6903. ;; Move whitespace back to beginning
  6904. (save-excursion
  6905. (goto-char ins-end)
  6906. (let ((kill-whole-line t))
  6907. (kill-line (- ne-ins ne-beg)) (point)))
  6908. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6909. (move-marker ins-point nil)
  6910. (if folded
  6911. (hide-subtree)
  6912. (org-show-entry)
  6913. (show-children)
  6914. (org-cycle-hide-drawers 'children))
  6915. (org-clean-visibility-after-subtree-move)
  6916. ;; move back to the initial column we were at
  6917. (move-to-column col)))
  6918. (defvar org-subtree-clip ""
  6919. "Clipboard for cut and paste of subtrees.
  6920. This is actually only a copy of the kill, because we use the normal kill
  6921. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6922. (defvar org-subtree-clip-folded nil
  6923. "Was the last copied subtree folded?
  6924. This is used to fold the tree back after pasting.")
  6925. (defun org-cut-subtree (&optional n)
  6926. "Cut the current subtree into the clipboard.
  6927. With prefix arg N, cut this many sequential subtrees.
  6928. This is a short-hand for marking the subtree and then cutting it."
  6929. (interactive "p")
  6930. (org-copy-subtree n 'cut))
  6931. (defun org-copy-subtree (&optional n cut force-store-markers)
  6932. "Cut the current subtree into the clipboard.
  6933. With prefix arg N, cut this many sequential subtrees.
  6934. This is a short-hand for marking the subtree and then copying it.
  6935. If CUT is non-nil, actually cut the subtree.
  6936. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6937. of some markers in the region, even if CUT is non-nil. This is
  6938. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6939. (interactive "p")
  6940. (let (beg end folded (beg0 (point)))
  6941. (if (org-called-interactively-p 'any)
  6942. (org-back-to-heading nil) ; take what looks like a subtree
  6943. (org-back-to-heading t)) ; take what is really there
  6944. (org-back-over-empty-lines)
  6945. (setq beg (point))
  6946. (skip-chars-forward " \t\r\n")
  6947. (save-match-data
  6948. (save-excursion (outline-end-of-heading)
  6949. (setq folded (outline-invisible-p)))
  6950. (condition-case nil
  6951. (org-forward-same-level (1- n) t)
  6952. (error nil))
  6953. (org-end-of-subtree t t))
  6954. (org-back-over-empty-lines)
  6955. (setq end (point))
  6956. (goto-char beg0)
  6957. (when (> end beg)
  6958. (setq org-subtree-clip-folded folded)
  6959. (when (or cut force-store-markers)
  6960. (org-save-markers-in-region beg end))
  6961. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6962. (setq org-subtree-clip (current-kill 0))
  6963. (message "%s: Subtree(s) with %d characters"
  6964. (if cut "Cut" "Copied")
  6965. (length org-subtree-clip)))))
  6966. (defun org-paste-subtree (&optional level tree for-yank)
  6967. "Paste the clipboard as a subtree, with modification of headline level.
  6968. The entire subtree is promoted or demoted in order to match a new headline
  6969. level.
  6970. If the cursor is at the beginning of a headline, the same level as
  6971. that headline is used to paste the tree
  6972. If not, the new level is derived from the *visible* headings
  6973. before and after the insertion point, and taken to be the inferior headline
  6974. level of the two. So if the previous visible heading is level 3 and the
  6975. next is level 4 (or vice versa), level 4 will be used for insertion.
  6976. This makes sure that the subtree remains an independent subtree and does
  6977. not swallow low level entries.
  6978. You can also force a different level, either by using a numeric prefix
  6979. argument, or by inserting the heading marker by hand. For example, if the
  6980. cursor is after \"*****\", then the tree will be shifted to level 5.
  6981. If optional TREE is given, use this text instead of the kill ring.
  6982. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6983. move back over whitespace before inserting, and move point to the end of
  6984. the inserted text when done."
  6985. (interactive "P")
  6986. (setq tree (or tree (and kill-ring (current-kill 0))))
  6987. (unless (org-kill-is-subtree-p tree)
  6988. (error "%s"
  6989. (substitute-command-keys
  6990. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6991. (org-with-limited-levels
  6992. (let* ((visp (not (outline-invisible-p)))
  6993. (txt tree)
  6994. (^re_ "\\(\\*+\\)[ \t]*")
  6995. (old-level (if (string-match org-outline-regexp-bol txt)
  6996. (- (match-end 0) (match-beginning 0) 1)
  6997. -1))
  6998. (force-level (cond (level (prefix-numeric-value level))
  6999. ((and (looking-at "[ \t]*$")
  7000. (string-match
  7001. "^\\*+$" (buffer-substring
  7002. (point-at-bol) (point))))
  7003. (- (match-end 1) (match-beginning 1)))
  7004. ((and (bolp)
  7005. (looking-at org-outline-regexp))
  7006. (- (match-end 0) (point) 1))
  7007. (t nil)))
  7008. (previous-level (save-excursion
  7009. (condition-case nil
  7010. (progn
  7011. (outline-previous-visible-heading 1)
  7012. (if (looking-at ^re_)
  7013. (- (match-end 0) (match-beginning 0) 1)
  7014. 1))
  7015. (error 1))))
  7016. (next-level (save-excursion
  7017. (condition-case nil
  7018. (progn
  7019. (or (looking-at org-outline-regexp)
  7020. (outline-next-visible-heading 1))
  7021. (if (looking-at ^re_)
  7022. (- (match-end 0) (match-beginning 0) 1)
  7023. 1))
  7024. (error 1))))
  7025. (new-level (or force-level (max previous-level next-level)))
  7026. (shift (if (or (= old-level -1)
  7027. (= new-level -1)
  7028. (= old-level new-level))
  7029. 0
  7030. (- new-level old-level)))
  7031. (delta (if (> shift 0) -1 1))
  7032. (func (if (> shift 0) 'org-demote 'org-promote))
  7033. (org-odd-levels-only nil)
  7034. beg end newend)
  7035. ;; Remove the forced level indicator
  7036. (if force-level
  7037. (delete-region (point-at-bol) (point)))
  7038. ;; Paste
  7039. (beginning-of-line (if (bolp) 1 2))
  7040. (unless for-yank (org-back-over-empty-lines))
  7041. (setq beg (point))
  7042. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7043. (insert-before-markers txt)
  7044. (unless (string-match "\n\\'" txt) (insert "\n"))
  7045. (setq newend (point))
  7046. (org-reinstall-markers-in-region beg)
  7047. (setq end (point))
  7048. (goto-char beg)
  7049. (skip-chars-forward " \t\n\r")
  7050. (setq beg (point))
  7051. (if (and (outline-invisible-p) visp)
  7052. (save-excursion (outline-show-heading)))
  7053. ;; Shift if necessary
  7054. (unless (= shift 0)
  7055. (save-restriction
  7056. (narrow-to-region beg end)
  7057. (while (not (= shift 0))
  7058. (org-map-region func (point-min) (point-max))
  7059. (setq shift (+ delta shift)))
  7060. (goto-char (point-min))
  7061. (setq newend (point-max))))
  7062. (when (or (org-called-interactively-p 'interactive) for-yank)
  7063. (message "Clipboard pasted as level %d subtree" new-level))
  7064. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7065. kill-ring
  7066. (eq org-subtree-clip (current-kill 0))
  7067. org-subtree-clip-folded)
  7068. ;; The tree was folded before it was killed/copied
  7069. (hide-subtree))
  7070. (and for-yank (goto-char newend)))))
  7071. (defun org-kill-is-subtree-p (&optional txt)
  7072. "Check if the current kill is an outline subtree, or a set of trees.
  7073. Returns nil if kill does not start with a headline, or if the first
  7074. headline level is not the largest headline level in the tree.
  7075. So this will actually accept several entries of equal levels as well,
  7076. which is OK for `org-paste-subtree'.
  7077. If optional TXT is given, check this string instead of the current kill."
  7078. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7079. (re (org-get-limited-outline-regexp))
  7080. (^re (concat "^" re))
  7081. (start-level (and kill
  7082. (string-match
  7083. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7084. kill)
  7085. (- (match-end 2) (match-beginning 2) 1)))
  7086. (start (1+ (or (match-beginning 2) -1))))
  7087. (if (not start-level)
  7088. (progn
  7089. nil) ;; does not even start with a heading
  7090. (catch 'exit
  7091. (while (setq start (string-match ^re kill (1+ start)))
  7092. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7093. (throw 'exit nil)))
  7094. t))))
  7095. (defvar org-markers-to-move nil
  7096. "Markers that should be moved with a cut-and-paste operation.
  7097. Those markers are stored together with their positions relative to
  7098. the start of the region.")
  7099. (defun org-save-markers-in-region (beg end)
  7100. "Check markers in region.
  7101. If these markers are between BEG and END, record their position relative
  7102. to BEG, so that after moving the block of text, we can put the markers back
  7103. into place.
  7104. This function gets called just before an entry or tree gets cut from the
  7105. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7106. called immediately, to move the markers with the entries."
  7107. (setq org-markers-to-move nil)
  7108. (when (featurep 'org-clock)
  7109. (org-clock-save-markers-for-cut-and-paste beg end))
  7110. (when (featurep 'org-agenda)
  7111. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7112. (defun org-check-and-save-marker (marker beg end)
  7113. "Check if MARKER is between BEG and END.
  7114. If yes, remember the marker and the distance to BEG."
  7115. (when (and (marker-buffer marker)
  7116. (equal (marker-buffer marker) (current-buffer)))
  7117. (if (and (>= marker beg) (< marker end))
  7118. (push (cons marker (- marker beg)) org-markers-to-move))))
  7119. (defun org-reinstall-markers-in-region (beg)
  7120. "Move all remembered markers to their position relative to BEG."
  7121. (mapc (lambda (x)
  7122. (move-marker (car x) (+ beg (cdr x))))
  7123. org-markers-to-move)
  7124. (setq org-markers-to-move nil))
  7125. (defun org-narrow-to-subtree ()
  7126. "Narrow buffer to the current subtree."
  7127. (interactive)
  7128. (save-excursion
  7129. (save-match-data
  7130. (org-with-limited-levels
  7131. (narrow-to-region
  7132. (progn (org-back-to-heading t) (point))
  7133. (progn (org-end-of-subtree t t)
  7134. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7135. (point)))))))
  7136. (defun org-narrow-to-block ()
  7137. "Narrow buffer to the current block."
  7138. (interactive)
  7139. (let* ((case-fold-search t)
  7140. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7141. "^[ \t]*#\\+end_.*")))
  7142. (if blockp
  7143. (narrow-to-region (car blockp) (cdr blockp))
  7144. (error "Not in a block"))))
  7145. (eval-when-compile
  7146. (defvar org-property-drawer-re))
  7147. (defvar org-property-start-re) ;; defined below
  7148. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7149. "Clone the task (subtree) at point N times.
  7150. The clones will be inserted as siblings.
  7151. In interactive use, the user will be prompted for the number of
  7152. clones to be produced, and for a time SHIFT, which may be a
  7153. repeater as used in time stamps, for example `+3d'.
  7154. When a valid repeater is given and the entry contains any time
  7155. stamps, the clones will become a sequence in time, with time
  7156. stamps in the subtree shifted for each clone produced. If SHIFT
  7157. is nil or the empty string, time stamps will be left alone. The
  7158. ID property of the original subtree is removed.
  7159. If the original subtree did contain time stamps with a repeater,
  7160. the following will happen:
  7161. - the repeater will be removed in each clone
  7162. - an additional clone will be produced, with the current, unshifted
  7163. date(s) in the entry.
  7164. - the original entry will be placed *after* all the clones, with
  7165. repeater intact.
  7166. - the start days in the repeater in the original entry will be shifted
  7167. to past the last clone.
  7168. In this way you can spell out a number of instances of a repeating task,
  7169. and still retain the repeater to cover future instances of the task."
  7170. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7171. (let (beg end template task idprop
  7172. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7173. (drawer-re org-drawer-regexp))
  7174. (if (not (and (integerp n) (> n 0)))
  7175. (error "Invalid number of replications %s" n))
  7176. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7177. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7178. shift)))
  7179. (error "Invalid shift specification %s" shift))
  7180. (when doshift
  7181. (setq shift-n (string-to-number (match-string 1 shift))
  7182. shift-what (cdr (assoc (match-string 2 shift)
  7183. '(("d" . day) ("w" . week)
  7184. ("m" . month) ("y" . year))))))
  7185. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7186. (setq nmin 1 nmax n)
  7187. (org-back-to-heading t)
  7188. (setq beg (point))
  7189. (setq idprop (org-entry-get nil "ID"))
  7190. (org-end-of-subtree t t)
  7191. (or (bolp) (insert "\n"))
  7192. (setq end (point))
  7193. (setq template (buffer-substring beg end))
  7194. (when (and doshift
  7195. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7196. (delete-region beg end)
  7197. (setq end beg)
  7198. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7199. (goto-char end)
  7200. (loop for n from nmin to nmax do
  7201. ;; prepare clone
  7202. (with-temp-buffer
  7203. (insert template)
  7204. (org-mode)
  7205. (goto-char (point-min))
  7206. (org-show-subtree)
  7207. (and idprop (if org-clone-delete-id
  7208. (org-entry-delete nil "ID")
  7209. (org-id-get-create t)))
  7210. (unless (= n 0)
  7211. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7212. (kill-whole-line))
  7213. (goto-char (point-min))
  7214. (while (re-search-forward drawer-re nil t)
  7215. (mapc (lambda (d)
  7216. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7217. (goto-char (point-min))
  7218. (when doshift
  7219. (while (re-search-forward org-ts-regexp-both nil t)
  7220. (org-timestamp-change (* n shift-n) shift-what))
  7221. (unless (= n n-no-remove)
  7222. (goto-char (point-min))
  7223. (while (re-search-forward org-ts-regexp nil t)
  7224. (save-excursion
  7225. (goto-char (match-beginning 0))
  7226. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7227. (delete-region (match-beginning 1) (match-end 1)))))))
  7228. (setq task (buffer-string)))
  7229. (insert task))
  7230. (goto-char beg)))
  7231. ;;; Outline Sorting
  7232. (defun org-sort (with-case)
  7233. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7234. Optional argument WITH-CASE means sort case-sensitively."
  7235. (interactive "P")
  7236. (cond
  7237. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7238. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7239. (t
  7240. (org-call-with-arg 'org-sort-entries with-case))))
  7241. (defun org-sort-remove-invisible (s)
  7242. (remove-text-properties 0 (length s) org-rm-props s)
  7243. (while (string-match org-bracket-link-regexp s)
  7244. (setq s (replace-match (if (match-end 2)
  7245. (match-string 3 s)
  7246. (match-string 1 s)) t t s)))
  7247. s)
  7248. (defvar org-priority-regexp) ; defined later in the file
  7249. (defvar org-after-sorting-entries-or-items-hook nil
  7250. "Hook that is run after a bunch of entries or items have been sorted.
  7251. When children are sorted, the cursor is in the parent line when this
  7252. hook gets called. When a region or a plain list is sorted, the cursor
  7253. will be in the first entry of the sorted region/list.")
  7254. (defun org-sort-entries
  7255. (&optional with-case sorting-type getkey-func compare-func property)
  7256. "Sort entries on a certain level of an outline tree.
  7257. If there is an active region, the entries in the region are sorted.
  7258. Else, if the cursor is before the first entry, sort the top-level items.
  7259. Else, the children of the entry at point are sorted.
  7260. Sorting can be alphabetically, numerically, by date/time as given by
  7261. a time stamp, by a property or by priority.
  7262. The command prompts for the sorting type unless it has been given to the
  7263. function through the SORTING-TYPE argument, which needs to be a character,
  7264. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7265. precise meaning of each character:
  7266. n Numerically, by converting the beginning of the entry/item to a number.
  7267. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7268. t By date/time, either the first active time stamp in the entry, or, if
  7269. none exist, by the first inactive one.
  7270. s By the scheduled date/time.
  7271. d By deadline date/time.
  7272. c By creation time, which is assumed to be the first inactive time stamp
  7273. at the beginning of a line.
  7274. p By priority according to the cookie.
  7275. r By the value of a property.
  7276. Capital letters will reverse the sort order.
  7277. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7278. called with point at the beginning of the record. It must return either
  7279. a string or a number that should serve as the sorting key for that record.
  7280. Comparing entries ignores case by default. However, with an optional argument
  7281. WITH-CASE, the sorting considers case as well."
  7282. (interactive "P")
  7283. (let ((case-func (if with-case 'identity 'downcase))
  7284. start beg end stars re re2
  7285. txt what tmp)
  7286. ;; Find beginning and end of region to sort
  7287. (cond
  7288. ((org-region-active-p)
  7289. ;; we will sort the region
  7290. (setq end (region-end)
  7291. what "region")
  7292. (goto-char (region-beginning))
  7293. (if (not (org-at-heading-p)) (outline-next-heading))
  7294. (setq start (point)))
  7295. ((or (org-at-heading-p)
  7296. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7297. ;; we will sort the children of the current headline
  7298. (org-back-to-heading)
  7299. (setq start (point)
  7300. end (progn (org-end-of-subtree t t)
  7301. (or (bolp) (insert "\n"))
  7302. (org-back-over-empty-lines)
  7303. (point))
  7304. what "children")
  7305. (goto-char start)
  7306. (show-subtree)
  7307. (outline-next-heading))
  7308. (t
  7309. ;; we will sort the top-level entries in this file
  7310. (goto-char (point-min))
  7311. (or (org-at-heading-p) (outline-next-heading))
  7312. (setq start (point))
  7313. (goto-char (point-max))
  7314. (beginning-of-line 1)
  7315. (when (looking-at ".*?\\S-")
  7316. ;; File ends in a non-white line
  7317. (end-of-line 1)
  7318. (insert "\n"))
  7319. (setq end (point-max))
  7320. (setq what "top-level")
  7321. (goto-char start)
  7322. (show-all)))
  7323. (setq beg (point))
  7324. (if (>= beg end) (error "Nothing to sort"))
  7325. (looking-at "\\(\\*+\\)")
  7326. (setq stars (match-string 1)
  7327. re (concat "^" (regexp-quote stars) " +")
  7328. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7329. txt (buffer-substring beg end))
  7330. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7331. (if (and (not (equal stars "*")) (string-match re2 txt))
  7332. (error "Region to sort contains a level above the first entry"))
  7333. (unless sorting-type
  7334. (message
  7335. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7336. [t]ime [s]cheduled [d]eadline [c]reated
  7337. A/N/T/S/D/C/P/O/F means reversed:"
  7338. what)
  7339. (setq sorting-type (read-char-exclusive))
  7340. (and (= (downcase sorting-type) ?f)
  7341. (setq getkey-func
  7342. (org-icompleting-read "Sort using function: "
  7343. obarray 'fboundp t nil nil))
  7344. (setq getkey-func (intern getkey-func)))
  7345. (and (= (downcase sorting-type) ?r)
  7346. (setq property
  7347. (org-icompleting-read "Property: "
  7348. (mapcar 'list (org-buffer-property-keys t))
  7349. nil t))))
  7350. (message "Sorting entries...")
  7351. (save-restriction
  7352. (narrow-to-region start end)
  7353. (let ((dcst (downcase sorting-type))
  7354. (case-fold-search nil)
  7355. (now (current-time)))
  7356. (sort-subr
  7357. (/= dcst sorting-type)
  7358. ;; This function moves to the beginning character of the "record" to
  7359. ;; be sorted.
  7360. (lambda nil
  7361. (if (re-search-forward re nil t)
  7362. (goto-char (match-beginning 0))
  7363. (goto-char (point-max))))
  7364. ;; This function moves to the last character of the "record" being
  7365. ;; sorted.
  7366. (lambda nil
  7367. (save-match-data
  7368. (condition-case nil
  7369. (outline-forward-same-level 1)
  7370. (error
  7371. (goto-char (point-max))))))
  7372. ;; This function returns the value that gets sorted against.
  7373. (lambda nil
  7374. (cond
  7375. ((= dcst ?n)
  7376. (if (looking-at org-complex-heading-regexp)
  7377. (string-to-number (match-string 4))
  7378. nil))
  7379. ((= dcst ?a)
  7380. (if (looking-at org-complex-heading-regexp)
  7381. (funcall case-func (match-string 4))
  7382. nil))
  7383. ((= dcst ?t)
  7384. (let ((end (save-excursion (outline-next-heading) (point))))
  7385. (if (or (re-search-forward org-ts-regexp end t)
  7386. (re-search-forward org-ts-regexp-both end t))
  7387. (org-time-string-to-seconds (match-string 0))
  7388. (org-float-time now))))
  7389. ((= dcst ?c)
  7390. (let ((end (save-excursion (outline-next-heading) (point))))
  7391. (if (re-search-forward
  7392. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7393. end t)
  7394. (org-time-string-to-seconds (match-string 0))
  7395. (org-float-time now))))
  7396. ((= dcst ?s)
  7397. (let ((end (save-excursion (outline-next-heading) (point))))
  7398. (if (re-search-forward org-scheduled-time-regexp end t)
  7399. (org-time-string-to-seconds (match-string 1))
  7400. (org-float-time now))))
  7401. ((= dcst ?d)
  7402. (let ((end (save-excursion (outline-next-heading) (point))))
  7403. (if (re-search-forward org-deadline-time-regexp end t)
  7404. (org-time-string-to-seconds (match-string 1))
  7405. (org-float-time now))))
  7406. ((= dcst ?p)
  7407. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7408. (string-to-char (match-string 2))
  7409. org-default-priority))
  7410. ((= dcst ?r)
  7411. (or (org-entry-get nil property) ""))
  7412. ((= dcst ?o)
  7413. (if (looking-at org-complex-heading-regexp)
  7414. (- 9999 (length (member (match-string 2)
  7415. org-todo-keywords-1)))))
  7416. ((= dcst ?f)
  7417. (if getkey-func
  7418. (progn
  7419. (setq tmp (funcall getkey-func))
  7420. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7421. tmp)
  7422. (error "Invalid key function `%s'" getkey-func)))
  7423. (t (error "Invalid sorting type `%c'" sorting-type))))
  7424. nil
  7425. (cond
  7426. ((= dcst ?a) 'string<)
  7427. ((= dcst ?f) compare-func)
  7428. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7429. (t nil)))))
  7430. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7431. (message "Sorting entries...done")))
  7432. (defun org-do-sort (table what &optional with-case sorting-type)
  7433. "Sort TABLE of WHAT according to SORTING-TYPE.
  7434. The user will be prompted for the SORTING-TYPE if the call to this
  7435. function does not specify it. WHAT is only for the prompt, to indicate
  7436. what is being sorted. The sorting key will be extracted from
  7437. the car of the elements of the table.
  7438. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7439. (unless sorting-type
  7440. (message
  7441. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7442. what)
  7443. (setq sorting-type (read-char-exclusive)))
  7444. (let ((dcst (downcase sorting-type))
  7445. extractfun comparefun)
  7446. ;; Define the appropriate functions
  7447. (cond
  7448. ((= dcst ?n)
  7449. (setq extractfun 'string-to-number
  7450. comparefun (if (= dcst sorting-type) '< '>)))
  7451. ((= dcst ?a)
  7452. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7453. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7454. comparefun (if (= dcst sorting-type)
  7455. 'string<
  7456. (lambda (a b) (and (not (string< a b))
  7457. (not (string= a b)))))))
  7458. ((= dcst ?t)
  7459. (setq extractfun
  7460. (lambda (x)
  7461. (if (or (string-match org-ts-regexp x)
  7462. (string-match org-ts-regexp-both x))
  7463. (org-float-time
  7464. (org-time-string-to-time (match-string 0 x)))
  7465. 0))
  7466. comparefun (if (= dcst sorting-type) '< '>)))
  7467. (t (error "Invalid sorting type `%c'" sorting-type)))
  7468. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7469. table)
  7470. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7471. ;;; The orgstruct minor mode
  7472. ;; Define a minor mode which can be used in other modes in order to
  7473. ;; integrate the org-mode structure editing commands.
  7474. ;; This is really a hack, because the org-mode structure commands use
  7475. ;; keys which normally belong to the major mode. Here is how it
  7476. ;; works: The minor mode defines all the keys necessary to operate the
  7477. ;; structure commands, but wraps the commands into a function which
  7478. ;; tests if the cursor is currently at a headline or a plain list
  7479. ;; item. If that is the case, the structure command is used,
  7480. ;; temporarily setting many Org-mode variables like regular
  7481. ;; expressions for filling etc. However, when any of those keys is
  7482. ;; used at a different location, function uses `key-binding' to look
  7483. ;; up if the key has an associated command in another currently active
  7484. ;; keymap (minor modes, major mode, global), and executes that
  7485. ;; command. There might be problems if any of the keys is otherwise
  7486. ;; used as a prefix key.
  7487. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7488. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7489. ;; addresses this by checking explicitly for both bindings.
  7490. (defvar orgstruct-mode-map (make-sparse-keymap)
  7491. "Keymap for the minor `orgstruct-mode'.")
  7492. (defvar org-local-vars nil
  7493. "List of local variables, for use by `orgstruct-mode'.")
  7494. ;;;###autoload
  7495. (define-minor-mode orgstruct-mode
  7496. "Toggle the minor mode `orgstruct-mode'.
  7497. This mode is for using Org-mode structure commands in other
  7498. modes. The following keys behave as if Org-mode were active, if
  7499. the cursor is on a headline, or on a plain list item (both as
  7500. defined by Org-mode).
  7501. M-up Move entry/item up
  7502. M-down Move entry/item down
  7503. M-left Promote
  7504. M-right Demote
  7505. M-S-up Move entry/item up
  7506. M-S-down Move entry/item down
  7507. M-S-left Promote subtree
  7508. M-S-right Demote subtree
  7509. M-q Fill paragraph and items like in Org-mode
  7510. C-c ^ Sort entries
  7511. C-c - Cycle list bullet
  7512. TAB Cycle item visibility
  7513. M-RET Insert new heading/item
  7514. S-M-RET Insert new TODO heading / Checkbox item
  7515. C-c C-c Set tags / toggle checkbox"
  7516. nil " OrgStruct" nil
  7517. (org-load-modules-maybe)
  7518. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7519. ;;;###autoload
  7520. (defun turn-on-orgstruct ()
  7521. "Unconditionally turn on `orgstruct-mode'."
  7522. (orgstruct-mode 1))
  7523. (defvar org-fb-vars nil)
  7524. (make-variable-buffer-local 'org-fb-vars)
  7525. (defun orgstruct++-mode (&optional arg)
  7526. "Toggle `orgstruct-mode', the enhanced version of it.
  7527. In addition to setting orgstruct-mode, this also exports all indentation
  7528. and autofilling variables from org-mode into the buffer. It will also
  7529. recognize item context in multiline items.
  7530. Note that turning off orgstruct-mode will *not* remove the
  7531. indentation/paragraph settings. This can only be done by refreshing the
  7532. major mode, for example with \\[normal-mode]."
  7533. (interactive "P")
  7534. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7535. (if (< arg 1)
  7536. (progn (orgstruct-mode -1)
  7537. (mapc (lambda(v)
  7538. (org-set-local (car v)
  7539. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7540. org-fb-vars))
  7541. (orgstruct-mode 1)
  7542. (setq org-fb-vars nil)
  7543. (let (var val)
  7544. (mapc
  7545. (lambda (x)
  7546. (when (string-match
  7547. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7548. (symbol-name (car x)))
  7549. (setq var (car x) val (nth 1 x))
  7550. (push (list var `(quote ,(eval var))) org-fb-vars)
  7551. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7552. org-local-vars)
  7553. (org-set-local 'orgstruct-is-++ t))))
  7554. (defvar orgstruct-is-++ nil
  7555. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7556. (make-variable-buffer-local 'orgstruct-is-++)
  7557. ;;;###autoload
  7558. (defun turn-on-orgstruct++ ()
  7559. "Unconditionally turn on `orgstruct++-mode'."
  7560. (orgstruct++-mode 1))
  7561. (defun orgstruct-error ()
  7562. "Error when there is no default binding for a structure key."
  7563. (interactive)
  7564. (error "This key has no function outside structure elements"))
  7565. (defun orgstruct-setup ()
  7566. "Setup orgstruct keymaps."
  7567. (let ((nfunc 0)
  7568. (bindings
  7569. (list
  7570. '([(meta up)] org-metaup)
  7571. '([(meta down)] org-metadown)
  7572. '([(meta left)] org-metaleft)
  7573. '([(meta right)] org-metaright)
  7574. '([(meta shift up)] org-shiftmetaup)
  7575. '([(meta shift down)] org-shiftmetadown)
  7576. '([(meta shift left)] org-shiftmetaleft)
  7577. '([(meta shift right)] org-shiftmetaright)
  7578. '([?\e (up)] org-metaup)
  7579. '([?\e (down)] org-metadown)
  7580. '([?\e (left)] org-metaleft)
  7581. '([?\e (right)] org-metaright)
  7582. '([?\e (shift up)] org-shiftmetaup)
  7583. '([?\e (shift down)] org-shiftmetadown)
  7584. '([?\e (shift left)] org-shiftmetaleft)
  7585. '([?\e (shift right)] org-shiftmetaright)
  7586. '([(shift up)] org-shiftup)
  7587. '([(shift down)] org-shiftdown)
  7588. '([(shift left)] org-shiftleft)
  7589. '([(shift right)] org-shiftright)
  7590. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7591. '("\M-q" fill-paragraph)
  7592. '("\C-c^" org-sort)
  7593. '("\C-c-" org-cycle-list-bullet)))
  7594. elt key fun cmd)
  7595. (while (setq elt (pop bindings))
  7596. (setq nfunc (1+ nfunc))
  7597. (setq key (org-key (car elt))
  7598. fun (nth 1 elt)
  7599. cmd (orgstruct-make-binding fun nfunc key))
  7600. (org-defkey orgstruct-mode-map key cmd))
  7601. ;; Special treatment needed for TAB and RET
  7602. (org-defkey orgstruct-mode-map [(tab)]
  7603. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7604. (org-defkey orgstruct-mode-map "\C-i"
  7605. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7606. (org-defkey orgstruct-mode-map "\M-\C-m"
  7607. (orgstruct-make-binding 'org-insert-heading 105
  7608. "\M-\C-m" [(meta return)]))
  7609. (org-defkey orgstruct-mode-map [(meta return)]
  7610. (orgstruct-make-binding 'org-insert-heading 106
  7611. [(meta return)] "\M-\C-m"))
  7612. (org-defkey orgstruct-mode-map [(shift meta return)]
  7613. (orgstruct-make-binding 'org-insert-todo-heading 107
  7614. [(meta return)] "\M-\C-m"))
  7615. (org-defkey orgstruct-mode-map "\e\C-m"
  7616. (orgstruct-make-binding 'org-insert-heading 108
  7617. "\e\C-m" [?\e (return)]))
  7618. (org-defkey orgstruct-mode-map [?\e (return)]
  7619. (orgstruct-make-binding 'org-insert-heading 109
  7620. [?\e (return)] "\e\C-m"))
  7621. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7622. (orgstruct-make-binding 'org-insert-todo-heading 110
  7623. [?\e (return)] "\e\C-m"))
  7624. (unless org-local-vars
  7625. (setq org-local-vars (org-get-local-variables)))
  7626. t))
  7627. (defun orgstruct-make-binding (fun n &rest keys)
  7628. "Create a function for binding in the structure minor mode.
  7629. FUN is the command to call inside a table. N is used to create a unique
  7630. command name. KEYS are keys that should be checked in for a command
  7631. to execute outside of tables."
  7632. (eval
  7633. (list 'defun
  7634. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7635. '(arg)
  7636. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7637. "Outside of structure, run the binding of `"
  7638. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7639. "'.")
  7640. '(interactive "p")
  7641. (list 'if
  7642. `(org-context-p 'headline 'item
  7643. (and orgstruct-is-++
  7644. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7645. 'item-body))
  7646. (list 'org-run-like-in-org-mode (list 'quote fun))
  7647. (list 'let '(orgstruct-mode)
  7648. (list 'call-interactively
  7649. (append '(or)
  7650. (mapcar (lambda (k)
  7651. (list 'key-binding k))
  7652. keys)
  7653. '('orgstruct-error))))))))
  7654. (defun org-context-p (&rest contexts)
  7655. "Check if local context is any of CONTEXTS.
  7656. Possible values in the list of contexts are `table', `headline', and `item'."
  7657. (let ((pos (point)))
  7658. (goto-char (point-at-bol))
  7659. (prog1 (or (and (memq 'table contexts)
  7660. (looking-at "[ \t]*|"))
  7661. (and (memq 'headline contexts)
  7662. (looking-at org-outline-regexp))
  7663. (and (memq 'item contexts)
  7664. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7665. (and (memq 'item-body contexts)
  7666. (org-in-item-p)))
  7667. (goto-char pos))))
  7668. (defun org-get-local-variables ()
  7669. "Return a list of all local variables in an org-mode buffer."
  7670. (let (varlist)
  7671. (with-current-buffer (get-buffer-create "*Org tmp*")
  7672. (erase-buffer)
  7673. (org-mode)
  7674. (setq varlist (buffer-local-variables)))
  7675. (kill-buffer "*Org tmp*")
  7676. (delq nil
  7677. (mapcar
  7678. (lambda (x)
  7679. (setq x
  7680. (if (symbolp x)
  7681. (list x)
  7682. (list (car x) (list 'quote (cdr x)))))
  7683. (if (string-match
  7684. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7685. (symbol-name (car x)))
  7686. x nil))
  7687. varlist))))
  7688. (defun org-clone-local-variables (from-buffer &optional regexp)
  7689. "Clone local variables from FROM-BUFFER.
  7690. Optional argument REGEXP selects variables to clone."
  7691. (mapc
  7692. (lambda (pair)
  7693. (and (symbolp (car pair))
  7694. (or (null regexp)
  7695. (string-match regexp (symbol-name (car pair))))
  7696. (set (make-local-variable (car pair))
  7697. (cdr pair))))
  7698. (buffer-local-variables from-buffer)))
  7699. ;;;###autoload
  7700. (defun org-run-like-in-org-mode (cmd)
  7701. "Run a command, pretending that the current buffer is in Org-mode.
  7702. This will temporarily bind local variables that are typically bound in
  7703. Org-mode to the values they have in Org-mode, and then interactively
  7704. call CMD."
  7705. (org-load-modules-maybe)
  7706. (unless org-local-vars
  7707. (setq org-local-vars (org-get-local-variables)))
  7708. (eval (list 'let org-local-vars
  7709. (list 'call-interactively (list 'quote cmd)))))
  7710. ;;;; Archiving
  7711. (defun org-get-category (&optional pos force-refresh)
  7712. "Get the category applying to position POS."
  7713. (save-match-data
  7714. (if force-refresh (org-refresh-category-properties))
  7715. (let ((pos (or pos (point))))
  7716. (or (get-text-property pos 'org-category)
  7717. (progn (org-refresh-category-properties)
  7718. (get-text-property pos 'org-category))))))
  7719. (defun org-refresh-category-properties ()
  7720. "Refresh category text properties in the buffer."
  7721. (let ((inhibit-read-only t)
  7722. (def-cat (cond
  7723. ((null org-category)
  7724. (if buffer-file-name
  7725. (file-name-sans-extension
  7726. (file-name-nondirectory buffer-file-name))
  7727. "???"))
  7728. ((symbolp org-category) (symbol-name org-category))
  7729. (t org-category)))
  7730. beg end cat pos optionp)
  7731. (org-unmodified
  7732. (save-excursion
  7733. (save-restriction
  7734. (widen)
  7735. (goto-char (point-min))
  7736. (put-text-property (point) (point-max) 'org-category def-cat)
  7737. (while (re-search-forward
  7738. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7739. (setq pos (match-end 0)
  7740. optionp (equal (char-after (match-beginning 0)) ?#)
  7741. cat (org-trim (match-string 2)))
  7742. (if optionp
  7743. (setq beg (point-at-bol) end (point-max))
  7744. (org-back-to-heading t)
  7745. (setq beg (point) end (org-end-of-subtree t t)))
  7746. (put-text-property beg end 'org-category cat)
  7747. (put-text-property beg end 'org-category-position beg)
  7748. (goto-char pos)))))))
  7749. ;;;; Link Stuff
  7750. ;;; Link abbreviations
  7751. (defun org-link-expand-abbrev (link)
  7752. "Apply replacements as defined in `org-link-abbrev-alist'."
  7753. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7754. (let* ((key (match-string 1 link))
  7755. (as (or (assoc key org-link-abbrev-alist-local)
  7756. (assoc key org-link-abbrev-alist)))
  7757. (tag (and (match-end 2) (match-string 3 link)))
  7758. rpl)
  7759. (if (not as)
  7760. link
  7761. (setq rpl (cdr as))
  7762. (cond
  7763. ((symbolp rpl) (funcall rpl tag))
  7764. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7765. ((string-match "%h" rpl)
  7766. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7767. (t (concat rpl tag)))))
  7768. link))
  7769. ;;; Storing and inserting links
  7770. (defvar org-insert-link-history nil
  7771. "Minibuffer history for links inserted with `org-insert-link'.")
  7772. (defvar org-stored-links nil
  7773. "Contains the links stored with `org-store-link'.")
  7774. (defvar org-store-link-plist nil
  7775. "Plist with info about the most recently link created with `org-store-link'.")
  7776. (defvar org-link-protocols nil
  7777. "Link protocols added to Org-mode using `org-add-link-type'.")
  7778. (defvar org-store-link-functions nil
  7779. "List of functions that are called to create and store a link.
  7780. Each function will be called in turn until one returns a non-nil
  7781. value. Each function should check if it is responsible for creating
  7782. this link (for example by looking at the major mode).
  7783. If not, it must exit and return nil.
  7784. If yes, it should return a non-nil value after a calling
  7785. `org-store-link-props' with a list of properties and values.
  7786. Special properties are:
  7787. :type The link prefix, like \"http\". This must be given.
  7788. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7789. This is obligatory as well.
  7790. :description Optional default description for the second pair
  7791. of brackets in an Org-mode link. The user can still change
  7792. this when inserting this link into an Org-mode buffer.
  7793. In addition to these, any additional properties can be specified
  7794. and then used in remember templates.")
  7795. (defun org-add-link-type (type &optional follow export)
  7796. "Add TYPE to the list of `org-link-types'.
  7797. Re-compute all regular expressions depending on `org-link-types'
  7798. FOLLOW and EXPORT are two functions.
  7799. FOLLOW should take the link path as the single argument and do whatever
  7800. is necessary to follow the link, for example find a file or display
  7801. a mail message.
  7802. EXPORT should format the link path for export to one of the export formats.
  7803. It should be a function accepting three arguments:
  7804. path the path of the link, the text after the prefix (like \"http:\")
  7805. desc the description of the link, if any, or a description added by
  7806. org-export-normalize-links if there is none
  7807. format the export format, a symbol like `html' or `latex' or `ascii'..
  7808. The function may use the FORMAT information to return different values
  7809. depending on the format. The return value will be put literally into
  7810. the exported file. If the return value is nil, this means Org should
  7811. do what it normally does with links which do not have EXPORT defined.
  7812. Org-mode has a built-in default for exporting links. If you are happy with
  7813. this default, there is no need to define an export function for the link
  7814. type. For a simple example of an export function, see `org-bbdb.el'."
  7815. (add-to-list 'org-link-types type t)
  7816. (org-make-link-regexps)
  7817. (if (assoc type org-link-protocols)
  7818. (setcdr (assoc type org-link-protocols) (list follow export))
  7819. (push (list type follow export) org-link-protocols)))
  7820. (defvar org-agenda-buffer-name)
  7821. ;;;###autoload
  7822. (defun org-store-link (arg)
  7823. "\\<org-mode-map>Store an org-link to the current location.
  7824. This link is added to `org-stored-links' and can later be inserted
  7825. into an org-buffer with \\[org-insert-link].
  7826. For some link types, a prefix arg is interpreted:
  7827. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7828. For file links, arg negates `org-context-in-file-links'."
  7829. (interactive "P")
  7830. (org-load-modules-maybe)
  7831. (setq org-store-link-plist nil) ; reset
  7832. (org-with-limited-levels
  7833. (let (link cpltxt desc description search txt custom-id agenda-link)
  7834. (cond
  7835. ((run-hook-with-args-until-success 'org-store-link-functions)
  7836. (setq link (plist-get org-store-link-plist :link)
  7837. desc (or (plist-get org-store-link-plist :description) link)))
  7838. ((org-src-edit-buffer-p)
  7839. (let (label gc)
  7840. (while (or (not label)
  7841. (save-excursion
  7842. (save-restriction
  7843. (widen)
  7844. (goto-char (point-min))
  7845. (re-search-forward
  7846. (regexp-quote (format org-coderef-label-format label))
  7847. nil t))))
  7848. (when label (message "Label exists already") (sit-for 2))
  7849. (setq label (read-string "Code line label: " label)))
  7850. (end-of-line 1)
  7851. (setq link (format org-coderef-label-format label))
  7852. (setq gc (- 79 (length link)))
  7853. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7854. (insert link)
  7855. (setq link (concat "(" label ")") desc nil)))
  7856. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7857. ;; We are in the agenda, link to referenced location
  7858. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7859. (get-text-property (point) 'org-marker))))
  7860. (when m
  7861. (org-with-point-at m
  7862. (setq agenda-link
  7863. (if (org-called-interactively-p 'any)
  7864. (call-interactively 'org-store-link)
  7865. (org-store-link nil)))))))
  7866. ((eq major-mode 'calendar-mode)
  7867. (let ((cd (calendar-cursor-to-date)))
  7868. (setq link
  7869. (format-time-string
  7870. (car org-time-stamp-formats)
  7871. (apply 'encode-time
  7872. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7873. nil nil nil))))
  7874. (org-store-link-props :type "calendar" :date cd)))
  7875. ((eq major-mode 'w3-mode)
  7876. (setq cpltxt (if (and (buffer-name)
  7877. (not (string-match "Untitled" (buffer-name))))
  7878. (buffer-name)
  7879. (url-view-url t))
  7880. link (org-make-link (url-view-url t)))
  7881. (org-store-link-props :type "w3" :url (url-view-url t)))
  7882. ((eq major-mode 'w3m-mode)
  7883. (setq cpltxt (or w3m-current-title w3m-current-url)
  7884. link (org-make-link w3m-current-url))
  7885. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7886. ((setq search (run-hook-with-args-until-success
  7887. 'org-create-file-search-functions))
  7888. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7889. "::" search))
  7890. (setq cpltxt (or description link)))
  7891. ((eq major-mode 'image-mode)
  7892. (setq cpltxt (concat "file:"
  7893. (abbreviate-file-name buffer-file-name))
  7894. link (org-make-link cpltxt))
  7895. (org-store-link-props :type "image" :file buffer-file-name))
  7896. ((eq major-mode 'dired-mode)
  7897. ;; link to the file in the current line
  7898. (let ((file (dired-get-filename nil t)))
  7899. (setq file (if file
  7900. (abbreviate-file-name
  7901. (expand-file-name (dired-get-filename nil t)))
  7902. ;; otherwise, no file so use current directory.
  7903. default-directory))
  7904. (setq cpltxt (concat "file:" file)
  7905. link (org-make-link cpltxt))))
  7906. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7907. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7908. (cond
  7909. ((org-in-regexp "<<\\(.*?\\)>>")
  7910. (setq cpltxt
  7911. (concat "file:"
  7912. (abbreviate-file-name
  7913. (buffer-file-name (buffer-base-buffer)))
  7914. "::" (match-string 1))
  7915. link (org-make-link cpltxt)))
  7916. ((and (featurep 'org-id)
  7917. (or (eq org-link-to-org-use-id t)
  7918. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7919. (org-called-interactively-p 'any))
  7920. (and (eq org-link-to-org-use-id
  7921. 'create-if-interactive-and-no-custom-id)
  7922. (org-called-interactively-p 'any)
  7923. (not custom-id))
  7924. (and org-link-to-org-use-id
  7925. (org-entry-get nil "ID"))))
  7926. ;; We can make a link using the ID.
  7927. (setq link (condition-case nil
  7928. (prog1 (org-id-store-link)
  7929. (setq desc (plist-get org-store-link-plist
  7930. :description)))
  7931. (error
  7932. ;; probably before first headline, link to file only
  7933. (concat "file:"
  7934. (abbreviate-file-name
  7935. (buffer-file-name (buffer-base-buffer))))))))
  7936. (t
  7937. ;; Just link to current headline
  7938. (setq cpltxt (concat "file:"
  7939. (abbreviate-file-name
  7940. (buffer-file-name (buffer-base-buffer)))))
  7941. ;; Add a context search string
  7942. (when (org-xor org-context-in-file-links arg)
  7943. (setq txt (cond
  7944. ((org-at-heading-p) nil)
  7945. ((org-region-active-p)
  7946. (buffer-substring (region-beginning) (region-end)))
  7947. (t nil)))
  7948. (when (or (null txt) (string-match "\\S-" txt))
  7949. (setq cpltxt
  7950. (concat cpltxt "::"
  7951. (condition-case nil
  7952. (org-make-org-heading-search-string txt)
  7953. (error "")))
  7954. desc (or (nth 4 (ignore-errors
  7955. (org-heading-components))) "NONE"))))
  7956. (if (string-match "::\\'" cpltxt)
  7957. (setq cpltxt (substring cpltxt 0 -2)))
  7958. (setq link (org-make-link cpltxt)))))
  7959. ((buffer-file-name (buffer-base-buffer))
  7960. ;; Just link to this file here.
  7961. (setq cpltxt (concat "file:"
  7962. (abbreviate-file-name
  7963. (buffer-file-name (buffer-base-buffer)))))
  7964. ;; Add a context string
  7965. (when (org-xor org-context-in-file-links arg)
  7966. (setq txt (if (org-region-active-p)
  7967. (buffer-substring (region-beginning) (region-end))
  7968. (buffer-substring (point-at-bol) (point-at-eol))))
  7969. ;; Only use search option if there is some text.
  7970. (when (string-match "\\S-" txt)
  7971. (setq cpltxt
  7972. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7973. desc "NONE")))
  7974. (setq link (org-make-link cpltxt)))
  7975. ((org-called-interactively-p 'interactive)
  7976. (error "Cannot link to a buffer which is not visiting a file"))
  7977. (t (setq link nil)))
  7978. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7979. (setq link (or link cpltxt)
  7980. desc (or desc cpltxt))
  7981. (if (equal desc "NONE") (setq desc nil))
  7982. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7983. (progn
  7984. (setq org-stored-links
  7985. (cons (list link desc) org-stored-links))
  7986. (message "Stored: %s" (or desc link))
  7987. (when custom-id
  7988. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7989. "::#" custom-id))
  7990. (setq org-stored-links
  7991. (cons (list link desc) org-stored-links))))
  7992. (or agenda-link (and link (org-make-link-string link desc)))))))
  7993. (defun org-store-link-props (&rest plist)
  7994. "Store link properties, extract names and addresses."
  7995. (let (x adr)
  7996. (when (setq x (plist-get plist :from))
  7997. (setq adr (mail-extract-address-components x))
  7998. (setq plist (plist-put plist :fromname (car adr)))
  7999. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8000. (when (setq x (plist-get plist :to))
  8001. (setq adr (mail-extract-address-components x))
  8002. (setq plist (plist-put plist :toname (car adr)))
  8003. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8004. (let ((from (plist-get plist :from))
  8005. (to (plist-get plist :to)))
  8006. (when (and from to org-from-is-user-regexp)
  8007. (setq plist
  8008. (plist-put plist :fromto
  8009. (if (string-match org-from-is-user-regexp from)
  8010. (concat "to %t")
  8011. (concat "from %f"))))))
  8012. (setq org-store-link-plist plist))
  8013. (defun org-add-link-props (&rest plist)
  8014. "Add these properties to the link property list."
  8015. (let (key value)
  8016. (while plist
  8017. (setq key (pop plist) value (pop plist))
  8018. (setq org-store-link-plist
  8019. (plist-put org-store-link-plist key value)))))
  8020. (defun org-email-link-description (&optional fmt)
  8021. "Return the description part of an email link.
  8022. This takes information from `org-store-link-plist' and formats it
  8023. according to FMT (default from `org-email-link-description-format')."
  8024. (setq fmt (or fmt org-email-link-description-format))
  8025. (let* ((p org-store-link-plist)
  8026. (to (plist-get p :toaddress))
  8027. (from (plist-get p :fromaddress))
  8028. (table
  8029. (list
  8030. (cons "%c" (plist-get p :fromto))
  8031. (cons "%F" (plist-get p :from))
  8032. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8033. (cons "%T" (plist-get p :to))
  8034. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8035. (cons "%s" (plist-get p :subject))
  8036. (cons "%d" (plist-get p :date))
  8037. (cons "%m" (plist-get p :message-id)))))
  8038. (when (string-match "%c" fmt)
  8039. ;; Check if the user wrote this message
  8040. (if (and org-from-is-user-regexp from to
  8041. (save-match-data (string-match org-from-is-user-regexp from)))
  8042. (setq fmt (replace-match "to %t" t t fmt))
  8043. (setq fmt (replace-match "from %f" t t fmt))))
  8044. (org-replace-escapes fmt table)))
  8045. (defun org-make-org-heading-search-string (&optional string heading)
  8046. "Make search string for STRING or current headline."
  8047. (interactive)
  8048. (let ((s (or string (org-get-heading)))
  8049. (lines org-context-in-file-links))
  8050. (unless (and string (not heading))
  8051. ;; We are using a headline, clean up garbage in there.
  8052. (if (string-match org-todo-regexp s)
  8053. (setq s (replace-match "" t t s)))
  8054. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8055. (setq s (replace-match "" t t s)))
  8056. (setq s (org-trim s))
  8057. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8058. org-comment-string "\\)") s)
  8059. (setq s (replace-match "" t t s)))
  8060. (while (string-match org-ts-regexp s)
  8061. (setq s (replace-match "" t t s))))
  8062. (or string (setq s (concat "*" s))) ; Add * for headlines
  8063. (when (and string (integerp lines) (> lines 0))
  8064. (let ((slines (org-split-string s "\n")))
  8065. (when (< lines (length slines))
  8066. (setq s (mapconcat
  8067. 'identity
  8068. (reverse (nthcdr (- (length slines) lines)
  8069. (reverse slines))) "\n")))))
  8070. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8071. (defun org-make-link (&rest strings)
  8072. "Concatenate STRINGS."
  8073. (apply 'concat strings))
  8074. (defun org-make-link-string (link &optional description)
  8075. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8076. (unless (string-match "\\S-" link)
  8077. (error "Empty link"))
  8078. (when (and description
  8079. (stringp description)
  8080. (not (string-match "\\S-" description)))
  8081. (setq description nil))
  8082. (when (stringp description)
  8083. ;; Remove brackets from the description, they are fatal.
  8084. (while (string-match "\\[" description)
  8085. (setq description (replace-match "{" t t description)))
  8086. (while (string-match "\\]" description)
  8087. (setq description (replace-match "}" t t description))))
  8088. (when (equal link description)
  8089. ;; No description needed, it is identical
  8090. (setq description nil))
  8091. (when (and (not description)
  8092. (not (string-match (org-image-file-name-regexp) link))
  8093. (not (equal link (org-link-escape link))))
  8094. (setq description (org-extract-attributes link)))
  8095. (setq link
  8096. (cond ((string-match (org-image-file-name-regexp) link) link)
  8097. ((string-match org-link-types-re link)
  8098. (concat (match-string 1 link)
  8099. (org-link-escape (substring link (match-end 1)))))
  8100. (t (org-link-escape link))))
  8101. (concat "[[" link "]"
  8102. (if description (concat "[" description "]") "")
  8103. "]"))
  8104. (defconst org-link-escape-chars
  8105. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8106. "List of characters that should be escaped in link.
  8107. This is the list that is used for internal purposes.")
  8108. (defvar org-url-encoding-use-url-hexify nil)
  8109. (defconst org-link-escape-chars-browser
  8110. '(?\ )
  8111. "List of escapes for characters that are problematic in links.
  8112. This is the list that is used before handing over to the browser.")
  8113. (defun org-link-escape (text &optional table merge)
  8114. "Return percent escaped representation of TEXT.
  8115. TEXT is a string with the text to escape.
  8116. Optional argument TABLE is a list with characters that should be
  8117. escaped. When nil, `org-link-escape-chars' is used.
  8118. If optional argument MERGE is set, merge TABLE into
  8119. `org-link-escape-chars'."
  8120. (if (and org-url-encoding-use-url-hexify (not table))
  8121. (url-hexify-string text)
  8122. (cond
  8123. ((and table merge)
  8124. (mapc (lambda (defchr)
  8125. (unless (member defchr table)
  8126. (setq table (cons defchr table)))) org-link-escape-chars))
  8127. ((null table)
  8128. (setq table org-link-escape-chars)))
  8129. (mapconcat
  8130. (lambda (char)
  8131. (if (or (member char table)
  8132. (< char 32) (= char 37) (> char 126))
  8133. (mapconcat (lambda (sequence-element)
  8134. (format "%%%.2X" sequence-element))
  8135. (or (encode-coding-char char 'utf-8)
  8136. (error "Unable to percent escape character: %s"
  8137. (char-to-string char))) "")
  8138. (char-to-string char))) text "")))
  8139. (defun org-link-unescape (str)
  8140. "Unhex hexified Unicode strings as returned from the JavaScript function
  8141. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8142. (unless (and (null str) (string= "" str))
  8143. (let ((pos 0) (case-fold-search t) unhexed)
  8144. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8145. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8146. (setq str (replace-match unhexed t t str))
  8147. (setq pos (+ pos (length unhexed))))))
  8148. str)
  8149. (defun org-link-unescape-compound (hex)
  8150. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8151. Note: this function also decodes single byte encodings like
  8152. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8153. (save-match-data
  8154. (let* ((bytes (cdr (split-string hex "%")))
  8155. (ret "")
  8156. (eat 0)
  8157. (sum 0))
  8158. (while bytes
  8159. (let* ((val (string-to-number (pop bytes) 16))
  8160. (shift-xor
  8161. (if (= 0 eat)
  8162. (cond
  8163. ((>= val 252) (cons 6 252))
  8164. ((>= val 248) (cons 5 248))
  8165. ((>= val 240) (cons 4 240))
  8166. ((>= val 224) (cons 3 224))
  8167. ((>= val 192) (cons 2 192))
  8168. (t (cons 0 0)))
  8169. (cons 6 128))))
  8170. (if (>= val 192) (setq eat (car shift-xor)))
  8171. (setq val (logxor val (cdr shift-xor)))
  8172. (setq sum (+ (lsh sum (car shift-xor)) val))
  8173. (if (> eat 0) (setq eat (- eat 1)))
  8174. (cond
  8175. ((= 0 eat) ;multi byte
  8176. (setq ret (concat ret (org-char-to-string sum)))
  8177. (setq sum 0))
  8178. ((not bytes) ; single byte(s)
  8179. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8180. )) ;; end (while bytes
  8181. ret )))
  8182. (defun org-link-unescape-single-byte-sequence (hex)
  8183. "Unhexify hex-encoded single byte character sequences."
  8184. (mapconcat (lambda (byte)
  8185. (char-to-string (string-to-number byte 16)))
  8186. (cdr (split-string hex "%")) ""))
  8187. (defun org-xor (a b)
  8188. "Exclusive or."
  8189. (if a (not b) b))
  8190. (defun org-fixup-message-id-for-http (s)
  8191. "Replace special characters in a message id, so it can be used in an http query."
  8192. (when (string-match "%" s)
  8193. (setq s (mapconcat (lambda (c)
  8194. (if (eq c ?%)
  8195. "%25"
  8196. (char-to-string c)))
  8197. s "")))
  8198. (while (string-match "<" s)
  8199. (setq s (replace-match "%3C" t t s)))
  8200. (while (string-match ">" s)
  8201. (setq s (replace-match "%3E" t t s)))
  8202. (while (string-match "@" s)
  8203. (setq s (replace-match "%40" t t s)))
  8204. s)
  8205. (defun org-link-prettify (link)
  8206. "Return a human-readable representation of LINK.
  8207. The car of LINK must be a raw link the cdr of LINK must be either
  8208. a link description or nil."
  8209. (let ((desc (or (cadr link) "<no description>")))
  8210. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8211. "[[" (car link) "]]")))
  8212. ;;;###autoload
  8213. (defun org-insert-link-global ()
  8214. "Insert a link like Org-mode does.
  8215. This command can be called in any mode to insert a link in Org-mode syntax."
  8216. (interactive)
  8217. (org-load-modules-maybe)
  8218. (org-run-like-in-org-mode 'org-insert-link))
  8219. (defun org-insert-link (&optional complete-file link-location default-description)
  8220. "Insert a link. At the prompt, enter the link.
  8221. Completion can be used to insert any of the link protocol prefixes like
  8222. http or ftp in use.
  8223. The history can be used to select a link previously stored with
  8224. `org-store-link'. When the empty string is entered (i.e. if you just
  8225. press RET at the prompt), the link defaults to the most recently
  8226. stored link. As SPC triggers completion in the minibuffer, you need to
  8227. use M-SPC or C-q SPC to force the insertion of a space character.
  8228. You will also be prompted for a description, and if one is given, it will
  8229. be displayed in the buffer instead of the link.
  8230. If there is already a link at point, this command will allow you to edit link
  8231. and description parts.
  8232. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8233. be selected using completion. The path to the file will be relative to the
  8234. current directory if the file is in the current directory or a subdirectory.
  8235. Otherwise, the link will be the absolute path as completed in the minibuffer
  8236. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8237. option `org-link-file-path-type'.
  8238. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8239. the current directory or below.
  8240. With three \\[universal-argument] prefixes, negate the meaning of
  8241. `org-keep-stored-link-after-insertion'.
  8242. If `org-make-link-description-function' is non-nil, this function will be
  8243. called with the link target, and the result will be the default
  8244. link description.
  8245. If the LINK-LOCATION parameter is non-nil, this value will be
  8246. used as the link location instead of reading one interactively.
  8247. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8248. be used as the default description."
  8249. (interactive "P")
  8250. (let* ((wcf (current-window-configuration))
  8251. (region (if (org-region-active-p)
  8252. (buffer-substring (region-beginning) (region-end))))
  8253. (remove (and region (list (region-beginning) (region-end))))
  8254. (desc region)
  8255. tmphist ; byte-compile incorrectly complains about this
  8256. (link link-location)
  8257. entry file all-prefixes)
  8258. (cond
  8259. (link-location) ; specified by arg, just use it.
  8260. ((org-in-regexp org-bracket-link-regexp 1)
  8261. ;; We do have a link at point, and we are going to edit it.
  8262. (setq remove (list (match-beginning 0) (match-end 0)))
  8263. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8264. (setq link (read-string "Link: "
  8265. (org-link-unescape
  8266. (org-match-string-no-properties 1)))))
  8267. ((or (org-in-regexp org-angle-link-re)
  8268. (org-in-regexp org-plain-link-re))
  8269. ;; Convert to bracket link
  8270. (setq remove (list (match-beginning 0) (match-end 0))
  8271. link (read-string "Link: "
  8272. (org-remove-angle-brackets (match-string 0)))))
  8273. ((member complete-file '((4) (16)))
  8274. ;; Completing read for file names.
  8275. (setq link (org-file-complete-link complete-file)))
  8276. (t
  8277. ;; Read link, with completion for stored links.
  8278. (with-output-to-temp-buffer "*Org Links*"
  8279. (princ "Insert a link.
  8280. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8281. (when org-stored-links
  8282. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8283. (princ (mapconcat 'org-link-prettify
  8284. (reverse org-stored-links) "\n"))))
  8285. (let ((cw (selected-window)))
  8286. (select-window (get-buffer-window "*Org Links*" 'visible))
  8287. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8288. (unless (pos-visible-in-window-p (point-max))
  8289. (org-fit-window-to-buffer))
  8290. (and (window-live-p cw) (select-window cw)))
  8291. ;; Fake a link history, containing the stored links.
  8292. (setq tmphist (append (mapcar 'car org-stored-links)
  8293. org-insert-link-history))
  8294. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8295. (mapcar 'car org-link-abbrev-alist)
  8296. org-link-types))
  8297. (unwind-protect
  8298. (progn
  8299. (setq link
  8300. (let ((org-completion-use-ido nil)
  8301. (org-completion-use-iswitchb nil))
  8302. (org-completing-read
  8303. "Link: "
  8304. (append
  8305. (mapcar (lambda (x) (list (concat x ":")))
  8306. all-prefixes)
  8307. (mapcar 'car org-stored-links))
  8308. nil nil nil
  8309. 'tmphist
  8310. (car (car org-stored-links)))))
  8311. (if (not (string-match "\\S-" link))
  8312. (error "No link selected"))
  8313. (if (or (member link all-prefixes)
  8314. (and (equal ":" (substring link -1))
  8315. (member (substring link 0 -1) all-prefixes)
  8316. (setq link (substring link 0 -1))))
  8317. (setq link (org-link-try-special-completion link))))
  8318. (set-window-configuration wcf)
  8319. (kill-buffer "*Org Links*"))
  8320. (setq entry (assoc link org-stored-links))
  8321. (or entry (push link org-insert-link-history))
  8322. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8323. (not org-keep-stored-link-after-insertion))
  8324. (setq org-stored-links (delq (assoc link org-stored-links)
  8325. org-stored-links)))
  8326. (setq desc (or desc (nth 1 entry)))))
  8327. (if (string-match org-plain-link-re link)
  8328. ;; URL-like link, normalize the use of angular brackets.
  8329. (setq link (org-make-link (org-remove-angle-brackets link))))
  8330. ;; Check if we are linking to the current file with a search option
  8331. ;; If yes, simplify the link by using only the search option.
  8332. (when (and buffer-file-name
  8333. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8334. (let* ((path (match-string 1 link))
  8335. (case-fold-search nil)
  8336. (search (match-string 2 link)))
  8337. (save-match-data
  8338. (if (equal (file-truename buffer-file-name) (file-truename path))
  8339. ;; We are linking to this same file, with a search option
  8340. (setq link search)))))
  8341. ;; Check if we can/should use a relative path. If yes, simplify the link
  8342. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8343. (let* ((type (match-string 1 link))
  8344. (path (match-string 2 link))
  8345. (origpath path)
  8346. (case-fold-search nil))
  8347. (cond
  8348. ((or (eq org-link-file-path-type 'absolute)
  8349. (equal complete-file '(16)))
  8350. (setq path (abbreviate-file-name (expand-file-name path))))
  8351. ((eq org-link-file-path-type 'noabbrev)
  8352. (setq path (expand-file-name path)))
  8353. ((eq org-link-file-path-type 'relative)
  8354. (setq path (file-relative-name path)))
  8355. (t
  8356. (save-match-data
  8357. (if (string-match (concat "^" (regexp-quote
  8358. (expand-file-name
  8359. (file-name-as-directory
  8360. default-directory))))
  8361. (expand-file-name path))
  8362. ;; We are linking a file with relative path name.
  8363. (setq path (substring (expand-file-name path)
  8364. (match-end 0)))
  8365. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8366. (setq link (concat type path))
  8367. (if (equal desc origpath)
  8368. (setq desc path))))
  8369. (if org-make-link-description-function
  8370. (setq desc (funcall org-make-link-description-function link desc))
  8371. (if default-description (setq desc default-description)))
  8372. (setq desc (read-string "Description: " desc))
  8373. (unless (string-match "\\S-" desc) (setq desc nil))
  8374. (if remove (apply 'delete-region remove))
  8375. (insert (org-make-link-string link desc))))
  8376. (defun org-link-try-special-completion (type)
  8377. "If there is completion support for link type TYPE, offer it."
  8378. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8379. (if (functionp fun)
  8380. (funcall fun)
  8381. (read-string "Link (no completion support): " (concat type ":")))))
  8382. (defun org-file-complete-link (&optional arg)
  8383. "Create a file link using completion."
  8384. (let (file link)
  8385. (setq file (read-file-name "File: "))
  8386. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8387. (pwd1 (file-name-as-directory (abbreviate-file-name
  8388. (expand-file-name ".")))))
  8389. (cond
  8390. ((equal arg '(16))
  8391. (setq link (org-make-link
  8392. "file:"
  8393. (abbreviate-file-name (expand-file-name file)))))
  8394. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8395. (setq link (org-make-link "file:" (match-string 1 file))))
  8396. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8397. (expand-file-name file))
  8398. (setq link (org-make-link
  8399. "file:" (match-string 1 (expand-file-name file)))))
  8400. (t (setq link (org-make-link "file:" file)))))
  8401. link))
  8402. (defun org-completing-read (&rest args)
  8403. "Completing-read with SPACE being a normal character."
  8404. (let ((enable-recursive-minibuffers t)
  8405. (minibuffer-local-completion-map
  8406. (copy-keymap minibuffer-local-completion-map)))
  8407. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8408. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8409. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8410. (apply 'org-icompleting-read args)))
  8411. (defun org-completing-read-no-i (&rest args)
  8412. (let (org-completion-use-ido org-completion-use-iswitchb)
  8413. (apply 'org-completing-read args)))
  8414. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8415. "Use iswitch as a completing-read replacement to choose from choices.
  8416. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8417. from."
  8418. (let* ((iswitchb-use-virtual-buffers nil)
  8419. (iswitchb-make-buflist-hook
  8420. (lambda ()
  8421. (setq iswitchb-temp-buflist choices))))
  8422. (iswitchb-read-buffer prompt)))
  8423. (defun org-icompleting-read (&rest args)
  8424. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8425. (org-without-partial-completion
  8426. (if (and org-completion-use-ido
  8427. (fboundp 'ido-completing-read)
  8428. (boundp 'ido-mode) ido-mode
  8429. (listp (second args)))
  8430. (let ((ido-enter-matching-directory nil))
  8431. (apply 'ido-completing-read (concat (car args))
  8432. (if (consp (car (nth 1 args)))
  8433. (mapcar 'car (nth 1 args))
  8434. (nth 1 args))
  8435. (cddr args)))
  8436. (if (and org-completion-use-iswitchb
  8437. (boundp 'iswitchb-mode) iswitchb-mode
  8438. (listp (second args)))
  8439. (apply 'org-iswitchb-completing-read (concat (car args))
  8440. (if (consp (car (nth 1 args)))
  8441. (mapcar 'car (nth 1 args))
  8442. (nth 1 args))
  8443. (cddr args))
  8444. (apply 'completing-read args)))))
  8445. (defun org-extract-attributes (s)
  8446. "Extract the attributes cookie from a string and set as text property."
  8447. (let (a attr (start 0) key value)
  8448. (save-match-data
  8449. (when (string-match "{{\\([^}]+\\)}}$" s)
  8450. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8451. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8452. (setq key (match-string 1 a) value (match-string 2 a)
  8453. start (match-end 0)
  8454. attr (plist-put attr (intern key) value))))
  8455. (org-add-props s nil 'org-attr attr))
  8456. s))
  8457. (defun org-extract-attributes-from-string (tag)
  8458. (let (key value attr)
  8459. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8460. (setq key (match-string 1 tag) value (match-string 2 tag)
  8461. tag (replace-match "" t t tag)
  8462. attr (plist-put attr (intern key) value)))
  8463. (cons tag attr)))
  8464. (defun org-attributes-to-string (plist)
  8465. "Format a property list into an HTML attribute list."
  8466. (let ((s "") key value)
  8467. (while plist
  8468. (setq key (pop plist) value (pop plist))
  8469. (and value
  8470. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8471. s))
  8472. ;;; Opening/following a link
  8473. (defvar org-link-search-failed nil)
  8474. (defvar org-open-link-functions nil
  8475. "Hook for functions finding a plain text link.
  8476. These functions must take a single argument, the link content.
  8477. They will be called for links that look like [[link text][description]]
  8478. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8479. like a filename (e.g. \"./blue.png\").
  8480. These functions will be called *before* Org attempts to resolve the
  8481. link by doing text searches in the current buffer - so if you want a
  8482. link \"[[target]]\" to still find \"<<target>>\", your function should
  8483. handle this as a special case.
  8484. When the function does handle the link, it must return a non-nil value.
  8485. If it decides that it is not responsible for this link, it must return
  8486. nil to indicate that that Org-mode can continue with other options
  8487. like exact and fuzzy text search.")
  8488. (defun org-next-link ()
  8489. "Move forward to the next link.
  8490. If the link is in hidden text, expose it."
  8491. (interactive)
  8492. (when (and org-link-search-failed (eq this-command last-command))
  8493. (goto-char (point-min))
  8494. (message "Link search wrapped back to beginning of buffer"))
  8495. (setq org-link-search-failed nil)
  8496. (let* ((pos (point))
  8497. (ct (org-context))
  8498. (a (assoc :link ct)))
  8499. (if a (goto-char (nth 2 a)))
  8500. (if (re-search-forward org-any-link-re nil t)
  8501. (progn
  8502. (goto-char (match-beginning 0))
  8503. (if (outline-invisible-p) (org-show-context)))
  8504. (goto-char pos)
  8505. (setq org-link-search-failed t)
  8506. (error "No further link found"))))
  8507. (defun org-previous-link ()
  8508. "Move backward to the previous link.
  8509. If the link is in hidden text, expose it."
  8510. (interactive)
  8511. (when (and org-link-search-failed (eq this-command last-command))
  8512. (goto-char (point-max))
  8513. (message "Link search wrapped back to end of buffer"))
  8514. (setq org-link-search-failed nil)
  8515. (let* ((pos (point))
  8516. (ct (org-context))
  8517. (a (assoc :link ct)))
  8518. (if a (goto-char (nth 1 a)))
  8519. (if (re-search-backward org-any-link-re nil t)
  8520. (progn
  8521. (goto-char (match-beginning 0))
  8522. (if (outline-invisible-p) (org-show-context)))
  8523. (goto-char pos)
  8524. (setq org-link-search-failed t)
  8525. (error "No further link found"))))
  8526. (defun org-translate-link (s)
  8527. "Translate a link string if a translation function has been defined."
  8528. (if (and org-link-translation-function
  8529. (fboundp org-link-translation-function)
  8530. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8531. (progn
  8532. (setq s (funcall org-link-translation-function
  8533. (match-string 1 s) (match-string 2 s)))
  8534. (concat (car s) ":" (cdr s)))
  8535. s))
  8536. (defun org-translate-link-from-planner (type path)
  8537. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8538. This is still an experimental function, your mileage may vary."
  8539. (cond
  8540. ((member type '("http" "https" "news" "ftp"))
  8541. ;; standard Internet links are the same.
  8542. nil)
  8543. ((and (equal type "irc") (string-match "^//" path))
  8544. ;; Planner has two / at the beginning of an irc link, we have 1.
  8545. ;; We should have zero, actually....
  8546. (setq path (substring path 1)))
  8547. ((and (equal type "lisp") (string-match "^/" path))
  8548. ;; Planner has a slash, we do not.
  8549. (setq type "elisp" path (substring path 1)))
  8550. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8551. ;; A typical message link. Planner has the id after the final slash,
  8552. ;; we separate it with a hash mark
  8553. (setq path (concat (match-string 1 path) "#"
  8554. (org-remove-angle-brackets (match-string 2 path)))))
  8555. )
  8556. (cons type path))
  8557. (defun org-find-file-at-mouse (ev)
  8558. "Open file link or URL at mouse."
  8559. (interactive "e")
  8560. (mouse-set-point ev)
  8561. (org-open-at-point 'in-emacs))
  8562. (defun org-open-at-mouse (ev)
  8563. "Open file link or URL at mouse.
  8564. See the docstring of `org-open-file' for details."
  8565. (interactive "e")
  8566. (mouse-set-point ev)
  8567. (if (eq major-mode 'org-agenda-mode)
  8568. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8569. (org-open-at-point))
  8570. (defvar org-window-config-before-follow-link nil
  8571. "The window configuration before following a link.
  8572. This is saved in case the need arises to restore it.")
  8573. (defvar org-open-link-marker (make-marker)
  8574. "Marker pointing to the location where `org-open-at-point; was called.")
  8575. ;;;###autoload
  8576. (defun org-open-at-point-global ()
  8577. "Follow a link like Org-mode does.
  8578. This command can be called in any mode to follow a link that has
  8579. Org-mode syntax."
  8580. (interactive)
  8581. (org-run-like-in-org-mode 'org-open-at-point))
  8582. ;;;###autoload
  8583. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8584. "Open a link in the string S, as if it was in Org-mode."
  8585. (interactive "sLink: \nP")
  8586. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8587. (with-temp-buffer
  8588. (let ((org-inhibit-startup (not reference-buffer)))
  8589. (org-mode)
  8590. (insert s)
  8591. (goto-char (point-min))
  8592. (when reference-buffer
  8593. (setq org-link-abbrev-alist-local
  8594. (with-current-buffer reference-buffer
  8595. org-link-abbrev-alist-local)))
  8596. (org-open-at-point arg reference-buffer)))))
  8597. (defvar org-open-at-point-functions nil
  8598. "Hook that is run when following a link at point.
  8599. Functions in this hook must return t if they identify and follow
  8600. a link at point. If they don't find anything interesting at point,
  8601. they must return nil.")
  8602. (defun org-open-at-point (&optional arg reference-buffer)
  8603. "Open link at or after point.
  8604. If there is no link at point, this function will search forward up to
  8605. the end of the current line.
  8606. Normally, files will be opened by an appropriate application. If the
  8607. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8608. With a double prefix argument, try to open outside of Emacs, in the
  8609. application the system uses for this file type."
  8610. (interactive "P")
  8611. ;; if in a code block, then open the block's results
  8612. (unless (call-interactively #'org-babel-open-src-block-result)
  8613. (org-load-modules-maybe)
  8614. (move-marker org-open-link-marker (point))
  8615. (setq org-window-config-before-follow-link (current-window-configuration))
  8616. (org-remove-occur-highlights nil nil t)
  8617. (cond
  8618. ((and (org-at-heading-p)
  8619. (not (org-in-regexp
  8620. (concat org-plain-link-re "\\|"
  8621. org-bracket-link-regexp "\\|"
  8622. org-angle-link-re "\\|"
  8623. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8624. (not (get-text-property (point) 'org-linked-text)))
  8625. (or (org-offer-links-in-entry arg)
  8626. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8627. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8628. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8629. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8630. (not (org-in-regexp org-bracket-link-regexp)))
  8631. (org-footnote-action))
  8632. (t
  8633. (let (type path link line search (pos (point)))
  8634. (catch 'match
  8635. (save-excursion
  8636. (skip-chars-forward "^]\n\r")
  8637. (when (org-in-regexp org-bracket-link-regexp 1)
  8638. (setq link (org-extract-attributes
  8639. (org-link-unescape (org-match-string-no-properties 1))))
  8640. (while (string-match " *\n *" link)
  8641. (setq link (replace-match " " t t link)))
  8642. (setq link (org-link-expand-abbrev link))
  8643. (cond
  8644. ((or (file-name-absolute-p link)
  8645. (string-match "^\\.\\.?/" link))
  8646. (setq type "file" path link))
  8647. ((string-match org-link-re-with-space3 link)
  8648. (setq type (match-string 1 link) path (match-string 2 link)))
  8649. (t (setq type "thisfile" path link)))
  8650. (throw 'match t)))
  8651. (when (get-text-property (point) 'org-linked-text)
  8652. (setq type "thisfile"
  8653. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8654. (1+ (point)) (point))
  8655. path (buffer-substring
  8656. (or (previous-single-property-change pos 'org-linked-text)
  8657. (point-min))
  8658. (or (next-single-property-change pos 'org-linked-text)
  8659. (point-max))))
  8660. (throw 'match t))
  8661. (save-excursion
  8662. (when (or (org-in-regexp org-angle-link-re)
  8663. (org-in-regexp org-plain-link-re))
  8664. (setq type (match-string 1)
  8665. path (org-link-unescape (match-string 2)))
  8666. (throw 'match t)))
  8667. (save-excursion
  8668. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8669. (setq type "tags"
  8670. path (match-string 1))
  8671. (while (string-match ":" path)
  8672. (setq path (replace-match "+" t t path)))
  8673. (throw 'match t)))
  8674. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8675. (setq type "tree-match"
  8676. path (match-string 1))
  8677. (throw 'match t)))
  8678. (unless path
  8679. (error "No link found"))
  8680. ;; switch back to reference buffer
  8681. ;; needed when if called in a temporary buffer through
  8682. ;; org-open-link-from-string
  8683. (with-current-buffer (or reference-buffer (current-buffer))
  8684. ;; Remove any trailing spaces in path
  8685. (if (string-match " +\\'" path)
  8686. (setq path (replace-match "" t t path)))
  8687. (if (and org-link-translation-function
  8688. (fboundp org-link-translation-function))
  8689. ;; Check if we need to translate the link
  8690. (let ((tmp (funcall org-link-translation-function type path)))
  8691. (setq type (car tmp) path (cdr tmp))))
  8692. (cond
  8693. ((assoc type org-link-protocols)
  8694. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8695. ((equal type "mailto")
  8696. (let ((cmd (car org-link-mailto-program))
  8697. (args (cdr org-link-mailto-program)) args1
  8698. (address path) (subject "") a)
  8699. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8700. (setq address (match-string 1 path)
  8701. subject (org-link-escape (match-string 2 path))))
  8702. (while args
  8703. (cond
  8704. ((not (stringp (car args))) (push (pop args) args1))
  8705. (t (setq a (pop args))
  8706. (if (string-match "%a" a)
  8707. (setq a (replace-match address t t a)))
  8708. (if (string-match "%s" a)
  8709. (setq a (replace-match subject t t a)))
  8710. (push a args1))))
  8711. (apply cmd (nreverse args1))))
  8712. ((member type '("http" "https" "ftp" "news"))
  8713. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8714. (org-link-escape
  8715. path org-link-escape-chars-browser)
  8716. path))))
  8717. ((string= type "doi")
  8718. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8719. (org-link-escape
  8720. path org-link-escape-chars-browser)
  8721. path))))
  8722. ((member type '("message"))
  8723. (browse-url (concat type ":" path)))
  8724. ((string= type "tags")
  8725. (org-tags-view arg path))
  8726. ((string= type "tree-match")
  8727. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8728. ((string= type "file")
  8729. (if (string-match "::\\([0-9]+\\)\\'" path)
  8730. (setq line (string-to-number (match-string 1 path))
  8731. path (substring path 0 (match-beginning 0)))
  8732. (if (string-match "::\\(.+\\)\\'" path)
  8733. (setq search (match-string 1 path)
  8734. path (substring path 0 (match-beginning 0)))))
  8735. (if (string-match "[*?{]" (file-name-nondirectory path))
  8736. (dired path)
  8737. (org-open-file path arg line search)))
  8738. ((string= type "shell")
  8739. (let ((cmd path))
  8740. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8741. (string-match org-confirm-shell-link-not-regexp cmd))
  8742. (not org-confirm-shell-link-function)
  8743. (funcall org-confirm-shell-link-function
  8744. (format "Execute \"%s\" in shell? "
  8745. (org-add-props cmd nil
  8746. 'face 'org-warning))))
  8747. (progn
  8748. (message "Executing %s" cmd)
  8749. (shell-command cmd))
  8750. (error "Abort"))))
  8751. ((string= type "elisp")
  8752. (let ((cmd path))
  8753. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8754. (string-match org-confirm-elisp-link-not-regexp cmd))
  8755. (not org-confirm-elisp-link-function)
  8756. (funcall org-confirm-elisp-link-function
  8757. (format "Execute \"%s\" as elisp? "
  8758. (org-add-props cmd nil
  8759. 'face 'org-warning))))
  8760. (message "%s => %s" cmd
  8761. (if (equal (string-to-char cmd) ?\()
  8762. (eval (read cmd))
  8763. (call-interactively (read cmd))))
  8764. (error "Abort"))))
  8765. ((and (string= type "thisfile")
  8766. (run-hook-with-args-until-success
  8767. 'org-open-link-functions path)))
  8768. ((string= type "thisfile")
  8769. (if arg
  8770. (switch-to-buffer-other-window
  8771. (org-get-buffer-for-internal-link (current-buffer)))
  8772. (org-mark-ring-push))
  8773. (let ((cmd `(org-link-search
  8774. ,path
  8775. ,(cond ((equal arg '(4)) ''occur)
  8776. ((equal arg '(16)) ''org-occur)
  8777. (t nil))
  8778. ,pos)))
  8779. (condition-case nil (let ((org-link-search-inhibit-query t))
  8780. (eval cmd))
  8781. (error (progn (widen) (eval cmd))))))
  8782. (t
  8783. (browse-url-at-point)))))))
  8784. (move-marker org-open-link-marker nil)
  8785. (run-hook-with-args 'org-follow-link-hook)))
  8786. (defun org-offer-links-in-entry (&optional nth zero)
  8787. "Offer links in the current entry and follow the selected link.
  8788. If there is only one link, follow it immediately as well.
  8789. If NTH is an integer, immediately pick the NTH link found.
  8790. If ZERO is a string, check also this string for a link, and if
  8791. there is one, offer it as link number zero."
  8792. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8793. "\\(" org-angle-link-re "\\)\\|"
  8794. "\\(" org-plain-link-re "\\)"))
  8795. (cnt ?0)
  8796. (in-emacs (if (integerp nth) nil nth))
  8797. have-zero end links link c)
  8798. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8799. (push (match-string 0 zero) links)
  8800. (setq cnt (1- cnt) have-zero t))
  8801. (save-excursion
  8802. (org-back-to-heading t)
  8803. (setq end (save-excursion (outline-next-heading) (point)))
  8804. (while (re-search-forward re end t)
  8805. (push (match-string 0) links))
  8806. (setq links (org-uniquify (reverse links))))
  8807. (cond
  8808. ((null links)
  8809. (message "No links"))
  8810. ((equal (length links) 1)
  8811. (setq link (list (car links))))
  8812. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8813. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8814. (t ; we have to select a link
  8815. (save-excursion
  8816. (save-window-excursion
  8817. (delete-other-windows)
  8818. (with-output-to-temp-buffer "*Select Link*"
  8819. (mapc (lambda (l)
  8820. (if (not (string-match org-bracket-link-regexp l))
  8821. (princ (format "[%c] %s\n" (incf cnt)
  8822. (org-remove-angle-brackets l)))
  8823. (if (match-end 3)
  8824. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8825. (match-string 3 l) (match-string 1 l)))
  8826. (princ (format "[%c] %s\n" (incf cnt)
  8827. (match-string 1 l))))))
  8828. links))
  8829. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8830. (message "Select link to open, RET to open all:")
  8831. (setq c (read-char-exclusive))
  8832. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8833. (when (equal c ?q) (error "Abort"))
  8834. (if (equal c ?\C-m)
  8835. (setq link links)
  8836. (setq nth (- c ?0))
  8837. (if have-zero (setq nth (1+ nth)))
  8838. (unless (and (integerp nth) (>= (length links) nth))
  8839. (error "Invalid link selection"))
  8840. (setq link (list (nth (1- nth) links))))))
  8841. (if link
  8842. (let ((buf (current-buffer)))
  8843. (dolist (l link)
  8844. (org-open-link-from-string l in-emacs buf))
  8845. t)
  8846. nil)))
  8847. ;; Add special file links that specify the way of opening
  8848. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8849. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8850. (defun org-open-file-with-system (path)
  8851. "Open file at PATH using the system way of opening it."
  8852. (org-open-file path 'system))
  8853. (defun org-open-file-with-emacs (path)
  8854. "Open file at PATH in Emacs."
  8855. (org-open-file path 'emacs))
  8856. (defun org-remove-file-link-modifiers ()
  8857. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8858. (goto-char (point-min))
  8859. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8860. (org-if-unprotected
  8861. (replace-match "file:" t t))))
  8862. (eval-after-load "org-exp"
  8863. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8864. 'org-remove-file-link-modifiers))
  8865. ;;;; Time estimates
  8866. (defun org-get-effort (&optional pom)
  8867. "Get the effort estimate for the current entry."
  8868. (org-entry-get pom org-effort-property))
  8869. ;;; File search
  8870. (defvar org-create-file-search-functions nil
  8871. "List of functions to construct the right search string for a file link.
  8872. These functions are called in turn with point at the location to
  8873. which the link should point.
  8874. A function in the hook should first test if it would like to
  8875. handle this file type, for example by checking the `major-mode'
  8876. or the file extension. If it decides not to handle this file, it
  8877. should just return nil to give other functions a chance. If it
  8878. does handle the file, it must return the search string to be used
  8879. when following the link. The search string will be part of the
  8880. file link, given after a double colon, and `org-open-at-point'
  8881. will automatically search for it. If special measures must be
  8882. taken to make the search successful, another function should be
  8883. added to the companion hook `org-execute-file-search-functions',
  8884. which see.
  8885. A function in this hook may also use `setq' to set the variable
  8886. `description' to provide a suggestion for the descriptive text to
  8887. be used for this link when it gets inserted into an Org-mode
  8888. buffer with \\[org-insert-link].")
  8889. (defvar org-execute-file-search-functions nil
  8890. "List of functions to execute a file search triggered by a link.
  8891. Functions added to this hook must accept a single argument, the
  8892. search string that was part of the file link, the part after the
  8893. double colon. The function must first check if it would like to
  8894. handle this search, for example by checking the `major-mode' or
  8895. the file extension. If it decides not to handle this search, it
  8896. should just return nil to give other functions a chance. If it
  8897. does handle the search, it must return a non-nil value to keep
  8898. other functions from trying.
  8899. Each function can access the current prefix argument through the
  8900. variable `current-prefix-argument'. Note that a single prefix is
  8901. used to force opening a link in Emacs, so it may be good to only
  8902. use a numeric or double prefix to guide the search function.
  8903. In case this is needed, a function in this hook can also restore
  8904. the window configuration before `org-open-at-point' was called using:
  8905. (set-window-configuration org-window-config-before-follow-link)")
  8906. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8907. (defun org-link-search (s &optional type avoid-pos stealth)
  8908. "Search for a link search option.
  8909. If S is surrounded by forward slashes, it is interpreted as a
  8910. regular expression. In org-mode files, this will create an `org-occur'
  8911. sparse tree. In ordinary files, `occur' will be used to list matches.
  8912. If the current buffer is in `dired-mode', grep will be used to search
  8913. in all files. If AVOID-POS is given, ignore matches near that position.
  8914. When optional argument STEALTH is non-nil, do not modify
  8915. visibility around point, thus ignoring
  8916. `org-show-hierarchy-above', `org-show-following-heading' and
  8917. `org-show-siblings' variables."
  8918. (let ((case-fold-search t)
  8919. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8920. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8921. (append '(("") (" ") ("\t") ("\n"))
  8922. org-emphasis-alist)
  8923. "\\|") "\\)"))
  8924. (pos (point))
  8925. (pre nil) (post nil)
  8926. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8927. (cond
  8928. ;; First check if there are any special search functions
  8929. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8930. ;; Now try the builtin stuff
  8931. ((and (equal (string-to-char s0) ?#)
  8932. (> (length s0) 1)
  8933. (save-excursion
  8934. (goto-char (point-min))
  8935. (and
  8936. (re-search-forward
  8937. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8938. (setq type 'dedicated
  8939. pos (match-beginning 0))))
  8940. ;; There is an exact target for this
  8941. (goto-char pos)
  8942. (org-back-to-heading t)))
  8943. ((save-excursion
  8944. (goto-char (point-min))
  8945. (and
  8946. (re-search-forward
  8947. (concat "<<" (regexp-quote s0) ">>") nil t)
  8948. (setq type 'dedicated
  8949. pos (match-beginning 0))))
  8950. ;; There is an exact target for this
  8951. (goto-char pos))
  8952. ((save-excursion
  8953. (goto-char (point-min))
  8954. (and
  8955. (re-search-forward
  8956. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8957. (setq type 'dedicated pos (match-beginning 0))))
  8958. ;; Found an invisible target.
  8959. (goto-char pos))
  8960. ((save-excursion
  8961. (goto-char (point-min))
  8962. (and
  8963. (re-search-forward
  8964. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8965. (setq type 'dedicated pos (match-beginning 0))))
  8966. ;; Found an element with a matching #+name affiliated keyword.
  8967. (goto-char pos))
  8968. ((and (string-match "^(\\(.*\\))$" s0)
  8969. (save-excursion
  8970. (goto-char (point-min))
  8971. (and
  8972. (re-search-forward
  8973. (concat "[^[]" (regexp-quote
  8974. (format org-coderef-label-format
  8975. (match-string 1 s0))))
  8976. nil t)
  8977. (setq type 'dedicated
  8978. pos (1+ (match-beginning 0))))))
  8979. ;; There is a coderef target for this
  8980. (goto-char pos))
  8981. ((string-match "^/\\(.*\\)/$" s)
  8982. ;; A regular expression
  8983. (cond
  8984. ((derived-mode-p 'org-mode)
  8985. (org-occur (match-string 1 s)))
  8986. ;;((eq major-mode 'dired-mode)
  8987. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8988. (t (org-do-occur (match-string 1 s)))))
  8989. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  8990. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8991. (goto-char (point-min))
  8992. (cond
  8993. ((let (case-fold-search)
  8994. (re-search-forward (format org-complex-heading-regexp-format
  8995. (regexp-quote s))
  8996. nil t))
  8997. ;; OK, found a match
  8998. (setq type 'dedicated)
  8999. (goto-char (match-beginning 0)))
  9000. ((and (not org-link-search-inhibit-query)
  9001. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9002. (y-or-n-p "No match - create this as a new heading? "))
  9003. (goto-char (point-max))
  9004. (or (bolp) (newline))
  9005. (insert "* " s "\n")
  9006. (beginning-of-line 0))
  9007. (t
  9008. (goto-char pos)
  9009. (error "No match"))))
  9010. (t
  9011. ;; A normal search string
  9012. (when (equal (string-to-char s) ?*)
  9013. ;; Anchor on headlines, post may include tags.
  9014. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9015. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9016. s (substring s 1)))
  9017. (remove-text-properties
  9018. 0 (length s)
  9019. '(face nil mouse-face nil keymap nil fontified nil) s)
  9020. ;; Make a series of regular expressions to find a match
  9021. (setq words (org-split-string s "[ \n\r\t]+")
  9022. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9023. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9024. "\\)" markers)
  9025. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9026. re2a (concat "[ \t\r\n]" re2a_)
  9027. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9028. re4 (concat "[^a-zA-Z_]" re4_)
  9029. re1 (concat pre re2 post)
  9030. re3 (concat pre (if pre re4_ re4) post)
  9031. re5 (concat pre ".*" re4)
  9032. re2 (concat pre re2)
  9033. re2a (concat pre (if pre re2a_ re2a))
  9034. re4 (concat pre (if pre re4_ re4))
  9035. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9036. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9037. re5 "\\)"
  9038. ))
  9039. (cond
  9040. ((eq type 'org-occur) (org-occur reall))
  9041. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9042. (t (goto-char (point-min))
  9043. (setq type 'fuzzy)
  9044. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9045. (org-search-not-self 1 re1 nil t)
  9046. (org-search-not-self 1 re2 nil t)
  9047. (org-search-not-self 1 re2a nil t)
  9048. (org-search-not-self 1 re3 nil t)
  9049. (org-search-not-self 1 re4 nil t)
  9050. (org-search-not-self 1 re5 nil t)
  9051. )
  9052. (goto-char (match-beginning 1))
  9053. (goto-char pos)
  9054. (error "No match"))))))
  9055. (and (derived-mode-p 'org-mode)
  9056. (not stealth)
  9057. (org-show-context 'link-search))
  9058. type))
  9059. (defun org-search-not-self (group &rest args)
  9060. "Execute `re-search-forward', but only accept matches that do not
  9061. enclose the position of `org-open-link-marker'."
  9062. (let ((m org-open-link-marker))
  9063. (catch 'exit
  9064. (while (apply 're-search-forward args)
  9065. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9066. (goto-char (match-end group))
  9067. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9068. (> (match-beginning 0) (marker-position m))
  9069. (< (match-end 0) (marker-position m)))
  9070. (save-match-data
  9071. (or (not (org-in-regexp
  9072. org-bracket-link-analytic-regexp 1))
  9073. (not (match-end 4)) ; no description
  9074. (and (<= (match-beginning 4) (point))
  9075. (>= (match-end 4) (point))))))
  9076. (throw 'exit (point))))))))
  9077. (defun org-get-buffer-for-internal-link (buffer)
  9078. "Return a buffer to be used for displaying the link target of internal links."
  9079. (cond
  9080. ((not org-display-internal-link-with-indirect-buffer)
  9081. buffer)
  9082. ((string-match "(Clone)$" (buffer-name buffer))
  9083. (message "Buffer is already a clone, not making another one")
  9084. ;; we also do not modify visibility in this case
  9085. buffer)
  9086. (t ; make a new indirect buffer for displaying the link
  9087. (let* ((bn (buffer-name buffer))
  9088. (ibn (concat bn "(Clone)"))
  9089. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9090. (with-current-buffer ib (org-overview))
  9091. ib))))
  9092. (defun org-do-occur (regexp &optional cleanup)
  9093. "Call the Emacs command `occur'.
  9094. If CLEANUP is non-nil, remove the printout of the regular expression
  9095. in the *Occur* buffer. This is useful if the regex is long and not useful
  9096. to read."
  9097. (occur regexp)
  9098. (when cleanup
  9099. (let ((cwin (selected-window)) win beg end)
  9100. (when (setq win (get-buffer-window "*Occur*"))
  9101. (select-window win))
  9102. (goto-char (point-min))
  9103. (when (re-search-forward "match[a-z]+" nil t)
  9104. (setq beg (match-end 0))
  9105. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9106. (setq end (1- (match-beginning 0)))))
  9107. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9108. (goto-char (point-min))
  9109. (select-window cwin))))
  9110. ;;; The mark ring for links jumps
  9111. (defvar org-mark-ring nil
  9112. "Mark ring for positions before jumps in Org-mode.")
  9113. (defvar org-mark-ring-last-goto nil
  9114. "Last position in the mark ring used to go back.")
  9115. ;; Fill and close the ring
  9116. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9117. (loop for i from 1 to org-mark-ring-length do
  9118. (push (make-marker) org-mark-ring))
  9119. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9120. org-mark-ring)
  9121. (defun org-mark-ring-push (&optional pos buffer)
  9122. "Put the current position or POS into the mark ring and rotate it."
  9123. (interactive)
  9124. (setq pos (or pos (point)))
  9125. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9126. (move-marker (car org-mark-ring)
  9127. (or pos (point))
  9128. (or buffer (current-buffer)))
  9129. (message "%s"
  9130. (substitute-command-keys
  9131. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9132. (defun org-mark-ring-goto (&optional n)
  9133. "Jump to the previous position in the mark ring.
  9134. With prefix arg N, jump back that many stored positions. When
  9135. called several times in succession, walk through the entire ring.
  9136. Org-mode commands jumping to a different position in the current file,
  9137. or to another Org-mode file, automatically push the old position
  9138. onto the ring."
  9139. (interactive "p")
  9140. (let (p m)
  9141. (if (eq last-command this-command)
  9142. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9143. (setq p org-mark-ring))
  9144. (setq org-mark-ring-last-goto p)
  9145. (setq m (car p))
  9146. (org-pop-to-buffer-same-window (marker-buffer m))
  9147. (goto-char m)
  9148. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9149. (defun org-remove-angle-brackets (s)
  9150. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9151. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9152. s)
  9153. (defun org-add-angle-brackets (s)
  9154. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9155. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9156. s)
  9157. (defun org-remove-double-quotes (s)
  9158. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9159. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9160. s)
  9161. ;;; Following specific links
  9162. (defun org-follow-timestamp-link ()
  9163. (cond
  9164. ((org-at-date-range-p t)
  9165. (let ((org-agenda-start-on-weekday)
  9166. (t1 (match-string 1))
  9167. (t2 (match-string 2)))
  9168. (setq t1 (time-to-days (org-time-string-to-time t1))
  9169. t2 (time-to-days (org-time-string-to-time t2)))
  9170. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9171. ((org-at-timestamp-p t)
  9172. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9173. (substring (match-string 1) 0 10)))
  9174. 1))
  9175. (t (error "This should not happen"))))
  9176. ;;; Following file links
  9177. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9178. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9179. (declare-function mailcap-mime-info
  9180. "mailcap" (string &optional request no-decode))
  9181. (defvar org-wait nil)
  9182. (defun org-open-file (path &optional in-emacs line search)
  9183. "Open the file at PATH.
  9184. First, this expands any special file name abbreviations. Then the
  9185. configuration variable `org-file-apps' is checked if it contains an
  9186. entry for this file type, and if yes, the corresponding command is launched.
  9187. If no application is found, Emacs simply visits the file.
  9188. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9189. With a double \\[universal-argument] \\[universal-argument] \
  9190. prefix arg, Org tries to avoid opening in Emacs
  9191. and to use an external application to visit the file.
  9192. Optional LINE specifies a line to go to, optional SEARCH a string
  9193. to search for. If LINE or SEARCH is given, the file will be
  9194. opened in Emacs, unless an entry from org-file-apps that makes
  9195. use of groups in a regexp matches.
  9196. If you want to change the way frames are used when following a
  9197. link, please customize `org-link-frame-setup'.
  9198. If the file does not exist, an error is thrown."
  9199. (let* ((file (if (equal path "")
  9200. buffer-file-name
  9201. (substitute-in-file-name (expand-file-name path))))
  9202. (file-apps (append org-file-apps (org-default-apps)))
  9203. (apps (org-remove-if
  9204. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9205. (apps-dlink (org-remove-if-not
  9206. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9207. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9208. (dirp (if remp nil (file-directory-p file)))
  9209. (file (if (and dirp org-open-directory-means-index-dot-org)
  9210. (concat (file-name-as-directory file) "index.org")
  9211. file))
  9212. (a-m-a-p (assq 'auto-mode apps))
  9213. (dfile (downcase file))
  9214. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9215. (link (cond ((and (eq line nil)
  9216. (eq search nil))
  9217. file)
  9218. (line
  9219. (concat file "::" (number-to-string line)))
  9220. (search
  9221. (concat file "::" search))))
  9222. (dlink (downcase link))
  9223. (old-buffer (current-buffer))
  9224. (old-pos (point))
  9225. (old-mode major-mode)
  9226. ext cmd link-match-data)
  9227. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9228. (setq ext (match-string 1 dfile))
  9229. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9230. (setq ext (match-string 1 dfile))))
  9231. (cond
  9232. ((member in-emacs '((16) system))
  9233. (setq cmd (cdr (assoc 'system apps))))
  9234. (in-emacs (setq cmd 'emacs))
  9235. (t
  9236. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9237. (and dirp (cdr (assoc 'directory apps)))
  9238. ; first, try matching against apps-dlink
  9239. ; if we get a match here, store the match data for later
  9240. (let ((match (assoc-default dlink apps-dlink
  9241. 'string-match)))
  9242. (if match
  9243. (progn (setq link-match-data (match-data))
  9244. match)
  9245. (progn (setq in-emacs (or in-emacs line search))
  9246. nil))) ; if we have no match in apps-dlink,
  9247. ; always open the file in emacs if line or search
  9248. ; is given (for backwards compatibility)
  9249. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9250. 'string-match)
  9251. (cdr (assoc ext apps))
  9252. (cdr (assoc t apps))))))
  9253. (when (eq cmd 'system)
  9254. (setq cmd (cdr (assoc 'system apps))))
  9255. (when (eq cmd 'default)
  9256. (setq cmd (cdr (assoc t apps))))
  9257. (when (eq cmd 'mailcap)
  9258. (require 'mailcap)
  9259. (mailcap-parse-mailcaps)
  9260. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9261. (command (mailcap-mime-info mime-type)))
  9262. (if (stringp command)
  9263. (setq cmd command)
  9264. (setq cmd 'emacs))))
  9265. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9266. (not (file-exists-p file))
  9267. (not org-open-non-existing-files))
  9268. (error "No such file: %s" file))
  9269. (cond
  9270. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9271. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9272. (while (string-match "['\"]%s['\"]" cmd)
  9273. (setq cmd (replace-match "%s" t t cmd)))
  9274. (while (string-match "%s" cmd)
  9275. (setq cmd (replace-match
  9276. (save-match-data
  9277. (shell-quote-argument
  9278. (convert-standard-filename file)))
  9279. t t cmd)))
  9280. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9281. (save-match-data
  9282. (let ((match-index 1)
  9283. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9284. (set-match-data link-match-data)
  9285. (while (<= match-index number-of-groups)
  9286. (let ((regex (concat "%" (number-to-string match-index)))
  9287. (replace-with (match-string match-index dlink)))
  9288. (while (string-match regex cmd)
  9289. (setq cmd (replace-match replace-with t t cmd))))
  9290. (setq match-index (+ match-index 1)))))
  9291. (save-window-excursion
  9292. (start-process-shell-command cmd nil cmd)
  9293. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9294. ))
  9295. ((or (stringp cmd)
  9296. (eq cmd 'emacs))
  9297. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9298. (widen)
  9299. (if line (org-goto-line line)
  9300. (if search (org-link-search search))))
  9301. ((consp cmd)
  9302. (let ((file (convert-standard-filename file)))
  9303. (save-match-data
  9304. (set-match-data link-match-data)
  9305. (eval cmd))))
  9306. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9307. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9308. (or (not (equal old-buffer (current-buffer)))
  9309. (not (equal old-pos (point))))
  9310. (org-mark-ring-push old-pos old-buffer))))
  9311. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9312. "This function returns non-nil if `entry' uses a regular
  9313. expression which should be matched against the whole link by
  9314. org-open-file.
  9315. It assumes that is the case when the entry uses a regular
  9316. expression which has at least one grouping construct and the
  9317. action is either a lisp form or a command string containing
  9318. '%1', i.e. using at least one subexpression match as a
  9319. parameter."
  9320. (let ((selector (car entry))
  9321. (action (cdr entry)))
  9322. (if (stringp selector)
  9323. (and (> (regexp-opt-depth selector) 0)
  9324. (or (and (stringp action)
  9325. (string-match "%[0-9]" action))
  9326. (consp action)))
  9327. nil)))
  9328. (defun org-default-apps ()
  9329. "Return the default applications for this operating system."
  9330. (cond
  9331. ((eq system-type 'darwin)
  9332. org-file-apps-defaults-macosx)
  9333. ((eq system-type 'windows-nt)
  9334. org-file-apps-defaults-windowsnt)
  9335. (t org-file-apps-defaults-gnu)))
  9336. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9337. "Convert extensions to regular expressions in the cars of LIST.
  9338. Also, weed out any non-string entries, because the return value is used
  9339. only for regexp matching.
  9340. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9341. point to the symbol `emacs', indicating that the file should
  9342. be opened in Emacs."
  9343. (append
  9344. (delq nil
  9345. (mapcar (lambda (x)
  9346. (if (not (stringp (car x)))
  9347. nil
  9348. (if (string-match "\\W" (car x))
  9349. x
  9350. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9351. list))
  9352. (if add-auto-mode
  9353. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9354. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9355. (defun org-file-remote-p (file)
  9356. "Test whether FILE specifies a location on a remote system.
  9357. Return non-nil if the location is indeed remote.
  9358. For example, the filename \"/user@host:/foo\" specifies a location
  9359. on the system \"/user@host:\"."
  9360. (cond ((fboundp 'file-remote-p)
  9361. (file-remote-p file))
  9362. ((fboundp 'tramp-handle-file-remote-p)
  9363. (tramp-handle-file-remote-p file))
  9364. ((and (boundp 'ange-ftp-name-format)
  9365. (string-match (car ange-ftp-name-format) file))
  9366. t)
  9367. (t nil)))
  9368. ;;;; Refiling
  9369. (defun org-get-org-file ()
  9370. "Read a filename, with default directory `org-directory'."
  9371. (let ((default (or org-default-notes-file remember-data-file)))
  9372. (read-file-name (format "File name [%s]: " default)
  9373. (file-name-as-directory org-directory)
  9374. default)))
  9375. (defun org-notes-order-reversed-p ()
  9376. "Check if the current file should receive notes in reversed order."
  9377. (cond
  9378. ((not org-reverse-note-order) nil)
  9379. ((eq t org-reverse-note-order) t)
  9380. ((not (listp org-reverse-note-order)) nil)
  9381. (t (catch 'exit
  9382. (let ((all org-reverse-note-order)
  9383. entry)
  9384. (while (setq entry (pop all))
  9385. (if (string-match (car entry) buffer-file-name)
  9386. (throw 'exit (cdr entry))))
  9387. nil)))))
  9388. (defvar org-refile-target-table nil
  9389. "The list of refile targets, created by `org-refile'.")
  9390. (defvar org-agenda-new-buffers nil
  9391. "Buffers created to visit agenda files.")
  9392. (defvar org-refile-cache nil
  9393. "Cache for refile targets.")
  9394. (defvar org-refile-markers nil
  9395. "All the markers used for caching refile locations.")
  9396. (defun org-refile-marker (pos)
  9397. "Get a new refile marker, but only if caching is in use."
  9398. (if (not org-refile-use-cache)
  9399. pos
  9400. (let ((m (make-marker)))
  9401. (move-marker m pos)
  9402. (push m org-refile-markers)
  9403. m)))
  9404. (defun org-refile-cache-clear ()
  9405. "Clear the refile cache and disable all the markers."
  9406. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9407. (setq org-refile-markers nil)
  9408. (setq org-refile-cache nil)
  9409. (message "Refile cache has been cleared"))
  9410. (defun org-refile-cache-check-set (set)
  9411. "Check if all the markers in the cache still have live buffers."
  9412. (let (marker)
  9413. (catch 'exit
  9414. (while (and set (setq marker (nth 3 (pop set))))
  9415. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9416. (when (and marker (null (marker-buffer marker)))
  9417. (message "not found") (sit-for 3)
  9418. (throw 'exit nil)))
  9419. t)))
  9420. (defun org-refile-cache-put (set &rest identifiers)
  9421. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9422. (let* ((key (sha1 (prin1-to-string identifiers)))
  9423. (entry (assoc key org-refile-cache)))
  9424. (if entry
  9425. (setcdr entry set)
  9426. (push (cons key set) org-refile-cache))))
  9427. (defun org-refile-cache-get (&rest identifiers)
  9428. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9429. (cond
  9430. ((not org-refile-cache) nil)
  9431. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9432. (t
  9433. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9434. org-refile-cache))))
  9435. (and set (org-refile-cache-check-set set) set)))))
  9436. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9437. "Produce a table with refile targets."
  9438. (let ((case-fold-search nil)
  9439. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9440. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9441. targets tgs txt re files f desc descre fast-path-p level pos0)
  9442. (message "Getting targets...")
  9443. (with-current-buffer (or default-buffer (current-buffer))
  9444. (while (setq entry (pop entries))
  9445. (setq files (car entry) desc (cdr entry))
  9446. (setq fast-path-p nil)
  9447. (cond
  9448. ((null files) (setq files (list (current-buffer))))
  9449. ((eq files 'org-agenda-files)
  9450. (setq files (org-agenda-files 'unrestricted)))
  9451. ((and (symbolp files) (fboundp files))
  9452. (setq files (funcall files)))
  9453. ((and (symbolp files) (boundp files))
  9454. (setq files (symbol-value files))))
  9455. (if (stringp files) (setq files (list files)))
  9456. (cond
  9457. ((eq (car desc) :tag)
  9458. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9459. ((eq (car desc) :todo)
  9460. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9461. ((eq (car desc) :regexp)
  9462. (setq descre (cdr desc)))
  9463. ((eq (car desc) :level)
  9464. (setq descre (concat "^\\*\\{" (number-to-string
  9465. (if org-odd-levels-only
  9466. (1- (* 2 (cdr desc)))
  9467. (cdr desc)))
  9468. "\\}[ \t]")))
  9469. ((eq (car desc) :maxlevel)
  9470. (setq fast-path-p t)
  9471. (setq descre (concat "^\\*\\{1," (number-to-string
  9472. (if org-odd-levels-only
  9473. (1- (* 2 (cdr desc)))
  9474. (cdr desc)))
  9475. "\\}[ \t]")))
  9476. (t (error "Bad refiling target description %s" desc)))
  9477. (while (setq f (pop files))
  9478. (with-current-buffer
  9479. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9480. (or
  9481. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9482. (progn
  9483. (if (bufferp f) (setq f (buffer-file-name
  9484. (buffer-base-buffer f))))
  9485. (setq f (and f (expand-file-name f)))
  9486. (if (eq org-refile-use-outline-path 'file)
  9487. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9488. (save-excursion
  9489. (save-restriction
  9490. (widen)
  9491. (goto-char (point-min))
  9492. (while (re-search-forward descre nil t)
  9493. (goto-char (setq pos0 (point-at-bol)))
  9494. (catch 'next
  9495. (when org-refile-target-verify-function
  9496. (save-match-data
  9497. (or (funcall org-refile-target-verify-function)
  9498. (throw 'next t))))
  9499. (when (and (looking-at org-complex-heading-regexp)
  9500. (not (member (match-string 4) excluded-entries))
  9501. (match-string 4))
  9502. (setq level (org-reduced-level
  9503. (- (match-end 1) (match-beginning 1)))
  9504. txt (org-link-display-format (match-string 4))
  9505. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9506. re (format org-complex-heading-regexp-format
  9507. (regexp-quote (match-string 4))))
  9508. (when org-refile-use-outline-path
  9509. (setq txt (mapconcat
  9510. 'org-protect-slash
  9511. (append
  9512. (if (eq org-refile-use-outline-path
  9513. 'file)
  9514. (list (file-name-nondirectory
  9515. (buffer-file-name
  9516. (buffer-base-buffer))))
  9517. (if (eq org-refile-use-outline-path
  9518. 'full-file-path)
  9519. (list (buffer-file-name
  9520. (buffer-base-buffer)))))
  9521. (org-get-outline-path fast-path-p
  9522. level txt)
  9523. (list txt))
  9524. "/")))
  9525. (push (list txt f re (org-refile-marker (point)))
  9526. tgs)))
  9527. (when (= (point) pos0)
  9528. ;; verification function has not moved point
  9529. (goto-char (point-at-eol))))))))
  9530. (when org-refile-use-cache
  9531. (org-refile-cache-put tgs (buffer-file-name) descre))
  9532. (setq targets (append tgs targets))
  9533. ))))
  9534. (message "Getting targets...done")
  9535. (nreverse targets)))
  9536. (defun org-protect-slash (s)
  9537. (while (string-match "/" s)
  9538. (setq s (replace-match "\\" t t s)))
  9539. s)
  9540. (defvar org-olpa (make-vector 20 nil))
  9541. (defun org-get-outline-path (&optional fastp level heading)
  9542. "Return the outline path to the current entry, as a list.
  9543. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9544. routine which makes outline path derivations for an entire file,
  9545. avoiding backtracing. Refile target collection makes use of that."
  9546. (if fastp
  9547. (progn
  9548. (if (> level 19)
  9549. (error "Outline path failure, more than 19 levels"))
  9550. (loop for i from level upto 19 do
  9551. (aset org-olpa i nil))
  9552. (prog1
  9553. (delq nil (append org-olpa nil))
  9554. (aset org-olpa level heading)))
  9555. (let (rtn case-fold-search)
  9556. (save-excursion
  9557. (save-restriction
  9558. (widen)
  9559. (while (org-up-heading-safe)
  9560. (when (looking-at org-complex-heading-regexp)
  9561. (push (org-match-string-no-properties 4) rtn)))
  9562. rtn)))))
  9563. (defun org-format-outline-path (path &optional width prefix)
  9564. "Format the outline path PATH for display.
  9565. Width is the maximum number of characters that is available.
  9566. Prefix is a prefix to be included in the returned string,
  9567. such as the file name."
  9568. (setq width (or width 79))
  9569. (if prefix (setq width (- width (length prefix))))
  9570. (if (not path)
  9571. (or prefix "")
  9572. (let* ((nsteps (length path))
  9573. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9574. (maxwidth (if (<= total-width width)
  9575. 10000 ;; everything fits
  9576. ;; we need to shorten the level headings
  9577. (/ (- width nsteps) nsteps)))
  9578. (org-odd-levels-only nil)
  9579. (n 0)
  9580. (total (1+ (length prefix))))
  9581. (setq maxwidth (max maxwidth 10))
  9582. (concat prefix
  9583. (mapconcat
  9584. (lambda (h)
  9585. (setq n (1+ n))
  9586. (if (and (= n nsteps) (< maxwidth 10000))
  9587. (setq maxwidth (- total-width total)))
  9588. (if (< (length h) maxwidth)
  9589. (progn (setq total (+ total (length h) 1)) h)
  9590. (setq h (substring h 0 (- maxwidth 2))
  9591. total (+ total maxwidth 1))
  9592. (if (string-match "[ \t]+\\'" h)
  9593. (setq h (substring h 0 (match-beginning 0))))
  9594. (setq h (concat h "..")))
  9595. (org-add-props h nil 'face
  9596. (nth (% (1- n) org-n-level-faces)
  9597. org-level-faces))
  9598. h)
  9599. path "/")))))
  9600. (defun org-display-outline-path (&optional file current)
  9601. "Display the current outline path in the echo area."
  9602. (interactive "P")
  9603. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9604. (case-fold-search nil)
  9605. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9606. (if current (setq path (append path
  9607. (save-excursion
  9608. (org-back-to-heading t)
  9609. (if (looking-at org-complex-heading-regexp)
  9610. (list (match-string 4)))))))
  9611. (message "%s"
  9612. (org-format-outline-path
  9613. path
  9614. (1- (frame-width))
  9615. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9616. (defvar org-refile-history nil
  9617. "History for refiling operations.")
  9618. (defvar org-after-refile-insert-hook nil
  9619. "Hook run after `org-refile' has inserted its stuff at the new location.
  9620. Note that this is still *before* the stuff will be removed from
  9621. the *old* location.")
  9622. (defvar org-capture-last-stored-marker)
  9623. (defun org-refile (&optional goto default-buffer rfloc)
  9624. "Move the entry or entries at point to another heading.
  9625. The list of target headings is compiled using the information in
  9626. `org-refile-targets', which see.
  9627. At the target location, the entry is filed as a subitem of the target
  9628. heading. Depending on `org-reverse-note-order', the new subitem will
  9629. either be the first or the last subitem.
  9630. If there is an active region, all entries in that region will be moved.
  9631. However, the region must fulfill the requirement that the first heading
  9632. is the first one sets the top-level of the moved text - at most siblings
  9633. below it are allowed.
  9634. With prefix arg GOTO, the command will only visit the target location
  9635. and not actually move anything.
  9636. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9637. go to the location where the last refiling operation has put the subtree.
  9638. With a prefix argument of `2', refile to the running clock.
  9639. RFLOC can be a refile location obtained in a different way.
  9640. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9641. If you are using target caching (see `org-refile-use-cache'),
  9642. you have to clear the target cache in order to find new targets.
  9643. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9644. prefix argument (`C-u C-u C-u C-c C-w')."
  9645. (interactive "P")
  9646. (if (member goto '(0 (64)))
  9647. (org-refile-cache-clear)
  9648. (let* ((cbuf (current-buffer))
  9649. (regionp (org-region-active-p))
  9650. (region-start (and regionp (region-beginning)))
  9651. (region-end (and regionp (region-end)))
  9652. (region-length (and regionp (- region-end region-start)))
  9653. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9654. pos it nbuf file re level reversed)
  9655. (setq last-command nil)
  9656. (when regionp
  9657. (goto-char region-start)
  9658. (or (bolp) (goto-char (point-at-bol)))
  9659. (setq region-start (point))
  9660. (unless (or (org-kill-is-subtree-p
  9661. (buffer-substring region-start region-end))
  9662. (prog1 org-refile-active-region-within-subtree
  9663. (org-toggle-heading)))
  9664. (error "The region is not a (sequence of) subtree(s)")))
  9665. (if (equal goto '(16))
  9666. (org-refile-goto-last-stored)
  9667. (when (or
  9668. (and (equal goto 2)
  9669. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9670. (prog1
  9671. (setq it (list (or org-clock-heading "running clock")
  9672. (buffer-file-name
  9673. (marker-buffer org-clock-hd-marker))
  9674. ""
  9675. (marker-position org-clock-hd-marker)))
  9676. (setq goto nil)))
  9677. (setq it (or rfloc
  9678. (let (heading-text)
  9679. (save-excursion
  9680. (unless goto
  9681. (org-back-to-heading t)
  9682. (setq heading-text
  9683. (nth 4 (org-heading-components))))
  9684. (org-refile-get-location
  9685. (cond (goto "Goto")
  9686. (regionp "Refile region to")
  9687. (t (concat "Refile subtree \""
  9688. heading-text "\" to")))
  9689. default-buffer
  9690. (and (not (equal '(4) goto))
  9691. org-refile-allow-creating-parent-nodes)
  9692. goto))))))
  9693. (setq file (nth 1 it)
  9694. re (nth 2 it)
  9695. pos (nth 3 it))
  9696. (if (and (not goto)
  9697. pos
  9698. (equal (buffer-file-name) file)
  9699. (if regionp
  9700. (and (>= pos region-start)
  9701. (<= pos region-end))
  9702. (and (>= pos (point))
  9703. (< pos (save-excursion
  9704. (org-end-of-subtree t t))))))
  9705. (error "Cannot refile to position inside the tree or region"))
  9706. (setq nbuf (or (find-buffer-visiting file)
  9707. (find-file-noselect file)))
  9708. (if goto
  9709. (progn
  9710. (org-pop-to-buffer-same-window nbuf)
  9711. (goto-char pos)
  9712. (org-show-context 'org-goto))
  9713. (if regionp
  9714. (progn
  9715. (org-kill-new (buffer-substring region-start region-end))
  9716. (org-save-markers-in-region region-start region-end))
  9717. (org-copy-subtree 1 nil t))
  9718. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9719. (find-file-noselect file)))
  9720. (setq reversed (org-notes-order-reversed-p))
  9721. (save-excursion
  9722. (save-restriction
  9723. (widen)
  9724. (if pos
  9725. (progn
  9726. (goto-char pos)
  9727. (looking-at org-outline-regexp)
  9728. (setq level (org-get-valid-level (funcall outline-level) 1))
  9729. (goto-char
  9730. (if reversed
  9731. (or (outline-next-heading) (point-max))
  9732. (or (save-excursion (org-get-next-sibling))
  9733. (org-end-of-subtree t t)
  9734. (point-max)))))
  9735. (setq level 1)
  9736. (if (not reversed)
  9737. (goto-char (point-max))
  9738. (goto-char (point-min))
  9739. (or (outline-next-heading) (goto-char (point-max)))))
  9740. (if (not (bolp)) (newline))
  9741. (org-paste-subtree level)
  9742. (when org-log-refile
  9743. (org-add-log-setup 'refile nil nil 'findpos
  9744. org-log-refile)
  9745. (unless (eq org-log-refile 'note)
  9746. (save-excursion (org-add-log-note))))
  9747. (and org-auto-align-tags (org-set-tags nil t))
  9748. (bookmark-set "org-refile-last-stored")
  9749. ;; If we are refiling for capture, make sure that the
  9750. ;; last-capture pointers point here
  9751. (when (org-bound-and-true-p org-refile-for-capture)
  9752. (bookmark-set "org-capture-last-stored-marker")
  9753. (move-marker org-capture-last-stored-marker (point)))
  9754. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9755. (run-hooks 'org-after-refile-insert-hook))))
  9756. (if regionp
  9757. (delete-region (point) (+ (point) region-length))
  9758. (org-cut-subtree))
  9759. (when (featurep 'org-inlinetask)
  9760. (org-inlinetask-remove-END-maybe))
  9761. (setq org-markers-to-move nil)
  9762. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9763. (defun org-refile-goto-last-stored ()
  9764. "Go to the location where the last refile was stored."
  9765. (interactive)
  9766. (bookmark-jump "org-refile-last-stored")
  9767. (message "This is the location of the last refile"))
  9768. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9769. no-exclude)
  9770. "Prompt the user for a refile location, using PROMPT.
  9771. PROMPT should not be suffixed with a colon and a space, because
  9772. this function appends the default value from
  9773. `org-refile-history' automatically, if that is not empty.
  9774. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9775. this is used for the GOTO interface."
  9776. (let ((org-refile-targets org-refile-targets)
  9777. (org-refile-use-outline-path org-refile-use-outline-path)
  9778. excluded-entries)
  9779. (when (and (derived-mode-p 'org-mode)
  9780. (not org-refile-use-cache)
  9781. (not no-exclude))
  9782. (org-map-tree
  9783. (lambda()
  9784. (setq excluded-entries
  9785. (append excluded-entries (list (org-get-heading t t)))))))
  9786. (setq org-refile-target-table
  9787. (org-refile-get-targets default-buffer excluded-entries)))
  9788. (unless org-refile-target-table
  9789. (error "No refile targets"))
  9790. (let* ((prompt (concat prompt
  9791. (and (car org-refile-history)
  9792. (concat " (default " (car org-refile-history) ")"))
  9793. ": "))
  9794. (cbuf (current-buffer))
  9795. (partial-completion-mode nil)
  9796. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9797. (cfunc (if (and org-refile-use-outline-path
  9798. org-outline-path-complete-in-steps)
  9799. 'org-olpath-completing-read
  9800. 'org-icompleting-read))
  9801. (extra (if org-refile-use-outline-path "/" ""))
  9802. (filename (and cfn (expand-file-name cfn)))
  9803. (tbl (mapcar
  9804. (lambda (x)
  9805. (if (and (not (member org-refile-use-outline-path
  9806. '(file full-file-path)))
  9807. (not (equal filename (nth 1 x))))
  9808. (cons (concat (car x) extra " ("
  9809. (file-name-nondirectory (nth 1 x)) ")")
  9810. (cdr x))
  9811. (cons (concat (car x) extra) (cdr x))))
  9812. org-refile-target-table))
  9813. (completion-ignore-case t)
  9814. pa answ parent-target child parent old-hist)
  9815. (setq old-hist org-refile-history)
  9816. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9817. nil 'org-refile-history (car org-refile-history)))
  9818. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9819. (org-refile-check-position pa)
  9820. (if pa
  9821. (progn
  9822. (when (or (not org-refile-history)
  9823. (not (eq old-hist org-refile-history))
  9824. (not (equal (car pa) (car org-refile-history))))
  9825. (setq org-refile-history
  9826. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9827. org-refile-history
  9828. (cdr org-refile-history))))
  9829. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9830. (pop org-refile-history)))
  9831. pa)
  9832. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9833. (progn
  9834. (setq parent (match-string 1 answ)
  9835. child (match-string 2 answ))
  9836. (setq parent-target (or (assoc parent tbl)
  9837. (assoc (concat parent "/") tbl)))
  9838. (when (and parent-target
  9839. (or (eq new-nodes t)
  9840. (and (eq new-nodes 'confirm)
  9841. (y-or-n-p (format "Create new node \"%s\"? "
  9842. child)))))
  9843. (org-refile-new-child parent-target child)))
  9844. (error "Invalid target location")))))
  9845. (declare-function org-string-nw-p "org-macs.el" (s))
  9846. (defun org-refile-check-position (refile-pointer)
  9847. "Check if the refile pointer matches the readline to which it points."
  9848. (let* ((file (nth 1 refile-pointer))
  9849. (re (nth 2 refile-pointer))
  9850. (pos (nth 3 refile-pointer))
  9851. buffer)
  9852. (when (org-string-nw-p re)
  9853. (setq buffer (if (markerp pos)
  9854. (marker-buffer pos)
  9855. (or (find-buffer-visiting file)
  9856. (find-file-noselect file))))
  9857. (with-current-buffer buffer
  9858. (save-excursion
  9859. (save-restriction
  9860. (widen)
  9861. (goto-char pos)
  9862. (beginning-of-line 1)
  9863. (unless (org-looking-at-p re)
  9864. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9865. (defun org-refile-new-child (parent-target child)
  9866. "Use refile target PARENT-TARGET to add new CHILD below it."
  9867. (unless parent-target
  9868. (error "Cannot find parent for new node"))
  9869. (let ((file (nth 1 parent-target))
  9870. (pos (nth 3 parent-target))
  9871. level)
  9872. (with-current-buffer (or (find-buffer-visiting file)
  9873. (find-file-noselect file))
  9874. (save-excursion
  9875. (save-restriction
  9876. (widen)
  9877. (if pos
  9878. (goto-char pos)
  9879. (goto-char (point-max))
  9880. (if (not (bolp)) (newline)))
  9881. (when (looking-at org-outline-regexp)
  9882. (setq level (funcall outline-level))
  9883. (org-end-of-subtree t t))
  9884. (org-back-over-empty-lines)
  9885. (insert "\n" (make-string
  9886. (if pos (org-get-valid-level level 1) 1) ?*)
  9887. " " child "\n")
  9888. (beginning-of-line 0)
  9889. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9890. (defun org-olpath-completing-read (prompt collection &rest args)
  9891. "Read an outline path like a file name."
  9892. (let ((thetable collection)
  9893. (org-completion-use-ido nil) ; does not work with ido.
  9894. (org-completion-use-iswitchb nil)) ; or iswitchb
  9895. (apply
  9896. 'org-icompleting-read prompt
  9897. (lambda (string predicate &optional flag)
  9898. (let (rtn r f (l (length string)))
  9899. (cond
  9900. ((eq flag nil)
  9901. ;; try completion
  9902. (try-completion string thetable))
  9903. ((eq flag t)
  9904. ;; all-completions
  9905. (setq rtn (all-completions string thetable predicate))
  9906. (mapcar
  9907. (lambda (x)
  9908. (setq r (substring x l))
  9909. (if (string-match " ([^)]*)$" x)
  9910. (setq f (match-string 0 x))
  9911. (setq f ""))
  9912. (if (string-match "/" r)
  9913. (concat string (substring r 0 (match-end 0)) f)
  9914. x))
  9915. rtn))
  9916. ((eq flag 'lambda)
  9917. ;; exact match?
  9918. (assoc string thetable)))))
  9919. args)))
  9920. ;;;; Dynamic blocks
  9921. (defun org-find-dblock (name)
  9922. "Find the first dynamic block with name NAME in the buffer.
  9923. If not found, stay at current position and return nil."
  9924. (let (pos)
  9925. (save-excursion
  9926. (goto-char (point-min))
  9927. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9928. nil t)
  9929. (match-beginning 0))))
  9930. (if pos (goto-char pos))
  9931. pos))
  9932. (defconst org-dblock-start-re
  9933. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9934. "Matches the start line of a dynamic block, with parameters.")
  9935. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9936. "Matches the end of a dynamic block.")
  9937. (defun org-create-dblock (plist)
  9938. "Create a dynamic block section, with parameters taken from PLIST.
  9939. PLIST must contain a :name entry which is used as name of the block."
  9940. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9941. (end-of-line 1)
  9942. (newline))
  9943. (let ((col (current-column))
  9944. (name (plist-get plist :name)))
  9945. (insert "#+BEGIN: " name)
  9946. (while plist
  9947. (if (eq (car plist) :name)
  9948. (setq plist (cddr plist))
  9949. (insert " " (prin1-to-string (pop plist)))))
  9950. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9951. (beginning-of-line -2)))
  9952. (defun org-prepare-dblock ()
  9953. "Prepare dynamic block for refresh.
  9954. This empties the block, puts the cursor at the insert position and returns
  9955. the property list including an extra property :name with the block name."
  9956. (unless (looking-at org-dblock-start-re)
  9957. (error "Not at a dynamic block"))
  9958. (let* ((begdel (1+ (match-end 0)))
  9959. (name (org-no-properties (match-string 1)))
  9960. (params (append (list :name name)
  9961. (read (concat "(" (match-string 3) ")")))))
  9962. (save-excursion
  9963. (beginning-of-line 1)
  9964. (skip-chars-forward " \t")
  9965. (setq params (plist-put params :indentation-column (current-column))))
  9966. (unless (re-search-forward org-dblock-end-re nil t)
  9967. (error "Dynamic block not terminated"))
  9968. (setq params
  9969. (append params
  9970. (list :content (buffer-substring
  9971. begdel (match-beginning 0)))))
  9972. (delete-region begdel (match-beginning 0))
  9973. (goto-char begdel)
  9974. (open-line 1)
  9975. params))
  9976. (defun org-map-dblocks (&optional command)
  9977. "Apply COMMAND to all dynamic blocks in the current buffer.
  9978. If COMMAND is not given, use `org-update-dblock'."
  9979. (let ((cmd (or command 'org-update-dblock)))
  9980. (save-excursion
  9981. (goto-char (point-min))
  9982. (while (re-search-forward org-dblock-start-re nil t)
  9983. (goto-char (match-beginning 0))
  9984. (save-excursion
  9985. (condition-case nil
  9986. (funcall cmd)
  9987. (error (message "Error during update of dynamic block"))))
  9988. (unless (re-search-forward org-dblock-end-re nil t)
  9989. (error "Dynamic block not terminated"))))))
  9990. (defun org-dblock-update (&optional arg)
  9991. "User command for updating dynamic blocks.
  9992. Update the dynamic block at point. With prefix ARG, update all dynamic
  9993. blocks in the buffer."
  9994. (interactive "P")
  9995. (if arg
  9996. (org-update-all-dblocks)
  9997. (or (looking-at org-dblock-start-re)
  9998. (org-beginning-of-dblock))
  9999. (org-update-dblock)))
  10000. (defun org-update-dblock ()
  10001. "Update the dynamic block at point.
  10002. This means to empty the block, parse for parameters and then call
  10003. the correct writing function."
  10004. (interactive)
  10005. (save-window-excursion
  10006. (let* ((pos (point))
  10007. (line (org-current-line))
  10008. (params (org-prepare-dblock))
  10009. (name (plist-get params :name))
  10010. (indent (plist-get params :indentation-column))
  10011. (cmd (intern (concat "org-dblock-write:" name))))
  10012. (message "Updating dynamic block `%s' at line %d..." name line)
  10013. (funcall cmd params)
  10014. (message "Updating dynamic block `%s' at line %d...done" name line)
  10015. (goto-char pos)
  10016. (when (and indent (> indent 0))
  10017. (setq indent (make-string indent ?\ ))
  10018. (save-excursion
  10019. (org-beginning-of-dblock)
  10020. (forward-line 1)
  10021. (while (not (looking-at org-dblock-end-re))
  10022. (insert indent)
  10023. (beginning-of-line 2))
  10024. (when (looking-at org-dblock-end-re)
  10025. (and (looking-at "[ \t]+")
  10026. (replace-match ""))
  10027. (insert indent)))))))
  10028. (defun org-beginning-of-dblock ()
  10029. "Find the beginning of the dynamic block at point.
  10030. Error if there is no such block at point."
  10031. (let ((pos (point))
  10032. beg)
  10033. (end-of-line 1)
  10034. (if (and (re-search-backward org-dblock-start-re nil t)
  10035. (setq beg (match-beginning 0))
  10036. (re-search-forward org-dblock-end-re nil t)
  10037. (> (match-end 0) pos))
  10038. (goto-char beg)
  10039. (goto-char pos)
  10040. (error "Not in a dynamic block"))))
  10041. ;;;###autoload
  10042. (defun org-update-all-dblocks ()
  10043. "Update all dynamic blocks in the buffer.
  10044. This function can be used in a hook."
  10045. (interactive)
  10046. (when (derived-mode-p 'org-mode)
  10047. (org-map-dblocks 'org-update-dblock)))
  10048. ;;;; Completion
  10049. (defconst org-additional-option-like-keywords
  10050. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10051. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10052. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10053. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10054. "BEGIN:" "END:"
  10055. "ORGTBL" "TBLFM:" "TBLNAME:"
  10056. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10057. "BEGIN_QUOTE" "END_QUOTE"
  10058. "BEGIN_VERSE" "END_VERSE"
  10059. "BEGIN_CENTER" "END_CENTER"
  10060. "BEGIN_SRC" "END_SRC"
  10061. "BEGIN_RESULT" "END_RESULT"
  10062. "NAME:" "RESULTS:"
  10063. "HEADER:" "HEADERS:"
  10064. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10065. "CAPTION:" "LABEL:"
  10066. "SETUPFILE:"
  10067. "INCLUDE:"
  10068. "BIND:"
  10069. "MACRO:"))
  10070. (defcustom org-structure-template-alist
  10071. '(
  10072. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10073. "<src lang=\"?\">\n\n</src>")
  10074. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10075. "<example>\n?\n</example>")
  10076. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10077. "<quote>\n?\n</quote>")
  10078. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10079. "<verse>\n?\n</verse>")
  10080. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10081. "<center>\n?\n</center>")
  10082. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10083. "<literal style=\"latex\">\n?\n</literal>")
  10084. ("L" "#+LaTeX: "
  10085. "<literal style=\"latex\">?</literal>")
  10086. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10087. "<literal style=\"html\">\n?\n</literal>")
  10088. ("H" "#+HTML: "
  10089. "<literal style=\"html\">?</literal>")
  10090. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10091. ("A" "#+ASCII: ")
  10092. ("i" "#+INDEX: ?"
  10093. "#+INDEX: ?")
  10094. ("I" "#+INCLUDE %file ?"
  10095. "<include file=%file markup=\"?\">")
  10096. )
  10097. "Structure completion elements.
  10098. This is a list of abbreviation keys and values. The value gets inserted
  10099. if you type `<' followed by the key and then press the completion key,
  10100. usually `M-TAB'. %file will be replaced by a file name after prompting
  10101. for the file using completion. The cursor will be placed at the position
  10102. of the `?` in the template.
  10103. There are two templates for each key, the first uses the original Org syntax,
  10104. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10105. the default when the /org-mtags.el/ module has been loaded. See also the
  10106. variable `org-mtags-prefer-muse-templates'."
  10107. :group 'org-completion
  10108. :type '(repeat
  10109. (string :tag "Key")
  10110. (string :tag "Template")
  10111. (string :tag "Muse Template")))
  10112. (defun org-try-structure-completion ()
  10113. "Try to complete a structure template before point.
  10114. This looks for strings like \"<e\" on an otherwise empty line and
  10115. expands them."
  10116. (let ((l (buffer-substring (point-at-bol) (point)))
  10117. a)
  10118. (when (and (looking-at "[ \t]*$")
  10119. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10120. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10121. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10122. (match-beginning 1)) a)
  10123. t)))
  10124. (defun org-complete-expand-structure-template (start cell)
  10125. "Expand a structure template."
  10126. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10127. (rpl (nth (if musep 2 1) cell))
  10128. (ind ""))
  10129. (delete-region start (point))
  10130. (when (string-match "\\`#\\+" rpl)
  10131. (cond
  10132. ((bolp))
  10133. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10134. (setq ind (buffer-substring (point-at-bol) (point))))
  10135. (t (newline))))
  10136. (setq start (point))
  10137. (if (string-match "%file" rpl)
  10138. (setq rpl (replace-match
  10139. (concat
  10140. "\""
  10141. (save-match-data
  10142. (abbreviate-file-name (read-file-name "Include file: ")))
  10143. "\"")
  10144. t t rpl)))
  10145. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10146. (concat "\n" ind)))
  10147. (insert rpl)
  10148. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10149. ;;;; TODO, DEADLINE, Comments
  10150. (defun org-toggle-comment ()
  10151. "Change the COMMENT state of an entry."
  10152. (interactive)
  10153. (save-excursion
  10154. (org-back-to-heading)
  10155. (let (case-fold-search)
  10156. (cond
  10157. ((looking-at (format org-heading-keyword-regexp-format
  10158. org-comment-string))
  10159. (goto-char (match-end 1))
  10160. (looking-at (concat " +" org-comment-string))
  10161. (replace-match "" t t)
  10162. (when (eolp) (insert " ")))
  10163. ((looking-at org-outline-regexp)
  10164. (goto-char (match-end 0))
  10165. (insert org-comment-string " "))))))
  10166. (defvar org-last-todo-state-is-todo nil
  10167. "This is non-nil when the last TODO state change led to a TODO state.
  10168. If the last change removed the TODO tag or switched to DONE, then
  10169. this is nil.")
  10170. (defvar org-setting-tags nil) ; dynamically skipped
  10171. (defvar org-todo-setup-filter-hook nil
  10172. "Hook for functions that pre-filter todo specs.
  10173. Each function takes a todo spec and returns either nil or the spec
  10174. transformed into canonical form." )
  10175. (defvar org-todo-get-default-hook nil
  10176. "Hook for functions that get a default item for todo.
  10177. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10178. nil or a string to be used for the todo mark." )
  10179. (defvar org-agenda-headline-snapshot-before-repeat)
  10180. (defun org-current-effective-time ()
  10181. "Return current time adjusted for `org-extend-today-until' variable"
  10182. (let* ((ct (org-current-time))
  10183. (dct (decode-time ct))
  10184. (ct1
  10185. (if (and org-use-effective-time
  10186. (< (nth 2 dct) org-extend-today-until))
  10187. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10188. ct)))
  10189. ct1))
  10190. (defun org-todo-yesterday (&optional arg)
  10191. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10192. (interactive "P")
  10193. (if (eq major-mode 'org-agenda-mode)
  10194. (apply 'org-agenda-todo-yesterday arg)
  10195. (let* ((hour (third (decode-time
  10196. (org-current-time))))
  10197. (org-extend-today-until (1+ hour)))
  10198. (org-todo arg))))
  10199. (defun org-todo (&optional arg)
  10200. "Change the TODO state of an item.
  10201. The state of an item is given by a keyword at the start of the heading,
  10202. like
  10203. *** TODO Write paper
  10204. *** DONE Call mom
  10205. The different keywords are specified in the variable `org-todo-keywords'.
  10206. By default the available states are \"TODO\" and \"DONE\".
  10207. So for this example: when the item starts with TODO, it is changed to DONE.
  10208. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10209. DONE are present, add TODO at the beginning of the heading.
  10210. With \\[universal-argument] prefix arg, use completion to determine the new \
  10211. state.
  10212. With numeric prefix arg, switch to that state.
  10213. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10214. keywords (nextset).
  10215. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10216. With a numeric prefix arg of 0, inhibit note taking for the change.
  10217. For calling through lisp, arg is also interpreted in the following way:
  10218. 'none -> empty state
  10219. \"\"(empty string) -> switch to empty state
  10220. 'done -> switch to DONE
  10221. 'nextset -> switch to the next set of keywords
  10222. 'previousset -> switch to the previous set of keywords
  10223. \"WAITING\" -> switch to the specified keyword, but only if it
  10224. really is a member of `org-todo-keywords'."
  10225. (interactive "P")
  10226. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10227. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10228. 'region-start-level 'region))
  10229. org-loop-over-headlines-in-active-region)
  10230. (org-map-entries
  10231. `(org-todo ,arg)
  10232. org-loop-over-headlines-in-active-region
  10233. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10234. (if (equal arg '(16)) (setq arg 'nextset))
  10235. (let ((org-blocker-hook org-blocker-hook)
  10236. (case-fold-search nil))
  10237. (when (equal arg '(64))
  10238. (setq arg nil org-blocker-hook nil))
  10239. (when (and org-blocker-hook
  10240. (or org-inhibit-blocking
  10241. (org-entry-get nil "NOBLOCKING")))
  10242. (setq org-blocker-hook nil))
  10243. (save-excursion
  10244. (catch 'exit
  10245. (org-back-to-heading t)
  10246. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10247. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10248. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10249. (let* ((match-data (match-data))
  10250. (startpos (point-at-bol))
  10251. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10252. (org-log-done org-log-done)
  10253. (org-log-repeat org-log-repeat)
  10254. (org-todo-log-states org-todo-log-states)
  10255. (org-inhibit-logging
  10256. (if (equal arg 0)
  10257. (progn (setq arg nil) 'note) org-inhibit-logging))
  10258. (this (match-string 1))
  10259. (hl-pos (match-beginning 0))
  10260. (head (org-get-todo-sequence-head this))
  10261. (ass (assoc head org-todo-kwd-alist))
  10262. (interpret (nth 1 ass))
  10263. (done-word (nth 3 ass))
  10264. (final-done-word (nth 4 ass))
  10265. (org-last-state (or this ""))
  10266. (completion-ignore-case t)
  10267. (member (member this org-todo-keywords-1))
  10268. (tail (cdr member))
  10269. (org-state (cond
  10270. ((and org-todo-key-trigger
  10271. (or (and (equal arg '(4))
  10272. (eq org-use-fast-todo-selection 'prefix))
  10273. (and (not arg) org-use-fast-todo-selection
  10274. (not (eq org-use-fast-todo-selection
  10275. 'prefix)))))
  10276. ;; Use fast selection
  10277. (org-fast-todo-selection))
  10278. ((and (equal arg '(4))
  10279. (or (not org-use-fast-todo-selection)
  10280. (not org-todo-key-trigger)))
  10281. ;; Read a state with completion
  10282. (org-icompleting-read
  10283. "State: " (mapcar (lambda(x) (list x))
  10284. org-todo-keywords-1)
  10285. nil t))
  10286. ((eq arg 'right)
  10287. (if this
  10288. (if tail (car tail) nil)
  10289. (car org-todo-keywords-1)))
  10290. ((eq arg 'left)
  10291. (if (equal member org-todo-keywords-1)
  10292. nil
  10293. (if this
  10294. (nth (- (length org-todo-keywords-1)
  10295. (length tail) 2)
  10296. org-todo-keywords-1)
  10297. (org-last org-todo-keywords-1))))
  10298. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10299. (setq arg nil))) ; hack to fall back to cycling
  10300. (arg
  10301. ;; user or caller requests a specific state
  10302. (cond
  10303. ((equal arg "") nil)
  10304. ((eq arg 'none) nil)
  10305. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10306. ((eq arg 'nextset)
  10307. (or (car (cdr (member head org-todo-heads)))
  10308. (car org-todo-heads)))
  10309. ((eq arg 'previousset)
  10310. (let ((org-todo-heads (reverse org-todo-heads)))
  10311. (or (car (cdr (member head org-todo-heads)))
  10312. (car org-todo-heads))))
  10313. ((car (member arg org-todo-keywords-1)))
  10314. ((stringp arg)
  10315. (error "State `%s' not valid in this file" arg))
  10316. ((nth (1- (prefix-numeric-value arg))
  10317. org-todo-keywords-1))))
  10318. ((null member) (or head (car org-todo-keywords-1)))
  10319. ((equal this final-done-word) nil) ;; -> make empty
  10320. ((null tail) nil) ;; -> first entry
  10321. ((memq interpret '(type priority))
  10322. (if (eq this-command last-command)
  10323. (car tail)
  10324. (if (> (length tail) 0)
  10325. (or done-word (car org-done-keywords))
  10326. nil)))
  10327. (t
  10328. (car tail))))
  10329. (org-state (or
  10330. (run-hook-with-args-until-success
  10331. 'org-todo-get-default-hook org-state org-last-state)
  10332. org-state))
  10333. (next (if org-state (concat " " org-state " ") " "))
  10334. (change-plist (list :type 'todo-state-change :from this :to org-state
  10335. :position startpos))
  10336. dolog now-done-p)
  10337. (when org-blocker-hook
  10338. (setq org-last-todo-state-is-todo
  10339. (not (member this org-done-keywords)))
  10340. (unless (save-excursion
  10341. (save-match-data
  10342. (org-with-wide-buffer
  10343. (run-hook-with-args-until-failure
  10344. 'org-blocker-hook change-plist))))
  10345. (if (org-called-interactively-p 'interactive)
  10346. (error "TODO state change from %s to %s blocked" this org-state)
  10347. ;; fail silently
  10348. (message "TODO state change from %s to %s blocked" this org-state)
  10349. (throw 'exit nil))))
  10350. (store-match-data match-data)
  10351. (replace-match next t t)
  10352. (unless (pos-visible-in-window-p hl-pos)
  10353. (message "TODO state changed to %s" (org-trim next)))
  10354. (unless head
  10355. (setq head (org-get-todo-sequence-head org-state)
  10356. ass (assoc head org-todo-kwd-alist)
  10357. interpret (nth 1 ass)
  10358. done-word (nth 3 ass)
  10359. final-done-word (nth 4 ass)))
  10360. (when (memq arg '(nextset previousset))
  10361. (message "Keyword-Set %d/%d: %s"
  10362. (- (length org-todo-sets) -1
  10363. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10364. (length org-todo-sets)
  10365. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10366. (setq org-last-todo-state-is-todo
  10367. (not (member org-state org-done-keywords)))
  10368. (setq now-done-p (and (member org-state org-done-keywords)
  10369. (not (member this org-done-keywords))))
  10370. (and logging (org-local-logging logging))
  10371. (when (and (or org-todo-log-states org-log-done)
  10372. (not (eq org-inhibit-logging t))
  10373. (not (memq arg '(nextset previousset))))
  10374. ;; we need to look at recording a time and note
  10375. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10376. (nth 2 (assoc this org-todo-log-states))))
  10377. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10378. (setq dolog 'time))
  10379. (when (and org-state
  10380. (member org-state org-not-done-keywords)
  10381. (not (member this org-not-done-keywords)))
  10382. ;; This is now a todo state and was not one before
  10383. ;; If there was a CLOSED time stamp, get rid of it.
  10384. (org-add-planning-info nil nil 'closed))
  10385. (when (and now-done-p org-log-done)
  10386. ;; It is now done, and it was not done before
  10387. (org-add-planning-info 'closed (org-current-effective-time))
  10388. (if (and (not dolog) (eq 'note org-log-done))
  10389. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10390. (when (and org-state dolog)
  10391. ;; This is a non-nil state, and we need to log it
  10392. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10393. ;; Fixup tag positioning
  10394. (org-todo-trigger-tag-changes org-state)
  10395. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10396. (when org-provide-todo-statistics
  10397. (org-update-parent-todo-statistics))
  10398. (run-hooks 'org-after-todo-state-change-hook)
  10399. (if (and arg (not (member org-state org-done-keywords)))
  10400. (setq head (org-get-todo-sequence-head org-state)))
  10401. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10402. ;; Do we need to trigger a repeat?
  10403. (when now-done-p
  10404. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10405. ;; This is for the agenda, take a snapshot of the headline.
  10406. (save-match-data
  10407. (setq org-agenda-headline-snapshot-before-repeat
  10408. (org-get-heading))))
  10409. (org-auto-repeat-maybe org-state))
  10410. ;; Fixup cursor location if close to the keyword
  10411. (if (and (outline-on-heading-p)
  10412. (not (bolp))
  10413. (save-excursion (beginning-of-line 1)
  10414. (looking-at org-todo-line-regexp))
  10415. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10416. (progn
  10417. (goto-char (or (match-end 2) (match-end 1)))
  10418. (and (looking-at " ") (just-one-space))))
  10419. (when org-trigger-hook
  10420. (save-excursion
  10421. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10422. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10423. "Block turning an entry into a TODO, using the hierarchy.
  10424. This checks whether the current task should be blocked from state
  10425. changes. Such blocking occurs when:
  10426. 1. The task has children which are not all in a completed state.
  10427. 2. A task has a parent with the property :ORDERED:, and there
  10428. are siblings prior to the current task with incomplete
  10429. status.
  10430. 3. The parent of the task is blocked because it has siblings that should
  10431. be done first, or is child of a block grandparent TODO entry."
  10432. (if (not org-enforce-todo-dependencies)
  10433. t ; if locally turned off don't block
  10434. (catch 'dont-block
  10435. ;; If this is not a todo state change, or if this entry is already DONE,
  10436. ;; do not block
  10437. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10438. (member (plist-get change-plist :from)
  10439. (cons 'done org-done-keywords))
  10440. (member (plist-get change-plist :to)
  10441. (cons 'todo org-not-done-keywords))
  10442. (not (plist-get change-plist :to)))
  10443. (throw 'dont-block t))
  10444. ;; If this task has children, and any are undone, it's blocked
  10445. (save-excursion
  10446. (org-back-to-heading t)
  10447. (let ((this-level (funcall outline-level)))
  10448. (outline-next-heading)
  10449. (let ((child-level (funcall outline-level)))
  10450. (while (and (not (eobp))
  10451. (> child-level this-level))
  10452. ;; this todo has children, check whether they are all
  10453. ;; completed
  10454. (if (and (not (org-entry-is-done-p))
  10455. (org-entry-is-todo-p))
  10456. (throw 'dont-block nil))
  10457. (outline-next-heading)
  10458. (setq child-level (funcall outline-level))))))
  10459. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10460. ;; any previous siblings are undone, it's blocked
  10461. (save-excursion
  10462. (org-back-to-heading t)
  10463. (let* ((pos (point))
  10464. (parent-pos (and (org-up-heading-safe) (point))))
  10465. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10466. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10467. (forward-line 1)
  10468. (re-search-forward org-not-done-heading-regexp pos t))
  10469. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10470. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10471. (while t
  10472. (goto-char parent-pos)
  10473. (if (not (looking-at org-not-done-heading-regexp))
  10474. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10475. (setq pos (point))
  10476. (setq parent-pos (and (org-up-heading-safe) (point)))
  10477. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10478. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10479. (forward-line 1)
  10480. (re-search-forward org-not-done-heading-regexp pos t))
  10481. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10482. (defcustom org-track-ordered-property-with-tag nil
  10483. "Should the ORDERED property also be shown as a tag?
  10484. The ORDERED property decides if an entry should require subtasks to be
  10485. completed in sequence. Since a property is not very visible, setting
  10486. this option means that toggling the ORDERED property with the command
  10487. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10488. not relevant for the behavior, but it makes things more visible.
  10489. Note that toggling the tag with tags commands will not change the property
  10490. and therefore not influence behavior!
  10491. This can be t, meaning the tag ORDERED should be used, It can also be a
  10492. string to select a different tag for this task."
  10493. :group 'org-todo
  10494. :type '(choice
  10495. (const :tag "No tracking" nil)
  10496. (const :tag "Track with ORDERED tag" t)
  10497. (string :tag "Use other tag")))
  10498. (defun org-toggle-ordered-property ()
  10499. "Toggle the ORDERED property of the current entry.
  10500. For better visibility, you can track the value of this property with a tag.
  10501. See variable `org-track-ordered-property-with-tag'."
  10502. (interactive)
  10503. (let* ((t1 org-track-ordered-property-with-tag)
  10504. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10505. (save-excursion
  10506. (org-back-to-heading)
  10507. (if (org-entry-get nil "ORDERED")
  10508. (progn
  10509. (org-delete-property "ORDERED")
  10510. (and tag (org-toggle-tag tag 'off))
  10511. (message "Subtasks can be completed in arbitrary order"))
  10512. (org-entry-put nil "ORDERED" "t")
  10513. (and tag (org-toggle-tag tag 'on))
  10514. (message "Subtasks must be completed in sequence")))))
  10515. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10516. (defun org-block-todo-from-checkboxes (change-plist)
  10517. "Block turning an entry into a TODO, using checkboxes.
  10518. This checks whether the current task should be blocked from state
  10519. changes because there are unchecked boxes in this entry."
  10520. (if (not org-enforce-todo-checkbox-dependencies)
  10521. t ; if locally turned off don't block
  10522. (catch 'dont-block
  10523. ;; If this is not a todo state change, or if this entry is already DONE,
  10524. ;; do not block
  10525. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10526. (member (plist-get change-plist :from)
  10527. (cons 'done org-done-keywords))
  10528. (member (plist-get change-plist :to)
  10529. (cons 'todo org-not-done-keywords))
  10530. (not (plist-get change-plist :to)))
  10531. (throw 'dont-block t))
  10532. ;; If this task has checkboxes that are not checked, it's blocked
  10533. (save-excursion
  10534. (org-back-to-heading t)
  10535. (let ((beg (point)) end)
  10536. (outline-next-heading)
  10537. (setq end (point))
  10538. (goto-char beg)
  10539. (if (org-list-search-forward
  10540. (concat (org-item-beginning-re)
  10541. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10542. "\\[[- ]\\]")
  10543. end t)
  10544. (progn
  10545. (if (boundp 'org-blocked-by-checkboxes)
  10546. (setq org-blocked-by-checkboxes t))
  10547. (throw 'dont-block nil)))))
  10548. t))) ; do not block
  10549. (defun org-entry-blocked-p ()
  10550. "Is the current entry blocked?"
  10551. (if (org-entry-get nil "NOBLOCKING")
  10552. nil ;; Never block this entry
  10553. (not
  10554. (run-hook-with-args-until-failure
  10555. 'org-blocker-hook
  10556. (list :type 'todo-state-change
  10557. :position (point)
  10558. :from 'todo
  10559. :to 'done)))))
  10560. (defun org-update-statistics-cookies (all)
  10561. "Update the statistics cookie, either from TODO or from checkboxes.
  10562. This should be called with the cursor in a line with a statistics cookie."
  10563. (interactive "P")
  10564. (if all
  10565. (progn
  10566. (org-update-checkbox-count 'all)
  10567. (org-map-entries 'org-update-parent-todo-statistics))
  10568. (if (not (org-at-heading-p))
  10569. (org-update-checkbox-count)
  10570. (let ((pos (move-marker (make-marker) (point)))
  10571. end l1 l2)
  10572. (ignore-errors (org-back-to-heading t))
  10573. (if (not (org-at-heading-p))
  10574. (org-update-checkbox-count)
  10575. (setq l1 (org-outline-level))
  10576. (setq end (save-excursion
  10577. (outline-next-heading)
  10578. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10579. (point)))
  10580. (if (and (save-excursion
  10581. (re-search-forward
  10582. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10583. (not (save-excursion (re-search-forward
  10584. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10585. (org-update-checkbox-count)
  10586. (if (and l2 (> l2 l1))
  10587. (progn
  10588. (goto-char end)
  10589. (org-update-parent-todo-statistics))
  10590. (goto-char pos)
  10591. (beginning-of-line 1)
  10592. (while (re-search-forward
  10593. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10594. (point-at-eol) t)
  10595. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10596. (goto-char pos)
  10597. (move-marker pos nil)))))
  10598. (defvar org-entry-property-inherited-from) ;; defined below
  10599. (defun org-update-parent-todo-statistics ()
  10600. "Update any statistics cookie in the parent of the current headline.
  10601. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10602. the entire subtree and this will travel up the hierarchy and update
  10603. statistics everywhere."
  10604. (let* ((prop (save-excursion (org-up-heading-safe)
  10605. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10606. (recursive (or (not org-hierarchical-todo-statistics)
  10607. (and prop (string-match "\\<recursive\\>" prop))))
  10608. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10609. 0))
  10610. (first t)
  10611. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10612. level ltoggle l1 new ndel
  10613. (cnt-all 0) (cnt-done 0) is-percent kwd
  10614. checkbox-beg ov ovs ove cookie-present)
  10615. (catch 'exit
  10616. (save-excursion
  10617. (beginning-of-line 1)
  10618. (setq ltoggle (funcall outline-level))
  10619. ;; Three situations are to consider:
  10620. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10621. ;; to the top-level ancestor on the headline;
  10622. ;; 2. If parent has "recursive" property, repeat up to the
  10623. ;; headline setting that property, taking inheritance into
  10624. ;; account;
  10625. ;; 3. Else, move up to direct parent and proceed only once.
  10626. (while (and (setq level (org-up-heading-safe))
  10627. (or recursive first)
  10628. (>= (point) lim))
  10629. (setq first nil cookie-present nil)
  10630. (unless (and level
  10631. (not (string-match
  10632. "\\<checkbox\\>"
  10633. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10634. "")))))
  10635. (throw 'exit nil))
  10636. (while (re-search-forward box-re (point-at-eol) t)
  10637. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10638. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10639. (save-match-data
  10640. (unless (outline-next-heading) (throw 'exit nil))
  10641. (while (and (looking-at org-complex-heading-regexp)
  10642. (> (setq l1 (length (match-string 1))) level))
  10643. (setq kwd (and (or recursive (= l1 ltoggle))
  10644. (match-string 2)))
  10645. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10646. (and (listp org-provide-todo-statistics)
  10647. (or (member kwd org-provide-todo-statistics)
  10648. (member kwd org-done-keywords))))
  10649. (setq cnt-all (1+ cnt-all))
  10650. (if (eq org-provide-todo-statistics t)
  10651. (and kwd (setq cnt-all (1+ cnt-all)))))
  10652. (and (member kwd org-done-keywords)
  10653. (setq cnt-done (1+ cnt-done)))
  10654. (outline-next-heading)))
  10655. (setq new
  10656. (if is-percent
  10657. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10658. (format "[%d/%d]" cnt-done cnt-all))
  10659. ndel (- (match-end 0) checkbox-beg))
  10660. ;; handle overlays when updating cookie from column view
  10661. (when (setq ov (car (overlays-at checkbox-beg)))
  10662. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10663. (delete-overlay ov))
  10664. (goto-char checkbox-beg)
  10665. (insert new)
  10666. (delete-region (point) (+ (point) ndel))
  10667. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10668. (when ov (move-overlay ov ovs ove)))
  10669. (when cookie-present
  10670. (run-hook-with-args 'org-after-todo-statistics-hook
  10671. cnt-done (- cnt-all cnt-done))))))
  10672. (run-hooks 'org-todo-statistics-hook)))
  10673. (defvar org-after-todo-statistics-hook nil
  10674. "Hook that is called after a TODO statistics cookie has been updated.
  10675. Each function is called with two arguments: the number of not-done entries
  10676. and the number of done entries.
  10677. For example, the following function, when added to this hook, will switch
  10678. an entry to DONE when all children are done, and back to TODO when new
  10679. entries are set to a TODO status. Note that this hook is only called
  10680. when there is a statistics cookie in the headline!
  10681. (defun org-summary-todo (n-done n-not-done)
  10682. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10683. (let (org-log-done org-log-states) ; turn off logging
  10684. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10685. ")
  10686. (defvar org-todo-statistics-hook nil
  10687. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10688. This hook runs even if there is no statistics cookie present, in which case
  10689. `org-after-todo-statistics-hook' would not run.")
  10690. (defun org-todo-trigger-tag-changes (state)
  10691. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10692. (let ((l org-todo-state-tags-triggers)
  10693. changes)
  10694. (when (or (not state) (equal state ""))
  10695. (setq changes (append changes (cdr (assoc "" l)))))
  10696. (when (and (stringp state) (> (length state) 0))
  10697. (setq changes (append changes (cdr (assoc state l)))))
  10698. (when (member state org-not-done-keywords)
  10699. (setq changes (append changes (cdr (assoc 'todo l)))))
  10700. (when (member state org-done-keywords)
  10701. (setq changes (append changes (cdr (assoc 'done l)))))
  10702. (dolist (c changes)
  10703. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10704. (defun org-local-logging (value)
  10705. "Get logging settings from a property VALUE."
  10706. (let* (words w a)
  10707. ;; directly set the variables, they are already local.
  10708. (setq org-log-done nil
  10709. org-log-repeat nil
  10710. org-todo-log-states nil)
  10711. (setq words (org-split-string value))
  10712. (while (setq w (pop words))
  10713. (cond
  10714. ((setq a (assoc w org-startup-options))
  10715. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10716. (set (nth 1 a) (nth 2 a))))
  10717. ((setq a (org-extract-log-state-settings w))
  10718. (and (member (car a) org-todo-keywords-1)
  10719. (push a org-todo-log-states)))))))
  10720. (defun org-get-todo-sequence-head (kwd)
  10721. "Return the head of the TODO sequence to which KWD belongs.
  10722. If KWD is not set, check if there is a text property remembering the
  10723. right sequence."
  10724. (let (p)
  10725. (cond
  10726. ((not kwd)
  10727. (or (get-text-property (point-at-bol) 'org-todo-head)
  10728. (progn
  10729. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10730. nil (point-at-eol)))
  10731. (get-text-property p 'org-todo-head))))
  10732. ((not (member kwd org-todo-keywords-1))
  10733. (car org-todo-keywords-1))
  10734. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10735. (defun org-fast-todo-selection ()
  10736. "Fast TODO keyword selection with single keys.
  10737. Returns the new TODO keyword, or nil if no state change should occur."
  10738. (let* ((fulltable org-todo-key-alist)
  10739. (done-keywords org-done-keywords) ;; needed for the faces.
  10740. (maxlen (apply 'max (mapcar
  10741. (lambda (x)
  10742. (if (stringp (car x)) (string-width (car x)) 0))
  10743. fulltable)))
  10744. (expert nil)
  10745. (fwidth (+ maxlen 3 1 3))
  10746. (ncol (/ (- (window-width) 4) fwidth))
  10747. tg cnt e c tbl
  10748. groups ingroup)
  10749. (save-excursion
  10750. (save-window-excursion
  10751. (if expert
  10752. (set-buffer (get-buffer-create " *Org todo*"))
  10753. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10754. (erase-buffer)
  10755. (org-set-local 'org-done-keywords done-keywords)
  10756. (setq tbl fulltable cnt 0)
  10757. (while (setq e (pop tbl))
  10758. (cond
  10759. ((equal e '(:startgroup))
  10760. (push '() groups) (setq ingroup t)
  10761. (when (not (= cnt 0))
  10762. (setq cnt 0)
  10763. (insert "\n"))
  10764. (insert "{ "))
  10765. ((equal e '(:endgroup))
  10766. (setq ingroup nil cnt 0)
  10767. (insert "}\n"))
  10768. ((equal e '(:newline))
  10769. (when (not (= cnt 0))
  10770. (setq cnt 0)
  10771. (insert "\n")
  10772. (setq e (car tbl))
  10773. (while (equal (car tbl) '(:newline))
  10774. (insert "\n")
  10775. (setq tbl (cdr tbl)))))
  10776. (t
  10777. (setq tg (car e) c (cdr e))
  10778. (if ingroup (push tg (car groups)))
  10779. (setq tg (org-add-props tg nil 'face
  10780. (org-get-todo-face tg)))
  10781. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10782. (insert "[" c "] " tg (make-string
  10783. (- fwidth 4 (length tg)) ?\ ))
  10784. (when (= (setq cnt (1+ cnt)) ncol)
  10785. (insert "\n")
  10786. (if ingroup (insert " "))
  10787. (setq cnt 0)))))
  10788. (insert "\n")
  10789. (goto-char (point-min))
  10790. (if (not expert) (org-fit-window-to-buffer))
  10791. (message "[a-z..]:Set [SPC]:clear")
  10792. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10793. (cond
  10794. ((or (= c ?\C-g)
  10795. (and (= c ?q) (not (rassoc c fulltable))))
  10796. (setq quit-flag t))
  10797. ((= c ?\ ) nil)
  10798. ((setq e (rassoc c fulltable) tg (car e))
  10799. tg)
  10800. (t (setq quit-flag t)))))))
  10801. (defun org-entry-is-todo-p ()
  10802. (member (org-get-todo-state) org-not-done-keywords))
  10803. (defun org-entry-is-done-p ()
  10804. (member (org-get-todo-state) org-done-keywords))
  10805. (defun org-get-todo-state ()
  10806. (save-excursion
  10807. (org-back-to-heading t)
  10808. (and (looking-at org-todo-line-regexp)
  10809. (match-end 2)
  10810. (match-string 2))))
  10811. (defun org-at-date-range-p (&optional inactive-ok)
  10812. "Is the cursor inside a date range?"
  10813. (interactive)
  10814. (save-excursion
  10815. (catch 'exit
  10816. (let ((pos (point)))
  10817. (skip-chars-backward "^[<\r\n")
  10818. (skip-chars-backward "<[")
  10819. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10820. (>= (match-end 0) pos)
  10821. (throw 'exit t))
  10822. (skip-chars-backward "^<[\r\n")
  10823. (skip-chars-backward "<[")
  10824. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10825. (>= (match-end 0) pos)
  10826. (throw 'exit t)))
  10827. nil)))
  10828. (defun org-get-repeat (&optional tagline)
  10829. "Check if there is a deadline/schedule with repeater in this entry."
  10830. (save-match-data
  10831. (save-excursion
  10832. (org-back-to-heading t)
  10833. (and (re-search-forward (if tagline
  10834. (concat tagline "\\s-*" org-repeat-re)
  10835. org-repeat-re)
  10836. (org-entry-end-position) t)
  10837. (match-string-no-properties 1)))))
  10838. (defvar org-last-changed-timestamp)
  10839. (defvar org-last-inserted-timestamp)
  10840. (defvar org-log-post-message)
  10841. (defvar org-log-note-purpose)
  10842. (defvar org-log-note-how)
  10843. (defvar org-log-note-extra)
  10844. (defun org-auto-repeat-maybe (done-word)
  10845. "Check if the current headline contains a repeated deadline/schedule.
  10846. If yes, set TODO state back to what it was and change the base date
  10847. of repeating deadline/scheduled time stamps to new date.
  10848. This function is run automatically after each state change to a DONE state."
  10849. ;; last-state is dynamically scoped into this function
  10850. (let* ((repeat (org-get-repeat))
  10851. (aa (assoc org-last-state org-todo-kwd-alist))
  10852. (interpret (nth 1 aa))
  10853. (head (nth 2 aa))
  10854. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10855. (msg "Entry repeats: ")
  10856. (org-log-done nil)
  10857. (org-todo-log-states nil)
  10858. re type n what ts time to-state)
  10859. (when repeat
  10860. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10861. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10862. org-todo-repeat-to-state))
  10863. (unless (and to-state (member to-state org-todo-keywords-1))
  10864. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10865. (org-todo to-state)
  10866. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10867. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10868. (org-time-stamp-format t t))))
  10869. (when org-log-repeat
  10870. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10871. (memq 'org-add-log-note post-command-hook))
  10872. ;; OK, we are already setup for some record
  10873. (if (eq org-log-repeat 'note)
  10874. ;; make sure we take a note, not only a time stamp
  10875. (setq org-log-note-how 'note))
  10876. ;; Set up for taking a record
  10877. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10878. org-last-state
  10879. 'findpos org-log-repeat)))
  10880. (org-back-to-heading t)
  10881. (org-add-planning-info nil nil 'closed)
  10882. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10883. org-deadline-time-regexp "\\)\\|\\("
  10884. org-ts-regexp "\\)"))
  10885. (while (re-search-forward
  10886. re (save-excursion (outline-next-heading) (point)) t)
  10887. (setq type (if (match-end 1) org-scheduled-string
  10888. (if (match-end 3) org-deadline-string "Plain:"))
  10889. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10890. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  10891. (setq n (string-to-number (match-string 2 ts))
  10892. what (match-string 3 ts))
  10893. (if (equal what "w") (setq n (* n 7) what "d"))
  10894. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  10895. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  10896. ;; Preparation, see if we need to modify the start date for the change
  10897. (when (match-end 1)
  10898. (setq time (save-match-data (org-time-string-to-time ts)))
  10899. (cond
  10900. ((equal (match-string 1 ts) ".")
  10901. ;; Shift starting date to today
  10902. (org-timestamp-change
  10903. (- (org-today) (time-to-days time))
  10904. 'day))
  10905. ((equal (match-string 1 ts) "+")
  10906. (let ((nshiftmax 10) (nshift 0))
  10907. (while (or (= nshift 0)
  10908. (<= (time-to-days time)
  10909. (time-to-days (current-time))))
  10910. (when (= (incf nshift) nshiftmax)
  10911. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10912. (error "Abort")))
  10913. (org-timestamp-change n (cdr (assoc what whata)))
  10914. (org-at-timestamp-p t)
  10915. (setq ts (match-string 1))
  10916. (setq time (save-match-data (org-time-string-to-time ts)))))
  10917. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10918. ;; rematch, so that we have everything in place for the real shift
  10919. (org-at-timestamp-p t)
  10920. (setq ts (match-string 1))
  10921. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  10922. (org-timestamp-change n (cdr (assoc what whata)))
  10923. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10924. (setq org-log-post-message msg)
  10925. (message "%s" msg))))
  10926. (defun org-show-todo-tree (arg)
  10927. "Make a compact tree which shows all headlines marked with TODO.
  10928. The tree will show the lines where the regexp matches, and all higher
  10929. headlines above the match.
  10930. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10931. With a numeric prefix N, construct a sparse tree for the Nth element
  10932. of `org-todo-keywords-1'."
  10933. (interactive "P")
  10934. (let ((case-fold-search nil)
  10935. (kwd-re
  10936. (cond ((null arg) org-not-done-regexp)
  10937. ((equal arg '(4))
  10938. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10939. (mapcar 'list org-todo-keywords-1))))
  10940. (concat "\\("
  10941. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10942. "\\)\\>")))
  10943. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10944. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10945. org-todo-keywords-1)))
  10946. (t (error "Invalid prefix argument: %s" arg)))))
  10947. (message "%d TODO entries found"
  10948. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10949. (defun org-deadline (&optional remove time)
  10950. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10951. With argument REMOVE, remove any deadline from the item.
  10952. With argument TIME, set the deadline at the corresponding date. TIME
  10953. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10954. (interactive "P")
  10955. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10956. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10957. 'region-start-level 'region))
  10958. org-loop-over-headlines-in-active-region)
  10959. (org-map-entries
  10960. `(org-deadline ',remove ,time)
  10961. org-loop-over-headlines-in-active-region
  10962. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10963. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10964. (repeater (and old-date
  10965. (string-match
  10966. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  10967. old-date)
  10968. (match-string 1 old-date))))
  10969. (if remove
  10970. (progn
  10971. (when (and old-date org-log-redeadline)
  10972. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10973. org-log-redeadline))
  10974. (org-remove-timestamp-with-keyword org-deadline-string)
  10975. (message "Item no longer has a deadline."))
  10976. (org-add-planning-info 'deadline time 'closed)
  10977. (when (and old-date org-log-redeadline
  10978. (not (equal old-date
  10979. (substring org-last-inserted-timestamp 1 -1))))
  10980. (org-add-log-setup 'redeadline nil old-date 'findpos
  10981. org-log-redeadline))
  10982. (when repeater
  10983. (save-excursion
  10984. (org-back-to-heading t)
  10985. (when (re-search-forward (concat org-deadline-string " "
  10986. org-last-inserted-timestamp)
  10987. (save-excursion
  10988. (outline-next-heading) (point)) t)
  10989. (goto-char (1- (match-end 0)))
  10990. (insert " " repeater)
  10991. (setq org-last-inserted-timestamp
  10992. (concat (substring org-last-inserted-timestamp 0 -1)
  10993. " " repeater
  10994. (substring org-last-inserted-timestamp -1))))))
  10995. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10996. (defun org-schedule (&optional remove time)
  10997. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10998. With argument REMOVE, remove any scheduling date from the item.
  10999. With argument TIME, scheduled at the corresponding date. TIME can
  11000. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11001. (interactive "P")
  11002. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11003. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11004. 'region-start-level 'region))
  11005. org-loop-over-headlines-in-active-region)
  11006. (org-map-entries
  11007. `(org-schedule ',remove ,time)
  11008. org-loop-over-headlines-in-active-region
  11009. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11010. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11011. (repeater (and old-date
  11012. (string-match
  11013. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11014. old-date)
  11015. (match-string 1 old-date))))
  11016. (if remove
  11017. (progn
  11018. (when (and old-date org-log-reschedule)
  11019. (org-add-log-setup 'delschedule nil old-date 'findpos
  11020. org-log-reschedule))
  11021. (org-remove-timestamp-with-keyword org-scheduled-string)
  11022. (message "Item is no longer scheduled."))
  11023. (org-add-planning-info 'scheduled time 'closed)
  11024. (when (and old-date org-log-reschedule
  11025. (not (equal old-date
  11026. (substring org-last-inserted-timestamp 1 -1))))
  11027. (org-add-log-setup 'reschedule nil old-date 'findpos
  11028. org-log-reschedule))
  11029. (when repeater
  11030. (save-excursion
  11031. (org-back-to-heading t)
  11032. (when (re-search-forward (concat org-scheduled-string " "
  11033. org-last-inserted-timestamp)
  11034. (save-excursion
  11035. (outline-next-heading) (point)) t)
  11036. (goto-char (1- (match-end 0)))
  11037. (insert " " repeater)
  11038. (setq org-last-inserted-timestamp
  11039. (concat (substring org-last-inserted-timestamp 0 -1)
  11040. " " repeater
  11041. (substring org-last-inserted-timestamp -1))))))
  11042. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11043. (defun org-get-scheduled-time (pom &optional inherit)
  11044. "Get the scheduled time as a time tuple, of a format suitable
  11045. for calling org-schedule with, or if there is no scheduling,
  11046. returns nil."
  11047. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11048. (when time
  11049. (apply 'encode-time (org-parse-time-string time)))))
  11050. (defun org-get-deadline-time (pom &optional inherit)
  11051. "Get the deadline as a time tuple, of a format suitable for
  11052. calling org-deadline with, or if there is no scheduling, returns
  11053. nil."
  11054. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11055. (when time
  11056. (apply 'encode-time (org-parse-time-string time)))))
  11057. (defun org-remove-timestamp-with-keyword (keyword)
  11058. "Remove all time stamps with KEYWORD in the current entry."
  11059. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11060. beg)
  11061. (save-excursion
  11062. (org-back-to-heading t)
  11063. (setq beg (point))
  11064. (outline-next-heading)
  11065. (while (re-search-backward re beg t)
  11066. (replace-match "")
  11067. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11068. (equal (char-before) ?\ ))
  11069. (backward-delete-char 1)
  11070. (if (string-match "^[ \t]*$" (buffer-substring
  11071. (point-at-bol) (point-at-eol)))
  11072. (delete-region (point-at-bol)
  11073. (min (point-max) (1+ (point-at-eol))))))))))
  11074. (defun org-add-planning-info (what &optional time &rest remove)
  11075. "Insert new timestamp with keyword in the line directly after the headline.
  11076. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11077. If non is given, the user is prompted for a date.
  11078. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11079. be removed."
  11080. (interactive)
  11081. (let (org-time-was-given org-end-time-was-given ts
  11082. end default-time default-input)
  11083. (catch 'exit
  11084. (when (and (memq what '(scheduled deadline))
  11085. (or (not time)
  11086. (and (stringp time)
  11087. (string-match "^[-+]+[0-9]" time))))
  11088. ;; Try to get a default date/time from existing timestamp
  11089. (save-excursion
  11090. (org-back-to-heading t)
  11091. (setq end (save-excursion (outline-next-heading) (point)))
  11092. (when (re-search-forward (if (eq what 'scheduled)
  11093. org-scheduled-time-regexp
  11094. org-deadline-time-regexp)
  11095. end t)
  11096. (setq ts (match-string 1)
  11097. default-time
  11098. (apply 'encode-time (org-parse-time-string ts))
  11099. default-input (and ts (org-get-compact-tod ts))))))
  11100. (when what
  11101. (setq time
  11102. (if (stringp time)
  11103. ;; This is a string (relative or absolute), set proper date
  11104. (apply 'encode-time
  11105. (org-read-date-analyze
  11106. time default-time (decode-time default-time)))
  11107. ;; If necessary, get the time from the user
  11108. (or time (org-read-date nil 'to-time nil nil
  11109. default-time default-input)))))
  11110. (when (and org-insert-labeled-timestamps-at-point
  11111. (member what '(scheduled deadline)))
  11112. (insert
  11113. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11114. (org-insert-time-stamp time org-time-was-given
  11115. nil nil nil (list org-end-time-was-given))
  11116. (setq what nil))
  11117. (save-excursion
  11118. (save-restriction
  11119. (let (col list elt ts buffer-invisibility-spec)
  11120. (org-back-to-heading t)
  11121. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11122. (goto-char (match-end 1))
  11123. (setq col (current-column))
  11124. (goto-char (match-end 0))
  11125. (if (eobp) (insert "\n") (forward-char 1))
  11126. (when (and (not what)
  11127. (not (looking-at
  11128. (concat "[ \t]*"
  11129. org-keyword-time-not-clock-regexp))))
  11130. ;; Nothing to add, nothing to remove...... :-)
  11131. (throw 'exit nil))
  11132. (if (and (not (looking-at org-outline-regexp))
  11133. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11134. "[^\r\n]*"))
  11135. (not (equal (match-string 1) org-clock-string)))
  11136. (narrow-to-region (match-beginning 0) (match-end 0))
  11137. (insert-before-markers "\n")
  11138. (backward-char 1)
  11139. (narrow-to-region (point) (point))
  11140. (and org-adapt-indentation (org-indent-to-column col)))
  11141. ;; Check if we have to remove something.
  11142. (setq list (cons what remove))
  11143. (while list
  11144. (setq elt (pop list))
  11145. (when (or (and (eq elt 'scheduled)
  11146. (re-search-forward org-scheduled-time-regexp nil t))
  11147. (and (eq elt 'deadline)
  11148. (re-search-forward org-deadline-time-regexp nil t))
  11149. (and (eq elt 'closed)
  11150. (re-search-forward org-closed-time-regexp nil t)))
  11151. (replace-match "")
  11152. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11153. (and (looking-at "[ \t]+") (replace-match ""))
  11154. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11155. (when what
  11156. (insert
  11157. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11158. (cond ((eq what 'scheduled) org-scheduled-string)
  11159. ((eq what 'deadline) org-deadline-string)
  11160. ((eq what 'closed) org-closed-string))
  11161. " ")
  11162. (setq ts (org-insert-time-stamp
  11163. time
  11164. (or org-time-was-given
  11165. (and (eq what 'closed) org-log-done-with-time))
  11166. (eq what 'closed)
  11167. nil nil (list org-end-time-was-given)))
  11168. (insert
  11169. (if (not (or (bolp) (eq (char-before) ?\ )
  11170. (memq (char-after) '(32 10))
  11171. (eobp))) " " ""))
  11172. (end-of-line 1))
  11173. (goto-char (point-min))
  11174. (widen)
  11175. (if (and (looking-at "[ \t]*\n")
  11176. (equal (char-before) ?\n))
  11177. (delete-region (1- (point)) (point-at-eol)))
  11178. ts))))))
  11179. (defvar org-log-note-marker (make-marker))
  11180. (defvar org-log-note-purpose nil)
  11181. (defvar org-log-note-state nil)
  11182. (defvar org-log-note-previous-state nil)
  11183. (defvar org-log-note-how nil)
  11184. (defvar org-log-note-extra nil)
  11185. (defvar org-log-note-window-configuration nil)
  11186. (defvar org-log-note-return-to (make-marker))
  11187. (defvar org-log-note-effective-time nil
  11188. "Remembered current time so that dynamically scoped
  11189. `org-extend-today-until' affects tha timestamps in state change
  11190. log")
  11191. (defvar org-log-post-message nil
  11192. "Message to be displayed after a log note has been stored.
  11193. The auto-repeater uses this.")
  11194. (defun org-add-note ()
  11195. "Add a note to the current entry.
  11196. This is done in the same way as adding a state change note."
  11197. (interactive)
  11198. (org-add-log-setup 'note nil nil 'findpos nil))
  11199. (defvar org-property-end-re)
  11200. (defun org-add-log-setup (&optional purpose state prev-state
  11201. findpos how extra)
  11202. "Set up the post command hook to take a note.
  11203. If this is about to TODO state change, the new state is expected in STATE.
  11204. When FINDPOS is non-nil, find the correct position for the note in
  11205. the current entry. If not, assume that it can be inserted at point.
  11206. HOW is an indicator what kind of note should be created.
  11207. EXTRA is additional text that will be inserted into the notes buffer."
  11208. (let* ((org-log-into-drawer (org-log-into-drawer))
  11209. (drawer (cond ((stringp org-log-into-drawer)
  11210. org-log-into-drawer)
  11211. (org-log-into-drawer "LOGBOOK")
  11212. (t nil))))
  11213. (save-restriction
  11214. (save-excursion
  11215. (when findpos
  11216. (org-back-to-heading t)
  11217. (narrow-to-region (point) (save-excursion
  11218. (outline-next-heading) (point)))
  11219. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11220. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11221. "[^\r\n]*\\)?"))
  11222. (goto-char (match-end 0))
  11223. (cond
  11224. (drawer
  11225. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11226. nil t)
  11227. (progn
  11228. (goto-char (match-end 0))
  11229. (or org-log-states-order-reversed
  11230. (and (re-search-forward org-property-end-re nil t)
  11231. (goto-char (1- (match-beginning 0))))))
  11232. (insert "\n:" drawer ":\n:END:")
  11233. (beginning-of-line 0)
  11234. (org-indent-line-function)
  11235. (beginning-of-line 2)
  11236. (org-indent-line-function)
  11237. (end-of-line 0)))
  11238. ((and org-log-state-notes-insert-after-drawers
  11239. (save-excursion
  11240. (forward-line) (looking-at org-drawer-regexp)))
  11241. (forward-line)
  11242. (while (looking-at org-drawer-regexp)
  11243. (goto-char (match-end 0))
  11244. (re-search-forward org-property-end-re (point-max) t)
  11245. (forward-line))
  11246. (forward-line -1)))
  11247. (unless org-log-states-order-reversed
  11248. (and (= (char-after) ?\n) (forward-char 1))
  11249. (org-skip-over-state-notes)
  11250. (skip-chars-backward " \t\n\r")))
  11251. (move-marker org-log-note-marker (point))
  11252. (setq org-log-note-purpose purpose
  11253. org-log-note-state state
  11254. org-log-note-previous-state prev-state
  11255. org-log-note-how how
  11256. org-log-note-extra extra
  11257. org-log-note-effective-time (org-current-effective-time))
  11258. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11259. (defun org-skip-over-state-notes ()
  11260. "Skip past the list of State notes in an entry."
  11261. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11262. (when (ignore-errors (goto-char (org-in-item-p)))
  11263. (let* ((struct (org-list-struct))
  11264. (prevs (org-list-prevs-alist struct)))
  11265. (while (looking-at "[ \t]*- State")
  11266. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11267. (org-list-get-item-end (point) struct)))))))
  11268. (defun org-add-log-note (&optional purpose)
  11269. "Pop up a window for taking a note, and add this note later at point."
  11270. (remove-hook 'post-command-hook 'org-add-log-note)
  11271. (setq org-log-note-window-configuration (current-window-configuration))
  11272. (delete-other-windows)
  11273. (move-marker org-log-note-return-to (point))
  11274. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11275. (goto-char org-log-note-marker)
  11276. (org-switch-to-buffer-other-window "*Org Note*")
  11277. (erase-buffer)
  11278. (if (memq org-log-note-how '(time state))
  11279. (let (current-prefix-arg) (org-store-log-note))
  11280. (let ((org-inhibit-startup t)) (org-mode))
  11281. (insert (format "# Insert note for %s.
  11282. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11283. (cond
  11284. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11285. ((eq org-log-note-purpose 'done) "closed todo item")
  11286. ((eq org-log-note-purpose 'state)
  11287. (format "state change from \"%s\" to \"%s\""
  11288. (or org-log-note-previous-state "")
  11289. (or org-log-note-state "")))
  11290. ((eq org-log-note-purpose 'reschedule)
  11291. "rescheduling")
  11292. ((eq org-log-note-purpose 'delschedule)
  11293. "no longer scheduled")
  11294. ((eq org-log-note-purpose 'redeadline)
  11295. "changing deadline")
  11296. ((eq org-log-note-purpose 'deldeadline)
  11297. "removing deadline")
  11298. ((eq org-log-note-purpose 'refile)
  11299. "refiling")
  11300. ((eq org-log-note-purpose 'note)
  11301. "this entry")
  11302. (t (error "This should not happen")))))
  11303. (if org-log-note-extra (insert org-log-note-extra))
  11304. (org-set-local 'org-finish-function 'org-store-log-note)
  11305. (run-hooks 'org-log-buffer-setup-hook)))
  11306. (defvar org-note-abort nil) ; dynamically scoped
  11307. (defun org-store-log-note ()
  11308. "Finish taking a log note, and insert it to where it belongs."
  11309. (let ((txt (buffer-string))
  11310. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11311. lines ind bul)
  11312. (kill-buffer (current-buffer))
  11313. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11314. (setq txt (replace-match "" t t txt)))
  11315. (if (string-match "\\s-+\\'" txt)
  11316. (setq txt (replace-match "" t t txt)))
  11317. (setq lines (org-split-string txt "\n"))
  11318. (when (and note (string-match "\\S-" note))
  11319. (setq note
  11320. (org-replace-escapes
  11321. note
  11322. (list (cons "%u" (user-login-name))
  11323. (cons "%U" user-full-name)
  11324. (cons "%t" (format-time-string
  11325. (org-time-stamp-format 'long 'inactive)
  11326. org-log-note-effective-time))
  11327. (cons "%T" (format-time-string
  11328. (org-time-stamp-format 'long nil)
  11329. org-log-note-effective-time))
  11330. (cons "%d" (format-time-string
  11331. (org-time-stamp-format nil 'inactive)
  11332. org-log-note-effective-time))
  11333. (cons "%D" (format-time-string
  11334. (org-time-stamp-format nil nil)
  11335. org-log-note-effective-time))
  11336. (cons "%s" (if org-log-note-state
  11337. (concat "\"" org-log-note-state "\"")
  11338. ""))
  11339. (cons "%S" (if org-log-note-previous-state
  11340. (concat "\"" org-log-note-previous-state "\"")
  11341. "\"\"")))))
  11342. (if lines (setq note (concat note " \\\\")))
  11343. (push note lines))
  11344. (when (or current-prefix-arg org-note-abort)
  11345. (when org-log-into-drawer
  11346. (org-remove-empty-drawer-at
  11347. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11348. org-log-note-marker))
  11349. (setq lines nil))
  11350. (when lines
  11351. (with-current-buffer (marker-buffer org-log-note-marker)
  11352. (save-excursion
  11353. (goto-char org-log-note-marker)
  11354. (move-marker org-log-note-marker nil)
  11355. (end-of-line 1)
  11356. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11357. (setq ind (save-excursion
  11358. (if (ignore-errors (goto-char (org-in-item-p)))
  11359. (let ((struct (org-list-struct)))
  11360. (org-list-get-ind
  11361. (org-list-get-top-point struct) struct))
  11362. (skip-chars-backward " \r\t\n")
  11363. (cond
  11364. ((and (org-at-heading-p)
  11365. org-adapt-indentation)
  11366. (1+ (org-current-level)))
  11367. ((org-at-heading-p) 0)
  11368. (t (org-get-indentation))))))
  11369. (setq bul (org-list-bullet-string "-"))
  11370. (org-indent-line-to ind)
  11371. (insert bul (pop lines))
  11372. (let ((ind-body (+ (length bul) ind)))
  11373. (while lines
  11374. (insert "\n")
  11375. (org-indent-line-to ind-body)
  11376. (insert (pop lines))))
  11377. (message "Note stored")
  11378. (org-back-to-heading t)
  11379. (org-cycle-hide-drawers 'children)))))
  11380. (set-window-configuration org-log-note-window-configuration)
  11381. (with-current-buffer (marker-buffer org-log-note-return-to)
  11382. (goto-char org-log-note-return-to))
  11383. (move-marker org-log-note-return-to nil)
  11384. (and org-log-post-message (message "%s" org-log-post-message)))
  11385. (defun org-remove-empty-drawer-at (drawer pos)
  11386. "Remove an empty drawer DRAWER at position POS.
  11387. POS may also be a marker."
  11388. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11389. (save-excursion
  11390. (save-restriction
  11391. (widen)
  11392. (goto-char pos)
  11393. (if (org-in-regexp
  11394. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11395. (replace-match ""))))))
  11396. (defun org-sparse-tree (&optional arg)
  11397. "Create a sparse tree, prompt for the details.
  11398. This command can create sparse trees. You first need to select the type
  11399. of match used to create the tree:
  11400. t Show all TODO entries.
  11401. T Show entries with a specific TODO keyword.
  11402. m Show entries selected by a tags/property match.
  11403. p Enter a property name and its value (both with completion on existing
  11404. names/values) and show entries with that property.
  11405. r Show entries matching a regular expression (`/' can be used as well)
  11406. d Show deadlines due within `org-deadline-warning-days'.
  11407. b Show deadlines and scheduled items before a date.
  11408. a Show deadlines and scheduled items after a date."
  11409. (interactive "P")
  11410. (let (ans kwd value)
  11411. (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")
  11412. (setq ans (read-char-exclusive))
  11413. (cond
  11414. ((equal ans ?d)
  11415. (call-interactively 'org-check-deadlines))
  11416. ((equal ans ?b)
  11417. (call-interactively 'org-check-before-date))
  11418. ((equal ans ?a)
  11419. (call-interactively 'org-check-after-date))
  11420. ((equal ans ?D)
  11421. (call-interactively 'org-check-dates-range))
  11422. ((equal ans ?t)
  11423. (org-show-todo-tree nil))
  11424. ((equal ans ?T)
  11425. (org-show-todo-tree '(4)))
  11426. ((member ans '(?T ?m))
  11427. (call-interactively 'org-match-sparse-tree))
  11428. ((member ans '(?p ?P))
  11429. (setq kwd (org-icompleting-read "Property: "
  11430. (mapcar 'list (org-buffer-property-keys))))
  11431. (setq value (org-icompleting-read "Value: "
  11432. (mapcar 'list (org-property-values kwd))))
  11433. (unless (string-match "\\`{.*}\\'" value)
  11434. (setq value (concat "\"" value "\"")))
  11435. (org-match-sparse-tree arg (concat kwd "=" value)))
  11436. ((member ans '(?r ?R ?/))
  11437. (call-interactively 'org-occur))
  11438. (t (error "No such sparse tree command \"%c\"" ans)))))
  11439. (defvar org-occur-highlights nil
  11440. "List of overlays used for occur matches.")
  11441. (make-variable-buffer-local 'org-occur-highlights)
  11442. (defvar org-occur-parameters nil
  11443. "Parameters of the active org-occur calls.
  11444. This is a list, each call to org-occur pushes as cons cell,
  11445. containing the regular expression and the callback, onto the list.
  11446. The list can contain several entries if `org-occur' has been called
  11447. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11448. will only contain one set of parameters. When the highlights are
  11449. removed (for example with `C-c C-c', or with the next edit (depending
  11450. on `org-remove-highlights-with-change'), this variable is emptied
  11451. as well.")
  11452. (make-variable-buffer-local 'org-occur-parameters)
  11453. (defun org-occur (regexp &optional keep-previous callback)
  11454. "Make a compact tree which shows all matches of REGEXP.
  11455. The tree will show the lines where the regexp matches, and all higher
  11456. headlines above the match. It will also show the heading after the match,
  11457. to make sure editing the matching entry is easy.
  11458. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11459. call to `org-occur' will be kept, to allow stacking of calls to this
  11460. command.
  11461. If CALLBACK is non-nil, it is a function which is called to confirm
  11462. that the match should indeed be shown."
  11463. (interactive "sRegexp: \nP")
  11464. (when (equal regexp "")
  11465. (error "Regexp cannot be empty"))
  11466. (unless keep-previous
  11467. (org-remove-occur-highlights nil nil t))
  11468. (push (cons regexp callback) org-occur-parameters)
  11469. (let ((cnt 0))
  11470. (save-excursion
  11471. (goto-char (point-min))
  11472. (if (or (not keep-previous) ; do not want to keep
  11473. (not org-occur-highlights)) ; no previous matches
  11474. ;; hide everything
  11475. (org-overview))
  11476. (while (re-search-forward regexp nil t)
  11477. (when (or (not callback)
  11478. (save-match-data (funcall callback)))
  11479. (setq cnt (1+ cnt))
  11480. (when org-highlight-sparse-tree-matches
  11481. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11482. (org-show-context 'occur-tree))))
  11483. (when org-remove-highlights-with-change
  11484. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11485. nil 'local))
  11486. (unless org-sparse-tree-open-archived-trees
  11487. (org-hide-archived-subtrees (point-min) (point-max)))
  11488. (run-hooks 'org-occur-hook)
  11489. (if (org-called-interactively-p 'interactive)
  11490. (message "%d match(es) for regexp %s" cnt regexp))
  11491. cnt))
  11492. (defun org-occur-next-match (&optional n reset)
  11493. "Function for `next-error-function' to find sparse tree matches.
  11494. N is the number of matches to move, when negative move backwards.
  11495. RESET is entirely ignored - this function always goes back to the
  11496. starting point when no match is found."
  11497. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11498. (search-func (if (< n 0)
  11499. 'previous-single-char-property-change
  11500. 'next-single-char-property-change))
  11501. (n (abs n))
  11502. (pos (point))
  11503. p1)
  11504. (catch 'exit
  11505. (while (setq p1 (funcall search-func (point) 'org-type))
  11506. (when (equal p1 limit)
  11507. (goto-char pos)
  11508. (error "No more matches"))
  11509. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11510. (setq n (1- n))
  11511. (when (= n 0)
  11512. (goto-char p1)
  11513. (throw 'exit (point))))
  11514. (goto-char p1))
  11515. (goto-char p1)
  11516. (error "No more matches"))))
  11517. (defun org-show-context (&optional key)
  11518. "Make sure point and context are visible.
  11519. How much context is shown depends upon the variables
  11520. `org-show-hierarchy-above', `org-show-following-heading',
  11521. `org-show-entry-below' and `org-show-siblings'."
  11522. (let ((heading-p (org-at-heading-p t))
  11523. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11524. (following-p (org-get-alist-option org-show-following-heading key))
  11525. (entry-p (org-get-alist-option org-show-entry-below key))
  11526. (siblings-p (org-get-alist-option org-show-siblings key)))
  11527. (catch 'exit
  11528. ;; Show heading or entry text
  11529. (if (and heading-p (not entry-p))
  11530. (org-flag-heading nil) ; only show the heading
  11531. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11532. (org-show-hidden-entry))) ; show entire entry
  11533. (when following-p
  11534. ;; Show next sibling, or heading below text
  11535. (save-excursion
  11536. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11537. (org-flag-heading nil))))
  11538. (when siblings-p (org-show-siblings))
  11539. (when hierarchy-p
  11540. ;; show all higher headings, possibly with siblings
  11541. (save-excursion
  11542. (while (and (condition-case nil
  11543. (progn (org-up-heading-all 1) t)
  11544. (error nil))
  11545. (not (bobp)))
  11546. (org-flag-heading nil)
  11547. (when siblings-p (org-show-siblings))))))))
  11548. (defvar org-reveal-start-hook nil
  11549. "Hook run before revealing a location.")
  11550. (defun org-reveal (&optional siblings)
  11551. "Show current entry, hierarchy above it, and the following headline.
  11552. This can be used to show a consistent set of context around locations
  11553. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11554. not t for the search context.
  11555. With optional argument SIBLINGS, on each level of the hierarchy all
  11556. siblings are shown. This repairs the tree structure to what it would
  11557. look like when opened with hierarchical calls to `org-cycle'.
  11558. With double optional argument \\[universal-argument] \\[universal-argument], \
  11559. go to the parent and show the
  11560. entire tree."
  11561. (interactive "P")
  11562. (run-hooks 'org-reveal-start-hook)
  11563. (let ((org-show-hierarchy-above t)
  11564. (org-show-following-heading t)
  11565. (org-show-siblings (if siblings t org-show-siblings)))
  11566. (org-show-context nil))
  11567. (when (equal siblings '(16))
  11568. (save-excursion
  11569. (when (org-up-heading-safe)
  11570. (org-show-subtree)
  11571. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11572. (defun org-highlight-new-match (beg end)
  11573. "Highlight from BEG to END and mark the highlight is an occur headline."
  11574. (let ((ov (make-overlay beg end)))
  11575. (overlay-put ov 'face 'secondary-selection)
  11576. (overlay-put ov 'org-type 'org-occur)
  11577. (push ov org-occur-highlights)))
  11578. (defun org-remove-occur-highlights (&optional beg end noremove)
  11579. "Remove the occur highlights from the buffer.
  11580. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11581. from the `before-change-functions' in the current buffer."
  11582. (interactive)
  11583. (unless org-inhibit-highlight-removal
  11584. (mapc 'delete-overlay org-occur-highlights)
  11585. (setq org-occur-highlights nil)
  11586. (setq org-occur-parameters nil)
  11587. (unless noremove
  11588. (remove-hook 'before-change-functions
  11589. 'org-remove-occur-highlights 'local))))
  11590. ;;;; Priorities
  11591. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11592. "Regular expression matching the priority indicator.")
  11593. (defvar org-remove-priority-next-time nil)
  11594. (defun org-priority-up ()
  11595. "Increase the priority of the current item."
  11596. (interactive)
  11597. (org-priority 'up))
  11598. (defun org-priority-down ()
  11599. "Decrease the priority of the current item."
  11600. (interactive)
  11601. (org-priority 'down))
  11602. (defun org-priority (&optional action)
  11603. "Change the priority of an item by ARG.
  11604. ACTION can be `set', `up', `down', or a character."
  11605. (interactive)
  11606. (unless org-enable-priority-commands
  11607. (error "Priority commands are disabled"))
  11608. (setq action (or action 'set))
  11609. (let (current new news have remove)
  11610. (save-excursion
  11611. (org-back-to-heading t)
  11612. (if (looking-at org-priority-regexp)
  11613. (setq current (string-to-char (match-string 2))
  11614. have t))
  11615. (cond
  11616. ((eq action 'remove)
  11617. (setq remove t new ?\ ))
  11618. ((or (eq action 'set)
  11619. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11620. (if (not (eq action 'set))
  11621. (setq new action)
  11622. (message "Priority %c-%c, SPC to remove: "
  11623. org-highest-priority org-lowest-priority)
  11624. (save-match-data
  11625. (setq new (read-char-exclusive))))
  11626. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11627. (= (upcase org-lowest-priority) org-lowest-priority))
  11628. (setq new (upcase new)))
  11629. (cond ((equal new ?\ ) (setq remove t))
  11630. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11631. (error "Priority must be between `%c' and `%c'"
  11632. org-highest-priority org-lowest-priority))))
  11633. ((eq action 'up)
  11634. (setq new (if have
  11635. (1- current) ; normal cycling
  11636. ;; last priority was empty
  11637. (if (eq last-command this-command)
  11638. org-lowest-priority ; wrap around empty to lowest
  11639. ;; default
  11640. (if org-priority-start-cycle-with-default
  11641. org-default-priority
  11642. (1- org-default-priority))))))
  11643. ((eq action 'down)
  11644. (setq new (if have
  11645. (1+ current) ; normal cycling
  11646. ;; last priority was empty
  11647. (if (eq last-command this-command)
  11648. org-highest-priority ; wrap around empty to highest
  11649. ;; default
  11650. (if org-priority-start-cycle-with-default
  11651. org-default-priority
  11652. (1+ org-default-priority))))))
  11653. (t (error "Invalid action")))
  11654. (if (or (< (upcase new) org-highest-priority)
  11655. (> (upcase new) org-lowest-priority))
  11656. (if (and (memq action '(up down))
  11657. (not have) (not (eq last-command this-command)))
  11658. ;; `new' is from default priority
  11659. (error
  11660. "The default can not be set, see `org-default-priority' why")
  11661. ;; normal cycling: `new' is beyond highest/lowest priority
  11662. ;; and is wrapped around to the empty priority
  11663. (setq remove t)))
  11664. (setq news (format "%c" new))
  11665. (if have
  11666. (if remove
  11667. (replace-match "" t t nil 1)
  11668. (replace-match news t t nil 2))
  11669. (if remove
  11670. (error "No priority cookie found in line")
  11671. (let ((case-fold-search nil))
  11672. (looking-at org-todo-line-regexp))
  11673. (if (match-end 2)
  11674. (progn
  11675. (goto-char (match-end 2))
  11676. (insert " [#" news "]"))
  11677. (goto-char (match-beginning 3))
  11678. (insert "[#" news "] "))))
  11679. (org-preserve-lc (org-set-tags nil 'align)))
  11680. (if remove
  11681. (message "Priority removed")
  11682. (message "Priority of current item set to %s" news))))
  11683. (defun org-get-priority (s)
  11684. "Find priority cookie and return priority."
  11685. (if (functionp org-get-priority-function)
  11686. (funcall org-get-priority-function)
  11687. (save-match-data
  11688. (if (not (string-match org-priority-regexp s))
  11689. (* 1000 (- org-lowest-priority org-default-priority))
  11690. (* 1000 (- org-lowest-priority
  11691. (string-to-char (match-string 2 s))))))))
  11692. ;;;; Tags
  11693. (defvar org-agenda-archives-mode)
  11694. (defvar org-map-continue-from nil
  11695. "Position from where mapping should continue.
  11696. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11697. (defvar org-scanner-tags nil
  11698. "The current tag list while the tags scanner is running.")
  11699. (defvar org-trust-scanner-tags nil
  11700. "Should `org-get-tags-at' use the tags for the scanner.
  11701. This is for internal dynamical scoping only.
  11702. When this is non-nil, the function `org-get-tags-at' will return the value
  11703. of `org-scanner-tags' instead of building the list by itself. This
  11704. can lead to large speed-ups when the tags scanner is used in a file with
  11705. many entries, and when the list of tags is retrieved, for example to
  11706. obtain a list of properties. Building the tags list for each entry in such
  11707. a file becomes an N^2 operation - but with this variable set, it scales
  11708. as N.")
  11709. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11710. "Scan headline tags with inheritance and produce output ACTION.
  11711. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11712. or `agenda' to produce an entry list for an agenda view. It can also be
  11713. a Lisp form or a function that should be called at each matched headline, in
  11714. this case the return value is a list of all return values from these calls.
  11715. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11716. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11717. only lines with a not-done TODO keyword are included in the output.
  11718. This should be the same variable that was scoped into
  11719. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11720. START-LEVEL can be a string with asterisks, reducing the scope to
  11721. headlines matching this string."
  11722. (require 'org-agenda)
  11723. (let* ((re (concat "^"
  11724. (if start-level
  11725. ;; Get the correct level to match
  11726. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11727. org-outline-regexp)
  11728. " *\\(\\<\\("
  11729. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11730. (org-re
  11731. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11732. (props (list 'face 'default
  11733. 'done-face 'org-agenda-done
  11734. 'undone-face 'default
  11735. 'mouse-face 'highlight
  11736. 'org-not-done-regexp org-not-done-regexp
  11737. 'org-todo-regexp org-todo-regexp
  11738. 'org-complex-heading-regexp org-complex-heading-regexp
  11739. 'help-echo
  11740. (format "mouse-2 or RET jump to org file %s"
  11741. (abbreviate-file-name
  11742. (or (buffer-file-name (buffer-base-buffer))
  11743. (buffer-name (buffer-base-buffer)))))))
  11744. (case-fold-search nil)
  11745. (org-map-continue-from nil)
  11746. lspos tags tags-list
  11747. (tags-alist (list (cons 0 org-file-tags)))
  11748. (llast 0) rtn rtn1 level category i txt
  11749. todo marker entry priority)
  11750. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11751. (setq action (list 'lambda nil action)))
  11752. (save-excursion
  11753. (goto-char (point-min))
  11754. (when (eq action 'sparse-tree)
  11755. (org-overview)
  11756. (org-remove-occur-highlights))
  11757. (while (re-search-forward re nil t)
  11758. (setq org-map-continue-from nil)
  11759. (catch :skip
  11760. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11761. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11762. (goto-char (setq lspos (match-beginning 0)))
  11763. (setq level (org-reduced-level (funcall outline-level))
  11764. category (org-get-category))
  11765. (setq i llast llast level)
  11766. ;; remove tag lists from same and sublevels
  11767. (while (>= i level)
  11768. (when (setq entry (assoc i tags-alist))
  11769. (setq tags-alist (delete entry tags-alist)))
  11770. (setq i (1- i)))
  11771. ;; add the next tags
  11772. (when tags
  11773. (setq tags (org-split-string tags ":")
  11774. tags-alist
  11775. (cons (cons level tags) tags-alist)))
  11776. ;; compile tags for current headline
  11777. (setq tags-list
  11778. (if org-use-tag-inheritance
  11779. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11780. tags)
  11781. org-scanner-tags tags-list)
  11782. (when org-use-tag-inheritance
  11783. (setcdr (car tags-alist)
  11784. (mapcar (lambda (x)
  11785. (setq x (copy-sequence x))
  11786. (org-add-prop-inherited x))
  11787. (cdar tags-alist))))
  11788. (when (and tags org-use-tag-inheritance
  11789. (or (not (eq t org-use-tag-inheritance))
  11790. org-tags-exclude-from-inheritance))
  11791. ;; selective inheritance, remove uninherited ones
  11792. (setcdr (car tags-alist)
  11793. (org-remove-uninherited-tags (cdar tags-alist))))
  11794. (when (and
  11795. ;; eval matcher only when the todo condition is OK
  11796. (and (or (not todo-only) (member todo org-not-done-keywords))
  11797. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11798. (eval matcher)))
  11799. ;; Call the skipper, but return t if it does not skip,
  11800. ;; so that the `and' form continues evaluating
  11801. (progn
  11802. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11803. t)
  11804. ;; Check if timestamps are deselecting this entry
  11805. (or (not todo-only)
  11806. (and (member todo org-not-done-keywords)
  11807. (or (not org-agenda-tags-todo-honor-ignore-options)
  11808. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11809. ;; Extra check for the archive tag
  11810. ;; FIXME: Does the skipper already do this????
  11811. (or
  11812. (not (member org-archive-tag tags-list))
  11813. ;; we have an archive tag, should we use this anyway?
  11814. (or (not org-agenda-skip-archived-trees)
  11815. (and (eq action 'agenda) org-agenda-archives-mode))))
  11816. ;; select this headline
  11817. (cond
  11818. ((eq action 'sparse-tree)
  11819. (and org-highlight-sparse-tree-matches
  11820. (org-get-heading) (match-end 0)
  11821. (org-highlight-new-match
  11822. (match-beginning 1) (match-end 1)))
  11823. (org-show-context 'tags-tree))
  11824. ((eq action 'agenda)
  11825. (setq txt (org-agenda-format-item
  11826. ""
  11827. (concat
  11828. (if (eq org-tags-match-list-sublevels 'indented)
  11829. (make-string (1- level) ?.) "")
  11830. (org-get-heading))
  11831. category
  11832. tags-list)
  11833. priority (org-get-priority txt))
  11834. (goto-char lspos)
  11835. (setq marker (org-agenda-new-marker))
  11836. (org-add-props txt props
  11837. 'org-marker marker 'org-hd-marker marker 'org-category category
  11838. 'todo-state todo
  11839. 'priority priority 'type "tagsmatch")
  11840. (push txt rtn))
  11841. ((functionp action)
  11842. (setq org-map-continue-from nil)
  11843. (save-excursion
  11844. (setq rtn1 (funcall action))
  11845. (push rtn1 rtn)))
  11846. (t (error "Invalid action")))
  11847. ;; if we are to skip sublevels, jump to end of subtree
  11848. (unless org-tags-match-list-sublevels
  11849. (org-end-of-subtree t)
  11850. (backward-char 1))))
  11851. ;; Get the correct position from where to continue
  11852. (if org-map-continue-from
  11853. (goto-char org-map-continue-from)
  11854. (and (= (point) lspos) (end-of-line 1)))))
  11855. (when (and (eq action 'sparse-tree)
  11856. (not org-sparse-tree-open-archived-trees))
  11857. (org-hide-archived-subtrees (point-min) (point-max)))
  11858. (nreverse rtn)))
  11859. (defun org-remove-uninherited-tags (tags)
  11860. "Remove all tags that are not inherited from the list TAGS."
  11861. (cond
  11862. ((eq org-use-tag-inheritance t)
  11863. (if org-tags-exclude-from-inheritance
  11864. (org-delete-all org-tags-exclude-from-inheritance tags)
  11865. tags))
  11866. ((not org-use-tag-inheritance) nil)
  11867. ((stringp org-use-tag-inheritance)
  11868. (delq nil (mapcar
  11869. (lambda (x)
  11870. (if (and (string-match org-use-tag-inheritance x)
  11871. (not (member x org-tags-exclude-from-inheritance)))
  11872. x nil))
  11873. tags)))
  11874. ((listp org-use-tag-inheritance)
  11875. (delq nil (mapcar
  11876. (lambda (x)
  11877. (if (member x org-use-tag-inheritance) x nil))
  11878. tags)))))
  11879. (defun org-match-sparse-tree (&optional todo-only match)
  11880. "Create a sparse tree according to tags string MATCH.
  11881. MATCH can contain positive and negative selection of tags, like
  11882. \"+WORK+URGENT-WITHBOSS\".
  11883. If optional argument TODO-ONLY is non-nil, only select lines that are
  11884. also TODO lines."
  11885. (interactive "P")
  11886. (org-prepare-agenda-buffers (list (current-buffer)))
  11887. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11888. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11889. (defvar org-cached-props nil)
  11890. (defun org-cached-entry-get (pom property)
  11891. (if (or (eq t org-use-property-inheritance)
  11892. (and (stringp org-use-property-inheritance)
  11893. (string-match org-use-property-inheritance property))
  11894. (and (listp org-use-property-inheritance)
  11895. (member property org-use-property-inheritance)))
  11896. ;; Caching is not possible, check it directly
  11897. (org-entry-get pom property 'inherit)
  11898. ;; Get all properties, so that we can do complicated checks easily
  11899. (cdr (assoc property (or org-cached-props
  11900. (setq org-cached-props
  11901. (org-entry-properties pom)))))))
  11902. (defun org-global-tags-completion-table (&optional files)
  11903. "Return the list of all tags in all agenda buffer/files.
  11904. Optional FILES argument is a list of files to which can be used
  11905. instead of the agenda files."
  11906. (save-excursion
  11907. (org-uniquify
  11908. (delq nil
  11909. (apply 'append
  11910. (mapcar
  11911. (lambda (file)
  11912. (set-buffer (find-file-noselect file))
  11913. (append (org-get-buffer-tags)
  11914. (mapcar (lambda (x) (if (stringp (car-safe x))
  11915. (list (car-safe x)) nil))
  11916. org-tag-alist)))
  11917. (if (and files (car files))
  11918. files
  11919. (org-agenda-files))))))))
  11920. (defun org-make-tags-matcher (match)
  11921. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11922. The variable `todo-only' is scoped dynamically into this function; it will be
  11923. set to t if the matcher restricts matching to TODO entries,
  11924. otherwise will not be touched.
  11925. Returns a cons of the selection string MATCH and the constructed
  11926. lisp form implementing the matcher. The matcher is to be
  11927. evaluated at an Org entry, with point on the headline,
  11928. and returns t if the entry matches the
  11929. selection string MATCH. The returned lisp form references
  11930. two variables with information about the entry, which must be
  11931. bound around the form's evaluation: todo, the TODO keyword at the
  11932. entry (or nil of none); and tags-list, the list of all tags at the
  11933. entry including inherited ones. Additionally, the category
  11934. of the entry (if any) must be specified as the text property
  11935. 'org-category on the headline.
  11936. See also `org-scan-tags'.
  11937. "
  11938. (declare (special todo-only))
  11939. (unless (boundp 'todo-only)
  11940. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11941. (unless match
  11942. ;; Get a new match request, with completion
  11943. (let ((org-last-tags-completion-table
  11944. (org-global-tags-completion-table)))
  11945. (setq match (org-completing-read-no-i
  11946. "Match: " 'org-tags-completion-function nil nil nil
  11947. 'org-tags-history))))
  11948. ;; Parse the string and create a lisp form
  11949. (let ((match0 match)
  11950. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11951. minus tag mm
  11952. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11953. orterms term orlist re-p str-p level-p level-op time-p
  11954. prop-p pn pv po gv rest)
  11955. (if (string-match "/+" match)
  11956. ;; match contains also a todo-matching request
  11957. (progn
  11958. (setq tagsmatch (substring match 0 (match-beginning 0))
  11959. todomatch (substring match (match-end 0)))
  11960. (if (string-match "^!" todomatch)
  11961. (setq todo-only t todomatch (substring todomatch 1)))
  11962. (if (string-match "^\\s-*$" todomatch)
  11963. (setq todomatch nil)))
  11964. ;; only matching tags
  11965. (setq tagsmatch match todomatch nil))
  11966. ;; Make the tags matcher
  11967. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11968. (setq tagsmatcher t)
  11969. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11970. (while (setq term (pop orterms))
  11971. (while (and (equal (substring term -1) "\\") orterms)
  11972. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11973. (while (string-match re term)
  11974. (setq rest (substring term (match-end 0))
  11975. minus (and (match-end 1)
  11976. (equal (match-string 1 term) "-"))
  11977. tag (save-match-data (replace-regexp-in-string
  11978. "\\\\-" "-"
  11979. (match-string 2 term)))
  11980. re-p (equal (string-to-char tag) ?{)
  11981. level-p (match-end 4)
  11982. prop-p (match-end 5)
  11983. mm (cond
  11984. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11985. (level-p
  11986. (setq level-op (org-op-to-function (match-string 3 term)))
  11987. `(,level-op level ,(string-to-number
  11988. (match-string 4 term))))
  11989. (prop-p
  11990. (setq pn (match-string 5 term)
  11991. po (match-string 6 term)
  11992. pv (match-string 7 term)
  11993. re-p (equal (string-to-char pv) ?{)
  11994. str-p (equal (string-to-char pv) ?\")
  11995. time-p (save-match-data
  11996. (string-match "^\"[[<].*[]>]\"$" pv))
  11997. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11998. (if time-p (setq pv (org-matcher-time pv)))
  11999. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12000. (cond
  12001. ((equal pn "CATEGORY")
  12002. (setq gv '(get-text-property (point) 'org-category)))
  12003. ((equal pn "TODO")
  12004. (setq gv 'todo))
  12005. (t
  12006. (setq gv `(org-cached-entry-get nil ,pn))))
  12007. (if re-p
  12008. (if (eq po 'org<>)
  12009. `(not (string-match ,pv (or ,gv "")))
  12010. `(string-match ,pv (or ,gv "")))
  12011. (if str-p
  12012. `(,po (or ,gv "") ,pv)
  12013. `(,po (string-to-number (or ,gv ""))
  12014. ,(string-to-number pv) ))))
  12015. (t `(member ,tag tags-list)))
  12016. mm (if minus (list 'not mm) mm)
  12017. term rest)
  12018. (push mm tagsmatcher))
  12019. (push (if (> (length tagsmatcher) 1)
  12020. (cons 'and tagsmatcher)
  12021. (car tagsmatcher))
  12022. orlist)
  12023. (setq tagsmatcher nil))
  12024. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12025. (setq tagsmatcher
  12026. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12027. ;; Make the todo matcher
  12028. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12029. (setq todomatcher t)
  12030. (setq orterms (org-split-string todomatch "|") orlist nil)
  12031. (while (setq term (pop orterms))
  12032. (while (string-match re term)
  12033. (setq minus (and (match-end 1)
  12034. (equal (match-string 1 term) "-"))
  12035. kwd (match-string 2 term)
  12036. re-p (equal (string-to-char kwd) ?{)
  12037. term (substring term (match-end 0))
  12038. mm (if re-p
  12039. `(string-match ,(substring kwd 1 -1) todo)
  12040. (list 'equal 'todo kwd))
  12041. mm (if minus (list 'not mm) mm))
  12042. (push mm todomatcher))
  12043. (push (if (> (length todomatcher) 1)
  12044. (cons 'and todomatcher)
  12045. (car todomatcher))
  12046. orlist)
  12047. (setq todomatcher nil))
  12048. (setq todomatcher (if (> (length orlist) 1)
  12049. (cons 'or orlist) (car orlist))))
  12050. ;; Return the string and lisp forms of the matcher
  12051. (setq matcher (if todomatcher
  12052. (list 'and tagsmatcher todomatcher)
  12053. tagsmatcher))
  12054. (when todo-only
  12055. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12056. matcher)))
  12057. (cons match0 matcher)))
  12058. (defun org-op-to-function (op &optional stringp)
  12059. "Turn an operator into the appropriate function."
  12060. (setq op
  12061. (cond
  12062. ((equal op "<" ) '(< string< org-time<))
  12063. ((equal op ">" ) '(> org-string> org-time>))
  12064. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12065. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12066. ((member op '("=" "==")) '(= string= org-time=))
  12067. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12068. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12069. (defun org<> (a b) (not (= a b)))
  12070. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12071. (defun org-string>= (a b) (not (string< a b)))
  12072. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12073. (defun org-string<> (a b) (not (string= a b)))
  12074. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12075. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12076. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12077. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12078. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12079. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12080. (defun org-2ft (s)
  12081. "Convert S to a floating point time.
  12082. If S is already a number, just return it. If it is a string, parse
  12083. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12084. (cond
  12085. ((numberp s) s)
  12086. ((stringp s)
  12087. (condition-case nil
  12088. (float-time (apply 'encode-time (org-parse-time-string s)))
  12089. (error 0.)))
  12090. (t 0.)))
  12091. (defun org-time-today ()
  12092. "Time in seconds today at 0:00.
  12093. Returns the float number of seconds since the beginning of the
  12094. epoch to the beginning of today (00:00)."
  12095. (float-time (apply 'encode-time
  12096. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12097. (defun org-matcher-time (s)
  12098. "Interpret a time comparison value."
  12099. (save-match-data
  12100. (cond
  12101. ((string= s "<now>") (float-time))
  12102. ((string= s "<today>") (org-time-today))
  12103. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12104. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12105. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12106. (+ (org-time-today)
  12107. (* (string-to-number (match-string 1 s))
  12108. (cdr (assoc (match-string 2 s)
  12109. '(("d" . 86400.0) ("w" . 604800.0)
  12110. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12111. (t (org-2ft s)))))
  12112. (defun org-match-any-p (re list)
  12113. "Does re match any element of list?"
  12114. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12115. (delq nil list))
  12116. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12117. (defvar org-tags-overlay (make-overlay 1 1))
  12118. (org-detach-overlay org-tags-overlay)
  12119. (defun org-get-local-tags-at (&optional pos)
  12120. "Get a list of tags defined in the current headline."
  12121. (org-get-tags-at pos 'local))
  12122. (defun org-get-local-tags ()
  12123. "Get a list of tags defined in the current headline."
  12124. (org-get-tags-at nil 'local))
  12125. (defun org-get-tags-at (&optional pos local)
  12126. "Get a list of all headline tags applicable at POS.
  12127. POS defaults to point. If tags are inherited, the list contains
  12128. the targets in the same sequence as the headlines appear, i.e.
  12129. the tags of the current headline come last.
  12130. When LOCAL is non-nil, only return tags from the current headline,
  12131. ignore inherited ones."
  12132. (interactive)
  12133. (if (and org-trust-scanner-tags
  12134. (or (not pos) (equal pos (point)))
  12135. (not local))
  12136. org-scanner-tags
  12137. (let (tags ltags lastpos parent)
  12138. (save-excursion
  12139. (save-restriction
  12140. (widen)
  12141. (goto-char (or pos (point)))
  12142. (save-match-data
  12143. (catch 'done
  12144. (condition-case nil
  12145. (progn
  12146. (org-back-to-heading t)
  12147. (while (not (equal lastpos (point)))
  12148. (setq lastpos (point))
  12149. (when (looking-at
  12150. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12151. (setq ltags (org-split-string
  12152. (org-match-string-no-properties 1) ":"))
  12153. (when parent
  12154. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12155. (setq tags (append
  12156. (if parent
  12157. (org-remove-uninherited-tags ltags)
  12158. ltags)
  12159. tags)))
  12160. (or org-use-tag-inheritance (throw 'done t))
  12161. (if local (throw 'done t))
  12162. (or (org-up-heading-safe) (error nil))
  12163. (setq parent t)))
  12164. (error nil)))))
  12165. (if local
  12166. tags
  12167. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12168. (defun org-add-prop-inherited (s)
  12169. (add-text-properties 0 (length s) '(inherited t) s)
  12170. s)
  12171. (defun org-toggle-tag (tag &optional onoff)
  12172. "Toggle the tag TAG for the current line.
  12173. If ONOFF is `on' or `off', don't toggle but set to this state."
  12174. (let (res current)
  12175. (save-excursion
  12176. (org-back-to-heading t)
  12177. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12178. (point-at-eol) t)
  12179. (progn
  12180. (setq current (match-string 1))
  12181. (replace-match ""))
  12182. (setq current ""))
  12183. (setq current (nreverse (org-split-string current ":")))
  12184. (cond
  12185. ((eq onoff 'on)
  12186. (setq res t)
  12187. (or (member tag current) (push tag current)))
  12188. ((eq onoff 'off)
  12189. (or (not (member tag current)) (setq current (delete tag current))))
  12190. (t (if (member tag current)
  12191. (setq current (delete tag current))
  12192. (setq res t)
  12193. (push tag current))))
  12194. (end-of-line 1)
  12195. (if current
  12196. (progn
  12197. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12198. (org-set-tags nil t))
  12199. (delete-horizontal-space))
  12200. (run-hooks 'org-after-tags-change-hook))
  12201. res))
  12202. (defun org-align-tags-here (to-col)
  12203. ;; Assumes that this is a headline
  12204. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12205. (beginning-of-line 1)
  12206. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12207. (< pos (match-beginning 2)))
  12208. (progn
  12209. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12210. (goto-char (match-beginning 1))
  12211. (insert " ")
  12212. (delete-region (point) (1+ (match-beginning 2)))
  12213. (setq ncol (max (current-column)
  12214. (1+ col)
  12215. (if (> to-col 0)
  12216. to-col
  12217. (- (abs to-col) tags-l))))
  12218. (setq p (point))
  12219. (insert (make-string (- ncol (current-column)) ?\ ))
  12220. (setq ncol (current-column))
  12221. (when indent-tabs-mode (tabify p (point-at-eol)))
  12222. (org-move-to-column (min ncol col) t))
  12223. (goto-char pos))))
  12224. (defun org-set-tags-command (&optional arg just-align)
  12225. "Call the set-tags command for the current entry."
  12226. (interactive "P")
  12227. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12228. (org-set-tags arg just-align)
  12229. (save-excursion
  12230. (org-back-to-heading t)
  12231. (org-set-tags arg just-align))))
  12232. (defun org-set-tags-to (data)
  12233. "Set the tags of the current entry to DATA, replacing the current tags.
  12234. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12235. If DATA is nil or the empty string, any tags will be removed."
  12236. (interactive "sTags: ")
  12237. (setq data
  12238. (cond
  12239. ((eq data nil) "")
  12240. ((equal data "") "")
  12241. ((stringp data)
  12242. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12243. ":"))
  12244. ((listp data)
  12245. (concat ":" (mapconcat 'identity data ":") ":"))
  12246. (t nil)))
  12247. (when data
  12248. (save-excursion
  12249. (org-back-to-heading t)
  12250. (when (looking-at org-complex-heading-regexp)
  12251. (if (match-end 5)
  12252. (progn
  12253. (goto-char (match-beginning 5))
  12254. (insert data)
  12255. (delete-region (point) (point-at-eol))
  12256. (org-set-tags nil 'align))
  12257. (goto-char (point-at-eol))
  12258. (insert " " data)
  12259. (org-set-tags nil 'align)))
  12260. (beginning-of-line 1)
  12261. (if (looking-at ".*?\\([ \t]+\\)$")
  12262. (delete-region (match-beginning 1) (match-end 1))))))
  12263. (defun org-align-all-tags ()
  12264. "Align the tags i all headings."
  12265. (interactive)
  12266. (save-excursion
  12267. (or (ignore-errors (org-back-to-heading t))
  12268. (outline-next-heading))
  12269. (if (org-at-heading-p)
  12270. (org-set-tags t)
  12271. (message "No headings"))))
  12272. (defvar org-indent-indentation-per-level)
  12273. (defun org-set-tags (&optional arg just-align)
  12274. "Set the tags for the current headline.
  12275. With prefix ARG, realign all tags in headings in the current buffer."
  12276. (interactive "P")
  12277. (let* ((re org-outline-regexp-bol)
  12278. (current (unless arg (org-get-tags-string)))
  12279. (col (current-column))
  12280. (org-setting-tags t)
  12281. table current-tags inherited-tags ; computed below when needed
  12282. tags p0 c0 c1 rpl di tc level)
  12283. (if arg
  12284. (save-excursion
  12285. (goto-char (point-min))
  12286. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12287. (while (re-search-forward re nil t)
  12288. (org-set-tags nil t)
  12289. (end-of-line 1)))
  12290. (message "All tags realigned to column %d" org-tags-column))
  12291. (if just-align
  12292. (setq tags current)
  12293. ;; Get a new set of tags from the user
  12294. (save-excursion
  12295. (setq table (append org-tag-persistent-alist
  12296. (or org-tag-alist (org-get-buffer-tags))
  12297. (and
  12298. org-complete-tags-always-offer-all-agenda-tags
  12299. (org-global-tags-completion-table
  12300. (org-agenda-files))))
  12301. org-last-tags-completion-table table
  12302. current-tags (org-split-string current ":")
  12303. inherited-tags (nreverse
  12304. (nthcdr (length current-tags)
  12305. (nreverse (org-get-tags-at))))
  12306. tags
  12307. (if (or (eq t org-use-fast-tag-selection)
  12308. (and org-use-fast-tag-selection
  12309. (delq nil (mapcar 'cdr table))))
  12310. (org-fast-tag-selection
  12311. current-tags inherited-tags table
  12312. (if org-fast-tag-selection-include-todo
  12313. org-todo-key-alist))
  12314. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12315. (org-trim
  12316. (org-icompleting-read "Tags: "
  12317. 'org-tags-completion-function
  12318. nil nil current 'org-tags-history))))))
  12319. (while (string-match "[-+&]+" tags)
  12320. ;; No boolean logic, just a list
  12321. (setq tags (replace-match ":" t t tags))))
  12322. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12323. (if org-tags-sort-function
  12324. (setq tags (mapconcat 'identity
  12325. (sort (org-split-string
  12326. tags (org-re "[^[:alnum:]_@#%]+"))
  12327. org-tags-sort-function) ":")))
  12328. (if (string-match "\\`[\t ]*\\'" tags)
  12329. (setq tags "")
  12330. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12331. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12332. ;; Insert new tags at the correct column
  12333. (beginning-of-line 1)
  12334. (setq level (or (and (looking-at org-outline-regexp)
  12335. (- (match-end 0) (point) 1))
  12336. 1))
  12337. (cond
  12338. ((and (equal current "") (equal tags "")))
  12339. ((re-search-forward
  12340. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12341. (point-at-eol) t)
  12342. (if (equal tags "")
  12343. (setq rpl "")
  12344. (goto-char (match-beginning 0))
  12345. (setq c0 (current-column)
  12346. ;; compute offset for the case of org-indent-mode active
  12347. di (if org-indent-mode
  12348. (* (1- org-indent-indentation-per-level) (1- level))
  12349. 0)
  12350. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12351. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12352. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12353. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12354. (replace-match rpl t t)
  12355. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12356. tags)
  12357. (t (error "Tags alignment failed")))
  12358. (org-move-to-column col)
  12359. (unless just-align
  12360. (run-hooks 'org-after-tags-change-hook)))))
  12361. (defun org-change-tag-in-region (beg end tag off)
  12362. "Add or remove TAG for each entry in the region.
  12363. This works in the agenda, and also in an org-mode buffer."
  12364. (interactive
  12365. (list (region-beginning) (region-end)
  12366. (let ((org-last-tags-completion-table
  12367. (if (derived-mode-p 'org-mode)
  12368. (org-get-buffer-tags)
  12369. (org-global-tags-completion-table))))
  12370. (org-icompleting-read
  12371. "Tag: " 'org-tags-completion-function nil nil nil
  12372. 'org-tags-history))
  12373. (progn
  12374. (message "[s]et or [r]emove? ")
  12375. (equal (read-char-exclusive) ?r))))
  12376. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12377. (let ((agendap (equal major-mode 'org-agenda-mode))
  12378. l1 l2 m buf pos newhead (cnt 0))
  12379. (goto-char end)
  12380. (setq l2 (1- (org-current-line)))
  12381. (goto-char beg)
  12382. (setq l1 (org-current-line))
  12383. (loop for l from l1 to l2 do
  12384. (org-goto-line l)
  12385. (setq m (get-text-property (point) 'org-hd-marker))
  12386. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12387. (and agendap m))
  12388. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12389. pos (if agendap m (point)))
  12390. (with-current-buffer buf
  12391. (save-excursion
  12392. (save-restriction
  12393. (goto-char pos)
  12394. (setq cnt (1+ cnt))
  12395. (org-toggle-tag tag (if off 'off 'on))
  12396. (setq newhead (org-get-heading)))))
  12397. (and agendap (org-agenda-change-all-lines newhead m))))
  12398. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12399. (defun org-tags-completion-function (string predicate &optional flag)
  12400. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12401. (confirm (lambda (x) (stringp (car x)))))
  12402. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12403. (setq s1 (match-string 1 string)
  12404. s2 (match-string 2 string))
  12405. (setq s1 "" s2 string))
  12406. (cond
  12407. ((eq flag nil)
  12408. ;; try completion
  12409. (setq rtn (try-completion s2 ctable confirm))
  12410. (if (stringp rtn)
  12411. (setq rtn
  12412. (concat s1 s2 (substring rtn (length s2))
  12413. (if (and org-add-colon-after-tag-completion
  12414. (assoc rtn ctable))
  12415. ":" ""))))
  12416. rtn)
  12417. ((eq flag t)
  12418. ;; all-completions
  12419. (all-completions s2 ctable confirm)
  12420. )
  12421. ((eq flag 'lambda)
  12422. ;; exact match?
  12423. (assoc s2 ctable)))
  12424. ))
  12425. (defun org-fast-tag-insert (kwd tags face &optional end)
  12426. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12427. (insert (format "%-12s" (concat kwd ":"))
  12428. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12429. (or end "")))
  12430. (defun org-fast-tag-show-exit (flag)
  12431. (save-excursion
  12432. (org-goto-line 3)
  12433. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12434. (replace-match ""))
  12435. (when flag
  12436. (end-of-line 1)
  12437. (org-move-to-column (- (window-width) 19) t)
  12438. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12439. (defun org-set-current-tags-overlay (current prefix)
  12440. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12441. (if (featurep 'xemacs)
  12442. (org-overlay-display org-tags-overlay (concat prefix s)
  12443. 'secondary-selection)
  12444. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12445. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12446. (defvar org-last-tag-selection-key nil)
  12447. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12448. "Fast tag selection with single keys.
  12449. CURRENT is the current list of tags in the headline, INHERITED is the
  12450. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12451. possibly with grouping information. TODO-TABLE is a similar table with
  12452. TODO keywords, should these have keys assigned to them.
  12453. If the keys are nil, a-z are automatically assigned.
  12454. Returns the new tags string, or nil to not change the current settings."
  12455. (let* ((fulltable (append table todo-table))
  12456. (maxlen (apply 'max (mapcar
  12457. (lambda (x)
  12458. (if (stringp (car x)) (string-width (car x)) 0))
  12459. fulltable)))
  12460. (buf (current-buffer))
  12461. (expert (eq org-fast-tag-selection-single-key 'expert))
  12462. (buffer-tags nil)
  12463. (fwidth (+ maxlen 3 1 3))
  12464. (ncol (/ (- (window-width) 4) fwidth))
  12465. (i-face 'org-done)
  12466. (c-face 'org-todo)
  12467. tg cnt e c char c1 c2 ntable tbl rtn
  12468. ov-start ov-end ov-prefix
  12469. (exit-after-next org-fast-tag-selection-single-key)
  12470. (done-keywords org-done-keywords)
  12471. groups ingroup)
  12472. (save-excursion
  12473. (beginning-of-line 1)
  12474. (if (looking-at
  12475. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12476. (setq ov-start (match-beginning 1)
  12477. ov-end (match-end 1)
  12478. ov-prefix "")
  12479. (setq ov-start (1- (point-at-eol))
  12480. ov-end (1+ ov-start))
  12481. (skip-chars-forward "^\n\r")
  12482. (setq ov-prefix
  12483. (concat
  12484. (buffer-substring (1- (point)) (point))
  12485. (if (> (current-column) org-tags-column)
  12486. " "
  12487. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12488. (move-overlay org-tags-overlay ov-start ov-end)
  12489. (save-window-excursion
  12490. (if expert
  12491. (set-buffer (get-buffer-create " *Org tags*"))
  12492. (delete-other-windows)
  12493. (split-window-vertically)
  12494. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12495. (erase-buffer)
  12496. (org-set-local 'org-done-keywords done-keywords)
  12497. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12498. (org-fast-tag-insert "Current" current c-face "\n\n")
  12499. (org-fast-tag-show-exit exit-after-next)
  12500. (org-set-current-tags-overlay current ov-prefix)
  12501. (setq tbl fulltable char ?a cnt 0)
  12502. (while (setq e (pop tbl))
  12503. (cond
  12504. ((equal (car e) :startgroup)
  12505. (push '() groups) (setq ingroup t)
  12506. (when (not (= cnt 0))
  12507. (setq cnt 0)
  12508. (insert "\n"))
  12509. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12510. ((equal (car e) :endgroup)
  12511. (setq ingroup nil cnt 0)
  12512. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12513. ((equal e '(:newline))
  12514. (when (not (= cnt 0))
  12515. (setq cnt 0)
  12516. (insert "\n")
  12517. (setq e (car tbl))
  12518. (while (equal (car tbl) '(:newline))
  12519. (insert "\n")
  12520. (setq tbl (cdr tbl)))))
  12521. (t
  12522. (setq tg (copy-sequence (car e)) c2 nil)
  12523. (if (cdr e)
  12524. (setq c (cdr e))
  12525. ;; automatically assign a character.
  12526. (setq c1 (string-to-char
  12527. (downcase (substring
  12528. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12529. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12530. (while (or (rassoc char ntable) (rassoc char table))
  12531. (setq char (1+ char)))
  12532. (setq c2 c1))
  12533. (setq c (or c2 char)))
  12534. (if ingroup (push tg (car groups)))
  12535. (setq tg (org-add-props tg nil 'face
  12536. (cond
  12537. ((not (assoc tg table))
  12538. (org-get-todo-face tg))
  12539. ((member tg current) c-face)
  12540. ((member tg inherited) i-face)
  12541. (t nil))))
  12542. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12543. (insert "[" c "] " tg (make-string
  12544. (- fwidth 4 (length tg)) ?\ ))
  12545. (push (cons tg c) ntable)
  12546. (when (= (setq cnt (1+ cnt)) ncol)
  12547. (insert "\n")
  12548. (if ingroup (insert " "))
  12549. (setq cnt 0)))))
  12550. (setq ntable (nreverse ntable))
  12551. (insert "\n")
  12552. (goto-char (point-min))
  12553. (if (not expert) (org-fit-window-to-buffer))
  12554. (setq rtn
  12555. (catch 'exit
  12556. (while t
  12557. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12558. (if (not groups) "no " "")
  12559. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12560. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12561. (setq org-last-tag-selection-key c)
  12562. (cond
  12563. ((= c ?\r) (throw 'exit t))
  12564. ((= c ?!)
  12565. (setq groups (not groups))
  12566. (goto-char (point-min))
  12567. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12568. ((= c ?\C-c)
  12569. (if (not expert)
  12570. (org-fast-tag-show-exit
  12571. (setq exit-after-next (not exit-after-next)))
  12572. (setq expert nil)
  12573. (delete-other-windows)
  12574. (set-window-buffer (split-window-vertically) " *Org tags*")
  12575. (org-switch-to-buffer-other-window " *Org tags*")
  12576. (org-fit-window-to-buffer)))
  12577. ((or (= c ?\C-g)
  12578. (and (= c ?q) (not (rassoc c ntable))))
  12579. (org-detach-overlay org-tags-overlay)
  12580. (setq quit-flag t))
  12581. ((= c ?\ )
  12582. (setq current nil)
  12583. (if exit-after-next (setq exit-after-next 'now)))
  12584. ((= c ?\t)
  12585. (condition-case nil
  12586. (setq tg (org-icompleting-read
  12587. "Tag: "
  12588. (or buffer-tags
  12589. (with-current-buffer buf
  12590. (org-get-buffer-tags)))))
  12591. (quit (setq tg "")))
  12592. (when (string-match "\\S-" tg)
  12593. (add-to-list 'buffer-tags (list tg))
  12594. (if (member tg current)
  12595. (setq current (delete tg current))
  12596. (push tg current)))
  12597. (if exit-after-next (setq exit-after-next 'now)))
  12598. ((setq e (rassoc c todo-table) tg (car e))
  12599. (with-current-buffer buf
  12600. (save-excursion (org-todo tg)))
  12601. (if exit-after-next (setq exit-after-next 'now)))
  12602. ((setq e (rassoc c ntable) tg (car e))
  12603. (if (member tg current)
  12604. (setq current (delete tg current))
  12605. (loop for g in groups do
  12606. (if (member tg g)
  12607. (mapc (lambda (x)
  12608. (setq current (delete x current)))
  12609. g)))
  12610. (push tg current))
  12611. (if exit-after-next (setq exit-after-next 'now))))
  12612. ;; Create a sorted list
  12613. (setq current
  12614. (sort current
  12615. (lambda (a b)
  12616. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12617. (if (eq exit-after-next 'now) (throw 'exit t))
  12618. (goto-char (point-min))
  12619. (beginning-of-line 2)
  12620. (delete-region (point) (point-at-eol))
  12621. (org-fast-tag-insert "Current" current c-face)
  12622. (org-set-current-tags-overlay current ov-prefix)
  12623. (while (re-search-forward
  12624. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12625. (setq tg (match-string 1))
  12626. (add-text-properties
  12627. (match-beginning 1) (match-end 1)
  12628. (list 'face
  12629. (cond
  12630. ((member tg current) c-face)
  12631. ((member tg inherited) i-face)
  12632. (t (get-text-property (match-beginning 1) 'face))))))
  12633. (goto-char (point-min)))))
  12634. (org-detach-overlay org-tags-overlay)
  12635. (if rtn
  12636. (mapconcat 'identity current ":")
  12637. nil))))
  12638. (defun org-get-tags-string ()
  12639. "Get the TAGS string in the current headline."
  12640. (unless (org-at-heading-p t)
  12641. (error "Not on a heading"))
  12642. (save-excursion
  12643. (beginning-of-line 1)
  12644. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12645. (org-match-string-no-properties 1)
  12646. "")))
  12647. (defun org-get-tags ()
  12648. "Get the list of tags specified in the current headline."
  12649. (org-split-string (org-get-tags-string) ":"))
  12650. (defun org-get-buffer-tags ()
  12651. "Get a table of all tags used in the buffer, for completion."
  12652. (let (tags)
  12653. (save-excursion
  12654. (goto-char (point-min))
  12655. (while (re-search-forward
  12656. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12657. (when (equal (char-after (point-at-bol 0)) ?*)
  12658. (mapc (lambda (x) (add-to-list 'tags x))
  12659. (org-split-string (org-match-string-no-properties 1) ":")))))
  12660. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12661. (mapcar 'list tags)))
  12662. ;;;; The mapping API
  12663. ;;;###autoload
  12664. (defun org-map-entries (func &optional match scope &rest skip)
  12665. "Call FUNC at each headline selected by MATCH in SCOPE.
  12666. FUNC is a function or a lisp form. The function will be called without
  12667. arguments, with the cursor positioned at the beginning of the headline.
  12668. The return values of all calls to the function will be collected and
  12669. returned as a list.
  12670. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12671. does not need to preserve point. After evaluation, the cursor will be
  12672. moved to the end of the line (presumably of the headline of the
  12673. processed entry) and search continues from there. Under some
  12674. circumstances, this may not produce the wanted results. For example,
  12675. if you have removed (e.g. archived) the current (sub)tree it could
  12676. mean that the next entry will be skipped entirely. In such cases, you
  12677. can specify the position from where search should continue by making
  12678. FUNC set the variable `org-map-continue-from' to the desired buffer
  12679. position.
  12680. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12681. Only headlines that are matched by this query will be considered during
  12682. the iteration. When MATCH is nil or t, all headlines will be
  12683. visited by the iteration.
  12684. SCOPE determines the scope of this command. It can be any of:
  12685. nil The current buffer, respecting the restriction if any
  12686. tree The subtree started with the entry at point
  12687. region The entries within the active region, if any
  12688. region-start-level
  12689. The entries within the active region, but only those at
  12690. the same level than the first one.
  12691. file The current buffer, without restriction
  12692. file-with-archives
  12693. The current buffer, and any archives associated with it
  12694. agenda All agenda files
  12695. agenda-with-archives
  12696. All agenda files with any archive files associated with them
  12697. \(file1 file2 ...)
  12698. If this is a list, all files in the list will be scanned
  12699. The remaining args are treated as settings for the skipping facilities of
  12700. the scanner. The following items can be given here:
  12701. archive skip trees with the archive tag.
  12702. comment skip trees with the COMMENT keyword
  12703. function or Emacs Lisp form:
  12704. will be used as value for `org-agenda-skip-function', so whenever
  12705. the function returns t, FUNC will not be called for that
  12706. entry and search will continue from the point where the
  12707. function leaves it.
  12708. If your function needs to retrieve the tags including inherited tags
  12709. at the *current* entry, you can use the value of the variable
  12710. `org-scanner-tags' which will be much faster than getting the value
  12711. with `org-get-tags-at'. If your function gets properties with
  12712. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12713. to t around the call to `org-entry-properties' to get the same speedup.
  12714. Note that if your function moves around to retrieve tags and properties at
  12715. a *different* entry, you cannot use these techniques."
  12716. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12717. (not (org-region-active-p)))
  12718. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12719. (org-agenda-skip-archived-trees (memq 'archive skip))
  12720. (org-agenda-skip-comment-trees (memq 'comment skip))
  12721. (org-agenda-skip-function
  12722. (car (org-delete-all '(comment archive) skip)))
  12723. (org-tags-match-list-sublevels t)
  12724. (start-level (eq scope 'region-start-level))
  12725. matcher file res
  12726. org-todo-keywords-for-agenda
  12727. org-done-keywords-for-agenda
  12728. org-todo-keyword-alist-for-agenda
  12729. org-drawers-for-agenda
  12730. org-tag-alist-for-agenda
  12731. todo-only)
  12732. (cond
  12733. ((eq match t) (setq matcher t))
  12734. ((eq match nil) (setq matcher t))
  12735. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12736. (save-excursion
  12737. (save-restriction
  12738. (cond ((eq scope 'tree)
  12739. (org-back-to-heading t)
  12740. (org-narrow-to-subtree)
  12741. (setq scope nil))
  12742. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12743. (org-region-active-p))
  12744. ;; If needed, set start-level to a string like "2"
  12745. (when start-level
  12746. (save-excursion
  12747. (goto-char (region-beginning))
  12748. (unless (org-at-heading-p) (outline-next-heading))
  12749. (setq start-level (org-current-level))))
  12750. (narrow-to-region (region-beginning)
  12751. (save-excursion
  12752. (goto-char (region-end))
  12753. (unless (and (bolp) (org-at-heading-p))
  12754. (outline-next-heading))
  12755. (point)))
  12756. (setq scope nil)))
  12757. (if (not scope)
  12758. (progn
  12759. (org-prepare-agenda-buffers
  12760. (list (buffer-file-name (current-buffer))))
  12761. (setq res (org-scan-tags func matcher todo-only start-level)))
  12762. ;; Get the right scope
  12763. (cond
  12764. ((and scope (listp scope) (symbolp (car scope)))
  12765. (setq scope (eval scope)))
  12766. ((eq scope 'agenda)
  12767. (setq scope (org-agenda-files t)))
  12768. ((eq scope 'agenda-with-archives)
  12769. (setq scope (org-agenda-files t))
  12770. (setq scope (org-add-archive-files scope)))
  12771. ((eq scope 'file)
  12772. (setq scope (list (buffer-file-name))))
  12773. ((eq scope 'file-with-archives)
  12774. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12775. (org-prepare-agenda-buffers scope)
  12776. (while (setq file (pop scope))
  12777. (with-current-buffer (org-find-base-buffer-visiting file)
  12778. (save-excursion
  12779. (save-restriction
  12780. (widen)
  12781. (goto-char (point-min))
  12782. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12783. res)))
  12784. ;;;; Properties
  12785. ;;; Setting and retrieving properties
  12786. (defconst org-special-properties
  12787. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12788. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12789. "The special properties valid in Org-mode.
  12790. These are properties that are not defined in the property drawer,
  12791. but in some other way.")
  12792. (defconst org-default-properties
  12793. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12794. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12795. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12796. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12797. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12798. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12799. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12800. "Some properties that are used by Org-mode for various purposes.
  12801. Being in this list makes sure that they are offered for completion.")
  12802. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12803. "Regular expression matching the first line of a property drawer.")
  12804. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12805. "Regular expression matching the last line of a property drawer.")
  12806. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12807. "Regular expression matching the first line of a property drawer.")
  12808. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12809. "Regular expression matching the first line of a property drawer.")
  12810. (defconst org-property-drawer-re
  12811. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12812. org-property-end-re "\\)\n?")
  12813. "Matches an entire property drawer.")
  12814. (defconst org-clock-drawer-re
  12815. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12816. org-property-end-re "\\)\n?")
  12817. "Matches an entire clock drawer.")
  12818. (defsubst org-re-property (property)
  12819. "Return a regexp matching PROPERTY.
  12820. Match group 1 will be set to the value "
  12821. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12822. (defun org-property-action ()
  12823. "Do an action on properties."
  12824. (interactive)
  12825. (let (c)
  12826. (org-at-property-p)
  12827. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12828. (setq c (read-char-exclusive))
  12829. (cond
  12830. ((equal c ?s)
  12831. (call-interactively 'org-set-property))
  12832. ((equal c ?d)
  12833. (call-interactively 'org-delete-property))
  12834. ((equal c ?D)
  12835. (call-interactively 'org-delete-property-globally))
  12836. ((equal c ?c)
  12837. (call-interactively 'org-compute-property-at-point))
  12838. (t (error "No such property action %c" c)))))
  12839. (defun org-set-effort (&optional value)
  12840. "Set the effort property of the current entry.
  12841. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12842. allowed value."
  12843. (interactive "P")
  12844. (if (equal value 0) (setq value 10))
  12845. (let* ((completion-ignore-case t)
  12846. (prop org-effort-property)
  12847. (cur (org-entry-get nil prop))
  12848. (allowed (org-property-get-allowed-values nil prop 'table))
  12849. (existing (mapcar 'list (org-property-values prop)))
  12850. rpl
  12851. (val (cond
  12852. ((stringp value) value)
  12853. ((and allowed (integerp value))
  12854. (or (car (nth (1- value) allowed))
  12855. (car (org-last allowed))))
  12856. (allowed
  12857. (message "Select 1-9,0, [RET%s]: %s"
  12858. (if cur (concat "=" cur) "")
  12859. (mapconcat 'car allowed " "))
  12860. (setq rpl (read-char-exclusive))
  12861. (if (equal rpl ?\r)
  12862. cur
  12863. (setq rpl (- rpl ?0))
  12864. (if (equal rpl 0) (setq rpl 10))
  12865. (if (and (> rpl 0) (<= rpl (length allowed)))
  12866. (car (nth (1- rpl) allowed))
  12867. (org-completing-read "Effort: " allowed nil))))
  12868. (t
  12869. (let (org-completion-use-ido org-completion-use-iswitchb)
  12870. (org-completing-read
  12871. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12872. (concat "[" cur "]") "")
  12873. ": ")
  12874. existing nil nil "" nil cur))))))
  12875. (unless (equal (org-entry-get nil prop) val)
  12876. (org-entry-put nil prop val))
  12877. (message "%s is now %s" prop val)))
  12878. (defun org-at-property-p ()
  12879. "Is cursor inside a property drawer?"
  12880. (save-excursion
  12881. (beginning-of-line 1)
  12882. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12883. (save-match-data ;; Used by calling procedures
  12884. (let ((p (point))
  12885. (range (unless (org-before-first-heading-p)
  12886. (org-get-property-block))))
  12887. (and range (<= (car range) p) (< p (cdr range))))))))
  12888. (defun org-get-property-block (&optional beg end force)
  12889. "Return the (beg . end) range of the body of the property drawer.
  12890. BEG and END can be beginning and end of subtree, if not given
  12891. they will be found.
  12892. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12893. (catch 'exit
  12894. (save-excursion
  12895. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12896. (end (or end (progn (outline-next-heading) (point)))))
  12897. (goto-char beg)
  12898. (if (re-search-forward org-property-start-re end t)
  12899. (setq beg (1+ (match-end 0)))
  12900. (if force
  12901. (save-excursion
  12902. (org-insert-property-drawer)
  12903. (setq end (progn (outline-next-heading) (point))))
  12904. (throw 'exit nil))
  12905. (goto-char beg)
  12906. (if (re-search-forward org-property-start-re end t)
  12907. (setq beg (1+ (match-end 0)))))
  12908. (if (re-search-forward org-property-end-re end t)
  12909. (setq end (match-beginning 0))
  12910. (or force (throw 'exit nil))
  12911. (goto-char beg)
  12912. (setq end beg)
  12913. (org-indent-line-function)
  12914. (insert ":END:\n"))
  12915. (cons beg end)))))
  12916. (defun org-entry-properties (&optional pom which specific)
  12917. "Get all properties of the entry at point-or-marker POM.
  12918. This includes the TODO keyword, the tags, time strings for deadline,
  12919. scheduled, and clocking, and any additional properties defined in the
  12920. entry. The return value is an alist, keys may occur multiple times
  12921. if the property key was used several times.
  12922. POM may also be nil, in which case the current entry is used.
  12923. If WHICH is nil or `all', get all properties. If WHICH is
  12924. `special' or `standard', only get that subclass. If WHICH
  12925. is a string only get exactly this property. SPECIFIC can be a string, the
  12926. specific property we are interested in. Specifying it can speed
  12927. things up because then unnecessary parsing is avoided."
  12928. (setq which (or which 'all))
  12929. (org-with-point-at pom
  12930. (let ((clockstr (substring org-clock-string 0 -1))
  12931. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12932. (case-fold-search nil)
  12933. beg end range props sum-props key key1 value string clocksum)
  12934. (save-excursion
  12935. (when (condition-case nil
  12936. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  12937. (error nil))
  12938. (setq beg (point))
  12939. (setq sum-props (get-text-property (point) 'org-summaries))
  12940. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12941. (outline-next-heading)
  12942. (setq end (point))
  12943. (when (memq which '(all special))
  12944. ;; Get the special properties, like TODO and tags
  12945. (goto-char beg)
  12946. (when (and (or (not specific) (string= specific "TODO"))
  12947. (looking-at org-todo-line-regexp) (match-end 2))
  12948. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12949. (when (and (or (not specific) (string= specific "PRIORITY"))
  12950. (looking-at org-priority-regexp))
  12951. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12952. (when (or (not specific) (string= specific "FILE"))
  12953. (push (cons "FILE" buffer-file-name) props))
  12954. (when (and (or (not specific) (string= specific "TAGS"))
  12955. (setq value (org-get-tags-string))
  12956. (string-match "\\S-" value))
  12957. (push (cons "TAGS" value) props))
  12958. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12959. (setq value (org-get-tags-at)))
  12960. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12961. ":"))
  12962. props))
  12963. (when (or (not specific) (string= specific "BLOCKED"))
  12964. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12965. (when (or (not specific)
  12966. (member specific
  12967. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12968. "TIMESTAMP" "TIMESTAMP_IA")))
  12969. (catch 'match
  12970. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12971. (setq key (if (match-end 1)
  12972. (substring (org-match-string-no-properties 1)
  12973. 0 -1))
  12974. string (if (equal key clockstr)
  12975. (org-no-properties
  12976. (org-trim
  12977. (buffer-substring
  12978. (match-beginning 3) (goto-char
  12979. (point-at-eol)))))
  12980. (substring (org-match-string-no-properties 3)
  12981. 1 -1)))
  12982. ;; Get the correct property name from the key. This is
  12983. ;; necessary if the user has configured time keywords.
  12984. (setq key1 (concat key ":"))
  12985. (cond
  12986. ((not key)
  12987. (setq key
  12988. (if (= (char-after (match-beginning 3)) ?\[)
  12989. "TIMESTAMP_IA" "TIMESTAMP")))
  12990. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12991. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12992. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12993. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12994. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12995. (progn
  12996. (push (cons key string) props)
  12997. ;; no need to search further if match is found
  12998. (throw 'match t))
  12999. (when (or (equal key "CLOCK") (not (assoc key props)))
  13000. (push (cons key string) props))))))
  13001. )
  13002. (when (memq which '(all standard))
  13003. ;; Get the standard properties, like :PROP: ...
  13004. (setq range (org-get-property-block beg end))
  13005. (when range
  13006. (goto-char (car range))
  13007. (while (re-search-forward
  13008. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13009. (cdr range) t)
  13010. (setq key (org-match-string-no-properties 1)
  13011. value (org-trim (or (org-match-string-no-properties 2) "")))
  13012. (unless (member key excluded)
  13013. (push (cons key (or value "")) props)))))
  13014. (if clocksum
  13015. (push (cons "CLOCKSUM"
  13016. (org-columns-number-to-string (/ (float clocksum) 60.)
  13017. 'add_times))
  13018. props))
  13019. (unless (assoc "CATEGORY" props)
  13020. (push (cons "CATEGORY" (org-get-category)) props))
  13021. (append sum-props (nreverse props)))))))
  13022. (defun org-entry-get (pom property &optional inherit literal-nil)
  13023. "Get value of PROPERTY for entry at point-or-marker POM.
  13024. If INHERIT is non-nil and the entry does not have the property,
  13025. then also check higher levels of the hierarchy.
  13026. If INHERIT is the symbol `selective', use inheritance only if the setting
  13027. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13028. If the property is present but empty, the return value is the empty string.
  13029. If the property is not present at all, nil is returned.
  13030. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13031. do not interpret it as the list atom nil. This is used for inheritance
  13032. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13033. (org-with-point-at pom
  13034. (if (and inherit (if (eq inherit 'selective)
  13035. (org-property-inherit-p property)
  13036. t))
  13037. (org-entry-get-with-inheritance property literal-nil)
  13038. (if (member property org-special-properties)
  13039. ;; We need a special property. Use `org-entry-properties' to
  13040. ;; retrieve it, but specify the wanted property
  13041. (cdr (assoc property (org-entry-properties nil 'special property)))
  13042. (let ((range (unless (org-before-first-heading-p)
  13043. (org-get-property-block)))
  13044. (props (list (or (assoc property org-file-properties)
  13045. (assoc property org-global-properties)
  13046. (assoc property org-global-properties-fixed))))
  13047. val)
  13048. (flet ((ap (key)
  13049. (when (re-search-forward
  13050. (org-re-property key) (cdr range) t)
  13051. (setq props
  13052. (org-update-property-plist
  13053. key
  13054. (if (match-end 1)
  13055. (org-match-string-no-properties 1) "")
  13056. props)))))
  13057. (when (and range (goto-char (car range)))
  13058. (ap property)
  13059. (goto-char (car range))
  13060. (while (ap (concat property "+")))
  13061. (setq val (cdr (assoc property props)))
  13062. (when val (if literal-nil val (org-not-nil val))))))))))
  13063. (defun org-property-or-variable-value (var &optional inherit)
  13064. "Check if there is a property fixing the value of VAR.
  13065. If yes, return this value. If not, return the current value of the variable."
  13066. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13067. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13068. (read prop)
  13069. (symbol-value var))))
  13070. (defun org-entry-delete (pom property)
  13071. "Delete the property PROPERTY from entry at point-or-marker POM."
  13072. (org-with-point-at pom
  13073. (if (member property org-special-properties)
  13074. nil ; cannot delete these properties.
  13075. (let ((range (org-get-property-block)))
  13076. (if (and range
  13077. (goto-char (car range))
  13078. (re-search-forward
  13079. (org-re-property property)
  13080. (cdr range) t))
  13081. (progn
  13082. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13083. t)
  13084. nil)))))
  13085. ;; Multi-values properties are properties that contain multiple values
  13086. ;; These values are assumed to be single words, separated by whitespace.
  13087. (defun org-entry-add-to-multivalued-property (pom property value)
  13088. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13089. (let* ((old (org-entry-get pom property))
  13090. (values (and old (org-split-string old "[ \t]"))))
  13091. (setq value (org-entry-protect-space value))
  13092. (unless (member value values)
  13093. (setq values (cons value values))
  13094. (org-entry-put pom property
  13095. (mapconcat 'identity values " ")))))
  13096. (defun org-entry-remove-from-multivalued-property (pom property value)
  13097. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13098. (let* ((old (org-entry-get pom property))
  13099. (values (and old (org-split-string old "[ \t]"))))
  13100. (setq value (org-entry-protect-space value))
  13101. (when (member value values)
  13102. (setq values (delete value values))
  13103. (org-entry-put pom property
  13104. (mapconcat 'identity values " ")))))
  13105. (defun org-entry-member-in-multivalued-property (pom property value)
  13106. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13107. (let* ((old (org-entry-get pom property))
  13108. (values (and old (org-split-string old "[ \t]"))))
  13109. (setq value (org-entry-protect-space value))
  13110. (member value values)))
  13111. (defun org-entry-get-multivalued-property (pom property)
  13112. "Return a list of values in a multivalued property."
  13113. (let* ((value (org-entry-get pom property))
  13114. (values (and value (org-split-string value "[ \t]"))))
  13115. (mapcar 'org-entry-restore-space values)))
  13116. (defun org-entry-put-multivalued-property (pom property &rest values)
  13117. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13118. VALUES should be a list of strings. Spaces will be protected."
  13119. (org-entry-put pom property
  13120. (mapconcat 'org-entry-protect-space values " "))
  13121. (let* ((value (org-entry-get pom property))
  13122. (values (and value (org-split-string value "[ \t]"))))
  13123. (mapcar 'org-entry-restore-space values)))
  13124. (defun org-entry-protect-space (s)
  13125. "Protect spaces and newline in string S."
  13126. (while (string-match " " s)
  13127. (setq s (replace-match "%20" t t s)))
  13128. (while (string-match "\n" s)
  13129. (setq s (replace-match "%0A" t t s)))
  13130. s)
  13131. (defun org-entry-restore-space (s)
  13132. "Restore spaces and newline in string S."
  13133. (while (string-match "%20" s)
  13134. (setq s (replace-match " " t t s)))
  13135. (while (string-match "%0A" s)
  13136. (setq s (replace-match "\n" t t s)))
  13137. s)
  13138. (defvar org-entry-property-inherited-from (make-marker)
  13139. "Marker pointing to the entry from where a property was inherited.
  13140. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13141. location of the entry where the inheritance search matched. If there was
  13142. no match, the marker will point nowhere.
  13143. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13144. is set.")
  13145. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13146. "Get entry property, and search higher levels if not present.
  13147. The search will stop at the first ancestor which has the property defined.
  13148. If the value found is \"nil\", return nil to show that the property
  13149. should be considered as undefined (this is the meaning of nil here).
  13150. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13151. (move-marker org-entry-property-inherited-from nil)
  13152. (let (tmp)
  13153. (unless (org-before-first-heading-p)
  13154. (save-excursion
  13155. (save-restriction
  13156. (widen)
  13157. (catch 'ex
  13158. (while t
  13159. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13160. (org-back-to-heading t)
  13161. (move-marker org-entry-property-inherited-from (point))
  13162. (throw 'ex tmp))
  13163. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13164. (setq tmp (or tmp
  13165. (cdr (assoc property org-file-properties))
  13166. (cdr (assoc property org-global-properties))
  13167. (cdr (assoc property org-global-properties-fixed))))
  13168. (if literal-nil tmp (org-not-nil tmp))))
  13169. (defvar org-property-changed-functions nil
  13170. "Hook called when the value of a property has changed.
  13171. Each hook function should accept two arguments, the name of the property
  13172. and the new value.")
  13173. (defun org-entry-put (pom property value)
  13174. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13175. (org-with-point-at pom
  13176. (org-back-to-heading t)
  13177. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13178. range)
  13179. (cond
  13180. ((equal property "TODO")
  13181. (when (and (stringp value) (string-match "\\S-" value)
  13182. (not (member value org-todo-keywords-1)))
  13183. (error "\"%s\" is not a valid TODO state" value))
  13184. (if (or (not value)
  13185. (not (string-match "\\S-" value)))
  13186. (setq value 'none))
  13187. (org-todo value)
  13188. (org-set-tags nil 'align))
  13189. ((equal property "PRIORITY")
  13190. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13191. (string-to-char value) ?\ ))
  13192. (org-set-tags nil 'align))
  13193. ((equal property "SCHEDULED")
  13194. (if (re-search-forward org-scheduled-time-regexp end t)
  13195. (cond
  13196. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13197. ((eq value 'later) (org-timestamp-change 1 'day))
  13198. (t (call-interactively 'org-schedule)))
  13199. (call-interactively 'org-schedule)))
  13200. ((equal property "DEADLINE")
  13201. (if (re-search-forward org-deadline-time-regexp end t)
  13202. (cond
  13203. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13204. ((eq value 'later) (org-timestamp-change 1 'day))
  13205. (t (call-interactively 'org-deadline)))
  13206. (call-interactively 'org-deadline)))
  13207. ((member property org-special-properties)
  13208. (error "The %s property can not yet be set with `org-entry-put'"
  13209. property))
  13210. (t ; a non-special property
  13211. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13212. (setq range (org-get-property-block beg end 'force))
  13213. (goto-char (car range))
  13214. (if (re-search-forward
  13215. (org-re-property property) (cdr range) t)
  13216. (progn
  13217. (delete-region (match-beginning 0) (match-end 0))
  13218. (goto-char (match-beginning 0)))
  13219. (goto-char (cdr range))
  13220. (insert "\n")
  13221. (backward-char 1)
  13222. (org-indent-line-function))
  13223. (insert ":" property ":")
  13224. (and value (insert " " value))
  13225. (org-indent-line-function)))))
  13226. (run-hook-with-args 'org-property-changed-functions property value)))
  13227. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13228. "Get all property keys in the current buffer.
  13229. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13230. like tags and TODO state.
  13231. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13232. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13233. and others.
  13234. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13235. formats in the current buffer."
  13236. (let (rtn range cfmt s p)
  13237. (save-excursion
  13238. (save-restriction
  13239. (widen)
  13240. (goto-char (point-min))
  13241. (while (re-search-forward org-property-start-re nil t)
  13242. (setq range (org-get-property-block))
  13243. (goto-char (car range))
  13244. (while (re-search-forward
  13245. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13246. (cdr range) t)
  13247. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13248. (outline-next-heading))))
  13249. (when include-specials
  13250. (setq rtn (append org-special-properties rtn)))
  13251. (when include-defaults
  13252. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13253. (add-to-list 'rtn org-effort-property))
  13254. (when include-columns
  13255. (save-excursion
  13256. (save-restriction
  13257. (widen)
  13258. (goto-char (point-min))
  13259. (while (re-search-forward
  13260. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13261. nil t)
  13262. (setq cfmt (match-string 2) s 0)
  13263. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13264. cfmt s)
  13265. (setq s (match-end 0)
  13266. p (match-string 1 cfmt))
  13267. (unless (or (equal p "ITEM")
  13268. (member p org-special-properties))
  13269. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13270. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13271. (defun org-property-values (key)
  13272. "Return a list of all values of property KEY in the current buffer."
  13273. (save-excursion
  13274. (save-restriction
  13275. (widen)
  13276. (goto-char (point-min))
  13277. (let ((re (org-re-property key))
  13278. values)
  13279. (while (re-search-forward re nil t)
  13280. (add-to-list 'values (org-trim (match-string 1))))
  13281. (delete "" values)))))
  13282. (defun org-insert-property-drawer ()
  13283. "Insert a property drawer into the current entry."
  13284. (org-back-to-heading t)
  13285. (looking-at org-outline-regexp)
  13286. (let ((indent (if org-adapt-indentation
  13287. (- (match-end 0) (match-beginning 0))
  13288. 0))
  13289. (beg (point))
  13290. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13291. end hiddenp)
  13292. (outline-next-heading)
  13293. (setq end (point))
  13294. (goto-char beg)
  13295. (while (re-search-forward re end t))
  13296. (setq hiddenp (outline-invisible-p))
  13297. (end-of-line 1)
  13298. (and (equal (char-after) ?\n) (forward-char 1))
  13299. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13300. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13301. ;; just skip this line
  13302. (beginning-of-line 2)
  13303. ;; Drawer start, find the end
  13304. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13305. (beginning-of-line 1)))
  13306. (org-skip-over-state-notes)
  13307. (skip-chars-backward " \t\n\r")
  13308. (if (eq (char-before) ?*) (forward-char 1))
  13309. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13310. (beginning-of-line 0)
  13311. (org-indent-to-column indent)
  13312. (beginning-of-line 2)
  13313. (org-indent-to-column indent)
  13314. (beginning-of-line 0)
  13315. (if hiddenp
  13316. (save-excursion
  13317. (org-back-to-heading t)
  13318. (hide-entry))
  13319. (org-flag-drawer t))))
  13320. (defun org-insert-drawer (&optional arg drawer)
  13321. "Insert a drawer at point.
  13322. Optional argument DRAWER, when non-nil, is a string representing
  13323. drawer's name. Otherwise, the user is prompted for a name.
  13324. If a region is active, insert the drawer around that region
  13325. instead.
  13326. Point is left between drawer's boundaries."
  13327. (interactive "P")
  13328. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13329. "LOGBOOK"))
  13330. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13331. ;; internally by Org. They are meant to be inserted
  13332. ;; automatically.
  13333. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13334. ;; Remove system drawers from list. Note: For some reason,
  13335. ;; `org-completing-read' ignores the predicate while
  13336. ;; `completing-read' handles it fine.
  13337. (drawer (if arg "PROPERTIES"
  13338. (or drawer
  13339. (completing-read
  13340. "Drawer: " org-drawers
  13341. (lambda (d) (not (member d system-drawers))))))))
  13342. (cond
  13343. ;; With C-u, fall back on `org-insert-property-drawer'
  13344. (arg (org-insert-property-drawer))
  13345. ;; With an active region, insert a drawer at point.
  13346. ((not (org-region-active-p))
  13347. (progn
  13348. (unless (bolp) (insert "\n"))
  13349. (insert (format ":%s:\n\n:END:\n" drawer))
  13350. (forward-line -2)))
  13351. ;; Otherwise, insert the drawer at point
  13352. (t
  13353. (let ((rbeg (region-beginning))
  13354. (rend (copy-marker (region-end))))
  13355. (unwind-protect
  13356. (progn
  13357. (goto-char rbeg)
  13358. (beginning-of-line)
  13359. (when (save-excursion
  13360. (re-search-forward org-outline-regexp-bol rend t))
  13361. (error "Drawers cannot contain headlines"))
  13362. ;; Position point at the beginning of the first
  13363. ;; non-blank line in region. Insert drawer's opening
  13364. ;; there, then indent it.
  13365. (org-skip-whitespace)
  13366. (beginning-of-line)
  13367. (insert ":" drawer ":\n")
  13368. (forward-line -1)
  13369. (indent-for-tab-command)
  13370. ;; Move point to the beginning of the first blank line
  13371. ;; after the last non-blank line in region. Insert
  13372. ;; drawer's closing, then indent it.
  13373. (goto-char rend)
  13374. (skip-chars-backward " \r\t\n")
  13375. (insert "\n:END:")
  13376. (indent-for-tab-command)
  13377. (unless (eolp) (insert "\n")))
  13378. ;; Clear marker, whatever the outcome of insertion is.
  13379. (set-marker rend nil)))))))
  13380. (defvar org-property-set-functions-alist nil
  13381. "Property set function alist.
  13382. Each entry should have the following format:
  13383. (PROPERTY . READ-FUNCTION)
  13384. The read function will be called with the same argument as
  13385. `org-completing-read'.")
  13386. (defun org-set-property-function (property)
  13387. "Get the function that should be used to set PROPERTY.
  13388. This is computed according to `org-property-set-functions-alist'."
  13389. (or (cdr (assoc property org-property-set-functions-alist))
  13390. 'org-completing-read))
  13391. (defun org-read-property-value (property)
  13392. "Read PROPERTY value from user."
  13393. (let* ((completion-ignore-case t)
  13394. (allowed (org-property-get-allowed-values nil property 'table))
  13395. (cur (org-entry-get nil property))
  13396. (prompt (concat property " value"
  13397. (if (and cur (string-match "\\S-" cur))
  13398. (concat " [" cur "]") "") ": "))
  13399. (set-function (org-set-property-function property))
  13400. (val (if allowed
  13401. (funcall set-function prompt allowed nil
  13402. (not (get-text-property 0 'org-unrestricted
  13403. (caar allowed))))
  13404. (let (org-completion-use-ido org-completion-use-iswitchb)
  13405. (funcall set-function prompt
  13406. (mapcar 'list (org-property-values property))
  13407. nil nil "" nil cur)))))
  13408. (if (equal val "")
  13409. cur
  13410. val)))
  13411. (defvar org-last-set-property nil)
  13412. (defun org-read-property-name ()
  13413. "Read a property name."
  13414. (let* ((completion-ignore-case t)
  13415. (keys (org-buffer-property-keys nil t t))
  13416. (default-prop (or (save-excursion
  13417. (save-match-data
  13418. (beginning-of-line)
  13419. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13420. (null (string= (match-string 1) "END"))
  13421. (match-string 1))))
  13422. org-last-set-property))
  13423. (property (org-icompleting-read
  13424. (concat "Property"
  13425. (if default-prop (concat " [" default-prop "]") "")
  13426. ": ")
  13427. (mapcar 'list keys)
  13428. nil nil nil nil
  13429. default-prop
  13430. )))
  13431. (if (member property keys)
  13432. property
  13433. (or (cdr (assoc (downcase property)
  13434. (mapcar (lambda (x) (cons (downcase x) x))
  13435. keys)))
  13436. property))))
  13437. (defun org-set-property (property value)
  13438. "In the current entry, set PROPERTY to VALUE.
  13439. When called interactively, this will prompt for a property name, offering
  13440. completion on existing and default properties. And then it will prompt
  13441. for a value, offering completion either on allowed values (via an inherited
  13442. xxx_ALL property) or on existing values in other instances of this property
  13443. in the current file."
  13444. (interactive (list nil nil))
  13445. (let* ((property (or property (org-read-property-name)))
  13446. (value (or value (org-read-property-value property)))
  13447. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13448. (setq org-last-set-property property)
  13449. ;; Possibly postprocess the inserted value:
  13450. (when fn (setq value (funcall fn value)))
  13451. (unless (equal (org-entry-get nil property) value)
  13452. (org-entry-put nil property value))))
  13453. (defun org-delete-property (property)
  13454. "In the current entry, delete PROPERTY."
  13455. (interactive
  13456. (let* ((completion-ignore-case t)
  13457. (prop (org-icompleting-read "Property: "
  13458. (org-entry-properties nil 'standard))))
  13459. (list prop)))
  13460. (message "Property %s %s" property
  13461. (if (org-entry-delete nil property)
  13462. "deleted"
  13463. "was not present in the entry")))
  13464. (defun org-delete-property-globally (property)
  13465. "Remove PROPERTY globally, from all entries."
  13466. (interactive
  13467. (let* ((completion-ignore-case t)
  13468. (prop (org-icompleting-read
  13469. "Globally remove property: "
  13470. (mapcar 'list (org-buffer-property-keys)))))
  13471. (list prop)))
  13472. (save-excursion
  13473. (save-restriction
  13474. (widen)
  13475. (goto-char (point-min))
  13476. (let ((cnt 0))
  13477. (while (re-search-forward
  13478. (org-re-property property)
  13479. nil t)
  13480. (setq cnt (1+ cnt))
  13481. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13482. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13483. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13484. (defun org-compute-property-at-point ()
  13485. "Compute the property at point.
  13486. This looks for an enclosing column format, extracts the operator and
  13487. then applies it to the property in the column format's scope."
  13488. (interactive)
  13489. (unless (org-at-property-p)
  13490. (error "Not at a property"))
  13491. (let ((prop (org-match-string-no-properties 2)))
  13492. (org-columns-get-format-and-top-level)
  13493. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13494. (error "No operator defined for property %s" prop))
  13495. (org-columns-compute prop)))
  13496. (defvar org-property-allowed-value-functions nil
  13497. "Hook for functions supplying allowed values for a specific property.
  13498. The functions must take a single argument, the name of the property, and
  13499. return a flat list of allowed values. If \":ETC\" is one of
  13500. the values, this means that these values are intended as defaults for
  13501. completion, but that other values should be allowed too.
  13502. The functions must return nil if they are not responsible for this
  13503. property.")
  13504. (defun org-property-get-allowed-values (pom property &optional table)
  13505. "Get allowed values for the property PROPERTY.
  13506. When TABLE is non-nil, return an alist that can directly be used for
  13507. completion."
  13508. (let (vals)
  13509. (cond
  13510. ((equal property "TODO")
  13511. (setq vals (org-with-point-at pom
  13512. (append org-todo-keywords-1 '("")))))
  13513. ((equal property "PRIORITY")
  13514. (let ((n org-lowest-priority))
  13515. (while (>= n org-highest-priority)
  13516. (push (char-to-string n) vals)
  13517. (setq n (1- n)))))
  13518. ((member property org-special-properties))
  13519. ((setq vals (run-hook-with-args-until-success
  13520. 'org-property-allowed-value-functions property)))
  13521. (t
  13522. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13523. (when (and vals (string-match "\\S-" vals))
  13524. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13525. (setq vals (mapcar (lambda (x)
  13526. (cond ((stringp x) x)
  13527. ((numberp x) (number-to-string x))
  13528. ((symbolp x) (symbol-name x))
  13529. (t "???")))
  13530. vals)))))
  13531. (when (member ":ETC" vals)
  13532. (setq vals (remove ":ETC" vals))
  13533. (org-add-props (car vals) '(org-unrestricted t)))
  13534. (if table (mapcar 'list vals) vals)))
  13535. (defun org-property-previous-allowed-value (&optional previous)
  13536. "Switch to the next allowed value for this property."
  13537. (interactive)
  13538. (org-property-next-allowed-value t))
  13539. (defun org-property-next-allowed-value (&optional previous)
  13540. "Switch to the next allowed value for this property."
  13541. (interactive)
  13542. (unless (org-at-property-p)
  13543. (error "Not at a property"))
  13544. (let* ((key (match-string 2))
  13545. (value (match-string 3))
  13546. (allowed (or (org-property-get-allowed-values (point) key)
  13547. (and (member value '("[ ]" "[-]" "[X]"))
  13548. '("[ ]" "[X]"))))
  13549. nval)
  13550. (unless allowed
  13551. (error "Allowed values for this property have not been defined"))
  13552. (if previous (setq allowed (reverse allowed)))
  13553. (if (member value allowed)
  13554. (setq nval (car (cdr (member value allowed)))))
  13555. (setq nval (or nval (car allowed)))
  13556. (if (equal nval value)
  13557. (error "Only one allowed value for this property"))
  13558. (org-at-property-p)
  13559. (replace-match (concat " :" key ": " nval) t t)
  13560. (org-indent-line-function)
  13561. (beginning-of-line 1)
  13562. (skip-chars-forward " \t")
  13563. (run-hook-with-args 'org-property-changed-functions key nval)))
  13564. (defun org-find-olp (path &optional this-buffer)
  13565. "Return a marker pointing to the entry at outline path OLP.
  13566. If anything goes wrong, throw an error.
  13567. You can wrap this call to catch the error like this:
  13568. (condition-case msg
  13569. (org-mobile-locate-entry (match-string 4))
  13570. (error (nth 1 msg)))
  13571. The return value will then be either a string with the error message,
  13572. or a marker if everything is OK.
  13573. If THIS-BUFFER is set, the outline path does not contain a file,
  13574. only headings."
  13575. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13576. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13577. (level 1)
  13578. (lmin 1)
  13579. (lmax 1)
  13580. limit re end found pos heading cnt flevel)
  13581. (unless buffer (error "File not found :%s" file))
  13582. (with-current-buffer buffer
  13583. (save-excursion
  13584. (save-restriction
  13585. (widen)
  13586. (setq limit (point-max))
  13587. (goto-char (point-min))
  13588. (while (setq heading (pop path))
  13589. (setq re (format org-complex-heading-regexp-format
  13590. (regexp-quote heading)))
  13591. (setq cnt 0 pos (point))
  13592. (while (re-search-forward re end t)
  13593. (setq level (- (match-end 1) (match-beginning 1)))
  13594. (if (and (>= level lmin) (<= level lmax))
  13595. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13596. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13597. lmax heading))
  13598. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13599. lmax heading))
  13600. (goto-char found)
  13601. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13602. (setq end (save-excursion (org-end-of-subtree t t))))
  13603. (when (org-at-heading-p)
  13604. (move-marker (make-marker) (point))))))))
  13605. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13606. "Find node HEADING in BUFFER.
  13607. Return a marker to the heading if it was found, or nil if not.
  13608. If POS-ONLY is set, return just the position instead of a marker.
  13609. The heading text must match exact, but it may have a TODO keyword,
  13610. a priority cookie and tags in the standard locations."
  13611. (with-current-buffer (or buffer (current-buffer))
  13612. (save-excursion
  13613. (save-restriction
  13614. (widen)
  13615. (goto-char (point-min))
  13616. (let (case-fold-search)
  13617. (if (re-search-forward
  13618. (format org-complex-heading-regexp-format
  13619. (regexp-quote heading)) nil t)
  13620. (if pos-only
  13621. (match-beginning 0)
  13622. (move-marker (make-marker) (match-beginning 0)))))))))
  13623. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13624. "Find Org node headline HEADING in all .org files in directory DIR.
  13625. When the target headline is found, return a marker to this location."
  13626. (let ((files (directory-files (or dir default-directory)
  13627. nil "\\`[^.#].*\\.org\\'"))
  13628. file visiting m buffer)
  13629. (catch 'found
  13630. (while (setq file (pop files))
  13631. (message "trying %s" file)
  13632. (setq visiting (org-find-base-buffer-visiting file))
  13633. (setq buffer (or visiting (find-file-noselect file)))
  13634. (setq m (org-find-exact-headline-in-buffer
  13635. heading buffer))
  13636. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13637. (and m (throw 'found m))))))
  13638. (defun org-find-entry-with-id (ident)
  13639. "Locate the entry that contains the ID property with exact value IDENT.
  13640. IDENT can be a string, a symbol or a number, this function will search for
  13641. the string representation of it.
  13642. Return the position where this entry starts, or nil if there is no such entry."
  13643. (interactive "sID: ")
  13644. (let ((id (cond
  13645. ((stringp ident) ident)
  13646. ((symbol-name ident) (symbol-name ident))
  13647. ((numberp ident) (number-to-string ident))
  13648. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13649. (case-fold-search nil))
  13650. (save-excursion
  13651. (save-restriction
  13652. (widen)
  13653. (goto-char (point-min))
  13654. (when (re-search-forward
  13655. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13656. nil t)
  13657. (org-back-to-heading t)
  13658. (point))))))
  13659. ;;;; Timestamps
  13660. (defvar org-last-changed-timestamp nil)
  13661. (defvar org-last-inserted-timestamp nil
  13662. "The last time stamp inserted with `org-insert-time-stamp'.")
  13663. (defvar org-time-was-given) ; dynamically scoped parameter
  13664. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13665. (defvar org-ts-what) ; dynamically scoped parameter
  13666. (defun org-time-stamp (arg &optional inactive)
  13667. "Prompt for a date/time and insert a time stamp.
  13668. If the user specifies a time like HH:MM, or if this command is called
  13669. with a prefix argument, the time stamp will contain date and time.
  13670. Otherwise, only the date will be included. All parts of a date not
  13671. specified by the user will be filled in from the current date/time.
  13672. So if you press just return without typing anything, the time stamp
  13673. will represent the current date/time. If there is already a timestamp
  13674. at the cursor, it will be modified."
  13675. (interactive "P")
  13676. (let* ((ts nil)
  13677. (default-time
  13678. ;; Default time is either today, or, when entering a range,
  13679. ;; the range start.
  13680. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13681. (save-excursion
  13682. (re-search-backward
  13683. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13684. (- (point) 20) t)))
  13685. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13686. (current-time)))
  13687. (default-input (and ts (org-get-compact-tod ts)))
  13688. (repeater (save-excursion
  13689. (save-match-data
  13690. (beginning-of-line)
  13691. (when (re-search-forward
  13692. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13693. (save-excursion (progn (end-of-line) (point))) t)
  13694. (match-string 0)))))
  13695. org-time-was-given org-end-time-was-given time)
  13696. (cond
  13697. ((and (org-at-timestamp-p t)
  13698. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13699. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13700. (insert "--")
  13701. (setq time (let ((this-command this-command))
  13702. (org-read-date arg 'totime nil nil
  13703. default-time default-input inactive)))
  13704. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13705. ((org-at-timestamp-p t)
  13706. (setq time (let ((this-command this-command))
  13707. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13708. (when (org-at-timestamp-p t) ; just to get the match data
  13709. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13710. (replace-match "")
  13711. (setq org-last-changed-timestamp
  13712. (org-insert-time-stamp
  13713. time (or org-time-was-given arg)
  13714. inactive nil nil (list org-end-time-was-given)))
  13715. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13716. (setq org-last-changed-timestamp
  13717. (concat (substring org-last-inserted-timestamp 0 -1)
  13718. " " repeater ">"))))
  13719. (message "Timestamp updated"))
  13720. (t
  13721. (setq time (let ((this-command this-command))
  13722. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13723. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13724. nil nil (list org-end-time-was-given))))))
  13725. ;; FIXME: can we use this for something else, like computing time differences?
  13726. (defun org-get-compact-tod (s)
  13727. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13728. (let* ((t1 (match-string 1 s))
  13729. (h1 (string-to-number (match-string 2 s)))
  13730. (m1 (string-to-number (match-string 3 s)))
  13731. (t2 (and (match-end 4) (match-string 5 s)))
  13732. (h2 (and t2 (string-to-number (match-string 6 s))))
  13733. (m2 (and t2 (string-to-number (match-string 7 s))))
  13734. dh dm)
  13735. (if (not t2)
  13736. t1
  13737. (setq dh (- h2 h1) dm (- m2 m1))
  13738. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13739. (concat t1 "+" (number-to-string dh)
  13740. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13741. (defun org-time-stamp-inactive (&optional arg)
  13742. "Insert an inactive time stamp.
  13743. An inactive time stamp is enclosed in square brackets instead of angle
  13744. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13745. does not link to the calendar and cannot be changed with the S-cursor keys.
  13746. So these are more for recording a certain time/date."
  13747. (interactive "P")
  13748. (org-time-stamp arg 'inactive))
  13749. (defvar org-date-ovl (make-overlay 1 1))
  13750. (overlay-put org-date-ovl 'face 'org-date-selected)
  13751. (org-detach-overlay org-date-ovl)
  13752. (defvar org-ans1) ; dynamically scoped parameter
  13753. (defvar org-ans2) ; dynamically scoped parameter
  13754. (defvar org-plain-time-of-day-regexp) ; defined below
  13755. (defvar org-overriding-default-time nil) ; dynamically scoped
  13756. (defvar org-read-date-overlay nil)
  13757. (defvar org-dcst nil) ; dynamically scoped
  13758. (defvar org-read-date-history nil)
  13759. (defvar org-read-date-final-answer nil)
  13760. (defvar org-read-date-analyze-futurep nil)
  13761. (defvar org-read-date-analyze-forced-year nil)
  13762. (defvar org-read-date-inactive)
  13763. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13764. default-time default-input inactive)
  13765. "Read a date, possibly a time, and make things smooth for the user.
  13766. The prompt will suggest to enter an ISO date, but you can also enter anything
  13767. which will at least partially be understood by `parse-time-string'.
  13768. Unrecognized parts of the date will default to the current day, month, year,
  13769. hour and minute. If this command is called to replace a timestamp at point,
  13770. or to enter the second timestamp of a range, the default time is taken
  13771. from the existing stamp. Furthermore, the command prefers the future,
  13772. so if you are giving a date where the year is not given, and the day-month
  13773. combination is already past in the current year, it will assume you
  13774. mean next year. For details, see the manual. A few examples:
  13775. 3-2-5 --> 2003-02-05
  13776. feb 15 --> currentyear-02-15
  13777. 2/15 --> currentyear-02-15
  13778. sep 12 9 --> 2009-09-12
  13779. 12:45 --> today 12:45
  13780. 22 sept 0:34 --> currentyear-09-22 0:34
  13781. 12 --> currentyear-currentmonth-12
  13782. Fri --> nearest Friday (today or later)
  13783. etc.
  13784. Furthermore you can specify a relative date by giving, as the *first* thing
  13785. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13786. change in days weeks, months, years.
  13787. With a single plus or minus, the date is relative to today. With a double
  13788. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13789. +4d --> four days from today
  13790. +4 --> same as above
  13791. +2w --> two weeks from today
  13792. ++5 --> five days from default date
  13793. The function understands only English month and weekday abbreviations.
  13794. While prompting, a calendar is popped up - you can also select the
  13795. date with the mouse (button 1). The calendar shows a period of three
  13796. months. To scroll it to other months, use the keys `>' and `<'.
  13797. If you don't like the calendar, turn it off with
  13798. \(setq org-read-date-popup-calendar nil)
  13799. With optional argument TO-TIME, the date will immediately be converted
  13800. to an internal time.
  13801. With an optional argument WITH-TIME, the prompt will suggest to also
  13802. insert a time. Note that when WITH-TIME is not set, you can still
  13803. enter a time, and this function will inform the calling routine about
  13804. this change. The calling routine may then choose to change the format
  13805. used to insert the time stamp into the buffer to include the time.
  13806. With optional argument FROM-STRING, read from this string instead from
  13807. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13808. the time/date that is used for everything that is not specified by the
  13809. user."
  13810. (require 'parse-time)
  13811. (let* ((org-time-stamp-rounding-minutes
  13812. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13813. (org-dcst org-display-custom-times)
  13814. (ct (org-current-time))
  13815. (org-def (or org-overriding-default-time default-time ct))
  13816. (org-defdecode (decode-time org-def))
  13817. (dummy (progn
  13818. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13819. (setcar (nthcdr 2 org-defdecode) -1)
  13820. (setcar (nthcdr 1 org-defdecode) 59)
  13821. (setq org-def (apply 'encode-time org-defdecode)
  13822. org-defdecode (decode-time org-def)))))
  13823. (calendar-frame-setup nil)
  13824. (calendar-setup nil)
  13825. (calendar-move-hook nil)
  13826. (calendar-view-diary-initially-flag nil)
  13827. (calendar-view-holidays-initially-flag nil)
  13828. (timestr (format-time-string
  13829. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13830. (prompt (concat (if prompt (concat prompt " ") "")
  13831. (format "Date+time [%s]: " timestr)))
  13832. ans (org-ans0 "") org-ans1 org-ans2 final)
  13833. (cond
  13834. (from-string (setq ans from-string))
  13835. (org-read-date-popup-calendar
  13836. (save-excursion
  13837. (save-window-excursion
  13838. (calendar)
  13839. (org-eval-in-calendar '(setq cursor-type nil) t)
  13840. (unwind-protect
  13841. (progn
  13842. (calendar-forward-day (- (time-to-days org-def)
  13843. (calendar-absolute-from-gregorian
  13844. (calendar-current-date))))
  13845. (org-eval-in-calendar nil t)
  13846. (let* ((old-map (current-local-map))
  13847. (map (copy-keymap calendar-mode-map))
  13848. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13849. (org-defkey map (kbd "RET") 'org-calendar-select)
  13850. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13851. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13852. (org-defkey minibuffer-local-map [(meta shift left)]
  13853. (lambda () (interactive)
  13854. (org-eval-in-calendar '(calendar-backward-month 1))))
  13855. (org-defkey minibuffer-local-map [(meta shift right)]
  13856. (lambda () (interactive)
  13857. (org-eval-in-calendar '(calendar-forward-month 1))))
  13858. (org-defkey minibuffer-local-map [(meta shift up)]
  13859. (lambda () (interactive)
  13860. (org-eval-in-calendar '(calendar-backward-year 1))))
  13861. (org-defkey minibuffer-local-map [(meta shift down)]
  13862. (lambda () (interactive)
  13863. (org-eval-in-calendar '(calendar-forward-year 1))))
  13864. (org-defkey minibuffer-local-map [?\e (shift left)]
  13865. (lambda () (interactive)
  13866. (org-eval-in-calendar '(calendar-backward-month 1))))
  13867. (org-defkey minibuffer-local-map [?\e (shift right)]
  13868. (lambda () (interactive)
  13869. (org-eval-in-calendar '(calendar-forward-month 1))))
  13870. (org-defkey minibuffer-local-map [?\e (shift up)]
  13871. (lambda () (interactive)
  13872. (org-eval-in-calendar '(calendar-backward-year 1))))
  13873. (org-defkey minibuffer-local-map [?\e (shift down)]
  13874. (lambda () (interactive)
  13875. (org-eval-in-calendar '(calendar-forward-year 1))))
  13876. (org-defkey minibuffer-local-map [(shift up)]
  13877. (lambda () (interactive)
  13878. (org-eval-in-calendar '(calendar-backward-week 1))))
  13879. (org-defkey minibuffer-local-map [(shift down)]
  13880. (lambda () (interactive)
  13881. (org-eval-in-calendar '(calendar-forward-week 1))))
  13882. (org-defkey minibuffer-local-map [(shift left)]
  13883. (lambda () (interactive)
  13884. (org-eval-in-calendar '(calendar-backward-day 1))))
  13885. (org-defkey minibuffer-local-map [(shift right)]
  13886. (lambda () (interactive)
  13887. (org-eval-in-calendar '(calendar-forward-day 1))))
  13888. (org-defkey minibuffer-local-map ">"
  13889. (lambda () (interactive)
  13890. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13891. (org-defkey minibuffer-local-map "<"
  13892. (lambda () (interactive)
  13893. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13894. (org-defkey minibuffer-local-map "\C-v"
  13895. (lambda () (interactive)
  13896. (org-eval-in-calendar
  13897. '(calendar-scroll-left-three-months 1))))
  13898. (org-defkey minibuffer-local-map "\M-v"
  13899. (lambda () (interactive)
  13900. (org-eval-in-calendar
  13901. '(calendar-scroll-right-three-months 1))))
  13902. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13903. (unwind-protect
  13904. (progn
  13905. (use-local-map map)
  13906. (setq org-read-date-inactive inactive)
  13907. (add-hook 'post-command-hook 'org-read-date-display)
  13908. (setq org-ans0 (read-string prompt default-input
  13909. 'org-read-date-history nil))
  13910. ;; org-ans0: from prompt
  13911. ;; org-ans1: from mouse click
  13912. ;; org-ans2: from calendar motion
  13913. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13914. (remove-hook 'post-command-hook 'org-read-date-display)
  13915. (use-local-map old-map)
  13916. (when org-read-date-overlay
  13917. (delete-overlay org-read-date-overlay)
  13918. (setq org-read-date-overlay nil)))))
  13919. (bury-buffer "*Calendar*")))))
  13920. (t ; Naked prompt only
  13921. (unwind-protect
  13922. (setq ans (read-string prompt default-input
  13923. 'org-read-date-history timestr))
  13924. (when org-read-date-overlay
  13925. (delete-overlay org-read-date-overlay)
  13926. (setq org-read-date-overlay nil)))))
  13927. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13928. (when org-read-date-analyze-forced-year
  13929. (message "Year was forced into %s"
  13930. (if org-read-date-force-compatible-dates
  13931. "compatible range (1970-2037)"
  13932. "range representable on this machine"))
  13933. (ding))
  13934. ;; One round trip to get rid of 34th of August and stuff like that....
  13935. (setq final (decode-time (apply 'encode-time final)))
  13936. (setq org-read-date-final-answer ans)
  13937. (if to-time
  13938. (apply 'encode-time final)
  13939. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13940. (format "%04d-%02d-%02d %02d:%02d"
  13941. (nth 5 final) (nth 4 final) (nth 3 final)
  13942. (nth 2 final) (nth 1 final))
  13943. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13944. (defvar org-def)
  13945. (defvar org-defdecode)
  13946. (defvar org-with-time)
  13947. (defun org-read-date-display ()
  13948. "Display the current date prompt interpretation in the minibuffer."
  13949. (when org-read-date-display-live
  13950. (when org-read-date-overlay
  13951. (delete-overlay org-read-date-overlay))
  13952. (when (minibufferp (current-buffer))
  13953. (save-excursion
  13954. (end-of-line 1)
  13955. (while (not (equal (buffer-substring
  13956. (max (point-min) (- (point) 4)) (point))
  13957. " "))
  13958. (insert " ")))
  13959. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13960. " " (or org-ans1 org-ans2)))
  13961. (org-end-time-was-given nil)
  13962. (f (org-read-date-analyze ans org-def org-defdecode))
  13963. (fmts (if org-dcst
  13964. org-time-stamp-custom-formats
  13965. org-time-stamp-formats))
  13966. (fmt (if (or org-with-time
  13967. (and (boundp 'org-time-was-given) org-time-was-given))
  13968. (cdr fmts)
  13969. (car fmts)))
  13970. (txt (format-time-string fmt (apply 'encode-time f)))
  13971. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13972. (txt (concat "=> " txt)))
  13973. (when (and org-end-time-was-given
  13974. (string-match org-plain-time-of-day-regexp txt))
  13975. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13976. org-end-time-was-given
  13977. (substring txt (match-end 0)))))
  13978. (when org-read-date-analyze-futurep
  13979. (setq txt (concat txt " (=>F)")))
  13980. (setq org-read-date-overlay
  13981. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13982. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  13983. (defun org-read-date-analyze (ans org-def org-defdecode)
  13984. "Analyze the combined answer of the date prompt."
  13985. ;; FIXME: cleanup and comment
  13986. (let ((nowdecode (decode-time (current-time)))
  13987. delta deltan deltaw deltadef year month day
  13988. hour minute second wday pm h2 m2 tl wday1
  13989. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13990. (setq org-read-date-analyze-futurep nil
  13991. org-read-date-analyze-forced-year nil)
  13992. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13993. (setq ans "+0"))
  13994. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  13995. (setq ans (replace-match "" t t ans)
  13996. deltan (car delta)
  13997. deltaw (nth 1 delta)
  13998. deltadef (nth 2 delta)))
  13999. ;; Check if there is an iso week date in there
  14000. ;; If yes, store the info and postpone interpreting it until the rest
  14001. ;; of the parsing is done
  14002. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14003. (setq iso-year (if (match-end 1)
  14004. (org-small-year-to-year
  14005. (string-to-number (match-string 1 ans))))
  14006. iso-weekday (if (match-end 3)
  14007. (string-to-number (match-string 3 ans)))
  14008. iso-week (string-to-number (match-string 2 ans)))
  14009. (setq ans (replace-match "" t t ans)))
  14010. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14011. (when (string-match
  14012. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14013. (setq year (if (match-end 2)
  14014. (string-to-number (match-string 2 ans))
  14015. (progn (setq kill-year t)
  14016. (string-to-number (format-time-string "%Y"))))
  14017. month (string-to-number (match-string 3 ans))
  14018. day (string-to-number (match-string 4 ans)))
  14019. (if (< year 100) (setq year (+ 2000 year)))
  14020. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14021. t nil ans)))
  14022. ;; Help matching dotted european dates
  14023. (when (string-match
  14024. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14025. (setq year (if (match-end 3)
  14026. (string-to-number (match-string 3 ans))
  14027. (progn (setq kill-year t)
  14028. (string-to-number (format-time-string "%Y"))))
  14029. day (string-to-number (match-string 1 ans))
  14030. month (string-to-number (match-string 2 ans))
  14031. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14032. t nil ans)))
  14033. ;; Help matching american dates, like 5/30 or 5/30/7
  14034. (when (string-match
  14035. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14036. (setq year (if (match-end 4)
  14037. (string-to-number (match-string 4 ans))
  14038. (progn (setq kill-year t)
  14039. (string-to-number (format-time-string "%Y"))))
  14040. month (string-to-number (match-string 1 ans))
  14041. day (string-to-number (match-string 2 ans)))
  14042. (if (< year 100) (setq year (+ 2000 year)))
  14043. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14044. t nil ans)))
  14045. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14046. ;; If there is a time with am/pm, and *no* time without it, we convert
  14047. ;; so that matching will be successful.
  14048. (loop for i from 1 to 2 do ; twice, for end time as well
  14049. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14050. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14051. (setq hour (string-to-number (match-string 1 ans))
  14052. minute (if (match-end 3)
  14053. (string-to-number (match-string 3 ans))
  14054. 0)
  14055. pm (equal ?p
  14056. (string-to-char (downcase (match-string 4 ans)))))
  14057. (if (and (= hour 12) (not pm))
  14058. (setq hour 0)
  14059. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14060. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14061. t t ans))))
  14062. ;; Check if a time range is given as a duration
  14063. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14064. (setq hour (string-to-number (match-string 1 ans))
  14065. h2 (+ hour (string-to-number (match-string 3 ans)))
  14066. minute (string-to-number (match-string 2 ans))
  14067. m2 (+ minute (if (match-end 5) (string-to-number
  14068. (match-string 5 ans))0)))
  14069. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14070. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14071. t t ans)))
  14072. ;; Check if there is a time range
  14073. (when (boundp 'org-end-time-was-given)
  14074. (setq org-time-was-given nil)
  14075. (when (and (string-match org-plain-time-of-day-regexp ans)
  14076. (match-end 8))
  14077. (setq org-end-time-was-given (match-string 8 ans))
  14078. (setq ans (concat (substring ans 0 (match-beginning 7))
  14079. (substring ans (match-end 7))))))
  14080. (setq tl (parse-time-string ans)
  14081. day (or (nth 3 tl) (nth 3 org-defdecode))
  14082. month (or (nth 4 tl)
  14083. (if (and org-read-date-prefer-future
  14084. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14085. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14086. (nth 4 org-defdecode)))
  14087. year (or (and (not kill-year) (nth 5 tl))
  14088. (if (and org-read-date-prefer-future
  14089. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14090. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14091. (nth 5 org-defdecode)))
  14092. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14093. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14094. second (or (nth 0 tl) 0)
  14095. wday (nth 6 tl))
  14096. (when (and (eq org-read-date-prefer-future 'time)
  14097. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14098. (equal day (nth 3 nowdecode))
  14099. (equal month (nth 4 nowdecode))
  14100. (equal year (nth 5 nowdecode))
  14101. (nth 2 tl)
  14102. (or (< (nth 2 tl) (nth 2 nowdecode))
  14103. (and (= (nth 2 tl) (nth 2 nowdecode))
  14104. (nth 1 tl)
  14105. (< (nth 1 tl) (nth 1 nowdecode)))))
  14106. (setq day (1+ day)
  14107. futurep t))
  14108. ;; Special date definitions below
  14109. (cond
  14110. (iso-week
  14111. ;; There was an iso week
  14112. (require 'cal-iso)
  14113. (setq futurep nil)
  14114. (setq year (or iso-year year)
  14115. day (or iso-weekday wday 1)
  14116. wday nil ; to make sure that the trigger below does not match
  14117. iso-date (calendar-gregorian-from-absolute
  14118. (calendar-absolute-from-iso
  14119. (list iso-week day year))))
  14120. ; FIXME: Should we also push ISO weeks into the future?
  14121. ; (when (and org-read-date-prefer-future
  14122. ; (not iso-year)
  14123. ; (< (calendar-absolute-from-gregorian iso-date)
  14124. ; (time-to-days (current-time))))
  14125. ; (setq year (1+ year)
  14126. ; iso-date (calendar-gregorian-from-absolute
  14127. ; (calendar-absolute-from-iso
  14128. ; (list iso-week day year)))))
  14129. (setq month (car iso-date)
  14130. year (nth 2 iso-date)
  14131. day (nth 1 iso-date)))
  14132. (deltan
  14133. (setq futurep nil)
  14134. (unless deltadef
  14135. (let ((now (decode-time (current-time))))
  14136. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14137. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14138. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14139. ((equal deltaw "m") (setq month (+ month deltan)))
  14140. ((equal deltaw "y") (setq year (+ year deltan)))))
  14141. ((and wday (not (nth 3 tl)))
  14142. (setq futurep nil)
  14143. ;; Weekday was given, but no day, so pick that day in the week
  14144. ;; on or after the derived date.
  14145. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14146. (unless (equal wday wday1)
  14147. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14148. (if (and (boundp 'org-time-was-given)
  14149. (nth 2 tl))
  14150. (setq org-time-was-given t))
  14151. (if (< year 100) (setq year (+ 2000 year)))
  14152. ;; Check of the date is representable
  14153. (if org-read-date-force-compatible-dates
  14154. (progn
  14155. (if (< year 1970)
  14156. (setq year 1970 org-read-date-analyze-forced-year t))
  14157. (if (> year 2037)
  14158. (setq year 2037 org-read-date-analyze-forced-year t)))
  14159. (condition-case nil
  14160. (ignore (encode-time second minute hour day month year))
  14161. (error
  14162. (setq year (nth 5 org-defdecode))
  14163. (setq org-read-date-analyze-forced-year t))))
  14164. (setq org-read-date-analyze-futurep futurep)
  14165. (list second minute hour day month year)))
  14166. (defvar parse-time-weekdays)
  14167. (defun org-read-date-get-relative (s today default)
  14168. "Check string S for special relative date string.
  14169. TODAY and DEFAULT are internal times, for today and for a default.
  14170. Return shift list (N what def-flag)
  14171. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14172. N is the number of WHATs to shift.
  14173. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14174. the DEFAULT date rather than TODAY."
  14175. (require 'parse-time)
  14176. (when (and
  14177. (string-match
  14178. (concat
  14179. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14180. "\\([0-9]+\\)?"
  14181. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14182. "\\([ \t]\\|$\\)") s)
  14183. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14184. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14185. (string-to-char (substring (match-string 1 s) -1))
  14186. ?+))
  14187. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14188. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14189. (what (if (match-end 3) (match-string 3 s) "d"))
  14190. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14191. (date (if rel default today))
  14192. (wday (nth 6 (decode-time date)))
  14193. delta)
  14194. (if wday1
  14195. (progn
  14196. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14197. (if (= dir ?-) (setq delta (- delta 7)))
  14198. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14199. (list delta "d" rel))
  14200. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14201. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14202. "Turn a user-specified date into the internal representation.
  14203. The internal representation needed by the calendar is (month day year).
  14204. This is a wrapper to handle the brain-dead convention in calendar that
  14205. user function argument order change dependent on argument order."
  14206. (if (boundp 'calendar-date-style)
  14207. (cond
  14208. ((eq calendar-date-style 'american)
  14209. (list arg1 arg2 arg3))
  14210. ((eq calendar-date-style 'european)
  14211. (list arg2 arg1 arg3))
  14212. ((eq calendar-date-style 'iso)
  14213. (list arg2 arg3 arg1)))
  14214. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14215. (if (org-bound-and-true-p european-calendar-style)
  14216. (list arg2 arg1 arg3)
  14217. (list arg1 arg2 arg3)))))
  14218. (defun org-eval-in-calendar (form &optional keepdate)
  14219. "Eval FORM in the calendar window and return to current window.
  14220. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14221. otherwise stick to the current value of `org-ans2'."
  14222. (let ((sf (selected-frame))
  14223. (sw (selected-window)))
  14224. (select-window (get-buffer-window "*Calendar*" t))
  14225. (eval form)
  14226. (when (and (not keepdate) (calendar-cursor-to-date))
  14227. (let* ((date (calendar-cursor-to-date))
  14228. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14229. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14230. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14231. (select-window sw)
  14232. (org-select-frame-set-input-focus sf)))
  14233. (defun org-calendar-select ()
  14234. "Return to `org-read-date' with the date currently selected.
  14235. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14236. (interactive)
  14237. (when (calendar-cursor-to-date)
  14238. (let* ((date (calendar-cursor-to-date))
  14239. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14240. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14241. (if (active-minibuffer-window) (exit-minibuffer))))
  14242. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14243. "Insert a date stamp for the date given by the internal TIME.
  14244. WITH-HM means use the stamp format that includes the time of the day.
  14245. INACTIVE means use square brackets instead of angular ones, so that the
  14246. stamp will not contribute to the agenda.
  14247. PRE and POST are optional strings to be inserted before and after the
  14248. stamp.
  14249. The command returns the inserted time stamp."
  14250. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14251. stamp)
  14252. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14253. (insert-before-markers (or pre ""))
  14254. (when (listp extra)
  14255. (setq extra (car extra))
  14256. (if (and (stringp extra)
  14257. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14258. (setq extra (format "-%02d:%02d"
  14259. (string-to-number (match-string 1 extra))
  14260. (string-to-number (match-string 2 extra))))
  14261. (setq extra nil)))
  14262. (when extra
  14263. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14264. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14265. (insert-before-markers (or post ""))
  14266. (setq org-last-inserted-timestamp stamp)))
  14267. (defun org-toggle-time-stamp-overlays ()
  14268. "Toggle the use of custom time stamp formats."
  14269. (interactive)
  14270. (setq org-display-custom-times (not org-display-custom-times))
  14271. (unless org-display-custom-times
  14272. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14273. (while (setq p (next-single-property-change p 'display))
  14274. (if (and (get-text-property p 'display)
  14275. (eq (get-text-property p 'face) 'org-date))
  14276. (remove-text-properties
  14277. p (setq p (next-single-property-change p 'display))
  14278. '(display t))))
  14279. (set-buffer-modified-p bmp)))
  14280. (if (featurep 'xemacs)
  14281. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14282. (org-restart-font-lock)
  14283. (setq org-table-may-need-update t)
  14284. (if org-display-custom-times
  14285. (message "Time stamps are overlaid with custom format")
  14286. (message "Time stamp overlays removed")))
  14287. (defun org-display-custom-time (beg end)
  14288. "Overlay modified time stamp format over timestamp between BEG and END."
  14289. (let* ((ts (buffer-substring beg end))
  14290. t1 w1 with-hm tf time str w2 (off 0))
  14291. (save-match-data
  14292. (setq t1 (org-parse-time-string ts t))
  14293. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14294. (setq off (- (match-end 0) (match-beginning 0)))))
  14295. (setq end (- end off))
  14296. (setq w1 (- end beg)
  14297. with-hm (and (nth 1 t1) (nth 2 t1))
  14298. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14299. time (org-fix-decoded-time t1)
  14300. str (org-add-props
  14301. (format-time-string
  14302. (substring tf 1 -1) (apply 'encode-time time))
  14303. nil 'mouse-face 'highlight)
  14304. w2 (length str))
  14305. (if (not (= w2 w1))
  14306. (add-text-properties (1+ beg) (+ 2 beg)
  14307. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14308. (if (featurep 'xemacs)
  14309. (progn
  14310. (put-text-property beg end 'invisible t)
  14311. (put-text-property beg end 'end-glyph (make-glyph str)))
  14312. (put-text-property beg end 'display str))))
  14313. (defun org-translate-time (string)
  14314. "Translate all timestamps in STRING to custom format.
  14315. But do this only if the variable `org-display-custom-times' is set."
  14316. (when org-display-custom-times
  14317. (save-match-data
  14318. (let* ((start 0)
  14319. (re org-ts-regexp-both)
  14320. t1 with-hm inactive tf time str beg end)
  14321. (while (setq start (string-match re string start))
  14322. (setq beg (match-beginning 0)
  14323. end (match-end 0)
  14324. t1 (save-match-data
  14325. (org-parse-time-string (substring string beg end) t))
  14326. with-hm (and (nth 1 t1) (nth 2 t1))
  14327. inactive (equal (substring string beg (1+ beg)) "[")
  14328. tf (funcall (if with-hm 'cdr 'car)
  14329. org-time-stamp-custom-formats)
  14330. time (org-fix-decoded-time t1)
  14331. str (format-time-string
  14332. (concat
  14333. (if inactive "[" "<") (substring tf 1 -1)
  14334. (if inactive "]" ">"))
  14335. (apply 'encode-time time))
  14336. string (replace-match str t t string)
  14337. start (+ start (length str)))))))
  14338. string)
  14339. (defun org-fix-decoded-time (time)
  14340. "Set 0 instead of nil for the first 6 elements of time.
  14341. Don't touch the rest."
  14342. (let ((n 0))
  14343. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14344. (defun org-days-to-time (timestamp-string)
  14345. "Difference between TIMESTAMP-STRING and now in days."
  14346. (- (time-to-days (org-time-string-to-time timestamp-string))
  14347. (time-to-days (current-time))))
  14348. (defun org-deadline-close (timestamp-string &optional ndays)
  14349. "Is the time in TIMESTAMP-STRING close to the current date?"
  14350. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14351. (and (< (org-days-to-time timestamp-string) ndays)
  14352. (not (org-entry-is-done-p))))
  14353. (defun org-get-wdays (ts)
  14354. "Get the deadline lead time appropriate for timestring TS."
  14355. (cond
  14356. ((<= org-deadline-warning-days 0)
  14357. ;; 0 or negative, enforce this value no matter what
  14358. (- org-deadline-warning-days))
  14359. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14360. ;; lead time is specified.
  14361. (floor (* (string-to-number (match-string 1 ts))
  14362. (cdr (assoc (match-string 2 ts)
  14363. '(("d" . 1) ("w" . 7)
  14364. ("m" . 30.4) ("y" . 365.25)))))))
  14365. ;; go for the default.
  14366. (t org-deadline-warning-days)))
  14367. (defun org-calendar-select-mouse (ev)
  14368. "Return to `org-read-date' with the date currently selected.
  14369. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14370. (interactive "e")
  14371. (mouse-set-point ev)
  14372. (when (calendar-cursor-to-date)
  14373. (let* ((date (calendar-cursor-to-date))
  14374. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14375. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14376. (if (active-minibuffer-window) (exit-minibuffer))))
  14377. (defun org-check-deadlines (ndays)
  14378. "Check if there are any deadlines due or past due.
  14379. A deadline is considered due if it happens within `org-deadline-warning-days'
  14380. days from today's date. If the deadline appears in an entry marked DONE,
  14381. it is not shown. The prefix arg NDAYS can be used to test that many
  14382. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14383. (interactive "P")
  14384. (let* ((org-warn-days
  14385. (cond
  14386. ((equal ndays '(4)) 100000)
  14387. (ndays (prefix-numeric-value ndays))
  14388. (t (abs org-deadline-warning-days))))
  14389. (case-fold-search nil)
  14390. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14391. (callback
  14392. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14393. (message "%d deadlines past-due or due within %d days"
  14394. (org-occur regexp nil callback)
  14395. org-warn-days)))
  14396. (defun org-check-before-date (date)
  14397. "Check if there are deadlines or scheduled entries before DATE."
  14398. (interactive (list (org-read-date)))
  14399. (let ((case-fold-search nil)
  14400. (regexp (concat "\\<\\(" org-deadline-string
  14401. "\\|" org-scheduled-string
  14402. "\\) *<\\([^>]+\\)>"))
  14403. (callback
  14404. (lambda () (time-less-p
  14405. (org-time-string-to-time (match-string 2))
  14406. (org-time-string-to-time date)))))
  14407. (message "%d entries before %s"
  14408. (org-occur regexp nil callback) date)))
  14409. (defun org-check-after-date (date)
  14410. "Check if there are deadlines or scheduled entries after DATE."
  14411. (interactive (list (org-read-date)))
  14412. (let ((case-fold-search nil)
  14413. (regexp (concat "\\<\\(" org-deadline-string
  14414. "\\|" org-scheduled-string
  14415. "\\) *<\\([^>]+\\)>"))
  14416. (callback
  14417. (lambda () (not
  14418. (time-less-p
  14419. (org-time-string-to-time (match-string 2))
  14420. (org-time-string-to-time date))))))
  14421. (message "%d entries after %s"
  14422. (org-occur regexp nil callback) date)))
  14423. (defun org-check-dates-range (start-date end-date)
  14424. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14425. (interactive (list (org-read-date nil nil nil "Range starts")
  14426. (org-read-date nil nil nil "Range end")))
  14427. (let ((case-fold-search nil)
  14428. (regexp (concat "\\<\\(" org-deadline-string
  14429. "\\|" org-scheduled-string
  14430. "\\) *<\\([^>]+\\)>"))
  14431. (callback
  14432. (lambda ()
  14433. (let ((match (match-string 2)))
  14434. (and
  14435. (not (time-less-p
  14436. (org-time-string-to-time match)
  14437. (org-time-string-to-time start-date)))
  14438. (time-less-p
  14439. (org-time-string-to-time match)
  14440. (org-time-string-to-time end-date)))))))
  14441. (message "%d entries between %s and %s"
  14442. (org-occur regexp nil callback) start-date end-date)))
  14443. (defun org-evaluate-time-range (&optional to-buffer)
  14444. "Evaluate a time range by computing the difference between start and end.
  14445. Normally the result is just printed in the echo area, but with prefix arg
  14446. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14447. If the time range is actually in a table, the result is inserted into the
  14448. next column.
  14449. For time difference computation, a year is assumed to be exactly 365
  14450. days in order to avoid rounding problems."
  14451. (interactive "P")
  14452. (or
  14453. (org-clock-update-time-maybe)
  14454. (save-excursion
  14455. (unless (org-at-date-range-p t)
  14456. (goto-char (point-at-bol))
  14457. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14458. (if (not (org-at-date-range-p t))
  14459. (error "Not at a time-stamp range, and none found in current line")))
  14460. (let* ((ts1 (match-string 1))
  14461. (ts2 (match-string 2))
  14462. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14463. (match-end (match-end 0))
  14464. (time1 (org-time-string-to-time ts1))
  14465. (time2 (org-time-string-to-time ts2))
  14466. (t1 (org-float-time time1))
  14467. (t2 (org-float-time time2))
  14468. (diff (abs (- t2 t1)))
  14469. (negative (< (- t2 t1) 0))
  14470. ;; (ys (floor (* 365 24 60 60)))
  14471. (ds (* 24 60 60))
  14472. (hs (* 60 60))
  14473. (fy "%dy %dd %02d:%02d")
  14474. (fy1 "%dy %dd")
  14475. (fd "%dd %02d:%02d")
  14476. (fd1 "%dd")
  14477. (fh "%02d:%02d")
  14478. y d h m align)
  14479. (if havetime
  14480. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14481. y 0
  14482. d (floor (/ diff ds)) diff (mod diff ds)
  14483. h (floor (/ diff hs)) diff (mod diff hs)
  14484. m (floor (/ diff 60)))
  14485. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14486. y 0
  14487. d (floor (+ (/ diff ds) 0.5))
  14488. h 0 m 0))
  14489. (if (not to-buffer)
  14490. (message "%s" (org-make-tdiff-string y d h m))
  14491. (if (org-at-table-p)
  14492. (progn
  14493. (goto-char match-end)
  14494. (setq align t)
  14495. (and (looking-at " *|") (goto-char (match-end 0))))
  14496. (goto-char match-end))
  14497. (if (looking-at
  14498. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14499. (replace-match ""))
  14500. (if negative (insert " -"))
  14501. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14502. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14503. (insert " " (format fh h m))))
  14504. (if align (org-table-align))
  14505. (message "Time difference inserted")))))
  14506. (defun org-make-tdiff-string (y d h m)
  14507. (let ((fmt "")
  14508. (l nil))
  14509. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14510. l (push y l)))
  14511. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14512. l (push d l)))
  14513. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14514. l (push h l)))
  14515. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14516. l (push m l)))
  14517. (apply 'format fmt (nreverse l))))
  14518. (defun org-time-string-to-time (s &optional buffer pos)
  14519. (condition-case errdata
  14520. (apply 'encode-time (org-parse-time-string s))
  14521. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14522. s (if (not (and buffer pos))
  14523. ""
  14524. (format " at %d in buffer `%s'" pos buffer))
  14525. (cdr errdata)))))
  14526. (defun org-time-string-to-seconds (s)
  14527. (org-float-time (org-time-string-to-time s)))
  14528. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14529. "Convert a time stamp to an absolute day number.
  14530. If there is a specifier for a cyclic time stamp, get the closest date to
  14531. DAYNR.
  14532. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14533. The variable date is bound by the calendar when this is called."
  14534. (cond
  14535. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14536. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14537. daynr
  14538. (+ daynr 1000)))
  14539. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14540. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14541. (time-to-days (current-time))) (match-string 0 s)
  14542. prefer show-all))
  14543. (t (time-to-days
  14544. (condition-case errdata
  14545. (apply 'encode-time (org-parse-time-string s))
  14546. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14547. s (if (not (and buffer pos))
  14548. ""
  14549. (format " at %d in buffer `%s'" pos buffer))
  14550. (cdr errdata))))))))
  14551. (defun org-days-to-iso-week (days)
  14552. "Return the iso week number."
  14553. (require 'cal-iso)
  14554. (car (calendar-iso-from-absolute days)))
  14555. (defun org-small-year-to-year (year)
  14556. "Convert 2-digit years into 4-digit years.
  14557. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14558. The year 2000 cannot be abbreviated. Any year larger than 99
  14559. is returned unchanged."
  14560. (if (< year 38)
  14561. (setq year (+ 2000 year))
  14562. (if (< year 100)
  14563. (setq year (+ 1900 year))))
  14564. year)
  14565. (defun org-time-from-absolute (d)
  14566. "Return the time corresponding to date D.
  14567. D may be an absolute day number, or a calendar-type list (month day year)."
  14568. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14569. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14570. (defun org-calendar-holiday ()
  14571. "List of holidays, for Diary display in Org-mode."
  14572. (require 'holidays)
  14573. (let ((hl (funcall
  14574. (if (fboundp 'calendar-check-holidays)
  14575. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14576. (if hl (mapconcat 'identity hl "; "))))
  14577. (defun org-diary-sexp-entry (sexp entry date)
  14578. "Process a SEXP diary ENTRY for DATE."
  14579. (require 'diary-lib)
  14580. (let ((result (if calendar-debug-sexp
  14581. (let ((stack-trace-on-error t))
  14582. (eval (car (read-from-string sexp))))
  14583. (condition-case nil
  14584. (eval (car (read-from-string sexp)))
  14585. (error
  14586. (beep)
  14587. (message "Bad sexp at line %d in %s: %s"
  14588. (org-current-line)
  14589. (buffer-file-name) sexp)
  14590. (sleep-for 2))))))
  14591. (cond ((stringp result) (split-string result "; "))
  14592. ((and (consp result)
  14593. (not (consp (cdr result)))
  14594. (stringp (cdr result))) (cdr result))
  14595. ((and (consp result)
  14596. (stringp (car result))) result)
  14597. (result entry)
  14598. (t nil))))
  14599. (defun org-diary-to-ical-string (frombuf)
  14600. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14601. This uses the icalendar.el library."
  14602. (let* ((tmpdir (if (featurep 'xemacs)
  14603. (temp-directory)
  14604. temporary-file-directory))
  14605. (tmpfile (make-temp-name
  14606. (expand-file-name "orgics" tmpdir)))
  14607. buf rtn b e)
  14608. (with-current-buffer frombuf
  14609. (icalendar-export-region (point-min) (point-max) tmpfile)
  14610. (setq buf (find-buffer-visiting tmpfile))
  14611. (set-buffer buf)
  14612. (goto-char (point-min))
  14613. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14614. (setq b (match-beginning 0)))
  14615. (goto-char (point-max))
  14616. (if (re-search-backward "^END:VEVENT" nil t)
  14617. (setq e (match-end 0)))
  14618. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14619. (kill-buffer buf)
  14620. (delete-file tmpfile)
  14621. rtn))
  14622. (defun org-closest-date (start current change prefer show-all)
  14623. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14624. When PREFER is `past', return a date that is either CURRENT or past.
  14625. When PREFER is `future', return a date that is either CURRENT or future.
  14626. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14627. ;; Make the proper lists from the dates
  14628. (catch 'exit
  14629. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14630. dn dw sday cday n1 n2 n0
  14631. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14632. (setq start (org-date-to-gregorian start)
  14633. current (org-date-to-gregorian
  14634. (if show-all
  14635. current
  14636. (time-to-days (current-time))))
  14637. sday (calendar-absolute-from-gregorian start)
  14638. cday (calendar-absolute-from-gregorian current))
  14639. (if (<= cday sday) (throw 'exit sday))
  14640. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14641. (setq dn (string-to-number (match-string 1 change))
  14642. dw (cdr (assoc (match-string 2 change) a1)))
  14643. (error "Invalid change specifier: %s" change))
  14644. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14645. (cond
  14646. ((eq dw 'day)
  14647. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14648. n2 (+ n1 dn)))
  14649. ((eq dw 'year)
  14650. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14651. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14652. (setq date1 (list m d y1)
  14653. n1 (calendar-absolute-from-gregorian date1)
  14654. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14655. n2 (calendar-absolute-from-gregorian date2)))
  14656. ((eq dw 'month)
  14657. ;; approx number of month between the two dates
  14658. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14659. ;; How often does dn fit in there?
  14660. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14661. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14662. m (+ m nm)
  14663. ny (floor (/ m 12))
  14664. y (+ y ny)
  14665. m (- m (* ny 12)))
  14666. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14667. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14668. (setq m2 (+ m dn) y2 y)
  14669. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14670. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14671. (while (<= n2 cday)
  14672. (setq n1 n2 m m2 y y2)
  14673. (setq m2 (+ m dn) y2 y)
  14674. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14675. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14676. ;; Make sure n1 is the earlier date
  14677. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14678. (if show-all
  14679. (cond
  14680. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14681. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14682. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14683. (cond
  14684. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14685. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14686. (t (if (= cday n1) n1 n2)))))))
  14687. (defun org-date-to-gregorian (date)
  14688. "Turn any specification of DATE into a Gregorian date for the calendar."
  14689. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14690. ((and (listp date) (= (length date) 3)) date)
  14691. ((stringp date)
  14692. (setq date (org-parse-time-string date))
  14693. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14694. ((listp date)
  14695. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14696. (defun org-parse-time-string (s &optional nodefault)
  14697. "Parse the standard Org-mode time string.
  14698. This should be a lot faster than the normal `parse-time-string'.
  14699. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14700. hour and minute fields will be nil if not given."
  14701. (if (string-match org-ts-regexp0 s)
  14702. (list 0
  14703. (if (or (match-beginning 8) (not nodefault))
  14704. (string-to-number (or (match-string 8 s) "0")))
  14705. (if (or (match-beginning 7) (not nodefault))
  14706. (string-to-number (or (match-string 7 s) "0")))
  14707. (string-to-number (match-string 4 s))
  14708. (string-to-number (match-string 3 s))
  14709. (string-to-number (match-string 2 s))
  14710. nil nil nil)
  14711. (error "Not a standard Org-mode time string: %s" s)))
  14712. (defun org-timestamp-up (&optional arg)
  14713. "Increase the date item at the cursor by one.
  14714. If the cursor is on the year, change the year. If it is on the month,
  14715. the day or the time, change that.
  14716. With prefix ARG, change by that many units."
  14717. (interactive "p")
  14718. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14719. (defun org-timestamp-down (&optional arg)
  14720. "Decrease the date item at the cursor by one.
  14721. If the cursor is on the year, change the year. If it is on the month,
  14722. the day or the time, change that.
  14723. With prefix ARG, change by that many units."
  14724. (interactive "p")
  14725. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14726. (defun org-timestamp-up-day (&optional arg)
  14727. "Increase the date in the time stamp by one day.
  14728. With prefix ARG, change that many days."
  14729. (interactive "p")
  14730. (if (and (not (org-at-timestamp-p t))
  14731. (org-at-heading-p))
  14732. (org-todo 'up)
  14733. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14734. (defun org-timestamp-down-day (&optional arg)
  14735. "Decrease the date in the time stamp by one day.
  14736. With prefix ARG, change that many days."
  14737. (interactive "p")
  14738. (if (and (not (org-at-timestamp-p t))
  14739. (org-at-heading-p))
  14740. (org-todo 'down)
  14741. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14742. (defun org-at-timestamp-p (&optional inactive-ok)
  14743. "Determine if the cursor is in or at a timestamp."
  14744. (interactive)
  14745. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14746. (pos (point))
  14747. (ans (or (looking-at tsr)
  14748. (save-excursion
  14749. (skip-chars-backward "^[<\n\r\t")
  14750. (if (> (point) (point-min)) (backward-char 1))
  14751. (and (looking-at tsr)
  14752. (> (- (match-end 0) pos) -1))))))
  14753. (and ans
  14754. (boundp 'org-ts-what)
  14755. (setq org-ts-what
  14756. (cond
  14757. ((= pos (match-beginning 0)) 'bracket)
  14758. ;; Point is considered to be "on the bracket" whether
  14759. ;; it's really on it or right after it.
  14760. ((or (= pos (1- (match-end 0)))
  14761. (= pos (match-end 0))) 'bracket)
  14762. ((org-pos-in-match-range pos 2) 'year)
  14763. ((org-pos-in-match-range pos 3) 'month)
  14764. ((org-pos-in-match-range pos 7) 'hour)
  14765. ((org-pos-in-match-range pos 8) 'minute)
  14766. ((or (org-pos-in-match-range pos 4)
  14767. (org-pos-in-match-range pos 5)) 'day)
  14768. ((and (> pos (or (match-end 8) (match-end 5)))
  14769. (< pos (match-end 0)))
  14770. (- pos (or (match-end 8) (match-end 5))))
  14771. (t 'day))))
  14772. ans))
  14773. (defun org-toggle-timestamp-type ()
  14774. "Toggle the type (<active> or [inactive]) of a time stamp."
  14775. (interactive)
  14776. (when (org-at-timestamp-p t)
  14777. (let ((beg (match-beginning 0)) (end (match-end 0))
  14778. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14779. (save-excursion
  14780. (goto-char beg)
  14781. (while (re-search-forward "[][<>]" end t)
  14782. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14783. t t)))
  14784. (message "Timestamp is now %sactive"
  14785. (if (equal (char-after beg) ?<) "" "in")))))
  14786. (defun org-timestamp-change (n &optional what updown)
  14787. "Change the date in the time stamp at point.
  14788. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14789. `year', `minute', `second'. If WHAT is not given, the cursor position
  14790. in the timestamp determines what will be changed."
  14791. (let ((origin (point)) origin-cat
  14792. with-hm inactive
  14793. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14794. org-ts-what
  14795. extra rem
  14796. ts time time0)
  14797. (if (not (org-at-timestamp-p t))
  14798. (error "Not at a timestamp"))
  14799. (if (and (not what) (eq org-ts-what 'bracket))
  14800. (org-toggle-timestamp-type)
  14801. ;; Point isn't on brackets. Remember the part of the time-stamp
  14802. ;; the point was in. Indeed, size of time-stamps may change,
  14803. ;; but point must be kept in the same category nonetheless.
  14804. (setq origin-cat org-ts-what)
  14805. (if (and (not what) (not (eq org-ts-what 'day))
  14806. org-display-custom-times
  14807. (get-text-property (point) 'display)
  14808. (not (get-text-property (1- (point)) 'display)))
  14809. (setq org-ts-what 'day))
  14810. (setq org-ts-what (or what org-ts-what)
  14811. inactive (= (char-after (match-beginning 0)) ?\[)
  14812. ts (match-string 0))
  14813. (replace-match "")
  14814. (if (string-match
  14815. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  14816. ts)
  14817. (setq extra (match-string 1 ts)))
  14818. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14819. (setq with-hm t))
  14820. (setq time0 (org-parse-time-string ts))
  14821. (when (and updown
  14822. (eq org-ts-what 'minute)
  14823. (not current-prefix-arg))
  14824. ;; This looks like s-up and s-down. Change by one rounding step.
  14825. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14826. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14827. (setcar (cdr time0) (+ (nth 1 time0)
  14828. (if (> n 0) (- rem) (- dm rem))))))
  14829. (setq time
  14830. (encode-time (or (car time0) 0)
  14831. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14832. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14833. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14834. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14835. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14836. (nthcdr 6 time0)))
  14837. (when (and (member org-ts-what '(hour minute))
  14838. extra
  14839. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14840. (setq extra (org-modify-ts-extra
  14841. extra
  14842. (if (eq org-ts-what 'hour) 2 5)
  14843. n dm)))
  14844. (when (integerp org-ts-what)
  14845. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14846. (if (eq what 'calendar)
  14847. (let ((cal-date (org-get-date-from-calendar)))
  14848. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14849. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14850. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14851. (setcar time0 (or (car time0) 0))
  14852. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14853. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14854. (setq time (apply 'encode-time time0))))
  14855. ;; Insert the new time-stamp, and ensure point stays in the same
  14856. ;; category as before (i.e. not after the last position in that
  14857. ;; category).
  14858. (let ((pos (point)))
  14859. ;; Stay before inserted string. `save-excursion' is of no use.
  14860. (setq org-last-changed-timestamp
  14861. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14862. (goto-char pos))
  14863. (save-match-data
  14864. (looking-at org-ts-regexp3)
  14865. (goto-char (cond
  14866. ;; `day' category ends before `hour' if any, or at
  14867. ;; the end of the day name.
  14868. ((eq origin-cat 'day)
  14869. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14870. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14871. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14872. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14873. ;; `year' and `month' have both fixed size: point
  14874. ;; couldn't have moved into another part.
  14875. (t origin))))
  14876. ;; Update clock if on a CLOCK line.
  14877. (org-clock-update-time-maybe)
  14878. ;; Try to recenter the calendar window, if any.
  14879. (if (and org-calendar-follow-timestamp-change
  14880. (get-buffer-window "*Calendar*" t)
  14881. (memq org-ts-what '(day month year)))
  14882. (org-recenter-calendar (time-to-days time))))))
  14883. (defun org-modify-ts-extra (s pos n dm)
  14884. "Change the different parts of the lead-time and repeat fields in timestamp."
  14885. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14886. ng h m new rem)
  14887. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14888. (cond
  14889. ((or (org-pos-in-match-range pos 2)
  14890. (org-pos-in-match-range pos 3))
  14891. (setq m (string-to-number (match-string 3 s))
  14892. h (string-to-number (match-string 2 s)))
  14893. (if (org-pos-in-match-range pos 2)
  14894. (setq h (+ h n))
  14895. (setq n (* dm (org-no-warnings (signum n))))
  14896. (when (not (= 0 (setq rem (% m dm))))
  14897. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14898. (setq m (+ m n)))
  14899. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14900. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14901. (setq h (min 24 (max 0 h)))
  14902. (setq ng 1 new (format "-%02d:%02d" h m)))
  14903. ((org-pos-in-match-range pos 6)
  14904. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14905. ((org-pos-in-match-range pos 5)
  14906. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14907. ((org-pos-in-match-range pos 9)
  14908. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14909. ((org-pos-in-match-range pos 8)
  14910. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14911. (when ng
  14912. (setq s (concat
  14913. (substring s 0 (match-beginning ng))
  14914. new
  14915. (substring s (match-end ng))))))
  14916. s))
  14917. (defun org-recenter-calendar (date)
  14918. "If the calendar is visible, recenter it to DATE."
  14919. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14920. (when cwin
  14921. (let ((calendar-move-hook nil))
  14922. (with-selected-window cwin
  14923. (calendar-goto-date (if (listp date) date
  14924. (calendar-gregorian-from-absolute date))))))))
  14925. (defun org-goto-calendar (&optional arg)
  14926. "Go to the Emacs calendar at the current date.
  14927. If there is a time stamp in the current line, go to that date.
  14928. A prefix ARG can be used to force the current date."
  14929. (interactive "P")
  14930. (let ((tsr org-ts-regexp) diff
  14931. (calendar-move-hook nil)
  14932. (calendar-view-holidays-initially-flag nil)
  14933. (calendar-view-diary-initially-flag nil))
  14934. (if (or (org-at-timestamp-p)
  14935. (save-excursion
  14936. (beginning-of-line 1)
  14937. (looking-at (concat ".*" tsr))))
  14938. (let ((d1 (time-to-days (current-time)))
  14939. (d2 (time-to-days
  14940. (org-time-string-to-time (match-string 1)))))
  14941. (setq diff (- d2 d1))))
  14942. (calendar)
  14943. (calendar-goto-today)
  14944. (if (and diff (not arg)) (calendar-forward-day diff))))
  14945. (defun org-get-date-from-calendar ()
  14946. "Return a list (month day year) of date at point in calendar."
  14947. (with-current-buffer "*Calendar*"
  14948. (save-match-data
  14949. (calendar-cursor-to-date))))
  14950. (defun org-date-from-calendar ()
  14951. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14952. If there is already a time stamp at the cursor position, update it."
  14953. (interactive)
  14954. (if (org-at-timestamp-p t)
  14955. (org-timestamp-change 0 'calendar)
  14956. (let ((cal-date (org-get-date-from-calendar)))
  14957. (org-insert-time-stamp
  14958. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14959. (defun org-minutes-to-hh:mm-string (m)
  14960. "Compute H:MM from a number of minutes."
  14961. (let ((h (/ m 60)))
  14962. (setq m (- m (* 60 h)))
  14963. (format org-time-clocksum-format h m)))
  14964. (defun org-hh:mm-string-to-minutes (s)
  14965. "Convert a string H:MM to a number of minutes.
  14966. If the string is just a number, interpret it as minutes.
  14967. In fact, the first hh:mm or number in the string will be taken,
  14968. there can be extra stuff in the string.
  14969. If no number is found, the return value is 0."
  14970. (cond
  14971. ((integerp s) s)
  14972. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14973. (+ (* (string-to-number (match-string 1 s)) 60)
  14974. (string-to-number (match-string 2 s))))
  14975. ((string-match "\\([0-9]+\\)" s)
  14976. (string-to-number (match-string 1 s)))
  14977. (t 0)))
  14978. (defcustom org-effort-durations
  14979. `(("h" . 60)
  14980. ("d" . ,(* 60 8))
  14981. ("w" . ,(* 60 8 5))
  14982. ("m" . ,(* 60 8 5 4))
  14983. ("y" . ,(* 60 8 5 40)))
  14984. "Conversion factor to minutes for an effort modifier.
  14985. Each entry has the form (MODIFIER . MINUTES).
  14986. In an effort string, a number followed by MODIFIER is multiplied
  14987. by the specified number of MINUTES to obtain an effort in
  14988. minutes.
  14989. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14990. effort string \"2hours\" is equivalent to 120 minutes."
  14991. :group 'org-agenda
  14992. :version "24.1"
  14993. :type '(alist :key-type (string :tag "Modifier")
  14994. :value-type (number :tag "Minutes")))
  14995. (defun org-duration-string-to-minutes (s &optional output-to-string)
  14996. "Convert a duration string S to minutes.
  14997. A bare number is interpreted as minutes, modifiers can be set by
  14998. customizing `org-effort-durations' (which see).
  14999. Entries containing a colon are interpreted as H:MM by
  15000. `org-hh:mm-string-to-minutes'."
  15001. (let ((result 0)
  15002. (re (concat "\\([0-9.]+\\) *\\("
  15003. (regexp-opt (mapcar 'car org-effort-durations))
  15004. "\\)")))
  15005. (while (string-match re s)
  15006. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15007. (string-to-number (match-string 1 s))))
  15008. (setq s (replace-match "" nil t s)))
  15009. (setq result (floor result))
  15010. (incf result (org-hh:mm-string-to-minutes s))
  15011. (if output-to-string (number-to-string result) result)))
  15012. ;;;; Files
  15013. (defun org-save-all-org-buffers ()
  15014. "Save all Org-mode buffers without user confirmation."
  15015. (interactive)
  15016. (message "Saving all Org-mode buffers...")
  15017. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15018. (when (featurep 'org-id) (org-id-locations-save))
  15019. (message "Saving all Org-mode buffers... done"))
  15020. (defun org-revert-all-org-buffers ()
  15021. "Revert all Org-mode buffers.
  15022. Prompt for confirmation when there are unsaved changes.
  15023. Be sure you know what you are doing before letting this function
  15024. overwrite your changes.
  15025. This function is useful in a setup where one tracks org files
  15026. with a version control system, to revert on one machine after pulling
  15027. changes from another. I believe the procedure must be like this:
  15028. 1. M-x org-save-all-org-buffers
  15029. 2. Pull changes from the other machine, resolve conflicts
  15030. 3. M-x org-revert-all-org-buffers"
  15031. (interactive)
  15032. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15033. (error "Abort"))
  15034. (save-excursion
  15035. (save-window-excursion
  15036. (mapc
  15037. (lambda (b)
  15038. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15039. (with-current-buffer b buffer-file-name))
  15040. (org-pop-to-buffer-same-window b)
  15041. (revert-buffer t 'no-confirm)))
  15042. (buffer-list))
  15043. (when (and (featurep 'org-id) org-id-track-globally)
  15044. (org-id-locations-load)))))
  15045. ;;;; Agenda files
  15046. ;;;###autoload
  15047. (defun org-switchb (&optional arg)
  15048. "Switch between Org buffers.
  15049. With one prefix argument, restrict available buffers to files.
  15050. With two prefix arguments, restrict available buffers to agenda files.
  15051. Defaults to `iswitchb' for buffer name completion.
  15052. Set `org-completion-use-ido' to make it use ido instead."
  15053. (interactive "P")
  15054. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15055. ((equal arg '(16)) (org-buffer-list 'agenda))
  15056. (t (org-buffer-list))))
  15057. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15058. (org-completion-use-ido org-completion-use-ido))
  15059. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15060. (setq org-completion-use-iswitchb t))
  15061. (org-pop-to-buffer-same-window
  15062. (org-icompleting-read "Org buffer: "
  15063. (mapcar 'list (mapcar 'buffer-name blist))
  15064. nil t))))
  15065. ;;; Define some older names previously used for this functionality
  15066. ;;;###autoload
  15067. (defalias 'org-ido-switchb 'org-switchb)
  15068. ;;;###autoload
  15069. (defalias 'org-iswitchb 'org-switchb)
  15070. (defun org-buffer-list (&optional predicate exclude-tmp)
  15071. "Return a list of Org buffers.
  15072. PREDICATE can be `export', `files' or `agenda'.
  15073. export restrict the list to Export buffers.
  15074. files restrict the list to buffers visiting Org files.
  15075. agenda restrict the list to buffers visiting agenda files.
  15076. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15077. (let* ((bfn nil)
  15078. (agenda-files (and (eq predicate 'agenda)
  15079. (mapcar 'file-truename (org-agenda-files t))))
  15080. (filter
  15081. (cond
  15082. ((eq predicate 'files)
  15083. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15084. ((eq predicate 'export)
  15085. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15086. ((eq predicate 'agenda)
  15087. (lambda (b)
  15088. (with-current-buffer b
  15089. (and (derived-mode-p 'org-mode)
  15090. (setq bfn (buffer-file-name b))
  15091. (member (file-truename bfn) agenda-files)))))
  15092. (t (lambda (b) (with-current-buffer b
  15093. (or (derived-mode-p 'org-mode)
  15094. (string-match "\*Org .*Export"
  15095. (buffer-name b)))))))))
  15096. (delq nil
  15097. (mapcar
  15098. (lambda(b)
  15099. (if (and (funcall filter b)
  15100. (or (not exclude-tmp)
  15101. (not (string-match "tmp" (buffer-name b)))))
  15102. b
  15103. nil))
  15104. (buffer-list)))))
  15105. (defun org-agenda-files (&optional unrestricted archives)
  15106. "Get the list of agenda files.
  15107. Optional UNRESTRICTED means return the full list even if a restriction
  15108. is currently in place.
  15109. When ARCHIVES is t, include all archive files that are really being
  15110. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15111. `org-agenda-archives-mode' is t."
  15112. (let ((files
  15113. (cond
  15114. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15115. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15116. ((listp org-agenda-files) org-agenda-files)
  15117. (t (error "Invalid value of `org-agenda-files'")))))
  15118. (setq files (apply 'append
  15119. (mapcar (lambda (f)
  15120. (if (file-directory-p f)
  15121. (directory-files
  15122. f t org-agenda-file-regexp)
  15123. (list f)))
  15124. files)))
  15125. (when org-agenda-skip-unavailable-files
  15126. (setq files (delq nil
  15127. (mapcar (function
  15128. (lambda (file)
  15129. (and (file-readable-p file) file)))
  15130. files))))
  15131. (when (or (eq archives t)
  15132. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15133. (setq files (org-add-archive-files files)))
  15134. files))
  15135. (defun org-agenda-file-p (&optional file)
  15136. "Return non-nil, if FILE is an agenda file.
  15137. If FILE is omitted, use the file associated with the current
  15138. buffer."
  15139. (member (or file (buffer-file-name))
  15140. (org-agenda-files t)))
  15141. (defun org-edit-agenda-file-list ()
  15142. "Edit the list of agenda files.
  15143. Depending on setup, this either uses customize to edit the variable
  15144. `org-agenda-files', or it visits the file that is holding the list. In the
  15145. latter case, the buffer is set up in a way that saving it automatically kills
  15146. the buffer and restores the previous window configuration."
  15147. (interactive)
  15148. (if (stringp org-agenda-files)
  15149. (let ((cw (current-window-configuration)))
  15150. (find-file org-agenda-files)
  15151. (org-set-local 'org-window-configuration cw)
  15152. (org-add-hook 'after-save-hook
  15153. (lambda ()
  15154. (set-window-configuration
  15155. (prog1 org-window-configuration
  15156. (kill-buffer (current-buffer))))
  15157. (org-install-agenda-files-menu)
  15158. (message "New agenda file list installed"))
  15159. nil 'local)
  15160. (message "%s" (substitute-command-keys
  15161. "Edit list and finish with \\[save-buffer]")))
  15162. (customize-variable 'org-agenda-files)))
  15163. (defun org-store-new-agenda-file-list (list)
  15164. "Set new value for the agenda file list and save it correctly."
  15165. (if (stringp org-agenda-files)
  15166. (let ((fe (org-read-agenda-file-list t)) b u)
  15167. (while (setq b (find-buffer-visiting org-agenda-files))
  15168. (kill-buffer b))
  15169. (with-temp-file org-agenda-files
  15170. (insert
  15171. (mapconcat
  15172. (lambda (f) ;; Keep un-expanded entries.
  15173. (if (setq u (assoc f fe))
  15174. (cdr u)
  15175. f))
  15176. list "\n")
  15177. "\n")))
  15178. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15179. (org-insert-mode-line-in-empty-file nil))
  15180. (setq org-agenda-files list)
  15181. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15182. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15183. "Read the list of agenda files from a file.
  15184. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15185. filenames, used by `org-store-new-agenda-file-list' to write back
  15186. un-expanded file names."
  15187. (when (file-directory-p org-agenda-files)
  15188. (error "`org-agenda-files' cannot be a single directory"))
  15189. (when (stringp org-agenda-files)
  15190. (with-temp-buffer
  15191. (insert-file-contents org-agenda-files)
  15192. (mapcar
  15193. (lambda (f)
  15194. (let ((e (expand-file-name (substitute-in-file-name f)
  15195. org-directory)))
  15196. (if pair-with-expansion
  15197. (cons e f)
  15198. e)))
  15199. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15200. ;;;###autoload
  15201. (defun org-cycle-agenda-files ()
  15202. "Cycle through the files in `org-agenda-files'.
  15203. If the current buffer visits an agenda file, find the next one in the list.
  15204. If the current buffer does not, find the first agenda file."
  15205. (interactive)
  15206. (let* ((fs (org-agenda-files t))
  15207. (files (append fs (list (car fs))))
  15208. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15209. file)
  15210. (unless files (error "No agenda files"))
  15211. (catch 'exit
  15212. (while (setq file (pop files))
  15213. (if (equal (file-truename file) tcf)
  15214. (when (car files)
  15215. (find-file (car files))
  15216. (throw 'exit t))))
  15217. (find-file (car fs)))
  15218. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15219. (defun org-agenda-file-to-front (&optional to-end)
  15220. "Move/add the current file to the top of the agenda file list.
  15221. If the file is not present in the list, it is added to the front. If it is
  15222. present, it is moved there. With optional argument TO-END, add/move to the
  15223. end of the list."
  15224. (interactive "P")
  15225. (let ((org-agenda-skip-unavailable-files nil)
  15226. (file-alist (mapcar (lambda (x)
  15227. (cons (file-truename x) x))
  15228. (org-agenda-files t)))
  15229. (ctf (file-truename buffer-file-name))
  15230. x had)
  15231. (setq x (assoc ctf file-alist) had x)
  15232. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15233. (if to-end
  15234. (setq file-alist (append (delq x file-alist) (list x)))
  15235. (setq file-alist (cons x (delq x file-alist))))
  15236. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15237. (org-install-agenda-files-menu)
  15238. (message "File %s to %s of agenda file list"
  15239. (if had "moved" "added") (if to-end "end" "front"))))
  15240. (defun org-remove-file (&optional file)
  15241. "Remove current file from the list of files in variable `org-agenda-files'.
  15242. These are the files which are being checked for agenda entries.
  15243. Optional argument FILE means use this file instead of the current."
  15244. (interactive)
  15245. (let* ((org-agenda-skip-unavailable-files nil)
  15246. (file (or file buffer-file-name))
  15247. (true-file (file-truename file))
  15248. (afile (abbreviate-file-name file))
  15249. (files (delq nil (mapcar
  15250. (lambda (x)
  15251. (if (equal true-file
  15252. (file-truename x))
  15253. nil x))
  15254. (org-agenda-files t)))))
  15255. (if (not (= (length files) (length (org-agenda-files t))))
  15256. (progn
  15257. (org-store-new-agenda-file-list files)
  15258. (org-install-agenda-files-menu)
  15259. (message "Removed file: %s" afile))
  15260. (message "File was not in list: %s (not removed)" afile))))
  15261. (defun org-file-menu-entry (file)
  15262. (vector file (list 'find-file file) t))
  15263. (defun org-check-agenda-file (file)
  15264. "Make sure FILE exists. If not, ask user what to do."
  15265. (when (not (file-exists-p file))
  15266. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15267. (abbreviate-file-name file))
  15268. (let ((r (downcase (read-char-exclusive))))
  15269. (cond
  15270. ((equal r ?r)
  15271. (org-remove-file file)
  15272. (throw 'nextfile t))
  15273. (t (error "Abort"))))))
  15274. (defun org-get-agenda-file-buffer (file)
  15275. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15276. it to the list of buffers which might be released later."
  15277. (let ((buf (org-find-base-buffer-visiting file)))
  15278. (if buf
  15279. buf ; just return it
  15280. ;; Make a new buffer and remember it
  15281. (setq buf (find-file-noselect file))
  15282. (if buf (push buf org-agenda-new-buffers))
  15283. buf)))
  15284. (defun org-release-buffers (blist)
  15285. "Release all buffers in list, asking the user for confirmation when needed.
  15286. When a buffer is unmodified, it is just killed. When modified, it is saved
  15287. \(if the user agrees) and then killed."
  15288. (let (buf file)
  15289. (while (setq buf (pop blist))
  15290. (setq file (buffer-file-name buf))
  15291. (when (and (buffer-modified-p buf)
  15292. file
  15293. (y-or-n-p (format "Save file %s? " file)))
  15294. (with-current-buffer buf (save-buffer)))
  15295. (kill-buffer buf))))
  15296. (defun org-prepare-agenda-buffers (files)
  15297. "Create buffers for all agenda files, protect archived trees and comments."
  15298. (interactive)
  15299. (let ((pa '(:org-archived t))
  15300. (pc '(:org-comment t))
  15301. (pall '(:org-archived t :org-comment t))
  15302. (inhibit-read-only t)
  15303. (rea (concat ":" org-archive-tag ":"))
  15304. bmp file re)
  15305. (save-excursion
  15306. (save-restriction
  15307. (while (setq file (pop files))
  15308. (catch 'nextfile
  15309. (if (bufferp file)
  15310. (set-buffer file)
  15311. (org-check-agenda-file file)
  15312. (set-buffer (org-get-agenda-file-buffer file)))
  15313. (widen)
  15314. (setq bmp (buffer-modified-p))
  15315. (org-refresh-category-properties)
  15316. (setq org-todo-keywords-for-agenda
  15317. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15318. (setq org-done-keywords-for-agenda
  15319. (append org-done-keywords-for-agenda org-done-keywords))
  15320. (setq org-todo-keyword-alist-for-agenda
  15321. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15322. (setq org-drawers-for-agenda
  15323. (append org-drawers-for-agenda org-drawers))
  15324. (setq org-tag-alist-for-agenda
  15325. (append org-tag-alist-for-agenda org-tag-alist))
  15326. (save-excursion
  15327. (remove-text-properties (point-min) (point-max) pall)
  15328. (when org-agenda-skip-archived-trees
  15329. (goto-char (point-min))
  15330. (while (re-search-forward rea nil t)
  15331. (if (org-at-heading-p t)
  15332. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15333. (goto-char (point-min))
  15334. (setq re (format org-heading-keyword-regexp-format
  15335. org-comment-string))
  15336. (while (re-search-forward re nil t)
  15337. (add-text-properties
  15338. (match-beginning 0) (org-end-of-subtree t) pc)))
  15339. (set-buffer-modified-p bmp)))))
  15340. (setq org-todo-keywords-for-agenda
  15341. (org-uniquify org-todo-keywords-for-agenda))
  15342. (setq org-todo-keyword-alist-for-agenda
  15343. (org-uniquify org-todo-keyword-alist-for-agenda)
  15344. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15345. ;;;; Embedded LaTeX
  15346. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15347. "Keymap for the minor `org-cdlatex-mode'.")
  15348. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15349. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15350. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15351. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15352. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15353. (defvar org-cdlatex-texmathp-advice-is-done nil
  15354. "Flag remembering if we have applied the advice to texmathp already.")
  15355. (define-minor-mode org-cdlatex-mode
  15356. "Toggle the minor `org-cdlatex-mode'.
  15357. This mode supports entering LaTeX environment and math in LaTeX fragments
  15358. in Org-mode.
  15359. \\{org-cdlatex-mode-map}"
  15360. nil " OCDL" nil
  15361. (when org-cdlatex-mode
  15362. (require 'cdlatex)
  15363. (run-hooks 'cdlatex-mode-hook)
  15364. (cdlatex-compute-tables))
  15365. (unless org-cdlatex-texmathp-advice-is-done
  15366. (setq org-cdlatex-texmathp-advice-is-done t)
  15367. (defadvice texmathp (around org-math-always-on activate)
  15368. "Always return t in org-mode buffers.
  15369. This is because we want to insert math symbols without dollars even outside
  15370. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15371. an embedded LaTeX fragment, let texmathp do its job.
  15372. \\[org-cdlatex-mode-map]"
  15373. (interactive)
  15374. (let (p)
  15375. (cond
  15376. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15377. ((eq this-command 'cdlatex-math-symbol)
  15378. (setq ad-return-value t
  15379. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15380. (t
  15381. (let ((p (org-inside-LaTeX-fragment-p)))
  15382. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15383. (setq ad-return-value t
  15384. texmathp-why '("Org-mode embedded math" . 0))
  15385. (if p ad-do-it)))))))))
  15386. (defun turn-on-org-cdlatex ()
  15387. "Unconditionally turn on `org-cdlatex-mode'."
  15388. (org-cdlatex-mode 1))
  15389. (defun org-inside-LaTeX-fragment-p ()
  15390. "Test if point is inside a LaTeX fragment.
  15391. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15392. sequence appearing also before point.
  15393. Even though the matchers for math are configurable, this function assumes
  15394. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15395. delimiters are skipped when they have been removed by customization.
  15396. The return value is nil, or a cons cell with the delimiter and the
  15397. position of this delimiter.
  15398. This function does a reasonably good job, but can locally be fooled by
  15399. for example currency specifications. For example it will assume being in
  15400. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15401. fragments that are properly closed, but during editing, we have to live
  15402. with the uncertainty caused by missing closing delimiters. This function
  15403. looks only before point, not after."
  15404. (catch 'exit
  15405. (let ((pos (point))
  15406. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15407. (lim (progn
  15408. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15409. (point)))
  15410. dd-on str (start 0) m re)
  15411. (goto-char pos)
  15412. (when dodollar
  15413. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15414. re (nth 1 (assoc "$" org-latex-regexps)))
  15415. (while (string-match re str start)
  15416. (cond
  15417. ((= (match-end 0) (length str))
  15418. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15419. ((= (match-end 0) (- (length str) 5))
  15420. (throw 'exit nil))
  15421. (t (setq start (match-end 0))))))
  15422. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15423. (goto-char pos)
  15424. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15425. (and (match-beginning 2) (throw 'exit nil))
  15426. ;; count $$
  15427. (while (re-search-backward "\\$\\$" lim t)
  15428. (setq dd-on (not dd-on)))
  15429. (goto-char pos)
  15430. (if dd-on (cons "$$" m))))))
  15431. (defun org-inside-latex-macro-p ()
  15432. "Is point inside a LaTeX macro or its arguments?"
  15433. (save-match-data
  15434. (org-in-regexp
  15435. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15436. (defun org-try-cdlatex-tab ()
  15437. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15438. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15439. - inside a LaTeX fragment, or
  15440. - after the first word in a line, where an abbreviation expansion could
  15441. insert a LaTeX environment."
  15442. (when org-cdlatex-mode
  15443. (cond
  15444. ;; Before any word on the line: No expansion possible.
  15445. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15446. ;; Just after first word on the line: Expand it. Make sure it
  15447. ;; cannot happen on headlines, though.
  15448. ((save-excursion
  15449. (skip-chars-backward "a-zA-Z0-9*")
  15450. (skip-chars-backward " \t")
  15451. (and (bolp) (not (org-at-heading-p))))
  15452. (cdlatex-tab) t)
  15453. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15454. (defun org-cdlatex-underscore-caret (&optional arg)
  15455. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15456. Revert to the normal definition outside of these fragments."
  15457. (interactive "P")
  15458. (if (org-inside-LaTeX-fragment-p)
  15459. (call-interactively 'cdlatex-sub-superscript)
  15460. (let (org-cdlatex-mode)
  15461. (call-interactively (key-binding (vector last-input-event))))))
  15462. (defun org-cdlatex-math-modify (&optional arg)
  15463. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15464. Revert to the normal definition outside of these fragments."
  15465. (interactive "P")
  15466. (if (org-inside-LaTeX-fragment-p)
  15467. (call-interactively 'cdlatex-math-modify)
  15468. (let (org-cdlatex-mode)
  15469. (call-interactively (key-binding (vector last-input-event))))))
  15470. (defvar org-latex-fragment-image-overlays nil
  15471. "List of overlays carrying the images of latex fragments.")
  15472. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15473. (defun org-remove-latex-fragment-image-overlays ()
  15474. "Remove all overlays with LaTeX fragment images in current buffer."
  15475. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15476. (setq org-latex-fragment-image-overlays nil))
  15477. (defun org-preview-latex-fragment (&optional subtree)
  15478. "Preview the LaTeX fragment at point, or all locally or globally.
  15479. If the cursor is in a LaTeX fragment, create the image and overlay
  15480. it over the source code. If there is no fragment at point, display
  15481. all fragments in the current text, from one headline to the next. With
  15482. prefix SUBTREE, display all fragments in the current subtree. With a
  15483. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15484. the cursor is before the first headline,
  15485. display all fragments in the buffer.
  15486. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15487. (interactive "P")
  15488. (unless buffer-file-name
  15489. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15490. (org-remove-latex-fragment-image-overlays)
  15491. (save-excursion
  15492. (save-restriction
  15493. (let (beg end at msg)
  15494. (cond
  15495. ((or (equal subtree '(16))
  15496. (not (save-excursion
  15497. (re-search-backward org-outline-regexp-bol nil t))))
  15498. (setq beg (point-min) end (point-max)
  15499. msg "Creating images for buffer...%s"))
  15500. ((equal subtree '(4))
  15501. (org-back-to-heading)
  15502. (setq beg (point) end (org-end-of-subtree t)
  15503. msg "Creating images for subtree...%s"))
  15504. (t
  15505. (if (setq at (org-inside-LaTeX-fragment-p))
  15506. (goto-char (max (point-min) (- (cdr at) 2)))
  15507. (org-back-to-heading))
  15508. (setq beg (point) end (progn (outline-next-heading) (point))
  15509. msg (if at "Creating image...%s"
  15510. "Creating images for entry...%s"))))
  15511. (message msg "")
  15512. (narrow-to-region beg end)
  15513. (goto-char beg)
  15514. (org-format-latex
  15515. (concat "ltxpng/" (file-name-sans-extension
  15516. (file-name-nondirectory
  15517. buffer-file-name)))
  15518. default-directory 'overlays msg at 'forbuffer
  15519. org-latex-create-formula-image-program)
  15520. (message msg "done. Use `C-c C-c' to remove images.")))))
  15521. (defvar org-latex-regexps
  15522. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15523. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15524. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15525. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15526. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15527. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15528. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15529. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15530. "Regular expressions for matching embedded LaTeX.")
  15531. (defvar org-export-have-math nil) ;; dynamic scoping
  15532. (defun org-format-latex (prefix &optional dir overlays msg at
  15533. forbuffer processing-type)
  15534. "Replace LaTeX fragments with links to an image, and produce images.
  15535. Some of the options can be changed using the variable
  15536. `org-format-latex-options'."
  15537. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15538. (let* ((prefixnodir (file-name-nondirectory prefix))
  15539. (absprefix (expand-file-name prefix dir))
  15540. (todir (file-name-directory absprefix))
  15541. (opt org-format-latex-options)
  15542. (matchers (plist-get opt :matchers))
  15543. (re-list org-latex-regexps)
  15544. (org-format-latex-header-extra
  15545. (plist-get (org-infile-export-plist) :latex-header-extra))
  15546. (cnt 0) txt hash link beg end re e checkdir
  15547. executables-checked string
  15548. m n block-type block linkfile movefile ov)
  15549. ;; Check the different regular expressions
  15550. (while (setq e (pop re-list))
  15551. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15552. block (if block-type "\n\n" ""))
  15553. (when (member m matchers)
  15554. (goto-char (point-min))
  15555. (while (re-search-forward re nil t)
  15556. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15557. (not (get-text-property (match-beginning n)
  15558. 'org-protected))
  15559. (or (not overlays)
  15560. (not (eq (get-char-property (match-beginning n)
  15561. 'org-overlay-type)
  15562. 'org-latex-overlay))))
  15563. (setq org-export-have-math t)
  15564. (cond
  15565. ((eq processing-type 'verbatim)
  15566. ;; Leave the text verbatim, just protect it
  15567. (add-text-properties (match-beginning n) (match-end n)
  15568. '(org-protected t)))
  15569. ((eq processing-type 'mathjax)
  15570. ;; Prepare for MathJax processing
  15571. (setq string (match-string n))
  15572. (if (member m '("$" "$1"))
  15573. (save-excursion
  15574. (delete-region (match-beginning n) (match-end n))
  15575. (goto-char (match-beginning n))
  15576. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15577. "\\)")
  15578. '(org-protected t))))
  15579. (add-text-properties (match-beginning n) (match-end n)
  15580. '(org-protected t))))
  15581. ((or (eq processing-type 'dvipng)
  15582. (eq processing-type 'imagemagick))
  15583. ;; Process to an image
  15584. (setq txt (match-string n)
  15585. beg (match-beginning n) end (match-end n)
  15586. cnt (1+ cnt))
  15587. (let (print-length print-level) ; make sure full list is printed
  15588. (setq hash (sha1 (prin1-to-string
  15589. (list org-format-latex-header
  15590. org-format-latex-header-extra
  15591. org-export-latex-default-packages-alist
  15592. org-export-latex-packages-alist
  15593. org-format-latex-options
  15594. forbuffer txt)))
  15595. linkfile (format "%s_%s.png" prefix hash)
  15596. movefile (format "%s_%s.png" absprefix hash)))
  15597. (setq link (concat block "[[file:" linkfile "]]" block))
  15598. (if msg (message msg cnt))
  15599. (goto-char beg)
  15600. (unless checkdir ; make sure the directory exists
  15601. (setq checkdir t)
  15602. (or (file-directory-p todir) (make-directory todir t)))
  15603. (cond
  15604. ((eq processing-type 'dvipng)
  15605. (unless executables-checked
  15606. (org-check-external-command
  15607. "latex" "needed to convert LaTeX fragments to images")
  15608. (org-check-external-command
  15609. "dvipng" "needed to convert LaTeX fragments to images")
  15610. (setq executables-checked t))
  15611. (unless (file-exists-p movefile)
  15612. (org-create-formula-image-with-dvipng
  15613. txt movefile opt forbuffer)))
  15614. ((eq processing-type 'imagemagick)
  15615. (unless executables-checked
  15616. (org-check-external-command
  15617. "convert" "you need to install imagemagick")
  15618. (setq executables-checked t))
  15619. (unless (file-exists-p movefile)
  15620. (org-create-formula-image-with-imagemagick
  15621. txt movefile opt forbuffer))))
  15622. (if overlays
  15623. (progn
  15624. (mapc (lambda (o)
  15625. (if (eq (overlay-get o 'org-overlay-type)
  15626. 'org-latex-overlay)
  15627. (delete-overlay o)))
  15628. (overlays-in beg end))
  15629. (setq ov (make-overlay beg end))
  15630. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15631. (if (featurep 'xemacs)
  15632. (progn
  15633. (overlay-put ov 'invisible t)
  15634. (overlay-put
  15635. ov 'end-glyph
  15636. (make-glyph (vector 'png :file movefile))))
  15637. (overlay-put
  15638. ov 'display
  15639. (list 'image :type 'png :file movefile :ascent 'center)))
  15640. (push ov org-latex-fragment-image-overlays)
  15641. (goto-char end))
  15642. (delete-region beg end)
  15643. (insert (org-add-props link
  15644. (list 'org-latex-src
  15645. (replace-regexp-in-string
  15646. "\"" "" txt)
  15647. 'org-latex-src-embed-type
  15648. (if block-type 'paragraph 'character))))))
  15649. ((eq processing-type 'mathml)
  15650. ;; Process to MathML
  15651. (unless executables-checked
  15652. (unless (save-match-data (org-format-latex-mathml-available-p))
  15653. (error "LaTeX to MathML converter not configured"))
  15654. (setq executables-checked t))
  15655. (setq txt (match-string n)
  15656. beg (match-beginning n) end (match-end n)
  15657. cnt (1+ cnt))
  15658. (if msg (message msg cnt))
  15659. (goto-char beg)
  15660. (delete-region beg end)
  15661. (insert (org-format-latex-as-mathml
  15662. txt block-type prefix dir)))
  15663. (t
  15664. (error "Unknown conversion type %s for latex fragments"
  15665. processing-type)))))))))
  15666. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15667. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15668. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15669. sucessful, return the portion between \"<math...> </math>\"
  15670. elements otherwise return nil. When MATHML-FILE is specified,
  15671. write the results in to that file. When invoked as an
  15672. interactive command, prompt for LATEX-FRAG, with initial value
  15673. set to the current active region and echo the results for user
  15674. inspection."
  15675. (interactive (list (let ((frag (when (region-active-p)
  15676. (buffer-substring-no-properties
  15677. (region-beginning) (region-end)))))
  15678. (read-string "LaTeX Fragment: " frag nil frag))))
  15679. (unless latex-frag (error "Invalid latex-frag"))
  15680. (let* ((tmp-in-file (file-relative-name
  15681. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15682. (ignore (write-region latex-frag nil tmp-in-file))
  15683. (tmp-out-file (file-relative-name
  15684. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15685. (cmd (format-spec
  15686. org-latex-to-mathml-convert-command
  15687. `((?j . ,(shell-quote-argument
  15688. (expand-file-name org-latex-to-mathml-jar-file)))
  15689. (?I . ,(shell-quote-argument tmp-in-file))
  15690. (?o . ,(shell-quote-argument tmp-out-file)))))
  15691. mathml shell-command-output)
  15692. (when (org-called-interactively-p 'any)
  15693. (unless (org-format-latex-mathml-available-p)
  15694. (error "LaTeX to MathML converter not configured")))
  15695. (message "Running %s" cmd)
  15696. (setq shell-command-output (shell-command-to-string cmd))
  15697. (setq mathml
  15698. (when (file-readable-p tmp-out-file)
  15699. (with-current-buffer (find-file-noselect tmp-out-file t)
  15700. (goto-char (point-min))
  15701. (when (re-search-forward
  15702. (concat
  15703. (regexp-quote
  15704. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15705. "\\(.\\|\n\\)*"
  15706. (regexp-quote "</math>")) nil t)
  15707. (prog1 (match-string 0) (kill-buffer))))))
  15708. (cond
  15709. (mathml
  15710. (setq mathml
  15711. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15712. (when mathml-file
  15713. (write-region mathml nil mathml-file))
  15714. (when (org-called-interactively-p 'any)
  15715. (message mathml)))
  15716. ((message "LaTeX to MathML conversion failed")
  15717. (message shell-command-output)))
  15718. (delete-file tmp-in-file)
  15719. (when (file-exists-p tmp-out-file)
  15720. (delete-file tmp-out-file))
  15721. mathml))
  15722. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15723. prefix &optional dir)
  15724. "Use `org-create-math-formula' but check local cache first."
  15725. (let* ((absprefix (expand-file-name prefix dir))
  15726. (print-length nil) (print-level nil)
  15727. (formula-id (concat
  15728. "formula-"
  15729. (sha1
  15730. (prin1-to-string
  15731. (list latex-frag
  15732. org-latex-to-mathml-convert-command)))))
  15733. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15734. (formula-cache-dir (file-name-directory formula-cache)))
  15735. (unless (file-directory-p formula-cache-dir)
  15736. (make-directory formula-cache-dir t))
  15737. (unless (file-exists-p formula-cache)
  15738. (org-create-math-formula latex-frag formula-cache))
  15739. (if (file-exists-p formula-cache)
  15740. ;; Successful conversion. Return the link to MathML file.
  15741. (org-add-props
  15742. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15743. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15744. 'org-latex-src-embed-type (if latex-frag-type
  15745. 'paragraph 'character)))
  15746. ;; Failed conversion. Return the LaTeX fragment verbatim
  15747. (add-text-properties
  15748. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15749. latex-frag)))
  15750. ;; This function borrows from Ganesh Swami's latex2png.el
  15751. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15752. "This calls dvipng."
  15753. (require 'org-latex)
  15754. (let* ((tmpdir (if (featurep 'xemacs)
  15755. (temp-directory)
  15756. temporary-file-directory))
  15757. (texfilebase (make-temp-name
  15758. (expand-file-name "orgtex" tmpdir)))
  15759. (texfile (concat texfilebase ".tex"))
  15760. (dvifile (concat texfilebase ".dvi"))
  15761. (pngfile (concat texfilebase ".png"))
  15762. (fnh (if (featurep 'xemacs)
  15763. (font-height (face-font 'default))
  15764. (face-attribute 'default :height nil)))
  15765. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15766. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15767. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15768. "Black"))
  15769. (bg (or (plist-get options (if buffer :background :html-background))
  15770. "Transparent")))
  15771. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15772. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15773. (with-temp-file texfile
  15774. (insert (org-splice-latex-header
  15775. org-format-latex-header
  15776. org-export-latex-default-packages-alist
  15777. org-export-latex-packages-alist t
  15778. org-format-latex-header-extra))
  15779. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15780. (require 'org-latex)
  15781. (org-export-latex-fix-inputenc))
  15782. (let ((dir default-directory))
  15783. (condition-case nil
  15784. (progn
  15785. (cd tmpdir)
  15786. (call-process "latex" nil nil nil texfile))
  15787. (error nil))
  15788. (cd dir))
  15789. (if (not (file-exists-p dvifile))
  15790. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15791. (condition-case nil
  15792. (if (featurep 'xemacs)
  15793. (call-process "dvipng" nil nil nil
  15794. "-fg" fg "-bg" bg
  15795. "-T" "tight"
  15796. "-o" pngfile
  15797. dvifile)
  15798. (call-process "dvipng" nil nil nil
  15799. "-fg" fg "-bg" bg
  15800. "-D" dpi
  15801. ;;"-x" scale "-y" scale
  15802. "-T" "tight"
  15803. "-o" pngfile
  15804. dvifile))
  15805. (error nil))
  15806. (if (not (file-exists-p pngfile))
  15807. (if org-format-latex-signal-error
  15808. (error "Failed to create png file from %s" texfile)
  15809. (message "Failed to create png file from %s" texfile)
  15810. nil)
  15811. ;; Use the requested file name and clean up
  15812. (copy-file pngfile tofile 'replace)
  15813. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  15814. (delete-file (concat texfilebase e)))
  15815. pngfile))))
  15816. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  15817. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  15818. "This calls convert, which is included into imagemagick."
  15819. (require 'org-latex)
  15820. (let* ((tmpdir (if (featurep 'xemacs)
  15821. (temp-directory)
  15822. temporary-file-directory))
  15823. (texfilebase (make-temp-name
  15824. (expand-file-name "orgtex" tmpdir)))
  15825. (texfile (concat texfilebase ".tex"))
  15826. (pdffile (concat texfilebase ".pdf"))
  15827. (pngfile (concat texfilebase ".png"))
  15828. (fnh (if (featurep 'xemacs)
  15829. (font-height (face-font 'default))
  15830. (face-attribute 'default :height nil)))
  15831. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15832. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15833. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15834. "black"))
  15835. (bg (or (plist-get options (if buffer :background :html-background))
  15836. "white")))
  15837. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  15838. (setq fg (org-latex-color-format fg)))
  15839. (if (eq bg 'default) (setq bg (org-latex-color :background))
  15840. (setq bg (org-latex-color-format
  15841. (if (string= bg "Transparent")(setq bg "white")))))
  15842. (with-temp-file texfile
  15843. (insert (org-splice-latex-header
  15844. org-format-latex-header
  15845. org-export-latex-default-packages-alist
  15846. org-export-latex-packages-alist t
  15847. org-format-latex-header-extra))
  15848. (insert "\n\\begin{document}\n"
  15849. "\\definecolor{fg}{rgb}{" fg "}\n"
  15850. "\\definecolor{bg}{rgb}{" bg "}\n"
  15851. "\n\\pagecolor{bg}\n"
  15852. "\n{\\color{fg}\n"
  15853. string
  15854. "\n}\n"
  15855. "\n\\end{document}\n" )
  15856. (require 'org-latex)
  15857. (org-export-latex-fix-inputenc))
  15858. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  15859. (condition-case nil
  15860. (progn
  15861. (cd tmpdir)
  15862. (setq cmds org-latex-to-pdf-process)
  15863. (while cmds
  15864. (setq latex-frags-cmds (pop cmds))
  15865. (if (listp latex-frags-cmds)
  15866. (setq cmds nil)
  15867. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  15868. (while latex-frags-cmds
  15869. (setq cmd (pop latex-frags-cmds))
  15870. (while (string-match "%b" cmd)
  15871. (setq cmd (replace-match
  15872. (save-match-data
  15873. (shell-quote-argument texfile))
  15874. t t cmd)))
  15875. (while (string-match "%f" cmd)
  15876. (setq cmd (replace-match
  15877. (save-match-data
  15878. (shell-quote-argument (file-name-nondirectory texfile)))
  15879. t t cmd)))
  15880. (while (string-match "%o" cmd)
  15881. (setq cmd (replace-match
  15882. (save-match-data
  15883. (shell-quote-argument (file-name-directory texfile)))
  15884. t t cmd)))
  15885. (setq cmd (split-string cmd))
  15886. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  15887. (error nil))
  15888. (cd dir))
  15889. (if (not (file-exists-p pdffile))
  15890. (progn (message "Failed to create pdf file from %s" texfile) nil)
  15891. (condition-case nil
  15892. (if (featurep 'xemacs)
  15893. (call-process "convert" nil nil nil
  15894. "-density" "96"
  15895. "-trim"
  15896. "-antialias"
  15897. pdffile
  15898. "-quality" "100"
  15899. ;; "-sharpen" "0x1.0"
  15900. pngfile)
  15901. (call-process "convert" nil nil nil
  15902. "-density" dpi
  15903. "-trim"
  15904. "-antialias"
  15905. pdffile
  15906. "-quality" "100"
  15907. ; "-sharpen" "0x1.0"
  15908. pngfile))
  15909. (error nil))
  15910. (if (not (file-exists-p pngfile))
  15911. (if org-format-latex-signal-error
  15912. (error "Failed to create png file from %s" texfile)
  15913. (message "Failed to create png file from %s" texfile)
  15914. nil)
  15915. ;; Use the requested file name and clean up
  15916. (copy-file pngfile tofile 'replace)
  15917. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  15918. (delete-file (concat texfilebase e)))
  15919. pngfile))))
  15920. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15921. "Fill a LaTeX header template TPL.
  15922. In the template, the following place holders will be recognized:
  15923. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15924. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15925. [PACKAGES] \\usepackage statements for PKG
  15926. [NO-PACKAGES] do not include PKG
  15927. [EXTRA] the string EXTRA
  15928. [NO-EXTRA] do not include EXTRA
  15929. For backward compatibility, if both the positive and the negative place
  15930. holder is missing, the positive one (without the \"NO-\") will be
  15931. assumed to be present at the end of the template.
  15932. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15933. EXTRA is a string.
  15934. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15935. (let (rpl (end ""))
  15936. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15937. (setq rpl (if (or (match-end 1) (not def-pkg))
  15938. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15939. tpl (replace-match rpl t t tpl))
  15940. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15941. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15942. (setq rpl (if (or (match-end 1) (not pkg))
  15943. "" (org-latex-packages-to-string pkg snippets-p t))
  15944. tpl (replace-match rpl t t tpl))
  15945. (if pkg (setq end
  15946. (concat end "\n"
  15947. (org-latex-packages-to-string pkg snippets-p)))))
  15948. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15949. (setq rpl (if (or (match-end 1) (not extra))
  15950. "" (concat extra "\n"))
  15951. tpl (replace-match rpl t t tpl))
  15952. (if (and extra (string-match "\\S-" extra))
  15953. (setq end (concat end "\n" extra))))
  15954. (if (string-match "\\S-" end)
  15955. (concat tpl "\n" end)
  15956. tpl)))
  15957. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15958. "Turn an alist of packages into a string with the \\usepackage macros."
  15959. (setq pkg (mapconcat (lambda(p)
  15960. (cond
  15961. ((stringp p) p)
  15962. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15963. (format "%% Package %s omitted" (cadr p)))
  15964. ((equal "" (car p))
  15965. (format "\\usepackage{%s}" (cadr p)))
  15966. (t
  15967. (format "\\usepackage[%s]{%s}"
  15968. (car p) (cadr p)))))
  15969. pkg
  15970. "\n"))
  15971. (if newline (concat pkg "\n") pkg))
  15972. (defun org-dvipng-color (attr)
  15973. "Return a RGB color specification for dvipng."
  15974. (apply 'format "rgb %s %s %s"
  15975. (mapcar 'org-normalize-color
  15976. (if (featurep 'xemacs)
  15977. (color-rgb-components
  15978. (face-property 'default
  15979. (cond ((eq attr :foreground) 'foreground)
  15980. ((eq attr :background) 'background))))
  15981. (color-values (face-attribute 'default attr nil))))))
  15982. (defun org-latex-color (attr)
  15983. "Return a RGB color for the LaTeX color package."
  15984. (apply 'format "%s,%s,%s"
  15985. (mapcar 'org-normalize-color
  15986. (if (featurep 'xemacs)
  15987. (color-rgb-components
  15988. (face-property 'default
  15989. (cond ((eq attr :foreground) 'foreground)
  15990. ((eq attr :background) 'background))))
  15991. (color-values (face-attribute 'default attr nil))))))
  15992. (defun org-latex-color-format (color-name)
  15993. "Convert COLOR-NAME to a RGB color value."
  15994. (apply 'format "%s,%s,%s"
  15995. (mapcar 'org-normalize-color
  15996. (color-values color-name))))
  15997. (defun org-normalize-color (value)
  15998. "Return string to be used as color value for an RGB component."
  15999. (format "%g" (/ value 65535.0)))
  16000. ;; Image display
  16001. (defvar org-inline-image-overlays nil)
  16002. (make-variable-buffer-local 'org-inline-image-overlays)
  16003. (defun org-toggle-inline-images (&optional include-linked)
  16004. "Toggle the display of inline images.
  16005. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16006. (interactive "P")
  16007. (if org-inline-image-overlays
  16008. (progn
  16009. (org-remove-inline-images)
  16010. (message "Inline image display turned off"))
  16011. (org-display-inline-images include-linked)
  16012. (if org-inline-image-overlays
  16013. (message "%d images displayed inline"
  16014. (length org-inline-image-overlays))
  16015. (message "No images to display inline"))))
  16016. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16017. "Display inline images.
  16018. Normally only links without a description part are inlined, because this
  16019. is how it will work for export. When INCLUDE-LINKED is set, also links
  16020. with a description part will be inlined. This can be nice for a quick
  16021. look at those images, but it does not reflect what exported files will look
  16022. like.
  16023. When REFRESH is set, refresh existing images between BEG and END.
  16024. This will create new image displays only if necessary.
  16025. BEG and END default to the buffer boundaries."
  16026. (interactive "P")
  16027. (unless refresh
  16028. (org-remove-inline-images)
  16029. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16030. (save-excursion
  16031. (save-restriction
  16032. (widen)
  16033. (setq beg (or beg (point-min)) end (or end (point-max)))
  16034. (goto-char beg)
  16035. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16036. (substring (org-image-file-name-regexp) 0 -2)
  16037. "\\)\\]" (if include-linked "" "\\]")))
  16038. old file ov img)
  16039. (while (re-search-forward re end t)
  16040. (setq old (get-char-property-and-overlay (match-beginning 1)
  16041. 'org-image-overlay))
  16042. (setq file (expand-file-name
  16043. (concat (or (match-string 3) "") (match-string 4))))
  16044. (when (file-exists-p file)
  16045. (if (and (car-safe old) refresh)
  16046. (image-refresh (overlay-get (cdr old) 'display))
  16047. (setq img (save-match-data (create-image file)))
  16048. (when img
  16049. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16050. (overlay-put ov 'display img)
  16051. (overlay-put ov 'face 'default)
  16052. (overlay-put ov 'org-image-overlay t)
  16053. (overlay-put ov 'modification-hooks
  16054. (list 'org-display-inline-modification-hook))
  16055. (push ov org-inline-image-overlays)))))))))
  16056. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16057. "Remove inline-display overlay if a corresponding region is modified."
  16058. (let ((inhibit-modification-hooks t))
  16059. (when (and ov after)
  16060. (delete ov org-inline-image-overlays)
  16061. (delete-overlay ov))))
  16062. (defun org-remove-inline-images ()
  16063. "Remove inline display of images."
  16064. (interactive)
  16065. (mapc 'delete-overlay org-inline-image-overlays)
  16066. (setq org-inline-image-overlays nil))
  16067. ;;;; Key bindings
  16068. ;; Outline functions from `outline-mode-prefix-map'
  16069. ;; that can be remapped in Org:
  16070. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16071. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16072. (define-key org-mode-map [remap outline-forward-same-level]
  16073. 'org-forward-same-level)
  16074. (define-key org-mode-map [remap outline-backward-same-level]
  16075. 'org-backward-same-level)
  16076. (define-key org-mode-map [remap show-branches]
  16077. 'org-kill-note-or-show-branches)
  16078. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16079. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16080. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16081. ;; Outline functions from `outline-mode-prefix-map'
  16082. ;; that can not be remapped in Org:
  16083. ;; - the column "key binding" shows whether the Outline function is still
  16084. ;; available in Org mode on the same key that it has been bound to in
  16085. ;; Outline mode:
  16086. ;; - "overridden": key used for a different functionality in Org mode
  16087. ;; - else: key still bound to the same Outline function in Org mode
  16088. ;; | Outline function | key binding | Org replacement |
  16089. ;; |------------------------------------+-------------+-----------------------|
  16090. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16091. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16092. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16093. ;; | `hide-subtree' | overridden | visibility cycling |
  16094. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16095. ;; | `hide-body' | overridden | no replacement |
  16096. ;; | `show-all' | overridden | no replacement |
  16097. ;; | `hide-entry' | overridden | visibility cycling |
  16098. ;; | `show-entry' | overridden | no replacement |
  16099. ;; | `hide-leaves' | overridden | no replacement |
  16100. ;; | `hide-sublevels' | overridden | no replacement |
  16101. ;; | `hide-other' | overridden | no replacement |
  16102. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  16103. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16104. ;; Make `C-c C-x' a prefix key
  16105. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16106. ;; TAB key with modifiers
  16107. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16108. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16109. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16110. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16111. ;; The following line is necessary under Suse GNU/Linux
  16112. (unless (featurep 'xemacs)
  16113. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16114. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16115. (define-key org-mode-map [backtab] 'org-shifttab)
  16116. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16117. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16118. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16119. ;; Cursor keys with modifiers
  16120. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16121. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16122. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16123. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16124. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16125. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16126. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16127. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16128. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16129. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16130. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16131. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16132. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16133. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16134. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16135. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16136. ;; Babel keys
  16137. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16138. (mapc (lambda (pair)
  16139. (define-key org-babel-map (car pair) (cdr pair)))
  16140. org-babel-key-bindings)
  16141. ;;; Extra keys for tty access.
  16142. ;; We only set them when really needed because otherwise the
  16143. ;; menus don't show the simple keys
  16144. (when (or org-use-extra-keys
  16145. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16146. (not window-system))
  16147. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16148. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16149. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16150. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16151. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16152. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16153. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16154. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16155. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16156. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16157. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16158. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16159. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16160. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16161. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16162. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16163. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16164. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16165. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16166. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16167. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16168. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16169. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16170. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16171. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16172. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16173. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16174. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16175. ;; All the other keys
  16176. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16177. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16178. (if (boundp 'narrow-map)
  16179. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16180. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16181. (if (boundp 'narrow-map)
  16182. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16183. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16184. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16185. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16186. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16187. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16188. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16189. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16190. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16191. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16192. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16193. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16194. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16195. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16196. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16197. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16198. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16199. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16200. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16201. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16202. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16203. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16204. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16205. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16206. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16207. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16208. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16209. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16210. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16211. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16212. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16213. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16214. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16215. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16216. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16217. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16218. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16219. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16220. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16221. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16222. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16223. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16224. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16225. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16226. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16227. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16228. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16229. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16230. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16231. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16232. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16233. (org-defkey org-mode-map "\C-m" 'org-return)
  16234. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16235. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16236. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16237. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16238. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16239. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16240. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16241. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16242. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16243. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16244. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16245. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16246. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16247. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16248. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16249. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16250. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16251. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16252. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16253. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16254. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16255. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16256. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16257. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16258. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16259. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16260. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16261. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16262. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16263. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16264. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16265. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16266. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16267. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16268. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16269. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16270. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16271. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16272. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16273. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16274. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16275. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16276. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16277. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16278. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16279. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16280. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16281. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16282. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16283. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16284. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16285. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16286. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16287. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16288. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16289. (when (featurep 'xemacs)
  16290. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16291. (defconst org-speed-commands-default
  16292. '(
  16293. ("Outline Navigation")
  16294. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16295. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16296. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16297. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16298. ("u" . (org-speed-move-safe 'outline-up-heading))
  16299. ("j" . org-goto)
  16300. ("g" . (org-refile t))
  16301. ("Outline Visibility")
  16302. ("c" . org-cycle)
  16303. ("C" . org-shifttab)
  16304. (" " . org-display-outline-path)
  16305. ("Outline Structure Editing")
  16306. ("U" . org-shiftmetaup)
  16307. ("D" . org-shiftmetadown)
  16308. ("r" . org-metaright)
  16309. ("l" . org-metaleft)
  16310. ("R" . org-shiftmetaright)
  16311. ("L" . org-shiftmetaleft)
  16312. ("i" . (progn (forward-char 1) (call-interactively
  16313. 'org-insert-heading-respect-content)))
  16314. ("^" . org-sort)
  16315. ("w" . org-refile)
  16316. ("a" . org-archive-subtree-default-with-confirmation)
  16317. ("." . org-mark-subtree)
  16318. ("Clock Commands")
  16319. ("I" . org-clock-in)
  16320. ("O" . org-clock-out)
  16321. ("Meta Data Editing")
  16322. ("t" . org-todo)
  16323. ("0" . (org-priority ?\ ))
  16324. ("1" . (org-priority ?A))
  16325. ("2" . (org-priority ?B))
  16326. ("3" . (org-priority ?C))
  16327. (";" . org-set-tags-command)
  16328. ("e" . org-set-effort)
  16329. ("Agenda Views etc")
  16330. ("v" . org-agenda)
  16331. ("/" . org-sparse-tree)
  16332. ("Misc")
  16333. ("o" . org-open-at-point)
  16334. ("?" . org-speed-command-help)
  16335. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16336. (">" . (org-agenda-remove-restriction-lock))
  16337. )
  16338. "The default speed commands.")
  16339. (defun org-print-speed-command (e)
  16340. (if (> (length (car e)) 1)
  16341. (progn
  16342. (princ "\n")
  16343. (princ (car e))
  16344. (princ "\n")
  16345. (princ (make-string (length (car e)) ?-))
  16346. (princ "\n"))
  16347. (princ (car e))
  16348. (princ " ")
  16349. (if (symbolp (cdr e))
  16350. (princ (symbol-name (cdr e)))
  16351. (prin1 (cdr e)))
  16352. (princ "\n")))
  16353. (defun org-speed-command-help ()
  16354. "Show the available speed commands."
  16355. (interactive)
  16356. (if (not org-use-speed-commands)
  16357. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16358. (with-output-to-temp-buffer "*Help*"
  16359. (princ "User-defined Speed commands\n===========================\n")
  16360. (mapc 'org-print-speed-command org-speed-commands-user)
  16361. (princ "\n")
  16362. (princ "Built-in Speed commands\n=======================\n")
  16363. (mapc 'org-print-speed-command org-speed-commands-default))
  16364. (with-current-buffer "*Help*"
  16365. (setq truncate-lines t))))
  16366. (defun org-speed-move-safe (cmd)
  16367. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16368. If not, return to the original position and throw an error."
  16369. (interactive)
  16370. (let ((pos (point)))
  16371. (call-interactively cmd)
  16372. (unless (and (bolp) (org-at-heading-p))
  16373. (goto-char pos)
  16374. (error "Boundary reached while executing %s" cmd))))
  16375. (defvar org-self-insert-command-undo-counter 0)
  16376. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16377. (defvar org-speed-command nil)
  16378. (defun org-speed-command-default-hook (keys)
  16379. "Hook for activating single-letter speed commands.
  16380. `org-speed-commands-default' specifies a minimal command set.
  16381. Use `org-speed-commands-user' for further customization."
  16382. (when (or (and (bolp) (looking-at org-outline-regexp))
  16383. (and (functionp org-use-speed-commands)
  16384. (funcall org-use-speed-commands)))
  16385. (cdr (assoc keys (append org-speed-commands-user
  16386. org-speed-commands-default)))))
  16387. (defun org-babel-speed-command-hook (keys)
  16388. "Hook for activating single-letter code block commands."
  16389. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16390. (cdr (assoc keys org-babel-key-bindings))))
  16391. (defcustom org-speed-command-hook
  16392. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16393. "Hook for activating speed commands at strategic locations.
  16394. Hook functions are called in sequence until a valid handler is
  16395. found.
  16396. Each hook takes a single argument, a user-pressed command key
  16397. which is also a `self-insert-command' from the global map.
  16398. Within the hook, examine the cursor position and the command key
  16399. and return nil or a valid handler as appropriate. Handler could
  16400. be one of an interactive command, a function, or a form.
  16401. Set `org-use-speed-commands' to non-nil value to enable this
  16402. hook. The default setting is `org-speed-command-default-hook'."
  16403. :group 'org-structure
  16404. :version "24.1"
  16405. :type 'hook)
  16406. (defun org-self-insert-command (N)
  16407. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16408. If the cursor is in a table looking at whitespace, the whitespace is
  16409. overwritten, and the table is not marked as requiring realignment."
  16410. (interactive "p")
  16411. (org-check-before-invisible-edit 'insert)
  16412. (cond
  16413. ((and org-use-speed-commands
  16414. (setq org-speed-command
  16415. (run-hook-with-args-until-success
  16416. 'org-speed-command-hook (this-command-keys))))
  16417. (cond
  16418. ((commandp org-speed-command)
  16419. (setq this-command org-speed-command)
  16420. (call-interactively org-speed-command))
  16421. ((functionp org-speed-command)
  16422. (funcall org-speed-command))
  16423. ((and org-speed-command (listp org-speed-command))
  16424. (eval org-speed-command))
  16425. (t (let (org-use-speed-commands)
  16426. (call-interactively 'org-self-insert-command)))))
  16427. ((and
  16428. (org-table-p)
  16429. (progn
  16430. ;; check if we blank the field, and if that triggers align
  16431. (and (featurep 'org-table) org-table-auto-blank-field
  16432. (member last-command
  16433. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16434. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16435. ;; got extra space, this field does not determine column width
  16436. (let (org-table-may-need-update) (org-table-blank-field))
  16437. ;; no extra space, this field may determine column width
  16438. (org-table-blank-field)))
  16439. t)
  16440. (eq N 1)
  16441. (looking-at "[^|\n]* |"))
  16442. (let (org-table-may-need-update)
  16443. (goto-char (1- (match-end 0)))
  16444. (backward-delete-char 1)
  16445. (goto-char (match-beginning 0))
  16446. (self-insert-command N)))
  16447. (t
  16448. (setq org-table-may-need-update t)
  16449. (self-insert-command N)
  16450. (org-fix-tags-on-the-fly)
  16451. (if org-self-insert-cluster-for-undo
  16452. (if (not (eq last-command 'org-self-insert-command))
  16453. (setq org-self-insert-command-undo-counter 1)
  16454. (if (>= org-self-insert-command-undo-counter 20)
  16455. (setq org-self-insert-command-undo-counter 1)
  16456. (and (> org-self-insert-command-undo-counter 0)
  16457. buffer-undo-list (listp buffer-undo-list)
  16458. (not (cadr buffer-undo-list)) ; remove nil entry
  16459. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16460. (setq org-self-insert-command-undo-counter
  16461. (1+ org-self-insert-command-undo-counter))))))))
  16462. (defun org-check-before-invisible-edit (kind)
  16463. "Check is editing if kind KIND would be dangerous with invisible text around.
  16464. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16465. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16466. (if (and org-catch-invisible-edits
  16467. (or (not (boundp 'visible-mode)) (not visible-mode))
  16468. (or (get-char-property (point) 'invisible)
  16469. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16470. ;; OK, we need to take a closer look
  16471. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16472. (invisible-before-point (if (bobp) nil (get-char-property
  16473. (1- (point)) 'invisible)))
  16474. (border-and-ok-direction
  16475. (or
  16476. ;; Check if we are acting predictably before invisible text
  16477. (and invisible-at-point (not invisible-before-point)
  16478. (memq kind '(insert delete-backward)))
  16479. ;; Check if we are acting predictably after invisible text
  16480. ;; This works not well, and I have turned it off. It seems
  16481. ;; better to always show and stop after invisible text.
  16482. ;; (and (not invisible-at-point) invisible-before-point
  16483. ;; (memq kind '(insert delete)))
  16484. )))
  16485. (when (or (memq invisible-at-point '(outline org-hide-block))
  16486. (memq invisible-before-point '(outline org-hide-block)))
  16487. (if (eq org-catch-invisible-edits 'error)
  16488. (error "Editing in invisible areas is prohibited - make visible first"))
  16489. ;; Make the area visible
  16490. (save-excursion
  16491. (if invisible-before-point
  16492. (goto-char (previous-single-char-property-change
  16493. (point) 'invisible)))
  16494. (org-cycle))
  16495. (cond
  16496. ((eq org-catch-invisible-edits 'show)
  16497. ;; That's it, we do the edit after showing
  16498. (message
  16499. "Unfolding invisible region around point before editing")
  16500. (sit-for 1))
  16501. ((and (eq org-catch-invisible-edits 'smart)
  16502. border-and-ok-direction)
  16503. (message "Unfolding invisible region around point before editing"))
  16504. (t
  16505. ;; Don't do the edit, make the user repeat it in full visibility
  16506. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16507. (defun org-fix-tags-on-the-fly ()
  16508. (when (and (equal (char-after (point-at-bol)) ?*)
  16509. (org-at-heading-p))
  16510. (org-align-tags-here org-tags-column)))
  16511. (defun org-delete-backward-char (N)
  16512. "Like `delete-backward-char', insert whitespace at field end in tables.
  16513. When deleting backwards, in tables this function will insert whitespace in
  16514. front of the next \"|\" separator, to keep the table aligned. The table will
  16515. still be marked for re-alignment if the field did fill the entire column,
  16516. because, in this case the deletion might narrow the column."
  16517. (interactive "p")
  16518. (org-check-before-invisible-edit 'delete-backward)
  16519. (if (and (org-table-p)
  16520. (eq N 1)
  16521. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16522. (looking-at ".*?|"))
  16523. (let ((pos (point))
  16524. (noalign (looking-at "[^|\n\r]* |"))
  16525. (c org-table-may-need-update))
  16526. (backward-delete-char N)
  16527. (if (not overwrite-mode)
  16528. (progn
  16529. (skip-chars-forward "^|")
  16530. (insert " ")
  16531. (goto-char (1- pos))))
  16532. ;; noalign: if there were two spaces at the end, this field
  16533. ;; does not determine the width of the column.
  16534. (if noalign (setq org-table-may-need-update c)))
  16535. (backward-delete-char N)
  16536. (org-fix-tags-on-the-fly)))
  16537. (defun org-delete-char (N)
  16538. "Like `delete-char', but insert whitespace at field end in tables.
  16539. When deleting characters, in tables this function will insert whitespace in
  16540. front of the next \"|\" separator, to keep the table aligned. The table will
  16541. still be marked for re-alignment if the field did fill the entire column,
  16542. because, in this case the deletion might narrow the column."
  16543. (interactive "p")
  16544. (org-check-before-invisible-edit 'delete)
  16545. (if (and (org-table-p)
  16546. (not (bolp))
  16547. (not (= (char-after) ?|))
  16548. (eq N 1))
  16549. (if (looking-at ".*?|")
  16550. (let ((pos (point))
  16551. (noalign (looking-at "[^|\n\r]* |"))
  16552. (c org-table-may-need-update))
  16553. (replace-match (concat
  16554. (substring (match-string 0) 1 -1)
  16555. " |"))
  16556. (goto-char pos)
  16557. ;; noalign: if there were two spaces at the end, this field
  16558. ;; does not determine the width of the column.
  16559. (if noalign (setq org-table-may-need-update c)))
  16560. (delete-char N))
  16561. (delete-char N)
  16562. (org-fix-tags-on-the-fly)))
  16563. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16564. (put 'org-self-insert-command 'delete-selection t)
  16565. (put 'orgtbl-self-insert-command 'delete-selection t)
  16566. (put 'org-delete-char 'delete-selection 'supersede)
  16567. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16568. (put 'org-yank 'delete-selection 'yank)
  16569. ;; Make `flyspell-mode' delay after some commands
  16570. (put 'org-self-insert-command 'flyspell-delayed t)
  16571. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16572. (put 'org-delete-char 'flyspell-delayed t)
  16573. (put 'org-delete-backward-char 'flyspell-delayed t)
  16574. ;; Make pabbrev-mode expand after org-mode commands
  16575. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16576. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16577. ;; How to do this: Measure non-white length of current string
  16578. ;; If equal to column width, we should realign.
  16579. (defun org-remap (map &rest commands)
  16580. "In MAP, remap the functions given in COMMANDS.
  16581. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16582. (let (new old)
  16583. (while commands
  16584. (setq old (pop commands) new (pop commands))
  16585. (if (fboundp 'command-remapping)
  16586. (org-defkey map (vector 'remap old) new)
  16587. (substitute-key-definition old new map global-map)))))
  16588. (when (eq org-enable-table-editor 'optimized)
  16589. ;; If the user wants maximum table support, we need to hijack
  16590. ;; some standard editing functions
  16591. (org-remap org-mode-map
  16592. 'self-insert-command 'org-self-insert-command
  16593. 'delete-char 'org-delete-char
  16594. 'delete-backward-char 'org-delete-backward-char)
  16595. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16596. (defvar org-ctrl-c-ctrl-c-hook nil
  16597. "Hook for functions attaching themselves to `C-c C-c'.
  16598. This can be used to add additional functionality to the C-c C-c
  16599. key which executes context-dependent commands. This hook is run
  16600. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16601. run after the last test.
  16602. Each function will be called with no arguments. The function
  16603. must check if the context is appropriate for it to act. If yes,
  16604. it should do its thing and then return a non-nil value. If the
  16605. context is wrong, just do nothing and return nil.")
  16606. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16607. "Hook for functions attaching themselves to `C-c C-c'.
  16608. This can be used to add additional functionality to the C-c C-c
  16609. key which executes context-dependent commands. This hook is run
  16610. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16611. before the first test.
  16612. Each function will be called with no arguments. The function
  16613. must check if the context is appropriate for it to act. If yes,
  16614. it should do its thing and then return a non-nil value. If the
  16615. context is wrong, just do nothing and return nil.")
  16616. (defvar org-tab-first-hook nil
  16617. "Hook for functions to attach themselves to TAB.
  16618. See `org-ctrl-c-ctrl-c-hook' for more information.
  16619. This hook runs as the first action when TAB is pressed, even before
  16620. `org-cycle' messes around with the `outline-regexp' to cater for
  16621. inline tasks and plain list item folding.
  16622. If any function in this hook returns t, any other actions that
  16623. would have been caused by TAB (such as table field motion or visibility
  16624. cycling) will not occur.")
  16625. (defvar org-tab-after-check-for-table-hook nil
  16626. "Hook for functions to attach themselves to TAB.
  16627. See `org-ctrl-c-ctrl-c-hook' for more information.
  16628. This hook runs after it has been established that the cursor is not in a
  16629. table, but before checking if the cursor is in a headline or if global cycling
  16630. should be done.
  16631. If any function in this hook returns t, not other actions like visibility
  16632. cycling will be done.")
  16633. (defvar org-tab-after-check-for-cycling-hook nil
  16634. "Hook for functions to attach themselves to TAB.
  16635. See `org-ctrl-c-ctrl-c-hook' for more information.
  16636. This hook runs after it has been established that not table field motion and
  16637. not visibility should be done because of current context. This is probably
  16638. the place where a package like yasnippets can hook in.")
  16639. (defvar org-tab-before-tab-emulation-hook nil
  16640. "Hook for functions to attach themselves to TAB.
  16641. See `org-ctrl-c-ctrl-c-hook' for more information.
  16642. This hook runs after every other options for TAB have been exhausted, but
  16643. before indentation and \t insertion takes place.")
  16644. (defvar org-metaleft-hook nil
  16645. "Hook for functions attaching themselves to `M-left'.
  16646. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16647. (defvar org-metaright-hook nil
  16648. "Hook for functions attaching themselves to `M-right'.
  16649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16650. (defvar org-metaup-hook nil
  16651. "Hook for functions attaching themselves to `M-up'.
  16652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16653. (defvar org-metadown-hook nil
  16654. "Hook for functions attaching themselves to `M-down'.
  16655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16656. (defvar org-shiftmetaleft-hook nil
  16657. "Hook for functions attaching themselves to `M-S-left'.
  16658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16659. (defvar org-shiftmetaright-hook nil
  16660. "Hook for functions attaching themselves to `M-S-right'.
  16661. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16662. (defvar org-shiftmetaup-hook nil
  16663. "Hook for functions attaching themselves to `M-S-up'.
  16664. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16665. (defvar org-shiftmetadown-hook nil
  16666. "Hook for functions attaching themselves to `M-S-down'.
  16667. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16668. (defvar org-metareturn-hook nil
  16669. "Hook for functions attaching themselves to `M-RET'.
  16670. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16671. (defvar org-shiftup-hook nil
  16672. "Hook for functions attaching themselves to `S-up'.
  16673. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16674. (defvar org-shiftup-final-hook nil
  16675. "Hook for functions attaching themselves to `S-up'.
  16676. This one runs after all other options except shift-select have been excluded.
  16677. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16678. (defvar org-shiftdown-hook nil
  16679. "Hook for functions attaching themselves to `S-down'.
  16680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16681. (defvar org-shiftdown-final-hook nil
  16682. "Hook for functions attaching themselves to `S-down'.
  16683. This one runs after all other options except shift-select have been excluded.
  16684. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16685. (defvar org-shiftleft-hook nil
  16686. "Hook for functions attaching themselves to `S-left'.
  16687. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16688. (defvar org-shiftleft-final-hook nil
  16689. "Hook for functions attaching themselves to `S-left'.
  16690. This one runs after all other options except shift-select have been excluded.
  16691. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16692. (defvar org-shiftright-hook nil
  16693. "Hook for functions attaching themselves to `S-right'.
  16694. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16695. (defvar org-shiftright-final-hook nil
  16696. "Hook for functions attaching themselves to `S-right'.
  16697. This one runs after all other options except shift-select have been excluded.
  16698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16699. (defun org-modifier-cursor-error ()
  16700. "Throw an error, a modified cursor command was applied in wrong context."
  16701. (error "This command is active in special context like tables, headlines or items"))
  16702. (defun org-shiftselect-error ()
  16703. "Throw an error because Shift-Cursor command was applied in wrong context."
  16704. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16705. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16706. (error "This command works only in special context like headlines or timestamps")))
  16707. (defun org-call-for-shift-select (cmd)
  16708. (let ((this-command-keys-shift-translated t))
  16709. (call-interactively cmd)))
  16710. (defun org-shifttab (&optional arg)
  16711. "Global visibility cycling or move to previous table field.
  16712. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16713. on context.
  16714. See the individual commands for more information."
  16715. (interactive "P")
  16716. (cond
  16717. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16718. ((integerp arg)
  16719. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16720. (message "Content view to level: %d" arg)
  16721. (org-content (prefix-numeric-value arg2))
  16722. (setq org-cycle-global-status 'overview)))
  16723. (t (call-interactively 'org-global-cycle))))
  16724. (defun org-shiftmetaleft ()
  16725. "Promote subtree or delete table column.
  16726. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16727. `org-table-delete-column', depending on context. See the
  16728. individual commands for more information."
  16729. (interactive)
  16730. (cond
  16731. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16732. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16733. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16734. ((if (not (org-region-active-p)) (org-at-item-p)
  16735. (save-excursion (goto-char (region-beginning))
  16736. (org-at-item-p)))
  16737. (call-interactively 'org-outdent-item-tree))
  16738. (t (org-modifier-cursor-error))))
  16739. (defun org-shiftmetaright ()
  16740. "Demote subtree or insert table column.
  16741. Calls `org-demote-subtree', `org-indent-item-tree', or
  16742. `org-table-insert-column', depending on context. See the
  16743. individual commands for more information."
  16744. (interactive)
  16745. (cond
  16746. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16747. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16748. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16749. ((if (not (org-region-active-p)) (org-at-item-p)
  16750. (save-excursion (goto-char (region-beginning))
  16751. (org-at-item-p)))
  16752. (call-interactively 'org-indent-item-tree))
  16753. (t (org-modifier-cursor-error))))
  16754. (defun org-shiftmetaup (&optional arg)
  16755. "Move subtree up or kill table row.
  16756. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16757. `org-move-item-up' depending on context. See the individual commands
  16758. for more information."
  16759. (interactive "P")
  16760. (cond
  16761. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16762. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16763. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16764. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16765. (t (org-modifier-cursor-error))))
  16766. (defun org-shiftmetadown (&optional arg)
  16767. "Move subtree down or insert table row.
  16768. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16769. `org-move-item-down', depending on context. See the individual
  16770. commands for more information."
  16771. (interactive "P")
  16772. (cond
  16773. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16774. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16775. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16776. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16777. (t (org-modifier-cursor-error))))
  16778. (defsubst org-hidden-tree-error ()
  16779. (error
  16780. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16781. (defun org-metaleft (&optional arg)
  16782. "Promote heading or move table column to left.
  16783. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16784. With no specific context, calls the Emacs default `backward-word'.
  16785. See the individual commands for more information."
  16786. (interactive "P")
  16787. (cond
  16788. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16789. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16790. ((org-with-limited-levels
  16791. (or (org-at-heading-p)
  16792. (and (org-region-active-p)
  16793. (save-excursion
  16794. (goto-char (region-beginning))
  16795. (org-at-heading-p)))))
  16796. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16797. (call-interactively 'org-do-promote))
  16798. ;; At an inline task.
  16799. ((org-at-heading-p)
  16800. (call-interactively 'org-inlinetask-promote))
  16801. ((or (org-at-item-p)
  16802. (and (org-region-active-p)
  16803. (save-excursion
  16804. (goto-char (region-beginning))
  16805. (org-at-item-p))))
  16806. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16807. (call-interactively 'org-outdent-item))
  16808. (t (call-interactively 'backward-word))))
  16809. (defun org-metaright (&optional arg)
  16810. "Demote a subtree, a list item or move table column to right.
  16811. In front of a drawer or a block keyword, indent it correctly.
  16812. With no specific context, calls the Emacs default `forward-word'.
  16813. See the individual commands for more information."
  16814. (interactive "P")
  16815. (cond
  16816. ((run-hook-with-args-until-success 'org-metaright-hook))
  16817. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16818. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16819. ((org-at-block-p) (call-interactively 'org-indent-block))
  16820. ((org-with-limited-levels
  16821. (or (org-at-heading-p)
  16822. (and (org-region-active-p)
  16823. (save-excursion
  16824. (goto-char (region-beginning))
  16825. (org-at-heading-p)))))
  16826. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16827. (call-interactively 'org-do-demote))
  16828. ;; At an inline task.
  16829. ((org-at-heading-p)
  16830. (call-interactively 'org-inlinetask-demote))
  16831. ((or (org-at-item-p)
  16832. (and (org-region-active-p)
  16833. (save-excursion
  16834. (goto-char (region-beginning))
  16835. (org-at-item-p))))
  16836. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16837. (call-interactively 'org-indent-item))
  16838. (t (call-interactively 'forward-word))))
  16839. (defun org-check-for-hidden (what)
  16840. "Check if there are hidden headlines/items in the current visual line.
  16841. WHAT can be either `headlines' or `items'. If the current line is
  16842. an outline or item heading and it has a folded subtree below it,
  16843. this function returns t, nil otherwise."
  16844. (let ((re (cond
  16845. ((eq what 'headlines) org-outline-regexp-bol)
  16846. ((eq what 'items) (org-item-beginning-re))
  16847. (t (error "This should not happen"))))
  16848. beg end)
  16849. (save-excursion
  16850. (catch 'exit
  16851. (unless (org-region-active-p)
  16852. (setq beg (point-at-bol))
  16853. (beginning-of-line 2)
  16854. (while (and (not (eobp)) ;; this is like `next-line'
  16855. (get-char-property (1- (point)) 'invisible))
  16856. (beginning-of-line 2))
  16857. (setq end (point))
  16858. (goto-char beg)
  16859. (goto-char (point-at-eol))
  16860. (setq end (max end (point)))
  16861. (while (re-search-forward re end t)
  16862. (if (get-char-property (match-beginning 0) 'invisible)
  16863. (throw 'exit t))))
  16864. nil))))
  16865. (defun org-metaup (&optional arg)
  16866. "Move subtree up or move table row up.
  16867. Calls `org-move-subtree-up' or `org-table-move-row' or
  16868. `org-move-item-up', depending on context. See the individual commands
  16869. for more information."
  16870. (interactive "P")
  16871. (cond
  16872. ((run-hook-with-args-until-success 'org-metaup-hook))
  16873. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16874. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16875. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16876. (t (transpose-lines 1) (beginning-of-line -1))))
  16877. (defun org-metadown (&optional arg)
  16878. "Move subtree down or move table row down.
  16879. Calls `org-move-subtree-down' or `org-table-move-row' or
  16880. `org-move-item-down', depending on context. See the individual
  16881. commands for more information."
  16882. (interactive "P")
  16883. (cond
  16884. ((run-hook-with-args-until-success 'org-metadown-hook))
  16885. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16886. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16887. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16888. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16889. (defun org-shiftup (&optional arg)
  16890. "Increase item in timestamp or increase priority of current headline.
  16891. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16892. depending on context. See the individual commands for more information."
  16893. (interactive "P")
  16894. (cond
  16895. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16896. ((and org-support-shift-select (org-region-active-p))
  16897. (org-call-for-shift-select 'previous-line))
  16898. ((org-at-timestamp-p t)
  16899. (call-interactively (if org-edit-timestamp-down-means-later
  16900. 'org-timestamp-down 'org-timestamp-up)))
  16901. ((and (not (eq org-support-shift-select 'always))
  16902. org-enable-priority-commands
  16903. (org-at-heading-p))
  16904. (call-interactively 'org-priority-up))
  16905. ((and (not org-support-shift-select) (org-at-item-p))
  16906. (call-interactively 'org-previous-item))
  16907. ((org-clocktable-try-shift 'up arg))
  16908. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16909. (org-support-shift-select
  16910. (org-call-for-shift-select 'previous-line))
  16911. (t (org-shiftselect-error))))
  16912. (defun org-shiftdown (&optional arg)
  16913. "Decrease item in timestamp or decrease priority of current headline.
  16914. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16915. depending on context. See the individual commands for more information."
  16916. (interactive "P")
  16917. (cond
  16918. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16919. ((and org-support-shift-select (org-region-active-p))
  16920. (org-call-for-shift-select 'next-line))
  16921. ((org-at-timestamp-p t)
  16922. (call-interactively (if org-edit-timestamp-down-means-later
  16923. 'org-timestamp-up 'org-timestamp-down)))
  16924. ((and (not (eq org-support-shift-select 'always))
  16925. org-enable-priority-commands
  16926. (org-at-heading-p))
  16927. (call-interactively 'org-priority-down))
  16928. ((and (not org-support-shift-select) (org-at-item-p))
  16929. (call-interactively 'org-next-item))
  16930. ((org-clocktable-try-shift 'down arg))
  16931. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16932. (org-support-shift-select
  16933. (org-call-for-shift-select 'next-line))
  16934. (t (org-shiftselect-error))))
  16935. (defun org-shiftright (&optional arg)
  16936. "Cycle the thing at point or in the current line, depending on context.
  16937. Depending on context, this does one of the following:
  16938. - switch a timestamp at point one day into the future
  16939. - on a headline, switch to the next TODO keyword.
  16940. - on an item, switch entire list to the next bullet type
  16941. - on a property line, switch to the next allowed value
  16942. - on a clocktable definition line, move time block into the future"
  16943. (interactive "P")
  16944. (cond
  16945. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16946. ((and org-support-shift-select (org-region-active-p))
  16947. (org-call-for-shift-select 'forward-char))
  16948. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16949. ((and (not (eq org-support-shift-select 'always))
  16950. (org-at-heading-p))
  16951. (let ((org-inhibit-logging
  16952. (not org-treat-S-cursor-todo-selection-as-state-change))
  16953. (org-inhibit-blocking
  16954. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16955. (org-call-with-arg 'org-todo 'right)))
  16956. ((or (and org-support-shift-select
  16957. (not (eq org-support-shift-select 'always))
  16958. (org-at-item-bullet-p))
  16959. (and (not org-support-shift-select) (org-at-item-p)))
  16960. (org-call-with-arg 'org-cycle-list-bullet nil))
  16961. ((and (not (eq org-support-shift-select 'always))
  16962. (org-at-property-p))
  16963. (call-interactively 'org-property-next-allowed-value))
  16964. ((org-clocktable-try-shift 'right arg))
  16965. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16966. (org-support-shift-select
  16967. (org-call-for-shift-select 'forward-char))
  16968. (t (org-shiftselect-error))))
  16969. (defun org-shiftleft (&optional arg)
  16970. "Cycle the thing at point or in the current line, depending on context.
  16971. Depending on context, this does one of the following:
  16972. - switch a timestamp at point one day into the past
  16973. - on a headline, switch to the previous TODO keyword.
  16974. - on an item, switch entire list to the previous bullet type
  16975. - on a property line, switch to the previous allowed value
  16976. - on a clocktable definition line, move time block into the past"
  16977. (interactive "P")
  16978. (cond
  16979. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16980. ((and org-support-shift-select (org-region-active-p))
  16981. (org-call-for-shift-select 'backward-char))
  16982. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16983. ((and (not (eq org-support-shift-select 'always))
  16984. (org-at-heading-p))
  16985. (let ((org-inhibit-logging
  16986. (not org-treat-S-cursor-todo-selection-as-state-change))
  16987. (org-inhibit-blocking
  16988. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16989. (org-call-with-arg 'org-todo 'left)))
  16990. ((or (and org-support-shift-select
  16991. (not (eq org-support-shift-select 'always))
  16992. (org-at-item-bullet-p))
  16993. (and (not org-support-shift-select) (org-at-item-p)))
  16994. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16995. ((and (not (eq org-support-shift-select 'always))
  16996. (org-at-property-p))
  16997. (call-interactively 'org-property-previous-allowed-value))
  16998. ((org-clocktable-try-shift 'left arg))
  16999. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17000. (org-support-shift-select
  17001. (org-call-for-shift-select 'backward-char))
  17002. (t (org-shiftselect-error))))
  17003. (defun org-shiftcontrolright ()
  17004. "Switch to next TODO set."
  17005. (interactive)
  17006. (cond
  17007. ((and org-support-shift-select (org-region-active-p))
  17008. (org-call-for-shift-select 'forward-word))
  17009. ((and (not (eq org-support-shift-select 'always))
  17010. (org-at-heading-p))
  17011. (org-call-with-arg 'org-todo 'nextset))
  17012. (org-support-shift-select
  17013. (org-call-for-shift-select 'forward-word))
  17014. (t (org-shiftselect-error))))
  17015. (defun org-shiftcontrolleft ()
  17016. "Switch to previous TODO set."
  17017. (interactive)
  17018. (cond
  17019. ((and org-support-shift-select (org-region-active-p))
  17020. (org-call-for-shift-select 'backward-word))
  17021. ((and (not (eq org-support-shift-select 'always))
  17022. (org-at-heading-p))
  17023. (org-call-with-arg 'org-todo 'previousset))
  17024. (org-support-shift-select
  17025. (org-call-for-shift-select 'backward-word))
  17026. (t (org-shiftselect-error))))
  17027. (defun org-shiftcontrolup ()
  17028. "Change timestamps synchronously up in CLOCK log lines."
  17029. (interactive)
  17030. (cond ((and (not org-support-shift-select)
  17031. (org-at-clock-log-p)
  17032. (org-at-timestamp-p t))
  17033. (org-clock-timestamps-up))
  17034. (t (org-shiftselect-error))))
  17035. (defun org-shiftcontroldown ()
  17036. "Change timestamps synchronously down in CLOCK log lines."
  17037. (interactive)
  17038. (cond ((and (not org-support-shift-select)
  17039. (org-at-clock-log-p)
  17040. (org-at-timestamp-p t))
  17041. (org-clock-timestamps-down))
  17042. (t (org-shiftselect-error))))
  17043. (defun org-ctrl-c-ret ()
  17044. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17045. (interactive)
  17046. (cond
  17047. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17048. (t (call-interactively 'org-insert-heading))))
  17049. (defun org-find-visible ()
  17050. (let ((s (point)))
  17051. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17052. (get-char-property s 'invisible)))
  17053. s))
  17054. (defun org-find-invisible ()
  17055. (let ((s (point)))
  17056. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17057. (not (get-char-property s 'invisible))))
  17058. s))
  17059. (defun org-copy-visible (beg end)
  17060. "Copy the visible parts of the region."
  17061. (interactive "r")
  17062. (let (snippets s)
  17063. (save-excursion
  17064. (save-restriction
  17065. (narrow-to-region beg end)
  17066. (setq s (goto-char (point-min)))
  17067. (while (not (= (point) (point-max)))
  17068. (goto-char (org-find-invisible))
  17069. (push (buffer-substring s (point)) snippets)
  17070. (setq s (goto-char (org-find-visible))))))
  17071. (kill-new (apply 'concat (nreverse snippets)))))
  17072. (defun org-copy-special ()
  17073. "Copy region in table or copy current subtree.
  17074. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17075. See the individual commands for more information."
  17076. (interactive)
  17077. (call-interactively
  17078. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17079. (defun org-cut-special ()
  17080. "Cut region in table or cut current subtree.
  17081. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17082. See the individual commands for more information."
  17083. (interactive)
  17084. (call-interactively
  17085. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17086. (defun org-paste-special (arg)
  17087. "Paste rectangular region into table, or past subtree relative to level.
  17088. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17089. See the individual commands for more information."
  17090. (interactive "P")
  17091. (if (org-at-table-p)
  17092. (org-table-paste-rectangle)
  17093. (org-paste-subtree arg)))
  17094. (defun org-edit-special (&optional arg)
  17095. "Call a special editor for the stuff at point.
  17096. When at a table, call the formula editor with `org-table-edit-formulas'.
  17097. When at the first line of an src example, call `org-edit-src-code'.
  17098. When in an #+include line, visit the include file. Otherwise call
  17099. `ffap' to visit the file at point."
  17100. (interactive)
  17101. ;; possibly prep session before editing source
  17102. (when arg
  17103. (let* ((info (org-babel-get-src-block-info))
  17104. (lang (nth 0 info))
  17105. (params (nth 2 info))
  17106. (session (cdr (assoc :session params))))
  17107. (when (and info session) ;; we are in a source-code block with a session
  17108. (funcall
  17109. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17110. (cond ;; proceed with `org-edit-special'
  17111. ((save-excursion
  17112. (beginning-of-line 1)
  17113. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17114. (find-file (org-trim (match-string 1))))
  17115. ((org-edit-src-code))
  17116. ((org-edit-fixed-width-region))
  17117. ((org-at-table.el-p)
  17118. (org-edit-src-code))
  17119. ((or (org-at-table-p)
  17120. (save-excursion
  17121. (beginning-of-line 1)
  17122. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17123. (call-interactively 'org-table-edit-formulas))
  17124. (t (call-interactively 'ffap))))
  17125. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17126. (defun org-ctrl-c-ctrl-c (&optional arg)
  17127. "Set tags in headline, or update according to changed information at point.
  17128. This command does many different things, depending on context:
  17129. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17130. this is what we do.
  17131. - If the cursor is on a statistics cookie, update it.
  17132. - If the cursor is in a headline, prompt for tags and insert them
  17133. into the current line, aligned to `org-tags-column'. When called
  17134. with prefix arg, realign all tags in the current buffer.
  17135. - If the cursor is in one of the special #+KEYWORD lines, this
  17136. triggers scanning the buffer for these lines and updating the
  17137. information.
  17138. - If the cursor is inside a table, realign the table. This command
  17139. works even if the automatic table editor has been turned off.
  17140. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17141. the entire table.
  17142. - If the cursor is at a footnote reference or definition, jump to
  17143. the corresponding definition or references, respectively.
  17144. - If the cursor is a the beginning of a dynamic block, update it.
  17145. - If the current buffer is a capture buffer, close note and file it.
  17146. - If the cursor is on a <<<target>>>, update radio targets and
  17147. corresponding links in this buffer.
  17148. - If the cursor is on a numbered item in a plain list, renumber the
  17149. ordered list.
  17150. - If the cursor is on a checkbox, toggle it.
  17151. - If the cursor is on a code block, evaluate it. The variable
  17152. `org-confirm-babel-evaluate' can be used to control prompting
  17153. before code block evaluation, by default every code block
  17154. evaluation requires confirmation. Code block evaluation can be
  17155. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17156. (interactive "P")
  17157. (let ((org-enable-table-editor t))
  17158. (cond
  17159. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17160. org-occur-highlights
  17161. org-latex-fragment-image-overlays)
  17162. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17163. (org-remove-occur-highlights)
  17164. (org-remove-latex-fragment-image-overlays)
  17165. (message "Temporary highlights/overlays removed from current buffer"))
  17166. ((and (local-variable-p 'org-finish-function (current-buffer))
  17167. (fboundp org-finish-function))
  17168. (funcall org-finish-function))
  17169. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17170. ((org-in-regexp org-ts-regexp-both)
  17171. (org-timestamp-change 0 'day))
  17172. ((or (looking-at org-property-start-re)
  17173. (org-at-property-p))
  17174. (call-interactively 'org-property-action))
  17175. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17176. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17177. (or (org-at-heading-p) (org-at-item-p)))
  17178. (call-interactively 'org-update-statistics-cookies))
  17179. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17180. ((org-at-table.el-p)
  17181. (message "Use C-c ' to edit table.el tables"))
  17182. ((org-at-table-p)
  17183. (org-table-maybe-eval-formula)
  17184. (if arg
  17185. (call-interactively 'org-table-recalculate)
  17186. (org-table-maybe-recalculate-line))
  17187. (call-interactively 'org-table-align)
  17188. (orgtbl-send-table 'maybe))
  17189. ((or (org-footnote-at-reference-p)
  17190. (org-footnote-at-definition-p))
  17191. (call-interactively 'org-footnote-action))
  17192. ((org-at-item-checkbox-p)
  17193. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17194. ;; list only if at top item.
  17195. (let* ((cbox (match-string 1))
  17196. (struct (org-list-struct))
  17197. (old-struct (copy-tree struct))
  17198. (parents (org-list-parents-alist struct))
  17199. (orderedp (org-entry-get nil "ORDERED"))
  17200. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17201. block-item)
  17202. ;; Use a light version of `org-toggle-checkbox' to avoid
  17203. ;; computing list structure twice.
  17204. (let ((new-box (cond
  17205. ((equal arg '(16)) "[-]")
  17206. ((equal arg '(4)) nil)
  17207. ((equal "[X]" cbox) "[ ]")
  17208. (t "[X]"))))
  17209. (if (and firstp arg)
  17210. ;; If at first item of sub-list, remove check-box from
  17211. ;; every item at the same level.
  17212. (mapc
  17213. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17214. (org-list-get-all-items
  17215. (point-at-bol) struct (org-list-prevs-alist struct)))
  17216. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17217. ;; Replicate `org-list-write-struct', while grabbing a return
  17218. ;; value from `org-list-struct-fix-box'.
  17219. (org-list-struct-fix-ind struct parents 2)
  17220. (org-list-struct-fix-item-end struct)
  17221. (let ((prevs (org-list-prevs-alist struct)))
  17222. (org-list-struct-fix-bul struct prevs)
  17223. (org-list-struct-fix-ind struct parents)
  17224. (setq block-item
  17225. (org-list-struct-fix-box struct parents prevs orderedp)))
  17226. (org-list-struct-apply-struct struct old-struct)
  17227. (org-update-checkbox-count-maybe)
  17228. (when block-item
  17229. (message
  17230. "Checkboxes were removed due to unchecked box at line %d"
  17231. (org-current-line block-item)))
  17232. (when firstp (org-list-send-list 'maybe))))
  17233. ((org-at-item-p)
  17234. ;; Cursor at an item: repair list. Do checkbox related actions
  17235. ;; only if function was called with an argument. Send list only
  17236. ;; if at top item.
  17237. (let* ((struct (org-list-struct))
  17238. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17239. old-struct)
  17240. (when arg
  17241. (setq old-struct (copy-tree struct))
  17242. (if firstp
  17243. ;; If at first item of sub-list, add check-box to every
  17244. ;; item at the same level.
  17245. (mapc
  17246. (lambda (pos)
  17247. (unless (org-list-get-checkbox pos struct)
  17248. (org-list-set-checkbox pos struct "[ ]")))
  17249. (org-list-get-all-items
  17250. (point-at-bol) struct (org-list-prevs-alist struct)))
  17251. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17252. (org-list-write-struct
  17253. struct (org-list-parents-alist struct) old-struct)
  17254. (when arg (org-update-checkbox-count-maybe))
  17255. (when firstp (org-list-send-list 'maybe))))
  17256. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17257. ;; Dynamic block
  17258. (beginning-of-line 1)
  17259. (save-excursion (org-update-dblock)))
  17260. ((save-excursion
  17261. (let ((case-fold-search t))
  17262. (beginning-of-line 1)
  17263. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17264. (cond
  17265. ((or (equal (match-string 1) "TBLFM")
  17266. (equal (match-string 1) "tblfm"))
  17267. ;; Recalculate the table before this line
  17268. (save-excursion
  17269. (beginning-of-line 1)
  17270. (skip-chars-backward " \r\n\t")
  17271. (if (org-at-table-p)
  17272. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17273. (t
  17274. (let ((org-inhibit-startup-visibility-stuff t)
  17275. (org-startup-align-all-tables nil))
  17276. (when (boundp 'org-table-coordinate-overlays)
  17277. (mapc 'delete-overlay org-table-coordinate-overlays)
  17278. (setq org-table-coordinate-overlays nil))
  17279. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17280. (message "Local setup has been refreshed"))))
  17281. ((org-clock-update-time-maybe))
  17282. (t
  17283. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17284. (error "C-c C-c can do nothing useful at this location"))))))
  17285. (defun org-mode-restart ()
  17286. "Restart Org-mode, to scan again for special lines.
  17287. Also updates the keyword regular expressions."
  17288. (interactive)
  17289. (org-mode)
  17290. (message "Org-mode restarted"))
  17291. (defun org-kill-note-or-show-branches ()
  17292. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17293. (interactive)
  17294. (if (not org-finish-function)
  17295. (progn
  17296. (hide-subtree)
  17297. (call-interactively 'show-branches))
  17298. (let ((org-note-abort t))
  17299. (funcall org-finish-function))))
  17300. (defun org-return (&optional indent)
  17301. "Goto next table row or insert a newline.
  17302. Calls `org-table-next-row' or `newline', depending on context.
  17303. See the individual commands for more information."
  17304. (interactive)
  17305. (cond
  17306. ((or (bobp) (org-in-src-block-p))
  17307. (if indent (newline-and-indent) (newline)))
  17308. ((org-at-table-p)
  17309. (org-table-justify-field-maybe)
  17310. (call-interactively 'org-table-next-row))
  17311. ;; when `newline-and-indent' is called within a list, make sure
  17312. ;; text moved stays inside the item.
  17313. ((and (org-in-item-p) indent)
  17314. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17315. (progn
  17316. (save-match-data (newline))
  17317. (org-indent-line-to (length (match-string 0))))
  17318. (let ((ind (org-get-indentation)))
  17319. (newline)
  17320. (if (org-looking-back org-list-end-re)
  17321. (org-indent-line-function)
  17322. (org-indent-line-to ind)))))
  17323. ((and org-return-follows-link
  17324. (let ((tprop (get-text-property (point) 'face)))
  17325. (or (eq tprop 'org-link)
  17326. (and (listp tprop) (memq 'org-link tprop)))))
  17327. (call-interactively 'org-open-at-point))
  17328. ((and (org-at-heading-p)
  17329. (looking-at
  17330. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17331. (org-show-entry)
  17332. (end-of-line 1)
  17333. (newline))
  17334. (t (if indent (newline-and-indent) (newline)))))
  17335. (defun org-return-indent ()
  17336. "Goto next table row or insert a newline and indent.
  17337. Calls `org-table-next-row' or `newline-and-indent', depending on
  17338. context. See the individual commands for more information."
  17339. (interactive)
  17340. (org-return t))
  17341. (defun org-ctrl-c-star ()
  17342. "Compute table, or change heading status of lines.
  17343. Calls `org-table-recalculate' or `org-toggle-heading',
  17344. depending on context."
  17345. (interactive)
  17346. (cond
  17347. ((org-at-table-p)
  17348. (call-interactively 'org-table-recalculate))
  17349. (t
  17350. ;; Convert all lines in region to list items
  17351. (call-interactively 'org-toggle-heading))))
  17352. (defun org-ctrl-c-minus ()
  17353. "Insert separator line in table or modify bullet status of line.
  17354. Also turns a plain line or a region of lines into list items.
  17355. Calls `org-table-insert-hline', `org-toggle-item', or
  17356. `org-cycle-list-bullet', depending on context."
  17357. (interactive)
  17358. (cond
  17359. ((org-at-table-p)
  17360. (call-interactively 'org-table-insert-hline))
  17361. ((org-region-active-p)
  17362. (call-interactively 'org-toggle-item))
  17363. ((org-in-item-p)
  17364. (call-interactively 'org-cycle-list-bullet))
  17365. (t
  17366. (call-interactively 'org-toggle-item))))
  17367. (defun org-toggle-item (arg)
  17368. "Convert headings or normal lines to items, items to normal lines.
  17369. If there is no active region, only the current line is considered.
  17370. If the first non blank line in the region is an headline, convert
  17371. all headlines to items, shifting text accordingly.
  17372. If it is an item, convert all items to normal lines.
  17373. If it is normal text, change region into an item. With a prefix
  17374. argument ARG, change each line in region into an item."
  17375. (interactive "P")
  17376. (let ((shift-text
  17377. (function
  17378. ;; Shift text in current section to IND, from point to END.
  17379. ;; The function leaves point to END line.
  17380. (lambda (ind end)
  17381. (let ((min-i 1000) (end (copy-marker end)))
  17382. ;; First determine the minimum indentation (MIN-I) of
  17383. ;; the text.
  17384. (save-excursion
  17385. (catch 'exit
  17386. (while (< (point) end)
  17387. (let ((i (org-get-indentation)))
  17388. (cond
  17389. ;; Skip blank lines and inline tasks.
  17390. ((looking-at "^[ \t]*$"))
  17391. ((looking-at org-outline-regexp-bol))
  17392. ;; We can't find less than 0 indentation.
  17393. ((zerop i) (throw 'exit (setq min-i 0)))
  17394. ((< i min-i) (setq min-i i))))
  17395. (forward-line))))
  17396. ;; Then indent each line so that a line indented to
  17397. ;; MIN-I becomes indented to IND. Ignore blank lines
  17398. ;; and inline tasks in the process.
  17399. (let ((delta (- ind min-i)))
  17400. (while (< (point) end)
  17401. (unless (or (looking-at "^[ \t]*$")
  17402. (looking-at org-outline-regexp-bol))
  17403. (org-indent-line-to (+ (org-get-indentation) delta)))
  17404. (forward-line)))))))
  17405. (skip-blanks
  17406. (function
  17407. ;; Return beginning of first non-blank line, starting from
  17408. ;; line at POS.
  17409. (lambda (pos)
  17410. (save-excursion
  17411. (goto-char pos)
  17412. (skip-chars-forward " \r\t\n")
  17413. (point-at-bol)))))
  17414. beg end)
  17415. ;; Determine boundaries of changes.
  17416. (if (org-region-active-p)
  17417. (setq beg (funcall skip-blanks (region-beginning))
  17418. end (copy-marker (region-end)))
  17419. (setq beg (funcall skip-blanks (point-at-bol))
  17420. end (copy-marker (point-at-eol))))
  17421. ;; Depending on the starting line, choose an action on the text
  17422. ;; between BEG and END.
  17423. (org-with-limited-levels
  17424. (save-excursion
  17425. (goto-char beg)
  17426. (cond
  17427. ;; Case 1. Start at an item: de-itemize. Note that it only
  17428. ;; happens when a region is active: `org-ctrl-c-minus'
  17429. ;; would call `org-cycle-list-bullet' otherwise.
  17430. ((org-at-item-p)
  17431. (while (< (point) end)
  17432. (when (org-at-item-p)
  17433. (skip-chars-forward " \t")
  17434. (delete-region (point) (match-end 0)))
  17435. (forward-line)))
  17436. ;; Case 2. Start at an heading: convert to items.
  17437. ((org-at-heading-p)
  17438. (let* ((bul (org-list-bullet-string "-"))
  17439. (bul-len (length bul))
  17440. ;; Indentation of the first heading. It should be
  17441. ;; relative to the indentation of its parent, if any.
  17442. (start-ind (save-excursion
  17443. (cond
  17444. ((not org-adapt-indentation) 0)
  17445. ((not (outline-previous-heading)) 0)
  17446. (t (length (match-string 0))))))
  17447. ;; Level of first heading. Further headings will be
  17448. ;; compared to it to determine hierarchy in the list.
  17449. (ref-level (org-reduced-level (org-outline-level))))
  17450. (while (< (point) end)
  17451. (let* ((level (org-reduced-level (org-outline-level)))
  17452. (delta (max 0 (- level ref-level))))
  17453. ;; If current headline is less indented than the first
  17454. ;; one, set it as reference, in order to preserve
  17455. ;; subtrees.
  17456. (when (< level ref-level) (setq ref-level level))
  17457. (replace-match bul t t)
  17458. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17459. ;; Ensure all text down to END (or SECTION-END) belongs
  17460. ;; to the newly created item.
  17461. (let ((section-end (save-excursion
  17462. (or (outline-next-heading) (point)))))
  17463. (forward-line)
  17464. (funcall shift-text
  17465. (+ start-ind (* (1+ delta) bul-len))
  17466. (min end section-end)))))))
  17467. ;; Case 3. Normal line with ARG: turn each non-item line into
  17468. ;; an item.
  17469. (arg
  17470. (while (< (point) end)
  17471. (unless (or (org-at-heading-p) (org-at-item-p))
  17472. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17473. (replace-match
  17474. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17475. (forward-line)))
  17476. ;; Case 4. Normal line without ARG: make the first line of
  17477. ;; region an item, and shift indentation of others
  17478. ;; lines to set them as item's body.
  17479. (t (let* ((bul (org-list-bullet-string "-"))
  17480. (bul-len (length bul))
  17481. (ref-ind (org-get-indentation)))
  17482. (skip-chars-forward " \t")
  17483. (insert bul)
  17484. (forward-line)
  17485. (while (< (point) end)
  17486. ;; Ensure that lines less indented than first one
  17487. ;; still get included in item body.
  17488. (funcall shift-text
  17489. (+ ref-ind bul-len)
  17490. (min end (save-excursion (or (outline-next-heading)
  17491. (point)))))
  17492. (forward-line)))))))))
  17493. (defun org-toggle-heading (&optional nstars)
  17494. "Convert headings to normal text, or items or text to headings.
  17495. If there is no active region, only the current line is considered.
  17496. With a \\[universal-argument] prefix, convert the whole list at
  17497. point into heading.
  17498. In a region:
  17499. - If the first non blank line is an headline, remove the stars
  17500. from all headlines in the region.
  17501. - If it is a normal line turn each and every normal line (i.e. not an
  17502. heading or an item) in the region into a heading.
  17503. - If it is a plain list item, turn all plain list items into headings.
  17504. When converting a line into a heading, the number of stars is chosen
  17505. such that the lines become children of the current entry. However,
  17506. when a prefix argument is given, its value determines the number of
  17507. stars to add."
  17508. (interactive "P")
  17509. (let ((skip-blanks
  17510. (function
  17511. ;; Return beginning of first non-blank line, starting from
  17512. ;; line at POS.
  17513. (lambda (pos)
  17514. (save-excursion
  17515. (goto-char pos)
  17516. (skip-chars-forward " \r\t\n")
  17517. (point-at-bol)))))
  17518. beg end)
  17519. ;; Determine boundaries of changes. If a universal prefix has
  17520. ;; been given, put the list in a region. If region ends at a bol,
  17521. ;; do not consider the last line to be in the region.
  17522. (when (and current-prefix-arg (org-at-item-p))
  17523. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17524. (org-mark-list))
  17525. (if (org-region-active-p)
  17526. (setq beg (funcall skip-blanks (region-beginning))
  17527. end (copy-marker (save-excursion
  17528. (goto-char (region-end))
  17529. (if (bolp) (point) (point-at-eol)))))
  17530. (setq beg (funcall skip-blanks (point-at-bol))
  17531. end (copy-marker (point-at-eol))))
  17532. ;; Ensure inline tasks don't count as headings.
  17533. (org-with-limited-levels
  17534. (save-excursion
  17535. (goto-char beg)
  17536. (cond
  17537. ;; Case 1. Started at an heading: de-star headings.
  17538. ((org-at-heading-p)
  17539. (while (< (point) end)
  17540. (when (org-at-heading-p t)
  17541. (looking-at org-outline-regexp) (replace-match ""))
  17542. (forward-line)))
  17543. ;; Case 2. Started at an item: change items into headlines.
  17544. ;; One star will be added by `org-list-to-subtree'.
  17545. ((org-at-item-p)
  17546. (let* ((stars (make-string
  17547. (if nstars
  17548. ;; subtract the star that will be added again by
  17549. ;; `org-list-to-subtree'
  17550. (1- (prefix-numeric-value current-prefix-arg))
  17551. (or (org-current-level) 0))
  17552. ?*))
  17553. (add-stars
  17554. (cond (nstars "") ; stars from prefix only
  17555. ((equal stars "") "") ; before first heading
  17556. (org-odd-levels-only "*") ; inside heading, odd
  17557. (t "")))) ; inside heading, oddeven
  17558. (while (< (point) end)
  17559. (when (org-at-item-p)
  17560. ;; Pay attention to cases when region ends before list.
  17561. (let* ((struct (org-list-struct))
  17562. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17563. (save-restriction
  17564. (narrow-to-region (point) list-end)
  17565. (insert
  17566. (org-list-to-subtree
  17567. (org-list-parse-list t)
  17568. '(:istart (concat stars add-stars (funcall get-stars depth))
  17569. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17570. (forward-line))))
  17571. ;; Case 3. Started at normal text: make every line an heading,
  17572. ;; skipping headlines and items.
  17573. (t (let* ((stars (make-string
  17574. (if nstars
  17575. (prefix-numeric-value current-prefix-arg)
  17576. (or (org-current-level) 0))
  17577. ?*))
  17578. (add-stars
  17579. (cond (nstars "") ; stars from prefix only
  17580. ((equal stars "") "*") ; before first heading
  17581. (org-odd-levels-only "**") ; inside heading, odd
  17582. (t "*"))) ; inside heading, oddeven
  17583. (rpl (concat stars add-stars " ")))
  17584. (while (< (point) end)
  17585. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17586. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17587. (replace-match (concat rpl (match-string 2))))
  17588. (forward-line)))))))))
  17589. (defun org-meta-return (&optional arg)
  17590. "Insert a new heading or wrap a region in a table.
  17591. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17592. See the individual commands for more information."
  17593. (interactive "P")
  17594. (cond
  17595. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17596. ((or (org-at-drawer-p) (org-at-property-p))
  17597. (newline-and-indent))
  17598. ((org-at-table-p)
  17599. (call-interactively 'org-table-wrap-region))
  17600. (t (call-interactively 'org-insert-heading))))
  17601. ;;; Menu entries
  17602. ;; Define the Org-mode menus
  17603. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17604. '("Tbl"
  17605. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17606. ["Next Field" org-cycle (org-at-table-p)]
  17607. ["Previous Field" org-shifttab (org-at-table-p)]
  17608. ["Next Row" org-return (org-at-table-p)]
  17609. "--"
  17610. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17611. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17612. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17613. "--"
  17614. ("Column"
  17615. ["Move Column Left" org-metaleft (org-at-table-p)]
  17616. ["Move Column Right" org-metaright (org-at-table-p)]
  17617. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17618. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17619. ("Row"
  17620. ["Move Row Up" org-metaup (org-at-table-p)]
  17621. ["Move Row Down" org-metadown (org-at-table-p)]
  17622. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17623. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17624. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17625. "--"
  17626. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17627. ("Rectangle"
  17628. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17629. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17630. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17631. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17632. "--"
  17633. ("Calculate"
  17634. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17635. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17636. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17637. "--"
  17638. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17639. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17640. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17641. "--"
  17642. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17643. "--"
  17644. ["Sum Column/Rectangle" org-table-sum
  17645. (or (org-at-table-p) (org-region-active-p))]
  17646. ["Which Column?" org-table-current-column (org-at-table-p)])
  17647. ["Debug Formulas"
  17648. org-table-toggle-formula-debugger
  17649. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17650. ["Show Col/Row Numbers"
  17651. org-table-toggle-coordinate-overlays
  17652. :style toggle
  17653. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17654. "--"
  17655. ["Create" org-table-create (and (not (org-at-table-p))
  17656. org-enable-table-editor)]
  17657. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17658. ["Import from File" org-table-import (not (org-at-table-p))]
  17659. ["Export to File" org-table-export (org-at-table-p)]
  17660. "--"
  17661. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17662. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17663. '("Org"
  17664. ("Show/Hide"
  17665. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17666. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17667. ["Sparse Tree..." org-sparse-tree t]
  17668. ["Reveal Context" org-reveal t]
  17669. ["Show All" show-all t]
  17670. "--"
  17671. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17672. "--"
  17673. ["New Heading" org-insert-heading t]
  17674. ("Navigate Headings"
  17675. ["Up" outline-up-heading t]
  17676. ["Next" outline-next-visible-heading t]
  17677. ["Previous" outline-previous-visible-heading t]
  17678. ["Next Same Level" outline-forward-same-level t]
  17679. ["Previous Same Level" outline-backward-same-level t]
  17680. "--"
  17681. ["Jump" org-goto t])
  17682. ("Edit Structure"
  17683. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17684. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17685. "--"
  17686. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17687. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17688. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17689. "--"
  17690. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17691. "--"
  17692. ["Copy visible text" org-copy-visible t]
  17693. "--"
  17694. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17695. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17696. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17697. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17698. "--"
  17699. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17700. "--"
  17701. ["Convert to odd levels" org-convert-to-odd-levels t]
  17702. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17703. ("Editing"
  17704. ["Emphasis..." org-emphasize t]
  17705. ["Edit Source Example" org-edit-special t]
  17706. "--"
  17707. ["Footnote new/jump" org-footnote-action t]
  17708. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17709. ("Archive"
  17710. ["Archive (default method)" org-archive-subtree-default t]
  17711. "--"
  17712. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17713. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17714. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17715. )
  17716. "--"
  17717. ("Hyperlinks"
  17718. ["Store Link (Global)" org-store-link t]
  17719. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17720. ["Insert Link" org-insert-link t]
  17721. ["Follow Link" org-open-at-point t]
  17722. "--"
  17723. ["Next link" org-next-link t]
  17724. ["Previous link" org-previous-link t]
  17725. "--"
  17726. ["Descriptive Links"
  17727. org-toggle-link-display
  17728. :style radio
  17729. :selected org-descriptive-links
  17730. ]
  17731. ["Literal Links"
  17732. org-toggle-link-display
  17733. :style radio
  17734. :selected (not org-descriptive-links)])
  17735. "--"
  17736. ("TODO Lists"
  17737. ["TODO/DONE/-" org-todo t]
  17738. ("Select keyword"
  17739. ["Next keyword" org-shiftright (org-at-heading-p)]
  17740. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17741. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17742. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17743. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17744. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17745. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17746. "--"
  17747. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17748. :selected org-enforce-todo-dependencies :style toggle :active t]
  17749. "Settings for tree at point"
  17750. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17751. :selected (org-entry-get nil "ORDERED")
  17752. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17753. ["Do Children parallel" org-toggle-ordered-property :style radio
  17754. :selected (not (org-entry-get nil "ORDERED"))
  17755. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17756. "--"
  17757. ["Set Priority" org-priority t]
  17758. ["Priority Up" org-shiftup t]
  17759. ["Priority Down" org-shiftdown t]
  17760. "--"
  17761. ["Get news from all feeds" org-feed-update-all t]
  17762. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17763. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17764. ("TAGS and Properties"
  17765. ["Set Tags" org-set-tags-command t]
  17766. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17767. "--"
  17768. ["Set property" org-set-property t]
  17769. ["Column view of properties" org-columns t]
  17770. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17771. ("Dates and Scheduling"
  17772. ["Timestamp" org-time-stamp t]
  17773. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17774. ("Change Date"
  17775. ["1 Day Later" org-shiftright t]
  17776. ["1 Day Earlier" org-shiftleft t]
  17777. ["1 ... Later" org-shiftup t]
  17778. ["1 ... Earlier" org-shiftdown t])
  17779. ["Compute Time Range" org-evaluate-time-range t]
  17780. ["Schedule Item" org-schedule t]
  17781. ["Deadline" org-deadline t]
  17782. "--"
  17783. ["Custom time format" org-toggle-time-stamp-overlays
  17784. :style radio :selected org-display-custom-times]
  17785. "--"
  17786. ["Goto Calendar" org-goto-calendar t]
  17787. ["Date from Calendar" org-date-from-calendar t]
  17788. "--"
  17789. ["Start/Restart Timer" org-timer-start t]
  17790. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17791. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17792. ["Insert Timer String" org-timer t]
  17793. ["Insert Timer Item" org-timer-item t])
  17794. ("Logging work"
  17795. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17796. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17797. ["Clock out" org-clock-out t]
  17798. ["Clock cancel" org-clock-cancel t]
  17799. "--"
  17800. ["Mark as default task" org-clock-mark-default-task t]
  17801. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17802. ["Goto running clock" org-clock-goto t]
  17803. "--"
  17804. ["Display times" org-clock-display t]
  17805. ["Create clock table" org-clock-report t]
  17806. "--"
  17807. ["Record DONE time"
  17808. (progn (setq org-log-done (not org-log-done))
  17809. (message "Switching to %s will %s record a timestamp"
  17810. (car org-done-keywords)
  17811. (if org-log-done "automatically" "not")))
  17812. :style toggle :selected org-log-done])
  17813. "--"
  17814. ["Agenda Command..." org-agenda t]
  17815. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17816. ("File List for Agenda")
  17817. ("Special views current file"
  17818. ["TODO Tree" org-show-todo-tree t]
  17819. ["Check Deadlines" org-check-deadlines t]
  17820. ["Timeline" org-timeline t]
  17821. ["Tags/Property tree" org-match-sparse-tree t])
  17822. "--"
  17823. ["Export/Publish..." org-export t]
  17824. ("LaTeX"
  17825. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17826. :selected org-cdlatex-mode]
  17827. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17828. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17829. ["Modify math symbol" org-cdlatex-math-modify
  17830. (org-inside-LaTeX-fragment-p)]
  17831. ["Insert citation" org-reftex-citation t]
  17832. "--"
  17833. ["Template for BEAMER" (progn (require 'org-beamer)
  17834. (org-insert-beamer-options-template)) t])
  17835. "--"
  17836. ("MobileOrg"
  17837. ["Push Files and Views" org-mobile-push t]
  17838. ["Get Captured and Flagged" org-mobile-pull t]
  17839. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17840. "--"
  17841. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17842. "--"
  17843. ("Documentation"
  17844. ["Show Version" org-version t]
  17845. ["Info Documentation" org-info t])
  17846. ("Customize"
  17847. ["Browse Org Group" org-customize t]
  17848. "--"
  17849. ["Expand This Menu" org-create-customize-menu
  17850. (fboundp 'customize-menu-create)])
  17851. ["Send bug report" org-submit-bug-report t]
  17852. "--"
  17853. ("Refresh/Reload"
  17854. ["Refresh setup current buffer" org-mode-restart t]
  17855. ["Reload Org (after update)" org-reload t]
  17856. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17857. ))
  17858. (defun org-info (&optional node)
  17859. "Read documentation for Org-mode in the info system.
  17860. With optional NODE, go directly to that node."
  17861. (interactive)
  17862. (info (format "(org)%s" (or node ""))))
  17863. ;;;###autoload
  17864. (defun org-submit-bug-report ()
  17865. "Submit a bug report on Org-mode via mail.
  17866. Don't hesitate to report any problems or inaccurate documentation.
  17867. If you don't have setup sending mail from (X)Emacs, please copy the
  17868. output buffer into your mail program, as it gives us important
  17869. information about your Org-mode version and configuration."
  17870. (interactive)
  17871. (require 'reporter)
  17872. (org-load-modules-maybe)
  17873. (org-require-autoloaded-modules)
  17874. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17875. (reporter-submit-bug-report
  17876. "emacs-orgmode@gnu.org"
  17877. (org-version)
  17878. (let (list)
  17879. (save-window-excursion
  17880. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17881. (delete-other-windows)
  17882. (erase-buffer)
  17883. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17884. We would like to add your full Org-mode and Outline configuration to the
  17885. bug report. This greatly simplifies the work of the maintainer and
  17886. other experts on the mailing list.
  17887. HOWEVER, some variables you have customized may contain private
  17888. information. The names of customers, colleagues, or friends, might
  17889. appear in the form of file names, tags, todo states, or search strings.
  17890. If you answer yes to the prompt, you might want to check and remove
  17891. such private information before sending the email.")
  17892. (add-text-properties (point-min) (point-max) '(face org-warning))
  17893. (when (yes-or-no-p "Include your Org-mode configuration ")
  17894. (mapatoms
  17895. (lambda (v)
  17896. (and (boundp v)
  17897. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17898. (or (and (symbol-value v)
  17899. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17900. (and
  17901. (get v 'custom-type) (get v 'standard-value)
  17902. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17903. (push v list)))))
  17904. (kill-buffer (get-buffer "*Warn about privacy*"))
  17905. list))
  17906. nil nil
  17907. "Remember to cover the basics, that is, what you expected to happen and
  17908. what in fact did happen. You don't know how to make a good report? See
  17909. http://orgmode.org/manual/Feedback.html#Feedback
  17910. Your bug report will be posted to the Org-mode mailing list.
  17911. ------------------------------------------------------------------------")
  17912. (save-excursion
  17913. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17914. (replace-match "\\1Bug: \\3 [\\2]")))))
  17915. (defun org-install-agenda-files-menu ()
  17916. (let ((bl (buffer-list)))
  17917. (save-excursion
  17918. (while bl
  17919. (set-buffer (pop bl))
  17920. (if (derived-mode-p 'org-mode) (setq bl nil)))
  17921. (when (derived-mode-p 'org-mode)
  17922. (easy-menu-change
  17923. '("Org") "File List for Agenda"
  17924. (append
  17925. (list
  17926. ["Edit File List" (org-edit-agenda-file-list) t]
  17927. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17928. ["Remove Current File from List" org-remove-file t]
  17929. ["Cycle through agenda files" org-cycle-agenda-files t]
  17930. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17931. "--")
  17932. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17933. ;;;; Documentation
  17934. ;;;###autoload
  17935. (defun org-require-autoloaded-modules ()
  17936. (interactive)
  17937. (mapc 'require
  17938. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17939. org-docbook org-exp org-html org-icalendar
  17940. org-id org-latex
  17941. org-publish org-remember org-table
  17942. org-timer org-xoxo)))
  17943. ;;;###autoload
  17944. (defun org-reload (&optional uncompiled)
  17945. "Reload all org lisp files.
  17946. With prefix arg UNCOMPILED, load the uncompiled versions."
  17947. (interactive "P")
  17948. (require 'find-func)
  17949. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17950. (dir-org (file-name-directory (org-find-library-name "org")))
  17951. (dir-org-contrib (ignore-errors
  17952. (file-name-directory
  17953. (org-find-library-name "org-contribdir"))))
  17954. (babel-files
  17955. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17956. (append (list nil "comint" "eval" "exp" "keys"
  17957. "lob" "ref" "table" "tangle")
  17958. (delq nil
  17959. (mapcar
  17960. (lambda (lang)
  17961. (when (cdr lang) (symbol-name (car lang))))
  17962. org-babel-load-languages)))))
  17963. (files
  17964. (append (directory-files dir-org t file-re)
  17965. babel-files
  17966. (and dir-org-contrib
  17967. (directory-files dir-org-contrib t file-re))))
  17968. (remove-re (concat (if (featurep 'xemacs)
  17969. "org-colview" "org-colview-xemacs")
  17970. "\\'")))
  17971. (setq files (mapcar 'file-name-sans-extension files))
  17972. (setq files (mapcar
  17973. (lambda (x) (if (string-match remove-re x) nil x))
  17974. files))
  17975. (setq files (delq nil files))
  17976. (mapc
  17977. (lambda (f)
  17978. (when (featurep (intern (file-name-nondirectory f)))
  17979. (if (and (not uncompiled)
  17980. (file-exists-p (concat f ".elc")))
  17981. (load (concat f ".elc") nil nil t)
  17982. (load (concat f ".el") nil nil t))))
  17983. files))
  17984. (org-version))
  17985. ;;;###autoload
  17986. (defun org-customize ()
  17987. "Call the customize function with org as argument."
  17988. (interactive)
  17989. (org-load-modules-maybe)
  17990. (org-require-autoloaded-modules)
  17991. (customize-browse 'org))
  17992. (defun org-create-customize-menu ()
  17993. "Create a full customization menu for Org-mode, insert it into the menu."
  17994. (interactive)
  17995. (org-load-modules-maybe)
  17996. (org-require-autoloaded-modules)
  17997. (if (fboundp 'customize-menu-create)
  17998. (progn
  17999. (easy-menu-change
  18000. '("Org") "Customize"
  18001. `(["Browse Org group" org-customize t]
  18002. "--"
  18003. ,(customize-menu-create 'org)
  18004. ["Set" Custom-set t]
  18005. ["Save" Custom-save t]
  18006. ["Reset to Current" Custom-reset-current t]
  18007. ["Reset to Saved" Custom-reset-saved t]
  18008. ["Reset to Standard Settings" Custom-reset-standard t]))
  18009. (message "\"Org\"-menu now contains full customization menu"))
  18010. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18011. ;;;; Miscellaneous stuff
  18012. ;;; Generally useful functions
  18013. (defun org-get-at-bol (property)
  18014. "Get text property PROPERTY at beginning of line."
  18015. (get-text-property (point-at-bol) property))
  18016. (defun org-find-text-property-in-string (prop s)
  18017. "Return the first non-nil value of property PROP in string S."
  18018. (or (get-text-property 0 prop s)
  18019. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18020. prop s)))
  18021. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18022. "Display the given MESSAGE as a warning."
  18023. (if (fboundp 'display-warning)
  18024. (display-warning 'org message
  18025. (if (featurep 'xemacs) 'warning :warning))
  18026. (let ((buf (get-buffer-create "*Org warnings*")))
  18027. (with-current-buffer buf
  18028. (goto-char (point-max))
  18029. (insert "Warning (Org): " message)
  18030. (unless (bolp)
  18031. (newline)))
  18032. (display-buffer buf)
  18033. (sit-for 0))))
  18034. (defun org-eval (form)
  18035. "Eval FORM and return result."
  18036. (condition-case error
  18037. (eval form)
  18038. (error (format "%%![Error: %s]" error))))
  18039. (defun org-in-clocktable-p ()
  18040. "Check if the cursor is in a clocktable."
  18041. (let ((pos (point)) start)
  18042. (save-excursion
  18043. (end-of-line 1)
  18044. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18045. (setq start (match-beginning 0))
  18046. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18047. (>= (match-end 0) pos)
  18048. start))))
  18049. (defun org-in-commented-line ()
  18050. "Is point in a line starting with `#'?"
  18051. (equal (char-after (point-at-bol)) ?#))
  18052. (defun org-in-indented-comment-line ()
  18053. "Is point in a line starting with `#' after some white space?"
  18054. (save-excursion
  18055. (save-match-data
  18056. (goto-char (point-at-bol))
  18057. (looking-at "[ \t]*#"))))
  18058. (defun org-in-verbatim-emphasis ()
  18059. (save-match-data
  18060. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18061. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18062. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18063. (if (and marker (marker-buffer marker)
  18064. (buffer-live-p (marker-buffer marker)))
  18065. (progn
  18066. (org-pop-to-buffer-same-window (marker-buffer marker))
  18067. (if (or (> marker (point-max)) (< marker (point-min)))
  18068. (widen))
  18069. (goto-char marker)
  18070. (org-show-context 'org-goto))
  18071. (if bookmark
  18072. (bookmark-jump bookmark)
  18073. (error "Cannot find location"))))
  18074. (defun org-quote-csv-field (s)
  18075. "Quote field for inclusion in CSV material."
  18076. (if (string-match "[\",]" s)
  18077. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18078. s))
  18079. (defun org-force-self-insert (N)
  18080. "Needed to enforce self-insert under remapping."
  18081. (interactive "p")
  18082. (self-insert-command N))
  18083. (defun org-string-width (s)
  18084. "Compute width of string, ignoring invisible characters.
  18085. This ignores character with invisibility property `org-link', and also
  18086. characters with property `org-cwidth', because these will become invisible
  18087. upon the next fontification round."
  18088. (let (b l)
  18089. (when (or (eq t buffer-invisibility-spec)
  18090. (assq 'org-link buffer-invisibility-spec))
  18091. (while (setq b (text-property-any 0 (length s)
  18092. 'invisible 'org-link s))
  18093. (setq s (concat (substring s 0 b)
  18094. (substring s (or (next-single-property-change
  18095. b 'invisible s) (length s)))))))
  18096. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18097. (setq s (concat (substring s 0 b)
  18098. (substring s (or (next-single-property-change
  18099. b 'org-cwidth s) (length s))))))
  18100. (setq l (string-width s) b -1)
  18101. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18102. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18103. l))
  18104. (defun org-shorten-string (s maxlength)
  18105. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18106. If the string is shorter or has length MAXLENGTH, just return the
  18107. original string. If it is longer, the functions finds a space in the
  18108. string, breaks this string off at that locations and adds three dots
  18109. as ellipsis. Including the ellipsis, the string will not be longer
  18110. than MAXLENGTH. If finding a good breaking point in the string does
  18111. not work, the string is just chopped off in the middle of a word
  18112. if necessary."
  18113. (if (<= (length s) maxlength)
  18114. s
  18115. (let* ((n (max (- maxlength 4) 1))
  18116. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18117. (if (string-match re s)
  18118. (concat (match-string 1 s) "...")
  18119. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18120. (defun org-get-indentation (&optional line)
  18121. "Get the indentation of the current line, interpreting tabs.
  18122. When LINE is given, assume it represents a line and compute its indentation."
  18123. (if line
  18124. (if (string-match "^ *" (org-remove-tabs line))
  18125. (match-end 0))
  18126. (save-excursion
  18127. (beginning-of-line 1)
  18128. (skip-chars-forward " \t")
  18129. (current-column))))
  18130. (defun org-get-string-indentation (s)
  18131. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18132. (let ((n -1) (i 0) (w tab-width) c)
  18133. (catch 'exit
  18134. (while (< (setq n (1+ n)) (length s))
  18135. (setq c (aref s n))
  18136. (cond ((= c ?\ ) (setq i (1+ i)))
  18137. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18138. (t (throw 'exit t)))))
  18139. i))
  18140. (defun org-remove-tabs (s &optional width)
  18141. "Replace tabulators in S with spaces.
  18142. Assumes that s is a single line, starting in column 0."
  18143. (setq width (or width tab-width))
  18144. (while (string-match "\t" s)
  18145. (setq s (replace-match
  18146. (make-string
  18147. (- (* width (/ (+ (match-beginning 0) width) width))
  18148. (match-beginning 0)) ?\ )
  18149. t t s)))
  18150. s)
  18151. (defun org-fix-indentation (line ind)
  18152. "Fix indentation in LINE.
  18153. IND is a cons cell with target and minimum indentation.
  18154. If the current indentation in LINE is smaller than the minimum,
  18155. leave it alone. If it is larger than ind, set it to the target."
  18156. (let* ((l (org-remove-tabs line))
  18157. (i (org-get-indentation l))
  18158. (i1 (car ind)) (i2 (cdr ind)))
  18159. (if (>= i i2) (setq l (substring line i2)))
  18160. (if (> i1 0)
  18161. (concat (make-string i1 ?\ ) l)
  18162. l)))
  18163. (defun org-remove-indentation (code &optional n)
  18164. "Remove the maximum common indentation from the lines in CODE.
  18165. N may optionally be the number of spaces to remove."
  18166. (with-temp-buffer
  18167. (insert code)
  18168. (org-do-remove-indentation n)
  18169. (buffer-string)))
  18170. (defun org-do-remove-indentation (&optional n)
  18171. "Remove the maximum common indentation from the buffer."
  18172. (untabify (point-min) (point-max))
  18173. (let ((min 10000) re)
  18174. (if n
  18175. (setq min n)
  18176. (goto-char (point-min))
  18177. (while (re-search-forward "^ *[^ \n]" nil t)
  18178. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18179. (unless (or (= min 0) (= min 10000))
  18180. (setq re (format "^ \\{%d\\}" min))
  18181. (goto-char (point-min))
  18182. (while (re-search-forward re nil t)
  18183. (replace-match "")
  18184. (end-of-line 1))
  18185. min)))
  18186. (defun org-fill-template (template alist)
  18187. "Find each %key of ALIST in TEMPLATE and replace it."
  18188. (let ((case-fold-search nil)
  18189. entry key value)
  18190. (setq alist (sort (copy-sequence alist)
  18191. (lambda (a b) (< (length (car a)) (length (car b))))))
  18192. (while (setq entry (pop alist))
  18193. (setq template
  18194. (replace-regexp-in-string
  18195. (concat "%" (regexp-quote (car entry)))
  18196. (cdr entry) template t t)))
  18197. template))
  18198. (defun org-base-buffer (buffer)
  18199. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18200. (if (not buffer)
  18201. buffer
  18202. (or (buffer-base-buffer buffer)
  18203. buffer)))
  18204. (defun org-trim (s)
  18205. "Remove whitespace at beginning and end of string."
  18206. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18207. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18208. s)
  18209. (defun org-wrap (string &optional width lines)
  18210. "Wrap string to either a number of lines, or a width in characters.
  18211. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18212. that costs. If there is a word longer than WIDTH, the text is actually
  18213. wrapped to the length of that word.
  18214. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18215. many lines, whatever width that takes.
  18216. The return value is a list of lines, without newlines at the end."
  18217. (let* ((words (org-split-string string "[ \t\n]+"))
  18218. (maxword (apply 'max (mapcar 'org-string-width words)))
  18219. w ll)
  18220. (cond (width
  18221. (org-do-wrap words (max maxword width)))
  18222. (lines
  18223. (setq w maxword)
  18224. (setq ll (org-do-wrap words maxword))
  18225. (if (<= (length ll) lines)
  18226. ll
  18227. (setq ll words)
  18228. (while (> (length ll) lines)
  18229. (setq w (1+ w))
  18230. (setq ll (org-do-wrap words w)))
  18231. ll))
  18232. (t (error "Cannot wrap this")))))
  18233. (defun org-do-wrap (words width)
  18234. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18235. (let (lines line)
  18236. (while words
  18237. (setq line (pop words))
  18238. (while (and words (< (+ (length line) (length (car words))) width))
  18239. (setq line (concat line " " (pop words))))
  18240. (setq lines (push line lines)))
  18241. (nreverse lines)))
  18242. (defun org-split-string (string &optional separators)
  18243. "Splits STRING into substrings at SEPARATORS.
  18244. No empty strings are returned if there are matches at the beginning
  18245. and end of string."
  18246. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18247. (start 0)
  18248. notfirst
  18249. (list nil))
  18250. (while (and (string-match rexp string
  18251. (if (and notfirst
  18252. (= start (match-beginning 0))
  18253. (< start (length string)))
  18254. (1+ start) start))
  18255. (< (match-beginning 0) (length string)))
  18256. (setq notfirst t)
  18257. (or (eq (match-beginning 0) 0)
  18258. (and (eq (match-beginning 0) (match-end 0))
  18259. (eq (match-beginning 0) start))
  18260. (setq list
  18261. (cons (substring string start (match-beginning 0))
  18262. list)))
  18263. (setq start (match-end 0)))
  18264. (or (eq start (length string))
  18265. (setq list
  18266. (cons (substring string start)
  18267. list)))
  18268. (nreverse list)))
  18269. (defun org-quote-vert (s)
  18270. "Replace \"|\" with \"\\vert\"."
  18271. (while (string-match "|" s)
  18272. (setq s (replace-match "\\vert" t t s)))
  18273. s)
  18274. (defun org-uuidgen-p (s)
  18275. "Is S an ID created by UUIDGEN?"
  18276. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18277. (defun org-in-src-block-p nil
  18278. "Whether point is in a code source block."
  18279. (let (ov)
  18280. (when (setq ov (overlays-at (point)))
  18281. (memq 'org-block-background
  18282. (overlay-properties
  18283. (car ov))))))
  18284. (defun org-context ()
  18285. "Return a list of contexts of the current cursor position.
  18286. If several contexts apply, all are returned.
  18287. Each context entry is a list with a symbol naming the context, and
  18288. two positions indicating start and end of the context. Possible
  18289. contexts are:
  18290. :headline anywhere in a headline
  18291. :headline-stars on the leading stars in a headline
  18292. :todo-keyword on a TODO keyword (including DONE) in a headline
  18293. :tags on the TAGS in a headline
  18294. :priority on the priority cookie in a headline
  18295. :item on the first line of a plain list item
  18296. :item-bullet on the bullet/number of a plain list item
  18297. :checkbox on the checkbox in a plain list item
  18298. :table in an org-mode table
  18299. :table-special on a special filed in a table
  18300. :table-table in a table.el table
  18301. :clocktable in a clocktable
  18302. :src-block in a source block
  18303. :link on a hyperlink
  18304. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18305. :target on a <<target>>
  18306. :radio-target on a <<<radio-target>>>
  18307. :latex-fragment on a LaTeX fragment
  18308. :latex-preview on a LaTeX fragment with overlaid preview image
  18309. This function expects the position to be visible because it uses font-lock
  18310. faces as a help to recognize the following contexts: :table-special, :link,
  18311. and :keyword."
  18312. (let* ((f (get-text-property (point) 'face))
  18313. (faces (if (listp f) f (list f)))
  18314. (case-fold-search t)
  18315. (p (point)) clist o)
  18316. ;; First the large context
  18317. (cond
  18318. ((org-at-heading-p t)
  18319. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18320. (when (progn
  18321. (beginning-of-line 1)
  18322. (looking-at org-todo-line-tags-regexp))
  18323. (push (org-point-in-group p 1 :headline-stars) clist)
  18324. (push (org-point-in-group p 2 :todo-keyword) clist)
  18325. (push (org-point-in-group p 4 :tags) clist))
  18326. (goto-char p)
  18327. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18328. (if (looking-at "\\[#[A-Z0-9]\\]")
  18329. (push (org-point-in-group p 0 :priority) clist)))
  18330. ((org-at-item-p)
  18331. (push (org-point-in-group p 2 :item-bullet) clist)
  18332. (push (list :item (point-at-bol)
  18333. (save-excursion (org-end-of-item) (point)))
  18334. clist)
  18335. (and (org-at-item-checkbox-p)
  18336. (push (org-point-in-group p 0 :checkbox) clist)))
  18337. ((org-at-table-p)
  18338. (push (list :table (org-table-begin) (org-table-end)) clist)
  18339. (if (memq 'org-formula faces)
  18340. (push (list :table-special
  18341. (previous-single-property-change p 'face)
  18342. (next-single-property-change p 'face)) clist)))
  18343. ((org-at-table-p 'any)
  18344. (push (list :table-table) clist)))
  18345. (goto-char p)
  18346. ;; New the "medium" contexts: clocktables, source blocks
  18347. (cond ((org-in-clocktable-p)
  18348. (push (list :clocktable
  18349. (and (or (looking-at "#\\+BEGIN: clocktable")
  18350. (search-backward "#+BEGIN: clocktable" nil t))
  18351. (match-beginning 0))
  18352. (and (re-search-forward "#\\+END:?" nil t)
  18353. (match-end 0))) clist))
  18354. ((org-in-src-block-p)
  18355. (push (list :src-block
  18356. (and (or (looking-at "#\\+BEGIN_SRC")
  18357. (search-backward "#+BEGIN_SRC" nil t))
  18358. (match-beginning 0))
  18359. (and (search-forward "#+END_SRC" nil t)
  18360. (match-beginning 0))) clist)))
  18361. (goto-char p)
  18362. ;; Now the small context
  18363. (cond
  18364. ((org-at-timestamp-p)
  18365. (push (org-point-in-group p 0 :timestamp) clist))
  18366. ((memq 'org-link faces)
  18367. (push (list :link
  18368. (previous-single-property-change p 'face)
  18369. (next-single-property-change p 'face)) clist))
  18370. ((memq 'org-special-keyword faces)
  18371. (push (list :keyword
  18372. (previous-single-property-change p 'face)
  18373. (next-single-property-change p 'face)) clist))
  18374. ((org-at-target-p)
  18375. (push (org-point-in-group p 0 :target) clist)
  18376. (goto-char (1- (match-beginning 0)))
  18377. (if (looking-at org-radio-target-regexp)
  18378. (push (org-point-in-group p 0 :radio-target) clist))
  18379. (goto-char p))
  18380. ((setq o (car (delq nil
  18381. (mapcar
  18382. (lambda (x)
  18383. (if (memq x org-latex-fragment-image-overlays) x))
  18384. (overlays-at (point))))))
  18385. (push (list :latex-fragment
  18386. (overlay-start o) (overlay-end o)) clist)
  18387. (push (list :latex-preview
  18388. (overlay-start o) (overlay-end o)) clist))
  18389. ((org-inside-LaTeX-fragment-p)
  18390. ;; FIXME: positions wrong.
  18391. (push (list :latex-fragment (point) (point)) clist)))
  18392. (setq clist (nreverse (delq nil clist)))
  18393. clist))
  18394. ;; FIXME: Compare with at-regexp-p Do we need both?
  18395. (defun org-in-regexp (re &optional nlines visually)
  18396. "Check if point is inside a match of regexp.
  18397. Normally only the current line is checked, but you can include NLINES extra
  18398. lines both before and after point into the search.
  18399. If VISUALLY is set, require that the cursor is not after the match but
  18400. really on, so that the block visually is on the match."
  18401. (catch 'exit
  18402. (let ((pos (point))
  18403. (eol (point-at-eol (+ 1 (or nlines 0))))
  18404. (inc (if visually 1 0)))
  18405. (save-excursion
  18406. (beginning-of-line (- 1 (or nlines 0)))
  18407. (while (re-search-forward re eol t)
  18408. (if (and (<= (match-beginning 0) pos)
  18409. (>= (+ inc (match-end 0)) pos))
  18410. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18411. (defun org-at-regexp-p (regexp)
  18412. "Is point inside a match of REGEXP in the current line?"
  18413. (catch 'exit
  18414. (save-excursion
  18415. (let ((pos (point)) (end (point-at-eol)))
  18416. (beginning-of-line 1)
  18417. (while (re-search-forward regexp end t)
  18418. (if (and (<= (match-beginning 0) pos)
  18419. (>= (match-end 0) pos))
  18420. (throw 'exit t)))
  18421. nil))))
  18422. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18423. "Non-nil when point is between matches of START-RE and END-RE.
  18424. Also return a non-nil value when point is on one of the matches.
  18425. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18426. buffer positions. Default values are the positions of headlines
  18427. surrounding the point.
  18428. The functions returns a cons cell whose car (resp. cdr) is the
  18429. position before START-RE (resp. after END-RE)."
  18430. (save-match-data
  18431. (let ((pos (point))
  18432. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18433. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18434. beg end)
  18435. (save-excursion
  18436. ;; Point is on a block when on START-RE or if START-RE can be
  18437. ;; found before it...
  18438. (and (or (org-at-regexp-p start-re)
  18439. (re-search-backward start-re limit-up t))
  18440. (setq beg (match-beginning 0))
  18441. ;; ... and END-RE after it...
  18442. (goto-char (match-end 0))
  18443. (re-search-forward end-re limit-down t)
  18444. (> (setq end (match-end 0)) pos)
  18445. ;; ... without another START-RE in-between.
  18446. (goto-char (match-beginning 0))
  18447. (not (re-search-backward start-re (1+ beg) t))
  18448. ;; Return value.
  18449. (cons beg end))))))
  18450. (defun org-in-block-p (names)
  18451. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18452. NAMES is a list of strings containing names of blocks.
  18453. Return first block name matched, or nil. Beware that in case of
  18454. nested blocks, the returned name may not belong to the closest
  18455. block from point."
  18456. (save-match-data
  18457. (catch 'exit
  18458. (let ((case-fold-search t)
  18459. (lim-up (save-excursion (outline-previous-heading)))
  18460. (lim-down (save-excursion (outline-next-heading))))
  18461. (mapc (lambda (name)
  18462. (let ((n (regexp-quote name)))
  18463. (when (org-between-regexps-p
  18464. (concat "^[ \t]*#\\+begin_" n)
  18465. (concat "^[ \t]*#\\+end_" n)
  18466. lim-up lim-down)
  18467. (throw 'exit n))))
  18468. names))
  18469. nil)))
  18470. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18471. "Call `multi-occur' with buffers for all agenda files."
  18472. (interactive "sOrg-files matching: \np")
  18473. (let* ((files (org-agenda-files))
  18474. (tnames (mapcar 'file-truename files))
  18475. (extra org-agenda-text-search-extra-files)
  18476. f)
  18477. (when (eq (car extra) 'agenda-archives)
  18478. (setq extra (cdr extra))
  18479. (setq files (org-add-archive-files files)))
  18480. (while (setq f (pop extra))
  18481. (unless (member (file-truename f) tnames)
  18482. (add-to-list 'files f 'append)
  18483. (add-to-list 'tnames (file-truename f) 'append)))
  18484. (multi-occur
  18485. (mapcar (lambda (x)
  18486. (with-current-buffer
  18487. (or (get-file-buffer x) (find-file-noselect x))
  18488. (widen)
  18489. (current-buffer)))
  18490. files)
  18491. regexp)))
  18492. (if (boundp 'occur-mode-find-occurrence-hook)
  18493. ;; Emacs 23
  18494. (add-hook 'occur-mode-find-occurrence-hook
  18495. (lambda ()
  18496. (when (derived-mode-p 'org-mode)
  18497. (org-reveal))))
  18498. ;; Emacs 22
  18499. (defadvice occur-mode-goto-occurrence
  18500. (after org-occur-reveal activate)
  18501. (and (derived-mode-p 'org-mode) (org-reveal)))
  18502. (defadvice occur-mode-goto-occurrence-other-window
  18503. (after org-occur-reveal activate)
  18504. (and (derived-mode-p 'org-mode) (org-reveal)))
  18505. (defadvice occur-mode-display-occurrence
  18506. (after org-occur-reveal activate)
  18507. (when (derived-mode-p 'org-mode)
  18508. (let ((pos (occur-mode-find-occurrence)))
  18509. (with-current-buffer (marker-buffer pos)
  18510. (save-excursion
  18511. (goto-char pos)
  18512. (org-reveal)))))))
  18513. (defun org-occur-link-in-agenda-files ()
  18514. "Create a link and search for it in the agendas.
  18515. The link is not stored in `org-stored-links', it is just created
  18516. for the search purpose."
  18517. (interactive)
  18518. (let ((link (condition-case nil
  18519. (org-store-link nil)
  18520. (error "Unable to create a link to here"))))
  18521. (org-occur-in-agenda-files (regexp-quote link))))
  18522. (defun org-uniquify (list)
  18523. "Remove duplicate elements from LIST."
  18524. (let (res)
  18525. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18526. res))
  18527. (defun org-delete-all (elts list)
  18528. "Remove all elements in ELTS from LIST."
  18529. (while elts
  18530. (setq list (delete (pop elts) list)))
  18531. list)
  18532. (defun org-count (cl-item cl-seq)
  18533. "Count the number of occurrences of ITEM in SEQ.
  18534. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18535. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18536. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18537. (while (< cl-start cl-end)
  18538. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18539. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18540. (setq cl-start (1+ cl-start)))
  18541. cl-count))
  18542. (defun org-remove-if (predicate seq)
  18543. "Remove everything from SEQ that fulfills PREDICATE."
  18544. (let (res e)
  18545. (while seq
  18546. (setq e (pop seq))
  18547. (if (not (funcall predicate e)) (push e res)))
  18548. (nreverse res)))
  18549. (defun org-remove-if-not (predicate seq)
  18550. "Remove everything from SEQ that does not fulfill PREDICATE."
  18551. (let (res e)
  18552. (while seq
  18553. (setq e (pop seq))
  18554. (if (funcall predicate e) (push e res)))
  18555. (nreverse res)))
  18556. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18557. "Reduce two-argument FUNCTION across SEQ.
  18558. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18559. \":initial-value\" removed."
  18560. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18561. (cadr (memq :initial-value cl-keys)))
  18562. (cl-seq (pop cl-seq))
  18563. (t (funcall cl-func)))))
  18564. (while cl-seq
  18565. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18566. cl-accum))
  18567. (defun org-back-over-empty-lines ()
  18568. "Move backwards over whitespace, to the beginning of the first empty line.
  18569. Returns the number of empty lines passed."
  18570. (let ((pos (point)))
  18571. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18572. (skip-chars-backward " \t\n\r")
  18573. (unless (eobp)
  18574. (forward-line -1)))
  18575. (beginning-of-line 2)
  18576. (goto-char (min (point) pos))
  18577. (count-lines (point) pos)))
  18578. (defun org-skip-whitespace ()
  18579. (skip-chars-forward " \t\n\r"))
  18580. (defun org-point-in-group (point group &optional context)
  18581. "Check if POINT is in match-group GROUP.
  18582. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18583. match. If the match group does not exist or point is not inside it,
  18584. return nil."
  18585. (and (match-beginning group)
  18586. (>= point (match-beginning group))
  18587. (<= point (match-end group))
  18588. (if context
  18589. (list context (match-beginning group) (match-end group))
  18590. t)))
  18591. (defun org-switch-to-buffer-other-window (&rest args)
  18592. "Switch to buffer in a second window on the current frame.
  18593. In particular, do not allow pop-up frames.
  18594. Returns the newly created buffer."
  18595. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18596. special-display-function)
  18597. (apply 'switch-to-buffer-other-window args)))
  18598. (defun org-combine-plists (&rest plists)
  18599. "Create a single property list from all plists in PLISTS.
  18600. The process starts by copying the first list, and then setting properties
  18601. from the other lists. Settings in the last list are the most significant
  18602. ones and overrule settings in the other lists."
  18603. (let ((rtn (copy-sequence (pop plists)))
  18604. p v ls)
  18605. (while plists
  18606. (setq ls (pop plists))
  18607. (while ls
  18608. (setq p (pop ls) v (pop ls))
  18609. (setq rtn (plist-put rtn p v))))
  18610. rtn))
  18611. (defun org-move-line-down (arg)
  18612. "Move the current line down. With prefix argument, move it past ARG lines."
  18613. (interactive "p")
  18614. (let ((col (current-column))
  18615. beg end pos)
  18616. (beginning-of-line 1) (setq beg (point))
  18617. (beginning-of-line 2) (setq end (point))
  18618. (beginning-of-line (+ 1 arg))
  18619. (setq pos (move-marker (make-marker) (point)))
  18620. (insert (delete-and-extract-region beg end))
  18621. (goto-char pos)
  18622. (org-move-to-column col)))
  18623. (defun org-move-line-up (arg)
  18624. "Move the current line up. With prefix argument, move it past ARG lines."
  18625. (interactive "p")
  18626. (let ((col (current-column))
  18627. beg end pos)
  18628. (beginning-of-line 1) (setq beg (point))
  18629. (beginning-of-line 2) (setq end (point))
  18630. (beginning-of-line (- arg))
  18631. (setq pos (move-marker (make-marker) (point)))
  18632. (insert (delete-and-extract-region beg end))
  18633. (goto-char pos)
  18634. (org-move-to-column col)))
  18635. (defun org-replace-escapes (string table)
  18636. "Replace %-escapes in STRING with values in TABLE.
  18637. TABLE is an association list with keys like \"%a\" and string values.
  18638. The sequences in STRING may contain normal field width and padding information,
  18639. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18640. so values can contain further %-escapes if they are define later in TABLE."
  18641. (let ((tbl (copy-alist table))
  18642. (case-fold-search nil)
  18643. (pchg 0)
  18644. e re rpl)
  18645. (while (setq e (pop tbl))
  18646. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18647. (when (and (cdr e) (string-match re (cdr e)))
  18648. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18649. (safe "SREF"))
  18650. (add-text-properties 0 3 (list 'sref sref) safe)
  18651. (setcdr e (replace-match safe t t (cdr e)))))
  18652. (while (string-match re string)
  18653. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18654. (cdr e)))
  18655. (setq string (replace-match rpl t t string))))
  18656. (while (setq pchg (next-property-change pchg string))
  18657. (let ((sref (get-text-property pchg 'sref string)))
  18658. (when (and sref (string-match "SREF" string pchg))
  18659. (setq string (replace-match sref t t string)))))
  18660. string))
  18661. (defun org-sublist (list start end)
  18662. "Return a section of LIST, from START to END.
  18663. Counting starts at 1."
  18664. (let (rtn (c start))
  18665. (setq list (nthcdr (1- start) list))
  18666. (while (and list (<= c end))
  18667. (push (pop list) rtn)
  18668. (setq c (1+ c)))
  18669. (nreverse rtn)))
  18670. (defun org-find-base-buffer-visiting (file)
  18671. "Like `find-buffer-visiting' but always return the base buffer and
  18672. not an indirect buffer."
  18673. (let ((buf (or (get-file-buffer file)
  18674. (find-buffer-visiting file))))
  18675. (if buf
  18676. (or (buffer-base-buffer buf) buf)
  18677. nil)))
  18678. (defun org-image-file-name-regexp (&optional extensions)
  18679. "Return regexp matching the file names of images.
  18680. If EXTENSIONS is given, only match these."
  18681. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18682. (image-file-name-regexp)
  18683. (let ((image-file-name-extensions
  18684. (or extensions
  18685. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18686. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18687. (concat "\\."
  18688. (regexp-opt (nconc (mapcar 'upcase
  18689. image-file-name-extensions)
  18690. image-file-name-extensions)
  18691. t)
  18692. "\\'"))))
  18693. (defun org-file-image-p (file &optional extensions)
  18694. "Return non-nil if FILE is an image."
  18695. (save-match-data
  18696. (string-match (org-image-file-name-regexp extensions) file)))
  18697. (defun org-get-cursor-date ()
  18698. "Return the date at cursor in as a time.
  18699. This works in the calendar and in the agenda, anywhere else it just
  18700. returns the current time."
  18701. (let (date day defd)
  18702. (cond
  18703. ((eq major-mode 'calendar-mode)
  18704. (setq date (calendar-cursor-to-date)
  18705. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18706. ((eq major-mode 'org-agenda-mode)
  18707. (setq day (get-text-property (point) 'day))
  18708. (if day
  18709. (setq date (calendar-gregorian-from-absolute day)
  18710. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18711. (nth 2 date))))))
  18712. (or defd (current-time))))
  18713. (defvar org-agenda-action-marker (make-marker)
  18714. "Marker pointing to the entry for the next agenda action.")
  18715. (defun org-mark-entry-for-agenda-action ()
  18716. "Mark the current entry as target of an agenda action.
  18717. Agenda actions are actions executed from the agenda with the key `k',
  18718. which make use of the date at the cursor."
  18719. (interactive)
  18720. (move-marker org-agenda-action-marker
  18721. (save-excursion (org-back-to-heading t) (point))
  18722. (current-buffer))
  18723. (message
  18724. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18725. (defun org-mark-subtree ()
  18726. "Mark the current subtree.
  18727. This puts point at the start of the current subtree, and mark at the end.
  18728. If point is in an inline task, mark that task instead."
  18729. (interactive)
  18730. (let ((inline-task-p
  18731. (and (featurep 'org-inlinetask)
  18732. (org-inlinetask-in-task-p)))
  18733. (beg))
  18734. ;; Get beginning of subtree
  18735. (cond
  18736. (inline-task-p (org-inlinetask-goto-beginning))
  18737. ((org-at-heading-p) (beginning-of-line))
  18738. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18739. (setq beg (point))
  18740. ;; Get end of it
  18741. (if inline-task-p
  18742. (org-inlinetask-goto-end)
  18743. (org-end-of-subtree))
  18744. ;; Mark zone
  18745. (push-mark (point) nil t)
  18746. (goto-char beg)))
  18747. ;;; Paragraph filling stuff.
  18748. ;; We want this to be just right, so use the full arsenal.
  18749. (defun org-indent-line-function ()
  18750. "Indent line depending on context."
  18751. (interactive)
  18752. (let* ((pos (point))
  18753. (itemp (org-at-item-p))
  18754. (case-fold-search t)
  18755. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18756. (inline-task-p (and (featurep 'org-inlinetask)
  18757. (org-inlinetask-in-task-p)))
  18758. (inline-re (and inline-task-p
  18759. (org-inlinetask-outline-regexp)))
  18760. column)
  18761. (beginning-of-line 1)
  18762. (cond
  18763. ;; Comments
  18764. ((looking-at "# ") (setq column 0))
  18765. ;; Headings
  18766. ((looking-at org-outline-regexp) (setq column 0))
  18767. ;; Included files
  18768. ((looking-at "#\\+include:") (setq column 0))
  18769. ;; Footnote definition
  18770. ((looking-at org-footnote-definition-re) (setq column 0))
  18771. ;; Literal examples
  18772. ((looking-at "[ \t]*:\\( \\|$\\)")
  18773. (setq column (org-get-indentation))) ; do nothing
  18774. ;; Lists
  18775. ((ignore-errors (goto-char (org-in-item-p)))
  18776. (setq column (if itemp
  18777. (org-get-indentation)
  18778. (org-list-item-body-column (point))))
  18779. (goto-char pos))
  18780. ;; Drawers
  18781. ((and (looking-at "[ \t]*:END:")
  18782. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18783. (save-excursion
  18784. (goto-char (1- (match-beginning 1)))
  18785. (setq column (current-column))))
  18786. ;; Special blocks
  18787. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18788. (save-excursion
  18789. (re-search-backward
  18790. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18791. (setq column (org-get-indentation (match-string 0))))
  18792. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18793. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18794. (save-excursion
  18795. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18796. (setq column
  18797. (cond ((equal (downcase (match-string 1)) "src")
  18798. ;; src blocks: let `org-edit-src-exit' handle them
  18799. (org-get-indentation))
  18800. ((equal (downcase (match-string 1)) "example")
  18801. (max (org-get-indentation)
  18802. (org-get-indentation (match-string 0))))
  18803. (t
  18804. (org-get-indentation (match-string 0))))))
  18805. ;; This line has nothing special, look at the previous relevant
  18806. ;; line to compute indentation
  18807. (t
  18808. (beginning-of-line 0)
  18809. (while (and (not (bobp))
  18810. (not (looking-at org-drawer-regexp))
  18811. ;; When point started in an inline task, do not move
  18812. ;; above task starting line.
  18813. (not (and inline-task-p (looking-at inline-re)))
  18814. ;; Skip drawers, blocks, empty lines, verbatim,
  18815. ;; comments, tables, footnotes definitions, lists,
  18816. ;; inline tasks.
  18817. (or (and (looking-at "[ \t]*:END:")
  18818. (re-search-backward org-drawer-regexp nil t))
  18819. (and (looking-at "[ \t]*#\\+end_")
  18820. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18821. (looking-at "[ \t]*[\n:#|]")
  18822. (looking-at org-footnote-definition-re)
  18823. (and (ignore-errors (goto-char (org-in-item-p)))
  18824. (goto-char
  18825. (org-list-get-top-point (org-list-struct))))
  18826. (and (not inline-task-p)
  18827. (featurep 'org-inlinetask)
  18828. (org-inlinetask-in-task-p)
  18829. (or (org-inlinetask-goto-beginning) t))))
  18830. (beginning-of-line 0))
  18831. (cond
  18832. ;; There was an heading above.
  18833. ((looking-at "\\*+[ \t]+")
  18834. (if (not org-adapt-indentation)
  18835. (setq column 0)
  18836. (goto-char (match-end 0))
  18837. (setq column (current-column))))
  18838. ;; A drawer had started and is unfinished
  18839. ((looking-at org-drawer-regexp)
  18840. (goto-char (1- (match-beginning 1)))
  18841. (setq column (current-column)))
  18842. ;; Else, nothing noticeable found: get indentation and go on.
  18843. (t (setq column (org-get-indentation))))))
  18844. ;; Now apply indentation and move cursor accordingly
  18845. (goto-char pos)
  18846. (if (<= (current-column) (current-indentation))
  18847. (org-indent-line-to column)
  18848. (save-excursion (org-indent-line-to column)))
  18849. ;; Special polishing for properties, see `org-property-format'
  18850. (setq column (current-column))
  18851. (beginning-of-line 1)
  18852. (if (looking-at
  18853. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18854. (replace-match (concat (match-string 1)
  18855. (format org-property-format
  18856. (match-string 2) (match-string 3)))
  18857. t t))
  18858. (org-move-to-column column)
  18859. (when (and orgstruct-is-++ (eq pos (point)))
  18860. (org-let org-fb-vars
  18861. '(indent-according-to-mode)))))
  18862. (defun org-indent-drawer ()
  18863. "Indent the drawer at point."
  18864. (interactive)
  18865. (let ((p (point))
  18866. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18867. (match-end 0)))
  18868. (folded
  18869. (save-excursion
  18870. (end-of-line)
  18871. (when (overlays-at (point))
  18872. (member 'invisible (overlay-properties
  18873. (car (overlays-at (point)))))))))
  18874. (when folded (org-cycle))
  18875. (indent-for-tab-command)
  18876. (while (and (move-beginning-of-line 2) (< (point) e))
  18877. (indent-for-tab-command))
  18878. (goto-char p)
  18879. (when folded (org-cycle)))
  18880. (message "Drawer at point indented"))
  18881. (defun org-indent-block ()
  18882. "Indent the block at point."
  18883. (interactive)
  18884. (let ((p (point))
  18885. (case-fold-search t)
  18886. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18887. (match-end 0)))
  18888. (folded
  18889. (save-excursion
  18890. (end-of-line)
  18891. (when (overlays-at (point))
  18892. (member 'invisible (overlay-properties
  18893. (car (overlays-at (point)))))))))
  18894. (when folded (org-cycle))
  18895. (indent-for-tab-command)
  18896. (while (and (move-beginning-of-line 2) (< (point) e))
  18897. (indent-for-tab-command))
  18898. (goto-char p)
  18899. (when folded (org-cycle)))
  18900. (message "Block at point indented"))
  18901. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18902. "Variable to store copy of `adaptive-fill-regexp'.
  18903. Since `adaptive-fill-regexp' is set to never match, we need to
  18904. store a backup of its value before entering `org-mode' so that
  18905. the functionality can be provided as a fall-back.")
  18906. (defun org-set-autofill-regexps ()
  18907. (interactive)
  18908. ;; In the paragraph separator we include headlines, because filling
  18909. ;; text in a line directly attached to a headline would otherwise
  18910. ;; fill the headline as well.
  18911. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18912. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18913. ;; The paragraph starter includes hand-formatted lists.
  18914. (org-set-local
  18915. 'paragraph-start
  18916. (concat
  18917. "\f" "\\|"
  18918. "[ ]*$" "\\|"
  18919. org-outline-regexp "\\|"
  18920. "[ \t]*#" "\\|"
  18921. (org-item-re) "\\|"
  18922. "[ \t]*[:|]" "\\|"
  18923. "\\$\\$" "\\|"
  18924. "\\\\\\(begin\\|end\\|[][]\\)"))
  18925. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18926. ;; But only if the user has not turned off tables or fixed-width regions
  18927. (org-set-local
  18928. 'auto-fill-inhibit-regexp
  18929. (concat org-outline-regexp
  18930. "\\|#\\+"
  18931. "\\|[ \t]*" org-keyword-time-regexp
  18932. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18933. (concat
  18934. "\\|[ \t]*["
  18935. (if org-enable-table-editor "|" "")
  18936. (if org-enable-fixed-width-editor ":" "")
  18937. "]"))))
  18938. ;; We use our own fill-paragraph function, to make sure that tables
  18939. ;; and fixed-width regions are not wrapped. That function will pass
  18940. ;; through to `fill-paragraph' when appropriate.
  18941. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18942. ;; Prevent auto-fill from inserting unwanted new items.
  18943. (if (boundp 'fill-nobreak-predicate)
  18944. (org-set-local
  18945. 'fill-nobreak-predicate
  18946. (org-uniquify
  18947. (append fill-nobreak-predicate
  18948. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18949. ;; Adaptive filling: To get full control, first make sure that
  18950. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18951. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18952. (org-set-local 'org-adaptive-fill-regexp-backup
  18953. adaptive-fill-regexp))
  18954. (org-set-local 'adaptive-fill-regexp "\000")
  18955. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18956. (org-set-local 'adaptive-fill-function
  18957. 'org-adaptive-fill-function)
  18958. (org-set-local
  18959. 'align-mode-rules-list
  18960. '((org-in-buffer-settings
  18961. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18962. (modes . '(org-mode))))))
  18963. (defun org-fill-item-nobreak-p ()
  18964. "Non-nil when a line break at point would insert a new item."
  18965. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18966. (defun org-fill-line-break-nobreak-p ()
  18967. "Non-nil when a line break at point would create an Org line break."
  18968. (save-excursion
  18969. (skip-chars-backward "[ \t]")
  18970. (skip-chars-backward "\\\\")
  18971. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18972. (defun org-fill-paragraph (&optional justify)
  18973. "Re-align a table, pass through to fill-paragraph if no table."
  18974. (let ((table-p (org-at-table-p))
  18975. (table.el-p (org-at-table.el-p))
  18976. (itemp (org-in-item-p)))
  18977. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18978. (save-excursion (goto-char (point-at-bol))
  18979. (looking-at org-outline-regexp)))
  18980. t) ; skip headlines
  18981. (table.el-p t) ; skip table.el tables
  18982. (table-p (org-table-align) t) ; align Org tables
  18983. (itemp ; align text in items
  18984. (let* ((struct (save-excursion (goto-char itemp)
  18985. (org-list-struct)))
  18986. (parents (org-list-parents-alist struct))
  18987. (children (org-list-get-children itemp struct parents))
  18988. beg end prev next prefix)
  18989. ;; Determine in which part of item point is: before
  18990. ;; first child, after last child, between two
  18991. ;; sub-lists, or simply in item if there's no child.
  18992. (cond
  18993. ((not children)
  18994. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18995. beg itemp
  18996. end (org-list-get-item-end itemp struct)))
  18997. ((< (point) (setq next (car children)))
  18998. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18999. beg itemp
  19000. end next))
  19001. ((> (point) (setq prev (car (last children))))
  19002. (setq beg (org-list-get-item-end prev struct)
  19003. end (org-list-get-item-end itemp struct)
  19004. prefix (save-excursion
  19005. (goto-char beg)
  19006. (skip-chars-forward " \t")
  19007. (make-string (current-column) ?\ ))))
  19008. (t (catch 'exit
  19009. (while (setq next (pop children))
  19010. (if (> (point) next)
  19011. (setq prev next)
  19012. (setq beg (org-list-get-item-end prev struct)
  19013. end next
  19014. prefix (save-excursion
  19015. (goto-char beg)
  19016. (skip-chars-forward " \t")
  19017. (make-string (current-column) ?\ )))
  19018. (throw 'exit nil))))))
  19019. ;; Use `fill-paragraph' with buffer narrowed to item
  19020. ;; without any child, and with our computed PREFIX.
  19021. (flet ((fill-context-prefix (from to &optional flr) prefix))
  19022. (save-restriction
  19023. (narrow-to-region beg end)
  19024. (save-excursion (fill-paragraph justify)))) t))
  19025. ;; Special case where point is not in a list but is on
  19026. ;; a paragraph adjacent to a list: make sure this paragraph
  19027. ;; doesn't get merged with the end of the list by narrowing
  19028. ;; buffer first.
  19029. ((and (derived-mode-p 'org-mode)
  19030. (save-excursion (forward-paragraph -1)
  19031. (setq itemp (org-in-item-p))))
  19032. (let ((struct (save-excursion (goto-char itemp)
  19033. (org-list-struct))))
  19034. (save-restriction
  19035. (narrow-to-region (org-list-get-bottom-point struct)
  19036. (save-excursion (forward-paragraph 1)
  19037. (point)))
  19038. (fill-paragraph justify) t)))
  19039. ;; Don't fill schedule/deadline line before a paragraph
  19040. ;; This only makes sense in real org-mode buffers
  19041. ((and (eq major-mode 'org-mode)
  19042. (save-excursion (forward-paragraph -1)
  19043. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  19044. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$")))))
  19045. (save-restriction
  19046. (narrow-to-region (1+ (match-end 0))
  19047. (save-excursion (forward-paragraph 1) (point)))
  19048. (fill-paragraph justify) t))
  19049. ;; Else falls back on `org-fill-paragraph-fallback-function'
  19050. (orgstruct-is-++
  19051. (org-let org-fb-vars
  19052. '(fill-paragraph)))
  19053. ;; (org-fill-paragraph-fallback-function
  19054. ;; (funcall org-fill-paragraph-fallback-function justify))
  19055. ;; Else simply call `fill-paragraph'.
  19056. (t nil))))
  19057. ;; For reference, this is the default value of adaptive-fill-regexp
  19058. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  19059. (defun org-adaptive-fill-function ()
  19060. "Return a fill prefix for org-mode files."
  19061. (let (itemp)
  19062. (save-excursion
  19063. (cond
  19064. ;; Comment line
  19065. ((looking-at "#[ \t]+")
  19066. (match-string-no-properties 0))
  19067. ;; Plain list item
  19068. ((org-at-item-p)
  19069. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  19070. ;; Point is in a list after `backward-paragraph': original
  19071. ;; point wasn't in the list, or filling would have been taken
  19072. ;; care of by `org-auto-fill-function', but the list and the
  19073. ;; real paragraph are not separated by a blank line. Thus, move
  19074. ;; point after the list to go back to real paragraph and
  19075. ;; determine fill-prefix.
  19076. ((setq itemp (org-in-item-p))
  19077. (goto-char itemp)
  19078. (let* ((struct (org-list-struct))
  19079. (bottom (org-list-get-bottom-point struct)))
  19080. (goto-char bottom)
  19081. (make-string (org-get-indentation) ?\ )))
  19082. ;; Other text
  19083. ((looking-at org-adaptive-fill-regexp-backup)
  19084. (match-string-no-properties 0))))))
  19085. (defun org-auto-fill-function ()
  19086. "Auto-fill function."
  19087. (unless (and org-src-prevent-auto-filling (org-in-src-block-p))
  19088. (let (itemp prefix)
  19089. ;; When in a list, compute an appropriate fill-prefix and make
  19090. ;; sure it will be used by `do-auto-fill'.
  19091. (cond ((setq itemp (org-in-item-p))
  19092. (progn
  19093. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  19094. (flet ((fill-context-prefix (from to &optional flr) prefix))
  19095. (do-auto-fill))))
  19096. (orgstruct-is-++
  19097. (org-let org-fb-vars
  19098. '(do-auto-fill)))
  19099. (t (do-auto-fill))))))
  19100. ;;; Other stuff.
  19101. (defun org-toggle-fixed-width-section (arg)
  19102. "Toggle the fixed-width export.
  19103. If there is no active region, the QUOTE keyword at the current headline is
  19104. inserted or removed. When present, it causes the text between this headline
  19105. and the next to be exported as fixed-width text, and unmodified.
  19106. If there is an active region, this command adds or removes a colon as the
  19107. first character of this line. If the first character of a line is a colon,
  19108. this line is also exported in fixed-width font."
  19109. (interactive "P")
  19110. (let* ((cc 0)
  19111. (regionp (org-region-active-p))
  19112. (beg (if regionp (region-beginning) (point)))
  19113. (end (if regionp (region-end)))
  19114. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19115. (case-fold-search nil)
  19116. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19117. off)
  19118. (if regionp
  19119. (save-excursion
  19120. (goto-char beg)
  19121. (setq cc (current-column))
  19122. (beginning-of-line 1)
  19123. (setq off (looking-at re))
  19124. (while (> nlines 0)
  19125. (setq nlines (1- nlines))
  19126. (beginning-of-line 1)
  19127. (cond
  19128. (arg
  19129. (org-move-to-column cc t)
  19130. (insert ": \n")
  19131. (forward-line -1))
  19132. ((and off (looking-at re))
  19133. (replace-match "" t t nil 1))
  19134. ((not off) (org-move-to-column cc t) (insert ": ")))
  19135. (forward-line 1)))
  19136. (save-excursion
  19137. (org-back-to-heading)
  19138. (cond
  19139. ((looking-at (format org-heading-keyword-regexp-format
  19140. org-quote-string))
  19141. (goto-char (match-end 1))
  19142. (looking-at (concat " +" org-quote-string))
  19143. (replace-match "" t t)
  19144. (when (eolp) (insert " ")))
  19145. ((looking-at org-outline-regexp)
  19146. (goto-char (match-end 0))
  19147. (insert org-quote-string " ")))))))
  19148. (defun org-reftex-citation ()
  19149. "Use reftex-citation to insert a citation into the buffer.
  19150. This looks for a line like
  19151. #+BIBLIOGRAPHY: foo plain option:-d
  19152. and derives from it that foo.bib is the bibliography file relevant
  19153. for this document. It then installs the necessary environment for RefTeX
  19154. to work in this buffer and calls `reftex-citation' to insert a citation
  19155. into the buffer.
  19156. Export of such citations to both LaTeX and HTML is handled by the contributed
  19157. package org-exp-bibtex by Taru Karttunen."
  19158. (interactive)
  19159. (let ((reftex-docstruct-symbol 'rds)
  19160. (reftex-cite-format "\\cite{%l}")
  19161. rds bib)
  19162. (save-excursion
  19163. (save-restriction
  19164. (widen)
  19165. (let ((case-fold-search t)
  19166. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19167. (if (not (save-excursion
  19168. (or (re-search-forward re nil t)
  19169. (re-search-backward re nil t))))
  19170. (error "No bibliography defined in file")
  19171. (setq bib (concat (match-string 1) ".bib")
  19172. rds (list (list 'bib bib)))))))
  19173. (call-interactively 'reftex-citation)))
  19174. ;;;; Functions extending outline functionality
  19175. (defun org-beginning-of-line (&optional arg)
  19176. "Go to the beginning of the current line. If that is invisible, continue
  19177. to a visible line beginning. This makes the function of C-a more intuitive.
  19178. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19179. first attempt, and only move to after the tags when the cursor is already
  19180. beyond the end of the headline."
  19181. (interactive "P")
  19182. (let ((pos (point))
  19183. (special (if (consp org-special-ctrl-a/e)
  19184. (car org-special-ctrl-a/e)
  19185. org-special-ctrl-a/e))
  19186. refpos)
  19187. (if (org-bound-and-true-p line-move-visual)
  19188. (beginning-of-visual-line 1)
  19189. (beginning-of-line 1))
  19190. (if (and arg (fboundp 'move-beginning-of-line))
  19191. (call-interactively 'move-beginning-of-line)
  19192. (if (bobp)
  19193. nil
  19194. (backward-char 1)
  19195. (if (org-truely-invisible-p)
  19196. (while (and (not (bobp)) (org-truely-invisible-p))
  19197. (backward-char 1)
  19198. (beginning-of-line 1))
  19199. (forward-char 1))))
  19200. (when special
  19201. (cond
  19202. ((and (looking-at org-complex-heading-regexp)
  19203. (= (char-after (match-end 1)) ?\ ))
  19204. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19205. (point-at-eol)))
  19206. (goto-char
  19207. (if (eq special t)
  19208. (cond ((> pos refpos) refpos)
  19209. ((= pos (point)) refpos)
  19210. (t (point)))
  19211. (cond ((> pos (point)) (point))
  19212. ((not (eq last-command this-command)) (point))
  19213. (t refpos)))))
  19214. ((org-at-item-p)
  19215. ;; Being at an item and not looking at an the item means point
  19216. ;; was previously moved to beginning of a visual line, which
  19217. ;; doesn't contain the item. Therefore, do nothing special,
  19218. ;; just stay here.
  19219. (when (looking-at org-list-full-item-re)
  19220. ;; Set special position at first white space character after
  19221. ;; bullet, and check-box, if any.
  19222. (let ((after-bullet
  19223. (let ((box (match-end 3)))
  19224. (if (not box) (match-end 1)
  19225. (let ((after (char-after box)))
  19226. (if (and after (= after ? )) (1+ box) box))))))
  19227. ;; Special case: Move point to special position when
  19228. ;; currently after it or at beginning of line.
  19229. (if (eq special t)
  19230. (when (or (> pos after-bullet) (= (point) pos))
  19231. (goto-char after-bullet))
  19232. ;; Reversed case: Move point to special position when
  19233. ;; point was already at beginning of line and command is
  19234. ;; repeated.
  19235. (when (and (= (point) pos) (eq last-command this-command))
  19236. (goto-char after-bullet))))))))
  19237. (org-no-warnings
  19238. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19239. (defun org-end-of-line (&optional arg)
  19240. "Go to the end of the line.
  19241. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19242. first attempt, and only move to after the tags when the cursor is already
  19243. beyond the end of the headline."
  19244. (interactive "P")
  19245. (let ((special (if (consp org-special-ctrl-a/e)
  19246. (cdr org-special-ctrl-a/e)
  19247. org-special-ctrl-a/e)))
  19248. (cond
  19249. ((or (not special) arg
  19250. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19251. (call-interactively
  19252. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19253. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19254. (t 'end-of-line))))
  19255. ((org-at-heading-p)
  19256. (let ((pos (point)))
  19257. (beginning-of-line 1)
  19258. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19259. (if (eq special t)
  19260. (if (or (< pos (match-beginning 1))
  19261. (= pos (match-end 0)))
  19262. (goto-char (match-beginning 1))
  19263. (goto-char (match-end 0)))
  19264. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19265. (goto-char (match-end 0))
  19266. (goto-char (match-beginning 1))))
  19267. (call-interactively (if (fboundp 'move-end-of-line)
  19268. 'move-end-of-line
  19269. 'end-of-line)))))
  19270. ((org-at-drawer-p)
  19271. (move-end-of-line 1)
  19272. (when (overlays-at (1- (point))) (backward-char 1)))
  19273. ;; At an item: Move before any hidden text.
  19274. (t (call-interactively
  19275. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19276. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19277. (t 'end-of-line)))))
  19278. (org-no-warnings
  19279. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19280. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19281. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19282. (defun org-backward-sentence (&optional arg)
  19283. "Go to beginning of sentence, or beginning of table field.
  19284. This will call `backward-sentence' or `org-table-beginning-of-field',
  19285. depending on context."
  19286. (interactive "P")
  19287. (cond
  19288. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19289. (t (call-interactively 'backward-sentence))))
  19290. (defun org-forward-sentence (&optional arg)
  19291. "Go to end of sentence, or end of table field.
  19292. This will call `forward-sentence' or `org-table-end-of-field',
  19293. depending on context."
  19294. (interactive "P")
  19295. (cond
  19296. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19297. (t (call-interactively 'forward-sentence))))
  19298. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19299. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19300. (defun org-kill-line (&optional arg)
  19301. "Kill line, to tags or end of line."
  19302. (interactive "P")
  19303. (cond
  19304. ((or (not org-special-ctrl-k)
  19305. (bolp)
  19306. (not (org-at-heading-p)))
  19307. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19308. org-ctrl-k-protect-subtree)
  19309. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19310. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19311. (error "C-k aborted - would kill hidden subtree")))
  19312. (call-interactively
  19313. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19314. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19315. (kill-region (point) (match-beginning 1))
  19316. (org-set-tags nil t))
  19317. (t (kill-region (point) (point-at-eol)))))
  19318. (define-key org-mode-map "\C-k" 'org-kill-line)
  19319. (defun org-yank (&optional arg)
  19320. "Yank. If the kill is a subtree, treat it specially.
  19321. This command will look at the current kill and check if is a single
  19322. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19323. cursor is at the beginning of a line or after the stars of a currently
  19324. empty headline, then the yank is handled specially. How exactly depends
  19325. on the value of the following variables, both set by default.
  19326. org-yank-folded-subtrees
  19327. When set, the subtree(s) will be folded after insertion, but only
  19328. if doing so would now swallow text after the yanked text.
  19329. org-yank-adjusted-subtrees
  19330. When set, the subtree will be promoted or demoted in order to
  19331. fit into the local outline tree structure, which means that the level
  19332. will be adjusted so that it becomes the smaller one of the two
  19333. *visible* surrounding headings.
  19334. Any prefix to this command will cause `yank' to be called directly with
  19335. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19336. will just
  19337. plainly yank the text as it is.
  19338. \[1] The test checks if the first non-white line is a heading
  19339. and if there are no other headings with fewer stars."
  19340. (interactive "P")
  19341. (org-yank-generic 'yank arg))
  19342. (defun org-yank-generic (command arg)
  19343. "Perform some yank-like command.
  19344. This function implements the behavior described in the `org-yank'
  19345. documentation. However, it has been generalized to work for any
  19346. interactive command with similar behavior."
  19347. ;; pretend to be command COMMAND
  19348. (setq this-command command)
  19349. (if arg
  19350. (call-interactively command)
  19351. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19352. (and (org-kill-is-subtree-p)
  19353. (or (bolp)
  19354. (and (looking-at "[ \t]*$")
  19355. (string-match
  19356. "\\`\\*+\\'"
  19357. (buffer-substring (point-at-bol) (point)))))))
  19358. swallowp)
  19359. (cond
  19360. ((and subtreep org-yank-folded-subtrees)
  19361. (let ((beg (point))
  19362. end)
  19363. (if (and subtreep org-yank-adjusted-subtrees)
  19364. (org-paste-subtree nil nil 'for-yank)
  19365. (call-interactively command))
  19366. (setq end (point))
  19367. (goto-char beg)
  19368. (when (and (bolp) subtreep
  19369. (not (setq swallowp
  19370. (org-yank-folding-would-swallow-text beg end))))
  19371. (org-with-limited-levels
  19372. (or (looking-at org-outline-regexp)
  19373. (re-search-forward org-outline-regexp-bol end t))
  19374. (while (and (< (point) end) (looking-at org-outline-regexp))
  19375. (hide-subtree)
  19376. (org-cycle-show-empty-lines 'folded)
  19377. (condition-case nil
  19378. (outline-forward-same-level 1)
  19379. (error (goto-char end))))))
  19380. (when swallowp
  19381. (message
  19382. "Inserted text not folded because that would swallow text"))
  19383. (goto-char end)
  19384. (skip-chars-forward " \t\n\r")
  19385. (beginning-of-line 1)
  19386. (push-mark beg 'nomsg)))
  19387. ((and subtreep org-yank-adjusted-subtrees)
  19388. (let ((beg (point-at-bol)))
  19389. (org-paste-subtree nil nil 'for-yank)
  19390. (push-mark beg 'nomsg)))
  19391. (t
  19392. (call-interactively command))))))
  19393. (defun org-yank-folding-would-swallow-text (beg end)
  19394. "Would hide-subtree at BEG swallow any text after END?"
  19395. (let (level)
  19396. (org-with-limited-levels
  19397. (save-excursion
  19398. (goto-char beg)
  19399. (when (or (looking-at org-outline-regexp)
  19400. (re-search-forward org-outline-regexp-bol end t))
  19401. (setq level (org-outline-level)))
  19402. (goto-char end)
  19403. (skip-chars-forward " \t\r\n\v\f")
  19404. (if (or (eobp)
  19405. (and (bolp) (looking-at org-outline-regexp)
  19406. (<= (org-outline-level) level)))
  19407. nil ; Nothing would be swallowed
  19408. t))))) ; something would swallow
  19409. (define-key org-mode-map "\C-y" 'org-yank)
  19410. (defun org-truely-invisible-p ()
  19411. "Check if point is at a character currently not visible.
  19412. This version does not only check the character property, but also
  19413. `visible-mode'."
  19414. ;; Early versions of noutline don't have `outline-invisible-p'.
  19415. (if (org-bound-and-true-p visible-mode)
  19416. nil
  19417. (outline-invisible-p)))
  19418. (defun org-invisible-p2 ()
  19419. "Check if point is at a character currently not visible."
  19420. (save-excursion
  19421. (if (and (eolp) (not (bobp))) (backward-char 1))
  19422. ;; Early versions of noutline don't have `outline-invisible-p'.
  19423. (outline-invisible-p)))
  19424. (defun org-back-to-heading (&optional invisible-ok)
  19425. "Call `outline-back-to-heading', but provide a better error message."
  19426. (condition-case nil
  19427. (outline-back-to-heading invisible-ok)
  19428. (error (error "Before first headline at position %d in buffer %s"
  19429. (point) (current-buffer)))))
  19430. (defun org-beginning-of-defun ()
  19431. "Go to the beginning of the subtree, i.e. back to the heading."
  19432. (org-back-to-heading))
  19433. (defun org-end-of-defun ()
  19434. "Go to the end of the subtree."
  19435. (org-end-of-subtree nil t))
  19436. (defun org-before-first-heading-p ()
  19437. "Before first heading?"
  19438. (save-excursion
  19439. (end-of-line)
  19440. (null (re-search-backward org-outline-regexp-bol nil t))))
  19441. (defun org-at-heading-p (&optional ignored)
  19442. (outline-on-heading-p t))
  19443. ;; Compatibility alias with Org versions < 7.8.03
  19444. (defalias 'org-on-heading-p 'org-at-heading-p)
  19445. (defun org-at-drawer-p nil
  19446. "Is cursor at a drawer keyword?"
  19447. (save-excursion
  19448. (move-beginning-of-line 1)
  19449. (looking-at org-drawer-regexp)))
  19450. (defun org-at-block-p nil
  19451. "Is cursor at a block keyword?"
  19452. (save-excursion
  19453. (move-beginning-of-line 1)
  19454. (looking-at org-block-regexp)))
  19455. (defun org-point-at-end-of-empty-headline ()
  19456. "If point is at the end of an empty headline, return t, else nil.
  19457. If the heading only contains a TODO keyword, it is still still considered
  19458. empty."
  19459. (and (looking-at "[ \t]*$")
  19460. (when org-todo-line-regexp
  19461. (save-excursion
  19462. (beginning-of-line 1)
  19463. (let ((case-fold-search nil))
  19464. (looking-at org-todo-line-regexp)
  19465. (string= (match-string 3) ""))))))
  19466. (defun org-at-heading-or-item-p ()
  19467. (or (org-at-heading-p) (org-at-item-p)))
  19468. (defun org-at-target-p ()
  19469. (or (org-in-regexp org-radio-target-regexp)
  19470. (org-in-regexp org-target-regexp)))
  19471. ;; Compatibility alias with Org versions < 7.8.03
  19472. (defalias 'org-on-target-p 'org-at-target-p)
  19473. (defun org-up-heading-all (arg)
  19474. "Move to the heading line of which the present line is a subheading.
  19475. This function considers both visible and invisible heading lines.
  19476. With argument, move up ARG levels."
  19477. (if (fboundp 'outline-up-heading-all)
  19478. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19479. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19480. (defun org-up-heading-safe ()
  19481. "Move to the heading line of which the present line is a subheading.
  19482. This version will not throw an error. It will return the level of the
  19483. headline found, or nil if no higher level is found.
  19484. Also, this function will be a lot faster than `outline-up-heading',
  19485. because it relies on stars being the outline starters. This can really
  19486. make a significant difference in outlines with very many siblings."
  19487. (let (start-level re)
  19488. (org-back-to-heading t)
  19489. (setq start-level (funcall outline-level))
  19490. (if (equal start-level 1)
  19491. nil
  19492. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19493. (if (re-search-backward re nil t)
  19494. (funcall outline-level)))))
  19495. (defun org-first-sibling-p ()
  19496. "Is this heading the first child of its parents?"
  19497. (interactive)
  19498. (let ((re org-outline-regexp-bol)
  19499. level l)
  19500. (unless (org-at-heading-p t)
  19501. (error "Not at a heading"))
  19502. (setq level (funcall outline-level))
  19503. (save-excursion
  19504. (if (not (re-search-backward re nil t))
  19505. t
  19506. (setq l (funcall outline-level))
  19507. (< l level)))))
  19508. (defun org-goto-sibling (&optional previous)
  19509. "Goto the next sibling, even if it is invisible.
  19510. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19511. when a sibling was found. When none is found, return nil and don't
  19512. move point."
  19513. (let ((fun (if previous 're-search-backward 're-search-forward))
  19514. (pos (point))
  19515. (re org-outline-regexp-bol)
  19516. level l)
  19517. (when (condition-case nil (org-back-to-heading t) (error nil))
  19518. (setq level (funcall outline-level))
  19519. (catch 'exit
  19520. (or previous (forward-char 1))
  19521. (while (funcall fun re nil t)
  19522. (setq l (funcall outline-level))
  19523. (when (< l level) (goto-char pos) (throw 'exit nil))
  19524. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19525. (goto-char pos)
  19526. nil))))
  19527. (defun org-show-siblings ()
  19528. "Show all siblings of the current headline."
  19529. (save-excursion
  19530. (while (org-goto-sibling) (org-flag-heading nil)))
  19531. (save-excursion
  19532. (while (org-goto-sibling 'previous)
  19533. (org-flag-heading nil))))
  19534. (defun org-goto-first-child ()
  19535. "Goto the first child, even if it is invisible.
  19536. Return t when a child was found. Otherwise don't move point and
  19537. return nil."
  19538. (let (level (pos (point)) (re org-outline-regexp-bol))
  19539. (when (condition-case nil (org-back-to-heading t) (error nil))
  19540. (setq level (outline-level))
  19541. (forward-char 1)
  19542. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19543. (progn (goto-char (match-beginning 0)) t)
  19544. (goto-char pos) nil))))
  19545. (defun org-show-hidden-entry ()
  19546. "Show an entry where even the heading is hidden."
  19547. (save-excursion
  19548. (org-show-entry)))
  19549. (defun org-flag-heading (flag &optional entry)
  19550. "Flag the current heading. FLAG non-nil means make invisible.
  19551. When ENTRY is non-nil, show the entire entry."
  19552. (save-excursion
  19553. (org-back-to-heading t)
  19554. ;; Check if we should show the entire entry
  19555. (if entry
  19556. (progn
  19557. (org-show-entry)
  19558. (save-excursion
  19559. (and (outline-next-heading)
  19560. (org-flag-heading nil))))
  19561. (outline-flag-region (max (point-min) (1- (point)))
  19562. (save-excursion (outline-end-of-heading) (point))
  19563. flag))))
  19564. (defun org-get-next-sibling ()
  19565. "Move to next heading of the same level, and return point.
  19566. If there is no such heading, return nil.
  19567. This is like outline-next-sibling, but invisible headings are ok."
  19568. (let ((level (funcall outline-level)))
  19569. (outline-next-heading)
  19570. (while (and (not (eobp)) (> (funcall outline-level) level))
  19571. (outline-next-heading))
  19572. (if (or (eobp) (< (funcall outline-level) level))
  19573. nil
  19574. (point))))
  19575. (defun org-get-last-sibling ()
  19576. "Move to previous heading of the same level, and return point.
  19577. If there is no such heading, return nil."
  19578. (let ((opoint (point))
  19579. (level (funcall outline-level)))
  19580. (outline-previous-heading)
  19581. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19582. (while (and (> (funcall outline-level) level)
  19583. (not (bobp)))
  19584. (outline-previous-heading))
  19585. (if (< (funcall outline-level) level)
  19586. nil
  19587. (point)))))
  19588. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19589. ;; This contains an exact copy of the original function, but it uses
  19590. ;; `org-back-to-heading', to make it work also in invisible
  19591. ;; trees. And is uses an invisible-OK argument.
  19592. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19593. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19594. ;; with many children and grandchildren etc, this can be much faster
  19595. ;; than the outline version.
  19596. (org-back-to-heading invisible-OK)
  19597. (let ((first t)
  19598. (level (funcall outline-level)))
  19599. (if (and (derived-mode-p 'org-mode) (< level 1000))
  19600. ;; A true heading (not a plain list item), in Org-mode
  19601. ;; This means we can easily find the end by looking
  19602. ;; only for the right number of stars. Using a regexp to do
  19603. ;; this is so much faster than using a Lisp loop.
  19604. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19605. (forward-char 1)
  19606. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19607. ;; something else, do it the slow way
  19608. (while (and (not (eobp))
  19609. (or first (> (funcall outline-level) level)))
  19610. (setq first nil)
  19611. (outline-next-heading)))
  19612. (unless to-heading
  19613. (if (memq (preceding-char) '(?\n ?\^M))
  19614. (progn
  19615. ;; Go to end of line before heading
  19616. (forward-char -1)
  19617. (if (memq (preceding-char) '(?\n ?\^M))
  19618. ;; leave blank line before heading
  19619. (forward-char -1))))))
  19620. (point))
  19621. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19622. "Use Org version in org-mode, for dramatic speed-up."
  19623. (if (derived-mode-p 'org-mode)
  19624. (progn
  19625. (org-end-of-subtree nil t)
  19626. (unless (eobp) (backward-char 1)))
  19627. ad-do-it))
  19628. (defun org-end-of-meta-data-and-drawers ()
  19629. "Jump to the first text after meta data and drawers in the current entry.
  19630. This will move over empty lines, lines with planning time stamps,
  19631. clocking lines, and drawers."
  19632. (org-back-to-heading t)
  19633. (let ((end (save-excursion (outline-next-heading) (point)))
  19634. (re (concat "\\(" org-drawer-regexp "\\)"
  19635. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19636. (forward-line 1)
  19637. (while (re-search-forward re end t)
  19638. (if (not (match-end 1))
  19639. ;; empty or planning line
  19640. (forward-line 1)
  19641. ;; a drawer, find the end
  19642. (re-search-forward "^[ \t]*:END:" end 'move)
  19643. (forward-line 1)))
  19644. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19645. (point)))
  19646. (defun org-forward-same-level (arg &optional invisible-ok)
  19647. "Move forward to the arg'th subheading at same level as this one.
  19648. Stop at the first and last subheadings of a superior heading.
  19649. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19650. it wil also look at invisible ones."
  19651. (interactive "p")
  19652. (org-back-to-heading invisible-ok)
  19653. (org-at-heading-p)
  19654. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19655. (re (format "^\\*\\{1,%d\\} " level))
  19656. l)
  19657. (forward-char 1)
  19658. (while (> arg 0)
  19659. (while (and (re-search-forward re nil 'move)
  19660. (setq l (- (match-end 0) (match-beginning 0) 1))
  19661. (= l level)
  19662. (not invisible-ok)
  19663. (progn (backward-char 1) (outline-invisible-p)))
  19664. (if (< l level) (setq arg 1)))
  19665. (setq arg (1- arg)))
  19666. (beginning-of-line 1)))
  19667. (defun org-backward-same-level (arg &optional invisible-ok)
  19668. "Move backward to the arg'th subheading at same level as this one.
  19669. Stop at the first and last subheadings of a superior heading."
  19670. (interactive "p")
  19671. (org-back-to-heading)
  19672. (org-at-heading-p)
  19673. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19674. (re (format "^\\*\\{1,%d\\} " level))
  19675. l)
  19676. (while (> arg 0)
  19677. (while (and (re-search-backward re nil 'move)
  19678. (setq l (- (match-end 0) (match-beginning 0) 1))
  19679. (= l level)
  19680. (not invisible-ok)
  19681. (outline-invisible-p))
  19682. (if (< l level) (setq arg 1)))
  19683. (setq arg (1- arg)))))
  19684. (defun org-show-subtree ()
  19685. "Show everything after this heading at deeper levels."
  19686. (interactive)
  19687. (outline-flag-region
  19688. (point)
  19689. (save-excursion
  19690. (org-end-of-subtree t t))
  19691. nil))
  19692. (defun org-show-entry ()
  19693. "Show the body directly following this heading.
  19694. Show the heading too, if it is currently invisible."
  19695. (interactive)
  19696. (save-excursion
  19697. (condition-case nil
  19698. (progn
  19699. (org-back-to-heading t)
  19700. (outline-flag-region
  19701. (max (point-min) (1- (point)))
  19702. (save-excursion
  19703. (if (re-search-forward
  19704. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19705. (match-beginning 1)
  19706. (point-max)))
  19707. nil)
  19708. (org-cycle-hide-drawers 'children))
  19709. (error nil))))
  19710. (defun org-make-options-regexp (kwds &optional extra)
  19711. "Make a regular expression for keyword lines."
  19712. (concat
  19713. "^"
  19714. "#?[ \t]*\\+\\("
  19715. (mapconcat 'regexp-quote kwds "\\|")
  19716. (if extra (concat "\\|" extra))
  19717. "\\):[ \t]*"
  19718. "\\(.*\\)"))
  19719. ;; Make isearch reveal the necessary context
  19720. (defun org-isearch-end ()
  19721. "Reveal context after isearch exits."
  19722. (when isearch-success ; only if search was successful
  19723. (if (featurep 'xemacs)
  19724. ;; Under XEmacs, the hook is run in the correct place,
  19725. ;; we directly show the context.
  19726. (org-show-context 'isearch)
  19727. ;; In Emacs the hook runs *before* restoring the overlays.
  19728. ;; So we have to use a one-time post-command-hook to do this.
  19729. ;; (Emacs 22 has a special variable, see function `org-mode')
  19730. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19731. isearch-mode-end-hook-quit)
  19732. ;; Only when the isearch was not quitted.
  19733. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19734. 'append 'local)))))
  19735. (defun org-isearch-post-command ()
  19736. "Remove self from hook, and show context."
  19737. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19738. (org-show-context 'isearch))
  19739. ;;;; Integration with and fixes for other packages
  19740. ;;; Imenu support
  19741. (defvar org-imenu-markers nil
  19742. "All markers currently used by Imenu.")
  19743. (make-variable-buffer-local 'org-imenu-markers)
  19744. (defun org-imenu-new-marker (&optional pos)
  19745. "Return a new marker for use by Imenu, and remember the marker."
  19746. (let ((m (make-marker)))
  19747. (move-marker m (or pos (point)))
  19748. (push m org-imenu-markers)
  19749. m))
  19750. (defun org-imenu-get-tree ()
  19751. "Produce the index for Imenu."
  19752. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19753. (setq org-imenu-markers nil)
  19754. (let* ((n org-imenu-depth)
  19755. (re (concat "^" (org-get-limited-outline-regexp)))
  19756. (subs (make-vector (1+ n) nil))
  19757. (last-level 0)
  19758. m level head)
  19759. (save-excursion
  19760. (save-restriction
  19761. (widen)
  19762. (goto-char (point-max))
  19763. (while (re-search-backward re nil t)
  19764. (setq level (org-reduced-level (funcall outline-level)))
  19765. (when (and (<= level n)
  19766. (looking-at org-complex-heading-regexp))
  19767. (setq head (org-link-display-format
  19768. (org-match-string-no-properties 4))
  19769. m (org-imenu-new-marker))
  19770. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19771. (if (>= level last-level)
  19772. (push (cons head m) (aref subs level))
  19773. (push (cons head (aref subs (1+ level))) (aref subs level))
  19774. (loop for i from (1+ level) to n do (aset subs i nil)))
  19775. (setq last-level level)))))
  19776. (aref subs 1)))
  19777. (eval-after-load "imenu"
  19778. '(progn
  19779. (add-hook 'imenu-after-jump-hook
  19780. (lambda ()
  19781. (if (derived-mode-p 'org-mode)
  19782. (org-show-context 'org-goto))))))
  19783. (defun org-link-display-format (link)
  19784. "Replace a link with either the description, or the link target
  19785. if no description is present"
  19786. (save-match-data
  19787. (if (string-match org-bracket-link-analytic-regexp link)
  19788. (replace-match (if (match-end 5)
  19789. (match-string 5 link)
  19790. (concat (match-string 1 link)
  19791. (match-string 3 link)))
  19792. nil t link)
  19793. link)))
  19794. (defun org-toggle-link-display ()
  19795. "Toggle the literal or descriptive display of links."
  19796. (interactive)
  19797. (if org-descriptive-links
  19798. (progn (org-remove-from-invisibility-spec '(org-link))
  19799. (org-restart-font-lock)
  19800. (setq org-descriptive-links nil))
  19801. (progn (add-to-invisibility-spec '(org-link))
  19802. (org-restart-font-lock)
  19803. (setq org-descriptive-links t))))
  19804. ;; Speedbar support
  19805. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19806. "Overlay marking the agenda restriction line in speedbar.")
  19807. (overlay-put org-speedbar-restriction-lock-overlay
  19808. 'face 'org-agenda-restriction-lock)
  19809. (overlay-put org-speedbar-restriction-lock-overlay
  19810. 'help-echo "Agendas are currently limited to this item.")
  19811. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19812. (defun org-speedbar-set-agenda-restriction ()
  19813. "Restrict future agenda commands to the location at point in speedbar.
  19814. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19815. (interactive)
  19816. (require 'org-agenda)
  19817. (let (p m tp np dir txt)
  19818. (cond
  19819. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19820. 'org-imenu t))
  19821. (setq m (get-text-property p 'org-imenu-marker))
  19822. (with-current-buffer (marker-buffer m)
  19823. (goto-char m)
  19824. (org-agenda-set-restriction-lock 'subtree)))
  19825. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19826. 'speedbar-function 'speedbar-find-file))
  19827. (setq tp (previous-single-property-change
  19828. (1+ p) 'speedbar-function)
  19829. np (next-single-property-change
  19830. tp 'speedbar-function)
  19831. dir (speedbar-line-directory)
  19832. txt (buffer-substring-no-properties (or tp (point-min))
  19833. (or np (point-max))))
  19834. (with-current-buffer (find-file-noselect
  19835. (let ((default-directory dir))
  19836. (expand-file-name txt)))
  19837. (unless (derived-mode-p 'org-mode)
  19838. (error "Cannot restrict to non-Org-mode file"))
  19839. (org-agenda-set-restriction-lock 'file)))
  19840. (t (error "Don't know how to restrict Org-mode's agenda")))
  19841. (move-overlay org-speedbar-restriction-lock-overlay
  19842. (point-at-bol) (point-at-eol))
  19843. (setq current-prefix-arg nil)
  19844. (org-agenda-maybe-redo)))
  19845. (eval-after-load "speedbar"
  19846. '(progn
  19847. (speedbar-add-supported-extension ".org")
  19848. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19849. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19850. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19851. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19852. (add-hook 'speedbar-visiting-tag-hook
  19853. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  19854. ;;; Fixes and Hacks for problems with other packages
  19855. ;; Make flyspell not check words in links, to not mess up our keymap
  19856. (defun org-mode-flyspell-verify ()
  19857. "Don't let flyspell put overlays at active buttons, or on
  19858. {todo,all-time,additional-option-like}-keywords."
  19859. (let ((pos (max (1- (point)) (point-min)))
  19860. (word (thing-at-point 'word)))
  19861. (and (not (get-text-property pos 'keymap))
  19862. (not (get-text-property pos 'org-no-flyspell))
  19863. (not (member word org-todo-keywords-1))
  19864. (not (member word org-all-time-keywords))
  19865. (not (member word org-additional-option-like-keywords)))))
  19866. (defun org-remove-flyspell-overlays-in (beg end)
  19867. "Remove flyspell overlays in region."
  19868. (and (org-bound-and-true-p flyspell-mode)
  19869. (fboundp 'flyspell-delete-region-overlays)
  19870. (flyspell-delete-region-overlays beg end))
  19871. (add-text-properties beg end '(org-no-flyspell t)))
  19872. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19873. (eval-after-load "bookmark"
  19874. '(if (boundp 'bookmark-after-jump-hook)
  19875. ;; We can use the hook
  19876. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19877. ;; Hook not available, use advice
  19878. (defadvice bookmark-jump (after org-make-visible activate)
  19879. "Make the position visible."
  19880. (org-bookmark-jump-unhide))))
  19881. ;; Make sure saveplace shows the location if it was hidden
  19882. (eval-after-load "saveplace"
  19883. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19884. "Make the position visible."
  19885. (org-bookmark-jump-unhide)))
  19886. ;; Make sure ecb shows the location if it was hidden
  19887. (eval-after-load "ecb"
  19888. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19889. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19890. (if (derived-mode-p 'org-mode)
  19891. (org-show-context))))
  19892. (defun org-bookmark-jump-unhide ()
  19893. "Unhide the current position, to show the bookmark location."
  19894. (and (derived-mode-p 'org-mode)
  19895. (or (outline-invisible-p)
  19896. (save-excursion (goto-char (max (point-min) (1- (point))))
  19897. (outline-invisible-p)))
  19898. (org-show-context 'bookmark-jump)))
  19899. ;; Make session.el ignore our circular variable
  19900. (eval-after-load "session"
  19901. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19902. ;;;; Experimental code
  19903. (defun org-closed-in-range ()
  19904. "Sparse tree of items closed in a certain time range.
  19905. Still experimental, may disappear in the future."
  19906. (interactive)
  19907. ;; Get the time interval from the user.
  19908. (let* ((time1 (org-float-time
  19909. (org-read-date nil 'to-time nil "Starting date: ")))
  19910. (time2 (org-float-time
  19911. (org-read-date nil 'to-time nil "End date:")))
  19912. ;; callback function
  19913. (callback (lambda ()
  19914. (let ((time
  19915. (org-float-time
  19916. (apply 'encode-time
  19917. (org-parse-time-string
  19918. (match-string 1))))))
  19919. ;; check if time in interval
  19920. (and (>= time time1) (<= time time2))))))
  19921. ;; make tree, check each match with the callback
  19922. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19923. ;;;; Finish up
  19924. (provide 'org)
  19925. (run-hooks 'org-load-hook)
  19926. ;;; org.el ends here