org.el 727 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.01trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  83. ;; the file noutline.el being loaded.
  84. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  85. ;; We require noutline, which might be provided in outline.el
  86. (require 'outline) (require 'noutline)
  87. ;; Other stuff we need.
  88. (require 'time-date)
  89. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  90. (require 'easymenu)
  91. (require 'overlay)
  92. (require 'org-macs)
  93. (require 'org-entities)
  94. (require 'org-compat)
  95. (require 'org-faces)
  96. (require 'org-list)
  97. (require 'org-src)
  98. (require 'org-footnote)
  99. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "C" C)
  141. (const :tag "R" R)
  142. (const :tag "Asymptote" asymptote)
  143. (const :tag "Clojure" clojure)
  144. (const :tag "CSS" css)
  145. (const :tag "Ditaa" ditaa)
  146. (const :tag "Dot" dot)
  147. (const :tag "Emacs Lisp" emacs-lisp)
  148. (const :tag "Gnuplot" gnuplot)
  149. (const :tag "Haskell" haskell)
  150. (const :tag "Latex" latex)
  151. (const :tag "Ledger" ledger)
  152. (const :tag "Matlab" matlab)
  153. (const :tag "Mscgen" mscgen)
  154. (const :tag "Ocaml" ocaml)
  155. (const :tag "Octave" octave)
  156. (const :tag "Perl" perl)
  157. (const :tag "Python" python)
  158. (const :tag "Ruby" ruby)
  159. (const :tag "Sass" sass)
  160. (const :tag "Screen" screen)
  161. (const :tag "Shell Script" sh)
  162. (const :tag "Sql" sql)
  163. (const :tag "Sqlite" sqlite))
  164. :value-type (boolean :tag "Activate" :value t)))
  165. ;;;; Customization variables
  166. (defcustom org-clone-delete-id nil
  167. "Remove ID property of clones of a subtree.
  168. When non-nil, clones of a subtree don't inherit the ID property.
  169. Otherwise they inherit the ID property with a new unique
  170. identifier."
  171. :type 'boolean
  172. :group 'org-id)
  173. ;;; Version
  174. (defconst org-version "7.01trans"
  175. "The version number of the file org.el.")
  176. (defun org-version (&optional here)
  177. "Show the org-mode version in the echo area.
  178. With prefix arg HERE, insert it at point."
  179. (interactive "P")
  180. (let* ((origin default-directory)
  181. (version org-version)
  182. (git-version)
  183. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  184. (when (and (file-exists-p (expand-file-name ".git" dir))
  185. (executable-find "git"))
  186. (unwind-protect
  187. (progn
  188. (cd dir)
  189. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  190. (with-current-buffer "*Shell Command Output*"
  191. (goto-char (point-min))
  192. (setq git-version (buffer-substring (point) (point-at-eol))))
  193. (subst-char-in-string ?- ?. git-version t)
  194. (when (string-match "\\S-"
  195. (shell-command-to-string
  196. "git diff-index --name-only HEAD --"))
  197. (setq git-version (concat git-version ".dirty")))
  198. (setq version (concat version " (" git-version ")"))))
  199. (cd origin)))
  200. (setq version (format "Org-mode version %s" version))
  201. (if here (insert version))
  202. (message version)))
  203. ;;; Compatibility constants
  204. ;;; The custom variables
  205. (defgroup org nil
  206. "Outline-based notes management and organizer."
  207. :tag "Org"
  208. :group 'outlines
  209. :group 'calendar)
  210. (defcustom org-mode-hook nil
  211. "Mode hook for Org-mode, run after the mode was turned on."
  212. :group 'org
  213. :type 'hook)
  214. (defcustom org-load-hook nil
  215. "Hook that is run after org.el has been loaded."
  216. :group 'org
  217. :type 'hook)
  218. (defvar org-modules) ; defined below
  219. (defvar org-modules-loaded nil
  220. "Have the modules been loaded already?")
  221. (defun org-load-modules-maybe (&optional force)
  222. "Load all extensions listed in `org-modules'."
  223. (when (or force (not org-modules-loaded))
  224. (mapc (lambda (ext)
  225. (condition-case nil (require ext)
  226. (error (message "Problems while trying to load feature `%s'" ext))))
  227. org-modules)
  228. (setq org-modules-loaded t)))
  229. (defun org-set-modules (var value)
  230. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  231. (set var value)
  232. (when (featurep 'org)
  233. (org-load-modules-maybe 'force)))
  234. (when (org-bound-and-true-p org-modules)
  235. (let ((a (member 'org-infojs org-modules)))
  236. (and a (setcar a 'org-jsinfo))))
  237. (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)
  238. "Modules that should always be loaded together with org.el.
  239. If a description starts with <C>, the file is not part of Emacs
  240. and loading it will require that you have downloaded and properly installed
  241. the org-mode distribution.
  242. You can also use this system to load external packages (i.e. neither Org
  243. core modules, nor modules from the CONTRIB directory). Just add symbols
  244. to the end of the list. If the package is called org-xyz.el, then you need
  245. to add the symbol `xyz', and the package must have a call to
  246. (provide 'org-xyz)"
  247. :group 'org
  248. :set 'org-set-modules
  249. :type
  250. '(set :greedy t
  251. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  252. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  253. (const :tag " crypt: Encryption of subtrees" org-crypt)
  254. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  255. (const :tag " docview: Links to doc-view buffers" org-docview)
  256. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  257. (const :tag " id: Global IDs for identifying entries" org-id)
  258. (const :tag " info: Links to Info nodes" org-info)
  259. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  260. (const :tag " habit: Track your consistency with habits" org-habit)
  261. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  262. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  263. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  264. (const :tag " mew Links to Mew folders/messages" org-mew)
  265. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  266. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  267. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  268. (const :tag " vm: Links to VM folders/messages" org-vm)
  269. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  270. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  271. (const :tag " mouse: Additional mouse support" org-mouse)
  272. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  273. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  274. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  275. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  276. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  277. (const :tag "C collector: Collect properties into tables" org-collector)
  278. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  279. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  280. (const :tag "C eval: Include command output as text" org-eval)
  281. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  282. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  283. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  284. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  285. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  286. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  287. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  288. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  289. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  290. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  291. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  292. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  293. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  294. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  295. (const :tag "C registry: A registry for Org-mode links" org-registry)
  296. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  297. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  298. (const :tag "C secretary: Team management with org-mode" org-secretary)
  299. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  300. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  301. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  302. (const :tag "C track: Keep up with Org-mode development" org-track)
  303. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  304. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  305. (defcustom org-support-shift-select nil
  306. "Non-nil means make shift-cursor commands select text when possible.
  307. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  308. selecting a region, or enlarge regions started in this way.
  309. In Org-mode, in special contexts, these same keys are used for other
  310. purposes, important enough to compete with shift selection. Org tries
  311. to balance these needs by supporting `shift-select-mode' outside these
  312. special contexts, under control of this variable.
  313. The default of this variable is nil, to avoid confusing behavior. Shifted
  314. cursor keys will then execute Org commands in the following contexts:
  315. - on a headline, changing TODO state (left/right) and priority (up/down)
  316. - on a time stamp, changing the time
  317. - in a plain list item, changing the bullet type
  318. - in a property definition line, switching between allowed values
  319. - in the BEGIN line of a clock table (changing the time block).
  320. Outside these contexts, the commands will throw an error.
  321. When this variable is t and the cursor is not in a special context,
  322. Org-mode will support shift-selection for making and enlarging regions.
  323. To make this more effective, the bullet cycling will no longer happen
  324. anywhere in an item line, but only if the cursor is exactly on the bullet.
  325. If you set this variable to the symbol `always', then the keys
  326. will not be special in headlines, property lines, and item lines, to make
  327. shift selection work there as well. If this is what you want, you can
  328. use the following alternative commands: `C-c C-t' and `C-c ,' to
  329. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  330. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  331. edited by hand or in column view.
  332. However, when the cursor is on a timestamp, shift-cursor commands
  333. will still edit the time stamp - this is just too good to give up.
  334. XEmacs user should have this variable set to nil, because shift-select-mode
  335. is Emacs 23 only."
  336. :group 'org
  337. :type '(choice
  338. (const :tag "Never" nil)
  339. (const :tag "When outside special context" t)
  340. (const :tag "Everywhere except timestamps" always)))
  341. (defgroup org-startup nil
  342. "Options concerning startup of Org-mode."
  343. :tag "Org Startup"
  344. :group 'org)
  345. (defcustom org-startup-folded t
  346. "Non-nil means entering Org-mode will switch to OVERVIEW.
  347. This can also be configured on a per-file basis by adding one of
  348. the following lines anywhere in the buffer:
  349. #+STARTUP: fold (or `overview', this is equivalent)
  350. #+STARTUP: nofold (or `showall', this is equivalent)
  351. #+STARTUP: content
  352. #+STARTUP: showeverything"
  353. :group 'org-startup
  354. :type '(choice
  355. (const :tag "nofold: show all" nil)
  356. (const :tag "fold: overview" t)
  357. (const :tag "content: all headlines" content)
  358. (const :tag "show everything, even drawers" showeverything)))
  359. (defcustom org-startup-truncated t
  360. "Non-nil means entering Org-mode will set `truncate-lines'.
  361. This is useful since some lines containing links can be very long and
  362. uninteresting. Also tables look terrible when wrapped."
  363. :group 'org-startup
  364. :type 'boolean)
  365. (defcustom org-startup-indented nil
  366. "Non-nil means turn on `org-indent-mode' on startup.
  367. This can also be configured on a per-file basis by adding one of
  368. the following lines anywhere in the buffer:
  369. #+STARTUP: indent
  370. #+STARTUP: noindent"
  371. :group 'org-structure
  372. :type '(choice
  373. (const :tag "Not" nil)
  374. (const :tag "Globally (slow on startup in large files)" t)))
  375. (defcustom org-use-sub-superscripts t
  376. "Non-nil means interpret \"_\" and \"^\" for export.
  377. When this option is turned on, you can use TeX-like syntax for sub- and
  378. superscripts. Several characters after \"_\" or \"^\" will be
  379. considered as a single item - so grouping with {} is normally not
  380. needed. For example, the following things will be parsed as single
  381. sub- or superscripts.
  382. 10^24 or 10^tau several digits will be considered 1 item.
  383. 10^-12 or 10^-tau a leading sign with digits or a word
  384. x^2-y^3 will be read as x^2 - y^3, because items are
  385. terminated by almost any nonword/nondigit char.
  386. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  387. Still, ambiguity is possible - so when in doubt use {} to enclose the
  388. sub/superscript. If you set this variable to the symbol `{}',
  389. the braces are *required* in order to trigger interpretations as
  390. sub/superscript. This can be helpful in documents that need \"_\"
  391. frequently in plain text.
  392. Not all export backends support this, but HTML does.
  393. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  394. :group 'org-startup
  395. :group 'org-export-translation
  396. :type '(choice
  397. (const :tag "Always interpret" t)
  398. (const :tag "Only with braces" {})
  399. (const :tag "Never interpret" nil)))
  400. (if (fboundp 'defvaralias)
  401. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  402. (defcustom org-startup-with-beamer-mode nil
  403. "Non-nil means turn on `org-beamer-mode' on startup.
  404. This can also be configured on a per-file basis by adding one of
  405. the following lines anywhere in the buffer:
  406. #+STARTUP: beamer"
  407. :group 'org-startup
  408. :type 'boolean)
  409. (defcustom org-startup-align-all-tables nil
  410. "Non-nil means align all tables when visiting a file.
  411. This is useful when the column width in tables is forced with <N> cookies
  412. in table fields. Such tables will look correct only after the first re-align.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: align
  416. #+STARTUP: noalign"
  417. :group 'org-startup
  418. :type 'boolean)
  419. (defcustom org-insert-mode-line-in-empty-file nil
  420. "Non-nil means insert the first line setting Org-mode in empty files.
  421. When the function `org-mode' is called interactively in an empty file, this
  422. normally means that the file name does not automatically trigger Org-mode.
  423. To ensure that the file will always be in Org-mode in the future, a
  424. line enforcing Org-mode will be inserted into the buffer, if this option
  425. has been set."
  426. :group 'org-startup
  427. :type 'boolean)
  428. (defcustom org-replace-disputed-keys nil
  429. "Non-nil means use alternative key bindings for some keys.
  430. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  431. These keys are also used by other packages like shift-selection-mode'
  432. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  433. If you want to use Org-mode together with one of these other modes,
  434. or more generally if you would like to move some Org-mode commands to
  435. other keys, set this variable and configure the keys with the variable
  436. `org-disputed-keys'.
  437. This option is only relevant at load-time of Org-mode, and must be set
  438. *before* org.el is loaded. Changing it requires a restart of Emacs to
  439. become effective."
  440. :group 'org-startup
  441. :type 'boolean)
  442. (defcustom org-use-extra-keys nil
  443. "Non-nil means use extra key sequence definitions for certain commands.
  444. This happens automatically if you run XEmacs or if `window-system'
  445. is nil. This variable lets you do the same manually. You must
  446. set it before loading org.
  447. Example: on Carbon Emacs 22 running graphically, with an external
  448. keyboard on a Powerbook, the default way of setting M-left might
  449. not work for either Alt or ESC. Setting this variable will make
  450. it work for ESC."
  451. :group 'org-startup
  452. :type 'boolean)
  453. (if (fboundp 'defvaralias)
  454. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  455. (defcustom org-disputed-keys
  456. '(([(shift up)] . [(meta p)])
  457. ([(shift down)] . [(meta n)])
  458. ([(shift left)] . [(meta -)])
  459. ([(shift right)] . [(meta +)])
  460. ([(control shift right)] . [(meta shift +)])
  461. ([(control shift left)] . [(meta shift -)]))
  462. "Keys for which Org-mode and other modes compete.
  463. This is an alist, cars are the default keys, second element specifies
  464. the alternative to use when `org-replace-disputed-keys' is t.
  465. Keys can be specified in any syntax supported by `define-key'.
  466. The value of this option takes effect only at Org-mode's startup,
  467. therefore you'll have to restart Emacs to apply it after changing."
  468. :group 'org-startup
  469. :type 'alist)
  470. (defun org-key (key)
  471. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  472. Or return the original if not disputed.
  473. Also apply the translations defined in `org-xemacs-key-equivalents'."
  474. (when org-replace-disputed-keys
  475. (let* ((nkey (key-description key))
  476. (x (org-find-if (lambda (x)
  477. (equal (key-description (car x)) nkey))
  478. org-disputed-keys)))
  479. (setq key (if x (cdr x) key))))
  480. (when (featurep 'xemacs)
  481. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  482. key)
  483. (defun org-find-if (predicate seq)
  484. (catch 'exit
  485. (while seq
  486. (if (funcall predicate (car seq))
  487. (throw 'exit (car seq))
  488. (pop seq)))))
  489. (defun org-defkey (keymap key def)
  490. "Define a key, possibly translated, as returned by `org-key'."
  491. (define-key keymap (org-key key) def))
  492. (defcustom org-ellipsis nil
  493. "The ellipsis to use in the Org-mode outline.
  494. When nil, just use the standard three dots. When a string, use that instead,
  495. When a face, use the standard 3 dots, but with the specified face.
  496. The change affects only Org-mode (which will then use its own display table).
  497. Changing this requires executing `M-x org-mode' in a buffer to become
  498. effective."
  499. :group 'org-startup
  500. :type '(choice (const :tag "Default" nil)
  501. (face :tag "Face" :value org-warning)
  502. (string :tag "String" :value "...#")))
  503. (defvar org-display-table nil
  504. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  505. (defgroup org-keywords nil
  506. "Keywords in Org-mode."
  507. :tag "Org Keywords"
  508. :group 'org)
  509. (defcustom org-deadline-string "DEADLINE:"
  510. "String to mark deadline entries.
  511. A deadline is this string, followed by a time stamp. Should be a word,
  512. terminated by a colon. You can insert a schedule keyword and
  513. a timestamp with \\[org-deadline].
  514. Changes become only effective after restarting Emacs."
  515. :group 'org-keywords
  516. :type 'string)
  517. (defcustom org-scheduled-string "SCHEDULED:"
  518. "String to mark scheduled TODO entries.
  519. A schedule is this string, followed by a time stamp. Should be a word,
  520. terminated by a colon. You can insert a schedule keyword and
  521. a timestamp with \\[org-schedule].
  522. Changes become only effective after restarting Emacs."
  523. :group 'org-keywords
  524. :type 'string)
  525. (defcustom org-closed-string "CLOSED:"
  526. "String used as the prefix for timestamps logging closing a TODO entry."
  527. :group 'org-keywords
  528. :type 'string)
  529. (defcustom org-clock-string "CLOCK:"
  530. "String used as prefix for timestamps clocking work hours on an item."
  531. :group 'org-keywords
  532. :type 'string)
  533. (defcustom org-comment-string "COMMENT"
  534. "Entries starting with this keyword will never be exported.
  535. An entry can be toggled between COMMENT and normal with
  536. \\[org-toggle-comment].
  537. Changes become only effective after restarting Emacs."
  538. :group 'org-keywords
  539. :type 'string)
  540. (defcustom org-quote-string "QUOTE"
  541. "Entries starting with this keyword will be exported in fixed-width font.
  542. Quoting applies only to the text in the entry following the headline, and does
  543. not extend beyond the next headline, even if that is lower level.
  544. An entry can be toggled between QUOTE and normal with
  545. \\[org-toggle-fixed-width-section]."
  546. :group 'org-keywords
  547. :type 'string)
  548. (defconst org-repeat-re
  549. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  550. "Regular expression for specifying repeated events.
  551. After a match, group 1 contains the repeat expression.")
  552. (defgroup org-structure nil
  553. "Options concerning the general structure of Org-mode files."
  554. :tag "Org Structure"
  555. :group 'org)
  556. (defgroup org-reveal-location nil
  557. "Options about how to make context of a location visible."
  558. :tag "Org Reveal Location"
  559. :group 'org-structure)
  560. (defconst org-context-choice
  561. '(choice
  562. (const :tag "Always" t)
  563. (const :tag "Never" nil)
  564. (repeat :greedy t :tag "Individual contexts"
  565. (cons
  566. (choice :tag "Context"
  567. (const agenda)
  568. (const org-goto)
  569. (const occur-tree)
  570. (const tags-tree)
  571. (const link-search)
  572. (const mark-goto)
  573. (const bookmark-jump)
  574. (const isearch)
  575. (const default))
  576. (boolean))))
  577. "Contexts for the reveal options.")
  578. (defcustom org-show-hierarchy-above '((default . t))
  579. "Non-nil means show full hierarchy when revealing a location.
  580. Org-mode often shows locations in an org-mode file which might have
  581. been invisible before. When this is set, the hierarchy of headings
  582. above the exposed location is shown.
  583. Turning this off for example for sparse trees makes them very compact.
  584. Instead of t, this can also be an alist specifying this option for different
  585. contexts. Valid contexts are
  586. agenda when exposing an entry from the agenda
  587. org-goto when using the command `org-goto' on key C-c C-j
  588. occur-tree when using the command `org-occur' on key C-c /
  589. tags-tree when constructing a sparse tree based on tags matches
  590. link-search when exposing search matches associated with a link
  591. mark-goto when exposing the jump goal of a mark
  592. bookmark-jump when exposing a bookmark location
  593. isearch when exiting from an incremental search
  594. default default for all contexts not set explicitly"
  595. :group 'org-reveal-location
  596. :type org-context-choice)
  597. (defcustom org-show-following-heading '((default . nil))
  598. "Non-nil means show following heading when revealing a location.
  599. Org-mode often shows locations in an org-mode file which might have
  600. been invisible before. When this is set, the heading following the
  601. match is shown.
  602. Turning this off for example for sparse trees makes them very compact,
  603. but makes it harder to edit the location of the match. In such a case,
  604. use the command \\[org-reveal] to show more context.
  605. Instead of t, this can also be an alist specifying this option for different
  606. contexts. See `org-show-hierarchy-above' for valid contexts."
  607. :group 'org-reveal-location
  608. :type org-context-choice)
  609. (defcustom org-show-siblings '((default . nil) (isearch t))
  610. "Non-nil means show all sibling heading when revealing a location.
  611. Org-mode often shows locations in an org-mode file which might have
  612. been invisible before. When this is set, the sibling of the current entry
  613. heading are all made visible. If `org-show-hierarchy-above' is t,
  614. the same happens on each level of the hierarchy above the current entry.
  615. By default this is on for the isearch context, off for all other contexts.
  616. Turning this off for example for sparse trees makes them very compact,
  617. but makes it harder to edit the location of the match. In such a case,
  618. use the command \\[org-reveal] to show more context.
  619. Instead of t, this can also be an alist specifying this option for different
  620. contexts. See `org-show-hierarchy-above' for valid contexts."
  621. :group 'org-reveal-location
  622. :type org-context-choice)
  623. (defcustom org-show-entry-below '((default . nil))
  624. "Non-nil means show the entry below a headline when revealing a location.
  625. Org-mode often shows locations in an org-mode file which might have
  626. been invisible before. When this is set, the text below the headline that is
  627. exposed is also shown.
  628. By default this is off for all contexts.
  629. Instead of t, this can also be an alist specifying this option for different
  630. contexts. See `org-show-hierarchy-above' for valid contexts."
  631. :group 'org-reveal-location
  632. :type org-context-choice)
  633. (defcustom org-indirect-buffer-display 'other-window
  634. "How should indirect tree buffers be displayed?
  635. This applies to indirect buffers created with the commands
  636. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  637. Valid values are:
  638. current-window Display in the current window
  639. other-window Just display in another window.
  640. dedicated-frame Create one new frame, and re-use it each time.
  641. new-frame Make a new frame each time. Note that in this case
  642. previously-made indirect buffers are kept, and you need to
  643. kill these buffers yourself."
  644. :group 'org-structure
  645. :group 'org-agenda-windows
  646. :type '(choice
  647. (const :tag "In current window" current-window)
  648. (const :tag "In current frame, other window" other-window)
  649. (const :tag "Each time a new frame" new-frame)
  650. (const :tag "One dedicated frame" dedicated-frame)))
  651. (defcustom org-use-speed-commands nil
  652. "Non-nil means activate single letter commands at beginning of a headline.
  653. This may also be a function to test for appropriate locations where speed
  654. commands should be active."
  655. :group 'org-structure
  656. :type '(choice
  657. (const :tag "Never" nil)
  658. (const :tag "At beginning of headline stars" t)
  659. (function)))
  660. (defcustom org-speed-commands-user nil
  661. "Alist of additional speed commands.
  662. This list will be checked before `org-speed-commands-default'
  663. when the variable `org-use-speed-commands' is non-nil
  664. and when the cursor is at the beginning of a headline.
  665. The car if each entry is a string with a single letter, which must
  666. be assigned to `self-insert-command' in the global map.
  667. The cdr is either a command to be called interactively, a function
  668. to be called, or a form to be evaluated.
  669. An entry that is just a list with a single string will be interpreted
  670. as a descriptive headline that will be added when listing the speed
  671. commands in the Help buffer using the `?' speed command."
  672. :group 'org-structure
  673. :type '(repeat :value ("k" . ignore)
  674. (choice :value ("k" . ignore)
  675. (list :tag "Descriptive Headline" (string :tag "Headline"))
  676. (cons :tag "Letter and Command"
  677. (string :tag "Command letter")
  678. (choice
  679. (function)
  680. (sexp))))))
  681. (defgroup org-cycle nil
  682. "Options concerning visibility cycling in Org-mode."
  683. :tag "Org Cycle"
  684. :group 'org-structure)
  685. (defcustom org-cycle-skip-children-state-if-no-children t
  686. "Non-nil means skip CHILDREN state in entries that don't have any."
  687. :group 'org-cycle
  688. :type 'boolean)
  689. (defcustom org-cycle-max-level nil
  690. "Maximum level which should still be subject to visibility cycling.
  691. Levels higher than this will, for cycling, be treated as text, not a headline.
  692. When `org-odd-levels-only' is set, a value of N in this variable actually
  693. means 2N-1 stars as the limiting headline.
  694. When nil, cycle all levels.
  695. Note that the limiting level of cycling is also influenced by
  696. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  697. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  698. than its value."
  699. :group 'org-cycle
  700. :type '(choice
  701. (const :tag "No limit" nil)
  702. (integer :tag "Maximum level")))
  703. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  704. "Names of drawers. Drawers are not opened by cycling on the headline above.
  705. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  706. this:
  707. :DRAWERNAME:
  708. .....
  709. :END:
  710. The drawer \"PROPERTIES\" is special for capturing properties through
  711. the property API.
  712. Drawers can be defined on the per-file basis with a line like:
  713. #+DRAWERS: HIDDEN STATE PROPERTIES"
  714. :group 'org-structure
  715. :group 'org-cycle
  716. :type '(repeat (string :tag "Drawer Name")))
  717. (defcustom org-hide-block-startup nil
  718. "Non-nil means entering Org-mode will fold all blocks.
  719. This can also be set in on a per-file basis with
  720. #+STARTUP: hideblocks
  721. #+STARTUP: showblocks"
  722. :group 'org-startup
  723. :group 'org-cycle
  724. :type 'boolean)
  725. (defcustom org-cycle-global-at-bob nil
  726. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  727. This makes it possible to do global cycling without having to use S-TAB or
  728. \\[universal-argument] TAB. For this special case to work, the first line \
  729. of the buffer
  730. must not be a headline - it may be empty or some other text. When used in
  731. this way, `org-cycle-hook' is disables temporarily, to make sure the
  732. cursor stays at the beginning of the buffer.
  733. When this option is nil, don't do anything special at the beginning
  734. of the buffer."
  735. :group 'org-cycle
  736. :type 'boolean)
  737. (defcustom org-cycle-level-after-item/entry-creation t
  738. "Non-nil means cycle entry level or item indentation in new empty entries.
  739. When the cursor is at the end of an empty headline, i.e with only stars
  740. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  741. and then all possible ancestor states, before returning to the original state.
  742. This makes data entry extremely fast: M-RET to create a new headline,
  743. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  744. When the cursor is at the end of an empty plain list item, one TAB will
  745. make it a subitem, two or more tabs will back up to make this an item
  746. higher up in the item hierarchy."
  747. :group 'org-cycle
  748. :type 'boolean)
  749. (defcustom org-cycle-emulate-tab t
  750. "Where should `org-cycle' emulate TAB.
  751. nil Never
  752. white Only in completely white lines
  753. whitestart Only at the beginning of lines, before the first non-white char
  754. t Everywhere except in headlines
  755. exc-hl-bol Everywhere except at the start of a headline
  756. If TAB is used in a place where it does not emulate TAB, the current subtree
  757. visibility is cycled."
  758. :group 'org-cycle
  759. :type '(choice (const :tag "Never" nil)
  760. (const :tag "Only in completely white lines" white)
  761. (const :tag "Before first char in a line" whitestart)
  762. (const :tag "Everywhere except in headlines" t)
  763. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  764. ))
  765. (defcustom org-cycle-separator-lines 2
  766. "Number of empty lines needed to keep an empty line between collapsed trees.
  767. If you leave an empty line between the end of a subtree and the following
  768. headline, this empty line is hidden when the subtree is folded.
  769. Org-mode will leave (exactly) one empty line visible if the number of
  770. empty lines is equal or larger to the number given in this variable.
  771. So the default 2 means at least 2 empty lines after the end of a subtree
  772. are needed to produce free space between a collapsed subtree and the
  773. following headline.
  774. If the number is negative, and the number of empty lines is at least -N,
  775. all empty lines are shown.
  776. Special case: when 0, never leave empty lines in collapsed view."
  777. :group 'org-cycle
  778. :type 'integer)
  779. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  780. (defcustom org-pre-cycle-hook nil
  781. "Hook that is run before visibility cycling is happening.
  782. The function(s) in this hook must accept a single argument which indicates
  783. the new state that will be set right after running this hook. The
  784. argument is a symbol. Before a global state change, it can have the values
  785. `overview', `content', or `all'. Before a local state change, it can have
  786. the values `folded', `children', or `subtree'."
  787. :group 'org-cycle
  788. :type 'hook)
  789. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  790. org-cycle-hide-drawers
  791. org-cycle-show-empty-lines
  792. org-optimize-window-after-visibility-change)
  793. "Hook that is run after `org-cycle' has changed the buffer visibility.
  794. The function(s) in this hook must accept a single argument which indicates
  795. the new state that was set by the most recent `org-cycle' command. The
  796. argument is a symbol. After a global state change, it can have the values
  797. `overview', `content', or `all'. After a local state change, it can have
  798. the values `folded', `children', or `subtree'."
  799. :group 'org-cycle
  800. :type 'hook)
  801. (defgroup org-edit-structure nil
  802. "Options concerning structure editing in Org-mode."
  803. :tag "Org Edit Structure"
  804. :group 'org-structure)
  805. (defcustom org-odd-levels-only nil
  806. "Non-nil means skip even levels and only use odd levels for the outline.
  807. This has the effect that two stars are being added/taken away in
  808. promotion/demotion commands. It also influences how levels are
  809. handled by the exporters.
  810. Changing it requires restart of `font-lock-mode' to become effective
  811. for fontification also in regions already fontified.
  812. You may also set this on a per-file basis by adding one of the following
  813. lines to the buffer:
  814. #+STARTUP: odd
  815. #+STARTUP: oddeven"
  816. :group 'org-edit-structure
  817. :group 'org-appearance
  818. :type 'boolean)
  819. (defcustom org-adapt-indentation t
  820. "Non-nil means adapt indentation to outline node level.
  821. When this variable is set, Org assumes that you write outlines by
  822. indenting text in each node to align with the headline (after the stars).
  823. The following issues are influenced by this variable:
  824. - When this is set and the *entire* text in an entry is indented, the
  825. indentation is increased by one space in a demotion command, and
  826. decreased by one in a promotion command. If any line in the entry
  827. body starts with text at column 0, indentation is not changed at all.
  828. - Property drawers and planning information is inserted indented when
  829. this variable s set. When nil, they will not be indented.
  830. - TAB indents a line relative to context. The lines below a headline
  831. will be indented when this variable is set.
  832. Note that this is all about true indentation, by adding and removing
  833. space characters. See also `org-indent.el' which does level-dependent
  834. indentation in a virtual way, i.e. at display time in Emacs."
  835. :group 'org-edit-structure
  836. :type 'boolean)
  837. (defcustom org-special-ctrl-a/e nil
  838. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  839. When t, `C-a' will bring back the cursor to the beginning of the
  840. headline text, i.e. after the stars and after a possible TODO keyword.
  841. In an item, this will be the position after the bullet.
  842. When the cursor is already at that position, another `C-a' will bring
  843. it to the beginning of the line.
  844. `C-e' will jump to the end of the headline, ignoring the presence of tags
  845. in the headline. A second `C-e' will then jump to the true end of the
  846. line, after any tags. This also means that, when this variable is
  847. non-nil, `C-e' also will never jump beyond the end of the heading of a
  848. folded section, i.e. not after the ellipses.
  849. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  850. going to the true line boundary first. Only a directly following, identical
  851. keypress will bring the cursor to the special positions.
  852. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  853. set separately."
  854. :group 'org-edit-structure
  855. :type '(choice
  856. (const :tag "off" nil)
  857. (const :tag "on: after stars/bullet and before tags first" t)
  858. (const :tag "reversed: true line boundary first" reversed)
  859. (cons :tag "Set C-a and C-e separately"
  860. (choice :tag "Special C-a"
  861. (const :tag "off" nil)
  862. (const :tag "on: after stars/bullet first" t)
  863. (const :tag "reversed: before stars/bullet first" reversed))
  864. (choice :tag "Special C-e"
  865. (const :tag "off" nil)
  866. (const :tag "on: before tags first" t)
  867. (const :tag "reversed: after tags first" reversed)))))
  868. (if (fboundp 'defvaralias)
  869. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  870. (defcustom org-special-ctrl-k nil
  871. "Non-nil means `C-k' will behave specially in headlines.
  872. When nil, `C-k' will call the default `kill-line' command.
  873. When t, the following will happen while the cursor is in the headline:
  874. - When the cursor is at the beginning of a headline, kill the entire
  875. line and possible the folded subtree below the line.
  876. - When in the middle of the headline text, kill the headline up to the tags.
  877. - When after the headline text, kill the tags."
  878. :group 'org-edit-structure
  879. :type 'boolean)
  880. (defcustom org-ctrl-k-protect-subtree nil
  881. "Non-nil means, do not delete a hidden subtree with C-k.
  882. When set to the symbol `error', simply throw an error when C-k is
  883. used to kill (part-of) a headline that has hidden text behind it.
  884. Any other non-nil value will result in a query to the user, if it is
  885. OK to kill that hidden subtree. When nil, kill without remorse."
  886. :group 'org-edit-structure
  887. :type '(choice
  888. (const :tag "Do not protect hidden subtrees" nil)
  889. (const :tag "Protect hidden subtrees with a security query" t)
  890. (const :tag "Never kill a hidden subtree with C-k" error)))
  891. (defcustom org-yank-folded-subtrees t
  892. "Non-nil means when yanking subtrees, fold them.
  893. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  894. it starts with a heading and all other headings in it are either children
  895. or siblings, then fold all the subtrees. However, do this only if no
  896. text after the yank would be swallowed into a folded tree by this action."
  897. :group 'org-edit-structure
  898. :type 'boolean)
  899. (defcustom org-yank-adjusted-subtrees nil
  900. "Non-nil means when yanking subtrees, adjust the level.
  901. With this setting, `org-paste-subtree' is used to insert the subtree, see
  902. this function for details."
  903. :group 'org-edit-structure
  904. :type 'boolean)
  905. (defcustom org-M-RET-may-split-line '((default . t))
  906. "Non-nil means M-RET will split the line at the cursor position.
  907. When nil, it will go to the end of the line before making a
  908. new line.
  909. You may also set this option in a different way for different
  910. contexts. Valid contexts are:
  911. headline when creating a new headline
  912. item when creating a new item
  913. table in a table field
  914. default the value to be used for all contexts not explicitly
  915. customized"
  916. :group 'org-structure
  917. :group 'org-table
  918. :type '(choice
  919. (const :tag "Always" t)
  920. (const :tag "Never" nil)
  921. (repeat :greedy t :tag "Individual contexts"
  922. (cons
  923. (choice :tag "Context"
  924. (const headline)
  925. (const item)
  926. (const table)
  927. (const default))
  928. (boolean)))))
  929. (defcustom org-insert-heading-respect-content nil
  930. "Non-nil means insert new headings after the current subtree.
  931. When nil, the new heading is created directly after the current line.
  932. The commands \\[org-insert-heading-respect-content] and
  933. \\[org-insert-todo-heading-respect-content] turn this variable on
  934. for the duration of the command."
  935. :group 'org-structure
  936. :type 'boolean)
  937. (defcustom org-blank-before-new-entry '((heading . auto)
  938. (plain-list-item . auto))
  939. "Should `org-insert-heading' leave a blank line before new heading/item?
  940. The value is an alist, with `heading' and `plain-list-item' as car,
  941. and a boolean flag as cdr. For plain lists, if the variable
  942. `org-empty-line-terminates-plain-lists' is set, the setting here
  943. is ignored and no empty line is inserted, to keep the list in tact."
  944. :group 'org-edit-structure
  945. :type '(list
  946. (cons (const heading)
  947. (choice (const :tag "Never" nil)
  948. (const :tag "Always" t)
  949. (const :tag "Auto" auto)))
  950. (cons (const plain-list-item)
  951. (choice (const :tag "Never" nil)
  952. (const :tag "Always" t)
  953. (const :tag "Auto" auto)))))
  954. (defcustom org-insert-heading-hook nil
  955. "Hook being run after inserting a new heading."
  956. :group 'org-edit-structure
  957. :type 'hook)
  958. (defcustom org-enable-fixed-width-editor t
  959. "Non-nil means lines starting with \":\" are treated as fixed-width.
  960. This currently only means they are never auto-wrapped.
  961. When nil, such lines will be treated like ordinary lines.
  962. See also the QUOTE keyword."
  963. :group 'org-edit-structure
  964. :type 'boolean)
  965. (defcustom org-goto-auto-isearch t
  966. "Non-nil means typing characters in `org-goto' starts incremental search."
  967. :group 'org-edit-structure
  968. :type 'boolean)
  969. (defgroup org-sparse-trees nil
  970. "Options concerning sparse trees in Org-mode."
  971. :tag "Org Sparse Trees"
  972. :group 'org-structure)
  973. (defcustom org-highlight-sparse-tree-matches t
  974. "Non-nil means highlight all matches that define a sparse tree.
  975. The highlights will automatically disappear the next time the buffer is
  976. changed by an edit command."
  977. :group 'org-sparse-trees
  978. :type 'boolean)
  979. (defcustom org-remove-highlights-with-change t
  980. "Non-nil means any change to the buffer will remove temporary highlights.
  981. Such highlights are created by `org-occur' and `org-clock-display'.
  982. When nil, `C-c C-c needs to be used to get rid of the highlights.
  983. The highlights created by `org-preview-latex-fragment' always need
  984. `C-c C-c' to be removed."
  985. :group 'org-sparse-trees
  986. :group 'org-time
  987. :type 'boolean)
  988. (defcustom org-occur-hook '(org-first-headline-recenter)
  989. "Hook that is run after `org-occur' has constructed a sparse tree.
  990. This can be used to recenter the window to show as much of the structure
  991. as possible."
  992. :group 'org-sparse-trees
  993. :type 'hook)
  994. (defgroup org-imenu-and-speedbar nil
  995. "Options concerning imenu and speedbar in Org-mode."
  996. :tag "Org Imenu and Speedbar"
  997. :group 'org-structure)
  998. (defcustom org-imenu-depth 2
  999. "The maximum level for Imenu access to Org-mode headlines.
  1000. This also applied for speedbar access."
  1001. :group 'org-imenu-and-speedbar
  1002. :type 'integer)
  1003. (defgroup org-table nil
  1004. "Options concerning tables in Org-mode."
  1005. :tag "Org Table"
  1006. :group 'org)
  1007. (defcustom org-enable-table-editor 'optimized
  1008. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1009. When nil, such lines will be treated like ordinary lines.
  1010. When equal to the symbol `optimized', the table editor will be optimized to
  1011. do the following:
  1012. - Automatic overwrite mode in front of whitespace in table fields.
  1013. This makes the structure of the table stay in tact as long as the edited
  1014. field does not exceed the column width.
  1015. - Minimize the number of realigns. Normally, the table is aligned each time
  1016. TAB or RET are pressed to move to another field. With optimization this
  1017. happens only if changes to a field might have changed the column width.
  1018. Optimization requires replacing the functions `self-insert-command',
  1019. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1020. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1021. very good at guessing when a re-align will be necessary, but you can always
  1022. force one with \\[org-ctrl-c-ctrl-c].
  1023. If you would like to use the optimized version in Org-mode, but the
  1024. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1025. This variable can be used to turn on and off the table editor during a session,
  1026. but in order to toggle optimization, a restart is required.
  1027. See also the variable `org-table-auto-blank-field'."
  1028. :group 'org-table
  1029. :type '(choice
  1030. (const :tag "off" nil)
  1031. (const :tag "on" t)
  1032. (const :tag "on, optimized" optimized)))
  1033. (defcustom org-self-insert-cluster-for-undo t
  1034. "Non-nil means cluster self-insert commands for undo when possible.
  1035. If this is set, then, like in the Emacs command loop, 20 consecutive
  1036. characters will be undone together.
  1037. This is configurable, because there is some impact on typing performance."
  1038. :group 'org-table
  1039. :type 'boolean)
  1040. (defcustom org-table-tab-recognizes-table.el t
  1041. "Non-nil means TAB will automatically notice a table.el table.
  1042. When it sees such a table, it moves point into it and - if necessary -
  1043. calls `table-recognize-table'."
  1044. :group 'org-table-editing
  1045. :type 'boolean)
  1046. (defgroup org-link nil
  1047. "Options concerning links in Org-mode."
  1048. :tag "Org Link"
  1049. :group 'org)
  1050. (defvar org-link-abbrev-alist-local nil
  1051. "Buffer-local version of `org-link-abbrev-alist', which see.
  1052. The value of this is taken from the #+LINK lines.")
  1053. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1054. (defcustom org-link-abbrev-alist nil
  1055. "Alist of link abbreviations.
  1056. The car of each element is a string, to be replaced at the start of a link.
  1057. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1058. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1059. [[linkkey:tag][description]]
  1060. The 'linkkey' must be a word word, starting with a letter, followed
  1061. by letters, numbers, '-' or '_'.
  1062. If REPLACE is a string, the tag will simply be appended to create the link.
  1063. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1064. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1065. be inserted at that point (see the function `url-hexify-string').
  1066. REPLACE may also be a function that will be called with the tag as the
  1067. only argument to create the link, which should be returned as a string.
  1068. See the manual for examples."
  1069. :group 'org-link
  1070. :type '(repeat
  1071. (cons
  1072. (string :tag "Protocol")
  1073. (choice
  1074. (string :tag "Format")
  1075. (function)))))
  1076. (defcustom org-descriptive-links t
  1077. "Non-nil means hide link part and only show description of bracket links.
  1078. Bracket links are like [[link][description]]. This variable sets the initial
  1079. state in new org-mode buffers. The setting can then be toggled on a
  1080. per-buffer basis from the Org->Hyperlinks menu."
  1081. :group 'org-link
  1082. :type 'boolean)
  1083. (defcustom org-link-file-path-type 'adaptive
  1084. "How the path name in file links should be stored.
  1085. Valid values are:
  1086. relative Relative to the current directory, i.e. the directory of the file
  1087. into which the link is being inserted.
  1088. absolute Absolute path, if possible with ~ for home directory.
  1089. noabbrev Absolute path, no abbreviation of home directory.
  1090. adaptive Use relative path for files in the current directory and sub-
  1091. directories of it. For other files, use an absolute path."
  1092. :group 'org-link
  1093. :type '(choice
  1094. (const relative)
  1095. (const absolute)
  1096. (const noabbrev)
  1097. (const adaptive)))
  1098. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1099. "Types of links that should be activated in Org-mode files.
  1100. This is a list of symbols, each leading to the activation of a certain link
  1101. type. In principle, it does not hurt to turn on most link types - there may
  1102. be a small gain when turning off unused link types. The types are:
  1103. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1104. angular Links in angular brackets that may contain whitespace like
  1105. <bbdb:Carsten Dominik>.
  1106. plain Plain links in normal text, no whitespace, like http://google.com.
  1107. radio Text that is matched by a radio target, see manual for details.
  1108. tag Tag settings in a headline (link to tag search).
  1109. date Time stamps (link to calendar).
  1110. footnote Footnote labels.
  1111. Changing this variable requires a restart of Emacs to become effective."
  1112. :group 'org-link
  1113. :type '(set :greedy t
  1114. (const :tag "Double bracket links (new style)" bracket)
  1115. (const :tag "Angular bracket links (old style)" angular)
  1116. (const :tag "Plain text links" plain)
  1117. (const :tag "Radio target matches" radio)
  1118. (const :tag "Tags" tag)
  1119. (const :tag "Timestamps" date)
  1120. (const :tag "Footnotes" footnote)))
  1121. (defcustom org-make-link-description-function nil
  1122. "Function to use to generate link descriptions from links.
  1123. If nil the link location will be used. This function must take
  1124. two parameters; the first is the link and the second the
  1125. description `org-insert-link' has generated, and should return the
  1126. description to use."
  1127. :group 'org-link
  1128. :type 'function)
  1129. (defgroup org-link-store nil
  1130. "Options concerning storing links in Org-mode."
  1131. :tag "Org Store Link"
  1132. :group 'org-link)
  1133. (defcustom org-email-link-description-format "Email %c: %.30s"
  1134. "Format of the description part of a link to an email or usenet message.
  1135. The following %-escapes will be replaced by corresponding information:
  1136. %F full \"From\" field
  1137. %f name, taken from \"From\" field, address if no name
  1138. %T full \"To\" field
  1139. %t first name in \"To\" field, address if no name
  1140. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1141. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1142. %s subject
  1143. %m message-id.
  1144. You may use normal field width specification between the % and the letter.
  1145. This is for example useful to limit the length of the subject.
  1146. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1147. :group 'org-link-store
  1148. :type 'string)
  1149. (defcustom org-from-is-user-regexp
  1150. (let (r1 r2)
  1151. (when (and user-mail-address (not (string= user-mail-address "")))
  1152. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1153. (when (and user-full-name (not (string= user-full-name "")))
  1154. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1155. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1156. "Regexp matched against the \"From:\" header of an email or usenet message.
  1157. It should match if the message is from the user him/herself."
  1158. :group 'org-link-store
  1159. :type 'regexp)
  1160. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1161. "Non-nil means storing a link to an Org file will use entry IDs.
  1162. Note that before this variable is even considered, org-id must be loaded,
  1163. so please customize `org-modules' and turn it on.
  1164. The variable can have the following values:
  1165. t Create an ID if needed to make a link to the current entry.
  1166. create-if-interactive
  1167. If `org-store-link' is called directly (interactively, as a user
  1168. command), do create an ID to support the link. But when doing the
  1169. job for remember, only use the ID if it already exists. The
  1170. purpose of this setting is to avoid proliferation of unwanted
  1171. IDs, just because you happen to be in an Org file when you
  1172. call `org-remember' that automatically and preemptively
  1173. creates a link. If you do want to get an ID link in a remember
  1174. template to an entry not having an ID, create it first by
  1175. explicitly creating a link to it, using `C-c C-l' first.
  1176. create-if-interactive-and-no-custom-id
  1177. Like create-if-interactive, but do not create an ID if there is
  1178. a CUSTOM_ID property defined in the entry. This is the default.
  1179. use-existing
  1180. Use existing ID, do not create one.
  1181. nil Never use an ID to make a link, instead link using a text search for
  1182. the headline text."
  1183. :group 'org-link-store
  1184. :type '(choice
  1185. (const :tag "Create ID to make link" t)
  1186. (const :tag "Create if storing link interactively"
  1187. create-if-interactive)
  1188. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1189. create-if-interactive-and-no-custom-id)
  1190. (const :tag "Only use existing" use-existing)
  1191. (const :tag "Do not use ID to create link" nil)))
  1192. (defcustom org-context-in-file-links t
  1193. "Non-nil means file links from `org-store-link' contain context.
  1194. A search string will be added to the file name with :: as separator and
  1195. used to find the context when the link is activated by the command
  1196. `org-open-at-point'.
  1197. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1198. negates this setting for the duration of the command."
  1199. :group 'org-link-store
  1200. :type 'boolean)
  1201. (defcustom org-keep-stored-link-after-insertion nil
  1202. "Non-nil means keep link in list for entire session.
  1203. The command `org-store-link' adds a link pointing to the current
  1204. location to an internal list. These links accumulate during a session.
  1205. The command `org-insert-link' can be used to insert links into any
  1206. Org-mode file (offering completion for all stored links). When this
  1207. option is nil, every link which has been inserted once using \\[org-insert-link]
  1208. will be removed from the list, to make completing the unused links
  1209. more efficient."
  1210. :group 'org-link-store
  1211. :type 'boolean)
  1212. (defgroup org-link-follow nil
  1213. "Options concerning following links in Org-mode."
  1214. :tag "Org Follow Link"
  1215. :group 'org-link)
  1216. (defcustom org-link-translation-function nil
  1217. "Function to translate links with different syntax to Org syntax.
  1218. This can be used to translate links created for example by the Planner
  1219. or emacs-wiki packages to Org syntax.
  1220. The function must accept two parameters, a TYPE containing the link
  1221. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1222. which is everything after the link protocol. It should return a cons
  1223. with possibly modified values of type and path.
  1224. Org contains a function for this, so if you set this variable to
  1225. `org-translate-link-from-planner', you should be able follow many
  1226. links created by planner."
  1227. :group 'org-link-follow
  1228. :type 'function)
  1229. (defcustom org-follow-link-hook nil
  1230. "Hook that is run after a link has been followed."
  1231. :group 'org-link-follow
  1232. :type 'hook)
  1233. (defcustom org-tab-follows-link nil
  1234. "Non-nil means on links TAB will follow the link.
  1235. Needs to be set before org.el is loaded.
  1236. This really should not be used, it does not make sense, and the
  1237. implementation is bad."
  1238. :group 'org-link-follow
  1239. :type 'boolean)
  1240. (defcustom org-return-follows-link nil
  1241. "Non-nil means on links RET will follow the link."
  1242. :group 'org-link-follow
  1243. :type 'boolean)
  1244. (defcustom org-mouse-1-follows-link
  1245. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1246. "Non-nil means mouse-1 on a link will follow the link.
  1247. A longer mouse click will still set point. Does not work on XEmacs.
  1248. Needs to be set before org.el is loaded."
  1249. :group 'org-link-follow
  1250. :type 'boolean)
  1251. (defcustom org-mark-ring-length 4
  1252. "Number of different positions to be recorded in the ring.
  1253. Changing this requires a restart of Emacs to work correctly."
  1254. :group 'org-link-follow
  1255. :type 'integer)
  1256. (defcustom org-link-frame-setup
  1257. '((vm . vm-visit-folder-other-frame)
  1258. (gnus . org-gnus-no-new-news)
  1259. (file . find-file-other-window)
  1260. (wl . wl-other-frame))
  1261. "Setup the frame configuration for following links.
  1262. When following a link with Emacs, it may often be useful to display
  1263. this link in another window or frame. This variable can be used to
  1264. set this up for the different types of links.
  1265. For VM, use any of
  1266. `vm-visit-folder'
  1267. `vm-visit-folder-other-frame'
  1268. For Gnus, use any of
  1269. `gnus'
  1270. `gnus-other-frame'
  1271. `org-gnus-no-new-news'
  1272. For FILE, use any of
  1273. `find-file'
  1274. `find-file-other-window'
  1275. `find-file-other-frame'
  1276. For Wanderlust use any of
  1277. `wl'
  1278. `wl-other-frame'
  1279. For the calendar, use the variable `calendar-setup'.
  1280. For BBDB, it is currently only possible to display the matches in
  1281. another window."
  1282. :group 'org-link-follow
  1283. :type '(list
  1284. (cons (const vm)
  1285. (choice
  1286. (const vm-visit-folder)
  1287. (const vm-visit-folder-other-window)
  1288. (const vm-visit-folder-other-frame)))
  1289. (cons (const gnus)
  1290. (choice
  1291. (const gnus)
  1292. (const gnus-other-frame)
  1293. (const org-gnus-no-new-news)))
  1294. (cons (const file)
  1295. (choice
  1296. (const find-file)
  1297. (const find-file-other-window)
  1298. (const find-file-other-frame)))
  1299. (cons (const wl)
  1300. (choice
  1301. (const wl)
  1302. (const wl-other-frame)))))
  1303. (defcustom org-display-internal-link-with-indirect-buffer nil
  1304. "Non-nil means use indirect buffer to display infile links.
  1305. Activating internal links (from one location in a file to another location
  1306. in the same file) normally just jumps to the location. When the link is
  1307. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1308. is displayed in
  1309. another window. When this option is set, the other window actually displays
  1310. an indirect buffer clone of the current buffer, to avoid any visibility
  1311. changes to the current buffer."
  1312. :group 'org-link-follow
  1313. :type 'boolean)
  1314. (defcustom org-open-non-existing-files nil
  1315. "Non-nil means `org-open-file' will open non-existing files.
  1316. When nil, an error will be generated.
  1317. This variable applies only to external applications because they
  1318. might choke on non-existing files. If the link is to a file that
  1319. will be opened in Emacs, the variable is ignored."
  1320. :group 'org-link-follow
  1321. :type 'boolean)
  1322. (defcustom org-open-directory-means-index-dot-org nil
  1323. "Non-nil means a link to a directory really means to index.org.
  1324. When nil, following a directory link will run dired or open a finder/explorer
  1325. window on that directory."
  1326. :group 'org-link-follow
  1327. :type 'boolean)
  1328. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1329. "Function and arguments to call for following mailto links.
  1330. This is a list with the first element being a Lisp function, and the
  1331. remaining elements being arguments to the function. In string arguments,
  1332. %a will be replaced by the address, and %s will be replaced by the subject
  1333. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1334. :group 'org-link-follow
  1335. :type '(choice
  1336. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1337. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1338. (const :tag "message-mail" (message-mail "%a" "%s"))
  1339. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1340. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1341. "Non-nil means ask for confirmation before executing shell links.
  1342. Shell links can be dangerous: just think about a link
  1343. [[shell:rm -rf ~/*][Google Search]]
  1344. This link would show up in your Org-mode document as \"Google Search\",
  1345. but really it would remove your entire home directory.
  1346. Therefore we advise against setting this variable to nil.
  1347. Just change it to `y-or-n-p' if you want to confirm with a
  1348. single keystroke rather than having to type \"yes\"."
  1349. :group 'org-link-follow
  1350. :type '(choice
  1351. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1352. (const :tag "with y-or-n (faster)" y-or-n-p)
  1353. (const :tag "no confirmation (dangerous)" nil)))
  1354. (put 'org-confirm-shell-link-function
  1355. 'safe-local-variable
  1356. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1357. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1358. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1359. Elisp links can be dangerous: just think about a link
  1360. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1361. This link would show up in your Org-mode document as \"Google Search\",
  1362. but really it would remove your entire home directory.
  1363. Therefore we advise against setting this variable to nil.
  1364. Just change it to `y-or-n-p' if you want to confirm with a
  1365. single keystroke rather than having to type \"yes\"."
  1366. :group 'org-link-follow
  1367. :type '(choice
  1368. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1369. (const :tag "with y-or-n (faster)" y-or-n-p)
  1370. (const :tag "no confirmation (dangerous)" nil)))
  1371. (put 'org-confirm-shell-link-function
  1372. 'safe-local-variable
  1373. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1374. (defconst org-file-apps-defaults-gnu
  1375. '((remote . emacs)
  1376. (system . mailcap)
  1377. (t . mailcap))
  1378. "Default file applications on a UNIX or GNU/Linux system.
  1379. See `org-file-apps'.")
  1380. (defconst org-file-apps-defaults-macosx
  1381. '((remote . emacs)
  1382. (t . "open %s")
  1383. (system . "open %s")
  1384. ("ps.gz" . "gv %s")
  1385. ("eps.gz" . "gv %s")
  1386. ("dvi" . "xdvi %s")
  1387. ("fig" . "xfig %s"))
  1388. "Default file applications on a MacOS X system.
  1389. The system \"open\" is known as a default, but we use X11 applications
  1390. for some files for which the OS does not have a good default.
  1391. See `org-file-apps'.")
  1392. (defconst org-file-apps-defaults-windowsnt
  1393. (list
  1394. '(remote . emacs)
  1395. (cons t
  1396. (list (if (featurep 'xemacs)
  1397. 'mswindows-shell-execute
  1398. 'w32-shell-execute)
  1399. "open" 'file))
  1400. (cons 'system
  1401. (list (if (featurep 'xemacs)
  1402. 'mswindows-shell-execute
  1403. 'w32-shell-execute)
  1404. "open" 'file)))
  1405. "Default file applications on a Windows NT system.
  1406. The system \"open\" is used for most files.
  1407. See `org-file-apps'.")
  1408. (defcustom org-file-apps
  1409. '(
  1410. (auto-mode . emacs)
  1411. ("\\.mm\\'" . default)
  1412. ("\\.x?html?\\'" . default)
  1413. ("\\.pdf\\'" . default)
  1414. )
  1415. "External applications for opening `file:path' items in a document.
  1416. Org-mode uses system defaults for different file types, but
  1417. you can use this variable to set the application for a given file
  1418. extension. The entries in this list are cons cells where the car identifies
  1419. files and the cdr the corresponding command. Possible values for the
  1420. file identifier are
  1421. \"string\" A string as a file identifier can be interpreted in different
  1422. ways, depending on its contents:
  1423. - Alphanumeric characters only:
  1424. Match links with this file extension.
  1425. Example: (\"pdf\" . \"evince %s\")
  1426. to open PDFs with evince.
  1427. - Regular expression: Match links where the
  1428. filename matches the regexp. If you want to
  1429. use groups here, use shy groups.
  1430. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1431. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1432. to open *.html and *.xhtml with firefox.
  1433. - Regular expression which contains (non-shy) groups:
  1434. Match links where the whole link, including \"::\", and
  1435. anything after that, matches the regexp.
  1436. In a custom command string, %1, %2, etc. are replaced with
  1437. the parts of the link that were matched by the groups.
  1438. For backwards compatibility, if a command string is given
  1439. that does not use any of the group matches, this case is
  1440. handled identically to the second one (i.e. match against
  1441. file name only).
  1442. In a custom lisp form, you can access the group matches with
  1443. (match-string n link).
  1444. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1445. to open [[file:document.pdf::5]] with evince at page 5.
  1446. `directory' Matches a directory
  1447. `remote' Matches a remote file, accessible through tramp or efs.
  1448. Remote files most likely should be visited through Emacs
  1449. because external applications cannot handle such paths.
  1450. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1451. so all files Emacs knows how to handle. Using this with
  1452. command `emacs' will open most files in Emacs. Beware that this
  1453. will also open html files inside Emacs, unless you add
  1454. (\"html\" . default) to the list as well.
  1455. t Default for files not matched by any of the other options.
  1456. `system' The system command to open files, like `open' on Windows
  1457. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1458. that will be selected if you call `C-c C-o' with a double
  1459. \\[universal-argument] \\[universal-argument] prefix.
  1460. Possible values for the command are:
  1461. `emacs' The file will be visited by the current Emacs process.
  1462. `default' Use the default application for this file type, which is the
  1463. association for t in the list, most likely in the system-specific
  1464. part.
  1465. This can be used to overrule an unwanted setting in the
  1466. system-specific variable.
  1467. `system' Use the system command for opening files, like \"open\".
  1468. This command is specified by the entry whose car is `system'.
  1469. Most likely, the system-specific version of this variable
  1470. does define this command, but you can overrule/replace it
  1471. here.
  1472. string A command to be executed by a shell; %s will be replaced
  1473. by the path to the file.
  1474. sexp A Lisp form which will be evaluated. The file path will
  1475. be available in the Lisp variable `file'.
  1476. For more examples, see the system specific constants
  1477. `org-file-apps-defaults-macosx'
  1478. `org-file-apps-defaults-windowsnt'
  1479. `org-file-apps-defaults-gnu'."
  1480. :group 'org-link-follow
  1481. :type '(repeat
  1482. (cons (choice :value ""
  1483. (string :tag "Extension")
  1484. (const :tag "System command to open files" system)
  1485. (const :tag "Default for unrecognized files" t)
  1486. (const :tag "Remote file" remote)
  1487. (const :tag "Links to a directory" directory)
  1488. (const :tag "Any files that have Emacs modes"
  1489. auto-mode))
  1490. (choice :value ""
  1491. (const :tag "Visit with Emacs" emacs)
  1492. (const :tag "Use default" default)
  1493. (const :tag "Use the system command" system)
  1494. (string :tag "Command")
  1495. (sexp :tag "Lisp form")))))
  1496. (defgroup org-refile nil
  1497. "Options concerning refiling entries in Org-mode."
  1498. :tag "Org Refile"
  1499. :group 'org)
  1500. (defcustom org-directory "~/org"
  1501. "Directory with org files.
  1502. This is just a default location to look for Org files. There is no need
  1503. at all to put your files into this directory. It is only used in the
  1504. following situations:
  1505. 1. When a remember template specifies a target file that is not an
  1506. absolute path. The path will then be interpreted relative to
  1507. `org-directory'
  1508. 2. When a remember note is filed away in an interactive way (when exiting the
  1509. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1510. with `org-directory' as the default path."
  1511. :group 'org-refile
  1512. :group 'org-remember
  1513. :type 'directory)
  1514. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1515. "Default target for storing notes.
  1516. Used as a fall back file for org-remember.el and org-capture.el, for
  1517. templates that do not specify a target file."
  1518. :group 'org-refile
  1519. :group 'org-remember
  1520. :type '(choice
  1521. (const :tag "Default from remember-data-file" nil)
  1522. file))
  1523. (defcustom org-goto-interface 'outline
  1524. "The default interface to be used for `org-goto'.
  1525. Allowed values are:
  1526. outline The interface shows an outline of the relevant file
  1527. and the correct heading is found by moving through
  1528. the outline or by searching with incremental search.
  1529. outline-path-completion Headlines in the current buffer are offered via
  1530. completion. This is the interface also used by
  1531. the refile command."
  1532. :group 'org-refile
  1533. :type '(choice
  1534. (const :tag "Outline" outline)
  1535. (const :tag "Outline-path-completion" outline-path-completion)))
  1536. (defcustom org-goto-max-level 5
  1537. "Maximum target level when running `org-goto' with refile interface."
  1538. :group 'org-refile
  1539. :type 'integer)
  1540. (defcustom org-reverse-note-order nil
  1541. "Non-nil means store new notes at the beginning of a file or entry.
  1542. When nil, new notes will be filed to the end of a file or entry.
  1543. This can also be a list with cons cells of regular expressions that
  1544. are matched against file names, and values."
  1545. :group 'org-remember
  1546. :group 'org-refile
  1547. :type '(choice
  1548. (const :tag "Reverse always" t)
  1549. (const :tag "Reverse never" nil)
  1550. (repeat :tag "By file name regexp"
  1551. (cons regexp boolean))))
  1552. (defcustom org-log-refile nil
  1553. "Information to record when a task is refiled.
  1554. Possible values are:
  1555. nil Don't add anything
  1556. time Add a time stamp to the task
  1557. note Prompt for a note and add it with template `org-log-note-headings'
  1558. This option can also be set with on a per-file-basis with
  1559. #+STARTUP: nologrefile
  1560. #+STARTUP: logrefile
  1561. #+STARTUP: lognoterefile
  1562. You can have local logging settings for a subtree by setting the LOGGING
  1563. property to one or more of these keywords.
  1564. When bulk-refiling from the agenda, the value `note' is forbidden and
  1565. will temporarily be changed to `time'."
  1566. :group 'org-refile
  1567. :group 'org-progress
  1568. :type '(choice
  1569. (const :tag "No logging" nil)
  1570. (const :tag "Record timestamp" time)
  1571. (const :tag "Record timestamp with note." note)))
  1572. (defcustom org-refile-targets nil
  1573. "Targets for refiling entries with \\[org-refile].
  1574. This is list of cons cells. Each cell contains:
  1575. - a specification of the files to be considered, either a list of files,
  1576. or a symbol whose function or variable value will be used to retrieve
  1577. a file name or a list of file names. If you use `org-agenda-files' for
  1578. that, all agenda files will be scanned for targets. Nil means consider
  1579. headings in the current buffer.
  1580. - A specification of how to find candidate refile targets. This may be
  1581. any of:
  1582. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1583. This tag has to be present in all target headlines, inheritance will
  1584. not be considered.
  1585. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1586. todo keyword.
  1587. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1588. headlines that are refiling targets.
  1589. - a cons cell (:level . N). Any headline of level N is considered a target.
  1590. Note that, when `org-odd-levels-only' is set, level corresponds to
  1591. order in hierarchy, not to the number of stars.
  1592. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1593. Note that, when `org-odd-levels-only' is set, level corresponds to
  1594. order in hierarchy, not to the number of stars.
  1595. You can set the variable `org-refile-target-verify-function' to a function
  1596. to verify each headline found by the simple criteria above.
  1597. When this variable is nil, all top-level headlines in the current buffer
  1598. are used, equivalent to the value `((nil . (:level . 1))'."
  1599. :group 'org-refile
  1600. :type '(repeat
  1601. (cons
  1602. (choice :value org-agenda-files
  1603. (const :tag "All agenda files" org-agenda-files)
  1604. (const :tag "Current buffer" nil)
  1605. (function) (variable) (file))
  1606. (choice :tag "Identify target headline by"
  1607. (cons :tag "Specific tag" (const :value :tag) (string))
  1608. (cons :tag "TODO keyword" (const :value :todo) (string))
  1609. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1610. (cons :tag "Level number" (const :value :level) (integer))
  1611. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1612. (defcustom org-refile-target-verify-function nil
  1613. "Function to verify if the headline at point should be a refile target.
  1614. The function will be called without arguments, with point at the
  1615. beginning of the headline. It should return t and leave point
  1616. where it is if the headline is a valid target for refiling.
  1617. If the target should not be selected, the function must return nil.
  1618. In addition to this, it may move point to a place from where the search
  1619. should be continued. For example, the function may decide that the entire
  1620. subtree of the current entry should be excluded and move point to the end
  1621. of the subtree."
  1622. :group 'org-refile
  1623. :type 'function)
  1624. (defcustom org-refile-use-cache nil
  1625. "Non-nil means cache refile targets to speed up the process.
  1626. The cache for a particular file will be updated automatically when
  1627. the buffer has been killed, or when any of the marker used for flagging
  1628. refile targets no longer points at a live buffer.
  1629. If you have added new entries to a buffer that might themselves be targets,
  1630. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1631. find that easier, `C-u C-u C-u C-c C-w'."
  1632. :group 'org-refile
  1633. :type 'boolean)
  1634. (defcustom org-refile-use-outline-path nil
  1635. "Non-nil means provide refile targets as paths.
  1636. So a level 3 headline will be available as level1/level2/level3.
  1637. When the value is `file', also include the file name (without directory)
  1638. into the path. In this case, you can also stop the completion after
  1639. the file name, to get entries inserted as top level in the file.
  1640. When `full-file-path', include the full file path."
  1641. :group 'org-refile
  1642. :type '(choice
  1643. (const :tag "Not" nil)
  1644. (const :tag "Yes" t)
  1645. (const :tag "Start with file name" file)
  1646. (const :tag "Start with full file path" full-file-path)))
  1647. (defcustom org-outline-path-complete-in-steps t
  1648. "Non-nil means complete the outline path in hierarchical steps.
  1649. When Org-mode uses the refile interface to select an outline path
  1650. \(see variable `org-refile-use-outline-path'), the completion of
  1651. the path can be done is a single go, or if can be done in steps down
  1652. the headline hierarchy. Going in steps is probably the best if you
  1653. do not use a special completion package like `ido' or `icicles'.
  1654. However, when using these packages, going in one step can be very
  1655. fast, while still showing the whole path to the entry."
  1656. :group 'org-refile
  1657. :type 'boolean)
  1658. (defcustom org-refile-allow-creating-parent-nodes nil
  1659. "Non-nil means allow to create new nodes as refile targets.
  1660. New nodes are then created by adding \"/new node name\" to the completion
  1661. of an existing node. When the value of this variable is `confirm',
  1662. new node creation must be confirmed by the user (recommended)
  1663. When nil, the completion must match an existing entry.
  1664. Note that, if the new heading is not seen by the criteria
  1665. listed in `org-refile-targets', multiple instances of the same
  1666. heading would be created by trying again to file under the new
  1667. heading."
  1668. :group 'org-refile
  1669. :type '(choice
  1670. (const :tag "Never" nil)
  1671. (const :tag "Always" t)
  1672. (const :tag "Prompt for confirmation" confirm)))
  1673. (defgroup org-todo nil
  1674. "Options concerning TODO items in Org-mode."
  1675. :tag "Org TODO"
  1676. :group 'org)
  1677. (defgroup org-progress nil
  1678. "Options concerning Progress logging in Org-mode."
  1679. :tag "Org Progress"
  1680. :group 'org-time)
  1681. (defvar org-todo-interpretation-widgets
  1682. '(
  1683. (:tag "Sequence (cycling hits every state)" sequence)
  1684. (:tag "Type (cycling directly to DONE)" type))
  1685. "The available interpretation symbols for customizing `org-todo-keywords'.
  1686. Interested libraries should add to this list.")
  1687. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1688. "List of TODO entry keyword sequences and their interpretation.
  1689. \\<org-mode-map>This is a list of sequences.
  1690. Each sequence starts with a symbol, either `sequence' or `type',
  1691. indicating if the keywords should be interpreted as a sequence of
  1692. action steps, or as different types of TODO items. The first
  1693. keywords are states requiring action - these states will select a headline
  1694. for inclusion into the global TODO list Org-mode produces. If one of
  1695. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1696. signify that no further action is necessary. If \"|\" is not found,
  1697. the last keyword is treated as the only DONE state of the sequence.
  1698. The command \\[org-todo] cycles an entry through these states, and one
  1699. additional state where no keyword is present. For details about this
  1700. cycling, see the manual.
  1701. TODO keywords and interpretation can also be set on a per-file basis with
  1702. the special #+SEQ_TODO and #+TYP_TODO lines.
  1703. Each keyword can optionally specify a character for fast state selection
  1704. \(in combination with the variable `org-use-fast-todo-selection')
  1705. and specifiers for state change logging, using the same syntax
  1706. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1707. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1708. indicates to record a time stamp each time this state is selected.
  1709. Each keyword may also specify if a timestamp or a note should be
  1710. recorded when entering or leaving the state, by adding additional
  1711. characters in the parenthesis after the keyword. This looks like this:
  1712. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1713. record only the time of the state change. With X and Y being either
  1714. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1715. Y when leaving the state if and only if the *target* state does not
  1716. define X. You may omit any of the fast-selection key or X or /Y,
  1717. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1718. For backward compatibility, this variable may also be just a list
  1719. of keywords - in this case the interpretation (sequence or type) will be
  1720. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1721. :group 'org-todo
  1722. :group 'org-keywords
  1723. :type '(choice
  1724. (repeat :tag "Old syntax, just keywords"
  1725. (string :tag "Keyword"))
  1726. (repeat :tag "New syntax"
  1727. (cons
  1728. (choice
  1729. :tag "Interpretation"
  1730. ;;Quick and dirty way to see
  1731. ;;`org-todo-interpretations'. This takes the
  1732. ;;place of item arguments
  1733. :convert-widget
  1734. (lambda (widget)
  1735. (widget-put widget
  1736. :args (mapcar
  1737. #'(lambda (x)
  1738. (widget-convert
  1739. (cons 'const x)))
  1740. org-todo-interpretation-widgets))
  1741. widget))
  1742. (repeat
  1743. (string :tag "Keyword"))))))
  1744. (defvar org-todo-keywords-1 nil
  1745. "All TODO and DONE keywords active in a buffer.")
  1746. (make-variable-buffer-local 'org-todo-keywords-1)
  1747. (defvar org-todo-keywords-for-agenda nil)
  1748. (defvar org-done-keywords-for-agenda nil)
  1749. (defvar org-drawers-for-agenda nil)
  1750. (defvar org-todo-keyword-alist-for-agenda nil)
  1751. (defvar org-tag-alist-for-agenda nil)
  1752. (defvar org-agenda-contributing-files nil)
  1753. (defvar org-not-done-keywords nil)
  1754. (make-variable-buffer-local 'org-not-done-keywords)
  1755. (defvar org-done-keywords nil)
  1756. (make-variable-buffer-local 'org-done-keywords)
  1757. (defvar org-todo-heads nil)
  1758. (make-variable-buffer-local 'org-todo-heads)
  1759. (defvar org-todo-sets nil)
  1760. (make-variable-buffer-local 'org-todo-sets)
  1761. (defvar org-todo-log-states nil)
  1762. (make-variable-buffer-local 'org-todo-log-states)
  1763. (defvar org-todo-kwd-alist nil)
  1764. (make-variable-buffer-local 'org-todo-kwd-alist)
  1765. (defvar org-todo-key-alist nil)
  1766. (make-variable-buffer-local 'org-todo-key-alist)
  1767. (defvar org-todo-key-trigger nil)
  1768. (make-variable-buffer-local 'org-todo-key-trigger)
  1769. (defcustom org-todo-interpretation 'sequence
  1770. "Controls how TODO keywords are interpreted.
  1771. This variable is in principle obsolete and is only used for
  1772. backward compatibility, if the interpretation of todo keywords is
  1773. not given already in `org-todo-keywords'. See that variable for
  1774. more information."
  1775. :group 'org-todo
  1776. :group 'org-keywords
  1777. :type '(choice (const sequence)
  1778. (const type)))
  1779. (defcustom org-use-fast-todo-selection t
  1780. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1781. This variable describes if and under what circumstances the cycling
  1782. mechanism for TODO keywords will be replaced by a single-key, direct
  1783. selection scheme.
  1784. When nil, fast selection is never used.
  1785. When the symbol `prefix', it will be used when `org-todo' is called with
  1786. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1787. in an agenda buffer.
  1788. When t, fast selection is used by default. In this case, the prefix
  1789. argument forces cycling instead.
  1790. In all cases, the special interface is only used if access keys have actually
  1791. been assigned by the user, i.e. if keywords in the configuration are followed
  1792. by a letter in parenthesis, like TODO(t)."
  1793. :group 'org-todo
  1794. :type '(choice
  1795. (const :tag "Never" nil)
  1796. (const :tag "By default" t)
  1797. (const :tag "Only with C-u C-c C-t" prefix)))
  1798. (defcustom org-provide-todo-statistics t
  1799. "Non-nil means update todo statistics after insert and toggle.
  1800. ALL-HEADLINES means update todo statistics by including headlines
  1801. with no TODO keyword as well, counting them as not done.
  1802. A list of TODO keywords means the same, but skip keywords that are
  1803. not in this list.
  1804. When this is set, todo statistics is updated in the parent of the
  1805. current entry each time a todo state is changed."
  1806. :group 'org-todo
  1807. :type '(choice
  1808. (const :tag "Yes, only for TODO entries" t)
  1809. (const :tag "Yes, including all entries" 'all-headlines)
  1810. (repeat :tag "Yes, for TODOs in this list"
  1811. (string :tag "TODO keyword"))
  1812. (other :tag "No TODO statistics" nil)))
  1813. (defcustom org-hierarchical-todo-statistics t
  1814. "Non-nil means TODO statistics covers just direct children.
  1815. When nil, all entries in the subtree are considered.
  1816. This has only an effect if `org-provide-todo-statistics' is set.
  1817. To set this to nil for only a single subtree, use a COOKIE_DATA
  1818. property and include the word \"recursive\" into the value."
  1819. :group 'org-todo
  1820. :type 'boolean)
  1821. (defcustom org-after-todo-state-change-hook nil
  1822. "Hook which is run after the state of a TODO item was changed.
  1823. The new state (a string with a TODO keyword, or nil) is available in the
  1824. Lisp variable `state'."
  1825. :group 'org-todo
  1826. :type 'hook)
  1827. (defvar org-blocker-hook nil
  1828. "Hook for functions that are allowed to block a state change.
  1829. Each function gets as its single argument a property list, see
  1830. `org-trigger-hook' for more information about this list.
  1831. If any of the functions in this hook returns nil, the state change
  1832. is blocked.")
  1833. (defvar org-trigger-hook nil
  1834. "Hook for functions that are triggered by a state change.
  1835. Each function gets as its single argument a property list with at least
  1836. the following elements:
  1837. (:type type-of-change :position pos-at-entry-start
  1838. :from old-state :to new-state)
  1839. Depending on the type, more properties may be present.
  1840. This mechanism is currently implemented for:
  1841. TODO state changes
  1842. ------------------
  1843. :type todo-state-change
  1844. :from previous state (keyword as a string), or nil, or a symbol
  1845. 'todo' or 'done', to indicate the general type of state.
  1846. :to new state, like in :from")
  1847. (defcustom org-enforce-todo-dependencies nil
  1848. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1849. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1850. be blocked if any prior sibling is not yet done.
  1851. Finally, if the parent is blocked because of ordered siblings of its own,
  1852. the child will also be blocked.
  1853. This variable needs to be set before org.el is loaded, and you need to
  1854. restart Emacs after a change to make the change effective. The only way
  1855. to change is while Emacs is running is through the customize interface."
  1856. :set (lambda (var val)
  1857. (set var val)
  1858. (if val
  1859. (add-hook 'org-blocker-hook
  1860. 'org-block-todo-from-children-or-siblings-or-parent)
  1861. (remove-hook 'org-blocker-hook
  1862. 'org-block-todo-from-children-or-siblings-or-parent)))
  1863. :group 'org-todo
  1864. :type 'boolean)
  1865. (defcustom org-enforce-todo-checkbox-dependencies nil
  1866. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1867. When this is nil, checkboxes have no influence on switching TODO states.
  1868. When non-nil, you first need to check off all check boxes before the TODO
  1869. entry can be switched to DONE.
  1870. This variable needs to be set before org.el is loaded, and you need to
  1871. restart Emacs after a change to make the change effective. The only way
  1872. to change is while Emacs is running is through the customize interface."
  1873. :set (lambda (var val)
  1874. (set var val)
  1875. (if val
  1876. (add-hook 'org-blocker-hook
  1877. 'org-block-todo-from-checkboxes)
  1878. (remove-hook 'org-blocker-hook
  1879. 'org-block-todo-from-checkboxes)))
  1880. :group 'org-todo
  1881. :type 'boolean)
  1882. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1883. "Non-nil means inserting a TODO heading is treated as state change.
  1884. So when the command \\[org-insert-todo-heading] is used, state change
  1885. logging will apply if appropriate. When nil, the new TODO item will
  1886. be inserted directly, and no logging will take place."
  1887. :group 'org-todo
  1888. :type 'boolean)
  1889. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1890. "Non-nil means switching TODO states with S-cursor counts as state change.
  1891. This is the default behavior. However, setting this to nil allows a
  1892. convenient way to select a TODO state and bypass any logging associated
  1893. with that."
  1894. :group 'org-todo
  1895. :type 'boolean)
  1896. (defcustom org-todo-state-tags-triggers nil
  1897. "Tag changes that should be triggered by TODO state changes.
  1898. This is a list. Each entry is
  1899. (state-change (tag . flag) .......)
  1900. State-change can be a string with a state, and empty string to indicate the
  1901. state that has no TODO keyword, or it can be one of the symbols `todo'
  1902. or `done', meaning any not-done or done state, respectively."
  1903. :group 'org-todo
  1904. :group 'org-tags
  1905. :type '(repeat
  1906. (cons (choice :tag "When changing to"
  1907. (const :tag "Not-done state" todo)
  1908. (const :tag "Done state" done)
  1909. (string :tag "State"))
  1910. (repeat
  1911. (cons :tag "Tag action"
  1912. (string :tag "Tag")
  1913. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1914. (defcustom org-log-done nil
  1915. "Information to record when a task moves to the DONE state.
  1916. Possible values are:
  1917. nil Don't add anything, just change the keyword
  1918. time Add a time stamp to the task
  1919. note Prompt for a note and add it with template `org-log-note-headings'
  1920. This option can also be set with on a per-file-basis with
  1921. #+STARTUP: nologdone
  1922. #+STARTUP: logdone
  1923. #+STARTUP: lognotedone
  1924. You can have local logging settings for a subtree by setting the LOGGING
  1925. property to one or more of these keywords."
  1926. :group 'org-todo
  1927. :group 'org-progress
  1928. :type '(choice
  1929. (const :tag "No logging" nil)
  1930. (const :tag "Record CLOSED timestamp" time)
  1931. (const :tag "Record CLOSED timestamp with note." note)))
  1932. ;; Normalize old uses of org-log-done.
  1933. (cond
  1934. ((eq org-log-done t) (setq org-log-done 'time))
  1935. ((and (listp org-log-done) (memq 'done org-log-done))
  1936. (setq org-log-done 'note)))
  1937. (defcustom org-log-reschedule nil
  1938. "Information to record when the scheduling date of a tasks is modified.
  1939. Possible values are:
  1940. nil Don't add anything, just change the date
  1941. time Add a time stamp to the task
  1942. note Prompt for a note and add it with template `org-log-note-headings'
  1943. This option can also be set with on a per-file-basis with
  1944. #+STARTUP: nologreschedule
  1945. #+STARTUP: logreschedule
  1946. #+STARTUP: lognotereschedule"
  1947. :group 'org-todo
  1948. :group 'org-progress
  1949. :type '(choice
  1950. (const :tag "No logging" nil)
  1951. (const :tag "Record timestamp" time)
  1952. (const :tag "Record timestamp with note." note)))
  1953. (defcustom org-log-redeadline nil
  1954. "Information to record when the deadline date of a tasks is modified.
  1955. Possible values are:
  1956. nil Don't add anything, just change the date
  1957. time Add a time stamp to the task
  1958. note Prompt for a note and add it with template `org-log-note-headings'
  1959. This option can also be set with on a per-file-basis with
  1960. #+STARTUP: nologredeadline
  1961. #+STARTUP: logredeadline
  1962. #+STARTUP: lognoteredeadline
  1963. You can have local logging settings for a subtree by setting the LOGGING
  1964. property to one or more of these keywords."
  1965. :group 'org-todo
  1966. :group 'org-progress
  1967. :type '(choice
  1968. (const :tag "No logging" nil)
  1969. (const :tag "Record timestamp" time)
  1970. (const :tag "Record timestamp with note." note)))
  1971. (defcustom org-log-note-clock-out nil
  1972. "Non-nil means record a note when clocking out of an item.
  1973. This can also be configured on a per-file basis by adding one of
  1974. the following lines anywhere in the buffer:
  1975. #+STARTUP: lognoteclock-out
  1976. #+STARTUP: nolognoteclock-out"
  1977. :group 'org-todo
  1978. :group 'org-progress
  1979. :type 'boolean)
  1980. (defcustom org-log-done-with-time t
  1981. "Non-nil means the CLOSED time stamp will contain date and time.
  1982. When nil, only the date will be recorded."
  1983. :group 'org-progress
  1984. :type 'boolean)
  1985. (defcustom org-log-note-headings
  1986. '((done . "CLOSING NOTE %t")
  1987. (state . "State %-12s from %-12S %t")
  1988. (note . "Note taken on %t")
  1989. (reschedule . "Rescheduled from %S on %t")
  1990. (delschedule . "Not scheduled, was %S on %t")
  1991. (redeadline . "New deadline from %S on %t")
  1992. (deldeadline . "Removed deadline, was %S on %t")
  1993. (refile . "Refiled on %t")
  1994. (clock-out . ""))
  1995. "Headings for notes added to entries.
  1996. The value is an alist, with the car being a symbol indicating the note
  1997. context, and the cdr is the heading to be used. The heading may also be the
  1998. empty string.
  1999. %t in the heading will be replaced by a time stamp.
  2000. %T will be an active time stamp instead the default inactive one
  2001. %s will be replaced by the new TODO state, in double quotes.
  2002. %S will be replaced by the old TODO state, in double quotes.
  2003. %u will be replaced by the user name.
  2004. %U will be replaced by the full user name.
  2005. In fact, it is not a good idea to change the `state' entry, because
  2006. agenda log mode depends on the format of these entries."
  2007. :group 'org-todo
  2008. :group 'org-progress
  2009. :type '(list :greedy t
  2010. (cons (const :tag "Heading when closing an item" done) string)
  2011. (cons (const :tag
  2012. "Heading when changing todo state (todo sequence only)"
  2013. state) string)
  2014. (cons (const :tag "Heading when just taking a note" note) string)
  2015. (cons (const :tag "Heading when clocking out" clock-out) string)
  2016. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2017. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2018. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2019. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2020. (cons (const :tag "Heading when refiling" refile) string)))
  2021. (unless (assq 'note org-log-note-headings)
  2022. (push '(note . "%t") org-log-note-headings))
  2023. (defcustom org-log-into-drawer nil
  2024. "Non-nil means insert state change notes and time stamps into a drawer.
  2025. When nil, state changes notes will be inserted after the headline and
  2026. any scheduling and clock lines, but not inside a drawer.
  2027. The value of this variable should be the name of the drawer to use.
  2028. LOGBOOK is proposed at the default drawer for this purpose, you can
  2029. also set this to a string to define the drawer of your choice.
  2030. A value of t is also allowed, representing \"LOGBOOK\".
  2031. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2032. will be ignored.
  2033. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2034. a subtree."
  2035. :group 'org-todo
  2036. :group 'org-progress
  2037. :type '(choice
  2038. (const :tag "Not into a drawer" nil)
  2039. (const :tag "LOGBOOK" t)
  2040. (string :tag "Other")))
  2041. (if (fboundp 'defvaralias)
  2042. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2043. (defun org-log-into-drawer ()
  2044. "Return the value of `org-log-into-drawer', but let properties overrule.
  2045. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2046. used instead of the default value."
  2047. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  2048. (cond
  2049. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2050. ((equal p "t") "LOGBOOK")
  2051. (t p))))
  2052. (defcustom org-log-state-notes-insert-after-drawers nil
  2053. "Non-nil means insert state change notes after any drawers in entry.
  2054. Only the drawers that *immediately* follow the headline and the
  2055. deadline/scheduled line are skipped.
  2056. When nil, insert notes right after the heading and perhaps the line
  2057. with deadline/scheduling if present.
  2058. This variable will have no effect if `org-log-into-drawer' is
  2059. set."
  2060. :group 'org-todo
  2061. :group 'org-progress
  2062. :type 'boolean)
  2063. (defcustom org-log-states-order-reversed t
  2064. "Non-nil means the latest state note will be directly after heading.
  2065. When nil, the state change notes will be ordered according to time."
  2066. :group 'org-todo
  2067. :group 'org-progress
  2068. :type 'boolean)
  2069. (defcustom org-todo-repeat-to-state nil
  2070. "The TODO state to which a repeater should return the repeating task.
  2071. By default this is the first task in a TODO sequence, or the previous state
  2072. in a TODO_TYP set. But you can specify another task here.
  2073. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2074. :group 'org-todo
  2075. :type '(choice (const :tag "Head of sequence" nil)
  2076. (string :tag "Specific state")))
  2077. (defcustom org-log-repeat 'time
  2078. "Non-nil means record moving through the DONE state when triggering repeat.
  2079. An auto-repeating task is immediately switched back to TODO when
  2080. marked DONE. If you are not logging state changes (by adding \"@\"
  2081. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2082. record a closing note, there will be no record of the task moving
  2083. through DONE. This variable forces taking a note anyway.
  2084. nil Don't force a record
  2085. time Record a time stamp
  2086. note Record a note
  2087. This option can also be set with on a per-file-basis with
  2088. #+STARTUP: logrepeat
  2089. #+STARTUP: lognoterepeat
  2090. #+STARTUP: nologrepeat
  2091. You can have local logging settings for a subtree by setting the LOGGING
  2092. property to one or more of these keywords."
  2093. :group 'org-todo
  2094. :group 'org-progress
  2095. :type '(choice
  2096. (const :tag "Don't force a record" nil)
  2097. (const :tag "Force recording the DONE state" time)
  2098. (const :tag "Force recording a note with the DONE state" note)))
  2099. (defgroup org-priorities nil
  2100. "Priorities in Org-mode."
  2101. :tag "Org Priorities"
  2102. :group 'org-todo)
  2103. (defcustom org-enable-priority-commands t
  2104. "Non-nil means priority commands are active.
  2105. When nil, these commands will be disabled, so that you never accidentally
  2106. set a priority."
  2107. :group 'org-priorities
  2108. :type 'boolean)
  2109. (defcustom org-highest-priority ?A
  2110. "The highest priority of TODO items. A character like ?A, ?B etc.
  2111. Must have a smaller ASCII number than `org-lowest-priority'."
  2112. :group 'org-priorities
  2113. :type 'character)
  2114. (defcustom org-lowest-priority ?C
  2115. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2116. Must have a larger ASCII number than `org-highest-priority'."
  2117. :group 'org-priorities
  2118. :type 'character)
  2119. (defcustom org-default-priority ?B
  2120. "The default priority of TODO items.
  2121. This is the priority an item get if no explicit priority is given."
  2122. :group 'org-priorities
  2123. :type 'character)
  2124. (defcustom org-priority-start-cycle-with-default t
  2125. "Non-nil means start with default priority when starting to cycle.
  2126. When this is nil, the first step in the cycle will be (depending on the
  2127. command used) one higher or lower that the default priority."
  2128. :group 'org-priorities
  2129. :type 'boolean)
  2130. (defgroup org-time nil
  2131. "Options concerning time stamps and deadlines in Org-mode."
  2132. :tag "Org Time"
  2133. :group 'org)
  2134. (defcustom org-insert-labeled-timestamps-at-point nil
  2135. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2136. When nil, these labeled time stamps are forces into the second line of an
  2137. entry, just after the headline. When scheduling from the global TODO list,
  2138. the time stamp will always be forced into the second line."
  2139. :group 'org-time
  2140. :type 'boolean)
  2141. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2142. "Formats for `format-time-string' which are used for time stamps.
  2143. It is not recommended to change this constant.")
  2144. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2145. "Number of minutes to round time stamps to.
  2146. These are two values, the first applies when first creating a time stamp.
  2147. The second applies when changing it with the commands `S-up' and `S-down'.
  2148. When changing the time stamp, this means that it will change in steps
  2149. of N minutes, as given by the second value.
  2150. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2151. numbers should be factors of 60, so for example 5, 10, 15.
  2152. When this is larger than 1, you can still force an exact time stamp by using
  2153. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2154. and by using a prefix arg to `S-up/down' to specify the exact number
  2155. of minutes to shift."
  2156. :group 'org-time
  2157. :get '(lambda (var) ; Make sure both elements are there
  2158. (if (integerp (default-value var))
  2159. (list (default-value var) 5)
  2160. (default-value var)))
  2161. :type '(list
  2162. (integer :tag "when inserting times")
  2163. (integer :tag "when modifying times")))
  2164. ;; Normalize old customizations of this variable.
  2165. (when (integerp org-time-stamp-rounding-minutes)
  2166. (setq org-time-stamp-rounding-minutes
  2167. (list org-time-stamp-rounding-minutes
  2168. org-time-stamp-rounding-minutes)))
  2169. (defcustom org-display-custom-times nil
  2170. "Non-nil means overlay custom formats over all time stamps.
  2171. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2172. To turn this on on a per-file basis, insert anywhere in the file:
  2173. #+STARTUP: customtime"
  2174. :group 'org-time
  2175. :set 'set-default
  2176. :type 'sexp)
  2177. (make-variable-buffer-local 'org-display-custom-times)
  2178. (defcustom org-time-stamp-custom-formats
  2179. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2180. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2181. These are overlayed over the default ISO format if the variable
  2182. `org-display-custom-times' is set. Time like %H:%M should be at the
  2183. end of the second format. The custom formats are also honored by export
  2184. commands, if custom time display is turned on at the time of export."
  2185. :group 'org-time
  2186. :type 'sexp)
  2187. (defun org-time-stamp-format (&optional long inactive)
  2188. "Get the right format for a time string."
  2189. (let ((f (if long (cdr org-time-stamp-formats)
  2190. (car org-time-stamp-formats))))
  2191. (if inactive
  2192. (concat "[" (substring f 1 -1) "]")
  2193. f)))
  2194. (defcustom org-time-clocksum-format "%d:%02d"
  2195. "The format string used when creating CLOCKSUM lines.
  2196. This is also used when org-mode generates a time duration."
  2197. :group 'org-time
  2198. :type 'string)
  2199. (defcustom org-time-clocksum-use-fractional nil
  2200. "If non-nil, \\[org-clock-display] uses fractional times.
  2201. org-mode generates a time duration."
  2202. :group 'org-time
  2203. :type 'boolean)
  2204. (defcustom org-time-clocksum-fractional-format "%.2f"
  2205. "The format string used when creating CLOCKSUM lines, or when
  2206. org-mode generates a time duration."
  2207. :group 'org-time
  2208. :type 'string)
  2209. (defcustom org-deadline-warning-days 14
  2210. "No. of days before expiration during which a deadline becomes active.
  2211. This variable governs the display in sparse trees and in the agenda.
  2212. When 0 or negative, it means use this number (the absolute value of it)
  2213. even if a deadline has a different individual lead time specified.
  2214. Custom commands can set this variable in the options section."
  2215. :group 'org-time
  2216. :group 'org-agenda-daily/weekly
  2217. :type 'integer)
  2218. (defcustom org-read-date-prefer-future t
  2219. "Non-nil means assume future for incomplete date input from user.
  2220. This affects the following situations:
  2221. 1. The user gives a month but not a year.
  2222. For example, if it is April and you enter \"feb 2\", this will be read
  2223. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2224. 2. The user gives a day, but no month.
  2225. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2226. read this as the third of *next* month. However, if you enter \"17\",
  2227. it will be considered as *this* month.
  2228. If you set this variable to the symbol `time', then also the following
  2229. will work:
  2230. 3. If the user gives a time, but no day. If the time is before now,
  2231. to will be interpreted as tomorrow.
  2232. Currently none of this works for ISO week specifications.
  2233. When this option is nil, the current day, month and year will always be
  2234. used as defaults."
  2235. :group 'org-time
  2236. :type '(choice
  2237. (const :tag "Never" nil)
  2238. (const :tag "Check month and day" t)
  2239. (const :tag "Check month, day, and time" time)))
  2240. (defcustom org-read-date-display-live t
  2241. "Non-nil means display current interpretation of date prompt live.
  2242. This display will be in an overlay, in the minibuffer."
  2243. :group 'org-time
  2244. :type 'boolean)
  2245. (defcustom org-read-date-popup-calendar t
  2246. "Non-nil means pop up a calendar when prompting for a date.
  2247. In the calendar, the date can be selected with mouse-1. However, the
  2248. minibuffer will also be active, and you can simply enter the date as well.
  2249. When nil, only the minibuffer will be available."
  2250. :group 'org-time
  2251. :type 'boolean)
  2252. (if (fboundp 'defvaralias)
  2253. (defvaralias 'org-popup-calendar-for-date-prompt
  2254. 'org-read-date-popup-calendar))
  2255. (defcustom org-read-date-minibuffer-setup-hook nil
  2256. "Hook to be used to set up keys for the date/time interface.
  2257. Add key definitions to `minibuffer-local-map', which will be a temporary
  2258. copy."
  2259. :group 'org-time
  2260. :type 'hook)
  2261. (defcustom org-extend-today-until 0
  2262. "The hour when your day really ends. Must be an integer.
  2263. This has influence for the following applications:
  2264. - When switching the agenda to \"today\". It it is still earlier than
  2265. the time given here, the day recognized as TODAY is actually yesterday.
  2266. - When a date is read from the user and it is still before the time given
  2267. here, the current date and time will be assumed to be yesterday, 23:59.
  2268. Also, timestamps inserted in remember templates follow this rule.
  2269. IMPORTANT: This is a feature whose implementation is and likely will
  2270. remain incomplete. Really, it is only here because past midnight seems to
  2271. be the favorite working time of John Wiegley :-)"
  2272. :group 'org-time
  2273. :type 'integer)
  2274. (defcustom org-edit-timestamp-down-means-later nil
  2275. "Non-nil means S-down will increase the time in a time stamp.
  2276. When nil, S-up will increase."
  2277. :group 'org-time
  2278. :type 'boolean)
  2279. (defcustom org-calendar-follow-timestamp-change t
  2280. "Non-nil means make the calendar window follow timestamp changes.
  2281. When a timestamp is modified and the calendar window is visible, it will be
  2282. moved to the new date."
  2283. :group 'org-time
  2284. :type 'boolean)
  2285. (defgroup org-tags nil
  2286. "Options concerning tags in Org-mode."
  2287. :tag "Org Tags"
  2288. :group 'org)
  2289. (defcustom org-tag-alist nil
  2290. "List of tags allowed in Org-mode files.
  2291. When this list is nil, Org-mode will base TAG input on what is already in the
  2292. buffer.
  2293. The value of this variable is an alist, the car of each entry must be a
  2294. keyword as a string, the cdr may be a character that is used to select
  2295. that tag through the fast-tag-selection interface.
  2296. See the manual for details."
  2297. :group 'org-tags
  2298. :type '(repeat
  2299. (choice
  2300. (cons (string :tag "Tag name")
  2301. (character :tag "Access char"))
  2302. (list :tag "Start radio group"
  2303. (const :startgroup)
  2304. (option (string :tag "Group description")))
  2305. (list :tag "End radio group"
  2306. (const :endgroup)
  2307. (option (string :tag "Group description")))
  2308. (const :tag "New line" (:newline)))))
  2309. (defcustom org-tag-persistent-alist nil
  2310. "List of tags that will always appear in all Org-mode files.
  2311. This is in addition to any in buffer settings or customizations
  2312. of `org-tag-alist'.
  2313. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2314. The value of this variable is an alist, the car of each entry must be a
  2315. keyword as a string, the cdr may be a character that is used to select
  2316. that tag through the fast-tag-selection interface.
  2317. See the manual for details.
  2318. To disable these tags on a per-file basis, insert anywhere in the file:
  2319. #+STARTUP: noptag"
  2320. :group 'org-tags
  2321. :type '(repeat
  2322. (choice
  2323. (cons (string :tag "Tag name")
  2324. (character :tag "Access char"))
  2325. (const :tag "Start radio group" (:startgroup))
  2326. (const :tag "End radio group" (:endgroup))
  2327. (const :tag "New line" (:newline)))))
  2328. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2329. "If non-nil, always offer completion for all tags of all agenda files.
  2330. Instead of customizing this variable directly, you might want to
  2331. set it locally for remember buffers, because there no list of
  2332. tags in that file can be created dynamically (there are none).
  2333. (add-hook 'org-remember-mode-hook
  2334. (lambda ()
  2335. (set (make-local-variable
  2336. 'org-complete-tags-always-offer-all-agenda-tags)
  2337. t)))"
  2338. :group 'org-tags
  2339. :type 'boolean)
  2340. (defvar org-file-tags nil
  2341. "List of tags that can be inherited by all entries in the file.
  2342. The tags will be inherited if the variable `org-use-tag-inheritance'
  2343. says they should be.
  2344. This variable is populated from #+FILETAGS lines.")
  2345. (defcustom org-use-fast-tag-selection 'auto
  2346. "Non-nil means use fast tag selection scheme.
  2347. This is a special interface to select and deselect tags with single keys.
  2348. When nil, fast selection is never used.
  2349. When the symbol `auto', fast selection is used if and only if selection
  2350. characters for tags have been configured, either through the variable
  2351. `org-tag-alist' or through a #+TAGS line in the buffer.
  2352. When t, fast selection is always used and selection keys are assigned
  2353. automatically if necessary."
  2354. :group 'org-tags
  2355. :type '(choice
  2356. (const :tag "Always" t)
  2357. (const :tag "Never" nil)
  2358. (const :tag "When selection characters are configured" 'auto)))
  2359. (defcustom org-fast-tag-selection-single-key nil
  2360. "Non-nil means fast tag selection exits after first change.
  2361. When nil, you have to press RET to exit it.
  2362. During fast tag selection, you can toggle this flag with `C-c'.
  2363. This variable can also have the value `expert'. In this case, the window
  2364. displaying the tags menu is not even shown, until you press C-c again."
  2365. :group 'org-tags
  2366. :type '(choice
  2367. (const :tag "No" nil)
  2368. (const :tag "Yes" t)
  2369. (const :tag "Expert" expert)))
  2370. (defvar org-fast-tag-selection-include-todo nil
  2371. "Non-nil means fast tags selection interface will also offer TODO states.
  2372. This is an undocumented feature, you should not rely on it.")
  2373. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2374. "The column to which tags should be indented in a headline.
  2375. If this number is positive, it specifies the column. If it is negative,
  2376. it means that the tags should be flushright to that column. For example,
  2377. -80 works well for a normal 80 character screen."
  2378. :group 'org-tags
  2379. :type 'integer)
  2380. (defcustom org-auto-align-tags t
  2381. "Non-nil means realign tags after pro/demotion of TODO state change.
  2382. These operations change the length of a headline and therefore shift
  2383. the tags around. With this options turned on, after each such operation
  2384. the tags are again aligned to `org-tags-column'."
  2385. :group 'org-tags
  2386. :type 'boolean)
  2387. (defcustom org-use-tag-inheritance t
  2388. "Non-nil means tags in levels apply also for sublevels.
  2389. When nil, only the tags directly given in a specific line apply there.
  2390. This may also be a list of tags that should be inherited, or a regexp that
  2391. matches tags that should be inherited. Additional control is possible
  2392. with the variable `org-tags-exclude-from-inheritance' which gives an
  2393. explicit list of tags to be excluded from inheritance., even if the value of
  2394. `org-use-tag-inheritance' would select it for inheritance.
  2395. If this option is t, a match early-on in a tree can lead to a large
  2396. number of matches in the subtree when constructing the agenda or creating
  2397. a sparse tree. If you only want to see the first match in a tree during
  2398. a search, check out the variable `org-tags-match-list-sublevels'."
  2399. :group 'org-tags
  2400. :type '(choice
  2401. (const :tag "Not" nil)
  2402. (const :tag "Always" t)
  2403. (repeat :tag "Specific tags" (string :tag "Tag"))
  2404. (regexp :tag "Tags matched by regexp")))
  2405. (defcustom org-tags-exclude-from-inheritance nil
  2406. "List of tags that should never be inherited.
  2407. This is a way to exclude a few tags from inheritance. For way to do
  2408. the opposite, to actively allow inheritance for selected tags,
  2409. see the variable `org-use-tag-inheritance'."
  2410. :group 'org-tags
  2411. :type '(repeat (string :tag "Tag")))
  2412. (defun org-tag-inherit-p (tag)
  2413. "Check if TAG is one that should be inherited."
  2414. (cond
  2415. ((member tag org-tags-exclude-from-inheritance) nil)
  2416. ((eq org-use-tag-inheritance t) t)
  2417. ((not org-use-tag-inheritance) nil)
  2418. ((stringp org-use-tag-inheritance)
  2419. (string-match org-use-tag-inheritance tag))
  2420. ((listp org-use-tag-inheritance)
  2421. (member tag org-use-tag-inheritance))
  2422. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2423. (defcustom org-tags-match-list-sublevels t
  2424. "Non-nil means list also sublevels of headlines matching a search.
  2425. This variable applies to tags/property searches, and also to stuck
  2426. projects because this search is based on a tags match as well.
  2427. When set to the symbol `indented', sublevels are indented with
  2428. leading dots.
  2429. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2430. the sublevels of a headline matching a tag search often also match
  2431. the same search. Listing all of them can create very long lists.
  2432. Setting this variable to nil causes subtrees of a match to be skipped.
  2433. This variable is semi-obsolete and probably should always be true. It
  2434. is better to limit inheritance to certain tags using the variables
  2435. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2436. :group 'org-tags
  2437. :type '(choice
  2438. (const :tag "No, don't list them" nil)
  2439. (const :tag "Yes, do list them" t)
  2440. (const :tag "List them, indented with leading dots" indented)))
  2441. (defcustom org-tags-sort-function nil
  2442. "When set, tags are sorted using this function as a comparator."
  2443. :group 'org-tags
  2444. :type '(choice
  2445. (const :tag "No sorting" nil)
  2446. (const :tag "Alphabetical" string<)
  2447. (const :tag "Reverse alphabetical" string>)
  2448. (function :tag "Custom function" nil)))
  2449. (defvar org-tags-history nil
  2450. "History of minibuffer reads for tags.")
  2451. (defvar org-last-tags-completion-table nil
  2452. "The last used completion table for tags.")
  2453. (defvar org-after-tags-change-hook nil
  2454. "Hook that is run after the tags in a line have changed.")
  2455. (defgroup org-properties nil
  2456. "Options concerning properties in Org-mode."
  2457. :tag "Org Properties"
  2458. :group 'org)
  2459. (defcustom org-property-format "%-10s %s"
  2460. "How property key/value pairs should be formatted by `indent-line'.
  2461. When `indent-line' hits a property definition, it will format the line
  2462. according to this format, mainly to make sure that the values are
  2463. lined-up with respect to each other."
  2464. :group 'org-properties
  2465. :type 'string)
  2466. (defcustom org-use-property-inheritance nil
  2467. "Non-nil means properties apply also for sublevels.
  2468. This setting is chiefly used during property searches. Turning it on can
  2469. cause significant overhead when doing a search, which is why it is not
  2470. on by default.
  2471. When nil, only the properties directly given in the current entry count.
  2472. When t, every property is inherited. The value may also be a list of
  2473. properties that should have inheritance, or a regular expression matching
  2474. properties that should be inherited.
  2475. However, note that some special properties use inheritance under special
  2476. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2477. and the properties ending in \"_ALL\" when they are used as descriptor
  2478. for valid values of a property.
  2479. Note for programmers:
  2480. When querying an entry with `org-entry-get', you can control if inheritance
  2481. should be used. By default, `org-entry-get' looks only at the local
  2482. properties. You can request inheritance by setting the inherit argument
  2483. to t (to force inheritance) or to `selective' (to respect the setting
  2484. in this variable)."
  2485. :group 'org-properties
  2486. :type '(choice
  2487. (const :tag "Not" nil)
  2488. (const :tag "Always" t)
  2489. (repeat :tag "Specific properties" (string :tag "Property"))
  2490. (regexp :tag "Properties matched by regexp")))
  2491. (defun org-property-inherit-p (property)
  2492. "Check if PROPERTY is one that should be inherited."
  2493. (cond
  2494. ((eq org-use-property-inheritance t) t)
  2495. ((not org-use-property-inheritance) nil)
  2496. ((stringp org-use-property-inheritance)
  2497. (string-match org-use-property-inheritance property))
  2498. ((listp org-use-property-inheritance)
  2499. (member property org-use-property-inheritance))
  2500. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2501. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2502. "The default column format, if no other format has been defined.
  2503. This variable can be set on the per-file basis by inserting a line
  2504. #+COLUMNS: %25ITEM ....."
  2505. :group 'org-properties
  2506. :type 'string)
  2507. (defcustom org-columns-ellipses ".."
  2508. "The ellipses to be used when a field in column view is truncated.
  2509. When this is the empty string, as many characters as possible are shown,
  2510. but then there will be no visual indication that the field has been truncated.
  2511. When this is a string of length N, the last N characters of a truncated
  2512. field are replaced by this string. If the column is narrower than the
  2513. ellipses string, only part of the ellipses string will be shown."
  2514. :group 'org-properties
  2515. :type 'string)
  2516. (defcustom org-columns-modify-value-for-display-function nil
  2517. "Function that modifies values for display in column view.
  2518. For example, it can be used to cut out a certain part from a time stamp.
  2519. The function must take 2 arguments:
  2520. column-title The title of the column (*not* the property name)
  2521. value The value that should be modified.
  2522. The function should return the value that should be displayed,
  2523. or nil if the normal value should be used."
  2524. :group 'org-properties
  2525. :type 'function)
  2526. (defcustom org-effort-property "Effort"
  2527. "The property that is being used to keep track of effort estimates.
  2528. Effort estimates given in this property need to have the format H:MM."
  2529. :group 'org-properties
  2530. :group 'org-progress
  2531. :type '(string :tag "Property"))
  2532. (defconst org-global-properties-fixed
  2533. '(("VISIBILITY_ALL" . "folded children content all")
  2534. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2535. "List of property/value pairs that can be inherited by any entry.
  2536. These are fixed values, for the preset properties. The user variable
  2537. that can be used to add to this list is `org-global-properties'.
  2538. The entries in this list are cons cells where the car is a property
  2539. name and cdr is a string with the value. If the value represents
  2540. multiple items like an \"_ALL\" property, separate the items by
  2541. spaces.")
  2542. (defcustom org-global-properties nil
  2543. "List of property/value pairs that can be inherited by any entry.
  2544. This list will be combined with the constant `org-global-properties-fixed'.
  2545. The entries in this list are cons cells where the car is a property
  2546. name and cdr is a string with the value.
  2547. You can set buffer-local values for the same purpose in the variable
  2548. `org-file-properties' this by adding lines like
  2549. #+PROPERTY: NAME VALUE"
  2550. :group 'org-properties
  2551. :type '(repeat
  2552. (cons (string :tag "Property")
  2553. (string :tag "Value"))))
  2554. (defvar org-file-properties nil
  2555. "List of property/value pairs that can be inherited by any entry.
  2556. Valid for the current buffer.
  2557. This variable is populated from #+PROPERTY lines.")
  2558. (make-variable-buffer-local 'org-file-properties)
  2559. (defgroup org-agenda nil
  2560. "Options concerning agenda views in Org-mode."
  2561. :tag "Org Agenda"
  2562. :group 'org)
  2563. (defvar org-category nil
  2564. "Variable used by org files to set a category for agenda display.
  2565. Such files should use a file variable to set it, for example
  2566. # -*- mode: org; org-category: \"ELisp\"
  2567. or contain a special line
  2568. #+CATEGORY: ELisp
  2569. If the file does not specify a category, then file's base name
  2570. is used instead.")
  2571. (make-variable-buffer-local 'org-category)
  2572. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2573. (defcustom org-agenda-files nil
  2574. "The files to be used for agenda display.
  2575. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2576. \\[org-remove-file]. You can also use customize to edit the list.
  2577. If an entry is a directory, all files in that directory that are matched by
  2578. `org-agenda-file-regexp' will be part of the file list.
  2579. If the value of the variable is not a list but a single file name, then
  2580. the list of agenda files is actually stored and maintained in that file, one
  2581. agenda file per line. In this file paths can be given relative to
  2582. `org-directory'. Tilde expansion and environment variable substitution
  2583. are also made."
  2584. :group 'org-agenda
  2585. :type '(choice
  2586. (repeat :tag "List of files and directories" file)
  2587. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2588. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2589. "Regular expression to match files for `org-agenda-files'.
  2590. If any element in the list in that variable contains a directory instead
  2591. of a normal file, all files in that directory that are matched by this
  2592. regular expression will be included."
  2593. :group 'org-agenda
  2594. :type 'regexp)
  2595. (defcustom org-agenda-text-search-extra-files nil
  2596. "List of extra files to be searched by text search commands.
  2597. These files will be search in addition to the agenda files by the
  2598. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2599. Note that these files will only be searched for text search commands,
  2600. not for the other agenda views like todo lists, tag searches or the weekly
  2601. agenda. This variable is intended to list notes and possibly archive files
  2602. that should also be searched by these two commands.
  2603. In fact, if the first element in the list is the symbol `agenda-archives',
  2604. than all archive files of all agenda files will be added to the search
  2605. scope."
  2606. :group 'org-agenda
  2607. :type '(set :greedy t
  2608. (const :tag "Agenda Archives" agenda-archives)
  2609. (repeat :inline t (file))))
  2610. (if (fboundp 'defvaralias)
  2611. (defvaralias 'org-agenda-multi-occur-extra-files
  2612. 'org-agenda-text-search-extra-files))
  2613. (defcustom org-agenda-skip-unavailable-files nil
  2614. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2615. A nil value means to remove them, after a query, from the list."
  2616. :group 'org-agenda
  2617. :type 'boolean)
  2618. (defcustom org-calendar-to-agenda-key [?c]
  2619. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2620. The command `org-calendar-goto-agenda' will be bound to this key. The
  2621. default is the character `c' because then `c' can be used to switch back and
  2622. forth between agenda and calendar."
  2623. :group 'org-agenda
  2624. :type 'sexp)
  2625. (defcustom org-calendar-agenda-action-key [?k]
  2626. "The key to be installed in `calendar-mode-map' for agenda-action.
  2627. The command `org-agenda-action' will be bound to this key. The
  2628. default is the character `k' because we use the same key in the agenda."
  2629. :group 'org-agenda
  2630. :type 'sexp)
  2631. (defcustom org-calendar-insert-diary-entry-key [?i]
  2632. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2633. This option is irrelevant until `org-agenda-diary-file' has been configured
  2634. to point to an Org-mode file. When that is the case, the command
  2635. `org-agenda-diary-entry' will be bound to the key given here, by default
  2636. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2637. if you want to continue doing this, you need to change this to a different
  2638. key."
  2639. :group 'org-agenda
  2640. :type 'sexp)
  2641. (defcustom org-agenda-diary-file 'diary-file
  2642. "File to which to add new entries with the `i' key in agenda and calendar.
  2643. When this is the symbol `diary-file', the functionality in the Emacs
  2644. calendar will be used to add entries to the `diary-file'. But when this
  2645. points to a file, `org-agenda-diary-entry' will be used instead."
  2646. :group 'org-agenda
  2647. :type '(choice
  2648. (const :tag "The standard Emacs diary file" diary-file)
  2649. (file :tag "Special Org file diary entries")))
  2650. (eval-after-load "calendar"
  2651. '(progn
  2652. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2653. 'org-calendar-goto-agenda)
  2654. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2655. 'org-agenda-action)
  2656. (add-hook 'calendar-mode-hook
  2657. (lambda ()
  2658. (unless (eq org-agenda-diary-file 'diary-file)
  2659. (define-key calendar-mode-map
  2660. org-calendar-insert-diary-entry-key
  2661. 'org-agenda-diary-entry))))))
  2662. (defgroup org-latex nil
  2663. "Options for embedding LaTeX code into Org-mode."
  2664. :tag "Org LaTeX"
  2665. :group 'org)
  2666. (defcustom org-format-latex-options
  2667. '(:foreground default :background default :scale 1.0
  2668. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2669. :html-resolution 140 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2670. "Options for creating images from LaTeX fragments.
  2671. This is a property list with the following properties:
  2672. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2673. `default' means use the foreground of the default face.
  2674. :background the background color, or \"Transparent\".
  2675. `default' means use the background of the default face.
  2676. :scale a scaling factor for the size of the images, to get more pixels
  2677. :html-foreground, :html-background, :html-scale
  2678. the same numbers for HTML export.
  2679. <<<<<<< HEAD
  2680. =======
  2681. :html-resolution the resolution of images for HTML output
  2682. >>>>>>> 9330ad5b8106f0c6c0810c993db2f0def1741f63
  2683. :matchers a list indicating which matchers should be used to
  2684. find LaTeX fragments. Valid members of this list are:
  2685. \"begin\" find environments
  2686. \"$1\" find single characters surrounded by $.$
  2687. \"$\" find math expressions surrounded by $...$
  2688. \"$$\" find math expressions surrounded by $$....$$
  2689. \"\\(\" find math expressions surrounded by \\(...\\)
  2690. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2691. :group 'org-latex
  2692. :type 'plist)
  2693. (defcustom org-format-latex-signal-error t
  2694. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2695. When nil, just push out a message."
  2696. :group 'org-latex
  2697. :type 'boolean)
  2698. (defcustom org-format-latex-header "\\documentclass{article}
  2699. \\usepackage[usenames]{color}
  2700. \\usepackage{amsmath}
  2701. \\usepackage[mathscr]{eucal}
  2702. \\pagestyle{empty} % do not remove
  2703. \[PACKAGES]
  2704. \[DEFAULT-PACKAGES]
  2705. % The settings below are copied from fullpage.sty
  2706. \\setlength{\\textwidth}{\\paperwidth}
  2707. \\addtolength{\\textwidth}{-3cm}
  2708. \\setlength{\\oddsidemargin}{1.5cm}
  2709. \\addtolength{\\oddsidemargin}{-2.54cm}
  2710. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2711. \\setlength{\\textheight}{\\paperheight}
  2712. \\addtolength{\\textheight}{-\\headheight}
  2713. \\addtolength{\\textheight}{-\\headsep}
  2714. \\addtolength{\\textheight}{-\\footskip}
  2715. \\addtolength{\\textheight}{-3cm}
  2716. \\setlength{\\topmargin}{1.5cm}
  2717. \\addtolength{\\topmargin}{-2.54cm}"
  2718. "The document header used for processing LaTeX fragments.
  2719. It is imperative that this header make sure that no page number
  2720. appears on the page. The package defined in the variables
  2721. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2722. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2723. will be appended."
  2724. :group 'org-latex
  2725. :type 'string)
  2726. (defvar org-format-latex-header-extra nil)
  2727. (defun org-set-packages-alist (var val)
  2728. "Set the packages alist and make sure it has 3 elements per entry."
  2729. (set var (mapcar (lambda (x)
  2730. (if (and (consp x) (= (length x) 2))
  2731. (list (car x) (nth 1 x) t)
  2732. x))
  2733. val)))
  2734. (defun org-get-packages-alist (var)
  2735. "Get the packages alist and make sure it has 3 elements per entry."
  2736. (mapcar (lambda (x)
  2737. (if (and (consp x) (= (length x) 2))
  2738. (list (car x) (nth 1 x) t)
  2739. x))
  2740. (default-value var)))
  2741. ;; The following variables are defined here because is it also used
  2742. ;; when formatting latex fragments. Originally it was part of the
  2743. ;; LaTeX exporter, which is why the name includes "export".
  2744. (defcustom org-export-latex-default-packages-alist
  2745. '(("AUTO" "inputenc" t)
  2746. ("T1" "fontenc" t)
  2747. ("" "fixltx2e" nil)
  2748. ("" "graphicx" t)
  2749. ("" "longtable" nil)
  2750. ("" "float" nil)
  2751. ("" "wrapfig" nil)
  2752. ("" "soul" t)
  2753. ("" "t1enc" t)
  2754. ("" "textcomp" t)
  2755. ("" "marvosym" t)
  2756. ("" "wasysym" t)
  2757. ("" "latexsym" t)
  2758. ("" "amssymb" t)
  2759. ("" "hyperref" nil)
  2760. "\\tolerance=1000"
  2761. )
  2762. "Alist of default packages to be inserted in the header.
  2763. Change this only if one of the packages here causes an incompatibility
  2764. with another package you are using.
  2765. The packages in this list are needed by one part or another of Org-mode
  2766. to function properly.
  2767. - inputenc, fontenc, t1enc: for basic font and character selection
  2768. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2769. for interpreting the entities in `org-entities'. You can skip some of these
  2770. packages if you don't use any of the symbols in it.
  2771. - graphicx: for including images
  2772. - float, wrapfig: for figure placement
  2773. - longtable: for long tables
  2774. - hyperref: for cross references
  2775. Therefore you should not modify this variable unless you know what you
  2776. are doing. The one reason to change it anyway is that you might be loading
  2777. some other package that conflicts with one of the default packages.
  2778. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2779. If SNIPPET-FLAG is t, the package also needs to be included when
  2780. compiling LaTeX snippets into images for inclusion into HTML."
  2781. :group 'org-export-latex
  2782. :set 'org-set-packages-alist
  2783. :get 'org-get-packages-alist
  2784. :type '(repeat
  2785. (choice
  2786. (list :tag "options/package pair"
  2787. (string :tag "options")
  2788. (string :tag "package")
  2789. (boolean :tag "Snippet"))
  2790. (string :tag "A line of LaTeX"))))
  2791. (defcustom org-export-latex-packages-alist nil
  2792. "Alist of packages to be inserted in every LaTeX header.
  2793. These will be inserted after `org-export-latex-default-packages-alist'.
  2794. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2795. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2796. turning LaTeX snippets into images for inclusion into HTML.
  2797. Make sure that you only list packages here which:
  2798. - you want in every file
  2799. - do not conflict with the default packages in
  2800. `org-export-latex-default-packages-alist'
  2801. - do not conflict with the setup in `org-format-latex-header'."
  2802. :group 'org-export-latex
  2803. :set 'org-set-packages-alist
  2804. :get 'org-get-packages-alist
  2805. :type '(repeat
  2806. (choice
  2807. (list :tag "options/package pair"
  2808. (string :tag "options")
  2809. (string :tag "package")
  2810. (boolean :tag "Snippet"))
  2811. (string :tag "A line of LaTeX"))))
  2812. (defgroup org-appearance nil
  2813. "Settings for Org-mode appearance."
  2814. :tag "Org Appearance"
  2815. :group 'org)
  2816. (defcustom org-level-color-stars-only nil
  2817. "Non-nil means fontify only the stars in each headline.
  2818. When nil, the entire headline is fontified.
  2819. Changing it requires restart of `font-lock-mode' to become effective
  2820. also in regions already fontified."
  2821. :group 'org-appearance
  2822. :type 'boolean)
  2823. (defcustom org-hide-leading-stars nil
  2824. "Non-nil means hide the first N-1 stars in a headline.
  2825. This works by using the face `org-hide' for these stars. This
  2826. face is white for a light background, and black for a dark
  2827. background. You may have to customize the face `org-hide' to
  2828. make this work.
  2829. Changing it requires restart of `font-lock-mode' to become effective
  2830. also in regions already fontified.
  2831. You may also set this on a per-file basis by adding one of the following
  2832. lines to the buffer:
  2833. #+STARTUP: hidestars
  2834. #+STARTUP: showstars"
  2835. :group 'org-appearance
  2836. :type 'boolean)
  2837. (defcustom org-hidden-keywords nil
  2838. "List of keywords that should be hidden when typed in the org buffer.
  2839. For example, add #+TITLE to this list in order to make the
  2840. document title appear in the buffer without the initial #+TITLE:
  2841. keyword."
  2842. :group 'org-appearance
  2843. :type '(set (const :tag "#+AUTHOR" author)
  2844. (const :tag "#+DATE" date)
  2845. (const :tag "#+EMAIL" email)
  2846. (const :tag "#+TITLE" title)))
  2847. (defcustom org-fontify-done-headline nil
  2848. "Non-nil means change the face of a headline if it is marked DONE.
  2849. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2850. When this is non-nil, the headline after the keyword is set to the
  2851. `org-headline-done' as an additional indication."
  2852. :group 'org-appearance
  2853. :type 'boolean)
  2854. (defcustom org-fontify-emphasized-text t
  2855. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2856. Changing this variable requires a restart of Emacs to take effect."
  2857. :group 'org-appearance
  2858. :type 'boolean)
  2859. (defcustom org-fontify-whole-heading-line nil
  2860. "Non-nil means fontify the whole line for headings.
  2861. This is useful when setting a background color for the
  2862. org-level-* faces."
  2863. :group 'org-appearance
  2864. :type 'boolean)
  2865. (defcustom org-highlight-latex-fragments-and-specials nil
  2866. "Non-nil means fontify what is treated specially by the exporters."
  2867. :group 'org-appearance
  2868. :type 'boolean)
  2869. (defcustom org-hide-emphasis-markers nil
  2870. "Non-nil mean font-lock should hide the emphasis marker characters."
  2871. :group 'org-appearance
  2872. :type 'boolean)
  2873. (defcustom org-pretty-entities nil
  2874. "Non-nil means show entities as UTF8 characters.
  2875. When nil, the \\name form remains in the buffer."
  2876. :group 'org-appearance
  2877. :type 'boolean)
  2878. (defcustom org-pretty-entities-include-sub-superscripts t
  2879. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2880. :group 'org-appearance
  2881. :type 'boolean)
  2882. (defvar org-emph-re nil
  2883. "Regular expression for matching emphasis.
  2884. After a match, the match groups contain these elements:
  2885. 1 The character before the proper match, or empty at beginning of line
  2886. 2 The proper match, including the leading and trailing markers
  2887. 3 The leading marker like * or /, indicating the type of highlighting
  2888. 4 The text between the emphasis markers, not including the markers
  2889. 5 The character after the match, empty at the end of a line")
  2890. (defvar org-verbatim-re nil
  2891. "Regular expression for matching verbatim text.")
  2892. (defvar org-emphasis-regexp-components) ; defined just below
  2893. (defvar org-emphasis-alist) ; defined just below
  2894. (defun org-set-emph-re (var val)
  2895. "Set variable and compute the emphasis regular expression."
  2896. (set var val)
  2897. (when (and (boundp 'org-emphasis-alist)
  2898. (boundp 'org-emphasis-regexp-components)
  2899. org-emphasis-alist org-emphasis-regexp-components)
  2900. (let* ((e org-emphasis-regexp-components)
  2901. (pre (car e))
  2902. (post (nth 1 e))
  2903. (border (nth 2 e))
  2904. (body (nth 3 e))
  2905. (nl (nth 4 e))
  2906. (body1 (concat body "*?"))
  2907. (markers (mapconcat 'car org-emphasis-alist ""))
  2908. (vmarkers (mapconcat
  2909. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2910. org-emphasis-alist "")))
  2911. ;; make sure special characters appear at the right position in the class
  2912. (if (string-match "\\^" markers)
  2913. (setq markers (concat (replace-match "" t t markers) "^")))
  2914. (if (string-match "-" markers)
  2915. (setq markers (concat (replace-match "" t t markers) "-")))
  2916. (if (string-match "\\^" vmarkers)
  2917. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2918. (if (string-match "-" vmarkers)
  2919. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2920. (if (> nl 0)
  2921. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2922. (int-to-string nl) "\\}")))
  2923. ;; Make the regexp
  2924. (setq org-emph-re
  2925. (concat "\\([" pre "]\\|^\\)"
  2926. "\\("
  2927. "\\([" markers "]\\)"
  2928. "\\("
  2929. "[^" border "]\\|"
  2930. "[^" border "]"
  2931. body1
  2932. "[^" border "]"
  2933. "\\)"
  2934. "\\3\\)"
  2935. "\\([" post "]\\|$\\)"))
  2936. (setq org-verbatim-re
  2937. (concat "\\([" pre "]\\|^\\)"
  2938. "\\("
  2939. "\\([" vmarkers "]\\)"
  2940. "\\("
  2941. "[^" border "]\\|"
  2942. "[^" border "]"
  2943. body1
  2944. "[^" border "]"
  2945. "\\)"
  2946. "\\3\\)"
  2947. "\\([" post "]\\|$\\)")))))
  2948. (defcustom org-emphasis-regexp-components
  2949. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2950. "Components used to build the regular expression for emphasis.
  2951. This is a list with 6 entries. Terminology: In an emphasis string
  2952. like \" *strong word* \", we call the initial space PREMATCH, the final
  2953. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2954. and \"trong wor\" is the body. The different components in this variable
  2955. specify what is allowed/forbidden in each part:
  2956. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2957. post Chars allowed as postmatch. End of line will be allowed too.
  2958. border The chars *forbidden* as border characters.
  2959. body-regexp A regexp like \".\" to match a body character. Don't use
  2960. non-shy groups here, and don't allow newline here.
  2961. newline The maximum number of newlines allowed in an emphasis exp.
  2962. Use customize to modify this, or restart Emacs after changing it."
  2963. :group 'org-appearance
  2964. :set 'org-set-emph-re
  2965. :type '(list
  2966. (sexp :tag "Allowed chars in pre ")
  2967. (sexp :tag "Allowed chars in post ")
  2968. (sexp :tag "Forbidden chars in border ")
  2969. (sexp :tag "Regexp for body ")
  2970. (integer :tag "number of newlines allowed")
  2971. (option (boolean :tag "Please ignore this button"))))
  2972. (defcustom org-emphasis-alist
  2973. `(("*" bold "<b>" "</b>")
  2974. ("/" italic "<i>" "</i>")
  2975. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2976. ("=" org-code "<code>" "</code>" verbatim)
  2977. ("~" org-verbatim "<code>" "</code>" verbatim)
  2978. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2979. "<del>" "</del>")
  2980. )
  2981. "Special syntax for emphasized text.
  2982. Text starting and ending with a special character will be emphasized, for
  2983. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2984. characters, the face to be used by font-lock for highlighting in Org-mode
  2985. Emacs buffers, and the HTML tags to be used for this.
  2986. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2987. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  2988. Use customize to modify this, or restart Emacs after changing it."
  2989. :group 'org-appearance
  2990. :set 'org-set-emph-re
  2991. :type '(repeat
  2992. (list
  2993. (string :tag "Marker character")
  2994. (choice
  2995. (face :tag "Font-lock-face")
  2996. (plist :tag "Face property list"))
  2997. (string :tag "HTML start tag")
  2998. (string :tag "HTML end tag")
  2999. (option (const verbatim)))))
  3000. (defvar org-protecting-blocks
  3001. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3002. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3003. This is needed for font-lock setup.")
  3004. ;;; Miscellaneous options
  3005. (defgroup org-completion nil
  3006. "Completion in Org-mode."
  3007. :tag "Org Completion"
  3008. :group 'org)
  3009. (defcustom org-completion-use-ido nil
  3010. "Non-nil means use ido completion wherever possible.
  3011. Note that `ido-mode' must be active for this variable to be relevant.
  3012. If you decide to turn this variable on, you might well want to turn off
  3013. `org-outline-path-complete-in-steps'.
  3014. See also `org-completion-use-iswitchb'."
  3015. :group 'org-completion
  3016. :type 'boolean)
  3017. (defcustom org-completion-use-iswitchb nil
  3018. "Non-nil means use iswitchb completion wherever possible.
  3019. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3020. If you decide to turn this variable on, you might well want to turn off
  3021. `org-outline-path-complete-in-steps'.
  3022. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3023. :group 'org-completion
  3024. :type 'boolean)
  3025. (defcustom org-completion-fallback-command 'hippie-expand
  3026. "The expansion command called by \\[org-complete] in normal context.
  3027. Normal means no org-mode-specific context."
  3028. :group 'org-completion
  3029. :type 'function)
  3030. ;;; Functions and variables from their packages
  3031. ;; Declared here to avoid compiler warnings
  3032. ;; XEmacs only
  3033. (defvar outline-mode-menu-heading)
  3034. (defvar outline-mode-menu-show)
  3035. (defvar outline-mode-menu-hide)
  3036. (defvar zmacs-regions) ; XEmacs regions
  3037. ;; Emacs only
  3038. (defvar mark-active)
  3039. ;; Various packages
  3040. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3041. (declare-function calendar-forward-day "cal-move" (arg))
  3042. (declare-function calendar-goto-date "cal-move" (date))
  3043. (declare-function calendar-goto-today "cal-move" ())
  3044. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3045. (defvar calc-embedded-close-formula)
  3046. (defvar calc-embedded-open-formula)
  3047. (declare-function cdlatex-tab "ext:cdlatex" ())
  3048. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3049. (defvar font-lock-unfontify-region-function)
  3050. (declare-function iswitchb-read-buffer "iswitchb"
  3051. (prompt &optional default require-match start matches-set))
  3052. (defvar iswitchb-temp-buflist)
  3053. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3054. (defvar org-agenda-tags-todo-honor-ignore-options)
  3055. (declare-function org-agenda-skip "org-agenda" ())
  3056. (declare-function
  3057. org-format-agenda-item "org-agenda"
  3058. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3059. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3060. (declare-function org-agenda-change-all-lines "org-agenda"
  3061. (newhead hdmarker &optional fixface just-this))
  3062. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3063. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3064. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3065. (beg end))
  3066. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3067. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3068. "org-agenda" (&optional end))
  3069. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3070. (declare-function org-indent-mode "org-indent" (&optional arg))
  3071. (declare-function parse-time-string "parse-time" (string))
  3072. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3073. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3074. (defvar remember-data-file)
  3075. (defvar texmathp-why)
  3076. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3077. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3078. (defvar w3m-current-url)
  3079. (defvar w3m-current-title)
  3080. (defvar org-latex-regexps)
  3081. ;;; Autoload and prepare some org modules
  3082. ;; Some table stuff that needs to be defined here, because it is used
  3083. ;; by the functions setting up org-mode or checking for table context.
  3084. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3085. "Detect an org-type or table-type table.")
  3086. (defconst org-table-line-regexp "^[ \t]*|"
  3087. "Detect an org-type table line.")
  3088. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3089. "Detect an org-type table line.")
  3090. (defconst org-table-hline-regexp "^[ \t]*|-"
  3091. "Detect an org-type table hline.")
  3092. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3093. "Detect a table-type table hline.")
  3094. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3095. "Detect the first line outside a table when searching from within it.
  3096. This works for both table types.")
  3097. ;; Autoload the functions in org-table.el that are needed by functions here.
  3098. (eval-and-compile
  3099. (org-autoload "org-table"
  3100. '(org-table-align org-table-begin org-table-blank-field
  3101. org-table-convert org-table-convert-region org-table-copy-down
  3102. org-table-copy-region org-table-create
  3103. org-table-create-or-convert-from-region
  3104. org-table-create-with-table.el org-table-current-dline
  3105. org-table-cut-region org-table-delete-column org-table-edit-field
  3106. org-table-edit-formulas org-table-end org-table-eval-formula
  3107. org-table-export org-table-field-info
  3108. org-table-get-stored-formulas org-table-goto-column
  3109. org-table-hline-and-move org-table-import org-table-insert-column
  3110. org-table-insert-hline org-table-insert-row org-table-iterate
  3111. org-table-justify-field-maybe org-table-kill-row
  3112. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3113. org-table-move-column org-table-move-column-left
  3114. org-table-move-column-right org-table-move-row
  3115. org-table-move-row-down org-table-move-row-up
  3116. org-table-next-field org-table-next-row org-table-paste-rectangle
  3117. org-table-previous-field org-table-recalculate
  3118. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3119. org-table-toggle-coordinate-overlays
  3120. org-table-toggle-formula-debugger org-table-wrap-region
  3121. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3122. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3123. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3124. (defun org-at-table-p (&optional table-type)
  3125. "Return t if the cursor is inside an org-type table.
  3126. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3127. (if org-enable-table-editor
  3128. (save-excursion
  3129. (beginning-of-line 1)
  3130. (looking-at (if table-type org-table-any-line-regexp
  3131. org-table-line-regexp)))
  3132. nil))
  3133. (defsubst org-table-p () (org-at-table-p))
  3134. (defun org-at-table.el-p ()
  3135. "Return t if and only if we are at a table.el table."
  3136. (and (org-at-table-p 'any)
  3137. (save-excursion
  3138. (goto-char (org-table-begin 'any))
  3139. (looking-at org-table1-hline-regexp))))
  3140. (defun org-table-recognize-table.el ()
  3141. "If there is a table.el table nearby, recognize it and move into it."
  3142. (if org-table-tab-recognizes-table.el
  3143. (if (org-at-table.el-p)
  3144. (progn
  3145. (beginning-of-line 1)
  3146. (if (looking-at org-table-dataline-regexp)
  3147. nil
  3148. (if (looking-at org-table1-hline-regexp)
  3149. (progn
  3150. (beginning-of-line 2)
  3151. (if (looking-at org-table-any-border-regexp)
  3152. (beginning-of-line -1)))))
  3153. (if (re-search-forward "|" (org-table-end t) t)
  3154. (progn
  3155. (require 'table)
  3156. (if (table--at-cell-p (point))
  3157. t
  3158. (message "recognizing table.el table...")
  3159. (table-recognize-table)
  3160. (message "recognizing table.el table...done")))
  3161. (error "This should not happen"))
  3162. t)
  3163. nil)
  3164. nil))
  3165. (defun org-at-table-hline-p ()
  3166. "Return t if the cursor is inside a hline in a table."
  3167. (if org-enable-table-editor
  3168. (save-excursion
  3169. (beginning-of-line 1)
  3170. (looking-at org-table-hline-regexp))
  3171. nil))
  3172. (defvar org-table-clean-did-remove-column nil)
  3173. (defun org-table-map-tables (function &optional quietly)
  3174. "Apply FUNCTION to the start of all tables in the buffer."
  3175. (save-excursion
  3176. (save-restriction
  3177. (widen)
  3178. (goto-char (point-min))
  3179. (while (re-search-forward org-table-any-line-regexp nil t)
  3180. (unless quietly
  3181. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3182. (beginning-of-line 1)
  3183. (when (looking-at org-table-line-regexp)
  3184. (save-excursion (funcall function))
  3185. (or (looking-at org-table-line-regexp)
  3186. (forward-char 1)))
  3187. (re-search-forward org-table-any-border-regexp nil 1))))
  3188. (unless quietly (message "Mapping tables: done")))
  3189. ;; Declare and autoload functions from org-exp.el & Co
  3190. (declare-function org-default-export-plist "org-exp")
  3191. (declare-function org-infile-export-plist "org-exp")
  3192. (declare-function org-get-current-options "org-exp")
  3193. (eval-and-compile
  3194. (org-autoload "org-exp"
  3195. '(org-export org-export-visible
  3196. org-insert-export-options-template
  3197. org-table-clean-before-export))
  3198. (org-autoload "org-ascii"
  3199. '(org-export-as-ascii org-export-ascii-preprocess
  3200. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3201. org-export-region-as-ascii))
  3202. (org-autoload "org-latex"
  3203. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3204. org-replace-region-by-latex org-export-region-as-latex
  3205. org-export-as-latex org-export-as-pdf
  3206. org-export-as-pdf-and-open))
  3207. (org-autoload "org-html"
  3208. '(org-export-as-html-and-open
  3209. org-export-as-html-batch org-export-as-html-to-buffer
  3210. org-replace-region-by-html org-export-region-as-html
  3211. org-export-as-html))
  3212. (org-autoload "org-docbook"
  3213. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3214. org-replace-region-by-docbook org-export-region-as-docbook
  3215. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3216. org-export-as-docbook))
  3217. (org-autoload "org-icalendar"
  3218. '(org-export-icalendar-this-file
  3219. org-export-icalendar-all-agenda-files
  3220. org-export-icalendar-combine-agenda-files))
  3221. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3222. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3223. ;; Declare and autoload functions from org-agenda.el
  3224. (eval-and-compile
  3225. (org-autoload "org-agenda"
  3226. '(org-agenda org-agenda-list org-search-view
  3227. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3228. org-diary org-agenda-to-appt
  3229. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3230. ;; Autoload org-remember
  3231. (eval-and-compile
  3232. (org-autoload "org-remember"
  3233. '(org-remember-insinuate org-remember-annotation
  3234. org-remember-apply-template org-remember org-remember-handler)))
  3235. (eval-and-compile
  3236. (org-autoload "org-capture"
  3237. '(org-capture org-capture-insert-template-here
  3238. org-capture-import-remember-templates)))
  3239. ;; Autoload org-clock.el
  3240. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3241. (beg end))
  3242. (declare-function org-clock-update-mode-line "org-clock" ())
  3243. (declare-function org-resolve-clocks "org-clock"
  3244. (&optional also-non-dangling-p prompt last-valid))
  3245. (defvar org-clock-start-time)
  3246. (defvar org-clock-marker (make-marker)
  3247. "Marker recording the last clock-in.")
  3248. (defvar org-clock-hd-marker (make-marker)
  3249. "Marker recording the last clock-in, but the headline position.")
  3250. (defvar org-clock-heading ""
  3251. "The heading of the current clock entry.")
  3252. (defun org-clock-is-active ()
  3253. "Return non-nil if clock is currently running.
  3254. The return value is actually the clock marker."
  3255. (marker-buffer org-clock-marker))
  3256. (eval-and-compile
  3257. (org-autoload
  3258. "org-clock"
  3259. '(org-clock-in org-clock-out org-clock-cancel
  3260. org-clock-goto org-clock-sum org-clock-display
  3261. org-clock-remove-overlays org-clock-report
  3262. org-clocktable-shift org-dblock-write:clocktable
  3263. org-get-clocktable org-resolve-clocks)))
  3264. (defun org-clock-update-time-maybe ()
  3265. "If this is a CLOCK line, update it and return t.
  3266. Otherwise, return nil."
  3267. (interactive)
  3268. (save-excursion
  3269. (beginning-of-line 1)
  3270. (skip-chars-forward " \t")
  3271. (when (looking-at org-clock-string)
  3272. (let ((re (concat "[ \t]*" org-clock-string
  3273. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3274. "\\([ \t]*=>.*\\)?\\)?"))
  3275. ts te h m s neg)
  3276. (cond
  3277. ((not (looking-at re))
  3278. nil)
  3279. ((not (match-end 2))
  3280. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3281. (> org-clock-marker (point))
  3282. (<= org-clock-marker (point-at-eol)))
  3283. ;; The clock is running here
  3284. (setq org-clock-start-time
  3285. (apply 'encode-time
  3286. (org-parse-time-string (match-string 1))))
  3287. (org-clock-update-mode-line)))
  3288. (t
  3289. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3290. (end-of-line 1)
  3291. (setq ts (match-string 1)
  3292. te (match-string 3))
  3293. (setq s (- (org-float-time
  3294. (apply 'encode-time (org-parse-time-string te)))
  3295. (org-float-time
  3296. (apply 'encode-time (org-parse-time-string ts))))
  3297. neg (< s 0)
  3298. s (abs s)
  3299. h (floor (/ s 3600))
  3300. s (- s (* 3600 h))
  3301. m (floor (/ s 60))
  3302. s (- s (* 60 s)))
  3303. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3304. t))))))
  3305. (defun org-check-running-clock ()
  3306. "Check if the current buffer contains the running clock.
  3307. If yes, offer to stop it and to save the buffer with the changes."
  3308. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3309. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3310. (buffer-name))))
  3311. (org-clock-out)
  3312. (when (y-or-n-p "Save changed buffer?")
  3313. (save-buffer))))
  3314. (defun org-clocktable-try-shift (dir n)
  3315. "Check if this line starts a clock table, if yes, shift the time block."
  3316. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3317. (org-clocktable-shift dir n)))
  3318. ;; Autoload org-timer.el
  3319. (eval-and-compile
  3320. (org-autoload
  3321. "org-timer"
  3322. '(org-timer-start org-timer org-timer-item
  3323. org-timer-change-times-in-region
  3324. org-timer-set-timer
  3325. org-timer-reset-timers
  3326. org-timer-show-remaining-time)))
  3327. ;; Autoload org-feed.el
  3328. (eval-and-compile
  3329. (org-autoload
  3330. "org-feed"
  3331. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3332. ;; Autoload org-indent.el
  3333. ;; Define the variable already here, to make sure we have it.
  3334. (defvar org-indent-mode nil
  3335. "Non-nil if Org-Indent mode is enabled.
  3336. Use the command `org-indent-mode' to change this variable.")
  3337. (eval-and-compile
  3338. (org-autoload
  3339. "org-indent"
  3340. '(org-indent-mode)))
  3341. ;; Autoload org-mobile.el
  3342. (eval-and-compile
  3343. (org-autoload
  3344. "org-mobile"
  3345. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3346. ;; Autoload archiving code
  3347. ;; The stuff that is needed for cycling and tags has to be defined here.
  3348. (defgroup org-archive nil
  3349. "Options concerning archiving in Org-mode."
  3350. :tag "Org Archive"
  3351. :group 'org-structure)
  3352. (defcustom org-archive-location "%s_archive::"
  3353. "The location where subtrees should be archived.
  3354. The value of this variable is a string, consisting of two parts,
  3355. separated by a double-colon. The first part is a filename and
  3356. the second part is a headline.
  3357. When the filename is omitted, archiving happens in the same file.
  3358. %s in the filename will be replaced by the current file
  3359. name (without the directory part). Archiving to a different file
  3360. is useful to keep archived entries from contributing to the
  3361. Org-mode Agenda.
  3362. The archived entries will be filed as subtrees of the specified
  3363. headline. When the headline is omitted, the subtrees are simply
  3364. filed away at the end of the file, as top-level entries. Also in
  3365. the heading you can use %s to represent the file name, this can be
  3366. useful when using the same archive for a number of different files.
  3367. Here are a few examples:
  3368. \"%s_archive::\"
  3369. If the current file is Projects.org, archive in file
  3370. Projects.org_archive, as top-level trees. This is the default.
  3371. \"::* Archived Tasks\"
  3372. Archive in the current file, under the top-level headline
  3373. \"* Archived Tasks\".
  3374. \"~/org/archive.org::\"
  3375. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3376. \"~/org/archive.org::From %s\"
  3377. Archive in file ~/org/archive.org (absolute path), under headlines
  3378. \"From FILENAME\" where file name is the current file name.
  3379. \"basement::** Finished Tasks\"
  3380. Archive in file ./basement (relative path), as level 3 trees
  3381. below the level 2 heading \"** Finished Tasks\".
  3382. You may set this option on a per-file basis by adding to the buffer a
  3383. line like
  3384. #+ARCHIVE: basement::** Finished Tasks
  3385. You may also define it locally for a subtree by setting an ARCHIVE property
  3386. in the entry. If such a property is found in an entry, or anywhere up
  3387. the hierarchy, it will be used."
  3388. :group 'org-archive
  3389. :type 'string)
  3390. (defcustom org-archive-tag "ARCHIVE"
  3391. "The tag that marks a subtree as archived.
  3392. An archived subtree does not open during visibility cycling, and does
  3393. not contribute to the agenda listings.
  3394. After changing this, font-lock must be restarted in the relevant buffers to
  3395. get the proper fontification."
  3396. :group 'org-archive
  3397. :group 'org-keywords
  3398. :type 'string)
  3399. (defcustom org-agenda-skip-archived-trees t
  3400. "Non-nil means the agenda will skip any items located in archived trees.
  3401. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3402. variable is no longer recommended, you should leave it at the value t.
  3403. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3404. :group 'org-archive
  3405. :group 'org-agenda-skip
  3406. :type 'boolean)
  3407. (defcustom org-columns-skip-archived-trees t
  3408. "Non-nil means ignore archived trees when creating column view."
  3409. :group 'org-archive
  3410. :group 'org-properties
  3411. :type 'boolean)
  3412. (defcustom org-cycle-open-archived-trees nil
  3413. "Non-nil means `org-cycle' will open archived trees.
  3414. An archived tree is a tree marked with the tag ARCHIVE.
  3415. When nil, archived trees will stay folded. You can still open them with
  3416. normal outline commands like `show-all', but not with the cycling commands."
  3417. :group 'org-archive
  3418. :group 'org-cycle
  3419. :type 'boolean)
  3420. (defcustom org-sparse-tree-open-archived-trees nil
  3421. "Non-nil means sparse tree construction shows matches in archived trees.
  3422. When nil, matches in these trees are highlighted, but the trees are kept in
  3423. collapsed state."
  3424. :group 'org-archive
  3425. :group 'org-sparse-trees
  3426. :type 'boolean)
  3427. (defun org-cycle-hide-archived-subtrees (state)
  3428. "Re-hide all archived subtrees after a visibility state change."
  3429. (when (and (not org-cycle-open-archived-trees)
  3430. (not (memq state '(overview folded))))
  3431. (save-excursion
  3432. (let* ((globalp (memq state '(contents all)))
  3433. (beg (if globalp (point-min) (point)))
  3434. (end (if globalp (point-max) (org-end-of-subtree t))))
  3435. (org-hide-archived-subtrees beg end)
  3436. (goto-char beg)
  3437. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3438. (message "%s" (substitute-command-keys
  3439. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3440. (defun org-force-cycle-archived ()
  3441. "Cycle subtree even if it is archived."
  3442. (interactive)
  3443. (setq this-command 'org-cycle)
  3444. (let ((org-cycle-open-archived-trees t))
  3445. (call-interactively 'org-cycle)))
  3446. (defun org-hide-archived-subtrees (beg end)
  3447. "Re-hide all archived subtrees after a visibility state change."
  3448. (save-excursion
  3449. (let* ((re (concat ":" org-archive-tag ":")))
  3450. (goto-char beg)
  3451. (while (re-search-forward re end t)
  3452. (when (org-on-heading-p)
  3453. (org-flag-subtree t)
  3454. (org-end-of-subtree t))))))
  3455. (defun org-flag-subtree (flag)
  3456. (save-excursion
  3457. (org-back-to-heading t)
  3458. (outline-end-of-heading)
  3459. (outline-flag-region (point)
  3460. (progn (org-end-of-subtree t) (point))
  3461. flag)))
  3462. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3463. (eval-and-compile
  3464. (org-autoload "org-archive"
  3465. '(org-add-archive-files org-archive-subtree
  3466. org-archive-to-archive-sibling org-toggle-archive-tag
  3467. org-archive-subtree-default
  3468. org-archive-subtree-default-with-confirmation)))
  3469. ;; Autoload Column View Code
  3470. (declare-function org-columns-number-to-string "org-colview")
  3471. (declare-function org-columns-get-format-and-top-level "org-colview")
  3472. (declare-function org-columns-compute "org-colview")
  3473. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3474. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3475. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3476. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3477. ;; Autoload ID code
  3478. (declare-function org-id-store-link "org-id")
  3479. (declare-function org-id-locations-load "org-id")
  3480. (declare-function org-id-locations-save "org-id")
  3481. (defvar org-id-track-globally)
  3482. (org-autoload "org-id"
  3483. '(org-id-get-create org-id-new org-id-copy org-id-get
  3484. org-id-get-with-outline-path-completion
  3485. org-id-get-with-outline-drilling org-id-store-link
  3486. org-id-goto org-id-find org-id-store-link))
  3487. ;; Autoload Plotting Code
  3488. (org-autoload "org-plot"
  3489. '(org-plot/gnuplot))
  3490. ;;; Variables for pre-computed regular expressions, all buffer local
  3491. (defvar org-drawer-regexp nil
  3492. "Matches first line of a hidden block.")
  3493. (make-variable-buffer-local 'org-drawer-regexp)
  3494. (defvar org-todo-regexp nil
  3495. "Matches any of the TODO state keywords.")
  3496. (make-variable-buffer-local 'org-todo-regexp)
  3497. (defvar org-not-done-regexp nil
  3498. "Matches any of the TODO state keywords except the last one.")
  3499. (make-variable-buffer-local 'org-not-done-regexp)
  3500. (defvar org-not-done-heading-regexp nil
  3501. "Matches a TODO headline that is not done.")
  3502. (make-variable-buffer-local 'org-not-done-regexp)
  3503. (defvar org-todo-line-regexp nil
  3504. "Matches a headline and puts TODO state into group 2 if present.")
  3505. (make-variable-buffer-local 'org-todo-line-regexp)
  3506. (defvar org-complex-heading-regexp nil
  3507. "Matches a headline and puts everything into groups:
  3508. group 1: the stars
  3509. group 2: The todo keyword, maybe
  3510. group 3: Priority cookie
  3511. group 4: True headline
  3512. group 5: Tags")
  3513. (make-variable-buffer-local 'org-complex-heading-regexp)
  3514. (defvar org-complex-heading-regexp-format nil)
  3515. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3516. (defvar org-todo-line-tags-regexp nil
  3517. "Matches a headline and puts TODO state into group 2 if present.
  3518. Also put tags into group 4 if tags are present.")
  3519. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3520. (defvar org-nl-done-regexp nil
  3521. "Matches newline followed by a headline with the DONE keyword.")
  3522. (make-variable-buffer-local 'org-nl-done-regexp)
  3523. (defvar org-looking-at-done-regexp nil
  3524. "Matches the DONE keyword a point.")
  3525. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3526. (defvar org-ds-keyword-length 12
  3527. "Maximum length of the Deadline and SCHEDULED keywords.")
  3528. (make-variable-buffer-local 'org-ds-keyword-length)
  3529. (defvar org-deadline-regexp nil
  3530. "Matches the DEADLINE keyword.")
  3531. (make-variable-buffer-local 'org-deadline-regexp)
  3532. (defvar org-deadline-time-regexp nil
  3533. "Matches the DEADLINE keyword together with a time stamp.")
  3534. (make-variable-buffer-local 'org-deadline-time-regexp)
  3535. (defvar org-deadline-line-regexp nil
  3536. "Matches the DEADLINE keyword and the rest of the line.")
  3537. (make-variable-buffer-local 'org-deadline-line-regexp)
  3538. (defvar org-scheduled-regexp nil
  3539. "Matches the SCHEDULED keyword.")
  3540. (make-variable-buffer-local 'org-scheduled-regexp)
  3541. (defvar org-scheduled-time-regexp nil
  3542. "Matches the SCHEDULED keyword together with a time stamp.")
  3543. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3544. (defvar org-closed-time-regexp nil
  3545. "Matches the CLOSED keyword together with a time stamp.")
  3546. (make-variable-buffer-local 'org-closed-time-regexp)
  3547. (defvar org-keyword-time-regexp nil
  3548. "Matches any of the 4 keywords, together with the time stamp.")
  3549. (make-variable-buffer-local 'org-keyword-time-regexp)
  3550. (defvar org-keyword-time-not-clock-regexp nil
  3551. "Matches any of the 3 keywords, together with the time stamp.")
  3552. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3553. (defvar org-maybe-keyword-time-regexp nil
  3554. "Matches a timestamp, possibly preceded by a keyword.")
  3555. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3556. (defvar org-planning-or-clock-line-re nil
  3557. "Matches a line with planning or clock info.")
  3558. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3559. (defvar org-all-time-keywords nil
  3560. "List of time keywords.")
  3561. (make-variable-buffer-local 'org-all-time-keywords)
  3562. (defconst org-plain-time-of-day-regexp
  3563. (concat
  3564. "\\(\\<[012]?[0-9]"
  3565. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3566. "\\(--?"
  3567. "\\(\\<[012]?[0-9]"
  3568. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3569. "\\)?")
  3570. "Regular expression to match a plain time or time range.
  3571. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3572. groups carry important information:
  3573. 0 the full match
  3574. 1 the first time, range or not
  3575. 8 the second time, if it is a range.")
  3576. (defconst org-plain-time-extension-regexp
  3577. (concat
  3578. "\\(\\<[012]?[0-9]"
  3579. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3580. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3581. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3582. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3583. groups carry important information:
  3584. 0 the full match
  3585. 7 hours of duration
  3586. 9 minutes of duration")
  3587. (defconst org-stamp-time-of-day-regexp
  3588. (concat
  3589. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3590. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3591. "\\(--?"
  3592. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3593. "Regular expression to match a timestamp time or time range.
  3594. After a match, the following groups carry important information:
  3595. 0 the full match
  3596. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3597. 2 the first time, range or not
  3598. 4 the second time, if it is a range.")
  3599. (defconst org-startup-options
  3600. '(("fold" org-startup-folded t)
  3601. ("overview" org-startup-folded t)
  3602. ("nofold" org-startup-folded nil)
  3603. ("showall" org-startup-folded nil)
  3604. ("showeverything" org-startup-folded showeverything)
  3605. ("content" org-startup-folded content)
  3606. ("indent" org-startup-indented t)
  3607. ("noindent" org-startup-indented nil)
  3608. ("hidestars" org-hide-leading-stars t)
  3609. ("showstars" org-hide-leading-stars nil)
  3610. ("odd" org-odd-levels-only t)
  3611. ("oddeven" org-odd-levels-only nil)
  3612. ("align" org-startup-align-all-tables t)
  3613. ("noalign" org-startup-align-all-tables nil)
  3614. ("customtime" org-display-custom-times t)
  3615. ("logdone" org-log-done time)
  3616. ("lognotedone" org-log-done note)
  3617. ("nologdone" org-log-done nil)
  3618. ("lognoteclock-out" org-log-note-clock-out t)
  3619. ("nolognoteclock-out" org-log-note-clock-out nil)
  3620. ("logrepeat" org-log-repeat state)
  3621. ("lognoterepeat" org-log-repeat note)
  3622. ("nologrepeat" org-log-repeat nil)
  3623. ("logreschedule" org-log-reschedule time)
  3624. ("lognotereschedule" org-log-reschedule note)
  3625. ("nologreschedule" org-log-reschedule nil)
  3626. ("logredeadline" org-log-redeadline time)
  3627. ("lognoteredeadline" org-log-redeadline note)
  3628. ("nologredeadline" org-log-redeadline nil)
  3629. ("logrefile" org-log-refile time)
  3630. ("lognoterefile" org-log-refile note)
  3631. ("nologrefile" org-log-refile nil)
  3632. ("fninline" org-footnote-define-inline t)
  3633. ("nofninline" org-footnote-define-inline nil)
  3634. ("fnlocal" org-footnote-section nil)
  3635. ("fnauto" org-footnote-auto-label t)
  3636. ("fnprompt" org-footnote-auto-label nil)
  3637. ("fnconfirm" org-footnote-auto-label confirm)
  3638. ("fnplain" org-footnote-auto-label plain)
  3639. ("fnadjust" org-footnote-auto-adjust t)
  3640. ("nofnadjust" org-footnote-auto-adjust nil)
  3641. ("constcgs" constants-unit-system cgs)
  3642. ("constSI" constants-unit-system SI)
  3643. ("noptag" org-tag-persistent-alist nil)
  3644. ("hideblocks" org-hide-block-startup t)
  3645. ("nohideblocks" org-hide-block-startup nil)
  3646. ("beamer" org-startup-with-beamer-mode t)
  3647. ("entitiespretty" org-pretty-entities t)
  3648. ("entitiesplain" org-pretty-entities nil))
  3649. "Variable associated with STARTUP options for org-mode.
  3650. Each element is a list of three items: The startup options as written
  3651. in the #+STARTUP line, the corresponding variable, and the value to
  3652. set this variable to if the option is found. An optional forth element PUSH
  3653. means to push this value onto the list in the variable.")
  3654. (defun org-set-regexps-and-options ()
  3655. "Precompute regular expressions for current buffer."
  3656. (when (org-mode-p)
  3657. (org-set-local 'org-todo-kwd-alist nil)
  3658. (org-set-local 'org-todo-key-alist nil)
  3659. (org-set-local 'org-todo-key-trigger nil)
  3660. (org-set-local 'org-todo-keywords-1 nil)
  3661. (org-set-local 'org-done-keywords nil)
  3662. (org-set-local 'org-todo-heads nil)
  3663. (org-set-local 'org-todo-sets nil)
  3664. (org-set-local 'org-todo-log-states nil)
  3665. (org-set-local 'org-file-properties nil)
  3666. (org-set-local 'org-file-tags nil)
  3667. (let ((re (org-make-options-regexp
  3668. '("CATEGORY" "TODO" "COLUMNS"
  3669. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3670. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3671. "OPTIONS")
  3672. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3673. (splitre "[ \t]+")
  3674. (scripts org-use-sub-superscripts)
  3675. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3676. tail sep kws1 prio props ftags drawers beamer-p
  3677. ext-setup-or-nil setup-contents (start 0))
  3678. (save-excursion
  3679. (save-restriction
  3680. (widen)
  3681. (goto-char (point-min))
  3682. (while (or (and ext-setup-or-nil
  3683. (string-match re ext-setup-or-nil start)
  3684. (setq start (match-end 0)))
  3685. (and (setq ext-setup-or-nil nil start 0)
  3686. (re-search-forward re nil t)))
  3687. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3688. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3689. (if (stringp value) (setq value (org-trim value)))
  3690. (cond
  3691. ((equal key "CATEGORY")
  3692. (setq cat value))
  3693. ((member key '("SEQ_TODO" "TODO"))
  3694. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3695. ((equal key "TYP_TODO")
  3696. (push (cons 'type (org-split-string value splitre)) kwds))
  3697. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3698. ;; general TODO-like setup
  3699. (push (cons (intern (downcase (match-string 1 key)))
  3700. (org-split-string value splitre)) kwds))
  3701. ((equal key "TAGS")
  3702. (setq tags (append tags (if tags '("\\n") nil)
  3703. (org-split-string value splitre))))
  3704. ((equal key "COLUMNS")
  3705. (org-set-local 'org-columns-default-format value))
  3706. ((equal key "LINK")
  3707. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3708. (push (cons (match-string 1 value)
  3709. (org-trim (match-string 2 value)))
  3710. links)))
  3711. ((equal key "PRIORITIES")
  3712. (setq prio (org-split-string value " +")))
  3713. ((equal key "PROPERTY")
  3714. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3715. (push (cons (match-string 1 value) (match-string 2 value))
  3716. props)))
  3717. ((equal key "FILETAGS")
  3718. (when (string-match "\\S-" value)
  3719. (setq ftags
  3720. (append
  3721. ftags
  3722. (apply 'append
  3723. (mapcar (lambda (x) (org-split-string x ":"))
  3724. (org-split-string value)))))))
  3725. ((equal key "DRAWERS")
  3726. (setq drawers (org-split-string value splitre)))
  3727. ((equal key "CONSTANTS")
  3728. (setq const (append const (org-split-string value splitre))))
  3729. ((equal key "STARTUP")
  3730. (let ((opts (org-split-string value splitre))
  3731. l var val)
  3732. (while (setq l (pop opts))
  3733. (when (setq l (assoc l org-startup-options))
  3734. (setq var (nth 1 l) val (nth 2 l))
  3735. (if (not (nth 3 l))
  3736. (set (make-local-variable var) val)
  3737. (if (not (listp (symbol-value var)))
  3738. (set (make-local-variable var) nil))
  3739. (set (make-local-variable var) (symbol-value var))
  3740. (add-to-list var val))))))
  3741. ((equal key "ARCHIVE")
  3742. (setq arch value)
  3743. (remove-text-properties 0 (length arch)
  3744. '(face t fontified t) arch))
  3745. ((equal key "LATEX_CLASS")
  3746. (setq beamer-p (equal value "beamer")))
  3747. ((equal key "OPTIONS")
  3748. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3749. (setq scripts (read (match-string 2 value)))))
  3750. ((equal key "SETUPFILE")
  3751. (setq setup-contents (org-file-contents
  3752. (expand-file-name
  3753. (org-remove-double-quotes value))
  3754. 'noerror))
  3755. (if (not ext-setup-or-nil)
  3756. (setq ext-setup-or-nil setup-contents start 0)
  3757. (setq ext-setup-or-nil
  3758. (concat (substring ext-setup-or-nil 0 start)
  3759. "\n" setup-contents "\n"
  3760. (substring ext-setup-or-nil start)))))
  3761. ))))
  3762. (org-set-local 'org-use-sub-superscripts scripts)
  3763. (when cat
  3764. (org-set-local 'org-category (intern cat))
  3765. (push (cons "CATEGORY" cat) props))
  3766. (when prio
  3767. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3768. (setq prio (mapcar 'string-to-char prio))
  3769. (org-set-local 'org-highest-priority (nth 0 prio))
  3770. (org-set-local 'org-lowest-priority (nth 1 prio))
  3771. (org-set-local 'org-default-priority (nth 2 prio)))
  3772. (and props (org-set-local 'org-file-properties (nreverse props)))
  3773. (and ftags (org-set-local 'org-file-tags
  3774. (mapcar 'org-add-prop-inherited ftags)))
  3775. (and drawers (org-set-local 'org-drawers drawers))
  3776. (and arch (org-set-local 'org-archive-location arch))
  3777. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3778. ;; Process the TODO keywords
  3779. (unless kwds
  3780. ;; Use the global values as if they had been given locally.
  3781. (setq kwds (default-value 'org-todo-keywords))
  3782. (if (stringp (car kwds))
  3783. (setq kwds (list (cons org-todo-interpretation
  3784. (default-value 'org-todo-keywords)))))
  3785. (setq kwds (reverse kwds)))
  3786. (setq kwds (nreverse kwds))
  3787. (let (inter kws kw)
  3788. (while (setq kws (pop kwds))
  3789. (let ((kws (or
  3790. (run-hook-with-args-until-success
  3791. 'org-todo-setup-filter-hook kws)
  3792. kws)))
  3793. (setq inter (pop kws) sep (member "|" kws)
  3794. kws0 (delete "|" (copy-sequence kws))
  3795. kwsa nil
  3796. kws1 (mapcar
  3797. (lambda (x)
  3798. ;; 1 2
  3799. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3800. (progn
  3801. (setq kw (match-string 1 x)
  3802. key (and (match-end 2) (match-string 2 x))
  3803. log (org-extract-log-state-settings x))
  3804. (push (cons kw (and key (string-to-char key))) kwsa)
  3805. (and log (push log org-todo-log-states))
  3806. kw)
  3807. (error "Invalid TODO keyword %s" x)))
  3808. kws0)
  3809. kwsa (if kwsa (append '((:startgroup))
  3810. (nreverse kwsa)
  3811. '((:endgroup))))
  3812. hw (car kws1)
  3813. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3814. tail (list inter hw (car dws) (org-last dws))))
  3815. (add-to-list 'org-todo-heads hw 'append)
  3816. (push kws1 org-todo-sets)
  3817. (setq org-done-keywords (append org-done-keywords dws nil))
  3818. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3819. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3820. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3821. (setq org-todo-sets (nreverse org-todo-sets)
  3822. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3823. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3824. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3825. ;; Process the constants
  3826. (when const
  3827. (let (e cst)
  3828. (while (setq e (pop const))
  3829. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3830. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3831. (setq org-table-formula-constants-local cst)))
  3832. ;; Process the tags.
  3833. (when tags
  3834. (let (e tgs)
  3835. (while (setq e (pop tags))
  3836. (cond
  3837. ((equal e "{") (push '(:startgroup) tgs))
  3838. ((equal e "}") (push '(:endgroup) tgs))
  3839. ((equal e "\\n") (push '(:newline) tgs))
  3840. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3841. (push (cons (match-string 1 e)
  3842. (string-to-char (match-string 2 e)))
  3843. tgs))
  3844. (t (push (list e) tgs))))
  3845. (org-set-local 'org-tag-alist nil)
  3846. (while (setq e (pop tgs))
  3847. (or (and (stringp (car e))
  3848. (assoc (car e) org-tag-alist))
  3849. (push e org-tag-alist)))))
  3850. ;; Compute the regular expressions and other local variables
  3851. (if (not org-done-keywords)
  3852. (setq org-done-keywords (and org-todo-keywords-1
  3853. (list (org-last org-todo-keywords-1)))))
  3854. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3855. (length org-scheduled-string)
  3856. (length org-clock-string)
  3857. (length org-closed-string)))
  3858. org-drawer-regexp
  3859. (concat "^[ \t]*:\\("
  3860. (mapconcat 'regexp-quote org-drawers "\\|")
  3861. "\\):[ \t]*$")
  3862. org-not-done-keywords
  3863. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3864. org-todo-regexp
  3865. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3866. "\\|") "\\)\\>")
  3867. org-not-done-regexp
  3868. (concat "\\<\\("
  3869. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3870. "\\)\\>")
  3871. org-not-done-heading-regexp
  3872. (concat "^\\(\\*+\\)[ \t]+\\("
  3873. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3874. "\\)\\>")
  3875. org-todo-line-regexp
  3876. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3877. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3878. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3879. org-complex-heading-regexp
  3880. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3881. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3882. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3883. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3884. org-complex-heading-regexp-format
  3885. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3886. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3887. "\\)\\>\\)?"
  3888. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3889. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3890. "[ \t]*\\(%s\\)"
  3891. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3892. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3893. org-nl-done-regexp
  3894. (concat "\n\\*+[ \t]+"
  3895. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3896. "\\)" "\\>")
  3897. org-todo-line-tags-regexp
  3898. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3899. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3900. (org-re
  3901. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3902. org-looking-at-done-regexp
  3903. (concat "^" "\\(?:"
  3904. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3905. "\\>")
  3906. org-deadline-regexp (concat "\\<" org-deadline-string)
  3907. org-deadline-time-regexp
  3908. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3909. org-deadline-line-regexp
  3910. (concat "\\<\\(" org-deadline-string "\\).*")
  3911. org-scheduled-regexp
  3912. (concat "\\<" org-scheduled-string)
  3913. org-scheduled-time-regexp
  3914. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3915. org-closed-time-regexp
  3916. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3917. org-keyword-time-regexp
  3918. (concat "\\<\\(" org-scheduled-string
  3919. "\\|" org-deadline-string
  3920. "\\|" org-closed-string
  3921. "\\|" org-clock-string "\\)"
  3922. " *[[<]\\([^]>]+\\)[]>]")
  3923. org-keyword-time-not-clock-regexp
  3924. (concat "\\<\\(" org-scheduled-string
  3925. "\\|" org-deadline-string
  3926. "\\|" org-closed-string
  3927. "\\)"
  3928. " *[[<]\\([^]>]+\\)[]>]")
  3929. org-maybe-keyword-time-regexp
  3930. (concat "\\(\\<\\(" org-scheduled-string
  3931. "\\|" org-deadline-string
  3932. "\\|" org-closed-string
  3933. "\\|" org-clock-string "\\)\\)?"
  3934. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3935. org-planning-or-clock-line-re
  3936. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3937. "\\|" org-deadline-string
  3938. "\\|" org-closed-string "\\|" org-clock-string
  3939. "\\)\\>\\)")
  3940. org-all-time-keywords
  3941. (mapcar (lambda (w) (substring w 0 -1))
  3942. (list org-scheduled-string org-deadline-string
  3943. org-clock-string org-closed-string))
  3944. )
  3945. (org-compute-latex-and-specials-regexp)
  3946. (org-set-font-lock-defaults))))
  3947. (defun org-file-contents (file &optional noerror)
  3948. "Return the contents of FILE, as a string."
  3949. (if (or (not file)
  3950. (not (file-readable-p file)))
  3951. (if noerror
  3952. (progn
  3953. (message "Cannot read file \"%s\"" file)
  3954. (ding) (sit-for 2)
  3955. "")
  3956. (error "Cannot read file \"%s\"" file))
  3957. (with-temp-buffer
  3958. (insert-file-contents file)
  3959. (buffer-string))))
  3960. (defun org-extract-log-state-settings (x)
  3961. "Extract the log state setting from a TODO keyword string.
  3962. This will extract info from a string like \"WAIT(w@/!)\"."
  3963. (let (kw key log1 log2)
  3964. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3965. (setq kw (match-string 1 x)
  3966. key (and (match-end 2) (match-string 2 x))
  3967. log1 (and (match-end 3) (match-string 3 x))
  3968. log2 (and (match-end 4) (match-string 4 x)))
  3969. (and (or log1 log2)
  3970. (list kw
  3971. (and log1 (if (equal log1 "!") 'time 'note))
  3972. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3973. (defun org-remove-keyword-keys (list)
  3974. "Remove a pair of parenthesis at the end of each string in LIST."
  3975. (mapcar (lambda (x)
  3976. (if (string-match "(.*)$" x)
  3977. (substring x 0 (match-beginning 0))
  3978. x))
  3979. list))
  3980. (defun org-assign-fast-keys (alist)
  3981. "Assign fast keys to a keyword-key alist.
  3982. Respect keys that are already there."
  3983. (let (new e (alt ?0))
  3984. (while (setq e (pop alist))
  3985. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3986. (cdr e)) ;; Key already assigned.
  3987. (push e new)
  3988. (let ((clist (string-to-list (downcase (car e))))
  3989. (used (append new alist)))
  3990. (when (= (car clist) ?@)
  3991. (pop clist))
  3992. (while (and clist (rassoc (car clist) used))
  3993. (pop clist))
  3994. (unless clist
  3995. (while (rassoc alt used)
  3996. (incf alt)))
  3997. (push (cons (car e) (or (car clist) alt)) new))))
  3998. (nreverse new)))
  3999. ;;; Some variables used in various places
  4000. (defvar org-window-configuration nil
  4001. "Used in various places to store a window configuration.")
  4002. (defvar org-selected-window nil
  4003. "Used in various places to store a window configuration.")
  4004. (defvar org-finish-function nil
  4005. "Function to be called when `C-c C-c' is used.
  4006. This is for getting out of special buffers like remember.")
  4007. ;; FIXME: Occasionally check by commenting these, to make sure
  4008. ;; no other functions uses these, forgetting to let-bind them.
  4009. (defvar entry)
  4010. (defvar last-state)
  4011. (defvar date)
  4012. ;; Defined somewhere in this file, but used before definition.
  4013. (defvar org-entities) ;; defined in org-entities.el
  4014. (defvar org-struct-menu)
  4015. (defvar org-org-menu)
  4016. (defvar org-tbl-menu)
  4017. ;;;; Define the Org-mode
  4018. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4019. (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"))
  4020. ;; We use a before-change function to check if a table might need
  4021. ;; an update.
  4022. (defvar org-table-may-need-update t
  4023. "Indicates that a table might need an update.
  4024. This variable is set by `org-before-change-function'.
  4025. `org-table-align' sets it back to nil.")
  4026. (defun org-before-change-function (beg end)
  4027. "Every change indicates that a table might need an update."
  4028. (setq org-table-may-need-update t))
  4029. (defvar org-mode-map)
  4030. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4031. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4032. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4033. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4034. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4035. (defvar org-table-buffer-is-an nil)
  4036. (defconst org-outline-regexp "\\*+ ")
  4037. ;;;###autoload
  4038. (define-derived-mode org-mode outline-mode "Org"
  4039. "Outline-based notes management and organizer, alias
  4040. \"Carsten's outline-mode for keeping track of everything.\"
  4041. Org-mode develops organizational tasks around a NOTES file which
  4042. contains information about projects as plain text. Org-mode is
  4043. implemented on top of outline-mode, which is ideal to keep the content
  4044. of large files well structured. It supports ToDo items, deadlines and
  4045. time stamps, which magically appear in the diary listing of the Emacs
  4046. calendar. Tables are easily created with a built-in table editor.
  4047. Plain text URL-like links connect to websites, emails (VM), Usenet
  4048. messages (Gnus), BBDB entries, and any files related to the project.
  4049. For printing and sharing of notes, an Org-mode file (or a part of it)
  4050. can be exported as a structured ASCII or HTML file.
  4051. The following commands are available:
  4052. \\{org-mode-map}"
  4053. ;; Get rid of Outline menus, they are not needed
  4054. ;; Need to do this here because define-derived-mode sets up
  4055. ;; the keymap so late. Still, it is a waste to call this each time
  4056. ;; we switch another buffer into org-mode.
  4057. (if (featurep 'xemacs)
  4058. (when (boundp 'outline-mode-menu-heading)
  4059. ;; Assume this is Greg's port, it uses easymenu
  4060. (easy-menu-remove outline-mode-menu-heading)
  4061. (easy-menu-remove outline-mode-menu-show)
  4062. (easy-menu-remove outline-mode-menu-hide))
  4063. (define-key org-mode-map [menu-bar headings] 'undefined)
  4064. (define-key org-mode-map [menu-bar hide] 'undefined)
  4065. (define-key org-mode-map [menu-bar show] 'undefined))
  4066. (org-load-modules-maybe)
  4067. (easy-menu-add org-org-menu)
  4068. (easy-menu-add org-tbl-menu)
  4069. (org-install-agenda-files-menu)
  4070. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4071. (add-to-invisibility-spec '(org-cwidth))
  4072. (add-to-invisibility-spec '(org-hide-block . t))
  4073. (when (featurep 'xemacs)
  4074. (org-set-local 'line-move-ignore-invisible t))
  4075. (org-set-local 'outline-regexp org-outline-regexp)
  4076. (org-set-local 'outline-level 'org-outline-level)
  4077. (when (and org-ellipsis
  4078. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4079. (fboundp 'make-glyph-code))
  4080. (unless org-display-table
  4081. (setq org-display-table (make-display-table)))
  4082. (set-display-table-slot
  4083. org-display-table 4
  4084. (vconcat (mapcar
  4085. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4086. org-ellipsis)))
  4087. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4088. (setq buffer-display-table org-display-table))
  4089. (org-set-regexps-and-options)
  4090. (when (and org-tag-faces (not org-tags-special-faces-re))
  4091. ;; tag faces set outside customize.... force initialization.
  4092. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4093. ;; Calc embedded
  4094. (org-set-local 'calc-embedded-open-mode "# ")
  4095. (modify-syntax-entry ?@ "w")
  4096. (if org-startup-truncated (setq truncate-lines t))
  4097. (org-set-local 'font-lock-unfontify-region-function
  4098. 'org-unfontify-region)
  4099. ;; Activate before-change-function
  4100. (org-set-local 'org-table-may-need-update t)
  4101. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4102. 'local)
  4103. ;; Check for running clock before killing a buffer
  4104. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4105. ;; Paragraphs and auto-filling
  4106. (org-set-autofill-regexps)
  4107. (setq indent-line-function 'org-indent-line-function)
  4108. (org-update-radio-target-regexp)
  4109. ;; Beginning/end of defun
  4110. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4111. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4112. ;; Make sure dependence stuff works reliably, even for users who set it
  4113. ;; too late :-(
  4114. (if org-enforce-todo-dependencies
  4115. (add-hook 'org-blocker-hook
  4116. 'org-block-todo-from-children-or-siblings-or-parent)
  4117. (remove-hook 'org-blocker-hook
  4118. 'org-block-todo-from-children-or-siblings-or-parent))
  4119. (if org-enforce-todo-checkbox-dependencies
  4120. (add-hook 'org-blocker-hook
  4121. 'org-block-todo-from-checkboxes)
  4122. (remove-hook 'org-blocker-hook
  4123. 'org-block-todo-from-checkboxes))
  4124. ;; Comment characters
  4125. (org-set-local 'comment-start "#")
  4126. (org-set-local 'comment-padding " ")
  4127. ;; Align options lines
  4128. (org-set-local
  4129. 'align-mode-rules-list
  4130. '((org-in-buffer-settings
  4131. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4132. (modes . '(org-mode)))))
  4133. ;; Imenu
  4134. (org-set-local 'imenu-create-index-function
  4135. 'org-imenu-get-tree)
  4136. ;; Make isearch reveal context
  4137. (if (or (featurep 'xemacs)
  4138. (not (boundp 'outline-isearch-open-invisible-function)))
  4139. ;; Emacs 21 and XEmacs make use of the hook
  4140. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4141. ;; Emacs 22 deals with this through a special variable
  4142. (org-set-local 'outline-isearch-open-invisible-function
  4143. (lambda (&rest ignore) (org-show-context 'isearch))))
  4144. ;; Turn on org-beamer-mode?
  4145. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4146. ;; If empty file that did not turn on org-mode automatically, make it to.
  4147. (if (and org-insert-mode-line-in-empty-file
  4148. (interactive-p)
  4149. (= (point-min) (point-max)))
  4150. (insert "# -*- mode: org -*-\n\n"))
  4151. (unless org-inhibit-startup
  4152. (when org-startup-align-all-tables
  4153. (let ((bmp (buffer-modified-p)))
  4154. (org-table-map-tables 'org-table-align 'quietly)
  4155. (set-buffer-modified-p bmp)))
  4156. (when org-startup-indented
  4157. (require 'org-indent)
  4158. (org-indent-mode 1))
  4159. (unless org-inhibit-startup-visibility-stuff
  4160. (org-set-startup-visibility))))
  4161. (when (fboundp 'abbrev-table-put)
  4162. (abbrev-table-put org-mode-abbrev-table
  4163. :parents (list text-mode-abbrev-table)))
  4164. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4165. (defun org-current-time ()
  4166. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4167. (if (> (car org-time-stamp-rounding-minutes) 1)
  4168. (let ((r (car org-time-stamp-rounding-minutes))
  4169. (time (decode-time)))
  4170. (apply 'encode-time
  4171. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4172. (nthcdr 2 time))))
  4173. (current-time)))
  4174. ;;;; Font-Lock stuff, including the activators
  4175. (defvar org-mouse-map (make-sparse-keymap))
  4176. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4177. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4178. (when org-mouse-1-follows-link
  4179. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4180. (when org-tab-follows-link
  4181. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4182. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4183. (require 'font-lock)
  4184. (defconst org-non-link-chars "]\t\n\r<>")
  4185. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4186. "shell" "elisp" "doi"))
  4187. (defvar org-link-types-re nil
  4188. "Matches a link that has a url-like prefix like \"http:\"")
  4189. (defvar org-link-re-with-space nil
  4190. "Matches a link with spaces, optional angular brackets around it.")
  4191. (defvar org-link-re-with-space2 nil
  4192. "Matches a link with spaces, optional angular brackets around it.")
  4193. (defvar org-link-re-with-space3 nil
  4194. "Matches a link with spaces, only for internal part in bracket links.")
  4195. (defvar org-angle-link-re nil
  4196. "Matches link with angular brackets, spaces are allowed.")
  4197. (defvar org-plain-link-re nil
  4198. "Matches plain link, without spaces.")
  4199. (defvar org-bracket-link-regexp nil
  4200. "Matches a link in double brackets.")
  4201. (defvar org-bracket-link-analytic-regexp nil
  4202. "Regular expression used to analyze links.
  4203. Here is what the match groups contain after a match:
  4204. 1: http:
  4205. 2: http
  4206. 3: path
  4207. 4: [desc]
  4208. 5: desc")
  4209. (defvar org-bracket-link-analytic-regexp++ nil
  4210. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4211. (defvar org-any-link-re nil
  4212. "Regular expression matching any link.")
  4213. (defcustom org-match-sexp-depth 3
  4214. "Number of stacked braces for sub/superscript matching.
  4215. This has to be set before loading org.el to be effective."
  4216. :group 'org-export-translation ; ??????????????????????????/
  4217. :type 'integer)
  4218. (defun org-create-multibrace-regexp (left right n)
  4219. "Create a regular expression which will match a balanced sexp.
  4220. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4221. as single character strings.
  4222. The regexp returned will match the entire expression including the
  4223. delimiters. It will also define a single group which contains the
  4224. match except for the outermost delimiters. The maximum depth of
  4225. stacked delimiters is N. Escaping delimiters is not possible."
  4226. (let* ((nothing (concat "[^" left right "]*?"))
  4227. (or "\\|")
  4228. (re nothing)
  4229. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4230. (while (> n 1)
  4231. (setq n (1- n)
  4232. re (concat re or next)
  4233. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4234. (concat left "\\(" re "\\)" right)))
  4235. (defvar org-match-substring-regexp
  4236. (concat
  4237. "\\([^\\]\\)\\([_^]\\)\\("
  4238. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4239. "\\|"
  4240. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4241. "\\|"
  4242. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4243. "The regular expression matching a sub- or superscript.")
  4244. (defvar org-match-substring-with-braces-regexp
  4245. (concat
  4246. "\\([^\\]\\)\\([_^]\\)\\("
  4247. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4248. "\\)")
  4249. "The regular expression matching a sub- or superscript, forcing braces.")
  4250. (defun org-make-link-regexps ()
  4251. "Update the link regular expressions.
  4252. This should be called after the variable `org-link-types' has changed."
  4253. (setq org-link-types-re
  4254. (concat
  4255. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4256. org-link-re-with-space
  4257. (concat
  4258. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4259. "\\([^" org-non-link-chars " ]"
  4260. "[^" org-non-link-chars "]*"
  4261. "[^" org-non-link-chars " ]\\)>?")
  4262. org-link-re-with-space2
  4263. (concat
  4264. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4265. "\\([^" org-non-link-chars " ]"
  4266. "[^\t\n\r]*"
  4267. "[^" org-non-link-chars " ]\\)>?")
  4268. org-link-re-with-space3
  4269. (concat
  4270. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4271. "\\([^" org-non-link-chars " ]"
  4272. "[^\t\n\r]*\\)")
  4273. org-angle-link-re
  4274. (concat
  4275. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4276. "\\([^" org-non-link-chars " ]"
  4277. "[^" org-non-link-chars "]*"
  4278. "\\)>")
  4279. org-plain-link-re
  4280. (concat
  4281. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4282. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4283. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4284. org-bracket-link-regexp
  4285. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4286. org-bracket-link-analytic-regexp
  4287. (concat
  4288. "\\[\\["
  4289. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4290. "\\([^]]+\\)"
  4291. "\\]"
  4292. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4293. "\\]")
  4294. org-bracket-link-analytic-regexp++
  4295. (concat
  4296. "\\[\\["
  4297. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4298. "\\([^]]+\\)"
  4299. "\\]"
  4300. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4301. "\\]")
  4302. org-any-link-re
  4303. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4304. org-angle-link-re "\\)\\|\\("
  4305. org-plain-link-re "\\)")))
  4306. (org-make-link-regexps)
  4307. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4308. "Regular expression for fast time stamp matching.")
  4309. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4310. "Regular expression for fast time stamp matching.")
  4311. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4312. "Regular expression matching time strings for analysis.
  4313. This one does not require the space after the date, so it can be used
  4314. on a string that terminates immediately after the date.")
  4315. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4316. "Regular expression matching time strings for analysis.")
  4317. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4318. "Regular expression matching time stamps, with groups.")
  4319. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4320. "Regular expression matching time stamps (also [..]), with groups.")
  4321. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4322. "Regular expression matching a time stamp range.")
  4323. (defconst org-tr-regexp-both
  4324. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4325. "Regular expression matching a time stamp range.")
  4326. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4327. org-ts-regexp "\\)?")
  4328. "Regular expression matching a time stamp or time stamp range.")
  4329. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4330. org-ts-regexp-both "\\)?")
  4331. "Regular expression matching a time stamp or time stamp range.
  4332. The time stamps may be either active or inactive.")
  4333. (defvar org-emph-face nil)
  4334. (defun org-do-emphasis-faces (limit)
  4335. "Run through the buffer and add overlays to links."
  4336. (let (rtn a)
  4337. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4338. (if (not (= (char-after (match-beginning 3))
  4339. (char-after (match-beginning 4))))
  4340. (progn
  4341. (setq rtn t)
  4342. (setq a (assoc (match-string 3) org-emphasis-alist))
  4343. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4344. 'face
  4345. (nth 1 a))
  4346. (and (nth 4 a)
  4347. (org-remove-flyspell-overlays-in
  4348. (match-beginning 0) (match-end 0)))
  4349. (add-text-properties (match-beginning 2) (match-end 2)
  4350. '(font-lock-multiline t org-emphasis t))
  4351. (when org-hide-emphasis-markers
  4352. (add-text-properties (match-end 4) (match-beginning 5)
  4353. '(invisible org-link))
  4354. (add-text-properties (match-beginning 3) (match-end 3)
  4355. '(invisible org-link)))))
  4356. (backward-char 1))
  4357. rtn))
  4358. (defun org-emphasize (&optional char)
  4359. "Insert or change an emphasis, i.e. a font like bold or italic.
  4360. If there is an active region, change that region to a new emphasis.
  4361. If there is no region, just insert the marker characters and position
  4362. the cursor between them.
  4363. CHAR should be either the marker character, or the first character of the
  4364. HTML tag associated with that emphasis. If CHAR is a space, the means
  4365. to remove the emphasis of the selected region.
  4366. If char is not given (for example in an interactive call) it
  4367. will be prompted for."
  4368. (interactive)
  4369. (let ((eal org-emphasis-alist) e det
  4370. (erc org-emphasis-regexp-components)
  4371. (prompt "")
  4372. (string "") beg end move tag c s)
  4373. (if (org-region-active-p)
  4374. (setq beg (region-beginning) end (region-end)
  4375. string (buffer-substring beg end))
  4376. (setq move t))
  4377. (while (setq e (pop eal))
  4378. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4379. c (aref tag 0))
  4380. (push (cons c (string-to-char (car e))) det)
  4381. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4382. (substring tag 1)))))
  4383. (setq det (nreverse det))
  4384. (unless char
  4385. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4386. (setq char (read-char-exclusive)))
  4387. (setq char (or (cdr (assoc char det)) char))
  4388. (if (equal char ?\ )
  4389. (setq s "" move nil)
  4390. (unless (assoc (char-to-string char) org-emphasis-alist)
  4391. (error "No such emphasis marker: \"%c\"" char))
  4392. (setq s (char-to-string char)))
  4393. (while (and (> (length string) 1)
  4394. (equal (substring string 0 1) (substring string -1))
  4395. (assoc (substring string 0 1) org-emphasis-alist))
  4396. (setq string (substring string 1 -1)))
  4397. (setq string (concat s string s))
  4398. (if beg (delete-region beg end))
  4399. (unless (or (bolp)
  4400. (string-match (concat "[" (nth 0 erc) "\n]")
  4401. (char-to-string (char-before (point)))))
  4402. (insert " "))
  4403. (unless (or (eobp)
  4404. (string-match (concat "[" (nth 1 erc) "\n]")
  4405. (char-to-string (char-after (point)))))
  4406. (insert " ") (backward-char 1))
  4407. (insert string)
  4408. (and move (backward-char 1))))
  4409. (defconst org-nonsticky-props
  4410. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4411. (defsubst org-rear-nonsticky-at (pos)
  4412. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4413. (defun org-activate-plain-links (limit)
  4414. "Run through the buffer and add overlays to links."
  4415. (catch 'exit
  4416. (let (f)
  4417. (if (re-search-forward org-plain-link-re limit t)
  4418. (progn
  4419. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4420. (setq f (get-text-property (match-beginning 0) 'face))
  4421. (if (or (eq f 'org-tag)
  4422. (and (listp f) (memq 'org-tag f)))
  4423. nil
  4424. (add-text-properties (match-beginning 0) (match-end 0)
  4425. (list 'mouse-face 'highlight
  4426. 'face 'org-link
  4427. 'keymap org-mouse-map))
  4428. (org-rear-nonsticky-at (match-end 0)))
  4429. t)))))
  4430. (defun org-activate-code (limit)
  4431. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4432. (progn
  4433. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4434. (remove-text-properties (match-beginning 0) (match-end 0)
  4435. '(display t invisible t intangible t))
  4436. t)))
  4437. (defun org-fontify-meta-lines-and-blocks (limit)
  4438. "Fontify #+ lines and blocks, in the correct ways."
  4439. (let ((case-fold-search t))
  4440. (if (re-search-forward
  4441. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4442. limit t)
  4443. (let ((beg (match-beginning 0))
  4444. (beg1 (line-beginning-position 2))
  4445. (dc1 (downcase (match-string 2)))
  4446. (dc3 (downcase (match-string 3)))
  4447. end end1 quoting block-type)
  4448. (cond
  4449. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4450. ;; a single line of backend-specific content
  4451. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4452. (remove-text-properties (match-beginning 0) (match-end 0)
  4453. '(display t invisible t intangible t))
  4454. (add-text-properties (match-beginning 1) (match-end 3)
  4455. '(font-lock-fontified t face org-meta-line))
  4456. (add-text-properties (match-beginning 6) (match-end 6)
  4457. '(font-lock-fontified t face org-block))
  4458. t)
  4459. ((and (match-end 4) (equal dc3 "begin"))
  4460. ;; Truly a block
  4461. (setq block-type (downcase (match-string 5))
  4462. quoting (member block-type org-protecting-blocks))
  4463. (when (re-search-forward
  4464. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4465. nil t) ;; on purpose, we look further than LIMIT
  4466. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4467. (when quoting
  4468. (remove-text-properties beg end
  4469. '(display t invisible t intangible t)))
  4470. (add-text-properties
  4471. beg end
  4472. '(font-lock-fontified t font-lock-multiline t))
  4473. (add-text-properties beg beg1 '(face org-meta-line))
  4474. (add-text-properties end1 end '(face org-meta-line))
  4475. (cond
  4476. (quoting
  4477. (add-text-properties beg1 end1 '(face org-block)))
  4478. ((not org-fontify-quote-and-verse-blocks))
  4479. ((string= block-type "quote")
  4480. (add-text-properties beg1 end1 '(face org-quote)))
  4481. ((string= block-type "verse")
  4482. (add-text-properties beg1 end1 '(face org-verse))))
  4483. t))
  4484. ((member dc1 '("title:" "author:" "email:" "date:"))
  4485. (add-text-properties
  4486. beg (match-end 3)
  4487. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4488. '(font-lock-fontified t invisible t)
  4489. '(font-lock-fontified t face org-document-info-keyword)))
  4490. (add-text-properties
  4491. (match-beginning 6) (match-end 6)
  4492. (if (string-equal dc1 "title:")
  4493. '(font-lock-fontified t face org-document-title)
  4494. '(font-lock-fontified t face org-document-info))))
  4495. ((not (member (char-after beg) '(?\ ?\t)))
  4496. ;; just any other in-buffer setting, but not indented
  4497. (add-text-properties
  4498. beg (match-end 0)
  4499. '(font-lock-fontified t face org-meta-line))
  4500. t)
  4501. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4502. "orgtbl:" "tblfm:" "tblname:" "result:"
  4503. "results:" "source:" "srcname:" "call:"))
  4504. (and (match-end 4) (equal dc3 "attr")))
  4505. (add-text-properties
  4506. beg (match-end 0)
  4507. '(font-lock-fontified t face org-meta-line))
  4508. t)
  4509. ((member dc3 '(" " ""))
  4510. (add-text-properties
  4511. beg (match-end 0)
  4512. '(font-lock-fontified t face font-lock-comment-face)))
  4513. (t nil))))))
  4514. (defun org-activate-angle-links (limit)
  4515. "Run through the buffer and add overlays to links."
  4516. (if (re-search-forward org-angle-link-re limit t)
  4517. (progn
  4518. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4519. (add-text-properties (match-beginning 0) (match-end 0)
  4520. (list 'mouse-face 'highlight
  4521. 'keymap org-mouse-map))
  4522. (org-rear-nonsticky-at (match-end 0))
  4523. t)))
  4524. (defun org-activate-footnote-links (limit)
  4525. "Run through the buffer and add overlays to links."
  4526. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4527. limit t)
  4528. (progn
  4529. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4530. (add-text-properties (match-beginning 2) (match-end 2)
  4531. (list 'mouse-face 'highlight
  4532. 'keymap org-mouse-map
  4533. 'help-echo
  4534. (if (= (point-at-bol) (match-beginning 2))
  4535. "Footnote definition"
  4536. "Footnote reference")
  4537. ))
  4538. (org-rear-nonsticky-at (match-end 2))
  4539. t)))
  4540. (defun org-activate-bracket-links (limit)
  4541. "Run through the buffer and add overlays to bracketed links."
  4542. (if (re-search-forward org-bracket-link-regexp limit t)
  4543. (let* ((help (concat "LINK: "
  4544. (org-match-string-no-properties 1)))
  4545. ;; FIXME: above we should remove the escapes.
  4546. ;; but that requires another match, protecting match data,
  4547. ;; a lot of overhead for font-lock.
  4548. (ip (org-maybe-intangible
  4549. (list 'invisible 'org-link
  4550. 'keymap org-mouse-map 'mouse-face 'highlight
  4551. 'font-lock-multiline t 'help-echo help)))
  4552. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4553. 'font-lock-multiline t 'help-echo help)))
  4554. ;; We need to remove the invisible property here. Table narrowing
  4555. ;; may have made some of this invisible.
  4556. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4557. (remove-text-properties (match-beginning 0) (match-end 0)
  4558. '(invisible nil))
  4559. (if (match-end 3)
  4560. (progn
  4561. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4562. (org-rear-nonsticky-at (match-beginning 3))
  4563. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4564. (org-rear-nonsticky-at (match-end 3))
  4565. (add-text-properties (match-end 3) (match-end 0) ip)
  4566. (org-rear-nonsticky-at (match-end 0)))
  4567. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4568. (org-rear-nonsticky-at (match-beginning 1))
  4569. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4570. (org-rear-nonsticky-at (match-end 1))
  4571. (add-text-properties (match-end 1) (match-end 0) ip)
  4572. (org-rear-nonsticky-at (match-end 0)))
  4573. t)))
  4574. (defun org-activate-dates (limit)
  4575. "Run through the buffer and add overlays to dates."
  4576. (if (re-search-forward org-tsr-regexp-both limit t)
  4577. (progn
  4578. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4579. (add-text-properties (match-beginning 0) (match-end 0)
  4580. (list 'mouse-face 'highlight
  4581. 'keymap org-mouse-map))
  4582. (org-rear-nonsticky-at (match-end 0))
  4583. (when org-display-custom-times
  4584. (if (match-end 3)
  4585. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4586. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4587. t)))
  4588. (defvar org-target-link-regexp nil
  4589. "Regular expression matching radio targets in plain text.")
  4590. (make-variable-buffer-local 'org-target-link-regexp)
  4591. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4592. "Regular expression matching a link target.")
  4593. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4594. "Regular expression matching a radio target.")
  4595. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4596. "Regular expression matching any target.")
  4597. (defun org-activate-target-links (limit)
  4598. "Run through the buffer and add overlays to target matches."
  4599. (when org-target-link-regexp
  4600. (let ((case-fold-search t))
  4601. (if (re-search-forward org-target-link-regexp limit t)
  4602. (progn
  4603. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4604. (add-text-properties (match-beginning 0) (match-end 0)
  4605. (list 'mouse-face 'highlight
  4606. 'keymap org-mouse-map
  4607. 'help-echo "Radio target link"
  4608. 'org-linked-text t))
  4609. (org-rear-nonsticky-at (match-end 0))
  4610. t)))))
  4611. (defun org-update-radio-target-regexp ()
  4612. "Find all radio targets in this file and update the regular expression."
  4613. (interactive)
  4614. (when (memq 'radio org-activate-links)
  4615. (setq org-target-link-regexp
  4616. (org-make-target-link-regexp (org-all-targets 'radio)))
  4617. (org-restart-font-lock)))
  4618. (defun org-hide-wide-columns (limit)
  4619. (let (s e)
  4620. (setq s (text-property-any (point) (or limit (point-max))
  4621. 'org-cwidth t))
  4622. (when s
  4623. (setq e (next-single-property-change s 'org-cwidth))
  4624. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4625. (goto-char e)
  4626. t)))
  4627. (defvar org-latex-and-specials-regexp nil
  4628. "Regular expression for highlighting export special stuff.")
  4629. (defvar org-match-substring-regexp)
  4630. (defvar org-match-substring-with-braces-regexp)
  4631. ;; This should be with the exporter code, but we also use if for font-locking
  4632. (defconst org-export-html-special-string-regexps
  4633. '(("\\\\-" . "&shy;")
  4634. ("---\\([^-]\\)" . "&mdash;\\1")
  4635. ("--\\([^-]\\)" . "&ndash;\\1")
  4636. ("\\.\\.\\." . "&hellip;"))
  4637. "Regular expressions for special string conversion.")
  4638. (defun org-compute-latex-and-specials-regexp ()
  4639. "Compute regular expression for stuff treated specially by exporters."
  4640. (if (not org-highlight-latex-fragments-and-specials)
  4641. (org-set-local 'org-latex-and-specials-regexp nil)
  4642. (require 'org-exp)
  4643. (let*
  4644. ((matchers (plist-get org-format-latex-options :matchers))
  4645. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4646. org-latex-regexps)))
  4647. (org-export-allow-BIND nil)
  4648. (options (org-combine-plists (org-default-export-plist)
  4649. (org-infile-export-plist)))
  4650. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4651. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4652. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4653. (org-export-html-expand (plist-get options :expand-quoted-html))
  4654. (org-export-with-special-strings (plist-get options :special-strings))
  4655. (re-sub
  4656. (cond
  4657. ((equal org-export-with-sub-superscripts '{})
  4658. (list org-match-substring-with-braces-regexp))
  4659. (org-export-with-sub-superscripts
  4660. (list org-match-substring-regexp))
  4661. (t nil)))
  4662. (re-latex
  4663. (if org-export-with-LaTeX-fragments
  4664. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4665. (re-macros
  4666. (if org-export-with-TeX-macros
  4667. (list (concat "\\\\"
  4668. (regexp-opt
  4669. (append
  4670. (delq nil
  4671. (mapcar 'car-safe
  4672. (append org-entities-user
  4673. org-entities)))
  4674. (if (boundp 'org-latex-entities)
  4675. (mapcar (lambda (x)
  4676. (or (car-safe x) x))
  4677. org-latex-entities)
  4678. nil))
  4679. 'words))) ; FIXME
  4680. ))
  4681. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4682. (re-special (if org-export-with-special-strings
  4683. (mapcar (lambda (x) (car x))
  4684. org-export-html-special-string-regexps)))
  4685. (re-rest
  4686. (delq nil
  4687. (list
  4688. (if org-export-html-expand "@<[^>\n]+>")
  4689. ))))
  4690. (org-set-local
  4691. 'org-latex-and-specials-regexp
  4692. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4693. re-rest) "\\|")))))
  4694. (defun org-do-latex-and-special-faces (limit)
  4695. "Run through the buffer and add overlays to links."
  4696. (when org-latex-and-specials-regexp
  4697. (let (rtn d)
  4698. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4699. limit t))
  4700. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4701. 'face))
  4702. '(org-code org-verbatim underline)))
  4703. (progn
  4704. (setq rtn t
  4705. d (cond ((member (char-after (1+ (match-beginning 0)))
  4706. '(?_ ?^)) 1)
  4707. (t 0)))
  4708. (font-lock-prepend-text-property
  4709. (+ d (match-beginning 0)) (match-end 0)
  4710. 'face 'org-latex-and-export-specials)
  4711. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4712. '(font-lock-multiline t)))))
  4713. rtn)))
  4714. (defun org-restart-font-lock ()
  4715. "Restart `font-lock-mode', to force refontification."
  4716. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4717. (font-lock-mode -1)
  4718. (font-lock-mode 1)))
  4719. (defun org-all-targets (&optional radio)
  4720. "Return a list of all targets in this file.
  4721. With optional argument RADIO, only find radio targets."
  4722. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4723. rtn)
  4724. (save-excursion
  4725. (goto-char (point-min))
  4726. (while (re-search-forward re nil t)
  4727. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4728. rtn)))
  4729. (defun org-make-target-link-regexp (targets)
  4730. "Make regular expression matching all strings in TARGETS.
  4731. The regular expression finds the targets also if there is a line break
  4732. between words."
  4733. (and targets
  4734. (concat
  4735. "\\<\\("
  4736. (mapconcat
  4737. (lambda (x)
  4738. (while (string-match " +" x)
  4739. (setq x (replace-match "\\s-+" t t x)))
  4740. x)
  4741. targets
  4742. "\\|")
  4743. "\\)\\>")))
  4744. (defun org-activate-tags (limit)
  4745. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4746. (progn
  4747. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4748. (add-text-properties (match-beginning 1) (match-end 1)
  4749. (list 'mouse-face 'highlight
  4750. 'keymap org-mouse-map))
  4751. (org-rear-nonsticky-at (match-end 1))
  4752. t)))
  4753. (defun org-outline-level ()
  4754. "Compute the outline level of the heading at point.
  4755. This function assumes that the cursor is at the beginning of a line matched
  4756. by `outline-regexp'. Otherwise it returns garbage.
  4757. If this is called at a normal headline, the level is the number of stars.
  4758. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4759. For plain list items, if they are matched by `outline-regexp', this returns
  4760. 1000 plus the line indentation."
  4761. (save-excursion
  4762. (looking-at outline-regexp)
  4763. (if (match-beginning 1)
  4764. (+ (org-get-string-indentation (match-string 1)) 1000)
  4765. (1- (- (match-end 0) (match-beginning 0))))))
  4766. (defvar org-font-lock-keywords nil)
  4767. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4768. "Regular expression matching a property line.")
  4769. (defvar org-font-lock-hook nil
  4770. "Functions to be called for special font lock stuff.")
  4771. (defun org-font-lock-hook (limit)
  4772. (run-hook-with-args 'org-font-lock-hook limit))
  4773. (defun org-set-font-lock-defaults ()
  4774. (let* ((em org-fontify-emphasized-text)
  4775. (lk org-activate-links)
  4776. (org-font-lock-extra-keywords
  4777. (list
  4778. ;; Call the hook
  4779. '(org-font-lock-hook)
  4780. ;; Headlines
  4781. `(,(if org-fontify-whole-heading-line
  4782. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4783. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4784. (1 (org-get-level-face 1))
  4785. (2 (org-get-level-face 2))
  4786. (3 (org-get-level-face 3)))
  4787. ;; Table lines
  4788. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4789. (1 'org-table t))
  4790. ;; Table internals
  4791. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4792. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4793. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4794. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4795. ;; Drawers
  4796. (list org-drawer-regexp '(0 'org-special-keyword t))
  4797. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4798. ;; Properties
  4799. (list org-property-re
  4800. '(1 'org-special-keyword t)
  4801. '(3 'org-property-value t))
  4802. ;; Links
  4803. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4804. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4805. (if (memq 'plain lk) '(org-activate-plain-links))
  4806. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4807. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4808. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4809. (if (memq 'footnote lk) '(org-activate-footnote-links
  4810. (2 'org-footnote t)))
  4811. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4812. '(org-hide-wide-columns (0 nil append))
  4813. ;; TODO lines
  4814. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4815. '(1 (org-get-todo-face 1) t))
  4816. ;; DONE
  4817. (if org-fontify-done-headline
  4818. (list (concat "^[*]+ +\\<\\("
  4819. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4820. "\\)\\(.*\\)")
  4821. '(2 'org-headline-done t))
  4822. nil)
  4823. ;; Priorities
  4824. '(org-font-lock-add-priority-faces)
  4825. ;; Tags
  4826. '(org-font-lock-add-tag-faces)
  4827. ;; Special keywords
  4828. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4829. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4830. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4831. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4832. ;; Emphasis
  4833. (if em
  4834. (if (featurep 'xemacs)
  4835. '(org-do-emphasis-faces (0 nil append))
  4836. '(org-do-emphasis-faces)))
  4837. ;; Checkboxes
  4838. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4839. 2 'org-checkbox prepend)
  4840. (if org-provide-checkbox-statistics
  4841. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4842. (0 (org-get-checkbox-statistics-face) t)))
  4843. ;; Description list items
  4844. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4845. 2 'bold prepend)
  4846. ;; ARCHIVEd headings
  4847. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4848. '(1 'org-archived prepend))
  4849. ;; Specials
  4850. '(org-do-latex-and-special-faces)
  4851. '(org-fontify-entities)
  4852. '(org-raise-scripts)
  4853. ;; Code
  4854. '(org-activate-code (1 'org-code t))
  4855. ;; COMMENT
  4856. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4857. "\\|" org-quote-string "\\)\\>")
  4858. '(1 'org-special-keyword t))
  4859. '("^#.*" (0 'font-lock-comment-face t))
  4860. ;; Blocks and meta lines
  4861. '(org-fontify-meta-lines-and-blocks)
  4862. )))
  4863. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4864. ;; Now set the full font-lock-keywords
  4865. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4866. (org-set-local 'font-lock-defaults
  4867. '(org-font-lock-keywords t nil nil backward-paragraph))
  4868. (kill-local-variable 'font-lock-keywords) nil))
  4869. (defun org-toggle-pretty-entities ()
  4870. "Toggle the composition display of entities as UTF8 characters."
  4871. (interactive)
  4872. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  4873. (org-restart-font-lock)
  4874. (if org-pretty-entities
  4875. (message "Entities are displayed as UTF8 characers")
  4876. (save-restriction
  4877. (widen)
  4878. (decompose-region (point-min) (point-max))
  4879. (message "Entities are displayed plain"))))
  4880. (defun org-fontify-entities (limit)
  4881. "Find an entity to fontify."
  4882. (let (ee)
  4883. (when org-pretty-entities
  4884. (catch 'match
  4885. (while (re-search-forward
  4886. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  4887. limit t)
  4888. (if (and (not (org-in-indented-comment-line))
  4889. (setq ee (org-entity-get (match-string 1)))
  4890. (= (length (nth 6 ee)) 1))
  4891. (progn
  4892. (add-text-properties
  4893. (match-beginning 0) (match-end 1)
  4894. (list 'font-lock-fontified t))
  4895. (compose-region (match-beginning 0) (match-end 1)
  4896. (nth 6 ee) nil)
  4897. (backward-char 1)
  4898. (throw 'match t))))
  4899. nil))))
  4900. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4901. "Fontify string S like in Org-mode."
  4902. (with-temp-buffer
  4903. (insert s)
  4904. (let ((org-odd-levels-only odd-levels))
  4905. (org-mode)
  4906. (font-lock-fontify-buffer)
  4907. (buffer-string))))
  4908. (defvar org-m nil)
  4909. (defvar org-l nil)
  4910. (defvar org-f nil)
  4911. (defun org-get-level-face (n)
  4912. "Get the right face for match N in font-lock matching of headlines."
  4913. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4914. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4915. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4916. (cond
  4917. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4918. ((eq n 2) org-f)
  4919. (t (if org-level-color-stars-only nil org-f))))
  4920. (defun org-get-todo-face (kwd)
  4921. "Get the right face for a TODO keyword KWD.
  4922. If KWD is a number, get the corresponding match group."
  4923. (if (numberp kwd) (setq kwd (match-string kwd)))
  4924. (or (org-face-from-face-or-color
  4925. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4926. (and (member kwd org-done-keywords) 'org-done)
  4927. 'org-todo))
  4928. (defun org-face-from-face-or-color (context inherit face-or-color)
  4929. "Create a face list that inherits INHERIT, but sets the foreground color.
  4930. When FACE-OR-COLOR is not a string, just return it."
  4931. (if (stringp face-or-color)
  4932. (list :inherit inherit
  4933. (cdr (assoc context org-faces-easy-properties))
  4934. face-or-color)
  4935. face-or-color))
  4936. (defun org-font-lock-add-tag-faces (limit)
  4937. "Add the special tag faces."
  4938. (when (and org-tag-faces org-tags-special-faces-re)
  4939. (while (re-search-forward org-tags-special-faces-re limit t)
  4940. (add-text-properties (match-beginning 1) (match-end 1)
  4941. (list 'face (org-get-tag-face 1)
  4942. 'font-lock-fontified t))
  4943. (backward-char 1))))
  4944. (defun org-font-lock-add-priority-faces (limit)
  4945. "Add the special priority faces."
  4946. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4947. (add-text-properties
  4948. (match-beginning 0) (match-end 0)
  4949. (list 'face (or (org-face-from-face-or-color
  4950. 'priority 'org-special-keyword
  4951. (cdr (assoc (char-after (match-beginning 1))
  4952. org-priority-faces)))
  4953. 'org-special-keyword)
  4954. 'font-lock-fontified t))))
  4955. (defun org-get-tag-face (kwd)
  4956. "Get the right face for a TODO keyword KWD.
  4957. If KWD is a number, get the corresponding match group."
  4958. (if (numberp kwd) (setq kwd (match-string kwd)))
  4959. (or (org-face-from-face-or-color
  4960. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4961. 'org-tag))
  4962. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4963. "Remove fontification and activation overlays from links."
  4964. (font-lock-default-unfontify-region beg end)
  4965. (let* ((buffer-undo-list t)
  4966. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4967. (inhibit-modification-hooks t)
  4968. deactivate-mark buffer-file-name buffer-file-truename)
  4969. (decompose-region beg end)
  4970. (remove-text-properties
  4971. beg end
  4972. (if org-indent-mode
  4973. ;; also remove line-prefix and wrap-prefix properties
  4974. '(mouse-face t keymap t org-linked-text t
  4975. invisible t intangible t
  4976. line-prefix t wrap-prefix t
  4977. org-no-flyspell t org-emphasis t)
  4978. '(mouse-face t keymap t org-linked-text t
  4979. invisible t intangible t
  4980. org-no-flyspell t org-emphasis t)))
  4981. (org-remove-font-lock-display-properties beg end)))
  4982. (defconst org-script-display '(((raise -0.3) (height 0.7))
  4983. ((raise 0.3) (height 0.7))
  4984. ((raise -0.5))
  4985. ((raise 0.5)))
  4986. "Display properties for showing superscripts and subscripts.")
  4987. (defun org-remove-font-lock-display-properties (beg end)
  4988. "Remove specific display properties that have been added by font lock.
  4989. The will remove the raise properties that are used to show superscripts
  4990. and subscripts."
  4991. (let (next prop)
  4992. (while (< beg end)
  4993. (setq next (next-single-property-change beg 'display nil end)
  4994. prop (get-text-property beg 'display))
  4995. (if (member prop org-script-display)
  4996. (put-text-property beg next 'display nil))
  4997. (setq beg next))))
  4998. (defun org-raise-scripts (limit)
  4999. "Add raise properties to sub/superscripts."
  5000. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5001. (if (re-search-forward
  5002. (if (eq org-use-sub-superscripts t)
  5003. org-match-substring-regexp
  5004. org-match-substring-with-braces-regexp)
  5005. limit t)
  5006. (let* ((pos (point)) table-p comment-p
  5007. (mpos (match-beginning 3))
  5008. (emph-p (get-text-property mpos 'org-emphasis))
  5009. (link-p (get-text-property mpos 'mouse-face))
  5010. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5011. (goto-char (point-at-bol))
  5012. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5013. comment-p (org-looking-at-p "[ \t]*#"))
  5014. (goto-char pos)
  5015. ;; FIXME: Should we go back one character here, for a_b^c
  5016. ;; (goto-char (1- pos)) ;????????????????????
  5017. (if (or comment-p emph-p link-p keyw-p)
  5018. t
  5019. (put-text-property (match-beginning 3) (match-end 0)
  5020. 'display
  5021. (if (equal (char-after (match-beginning 2)) ?^)
  5022. (nth (if table-p 3 1) org-script-display)
  5023. (nth (if table-p 2 0) org-script-display)))
  5024. (add-text-properties (match-beginning 2) (match-end 2)
  5025. (list 'invisible t
  5026. 'org-dwidth t 'org-dwidth-n 1))
  5027. (if (and (eq (char-after (match-beginning 3)) ?{)
  5028. (eq (char-before (match-end 3)) ?}))
  5029. (progn
  5030. (add-text-properties
  5031. (match-beginning 3) (1+ (match-beginning 3))
  5032. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5033. (add-text-properties
  5034. (1- (match-end 3)) (match-end 3)
  5035. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5036. t)))))
  5037. ;;;; Visibility cycling, including org-goto and indirect buffer
  5038. ;;; Cycling
  5039. (defvar org-cycle-global-status nil)
  5040. (make-variable-buffer-local 'org-cycle-global-status)
  5041. (defvar org-cycle-subtree-status nil)
  5042. (make-variable-buffer-local 'org-cycle-subtree-status)
  5043. ;;;###autoload
  5044. (defvar org-inlinetask-min-level)
  5045. (defun org-cycle (&optional arg)
  5046. "TAB-action and visibility cycling for Org-mode.
  5047. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5048. is outline visibility cycling, but it also invokes other actions
  5049. in special contexts.
  5050. - When this function is called with a prefix argument, rotate the entire
  5051. buffer through 3 states (global cycling)
  5052. 1. OVERVIEW: Show only top-level headlines.
  5053. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5054. 3. SHOW ALL: Show everything.
  5055. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5056. determined by the variable `org-startup-folded', and by any VISIBILITY
  5057. properties in the buffer.
  5058. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5059. including any drawers.
  5060. - When inside a table, re-align the table and move to the next field.
  5061. - When point is at the beginning of a headline, rotate the subtree started
  5062. by this line through 3 different states (local cycling)
  5063. 1. FOLDED: Only the main headline is shown.
  5064. 2. CHILDREN: The main headline and the direct children are shown.
  5065. From this state, you can move to one of the children
  5066. and zoom in further.
  5067. 3. SUBTREE: Show the entire subtree, including body text.
  5068. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5069. - When point is at the beginning of an empty headline and the variable
  5070. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5071. of the headline by demoting and promoting it to likely levels. This
  5072. speeds up creation document structure by pressing TAB once or several
  5073. times right after creating a new headline.
  5074. - When there is a numeric prefix, go up to a heading with level ARG, do
  5075. a `show-subtree' and return to the previous cursor position. If ARG
  5076. is negative, go up that many levels.
  5077. - When point is not at the beginning of a headline, execute the global
  5078. binding for TAB, which is re-indenting the line. See the option
  5079. `org-cycle-emulate-tab' for details.
  5080. - Special case: if point is at the beginning of the buffer and there is
  5081. no headline in line 1, this function will act as if called with prefix arg.
  5082. But only if also the variable `org-cycle-global-at-bob' is t."
  5083. (interactive "P")
  5084. (org-load-modules-maybe)
  5085. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5086. (and org-cycle-level-after-item/entry-creation
  5087. (or (org-cycle-level)
  5088. (org-cycle-item-indentation))))
  5089. (let* ((limit-level
  5090. (or org-cycle-max-level
  5091. (and (boundp 'org-inlinetask-min-level)
  5092. org-inlinetask-min-level
  5093. (1- org-inlinetask-min-level))))
  5094. (nstars (and limit-level
  5095. (if org-odd-levels-only
  5096. (and limit-level (1- (* limit-level 2)))
  5097. limit-level)))
  5098. (outline-regexp
  5099. (cond
  5100. ((not (org-mode-p)) outline-regexp)
  5101. ((or (eq org-cycle-include-plain-lists 'integrate)
  5102. (and org-cycle-include-plain-lists (org-at-item-p)))
  5103. (concat "\\(?:\\*"
  5104. (if nstars (format "\\{1,%d\\}" nstars) "+")
  5105. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  5106. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  5107. (bob-special (and org-cycle-global-at-bob (bobp)
  5108. (not (looking-at outline-regexp))))
  5109. (org-cycle-hook
  5110. (if bob-special
  5111. (delq 'org-optimize-window-after-visibility-change
  5112. (copy-sequence org-cycle-hook))
  5113. org-cycle-hook))
  5114. (pos (point)))
  5115. (if (or bob-special (equal arg '(4)))
  5116. ;; special case: use global cycling
  5117. (setq arg t))
  5118. (cond
  5119. ((equal arg '(16))
  5120. (org-set-startup-visibility)
  5121. (message "Startup visibility, plus VISIBILITY properties"))
  5122. ((equal arg '(64))
  5123. (show-all)
  5124. (message "Entire buffer visible, including drawers"))
  5125. ((org-at-table-p 'any)
  5126. ;; Enter the table or move to the next field in the table
  5127. (if (org-at-table.el-p)
  5128. (message "Use C-c ' to edit table.el tables")
  5129. (if arg (org-table-edit-field t)
  5130. (org-table-justify-field-maybe)
  5131. (call-interactively 'org-table-next-field))))
  5132. ((run-hook-with-args-until-success
  5133. 'org-tab-after-check-for-table-hook))
  5134. ((eq arg t) ;; Global cycling
  5135. (org-cycle-internal-global))
  5136. ((and org-drawers org-drawer-regexp
  5137. (save-excursion
  5138. (beginning-of-line 1)
  5139. (looking-at org-drawer-regexp)))
  5140. ;; Toggle block visibility
  5141. (org-flag-drawer
  5142. (not (get-char-property (match-end 0) 'invisible))))
  5143. ((integerp arg)
  5144. ;; Show-subtree, ARG levels up from here.
  5145. (save-excursion
  5146. (org-back-to-heading)
  5147. (outline-up-heading (if (< arg 0) (- arg)
  5148. (- (funcall outline-level) arg)))
  5149. (org-show-subtree)))
  5150. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5151. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5152. (org-cycle-internal-local))
  5153. ;; TAB emulation and template completion
  5154. (buffer-read-only (org-back-to-heading))
  5155. ((run-hook-with-args-until-success
  5156. 'org-tab-after-check-for-cycling-hook))
  5157. ((org-try-structure-completion))
  5158. ((org-try-cdlatex-tab))
  5159. ((run-hook-with-args-until-success
  5160. 'org-tab-before-tab-emulation-hook))
  5161. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5162. (or (not (bolp))
  5163. (not (looking-at outline-regexp))))
  5164. (call-interactively (global-key-binding "\t")))
  5165. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5166. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5167. (or (and (eq org-cycle-emulate-tab 'white)
  5168. (= (match-end 0) (point-at-eol)))
  5169. (and (eq org-cycle-emulate-tab 'whitestart)
  5170. (>= (match-end 0) pos))))
  5171. t
  5172. (eq org-cycle-emulate-tab t))
  5173. (call-interactively (global-key-binding "\t")))
  5174. (t (save-excursion
  5175. (org-back-to-heading)
  5176. (org-cycle)))))))
  5177. (defun org-cycle-internal-global ()
  5178. "Do the global cycling action."
  5179. (cond
  5180. ((and (eq last-command this-command)
  5181. (eq org-cycle-global-status 'overview))
  5182. ;; We just created the overview - now do table of contents
  5183. ;; This can be slow in very large buffers, so indicate action
  5184. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5185. (message "CONTENTS...")
  5186. (org-content)
  5187. (message "CONTENTS...done")
  5188. (setq org-cycle-global-status 'contents)
  5189. (run-hook-with-args 'org-cycle-hook 'contents))
  5190. ((and (eq last-command this-command)
  5191. (eq org-cycle-global-status 'contents))
  5192. ;; We just showed the table of contents - now show everything
  5193. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5194. (show-all)
  5195. (message "SHOW ALL")
  5196. (setq org-cycle-global-status 'all)
  5197. (run-hook-with-args 'org-cycle-hook 'all))
  5198. (t
  5199. ;; Default action: go to overview
  5200. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5201. (org-overview)
  5202. (message "OVERVIEW")
  5203. (setq org-cycle-global-status 'overview)
  5204. (run-hook-with-args 'org-cycle-hook 'overview))))
  5205. (defun org-cycle-internal-local ()
  5206. "Do the local cycling action."
  5207. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  5208. ;; First, some boundaries
  5209. (save-excursion
  5210. (org-back-to-heading)
  5211. (setq level (funcall outline-level))
  5212. (save-excursion
  5213. (beginning-of-line 2)
  5214. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  5215. ; XEmacs does not have `next-single-char-property-change'
  5216. ; I'm not sure about Emacs 21.
  5217. (while (and (not (eobp)) ;; this is like `next-line'
  5218. (get-char-property (1- (point)) 'invisible))
  5219. (beginning-of-line 2))
  5220. (while (and (not (eobp)) ;; this is like `next-line'
  5221. (get-char-property (1- (point)) 'invisible))
  5222. (goto-char (next-single-char-property-change (point) 'invisible))
  5223. (and (eolp) (beginning-of-line 2))))
  5224. (setq eol (point)))
  5225. (outline-end-of-heading) (setq eoh (point))
  5226. (save-excursion
  5227. (outline-next-heading)
  5228. (setq has-children (and (org-at-heading-p t)
  5229. (> (funcall outline-level) level))))
  5230. (org-end-of-subtree t)
  5231. (unless (eobp)
  5232. (skip-chars-forward " \t\n")
  5233. (beginning-of-line 1) ; in case this is an item
  5234. )
  5235. (setq eos (if (eobp) (point) (1- (point)))))
  5236. ;; Find out what to do next and set `this-command'
  5237. (cond
  5238. ((= eos eoh)
  5239. ;; Nothing is hidden behind this heading
  5240. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5241. (message "EMPTY ENTRY")
  5242. (setq org-cycle-subtree-status nil)
  5243. (save-excursion
  5244. (goto-char eos)
  5245. (outline-next-heading)
  5246. (if (org-invisible-p) (org-flag-heading nil))))
  5247. ((and (or (>= eol eos)
  5248. (not (string-match "\\S-" (buffer-substring eol eos))))
  5249. (or has-children
  5250. (not (setq children-skipped
  5251. org-cycle-skip-children-state-if-no-children))))
  5252. ;; Entire subtree is hidden in one line: children view
  5253. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5254. (org-show-entry)
  5255. (show-children)
  5256. (message "CHILDREN")
  5257. (save-excursion
  5258. (goto-char eos)
  5259. (outline-next-heading)
  5260. (if (org-invisible-p) (org-flag-heading nil)))
  5261. (setq org-cycle-subtree-status 'children)
  5262. (run-hook-with-args 'org-cycle-hook 'children))
  5263. ((or children-skipped
  5264. (and (eq last-command this-command)
  5265. (eq org-cycle-subtree-status 'children)))
  5266. ;; We just showed the children, or no children are there,
  5267. ;; now show everything.
  5268. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5269. (org-show-subtree)
  5270. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5271. (setq org-cycle-subtree-status 'subtree)
  5272. (run-hook-with-args 'org-cycle-hook 'subtree))
  5273. (t
  5274. ;; Default action: hide the subtree.
  5275. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5276. (hide-subtree)
  5277. (message "FOLDED")
  5278. (setq org-cycle-subtree-status 'folded)
  5279. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5280. ;;;###autoload
  5281. (defun org-global-cycle (&optional arg)
  5282. "Cycle the global visibility. For details see `org-cycle'.
  5283. With \\[universal-argument] prefix arg, switch to startup visibility.
  5284. With a numeric prefix, show all headlines up to that level."
  5285. (interactive "P")
  5286. (let ((org-cycle-include-plain-lists
  5287. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5288. (cond
  5289. ((integerp arg)
  5290. (show-all)
  5291. (hide-sublevels arg)
  5292. (setq org-cycle-global-status 'contents))
  5293. ((equal arg '(4))
  5294. (org-set-startup-visibility)
  5295. (message "Startup visibility, plus VISIBILITY properties."))
  5296. (t
  5297. (org-cycle '(4))))))
  5298. (defun org-set-startup-visibility ()
  5299. "Set the visibility required by startup options and properties."
  5300. (cond
  5301. ((eq org-startup-folded t)
  5302. (org-cycle '(4)))
  5303. ((eq org-startup-folded 'content)
  5304. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5305. (org-cycle '(4)) (org-cycle '(4)))))
  5306. (unless (eq org-startup-folded 'showeverything)
  5307. (if org-hide-block-startup (org-hide-block-all))
  5308. (org-set-visibility-according-to-property 'no-cleanup)
  5309. (org-cycle-hide-archived-subtrees 'all)
  5310. (org-cycle-hide-drawers 'all)
  5311. (org-cycle-show-empty-lines t)))
  5312. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5313. "Switch subtree visibilities according to :VISIBILITY: property."
  5314. (interactive)
  5315. (let (org-show-entry-below state)
  5316. (save-excursion
  5317. (goto-char (point-min))
  5318. (while (re-search-forward
  5319. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5320. nil t)
  5321. (setq state (match-string 1))
  5322. (save-excursion
  5323. (org-back-to-heading t)
  5324. (hide-subtree)
  5325. (org-reveal)
  5326. (cond
  5327. ((equal state '("fold" "folded"))
  5328. (hide-subtree))
  5329. ((equal state "children")
  5330. (org-show-hidden-entry)
  5331. (show-children))
  5332. ((equal state "content")
  5333. (save-excursion
  5334. (save-restriction
  5335. (org-narrow-to-subtree)
  5336. (org-content))))
  5337. ((member state '("all" "showall"))
  5338. (show-subtree)))))
  5339. (unless no-cleanup
  5340. (org-cycle-hide-archived-subtrees 'all)
  5341. (org-cycle-hide-drawers 'all)
  5342. (org-cycle-show-empty-lines 'all)))))
  5343. (defun org-overview ()
  5344. "Switch to overview mode, showing only top-level headlines.
  5345. Really, this shows all headlines with level equal or greater than the level
  5346. of the first headline in the buffer. This is important, because if the
  5347. first headline is not level one, then (hide-sublevels 1) gives confusing
  5348. results."
  5349. (interactive)
  5350. (let ((level (save-excursion
  5351. (goto-char (point-min))
  5352. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5353. (progn
  5354. (goto-char (match-beginning 0))
  5355. (funcall outline-level))))))
  5356. (and level (hide-sublevels level))))
  5357. (defun org-content (&optional arg)
  5358. "Show all headlines in the buffer, like a table of contents.
  5359. With numerical argument N, show content up to level N."
  5360. (interactive "P")
  5361. (save-excursion
  5362. ;; Visit all headings and show their offspring
  5363. (and (integerp arg) (org-overview))
  5364. (goto-char (point-max))
  5365. (catch 'exit
  5366. (while (and (progn (condition-case nil
  5367. (outline-previous-visible-heading 1)
  5368. (error (goto-char (point-min))))
  5369. t)
  5370. (looking-at outline-regexp))
  5371. (if (integerp arg)
  5372. (show-children (1- arg))
  5373. (show-branches))
  5374. (if (bobp) (throw 'exit nil))))))
  5375. (defun org-optimize-window-after-visibility-change (state)
  5376. "Adjust the window after a change in outline visibility.
  5377. This function is the default value of the hook `org-cycle-hook'."
  5378. (when (get-buffer-window (current-buffer))
  5379. (cond
  5380. ((eq state 'content) nil)
  5381. ((eq state 'all) nil)
  5382. ((eq state 'folded) nil)
  5383. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5384. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5385. (defun org-remove-empty-overlays-at (pos)
  5386. "Remove outline overlays that do not contain non-white stuff."
  5387. (mapc
  5388. (lambda (o)
  5389. (and (eq 'outline (overlay-get o 'invisible))
  5390. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5391. (overlay-end o))))
  5392. (delete-overlay o)))
  5393. (overlays-at pos)))
  5394. (defun org-clean-visibility-after-subtree-move ()
  5395. "Fix visibility issues after moving a subtree."
  5396. ;; First, find a reasonable region to look at:
  5397. ;; Start two siblings above, end three below
  5398. (let* ((beg (save-excursion
  5399. (and (org-get-last-sibling)
  5400. (org-get-last-sibling))
  5401. (point)))
  5402. (end (save-excursion
  5403. (and (org-get-next-sibling)
  5404. (org-get-next-sibling)
  5405. (org-get-next-sibling))
  5406. (if (org-at-heading-p)
  5407. (point-at-eol)
  5408. (point))))
  5409. (level (looking-at "\\*+"))
  5410. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5411. (save-excursion
  5412. (save-restriction
  5413. (narrow-to-region beg end)
  5414. (when re
  5415. ;; Properly fold already folded siblings
  5416. (goto-char (point-min))
  5417. (while (re-search-forward re nil t)
  5418. (if (and (not (org-invisible-p))
  5419. (save-excursion
  5420. (goto-char (point-at-eol)) (org-invisible-p)))
  5421. (hide-entry))))
  5422. (org-cycle-show-empty-lines 'overview)
  5423. (org-cycle-hide-drawers 'overview)))))
  5424. (defun org-cycle-show-empty-lines (state)
  5425. "Show empty lines above all visible headlines.
  5426. The region to be covered depends on STATE when called through
  5427. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5428. entire buffer covered. Note that an empty line is only shown if there
  5429. are at least `org-cycle-separator-lines' empty lines before the headline."
  5430. (when (not (= org-cycle-separator-lines 0))
  5431. (save-excursion
  5432. (let* ((n (abs org-cycle-separator-lines))
  5433. (re (cond
  5434. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5435. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5436. (t (let ((ns (number-to-string (- n 2))))
  5437. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5438. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5439. beg end b e)
  5440. (cond
  5441. ((memq state '(overview contents t))
  5442. (setq beg (point-min) end (point-max)))
  5443. ((memq state '(children folded))
  5444. (setq beg (point) end (progn (org-end-of-subtree t t)
  5445. (beginning-of-line 2)
  5446. (point)))))
  5447. (when beg
  5448. (goto-char beg)
  5449. (while (re-search-forward re end t)
  5450. (unless (get-char-property (match-end 1) 'invisible)
  5451. (setq e (match-end 1))
  5452. (if (< org-cycle-separator-lines 0)
  5453. (setq b (save-excursion
  5454. (goto-char (match-beginning 0))
  5455. (org-back-over-empty-lines)
  5456. (if (save-excursion
  5457. (goto-char (max (point-min) (1- (point))))
  5458. (org-on-heading-p))
  5459. (1- (point))
  5460. (point))))
  5461. (setq b (match-beginning 1)))
  5462. (outline-flag-region b e nil)))))))
  5463. ;; Never hide empty lines at the end of the file.
  5464. (save-excursion
  5465. (goto-char (point-max))
  5466. (outline-previous-heading)
  5467. (outline-end-of-heading)
  5468. (if (and (looking-at "[ \t\n]+")
  5469. (= (match-end 0) (point-max)))
  5470. (outline-flag-region (point) (match-end 0) nil))))
  5471. (defun org-show-empty-lines-in-parent ()
  5472. "Move to the parent and re-show empty lines before visible headlines."
  5473. (save-excursion
  5474. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5475. (org-cycle-show-empty-lines context))))
  5476. (defun org-files-list ()
  5477. "Return `org-agenda-files' list, plus all open org-mode files.
  5478. This is useful for operations that need to scan all of a user's
  5479. open and agenda-wise Org files."
  5480. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5481. (dolist (buf (buffer-list))
  5482. (with-current-buffer buf
  5483. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5484. (let ((file (expand-file-name (buffer-file-name))))
  5485. (unless (member file files)
  5486. (push file files))))))
  5487. files))
  5488. (defsubst org-entry-beginning-position ()
  5489. "Return the beginning position of the current entry."
  5490. (save-excursion (outline-back-to-heading t) (point)))
  5491. (defsubst org-entry-end-position ()
  5492. "Return the end position of the current entry."
  5493. (save-excursion (outline-next-heading) (point)))
  5494. (defun org-cycle-hide-drawers (state)
  5495. "Re-hide all drawers after a visibility state change."
  5496. (when (and (org-mode-p)
  5497. (not (memq state '(overview folded contents))))
  5498. (save-excursion
  5499. (let* ((globalp (memq state '(contents all)))
  5500. (beg (if globalp (point-min) (point)))
  5501. (end (if globalp (point-max)
  5502. (if (eq state 'children)
  5503. (save-excursion (outline-next-heading) (point))
  5504. (org-end-of-subtree t)))))
  5505. (goto-char beg)
  5506. (while (re-search-forward org-drawer-regexp end t)
  5507. (org-flag-drawer t))))))
  5508. (defun org-flag-drawer (flag)
  5509. (save-excursion
  5510. (beginning-of-line 1)
  5511. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5512. (let ((b (match-end 0))
  5513. (outline-regexp org-outline-regexp))
  5514. (if (re-search-forward
  5515. "^[ \t]*:END:"
  5516. (save-excursion (outline-next-heading) (point)) t)
  5517. (outline-flag-region b (point-at-eol) flag)
  5518. (error ":END: line missing at position %s" b))))))
  5519. (defun org-subtree-end-visible-p ()
  5520. "Is the end of the current subtree visible?"
  5521. (pos-visible-in-window-p
  5522. (save-excursion (org-end-of-subtree t) (point))))
  5523. (defun org-first-headline-recenter (&optional N)
  5524. "Move cursor to the first headline and recenter the headline.
  5525. Optional argument N means put the headline into the Nth line of the window."
  5526. (goto-char (point-min))
  5527. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5528. (beginning-of-line)
  5529. (recenter (prefix-numeric-value N))))
  5530. ;;; Saving and restoring visibility
  5531. (defun org-outline-overlay-data (&optional use-markers)
  5532. "Return a list of the locations of all outline overlays.
  5533. The are overlays with the `invisible' property value `outline'.
  5534. The return values is a list of cons cells, with start and stop
  5535. positions for each overlay.
  5536. If USE-MARKERS is set, return the positions as markers."
  5537. (let (beg end)
  5538. (save-excursion
  5539. (save-restriction
  5540. (widen)
  5541. (delq nil
  5542. (mapcar (lambda (o)
  5543. (when (eq (overlay-get o 'invisible) 'outline)
  5544. (setq beg (overlay-start o)
  5545. end (overlay-end o))
  5546. (and beg end (> end beg)
  5547. (if use-markers
  5548. (cons (move-marker (make-marker) beg)
  5549. (move-marker (make-marker) end))
  5550. (cons beg end)))))
  5551. (overlays-in (point-min) (point-max))))))))
  5552. (defun org-set-outline-overlay-data (data)
  5553. "Create visibility overlays for all positions in DATA.
  5554. DATA should have been made by `org-outline-overlay-data'."
  5555. (let (o)
  5556. (save-excursion
  5557. (save-restriction
  5558. (widen)
  5559. (show-all)
  5560. (mapc (lambda (c)
  5561. (setq o (make-overlay (car c) (cdr c)))
  5562. (overlay-put o 'invisible 'outline))
  5563. data)))))
  5564. (defmacro org-save-outline-visibility (use-markers &rest body)
  5565. "Save and restore outline visibility around BODY.
  5566. If USE-MARKERS is non-nil, use markers for the positions.
  5567. This means that the buffer may change while running BODY,
  5568. but it also means that the buffer should stay alive
  5569. during the operation, because otherwise all these markers will
  5570. point nowhere."
  5571. (declare (indent 1))
  5572. `(let ((data (org-outline-overlay-data ,use-markers)))
  5573. (unwind-protect
  5574. (progn
  5575. ,@body
  5576. (org-set-outline-overlay-data data))
  5577. (when ,use-markers
  5578. (mapc (lambda (c)
  5579. (and (markerp (car c)) (move-marker (car c) nil))
  5580. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5581. data)))))
  5582. ;;; Folding of blocks
  5583. (defconst org-block-regexp
  5584. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5585. "Regular expression for hiding blocks.")
  5586. (defvar org-hide-block-overlays nil
  5587. "Overlays hiding blocks.")
  5588. (make-variable-buffer-local 'org-hide-block-overlays)
  5589. (defun org-block-map (function &optional start end)
  5590. "Call FUNCTION at the head of all source blocks in the current buffer.
  5591. Optional arguments START and END can be used to limit the range."
  5592. (let ((start (or start (point-min)))
  5593. (end (or end (point-max))))
  5594. (save-excursion
  5595. (goto-char start)
  5596. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5597. (save-excursion
  5598. (save-match-data
  5599. (goto-char (match-beginning 0))
  5600. (funcall function)))))))
  5601. (defun org-hide-block-toggle-all ()
  5602. "Toggle the visibility of all blocks in the current buffer."
  5603. (org-block-map #'org-hide-block-toggle))
  5604. (defun org-hide-block-all ()
  5605. "Fold all blocks in the current buffer."
  5606. (interactive)
  5607. (org-show-block-all)
  5608. (org-block-map #'org-hide-block-toggle-maybe))
  5609. (defun org-show-block-all ()
  5610. "Unfold all blocks in the current buffer."
  5611. (interactive)
  5612. (mapc 'delete-overlay org-hide-block-overlays)
  5613. (setq org-hide-block-overlays nil))
  5614. (defun org-hide-block-toggle-maybe ()
  5615. "Toggle visibility of block at point."
  5616. (interactive)
  5617. (let ((case-fold-search t))
  5618. (if (save-excursion
  5619. (beginning-of-line 1)
  5620. (looking-at org-block-regexp))
  5621. (progn (org-hide-block-toggle)
  5622. t) ;; to signal that we took action
  5623. nil))) ;; to signal that we did not
  5624. (defun org-hide-block-toggle (&optional force)
  5625. "Toggle the visibility of the current block."
  5626. (interactive)
  5627. (save-excursion
  5628. (beginning-of-line)
  5629. (if (re-search-forward org-block-regexp nil t)
  5630. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5631. (end (match-end 0)) ;; end of entire body
  5632. ov)
  5633. (if (memq t (mapcar (lambda (overlay)
  5634. (eq (overlay-get overlay 'invisible)
  5635. 'org-hide-block))
  5636. (overlays-at start)))
  5637. (if (or (not force) (eq force 'off))
  5638. (mapc (lambda (ov)
  5639. (when (member ov org-hide-block-overlays)
  5640. (setq org-hide-block-overlays
  5641. (delq ov org-hide-block-overlays)))
  5642. (when (eq (overlay-get ov 'invisible)
  5643. 'org-hide-block)
  5644. (delete-overlay ov)))
  5645. (overlays-at start)))
  5646. (setq ov (make-overlay start end))
  5647. (overlay-put ov 'invisible 'org-hide-block)
  5648. ;; make the block accessible to isearch
  5649. (overlay-put
  5650. ov 'isearch-open-invisible
  5651. (lambda (ov)
  5652. (when (member ov org-hide-block-overlays)
  5653. (setq org-hide-block-overlays
  5654. (delq ov org-hide-block-overlays)))
  5655. (when (eq (overlay-get ov 'invisible)
  5656. 'org-hide-block)
  5657. (delete-overlay ov))))
  5658. (push ov org-hide-block-overlays)))
  5659. (error "Not looking at a source block"))))
  5660. ;; org-tab-after-check-for-cycling-hook
  5661. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5662. ;; Remove overlays when changing major mode
  5663. (add-hook 'org-mode-hook
  5664. (lambda () (org-add-hook 'change-major-mode-hook
  5665. 'org-show-block-all 'append 'local)))
  5666. ;;; Org-goto
  5667. (defvar org-goto-window-configuration nil)
  5668. (defvar org-goto-marker nil)
  5669. (defvar org-goto-map
  5670. (let ((map (make-sparse-keymap)))
  5671. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5672. (while (setq cmd (pop cmds))
  5673. (substitute-key-definition cmd cmd map global-map)))
  5674. (suppress-keymap map)
  5675. (org-defkey map "\C-m" 'org-goto-ret)
  5676. (org-defkey map [(return)] 'org-goto-ret)
  5677. (org-defkey map [(left)] 'org-goto-left)
  5678. (org-defkey map [(right)] 'org-goto-right)
  5679. (org-defkey map [(control ?g)] 'org-goto-quit)
  5680. (org-defkey map "\C-i" 'org-cycle)
  5681. (org-defkey map [(tab)] 'org-cycle)
  5682. (org-defkey map [(down)] 'outline-next-visible-heading)
  5683. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5684. (if org-goto-auto-isearch
  5685. (if (fboundp 'define-key-after)
  5686. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5687. nil)
  5688. (org-defkey map "q" 'org-goto-quit)
  5689. (org-defkey map "n" 'outline-next-visible-heading)
  5690. (org-defkey map "p" 'outline-previous-visible-heading)
  5691. (org-defkey map "f" 'outline-forward-same-level)
  5692. (org-defkey map "b" 'outline-backward-same-level)
  5693. (org-defkey map "u" 'outline-up-heading))
  5694. (org-defkey map "/" 'org-occur)
  5695. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5696. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5697. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5698. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5699. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5700. map))
  5701. (defconst org-goto-help
  5702. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5703. RET=jump to location [Q]uit and return to previous location
  5704. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5705. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5706. ;; FIXME: Docstring does not mention both interfaces
  5707. (defun org-goto (&optional alternative-interface)
  5708. "Look up a different location in the current file, keeping current visibility.
  5709. When you want look-up or go to a different location in a document, the
  5710. fastest way is often to fold the entire buffer and then dive into the tree.
  5711. This method has the disadvantage, that the previous location will be folded,
  5712. which may not be what you want.
  5713. This command works around this by showing a copy of the current buffer
  5714. in an indirect buffer, in overview mode. You can dive into the tree in
  5715. that copy, use org-occur and incremental search to find a location.
  5716. When pressing RET or `Q', the command returns to the original buffer in
  5717. which the visibility is still unchanged. After RET is will also jump to
  5718. the location selected in the indirect buffer and expose the
  5719. the headline hierarchy above."
  5720. (interactive "P")
  5721. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5722. (org-refile-use-outline-path t)
  5723. (org-refile-target-verify-function nil)
  5724. (interface
  5725. (if (not alternative-interface)
  5726. org-goto-interface
  5727. (if (eq org-goto-interface 'outline)
  5728. 'outline-path-completion
  5729. 'outline)))
  5730. (org-goto-start-pos (point))
  5731. (selected-point
  5732. (if (eq interface 'outline)
  5733. (car (org-get-location (current-buffer) org-goto-help))
  5734. (nth 3 (org-refile-get-location "Goto: ")))))
  5735. (if selected-point
  5736. (progn
  5737. (org-mark-ring-push org-goto-start-pos)
  5738. (goto-char selected-point)
  5739. (if (or (org-invisible-p) (org-invisible-p2))
  5740. (org-show-context 'org-goto)))
  5741. (message "Quit"))))
  5742. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5743. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5744. (defvar org-goto-local-auto-isearch-map) ; defined below
  5745. (defun org-get-location (buf help)
  5746. "Let the user select a location in the Org-mode buffer BUF.
  5747. This function uses a recursive edit. It returns the selected position
  5748. or nil."
  5749. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5750. (isearch-hide-immediately nil)
  5751. (isearch-search-fun-function
  5752. (lambda () 'org-goto-local-search-headings))
  5753. (org-goto-selected-point org-goto-exit-command)
  5754. (pop-up-frames nil)
  5755. (special-display-buffer-names nil)
  5756. (special-display-regexps nil)
  5757. (special-display-function nil))
  5758. (save-excursion
  5759. (save-window-excursion
  5760. (delete-other-windows)
  5761. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5762. (switch-to-buffer
  5763. (condition-case nil
  5764. (make-indirect-buffer (current-buffer) "*org-goto*")
  5765. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5766. (with-output-to-temp-buffer "*Help*"
  5767. (princ help))
  5768. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5769. (setq buffer-read-only nil)
  5770. (let ((org-startup-truncated t)
  5771. (org-startup-folded nil)
  5772. (org-startup-align-all-tables nil))
  5773. (org-mode)
  5774. (org-overview))
  5775. (setq buffer-read-only t)
  5776. (if (and (boundp 'org-goto-start-pos)
  5777. (integer-or-marker-p org-goto-start-pos))
  5778. (let ((org-show-hierarchy-above t)
  5779. (org-show-siblings t)
  5780. (org-show-following-heading t))
  5781. (goto-char org-goto-start-pos)
  5782. (and (org-invisible-p) (org-show-context)))
  5783. (goto-char (point-min)))
  5784. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5785. (message "Select location and press RET")
  5786. (use-local-map org-goto-map)
  5787. (recursive-edit)
  5788. ))
  5789. (kill-buffer "*org-goto*")
  5790. (cons org-goto-selected-point org-goto-exit-command)))
  5791. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5792. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5793. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5794. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5795. (defun org-goto-local-search-headings (string bound noerror)
  5796. "Search and make sure that any matches are in headlines."
  5797. (catch 'return
  5798. (while (if isearch-forward
  5799. (search-forward string bound noerror)
  5800. (search-backward string bound noerror))
  5801. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5802. (and (member :headline context)
  5803. (not (member :tags context))))
  5804. (throw 'return (point))))))
  5805. (defun org-goto-local-auto-isearch ()
  5806. "Start isearch."
  5807. (interactive)
  5808. (goto-char (point-min))
  5809. (let ((keys (this-command-keys)))
  5810. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5811. (isearch-mode t)
  5812. (isearch-process-search-char (string-to-char keys)))))
  5813. (defun org-goto-ret (&optional arg)
  5814. "Finish `org-goto' by going to the new location."
  5815. (interactive "P")
  5816. (setq org-goto-selected-point (point)
  5817. org-goto-exit-command 'return)
  5818. (throw 'exit nil))
  5819. (defun org-goto-left ()
  5820. "Finish `org-goto' by going to the new location."
  5821. (interactive)
  5822. (if (org-on-heading-p)
  5823. (progn
  5824. (beginning-of-line 1)
  5825. (setq org-goto-selected-point (point)
  5826. org-goto-exit-command 'left)
  5827. (throw 'exit nil))
  5828. (error "Not on a heading")))
  5829. (defun org-goto-right ()
  5830. "Finish `org-goto' by going to the new location."
  5831. (interactive)
  5832. (if (org-on-heading-p)
  5833. (progn
  5834. (setq org-goto-selected-point (point)
  5835. org-goto-exit-command 'right)
  5836. (throw 'exit nil))
  5837. (error "Not on a heading")))
  5838. (defun org-goto-quit ()
  5839. "Finish `org-goto' without cursor motion."
  5840. (interactive)
  5841. (setq org-goto-selected-point nil)
  5842. (setq org-goto-exit-command 'quit)
  5843. (throw 'exit nil))
  5844. ;;; Indirect buffer display of subtrees
  5845. (defvar org-indirect-dedicated-frame nil
  5846. "This is the frame being used for indirect tree display.")
  5847. (defvar org-last-indirect-buffer nil)
  5848. (defun org-tree-to-indirect-buffer (&optional arg)
  5849. "Create indirect buffer and narrow it to current subtree.
  5850. With numerical prefix ARG, go up to this level and then take that tree.
  5851. If ARG is negative, go up that many levels.
  5852. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5853. indirect buffer previously made with this command, to avoid proliferation of
  5854. indirect buffers. However, when you call the command with a \
  5855. \\[universal-argument] prefix, or
  5856. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5857. is kept so that you can work with several indirect buffers at the same time.
  5858. If `org-indirect-buffer-display' is `dedicated-frame', the \
  5859. \\[universal-argument] prefix also
  5860. requests that a new frame be made for the new buffer, so that the dedicated
  5861. frame is not changed."
  5862. (interactive "P")
  5863. (let ((cbuf (current-buffer))
  5864. (cwin (selected-window))
  5865. (pos (point))
  5866. beg end level heading ibuf)
  5867. (save-excursion
  5868. (org-back-to-heading t)
  5869. (when (numberp arg)
  5870. (setq level (org-outline-level))
  5871. (if (< arg 0) (setq arg (+ level arg)))
  5872. (while (> (setq level (org-outline-level)) arg)
  5873. (outline-up-heading 1 t)))
  5874. (setq beg (point)
  5875. heading (org-get-heading))
  5876. (org-end-of-subtree t t)
  5877. (if (org-on-heading-p) (backward-char 1))
  5878. (setq end (point)))
  5879. (if (and (buffer-live-p org-last-indirect-buffer)
  5880. (not (eq org-indirect-buffer-display 'new-frame))
  5881. (not arg))
  5882. (kill-buffer org-last-indirect-buffer))
  5883. (setq ibuf (org-get-indirect-buffer cbuf)
  5884. org-last-indirect-buffer ibuf)
  5885. (cond
  5886. ((or (eq org-indirect-buffer-display 'new-frame)
  5887. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5888. (select-frame (make-frame))
  5889. (delete-other-windows)
  5890. (switch-to-buffer ibuf)
  5891. (org-set-frame-title heading))
  5892. ((eq org-indirect-buffer-display 'dedicated-frame)
  5893. (raise-frame
  5894. (select-frame (or (and org-indirect-dedicated-frame
  5895. (frame-live-p org-indirect-dedicated-frame)
  5896. org-indirect-dedicated-frame)
  5897. (setq org-indirect-dedicated-frame (make-frame)))))
  5898. (delete-other-windows)
  5899. (switch-to-buffer ibuf)
  5900. (org-set-frame-title (concat "Indirect: " heading)))
  5901. ((eq org-indirect-buffer-display 'current-window)
  5902. (switch-to-buffer ibuf))
  5903. ((eq org-indirect-buffer-display 'other-window)
  5904. (pop-to-buffer ibuf))
  5905. (t (error "Invalid value")))
  5906. (if (featurep 'xemacs)
  5907. (save-excursion (org-mode) (turn-on-font-lock)))
  5908. (narrow-to-region beg end)
  5909. (show-all)
  5910. (goto-char pos)
  5911. (and (window-live-p cwin) (select-window cwin))))
  5912. (defun org-get-indirect-buffer (&optional buffer)
  5913. (setq buffer (or buffer (current-buffer)))
  5914. (let ((n 1) (base (buffer-name buffer)) bname)
  5915. (while (buffer-live-p
  5916. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5917. (setq n (1+ n)))
  5918. (condition-case nil
  5919. (make-indirect-buffer buffer bname 'clone)
  5920. (error (make-indirect-buffer buffer bname)))))
  5921. (defun org-set-frame-title (title)
  5922. "Set the title of the current frame to the string TITLE."
  5923. ;; FIXME: how to name a single frame in XEmacs???
  5924. (unless (featurep 'xemacs)
  5925. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5926. ;;;; Structure editing
  5927. ;;; Inserting headlines
  5928. (defun org-previous-line-empty-p ()
  5929. (save-excursion
  5930. (and (not (bobp))
  5931. (or (beginning-of-line 0) t)
  5932. (save-match-data
  5933. (looking-at "[ \t]*$")))))
  5934. (defun org-insert-heading (&optional force-heading invisible-ok)
  5935. "Insert a new heading or item with same depth at point.
  5936. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5937. If point is at the beginning of a headline, insert a sibling before the
  5938. current headline. If point is not at the beginning, do not split the line,
  5939. but create the new headline after the current line.
  5940. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5941. This is important for non-interactive uses of the command."
  5942. (interactive "P")
  5943. (if (or (= (buffer-size) 0)
  5944. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5945. (org-on-heading-p))))
  5946. (not (org-in-item-p))))
  5947. (insert "\n* ")
  5948. (when (or force-heading (not (org-insert-item)))
  5949. (let* ((empty-line-p nil)
  5950. (head (save-excursion
  5951. (condition-case nil
  5952. (progn
  5953. (org-back-to-heading invisible-ok)
  5954. (setq empty-line-p (org-previous-line-empty-p))
  5955. (match-string 0))
  5956. (error "*"))))
  5957. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5958. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5959. pos hide-previous previous-pos)
  5960. (cond
  5961. ((and (org-on-heading-p) (bolp)
  5962. (or (bobp)
  5963. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5964. ;; insert before the current line
  5965. (open-line (if blank 2 1)))
  5966. ((and (bolp)
  5967. (not org-insert-heading-respect-content)
  5968. (or (bobp)
  5969. (save-excursion
  5970. (backward-char 1) (not (org-invisible-p)))))
  5971. ;; insert right here
  5972. nil)
  5973. (t
  5974. ;; somewhere in the line
  5975. (save-excursion
  5976. (setq previous-pos (point-at-bol))
  5977. (end-of-line)
  5978. (setq hide-previous (org-invisible-p)))
  5979. (and org-insert-heading-respect-content (org-show-subtree))
  5980. (let ((split
  5981. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5982. (save-excursion
  5983. (let ((p (point)))
  5984. (goto-char (point-at-bol))
  5985. (and (looking-at org-complex-heading-regexp)
  5986. (> p (match-beginning 4)))))))
  5987. tags pos)
  5988. (cond
  5989. (org-insert-heading-respect-content
  5990. (org-end-of-subtree nil t)
  5991. (or (bolp) (newline))
  5992. (or (org-previous-line-empty-p)
  5993. (and blank (newline)))
  5994. (open-line 1))
  5995. ((org-on-heading-p)
  5996. (when hide-previous
  5997. (show-children)
  5998. (org-show-entry))
  5999. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  6000. (setq tags (and (match-end 2) (match-string 2)))
  6001. (and (match-end 1)
  6002. (delete-region (match-beginning 1) (match-end 1)))
  6003. (setq pos (point-at-bol))
  6004. (or split (end-of-line 1))
  6005. (delete-horizontal-space)
  6006. (if (string-match "\\`\\*+\\'"
  6007. (buffer-substring (point-at-bol) (point)))
  6008. (insert " "))
  6009. (newline (if blank 2 1))
  6010. (when tags
  6011. (save-excursion
  6012. (goto-char pos)
  6013. (end-of-line 1)
  6014. (insert " " tags)
  6015. (org-set-tags nil 'align))))
  6016. (t
  6017. (or split (end-of-line 1))
  6018. (newline (if blank 2 1)))))))
  6019. (insert head) (just-one-space)
  6020. (setq pos (point))
  6021. (end-of-line 1)
  6022. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6023. (when (and org-insert-heading-respect-content hide-previous)
  6024. (save-excursion
  6025. (goto-char previous-pos)
  6026. (hide-subtree)))
  6027. (run-hooks 'org-insert-heading-hook)))))
  6028. (defun org-get-heading (&optional no-tags)
  6029. "Return the heading of the current entry, without the stars."
  6030. (save-excursion
  6031. (org-back-to-heading t)
  6032. (if (looking-at
  6033. (if no-tags
  6034. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  6035. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6036. (match-string 1) "")))
  6037. (defun org-heading-components ()
  6038. "Return the components of the current heading.
  6039. This is a list with the following elements:
  6040. - the level as an integer
  6041. - the reduced level, different if `org-odd-levels-only' is set.
  6042. - the TODO keyword, or nil
  6043. - the priority character, like ?A, or nil if no priority is given
  6044. - the headline text itself, or the tags string if no headline text
  6045. - the tags string, or nil."
  6046. (save-excursion
  6047. (org-back-to-heading t)
  6048. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6049. (list (length (match-string 1))
  6050. (org-reduced-level (length (match-string 1)))
  6051. (org-match-string-no-properties 2)
  6052. (and (match-end 3) (aref (match-string 3) 2))
  6053. (org-match-string-no-properties 4)
  6054. (org-match-string-no-properties 5)))))
  6055. (defun org-get-entry ()
  6056. "Get the entry text, after heading, entire subtree."
  6057. (save-excursion
  6058. (org-back-to-heading t)
  6059. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6060. (defun org-insert-heading-after-current ()
  6061. "Insert a new heading with same level as current, after current subtree."
  6062. (interactive)
  6063. (org-back-to-heading)
  6064. (org-insert-heading)
  6065. (org-move-subtree-down)
  6066. (end-of-line 1))
  6067. (defun org-insert-heading-respect-content ()
  6068. (interactive)
  6069. (let ((org-insert-heading-respect-content t))
  6070. (org-insert-heading t)))
  6071. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6072. (interactive "P")
  6073. (let ((org-insert-heading-respect-content t))
  6074. (org-insert-todo-heading force-state t)))
  6075. (defun org-insert-todo-heading (arg &optional force-heading)
  6076. "Insert a new heading with the same level and TODO state as current heading.
  6077. If the heading has no TODO state, or if the state is DONE, use the first
  6078. state (TODO by default). Also with prefix arg, force first state."
  6079. (interactive "P")
  6080. (when (or force-heading (not (org-insert-item 'checkbox)))
  6081. (org-insert-heading force-heading)
  6082. (save-excursion
  6083. (org-back-to-heading)
  6084. (outline-previous-heading)
  6085. (looking-at org-todo-line-regexp))
  6086. (let*
  6087. ((new-mark-x
  6088. (if (or arg
  6089. (not (match-beginning 2))
  6090. (member (match-string 2) org-done-keywords))
  6091. (car org-todo-keywords-1)
  6092. (match-string 2)))
  6093. (new-mark
  6094. (or
  6095. (run-hook-with-args-until-success
  6096. 'org-todo-get-default-hook new-mark-x nil)
  6097. new-mark-x)))
  6098. (beginning-of-line 1)
  6099. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6100. (if org-treat-insert-todo-heading-as-state-change
  6101. (org-todo new-mark)
  6102. (insert new-mark " "))))
  6103. (when org-provide-todo-statistics
  6104. (org-update-parent-todo-statistics))))
  6105. (defun org-insert-subheading (arg)
  6106. "Insert a new subheading and demote it.
  6107. Works for outline headings and for plain lists alike."
  6108. (interactive "P")
  6109. (org-insert-heading arg)
  6110. (cond
  6111. ((org-on-heading-p) (org-do-demote))
  6112. ((org-at-item-p) (org-indent-item 1))))
  6113. (defun org-insert-todo-subheading (arg)
  6114. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6115. Works for outline headings and for plain lists alike."
  6116. (interactive "P")
  6117. (org-insert-todo-heading arg)
  6118. (cond
  6119. ((org-on-heading-p) (org-do-demote))
  6120. ((org-at-item-p) (org-indent-item 1))))
  6121. ;;; Promotion and Demotion
  6122. (defvar org-after-demote-entry-hook nil
  6123. "Hook run after an entry has been demoted.
  6124. The cursor will be at the beginning of the entry.
  6125. When a subtree is being demoted, the hook will be called for each node.")
  6126. (defvar org-after-promote-entry-hook nil
  6127. "Hook run after an entry has been promoted.
  6128. The cursor will be at the beginning of the entry.
  6129. When a subtree is being promoted, the hook will be called for each node.")
  6130. (defun org-promote-subtree ()
  6131. "Promote the entire subtree.
  6132. See also `org-promote'."
  6133. (interactive)
  6134. (save-excursion
  6135. (org-map-tree 'org-promote))
  6136. (org-fix-position-after-promote))
  6137. (defun org-demote-subtree ()
  6138. "Demote the entire subtree. See `org-demote'.
  6139. See also `org-promote'."
  6140. (interactive)
  6141. (save-excursion
  6142. (org-map-tree 'org-demote))
  6143. (org-fix-position-after-promote))
  6144. (defun org-do-promote ()
  6145. "Promote the current heading higher up the tree.
  6146. If the region is active in `transient-mark-mode', promote all headings
  6147. in the region."
  6148. (interactive)
  6149. (save-excursion
  6150. (if (org-region-active-p)
  6151. (org-map-region 'org-promote (region-beginning) (region-end))
  6152. (org-promote)))
  6153. (org-fix-position-after-promote))
  6154. (defun org-do-demote ()
  6155. "Demote the current heading lower down the tree.
  6156. If the region is active in `transient-mark-mode', demote all headings
  6157. in the region."
  6158. (interactive)
  6159. (save-excursion
  6160. (if (org-region-active-p)
  6161. (org-map-region 'org-demote (region-beginning) (region-end))
  6162. (org-demote)))
  6163. (org-fix-position-after-promote))
  6164. (defun org-fix-position-after-promote ()
  6165. "Make sure that after pro/demotion cursor position is right."
  6166. (let ((pos (point)))
  6167. (when (save-excursion
  6168. (beginning-of-line 1)
  6169. (looking-at org-todo-line-regexp)
  6170. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6171. (cond ((eobp) (insert " "))
  6172. ((eolp) (insert " "))
  6173. ((equal (char-after) ?\ ) (forward-char 1))))))
  6174. (defun org-current-level ()
  6175. "Return the level of the current entry, or nil if before the first headline.
  6176. The level is the number of stars at the beginning of the headline."
  6177. (save-excursion
  6178. (condition-case nil
  6179. (progn
  6180. (org-back-to-heading t)
  6181. (funcall outline-level))
  6182. (error nil))))
  6183. (defun org-get-previous-line-level ()
  6184. "Return the outline depth of the last headline before the current line.
  6185. Returns 0 for the first headline in the buffer, and nil if before the
  6186. first headline."
  6187. (let ((current-level (org-current-level))
  6188. (prev-level (when (> (line-number-at-pos) 1)
  6189. (save-excursion
  6190. (beginning-of-line 0)
  6191. (org-current-level)))))
  6192. (cond ((null current-level) nil) ; Before first headline
  6193. ((null prev-level) 0) ; At first headline
  6194. (prev-level))))
  6195. (defun org-reduced-level (l)
  6196. "Compute the effective level of a heading.
  6197. This takes into account the setting of `org-odd-levels-only'."
  6198. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6199. (defun org-level-increment ()
  6200. "Return the number of stars that will be added or removed at a
  6201. time to headlines when structure editing, based on the value of
  6202. `org-odd-levels-only'."
  6203. (if org-odd-levels-only 2 1))
  6204. (defun org-get-valid-level (level &optional change)
  6205. "Rectify a level change under the influence of `org-odd-levels-only'
  6206. LEVEL is a current level, CHANGE is by how much the level should be
  6207. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6208. even level numbers will become the next higher odd number."
  6209. (if org-odd-levels-only
  6210. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6211. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6212. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6213. (max 1 (+ level (or change 0)))))
  6214. (if (boundp 'define-obsolete-function-alias)
  6215. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6216. (define-obsolete-function-alias 'org-get-legal-level
  6217. 'org-get-valid-level)
  6218. (define-obsolete-function-alias 'org-get-legal-level
  6219. 'org-get-valid-level "23.1")))
  6220. (defun org-promote ()
  6221. "Promote the current heading higher up the tree.
  6222. If the region is active in `transient-mark-mode', promote all headings
  6223. in the region."
  6224. (org-back-to-heading t)
  6225. (let* ((level (save-match-data (funcall outline-level)))
  6226. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6227. (diff (abs (- level (length up-head) -1))))
  6228. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6229. (replace-match up-head nil t)
  6230. ;; Fixup tag positioning
  6231. (and org-auto-align-tags (org-set-tags nil t))
  6232. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6233. (run-hooks 'org-after-promote-entry-hook)))
  6234. (defun org-demote ()
  6235. "Demote the current heading lower down the tree.
  6236. If the region is active in `transient-mark-mode', demote all headings
  6237. in the region."
  6238. (org-back-to-heading t)
  6239. (let* ((level (save-match-data (funcall outline-level)))
  6240. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6241. (diff (abs (- level (length down-head) -1))))
  6242. (replace-match down-head nil t)
  6243. ;; Fixup tag positioning
  6244. (and org-auto-align-tags (org-set-tags nil t))
  6245. (if org-adapt-indentation (org-fixup-indentation diff))
  6246. (run-hooks 'org-after-demote-entry-hook)))
  6247. (defun org-cycle-level ()
  6248. "Cycle the level of an empty headline through possible states.
  6249. This goes first to child, then to parent, level, then up the hierarchy.
  6250. After top level, it switches back to sibling level."
  6251. (interactive)
  6252. (let ((org-adapt-indentation nil))
  6253. (when (org-point-at-end-of-empty-headline)
  6254. (setq this-command 'org-cycle-level) ; Only needed for caching
  6255. (let ((cur-level (org-current-level))
  6256. (prev-level (org-get-previous-line-level)))
  6257. (cond
  6258. ;; If first headline in file, promote to top-level.
  6259. ((= prev-level 0)
  6260. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6261. do (org-do-promote)))
  6262. ;; If same level as prev, demote one.
  6263. ((= prev-level cur-level)
  6264. (org-do-demote))
  6265. ;; If parent is top-level, promote to top level if not already.
  6266. ((= prev-level 1)
  6267. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6268. do (org-do-promote)))
  6269. ;; If top-level, return to prev-level.
  6270. ((= cur-level 1)
  6271. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6272. do (org-do-demote)))
  6273. ;; If less than prev-level, promote one.
  6274. ((< cur-level prev-level)
  6275. (org-do-promote))
  6276. ;; If deeper than prev-level, promote until higher than
  6277. ;; prev-level.
  6278. ((> cur-level prev-level)
  6279. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6280. do (org-do-promote))))
  6281. t))))
  6282. (defun org-map-tree (fun)
  6283. "Call FUN for every heading underneath the current one."
  6284. (org-back-to-heading)
  6285. (let ((level (funcall outline-level)))
  6286. (save-excursion
  6287. (funcall fun)
  6288. (while (and (progn
  6289. (outline-next-heading)
  6290. (> (funcall outline-level) level))
  6291. (not (eobp)))
  6292. (funcall fun)))))
  6293. (defun org-map-region (fun beg end)
  6294. "Call FUN for every heading between BEG and END."
  6295. (let ((org-ignore-region t))
  6296. (save-excursion
  6297. (setq end (copy-marker end))
  6298. (goto-char beg)
  6299. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6300. (< (point) end))
  6301. (funcall fun))
  6302. (while (and (progn
  6303. (outline-next-heading)
  6304. (< (point) end))
  6305. (not (eobp)))
  6306. (funcall fun)))))
  6307. (defun org-fixup-indentation (diff)
  6308. "Change the indentation in the current entry by DIFF.
  6309. However, if any line in the current entry has no indentation, or if it
  6310. would end up with no indentation after the change, nothing at all is done."
  6311. (save-excursion
  6312. (let ((end (save-excursion (outline-next-heading)
  6313. (point-marker)))
  6314. (prohibit (if (> diff 0)
  6315. "^\\S-"
  6316. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6317. col)
  6318. (unless (save-excursion (end-of-line 1)
  6319. (re-search-forward prohibit end t))
  6320. (while (and (< (point) end)
  6321. (re-search-forward "^[ \t]+" end t))
  6322. (goto-char (match-end 0))
  6323. (setq col (current-column))
  6324. (if (< diff 0) (replace-match ""))
  6325. (org-indent-to-column (+ diff col))))
  6326. (move-marker end nil))))
  6327. (defun org-convert-to-odd-levels ()
  6328. "Convert an org-mode file with all levels allowed to one with odd levels.
  6329. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6330. level 5 etc."
  6331. (interactive)
  6332. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6333. (let ((outline-regexp org-outline-regexp)
  6334. (outline-level 'org-outline-level)
  6335. (org-odd-levels-only nil) n)
  6336. (save-excursion
  6337. (goto-char (point-min))
  6338. (while (re-search-forward "^\\*\\*+ " nil t)
  6339. (setq n (- (length (match-string 0)) 2))
  6340. (while (>= (setq n (1- n)) 0)
  6341. (org-demote))
  6342. (end-of-line 1))))))
  6343. (defun org-convert-to-oddeven-levels ()
  6344. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6345. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6346. file contains a section with an even level, conversion would
  6347. destroy the structure of the file. An error is signaled in this
  6348. case."
  6349. (interactive)
  6350. (goto-char (point-min))
  6351. ;; First check if there are no even levels
  6352. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6353. (org-show-context t)
  6354. (error "Not all levels are odd in this file. Conversion not possible"))
  6355. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6356. (let ((outline-regexp org-outline-regexp)
  6357. (outline-level 'org-outline-level)
  6358. (org-odd-levels-only nil) n)
  6359. (save-excursion
  6360. (goto-char (point-min))
  6361. (while (re-search-forward "^\\*\\*+ " nil t)
  6362. (setq n (/ (1- (length (match-string 0))) 2))
  6363. (while (>= (setq n (1- n)) 0)
  6364. (org-promote))
  6365. (end-of-line 1))))))
  6366. (defun org-tr-level (n)
  6367. "Make N odd if required."
  6368. (if org-odd-levels-only (1+ (/ n 2)) n))
  6369. ;;; Vertical tree motion, cutting and pasting of subtrees
  6370. (defun org-move-subtree-up (&optional arg)
  6371. "Move the current subtree up past ARG headlines of the same level."
  6372. (interactive "p")
  6373. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6374. (defun org-move-subtree-down (&optional arg)
  6375. "Move the current subtree down past ARG headlines of the same level."
  6376. (interactive "p")
  6377. (setq arg (prefix-numeric-value arg))
  6378. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6379. 'org-get-last-sibling))
  6380. (ins-point (make-marker))
  6381. (cnt (abs arg))
  6382. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6383. ;; Select the tree
  6384. (org-back-to-heading)
  6385. (setq beg0 (point))
  6386. (save-excursion
  6387. (setq ne-beg (org-back-over-empty-lines))
  6388. (setq beg (point)))
  6389. (save-match-data
  6390. (save-excursion (outline-end-of-heading)
  6391. (setq folded (org-invisible-p)))
  6392. (outline-end-of-subtree))
  6393. (outline-next-heading)
  6394. (setq ne-end (org-back-over-empty-lines))
  6395. (setq end (point))
  6396. (goto-char beg0)
  6397. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6398. ;; include less whitespace
  6399. (save-excursion
  6400. (goto-char beg)
  6401. (forward-line (- ne-beg ne-end))
  6402. (setq beg (point))))
  6403. ;; Find insertion point, with error handling
  6404. (while (> cnt 0)
  6405. (or (and (funcall movfunc) (looking-at outline-regexp))
  6406. (progn (goto-char beg0)
  6407. (error "Cannot move past superior level or buffer limit")))
  6408. (setq cnt (1- cnt)))
  6409. (if (> arg 0)
  6410. ;; Moving forward - still need to move over subtree
  6411. (progn (org-end-of-subtree t t)
  6412. (save-excursion
  6413. (org-back-over-empty-lines)
  6414. (or (bolp) (newline)))))
  6415. (setq ne-ins (org-back-over-empty-lines))
  6416. (move-marker ins-point (point))
  6417. (setq txt (buffer-substring beg end))
  6418. (org-save-markers-in-region beg end)
  6419. (delete-region beg end)
  6420. (org-remove-empty-overlays-at beg)
  6421. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6422. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6423. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6424. (let ((bbb (point)))
  6425. (insert-before-markers txt)
  6426. (org-reinstall-markers-in-region bbb)
  6427. (move-marker ins-point bbb))
  6428. (or (bolp) (insert "\n"))
  6429. (setq ins-end (point))
  6430. (goto-char ins-point)
  6431. (org-skip-whitespace)
  6432. (when (and (< arg 0)
  6433. (org-first-sibling-p)
  6434. (> ne-ins ne-beg))
  6435. ;; Move whitespace back to beginning
  6436. (save-excursion
  6437. (goto-char ins-end)
  6438. (let ((kill-whole-line t))
  6439. (kill-line (- ne-ins ne-beg)) (point)))
  6440. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6441. (move-marker ins-point nil)
  6442. (if folded
  6443. (hide-subtree)
  6444. (org-show-entry)
  6445. (show-children)
  6446. (org-cycle-hide-drawers 'children))
  6447. (org-clean-visibility-after-subtree-move)))
  6448. (defvar org-subtree-clip ""
  6449. "Clipboard for cut and paste of subtrees.
  6450. This is actually only a copy of the kill, because we use the normal kill
  6451. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6452. (defvar org-subtree-clip-folded nil
  6453. "Was the last copied subtree folded?
  6454. This is used to fold the tree back after pasting.")
  6455. (defun org-cut-subtree (&optional n)
  6456. "Cut the current subtree into the clipboard.
  6457. With prefix arg N, cut this many sequential subtrees.
  6458. This is a short-hand for marking the subtree and then cutting it."
  6459. (interactive "p")
  6460. (org-copy-subtree n 'cut))
  6461. (defun org-copy-subtree (&optional n cut force-store-markers)
  6462. "Cut the current subtree into the clipboard.
  6463. With prefix arg N, cut this many sequential subtrees.
  6464. This is a short-hand for marking the subtree and then copying it.
  6465. If CUT is non-nil, actually cut the subtree.
  6466. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6467. of some markers in the region, even if CUT is non-nil. This is
  6468. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6469. (interactive "p")
  6470. (let (beg end folded (beg0 (point)))
  6471. (if (interactive-p)
  6472. (org-back-to-heading nil) ; take what looks like a subtree
  6473. (org-back-to-heading t)) ; take what is really there
  6474. (org-back-over-empty-lines)
  6475. (setq beg (point))
  6476. (skip-chars-forward " \t\r\n")
  6477. (save-match-data
  6478. (save-excursion (outline-end-of-heading)
  6479. (setq folded (org-invisible-p)))
  6480. (condition-case nil
  6481. (org-forward-same-level (1- n) t)
  6482. (error nil))
  6483. (org-end-of-subtree t t))
  6484. (org-back-over-empty-lines)
  6485. (setq end (point))
  6486. (goto-char beg0)
  6487. (when (> end beg)
  6488. (setq org-subtree-clip-folded folded)
  6489. (when (or cut force-store-markers)
  6490. (org-save-markers-in-region beg end))
  6491. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6492. (setq org-subtree-clip (current-kill 0))
  6493. (message "%s: Subtree(s) with %d characters"
  6494. (if cut "Cut" "Copied")
  6495. (length org-subtree-clip)))))
  6496. (defun org-paste-subtree (&optional level tree for-yank)
  6497. "Paste the clipboard as a subtree, with modification of headline level.
  6498. The entire subtree is promoted or demoted in order to match a new headline
  6499. level.
  6500. If the cursor is at the beginning of a headline, the same level as
  6501. that headline is used to paste the tree
  6502. If not, the new level is derived from the *visible* headings
  6503. before and after the insertion point, and taken to be the inferior headline
  6504. level of the two. So if the previous visible heading is level 3 and the
  6505. next is level 4 (or vice versa), level 4 will be used for insertion.
  6506. This makes sure that the subtree remains an independent subtree and does
  6507. not swallow low level entries.
  6508. You can also force a different level, either by using a numeric prefix
  6509. argument, or by inserting the heading marker by hand. For example, if the
  6510. cursor is after \"*****\", then the tree will be shifted to level 5.
  6511. If optional TREE is given, use this text instead of the kill ring.
  6512. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6513. move back over whitespace before inserting, and move point to the end of
  6514. the inserted text when done."
  6515. (interactive "P")
  6516. (setq tree (or tree (and kill-ring (current-kill 0))))
  6517. (unless (org-kill-is-subtree-p tree)
  6518. (error "%s"
  6519. (substitute-command-keys
  6520. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6521. (let* ((visp (not (org-invisible-p)))
  6522. (txt tree)
  6523. (^re (concat "^\\(" outline-regexp "\\)"))
  6524. (re (concat "\\(" outline-regexp "\\)"))
  6525. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6526. (old-level (if (string-match ^re txt)
  6527. (- (match-end 0) (match-beginning 0) 1)
  6528. -1))
  6529. (force-level (cond (level (prefix-numeric-value level))
  6530. ((and (looking-at "[ \t]*$")
  6531. (string-match
  6532. ^re_ (buffer-substring
  6533. (point-at-bol) (point))))
  6534. (- (match-end 1) (match-beginning 1)))
  6535. ((and (bolp)
  6536. (looking-at org-outline-regexp))
  6537. (- (match-end 0) (point) 1))
  6538. (t nil)))
  6539. (previous-level (save-excursion
  6540. (condition-case nil
  6541. (progn
  6542. (outline-previous-visible-heading 1)
  6543. (if (looking-at re)
  6544. (- (match-end 0) (match-beginning 0) 1)
  6545. 1))
  6546. (error 1))))
  6547. (next-level (save-excursion
  6548. (condition-case nil
  6549. (progn
  6550. (or (looking-at outline-regexp)
  6551. (outline-next-visible-heading 1))
  6552. (if (looking-at re)
  6553. (- (match-end 0) (match-beginning 0) 1)
  6554. 1))
  6555. (error 1))))
  6556. (new-level (or force-level (max previous-level next-level)))
  6557. (shift (if (or (= old-level -1)
  6558. (= new-level -1)
  6559. (= old-level new-level))
  6560. 0
  6561. (- new-level old-level)))
  6562. (delta (if (> shift 0) -1 1))
  6563. (func (if (> shift 0) 'org-demote 'org-promote))
  6564. (org-odd-levels-only nil)
  6565. beg end newend)
  6566. ;; Remove the forced level indicator
  6567. (if force-level
  6568. (delete-region (point-at-bol) (point)))
  6569. ;; Paste
  6570. (beginning-of-line 1)
  6571. (unless for-yank (org-back-over-empty-lines))
  6572. (setq beg (point))
  6573. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6574. (insert-before-markers txt)
  6575. (unless (string-match "\n\\'" txt) (insert "\n"))
  6576. (setq newend (point))
  6577. (org-reinstall-markers-in-region beg)
  6578. (setq end (point))
  6579. (goto-char beg)
  6580. (skip-chars-forward " \t\n\r")
  6581. (setq beg (point))
  6582. (if (and (org-invisible-p) visp)
  6583. (save-excursion (outline-show-heading)))
  6584. ;; Shift if necessary
  6585. (unless (= shift 0)
  6586. (save-restriction
  6587. (narrow-to-region beg end)
  6588. (while (not (= shift 0))
  6589. (org-map-region func (point-min) (point-max))
  6590. (setq shift (+ delta shift)))
  6591. (goto-char (point-min))
  6592. (setq newend (point-max))))
  6593. (when (or (interactive-p) for-yank)
  6594. (message "Clipboard pasted as level %d subtree" new-level))
  6595. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6596. kill-ring
  6597. (eq org-subtree-clip (current-kill 0))
  6598. org-subtree-clip-folded)
  6599. ;; The tree was folded before it was killed/copied
  6600. (hide-subtree))
  6601. (and for-yank (goto-char newend))))
  6602. (defun org-kill-is-subtree-p (&optional txt)
  6603. "Check if the current kill is an outline subtree, or a set of trees.
  6604. Returns nil if kill does not start with a headline, or if the first
  6605. headline level is not the largest headline level in the tree.
  6606. So this will actually accept several entries of equal levels as well,
  6607. which is OK for `org-paste-subtree'.
  6608. If optional TXT is given, check this string instead of the current kill."
  6609. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6610. (start-level (and kill
  6611. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6612. org-outline-regexp "\\)")
  6613. kill)
  6614. (- (match-end 2) (match-beginning 2) 1)))
  6615. (re (concat "^" org-outline-regexp))
  6616. (start (1+ (or (match-beginning 2) -1))))
  6617. (if (not start-level)
  6618. (progn
  6619. nil) ;; does not even start with a heading
  6620. (catch 'exit
  6621. (while (setq start (string-match re kill (1+ start)))
  6622. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6623. (throw 'exit nil)))
  6624. t))))
  6625. (defvar org-markers-to-move nil
  6626. "Markers that should be moved with a cut-and-paste operation.
  6627. Those markers are stored together with their positions relative to
  6628. the start of the region.")
  6629. (defun org-save-markers-in-region (beg end)
  6630. "Check markers in region.
  6631. If these markers are between BEG and END, record their position relative
  6632. to BEG, so that after moving the block of text, we can put the markers back
  6633. into place.
  6634. This function gets called just before an entry or tree gets cut from the
  6635. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6636. called immediately, to move the markers with the entries."
  6637. (setq org-markers-to-move nil)
  6638. (when (featurep 'org-clock)
  6639. (org-clock-save-markers-for-cut-and-paste beg end))
  6640. (when (featurep 'org-agenda)
  6641. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6642. (defun org-check-and-save-marker (marker beg end)
  6643. "Check if MARKER is between BEG and END.
  6644. If yes, remember the marker and the distance to BEG."
  6645. (when (and (marker-buffer marker)
  6646. (equal (marker-buffer marker) (current-buffer)))
  6647. (if (and (>= marker beg) (< marker end))
  6648. (push (cons marker (- marker beg)) org-markers-to-move))))
  6649. (defun org-reinstall-markers-in-region (beg)
  6650. "Move all remembered markers to their position relative to BEG."
  6651. (mapc (lambda (x)
  6652. (move-marker (car x) (+ beg (cdr x))))
  6653. org-markers-to-move)
  6654. (setq org-markers-to-move nil))
  6655. (defun org-narrow-to-subtree ()
  6656. "Narrow buffer to the current subtree."
  6657. (interactive)
  6658. (save-excursion
  6659. (save-match-data
  6660. (narrow-to-region
  6661. (progn (org-back-to-heading t) (point))
  6662. (progn (org-end-of-subtree t t)
  6663. (if (org-on-heading-p) (backward-char 1))
  6664. (point))))))
  6665. (eval-when-compile
  6666. (defvar org-property-drawer-re))
  6667. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6668. "Clone the task (subtree) at point N times.
  6669. The clones will be inserted as siblings.
  6670. In interactive use, the user will be prompted for the number of
  6671. clones to be produced, and for a time SHIFT, which may be a
  6672. repeater as used in time stamps, for example `+3d'.
  6673. When a valid repeater is given and the entry contains any time
  6674. stamps, the clones will become a sequence in time, with time
  6675. stamps in the subtree shifted for each clone produced. If SHIFT
  6676. is nil or the empty string, time stamps will be left alone. The
  6677. ID property of the original subtree is removed.
  6678. If the original subtree did contain time stamps with a repeater,
  6679. the following will happen:
  6680. - the repeater will be removed in each clone
  6681. - an additional clone will be produced, with the current, unshifted
  6682. date(s) in the entry.
  6683. - the original entry will be placed *after* all the clones, with
  6684. repeater intact.
  6685. - the start days in the repeater in the original entry will be shifted
  6686. to past the last clone.
  6687. I this way you can spell out a number of instances of a repeating task,
  6688. and still retain the repeater to cover future instances of the task."
  6689. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6690. (let (beg end template task idprop
  6691. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6692. (if (not (and (integerp n) (> n 0)))
  6693. (error "Invalid number of replications %s" n))
  6694. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6695. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6696. shift)))
  6697. (error "Invalid shift specification %s" shift))
  6698. (when doshift
  6699. (setq shift-n (string-to-number (match-string 1 shift))
  6700. shift-what (cdr (assoc (match-string 2 shift)
  6701. '(("d" . day) ("w" . week)
  6702. ("m" . month) ("y" . year))))))
  6703. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6704. (setq nmin 1 nmax n)
  6705. (org-back-to-heading t)
  6706. (setq beg (point))
  6707. (setq idprop (org-entry-get nil "ID"))
  6708. (org-end-of-subtree t t)
  6709. (or (bolp) (insert "\n"))
  6710. (setq end (point))
  6711. (setq template (buffer-substring beg end))
  6712. (when (and doshift
  6713. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6714. (delete-region beg end)
  6715. (setq end beg)
  6716. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6717. (goto-char end)
  6718. (loop for n from nmin to nmax do
  6719. ;; prepare clone
  6720. (with-temp-buffer
  6721. (insert template)
  6722. (org-mode)
  6723. (goto-char (point-min))
  6724. (and idprop (if org-clone-delete-id
  6725. (org-entry-delete nil "ID")
  6726. (org-id-get-create t)))
  6727. (while (re-search-forward org-property-drawer-re nil t)
  6728. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6729. (goto-char (point-min))
  6730. (when doshift
  6731. (while (re-search-forward org-ts-regexp-both nil t)
  6732. (org-timestamp-change (* n shift-n) shift-what))
  6733. (unless (= n n-no-remove)
  6734. (goto-char (point-min))
  6735. (while (re-search-forward org-ts-regexp nil t)
  6736. (save-excursion
  6737. (goto-char (match-beginning 0))
  6738. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6739. (delete-region (match-beginning 1) (match-end 1)))))))
  6740. (setq task (buffer-string)))
  6741. (insert task))
  6742. (goto-char beg)))
  6743. ;;; Outline Sorting
  6744. (defun org-sort (with-case)
  6745. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6746. Optional argument WITH-CASE means sort case-sensitively.
  6747. With a double prefix argument, also remove duplicate entries."
  6748. (interactive "P")
  6749. (if (org-at-table-p)
  6750. (org-call-with-arg 'org-table-sort-lines with-case)
  6751. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6752. (defun org-sort-remove-invisible (s)
  6753. (remove-text-properties 0 (length s) org-rm-props s)
  6754. (while (string-match org-bracket-link-regexp s)
  6755. (setq s (replace-match (if (match-end 2)
  6756. (match-string 3 s)
  6757. (match-string 1 s)) t t s)))
  6758. s)
  6759. (defvar org-priority-regexp) ; defined later in the file
  6760. (defvar org-after-sorting-entries-or-items-hook nil
  6761. "Hook that is run after a bunch of entries or items have been sorted.
  6762. When children are sorted, the cursor is in the parent line when this
  6763. hook gets called. When a region or a plain list is sorted, the cursor
  6764. will be in the first entry of the sorted region/list.")
  6765. (defun org-sort-entries-or-items
  6766. (&optional with-case sorting-type getkey-func compare-func property)
  6767. "Sort entries on a certain level of an outline tree, or plain list items.
  6768. If there is an active region, the entries in the region are sorted.
  6769. Else, if the cursor is before the first entry, sort the top-level items.
  6770. Else, the children of the entry at point are sorted.
  6771. If the cursor is at the first item in a plain list, the list items will be
  6772. sorted.
  6773. Sorting can be alphabetically, numerically, by date/time as given by
  6774. a time stamp, by a property or by priority.
  6775. The command prompts for the sorting type unless it has been given to the
  6776. function through the SORTING-TYPE argument, which needs to be a character,
  6777. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6778. precise meaning of each character:
  6779. n Numerically, by converting the beginning of the entry/item to a number.
  6780. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6781. t By date/time, either the first active time stamp in the entry, or, if
  6782. none exist, by the first inactive one.
  6783. In items, only the first line will be checked.
  6784. s By the scheduled date/time.
  6785. d By deadline date/time.
  6786. c By creation time, which is assumed to be the first inactive time stamp
  6787. at the beginning of a line.
  6788. p By priority according to the cookie.
  6789. r By the value of a property.
  6790. Capital letters will reverse the sort order.
  6791. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6792. called with point at the beginning of the record. It must return either
  6793. a string or a number that should serve as the sorting key for that record.
  6794. Comparing entries ignores case by default. However, with an optional argument
  6795. WITH-CASE, the sorting considers case as well."
  6796. (interactive "P")
  6797. (let ((case-func (if with-case 'identity 'downcase))
  6798. start beg end stars re re2
  6799. txt what tmp plain-list-p)
  6800. ;; Find beginning and end of region to sort
  6801. (cond
  6802. ((org-region-active-p)
  6803. ;; we will sort the region
  6804. (setq end (region-end)
  6805. what "region")
  6806. (goto-char (region-beginning))
  6807. (if (not (org-on-heading-p)) (outline-next-heading))
  6808. (setq start (point)))
  6809. ((org-at-item-p)
  6810. ;; we will sort this plain list
  6811. (org-beginning-of-item-list) (setq start (point))
  6812. (org-end-of-item-list)
  6813. (or (bolp) (insert "\n"))
  6814. (setq end (point))
  6815. (goto-char start)
  6816. (setq plain-list-p t
  6817. what "plain list"))
  6818. ((or (org-on-heading-p)
  6819. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6820. ;; we will sort the children of the current headline
  6821. (org-back-to-heading)
  6822. (setq start (point)
  6823. end (progn (org-end-of-subtree t t)
  6824. (or (bolp) (insert "\n"))
  6825. (org-back-over-empty-lines)
  6826. (point))
  6827. what "children")
  6828. (goto-char start)
  6829. (show-subtree)
  6830. (outline-next-heading))
  6831. (t
  6832. ;; we will sort the top-level entries in this file
  6833. (goto-char (point-min))
  6834. (or (org-on-heading-p) (outline-next-heading))
  6835. (setq start (point))
  6836. (goto-char (point-max))
  6837. (beginning-of-line 1)
  6838. (when (looking-at ".*?\\S-")
  6839. ;; File ends in a non-white line
  6840. (end-of-line 1)
  6841. (insert "\n"))
  6842. (setq end (point-max))
  6843. (setq what "top-level")
  6844. (goto-char start)
  6845. (show-all)))
  6846. (setq beg (point))
  6847. (if (>= beg end) (error "Nothing to sort"))
  6848. (unless plain-list-p
  6849. (looking-at "\\(\\*+\\)")
  6850. (setq stars (match-string 1)
  6851. re (concat "^" (regexp-quote stars) " +")
  6852. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6853. txt (buffer-substring beg end))
  6854. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6855. (if (and (not (equal stars "*")) (string-match re2 txt))
  6856. (error "Region to sort contains a level above the first entry")))
  6857. (unless sorting-type
  6858. (message
  6859. (if plain-list-p
  6860. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6861. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6862. [t]ime [s]cheduled [d]eadline [c]reated
  6863. A/N/T/S/D/C/P/O/F means reversed:")
  6864. what)
  6865. (setq sorting-type (read-char-exclusive))
  6866. (and (= (downcase sorting-type) ?f)
  6867. (setq getkey-func
  6868. (org-icompleting-read "Sort using function: "
  6869. obarray 'fboundp t nil nil))
  6870. (setq getkey-func (intern getkey-func)))
  6871. (and (= (downcase sorting-type) ?r)
  6872. (setq property
  6873. (org-icompleting-read "Property: "
  6874. (mapcar 'list (org-buffer-property-keys t))
  6875. nil t))))
  6876. (message "Sorting entries...")
  6877. (save-restriction
  6878. (narrow-to-region start end)
  6879. (let ((dcst (downcase sorting-type))
  6880. (case-fold-search nil)
  6881. (now (current-time)))
  6882. (sort-subr
  6883. (/= dcst sorting-type)
  6884. ;; This function moves to the beginning character of the "record" to
  6885. ;; be sorted.
  6886. (if plain-list-p
  6887. (lambda nil
  6888. (if (org-at-item-p) t (goto-char (point-max))))
  6889. (lambda nil
  6890. (if (re-search-forward re nil t)
  6891. (goto-char (match-beginning 0))
  6892. (goto-char (point-max)))))
  6893. ;; This function moves to the last character of the "record" being
  6894. ;; sorted.
  6895. (if plain-list-p
  6896. 'org-end-of-item
  6897. (lambda nil
  6898. (save-match-data
  6899. (condition-case nil
  6900. (outline-forward-same-level 1)
  6901. (error
  6902. (goto-char (point-max)))))))
  6903. ;; This function returns the value that gets sorted against.
  6904. (if plain-list-p
  6905. (lambda nil
  6906. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6907. (cond
  6908. ((= dcst ?n)
  6909. (string-to-number (buffer-substring (match-end 0)
  6910. (point-at-eol))))
  6911. ((= dcst ?a)
  6912. (buffer-substring (match-end 0) (point-at-eol)))
  6913. ((= dcst ?t)
  6914. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6915. (re-search-forward org-ts-regexp-both
  6916. (point-at-eol) t))
  6917. (org-time-string-to-seconds (match-string 0))
  6918. (org-float-time now)))
  6919. ((= dcst ?f)
  6920. (if getkey-func
  6921. (progn
  6922. (setq tmp (funcall getkey-func))
  6923. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6924. tmp)
  6925. (error "Invalid key function `%s'" getkey-func)))
  6926. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6927. (lambda nil
  6928. (cond
  6929. ((= dcst ?n)
  6930. (if (looking-at org-complex-heading-regexp)
  6931. (string-to-number (match-string 4))
  6932. nil))
  6933. ((= dcst ?a)
  6934. (if (looking-at org-complex-heading-regexp)
  6935. (funcall case-func (match-string 4))
  6936. nil))
  6937. ((= dcst ?t)
  6938. (let ((end (save-excursion (outline-next-heading) (point))))
  6939. (if (or (re-search-forward org-ts-regexp end t)
  6940. (re-search-forward org-ts-regexp-both end t))
  6941. (org-time-string-to-seconds (match-string 0))
  6942. (org-float-time now))))
  6943. ((= dcst ?c)
  6944. (let ((end (save-excursion (outline-next-heading) (point))))
  6945. (if (re-search-forward
  6946. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6947. end t)
  6948. (org-time-string-to-seconds (match-string 0))
  6949. (org-float-time now))))
  6950. ((= dcst ?s)
  6951. (let ((end (save-excursion (outline-next-heading) (point))))
  6952. (if (re-search-forward org-scheduled-time-regexp end t)
  6953. (org-time-string-to-seconds (match-string 1))
  6954. (org-float-time now))))
  6955. ((= dcst ?d)
  6956. (let ((end (save-excursion (outline-next-heading) (point))))
  6957. (if (re-search-forward org-deadline-time-regexp end t)
  6958. (org-time-string-to-seconds (match-string 1))
  6959. (org-float-time now))))
  6960. ((= dcst ?p)
  6961. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6962. (string-to-char (match-string 2))
  6963. org-default-priority))
  6964. ((= dcst ?r)
  6965. (or (org-entry-get nil property) ""))
  6966. ((= dcst ?o)
  6967. (if (looking-at org-complex-heading-regexp)
  6968. (- 9999 (length (member (match-string 2)
  6969. org-todo-keywords-1)))))
  6970. ((= dcst ?f)
  6971. (if getkey-func
  6972. (progn
  6973. (setq tmp (funcall getkey-func))
  6974. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6975. tmp)
  6976. (error "Invalid key function `%s'" getkey-func)))
  6977. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6978. nil
  6979. (cond
  6980. ((= dcst ?a) 'string<)
  6981. ((= dcst ?f) compare-func)
  6982. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6983. (t nil)))))
  6984. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6985. (message "Sorting entries...done")))
  6986. (defun org-do-sort (table what &optional with-case sorting-type)
  6987. "Sort TABLE of WHAT according to SORTING-TYPE.
  6988. The user will be prompted for the SORTING-TYPE if the call to this
  6989. function does not specify it. WHAT is only for the prompt, to indicate
  6990. what is being sorted. The sorting key will be extracted from
  6991. the car of the elements of the table.
  6992. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6993. (unless sorting-type
  6994. (message
  6995. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6996. what)
  6997. (setq sorting-type (read-char-exclusive)))
  6998. (let ((dcst (downcase sorting-type))
  6999. extractfun comparefun)
  7000. ;; Define the appropriate functions
  7001. (cond
  7002. ((= dcst ?n)
  7003. (setq extractfun 'string-to-number
  7004. comparefun (if (= dcst sorting-type) '< '>)))
  7005. ((= dcst ?a)
  7006. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7007. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7008. comparefun (if (= dcst sorting-type)
  7009. 'string<
  7010. (lambda (a b) (and (not (string< a b))
  7011. (not (string= a b)))))))
  7012. ((= dcst ?t)
  7013. (setq extractfun
  7014. (lambda (x)
  7015. (if (or (string-match org-ts-regexp x)
  7016. (string-match org-ts-regexp-both x))
  7017. (org-float-time
  7018. (org-time-string-to-time (match-string 0 x)))
  7019. 0))
  7020. comparefun (if (= dcst sorting-type) '< '>)))
  7021. (t (error "Invalid sorting type `%c'" sorting-type)))
  7022. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7023. table)
  7024. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7025. ;;; The orgstruct minor mode
  7026. ;; Define a minor mode which can be used in other modes in order to
  7027. ;; integrate the org-mode structure editing commands.
  7028. ;; This is really a hack, because the org-mode structure commands use
  7029. ;; keys which normally belong to the major mode. Here is how it
  7030. ;; works: The minor mode defines all the keys necessary to operate the
  7031. ;; structure commands, but wraps the commands into a function which
  7032. ;; tests if the cursor is currently at a headline or a plain list
  7033. ;; item. If that is the case, the structure command is used,
  7034. ;; temporarily setting many Org-mode variables like regular
  7035. ;; expressions for filling etc. However, when any of those keys is
  7036. ;; used at a different location, function uses `key-binding' to look
  7037. ;; up if the key has an associated command in another currently active
  7038. ;; keymap (minor modes, major mode, global), and executes that
  7039. ;; command. There might be problems if any of the keys is otherwise
  7040. ;; used as a prefix key.
  7041. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7042. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7043. ;; addresses this by checking explicitly for both bindings.
  7044. (defvar orgstruct-mode-map (make-sparse-keymap)
  7045. "Keymap for the minor `orgstruct-mode'.")
  7046. (defvar org-local-vars nil
  7047. "List of local variables, for use by `orgstruct-mode'.")
  7048. ;;;###autoload
  7049. (define-minor-mode orgstruct-mode
  7050. "Toggle the minor mode `orgstruct-mode'.
  7051. This mode is for using Org-mode structure commands in other
  7052. modes. The following keys behave as if Org-mode were active, if
  7053. the cursor is on a headline, or on a plain list item (both as
  7054. defined by Org-mode).
  7055. M-up Move entry/item up
  7056. M-down Move entry/item down
  7057. M-left Promote
  7058. M-right Demote
  7059. M-S-up Move entry/item up
  7060. M-S-down Move entry/item down
  7061. M-S-left Promote subtree
  7062. M-S-right Demote subtree
  7063. M-q Fill paragraph and items like in Org-mode
  7064. C-c ^ Sort entries
  7065. C-c - Cycle list bullet
  7066. TAB Cycle item visibility
  7067. M-RET Insert new heading/item
  7068. S-M-RET Insert new TODO heading / Checkbox item
  7069. C-c C-c Set tags / toggle checkbox"
  7070. nil " OrgStruct" nil
  7071. (org-load-modules-maybe)
  7072. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7073. ;;;###autoload
  7074. (defun turn-on-orgstruct ()
  7075. "Unconditionally turn on `orgstruct-mode'."
  7076. (orgstruct-mode 1))
  7077. (defun orgstruct++-mode (&optional arg)
  7078. "Toggle `orgstruct-mode', the enhanced version of it.
  7079. In addition to setting orgstruct-mode, this also exports all indentation
  7080. and autofilling variables from org-mode into the buffer. It will also
  7081. recognize item context in multiline items.
  7082. Note that turning off orgstruct-mode will *not* remove the
  7083. indentation/paragraph settings. This can only be done by refreshing the
  7084. major mode, for example with \\[normal-mode]."
  7085. (interactive "P")
  7086. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7087. (if (< arg 1)
  7088. (orgstruct-mode -1)
  7089. (orgstruct-mode 1)
  7090. (let (var val)
  7091. (mapc
  7092. (lambda (x)
  7093. (when (string-match
  7094. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7095. (symbol-name (car x)))
  7096. (setq var (car x) val (nth 1 x))
  7097. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7098. org-local-vars)
  7099. (org-set-local 'orgstruct-is-++ t))))
  7100. (defvar orgstruct-is-++ nil
  7101. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7102. (make-variable-buffer-local 'orgstruct-is-++)
  7103. ;;;###autoload
  7104. (defun turn-on-orgstruct++ ()
  7105. "Unconditionally turn on `orgstruct++-mode'."
  7106. (orgstruct++-mode 1))
  7107. (defun orgstruct-error ()
  7108. "Error when there is no default binding for a structure key."
  7109. (interactive)
  7110. (error "This key has no function outside structure elements"))
  7111. (defun orgstruct-setup ()
  7112. "Setup orgstruct keymaps."
  7113. (let ((nfunc 0)
  7114. (bindings
  7115. (list
  7116. '([(meta up)] org-metaup)
  7117. '([(meta down)] org-metadown)
  7118. '([(meta left)] org-metaleft)
  7119. '([(meta right)] org-metaright)
  7120. '([(meta shift up)] org-shiftmetaup)
  7121. '([(meta shift down)] org-shiftmetadown)
  7122. '([(meta shift left)] org-shiftmetaleft)
  7123. '([(meta shift right)] org-shiftmetaright)
  7124. '([?\e (up)] org-metaup)
  7125. '([?\e (down)] org-metadown)
  7126. '([?\e (left)] org-metaleft)
  7127. '([?\e (right)] org-metaright)
  7128. '([?\e (shift up)] org-shiftmetaup)
  7129. '([?\e (shift down)] org-shiftmetadown)
  7130. '([?\e (shift left)] org-shiftmetaleft)
  7131. '([?\e (shift right)] org-shiftmetaright)
  7132. '([(shift up)] org-shiftup)
  7133. '([(shift down)] org-shiftdown)
  7134. '([(shift left)] org-shiftleft)
  7135. '([(shift right)] org-shiftright)
  7136. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7137. '("\M-q" fill-paragraph)
  7138. '("\C-c^" org-sort)
  7139. '("\C-c-" org-cycle-list-bullet)))
  7140. elt key fun cmd)
  7141. (while (setq elt (pop bindings))
  7142. (setq nfunc (1+ nfunc))
  7143. (setq key (org-key (car elt))
  7144. fun (nth 1 elt)
  7145. cmd (orgstruct-make-binding fun nfunc key))
  7146. (org-defkey orgstruct-mode-map key cmd))
  7147. ;; Special treatment needed for TAB and RET
  7148. (org-defkey orgstruct-mode-map [(tab)]
  7149. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7150. (org-defkey orgstruct-mode-map "\C-i"
  7151. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7152. (org-defkey orgstruct-mode-map "\M-\C-m"
  7153. (orgstruct-make-binding 'org-insert-heading 105
  7154. "\M-\C-m" [(meta return)]))
  7155. (org-defkey orgstruct-mode-map [(meta return)]
  7156. (orgstruct-make-binding 'org-insert-heading 106
  7157. [(meta return)] "\M-\C-m"))
  7158. (org-defkey orgstruct-mode-map [(shift meta return)]
  7159. (orgstruct-make-binding 'org-insert-todo-heading 107
  7160. [(meta return)] "\M-\C-m"))
  7161. (org-defkey orgstruct-mode-map "\e\C-m"
  7162. (orgstruct-make-binding 'org-insert-heading 108
  7163. "\e\C-m" [?\e (return)]))
  7164. (org-defkey orgstruct-mode-map [?\e (return)]
  7165. (orgstruct-make-binding 'org-insert-heading 109
  7166. [?\e (return)] "\e\C-m"))
  7167. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7168. (orgstruct-make-binding 'org-insert-todo-heading 110
  7169. [?\e (return)] "\e\C-m"))
  7170. (unless org-local-vars
  7171. (setq org-local-vars (org-get-local-variables)))
  7172. t))
  7173. (defun orgstruct-make-binding (fun n &rest keys)
  7174. "Create a function for binding in the structure minor mode.
  7175. FUN is the command to call inside a table. N is used to create a unique
  7176. command name. KEYS are keys that should be checked in for a command
  7177. to execute outside of tables."
  7178. (eval
  7179. (list 'defun
  7180. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7181. '(arg)
  7182. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7183. "Outside of structure, run the binding of `"
  7184. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7185. "'.")
  7186. '(interactive "p")
  7187. (list 'if
  7188. `(org-context-p 'headline 'item
  7189. (and orgstruct-is-++
  7190. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7191. 'item-body))
  7192. (list 'org-run-like-in-org-mode (list 'quote fun))
  7193. (list 'let '(orgstruct-mode)
  7194. (list 'call-interactively
  7195. (append '(or)
  7196. (mapcar (lambda (k)
  7197. (list 'key-binding k))
  7198. keys)
  7199. '('orgstruct-error))))))))
  7200. (defun org-context-p (&rest contexts)
  7201. "Check if local context is any of CONTEXTS.
  7202. Possible values in the list of contexts are `table', `headline', and `item'."
  7203. (let ((pos (point)))
  7204. (goto-char (point-at-bol))
  7205. (prog1 (or (and (memq 'table contexts)
  7206. (looking-at "[ \t]*|"))
  7207. (and (memq 'headline contexts)
  7208. ;;????????? (looking-at "\\*+"))
  7209. (looking-at outline-regexp))
  7210. (and (memq 'item contexts)
  7211. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7212. (and (memq 'item-body contexts)
  7213. (org-in-item-p)))
  7214. (goto-char pos))))
  7215. (defun org-get-local-variables ()
  7216. "Return a list of all local variables in an org-mode buffer."
  7217. (let (varlist)
  7218. (with-current-buffer (get-buffer-create "*Org tmp*")
  7219. (erase-buffer)
  7220. (org-mode)
  7221. (setq varlist (buffer-local-variables)))
  7222. (kill-buffer "*Org tmp*")
  7223. (delq nil
  7224. (mapcar
  7225. (lambda (x)
  7226. (setq x
  7227. (if (symbolp x)
  7228. (list x)
  7229. (list (car x) (list 'quote (cdr x)))))
  7230. (if (string-match
  7231. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7232. (symbol-name (car x)))
  7233. x nil))
  7234. varlist))))
  7235. ;;;###autoload
  7236. (defun org-run-like-in-org-mode (cmd)
  7237. "Run a command, pretending that the current buffer is in Org-mode.
  7238. This will temporarily bind local variables that are typically bound in
  7239. Org-mode to the values they have in Org-mode, and then interactively
  7240. call CMD."
  7241. (org-load-modules-maybe)
  7242. (unless org-local-vars
  7243. (setq org-local-vars (org-get-local-variables)))
  7244. (eval (list 'let org-local-vars
  7245. (list 'call-interactively (list 'quote cmd)))))
  7246. ;;;; Archiving
  7247. (defun org-get-category (&optional pos)
  7248. "Get the category applying to position POS."
  7249. (get-text-property (or pos (point)) 'org-category))
  7250. (defun org-refresh-category-properties ()
  7251. "Refresh category text properties in the buffer."
  7252. (let ((def-cat (cond
  7253. ((null org-category)
  7254. (if buffer-file-name
  7255. (file-name-sans-extension
  7256. (file-name-nondirectory buffer-file-name))
  7257. "???"))
  7258. ((symbolp org-category) (symbol-name org-category))
  7259. (t org-category)))
  7260. beg end cat pos optionp)
  7261. (org-unmodified
  7262. (save-excursion
  7263. (save-restriction
  7264. (widen)
  7265. (goto-char (point-min))
  7266. (put-text-property (point) (point-max) 'org-category def-cat)
  7267. (while (re-search-forward
  7268. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7269. (setq pos (match-end 0)
  7270. optionp (equal (char-after (match-beginning 0)) ?#)
  7271. cat (org-trim (match-string 2)))
  7272. (if optionp
  7273. (setq beg (point-at-bol) end (point-max))
  7274. (org-back-to-heading t)
  7275. (setq beg (point) end (org-end-of-subtree t t)))
  7276. (put-text-property beg end 'org-category cat)
  7277. (goto-char pos)))))))
  7278. ;;;; Link Stuff
  7279. ;;; Link abbreviations
  7280. (defun org-link-expand-abbrev (link)
  7281. "Apply replacements as defined in `org-link-abbrev-alist."
  7282. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  7283. (let* ((key (match-string 1 link))
  7284. (as (or (assoc key org-link-abbrev-alist-local)
  7285. (assoc key org-link-abbrev-alist)))
  7286. (tag (and (match-end 2) (match-string 3 link)))
  7287. rpl)
  7288. (if (not as)
  7289. link
  7290. (setq rpl (cdr as))
  7291. (cond
  7292. ((symbolp rpl) (funcall rpl tag))
  7293. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7294. ((string-match "%h" rpl)
  7295. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7296. (t (concat rpl tag)))))
  7297. link))
  7298. ;;; Storing and inserting links
  7299. (defvar org-insert-link-history nil
  7300. "Minibuffer history for links inserted with `org-insert-link'.")
  7301. (defvar org-stored-links nil
  7302. "Contains the links stored with `org-store-link'.")
  7303. (defvar org-store-link-plist nil
  7304. "Plist with info about the most recently link created with `org-store-link'.")
  7305. (defvar org-link-protocols nil
  7306. "Link protocols added to Org-mode using `org-add-link-type'.")
  7307. (defvar org-store-link-functions nil
  7308. "List of functions that are called to create and store a link.
  7309. Each function will be called in turn until one returns a non-nil
  7310. value. Each function should check if it is responsible for creating
  7311. this link (for example by looking at the major mode).
  7312. If not, it must exit and return nil.
  7313. If yes, it should return a non-nil value after a calling
  7314. `org-store-link-props' with a list of properties and values.
  7315. Special properties are:
  7316. :type The link prefix, like \"http\". This must be given.
  7317. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7318. This is obligatory as well.
  7319. :description Optional default description for the second pair
  7320. of brackets in an Org-mode link. The user can still change
  7321. this when inserting this link into an Org-mode buffer.
  7322. In addition to these, any additional properties can be specified
  7323. and then used in remember templates.")
  7324. (defun org-add-link-type (type &optional follow export)
  7325. "Add TYPE to the list of `org-link-types'.
  7326. Re-compute all regular expressions depending on `org-link-types'
  7327. FOLLOW and EXPORT are two functions.
  7328. FOLLOW should take the link path as the single argument and do whatever
  7329. is necessary to follow the link, for example find a file or display
  7330. a mail message.
  7331. EXPORT should format the link path for export to one of the export formats.
  7332. It should be a function accepting three arguments:
  7333. path the path of the link, the text after the prefix (like \"http:\")
  7334. desc the description of the link, if any, nil if there was no description
  7335. format the export format, a symbol like `html' or `latex'.
  7336. The function may use the FORMAT information to return different values
  7337. depending on the format. The return value will be put literally into
  7338. the exported file.
  7339. Org-mode has a built-in default for exporting links. If you are happy with
  7340. this default, there is no need to define an export function for the link
  7341. type. For a simple example of an export function, see `org-bbdb.el'."
  7342. (add-to-list 'org-link-types type t)
  7343. (org-make-link-regexps)
  7344. (if (assoc type org-link-protocols)
  7345. (setcdr (assoc type org-link-protocols) (list follow export))
  7346. (push (list type follow export) org-link-protocols)))
  7347. (defvar org-agenda-buffer-name)
  7348. ;;;###autoload
  7349. (defun org-store-link (arg)
  7350. "\\<org-mode-map>Store an org-link to the current location.
  7351. This link is added to `org-stored-links' and can later be inserted
  7352. into an org-buffer with \\[org-insert-link].
  7353. For some link types, a prefix arg is interpreted:
  7354. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7355. For file links, arg negates `org-context-in-file-links'."
  7356. (interactive "P")
  7357. (org-load-modules-maybe)
  7358. (setq org-store-link-plist nil) ; reset
  7359. (let ((outline-regexp (org-get-limited-outline-regexp))
  7360. link cpltxt desc description search txt custom-id)
  7361. (cond
  7362. ((run-hook-with-args-until-success 'org-store-link-functions)
  7363. (setq link (plist-get org-store-link-plist :link)
  7364. desc (or (plist-get org-store-link-plist :description) link)))
  7365. ((equal (buffer-name) "*Org Edit Src Example*")
  7366. (let (label gc)
  7367. (while (or (not label)
  7368. (save-excursion
  7369. (save-restriction
  7370. (widen)
  7371. (goto-char (point-min))
  7372. (re-search-forward
  7373. (regexp-quote (format org-coderef-label-format label))
  7374. nil t))))
  7375. (when label (message "Label exists already") (sit-for 2))
  7376. (setq label (read-string "Code line label: " label)))
  7377. (end-of-line 1)
  7378. (setq link (format org-coderef-label-format label))
  7379. (setq gc (- 79 (length link)))
  7380. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7381. (insert link)
  7382. (setq link (concat "(" label ")") desc nil)))
  7383. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7384. ;; We are in the agenda, link to referenced location
  7385. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7386. (get-text-property (point) 'org-marker))))
  7387. (when m
  7388. (org-with-point-at m
  7389. (if (interactive-p)
  7390. (call-interactively 'org-store-link)
  7391. (org-store-link nil))))))
  7392. ((eq major-mode 'calendar-mode)
  7393. (let ((cd (calendar-cursor-to-date)))
  7394. (setq link
  7395. (format-time-string
  7396. (car org-time-stamp-formats)
  7397. (apply 'encode-time
  7398. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7399. nil nil nil))))
  7400. (org-store-link-props :type "calendar" :date cd)))
  7401. ((eq major-mode 'w3-mode)
  7402. (setq cpltxt (if (and (buffer-name)
  7403. (not (string-match "Untitled" (buffer-name))))
  7404. (buffer-name)
  7405. (url-view-url t))
  7406. link (org-make-link (url-view-url t)))
  7407. (org-store-link-props :type "w3" :url (url-view-url t)))
  7408. ((eq major-mode 'w3m-mode)
  7409. (setq cpltxt (or w3m-current-title w3m-current-url)
  7410. link (org-make-link w3m-current-url))
  7411. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7412. ((setq search (run-hook-with-args-until-success
  7413. 'org-create-file-search-functions))
  7414. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7415. "::" search))
  7416. (setq cpltxt (or description link)))
  7417. ((eq major-mode 'image-mode)
  7418. (setq cpltxt (concat "file:"
  7419. (abbreviate-file-name buffer-file-name))
  7420. link (org-make-link cpltxt))
  7421. (org-store-link-props :type "image" :file buffer-file-name))
  7422. ((eq major-mode 'dired-mode)
  7423. ;; link to the file in the current line
  7424. (let ((file (dired-get-filename nil t)))
  7425. (setq file (if file
  7426. (abbreviate-file-name
  7427. (expand-file-name (dired-get-filename nil t)))
  7428. ;; otherwise, no file so use current directory.
  7429. default-directory))
  7430. (setq cpltxt (concat "file:" file)
  7431. link (org-make-link cpltxt))))
  7432. ((and buffer-file-name (org-mode-p))
  7433. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7434. (cond
  7435. ((org-in-regexp "<<\\(.*?\\)>>")
  7436. (setq cpltxt
  7437. (concat "file:"
  7438. (abbreviate-file-name buffer-file-name)
  7439. "::" (match-string 1))
  7440. link (org-make-link cpltxt)))
  7441. ((and (featurep 'org-id)
  7442. (or (eq org-link-to-org-use-id t)
  7443. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7444. (interactive-p))
  7445. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7446. (interactive-p)
  7447. (not custom-id))
  7448. (and org-link-to-org-use-id
  7449. (condition-case nil
  7450. (org-entry-get nil "ID")
  7451. (error nil)))))
  7452. ;; We can make a link using the ID.
  7453. (setq link (condition-case nil
  7454. (prog1 (org-id-store-link)
  7455. (setq desc (plist-get org-store-link-plist
  7456. :description)))
  7457. (error
  7458. ;; probably before first headline, link to file only
  7459. (concat "file:"
  7460. (abbreviate-file-name buffer-file-name))))))
  7461. (t
  7462. ;; Just link to current headline
  7463. (setq cpltxt (concat "file:"
  7464. (abbreviate-file-name buffer-file-name)))
  7465. ;; Add a context search string
  7466. (when (org-xor org-context-in-file-links arg)
  7467. (setq txt (cond
  7468. ((org-on-heading-p) nil)
  7469. ((org-region-active-p)
  7470. (buffer-substring (region-beginning) (region-end)))
  7471. (t nil)))
  7472. (when (or (null txt) (string-match "\\S-" txt))
  7473. (setq cpltxt
  7474. (concat cpltxt "::"
  7475. (condition-case nil
  7476. (org-make-org-heading-search-string txt)
  7477. (error "")))
  7478. desc (or (nth 4 (ignore-errors
  7479. (org-heading-components))) "NONE"))))
  7480. (if (string-match "::\\'" cpltxt)
  7481. (setq cpltxt (substring cpltxt 0 -2)))
  7482. (setq link (org-make-link cpltxt)))))
  7483. ((buffer-file-name (buffer-base-buffer))
  7484. ;; Just link to this file here.
  7485. (setq cpltxt (concat "file:"
  7486. (abbreviate-file-name
  7487. (buffer-file-name (buffer-base-buffer)))))
  7488. ;; Add a context string
  7489. (when (org-xor org-context-in-file-links arg)
  7490. (setq txt (if (org-region-active-p)
  7491. (buffer-substring (region-beginning) (region-end))
  7492. (buffer-substring (point-at-bol) (point-at-eol))))
  7493. ;; Only use search option if there is some text.
  7494. (when (string-match "\\S-" txt)
  7495. (setq cpltxt
  7496. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7497. desc "NONE")))
  7498. (setq link (org-make-link cpltxt)))
  7499. ((interactive-p)
  7500. (error "Cannot link to a buffer which is not visiting a file"))
  7501. (t (setq link nil)))
  7502. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7503. (setq link (or link cpltxt)
  7504. desc (or desc cpltxt))
  7505. (if (equal desc "NONE") (setq desc nil))
  7506. (if (and (or (interactive-p) executing-kbd-macro) link)
  7507. (progn
  7508. (setq org-stored-links
  7509. (cons (list link desc) org-stored-links))
  7510. (message "Stored: %s" (or desc link))
  7511. (when custom-id
  7512. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7513. "::#" custom-id))
  7514. (setq org-stored-links
  7515. (cons (list link desc) org-stored-links))))
  7516. (and link (org-make-link-string link desc)))))
  7517. (defun org-store-link-props (&rest plist)
  7518. "Store link properties, extract names and addresses."
  7519. (let (x adr)
  7520. (when (setq x (plist-get plist :from))
  7521. (setq adr (mail-extract-address-components x))
  7522. (setq plist (plist-put plist :fromname (car adr)))
  7523. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7524. (when (setq x (plist-get plist :to))
  7525. (setq adr (mail-extract-address-components x))
  7526. (setq plist (plist-put plist :toname (car adr)))
  7527. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7528. (let ((from (plist-get plist :from))
  7529. (to (plist-get plist :to)))
  7530. (when (and from to org-from-is-user-regexp)
  7531. (setq plist
  7532. (plist-put plist :fromto
  7533. (if (string-match org-from-is-user-regexp from)
  7534. (concat "to %t")
  7535. (concat "from %f"))))))
  7536. (setq org-store-link-plist plist))
  7537. (defun org-add-link-props (&rest plist)
  7538. "Add these properties to the link property list."
  7539. (let (key value)
  7540. (while plist
  7541. (setq key (pop plist) value (pop plist))
  7542. (setq org-store-link-plist
  7543. (plist-put org-store-link-plist key value)))))
  7544. (defun org-email-link-description (&optional fmt)
  7545. "Return the description part of an email link.
  7546. This takes information from `org-store-link-plist' and formats it
  7547. according to FMT (default from `org-email-link-description-format')."
  7548. (setq fmt (or fmt org-email-link-description-format))
  7549. (let* ((p org-store-link-plist)
  7550. (to (plist-get p :toaddress))
  7551. (from (plist-get p :fromaddress))
  7552. (table
  7553. (list
  7554. (cons "%c" (plist-get p :fromto))
  7555. (cons "%F" (plist-get p :from))
  7556. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7557. (cons "%T" (plist-get p :to))
  7558. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7559. (cons "%s" (plist-get p :subject))
  7560. (cons "%m" (plist-get p :message-id)))))
  7561. (when (string-match "%c" fmt)
  7562. ;; Check if the user wrote this message
  7563. (if (and org-from-is-user-regexp from to
  7564. (save-match-data (string-match org-from-is-user-regexp from)))
  7565. (setq fmt (replace-match "to %t" t t fmt))
  7566. (setq fmt (replace-match "from %f" t t fmt))))
  7567. (org-replace-escapes fmt table)))
  7568. (defun org-make-org-heading-search-string (&optional string heading)
  7569. "Make search string for STRING or current headline."
  7570. (interactive)
  7571. (let ((s (or string (org-get-heading))))
  7572. (unless (and string (not heading))
  7573. ;; We are using a headline, clean up garbage in there.
  7574. (if (string-match org-todo-regexp s)
  7575. (setq s (replace-match "" t t s)))
  7576. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7577. (setq s (replace-match "" t t s)))
  7578. (setq s (org-trim s))
  7579. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7580. org-comment-string "\\)") s)
  7581. (setq s (replace-match "" t t s)))
  7582. (while (string-match org-ts-regexp s)
  7583. (setq s (replace-match "" t t s))))
  7584. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7585. (setq s (replace-match " " t t s)))
  7586. (or string (setq s (concat "*" s))) ; Add * for headlines
  7587. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7588. (defun org-make-link (&rest strings)
  7589. "Concatenate STRINGS."
  7590. (apply 'concat strings))
  7591. (defun org-make-link-string (link &optional description)
  7592. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7593. (unless (string-match "\\S-" link)
  7594. (error "Empty link"))
  7595. (when (and description
  7596. (stringp description)
  7597. (not (string-match "\\S-" description)))
  7598. (setq description nil))
  7599. (when (stringp description)
  7600. ;; Remove brackets from the description, they are fatal.
  7601. (while (string-match "\\[" description)
  7602. (setq description (replace-match "{" t t description)))
  7603. (while (string-match "\\]" description)
  7604. (setq description (replace-match "}" t t description))))
  7605. (when (equal (org-link-escape link) description)
  7606. ;; No description needed, it is identical
  7607. (setq description nil))
  7608. (when (and (not description)
  7609. (not (equal link (org-link-escape link))))
  7610. (setq description (org-extract-attributes link)))
  7611. (concat "[[" (org-link-escape link) "]"
  7612. (if description (concat "[" description "]") "")
  7613. "]"))
  7614. (defconst org-link-escape-chars
  7615. '((?\ . "%20")
  7616. (?\[ . "%5B")
  7617. (?\] . "%5D")
  7618. (?\340 . "%E0") ; `a
  7619. (?\342 . "%E2") ; ^a
  7620. (?\347 . "%E7") ; ,c
  7621. (?\350 . "%E8") ; `e
  7622. (?\351 . "%E9") ; 'e
  7623. (?\352 . "%EA") ; ^e
  7624. (?\356 . "%EE") ; ^i
  7625. (?\364 . "%F4") ; ^o
  7626. (?\371 . "%F9") ; `u
  7627. (?\373 . "%FB") ; ^u
  7628. (?\; . "%3B")
  7629. ;; (?? . "%3F")
  7630. (?= . "%3D")
  7631. (?+ . "%2B")
  7632. )
  7633. "Association list of escapes for some characters problematic in links.
  7634. This is the list that is used for internal purposes.")
  7635. (defvar org-url-encoding-use-url-hexify nil)
  7636. (defconst org-link-escape-chars-browser
  7637. '((?\ . "%20")) ; 32 for the SPC char
  7638. "Association list of escapes for some characters problematic in links.
  7639. This is the list that is used before handing over to the browser.")
  7640. (defun org-link-escape (text &optional table)
  7641. "Escape characters in TEXT that are problematic for links."
  7642. (if (and org-url-encoding-use-url-hexify (not table))
  7643. (url-hexify-string text)
  7644. (setq table (or table org-link-escape-chars))
  7645. (when text
  7646. (let ((re (mapconcat (lambda (x) (regexp-quote
  7647. (char-to-string (car x))))
  7648. table "\\|")))
  7649. (while (string-match re text)
  7650. (setq text
  7651. (replace-match
  7652. (cdr (assoc (string-to-char (match-string 0 text))
  7653. table))
  7654. t t text)))
  7655. text))))
  7656. (defun org-link-unescape (text &optional table)
  7657. "Reverse the action of `org-link-escape'."
  7658. (if (and org-url-encoding-use-url-hexify (not table))
  7659. (url-unhex-string text)
  7660. (setq table (or table org-link-escape-chars))
  7661. (when text
  7662. (let ((case-fold-search t)
  7663. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7664. table "\\|")))
  7665. (while (string-match re text)
  7666. (setq text
  7667. (replace-match
  7668. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7669. table)))
  7670. t t text)))
  7671. text))))
  7672. (defun org-xor (a b)
  7673. "Exclusive or."
  7674. (if a (not b) b))
  7675. (defun org-fixup-message-id-for-http (s)
  7676. "Replace special characters in a message id, so it can be used in an http query."
  7677. (when (string-match "%" s)
  7678. (setq s (mapconcat (lambda (c)
  7679. (if (eq c ?%)
  7680. "%25"
  7681. (char-to-string c)))
  7682. s "")))
  7683. (while (string-match "<" s)
  7684. (setq s (replace-match "%3C" t t s)))
  7685. (while (string-match ">" s)
  7686. (setq s (replace-match "%3E" t t s)))
  7687. (while (string-match "@" s)
  7688. (setq s (replace-match "%40" t t s)))
  7689. s)
  7690. ;;;###autoload
  7691. (defun org-insert-link-global ()
  7692. "Insert a link like Org-mode does.
  7693. This command can be called in any mode to insert a link in Org-mode syntax."
  7694. (interactive)
  7695. (org-load-modules-maybe)
  7696. (org-run-like-in-org-mode 'org-insert-link))
  7697. (defun org-insert-link (&optional complete-file link-location)
  7698. "Insert a link. At the prompt, enter the link.
  7699. Completion can be used to insert any of the link protocol prefixes like
  7700. http or ftp in use.
  7701. The history can be used to select a link previously stored with
  7702. `org-store-link'. When the empty string is entered (i.e. if you just
  7703. press RET at the prompt), the link defaults to the most recently
  7704. stored link. As SPC triggers completion in the minibuffer, you need to
  7705. use M-SPC or C-q SPC to force the insertion of a space character.
  7706. You will also be prompted for a description, and if one is given, it will
  7707. be displayed in the buffer instead of the link.
  7708. If there is already a link at point, this command will allow you to edit link
  7709. and description parts.
  7710. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7711. be selected using completion. The path to the file will be relative to the
  7712. current directory if the file is in the current directory or a subdirectory.
  7713. Otherwise, the link will be the absolute path as completed in the minibuffer
  7714. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7715. option `org-link-file-path-type'.
  7716. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7717. the current directory or below.
  7718. With three \\[universal-argument] prefixes, negate the meaning of
  7719. `org-keep-stored-link-after-insertion'.
  7720. If `org-make-link-description-function' is non-nil, this function will be
  7721. called with the link target, and the result will be the default
  7722. link description.
  7723. If the LINK-LOCATION parameter is non-nil, this value will be
  7724. used as the link location instead of reading one interactively."
  7725. (interactive "P")
  7726. (let* ((wcf (current-window-configuration))
  7727. (region (if (org-region-active-p)
  7728. (buffer-substring (region-beginning) (region-end))))
  7729. (remove (and region (list (region-beginning) (region-end))))
  7730. (desc region)
  7731. tmphist ; byte-compile incorrectly complains about this
  7732. (link link-location)
  7733. entry file all-prefixes)
  7734. (cond
  7735. (link-location) ; specified by arg, just use it.
  7736. ((org-in-regexp org-bracket-link-regexp 1)
  7737. ;; We do have a link at point, and we are going to edit it.
  7738. (setq remove (list (match-beginning 0) (match-end 0)))
  7739. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7740. (setq link (read-string "Link: "
  7741. (org-link-unescape
  7742. (org-match-string-no-properties 1)))))
  7743. ((or (org-in-regexp org-angle-link-re)
  7744. (org-in-regexp org-plain-link-re))
  7745. ;; Convert to bracket link
  7746. (setq remove (list (match-beginning 0) (match-end 0))
  7747. link (read-string "Link: "
  7748. (org-remove-angle-brackets (match-string 0)))))
  7749. ((member complete-file '((4) (16)))
  7750. ;; Completing read for file names.
  7751. (setq link (org-file-complete-link complete-file)))
  7752. (t
  7753. ;; Read link, with completion for stored links.
  7754. (with-output-to-temp-buffer "*Org Links*"
  7755. (princ "Insert a link.
  7756. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7757. (when org-stored-links
  7758. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7759. (princ (mapconcat
  7760. (lambda (x)
  7761. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7762. (reverse org-stored-links) "\n"))))
  7763. (let ((cw (selected-window)))
  7764. (select-window (get-buffer-window "*Org Links*" 'visible))
  7765. (setq truncate-lines t)
  7766. (unless (pos-visible-in-window-p (point-max))
  7767. (org-fit-window-to-buffer))
  7768. (and (window-live-p cw) (select-window cw)))
  7769. ;; Fake a link history, containing the stored links.
  7770. (setq tmphist (append (mapcar 'car org-stored-links)
  7771. org-insert-link-history))
  7772. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7773. (mapcar 'car org-link-abbrev-alist)
  7774. org-link-types))
  7775. (unwind-protect
  7776. (progn
  7777. (setq link
  7778. (let ((org-completion-use-ido nil)
  7779. (org-completion-use-iswitchb nil))
  7780. (org-completing-read
  7781. "Link: "
  7782. (append
  7783. (mapcar (lambda (x) (list (concat x ":")))
  7784. all-prefixes)
  7785. (mapcar 'car org-stored-links))
  7786. nil nil nil
  7787. 'tmphist
  7788. (car (car org-stored-links)))))
  7789. (if (not (string-match "\\S-" link))
  7790. (error "No link selected"))
  7791. (if (or (member link all-prefixes)
  7792. (and (equal ":" (substring link -1))
  7793. (member (substring link 0 -1) all-prefixes)
  7794. (setq link (substring link 0 -1))))
  7795. (setq link (org-link-try-special-completion link))))
  7796. (set-window-configuration wcf)
  7797. (kill-buffer "*Org Links*"))
  7798. (setq entry (assoc link org-stored-links))
  7799. (or entry (push link org-insert-link-history))
  7800. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7801. (not org-keep-stored-link-after-insertion))
  7802. (setq org-stored-links (delq (assoc link org-stored-links)
  7803. org-stored-links)))
  7804. (setq desc (or desc (nth 1 entry)))))
  7805. (if (string-match org-plain-link-re link)
  7806. ;; URL-like link, normalize the use of angular brackets.
  7807. (setq link (org-make-link (org-remove-angle-brackets link))))
  7808. ;; Check if we are linking to the current file with a search option
  7809. ;; If yes, simplify the link by using only the search option.
  7810. (when (and buffer-file-name
  7811. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7812. (let* ((path (match-string 1 link))
  7813. (case-fold-search nil)
  7814. (search (match-string 2 link)))
  7815. (save-match-data
  7816. (if (equal (file-truename buffer-file-name) (file-truename path))
  7817. ;; We are linking to this same file, with a search option
  7818. (setq link search)))))
  7819. ;; Check if we can/should use a relative path. If yes, simplify the link
  7820. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7821. (let* ((type (match-string 1 link))
  7822. (path (match-string 2 link))
  7823. (origpath path)
  7824. (case-fold-search nil))
  7825. (cond
  7826. ((or (eq org-link-file-path-type 'absolute)
  7827. (equal complete-file '(16)))
  7828. (setq path (abbreviate-file-name (expand-file-name path))))
  7829. ((eq org-link-file-path-type 'noabbrev)
  7830. (setq path (expand-file-name path)))
  7831. ((eq org-link-file-path-type 'relative)
  7832. (setq path (file-relative-name path)))
  7833. (t
  7834. (save-match-data
  7835. (if (string-match (concat "^" (regexp-quote
  7836. (expand-file-name
  7837. (file-name-as-directory
  7838. default-directory))))
  7839. (expand-file-name path))
  7840. ;; We are linking a file with relative path name.
  7841. (setq path (substring (expand-file-name path)
  7842. (match-end 0)))
  7843. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7844. (setq link (concat type path))
  7845. (if (equal desc origpath)
  7846. (setq desc path))))
  7847. (if org-make-link-description-function
  7848. (setq desc (funcall org-make-link-description-function link desc)))
  7849. (setq desc (read-string "Description: " desc))
  7850. (unless (string-match "\\S-" desc) (setq desc nil))
  7851. (if remove (apply 'delete-region remove))
  7852. (insert (org-make-link-string link desc))))
  7853. (defun org-link-try-special-completion (type)
  7854. "If there is completion support for link type TYPE, offer it."
  7855. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7856. (if (functionp fun)
  7857. (funcall fun)
  7858. (read-string "Link (no completion support): " (concat type ":")))))
  7859. (defun org-file-complete-link (&optional arg)
  7860. "Create a file link using completion."
  7861. (let (file link)
  7862. (setq file (read-file-name "File: "))
  7863. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7864. (pwd1 (file-name-as-directory (abbreviate-file-name
  7865. (expand-file-name ".")))))
  7866. (cond
  7867. ((equal arg '(16))
  7868. (setq link (org-make-link
  7869. "file:"
  7870. (abbreviate-file-name (expand-file-name file)))))
  7871. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7872. (setq link (org-make-link "file:" (match-string 1 file))))
  7873. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7874. (expand-file-name file))
  7875. (setq link (org-make-link
  7876. "file:" (match-string 1 (expand-file-name file)))))
  7877. (t (setq link (org-make-link "file:" file)))))
  7878. link))
  7879. (defun org-completing-read (&rest args)
  7880. "Completing-read with SPACE being a normal character."
  7881. (let ((minibuffer-local-completion-map
  7882. (copy-keymap minibuffer-local-completion-map)))
  7883. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7884. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7885. (apply 'org-icompleting-read args)))
  7886. (defun org-completing-read-no-i (&rest args)
  7887. (let (org-completion-use-ido org-completion-use-iswitchb)
  7888. (apply 'org-completing-read args)))
  7889. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7890. "Use iswitch as a completing-read replacement to choose from choices.
  7891. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7892. from."
  7893. (let* ((iswitchb-use-virtual-buffers nil)
  7894. (iswitchb-make-buflist-hook
  7895. (lambda ()
  7896. (setq iswitchb-temp-buflist choices))))
  7897. (iswitchb-read-buffer prompt)))
  7898. (defun org-icompleting-read (&rest args)
  7899. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7900. (org-without-partial-completion
  7901. (if (and org-completion-use-ido
  7902. (fboundp 'ido-completing-read)
  7903. (boundp 'ido-mode) ido-mode
  7904. (listp (second args)))
  7905. (let ((ido-enter-matching-directory nil))
  7906. (apply 'ido-completing-read (concat (car args))
  7907. (if (consp (car (nth 1 args)))
  7908. (mapcar (lambda (x) (car x)) (nth 1 args))
  7909. (nth 1 args))
  7910. (cddr args)))
  7911. (if (and org-completion-use-iswitchb
  7912. (boundp 'iswitchb-mode) iswitchb-mode
  7913. (listp (second args)))
  7914. (apply 'org-iswitchb-completing-read (concat (car args))
  7915. (if (consp (car (nth 1 args)))
  7916. (mapcar (lambda (x) (car x)) (nth 1 args))
  7917. (nth 1 args))
  7918. (cddr args))
  7919. (apply 'completing-read args)))))
  7920. (defun org-extract-attributes (s)
  7921. "Extract the attributes cookie from a string and set as text property."
  7922. (let (a attr (start 0) key value)
  7923. (save-match-data
  7924. (when (string-match "{{\\([^}]+\\)}}$" s)
  7925. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7926. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7927. (setq key (match-string 1 a) value (match-string 2 a)
  7928. start (match-end 0)
  7929. attr (plist-put attr (intern key) value))))
  7930. (org-add-props s nil 'org-attr attr))
  7931. s))
  7932. (defun org-extract-attributes-from-string (tag)
  7933. (let (key value attr)
  7934. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7935. (setq key (match-string 1 tag) value (match-string 2 tag)
  7936. tag (replace-match "" t t tag)
  7937. attr (plist-put attr (intern key) value)))
  7938. (cons tag attr)))
  7939. (defun org-attributes-to-string (plist)
  7940. "Format a property list into an HTML attribute list."
  7941. (let ((s "") key value)
  7942. (while plist
  7943. (setq key (pop plist) value (pop plist))
  7944. (and value
  7945. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7946. s))
  7947. ;;; Opening/following a link
  7948. (defvar org-link-search-failed nil)
  7949. (defvar org-open-link-functions nil
  7950. "Hook for functions finding a plain text link.
  7951. These functions must take a single argument, the link content.
  7952. They will be called for links that look like [[link text][description]]
  7953. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7954. like a filename (e.g. \"./blue.png\").
  7955. These functions will be called *before* Org attempts to resolve the
  7956. link by doing text searches in the current buffer - so if you want a
  7957. link \"[[target]]\" to still find \"<<target>>\", your function should
  7958. handle this as a special case.
  7959. When the function does handle the link, it must return a non-nil value.
  7960. If it decides that it is not responsible for this link, it must return
  7961. nil to indicate that that Org-mode can continue with other options
  7962. like exact and fuzzy text search.")
  7963. (defun org-next-link ()
  7964. "Move forward to the next link.
  7965. If the link is in hidden text, expose it."
  7966. (interactive)
  7967. (when (and org-link-search-failed (eq this-command last-command))
  7968. (goto-char (point-min))
  7969. (message "Link search wrapped back to beginning of buffer"))
  7970. (setq org-link-search-failed nil)
  7971. (let* ((pos (point))
  7972. (ct (org-context))
  7973. (a (assoc :link ct)))
  7974. (if a (goto-char (nth 2 a)))
  7975. (if (re-search-forward org-any-link-re nil t)
  7976. (progn
  7977. (goto-char (match-beginning 0))
  7978. (if (org-invisible-p) (org-show-context)))
  7979. (goto-char pos)
  7980. (setq org-link-search-failed t)
  7981. (error "No further link found"))))
  7982. (defun org-previous-link ()
  7983. "Move backward to the previous link.
  7984. If the link is in hidden text, expose it."
  7985. (interactive)
  7986. (when (and org-link-search-failed (eq this-command last-command))
  7987. (goto-char (point-max))
  7988. (message "Link search wrapped back to end of buffer"))
  7989. (setq org-link-search-failed nil)
  7990. (let* ((pos (point))
  7991. (ct (org-context))
  7992. (a (assoc :link ct)))
  7993. (if a (goto-char (nth 1 a)))
  7994. (if (re-search-backward org-any-link-re nil t)
  7995. (progn
  7996. (goto-char (match-beginning 0))
  7997. (if (org-invisible-p) (org-show-context)))
  7998. (goto-char pos)
  7999. (setq org-link-search-failed t)
  8000. (error "No further link found"))))
  8001. (defun org-translate-link (s)
  8002. "Translate a link string if a translation function has been defined."
  8003. (if (and org-link-translation-function
  8004. (fboundp org-link-translation-function)
  8005. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8006. (progn
  8007. (setq s (funcall org-link-translation-function
  8008. (match-string 1) (match-string 2)))
  8009. (concat (car s) ":" (cdr s)))
  8010. s))
  8011. (defun org-translate-link-from-planner (type path)
  8012. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8013. This is still an experimental function, your mileage may vary."
  8014. (cond
  8015. ((member type '("http" "https" "news" "ftp"))
  8016. ;; standard Internet links are the same.
  8017. nil)
  8018. ((and (equal type "irc") (string-match "^//" path))
  8019. ;; Planner has two / at the beginning of an irc link, we have 1.
  8020. ;; We should have zero, actually....
  8021. (setq path (substring path 1)))
  8022. ((and (equal type "lisp") (string-match "^/" path))
  8023. ;; Planner has a slash, we do not.
  8024. (setq type "elisp" path (substring path 1)))
  8025. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8026. ;; A typical message link. Planner has the id after the final slash,
  8027. ;; we separate it with a hash mark
  8028. (setq path (concat (match-string 1 path) "#"
  8029. (org-remove-angle-brackets (match-string 2 path)))))
  8030. )
  8031. (cons type path))
  8032. (defun org-find-file-at-mouse (ev)
  8033. "Open file link or URL at mouse."
  8034. (interactive "e")
  8035. (mouse-set-point ev)
  8036. (org-open-at-point 'in-emacs))
  8037. (defun org-open-at-mouse (ev)
  8038. "Open file link or URL at mouse."
  8039. (interactive "e")
  8040. (mouse-set-point ev)
  8041. (if (eq major-mode 'org-agenda-mode)
  8042. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8043. (org-open-at-point))
  8044. (defvar org-window-config-before-follow-link nil
  8045. "The window configuration before following a link.
  8046. This is saved in case the need arises to restore it.")
  8047. (defvar org-open-link-marker (make-marker)
  8048. "Marker pointing to the location where `org-open-at-point; was called.")
  8049. ;;;###autoload
  8050. (defun org-open-at-point-global ()
  8051. "Follow a link like Org-mode does.
  8052. This command can be called in any mode to follow a link that has
  8053. Org-mode syntax."
  8054. (interactive)
  8055. (org-run-like-in-org-mode 'org-open-at-point))
  8056. ;;;###autoload
  8057. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8058. "Open a link in the string S, as if it was in Org-mode."
  8059. (interactive "sLink: \nP")
  8060. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8061. (with-temp-buffer
  8062. (let ((org-inhibit-startup t))
  8063. (org-mode)
  8064. (insert s)
  8065. (goto-char (point-min))
  8066. (when reference-buffer
  8067. (setq org-link-abbrev-alist-local
  8068. (with-current-buffer reference-buffer
  8069. org-link-abbrev-alist-local)))
  8070. (org-open-at-point arg reference-buffer)))))
  8071. (defun org-open-at-point (&optional in-emacs reference-buffer)
  8072. "Open link at or after point.
  8073. If there is no link at point, this function will search forward up to
  8074. the end of the current line.
  8075. Normally, files will be opened by an appropriate application. If the
  8076. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  8077. With a double prefix argument, try to open outside of Emacs, in the
  8078. application the system uses for this file type."
  8079. (interactive "P")
  8080. ;; if in a code block, then open the block's results
  8081. (unless (call-interactively #'org-babel-open-src-block-result)
  8082. (org-load-modules-maybe)
  8083. (move-marker org-open-link-marker (point))
  8084. (setq org-window-config-before-follow-link (current-window-configuration))
  8085. (org-remove-occur-highlights nil nil t)
  8086. (cond
  8087. ((and (org-on-heading-p)
  8088. (not (org-in-regexp
  8089. (concat org-plain-link-re "\\|"
  8090. org-bracket-link-regexp "\\|"
  8091. org-angle-link-re "\\|"
  8092. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8093. (not (get-text-property (point) 'org-linked-text)))
  8094. (or (org-offer-links-in-entry in-emacs)
  8095. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8096. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8097. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8098. (org-footnote-action))
  8099. (t
  8100. (let (type path link line search (pos (point)))
  8101. (catch 'match
  8102. (save-excursion
  8103. (skip-chars-forward "^]\n\r")
  8104. (when (org-in-regexp org-bracket-link-regexp 1)
  8105. (setq link (org-extract-attributes
  8106. (org-link-unescape (org-match-string-no-properties 1))))
  8107. (while (string-match " *\n *" link)
  8108. (setq link (replace-match " " t t link)))
  8109. (setq link (org-link-expand-abbrev link))
  8110. (cond
  8111. ((or (file-name-absolute-p link)
  8112. (string-match "^\\.\\.?/" link))
  8113. (setq type "file" path link))
  8114. ((string-match org-link-re-with-space3 link)
  8115. (setq type (match-string 1 link) path (match-string 2 link)))
  8116. (t (setq type "thisfile" path link)))
  8117. (throw 'match t)))
  8118. (when (get-text-property (point) 'org-linked-text)
  8119. (setq type "thisfile"
  8120. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8121. (1+ (point)) (point))
  8122. path (buffer-substring
  8123. (previous-single-property-change pos 'org-linked-text)
  8124. (next-single-property-change pos 'org-linked-text)))
  8125. (throw 'match t))
  8126. (save-excursion
  8127. (when (or (org-in-regexp org-angle-link-re)
  8128. (org-in-regexp org-plain-link-re))
  8129. (setq type (match-string 1) path (match-string 2))
  8130. (throw 'match t)))
  8131. (save-excursion
  8132. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  8133. (setq type "tags"
  8134. path (match-string 1))
  8135. (while (string-match ":" path)
  8136. (setq path (replace-match "+" t t path)))
  8137. (throw 'match t)))
  8138. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8139. (setq type "tree-match"
  8140. path (match-string 1))
  8141. (throw 'match t)))
  8142. (unless path
  8143. (error "No link found"))
  8144. ;; switch back to reference buffer
  8145. ;; needed when if called in a temporary buffer through
  8146. ;; org-open-link-from-string
  8147. (with-current-buffer (or reference-buffer (current-buffer))
  8148. ;; Remove any trailing spaces in path
  8149. (if (string-match " +\\'" path)
  8150. (setq path (replace-match "" t t path)))
  8151. (if (and org-link-translation-function
  8152. (fboundp org-link-translation-function))
  8153. ;; Check if we need to translate the link
  8154. (let ((tmp (funcall org-link-translation-function type path)))
  8155. (setq type (car tmp) path (cdr tmp))))
  8156. (cond
  8157. ((assoc type org-link-protocols)
  8158. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8159. ((equal type "mailto")
  8160. (let ((cmd (car org-link-mailto-program))
  8161. (args (cdr org-link-mailto-program)) args1
  8162. (address path) (subject "") a)
  8163. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8164. (setq address (match-string 1 path)
  8165. subject (org-link-escape (match-string 2 path))))
  8166. (while args
  8167. (cond
  8168. ((not (stringp (car args))) (push (pop args) args1))
  8169. (t (setq a (pop args))
  8170. (if (string-match "%a" a)
  8171. (setq a (replace-match address t t a)))
  8172. (if (string-match "%s" a)
  8173. (setq a (replace-match subject t t a)))
  8174. (push a args1))))
  8175. (apply cmd (nreverse args1))))
  8176. ((member type '("http" "https" "ftp" "news"))
  8177. (browse-url (concat type ":" (org-link-escape
  8178. path org-link-escape-chars-browser))))
  8179. ((string= type "doi")
  8180. (browse-url (concat "http://dx.doi.org/"
  8181. (org-link-escape
  8182. path org-link-escape-chars-browser))))
  8183. ((member type '("message"))
  8184. (browse-url (concat type ":" path)))
  8185. ((string= type "tags")
  8186. (org-tags-view in-emacs path))
  8187. ((string= type "tree-match")
  8188. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8189. ((string= type "file")
  8190. (if (string-match "::\\([0-9]+\\)\\'" path)
  8191. (setq line (string-to-number (match-string 1 path))
  8192. path (substring path 0 (match-beginning 0)))
  8193. (if (string-match "::\\(.+\\)\\'" path)
  8194. (setq search (match-string 1 path)
  8195. path (substring path 0 (match-beginning 0)))))
  8196. (if (string-match "[*?{]" (file-name-nondirectory path))
  8197. (dired path)
  8198. (org-open-file path in-emacs line search)))
  8199. ((string= type "news")
  8200. (require 'org-gnus)
  8201. (org-gnus-follow-link path))
  8202. ((string= type "shell")
  8203. (let ((cmd path))
  8204. (if (or (not org-confirm-shell-link-function)
  8205. (funcall org-confirm-shell-link-function
  8206. (format "Execute \"%s\" in shell? "
  8207. (org-add-props cmd nil
  8208. 'face 'org-warning))))
  8209. (progn
  8210. (message "Executing %s" cmd)
  8211. (shell-command cmd))
  8212. (error "Abort"))))
  8213. ((string= type "elisp")
  8214. (let ((cmd path))
  8215. (if (or (not org-confirm-elisp-link-function)
  8216. (funcall org-confirm-elisp-link-function
  8217. (format "Execute \"%s\" as elisp? "
  8218. (org-add-props cmd nil
  8219. 'face 'org-warning))))
  8220. (message "%s => %s" cmd
  8221. (if (equal (string-to-char cmd) ?\()
  8222. (eval (read cmd))
  8223. (call-interactively (read cmd))))
  8224. (error "Abort"))))
  8225. ((and (string= type "thisfile")
  8226. (run-hook-with-args-until-success
  8227. 'org-open-link-functions path)))
  8228. ((string= type "thisfile")
  8229. (if in-emacs
  8230. (switch-to-buffer-other-window
  8231. (org-get-buffer-for-internal-link (current-buffer)))
  8232. (org-mark-ring-push))
  8233. (let ((cmd `(org-link-search
  8234. ,path
  8235. ,(cond ((equal in-emacs '(4)) 'occur)
  8236. ((equal in-emacs '(16)) 'org-occur)
  8237. (t nil))
  8238. ,pos)))
  8239. (condition-case nil (eval cmd)
  8240. (error (progn (widen) (eval cmd))))))
  8241. (t
  8242. (browse-url-at-point)))))))
  8243. (move-marker org-open-link-marker nil)
  8244. (run-hook-with-args 'org-follow-link-hook)))
  8245. (defun org-offer-links-in-entry (&optional nth zero)
  8246. "Offer links in the current entry and follow the selected link.
  8247. If there is only one link, follow it immediately as well.
  8248. If NTH is an integer, immediately pick the NTH link found.
  8249. If ZERO is a string, check also this string for a link, and if
  8250. there is one, offer it as link number zero."
  8251. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8252. "\\(" org-angle-link-re "\\)\\|"
  8253. "\\(" org-plain-link-re "\\)"))
  8254. (cnt ?0)
  8255. (in-emacs (if (integerp nth) nil nth))
  8256. have-zero end links link c)
  8257. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8258. (push (match-string 0 zero) links)
  8259. (setq cnt (1- cnt) have-zero t))
  8260. (save-excursion
  8261. (org-back-to-heading t)
  8262. (setq end (save-excursion (outline-next-heading) (point)))
  8263. (while (re-search-forward re end t)
  8264. (push (match-string 0) links))
  8265. (setq links (org-uniquify (reverse links))))
  8266. (cond
  8267. ((null links)
  8268. (message "No links"))
  8269. ((equal (length links) 1)
  8270. (setq link (list (car links))))
  8271. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8272. (setq link (nth (if have-zero nth (1- nth)) links)))
  8273. (t ; we have to select a link
  8274. (save-excursion
  8275. (save-window-excursion
  8276. (delete-other-windows)
  8277. (with-output-to-temp-buffer "*Select Link*"
  8278. (mapc (lambda (l)
  8279. (if (not (string-match org-bracket-link-regexp l))
  8280. (princ (format "[%c] %s\n" (incf cnt)
  8281. (org-remove-angle-brackets l)))
  8282. (if (match-end 3)
  8283. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8284. (match-string 3 l) (match-string 1 l)))
  8285. (princ (format "[%c] %s\n" (incf cnt)
  8286. (match-string 1 l))))))
  8287. links))
  8288. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8289. (message "Select link to open, RET to open all:")
  8290. (setq c (read-char-exclusive))
  8291. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8292. (when (equal c ?q) (error "Abort"))
  8293. (if (equal c ?\C-m)
  8294. (setq link links)
  8295. (setq nth (- c ?0))
  8296. (if have-zero (setq nth (1+ nth)))
  8297. (unless (and (integerp nth) (>= (length links) nth))
  8298. (error "Invalid link selection"))
  8299. (setq link (list (nth (1- nth) links))))))
  8300. (if link
  8301. (let ((buf (current-buffer)))
  8302. (dolist (l link)
  8303. (org-open-link-from-string l in-emacs buf))
  8304. t)
  8305. nil)))
  8306. ;; Add special file links that specify the way of opening
  8307. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8308. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8309. (defun org-open-file-with-system (path)
  8310. "Open file at PATH using the system way of opening it."
  8311. (org-open-file path 'system))
  8312. (defun org-open-file-with-emacs (path)
  8313. "Open file at PATH in Emacs."
  8314. (org-open-file path 'emacs))
  8315. (defun org-remove-file-link-modifiers ()
  8316. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8317. (goto-char (point-min))
  8318. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8319. (org-if-unprotected
  8320. (replace-match "file:" t t))))
  8321. (eval-after-load "org-exp"
  8322. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8323. 'org-remove-file-link-modifiers))
  8324. ;;;; Time estimates
  8325. (defun org-get-effort (&optional pom)
  8326. "Get the effort estimate for the current entry."
  8327. (org-entry-get pom org-effort-property))
  8328. ;;; File search
  8329. (defvar org-create-file-search-functions nil
  8330. "List of functions to construct the right search string for a file link.
  8331. These functions are called in turn with point at the location to
  8332. which the link should point.
  8333. A function in the hook should first test if it would like to
  8334. handle this file type, for example by checking the `major-mode'
  8335. or the file extension. If it decides not to handle this file, it
  8336. should just return nil to give other functions a chance. If it
  8337. does handle the file, it must return the search string to be used
  8338. when following the link. The search string will be part of the
  8339. file link, given after a double colon, and `org-open-at-point'
  8340. will automatically search for it. If special measures must be
  8341. taken to make the search successful, another function should be
  8342. added to the companion hook `org-execute-file-search-functions',
  8343. which see.
  8344. A function in this hook may also use `setq' to set the variable
  8345. `description' to provide a suggestion for the descriptive text to
  8346. be used for this link when it gets inserted into an Org-mode
  8347. buffer with \\[org-insert-link].")
  8348. (defvar org-execute-file-search-functions nil
  8349. "List of functions to execute a file search triggered by a link.
  8350. Functions added to this hook must accept a single argument, the
  8351. search string that was part of the file link, the part after the
  8352. double colon. The function must first check if it would like to
  8353. handle this search, for example by checking the `major-mode' or
  8354. the file extension. If it decides not to handle this search, it
  8355. should just return nil to give other functions a chance. If it
  8356. does handle the search, it must return a non-nil value to keep
  8357. other functions from trying.
  8358. Each function can access the current prefix argument through the
  8359. variable `current-prefix-argument'. Note that a single prefix is
  8360. used to force opening a link in Emacs, so it may be good to only
  8361. use a numeric or double prefix to guide the search function.
  8362. In case this is needed, a function in this hook can also restore
  8363. the window configuration before `org-open-at-point' was called using:
  8364. (set-window-configuration org-window-config-before-follow-link)")
  8365. (defun org-link-search (s &optional type avoid-pos)
  8366. "Search for a link search option.
  8367. If S is surrounded by forward slashes, it is interpreted as a
  8368. regular expression. In org-mode files, this will create an `org-occur'
  8369. sparse tree. In ordinary files, `occur' will be used to list matches.
  8370. If the current buffer is in `dired-mode', grep will be used to search
  8371. in all files. If AVOID-POS is given, ignore matches near that position."
  8372. (let ((case-fold-search t)
  8373. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8374. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8375. (append '(("") (" ") ("\t") ("\n"))
  8376. org-emphasis-alist)
  8377. "\\|") "\\)"))
  8378. (pos (point))
  8379. (pre nil) (post nil)
  8380. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8381. (cond
  8382. ;; First check if there are any special
  8383. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8384. ;; Now try the builtin stuff
  8385. ((and (equal (string-to-char s0) ?#)
  8386. (> (length s0) 1)
  8387. (save-excursion
  8388. (goto-char (point-min))
  8389. (and
  8390. (re-search-forward
  8391. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8392. (setq type 'dedicated
  8393. pos (match-beginning 0))))
  8394. ;; There is an exact target for this
  8395. (goto-char pos)
  8396. (org-back-to-heading t)))
  8397. ((save-excursion
  8398. (goto-char (point-min))
  8399. (and
  8400. (re-search-forward
  8401. (concat "<<" (regexp-quote s0) ">>") nil t)
  8402. (setq type 'dedicated
  8403. pos (match-beginning 0))))
  8404. ;; There is an exact target for this
  8405. (goto-char pos))
  8406. ((and (string-match "^(\\(.*\\))$" s0)
  8407. (save-excursion
  8408. (goto-char (point-min))
  8409. (and
  8410. (re-search-forward
  8411. (concat "[^[]" (regexp-quote
  8412. (format org-coderef-label-format
  8413. (match-string 1 s0))))
  8414. nil t)
  8415. (setq type 'dedicated
  8416. pos (1+ (match-beginning 0))))))
  8417. ;; There is a coderef target for this
  8418. (goto-char pos))
  8419. ((string-match "^/\\(.*\\)/$" s)
  8420. ;; A regular expression
  8421. (cond
  8422. ((org-mode-p)
  8423. (org-occur (match-string 1 s)))
  8424. ;;((eq major-mode 'dired-mode)
  8425. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8426. (t (org-do-occur (match-string 1 s)))))
  8427. (t
  8428. ;; A normal search strings
  8429. (when (equal (string-to-char s) ?*)
  8430. ;; Anchor on headlines, post may include tags.
  8431. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8432. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8433. s (substring s 1)))
  8434. (remove-text-properties
  8435. 0 (length s)
  8436. '(face nil mouse-face nil keymap nil fontified nil) s)
  8437. ;; Make a series of regular expressions to find a match
  8438. (setq words (org-split-string s "[ \n\r\t]+")
  8439. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8440. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8441. "\\)" markers)
  8442. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8443. re2a (concat "[ \t\r\n]" re2a_)
  8444. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8445. re4 (concat "[^a-zA-Z_]" re4_)
  8446. re1 (concat pre re2 post)
  8447. re3 (concat pre (if pre re4_ re4) post)
  8448. re5 (concat pre ".*" re4)
  8449. re2 (concat pre re2)
  8450. re2a (concat pre (if pre re2a_ re2a))
  8451. re4 (concat pre (if pre re4_ re4))
  8452. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8453. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8454. re5 "\\)"
  8455. ))
  8456. (cond
  8457. ((eq type 'org-occur) (org-occur reall))
  8458. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8459. (t (goto-char (point-min))
  8460. (setq type 'fuzzy)
  8461. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8462. (org-search-not-self 1 re1 nil t)
  8463. (org-search-not-self 1 re2 nil t)
  8464. (org-search-not-self 1 re2a nil t)
  8465. (org-search-not-self 1 re3 nil t)
  8466. (org-search-not-self 1 re4 nil t)
  8467. (org-search-not-self 1 re5 nil t)
  8468. )
  8469. (goto-char (match-beginning 1))
  8470. (goto-char pos)
  8471. (error "No match")))))
  8472. (t
  8473. ;; Normal string-search
  8474. (goto-char (point-min))
  8475. (if (search-forward s nil t)
  8476. (goto-char (match-beginning 0))
  8477. (error "No match"))))
  8478. (and (org-mode-p) (org-show-context 'link-search))
  8479. type))
  8480. (defun org-search-not-self (group &rest args)
  8481. "Execute `re-search-forward', but only accept matches that do not
  8482. enclose the position of `org-open-link-marker'."
  8483. (let ((m org-open-link-marker))
  8484. (catch 'exit
  8485. (while (apply 're-search-forward args)
  8486. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8487. (goto-char (match-end group))
  8488. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8489. (> (match-beginning 0) (marker-position m))
  8490. (< (match-end 0) (marker-position m)))
  8491. (save-match-data
  8492. (or (not (org-in-regexp
  8493. org-bracket-link-analytic-regexp 1))
  8494. (not (match-end 4)) ; no description
  8495. (and (<= (match-beginning 4) (point))
  8496. (>= (match-end 4) (point))))))
  8497. (throw 'exit (point))))))))
  8498. (defun org-get-buffer-for-internal-link (buffer)
  8499. "Return a buffer to be used for displaying the link target of internal links."
  8500. (cond
  8501. ((not org-display-internal-link-with-indirect-buffer)
  8502. buffer)
  8503. ((string-match "(Clone)$" (buffer-name buffer))
  8504. (message "Buffer is already a clone, not making another one")
  8505. ;; we also do not modify visibility in this case
  8506. buffer)
  8507. (t ; make a new indirect buffer for displaying the link
  8508. (let* ((bn (buffer-name buffer))
  8509. (ibn (concat bn "(Clone)"))
  8510. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8511. (with-current-buffer ib (org-overview))
  8512. ib))))
  8513. (defun org-do-occur (regexp &optional cleanup)
  8514. "Call the Emacs command `occur'.
  8515. If CLEANUP is non-nil, remove the printout of the regular expression
  8516. in the *Occur* buffer. This is useful if the regex is long and not useful
  8517. to read."
  8518. (occur regexp)
  8519. (when cleanup
  8520. (let ((cwin (selected-window)) win beg end)
  8521. (when (setq win (get-buffer-window "*Occur*"))
  8522. (select-window win))
  8523. (goto-char (point-min))
  8524. (when (re-search-forward "match[a-z]+" nil t)
  8525. (setq beg (match-end 0))
  8526. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8527. (setq end (1- (match-beginning 0)))))
  8528. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8529. (goto-char (point-min))
  8530. (select-window cwin))))
  8531. ;;; The mark ring for links jumps
  8532. (defvar org-mark-ring nil
  8533. "Mark ring for positions before jumps in Org-mode.")
  8534. (defvar org-mark-ring-last-goto nil
  8535. "Last position in the mark ring used to go back.")
  8536. ;; Fill and close the ring
  8537. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8538. (loop for i from 1 to org-mark-ring-length do
  8539. (push (make-marker) org-mark-ring))
  8540. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8541. org-mark-ring)
  8542. (defun org-mark-ring-push (&optional pos buffer)
  8543. "Put the current position or POS into the mark ring and rotate it."
  8544. (interactive)
  8545. (setq pos (or pos (point)))
  8546. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8547. (move-marker (car org-mark-ring)
  8548. (or pos (point))
  8549. (or buffer (current-buffer)))
  8550. (message "%s"
  8551. (substitute-command-keys
  8552. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8553. (defun org-mark-ring-goto (&optional n)
  8554. "Jump to the previous position in the mark ring.
  8555. With prefix arg N, jump back that many stored positions. When
  8556. called several times in succession, walk through the entire ring.
  8557. Org-mode commands jumping to a different position in the current file,
  8558. or to another Org-mode file, automatically push the old position
  8559. onto the ring."
  8560. (interactive "p")
  8561. (let (p m)
  8562. (if (eq last-command this-command)
  8563. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8564. (setq p org-mark-ring))
  8565. (setq org-mark-ring-last-goto p)
  8566. (setq m (car p))
  8567. (switch-to-buffer (marker-buffer m))
  8568. (goto-char m)
  8569. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8570. (defun org-remove-angle-brackets (s)
  8571. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8572. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8573. s)
  8574. (defun org-add-angle-brackets (s)
  8575. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8576. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8577. s)
  8578. (defun org-remove-double-quotes (s)
  8579. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8580. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8581. s)
  8582. ;;; Following specific links
  8583. (defun org-follow-timestamp-link ()
  8584. (cond
  8585. ((org-at-date-range-p t)
  8586. (let ((org-agenda-start-on-weekday)
  8587. (t1 (match-string 1))
  8588. (t2 (match-string 2)))
  8589. (setq t1 (time-to-days (org-time-string-to-time t1))
  8590. t2 (time-to-days (org-time-string-to-time t2)))
  8591. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8592. ((org-at-timestamp-p t)
  8593. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8594. (substring (match-string 1) 0 10)))
  8595. 1))
  8596. (t (error "This should not happen"))))
  8597. ;;; Following file links
  8598. (defvar org-wait nil)
  8599. (defun org-open-file (path &optional in-emacs line search)
  8600. "Open the file at PATH.
  8601. First, this expands any special file name abbreviations. Then the
  8602. configuration variable `org-file-apps' is checked if it contains an
  8603. entry for this file type, and if yes, the corresponding command is launched.
  8604. If no application is found, Emacs simply visits the file.
  8605. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8606. With a double \\[universal-argument] \\[universal-argument] \
  8607. prefix arg, Org tries to avoid opening in Emacs
  8608. and to use an external application to visit the file.
  8609. Optional LINE specifies a line to go to, optional SEARCH a string
  8610. to search for. If LINE or SEARCH is given, the file will be
  8611. opened in Emacs, unless an entry from org-file-apps that makes
  8612. use of groups in a regexp matches.
  8613. If the file does not exist, an error is thrown."
  8614. (let* ((file (if (equal path "")
  8615. buffer-file-name
  8616. (substitute-in-file-name (expand-file-name path))))
  8617. (file-apps (append org-file-apps (org-default-apps)))
  8618. (apps (org-remove-if
  8619. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8620. (apps-dlink (org-remove-if-not
  8621. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8622. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8623. (dirp (if remp nil (file-directory-p file)))
  8624. (file (if (and dirp org-open-directory-means-index-dot-org)
  8625. (concat (file-name-as-directory file) "index.org")
  8626. file))
  8627. (a-m-a-p (assq 'auto-mode apps))
  8628. (dfile (downcase file))
  8629. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8630. (link (cond ((and (eq line nil)
  8631. (eq search nil))
  8632. file)
  8633. (line
  8634. (concat file "::" (number-to-string line)))
  8635. (search
  8636. (concat file "::" search))))
  8637. (dlink (downcase link))
  8638. (old-buffer (current-buffer))
  8639. (old-pos (point))
  8640. (old-mode major-mode)
  8641. ext cmd link-match-data)
  8642. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8643. (setq ext (match-string 1 dfile))
  8644. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8645. (setq ext (match-string 1 dfile))))
  8646. (cond
  8647. ((member in-emacs '((16) system))
  8648. (setq cmd (cdr (assoc 'system apps))))
  8649. (in-emacs (setq cmd 'emacs))
  8650. (t
  8651. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8652. (and dirp (cdr (assoc 'directory apps)))
  8653. ; first, try matching against apps-dlink
  8654. ; if we get a match here, store the match data for later
  8655. (let ((match (assoc-default dlink apps-dlink
  8656. 'string-match)))
  8657. (if match
  8658. (progn (setq link-match-data (match-data))
  8659. match)
  8660. (progn (setq in-emacs (or in-emacs line search))
  8661. nil))) ; if we have no match in apps-dlink,
  8662. ; always open the file in emacs if line or search
  8663. ; is given (for backwards compatibility)
  8664. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8665. 'string-match)
  8666. (cdr (assoc ext apps))
  8667. (cdr (assoc t apps))))))
  8668. (when (eq cmd 'system)
  8669. (setq cmd (cdr (assoc 'system apps))))
  8670. (when (eq cmd 'default)
  8671. (setq cmd (cdr (assoc t apps))))
  8672. (when (eq cmd 'mailcap)
  8673. (require 'mailcap)
  8674. (mailcap-parse-mailcaps)
  8675. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8676. (command (mailcap-mime-info mime-type)))
  8677. (if (stringp command)
  8678. (setq cmd command)
  8679. (setq cmd 'emacs))))
  8680. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8681. (not (file-exists-p file))
  8682. (not org-open-non-existing-files))
  8683. (error "No such file: %s" file))
  8684. (cond
  8685. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8686. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8687. (while (string-match "['\"]%s['\"]" cmd)
  8688. (setq cmd (replace-match "%s" t t cmd)))
  8689. (while (string-match "%s" cmd)
  8690. (setq cmd (replace-match
  8691. (save-match-data
  8692. (shell-quote-argument
  8693. (convert-standard-filename file)))
  8694. t t cmd)))
  8695. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8696. (save-match-data
  8697. (let ((match-index 1)
  8698. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8699. (set-match-data link-match-data)
  8700. (while (<= match-index number-of-groups)
  8701. (let ((regex (concat "%" (number-to-string match-index)))
  8702. (replace-with (match-string match-index dlink)))
  8703. (while (string-match regex cmd)
  8704. (setq cmd (replace-match replace-with t t cmd))))
  8705. (setq match-index (+ match-index 1)))))
  8706. (save-window-excursion
  8707. (start-process-shell-command cmd nil cmd)
  8708. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8709. ))
  8710. ((or (stringp cmd)
  8711. (eq cmd 'emacs))
  8712. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8713. (widen)
  8714. (if line (org-goto-line line)
  8715. (if search (org-link-search search))))
  8716. ((consp cmd)
  8717. (let ((file (convert-standard-filename file)))
  8718. (save-match-data
  8719. (set-match-data link-match-data)
  8720. (eval cmd))))
  8721. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8722. (and (org-mode-p) (eq old-mode 'org-mode)
  8723. (or (not (equal old-buffer (current-buffer)))
  8724. (not (equal old-pos (point))))
  8725. (org-mark-ring-push old-pos old-buffer))))
  8726. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8727. "This function returns non-nil if `entry' uses a regular
  8728. expression which should be matched against the whole link by
  8729. org-open-file.
  8730. It assumes that is the case when the entry uses a regular
  8731. expression which has at least one grouping construct and the
  8732. action is either a lisp form or a command string containing
  8733. '%1', i.e. using at least one subexpression match as a
  8734. parameter."
  8735. (let ((selector (car entry))
  8736. (action (cdr entry)))
  8737. (if (stringp selector)
  8738. (and (> (regexp-opt-depth selector) 0)
  8739. (or (and (stringp action)
  8740. (string-match "%[0-9]" action))
  8741. (consp action)))
  8742. nil)))
  8743. (defun org-default-apps ()
  8744. "Return the default applications for this operating system."
  8745. (cond
  8746. ((eq system-type 'darwin)
  8747. org-file-apps-defaults-macosx)
  8748. ((eq system-type 'windows-nt)
  8749. org-file-apps-defaults-windowsnt)
  8750. (t org-file-apps-defaults-gnu)))
  8751. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8752. "Convert extensions to regular expressions in the cars of LIST.
  8753. Also, weed out any non-string entries, because the return value is used
  8754. only for regexp matching.
  8755. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8756. point to the symbol `emacs', indicating that the file should
  8757. be opened in Emacs."
  8758. (append
  8759. (delq nil
  8760. (mapcar (lambda (x)
  8761. (if (not (stringp (car x)))
  8762. nil
  8763. (if (string-match "\\W" (car x))
  8764. x
  8765. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8766. list))
  8767. (if add-auto-mode
  8768. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8769. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8770. (defun org-file-remote-p (file)
  8771. "Test whether FILE specifies a location on a remote system.
  8772. Return non-nil if the location is indeed remote.
  8773. For example, the filename \"/user@host:/foo\" specifies a location
  8774. on the system \"/user@host:\"."
  8775. (cond ((fboundp 'file-remote-p)
  8776. (file-remote-p file))
  8777. ((fboundp 'tramp-handle-file-remote-p)
  8778. (tramp-handle-file-remote-p file))
  8779. ((and (boundp 'ange-ftp-name-format)
  8780. (string-match (car ange-ftp-name-format) file))
  8781. t)
  8782. (t nil)))
  8783. ;;;; Refiling
  8784. (defun org-get-org-file ()
  8785. "Read a filename, with default directory `org-directory'."
  8786. (let ((default (or org-default-notes-file remember-data-file)))
  8787. (read-file-name (format "File name [%s]: " default)
  8788. (file-name-as-directory org-directory)
  8789. default)))
  8790. (defun org-notes-order-reversed-p ()
  8791. "Check if the current file should receive notes in reversed order."
  8792. (cond
  8793. ((not org-reverse-note-order) nil)
  8794. ((eq t org-reverse-note-order) t)
  8795. ((not (listp org-reverse-note-order)) nil)
  8796. (t (catch 'exit
  8797. (let ((all org-reverse-note-order)
  8798. entry)
  8799. (while (setq entry (pop all))
  8800. (if (string-match (car entry) buffer-file-name)
  8801. (throw 'exit (cdr entry))))
  8802. nil)))))
  8803. (defvar org-refile-target-table nil
  8804. "The list of refile targets, created by `org-refile'.")
  8805. (defvar org-agenda-new-buffers nil
  8806. "Buffers created to visit agenda files.")
  8807. (defvar org-refile-cache nil
  8808. "Cache for refile targets.")
  8809. (defvar org-refile-markers nil
  8810. "All the markers used for caching refile locations.")
  8811. (defun org-refile-marker (pos)
  8812. "Get a new refile marker, but only if caching is in use."
  8813. (if (not org-refile-use-cache)
  8814. pos
  8815. (let ((m (make-marker)))
  8816. (move-marker m pos)
  8817. (push m org-refile-markers)
  8818. m)))
  8819. (defun org-refile-cache-clear ()
  8820. "Clear the refile cache and disable all the markers."
  8821. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  8822. (setq org-refile-markers nil)
  8823. (setq org-refile-cache nil)
  8824. (message "Refile cache has been cleared"))
  8825. (defun org-refile-cache-check-set (set)
  8826. "Check if all the markers in the cache still have live buffers."
  8827. (let (marker)
  8828. (catch 'exit
  8829. (while (and set (setq marker (nth 3 (pop set))))
  8830. ;; if org-refile-use-outline-path is 'file, marker may be nil
  8831. (when (and marker (null (marker-buffer marker)))
  8832. (message "not found") (sit-for 3)
  8833. (throw 'exit nil)))
  8834. t)))
  8835. (defun org-refile-cache-put (set &rest identifiers)
  8836. "Push the refile targets SET into the cache, under IDENTIFIERS."
  8837. (let* ((key (sha1 (prin1-to-string identifiers)))
  8838. (entry (assoc key org-refile-cache)))
  8839. (if entry
  8840. (setcdr entry set)
  8841. (push (cons key set) org-refile-cache))))
  8842. (defun org-refile-cache-get (&rest identifiers)
  8843. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  8844. (cond
  8845. ((not org-refile-cache) nil)
  8846. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  8847. (t
  8848. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  8849. org-refile-cache))))
  8850. (and set (org-refile-cache-check-set set) set)))))
  8851. (defun org-get-refile-targets (&optional default-buffer)
  8852. "Produce a table with refile targets."
  8853. (let ((case-fold-search nil)
  8854. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8855. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8856. targets tgs txt re files f desc descre fast-path-p level pos0)
  8857. (message "Getting targets...")
  8858. (with-current-buffer (or default-buffer (current-buffer))
  8859. (while (setq entry (pop entries))
  8860. (setq files (car entry) desc (cdr entry))
  8861. (setq fast-path-p nil)
  8862. (cond
  8863. ((null files) (setq files (list (current-buffer))))
  8864. ((eq files 'org-agenda-files)
  8865. (setq files (org-agenda-files 'unrestricted)))
  8866. ((and (symbolp files) (fboundp files))
  8867. (setq files (funcall files)))
  8868. ((and (symbolp files) (boundp files))
  8869. (setq files (symbol-value files))))
  8870. (if (stringp files) (setq files (list files)))
  8871. (cond
  8872. ((eq (car desc) :tag)
  8873. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8874. ((eq (car desc) :todo)
  8875. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8876. ((eq (car desc) :regexp)
  8877. (setq descre (cdr desc)))
  8878. ((eq (car desc) :level)
  8879. (setq descre (concat "^\\*\\{" (number-to-string
  8880. (if org-odd-levels-only
  8881. (1- (* 2 (cdr desc)))
  8882. (cdr desc)))
  8883. "\\}[ \t]")))
  8884. ((eq (car desc) :maxlevel)
  8885. (setq fast-path-p t)
  8886. (setq descre (concat "^\\*\\{1," (number-to-string
  8887. (if org-odd-levels-only
  8888. (1- (* 2 (cdr desc)))
  8889. (cdr desc)))
  8890. "\\}[ \t]")))
  8891. (t (error "Bad refiling target description %s" desc)))
  8892. (while (setq f (pop files))
  8893. (with-current-buffer
  8894. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8895. (or
  8896. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  8897. (progn
  8898. (if (bufferp f) (setq f (buffer-file-name
  8899. (buffer-base-buffer f))))
  8900. (setq f (and f (expand-file-name f)))
  8901. (if (eq org-refile-use-outline-path 'file)
  8902. (push (list (file-name-nondirectory f) f nil nil) tgs))
  8903. (save-excursion
  8904. (save-restriction
  8905. (widen)
  8906. (goto-char (point-min))
  8907. (while (re-search-forward descre nil t)
  8908. (goto-char (setq pos0 (point-at-bol)))
  8909. (catch 'next
  8910. (when org-refile-target-verify-function
  8911. (save-match-data
  8912. (or (funcall org-refile-target-verify-function)
  8913. (throw 'next t))))
  8914. (when (looking-at org-complex-heading-regexp)
  8915. (setq level (org-reduced-level
  8916. (- (match-end 1) (match-beginning 1)))
  8917. txt (org-link-display-format (match-string 4))
  8918. re (concat "^" (regexp-quote
  8919. (buffer-substring
  8920. (match-beginning 1)
  8921. (match-end 4)))))
  8922. (if (match-end 5) (setq re (concat
  8923. re "[ \t]+"
  8924. (regexp-quote
  8925. (match-string 5)))))
  8926. (setq re (concat re "[ \t]*$"))
  8927. (when org-refile-use-outline-path
  8928. (setq txt (mapconcat
  8929. 'org-protect-slash
  8930. (append
  8931. (if (eq org-refile-use-outline-path
  8932. 'file)
  8933. (list (file-name-nondirectory
  8934. (buffer-file-name
  8935. (buffer-base-buffer))))
  8936. (if (eq org-refile-use-outline-path
  8937. 'full-file-path)
  8938. (list (buffer-file-name
  8939. (buffer-base-buffer)))))
  8940. (org-get-outline-path fast-path-p
  8941. level txt)
  8942. (list txt))
  8943. "/")))
  8944. (push (list txt f re (org-refile-marker (point)))
  8945. tgs)))
  8946. (when (= (point) pos0)
  8947. ;; verification function has not moved point
  8948. (goto-char (point-at-eol))))))))
  8949. (when org-refile-use-cache
  8950. (org-refile-cache-put tgs (buffer-file-name) descre))
  8951. (setq targets (append tgs targets))
  8952. ))))
  8953. (message "Getting targets...done")
  8954. (nreverse targets)))
  8955. (defun org-protect-slash (s)
  8956. (while (string-match "/" s)
  8957. (setq s (replace-match "\\" t t s)))
  8958. s)
  8959. (defvar org-olpa (make-vector 20 nil))
  8960. (defun org-get-outline-path (&optional fastp level heading)
  8961. "Return the outline path to the current entry, as a list.
  8962. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  8963. routine which makes outline path derivations for an entire file,
  8964. avoiding backtracing. Refile target collection makes use of that."
  8965. (if fastp
  8966. (progn
  8967. (if (> level 19)
  8968. (error "Outline path failure, more than 19 levels"))
  8969. (loop for i from level upto 19 do
  8970. (aset org-olpa i nil))
  8971. (prog1
  8972. (delq nil (append org-olpa nil))
  8973. (aset org-olpa level heading)))
  8974. (let (rtn case-fold-search)
  8975. (save-excursion
  8976. (save-restriction
  8977. (widen)
  8978. (while (org-up-heading-safe)
  8979. (when (looking-at org-complex-heading-regexp)
  8980. (push (org-match-string-no-properties 4) rtn)))
  8981. rtn)))))
  8982. (defun org-format-outline-path (path &optional width prefix)
  8983. "Format the outline path PATH for display.
  8984. Width is the maximum number of characters that is available.
  8985. Prefix is a prefix to be included in the returned string,
  8986. such as the file name."
  8987. (setq width (or width 79))
  8988. (if prefix (setq width (- width (length prefix))))
  8989. (if (not path)
  8990. (or prefix "")
  8991. (let* ((nsteps (length path))
  8992. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8993. (maxwidth (if (<= total-width width)
  8994. 10000 ;; everything fits
  8995. ;; we need to shorten the level headings
  8996. (/ (- width nsteps) nsteps)))
  8997. (org-odd-levels-only nil)
  8998. (n 0)
  8999. (total (1+ (length prefix))))
  9000. (setq maxwidth (max maxwidth 10))
  9001. (concat prefix
  9002. (mapconcat
  9003. (lambda (h)
  9004. (setq n (1+ n))
  9005. (if (and (= n nsteps) (< maxwidth 10000))
  9006. (setq maxwidth (- total-width total)))
  9007. (if (< (length h) maxwidth)
  9008. (progn (setq total (+ total (length h) 1)) h)
  9009. (setq h (substring h 0 (- maxwidth 2))
  9010. total (+ total maxwidth 1))
  9011. (if (string-match "[ \t]+\\'" h)
  9012. (setq h (substring h 0 (match-beginning 0))))
  9013. (setq h (concat h "..")))
  9014. (org-add-props h nil 'face
  9015. (nth (% (1- n) org-n-level-faces)
  9016. org-level-faces))
  9017. h)
  9018. path "/")))))
  9019. (defun org-display-outline-path (&optional file current)
  9020. "Display the current outline path in the echo area."
  9021. (interactive "P")
  9022. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9023. (case-fold-search nil)
  9024. (path (and (org-mode-p) (org-get-outline-path))))
  9025. (if current (setq path (append path
  9026. (save-excursion
  9027. (org-back-to-heading t)
  9028. (if (looking-at org-complex-heading-regexp)
  9029. (list (match-string 4)))))))
  9030. (message "%s"
  9031. (org-format-outline-path
  9032. path
  9033. (1- (frame-width))
  9034. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9035. (defvar org-refile-history nil
  9036. "History for refiling operations.")
  9037. (defvar org-after-refile-insert-hook nil
  9038. "Hook run after `org-refile' has inserted its stuff at the new location.
  9039. Note that this is still *before* the stuff will be removed from
  9040. the *old* location.")
  9041. (defvar org-capture-last-stored-marker)
  9042. (defun org-refile (&optional goto default-buffer rfloc)
  9043. "Move the entry at point to another heading.
  9044. The list of target headings is compiled using the information in
  9045. `org-refile-targets', which see. This list is created before each use
  9046. and will therefore always be up-to-date.
  9047. At the target location, the entry is filed as a subitem of the target heading.
  9048. Depending on `org-reverse-note-order', the new subitem will either be the
  9049. first or the last subitem.
  9050. If there is an active region, all entries in that region will be moved.
  9051. However, the region must fulfill the requirement that the first heading
  9052. is the first one sets the top-level of the moved text - at most siblings
  9053. below it are allowed.
  9054. With prefix arg GOTO, the command will only visit the target location,
  9055. not actually move anything.
  9056. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9057. go to the location where the last refiling
  9058. operation has put the subtree.
  9059. With a prefix argument of `2', refile to the running clock.
  9060. RFLOC can be a refile location obtained in a different way.
  9061. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9062. If you are using target caching (see `org-refile-use-cache'),
  9063. You have to clear the target cache in order to find new targets.
  9064. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9065. (interactive "P")
  9066. (if (member goto '(0 (64)))
  9067. (org-refile-cache-clear)
  9068. (let* ((cbuf (current-buffer))
  9069. (regionp (org-region-active-p))
  9070. (region-start (and regionp (region-beginning)))
  9071. (region-end (and regionp (region-end)))
  9072. (region-length (and regionp (- region-end region-start)))
  9073. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9074. pos it nbuf file re level reversed)
  9075. (setq last-command nil)
  9076. (when regionp
  9077. (goto-char region-start)
  9078. (or (bolp) (goto-char (point-at-bol)))
  9079. (setq region-start (point))
  9080. (unless (org-kill-is-subtree-p
  9081. (buffer-substring region-start region-end))
  9082. (error "The region is not a (sequence of) subtree(s)")))
  9083. (if (equal goto '(16))
  9084. (org-refile-goto-last-stored)
  9085. (when (or
  9086. (and (equal goto 2)
  9087. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9088. (prog1
  9089. (setq it (list (or org-clock-heading "running clock")
  9090. (buffer-file-name
  9091. (marker-buffer org-clock-hd-marker))
  9092. ""
  9093. (marker-position org-clock-hd-marker)))
  9094. (setq goto nil)))
  9095. (setq it (or rfloc
  9096. (save-excursion
  9097. (org-refile-get-location
  9098. (if goto "Goto: " "Refile to: ") default-buffer
  9099. org-refile-allow-creating-parent-nodes)))))
  9100. (setq file (nth 1 it)
  9101. re (nth 2 it)
  9102. pos (nth 3 it))
  9103. (if (and (not goto)
  9104. pos
  9105. (equal (buffer-file-name) file)
  9106. (if regionp
  9107. (and (>= pos region-start)
  9108. (<= pos region-end))
  9109. (and (>= pos (point))
  9110. (< pos (save-excursion
  9111. (org-end-of-subtree t t))))))
  9112. (error "Cannot refile to position inside the tree or region"))
  9113. (setq nbuf (or (find-buffer-visiting file)
  9114. (find-file-noselect file)))
  9115. (if goto
  9116. (progn
  9117. (switch-to-buffer nbuf)
  9118. (goto-char pos)
  9119. (org-show-context 'org-goto))
  9120. (if regionp
  9121. (progn
  9122. (org-kill-new (buffer-substring region-start region-end))
  9123. (org-save-markers-in-region region-start region-end))
  9124. (org-copy-subtree 1 nil t))
  9125. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9126. (find-file-noselect file)))
  9127. (setq reversed (org-notes-order-reversed-p))
  9128. (save-excursion
  9129. (save-restriction
  9130. (widen)
  9131. (if pos
  9132. (progn
  9133. (goto-char pos)
  9134. (looking-at outline-regexp)
  9135. (setq level (org-get-valid-level (funcall outline-level) 1))
  9136. (goto-char
  9137. (if reversed
  9138. (or (outline-next-heading) (point-max))
  9139. (or (save-excursion (org-get-next-sibling))
  9140. (org-end-of-subtree t t)
  9141. (point-max)))))
  9142. (setq level 1)
  9143. (if (not reversed)
  9144. (goto-char (point-max))
  9145. (goto-char (point-min))
  9146. (or (outline-next-heading) (goto-char (point-max)))))
  9147. (if (not (bolp)) (newline))
  9148. (org-paste-subtree level)
  9149. (when org-log-refile
  9150. (org-add-log-setup 'refile nil nil 'findpos
  9151. org-log-refile)
  9152. (unless (eq org-log-refile 'note)
  9153. (save-excursion (org-add-log-note))))
  9154. (and org-auto-align-tags (org-set-tags nil t))
  9155. (bookmark-set "org-refile-last-stored")
  9156. ;; If we are refiling for capture, make sure that the
  9157. ;; last-capture pointers point here
  9158. (when (org-bound-and-true-p org-refile-for-capture)
  9159. (bookmark-set "org-capture-last-stored-marker")
  9160. (move-marker org-capture-last-stored-marker (point)))
  9161. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9162. (run-hooks 'org-after-refile-insert-hook))))
  9163. (if regionp
  9164. (delete-region (point) (+ (point) region-length))
  9165. (org-cut-subtree))
  9166. (when (featurep 'org-inlinetask)
  9167. (org-inlinetask-remove-END-maybe))
  9168. (setq org-markers-to-move nil)
  9169. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9170. (defun org-refile-goto-last-stored ()
  9171. "Go to the location where the last refile was stored."
  9172. (interactive)
  9173. (bookmark-jump "org-refile-last-stored")
  9174. (message "This is the location of the last refile"))
  9175. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9176. "Prompt the user for a refile location, using PROMPT."
  9177. (let ((org-refile-targets org-refile-targets)
  9178. (org-refile-use-outline-path org-refile-use-outline-path))
  9179. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  9180. (unless org-refile-target-table
  9181. (error "No refile targets"))
  9182. (let* ((cbuf (current-buffer))
  9183. (partial-completion-mode nil)
  9184. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9185. (cfunc (if (and org-refile-use-outline-path
  9186. org-outline-path-complete-in-steps)
  9187. 'org-olpath-completing-read
  9188. 'org-icompleting-read))
  9189. (extra (if org-refile-use-outline-path "/" ""))
  9190. (filename (and cfn (expand-file-name cfn)))
  9191. (tbl (mapcar
  9192. (lambda (x)
  9193. (if (and (not (member org-refile-use-outline-path
  9194. '(file full-file-path)))
  9195. (not (equal filename (nth 1 x))))
  9196. (cons (concat (car x) extra " ("
  9197. (file-name-nondirectory (nth 1 x)) ")")
  9198. (cdr x))
  9199. (cons (concat (car x) extra) (cdr x))))
  9200. org-refile-target-table))
  9201. (completion-ignore-case t)
  9202. pa answ parent-target child parent old-hist)
  9203. (setq old-hist org-refile-history)
  9204. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9205. nil 'org-refile-history))
  9206. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9207. (if pa
  9208. (progn
  9209. (when (or (not org-refile-history)
  9210. (not (eq old-hist org-refile-history))
  9211. (not (equal (car pa) (car org-refile-history))))
  9212. (setq org-refile-history
  9213. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9214. org-refile-history
  9215. (cdr org-refile-history))))
  9216. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9217. (pop org-refile-history)))
  9218. pa)
  9219. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9220. (progn
  9221. (setq parent (match-string 1 answ)
  9222. child (match-string 2 answ))
  9223. (setq parent-target (or (assoc parent tbl)
  9224. (assoc (concat parent "/") tbl)))
  9225. (when (and parent-target
  9226. (or (eq new-nodes t)
  9227. (and (eq new-nodes 'confirm)
  9228. (y-or-n-p (format "Create new node \"%s\"? "
  9229. child)))))
  9230. (org-refile-new-child parent-target child)))
  9231. (error "Invalid target location")))))
  9232. (defun org-refile-new-child (parent-target child)
  9233. "Use refile target PARENT-TARGET to add new CHILD below it."
  9234. (unless parent-target
  9235. (error "Cannot find parent for new node"))
  9236. (let ((file (nth 1 parent-target))
  9237. (pos (nth 3 parent-target))
  9238. level)
  9239. (with-current-buffer (or (find-buffer-visiting file)
  9240. (find-file-noselect file))
  9241. (save-excursion
  9242. (save-restriction
  9243. (widen)
  9244. (if pos
  9245. (goto-char pos)
  9246. (goto-char (point-max))
  9247. (if (not (bolp)) (newline)))
  9248. (when (looking-at outline-regexp)
  9249. (setq level (funcall outline-level))
  9250. (org-end-of-subtree t t))
  9251. (org-back-over-empty-lines)
  9252. (insert "\n" (make-string
  9253. (if pos (org-get-valid-level level 1) 1) ?*)
  9254. " " child "\n")
  9255. (beginning-of-line 0)
  9256. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9257. (defun org-olpath-completing-read (prompt collection &rest args)
  9258. "Read an outline path like a file name."
  9259. (let ((thetable collection)
  9260. (org-completion-use-ido nil) ; does not work with ido.
  9261. (org-completion-use-iswitchb nil)) ; or iswitchb
  9262. (apply
  9263. 'org-icompleting-read prompt
  9264. (lambda (string predicate &optional flag)
  9265. (let (rtn r f (l (length string)))
  9266. (cond
  9267. ((eq flag nil)
  9268. ;; try completion
  9269. (try-completion string thetable))
  9270. ((eq flag t)
  9271. ;; all-completions
  9272. (setq rtn (all-completions string thetable predicate))
  9273. (mapcar
  9274. (lambda (x)
  9275. (setq r (substring x l))
  9276. (if (string-match " ([^)]*)$" x)
  9277. (setq f (match-string 0 x))
  9278. (setq f ""))
  9279. (if (string-match "/" r)
  9280. (concat string (substring r 0 (match-end 0)) f)
  9281. x))
  9282. rtn))
  9283. ((eq flag 'lambda)
  9284. ;; exact match?
  9285. (assoc string thetable)))
  9286. ))
  9287. args)))
  9288. ;;;; Dynamic blocks
  9289. (defun org-find-dblock (name)
  9290. "Find the first dynamic block with name NAME in the buffer.
  9291. If not found, stay at current position and return nil."
  9292. (let (pos)
  9293. (save-excursion
  9294. (goto-char (point-min))
  9295. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9296. nil t)
  9297. (match-beginning 0))))
  9298. (if pos (goto-char pos))
  9299. pos))
  9300. (defconst org-dblock-start-re
  9301. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9302. "Matches the start line of a dynamic block, with parameters.")
  9303. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9304. "Matches the end of a dynamic block.")
  9305. (defun org-create-dblock (plist)
  9306. "Create a dynamic block section, with parameters taken from PLIST.
  9307. PLIST must contain a :name entry which is used as name of the block."
  9308. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9309. (end-of-line 1)
  9310. (newline))
  9311. (let ((col (current-column))
  9312. (name (plist-get plist :name)))
  9313. (insert "#+BEGIN: " name)
  9314. (while plist
  9315. (if (eq (car plist) :name)
  9316. (setq plist (cddr plist))
  9317. (insert " " (prin1-to-string (pop plist)))))
  9318. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9319. (beginning-of-line -2)))
  9320. (defun org-prepare-dblock ()
  9321. "Prepare dynamic block for refresh.
  9322. This empties the block, puts the cursor at the insert position and returns
  9323. the property list including an extra property :name with the block name."
  9324. (unless (looking-at org-dblock-start-re)
  9325. (error "Not at a dynamic block"))
  9326. (let* ((begdel (1+ (match-end 0)))
  9327. (name (org-no-properties (match-string 1)))
  9328. (params (append (list :name name)
  9329. (read (concat "(" (match-string 3) ")")))))
  9330. (save-excursion
  9331. (beginning-of-line 1)
  9332. (skip-chars-forward " \t")
  9333. (setq params (plist-put params :indentation-column (current-column))))
  9334. (unless (re-search-forward org-dblock-end-re nil t)
  9335. (error "Dynamic block not terminated"))
  9336. (setq params
  9337. (append params
  9338. (list :content (buffer-substring
  9339. begdel (match-beginning 0)))))
  9340. (delete-region begdel (match-beginning 0))
  9341. (goto-char begdel)
  9342. (open-line 1)
  9343. params))
  9344. (defun org-map-dblocks (&optional command)
  9345. "Apply COMMAND to all dynamic blocks in the current buffer.
  9346. If COMMAND is not given, use `org-update-dblock'."
  9347. (let ((cmd (or command 'org-update-dblock)))
  9348. (save-excursion
  9349. (goto-char (point-min))
  9350. (while (re-search-forward org-dblock-start-re nil t)
  9351. (goto-char (match-beginning 0))
  9352. (save-excursion
  9353. (condition-case nil
  9354. (funcall cmd)
  9355. (error (message "Error during update of dynamic block"))))
  9356. (unless (re-search-forward org-dblock-end-re nil t)
  9357. (error "Dynamic block not terminated"))))))
  9358. (defun org-dblock-update (&optional arg)
  9359. "User command for updating dynamic blocks.
  9360. Update the dynamic block at point. With prefix ARG, update all dynamic
  9361. blocks in the buffer."
  9362. (interactive "P")
  9363. (if arg
  9364. (org-update-all-dblocks)
  9365. (or (looking-at org-dblock-start-re)
  9366. (org-beginning-of-dblock))
  9367. (org-update-dblock)))
  9368. (defun org-update-dblock ()
  9369. "Update the dynamic block at point.
  9370. This means to empty the block, parse for parameters and then call
  9371. the correct writing function."
  9372. (save-window-excursion
  9373. (let* ((pos (point))
  9374. (line (org-current-line))
  9375. (params (org-prepare-dblock))
  9376. (name (plist-get params :name))
  9377. (indent (plist-get params :indentation-column))
  9378. (cmd (intern (concat "org-dblock-write:" name))))
  9379. (message "Updating dynamic block `%s' at line %d..." name line)
  9380. (funcall cmd params)
  9381. (message "Updating dynamic block `%s' at line %d...done" name line)
  9382. (goto-char pos)
  9383. (when (and indent (> indent 0))
  9384. (setq indent (make-string indent ?\ ))
  9385. (save-excursion
  9386. (org-beginning-of-dblock)
  9387. (forward-line 1)
  9388. (while (not (looking-at org-dblock-end-re))
  9389. (insert indent)
  9390. (beginning-of-line 2))
  9391. (when (looking-at org-dblock-end-re)
  9392. (and (looking-at "[ \t]+")
  9393. (replace-match ""))
  9394. (insert indent)))))))
  9395. (defun org-beginning-of-dblock ()
  9396. "Find the beginning of the dynamic block at point.
  9397. Error if there is no such block at point."
  9398. (let ((pos (point))
  9399. beg)
  9400. (end-of-line 1)
  9401. (if (and (re-search-backward org-dblock-start-re nil t)
  9402. (setq beg (match-beginning 0))
  9403. (re-search-forward org-dblock-end-re nil t)
  9404. (> (match-end 0) pos))
  9405. (goto-char beg)
  9406. (goto-char pos)
  9407. (error "Not in a dynamic block"))))
  9408. (defun org-update-all-dblocks ()
  9409. "Update all dynamic blocks in the buffer.
  9410. This function can be used in a hook."
  9411. (when (org-mode-p)
  9412. (org-map-dblocks 'org-update-dblock)))
  9413. ;;;; Completion
  9414. (defconst org-additional-option-like-keywords
  9415. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  9416. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  9417. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9418. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  9419. "BEGIN:" "END:"
  9420. "ORGTBL" "TBLFM:" "TBLNAME:"
  9421. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9422. "BEGIN_QUOTE" "END_QUOTE"
  9423. "BEGIN_VERSE" "END_VERSE"
  9424. "BEGIN_CENTER" "END_CENTER"
  9425. "BEGIN_SRC" "END_SRC"
  9426. "CATEGORY" "COLUMNS"
  9427. "CAPTION" "LABEL"
  9428. "SETUPFILE"
  9429. "BIND"
  9430. "MACRO"))
  9431. (defcustom org-structure-template-alist
  9432. '(
  9433. ("s" "#+begin_src ?\n\n#+end_src"
  9434. "<src lang=\"?\">\n\n</src>")
  9435. ("e" "#+begin_example\n?\n#+end_example"
  9436. "<example>\n?\n</example>")
  9437. ("q" "#+begin_quote\n?\n#+end_quote"
  9438. "<quote>\n?\n</quote>")
  9439. ("v" "#+begin_verse\n?\n#+end_verse"
  9440. "<verse>\n?\n/verse>")
  9441. ("c" "#+begin_center\n?\n#+end_center"
  9442. "<center>\n?\n/center>")
  9443. ("l" "#+begin_latex\n?\n#+end_latex"
  9444. "<literal style=\"latex\">\n?\n</literal>")
  9445. ("L" "#+latex: "
  9446. "<literal style=\"latex\">?</literal>")
  9447. ("h" "#+begin_html\n?\n#+end_html"
  9448. "<literal style=\"html\">\n?\n</literal>")
  9449. ("H" "#+html: "
  9450. "<literal style=\"html\">?</literal>")
  9451. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9452. ("A" "#+ascii: ")
  9453. ("i" "#+include %file ?"
  9454. "<include file=%file markup=\"?\">")
  9455. )
  9456. "Structure completion elements.
  9457. This is a list of abbreviation keys and values. The value gets inserted
  9458. if you type `<' followed by the key and then press the completion key,
  9459. usually `M-TAB'. %file will be replaced by a file name after prompting
  9460. for the file using completion.
  9461. There are two templates for each key, the first uses the original Org syntax,
  9462. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9463. the default when the /org-mtags.el/ module has been loaded. See also the
  9464. variable `org-mtags-prefer-muse-templates'.
  9465. This is an experimental feature, it is undecided if it is going to stay in."
  9466. :group 'org-completion
  9467. :type '(repeat
  9468. (string :tag "Key")
  9469. (string :tag "Template")
  9470. (string :tag "Muse Template")))
  9471. (defun org-try-structure-completion ()
  9472. "Try to complete a structure template before point.
  9473. This looks for strings like \"<e\" on an otherwise empty line and
  9474. expands them."
  9475. (let ((l (buffer-substring (point-at-bol) (point)))
  9476. a)
  9477. (when (and (looking-at "[ \t]*$")
  9478. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9479. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9480. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9481. (match-beginning 1)) a)
  9482. t)))
  9483. (defun org-complete-expand-structure-template (start cell)
  9484. "Expand a structure template."
  9485. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9486. (rpl (nth (if musep 2 1) cell))
  9487. (ind ""))
  9488. (delete-region start (point))
  9489. (when (string-match "\\`#\\+" rpl)
  9490. (cond
  9491. ((bolp))
  9492. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9493. (setq ind (buffer-substring (point-at-bol) (point))))
  9494. (t (newline))))
  9495. (setq start (point))
  9496. (if (string-match "%file" rpl)
  9497. (setq rpl (replace-match
  9498. (concat
  9499. "\""
  9500. (save-match-data
  9501. (abbreviate-file-name (read-file-name "Include file: ")))
  9502. "\"")
  9503. t t rpl)))
  9504. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9505. (concat "\n" ind)))
  9506. (insert rpl)
  9507. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9508. (defun org-complete (&optional arg)
  9509. "Perform completion on word at point.
  9510. At the beginning of a headline, this completes TODO keywords as given in
  9511. `org-todo-keywords'.
  9512. If the current word is preceded by a backslash, completes the TeX symbols
  9513. that are supported for HTML support.
  9514. If the current word is preceded by \"#+\", completes special words for
  9515. setting file options.
  9516. In the line after \"#+STARTUP:, complete valid keywords.\"
  9517. At all other locations, this simply calls the value of
  9518. `org-completion-fallback-command'."
  9519. (interactive "P")
  9520. (org-without-partial-completion
  9521. (catch 'exit
  9522. (let* ((a nil)
  9523. (end (point))
  9524. (beg1 (save-excursion
  9525. (skip-chars-backward (org-re "[:alnum:]_@"))
  9526. (point)))
  9527. (beg (save-excursion
  9528. (skip-chars-backward "a-zA-Z0-9_:$")
  9529. (point)))
  9530. (confirm (lambda (x) (stringp (car x))))
  9531. (searchhead (equal (char-before beg) ?*))
  9532. (struct
  9533. (when (and (member (char-before beg1) '(?. ?<))
  9534. (setq a (assoc (buffer-substring beg1 (point))
  9535. org-structure-template-alist)))
  9536. (org-complete-expand-structure-template (1- beg1) a)
  9537. (throw 'exit t)))
  9538. (tag (and (equal (char-before beg1) ?:)
  9539. (equal (char-after (point-at-bol)) ?*)))
  9540. (prop (and (equal (char-before beg1) ?:)
  9541. (not (equal (char-after (point-at-bol)) ?*))))
  9542. (texp (equal (char-before beg) ?\\))
  9543. (link (equal (char-before beg) ?\[))
  9544. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9545. beg)
  9546. "#+"))
  9547. (startup (string-match "^#\\+STARTUP:.*"
  9548. (buffer-substring (point-at-bol) (point))))
  9549. (completion-ignore-case opt)
  9550. (type nil)
  9551. (tbl nil)
  9552. (table (cond
  9553. (opt
  9554. (setq type :opt)
  9555. (require 'org-exp)
  9556. (append
  9557. (delq nil
  9558. (mapcar
  9559. (lambda (x)
  9560. (if (string-match
  9561. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9562. (cons (match-string 2 x)
  9563. (match-string 1 x))))
  9564. (org-split-string (org-get-current-options) "\n")))
  9565. (mapcar 'list org-additional-option-like-keywords)))
  9566. (startup
  9567. (setq type :startup)
  9568. org-startup-options)
  9569. (link (append org-link-abbrev-alist-local
  9570. org-link-abbrev-alist))
  9571. (texp
  9572. (setq type :tex)
  9573. (append org-entities-user org-entities))
  9574. ((string-match "\\`\\*+[ \t]+\\'"
  9575. (buffer-substring (point-at-bol) beg))
  9576. (setq type :todo)
  9577. (mapcar 'list org-todo-keywords-1))
  9578. (searchhead
  9579. (setq type :searchhead)
  9580. (save-excursion
  9581. (goto-char (point-min))
  9582. (while (re-search-forward org-todo-line-regexp nil t)
  9583. (push (list
  9584. (org-make-org-heading-search-string
  9585. (match-string 3) t))
  9586. tbl)))
  9587. tbl)
  9588. (tag (setq type :tag beg beg1)
  9589. (or org-tag-alist (org-get-buffer-tags)))
  9590. (prop (setq type :prop beg beg1)
  9591. (mapcar 'list (org-buffer-property-keys nil t t)))
  9592. (t (progn
  9593. (call-interactively org-completion-fallback-command)
  9594. (throw 'exit nil)))))
  9595. (pattern (buffer-substring-no-properties beg end))
  9596. (completion (try-completion pattern table confirm)))
  9597. (cond ((eq completion t)
  9598. (if (not (assoc (upcase pattern) table))
  9599. (message "Already complete")
  9600. (if (and (equal type :opt)
  9601. (not (member (car (assoc (upcase pattern) table))
  9602. org-additional-option-like-keywords)))
  9603. (insert (substring (cdr (assoc (upcase pattern) table))
  9604. (length pattern)))
  9605. (if (memq type '(:tag :prop)) (insert ":")))))
  9606. ((null completion)
  9607. (message "Can't find completion for \"%s\"" pattern)
  9608. (ding))
  9609. ((not (string= pattern completion))
  9610. (delete-region beg end)
  9611. (if (string-match " +$" completion)
  9612. (setq completion (replace-match "" t t completion)))
  9613. (insert completion)
  9614. (if (get-buffer-window "*Completions*")
  9615. (delete-window (get-buffer-window "*Completions*")))
  9616. (if (assoc completion table)
  9617. (if (eq type :todo) (insert " ")
  9618. (if (memq type '(:tag :prop)) (insert ":"))))
  9619. (if (and (equal type :opt) (assoc completion table))
  9620. (message "%s" (substitute-command-keys
  9621. "Press \\[org-complete] again to insert example settings"))))
  9622. (t
  9623. (message "Making completion list...")
  9624. (let ((list (sort (all-completions pattern table confirm)
  9625. 'string<)))
  9626. (with-output-to-temp-buffer "*Completions*"
  9627. (condition-case nil
  9628. ;; Protection needed for XEmacs and emacs 21
  9629. (display-completion-list list pattern)
  9630. (error (display-completion-list list)))))
  9631. (message "Making completion list...%s" "done")))))))
  9632. ;;;; TODO, DEADLINE, Comments
  9633. (defun org-toggle-comment ()
  9634. "Change the COMMENT state of an entry."
  9635. (interactive)
  9636. (save-excursion
  9637. (org-back-to-heading)
  9638. (let (case-fold-search)
  9639. (if (looking-at (concat outline-regexp
  9640. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9641. (replace-match "" t t nil 1)
  9642. (if (looking-at outline-regexp)
  9643. (progn
  9644. (goto-char (match-end 0))
  9645. (insert org-comment-string " ")))))))
  9646. (defvar org-last-todo-state-is-todo nil
  9647. "This is non-nil when the last TODO state change led to a TODO state.
  9648. If the last change removed the TODO tag or switched to DONE, then
  9649. this is nil.")
  9650. (defvar org-setting-tags nil) ; dynamically skipped
  9651. (defun org-parse-local-options (string var)
  9652. "Parse STRING for startup setting relevant for variable VAR."
  9653. (let ((rtn (symbol-value var))
  9654. e opts)
  9655. (save-match-data
  9656. (if (or (not string) (not (string-match "\\S-" string)))
  9657. rtn
  9658. (setq opts (delq nil (mapcar (lambda (x)
  9659. (setq e (assoc x org-startup-options))
  9660. (if (eq (nth 1 e) var) e nil))
  9661. (org-split-string string "[ \t]+"))))
  9662. (if (not opts)
  9663. rtn
  9664. (setq rtn nil)
  9665. (while (setq e (pop opts))
  9666. (if (not (nth 3 e))
  9667. (setq rtn (nth 2 e))
  9668. (if (not (listp rtn)) (setq rtn nil))
  9669. (push (nth 2 e) rtn)))
  9670. rtn)))))
  9671. (defvar org-todo-setup-filter-hook nil
  9672. "Hook for functions that pre-filter todo specs.
  9673. Each function takes a todo spec and returns either nil or the spec
  9674. transformed into canonical form." )
  9675. (defvar org-todo-get-default-hook nil
  9676. "Hook for functions that get a default item for todo.
  9677. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9678. nil or a string to be used for the todo mark." )
  9679. (defvar org-agenda-headline-snapshot-before-repeat)
  9680. (defun org-todo (&optional arg)
  9681. "Change the TODO state of an item.
  9682. The state of an item is given by a keyword at the start of the heading,
  9683. like
  9684. *** TODO Write paper
  9685. *** DONE Call mom
  9686. The different keywords are specified in the variable `org-todo-keywords'.
  9687. By default the available states are \"TODO\" and \"DONE\".
  9688. So for this example: when the item starts with TODO, it is changed to DONE.
  9689. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9690. DONE are present, add TODO at the beginning of the heading.
  9691. With \\[universal-argument] prefix arg, use completion to determine the new \
  9692. state.
  9693. With numeric prefix arg, switch to that state.
  9694. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9695. keywords (nextset).
  9696. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9697. For calling through lisp, arg is also interpreted in the following way:
  9698. 'none -> empty state
  9699. \"\"(empty string) -> switch to empty state
  9700. 'done -> switch to DONE
  9701. 'nextset -> switch to the next set of keywords
  9702. 'previousset -> switch to the previous set of keywords
  9703. \"WAITING\" -> switch to the specified keyword, but only if it
  9704. really is a member of `org-todo-keywords'."
  9705. (interactive "P")
  9706. (if (equal arg '(16)) (setq arg 'nextset))
  9707. (let ((org-blocker-hook org-blocker-hook)
  9708. (case-fold-search nil))
  9709. (when (equal arg '(64))
  9710. (setq arg nil org-blocker-hook nil))
  9711. (when (and org-blocker-hook
  9712. (or org-inhibit-blocking
  9713. (org-entry-get nil "NOBLOCKING")))
  9714. (setq org-blocker-hook nil))
  9715. (save-excursion
  9716. (catch 'exit
  9717. (org-back-to-heading t)
  9718. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9719. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9720. (looking-at " *"))
  9721. (let* ((match-data (match-data))
  9722. (startpos (point-at-bol))
  9723. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9724. (org-log-done org-log-done)
  9725. (org-log-repeat org-log-repeat)
  9726. (org-todo-log-states org-todo-log-states)
  9727. (this (match-string 1))
  9728. (hl-pos (match-beginning 0))
  9729. (head (org-get-todo-sequence-head this))
  9730. (ass (assoc head org-todo-kwd-alist))
  9731. (interpret (nth 1 ass))
  9732. (done-word (nth 3 ass))
  9733. (final-done-word (nth 4 ass))
  9734. (last-state (or this ""))
  9735. (completion-ignore-case t)
  9736. (member (member this org-todo-keywords-1))
  9737. (tail (cdr member))
  9738. (state (cond
  9739. ((and org-todo-key-trigger
  9740. (or (and (equal arg '(4))
  9741. (eq org-use-fast-todo-selection 'prefix))
  9742. (and (not arg) org-use-fast-todo-selection
  9743. (not (eq org-use-fast-todo-selection
  9744. 'prefix)))))
  9745. ;; Use fast selection
  9746. (org-fast-todo-selection))
  9747. ((and (equal arg '(4))
  9748. (or (not org-use-fast-todo-selection)
  9749. (not org-todo-key-trigger)))
  9750. ;; Read a state with completion
  9751. (org-icompleting-read
  9752. "State: " (mapcar (lambda(x) (list x))
  9753. org-todo-keywords-1)
  9754. nil t))
  9755. ((eq arg 'right)
  9756. (if this
  9757. (if tail (car tail) nil)
  9758. (car org-todo-keywords-1)))
  9759. ((eq arg 'left)
  9760. (if (equal member org-todo-keywords-1)
  9761. nil
  9762. (if this
  9763. (nth (- (length org-todo-keywords-1)
  9764. (length tail) 2)
  9765. org-todo-keywords-1)
  9766. (org-last org-todo-keywords-1))))
  9767. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9768. (setq arg nil))) ; hack to fall back to cycling
  9769. (arg
  9770. ;; user or caller requests a specific state
  9771. (cond
  9772. ((equal arg "") nil)
  9773. ((eq arg 'none) nil)
  9774. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9775. ((eq arg 'nextset)
  9776. (or (car (cdr (member head org-todo-heads)))
  9777. (car org-todo-heads)))
  9778. ((eq arg 'previousset)
  9779. (let ((org-todo-heads (reverse org-todo-heads)))
  9780. (or (car (cdr (member head org-todo-heads)))
  9781. (car org-todo-heads))))
  9782. ((car (member arg org-todo-keywords-1)))
  9783. ((stringp arg)
  9784. (error "State `%s' not valid in this file" arg))
  9785. ((nth (1- (prefix-numeric-value arg))
  9786. org-todo-keywords-1))))
  9787. ((null member) (or head (car org-todo-keywords-1)))
  9788. ((equal this final-done-word) nil) ;; -> make empty
  9789. ((null tail) nil) ;; -> first entry
  9790. ((memq interpret '(type priority))
  9791. (if (eq this-command last-command)
  9792. (car tail)
  9793. (if (> (length tail) 0)
  9794. (or done-word (car org-done-keywords))
  9795. nil)))
  9796. (t
  9797. (car tail))))
  9798. (state (or
  9799. (run-hook-with-args-until-success
  9800. 'org-todo-get-default-hook state last-state)
  9801. state))
  9802. (next (if state (concat " " state " ") " "))
  9803. (change-plist (list :type 'todo-state-change :from this :to state
  9804. :position startpos))
  9805. dolog now-done-p)
  9806. (when org-blocker-hook
  9807. (setq org-last-todo-state-is-todo
  9808. (not (member this org-done-keywords)))
  9809. (unless (save-excursion
  9810. (save-match-data
  9811. (run-hook-with-args-until-failure
  9812. 'org-blocker-hook change-plist)))
  9813. (if (interactive-p)
  9814. (error "TODO state change from %s to %s blocked" this state)
  9815. ;; fail silently
  9816. (message "TODO state change from %s to %s blocked" this state)
  9817. (throw 'exit nil))))
  9818. (store-match-data match-data)
  9819. (replace-match next t t)
  9820. (unless (pos-visible-in-window-p hl-pos)
  9821. (message "TODO state changed to %s" (org-trim next)))
  9822. (unless head
  9823. (setq head (org-get-todo-sequence-head state)
  9824. ass (assoc head org-todo-kwd-alist)
  9825. interpret (nth 1 ass)
  9826. done-word (nth 3 ass)
  9827. final-done-word (nth 4 ass)))
  9828. (when (memq arg '(nextset previousset))
  9829. (message "Keyword-Set %d/%d: %s"
  9830. (- (length org-todo-sets) -1
  9831. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9832. (length org-todo-sets)
  9833. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9834. (setq org-last-todo-state-is-todo
  9835. (not (member state org-done-keywords)))
  9836. (setq now-done-p (and (member state org-done-keywords)
  9837. (not (member this org-done-keywords))))
  9838. (and logging (org-local-logging logging))
  9839. (when (and (or org-todo-log-states org-log-done)
  9840. (not (eq org-inhibit-logging t))
  9841. (not (memq arg '(nextset previousset))))
  9842. ;; we need to look at recording a time and note
  9843. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9844. (nth 2 (assoc this org-todo-log-states))))
  9845. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9846. (setq dolog 'time))
  9847. (when (and state
  9848. (member state org-not-done-keywords)
  9849. (not (member this org-not-done-keywords)))
  9850. ;; This is now a todo state and was not one before
  9851. ;; If there was a CLOSED time stamp, get rid of it.
  9852. (org-add-planning-info nil nil 'closed))
  9853. (when (and now-done-p org-log-done)
  9854. ;; It is now done, and it was not done before
  9855. (org-add-planning-info 'closed (org-current-time))
  9856. (if (and (not dolog) (eq 'note org-log-done))
  9857. (org-add-log-setup 'done state this 'findpos 'note)))
  9858. (when (and state dolog)
  9859. ;; This is a non-nil state, and we need to log it
  9860. (org-add-log-setup 'state state this 'findpos dolog)))
  9861. ;; Fixup tag positioning
  9862. (org-todo-trigger-tag-changes state)
  9863. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9864. (when org-provide-todo-statistics
  9865. (org-update-parent-todo-statistics))
  9866. (run-hooks 'org-after-todo-state-change-hook)
  9867. (if (and arg (not (member state org-done-keywords)))
  9868. (setq head (org-get-todo-sequence-head state)))
  9869. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9870. ;; Do we need to trigger a repeat?
  9871. (when now-done-p
  9872. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9873. ;; This is for the agenda, take a snapshot of the headline.
  9874. (save-match-data
  9875. (setq org-agenda-headline-snapshot-before-repeat
  9876. (org-get-heading))))
  9877. (org-auto-repeat-maybe state))
  9878. ;; Fixup cursor location if close to the keyword
  9879. (if (and (outline-on-heading-p)
  9880. (not (bolp))
  9881. (save-excursion (beginning-of-line 1)
  9882. (looking-at org-todo-line-regexp))
  9883. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9884. (progn
  9885. (goto-char (or (match-end 2) (match-end 1)))
  9886. (and (looking-at " ") (just-one-space))))
  9887. (when org-trigger-hook
  9888. (save-excursion
  9889. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9890. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9891. "Block turning an entry into a TODO, using the hierarchy.
  9892. This checks whether the current task should be blocked from state
  9893. changes. Such blocking occurs when:
  9894. 1. The task has children which are not all in a completed state.
  9895. 2. A task has a parent with the property :ORDERED:, and there
  9896. are siblings prior to the current task with incomplete
  9897. status.
  9898. 3. The parent of the task is blocked because it has siblings that should
  9899. be done first, or is child of a block grandparent TODO entry."
  9900. (if (not org-enforce-todo-dependencies)
  9901. t ; if locally turned off don't block
  9902. (catch 'dont-block
  9903. ;; If this is not a todo state change, or if this entry is already DONE,
  9904. ;; do not block
  9905. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9906. (member (plist-get change-plist :from)
  9907. (cons 'done org-done-keywords))
  9908. (member (plist-get change-plist :to)
  9909. (cons 'todo org-not-done-keywords))
  9910. (not (plist-get change-plist :to)))
  9911. (throw 'dont-block t))
  9912. ;; If this task has children, and any are undone, it's blocked
  9913. (save-excursion
  9914. (org-back-to-heading t)
  9915. (let ((this-level (funcall outline-level)))
  9916. (outline-next-heading)
  9917. (let ((child-level (funcall outline-level)))
  9918. (while (and (not (eobp))
  9919. (> child-level this-level))
  9920. ;; this todo has children, check whether they are all
  9921. ;; completed
  9922. (if (and (not (org-entry-is-done-p))
  9923. (org-entry-is-todo-p))
  9924. (throw 'dont-block nil))
  9925. (outline-next-heading)
  9926. (setq child-level (funcall outline-level))))))
  9927. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9928. ;; any previous siblings are undone, it's blocked
  9929. (save-excursion
  9930. (org-back-to-heading t)
  9931. (let* ((pos (point))
  9932. (parent-pos (and (org-up-heading-safe) (point))))
  9933. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9934. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9935. (forward-line 1)
  9936. (re-search-forward org-not-done-heading-regexp pos t))
  9937. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9938. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9939. (while t
  9940. (goto-char parent-pos)
  9941. (if (not (looking-at org-not-done-heading-regexp))
  9942. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9943. (setq pos (point))
  9944. (setq parent-pos (and (org-up-heading-safe) (point)))
  9945. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9946. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  9947. (forward-line 1)
  9948. (re-search-forward org-not-done-heading-regexp pos t))
  9949. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9950. (defcustom org-track-ordered-property-with-tag nil
  9951. "Should the ORDERED property also be shown as a tag?
  9952. The ORDERED property decides if an entry should require subtasks to be
  9953. completed in sequence. Since a property is not very visible, setting
  9954. this option means that toggling the ORDERED property with the command
  9955. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9956. not relevant for the behavior, but it makes things more visible.
  9957. Note that toggling the tag with tags commands will not change the property
  9958. and therefore not influence behavior!
  9959. This can be t, meaning the tag ORDERED should be used, It can also be a
  9960. string to select a different tag for this task."
  9961. :group 'org-todo
  9962. :type '(choice
  9963. (const :tag "No tracking" nil)
  9964. (const :tag "Track with ORDERED tag" t)
  9965. (string :tag "Use other tag")))
  9966. (defun org-toggle-ordered-property ()
  9967. "Toggle the ORDERED property of the current entry.
  9968. For better visibility, you can track the value of this property with a tag.
  9969. See variable `org-track-ordered-property-with-tag'."
  9970. (interactive)
  9971. (let* ((t1 org-track-ordered-property-with-tag)
  9972. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9973. (save-excursion
  9974. (org-back-to-heading)
  9975. (if (org-entry-get nil "ORDERED")
  9976. (progn
  9977. (org-delete-property "ORDERED")
  9978. (and tag (org-toggle-tag tag 'off))
  9979. (message "Subtasks can be completed in arbitrary order"))
  9980. (org-entry-put nil "ORDERED" "t")
  9981. (and tag (org-toggle-tag tag 'on))
  9982. (message "Subtasks must be completed in sequence")))))
  9983. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9984. (defun org-block-todo-from-checkboxes (change-plist)
  9985. "Block turning an entry into a TODO, using checkboxes.
  9986. This checks whether the current task should be blocked from state
  9987. changes because there are unchecked boxes in this entry."
  9988. (if (not org-enforce-todo-checkbox-dependencies)
  9989. t ; if locally turned off don't block
  9990. (catch 'dont-block
  9991. ;; If this is not a todo state change, or if this entry is already DONE,
  9992. ;; do not block
  9993. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9994. (member (plist-get change-plist :from)
  9995. (cons 'done org-done-keywords))
  9996. (member (plist-get change-plist :to)
  9997. (cons 'todo org-not-done-keywords))
  9998. (not (plist-get change-plist :to)))
  9999. (throw 'dont-block t))
  10000. ;; If this task has checkboxes that are not checked, it's blocked
  10001. (save-excursion
  10002. (org-back-to-heading t)
  10003. (let ((beg (point)) end)
  10004. (outline-next-heading)
  10005. (setq end (point))
  10006. (goto-char beg)
  10007. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10008. end t)
  10009. (progn
  10010. (if (boundp 'org-blocked-by-checkboxes)
  10011. (setq org-blocked-by-checkboxes t))
  10012. (throw 'dont-block nil)))))
  10013. t))) ; do not block
  10014. (defun org-entry-blocked-p ()
  10015. "Is the current entry blocked?"
  10016. (if (org-entry-get nil "NOBLOCKING")
  10017. nil ;; Never block this entry
  10018. (not
  10019. (run-hook-with-args-until-failure
  10020. 'org-blocker-hook
  10021. (list :type 'todo-state-change
  10022. :position (point)
  10023. :from 'todo
  10024. :to 'done)))))
  10025. (defun org-update-statistics-cookies (all)
  10026. "Update the statistics cookie, either from TODO or from checkboxes.
  10027. This should be called with the cursor in a line with a statistics cookie."
  10028. (interactive "P")
  10029. (if all
  10030. (progn
  10031. (org-update-checkbox-count 'all)
  10032. (org-map-entries 'org-update-parent-todo-statistics))
  10033. (if (not (org-on-heading-p))
  10034. (org-update-checkbox-count)
  10035. (let ((pos (move-marker (make-marker) (point)))
  10036. end l1 l2)
  10037. (ignore-errors (org-back-to-heading t))
  10038. (if (not (org-on-heading-p))
  10039. (org-update-checkbox-count)
  10040. (setq l1 (org-outline-level))
  10041. (setq end (save-excursion
  10042. (outline-next-heading)
  10043. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10044. (point)))
  10045. (if (and (save-excursion
  10046. (re-search-forward
  10047. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10048. (not (save-excursion (re-search-forward
  10049. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10050. (org-update-checkbox-count)
  10051. (if (and l2 (> l2 l1))
  10052. (progn
  10053. (goto-char end)
  10054. (org-update-parent-todo-statistics))
  10055. (goto-char pos)
  10056. (beginning-of-line 1)
  10057. (while (re-search-forward
  10058. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10059. (point-at-eol) t)
  10060. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10061. (goto-char pos)
  10062. (move-marker pos nil)))))
  10063. (defvar org-entry-property-inherited-from) ;; defined below
  10064. (defun org-update-parent-todo-statistics ()
  10065. "Update any statistics cookie in the parent of the current headline.
  10066. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10067. the entire subtree and this will travel up the hierarchy and update
  10068. statistics everywhere."
  10069. (interactive)
  10070. (let* ((lim 0) prop
  10071. (recursive (or (not org-hierarchical-todo-statistics)
  10072. (string-match
  10073. "\\<recursive\\>"
  10074. (or (setq prop (org-entry-get
  10075. nil "COOKIE_DATA" 'inherit)) ""))))
  10076. (lim (or (and prop (marker-position
  10077. org-entry-property-inherited-from))
  10078. lim))
  10079. (first t)
  10080. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10081. level ltoggle l1 new ndel
  10082. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  10083. (catch 'exit
  10084. (save-excursion
  10085. (beginning-of-line 1)
  10086. (if (org-at-heading-p)
  10087. (setq ltoggle (funcall outline-level))
  10088. (error "This should not happen"))
  10089. (while (and (setq level (org-up-heading-safe))
  10090. (or recursive first)
  10091. (>= (point) lim))
  10092. (setq first nil cookie-present nil)
  10093. (unless (and level
  10094. (not (string-match
  10095. "\\<checkbox\\>"
  10096. (downcase
  10097. (or (org-entry-get
  10098. nil "COOKIE_DATA")
  10099. "")))))
  10100. (throw 'exit nil))
  10101. (while (re-search-forward box-re (point-at-eol) t)
  10102. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10103. (setq is-percent (match-end 2))
  10104. (save-match-data
  10105. (unless (outline-next-heading) (throw 'exit nil))
  10106. (while (and (looking-at org-complex-heading-regexp)
  10107. (> (setq l1 (length (match-string 1))) level))
  10108. (setq kwd (and (or recursive (= l1 ltoggle))
  10109. (match-string 2)))
  10110. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10111. (and (listp org-provide-todo-statistics)
  10112. (or (member kwd org-provide-todo-statistics)
  10113. (member kwd org-done-keywords))))
  10114. (setq cnt-all (1+ cnt-all))
  10115. (if (eq org-provide-todo-statistics t)
  10116. (and kwd (setq cnt-all (1+ cnt-all)))))
  10117. (and (member kwd org-done-keywords)
  10118. (setq cnt-done (1+ cnt-done)))
  10119. (outline-next-heading)))
  10120. (setq new
  10121. (if is-percent
  10122. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10123. (format "[%d/%d]" cnt-done cnt-all))
  10124. ndel (- (match-end 0) (match-beginning 0)))
  10125. (goto-char (match-beginning 0))
  10126. (insert new)
  10127. (delete-region (point) (+ (point) ndel)))
  10128. (when cookie-present
  10129. (run-hook-with-args 'org-after-todo-statistics-hook
  10130. cnt-done (- cnt-all cnt-done))))))
  10131. (run-hooks 'org-todo-statistics-hook)))
  10132. (defvar org-after-todo-statistics-hook nil
  10133. "Hook that is called after a TODO statistics cookie has been updated.
  10134. Each function is called with two arguments: the number of not-done entries
  10135. and the number of done entries.
  10136. For example, the following function, when added to this hook, will switch
  10137. an entry to DONE when all children are done, and back to TODO when new
  10138. entries are set to a TODO status. Note that this hook is only called
  10139. when there is a statistics cookie in the headline!
  10140. (defun org-summary-todo (n-done n-not-done)
  10141. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10142. (let (org-log-done org-log-states) ; turn off logging
  10143. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10144. ")
  10145. (defvar org-todo-statistics-hook nil
  10146. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10147. This hook runs even if there is no statistics cookie present, in which case
  10148. `org-after-todo-statistics-hook' would not run.")
  10149. (defun org-todo-trigger-tag-changes (state)
  10150. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10151. (let ((l org-todo-state-tags-triggers)
  10152. changes)
  10153. (when (or (not state) (equal state ""))
  10154. (setq changes (append changes (cdr (assoc "" l)))))
  10155. (when (and (stringp state) (> (length state) 0))
  10156. (setq changes (append changes (cdr (assoc state l)))))
  10157. (when (member state org-not-done-keywords)
  10158. (setq changes (append changes (cdr (assoc 'todo l)))))
  10159. (when (member state org-done-keywords)
  10160. (setq changes (append changes (cdr (assoc 'done l)))))
  10161. (dolist (c changes)
  10162. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10163. (defun org-local-logging (value)
  10164. "Get logging settings from a property VALUE."
  10165. (let* (words w a)
  10166. ;; directly set the variables, they are already local.
  10167. (setq org-log-done nil
  10168. org-log-repeat nil
  10169. org-todo-log-states nil)
  10170. (setq words (org-split-string value))
  10171. (while (setq w (pop words))
  10172. (cond
  10173. ((setq a (assoc w org-startup-options))
  10174. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10175. (set (nth 1 a) (nth 2 a))))
  10176. ((setq a (org-extract-log-state-settings w))
  10177. (and (member (car a) org-todo-keywords-1)
  10178. (push a org-todo-log-states)))))))
  10179. (defun org-get-todo-sequence-head (kwd)
  10180. "Return the head of the TODO sequence to which KWD belongs.
  10181. If KWD is not set, check if there is a text property remembering the
  10182. right sequence."
  10183. (let (p)
  10184. (cond
  10185. ((not kwd)
  10186. (or (get-text-property (point-at-bol) 'org-todo-head)
  10187. (progn
  10188. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10189. nil (point-at-eol)))
  10190. (get-text-property p 'org-todo-head))))
  10191. ((not (member kwd org-todo-keywords-1))
  10192. (car org-todo-keywords-1))
  10193. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10194. (defun org-fast-todo-selection ()
  10195. "Fast TODO keyword selection with single keys.
  10196. Returns the new TODO keyword, or nil if no state change should occur."
  10197. (let* ((fulltable org-todo-key-alist)
  10198. (done-keywords org-done-keywords) ;; needed for the faces.
  10199. (maxlen (apply 'max (mapcar
  10200. (lambda (x)
  10201. (if (stringp (car x)) (string-width (car x)) 0))
  10202. fulltable)))
  10203. (expert nil)
  10204. (fwidth (+ maxlen 3 1 3))
  10205. (ncol (/ (- (window-width) 4) fwidth))
  10206. tg cnt e c tbl
  10207. groups ingroup)
  10208. (save-excursion
  10209. (save-window-excursion
  10210. (if expert
  10211. (set-buffer (get-buffer-create " *Org todo*"))
  10212. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10213. (erase-buffer)
  10214. (org-set-local 'org-done-keywords done-keywords)
  10215. (setq tbl fulltable cnt 0)
  10216. (while (setq e (pop tbl))
  10217. (cond
  10218. ((equal e '(:startgroup))
  10219. (push '() groups) (setq ingroup t)
  10220. (when (not (= cnt 0))
  10221. (setq cnt 0)
  10222. (insert "\n"))
  10223. (insert "{ "))
  10224. ((equal e '(:endgroup))
  10225. (setq ingroup nil cnt 0)
  10226. (insert "}\n"))
  10227. ((equal e '(:newline))
  10228. (when (not (= cnt 0))
  10229. (setq cnt 0)
  10230. (insert "\n")
  10231. (setq e (car tbl))
  10232. (while (equal (car tbl) '(:newline))
  10233. (insert "\n")
  10234. (setq tbl (cdr tbl)))))
  10235. (t
  10236. (setq tg (car e) c (cdr e))
  10237. (if ingroup (push tg (car groups)))
  10238. (setq tg (org-add-props tg nil 'face
  10239. (org-get-todo-face tg)))
  10240. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10241. (insert "[" c "] " tg (make-string
  10242. (- fwidth 4 (length tg)) ?\ ))
  10243. (when (= (setq cnt (1+ cnt)) ncol)
  10244. (insert "\n")
  10245. (if ingroup (insert " "))
  10246. (setq cnt 0)))))
  10247. (insert "\n")
  10248. (goto-char (point-min))
  10249. (if (not expert) (org-fit-window-to-buffer))
  10250. (message "[a-z..]:Set [SPC]:clear")
  10251. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10252. (cond
  10253. ((or (= c ?\C-g)
  10254. (and (= c ?q) (not (rassoc c fulltable))))
  10255. (setq quit-flag t))
  10256. ((= c ?\ ) nil)
  10257. ((setq e (rassoc c fulltable) tg (car e))
  10258. tg)
  10259. (t (setq quit-flag t)))))))
  10260. (defun org-entry-is-todo-p ()
  10261. (member (org-get-todo-state) org-not-done-keywords))
  10262. (defun org-entry-is-done-p ()
  10263. (member (org-get-todo-state) org-done-keywords))
  10264. (defun org-get-todo-state ()
  10265. (save-excursion
  10266. (org-back-to-heading t)
  10267. (and (looking-at org-todo-line-regexp)
  10268. (match-end 2)
  10269. (match-string 2))))
  10270. (defun org-at-date-range-p (&optional inactive-ok)
  10271. "Is the cursor inside a date range?"
  10272. (interactive)
  10273. (save-excursion
  10274. (catch 'exit
  10275. (let ((pos (point)))
  10276. (skip-chars-backward "^[<\r\n")
  10277. (skip-chars-backward "<[")
  10278. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10279. (>= (match-end 0) pos)
  10280. (throw 'exit t))
  10281. (skip-chars-backward "^<[\r\n")
  10282. (skip-chars-backward "<[")
  10283. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10284. (>= (match-end 0) pos)
  10285. (throw 'exit t)))
  10286. nil)))
  10287. (defun org-get-repeat (&optional tagline)
  10288. "Check if there is a deadline/schedule with repeater in this entry."
  10289. (save-match-data
  10290. (save-excursion
  10291. (org-back-to-heading t)
  10292. (and (re-search-forward (if tagline
  10293. (concat tagline "\\s-*" org-repeat-re)
  10294. org-repeat-re)
  10295. (org-entry-end-position) t)
  10296. (match-string-no-properties 1)))))
  10297. (defvar org-last-changed-timestamp)
  10298. (defvar org-last-inserted-timestamp)
  10299. (defvar org-log-post-message)
  10300. (defvar org-log-note-purpose)
  10301. (defvar org-log-note-how)
  10302. (defvar org-log-note-extra)
  10303. (defun org-auto-repeat-maybe (done-word)
  10304. "Check if the current headline contains a repeated deadline/schedule.
  10305. If yes, set TODO state back to what it was and change the base date
  10306. of repeating deadline/scheduled time stamps to new date.
  10307. This function is run automatically after each state change to a DONE state."
  10308. ;; last-state is dynamically scoped into this function
  10309. (let* ((repeat (org-get-repeat))
  10310. (aa (assoc last-state org-todo-kwd-alist))
  10311. (interpret (nth 1 aa))
  10312. (head (nth 2 aa))
  10313. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10314. (msg "Entry repeats: ")
  10315. (org-log-done nil)
  10316. (org-todo-log-states nil)
  10317. (nshiftmax 10) (nshift 0)
  10318. re type n what ts time to-state)
  10319. (when repeat
  10320. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10321. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10322. org-todo-repeat-to-state))
  10323. (unless (and to-state (member to-state org-todo-keywords-1))
  10324. (setq to-state (if (eq interpret 'type) last-state head)))
  10325. (org-todo to-state)
  10326. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10327. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10328. (org-time-stamp-format t t))))
  10329. (when org-log-repeat
  10330. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10331. (memq 'org-add-log-note post-command-hook))
  10332. ;; OK, we are already setup for some record
  10333. (if (eq org-log-repeat 'note)
  10334. ;; make sure we take a note, not only a time stamp
  10335. (setq org-log-note-how 'note))
  10336. ;; Set up for taking a record
  10337. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10338. last-state
  10339. 'findpos org-log-repeat)))
  10340. (org-back-to-heading t)
  10341. (org-add-planning-info nil nil 'closed)
  10342. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10343. org-deadline-time-regexp "\\)\\|\\("
  10344. org-ts-regexp "\\)"))
  10345. (while (re-search-forward
  10346. re (save-excursion (outline-next-heading) (point)) t)
  10347. (setq type (if (match-end 1) org-scheduled-string
  10348. (if (match-end 3) org-deadline-string "Plain:"))
  10349. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10350. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10351. (setq n (string-to-number (match-string 2 ts))
  10352. what (match-string 3 ts))
  10353. (if (equal what "w") (setq n (* n 7) what "d"))
  10354. ;; Preparation, see if we need to modify the start date for the change
  10355. (when (match-end 1)
  10356. (setq time (save-match-data (org-time-string-to-time ts)))
  10357. (cond
  10358. ((equal (match-string 1 ts) ".")
  10359. ;; Shift starting date to today
  10360. (org-timestamp-change
  10361. (- (time-to-days (current-time)) (time-to-days time))
  10362. 'day))
  10363. ((equal (match-string 1 ts) "+")
  10364. (while (or (= nshift 0)
  10365. (<= (time-to-days time) (time-to-days (current-time))))
  10366. (when (= (incf nshift) nshiftmax)
  10367. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10368. (error "Abort")))
  10369. (org-timestamp-change n (cdr (assoc what whata)))
  10370. (org-at-timestamp-p t)
  10371. (setq ts (match-string 1))
  10372. (setq time (save-match-data (org-time-string-to-time ts))))
  10373. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10374. ;; rematch, so that we have everything in place for the real shift
  10375. (org-at-timestamp-p t)
  10376. (setq ts (match-string 1))
  10377. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10378. (org-timestamp-change n (cdr (assoc what whata)))
  10379. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10380. (setq org-log-post-message msg)
  10381. (message "%s" msg))))
  10382. (defun org-show-todo-tree (arg)
  10383. "Make a compact tree which shows all headlines marked with TODO.
  10384. The tree will show the lines where the regexp matches, and all higher
  10385. headlines above the match.
  10386. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10387. With a numeric prefix N, construct a sparse tree for the Nth element
  10388. of `org-todo-keywords-1'."
  10389. (interactive "P")
  10390. (let ((case-fold-search nil)
  10391. (kwd-re
  10392. (cond ((null arg) org-not-done-regexp)
  10393. ((equal arg '(4))
  10394. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10395. (mapcar 'list org-todo-keywords-1))))
  10396. (concat "\\("
  10397. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10398. "\\)\\>")))
  10399. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10400. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10401. org-todo-keywords-1)))
  10402. (t (error "Invalid prefix argument: %s" arg)))))
  10403. (message "%d TODO entries found"
  10404. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10405. (defun org-deadline (&optional remove time)
  10406. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10407. With argument REMOVE, remove any deadline from the item.
  10408. When TIME is set, it should be an internal time specification, and the
  10409. scheduling will use the corresponding date."
  10410. (interactive "P")
  10411. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10412. (repeater (and old-date
  10413. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10414. (match-string 1 old-date))))
  10415. (if remove
  10416. (progn
  10417. (when (and old-date org-log-redeadline)
  10418. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10419. org-log-redeadline))
  10420. (org-remove-timestamp-with-keyword org-deadline-string)
  10421. (message "Item no longer has a deadline."))
  10422. (org-add-planning-info 'deadline time 'closed)
  10423. (when (and old-date org-log-redeadline
  10424. (not (equal old-date
  10425. (substring org-last-inserted-timestamp 1 -1))))
  10426. (org-add-log-setup 'redeadline nil old-date 'findpos
  10427. org-log-redeadline))
  10428. (when repeater
  10429. (save-excursion
  10430. (org-back-to-heading t)
  10431. (when (re-search-forward (concat org-deadline-string " "
  10432. org-last-inserted-timestamp)
  10433. (save-excursion
  10434. (outline-next-heading) (point)) t)
  10435. (goto-char (1- (match-end 0)))
  10436. (insert " " repeater)
  10437. (setq org-last-inserted-timestamp
  10438. (concat (substring org-last-inserted-timestamp 0 -1)
  10439. " " repeater
  10440. (substring org-last-inserted-timestamp -1))))))
  10441. (message "Deadline on %s" org-last-inserted-timestamp))))
  10442. (defun org-schedule (&optional remove time)
  10443. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10444. With argument REMOVE, remove any scheduling date from the item.
  10445. When TIME is set, it should be an internal time specification, and the
  10446. scheduling will use the corresponding date."
  10447. (interactive "P")
  10448. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10449. (repeater (and old-date
  10450. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10451. (match-string 1 old-date))))
  10452. (if remove
  10453. (progn
  10454. (when (and old-date org-log-reschedule)
  10455. (org-add-log-setup 'delschedule nil old-date 'findpos
  10456. org-log-reschedule))
  10457. (org-remove-timestamp-with-keyword org-scheduled-string)
  10458. (message "Item is no longer scheduled."))
  10459. (org-add-planning-info 'scheduled time 'closed)
  10460. (when (and old-date org-log-reschedule
  10461. (not (equal old-date
  10462. (substring org-last-inserted-timestamp 1 -1))))
  10463. (org-add-log-setup 'reschedule nil old-date 'findpos
  10464. org-log-reschedule))
  10465. (when repeater
  10466. (save-excursion
  10467. (org-back-to-heading t)
  10468. (when (re-search-forward (concat org-scheduled-string " "
  10469. org-last-inserted-timestamp)
  10470. (save-excursion
  10471. (outline-next-heading) (point)) t)
  10472. (goto-char (1- (match-end 0)))
  10473. (insert " " repeater)
  10474. (setq org-last-inserted-timestamp
  10475. (concat (substring org-last-inserted-timestamp 0 -1)
  10476. " " repeater
  10477. (substring org-last-inserted-timestamp -1))))))
  10478. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10479. (defun org-get-scheduled-time (pom &optional inherit)
  10480. "Get the scheduled time as a time tuple, of a format suitable
  10481. for calling org-schedule with, or if there is no scheduling,
  10482. returns nil."
  10483. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10484. (when time
  10485. (apply 'encode-time (org-parse-time-string time)))))
  10486. (defun org-get-deadline-time (pom &optional inherit)
  10487. "Get the deadline as a time tuple, of a format suitable for
  10488. calling org-deadline with, or if there is no scheduling, returns
  10489. nil."
  10490. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10491. (when time
  10492. (apply 'encode-time (org-parse-time-string time)))))
  10493. (defun org-remove-timestamp-with-keyword (keyword)
  10494. "Remove all time stamps with KEYWORD in the current entry."
  10495. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10496. beg)
  10497. (save-excursion
  10498. (org-back-to-heading t)
  10499. (setq beg (point))
  10500. (outline-next-heading)
  10501. (while (re-search-backward re beg t)
  10502. (replace-match "")
  10503. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10504. (equal (char-before) ?\ ))
  10505. (backward-delete-char 1)
  10506. (if (string-match "^[ \t]*$" (buffer-substring
  10507. (point-at-bol) (point-at-eol)))
  10508. (delete-region (point-at-bol)
  10509. (min (point-max) (1+ (point-at-eol))))))))))
  10510. (defun org-add-planning-info (what &optional time &rest remove)
  10511. "Insert new timestamp with keyword in the line directly after the headline.
  10512. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10513. If non is given, the user is prompted for a date.
  10514. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10515. be removed."
  10516. (interactive)
  10517. (let (org-time-was-given org-end-time-was-given ts
  10518. end default-time default-input)
  10519. (catch 'exit
  10520. (when (and (not time) (memq what '(scheduled deadline)))
  10521. ;; Try to get a default date/time from existing timestamp
  10522. (save-excursion
  10523. (org-back-to-heading t)
  10524. (setq end (save-excursion (outline-next-heading) (point)))
  10525. (when (re-search-forward (if (eq what 'scheduled)
  10526. org-scheduled-time-regexp
  10527. org-deadline-time-regexp)
  10528. end t)
  10529. (setq ts (match-string 1)
  10530. default-time
  10531. (apply 'encode-time (org-parse-time-string ts))
  10532. default-input (and ts (org-get-compact-tod ts))))))
  10533. (when what
  10534. ;; If necessary, get the time from the user
  10535. (setq time (or time (org-read-date nil 'to-time nil nil
  10536. default-time default-input))))
  10537. (when (and org-insert-labeled-timestamps-at-point
  10538. (member what '(scheduled deadline)))
  10539. (insert
  10540. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10541. (org-insert-time-stamp time org-time-was-given
  10542. nil nil nil (list org-end-time-was-given))
  10543. (setq what nil))
  10544. (save-excursion
  10545. (save-restriction
  10546. (let (col list elt ts buffer-invisibility-spec)
  10547. (org-back-to-heading t)
  10548. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10549. (goto-char (match-end 1))
  10550. (setq col (current-column))
  10551. (goto-char (match-end 0))
  10552. (if (eobp) (insert "\n") (forward-char 1))
  10553. (when (and (not what)
  10554. (not (looking-at
  10555. (concat "[ \t]*"
  10556. org-keyword-time-not-clock-regexp))))
  10557. ;; Nothing to add, nothing to remove...... :-)
  10558. (throw 'exit nil))
  10559. (if (and (not (looking-at outline-regexp))
  10560. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10561. "[^\r\n]*"))
  10562. (not (equal (match-string 1) org-clock-string)))
  10563. (narrow-to-region (match-beginning 0) (match-end 0))
  10564. (insert-before-markers "\n")
  10565. (backward-char 1)
  10566. (narrow-to-region (point) (point))
  10567. (and org-adapt-indentation (org-indent-to-column col)))
  10568. ;; Check if we have to remove something.
  10569. (setq list (cons what remove))
  10570. (while list
  10571. (setq elt (pop list))
  10572. (goto-char (point-min))
  10573. (when (or (and (eq elt 'scheduled)
  10574. (re-search-forward org-scheduled-time-regexp nil t))
  10575. (and (eq elt 'deadline)
  10576. (re-search-forward org-deadline-time-regexp nil t))
  10577. (and (eq elt 'closed)
  10578. (re-search-forward org-closed-time-regexp nil t)))
  10579. (replace-match "")
  10580. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10581. (skip-chars-backward " ")
  10582. (if (looking-at " +") (replace-match ""))))
  10583. (goto-char (point-max))
  10584. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10585. (when what
  10586. (insert
  10587. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10588. (cond ((eq what 'scheduled) org-scheduled-string)
  10589. ((eq what 'deadline) org-deadline-string)
  10590. ((eq what 'closed) org-closed-string))
  10591. " ")
  10592. (setq ts (org-insert-time-stamp
  10593. time
  10594. (or org-time-was-given
  10595. (and (eq what 'closed) org-log-done-with-time))
  10596. (eq what 'closed)
  10597. nil nil (list org-end-time-was-given)))
  10598. (end-of-line 1))
  10599. (goto-char (point-min))
  10600. (widen)
  10601. (if (and (looking-at "[ \t]*\n")
  10602. (equal (char-before) ?\n))
  10603. (delete-region (1- (point)) (point-at-eol)))
  10604. ts))))))
  10605. (defvar org-log-note-marker (make-marker))
  10606. (defvar org-log-note-purpose nil)
  10607. (defvar org-log-note-state nil)
  10608. (defvar org-log-note-previous-state nil)
  10609. (defvar org-log-note-how nil)
  10610. (defvar org-log-note-extra nil)
  10611. (defvar org-log-note-window-configuration nil)
  10612. (defvar org-log-note-return-to (make-marker))
  10613. (defvar org-log-post-message nil
  10614. "Message to be displayed after a log note has been stored.
  10615. The auto-repeater uses this.")
  10616. (defun org-add-note ()
  10617. "Add a note to the current entry.
  10618. This is done in the same way as adding a state change note."
  10619. (interactive)
  10620. (org-add-log-setup 'note nil nil 'findpos nil))
  10621. (defvar org-property-end-re)
  10622. (defun org-add-log-setup (&optional purpose state prev-state
  10623. findpos how &optional extra)
  10624. "Set up the post command hook to take a note.
  10625. If this is about to TODO state change, the new state is expected in STATE.
  10626. When FINDPOS is non-nil, find the correct position for the note in
  10627. the current entry. If not, assume that it can be inserted at point.
  10628. HOW is an indicator what kind of note should be created.
  10629. EXTRA is additional text that will be inserted into the notes buffer."
  10630. (let* ((org-log-into-drawer (org-log-into-drawer))
  10631. (drawer (cond ((stringp org-log-into-drawer)
  10632. org-log-into-drawer)
  10633. (org-log-into-drawer "LOGBOOK")
  10634. (t nil))))
  10635. (save-restriction
  10636. (save-excursion
  10637. (when findpos
  10638. (org-back-to-heading t)
  10639. (narrow-to-region (point) (save-excursion
  10640. (outline-next-heading) (point)))
  10641. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10642. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10643. "[^\r\n]*\\)?"))
  10644. (goto-char (match-end 0))
  10645. (cond
  10646. (drawer
  10647. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10648. nil t)
  10649. (progn
  10650. (goto-char (match-end 0))
  10651. (or org-log-states-order-reversed
  10652. (and (re-search-forward org-property-end-re nil t)
  10653. (goto-char (1- (match-beginning 0))))))
  10654. (insert "\n:" drawer ":\n:END:")
  10655. (beginning-of-line 0)
  10656. (org-indent-line-function)
  10657. (beginning-of-line 2)
  10658. (org-indent-line-function)
  10659. (end-of-line 0)))
  10660. ((and org-log-state-notes-insert-after-drawers
  10661. (save-excursion
  10662. (forward-line) (looking-at org-drawer-regexp)))
  10663. (forward-line)
  10664. (while (looking-at org-drawer-regexp)
  10665. (goto-char (match-end 0))
  10666. (re-search-forward org-property-end-re (point-max) t)
  10667. (forward-line))
  10668. (forward-line -1)))
  10669. (unless org-log-states-order-reversed
  10670. (and (= (char-after) ?\n) (forward-char 1))
  10671. (org-skip-over-state-notes)
  10672. (skip-chars-backward " \t\n\r")))
  10673. (move-marker org-log-note-marker (point))
  10674. (setq org-log-note-purpose purpose
  10675. org-log-note-state state
  10676. org-log-note-previous-state prev-state
  10677. org-log-note-how how
  10678. org-log-note-extra extra)
  10679. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10680. (defun org-skip-over-state-notes ()
  10681. "Skip past the list of State notes in an entry."
  10682. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10683. (while (looking-at "[ \t]*- State")
  10684. (condition-case nil
  10685. (org-next-item)
  10686. (error (org-end-of-item)))))
  10687. (defun org-add-log-note (&optional purpose)
  10688. "Pop up a window for taking a note, and add this note later at point."
  10689. (remove-hook 'post-command-hook 'org-add-log-note)
  10690. (setq org-log-note-window-configuration (current-window-configuration))
  10691. (delete-other-windows)
  10692. (move-marker org-log-note-return-to (point))
  10693. (switch-to-buffer (marker-buffer org-log-note-marker))
  10694. (goto-char org-log-note-marker)
  10695. (org-switch-to-buffer-other-window "*Org Note*")
  10696. (erase-buffer)
  10697. (if (memq org-log-note-how '(time state))
  10698. (let (current-prefix-arg) (org-store-log-note))
  10699. (let ((org-inhibit-startup t)) (org-mode))
  10700. (insert (format "# Insert note for %s.
  10701. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10702. (cond
  10703. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10704. ((eq org-log-note-purpose 'done) "closed todo item")
  10705. ((eq org-log-note-purpose 'state)
  10706. (format "state change from \"%s\" to \"%s\""
  10707. (or org-log-note-previous-state "")
  10708. (or org-log-note-state "")))
  10709. ((eq org-log-note-purpose 'reschedule)
  10710. "rescheduling")
  10711. ((eq org-log-note-purpose 'delschedule)
  10712. "no longer scheduled")
  10713. ((eq org-log-note-purpose 'redeadline)
  10714. "changing deadline")
  10715. ((eq org-log-note-purpose 'deldeadline)
  10716. "removing deadline")
  10717. ((eq org-log-note-purpose 'refile)
  10718. "refiling")
  10719. ((eq org-log-note-purpose 'note)
  10720. "this entry")
  10721. (t (error "This should not happen")))))
  10722. (if org-log-note-extra (insert org-log-note-extra))
  10723. (org-set-local 'org-finish-function 'org-store-log-note)))
  10724. (defvar org-note-abort nil) ; dynamically scoped
  10725. (defun org-store-log-note ()
  10726. "Finish taking a log note, and insert it to where it belongs."
  10727. (let ((txt (buffer-string))
  10728. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10729. lines ind)
  10730. (kill-buffer (current-buffer))
  10731. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10732. (setq txt (replace-match "" t t txt)))
  10733. (if (string-match "\\s-+\\'" txt)
  10734. (setq txt (replace-match "" t t txt)))
  10735. (setq lines (org-split-string txt "\n"))
  10736. (when (and note (string-match "\\S-" note))
  10737. (setq note
  10738. (org-replace-escapes
  10739. note
  10740. (list (cons "%u" (user-login-name))
  10741. (cons "%U" user-full-name)
  10742. (cons "%t" (format-time-string
  10743. (org-time-stamp-format 'long 'inactive)
  10744. (current-time)))
  10745. (cons "%T" (format-time-string
  10746. (org-time-stamp-format 'long nil)
  10747. (current-time)))
  10748. (cons "%s" (if org-log-note-state
  10749. (concat "\"" org-log-note-state "\"")
  10750. ""))
  10751. (cons "%S" (if org-log-note-previous-state
  10752. (concat "\"" org-log-note-previous-state "\"")
  10753. "\"\"")))))
  10754. (if lines (setq note (concat note " \\\\")))
  10755. (push note lines))
  10756. (when (or current-prefix-arg org-note-abort)
  10757. (when org-log-into-drawer
  10758. (org-remove-empty-drawer-at
  10759. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10760. org-log-note-marker))
  10761. (setq lines nil))
  10762. (when lines
  10763. (with-current-buffer (marker-buffer org-log-note-marker)
  10764. (save-excursion
  10765. (goto-char org-log-note-marker)
  10766. (move-marker org-log-note-marker nil)
  10767. (end-of-line 1)
  10768. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10769. (insert "- " (pop lines))
  10770. (org-indent-line-function)
  10771. (beginning-of-line 1)
  10772. (looking-at "[ \t]*")
  10773. (setq ind (concat (match-string 0) " "))
  10774. (end-of-line 1)
  10775. (while lines (insert "\n" ind (pop lines)))
  10776. (message "Note stored")
  10777. (org-back-to-heading t)
  10778. (org-cycle-hide-drawers 'children)))))
  10779. (set-window-configuration org-log-note-window-configuration)
  10780. (with-current-buffer (marker-buffer org-log-note-return-to)
  10781. (goto-char org-log-note-return-to))
  10782. (move-marker org-log-note-return-to nil)
  10783. (and org-log-post-message (message "%s" org-log-post-message)))
  10784. (defun org-remove-empty-drawer-at (drawer pos)
  10785. "Remove an empty drawer DRAWER at position POS.
  10786. POS may also be a marker."
  10787. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10788. (save-excursion
  10789. (save-restriction
  10790. (widen)
  10791. (goto-char pos)
  10792. (if (org-in-regexp
  10793. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10794. (replace-match ""))))))
  10795. (defun org-sparse-tree (&optional arg)
  10796. "Create a sparse tree, prompt for the details.
  10797. This command can create sparse trees. You first need to select the type
  10798. of match used to create the tree:
  10799. t Show all TODO entries.
  10800. T Show entries with a specific TODO keyword.
  10801. m Show entries selected by a tags/property match.
  10802. p Enter a property name and its value (both with completion on existing
  10803. names/values) and show entries with that property.
  10804. / Show entries matching a regular expression (`r' can be used as well)
  10805. d Show deadlines due within `org-deadline-warning-days'.
  10806. b Show deadlines and scheduled items before a date.
  10807. a Show deadlines and scheduled items after a date."
  10808. (interactive "P")
  10809. (let (ans kwd value)
  10810. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty [d]eadlines\n [b]efore-date [a]fter-date")
  10811. (setq ans (read-char-exclusive))
  10812. (cond
  10813. ((equal ans ?d)
  10814. (call-interactively 'org-check-deadlines))
  10815. ((equal ans ?b)
  10816. (call-interactively 'org-check-before-date))
  10817. ((equal ans ?a)
  10818. (call-interactively 'org-check-after-date))
  10819. ((equal ans ?t)
  10820. (org-show-todo-tree nil))
  10821. ((equal ans ?T)
  10822. (org-show-todo-tree '(4)))
  10823. ((member ans '(?T ?m))
  10824. (call-interactively 'org-match-sparse-tree))
  10825. ((member ans '(?p ?P))
  10826. (setq kwd (org-icompleting-read "Property: "
  10827. (mapcar 'list (org-buffer-property-keys))))
  10828. (setq value (org-icompleting-read "Value: "
  10829. (mapcar 'list (org-property-values kwd))))
  10830. (unless (string-match "\\`{.*}\\'" value)
  10831. (setq value (concat "\"" value "\"")))
  10832. (org-match-sparse-tree arg (concat kwd "=" value)))
  10833. ((member ans '(?r ?R ?/))
  10834. (call-interactively 'org-occur))
  10835. (t (error "No such sparse tree command \"%c\"" ans)))))
  10836. (defvar org-occur-highlights nil
  10837. "List of overlays used for occur matches.")
  10838. (make-variable-buffer-local 'org-occur-highlights)
  10839. (defvar org-occur-parameters nil
  10840. "Parameters of the active org-occur calls.
  10841. This is a list, each call to org-occur pushes as cons cell,
  10842. containing the regular expression and the callback, onto the list.
  10843. The list can contain several entries if `org-occur' has been called
  10844. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10845. will only contain one set of parameters. When the highlights are
  10846. removed (for example with `C-c C-c', or with the next edit (depending
  10847. on `org-remove-highlights-with-change'), this variable is emptied
  10848. as well.")
  10849. (make-variable-buffer-local 'org-occur-parameters)
  10850. (defun org-occur (regexp &optional keep-previous callback)
  10851. "Make a compact tree which shows all matches of REGEXP.
  10852. The tree will show the lines where the regexp matches, and all higher
  10853. headlines above the match. It will also show the heading after the match,
  10854. to make sure editing the matching entry is easy.
  10855. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10856. call to `org-occur' will be kept, to allow stacking of calls to this
  10857. command.
  10858. If CALLBACK is non-nil, it is a function which is called to confirm
  10859. that the match should indeed be shown."
  10860. (interactive "sRegexp: \nP")
  10861. (when (equal regexp "")
  10862. (error "Regexp cannot be empty"))
  10863. (unless keep-previous
  10864. (org-remove-occur-highlights nil nil t))
  10865. (push (cons regexp callback) org-occur-parameters)
  10866. (let ((cnt 0))
  10867. (save-excursion
  10868. (goto-char (point-min))
  10869. (if (or (not keep-previous) ; do not want to keep
  10870. (not org-occur-highlights)) ; no previous matches
  10871. ;; hide everything
  10872. (org-overview))
  10873. (while (re-search-forward regexp nil t)
  10874. (when (or (not callback)
  10875. (save-match-data (funcall callback)))
  10876. (setq cnt (1+ cnt))
  10877. (when org-highlight-sparse-tree-matches
  10878. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10879. (org-show-context 'occur-tree))))
  10880. (when org-remove-highlights-with-change
  10881. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10882. nil 'local))
  10883. (unless org-sparse-tree-open-archived-trees
  10884. (org-hide-archived-subtrees (point-min) (point-max)))
  10885. (run-hooks 'org-occur-hook)
  10886. (if (interactive-p)
  10887. (message "%d match(es) for regexp %s" cnt regexp))
  10888. cnt))
  10889. (defun org-show-context (&optional key)
  10890. "Make sure point and context are visible.
  10891. How much context is shown depends upon the variables
  10892. `org-show-hierarchy-above', `org-show-following-heading'. and
  10893. `org-show-siblings'."
  10894. (let ((heading-p (org-on-heading-p t))
  10895. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10896. (following-p (org-get-alist-option org-show-following-heading key))
  10897. (entry-p (org-get-alist-option org-show-entry-below key))
  10898. (siblings-p (org-get-alist-option org-show-siblings key)))
  10899. (catch 'exit
  10900. ;; Show heading or entry text
  10901. (if (and heading-p (not entry-p))
  10902. (org-flag-heading nil) ; only show the heading
  10903. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10904. (org-show-hidden-entry))) ; show entire entry
  10905. (when following-p
  10906. ;; Show next sibling, or heading below text
  10907. (save-excursion
  10908. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10909. (org-flag-heading nil))))
  10910. (when siblings-p (org-show-siblings))
  10911. (when hierarchy-p
  10912. ;; show all higher headings, possibly with siblings
  10913. (save-excursion
  10914. (while (and (condition-case nil
  10915. (progn (org-up-heading-all 1) t)
  10916. (error nil))
  10917. (not (bobp)))
  10918. (org-flag-heading nil)
  10919. (when siblings-p (org-show-siblings))))))))
  10920. (defvar org-reveal-start-hook nil
  10921. "Hook run before revealing a location.")
  10922. (defun org-reveal (&optional siblings)
  10923. "Show current entry, hierarchy above it, and the following headline.
  10924. This can be used to show a consistent set of context around locations
  10925. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10926. not t for the search context.
  10927. With optional argument SIBLINGS, on each level of the hierarchy all
  10928. siblings are shown. This repairs the tree structure to what it would
  10929. look like when opened with hierarchical calls to `org-cycle'.
  10930. With double optional argument \\[universal-argument] \\[universal-argument], \
  10931. go to the parent and show the
  10932. entire tree."
  10933. (interactive "P")
  10934. (run-hooks 'org-reveal-start-hook)
  10935. (let ((org-show-hierarchy-above t)
  10936. (org-show-following-heading t)
  10937. (org-show-siblings (if siblings t org-show-siblings)))
  10938. (org-show-context nil))
  10939. (when (equal siblings '(16))
  10940. (save-excursion
  10941. (when (org-up-heading-safe)
  10942. (org-show-subtree)
  10943. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10944. (defun org-highlight-new-match (beg end)
  10945. "Highlight from BEG to END and mark the highlight is an occur headline."
  10946. (let ((ov (make-overlay beg end)))
  10947. (overlay-put ov 'face 'secondary-selection)
  10948. (push ov org-occur-highlights)))
  10949. (defun org-remove-occur-highlights (&optional beg end noremove)
  10950. "Remove the occur highlights from the buffer.
  10951. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10952. from the `before-change-functions' in the current buffer."
  10953. (interactive)
  10954. (unless org-inhibit-highlight-removal
  10955. (mapc 'delete-overlay org-occur-highlights)
  10956. (setq org-occur-highlights nil)
  10957. (setq org-occur-parameters nil)
  10958. (unless noremove
  10959. (remove-hook 'before-change-functions
  10960. 'org-remove-occur-highlights 'local))))
  10961. ;;;; Priorities
  10962. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10963. "Regular expression matching the priority indicator.")
  10964. (defvar org-remove-priority-next-time nil)
  10965. (defun org-priority-up ()
  10966. "Increase the priority of the current item."
  10967. (interactive)
  10968. (org-priority 'up))
  10969. (defun org-priority-down ()
  10970. "Decrease the priority of the current item."
  10971. (interactive)
  10972. (org-priority 'down))
  10973. (defun org-priority (&optional action)
  10974. "Change the priority of an item by ARG.
  10975. ACTION can be `set', `up', `down', or a character."
  10976. (interactive)
  10977. (unless org-enable-priority-commands
  10978. (error "Priority commands are disabled"))
  10979. (setq action (or action 'set))
  10980. (let (current new news have remove)
  10981. (save-excursion
  10982. (org-back-to-heading t)
  10983. (if (looking-at org-priority-regexp)
  10984. (setq current (string-to-char (match-string 2))
  10985. have t)
  10986. (setq current org-default-priority))
  10987. (cond
  10988. ((eq action 'remove)
  10989. (setq remove t new ?\ ))
  10990. ((or (eq action 'set)
  10991. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10992. (if (not (eq action 'set))
  10993. (setq new action)
  10994. (message "Priority %c-%c, SPC to remove: "
  10995. org-highest-priority org-lowest-priority)
  10996. (setq new (read-char-exclusive)))
  10997. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10998. (= (upcase org-lowest-priority) org-lowest-priority))
  10999. (setq new (upcase new)))
  11000. (cond ((equal new ?\ ) (setq remove t))
  11001. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11002. (error "Priority must be between `%c' and `%c'"
  11003. org-highest-priority org-lowest-priority))))
  11004. ((eq action 'up)
  11005. (if (and (not have) (eq last-command this-command))
  11006. (setq new org-lowest-priority)
  11007. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11008. org-default-priority (1- current)))))
  11009. ((eq action 'down)
  11010. (if (and (not have) (eq last-command this-command))
  11011. (setq new org-highest-priority)
  11012. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11013. org-default-priority (1+ current)))))
  11014. (t (error "Invalid action")))
  11015. (if (or (< (upcase new) org-highest-priority)
  11016. (> (upcase new) org-lowest-priority))
  11017. (setq remove t))
  11018. (setq news (format "%c" new))
  11019. (if have
  11020. (if remove
  11021. (replace-match "" t t nil 1)
  11022. (replace-match news t t nil 2))
  11023. (if remove
  11024. (error "No priority cookie found in line")
  11025. (let ((case-fold-search nil))
  11026. (looking-at org-todo-line-regexp))
  11027. (if (match-end 2)
  11028. (progn
  11029. (goto-char (match-end 2))
  11030. (insert " [#" news "]"))
  11031. (goto-char (match-beginning 3))
  11032. (insert "[#" news "] "))))
  11033. (org-preserve-lc (org-set-tags nil 'align)))
  11034. (if remove
  11035. (message "Priority removed")
  11036. (message "Priority of current item set to %s" news))))
  11037. (defun org-get-priority (s)
  11038. "Find priority cookie and return priority."
  11039. (save-match-data
  11040. (if (not (string-match org-priority-regexp s))
  11041. (* 1000 (- org-lowest-priority org-default-priority))
  11042. (* 1000 (- org-lowest-priority
  11043. (string-to-char (match-string 2 s)))))))
  11044. ;;;; Tags
  11045. (defvar org-agenda-archives-mode)
  11046. (defvar org-map-continue-from nil
  11047. "Position from where mapping should continue.
  11048. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11049. (defvar org-scanner-tags nil
  11050. "The current tag list while the tags scanner is running.")
  11051. (defvar org-trust-scanner-tags nil
  11052. "Should `org-get-tags-at' use the tags fro the scanner.
  11053. This is for internal dynamical scoping only.
  11054. When this is non-nil, the function `org-get-tags-at' will return the value
  11055. of `org-scanner-tags' instead of building the list by itself. This
  11056. can lead to large speed-ups when the tags scanner is used in a file with
  11057. many entries, and when the list of tags is retrieved, for example to
  11058. obtain a list of properties. Building the tags list for each entry in such
  11059. a file becomes an N^2 operation - but with this variable set, it scales
  11060. as N.")
  11061. (defun org-scan-tags (action matcher &optional todo-only)
  11062. "Scan headline tags with inheritance and produce output ACTION.
  11063. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11064. or `agenda' to produce an entry list for an agenda view. It can also be
  11065. a Lisp form or a function that should be called at each matched headline, in
  11066. this case the return value is a list of all return values from these calls.
  11067. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11068. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11069. only lines with a TODO keyword are included in the output."
  11070. (require 'org-agenda)
  11071. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11072. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11073. (org-re
  11074. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  11075. (props (list 'face 'default
  11076. 'done-face 'org-agenda-done
  11077. 'undone-face 'default
  11078. 'mouse-face 'highlight
  11079. 'org-not-done-regexp org-not-done-regexp
  11080. 'org-todo-regexp org-todo-regexp
  11081. 'help-echo
  11082. (format "mouse-2 or RET jump to org file %s"
  11083. (abbreviate-file-name
  11084. (or (buffer-file-name (buffer-base-buffer))
  11085. (buffer-name (buffer-base-buffer)))))))
  11086. (case-fold-search nil)
  11087. (org-map-continue-from nil)
  11088. lspos tags tags-list
  11089. (tags-alist (list (cons 0 org-file-tags)))
  11090. (llast 0) rtn rtn1 level category i txt
  11091. todo marker entry priority)
  11092. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11093. (setq action (list 'lambda nil action)))
  11094. (save-excursion
  11095. (goto-char (point-min))
  11096. (when (eq action 'sparse-tree)
  11097. (org-overview)
  11098. (org-remove-occur-highlights))
  11099. (while (re-search-forward re nil t)
  11100. (catch :skip
  11101. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11102. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11103. (goto-char (setq lspos (match-beginning 0)))
  11104. (setq level (org-reduced-level (funcall outline-level))
  11105. category (org-get-category))
  11106. (setq i llast llast level)
  11107. ;; remove tag lists from same and sublevels
  11108. (while (>= i level)
  11109. (when (setq entry (assoc i tags-alist))
  11110. (setq tags-alist (delete entry tags-alist)))
  11111. (setq i (1- i)))
  11112. ;; add the next tags
  11113. (when tags
  11114. (setq tags (org-split-string tags ":")
  11115. tags-alist
  11116. (cons (cons level tags) tags-alist)))
  11117. ;; compile tags for current headline
  11118. (setq tags-list
  11119. (if org-use-tag-inheritance
  11120. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11121. tags)
  11122. org-scanner-tags tags-list)
  11123. (when org-use-tag-inheritance
  11124. (setcdr (car tags-alist)
  11125. (mapcar (lambda (x)
  11126. (setq x (copy-sequence x))
  11127. (org-add-prop-inherited x))
  11128. (cdar tags-alist))))
  11129. (when (and tags org-use-tag-inheritance
  11130. (or (not (eq t org-use-tag-inheritance))
  11131. org-tags-exclude-from-inheritance))
  11132. ;; selective inheritance, remove uninherited ones
  11133. (setcdr (car tags-alist)
  11134. (org-remove-uniherited-tags (cdar tags-alist))))
  11135. (when (and (or (not todo-only)
  11136. (and (member todo org-not-done-keywords)
  11137. (or (not org-agenda-tags-todo-honor-ignore-options)
  11138. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11139. (let ((case-fold-search t)) (eval matcher))
  11140. (or
  11141. (not (member org-archive-tag tags-list))
  11142. ;; we have an archive tag, should we use this anyway?
  11143. (or (not org-agenda-skip-archived-trees)
  11144. (and (eq action 'agenda) org-agenda-archives-mode))))
  11145. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11146. ;; select this headline
  11147. (cond
  11148. ((eq action 'sparse-tree)
  11149. (and org-highlight-sparse-tree-matches
  11150. (org-get-heading) (match-end 0)
  11151. (org-highlight-new-match
  11152. (match-beginning 0) (match-beginning 1)))
  11153. (org-show-context 'tags-tree))
  11154. ((eq action 'agenda)
  11155. (setq txt (org-format-agenda-item
  11156. ""
  11157. (concat
  11158. (if (eq org-tags-match-list-sublevels 'indented)
  11159. (make-string (1- level) ?.) "")
  11160. (org-get-heading))
  11161. category
  11162. tags-list
  11163. )
  11164. priority (org-get-priority txt))
  11165. (goto-char lspos)
  11166. (setq marker (org-agenda-new-marker))
  11167. (org-add-props txt props
  11168. 'org-marker marker 'org-hd-marker marker 'org-category category
  11169. 'todo-state todo
  11170. 'priority priority 'type "tagsmatch")
  11171. (push txt rtn))
  11172. ((functionp action)
  11173. (setq org-map-continue-from nil)
  11174. (save-excursion
  11175. (setq rtn1 (funcall action))
  11176. (push rtn1 rtn)))
  11177. (t (error "Invalid action")))
  11178. ;; if we are to skip sublevels, jump to end of subtree
  11179. (unless org-tags-match-list-sublevels
  11180. (org-end-of-subtree t)
  11181. (backward-char 1))))
  11182. ;; Get the correct position from where to continue
  11183. (if org-map-continue-from
  11184. (goto-char org-map-continue-from)
  11185. (and (= (point) lspos) (end-of-line 1)))))
  11186. (when (and (eq action 'sparse-tree)
  11187. (not org-sparse-tree-open-archived-trees))
  11188. (org-hide-archived-subtrees (point-min) (point-max)))
  11189. (nreverse rtn)))
  11190. (defun org-remove-uniherited-tags (tags)
  11191. "Remove all tags that are not inherited from the list TAGS."
  11192. (cond
  11193. ((eq org-use-tag-inheritance t)
  11194. (if org-tags-exclude-from-inheritance
  11195. (org-delete-all org-tags-exclude-from-inheritance tags)
  11196. tags))
  11197. ((not org-use-tag-inheritance) nil)
  11198. ((stringp org-use-tag-inheritance)
  11199. (delq nil (mapcar
  11200. (lambda (x)
  11201. (if (and (string-match org-use-tag-inheritance x)
  11202. (not (member x org-tags-exclude-from-inheritance)))
  11203. x nil))
  11204. tags)))
  11205. ((listp org-use-tag-inheritance)
  11206. (delq nil (mapcar
  11207. (lambda (x)
  11208. (if (member x org-use-tag-inheritance) x nil))
  11209. tags)))))
  11210. (defvar todo-only) ;; dynamically scoped
  11211. (defun org-match-sparse-tree (&optional todo-only match)
  11212. "Create a sparse tree according to tags string MATCH.
  11213. MATCH can contain positive and negative selection of tags, like
  11214. \"+WORK+URGENT-WITHBOSS\".
  11215. If optional argument TODO-ONLY is non-nil, only select lines that are
  11216. also TODO lines."
  11217. (interactive "P")
  11218. (org-prepare-agenda-buffers (list (current-buffer)))
  11219. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11220. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11221. (defvar org-cached-props nil)
  11222. (defun org-cached-entry-get (pom property)
  11223. (if (or (eq t org-use-property-inheritance)
  11224. (and (stringp org-use-property-inheritance)
  11225. (string-match org-use-property-inheritance property))
  11226. (and (listp org-use-property-inheritance)
  11227. (member property org-use-property-inheritance)))
  11228. ;; Caching is not possible, check it directly
  11229. (org-entry-get pom property 'inherit)
  11230. ;; Get all properties, so that we can do complicated checks easily
  11231. (cdr (assoc property (or org-cached-props
  11232. (setq org-cached-props
  11233. (org-entry-properties pom)))))))
  11234. (defun org-global-tags-completion-table (&optional files)
  11235. "Return the list of all tags in all agenda buffer/files."
  11236. (save-excursion
  11237. (org-uniquify
  11238. (delq nil
  11239. (apply 'append
  11240. (mapcar
  11241. (lambda (file)
  11242. (set-buffer (find-file-noselect file))
  11243. (append (org-get-buffer-tags)
  11244. (mapcar (lambda (x) (if (stringp (car-safe x))
  11245. (list (car-safe x)) nil))
  11246. org-tag-alist)))
  11247. (if (and files (car files))
  11248. files
  11249. (org-agenda-files))))))))
  11250. (defun org-make-tags-matcher (match)
  11251. "Create the TAGS//TODO matcher form for the selection string MATCH."
  11252. ;; todo-only is scoped dynamically into this function, and the function
  11253. ;; may change it if the matcher asks for it.
  11254. (unless match
  11255. ;; Get a new match request, with completion
  11256. (let ((org-last-tags-completion-table
  11257. (org-global-tags-completion-table)))
  11258. (setq match (org-completing-read-no-i
  11259. "Match: " 'org-tags-completion-function nil nil nil
  11260. 'org-tags-history))))
  11261. ;; Parse the string and create a lisp form
  11262. (let ((match0 match)
  11263. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  11264. minus tag mm
  11265. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11266. orterms term orlist re-p str-p level-p level-op time-p
  11267. prop-p pn pv po cat-p gv rest)
  11268. (if (string-match "/+" match)
  11269. ;; match contains also a todo-matching request
  11270. (progn
  11271. (setq tagsmatch (substring match 0 (match-beginning 0))
  11272. todomatch (substring match (match-end 0)))
  11273. (if (string-match "^!" todomatch)
  11274. (setq todo-only t todomatch (substring todomatch 1)))
  11275. (if (string-match "^\\s-*$" todomatch)
  11276. (setq todomatch nil)))
  11277. ;; only matching tags
  11278. (setq tagsmatch match todomatch nil))
  11279. ;; Make the tags matcher
  11280. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11281. (setq tagsmatcher t)
  11282. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11283. (while (setq term (pop orterms))
  11284. (while (and (equal (substring term -1) "\\") orterms)
  11285. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11286. (while (string-match re term)
  11287. (setq rest (substring term (match-end 0))
  11288. minus (and (match-end 1)
  11289. (equal (match-string 1 term) "-"))
  11290. tag (match-string 2 term)
  11291. re-p (equal (string-to-char tag) ?{)
  11292. level-p (match-end 4)
  11293. prop-p (match-end 5)
  11294. mm (cond
  11295. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11296. (level-p
  11297. (setq level-op (org-op-to-function (match-string 3 term)))
  11298. `(,level-op level ,(string-to-number
  11299. (match-string 4 term))))
  11300. (prop-p
  11301. (setq pn (match-string 5 term)
  11302. po (match-string 6 term)
  11303. pv (match-string 7 term)
  11304. cat-p (equal pn "CATEGORY")
  11305. re-p (equal (string-to-char pv) ?{)
  11306. str-p (equal (string-to-char pv) ?\")
  11307. time-p (save-match-data
  11308. (string-match "^\"[[<].*[]>]\"$" pv))
  11309. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11310. (if time-p (setq pv (org-matcher-time pv)))
  11311. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11312. (cond
  11313. ((equal pn "CATEGORY")
  11314. (setq gv '(get-text-property (point) 'org-category)))
  11315. ((equal pn "TODO")
  11316. (setq gv 'todo))
  11317. (t
  11318. (setq gv `(org-cached-entry-get nil ,pn))))
  11319. (if re-p
  11320. (if (eq po 'org<>)
  11321. `(not (string-match ,pv (or ,gv "")))
  11322. `(string-match ,pv (or ,gv "")))
  11323. (if str-p
  11324. `(,po (or ,gv "") ,pv)
  11325. `(,po (string-to-number (or ,gv ""))
  11326. ,(string-to-number pv) ))))
  11327. (t `(member ,tag tags-list)))
  11328. mm (if minus (list 'not mm) mm)
  11329. term rest)
  11330. (push mm tagsmatcher))
  11331. (push (if (> (length tagsmatcher) 1)
  11332. (cons 'and tagsmatcher)
  11333. (car tagsmatcher))
  11334. orlist)
  11335. (setq tagsmatcher nil))
  11336. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11337. (setq tagsmatcher
  11338. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11339. ;; Make the todo matcher
  11340. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11341. (setq todomatcher t)
  11342. (setq orterms (org-split-string todomatch "|") orlist nil)
  11343. (while (setq term (pop orterms))
  11344. (while (string-match re term)
  11345. (setq minus (and (match-end 1)
  11346. (equal (match-string 1 term) "-"))
  11347. kwd (match-string 2 term)
  11348. re-p (equal (string-to-char kwd) ?{)
  11349. term (substring term (match-end 0))
  11350. mm (if re-p
  11351. `(string-match ,(substring kwd 1 -1) todo)
  11352. (list 'equal 'todo kwd))
  11353. mm (if minus (list 'not mm) mm))
  11354. (push mm todomatcher))
  11355. (push (if (> (length todomatcher) 1)
  11356. (cons 'and todomatcher)
  11357. (car todomatcher))
  11358. orlist)
  11359. (setq todomatcher nil))
  11360. (setq todomatcher (if (> (length orlist) 1)
  11361. (cons 'or orlist) (car orlist))))
  11362. ;; Return the string and lisp forms of the matcher
  11363. (setq matcher (if todomatcher
  11364. (list 'and tagsmatcher todomatcher)
  11365. tagsmatcher))
  11366. (cons match0 matcher)))
  11367. (defun org-op-to-function (op &optional stringp)
  11368. "Turn an operator into the appropriate function."
  11369. (setq op
  11370. (cond
  11371. ((equal op "<" ) '(< string< org-time<))
  11372. ((equal op ">" ) '(> org-string> org-time>))
  11373. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11374. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11375. ((member op '("=" "==")) '(= string= org-time=))
  11376. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11377. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11378. (defun org<> (a b) (not (= a b)))
  11379. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11380. (defun org-string>= (a b) (not (string< a b)))
  11381. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11382. (defun org-string<> (a b) (not (string= a b)))
  11383. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11384. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11385. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11386. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11387. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11388. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11389. (defun org-2ft (s)
  11390. "Convert S to a floating point time.
  11391. If S is already a number, just return it. If it is a string, parse
  11392. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11393. (cond
  11394. ((numberp s) s)
  11395. ((stringp s)
  11396. (condition-case nil
  11397. (float-time (apply 'encode-time (org-parse-time-string s)))
  11398. (error 0.)))
  11399. (t 0.)))
  11400. (defun org-time-today ()
  11401. "Time in seconds today at 0:00.
  11402. Returns the float number of seconds since the beginning of the
  11403. epoch to the beginning of today (00:00)."
  11404. (float-time (apply 'encode-time
  11405. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11406. (defun org-matcher-time (s)
  11407. "Interpret a time comparison value."
  11408. (save-match-data
  11409. (cond
  11410. ((string= s "<now>") (float-time))
  11411. ((string= s "<today>") (org-time-today))
  11412. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11413. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11414. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11415. (+ (org-time-today)
  11416. (* (string-to-number (match-string 1 s))
  11417. (cdr (assoc (match-string 2 s)
  11418. '(("d" . 86400.0) ("w" . 604800.0)
  11419. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11420. (t (org-2ft s)))))
  11421. (defun org-match-any-p (re list)
  11422. "Does re match any element of list?"
  11423. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11424. (delq nil list))
  11425. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11426. (defvar org-tags-overlay (make-overlay 1 1))
  11427. (org-detach-overlay org-tags-overlay)
  11428. (defun org-get-local-tags-at (&optional pos)
  11429. "Get a list of tags defined in the current headline."
  11430. (org-get-tags-at pos 'local))
  11431. (defun org-get-local-tags ()
  11432. "Get a list of tags defined in the current headline."
  11433. (org-get-tags-at nil 'local))
  11434. (defun org-get-tags-at (&optional pos local)
  11435. "Get a list of all headline tags applicable at POS.
  11436. POS defaults to point. If tags are inherited, the list contains
  11437. the targets in the same sequence as the headlines appear, i.e.
  11438. the tags of the current headline come last.
  11439. When LOCAL is non-nil, only return tags from the current headline,
  11440. ignore inherited ones."
  11441. (interactive)
  11442. (if (and org-trust-scanner-tags
  11443. (or (not pos) (equal pos (point)))
  11444. (not local))
  11445. org-scanner-tags
  11446. (let (tags ltags lastpos parent)
  11447. (save-excursion
  11448. (save-restriction
  11449. (widen)
  11450. (goto-char (or pos (point)))
  11451. (save-match-data
  11452. (catch 'done
  11453. (condition-case nil
  11454. (progn
  11455. (org-back-to-heading t)
  11456. (while (not (equal lastpos (point)))
  11457. (setq lastpos (point))
  11458. (when (looking-at
  11459. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11460. (setq ltags (org-split-string
  11461. (org-match-string-no-properties 1) ":"))
  11462. (when parent
  11463. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11464. (setq tags (append
  11465. (if parent
  11466. (org-remove-uniherited-tags ltags)
  11467. ltags)
  11468. tags)))
  11469. (or org-use-tag-inheritance (throw 'done t))
  11470. (if local (throw 'done t))
  11471. (or (org-up-heading-safe) (error nil))
  11472. (setq parent t)))
  11473. (error nil)))))
  11474. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11475. (defun org-add-prop-inherited (s)
  11476. (add-text-properties 0 (length s) '(inherited t) s)
  11477. s)
  11478. (defun org-toggle-tag (tag &optional onoff)
  11479. "Toggle the tag TAG for the current line.
  11480. If ONOFF is `on' or `off', don't toggle but set to this state."
  11481. (let (res current)
  11482. (save-excursion
  11483. (org-back-to-heading t)
  11484. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11485. (point-at-eol) t)
  11486. (progn
  11487. (setq current (match-string 1))
  11488. (replace-match ""))
  11489. (setq current ""))
  11490. (setq current (nreverse (org-split-string current ":")))
  11491. (cond
  11492. ((eq onoff 'on)
  11493. (setq res t)
  11494. (or (member tag current) (push tag current)))
  11495. ((eq onoff 'off)
  11496. (or (not (member tag current)) (setq current (delete tag current))))
  11497. (t (if (member tag current)
  11498. (setq current (delete tag current))
  11499. (setq res t)
  11500. (push tag current))))
  11501. (end-of-line 1)
  11502. (if current
  11503. (progn
  11504. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11505. (org-set-tags nil t))
  11506. (delete-horizontal-space))
  11507. (run-hooks 'org-after-tags-change-hook))
  11508. res))
  11509. (defun org-align-tags-here (to-col)
  11510. ;; Assumes that this is a headline
  11511. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11512. (beginning-of-line 1)
  11513. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11514. (< pos (match-beginning 2)))
  11515. (progn
  11516. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11517. (goto-char (match-beginning 1))
  11518. (insert " ")
  11519. (delete-region (point) (1+ (match-beginning 2)))
  11520. (setq ncol (max (1+ (current-column))
  11521. (1+ col)
  11522. (if (> to-col 0)
  11523. to-col
  11524. (- (abs to-col) tags-l))))
  11525. (setq p (point))
  11526. (insert (make-string (- ncol (current-column)) ?\ ))
  11527. (setq ncol (current-column))
  11528. (when indent-tabs-mode (tabify p (point-at-eol)))
  11529. (org-move-to-column (min ncol col) t))
  11530. (goto-char pos))))
  11531. (defun org-set-tags-command (&optional arg just-align)
  11532. "Call the set-tags command for the current entry."
  11533. (interactive "P")
  11534. (if (org-on-heading-p)
  11535. (org-set-tags arg just-align)
  11536. (save-excursion
  11537. (org-back-to-heading t)
  11538. (org-set-tags arg just-align))))
  11539. (defun org-set-tags-to (data)
  11540. "Set the tags of the current entry to DATA, replacing the current tags.
  11541. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11542. If DATA is nil or the empty string, any tags will be removed."
  11543. (interactive "sTags: ")
  11544. (setq data
  11545. (cond
  11546. ((eq data nil) "")
  11547. ((equal data "") "")
  11548. ((stringp data)
  11549. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11550. ":"))
  11551. ((listp data)
  11552. (concat ":" (mapconcat 'identity data ":") ":"))
  11553. (t nil)))
  11554. (when data
  11555. (save-excursion
  11556. (org-back-to-heading t)
  11557. (when (looking-at org-complex-heading-regexp)
  11558. (if (match-end 5)
  11559. (progn
  11560. (goto-char (match-beginning 5))
  11561. (insert data)
  11562. (delete-region (point) (point-at-eol))
  11563. (org-set-tags nil 'align))
  11564. (goto-char (point-at-eol))
  11565. (insert " " data)
  11566. (org-set-tags nil 'align)))
  11567. (beginning-of-line 1)
  11568. (if (looking-at ".*?\\([ \t]+\\)$")
  11569. (delete-region (match-beginning 1) (match-end 1))))))
  11570. (defun org-align-all-tags ()
  11571. "Align the tags i all headings."
  11572. (interactive)
  11573. (save-excursion
  11574. (or (ignore-errors (org-back-to-heading t))
  11575. (outline-next-heading))
  11576. (if (org-on-heading-p)
  11577. (org-set-tags t)
  11578. (message "No headings"))))
  11579. (defun org-set-tags (&optional arg just-align)
  11580. "Set the tags for the current headline.
  11581. With prefix ARG, realign all tags in headings in the current buffer."
  11582. (interactive "P")
  11583. (let* ((re (concat "^" outline-regexp))
  11584. (current (org-get-tags-string))
  11585. (col (current-column))
  11586. (org-setting-tags t)
  11587. table current-tags inherited-tags ; computed below when needed
  11588. tags p0 c0 c1 rpl)
  11589. (if arg
  11590. (save-excursion
  11591. (goto-char (point-min))
  11592. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11593. (while (re-search-forward re nil t)
  11594. (org-set-tags nil t)
  11595. (end-of-line 1)))
  11596. (message "All tags realigned to column %d" org-tags-column))
  11597. (if just-align
  11598. (setq tags current)
  11599. ;; Get a new set of tags from the user
  11600. (save-excursion
  11601. (setq table (append org-tag-persistent-alist
  11602. (or org-tag-alist (org-get-buffer-tags))
  11603. (and org-complete-tags-always-offer-all-agenda-tags
  11604. (org-global-tags-completion-table (org-agenda-files))))
  11605. org-last-tags-completion-table table
  11606. current-tags (org-split-string current ":")
  11607. inherited-tags (nreverse
  11608. (nthcdr (length current-tags)
  11609. (nreverse (org-get-tags-at))))
  11610. tags
  11611. (if (or (eq t org-use-fast-tag-selection)
  11612. (and org-use-fast-tag-selection
  11613. (delq nil (mapcar 'cdr table))))
  11614. (org-fast-tag-selection
  11615. current-tags inherited-tags table
  11616. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11617. (let ((org-add-colon-after-tag-completion t))
  11618. (org-trim
  11619. (org-without-partial-completion
  11620. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11621. nil nil current 'org-tags-history)))))))
  11622. (while (string-match "[-+&]+" tags)
  11623. ;; No boolean logic, just a list
  11624. (setq tags (replace-match ":" t t tags))))
  11625. (if org-tags-sort-function
  11626. (setq tags (mapconcat 'identity
  11627. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11628. org-tags-sort-function) ":")))
  11629. (if (string-match "\\`[\t ]*\\'" tags)
  11630. (setq tags "")
  11631. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11632. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11633. ;; Insert new tags at the correct column
  11634. (beginning-of-line 1)
  11635. (cond
  11636. ((and (equal current "") (equal tags "")))
  11637. ((re-search-forward
  11638. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11639. (point-at-eol) t)
  11640. (if (equal tags "")
  11641. (setq rpl "")
  11642. (goto-char (match-beginning 0))
  11643. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11644. (1+ (point)) (point))
  11645. c1 (max (1+ c0) (if (> org-tags-column 0)
  11646. org-tags-column
  11647. (- (- org-tags-column) (length tags))))
  11648. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11649. (replace-match rpl t t)
  11650. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11651. tags)
  11652. (t (error "Tags alignment failed")))
  11653. (org-move-to-column col)
  11654. (unless just-align
  11655. (run-hooks 'org-after-tags-change-hook)))))
  11656. (defun org-change-tag-in-region (beg end tag off)
  11657. "Add or remove TAG for each entry in the region.
  11658. This works in the agenda, and also in an org-mode buffer."
  11659. (interactive
  11660. (list (region-beginning) (region-end)
  11661. (let ((org-last-tags-completion-table
  11662. (if (org-mode-p)
  11663. (org-get-buffer-tags)
  11664. (org-global-tags-completion-table))))
  11665. (org-icompleting-read
  11666. "Tag: " 'org-tags-completion-function nil nil nil
  11667. 'org-tags-history))
  11668. (progn
  11669. (message "[s]et or [r]emove? ")
  11670. (equal (read-char-exclusive) ?r))))
  11671. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11672. (let ((agendap (equal major-mode 'org-agenda-mode))
  11673. l1 l2 m buf pos newhead (cnt 0))
  11674. (goto-char end)
  11675. (setq l2 (1- (org-current-line)))
  11676. (goto-char beg)
  11677. (setq l1 (org-current-line))
  11678. (loop for l from l1 to l2 do
  11679. (org-goto-line l)
  11680. (setq m (get-text-property (point) 'org-hd-marker))
  11681. (when (or (and (org-mode-p) (org-on-heading-p))
  11682. (and agendap m))
  11683. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11684. pos (if agendap m (point)))
  11685. (with-current-buffer buf
  11686. (save-excursion
  11687. (save-restriction
  11688. (goto-char pos)
  11689. (setq cnt (1+ cnt))
  11690. (org-toggle-tag tag (if off 'off 'on))
  11691. (setq newhead (org-get-heading)))))
  11692. (and agendap (org-agenda-change-all-lines newhead m))))
  11693. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11694. (defun org-tags-completion-function (string predicate &optional flag)
  11695. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11696. (confirm (lambda (x) (stringp (car x)))))
  11697. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11698. (setq s1 (match-string 1 string)
  11699. s2 (match-string 2 string))
  11700. (setq s1 "" s2 string))
  11701. (cond
  11702. ((eq flag nil)
  11703. ;; try completion
  11704. (setq rtn (try-completion s2 ctable confirm))
  11705. (if (stringp rtn)
  11706. (setq rtn
  11707. (concat s1 s2 (substring rtn (length s2))
  11708. (if (and org-add-colon-after-tag-completion
  11709. (assoc rtn ctable))
  11710. ":" ""))))
  11711. rtn)
  11712. ((eq flag t)
  11713. ;; all-completions
  11714. (all-completions s2 ctable confirm)
  11715. )
  11716. ((eq flag 'lambda)
  11717. ;; exact match?
  11718. (assoc s2 ctable)))
  11719. ))
  11720. (defun org-fast-tag-insert (kwd tags face &optional end)
  11721. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11722. (insert (format "%-12s" (concat kwd ":"))
  11723. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11724. (or end "")))
  11725. (defun org-fast-tag-show-exit (flag)
  11726. (save-excursion
  11727. (org-goto-line 3)
  11728. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11729. (replace-match ""))
  11730. (when flag
  11731. (end-of-line 1)
  11732. (org-move-to-column (- (window-width) 19) t)
  11733. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11734. (defun org-set-current-tags-overlay (current prefix)
  11735. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11736. (if (featurep 'xemacs)
  11737. (org-overlay-display org-tags-overlay (concat prefix s)
  11738. 'secondary-selection)
  11739. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11740. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11741. (defvar org-last-tag-selection-key nil)
  11742. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11743. "Fast tag selection with single keys.
  11744. CURRENT is the current list of tags in the headline, INHERITED is the
  11745. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11746. possibly with grouping information. TODO-TABLE is a similar table with
  11747. TODO keywords, should these have keys assigned to them.
  11748. If the keys are nil, a-z are automatically assigned.
  11749. Returns the new tags string, or nil to not change the current settings."
  11750. (let* ((fulltable (append table todo-table))
  11751. (maxlen (apply 'max (mapcar
  11752. (lambda (x)
  11753. (if (stringp (car x)) (string-width (car x)) 0))
  11754. fulltable)))
  11755. (buf (current-buffer))
  11756. (expert (eq org-fast-tag-selection-single-key 'expert))
  11757. (buffer-tags nil)
  11758. (fwidth (+ maxlen 3 1 3))
  11759. (ncol (/ (- (window-width) 4) fwidth))
  11760. (i-face 'org-done)
  11761. (c-face 'org-todo)
  11762. tg cnt e c char c1 c2 ntable tbl rtn
  11763. ov-start ov-end ov-prefix
  11764. (exit-after-next org-fast-tag-selection-single-key)
  11765. (done-keywords org-done-keywords)
  11766. groups ingroup)
  11767. (save-excursion
  11768. (beginning-of-line 1)
  11769. (if (looking-at
  11770. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11771. (setq ov-start (match-beginning 1)
  11772. ov-end (match-end 1)
  11773. ov-prefix "")
  11774. (setq ov-start (1- (point-at-eol))
  11775. ov-end (1+ ov-start))
  11776. (skip-chars-forward "^\n\r")
  11777. (setq ov-prefix
  11778. (concat
  11779. (buffer-substring (1- (point)) (point))
  11780. (if (> (current-column) org-tags-column)
  11781. " "
  11782. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11783. (move-overlay org-tags-overlay ov-start ov-end)
  11784. (save-window-excursion
  11785. (if expert
  11786. (set-buffer (get-buffer-create " *Org tags*"))
  11787. (delete-other-windows)
  11788. (split-window-vertically)
  11789. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11790. (erase-buffer)
  11791. (org-set-local 'org-done-keywords done-keywords)
  11792. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11793. (org-fast-tag-insert "Current" current c-face "\n\n")
  11794. (org-fast-tag-show-exit exit-after-next)
  11795. (org-set-current-tags-overlay current ov-prefix)
  11796. (setq tbl fulltable char ?a cnt 0)
  11797. (while (setq e (pop tbl))
  11798. (cond
  11799. ((equal (car e) :startgroup)
  11800. (push '() groups) (setq ingroup t)
  11801. (when (not (= cnt 0))
  11802. (setq cnt 0)
  11803. (insert "\n"))
  11804. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11805. ((equal (car e) :endgroup)
  11806. (setq ingroup nil cnt 0)
  11807. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11808. ((equal e '(:newline))
  11809. (when (not (= cnt 0))
  11810. (setq cnt 0)
  11811. (insert "\n")
  11812. (setq e (car tbl))
  11813. (while (equal (car tbl) '(:newline))
  11814. (insert "\n")
  11815. (setq tbl (cdr tbl)))))
  11816. (t
  11817. (setq tg (copy-sequence (car e)) c2 nil)
  11818. (if (cdr e)
  11819. (setq c (cdr e))
  11820. ;; automatically assign a character.
  11821. (setq c1 (string-to-char
  11822. (downcase (substring
  11823. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11824. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11825. (while (or (rassoc char ntable) (rassoc char table))
  11826. (setq char (1+ char)))
  11827. (setq c2 c1))
  11828. (setq c (or c2 char)))
  11829. (if ingroup (push tg (car groups)))
  11830. (setq tg (org-add-props tg nil 'face
  11831. (cond
  11832. ((not (assoc tg table))
  11833. (org-get-todo-face tg))
  11834. ((member tg current) c-face)
  11835. ((member tg inherited) i-face)
  11836. (t nil))))
  11837. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11838. (insert "[" c "] " tg (make-string
  11839. (- fwidth 4 (length tg)) ?\ ))
  11840. (push (cons tg c) ntable)
  11841. (when (= (setq cnt (1+ cnt)) ncol)
  11842. (insert "\n")
  11843. (if ingroup (insert " "))
  11844. (setq cnt 0)))))
  11845. (setq ntable (nreverse ntable))
  11846. (insert "\n")
  11847. (goto-char (point-min))
  11848. (if (not expert) (org-fit-window-to-buffer))
  11849. (setq rtn
  11850. (catch 'exit
  11851. (while t
  11852. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11853. (if (not groups) "no " "")
  11854. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11855. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11856. (setq org-last-tag-selection-key c)
  11857. (cond
  11858. ((= c ?\r) (throw 'exit t))
  11859. ((= c ?!)
  11860. (setq groups (not groups))
  11861. (goto-char (point-min))
  11862. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11863. ((= c ?\C-c)
  11864. (if (not expert)
  11865. (org-fast-tag-show-exit
  11866. (setq exit-after-next (not exit-after-next)))
  11867. (setq expert nil)
  11868. (delete-other-windows)
  11869. (split-window-vertically)
  11870. (org-switch-to-buffer-other-window " *Org tags*")
  11871. (org-fit-window-to-buffer)))
  11872. ((or (= c ?\C-g)
  11873. (and (= c ?q) (not (rassoc c ntable))))
  11874. (org-detach-overlay org-tags-overlay)
  11875. (setq quit-flag t))
  11876. ((= c ?\ )
  11877. (setq current nil)
  11878. (if exit-after-next (setq exit-after-next 'now)))
  11879. ((= c ?\t)
  11880. (condition-case nil
  11881. (setq tg (org-icompleting-read
  11882. "Tag: "
  11883. (or buffer-tags
  11884. (with-current-buffer buf
  11885. (org-get-buffer-tags)))))
  11886. (quit (setq tg "")))
  11887. (when (string-match "\\S-" tg)
  11888. (add-to-list 'buffer-tags (list tg))
  11889. (if (member tg current)
  11890. (setq current (delete tg current))
  11891. (push tg current)))
  11892. (if exit-after-next (setq exit-after-next 'now)))
  11893. ((setq e (rassoc c todo-table) tg (car e))
  11894. (with-current-buffer buf
  11895. (save-excursion (org-todo tg)))
  11896. (if exit-after-next (setq exit-after-next 'now)))
  11897. ((setq e (rassoc c ntable) tg (car e))
  11898. (if (member tg current)
  11899. (setq current (delete tg current))
  11900. (loop for g in groups do
  11901. (if (member tg g)
  11902. (mapc (lambda (x)
  11903. (setq current (delete x current)))
  11904. g)))
  11905. (push tg current))
  11906. (if exit-after-next (setq exit-after-next 'now))))
  11907. ;; Create a sorted list
  11908. (setq current
  11909. (sort current
  11910. (lambda (a b)
  11911. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11912. (if (eq exit-after-next 'now) (throw 'exit t))
  11913. (goto-char (point-min))
  11914. (beginning-of-line 2)
  11915. (delete-region (point) (point-at-eol))
  11916. (org-fast-tag-insert "Current" current c-face)
  11917. (org-set-current-tags-overlay current ov-prefix)
  11918. (while (re-search-forward
  11919. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11920. (setq tg (match-string 1))
  11921. (add-text-properties
  11922. (match-beginning 1) (match-end 1)
  11923. (list 'face
  11924. (cond
  11925. ((member tg current) c-face)
  11926. ((member tg inherited) i-face)
  11927. (t (get-text-property (match-beginning 1) 'face))))))
  11928. (goto-char (point-min)))))
  11929. (org-detach-overlay org-tags-overlay)
  11930. (if rtn
  11931. (mapconcat 'identity current ":")
  11932. nil))))
  11933. (defun org-get-tags-string ()
  11934. "Get the TAGS string in the current headline."
  11935. (unless (org-on-heading-p t)
  11936. (error "Not on a heading"))
  11937. (save-excursion
  11938. (beginning-of-line 1)
  11939. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11940. (org-match-string-no-properties 1)
  11941. "")))
  11942. (defun org-get-tags ()
  11943. "Get the list of tags specified in the current headline."
  11944. (org-split-string (org-get-tags-string) ":"))
  11945. (defun org-get-buffer-tags ()
  11946. "Get a table of all tags used in the buffer, for completion."
  11947. (let (tags)
  11948. (save-excursion
  11949. (goto-char (point-min))
  11950. (while (re-search-forward
  11951. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11952. (when (equal (char-after (point-at-bol 0)) ?*)
  11953. (mapc (lambda (x) (add-to-list 'tags x))
  11954. (org-split-string (org-match-string-no-properties 1) ":")))))
  11955. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11956. (mapcar 'list tags)))
  11957. ;;;; The mapping API
  11958. ;;;###autoload
  11959. (defun org-map-entries (func &optional match scope &rest skip)
  11960. "Call FUNC at each headline selected by MATCH in SCOPE.
  11961. FUNC is a function or a lisp form. The function will be called without
  11962. arguments, with the cursor positioned at the beginning of the headline.
  11963. The return values of all calls to the function will be collected and
  11964. returned as a list.
  11965. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11966. does not need to preserve point. After evaluation, the cursor will be
  11967. moved to the end of the line (presumably of the headline of the
  11968. processed entry) and search continues from there. Under some
  11969. circumstances, this may not produce the wanted results. For example,
  11970. if you have removed (e.g. archived) the current (sub)tree it could
  11971. mean that the next entry will be skipped entirely. In such cases, you
  11972. can specify the position from where search should continue by making
  11973. FUNC set the variable `org-map-continue-from' to the desired buffer
  11974. position.
  11975. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11976. Only headlines that are matched by this query will be considered during
  11977. the iteration. When MATCH is nil or t, all headlines will be
  11978. visited by the iteration.
  11979. SCOPE determines the scope of this command. It can be any of:
  11980. nil The current buffer, respecting the restriction if any
  11981. tree The subtree started with the entry at point
  11982. file The current buffer, without restriction
  11983. file-with-archives
  11984. The current buffer, and any archives associated with it
  11985. agenda All agenda files
  11986. agenda-with-archives
  11987. All agenda files with any archive files associated with them
  11988. \(file1 file2 ...)
  11989. If this is a list, all files in the list will be scanned
  11990. The remaining args are treated as settings for the skipping facilities of
  11991. the scanner. The following items can be given here:
  11992. archive skip trees with the archive tag.
  11993. comment skip trees with the COMMENT keyword
  11994. function or Emacs Lisp form:
  11995. will be used as value for `org-agenda-skip-function', so whenever
  11996. the function returns t, FUNC will not be called for that
  11997. entry and search will continue from the point where the
  11998. function leaves it.
  11999. If your function needs to retrieve the tags including inherited tags
  12000. at the *current* entry, you can use the value of the variable
  12001. `org-scanner-tags' which will be much faster than getting the value
  12002. with `org-get-tags-at'. If your function gets properties with
  12003. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12004. to t around the call to `org-entry-properties' to get the same speedup.
  12005. Note that if your function moves around to retrieve tags and properties at
  12006. a *different* entry, you cannot use these techniques."
  12007. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12008. (org-agenda-skip-archived-trees (memq 'archive skip))
  12009. (org-agenda-skip-comment-trees (memq 'comment skip))
  12010. (org-agenda-skip-function
  12011. (car (org-delete-all '(comment archive) skip)))
  12012. (org-tags-match-list-sublevels t)
  12013. matcher file res
  12014. org-todo-keywords-for-agenda
  12015. org-done-keywords-for-agenda
  12016. org-todo-keyword-alist-for-agenda
  12017. org-drawers-for-agenda
  12018. org-tag-alist-for-agenda)
  12019. (cond
  12020. ((eq match t) (setq matcher t))
  12021. ((eq match nil) (setq matcher t))
  12022. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12023. (save-excursion
  12024. (save-restriction
  12025. (when (eq scope 'tree)
  12026. (org-back-to-heading t)
  12027. (org-narrow-to-subtree)
  12028. (setq scope nil))
  12029. (if (not scope)
  12030. (progn
  12031. (org-prepare-agenda-buffers
  12032. (list (buffer-file-name (current-buffer))))
  12033. (setq res (org-scan-tags func matcher)))
  12034. ;; Get the right scope
  12035. (cond
  12036. ((and scope (listp scope) (symbolp (car scope)))
  12037. (setq scope (eval scope)))
  12038. ((eq scope 'agenda)
  12039. (setq scope (org-agenda-files t)))
  12040. ((eq scope 'agenda-with-archives)
  12041. (setq scope (org-agenda-files t))
  12042. (setq scope (org-add-archive-files scope)))
  12043. ((eq scope 'file)
  12044. (setq scope (list (buffer-file-name))))
  12045. ((eq scope 'file-with-archives)
  12046. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12047. (org-prepare-agenda-buffers scope)
  12048. (while (setq file (pop scope))
  12049. (with-current-buffer (org-find-base-buffer-visiting file)
  12050. (save-excursion
  12051. (save-restriction
  12052. (widen)
  12053. (goto-char (point-min))
  12054. (setq res (append res (org-scan-tags func matcher))))))))))
  12055. res))
  12056. ;;;; Properties
  12057. ;;; Setting and retrieving properties
  12058. (defconst org-special-properties
  12059. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12060. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  12061. "The special properties valid in Org-mode.
  12062. These are properties that are not defined in the property drawer,
  12063. but in some other way.")
  12064. (defconst org-default-properties
  12065. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12066. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12067. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12068. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12069. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12070. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12071. "Some properties that are used by Org-mode for various purposes.
  12072. Being in this list makes sure that they are offered for completion.")
  12073. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12074. "Regular expression matching the first line of a property drawer.")
  12075. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12076. "Regular expression matching the last line of a property drawer.")
  12077. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12078. "Regular expression matching the first line of a property drawer.")
  12079. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12080. "Regular expression matching the first line of a property drawer.")
  12081. (defconst org-property-drawer-re
  12082. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12083. org-property-end-re "\\)\n?")
  12084. "Matches an entire property drawer.")
  12085. (defconst org-clock-drawer-re
  12086. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12087. org-property-end-re "\\)\n?")
  12088. "Matches an entire clock drawer.")
  12089. (defun org-property-action ()
  12090. "Do an action on properties."
  12091. (interactive)
  12092. (let (c)
  12093. (org-at-property-p)
  12094. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12095. (setq c (read-char-exclusive))
  12096. (cond
  12097. ((equal c ?s)
  12098. (call-interactively 'org-set-property))
  12099. ((equal c ?d)
  12100. (call-interactively 'org-delete-property))
  12101. ((equal c ?D)
  12102. (call-interactively 'org-delete-property-globally))
  12103. ((equal c ?c)
  12104. (call-interactively 'org-compute-property-at-point))
  12105. (t (error "No such property action %c" c)))))
  12106. (defun org-set-effort (&optional value)
  12107. "Set the effort property of the current entry.
  12108. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12109. allowed value."
  12110. (interactive "P")
  12111. (if (equal value 0) (setq value 10))
  12112. (let* ((completion-ignore-case t)
  12113. (prop org-effort-property)
  12114. (cur (org-entry-get nil prop))
  12115. (allowed (org-property-get-allowed-values nil prop 'table))
  12116. (existing (mapcar 'list (org-property-values prop)))
  12117. rpl
  12118. (val (cond
  12119. ((stringp value) value)
  12120. ((and allowed (integerp value))
  12121. (or (car (nth (1- value) allowed))
  12122. (car (org-last allowed))))
  12123. (allowed
  12124. (message "Select 1-9,0, [RET%s]: %s"
  12125. (if cur (concat "=" cur) "")
  12126. (mapconcat 'car allowed " "))
  12127. (setq rpl (read-char-exclusive))
  12128. (if (equal rpl ?\r)
  12129. cur
  12130. (setq rpl (- rpl ?0))
  12131. (if (equal rpl 0) (setq rpl 10))
  12132. (if (and (> rpl 0) (<= rpl (length allowed)))
  12133. (car (nth (1- rpl) allowed))
  12134. (org-completing-read "Effort: " allowed nil))))
  12135. (t
  12136. (let (org-completion-use-ido org-completion-use-iswitchb)
  12137. (org-completing-read
  12138. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12139. (concat "[" cur "]") "")
  12140. ": ")
  12141. existing nil nil "" nil cur))))))
  12142. (unless (equal (org-entry-get nil prop) val)
  12143. (org-entry-put nil prop val))
  12144. (message "%s is now %s" prop val)))
  12145. (defun org-at-property-p ()
  12146. "Is cursor inside a property drawer?"
  12147. (save-excursion
  12148. (beginning-of-line 1)
  12149. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12150. (save-match-data ;; Used by calling procedures
  12151. (let ((p (point))
  12152. (range (unless (org-before-first-heading-p)
  12153. (org-get-property-block))))
  12154. (and range (<= (car range) p) (< p (cdr range))))))))
  12155. (defun org-get-property-block (&optional beg end force)
  12156. "Return the (beg . end) range of the body of the property drawer.
  12157. BEG and END can be beginning and end of subtree, if not given
  12158. they will be found.
  12159. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12160. (catch 'exit
  12161. (save-excursion
  12162. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12163. (end (or end (progn (outline-next-heading) (point)))))
  12164. (goto-char beg)
  12165. (if (re-search-forward org-property-start-re end t)
  12166. (setq beg (1+ (match-end 0)))
  12167. (if force
  12168. (save-excursion
  12169. (org-insert-property-drawer)
  12170. (setq end (progn (outline-next-heading) (point))))
  12171. (throw 'exit nil))
  12172. (goto-char beg)
  12173. (if (re-search-forward org-property-start-re end t)
  12174. (setq beg (1+ (match-end 0)))))
  12175. (if (re-search-forward org-property-end-re end t)
  12176. (setq end (match-beginning 0))
  12177. (or force (throw 'exit nil))
  12178. (goto-char beg)
  12179. (setq end beg)
  12180. (org-indent-line-function)
  12181. (insert ":END:\n"))
  12182. (cons beg end)))))
  12183. (defun org-entry-properties (&optional pom which specific)
  12184. "Get all properties of the entry at point-or-marker POM.
  12185. This includes the TODO keyword, the tags, time strings for deadline,
  12186. scheduled, and clocking, and any additional properties defined in the
  12187. entry. The return value is an alist, keys may occur multiple times
  12188. if the property key was used several times.
  12189. POM may also be nil, in which case the current entry is used.
  12190. If WHICH is nil or `all', get all properties. If WHICH is
  12191. `special' or `standard', only get that subclass. If WHICH
  12192. is a string only get exactly this property. Specific can be a string, the
  12193. specific property we are interested in. Specifying it can speed
  12194. things up because then unnecessary parsing is avoided."
  12195. (setq which (or which 'all))
  12196. (org-with-point-at pom
  12197. (let ((clockstr (substring org-clock-string 0 -1))
  12198. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12199. (case-fold-search nil)
  12200. beg end range props sum-props key key1 value string clocksum)
  12201. (save-excursion
  12202. (when (condition-case nil
  12203. (and (org-mode-p) (org-back-to-heading t))
  12204. (error nil))
  12205. (setq beg (point))
  12206. (setq sum-props (get-text-property (point) 'org-summaries))
  12207. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12208. (outline-next-heading)
  12209. (setq end (point))
  12210. (when (memq which '(all special))
  12211. ;; Get the special properties, like TODO and tags
  12212. (goto-char beg)
  12213. (when (and (or (not specific) (string= specific "TODO"))
  12214. (looking-at org-todo-line-regexp) (match-end 2))
  12215. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12216. (when (and (or (not specific) (string= specific "PRIORITY"))
  12217. (looking-at org-priority-regexp))
  12218. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12219. (when (and (or (not specific) (string= specific "TAGS"))
  12220. (setq value (org-get-tags-string))
  12221. (string-match "\\S-" value))
  12222. (push (cons "TAGS" value) props))
  12223. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12224. (setq value (org-get-tags-at)))
  12225. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12226. ":"))
  12227. props))
  12228. (when (or (not specific) (string= specific "BLOCKED"))
  12229. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12230. (when (or (not specific)
  12231. (member specific
  12232. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12233. "TIMESTAMP" "TIMESTAMP_IA")))
  12234. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12235. (setq key (if (match-end 1)
  12236. (substring (org-match-string-no-properties 1)
  12237. 0 -1))
  12238. string (if (equal key clockstr)
  12239. (org-no-properties
  12240. (org-trim
  12241. (buffer-substring
  12242. (match-beginning 3) (goto-char
  12243. (point-at-eol)))))
  12244. (substring (org-match-string-no-properties 3)
  12245. 1 -1)))
  12246. ;; Get the correct property name from the key. This is
  12247. ;; necessary if the user has configured time keywords.
  12248. (setq key1 (concat key ":"))
  12249. (cond
  12250. ((not key)
  12251. (setq key
  12252. (if (= (char-after (match-beginning 3)) ?\[)
  12253. "TIMESTAMP_IA" "TIMESTAMP")))
  12254. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12255. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12256. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12257. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12258. (when (or (equal key "CLOCK") (not (assoc key props)))
  12259. (push (cons key string) props))))
  12260. )
  12261. (when (memq which '(all standard))
  12262. ;; Get the standard properties, like :PROP: ...
  12263. (setq range (org-get-property-block beg end))
  12264. (when range
  12265. (goto-char (car range))
  12266. (while (re-search-forward
  12267. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12268. (cdr range) t)
  12269. (setq key (org-match-string-no-properties 1)
  12270. value (org-trim (or (org-match-string-no-properties 2) "")))
  12271. (unless (member key excluded)
  12272. (push (cons key (or value "")) props)))))
  12273. (if clocksum
  12274. (push (cons "CLOCKSUM"
  12275. (org-columns-number-to-string (/ (float clocksum) 60.)
  12276. 'add_times))
  12277. props))
  12278. (unless (assoc "CATEGORY" props)
  12279. (setq value (or (org-get-category)
  12280. (progn (org-refresh-category-properties)
  12281. (org-get-category))))
  12282. (push (cons "CATEGORY" value) props))
  12283. (append sum-props (nreverse props)))))))
  12284. (defun org-entry-get (pom property &optional inherit literal-nil)
  12285. "Get value of PROPERTY for entry at point-or-marker POM.
  12286. If INHERIT is non-nil and the entry does not have the property,
  12287. then also check higher levels of the hierarchy.
  12288. If INHERIT is the symbol `selective', use inheritance only if the setting
  12289. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12290. If the property is present but empty, the return value is the empty string.
  12291. If the property is not present at all, nil is returned.
  12292. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12293. do not interpret it as the list atom nil. This is used for inheritance
  12294. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12295. (org-with-point-at pom
  12296. (if (and inherit (if (eq inherit 'selective)
  12297. (org-property-inherit-p property)
  12298. t))
  12299. (org-entry-get-with-inheritance property literal-nil)
  12300. (if (member property org-special-properties)
  12301. ;; We need a special property. Use `org-entry-properties' to
  12302. ;; retrieve it, but specify the wanted property
  12303. (cdr (assoc property (org-entry-properties nil 'special property)))
  12304. (let ((range (org-get-property-block)))
  12305. (if (and range
  12306. (goto-char (car range))
  12307. (re-search-forward
  12308. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  12309. (cdr range) t))
  12310. ;; Found the property, return it.
  12311. (if (match-end 1)
  12312. (if literal-nil
  12313. (org-match-string-no-properties 1)
  12314. (org-not-nil (org-match-string-no-properties 1)))
  12315. "")))))))
  12316. (defun org-property-or-variable-value (var &optional inherit)
  12317. "Check if there is a property fixing the value of VAR.
  12318. If yes, return this value. If not, return the current value of the variable."
  12319. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12320. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12321. (read prop)
  12322. (symbol-value var))))
  12323. (defun org-entry-delete (pom property)
  12324. "Delete the property PROPERTY from entry at point-or-marker POM."
  12325. (org-with-point-at pom
  12326. (if (member property org-special-properties)
  12327. nil ; cannot delete these properties.
  12328. (let ((range (org-get-property-block)))
  12329. (if (and range
  12330. (goto-char (car range))
  12331. (re-search-forward
  12332. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  12333. (cdr range) t))
  12334. (progn
  12335. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12336. t)
  12337. nil)))))
  12338. ;; Multi-values properties are properties that contain multiple values
  12339. ;; These values are assumed to be single words, separated by whitespace.
  12340. (defun org-entry-add-to-multivalued-property (pom property value)
  12341. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12342. (let* ((old (org-entry-get pom property))
  12343. (values (and old (org-split-string old "[ \t]"))))
  12344. (setq value (org-entry-protect-space value))
  12345. (unless (member value values)
  12346. (setq values (cons value values))
  12347. (org-entry-put pom property
  12348. (mapconcat 'identity values " ")))))
  12349. (defun org-entry-remove-from-multivalued-property (pom property value)
  12350. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12351. (let* ((old (org-entry-get pom property))
  12352. (values (and old (org-split-string old "[ \t]"))))
  12353. (setq value (org-entry-protect-space value))
  12354. (when (member value values)
  12355. (setq values (delete value values))
  12356. (org-entry-put pom property
  12357. (mapconcat 'identity values " ")))))
  12358. (defun org-entry-member-in-multivalued-property (pom property value)
  12359. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12360. (let* ((old (org-entry-get pom property))
  12361. (values (and old (org-split-string old "[ \t]"))))
  12362. (setq value (org-entry-protect-space value))
  12363. (member value values)))
  12364. (defun org-entry-get-multivalued-property (pom property)
  12365. "Return a list of values in a multivalued property."
  12366. (let* ((value (org-entry-get pom property))
  12367. (values (and value (org-split-string value "[ \t]"))))
  12368. (mapcar 'org-entry-restore-space values)))
  12369. (defun org-entry-put-multivalued-property (pom property &rest values)
  12370. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12371. VALUES should be a list of strings. Spaces will be protected."
  12372. (org-entry-put pom property
  12373. (mapconcat 'org-entry-protect-space values " "))
  12374. (let* ((value (org-entry-get pom property))
  12375. (values (and value (org-split-string value "[ \t]"))))
  12376. (mapcar 'org-entry-restore-space values)))
  12377. (defun org-entry-protect-space (s)
  12378. "Protect spaces and newline in string S."
  12379. (while (string-match " " s)
  12380. (setq s (replace-match "%20" t t s)))
  12381. (while (string-match "\n" s)
  12382. (setq s (replace-match "%0A" t t s)))
  12383. s)
  12384. (defun org-entry-restore-space (s)
  12385. "Restore spaces and newline in string S."
  12386. (while (string-match "%20" s)
  12387. (setq s (replace-match " " t t s)))
  12388. (while (string-match "%0A" s)
  12389. (setq s (replace-match "\n" t t s)))
  12390. s)
  12391. (defvar org-entry-property-inherited-from (make-marker)
  12392. "Marker pointing to the entry from where a property was inherited.
  12393. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12394. location of the entry where the inheritance search matched. If there was
  12395. no match, the marker will point nowhere.
  12396. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12397. is set.")
  12398. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12399. "Get entry property, and search higher levels if not present.
  12400. The search will stop at the first ancestor which has the property defined.
  12401. If the value found is \"nil\", return nil to show that the property
  12402. should be considered as undefined (this is the meaning of nil here).
  12403. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12404. (move-marker org-entry-property-inherited-from nil)
  12405. (let (tmp)
  12406. (save-excursion
  12407. (save-restriction
  12408. (widen)
  12409. (catch 'ex
  12410. (while t
  12411. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12412. (org-back-to-heading t)
  12413. (move-marker org-entry-property-inherited-from (point))
  12414. (throw 'ex tmp))
  12415. (or (org-up-heading-safe) (throw 'ex nil)))))
  12416. (setq tmp (or tmp
  12417. (cdr (assoc property org-file-properties))
  12418. (cdr (assoc property org-global-properties))
  12419. (cdr (assoc property org-global-properties-fixed))))
  12420. (if literal-nil tmp (org-not-nil tmp)))))
  12421. (defvar org-property-changed-functions nil
  12422. "Hook called when the value of a property has changed.
  12423. Each hook function should accept two arguments, the name of the property
  12424. and the new value.")
  12425. (defun org-entry-put (pom property value)
  12426. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12427. (org-with-point-at pom
  12428. (org-back-to-heading t)
  12429. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12430. range)
  12431. (cond
  12432. ((equal property "TODO")
  12433. (when (and (stringp value) (string-match "\\S-" value)
  12434. (not (member value org-todo-keywords-1)))
  12435. (error "\"%s\" is not a valid TODO state" value))
  12436. (if (or (not value)
  12437. (not (string-match "\\S-" value)))
  12438. (setq value 'none))
  12439. (org-todo value)
  12440. (org-set-tags nil 'align))
  12441. ((equal property "PRIORITY")
  12442. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12443. (string-to-char value) ?\ ))
  12444. (org-set-tags nil 'align))
  12445. ((equal property "SCHEDULED")
  12446. (if (re-search-forward org-scheduled-time-regexp end t)
  12447. (cond
  12448. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12449. ((eq value 'later) (org-timestamp-change 1 'day))
  12450. (t (call-interactively 'org-schedule)))
  12451. (call-interactively 'org-schedule)))
  12452. ((equal property "DEADLINE")
  12453. (if (re-search-forward org-deadline-time-regexp end t)
  12454. (cond
  12455. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12456. ((eq value 'later) (org-timestamp-change 1 'day))
  12457. (t (call-interactively 'org-deadline)))
  12458. (call-interactively 'org-deadline)))
  12459. ((member property org-special-properties)
  12460. (error "The %s property can not yet be set with `org-entry-put'"
  12461. property))
  12462. (t ; a non-special property
  12463. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12464. (setq range (org-get-property-block beg end 'force))
  12465. (goto-char (car range))
  12466. (if (re-search-forward
  12467. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12468. (progn
  12469. (delete-region (match-beginning 1) (match-end 1))
  12470. (goto-char (match-beginning 1)))
  12471. (goto-char (cdr range))
  12472. (insert "\n")
  12473. (backward-char 1)
  12474. (org-indent-line-function)
  12475. (insert ":" property ":"))
  12476. (and value (insert " " value))
  12477. (org-indent-line-function)))))
  12478. (run-hook-with-args 'org-property-changed-functions property value)))
  12479. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12480. "Get all property keys in the current buffer.
  12481. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12482. like tags and TODO state.
  12483. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12484. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12485. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12486. formats in the current buffer."
  12487. (let (rtn range cfmt s p)
  12488. (save-excursion
  12489. (save-restriction
  12490. (widen)
  12491. (goto-char (point-min))
  12492. (while (re-search-forward org-property-start-re nil t)
  12493. (setq range (org-get-property-block))
  12494. (goto-char (car range))
  12495. (while (re-search-forward
  12496. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12497. (cdr range) t)
  12498. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12499. (outline-next-heading))))
  12500. (when include-specials
  12501. (setq rtn (append org-special-properties rtn)))
  12502. (when include-defaults
  12503. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12504. (add-to-list 'rtn org-effort-property))
  12505. (when include-columns
  12506. (save-excursion
  12507. (save-restriction
  12508. (widen)
  12509. (goto-char (point-min))
  12510. (while (re-search-forward
  12511. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12512. nil t)
  12513. (setq cfmt (match-string 2) s 0)
  12514. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12515. cfmt s)
  12516. (setq s (match-end 0)
  12517. p (match-string 1 cfmt))
  12518. (unless (or (equal p "ITEM")
  12519. (member p org-special-properties))
  12520. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12521. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12522. (defun org-property-values (key)
  12523. "Return a list of all values of property KEY."
  12524. (save-excursion
  12525. (save-restriction
  12526. (widen)
  12527. (goto-char (point-min))
  12528. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12529. values)
  12530. (while (re-search-forward re nil t)
  12531. (add-to-list 'values (org-trim (match-string 1))))
  12532. (delete "" values)))))
  12533. (defun org-insert-property-drawer ()
  12534. "Insert a property drawer into the current entry."
  12535. (interactive)
  12536. (org-back-to-heading t)
  12537. (looking-at outline-regexp)
  12538. (let ((indent (if org-adapt-indentation
  12539. (- (match-end 0)(match-beginning 0))
  12540. 0))
  12541. (beg (point))
  12542. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12543. end hiddenp)
  12544. (outline-next-heading)
  12545. (setq end (point))
  12546. (goto-char beg)
  12547. (while (re-search-forward re end t))
  12548. (setq hiddenp (org-invisible-p))
  12549. (end-of-line 1)
  12550. (and (equal (char-after) ?\n) (forward-char 1))
  12551. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12552. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12553. ;; just skip this line
  12554. (beginning-of-line 2)
  12555. ;; Drawer start, find the end
  12556. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12557. (beginning-of-line 1)))
  12558. (org-skip-over-state-notes)
  12559. (skip-chars-backward " \t\n\r")
  12560. (if (eq (char-before) ?*) (forward-char 1))
  12561. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12562. (beginning-of-line 0)
  12563. (org-indent-to-column indent)
  12564. (beginning-of-line 2)
  12565. (org-indent-to-column indent)
  12566. (beginning-of-line 0)
  12567. (if hiddenp
  12568. (save-excursion
  12569. (org-back-to-heading t)
  12570. (hide-entry))
  12571. (org-flag-drawer t))))
  12572. (defun org-set-property (property value)
  12573. "In the current entry, set PROPERTY to VALUE.
  12574. When called interactively, this will prompt for a property name, offering
  12575. completion on existing and default properties. And then it will prompt
  12576. for a value, offering completion either on allowed values (via an inherited
  12577. xxx_ALL property) or on existing values in other instances of this property
  12578. in the current file."
  12579. (interactive
  12580. (let* ((completion-ignore-case t)
  12581. (keys (org-buffer-property-keys nil t t))
  12582. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12583. (prop (if (member prop0 keys)
  12584. prop0
  12585. (or (cdr (assoc (downcase prop0)
  12586. (mapcar (lambda (x) (cons (downcase x) x))
  12587. keys)))
  12588. prop0)))
  12589. (cur (org-entry-get nil prop))
  12590. (prompt (concat prop " value"
  12591. (if (and cur (string-match "\\S-" cur))
  12592. (concat " [" cur "]") "") ": "))
  12593. (allowed (org-property-get-allowed-values nil prop 'table))
  12594. (existing (mapcar 'list (org-property-values prop)))
  12595. (val (if allowed
  12596. (org-completing-read prompt allowed nil
  12597. (not (get-text-property 0 'org-unrestricted
  12598. (caar allowed))))
  12599. (let (org-completion-use-ido org-completion-use-iswitchb)
  12600. (org-completing-read prompt existing nil nil "" nil cur)))))
  12601. (list prop (if (equal val "") cur val))))
  12602. (unless (equal (org-entry-get nil property) value)
  12603. (org-entry-put nil property value)))
  12604. (defun org-delete-property (property)
  12605. "In the current entry, delete PROPERTY."
  12606. (interactive
  12607. (let* ((completion-ignore-case t)
  12608. (prop (org-icompleting-read "Property: "
  12609. (org-entry-properties nil 'standard))))
  12610. (list prop)))
  12611. (message "Property %s %s" property
  12612. (if (org-entry-delete nil property)
  12613. "deleted"
  12614. "was not present in the entry")))
  12615. (defun org-delete-property-globally (property)
  12616. "Remove PROPERTY globally, from all entries."
  12617. (interactive
  12618. (let* ((completion-ignore-case t)
  12619. (prop (org-icompleting-read
  12620. "Globally remove property: "
  12621. (mapcar 'list (org-buffer-property-keys)))))
  12622. (list prop)))
  12623. (save-excursion
  12624. (save-restriction
  12625. (widen)
  12626. (goto-char (point-min))
  12627. (let ((cnt 0))
  12628. (while (re-search-forward
  12629. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12630. nil t)
  12631. (setq cnt (1+ cnt))
  12632. (replace-match ""))
  12633. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12634. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12635. (defun org-compute-property-at-point ()
  12636. "Compute the property at point.
  12637. This looks for an enclosing column format, extracts the operator and
  12638. then applies it to the property in the column format's scope."
  12639. (interactive)
  12640. (unless (org-at-property-p)
  12641. (error "Not at a property"))
  12642. (let ((prop (org-match-string-no-properties 2)))
  12643. (org-columns-get-format-and-top-level)
  12644. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12645. (error "No operator defined for property %s" prop))
  12646. (org-columns-compute prop)))
  12647. (defvar org-property-allowed-value-functions nil
  12648. "Hook for functions supplying allowed values for a specific property.
  12649. The functions must take a single argument, the name of the property, and
  12650. return a flat list of allowed values. If \":ETC\" is one of
  12651. the values, this means that these values are intended as defaults for
  12652. completion, but that other values should be allowed too.
  12653. The functions must return nil if they are not responsible for this
  12654. property.")
  12655. (defun org-property-get-allowed-values (pom property &optional table)
  12656. "Get allowed values for the property PROPERTY.
  12657. When TABLE is non-nil, return an alist that can directly be used for
  12658. completion."
  12659. (let (vals)
  12660. (cond
  12661. ((equal property "TODO")
  12662. (setq vals (org-with-point-at pom
  12663. (append org-todo-keywords-1 '("")))))
  12664. ((equal property "PRIORITY")
  12665. (let ((n org-lowest-priority))
  12666. (while (>= n org-highest-priority)
  12667. (push (char-to-string n) vals)
  12668. (setq n (1- n)))))
  12669. ((member property org-special-properties))
  12670. ((setq vals (run-hook-with-args-until-success
  12671. 'org-property-allowed-value-functions property)))
  12672. (t
  12673. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12674. (when (and vals (string-match "\\S-" vals))
  12675. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12676. (setq vals (mapcar (lambda (x)
  12677. (cond ((stringp x) x)
  12678. ((numberp x) (number-to-string x))
  12679. ((symbolp x) (symbol-name x))
  12680. (t "???")))
  12681. vals)))))
  12682. (when (member ":ETC" vals)
  12683. (setq vals (remove ":ETC" vals))
  12684. (org-add-props (car vals) '(org-unrestricted t)))
  12685. (if table (mapcar 'list vals) vals)))
  12686. (defun org-property-previous-allowed-value (&optional previous)
  12687. "Switch to the next allowed value for this property."
  12688. (interactive)
  12689. (org-property-next-allowed-value t))
  12690. (defun org-property-next-allowed-value (&optional previous)
  12691. "Switch to the next allowed value for this property."
  12692. (interactive)
  12693. (unless (org-at-property-p)
  12694. (error "Not at a property"))
  12695. (let* ((key (match-string 2))
  12696. (value (match-string 3))
  12697. (allowed (or (org-property-get-allowed-values (point) key)
  12698. (and (member value '("[ ]" "[-]" "[X]"))
  12699. '("[ ]" "[X]"))))
  12700. nval)
  12701. (unless allowed
  12702. (error "Allowed values for this property have not been defined"))
  12703. (if previous (setq allowed (reverse allowed)))
  12704. (if (member value allowed)
  12705. (setq nval (car (cdr (member value allowed)))))
  12706. (setq nval (or nval (car allowed)))
  12707. (if (equal nval value)
  12708. (error "Only one allowed value for this property"))
  12709. (org-at-property-p)
  12710. (replace-match (concat " :" key ": " nval) t t)
  12711. (org-indent-line-function)
  12712. (beginning-of-line 1)
  12713. (skip-chars-forward " \t")
  12714. (run-hook-with-args 'org-property-changed-functions key nval)))
  12715. (defun org-find-olp (path &optional this-buffer)
  12716. "Return a marker pointing to the entry at outline path OLP.
  12717. If anything goes wrong, throw an error.
  12718. You can wrap this call to catch the error like this:
  12719. (condition-case msg
  12720. (org-mobile-locate-entry (match-string 4))
  12721. (error (nth 1 msg)))
  12722. The return value will then be either a string with the error message,
  12723. or a marker if everything is OK.
  12724. If THIS-BUFFER is set, the outline path does not contain a file,
  12725. only headings."
  12726. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12727. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12728. (level 1)
  12729. (lmin 1)
  12730. (lmax 1)
  12731. limit re end found pos heading cnt)
  12732. (unless buffer (error "File not found :%s" file))
  12733. (with-current-buffer buffer
  12734. (save-excursion
  12735. (save-restriction
  12736. (widen)
  12737. (setq limit (point-max))
  12738. (goto-char (point-min))
  12739. (while (setq heading (pop path))
  12740. (setq re (format org-complex-heading-regexp-format
  12741. (regexp-quote heading)))
  12742. (setq cnt 0 pos (point))
  12743. (while (re-search-forward re end t)
  12744. (setq level (- (match-end 1) (match-beginning 1)))
  12745. (if (and (>= level lmin) (<= level lmax))
  12746. (setq found (match-beginning 0) cnt (1+ cnt))))
  12747. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12748. lmax heading))
  12749. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12750. lmax heading))
  12751. (goto-char found)
  12752. (setq lmin (1+ level) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12753. (setq end (save-excursion (org-end-of-subtree t t))))
  12754. (when (org-on-heading-p)
  12755. (move-marker (make-marker) (point))))))))
  12756. (defun org-find-entry-with-id (ident)
  12757. "Locate the entry that contains the ID property with exact value IDENT.
  12758. IDENT can be a string, a symbol or a number, this function will search for
  12759. the string representation of it.
  12760. Return the position where this entry starts, or nil if there is no such entry."
  12761. (interactive "sID: ")
  12762. (let ((id (cond
  12763. ((stringp ident) ident)
  12764. ((symbol-name ident) (symbol-name ident))
  12765. ((numberp ident) (number-to-string ident))
  12766. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12767. (case-fold-search nil))
  12768. (save-excursion
  12769. (save-restriction
  12770. (widen)
  12771. (goto-char (point-min))
  12772. (when (re-search-forward
  12773. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12774. nil t)
  12775. (org-back-to-heading t)
  12776. (point))))))
  12777. ;;;; Timestamps
  12778. (defvar org-last-changed-timestamp nil)
  12779. (defvar org-last-inserted-timestamp nil
  12780. "The last time stamp inserted with `org-insert-time-stamp'.")
  12781. (defvar org-time-was-given) ; dynamically scoped parameter
  12782. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12783. (defvar org-ts-what) ; dynamically scoped parameter
  12784. (defun org-time-stamp (arg &optional inactive)
  12785. "Prompt for a date/time and insert a time stamp.
  12786. If the user specifies a time like HH:MM, or if this command is called
  12787. with a prefix argument, the time stamp will contain date and time.
  12788. Otherwise, only the date will be included. All parts of a date not
  12789. specified by the user will be filled in from the current date/time.
  12790. So if you press just return without typing anything, the time stamp
  12791. will represent the current date/time. If there is already a timestamp
  12792. at the cursor, it will be modified."
  12793. (interactive "P")
  12794. (let* ((ts nil)
  12795. (default-time
  12796. ;; Default time is either today, or, when entering a range,
  12797. ;; the range start.
  12798. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12799. (save-excursion
  12800. (re-search-backward
  12801. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12802. (- (point) 20) t)))
  12803. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12804. (current-time)))
  12805. (default-input (and ts (org-get-compact-tod ts)))
  12806. org-time-was-given org-end-time-was-given time)
  12807. (cond
  12808. ((and (org-at-timestamp-p t)
  12809. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12810. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12811. (insert "--")
  12812. (setq time (let ((this-command this-command))
  12813. (org-read-date arg 'totime nil nil
  12814. default-time default-input)))
  12815. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12816. ((org-at-timestamp-p t)
  12817. (setq time (let ((this-command this-command))
  12818. (org-read-date arg 'totime nil nil default-time default-input)))
  12819. (when (org-at-timestamp-p t) ; just to get the match data
  12820. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12821. (replace-match "")
  12822. (setq org-last-changed-timestamp
  12823. (org-insert-time-stamp
  12824. time (or org-time-was-given arg)
  12825. inactive nil nil (list org-end-time-was-given))))
  12826. (message "Timestamp updated"))
  12827. (t
  12828. (setq time (let ((this-command this-command))
  12829. (org-read-date arg 'totime nil nil default-time default-input)))
  12830. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12831. nil nil (list org-end-time-was-given))))))
  12832. ;; FIXME: can we use this for something else, like computing time differences?
  12833. (defun org-get-compact-tod (s)
  12834. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12835. (let* ((t1 (match-string 1 s))
  12836. (h1 (string-to-number (match-string 2 s)))
  12837. (m1 (string-to-number (match-string 3 s)))
  12838. (t2 (and (match-end 4) (match-string 5 s)))
  12839. (h2 (and t2 (string-to-number (match-string 6 s))))
  12840. (m2 (and t2 (string-to-number (match-string 7 s))))
  12841. dh dm)
  12842. (if (not t2)
  12843. t1
  12844. (setq dh (- h2 h1) dm (- m2 m1))
  12845. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12846. (concat t1 "+" (number-to-string dh)
  12847. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12848. (defun org-time-stamp-inactive (&optional arg)
  12849. "Insert an inactive time stamp.
  12850. An inactive time stamp is enclosed in square brackets instead of angle
  12851. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12852. does not link to the calendar and cannot be changed with the S-cursor keys.
  12853. So these are more for recording a certain time/date."
  12854. (interactive "P")
  12855. (org-time-stamp arg 'inactive))
  12856. (defvar org-date-ovl (make-overlay 1 1))
  12857. (overlay-put org-date-ovl 'face 'org-warning)
  12858. (org-detach-overlay org-date-ovl)
  12859. (defvar org-ans1) ; dynamically scoped parameter
  12860. (defvar org-ans2) ; dynamically scoped parameter
  12861. (defvar org-plain-time-of-day-regexp) ; defined below
  12862. (defvar org-overriding-default-time nil) ; dynamically scoped
  12863. (defvar org-read-date-overlay nil)
  12864. (defvar org-dcst nil) ; dynamically scoped
  12865. (defvar org-read-date-history nil)
  12866. (defvar org-read-date-final-answer nil)
  12867. (defun org-read-date (&optional with-time to-time from-string prompt
  12868. default-time default-input)
  12869. "Read a date, possibly a time, and make things smooth for the user.
  12870. The prompt will suggest to enter an ISO date, but you can also enter anything
  12871. which will at least partially be understood by `parse-time-string'.
  12872. Unrecognized parts of the date will default to the current day, month, year,
  12873. hour and minute. If this command is called to replace a timestamp at point,
  12874. of to enter the second timestamp of a range, the default time is taken
  12875. from the existing stamp. Furthermore, the command prefers the future,
  12876. so if you are giving a date where the year is not given, and the day-month
  12877. combination is already past in the current year, it will assume you
  12878. mean next year. For details, see the manual. A few examples:
  12879. 3-2-5 --> 2003-02-05
  12880. feb 15 --> currentyear-02-15
  12881. 2/15 --> currentyear-02-15
  12882. sep 12 9 --> 2009-09-12
  12883. 12:45 --> today 12:45
  12884. 22 sept 0:34 --> currentyear-09-22 0:34
  12885. 12 --> currentyear-currentmonth-12
  12886. Fri --> nearest Friday (today or later)
  12887. etc.
  12888. Furthermore you can specify a relative date by giving, as the *first* thing
  12889. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12890. change in days weeks, months, years.
  12891. With a single plus or minus, the date is relative to today. With a double
  12892. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12893. +4d --> four days from today
  12894. +4 --> same as above
  12895. +2w --> two weeks from today
  12896. ++5 --> five days from default date
  12897. The function understands only English month and weekday abbreviations,
  12898. but this can be configured with the variables `parse-time-months' and
  12899. `parse-time-weekdays'.
  12900. While prompting, a calendar is popped up - you can also select the
  12901. date with the mouse (button 1). The calendar shows a period of three
  12902. months. To scroll it to other months, use the keys `>' and `<'.
  12903. If you don't like the calendar, turn it off with
  12904. \(setq org-read-date-popup-calendar nil)
  12905. With optional argument TO-TIME, the date will immediately be converted
  12906. to an internal time.
  12907. With an optional argument WITH-TIME, the prompt will suggest to also
  12908. insert a time. Note that when WITH-TIME is not set, you can still
  12909. enter a time, and this function will inform the calling routine about
  12910. this change. The calling routine may then choose to change the format
  12911. used to insert the time stamp into the buffer to include the time.
  12912. With optional argument FROM-STRING, read from this string instead from
  12913. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12914. the time/date that is used for everything that is not specified by the
  12915. user."
  12916. (require 'parse-time)
  12917. (let* ((org-time-stamp-rounding-minutes
  12918. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12919. (org-dcst org-display-custom-times)
  12920. (ct (org-current-time))
  12921. (def (or org-overriding-default-time default-time ct))
  12922. (defdecode (decode-time def))
  12923. (dummy (progn
  12924. (when (< (nth 2 defdecode) org-extend-today-until)
  12925. (setcar (nthcdr 2 defdecode) -1)
  12926. (setcar (nthcdr 1 defdecode) 59)
  12927. (setq def (apply 'encode-time defdecode)
  12928. defdecode (decode-time def)))))
  12929. (calendar-frame-setup nil)
  12930. (calendar-setup nil)
  12931. (calendar-move-hook nil)
  12932. (calendar-view-diary-initially-flag nil)
  12933. (calendar-view-holidays-initially-flag nil)
  12934. (timestr (format-time-string
  12935. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12936. (prompt (concat (if prompt (concat prompt " ") "")
  12937. (format "Date+time [%s]: " timestr)))
  12938. ans (org-ans0 "") org-ans1 org-ans2 final)
  12939. (cond
  12940. (from-string (setq ans from-string))
  12941. (org-read-date-popup-calendar
  12942. (save-excursion
  12943. (save-window-excursion
  12944. (calendar)
  12945. (calendar-forward-day (- (time-to-days def)
  12946. (calendar-absolute-from-gregorian
  12947. (calendar-current-date))))
  12948. (org-eval-in-calendar nil t)
  12949. (let* ((old-map (current-local-map))
  12950. (map (copy-keymap calendar-mode-map))
  12951. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12952. (org-defkey map (kbd "RET") 'org-calendar-select)
  12953. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12954. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12955. (org-defkey minibuffer-local-map [(meta shift left)]
  12956. (lambda () (interactive)
  12957. (org-eval-in-calendar '(calendar-backward-month 1))))
  12958. (org-defkey minibuffer-local-map [(meta shift right)]
  12959. (lambda () (interactive)
  12960. (org-eval-in-calendar '(calendar-forward-month 1))))
  12961. (org-defkey minibuffer-local-map [(meta shift up)]
  12962. (lambda () (interactive)
  12963. (org-eval-in-calendar '(calendar-backward-year 1))))
  12964. (org-defkey minibuffer-local-map [(meta shift down)]
  12965. (lambda () (interactive)
  12966. (org-eval-in-calendar '(calendar-forward-year 1))))
  12967. (org-defkey minibuffer-local-map [?\e (shift left)]
  12968. (lambda () (interactive)
  12969. (org-eval-in-calendar '(calendar-backward-month 1))))
  12970. (org-defkey minibuffer-local-map [?\e (shift right)]
  12971. (lambda () (interactive)
  12972. (org-eval-in-calendar '(calendar-forward-month 1))))
  12973. (org-defkey minibuffer-local-map [?\e (shift up)]
  12974. (lambda () (interactive)
  12975. (org-eval-in-calendar '(calendar-backward-year 1))))
  12976. (org-defkey minibuffer-local-map [?\e (shift down)]
  12977. (lambda () (interactive)
  12978. (org-eval-in-calendar '(calendar-forward-year 1))))
  12979. (org-defkey minibuffer-local-map [(shift up)]
  12980. (lambda () (interactive)
  12981. (org-eval-in-calendar '(calendar-backward-week 1))))
  12982. (org-defkey minibuffer-local-map [(shift down)]
  12983. (lambda () (interactive)
  12984. (org-eval-in-calendar '(calendar-forward-week 1))))
  12985. (org-defkey minibuffer-local-map [(shift left)]
  12986. (lambda () (interactive)
  12987. (org-eval-in-calendar '(calendar-backward-day 1))))
  12988. (org-defkey minibuffer-local-map [(shift right)]
  12989. (lambda () (interactive)
  12990. (org-eval-in-calendar '(calendar-forward-day 1))))
  12991. (org-defkey minibuffer-local-map ">"
  12992. (lambda () (interactive)
  12993. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12994. (org-defkey minibuffer-local-map "<"
  12995. (lambda () (interactive)
  12996. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12997. (org-defkey minibuffer-local-map "\C-v"
  12998. (lambda () (interactive)
  12999. (org-eval-in-calendar
  13000. '(calendar-scroll-left-three-months 1))))
  13001. (org-defkey minibuffer-local-map "\M-v"
  13002. (lambda () (interactive)
  13003. (org-eval-in-calendar
  13004. '(calendar-scroll-right-three-months 1))))
  13005. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13006. (unwind-protect
  13007. (progn
  13008. (use-local-map map)
  13009. (add-hook 'post-command-hook 'org-read-date-display)
  13010. (setq org-ans0 (read-string prompt default-input
  13011. 'org-read-date-history nil))
  13012. ;; org-ans0: from prompt
  13013. ;; org-ans1: from mouse click
  13014. ;; org-ans2: from calendar motion
  13015. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13016. (remove-hook 'post-command-hook 'org-read-date-display)
  13017. (use-local-map old-map)
  13018. (when org-read-date-overlay
  13019. (delete-overlay org-read-date-overlay)
  13020. (setq org-read-date-overlay nil)))))))
  13021. (t ; Naked prompt only
  13022. (unwind-protect
  13023. (setq ans (read-string prompt default-input
  13024. 'org-read-date-history timestr))
  13025. (when org-read-date-overlay
  13026. (delete-overlay org-read-date-overlay)
  13027. (setq org-read-date-overlay nil)))))
  13028. (setq final (org-read-date-analyze ans def defdecode))
  13029. (setq org-read-date-final-answer ans)
  13030. (if to-time
  13031. (apply 'encode-time final)
  13032. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13033. (format "%04d-%02d-%02d %02d:%02d"
  13034. (nth 5 final) (nth 4 final) (nth 3 final)
  13035. (nth 2 final) (nth 1 final))
  13036. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13037. (defvar def)
  13038. (defvar defdecode)
  13039. (defvar with-time)
  13040. (defvar org-read-date-analyze-futurep nil)
  13041. (defun org-read-date-display ()
  13042. "Display the current date prompt interpretation in the minibuffer."
  13043. (when org-read-date-display-live
  13044. (when org-read-date-overlay
  13045. (delete-overlay org-read-date-overlay))
  13046. (let ((p (point)))
  13047. (end-of-line 1)
  13048. (while (not (equal (buffer-substring
  13049. (max (point-min) (- (point) 4)) (point))
  13050. " "))
  13051. (insert " "))
  13052. (goto-char p))
  13053. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13054. " " (or org-ans1 org-ans2)))
  13055. (org-end-time-was-given nil)
  13056. (f (org-read-date-analyze ans def defdecode))
  13057. (fmts (if org-dcst
  13058. org-time-stamp-custom-formats
  13059. org-time-stamp-formats))
  13060. (fmt (if (or with-time
  13061. (and (boundp 'org-time-was-given) org-time-was-given))
  13062. (cdr fmts)
  13063. (car fmts)))
  13064. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13065. (when (and org-end-time-was-given
  13066. (string-match org-plain-time-of-day-regexp txt))
  13067. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13068. org-end-time-was-given
  13069. (substring txt (match-end 0)))))
  13070. (when org-read-date-analyze-futurep
  13071. (setq txt (concat txt " (=>F)")))
  13072. (setq org-read-date-overlay
  13073. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13074. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13075. (defun org-read-date-analyze (ans def defdecode)
  13076. "Analyze the combined answer of the date prompt."
  13077. ;; FIXME: cleanup and comment
  13078. (let ((nowdecode (decode-time (current-time)))
  13079. delta deltan deltaw deltadef year month day
  13080. hour minute second wday pm h2 m2 tl wday1
  13081. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13082. (setq org-read-date-analyze-futurep nil)
  13083. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13084. (setq ans "+0"))
  13085. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13086. (setq ans (replace-match "" t t ans)
  13087. deltan (car delta)
  13088. deltaw (nth 1 delta)
  13089. deltadef (nth 2 delta)))
  13090. ;; Check if there is an iso week date in there
  13091. ;; If yes, store the info and postpone interpreting it until the rest
  13092. ;; of the parsing is done
  13093. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13094. (setq iso-year (if (match-end 1)
  13095. (org-small-year-to-year
  13096. (string-to-number (match-string 1 ans))))
  13097. iso-weekday (if (match-end 3)
  13098. (string-to-number (match-string 3 ans)))
  13099. iso-week (string-to-number (match-string 2 ans)))
  13100. (setq ans (replace-match "" t t ans)))
  13101. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13102. (when (string-match
  13103. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13104. (setq year (if (match-end 2)
  13105. (string-to-number (match-string 2 ans))
  13106. (progn (setq kill-year t)
  13107. (string-to-number (format-time-string "%Y"))))
  13108. month (string-to-number (match-string 3 ans))
  13109. day (string-to-number (match-string 4 ans)))
  13110. (if (< year 100) (setq year (+ 2000 year)))
  13111. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13112. t nil ans)))
  13113. ;; Help matching american dates, like 5/30 or 5/30/7
  13114. (when (string-match
  13115. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13116. (setq year (if (match-end 4)
  13117. (string-to-number (match-string 4 ans))
  13118. (progn (setq kill-year t)
  13119. (string-to-number (format-time-string "%Y"))))
  13120. month (string-to-number (match-string 1 ans))
  13121. day (string-to-number (match-string 2 ans)))
  13122. (if (< year 100) (setq year (+ 2000 year)))
  13123. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13124. t nil ans)))
  13125. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13126. ;; If there is a time with am/pm, and *no* time without it, we convert
  13127. ;; so that matching will be successful.
  13128. (loop for i from 1 to 2 do ; twice, for end time as well
  13129. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13130. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13131. (setq hour (string-to-number (match-string 1 ans))
  13132. minute (if (match-end 3)
  13133. (string-to-number (match-string 3 ans))
  13134. 0)
  13135. pm (equal ?p
  13136. (string-to-char (downcase (match-string 4 ans)))))
  13137. (if (and (= hour 12) (not pm))
  13138. (setq hour 0)
  13139. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13140. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13141. t t ans))))
  13142. ;; Check if a time range is given as a duration
  13143. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13144. (setq hour (string-to-number (match-string 1 ans))
  13145. h2 (+ hour (string-to-number (match-string 3 ans)))
  13146. minute (string-to-number (match-string 2 ans))
  13147. m2 (+ minute (if (match-end 5) (string-to-number
  13148. (match-string 5 ans))0)))
  13149. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13150. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13151. t t ans)))
  13152. ;; Check if there is a time range
  13153. (when (boundp 'org-end-time-was-given)
  13154. (setq org-time-was-given nil)
  13155. (when (and (string-match org-plain-time-of-day-regexp ans)
  13156. (match-end 8))
  13157. (setq org-end-time-was-given (match-string 8 ans))
  13158. (setq ans (concat (substring ans 0 (match-beginning 7))
  13159. (substring ans (match-end 7))))))
  13160. (setq tl (parse-time-string ans)
  13161. day (or (nth 3 tl) (nth 3 defdecode))
  13162. month (or (nth 4 tl)
  13163. (if (and org-read-date-prefer-future
  13164. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13165. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13166. (nth 4 defdecode)))
  13167. year (or (and (not kill-year) (nth 5 tl))
  13168. (if (and org-read-date-prefer-future
  13169. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13170. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13171. (nth 5 defdecode)))
  13172. hour (or (nth 2 tl) (nth 2 defdecode))
  13173. minute (or (nth 1 tl) (nth 1 defdecode))
  13174. second (or (nth 0 tl) 0)
  13175. wday (nth 6 tl))
  13176. (when (and (eq org-read-date-prefer-future 'time)
  13177. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13178. (equal day (nth 3 nowdecode))
  13179. (equal month (nth 4 nowdecode))
  13180. (equal year (nth 5 nowdecode))
  13181. (nth 2 tl)
  13182. (or (< (nth 2 tl) (nth 2 nowdecode))
  13183. (and (= (nth 2 tl) (nth 2 nowdecode))
  13184. (nth 1 tl)
  13185. (< (nth 1 tl) (nth 1 nowdecode)))))
  13186. (setq day (1+ day)
  13187. futurep t))
  13188. ;; Special date definitions below
  13189. (cond
  13190. (iso-week
  13191. ;; There was an iso week
  13192. (require 'cal-iso)
  13193. (setq futurep nil)
  13194. (setq year (or iso-year year)
  13195. day (or iso-weekday wday 1)
  13196. wday nil ; to make sure that the trigger below does not match
  13197. iso-date (calendar-gregorian-from-absolute
  13198. (calendar-absolute-from-iso
  13199. (list iso-week day year))))
  13200. ; FIXME: Should we also push ISO weeks into the future?
  13201. ; (when (and org-read-date-prefer-future
  13202. ; (not iso-year)
  13203. ; (< (calendar-absolute-from-gregorian iso-date)
  13204. ; (time-to-days (current-time))))
  13205. ; (setq year (1+ year)
  13206. ; iso-date (calendar-gregorian-from-absolute
  13207. ; (calendar-absolute-from-iso
  13208. ; (list iso-week day year)))))
  13209. (setq month (car iso-date)
  13210. year (nth 2 iso-date)
  13211. day (nth 1 iso-date)))
  13212. (deltan
  13213. (setq futurep nil)
  13214. (unless deltadef
  13215. (let ((now (decode-time (current-time))))
  13216. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13217. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13218. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13219. ((equal deltaw "m") (setq month (+ month deltan)))
  13220. ((equal deltaw "y") (setq year (+ year deltan)))))
  13221. ((and wday (not (nth 3 tl)))
  13222. (setq futurep nil)
  13223. ;; Weekday was given, but no day, so pick that day in the week
  13224. ;; on or after the derived date.
  13225. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13226. (unless (equal wday wday1)
  13227. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13228. (if (and (boundp 'org-time-was-given)
  13229. (nth 2 tl))
  13230. (setq org-time-was-given t))
  13231. (if (< year 100) (setq year (+ 2000 year)))
  13232. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  13233. (setq org-read-date-analyze-futurep futurep)
  13234. (list second minute hour day month year)))
  13235. (defvar parse-time-weekdays)
  13236. (defun org-read-date-get-relative (s today default)
  13237. "Check string S for special relative date string.
  13238. TODAY and DEFAULT are internal times, for today and for a default.
  13239. Return shift list (N what def-flag)
  13240. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13241. N is the number of WHATs to shift.
  13242. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13243. the DEFAULT date rather than TODAY."
  13244. (when (and
  13245. (string-match
  13246. (concat
  13247. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13248. "\\([0-9]+\\)?"
  13249. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13250. "\\([ \t]\\|$\\)") s)
  13251. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13252. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13253. (string-to-char (substring (match-string 1 s) -1))
  13254. ?+))
  13255. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13256. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13257. (what (if (match-end 3) (match-string 3 s) "d"))
  13258. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13259. (date (if rel default today))
  13260. (wday (nth 6 (decode-time date)))
  13261. delta)
  13262. (if wday1
  13263. (progn
  13264. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13265. (if (= dir ?-) (setq delta (- delta 7)))
  13266. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13267. (list delta "d" rel))
  13268. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13269. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13270. "Turn a user-specified date into the internal representation.
  13271. The internal representation needed by the calendar is (month day year).
  13272. This is a wrapper to handle the brain-dead convention in calendar that
  13273. user function argument order change dependent on argument order."
  13274. (if (boundp 'calendar-date-style)
  13275. (cond
  13276. ((eq calendar-date-style 'american)
  13277. (list arg1 arg2 arg3))
  13278. ((eq calendar-date-style 'european)
  13279. (list arg2 arg1 arg3))
  13280. ((eq calendar-date-style 'iso)
  13281. (list arg2 arg3 arg1)))
  13282. (if (org-bound-and-true-p european-calendar-style)
  13283. (list arg2 arg1 arg3)
  13284. (list arg1 arg2 arg3))))
  13285. (defun org-eval-in-calendar (form &optional keepdate)
  13286. "Eval FORM in the calendar window and return to current window.
  13287. Also, store the cursor date in variable org-ans2."
  13288. (let ((sf (selected-frame))
  13289. (sw (selected-window)))
  13290. (select-window (get-buffer-window "*Calendar*" t))
  13291. (eval form)
  13292. (when (and (not keepdate) (calendar-cursor-to-date))
  13293. (let* ((date (calendar-cursor-to-date))
  13294. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13295. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13296. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13297. (select-window sw)
  13298. (org-select-frame-set-input-focus sf)))
  13299. (defun org-calendar-select ()
  13300. "Return to `org-read-date' with the date currently selected.
  13301. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13302. (interactive)
  13303. (when (calendar-cursor-to-date)
  13304. (let* ((date (calendar-cursor-to-date))
  13305. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13306. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13307. (if (active-minibuffer-window) (exit-minibuffer))))
  13308. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13309. "Insert a date stamp for the date given by the internal TIME.
  13310. WITH-HM means use the stamp format that includes the time of the day.
  13311. INACTIVE means use square brackets instead of angular ones, so that the
  13312. stamp will not contribute to the agenda.
  13313. PRE and POST are optional strings to be inserted before and after the
  13314. stamp.
  13315. The command returns the inserted time stamp."
  13316. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13317. stamp)
  13318. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13319. (insert-before-markers (or pre ""))
  13320. (when (listp extra)
  13321. (setq extra (car extra))
  13322. (if (and (stringp extra)
  13323. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13324. (setq extra (format "-%02d:%02d"
  13325. (string-to-number (match-string 1 extra))
  13326. (string-to-number (match-string 2 extra))))
  13327. (setq extra nil)))
  13328. (when extra
  13329. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13330. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13331. (insert-before-markers (or post ""))
  13332. (setq org-last-inserted-timestamp stamp)))
  13333. (defun org-toggle-time-stamp-overlays ()
  13334. "Toggle the use of custom time stamp formats."
  13335. (interactive)
  13336. (setq org-display-custom-times (not org-display-custom-times))
  13337. (unless org-display-custom-times
  13338. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13339. (while (setq p (next-single-property-change p 'display))
  13340. (if (and (get-text-property p 'display)
  13341. (eq (get-text-property p 'face) 'org-date))
  13342. (remove-text-properties
  13343. p (setq p (next-single-property-change p 'display))
  13344. '(display t))))
  13345. (set-buffer-modified-p bmp)))
  13346. (if (featurep 'xemacs)
  13347. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13348. (org-restart-font-lock)
  13349. (setq org-table-may-need-update t)
  13350. (if org-display-custom-times
  13351. (message "Time stamps are overlayed with custom format")
  13352. (message "Time stamp overlays removed")))
  13353. (defun org-display-custom-time (beg end)
  13354. "Overlay modified time stamp format over timestamp between BEG and END."
  13355. (let* ((ts (buffer-substring beg end))
  13356. t1 w1 with-hm tf time str w2 (off 0))
  13357. (save-match-data
  13358. (setq t1 (org-parse-time-string ts t))
  13359. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13360. (setq off (- (match-end 0) (match-beginning 0)))))
  13361. (setq end (- end off))
  13362. (setq w1 (- end beg)
  13363. with-hm (and (nth 1 t1) (nth 2 t1))
  13364. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13365. time (org-fix-decoded-time t1)
  13366. str (org-add-props
  13367. (format-time-string
  13368. (substring tf 1 -1) (apply 'encode-time time))
  13369. nil 'mouse-face 'highlight)
  13370. w2 (length str))
  13371. (if (not (= w2 w1))
  13372. (add-text-properties (1+ beg) (+ 2 beg)
  13373. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13374. (if (featurep 'xemacs)
  13375. (progn
  13376. (put-text-property beg end 'invisible t)
  13377. (put-text-property beg end 'end-glyph (make-glyph str)))
  13378. (put-text-property beg end 'display str))))
  13379. (defun org-translate-time (string)
  13380. "Translate all timestamps in STRING to custom format.
  13381. But do this only if the variable `org-display-custom-times' is set."
  13382. (when org-display-custom-times
  13383. (save-match-data
  13384. (let* ((start 0)
  13385. (re org-ts-regexp-both)
  13386. t1 with-hm inactive tf time str beg end)
  13387. (while (setq start (string-match re string start))
  13388. (setq beg (match-beginning 0)
  13389. end (match-end 0)
  13390. t1 (save-match-data
  13391. (org-parse-time-string (substring string beg end) t))
  13392. with-hm (and (nth 1 t1) (nth 2 t1))
  13393. inactive (equal (substring string beg (1+ beg)) "[")
  13394. tf (funcall (if with-hm 'cdr 'car)
  13395. org-time-stamp-custom-formats)
  13396. time (org-fix-decoded-time t1)
  13397. str (format-time-string
  13398. (concat
  13399. (if inactive "[" "<") (substring tf 1 -1)
  13400. (if inactive "]" ">"))
  13401. (apply 'encode-time time))
  13402. string (replace-match str t t string)
  13403. start (+ start (length str)))))))
  13404. string)
  13405. (defun org-fix-decoded-time (time)
  13406. "Set 0 instead of nil for the first 6 elements of time.
  13407. Don't touch the rest."
  13408. (let ((n 0))
  13409. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13410. (defun org-days-to-time (timestamp-string)
  13411. "Difference between TIMESTAMP-STRING and now in days."
  13412. (- (time-to-days (org-time-string-to-time timestamp-string))
  13413. (time-to-days (current-time))))
  13414. (defun org-deadline-close (timestamp-string &optional ndays)
  13415. "Is the time in TIMESTAMP-STRING close to the current date?"
  13416. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13417. (and (< (org-days-to-time timestamp-string) ndays)
  13418. (not (org-entry-is-done-p))))
  13419. (defun org-get-wdays (ts)
  13420. "Get the deadline lead time appropriate for timestring TS."
  13421. (cond
  13422. ((<= org-deadline-warning-days 0)
  13423. ;; 0 or negative, enforce this value no matter what
  13424. (- org-deadline-warning-days))
  13425. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13426. ;; lead time is specified.
  13427. (floor (* (string-to-number (match-string 1 ts))
  13428. (cdr (assoc (match-string 2 ts)
  13429. '(("d" . 1) ("w" . 7)
  13430. ("m" . 30.4) ("y" . 365.25)))))))
  13431. ;; go for the default.
  13432. (t org-deadline-warning-days)))
  13433. (defun org-calendar-select-mouse (ev)
  13434. "Return to `org-read-date' with the date currently selected.
  13435. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13436. (interactive "e")
  13437. (mouse-set-point ev)
  13438. (when (calendar-cursor-to-date)
  13439. (let* ((date (calendar-cursor-to-date))
  13440. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13441. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13442. (if (active-minibuffer-window) (exit-minibuffer))))
  13443. (defun org-check-deadlines (ndays)
  13444. "Check if there are any deadlines due or past due.
  13445. A deadline is considered due if it happens within `org-deadline-warning-days'
  13446. days from today's date. If the deadline appears in an entry marked DONE,
  13447. it is not shown. The prefix arg NDAYS can be used to test that many
  13448. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13449. (interactive "P")
  13450. (let* ((org-warn-days
  13451. (cond
  13452. ((equal ndays '(4)) 100000)
  13453. (ndays (prefix-numeric-value ndays))
  13454. (t (abs org-deadline-warning-days))))
  13455. (case-fold-search nil)
  13456. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13457. (callback
  13458. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13459. (message "%d deadlines past-due or due within %d days"
  13460. (org-occur regexp nil callback)
  13461. org-warn-days)))
  13462. (defun org-check-before-date (date)
  13463. "Check if there are deadlines or scheduled entries before DATE."
  13464. (interactive (list (org-read-date)))
  13465. (let ((case-fold-search nil)
  13466. (regexp (concat "\\<\\(" org-deadline-string
  13467. "\\|" org-scheduled-string
  13468. "\\) *<\\([^>]+\\)>"))
  13469. (callback
  13470. (lambda () (time-less-p
  13471. (org-time-string-to-time (match-string 2))
  13472. (org-time-string-to-time date)))))
  13473. (message "%d entries before %s"
  13474. (org-occur regexp nil callback) date)))
  13475. (defun org-check-after-date (date)
  13476. "Check if there are deadlines or scheduled entries after DATE."
  13477. (interactive (list (org-read-date)))
  13478. (let ((case-fold-search nil)
  13479. (regexp (concat "\\<\\(" org-deadline-string
  13480. "\\|" org-scheduled-string
  13481. "\\) *<\\([^>]+\\)>"))
  13482. (callback
  13483. (lambda () (not
  13484. (time-less-p
  13485. (org-time-string-to-time (match-string 2))
  13486. (org-time-string-to-time date))))))
  13487. (message "%d entries after %s"
  13488. (org-occur regexp nil callback) date)))
  13489. (defun org-evaluate-time-range (&optional to-buffer)
  13490. "Evaluate a time range by computing the difference between start and end.
  13491. Normally the result is just printed in the echo area, but with prefix arg
  13492. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13493. If the time range is actually in a table, the result is inserted into the
  13494. next column.
  13495. For time difference computation, a year is assumed to be exactly 365
  13496. days in order to avoid rounding problems."
  13497. (interactive "P")
  13498. (or
  13499. (org-clock-update-time-maybe)
  13500. (save-excursion
  13501. (unless (org-at-date-range-p t)
  13502. (goto-char (point-at-bol))
  13503. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13504. (if (not (org-at-date-range-p t))
  13505. (error "Not at a time-stamp range, and none found in current line")))
  13506. (let* ((ts1 (match-string 1))
  13507. (ts2 (match-string 2))
  13508. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13509. (match-end (match-end 0))
  13510. (time1 (org-time-string-to-time ts1))
  13511. (time2 (org-time-string-to-time ts2))
  13512. (t1 (org-float-time time1))
  13513. (t2 (org-float-time time2))
  13514. (diff (abs (- t2 t1)))
  13515. (negative (< (- t2 t1) 0))
  13516. ;; (ys (floor (* 365 24 60 60)))
  13517. (ds (* 24 60 60))
  13518. (hs (* 60 60))
  13519. (fy "%dy %dd %02d:%02d")
  13520. (fy1 "%dy %dd")
  13521. (fd "%dd %02d:%02d")
  13522. (fd1 "%dd")
  13523. (fh "%02d:%02d")
  13524. y d h m align)
  13525. (if havetime
  13526. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13527. y 0
  13528. d (floor (/ diff ds)) diff (mod diff ds)
  13529. h (floor (/ diff hs)) diff (mod diff hs)
  13530. m (floor (/ diff 60)))
  13531. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13532. y 0
  13533. d (floor (+ (/ diff ds) 0.5))
  13534. h 0 m 0))
  13535. (if (not to-buffer)
  13536. (message "%s" (org-make-tdiff-string y d h m))
  13537. (if (org-at-table-p)
  13538. (progn
  13539. (goto-char match-end)
  13540. (setq align t)
  13541. (and (looking-at " *|") (goto-char (match-end 0))))
  13542. (goto-char match-end))
  13543. (if (looking-at
  13544. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13545. (replace-match ""))
  13546. (if negative (insert " -"))
  13547. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13548. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13549. (insert " " (format fh h m))))
  13550. (if align (org-table-align))
  13551. (message "Time difference inserted")))))
  13552. (defun org-make-tdiff-string (y d h m)
  13553. (let ((fmt "")
  13554. (l nil))
  13555. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13556. l (push y l)))
  13557. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13558. l (push d l)))
  13559. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13560. l (push h l)))
  13561. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13562. l (push m l)))
  13563. (apply 'format fmt (nreverse l))))
  13564. (defun org-time-string-to-time (s)
  13565. (apply 'encode-time (org-parse-time-string s)))
  13566. (defun org-time-string-to-seconds (s)
  13567. (org-float-time (org-time-string-to-time s)))
  13568. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13569. "Convert a time stamp to an absolute day number.
  13570. If there is a specifier for a cyclic time stamp, get the closest date to
  13571. DAYNR.
  13572. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13573. the variable date is bound by the calendar when this is called."
  13574. (cond
  13575. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13576. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13577. daynr
  13578. (+ daynr 1000)))
  13579. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13580. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13581. (time-to-days (current-time))) (match-string 0 s)
  13582. prefer show-all))
  13583. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13584. (defun org-days-to-iso-week (days)
  13585. "Return the iso week number."
  13586. (require 'cal-iso)
  13587. (car (calendar-iso-from-absolute days)))
  13588. (defun org-small-year-to-year (year)
  13589. "Convert 2-digit years into 4-digit years.
  13590. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13591. The year 2000 cannot be abbreviated. Any year larger than 99
  13592. is returned unchanged."
  13593. (if (< year 38)
  13594. (setq year (+ 2000 year))
  13595. (if (< year 100)
  13596. (setq year (+ 1900 year))))
  13597. year)
  13598. (defun org-time-from-absolute (d)
  13599. "Return the time corresponding to date D.
  13600. D may be an absolute day number, or a calendar-type list (month day year)."
  13601. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13602. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13603. (defun org-calendar-holiday ()
  13604. "List of holidays, for Diary display in Org-mode."
  13605. (require 'holidays)
  13606. (let ((hl (funcall
  13607. (if (fboundp 'calendar-check-holidays)
  13608. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13609. (if hl (mapconcat 'identity hl "; "))))
  13610. (defun org-diary-sexp-entry (sexp entry date)
  13611. "Process a SEXP diary ENTRY for DATE."
  13612. (require 'diary-lib)
  13613. (let ((result (if calendar-debug-sexp
  13614. (let ((stack-trace-on-error t))
  13615. (eval (car (read-from-string sexp))))
  13616. (condition-case nil
  13617. (eval (car (read-from-string sexp)))
  13618. (error
  13619. (beep)
  13620. (message "Bad sexp at line %d in %s: %s"
  13621. (org-current-line)
  13622. (buffer-file-name) sexp)
  13623. (sleep-for 2))))))
  13624. (cond ((stringp result) result)
  13625. ((and (consp result)
  13626. (stringp (cdr result))) (cdr result))
  13627. (result entry)
  13628. (t nil))))
  13629. (defun org-diary-to-ical-string (frombuf)
  13630. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13631. This uses the icalendar.el library."
  13632. (let* ((tmpdir (if (featurep 'xemacs)
  13633. (temp-directory)
  13634. temporary-file-directory))
  13635. (tmpfile (make-temp-name
  13636. (expand-file-name "orgics" tmpdir)))
  13637. buf rtn b e)
  13638. (with-current-buffer frombuf
  13639. (icalendar-export-region (point-min) (point-max) tmpfile)
  13640. (setq buf (find-buffer-visiting tmpfile))
  13641. (set-buffer buf)
  13642. (goto-char (point-min))
  13643. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13644. (setq b (match-beginning 0)))
  13645. (goto-char (point-max))
  13646. (if (re-search-backward "^END:VEVENT" nil t)
  13647. (setq e (match-end 0)))
  13648. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13649. (kill-buffer buf)
  13650. (delete-file tmpfile)
  13651. rtn))
  13652. (defun org-closest-date (start current change prefer show-all)
  13653. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13654. When PREFER is `past' return a date that is either CURRENT or past.
  13655. When PREFER is `future', return a date that is either CURRENT or future.
  13656. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13657. ;; Make the proper lists from the dates
  13658. (catch 'exit
  13659. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13660. dn dw sday cday n1 n2 n0
  13661. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13662. (setq start (org-date-to-gregorian start)
  13663. current (org-date-to-gregorian
  13664. (if show-all
  13665. current
  13666. (time-to-days (current-time))))
  13667. sday (calendar-absolute-from-gregorian start)
  13668. cday (calendar-absolute-from-gregorian current))
  13669. (if (<= cday sday) (throw 'exit sday))
  13670. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13671. (setq dn (string-to-number (match-string 1 change))
  13672. dw (cdr (assoc (match-string 2 change) a1)))
  13673. (error "Invalid change specifier: %s" change))
  13674. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13675. (cond
  13676. ((eq dw 'day)
  13677. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13678. n2 (+ n1 dn)))
  13679. ((eq dw 'year)
  13680. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13681. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13682. (setq date1 (list m d y1)
  13683. n1 (calendar-absolute-from-gregorian date1)
  13684. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13685. n2 (calendar-absolute-from-gregorian date2)))
  13686. ((eq dw 'month)
  13687. ;; approx number of month between the two dates
  13688. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13689. ;; How often does dn fit in there?
  13690. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13691. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13692. m (+ m nm)
  13693. ny (floor (/ m 12))
  13694. y (+ y ny)
  13695. m (- m (* ny 12)))
  13696. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13697. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13698. (setq m2 (+ m dn) y2 y)
  13699. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13700. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13701. (while (<= n2 cday)
  13702. (setq n1 n2 m m2 y y2)
  13703. (setq m2 (+ m dn) y2 y)
  13704. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13705. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13706. ;; Make sure n1 is the earlier date
  13707. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13708. (if show-all
  13709. (cond
  13710. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13711. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13712. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13713. (cond
  13714. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13715. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13716. (t (if (= cday n1) n1 n2)))))))
  13717. (defun org-date-to-gregorian (date)
  13718. "Turn any specification of DATE into a Gregorian date for the calendar."
  13719. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13720. ((and (listp date) (= (length date) 3)) date)
  13721. ((stringp date)
  13722. (setq date (org-parse-time-string date))
  13723. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13724. ((listp date)
  13725. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13726. (defun org-parse-time-string (s &optional nodefault)
  13727. "Parse the standard Org-mode time string.
  13728. This should be a lot faster than the normal `parse-time-string'.
  13729. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13730. hour and minute fields will be nil if not given."
  13731. (if (string-match org-ts-regexp0 s)
  13732. (list 0
  13733. (if (or (match-beginning 8) (not nodefault))
  13734. (string-to-number (or (match-string 8 s) "0")))
  13735. (if (or (match-beginning 7) (not nodefault))
  13736. (string-to-number (or (match-string 7 s) "0")))
  13737. (string-to-number (match-string 4 s))
  13738. (string-to-number (match-string 3 s))
  13739. (string-to-number (match-string 2 s))
  13740. nil nil nil)
  13741. (error "Not a standard Org-mode time string: %s" s)))
  13742. (defun org-timestamp-up (&optional arg)
  13743. "Increase the date item at the cursor by one.
  13744. If the cursor is on the year, change the year. If it is on the month or
  13745. the day, change that.
  13746. With prefix ARG, change by that many units."
  13747. (interactive "p")
  13748. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  13749. (defun org-timestamp-down (&optional arg)
  13750. "Decrease the date item at the cursor by one.
  13751. If the cursor is on the year, change the year. If it is on the month or
  13752. the day, change that.
  13753. With prefix ARG, change by that many units."
  13754. (interactive "p")
  13755. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  13756. (defun org-timestamp-up-day (&optional arg)
  13757. "Increase the date in the time stamp by one day.
  13758. With prefix ARG, change that many days."
  13759. (interactive "p")
  13760. (if (and (not (org-at-timestamp-p t))
  13761. (org-on-heading-p))
  13762. (org-todo 'up)
  13763. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  13764. (defun org-timestamp-down-day (&optional arg)
  13765. "Decrease the date in the time stamp by one day.
  13766. With prefix ARG, change that many days."
  13767. (interactive "p")
  13768. (if (and (not (org-at-timestamp-p t))
  13769. (org-on-heading-p))
  13770. (org-todo 'down)
  13771. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  13772. (defun org-at-timestamp-p (&optional inactive-ok)
  13773. "Determine if the cursor is in or at a timestamp."
  13774. (interactive)
  13775. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13776. (pos (point))
  13777. (ans (or (looking-at tsr)
  13778. (save-excursion
  13779. (skip-chars-backward "^[<\n\r\t")
  13780. (if (> (point) (point-min)) (backward-char 1))
  13781. (and (looking-at tsr)
  13782. (> (- (match-end 0) pos) -1))))))
  13783. (and ans
  13784. (boundp 'org-ts-what)
  13785. (setq org-ts-what
  13786. (cond
  13787. ((= pos (match-beginning 0)) 'bracket)
  13788. ((= pos (1- (match-end 0))) 'bracket)
  13789. ((org-pos-in-match-range pos 2) 'year)
  13790. ((org-pos-in-match-range pos 3) 'month)
  13791. ((org-pos-in-match-range pos 7) 'hour)
  13792. ((org-pos-in-match-range pos 8) 'minute)
  13793. ((or (org-pos-in-match-range pos 4)
  13794. (org-pos-in-match-range pos 5)) 'day)
  13795. ((and (> pos (or (match-end 8) (match-end 5)))
  13796. (< pos (match-end 0)))
  13797. (- pos (or (match-end 8) (match-end 5))))
  13798. (t 'day))))
  13799. ans))
  13800. (defun org-toggle-timestamp-type ()
  13801. "Toggle the type (<active> or [inactive]) of a time stamp."
  13802. (interactive)
  13803. (when (org-at-timestamp-p t)
  13804. (let ((beg (match-beginning 0)) (end (match-end 0))
  13805. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13806. (save-excursion
  13807. (goto-char beg)
  13808. (while (re-search-forward "[][<>]" end t)
  13809. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13810. t t)))
  13811. (message "Timestamp is now %sactive"
  13812. (if (equal (char-after beg) ?<) "" "in")))))
  13813. (defun org-timestamp-change (n &optional what updown)
  13814. "Change the date in the time stamp at point.
  13815. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13816. `year', `minute', `second'. If WHAT is not given, the cursor position
  13817. in the timestamp determines what will be changed."
  13818. (let ((pos (point))
  13819. with-hm inactive
  13820. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13821. org-ts-what
  13822. extra rem
  13823. ts time time0)
  13824. (if (not (org-at-timestamp-p t))
  13825. (error "Not at a timestamp"))
  13826. (if (and (not what) (eq org-ts-what 'bracket))
  13827. (org-toggle-timestamp-type)
  13828. (if (and (not what) (not (eq org-ts-what 'day))
  13829. org-display-custom-times
  13830. (get-text-property (point) 'display)
  13831. (not (get-text-property (1- (point)) 'display)))
  13832. (setq org-ts-what 'day))
  13833. (setq org-ts-what (or what org-ts-what)
  13834. inactive (= (char-after (match-beginning 0)) ?\[)
  13835. ts (match-string 0))
  13836. (replace-match "")
  13837. (if (string-match
  13838. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13839. ts)
  13840. (setq extra (match-string 1 ts)))
  13841. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13842. (setq with-hm t))
  13843. (setq time0 (org-parse-time-string ts))
  13844. (when (and updown
  13845. (eq org-ts-what 'minute)
  13846. (not current-prefix-arg))
  13847. ;; This looks like s-up and s-down. Change by one rounding step.
  13848. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13849. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13850. (setcar (cdr time0) (+ (nth 1 time0)
  13851. (if (> n 0) (- rem) (- dm rem))))))
  13852. (setq time
  13853. (encode-time (or (car time0) 0)
  13854. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13855. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13856. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13857. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13858. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13859. (nthcdr 6 time0)))
  13860. (when (and (member org-ts-what '(hour minute))
  13861. extra
  13862. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13863. (setq extra (org-modify-ts-extra
  13864. extra
  13865. (if (eq org-ts-what 'hour) 2 5)
  13866. n dm)))
  13867. (when (integerp org-ts-what)
  13868. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13869. (if (eq what 'calendar)
  13870. (let ((cal-date (org-get-date-from-calendar)))
  13871. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13872. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13873. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13874. (setcar time0 (or (car time0) 0))
  13875. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13876. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13877. (setq time (apply 'encode-time time0))))
  13878. (setq org-last-changed-timestamp
  13879. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13880. (org-clock-update-time-maybe)
  13881. (goto-char pos)
  13882. ;; Try to recenter the calendar window, if any
  13883. (if (and org-calendar-follow-timestamp-change
  13884. (get-buffer-window "*Calendar*" t)
  13885. (memq org-ts-what '(day month year)))
  13886. (org-recenter-calendar (time-to-days time))))))
  13887. (defun org-modify-ts-extra (s pos n dm)
  13888. "Change the different parts of the lead-time and repeat fields in timestamp."
  13889. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13890. ng h m new rem)
  13891. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13892. (cond
  13893. ((or (org-pos-in-match-range pos 2)
  13894. (org-pos-in-match-range pos 3))
  13895. (setq m (string-to-number (match-string 3 s))
  13896. h (string-to-number (match-string 2 s)))
  13897. (if (org-pos-in-match-range pos 2)
  13898. (setq h (+ h n))
  13899. (setq n (* dm (org-no-warnings (signum n))))
  13900. (when (not (= 0 (setq rem (% m dm))))
  13901. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13902. (setq m (+ m n)))
  13903. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13904. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13905. (setq h (min 24 (max 0 h)))
  13906. (setq ng 1 new (format "-%02d:%02d" h m)))
  13907. ((org-pos-in-match-range pos 6)
  13908. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13909. ((org-pos-in-match-range pos 5)
  13910. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13911. ((org-pos-in-match-range pos 9)
  13912. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13913. ((org-pos-in-match-range pos 8)
  13914. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13915. (when ng
  13916. (setq s (concat
  13917. (substring s 0 (match-beginning ng))
  13918. new
  13919. (substring s (match-end ng))))))
  13920. s))
  13921. (defun org-recenter-calendar (date)
  13922. "If the calendar is visible, recenter it to DATE."
  13923. (let* ((win (selected-window))
  13924. (cwin (get-buffer-window "*Calendar*" t))
  13925. (calendar-move-hook nil))
  13926. (when cwin
  13927. (select-window cwin)
  13928. (calendar-goto-date (if (listp date) date
  13929. (calendar-gregorian-from-absolute date)))
  13930. (select-window win))))
  13931. (defun org-goto-calendar (&optional arg)
  13932. "Go to the Emacs calendar at the current date.
  13933. If there is a time stamp in the current line, go to that date.
  13934. A prefix ARG can be used to force the current date."
  13935. (interactive "P")
  13936. (let ((tsr org-ts-regexp) diff
  13937. (calendar-move-hook nil)
  13938. (calendar-view-holidays-initially-flag nil)
  13939. (calendar-view-diary-initially-flag nil))
  13940. (if (or (org-at-timestamp-p)
  13941. (save-excursion
  13942. (beginning-of-line 1)
  13943. (looking-at (concat ".*" tsr))))
  13944. (let ((d1 (time-to-days (current-time)))
  13945. (d2 (time-to-days
  13946. (org-time-string-to-time (match-string 1)))))
  13947. (setq diff (- d2 d1))))
  13948. (calendar)
  13949. (calendar-goto-today)
  13950. (if (and diff (not arg)) (calendar-forward-day diff))))
  13951. (defun org-get-date-from-calendar ()
  13952. "Return a list (month day year) of date at point in calendar."
  13953. (with-current-buffer "*Calendar*"
  13954. (save-match-data
  13955. (calendar-cursor-to-date))))
  13956. (defun org-date-from-calendar ()
  13957. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13958. If there is already a time stamp at the cursor position, update it."
  13959. (interactive)
  13960. (if (org-at-timestamp-p t)
  13961. (org-timestamp-change 0 'calendar)
  13962. (let ((cal-date (org-get-date-from-calendar)))
  13963. (org-insert-time-stamp
  13964. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13965. (defun org-minutes-to-hh:mm-string (m)
  13966. "Compute H:MM from a number of minutes."
  13967. (let ((h (/ m 60)))
  13968. (setq m (- m (* 60 h)))
  13969. (format org-time-clocksum-format h m)))
  13970. (defun org-hh:mm-string-to-minutes (s)
  13971. "Convert a string H:MM to a number of minutes.
  13972. If the string is just a number, interpret it as minutes.
  13973. In fact, the first hh:mm or number in the string will be taken,
  13974. there can be extra stuff in the string.
  13975. If no number is found, the return value is 0."
  13976. (cond
  13977. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13978. (+ (* (string-to-number (match-string 1 s)) 60)
  13979. (string-to-number (match-string 2 s))))
  13980. ((string-match "\\([0-9]+\\)" s)
  13981. (string-to-number (match-string 1 s)))
  13982. (t 0)))
  13983. ;;;; Files
  13984. (defun org-save-all-org-buffers ()
  13985. "Save all Org-mode buffers without user confirmation."
  13986. (interactive)
  13987. (message "Saving all Org-mode buffers...")
  13988. (save-some-buffers t 'org-mode-p)
  13989. (when (featurep 'org-id) (org-id-locations-save))
  13990. (message "Saving all Org-mode buffers... done"))
  13991. (defun org-revert-all-org-buffers ()
  13992. "Revert all Org-mode buffers.
  13993. Prompt for confirmation when there are unsaved changes.
  13994. Be sure you know what you are doing before letting this function
  13995. overwrite your changes.
  13996. This function is useful in a setup where one tracks org files
  13997. with a version control system, to revert on one machine after pulling
  13998. changes from another. I believe the procedure must be like this:
  13999. 1. M-x org-save-all-org-buffers
  14000. 2. Pull changes from the other machine, resolve conflicts
  14001. 3. M-x org-revert-all-org-buffers"
  14002. (interactive)
  14003. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14004. (error "Abort"))
  14005. (save-excursion
  14006. (save-window-excursion
  14007. (mapc
  14008. (lambda (b)
  14009. (when (and (with-current-buffer b (org-mode-p))
  14010. (with-current-buffer b buffer-file-name))
  14011. (switch-to-buffer b)
  14012. (revert-buffer t 'no-confirm)))
  14013. (buffer-list))
  14014. (when (and (featurep 'org-id) org-id-track-globally)
  14015. (org-id-locations-load)))))
  14016. ;;;; Agenda files
  14017. ;;;###autoload
  14018. (defun org-switchb (&optional arg)
  14019. "Switch between Org buffers.
  14020. With a prefix argument, restrict available to files.
  14021. With two prefix arguments, restrict available buffers to agenda files.
  14022. Defaults to `iswitchb' for buffer name completion.
  14023. Set `org-completion-use-ido' to make it use ido instead."
  14024. (interactive "P")
  14025. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14026. ((equal arg '(16)) (org-buffer-list 'agenda))
  14027. (t (org-buffer-list))))
  14028. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14029. (org-completion-use-ido org-completion-use-ido))
  14030. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14031. (setq org-completion-use-iswitchb t))
  14032. (switch-to-buffer
  14033. (org-icompleting-read "Org buffer: "
  14034. (mapcar 'list (mapcar 'buffer-name blist))
  14035. nil t))))
  14036. ;;; Define some older names previously used for this functionality
  14037. ;;;###autoload
  14038. (defalias 'org-ido-switchb 'org-switchb)
  14039. ;;;###autoload
  14040. (defalias 'org-iswitchb 'org-switchb)
  14041. (defun org-buffer-list (&optional predicate exclude-tmp)
  14042. "Return a list of Org buffers.
  14043. PREDICATE can be `export', `files' or `agenda'.
  14044. export restrict the list to Export buffers.
  14045. files restrict the list to buffers visiting Org files.
  14046. agenda restrict the list to buffers visiting agenda files.
  14047. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14048. (let* ((bfn nil)
  14049. (agenda-files (and (eq predicate 'agenda)
  14050. (mapcar 'file-truename (org-agenda-files t))))
  14051. (filter
  14052. (cond
  14053. ((eq predicate 'files)
  14054. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14055. ((eq predicate 'export)
  14056. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14057. ((eq predicate 'agenda)
  14058. (lambda (b)
  14059. (with-current-buffer b
  14060. (and (eq major-mode 'org-mode)
  14061. (setq bfn (buffer-file-name b))
  14062. (member (file-truename bfn) agenda-files)))))
  14063. (t (lambda (b) (with-current-buffer b
  14064. (or (eq major-mode 'org-mode)
  14065. (string-match "\*Org .*Export"
  14066. (buffer-name b)))))))))
  14067. (delq nil
  14068. (mapcar
  14069. (lambda(b)
  14070. (if (and (funcall filter b)
  14071. (or (not exclude-tmp)
  14072. (not (string-match "tmp" (buffer-name b)))))
  14073. b
  14074. nil))
  14075. (buffer-list)))))
  14076. (defun org-agenda-files (&optional unrestricted archives)
  14077. "Get the list of agenda files.
  14078. Optional UNRESTRICTED means return the full list even if a restriction
  14079. is currently in place.
  14080. When ARCHIVES is t, include all archive files that are really being
  14081. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14082. `org-agenda-archives-mode' is t."
  14083. (let ((files
  14084. (cond
  14085. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14086. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14087. ((listp org-agenda-files) org-agenda-files)
  14088. (t (error "Invalid value of `org-agenda-files'")))))
  14089. (setq files (apply 'append
  14090. (mapcar (lambda (f)
  14091. (if (file-directory-p f)
  14092. (directory-files
  14093. f t org-agenda-file-regexp)
  14094. (list f)))
  14095. files)))
  14096. (when org-agenda-skip-unavailable-files
  14097. (setq files (delq nil
  14098. (mapcar (function
  14099. (lambda (file)
  14100. (and (file-readable-p file) file)))
  14101. files))))
  14102. (when (or (eq archives t)
  14103. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14104. (setq files (org-add-archive-files files)))
  14105. files))
  14106. (defun org-agenda-file-p (&optional file)
  14107. "Return non-nil, if FILE is an agenda file.
  14108. If FILE is omitted, use the file associated with the current
  14109. buffer."
  14110. (member (or file (buffer-file-name))
  14111. (org-agenda-files t)))
  14112. (defun org-edit-agenda-file-list ()
  14113. "Edit the list of agenda files.
  14114. Depending on setup, this either uses customize to edit the variable
  14115. `org-agenda-files', or it visits the file that is holding the list. In the
  14116. latter case, the buffer is set up in a way that saving it automatically kills
  14117. the buffer and restores the previous window configuration."
  14118. (interactive)
  14119. (if (stringp org-agenda-files)
  14120. (let ((cw (current-window-configuration)))
  14121. (find-file org-agenda-files)
  14122. (org-set-local 'org-window-configuration cw)
  14123. (org-add-hook 'after-save-hook
  14124. (lambda ()
  14125. (set-window-configuration
  14126. (prog1 org-window-configuration
  14127. (kill-buffer (current-buffer))))
  14128. (org-install-agenda-files-menu)
  14129. (message "New agenda file list installed"))
  14130. nil 'local)
  14131. (message "%s" (substitute-command-keys
  14132. "Edit list and finish with \\[save-buffer]")))
  14133. (customize-variable 'org-agenda-files)))
  14134. (defun org-store-new-agenda-file-list (list)
  14135. "Set new value for the agenda file list and save it correctly."
  14136. (if (stringp org-agenda-files)
  14137. (let ((fe (org-read-agenda-file-list t)) b u)
  14138. (while (setq b (find-buffer-visiting org-agenda-files))
  14139. (kill-buffer b))
  14140. (with-temp-file org-agenda-files
  14141. (insert
  14142. (mapconcat
  14143. (lambda (f) ;; Keep un-expanded entries.
  14144. (if (setq u (assoc f fe))
  14145. (cdr u)
  14146. f))
  14147. list "\n")
  14148. "\n")))
  14149. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14150. (org-insert-mode-line-in-empty-file nil))
  14151. (setq org-agenda-files list)
  14152. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14153. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14154. "Read the list of agenda files from a file.
  14155. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14156. filenames, used by `org-store-new-agenda-file-list' to write back
  14157. un-expanded file names."
  14158. (when (file-directory-p org-agenda-files)
  14159. (error "`org-agenda-files' cannot be a single directory"))
  14160. (when (stringp org-agenda-files)
  14161. (with-temp-buffer
  14162. (insert-file-contents org-agenda-files)
  14163. (mapcar
  14164. (lambda (f)
  14165. (let ((e (expand-file-name (substitute-in-file-name f)
  14166. org-directory)))
  14167. (if pair-with-expansion
  14168. (cons e f)
  14169. e)))
  14170. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14171. ;;;###autoload
  14172. (defun org-cycle-agenda-files ()
  14173. "Cycle through the files in `org-agenda-files'.
  14174. If the current buffer visits an agenda file, find the next one in the list.
  14175. If the current buffer does not, find the first agenda file."
  14176. (interactive)
  14177. (let* ((fs (org-agenda-files t))
  14178. (files (append fs (list (car fs))))
  14179. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14180. file)
  14181. (unless files (error "No agenda files"))
  14182. (catch 'exit
  14183. (while (setq file (pop files))
  14184. (if (equal (file-truename file) tcf)
  14185. (when (car files)
  14186. (find-file (car files))
  14187. (throw 'exit t))))
  14188. (find-file (car fs)))
  14189. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14190. (defun org-agenda-file-to-front (&optional to-end)
  14191. "Move/add the current file to the top of the agenda file list.
  14192. If the file is not present in the list, it is added to the front. If it is
  14193. present, it is moved there. With optional argument TO-END, add/move to the
  14194. end of the list."
  14195. (interactive "P")
  14196. (let ((org-agenda-skip-unavailable-files nil)
  14197. (file-alist (mapcar (lambda (x)
  14198. (cons (file-truename x) x))
  14199. (org-agenda-files t)))
  14200. (ctf (file-truename buffer-file-name))
  14201. x had)
  14202. (setq x (assoc ctf file-alist) had x)
  14203. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14204. (if to-end
  14205. (setq file-alist (append (delq x file-alist) (list x)))
  14206. (setq file-alist (cons x (delq x file-alist))))
  14207. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14208. (org-install-agenda-files-menu)
  14209. (message "File %s to %s of agenda file list"
  14210. (if had "moved" "added") (if to-end "end" "front"))))
  14211. (defun org-remove-file (&optional file)
  14212. "Remove current file from the list of files in variable `org-agenda-files'.
  14213. These are the files which are being checked for agenda entries.
  14214. Optional argument FILE means use this file instead of the current."
  14215. (interactive)
  14216. (let* ((org-agenda-skip-unavailable-files nil)
  14217. (file (or file buffer-file-name))
  14218. (true-file (file-truename file))
  14219. (afile (abbreviate-file-name file))
  14220. (files (delq nil (mapcar
  14221. (lambda (x)
  14222. (if (equal true-file
  14223. (file-truename x))
  14224. nil x))
  14225. (org-agenda-files t)))))
  14226. (if (not (= (length files) (length (org-agenda-files t))))
  14227. (progn
  14228. (org-store-new-agenda-file-list files)
  14229. (org-install-agenda-files-menu)
  14230. (message "Removed file: %s" afile))
  14231. (message "File was not in list: %s (not removed)" afile))))
  14232. (defun org-file-menu-entry (file)
  14233. (vector file (list 'find-file file) t))
  14234. (defun org-check-agenda-file (file)
  14235. "Make sure FILE exists. If not, ask user what to do."
  14236. (when (not (file-exists-p file))
  14237. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14238. (abbreviate-file-name file))
  14239. (let ((r (downcase (read-char-exclusive))))
  14240. (cond
  14241. ((equal r ?r)
  14242. (org-remove-file file)
  14243. (throw 'nextfile t))
  14244. (t (error "Abort"))))))
  14245. (defun org-get-agenda-file-buffer (file)
  14246. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14247. it to the list of buffers which might be released later."
  14248. (let ((buf (org-find-base-buffer-visiting file)))
  14249. (if buf
  14250. buf ; just return it
  14251. ;; Make a new buffer and remember it
  14252. (setq buf (find-file-noselect file))
  14253. (if buf (push buf org-agenda-new-buffers))
  14254. buf)))
  14255. (defun org-release-buffers (blist)
  14256. "Release all buffers in list, asking the user for confirmation when needed.
  14257. When a buffer is unmodified, it is just killed. When modified, it is saved
  14258. \(if the user agrees) and then killed."
  14259. (let (buf file)
  14260. (while (setq buf (pop blist))
  14261. (setq file (buffer-file-name buf))
  14262. (when (and (buffer-modified-p buf)
  14263. file
  14264. (y-or-n-p (format "Save file %s? " file)))
  14265. (with-current-buffer buf (save-buffer)))
  14266. (kill-buffer buf))))
  14267. (defun org-prepare-agenda-buffers (files)
  14268. "Create buffers for all agenda files, protect archived trees and comments."
  14269. (interactive)
  14270. (let ((pa '(:org-archived t))
  14271. (pc '(:org-comment t))
  14272. (pall '(:org-archived t :org-comment t))
  14273. (inhibit-read-only t)
  14274. (rea (concat ":" org-archive-tag ":"))
  14275. bmp file re)
  14276. (save-excursion
  14277. (save-restriction
  14278. (while (setq file (pop files))
  14279. (catch 'nextfile
  14280. (if (bufferp file)
  14281. (set-buffer file)
  14282. (org-check-agenda-file file)
  14283. (set-buffer (org-get-agenda-file-buffer file)))
  14284. (widen)
  14285. (setq bmp (buffer-modified-p))
  14286. (org-refresh-category-properties)
  14287. (setq org-todo-keywords-for-agenda
  14288. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14289. (setq org-done-keywords-for-agenda
  14290. (append org-done-keywords-for-agenda org-done-keywords))
  14291. (setq org-todo-keyword-alist-for-agenda
  14292. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14293. (setq org-drawers-for-agenda
  14294. (append org-drawers-for-agenda org-drawers))
  14295. (setq org-tag-alist-for-agenda
  14296. (append org-tag-alist-for-agenda org-tag-alist))
  14297. (save-excursion
  14298. (remove-text-properties (point-min) (point-max) pall)
  14299. (when org-agenda-skip-archived-trees
  14300. (goto-char (point-min))
  14301. (while (re-search-forward rea nil t)
  14302. (if (org-on-heading-p t)
  14303. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14304. (goto-char (point-min))
  14305. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14306. (while (re-search-forward re nil t)
  14307. (add-text-properties
  14308. (match-beginning 0) (org-end-of-subtree t) pc)))
  14309. (set-buffer-modified-p bmp)))))
  14310. (setq org-todo-keywords-for-agenda
  14311. (org-uniquify org-todo-keywords-for-agenda))
  14312. (setq org-todo-keyword-alist-for-agenda
  14313. (org-uniquify org-todo-keyword-alist-for-agenda)
  14314. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14315. ;;;; Embedded LaTeX
  14316. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14317. "Keymap for the minor `org-cdlatex-mode'.")
  14318. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14319. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14320. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14321. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14322. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14323. (defvar org-cdlatex-texmathp-advice-is-done nil
  14324. "Flag remembering if we have applied the advice to texmathp already.")
  14325. (define-minor-mode org-cdlatex-mode
  14326. "Toggle the minor `org-cdlatex-mode'.
  14327. This mode supports entering LaTeX environment and math in LaTeX fragments
  14328. in Org-mode.
  14329. \\{org-cdlatex-mode-map}"
  14330. nil " OCDL" nil
  14331. (when org-cdlatex-mode (require 'cdlatex))
  14332. (unless org-cdlatex-texmathp-advice-is-done
  14333. (setq org-cdlatex-texmathp-advice-is-done t)
  14334. (defadvice texmathp (around org-math-always-on activate)
  14335. "Always return t in org-mode buffers.
  14336. This is because we want to insert math symbols without dollars even outside
  14337. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14338. an embedded LaTeX fragment, let texmathp do its job.
  14339. \\[org-cdlatex-mode-map]"
  14340. (interactive)
  14341. (let (p)
  14342. (cond
  14343. ((not (org-mode-p)) ad-do-it)
  14344. ((eq this-command 'cdlatex-math-symbol)
  14345. (setq ad-return-value t
  14346. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14347. (t
  14348. (let ((p (org-inside-LaTeX-fragment-p)))
  14349. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14350. (setq ad-return-value t
  14351. texmathp-why '("Org-mode embedded math" . 0))
  14352. (if p ad-do-it)))))))))
  14353. (defun turn-on-org-cdlatex ()
  14354. "Unconditionally turn on `org-cdlatex-mode'."
  14355. (org-cdlatex-mode 1))
  14356. (defun org-inside-LaTeX-fragment-p ()
  14357. "Test if point is inside a LaTeX fragment.
  14358. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14359. sequence appearing also before point.
  14360. Even though the matchers for math are configurable, this function assumes
  14361. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14362. delimiters are skipped when they have been removed by customization.
  14363. The return value is nil, or a cons cell with the delimiter and
  14364. and the position of this delimiter.
  14365. This function does a reasonably good job, but can locally be fooled by
  14366. for example currency specifications. For example it will assume being in
  14367. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14368. fragments that are properly closed, but during editing, we have to live
  14369. with the uncertainty caused by missing closing delimiters. This function
  14370. looks only before point, not after."
  14371. (catch 'exit
  14372. (let ((pos (point))
  14373. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14374. (lim (progn
  14375. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14376. (point)))
  14377. dd-on str (start 0) m re)
  14378. (goto-char pos)
  14379. (when dodollar
  14380. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14381. re (nth 1 (assoc "$" org-latex-regexps)))
  14382. (while (string-match re str start)
  14383. (cond
  14384. ((= (match-end 0) (length str))
  14385. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14386. ((= (match-end 0) (- (length str) 5))
  14387. (throw 'exit nil))
  14388. (t (setq start (match-end 0))))))
  14389. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14390. (goto-char pos)
  14391. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14392. (and (match-beginning 2) (throw 'exit nil))
  14393. ;; count $$
  14394. (while (re-search-backward "\\$\\$" lim t)
  14395. (setq dd-on (not dd-on)))
  14396. (goto-char pos)
  14397. (if dd-on (cons "$$" m))))))
  14398. (defun org-inside-latex-macro-p ()
  14399. "Is point inside a LaTeX macro or its arguments?"
  14400. (save-match-data
  14401. (org-in-regexp
  14402. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14403. (defun org-try-cdlatex-tab ()
  14404. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14405. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14406. - inside a LaTeX fragment, or
  14407. - after the first word in a line, where an abbreviation expansion could
  14408. insert a LaTeX environment."
  14409. (when org-cdlatex-mode
  14410. (cond
  14411. ((save-excursion
  14412. (skip-chars-backward "a-zA-Z0-9*")
  14413. (skip-chars-backward " \t")
  14414. (bolp))
  14415. (cdlatex-tab) t)
  14416. ((org-inside-LaTeX-fragment-p)
  14417. (cdlatex-tab) t)
  14418. (t nil))))
  14419. (defun org-cdlatex-underscore-caret (&optional arg)
  14420. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14421. Revert to the normal definition outside of these fragments."
  14422. (interactive "P")
  14423. (if (org-inside-LaTeX-fragment-p)
  14424. (call-interactively 'cdlatex-sub-superscript)
  14425. (let (org-cdlatex-mode)
  14426. (call-interactively (key-binding (vector last-input-event))))))
  14427. (defun org-cdlatex-math-modify (&optional arg)
  14428. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14429. Revert to the normal definition outside of these fragments."
  14430. (interactive "P")
  14431. (if (org-inside-LaTeX-fragment-p)
  14432. (call-interactively 'cdlatex-math-modify)
  14433. (let (org-cdlatex-mode)
  14434. (call-interactively (key-binding (vector last-input-event))))))
  14435. (defvar org-latex-fragment-image-overlays nil
  14436. "List of overlays carrying the images of latex fragments.")
  14437. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14438. (defun org-remove-latex-fragment-image-overlays ()
  14439. "Remove all overlays with LaTeX fragment images in current buffer."
  14440. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14441. (setq org-latex-fragment-image-overlays nil))
  14442. (defun org-preview-latex-fragment (&optional subtree)
  14443. "Preview the LaTeX fragment at point, or all locally or globally.
  14444. If the cursor is in a LaTeX fragment, create the image and overlay
  14445. it over the source code. If there is no fragment at point, display
  14446. all fragments in the current text, from one headline to the next. With
  14447. prefix SUBTREE, display all fragments in the current subtree. With a
  14448. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14449. the cursor is before the first headline,
  14450. display all fragments in the buffer.
  14451. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14452. (interactive "P")
  14453. (org-remove-latex-fragment-image-overlays)
  14454. (save-excursion
  14455. (save-restriction
  14456. (let (beg end at msg)
  14457. (cond
  14458. ((or (equal subtree '(16))
  14459. (not (save-excursion
  14460. (re-search-backward (concat "^" outline-regexp) nil t))))
  14461. (setq beg (point-min) end (point-max)
  14462. msg "Creating images for buffer...%s"))
  14463. ((equal subtree '(4))
  14464. (org-back-to-heading)
  14465. (setq beg (point) end (org-end-of-subtree t)
  14466. msg "Creating images for subtree...%s"))
  14467. (t
  14468. (if (setq at (org-inside-LaTeX-fragment-p))
  14469. (goto-char (max (point-min) (- (cdr at) 2)))
  14470. (org-back-to-heading))
  14471. (setq beg (point) end (progn (outline-next-heading) (point))
  14472. msg (if at "Creating image...%s"
  14473. "Creating images for entry...%s"))))
  14474. (message msg "")
  14475. (narrow-to-region beg end)
  14476. (goto-char beg)
  14477. (org-format-latex
  14478. (concat "ltxpng/" (file-name-sans-extension
  14479. (file-name-nondirectory
  14480. buffer-file-name)))
  14481. default-directory 'overlays msg at 'forbuffer)
  14482. (message msg "done. Use `C-c C-c' to remove images.")))))
  14483. (defvar org-latex-regexps
  14484. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14485. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14486. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14487. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14488. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14489. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14490. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14491. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14492. "Regular expressions for matching embedded LaTeX.")
  14493. (defun org-format-latex (prefix &optional dir overlays msg at
  14494. forbuffer protect-only)
  14495. "Replace LaTeX fragments with links to an image, and produce images.
  14496. Some of the options can be changed using the variable
  14497. `org-format-latex-options'."
  14498. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14499. (let* ((prefixnodir (file-name-nondirectory prefix))
  14500. (absprefix (expand-file-name prefix dir))
  14501. (todir (file-name-directory absprefix))
  14502. (opt org-format-latex-options)
  14503. (matchers (plist-get opt :matchers))
  14504. (re-list org-latex-regexps)
  14505. (org-format-latex-header-extra
  14506. (plist-get (org-infile-export-plist) :latex-header-extra))
  14507. (cnt 0) txt hash link beg end re e checkdir
  14508. executables-checked
  14509. m n block linkfile movefile ov)
  14510. ;; Check the different regular expressions
  14511. (while (setq e (pop re-list))
  14512. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14513. block (if (nth 3 e) "\n\n" ""))
  14514. (when (member m matchers)
  14515. (goto-char (point-min))
  14516. (while (re-search-forward re nil t)
  14517. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14518. (not (get-text-property (match-beginning n)
  14519. 'org-protected))
  14520. (or (not overlays)
  14521. (not (eq (get-char-property (match-beginning n)
  14522. 'org-overlay-type)
  14523. 'org-latex-overlay))))
  14524. (if protect-only
  14525. (add-text-properties (match-beginning n) (match-end n)
  14526. '(org-protected t))
  14527. (setq txt (match-string n)
  14528. beg (match-beginning n) end (match-end n)
  14529. cnt (1+ cnt))
  14530. (let (print-length print-level) ; make sure full list is printed
  14531. (setq hash (sha1 (prin1-to-string
  14532. (list org-format-latex-header
  14533. org-format-latex-header-extra
  14534. org-export-latex-default-packages-alist
  14535. org-export-latex-packages-alist
  14536. org-format-latex-options
  14537. forbuffer txt)))
  14538. linkfile (format "%s_%s.png" prefix hash)
  14539. movefile (format "%s_%s.png" absprefix hash)))
  14540. (setq link (concat block "[[file:" linkfile "]]" block))
  14541. (if msg (message msg cnt))
  14542. (goto-char beg)
  14543. (unless checkdir ; make sure the directory exists
  14544. (setq checkdir t)
  14545. (or (file-directory-p todir) (make-directory todir t)))
  14546. (unless executables-checked
  14547. (org-check-external-command
  14548. "latex" "needed to convert LaTeX fragments to images")
  14549. (org-check-external-command
  14550. "dvipng" "needed to convert LaTeX fragments to images")
  14551. (setq executables-checked t))
  14552. (unless (file-exists-p movefile)
  14553. (org-create-formula-image
  14554. txt movefile opt forbuffer))
  14555. (if overlays
  14556. (progn
  14557. (mapc (lambda (o)
  14558. (if (eq (overlay-get o 'org-overlay-type)
  14559. 'org-latex-overlay)
  14560. (delete-overlay o)))
  14561. (overlays-in beg end))
  14562. (setq ov (make-overlay beg end))
  14563. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14564. (if (featurep 'xemacs)
  14565. (progn
  14566. (overlay-put ov 'invisible t)
  14567. (overlay-put
  14568. ov 'end-glyph
  14569. (make-glyph (vector 'png :file movefile))))
  14570. (overlay-put
  14571. ov 'display
  14572. (list 'image :type 'png :file movefile :ascent 'center)))
  14573. (push ov org-latex-fragment-image-overlays)
  14574. (goto-char end))
  14575. (delete-region beg end)
  14576. (insert (org-add-props link
  14577. (list 'org-latex-src
  14578. (replace-regexp-in-string "\"" "" txt))))))))))))
  14579. ;; This function borrows from Ganesh Swami's latex2png.el
  14580. (defun org-create-formula-image (string tofile options buffer)
  14581. "This calls dvipng."
  14582. (require 'org-latex)
  14583. (let* ((tmpdir (if (featurep 'xemacs)
  14584. (temp-directory)
  14585. temporary-file-directory))
  14586. (texfilebase (make-temp-name
  14587. (expand-file-name "orgtex" tmpdir)))
  14588. (texfile (concat texfilebase ".tex"))
  14589. (dvifile (concat texfilebase ".dvi"))
  14590. (pngfile (concat texfilebase ".png"))
  14591. (fnh (if (featurep 'xemacs)
  14592. (font-height (get-face-font 'default))
  14593. (face-attribute 'default :height nil)))
  14594. (res (plist-get options :html-resolution))
  14595. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14596. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh res))))))
  14597. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14598. "Black"))
  14599. (bg (or (plist-get options (if buffer :background :html-background))
  14600. "Transparent")))
  14601. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14602. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14603. (with-temp-file texfile
  14604. (insert (org-splice-latex-header
  14605. org-format-latex-header
  14606. org-export-latex-default-packages-alist
  14607. org-export-latex-packages-alist t
  14608. org-format-latex-header-extra))
  14609. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14610. (require 'org-latex)
  14611. (org-export-latex-fix-inputenc))
  14612. (let ((dir default-directory))
  14613. (condition-case nil
  14614. (progn
  14615. (cd tmpdir)
  14616. (call-process "latex" nil nil nil texfile))
  14617. (error nil))
  14618. (cd dir))
  14619. (if (not (file-exists-p dvifile))
  14620. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14621. (condition-case nil
  14622. (call-process "dvipng" nil nil nil
  14623. "-fg" fg "-bg" bg
  14624. "-D" dpi
  14625. ;;"-x" scale "-y" scale
  14626. "-T" "tight"
  14627. "-o" pngfile
  14628. dvifile)
  14629. (error nil))
  14630. (if (not (file-exists-p pngfile))
  14631. (if org-format-latex-signal-error
  14632. (error "Failed to create png file from %s" texfile)
  14633. (message "Failed to create png file from %s" texfile)
  14634. nil)
  14635. ;; Use the requested file name and clean up
  14636. (copy-file pngfile tofile 'replace)
  14637. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14638. (delete-file (concat texfilebase e)))
  14639. pngfile))))
  14640. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14641. "Fill a LaTeX header template TPL.
  14642. In the template, the following place holders will be recognized:
  14643. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14644. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14645. [PACKAGES] \\usepackage statements for PKG
  14646. [NO-PACKAGES] do not include PKG
  14647. [EXTRA] the string EXTRA
  14648. [NO-EXTRA] do not include EXTRA
  14649. For backward compatibility, if both the positive and the negative place
  14650. holder is missing, the positive one (without the \"NO-\") will be
  14651. assumed to be present at the end of the template.
  14652. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14653. EXTRA is a string.
  14654. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14655. (let (rpl (end ""))
  14656. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14657. (setq rpl (if (or (match-end 1) (not def-pkg))
  14658. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14659. tpl (replace-match rpl t t tpl))
  14660. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14661. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14662. (setq rpl (if (or (match-end 1) (not pkg))
  14663. "" (org-latex-packages-to-string pkg snippets-p t))
  14664. tpl (replace-match rpl t t tpl))
  14665. (if pkg (setq end
  14666. (concat end "\n"
  14667. (org-latex-packages-to-string pkg snippets-p)))))
  14668. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14669. (setq rpl (if (or (match-end 1) (not extra))
  14670. "" (concat extra "\n"))
  14671. tpl (replace-match rpl t t tpl))
  14672. (if (and extra (string-match "\\S-" extra))
  14673. (setq end (concat end "\n" extra))))
  14674. (if (string-match "\\S-" end)
  14675. (concat tpl "\n" end)
  14676. tpl)))
  14677. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14678. "Turn an alist of packages into a string with the \\usepackage macros."
  14679. (setq pkg (mapconcat (lambda(p)
  14680. (cond
  14681. ((stringp p) p)
  14682. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14683. (format "%% Package %s omitted" (cadr p)))
  14684. ((equal "" (car p))
  14685. (format "\\usepackage{%s}" (cadr p)))
  14686. (t
  14687. (format "\\usepackage[%s]{%s}"
  14688. (car p) (cadr p)))))
  14689. pkg
  14690. "\n"))
  14691. (if newline (concat pkg "\n") pkg))
  14692. (defun org-dvipng-color (attr)
  14693. "Return an rgb color specification for dvipng."
  14694. (apply 'format "rgb %s %s %s"
  14695. (mapcar 'org-normalize-color
  14696. (color-values (face-attribute 'default attr nil)))))
  14697. (defun org-normalize-color (value)
  14698. "Return string to be used as color value for an RGB component."
  14699. (format "%g" (/ value 65535.0)))
  14700. ;; Image display
  14701. (defvar org-inline-image-overlays nil)
  14702. (make-variable-buffer-local 'org-inline-image-overlays)
  14703. (defun org-toggle-inline-images (&optional include-linked)
  14704. "Toggle the display of inline images.
  14705. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14706. (interactive "P")
  14707. (if org-inline-image-overlays
  14708. (progn
  14709. (org-remove-inline-images)
  14710. (message "Inline image display turned off"))
  14711. (org-display-inline-images include-linked)
  14712. (if org-inline-image-overlays
  14713. (message "%d images displayed inline"
  14714. (length org-inline-image-overlays))
  14715. (message "No images to display inline"))))
  14716. (defun org-display-inline-images (&optional include-linked refresh beg end)
  14717. "Display inline images.
  14718. Normally only links without a description part are inlined, because this
  14719. is how it will work for export. When INCLUDE-LINKED is set, also links
  14720. with a description part will be inlined. This can be nice for a quick
  14721. look at those images, but it does not reflect what exported files will look
  14722. like.
  14723. When REFRESH is set, refresh existing images between BEG and END.
  14724. This will create new image displays only if necessary.
  14725. BEG and END default to the buffer boundaries."
  14726. (interactive "P")
  14727. (unless refresh
  14728. (org-remove-inline-images)
  14729. (clear-image-cache))
  14730. (save-excursion
  14731. (save-restriction
  14732. (widen)
  14733. (setq beg (or beg (point-min)) end (or end (point-max)))
  14734. (goto-char (point-min))
  14735. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([-+~.:/\\_0-9a-zA-Z ]+"
  14736. (substring (org-image-file-name-regexp) 0 -2)
  14737. "\\)\\]" (if include-linked "" "\\]")))
  14738. old file ov img)
  14739. (while (re-search-forward re end t)
  14740. (setq old (get-char-property-and-overlay (match-beginning 1)
  14741. 'org-image-overlay))
  14742. (setq file (expand-file-name
  14743. (concat (or (match-string 3) "") (match-string 4))))
  14744. (when (file-exists-p file)
  14745. (if (and (car-safe old) refresh)
  14746. (image-refresh (overlay-get (cdr old) 'display))
  14747. (setq img (create-image file))
  14748. (when img
  14749. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14750. (overlay-put ov 'display img)
  14751. (overlay-put ov 'face 'default)
  14752. (overlay-put ov 'org-image-overlay t)
  14753. (overlay-put ov 'modification-hooks
  14754. (list 'org-display-inline-modification-hook))
  14755. (push ov org-inline-image-overlays)))))))))
  14756. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  14757. "Remove inline-display overlay if a corresponding region is modified."
  14758. (let ((inhibit-modification-hooks t))
  14759. (when (and ov after)
  14760. (delete ov org-inline-image-overlays)
  14761. (delete-overlay ov))))
  14762. (defun org-remove-inline-images ()
  14763. "Remove inline display of images."
  14764. (interactive)
  14765. (mapc 'delete-overlay org-inline-image-overlays)
  14766. (setq org-inline-image-overlays nil))
  14767. ;;;; Key bindings
  14768. ;; Make `C-c C-x' a prefix key
  14769. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14770. ;; TAB key with modifiers
  14771. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14772. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14773. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14774. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14775. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14776. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14777. ;; The following line is necessary under Suse GNU/Linux
  14778. (unless (featurep 'xemacs)
  14779. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14780. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14781. (define-key org-mode-map [backtab] 'org-shifttab)
  14782. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14783. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14784. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14785. ;; Cursor keys with modifiers
  14786. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14787. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14788. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14789. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14790. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14791. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14792. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14793. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14794. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14795. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14796. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14797. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14798. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14799. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14800. ;; Babel keys
  14801. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  14802. (mapc (lambda (pair)
  14803. (define-key org-babel-map (car pair) (cdr pair)))
  14804. org-babel-key-bindings)
  14805. ;;; Extra keys for tty access.
  14806. ;; We only set them when really needed because otherwise the
  14807. ;; menus don't show the simple keys
  14808. (when (or org-use-extra-keys
  14809. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14810. (not window-system))
  14811. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14812. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14813. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14814. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14815. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14816. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14817. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14818. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14819. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14820. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14821. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14822. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14823. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14824. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14825. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14826. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14827. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14828. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14829. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14830. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14831. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14832. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14833. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14834. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14835. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14836. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14837. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14838. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14839. ;; All the other keys
  14840. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14841. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14842. (if (boundp 'narrow-map)
  14843. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14844. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14845. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14846. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14847. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14848. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14849. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14850. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14851. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14852. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14853. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14854. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14855. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14856. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14857. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14858. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14859. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14860. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14861. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14862. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14863. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14864. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14865. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14866. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14867. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14868. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14869. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14870. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14871. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14872. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14873. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14874. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14875. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14876. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14877. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14878. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14879. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14880. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14881. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14882. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14883. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14884. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14885. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14886. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14887. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14888. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14889. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14890. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14891. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14892. (org-defkey org-mode-map "\C-m" 'org-return)
  14893. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14894. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14895. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14896. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14897. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14898. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14899. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14900. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14901. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14902. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14903. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14904. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14905. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14906. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14907. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14908. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14909. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14910. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14911. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14912. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14913. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14914. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14915. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14916. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14917. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14918. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14919. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14920. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14921. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14922. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14923. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14924. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14925. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14926. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14927. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14928. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  14929. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14930. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14931. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14932. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14933. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14934. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14935. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14936. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14937. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14938. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14939. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14940. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14941. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14942. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14943. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14944. (when (featurep 'xemacs)
  14945. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14946. (defconst org-speed-commands-default
  14947. '(
  14948. ("Outline Navigation")
  14949. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14950. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14951. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14952. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14953. ("u" . (org-speed-move-safe 'outline-up-heading))
  14954. ("j" . org-goto)
  14955. ("g" . (org-refile t))
  14956. ("Outline Visibility")
  14957. ("c" . org-cycle)
  14958. ("C" . org-shifttab)
  14959. (" " . org-display-outline-path)
  14960. ("Outline Structure Editing")
  14961. ("U" . org-shiftmetaup)
  14962. ("D" . org-shiftmetadown)
  14963. ("r" . org-metaright)
  14964. ("l" . org-metaleft)
  14965. ("R" . org-shiftmetaright)
  14966. ("L" . org-shiftmetaleft)
  14967. ("i" . (progn (forward-char 1) (call-interactively
  14968. 'org-insert-heading-respect-content)))
  14969. ("^" . org-sort)
  14970. ("w" . org-refile)
  14971. ("a" . org-archive-subtree-default-with-confirmation)
  14972. ("." . outline-mark-subtree)
  14973. ("Clock Commands")
  14974. ("I" . org-clock-in)
  14975. ("O" . org-clock-out)
  14976. ("Meta Data Editing")
  14977. ("t" . org-todo)
  14978. ("0" . (org-priority ?\ ))
  14979. ("1" . (org-priority ?A))
  14980. ("2" . (org-priority ?B))
  14981. ("3" . (org-priority ?C))
  14982. (";" . org-set-tags-command)
  14983. ("e" . org-set-effort)
  14984. ("Agenda Views etc")
  14985. ("v" . org-agenda)
  14986. ("/" . org-sparse-tree)
  14987. ("Misc")
  14988. ("o" . org-open-at-point)
  14989. ("?" . org-speed-command-help)
  14990. ("<" . (org-agenda-set-restriction-lock 'subtree))
  14991. (">" . (org-agenda-remove-restriction-lock))
  14992. )
  14993. "The default speed commands.")
  14994. (defun org-print-speed-command (e)
  14995. (if (> (length (car e)) 1)
  14996. (progn
  14997. (princ "\n")
  14998. (princ (car e))
  14999. (princ "\n")
  15000. (princ (make-string (length (car e)) ?-))
  15001. (princ "\n"))
  15002. (princ (car e))
  15003. (princ " ")
  15004. (if (symbolp (cdr e))
  15005. (princ (symbol-name (cdr e)))
  15006. (prin1 (cdr e)))
  15007. (princ "\n")))
  15008. (defun org-speed-command-help ()
  15009. "Show the available speed commands."
  15010. (interactive)
  15011. (if (not org-use-speed-commands)
  15012. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15013. (with-output-to-temp-buffer "*Help*"
  15014. (princ "User-defined Speed commands\n===========================\n")
  15015. (mapc 'org-print-speed-command org-speed-commands-user)
  15016. (princ "\n")
  15017. (princ "Built-in Speed commands\n=======================\n")
  15018. (mapc 'org-print-speed-command org-speed-commands-default))
  15019. (with-current-buffer "*Help*"
  15020. (setq truncate-lines t))))
  15021. (defun org-speed-move-safe (cmd)
  15022. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15023. If not, return to the original position and throw an error."
  15024. (interactive)
  15025. (let ((pos (point)))
  15026. (call-interactively cmd)
  15027. (unless (and (bolp) (org-on-heading-p))
  15028. (goto-char pos)
  15029. (error "Boundary reached while executing %s" cmd))))
  15030. (defvar org-self-insert-command-undo-counter 0)
  15031. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15032. (defvar org-speed-command nil)
  15033. (defun org-self-insert-command (N)
  15034. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15035. If the cursor is in a table looking at whitespace, the whitespace is
  15036. overwritten, and the table is not marked as requiring realignment."
  15037. (interactive "p")
  15038. (cond
  15039. ((and org-use-speed-commands
  15040. (or (and (bolp) (looking-at outline-regexp))
  15041. (and (functionp org-use-speed-commands)
  15042. (funcall org-use-speed-commands)))
  15043. (setq
  15044. org-speed-command
  15045. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  15046. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  15047. (cond
  15048. ((commandp org-speed-command)
  15049. (setq this-command org-speed-command)
  15050. (call-interactively org-speed-command))
  15051. ((functionp org-speed-command)
  15052. (funcall org-speed-command))
  15053. ((and org-speed-command (listp org-speed-command))
  15054. (eval org-speed-command))
  15055. (t (let (org-use-speed-commands)
  15056. (call-interactively 'org-self-insert-command)))))
  15057. ((and
  15058. (org-table-p)
  15059. (progn
  15060. ;; check if we blank the field, and if that triggers align
  15061. (and (featurep 'org-table) org-table-auto-blank-field
  15062. (member last-command
  15063. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15064. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15065. ;; got extra space, this field does not determine column width
  15066. (let (org-table-may-need-update) (org-table-blank-field))
  15067. ;; no extra space, this field may determine column width
  15068. (org-table-blank-field)))
  15069. t)
  15070. (eq N 1)
  15071. (looking-at "[^|\n]* |"))
  15072. (let (org-table-may-need-update)
  15073. (goto-char (1- (match-end 0)))
  15074. (delete-backward-char 1)
  15075. (goto-char (match-beginning 0))
  15076. (self-insert-command N)))
  15077. (t
  15078. (setq org-table-may-need-update t)
  15079. (self-insert-command N)
  15080. (org-fix-tags-on-the-fly)
  15081. (if org-self-insert-cluster-for-undo
  15082. (if (not (eq last-command 'org-self-insert-command))
  15083. (setq org-self-insert-command-undo-counter 1)
  15084. (if (>= org-self-insert-command-undo-counter 20)
  15085. (setq org-self-insert-command-undo-counter 1)
  15086. (and (> org-self-insert-command-undo-counter 0)
  15087. buffer-undo-list
  15088. (not (cadr buffer-undo-list)) ; remove nil entry
  15089. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15090. (setq org-self-insert-command-undo-counter
  15091. (1+ org-self-insert-command-undo-counter))))))))
  15092. (defun org-fix-tags-on-the-fly ()
  15093. (when (and (equal (char-after (point-at-bol)) ?*)
  15094. (org-on-heading-p))
  15095. (org-align-tags-here org-tags-column)))
  15096. (defun org-delete-backward-char (N)
  15097. "Like `delete-backward-char', insert whitespace at field end in tables.
  15098. When deleting backwards, in tables this function will insert whitespace in
  15099. front of the next \"|\" separator, to keep the table aligned. The table will
  15100. still be marked for re-alignment if the field did fill the entire column,
  15101. because, in this case the deletion might narrow the column."
  15102. (interactive "p")
  15103. (if (and (org-table-p)
  15104. (eq N 1)
  15105. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15106. (looking-at ".*?|"))
  15107. (let ((pos (point))
  15108. (noalign (looking-at "[^|\n\r]* |"))
  15109. (c org-table-may-need-update))
  15110. (backward-delete-char N)
  15111. (skip-chars-forward "^|")
  15112. (insert " ")
  15113. (goto-char (1- pos))
  15114. ;; noalign: if there were two spaces at the end, this field
  15115. ;; does not determine the width of the column.
  15116. (if noalign (setq org-table-may-need-update c)))
  15117. (backward-delete-char N)
  15118. (org-fix-tags-on-the-fly)))
  15119. (defun org-delete-char (N)
  15120. "Like `delete-char', but insert whitespace at field end in tables.
  15121. When deleting characters, in tables this function will insert whitespace in
  15122. front of the next \"|\" separator, to keep the table aligned. The table will
  15123. still be marked for re-alignment if the field did fill the entire column,
  15124. because, in this case the deletion might narrow the column."
  15125. (interactive "p")
  15126. (if (and (org-table-p)
  15127. (not (bolp))
  15128. (not (= (char-after) ?|))
  15129. (eq N 1))
  15130. (if (looking-at ".*?|")
  15131. (let ((pos (point))
  15132. (noalign (looking-at "[^|\n\r]* |"))
  15133. (c org-table-may-need-update))
  15134. (replace-match (concat
  15135. (substring (match-string 0) 1 -1)
  15136. " |"))
  15137. (goto-char pos)
  15138. ;; noalign: if there were two spaces at the end, this field
  15139. ;; does not determine the width of the column.
  15140. (if noalign (setq org-table-may-need-update c)))
  15141. (delete-char N))
  15142. (delete-char N)
  15143. (org-fix-tags-on-the-fly)))
  15144. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15145. (put 'org-self-insert-command 'delete-selection t)
  15146. (put 'orgtbl-self-insert-command 'delete-selection t)
  15147. (put 'org-delete-char 'delete-selection 'supersede)
  15148. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15149. (put 'org-yank 'delete-selection 'yank)
  15150. ;; Make `flyspell-mode' delay after some commands
  15151. (put 'org-self-insert-command 'flyspell-delayed t)
  15152. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15153. (put 'org-delete-char 'flyspell-delayed t)
  15154. (put 'org-delete-backward-char 'flyspell-delayed t)
  15155. ;; Make pabbrev-mode expand after org-mode commands
  15156. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15157. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15158. ;; How to do this: Measure non-white length of current string
  15159. ;; If equal to column width, we should realign.
  15160. (defun org-remap (map &rest commands)
  15161. "In MAP, remap the functions given in COMMANDS.
  15162. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15163. (let (new old)
  15164. (while commands
  15165. (setq old (pop commands) new (pop commands))
  15166. (if (fboundp 'command-remapping)
  15167. (org-defkey map (vector 'remap old) new)
  15168. (substitute-key-definition old new map global-map)))))
  15169. (when (eq org-enable-table-editor 'optimized)
  15170. ;; If the user wants maximum table support, we need to hijack
  15171. ;; some standard editing functions
  15172. (org-remap org-mode-map
  15173. 'self-insert-command 'org-self-insert-command
  15174. 'delete-char 'org-delete-char
  15175. 'delete-backward-char 'org-delete-backward-char)
  15176. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15177. (defvar org-ctrl-c-ctrl-c-hook nil
  15178. "Hook for functions attaching themselves to `C-c C-c'.
  15179. This can be used to add additional functionality to the C-c C-c key which
  15180. executes context-dependent commands.
  15181. Each function will be called with no arguments. The function must check
  15182. if the context is appropriate for it to act. If yes, it should do its
  15183. thing and then return a non-nil value. If the context is wrong,
  15184. just do nothing and return nil.")
  15185. (defvar org-tab-first-hook nil
  15186. "Hook for functions to attach themselves to TAB.
  15187. See `org-ctrl-c-ctrl-c-hook' for more information.
  15188. This hook runs as the first action when TAB is pressed, even before
  15189. `org-cycle' messes around with the `outline-regexp' to cater for
  15190. inline tasks and plain list item folding.
  15191. If any function in this hook returns t, any other actions that
  15192. would have been caused by TAB (such as table field motion or visibility
  15193. cycling) will not occur.")
  15194. (defvar org-tab-after-check-for-table-hook nil
  15195. "Hook for functions to attach themselves to TAB.
  15196. See `org-ctrl-c-ctrl-c-hook' for more information.
  15197. This hook runs after it has been established that the cursor is not in a
  15198. table, but before checking if the cursor is in a headline or if global cycling
  15199. should be done.
  15200. If any function in this hook returns t, not other actions like visibility
  15201. cycling will be done.")
  15202. (defvar org-tab-after-check-for-cycling-hook nil
  15203. "Hook for functions to attach themselves to TAB.
  15204. See `org-ctrl-c-ctrl-c-hook' for more information.
  15205. This hook runs after it has been established that not table field motion and
  15206. not visibility should be done because of current context. This is probably
  15207. the place where a package like yasnippets can hook in.")
  15208. (defvar org-tab-before-tab-emulation-hook nil
  15209. "Hook for functions to attach themselves to TAB.
  15210. See `org-ctrl-c-ctrl-c-hook' for more information.
  15211. This hook runs after every other options for TAB have been exhausted, but
  15212. before indentation and \t insertion takes place.")
  15213. (defvar org-metaleft-hook nil
  15214. "Hook for functions attaching themselves to `M-left'.
  15215. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15216. (defvar org-metaright-hook nil
  15217. "Hook for functions attaching themselves to `M-right'.
  15218. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15219. (defvar org-metaup-hook nil
  15220. "Hook for functions attaching themselves to `M-up'.
  15221. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15222. (defvar org-metadown-hook nil
  15223. "Hook for functions attaching themselves to `M-down'.
  15224. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15225. (defvar org-shiftmetaleft-hook nil
  15226. "Hook for functions attaching themselves to `M-S-left'.
  15227. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15228. (defvar org-shiftmetaright-hook nil
  15229. "Hook for functions attaching themselves to `M-S-right'.
  15230. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15231. (defvar org-shiftmetaup-hook nil
  15232. "Hook for functions attaching themselves to `M-S-up'.
  15233. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15234. (defvar org-shiftmetadown-hook nil
  15235. "Hook for functions attaching themselves to `M-S-down'.
  15236. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15237. (defvar org-metareturn-hook nil
  15238. "Hook for functions attaching themselves to `M-RET'.
  15239. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15240. (defvar org-shiftup-hook nil
  15241. "Hook for functions attaching themselves to `S-up'.
  15242. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15243. (defvar org-shiftup-final-hook nil
  15244. "Hook for functions attaching themselves to `S-up'.
  15245. This one runs after all other options except shift-select have been excluded.
  15246. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15247. (defvar org-shiftdown-hook nil
  15248. "Hook for functions attaching themselves to `S-down'.
  15249. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15250. (defvar org-shiftdown-final-hook nil
  15251. "Hook for functions attaching themselves to `S-down'.
  15252. This one runs after all other options except shift-select have been excluded.
  15253. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15254. (defvar org-shiftleft-hook nil
  15255. "Hook for functions attaching themselves to `S-left'.
  15256. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15257. (defvar org-shiftleft-final-hook nil
  15258. "Hook for functions attaching themselves to `S-left'.
  15259. This one runs after all other options except shift-select have been excluded.
  15260. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15261. (defvar org-shiftright-hook nil
  15262. "Hook for functions attaching themselves to `S-right'.
  15263. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15264. (defvar org-shiftright-final-hook nil
  15265. "Hook for functions attaching themselves to `S-right'.
  15266. This one runs after all other options except shift-select have been excluded.
  15267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15268. (defun org-modifier-cursor-error ()
  15269. "Throw an error, a modified cursor command was applied in wrong context."
  15270. (error "This command is active in special context like tables, headlines or items"))
  15271. (defun org-shiftselect-error ()
  15272. "Throw an error because Shift-Cursor command was applied in wrong context."
  15273. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15274. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15275. (error "This command works only in special context like headlines or timestamps")))
  15276. (defun org-call-for-shift-select (cmd)
  15277. (let ((this-command-keys-shift-translated t))
  15278. (call-interactively cmd)))
  15279. (defun org-shifttab (&optional arg)
  15280. "Global visibility cycling or move to previous table field.
  15281. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15282. on context.
  15283. See the individual commands for more information."
  15284. (interactive "P")
  15285. (cond
  15286. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15287. ((integerp arg)
  15288. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15289. (message "Content view to level: %d" arg)
  15290. (org-content (prefix-numeric-value arg2))
  15291. (setq org-cycle-global-status 'overview)))
  15292. (t (call-interactively 'org-global-cycle))))
  15293. (defun org-shiftmetaleft ()
  15294. "Promote subtree or delete table column.
  15295. Calls `org-promote-subtree', `org-outdent-item',
  15296. or `org-table-delete-column', depending on context.
  15297. See the individual commands for more information."
  15298. (interactive)
  15299. (cond
  15300. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15301. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15302. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15303. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15304. (t (org-modifier-cursor-error))))
  15305. (defun org-shiftmetaright ()
  15306. "Demote subtree or insert table column.
  15307. Calls `org-demote-subtree', `org-indent-item',
  15308. or `org-table-insert-column', depending on context.
  15309. See the individual commands for more information."
  15310. (interactive)
  15311. (cond
  15312. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15313. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15314. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15315. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15316. (t (org-modifier-cursor-error))))
  15317. (defun org-shiftmetaup (&optional arg)
  15318. "Move subtree up or kill table row.
  15319. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15320. `org-move-item-up' depending on context. See the individual commands
  15321. for more information."
  15322. (interactive "P")
  15323. (cond
  15324. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15325. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15326. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15327. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15328. (t (org-modifier-cursor-error))))
  15329. (defun org-shiftmetadown (&optional arg)
  15330. "Move subtree down or insert table row.
  15331. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15332. `org-move-item-down', depending on context. See the individual
  15333. commands for more information."
  15334. (interactive "P")
  15335. (cond
  15336. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15337. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15338. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15339. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15340. (t (org-modifier-cursor-error))))
  15341. (defsubst org-hidden-tree-error ()
  15342. (error
  15343. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15344. (defun org-metaleft (&optional arg)
  15345. "Promote heading or move table column to left.
  15346. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15347. With no specific context, calls the Emacs default `backward-word'.
  15348. See the individual commands for more information."
  15349. (interactive "P")
  15350. (cond
  15351. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15352. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15353. ((or (org-on-heading-p)
  15354. (and (org-region-active-p)
  15355. (save-excursion
  15356. (goto-char (region-beginning))
  15357. (org-on-heading-p))))
  15358. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15359. (call-interactively 'org-do-promote))
  15360. ((or (org-at-item-p)
  15361. (and (org-region-active-p)
  15362. (save-excursion
  15363. (goto-char (region-beginning))
  15364. (org-at-item-p))))
  15365. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15366. (call-interactively 'org-outdent-item))
  15367. (t (call-interactively 'backward-word))))
  15368. (defun org-metaright (&optional arg)
  15369. "Demote subtree or move table column to right.
  15370. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15371. With no specific context, calls the Emacs default `forward-word'.
  15372. See the individual commands for more information."
  15373. (interactive "P")
  15374. (cond
  15375. ((run-hook-with-args-until-success 'org-metaright-hook))
  15376. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15377. ((or (org-on-heading-p)
  15378. (and (org-region-active-p)
  15379. (save-excursion
  15380. (goto-char (region-beginning))
  15381. (org-on-heading-p))))
  15382. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15383. (call-interactively 'org-do-demote))
  15384. ((or (org-at-item-p)
  15385. (and (org-region-active-p)
  15386. (save-excursion
  15387. (goto-char (region-beginning))
  15388. (org-at-item-p))))
  15389. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15390. (call-interactively 'org-indent-item))
  15391. (t (call-interactively 'forward-word))))
  15392. (defun org-check-for-hidden (what)
  15393. "Check if there are hidden headlines/items in the current visual line.
  15394. WHAT can be either `headlines' or `items'. If the current line is
  15395. an outline or item heading and it has a folded subtree below it,
  15396. this function returns t, nil otherwise."
  15397. (let ((re (cond
  15398. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15399. ((eq what 'items) (concat "^" (org-item-re t)))
  15400. (t (error "This should not happen"))))
  15401. beg end)
  15402. (save-excursion
  15403. (catch 'exit
  15404. (unless (org-region-active-p)
  15405. (setq beg (point-at-bol))
  15406. (beginning-of-line 2)
  15407. (while (and (not (eobp)) ;; this is like `next-line'
  15408. (get-char-property (1- (point)) 'invisible))
  15409. (beginning-of-line 2))
  15410. (setq end (point))
  15411. (goto-char beg)
  15412. (goto-char (point-at-eol))
  15413. (setq end (max end (point)))
  15414. (while (re-search-forward re end t)
  15415. (if (get-char-property (match-beginning 0) 'invisible)
  15416. (throw 'exit t))))
  15417. nil))))
  15418. (defun org-metaup (&optional arg)
  15419. "Move subtree up or move table row up.
  15420. Calls `org-move-subtree-up' or `org-table-move-row' or
  15421. `org-move-item-up', depending on context. See the individual commands
  15422. for more information."
  15423. (interactive "P")
  15424. (cond
  15425. ((run-hook-with-args-until-success 'org-metaup-hook))
  15426. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15427. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15428. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15429. (t (transpose-lines 1) (beginning-of-line -1))))
  15430. (defun org-metadown (&optional arg)
  15431. "Move subtree down or move table row down.
  15432. Calls `org-move-subtree-down' or `org-table-move-row' or
  15433. `org-move-item-down', depending on context. See the individual
  15434. commands for more information."
  15435. (interactive "P")
  15436. (cond
  15437. ((run-hook-with-args-until-success 'org-metadown-hook))
  15438. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15439. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15440. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15441. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15442. (defun org-shiftup (&optional arg)
  15443. "Increase item in timestamp or increase priority of current headline.
  15444. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15445. depending on context. See the individual commands for more information."
  15446. (interactive "P")
  15447. (cond
  15448. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15449. ((and org-support-shift-select (org-region-active-p))
  15450. (org-call-for-shift-select 'previous-line))
  15451. ((org-at-timestamp-p t)
  15452. (call-interactively (if org-edit-timestamp-down-means-later
  15453. 'org-timestamp-down 'org-timestamp-up)))
  15454. ((and (not (eq org-support-shift-select 'always))
  15455. org-enable-priority-commands
  15456. (org-on-heading-p))
  15457. (call-interactively 'org-priority-up))
  15458. ((and (not org-support-shift-select) (org-at-item-p))
  15459. (call-interactively 'org-previous-item))
  15460. ((org-clocktable-try-shift 'up arg))
  15461. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15462. (org-support-shift-select
  15463. (org-call-for-shift-select 'previous-line))
  15464. (t (org-shiftselect-error))))
  15465. (defun org-shiftdown (&optional arg)
  15466. "Decrease item in timestamp or decrease priority of current headline.
  15467. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15468. depending on context. See the individual commands for more information."
  15469. (interactive "P")
  15470. (cond
  15471. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15472. ((and org-support-shift-select (org-region-active-p))
  15473. (org-call-for-shift-select 'next-line))
  15474. ((org-at-timestamp-p t)
  15475. (call-interactively (if org-edit-timestamp-down-means-later
  15476. 'org-timestamp-up 'org-timestamp-down)))
  15477. ((and (not (eq org-support-shift-select 'always))
  15478. org-enable-priority-commands
  15479. (org-on-heading-p))
  15480. (call-interactively 'org-priority-down))
  15481. ((and (not org-support-shift-select) (org-at-item-p))
  15482. (call-interactively 'org-next-item))
  15483. ((org-clocktable-try-shift 'down arg))
  15484. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15485. (org-support-shift-select
  15486. (org-call-for-shift-select 'next-line))
  15487. (t (org-shiftselect-error))))
  15488. (defun org-shiftright (&optional arg)
  15489. "Cycle the thing at point or in the current line, depending on context.
  15490. Depending on context, this does one of the following:
  15491. - switch a timestamp at point one day into the future
  15492. - on a headline, switch to the next TODO keyword.
  15493. - on an item, switch entire list to the next bullet type
  15494. - on a property line, switch to the next allowed value
  15495. - on a clocktable definition line, move time block into the future"
  15496. (interactive "P")
  15497. (cond
  15498. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15499. ((and org-support-shift-select (org-region-active-p))
  15500. (org-call-for-shift-select 'forward-char))
  15501. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15502. ((and (not (eq org-support-shift-select 'always))
  15503. (org-on-heading-p))
  15504. (let ((org-inhibit-logging
  15505. (not org-treat-S-cursor-todo-selection-as-state-change))
  15506. (org-inhibit-blocking
  15507. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15508. (org-call-with-arg 'org-todo 'right)))
  15509. ((or (and org-support-shift-select
  15510. (not (eq org-support-shift-select 'always))
  15511. (org-at-item-bullet-p))
  15512. (and (not org-support-shift-select) (org-at-item-p)))
  15513. (org-call-with-arg 'org-cycle-list-bullet nil))
  15514. ((and (not (eq org-support-shift-select 'always))
  15515. (org-at-property-p))
  15516. (call-interactively 'org-property-next-allowed-value))
  15517. ((org-clocktable-try-shift 'right arg))
  15518. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15519. (org-support-shift-select
  15520. (org-call-for-shift-select 'forward-char))
  15521. (t (org-shiftselect-error))))
  15522. (defun org-shiftleft (&optional arg)
  15523. "Cycle the thing at point or in the current line, depending on context.
  15524. Depending on context, this does one of the following:
  15525. - switch a timestamp at point one day into the past
  15526. - on a headline, switch to the previous TODO keyword.
  15527. - on an item, switch entire list to the previous bullet type
  15528. - on a property line, switch to the previous allowed value
  15529. - on a clocktable definition line, move time block into the past"
  15530. (interactive "P")
  15531. (cond
  15532. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15533. ((and org-support-shift-select (org-region-active-p))
  15534. (org-call-for-shift-select 'backward-char))
  15535. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15536. ((and (not (eq org-support-shift-select 'always))
  15537. (org-on-heading-p))
  15538. (let ((org-inhibit-logging
  15539. (not org-treat-S-cursor-todo-selection-as-state-change))
  15540. (org-inhibit-blocking
  15541. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15542. (org-call-with-arg 'org-todo 'left)))
  15543. ((or (and org-support-shift-select
  15544. (not (eq org-support-shift-select 'always))
  15545. (org-at-item-bullet-p))
  15546. (and (not org-support-shift-select) (org-at-item-p)))
  15547. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15548. ((and (not (eq org-support-shift-select 'always))
  15549. (org-at-property-p))
  15550. (call-interactively 'org-property-previous-allowed-value))
  15551. ((org-clocktable-try-shift 'left arg))
  15552. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15553. (org-support-shift-select
  15554. (org-call-for-shift-select 'backward-char))
  15555. (t (org-shiftselect-error))))
  15556. (defun org-shiftcontrolright ()
  15557. "Switch to next TODO set."
  15558. (interactive)
  15559. (cond
  15560. ((and org-support-shift-select (org-region-active-p))
  15561. (org-call-for-shift-select 'forward-word))
  15562. ((and (not (eq org-support-shift-select 'always))
  15563. (org-on-heading-p))
  15564. (org-call-with-arg 'org-todo 'nextset))
  15565. (org-support-shift-select
  15566. (org-call-for-shift-select 'forward-word))
  15567. (t (org-shiftselect-error))))
  15568. (defun org-shiftcontrolleft ()
  15569. "Switch to previous TODO set."
  15570. (interactive)
  15571. (cond
  15572. ((and org-support-shift-select (org-region-active-p))
  15573. (org-call-for-shift-select 'backward-word))
  15574. ((and (not (eq org-support-shift-select 'always))
  15575. (org-on-heading-p))
  15576. (org-call-with-arg 'org-todo 'previousset))
  15577. (org-support-shift-select
  15578. (org-call-for-shift-select 'backward-word))
  15579. (t (org-shiftselect-error))))
  15580. (defun org-ctrl-c-ret ()
  15581. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15582. (interactive)
  15583. (cond
  15584. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15585. (t (call-interactively 'org-insert-heading))))
  15586. (defun org-copy-special ()
  15587. "Copy region in table or copy current subtree.
  15588. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15589. See the individual commands for more information."
  15590. (interactive)
  15591. (call-interactively
  15592. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15593. (defun org-cut-special ()
  15594. "Cut region in table or cut current subtree.
  15595. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15596. See the individual commands for more information."
  15597. (interactive)
  15598. (call-interactively
  15599. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15600. (defun org-paste-special (arg)
  15601. "Paste rectangular region into table, or past subtree relative to level.
  15602. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15603. See the individual commands for more information."
  15604. (interactive "P")
  15605. (if (org-at-table-p)
  15606. (org-table-paste-rectangle)
  15607. (org-paste-subtree arg)))
  15608. (defun org-edit-special (&optional arg)
  15609. "Call a special editor for the stuff at point.
  15610. When at a table, call the formula editor with `org-table-edit-formulas'.
  15611. When at the first line of an src example, call `org-edit-src-code'.
  15612. When in an #+include line, visit the include file. Otherwise call
  15613. `ffap' to visit the file at point."
  15614. (interactive)
  15615. ;; possibly prep session before editing source
  15616. (when arg
  15617. (let* ((info (org-babel-get-src-block-info))
  15618. (lang (nth 0 info))
  15619. (params (nth 2 info))
  15620. (session (cdr (assoc :session params))))
  15621. (when (and info session) ;; we are in a source-code block with a session
  15622. (funcall
  15623. (intern (concat "org-babel-prep-session:" lang)) session params))))
  15624. (cond ;; proceed with `org-edit-special'
  15625. ((save-excursion
  15626. (beginning-of-line 1)
  15627. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15628. (find-file (org-trim (match-string 1))))
  15629. ((org-edit-src-code))
  15630. ((org-edit-fixed-width-region))
  15631. ((org-at-table.el-p)
  15632. (org-edit-src-code))
  15633. ((org-at-table-p)
  15634. (call-interactively 'org-table-edit-formulas))
  15635. (t (call-interactively 'ffap))))
  15636. (defun org-ctrl-c-ctrl-c (&optional arg)
  15637. "Set tags in headline, or update according to changed information at point.
  15638. This command does many different things, depending on context:
  15639. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15640. this is what we do.
  15641. - If the cursor is on a statistics cookie, update it.
  15642. - If the cursor is in a headline, prompt for tags and insert them
  15643. into the current line, aligned to `org-tags-column'. When called
  15644. with prefix arg, realign all tags in the current buffer.
  15645. - If the cursor is in one of the special #+KEYWORD lines, this
  15646. triggers scanning the buffer for these lines and updating the
  15647. information.
  15648. - If the cursor is inside a table, realign the table. This command
  15649. works even if the automatic table editor has been turned off.
  15650. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15651. the entire table.
  15652. - If the cursor is at a footnote reference or definition, jump to
  15653. the corresponding definition or references, respectively.
  15654. - If the cursor is a the beginning of a dynamic block, update it.
  15655. - If the current buffer is a remember buffer, close note and file
  15656. it. A prefix argument of 1 files to the default location
  15657. without further interaction. A prefix argument of 2 files to
  15658. the currently clocking task.
  15659. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15660. links in this buffer.
  15661. - If the cursor is on a numbered item in a plain list, renumber the
  15662. ordered list.
  15663. - If the cursor is on a checkbox, toggle it.
  15664. - If the cursor is on a code block, evaluate it. The variable
  15665. `org-confirm-babel-evaluate' can be used to control prompting
  15666. before code block evaluation, by default every code block
  15667. evaluation requires confirmation. Code block evaluation can be
  15668. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  15669. (interactive "P")
  15670. (let ((org-enable-table-editor t))
  15671. (cond
  15672. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15673. org-occur-highlights
  15674. org-latex-fragment-image-overlays)
  15675. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15676. (org-remove-occur-highlights)
  15677. (org-remove-latex-fragment-image-overlays)
  15678. (message "Temporary highlights/overlays removed from current buffer"))
  15679. ((and (local-variable-p 'org-finish-function (current-buffer))
  15680. (fboundp org-finish-function))
  15681. (funcall org-finish-function))
  15682. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15683. ((or (looking-at org-property-start-re)
  15684. (org-at-property-p))
  15685. (call-interactively 'org-property-action))
  15686. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15687. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15688. (or (org-on-heading-p) (org-at-item-p)))
  15689. (call-interactively 'org-update-statistics-cookies))
  15690. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15691. ((org-at-table.el-p)
  15692. (message "Use C-c ' to edit table.el tables"))
  15693. ((org-at-table-p)
  15694. (org-table-maybe-eval-formula)
  15695. (if arg
  15696. (call-interactively 'org-table-recalculate)
  15697. (org-table-maybe-recalculate-line))
  15698. (call-interactively 'org-table-align))
  15699. ((or (org-footnote-at-reference-p)
  15700. (org-footnote-at-definition-p))
  15701. (call-interactively 'org-footnote-action))
  15702. ((org-at-item-checkbox-p)
  15703. (call-interactively 'org-toggle-checkbox)
  15704. (org-list-send-list 'maybe))
  15705. ((org-at-item-p)
  15706. (if arg
  15707. (call-interactively 'org-toggle-checkbox)
  15708. (call-interactively 'org-maybe-renumber-ordered-list))
  15709. (org-list-send-list 'maybe))
  15710. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15711. ;; Dynamic block
  15712. (beginning-of-line 1)
  15713. (save-excursion (org-update-dblock)))
  15714. ((save-excursion
  15715. (beginning-of-line 1)
  15716. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15717. (cond
  15718. ((equal (match-string 1) "TBLFM")
  15719. ;; Recalculate the table before this line
  15720. (save-excursion
  15721. (beginning-of-line 1)
  15722. (skip-chars-backward " \r\n\t")
  15723. (if (org-at-table-p)
  15724. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15725. (t
  15726. (let ((org-inhibit-startup-visibility-stuff t)
  15727. (org-startup-align-all-tables nil))
  15728. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15729. (message "Local setup has been refreshed"))))
  15730. ((org-clock-update-time-maybe))
  15731. (t (error "C-c C-c can do nothing useful at this location")))))
  15732. (defun org-mode-restart ()
  15733. "Restart Org-mode, to scan again for special lines.
  15734. Also updates the keyword regular expressions."
  15735. (interactive)
  15736. (org-mode)
  15737. (message "Org-mode restarted"))
  15738. (defun org-kill-note-or-show-branches ()
  15739. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15740. (interactive)
  15741. (if (not org-finish-function)
  15742. (progn
  15743. (hide-subtree)
  15744. (call-interactively 'show-branches))
  15745. (let ((org-note-abort t))
  15746. (funcall org-finish-function))))
  15747. (defun org-return (&optional indent)
  15748. "Goto next table row or insert a newline.
  15749. Calls `org-table-next-row' or `newline', depending on context.
  15750. See the individual commands for more information."
  15751. (interactive)
  15752. (cond
  15753. ((bobp) (if indent (newline-and-indent) (newline)))
  15754. ((org-at-table-p)
  15755. (org-table-justify-field-maybe)
  15756. (call-interactively 'org-table-next-row))
  15757. ((and org-return-follows-link
  15758. (eq (get-text-property (point) 'face) 'org-link))
  15759. (call-interactively 'org-open-at-point))
  15760. ((and (org-at-heading-p)
  15761. (looking-at
  15762. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15763. (org-show-entry)
  15764. (end-of-line 1)
  15765. (newline))
  15766. (t (if indent (newline-and-indent) (newline)))))
  15767. (defun org-return-indent ()
  15768. "Goto next table row or insert a newline and indent.
  15769. Calls `org-table-next-row' or `newline-and-indent', depending on
  15770. context. See the individual commands for more information."
  15771. (interactive)
  15772. (org-return t))
  15773. (defun org-ctrl-c-star ()
  15774. "Compute table, or change heading status of lines.
  15775. Calls `org-table-recalculate' or `org-toggle-heading',
  15776. depending on context."
  15777. (interactive)
  15778. (cond
  15779. ((org-at-table-p)
  15780. (call-interactively 'org-table-recalculate))
  15781. (t
  15782. ;; Convert all lines in region to list items
  15783. (call-interactively 'org-toggle-heading))))
  15784. (defun org-ctrl-c-minus ()
  15785. "Insert separator line in table or modify bullet status of line.
  15786. Also turns a plain line or a region of lines into list items.
  15787. Calls `org-table-insert-hline', `org-toggle-item', or
  15788. `org-cycle-list-bullet', depending on context."
  15789. (interactive)
  15790. (cond
  15791. ((org-at-table-p)
  15792. (call-interactively 'org-table-insert-hline))
  15793. ((org-region-active-p)
  15794. (call-interactively 'org-toggle-item))
  15795. ((org-in-item-p)
  15796. (call-interactively 'org-cycle-list-bullet))
  15797. (t
  15798. (call-interactively 'org-toggle-item))))
  15799. (defun org-toggle-item ()
  15800. "Convert headings or normal lines to items, items to normal lines.
  15801. If there is no active region, only the current line is considered.
  15802. If the first line in the region is a headline, convert all headlines to items.
  15803. If the first line in the region is an item, convert all items to normal lines.
  15804. If the first line is normal text, add an item bullet to each line."
  15805. (interactive)
  15806. (let (l2 l beg end)
  15807. (if (org-region-active-p)
  15808. (setq beg (region-beginning) end (region-end))
  15809. (setq beg (point-at-bol)
  15810. end (min (1+ (point-at-eol)) (point-max))))
  15811. (save-excursion
  15812. (goto-char end)
  15813. (setq l2 (org-current-line))
  15814. (goto-char beg)
  15815. (beginning-of-line 1)
  15816. (setq l (1- (org-current-line)))
  15817. (if (org-at-item-p)
  15818. ;; We already have items, de-itemize
  15819. (while (< (setq l (1+ l)) l2)
  15820. (when (org-at-item-p)
  15821. (goto-char (match-beginning 2))
  15822. (delete-region (match-beginning 2) (match-end 2))
  15823. (and (looking-at "[ \t]+") (replace-match "")))
  15824. (beginning-of-line 2))
  15825. (if (org-on-heading-p)
  15826. ;; Headings, convert to items
  15827. (while (< (setq l (1+ l)) l2)
  15828. (if (looking-at org-outline-regexp)
  15829. (replace-match "- " t t))
  15830. (beginning-of-line 2))
  15831. ;; normal lines, turn them into items
  15832. (while (< (setq l (1+ l)) l2)
  15833. (unless (org-at-item-p)
  15834. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15835. (replace-match "\\1- \\2")))
  15836. (beginning-of-line 2)))))))
  15837. (defun org-toggle-heading (&optional nstars)
  15838. "Convert headings to normal text, or items or text to headings.
  15839. If there is no active region, only the current line is considered.
  15840. If the first line is a heading, remove the stars from all headlines
  15841. in the region.
  15842. If the first line is a plain list item, turn all plain list items
  15843. into headings.
  15844. If the first line is a normal line, turn each and every line in the
  15845. region into a heading.
  15846. When converting a line into a heading, the number of stars is chosen
  15847. such that the lines become children of the current entry. However,
  15848. when a prefix argument is given, its value determines the number of
  15849. stars to add."
  15850. (interactive "P")
  15851. (let (l2 l itemp beg end)
  15852. (if (org-region-active-p)
  15853. (setq beg (region-beginning) end (region-end))
  15854. (setq beg (point-at-bol)
  15855. end (min (1+ (point-at-eol)) (point-max))))
  15856. (save-excursion
  15857. (goto-char end)
  15858. (setq l2 (org-current-line))
  15859. (goto-char beg)
  15860. (beginning-of-line 1)
  15861. (setq l (1- (org-current-line)))
  15862. (if (org-on-heading-p)
  15863. ;; We already have headlines, de-star them
  15864. (while (< (setq l (1+ l)) l2)
  15865. (when (org-on-heading-p t)
  15866. (and (looking-at outline-regexp) (replace-match "")))
  15867. (beginning-of-line 2))
  15868. (setq itemp (org-at-item-p))
  15869. (let* ((stars
  15870. (if nstars
  15871. (make-string (prefix-numeric-value current-prefix-arg)
  15872. ?*)
  15873. (save-excursion
  15874. (if (re-search-backward org-complex-heading-regexp nil t)
  15875. (match-string 1) ""))))
  15876. (add-stars (cond (nstars "")
  15877. ((equal stars "") "*")
  15878. (org-odd-levels-only "**")
  15879. (t "*")))
  15880. (rpl (concat stars add-stars " ")))
  15881. (while (< (setq l (1+ l)) l2)
  15882. (if itemp
  15883. (and (org-at-item-p) (replace-match rpl t t))
  15884. (unless (org-on-heading-p)
  15885. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15886. (replace-match (concat rpl (match-string 2))))))
  15887. (beginning-of-line 2)))))))
  15888. (defun org-meta-return (&optional arg)
  15889. "Insert a new heading or wrap a region in a table.
  15890. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15891. See the individual commands for more information."
  15892. (interactive "P")
  15893. (cond
  15894. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15895. ((org-at-table-p)
  15896. (call-interactively 'org-table-wrap-region))
  15897. (t (call-interactively 'org-insert-heading))))
  15898. ;;; Menu entries
  15899. ;; Define the Org-mode menus
  15900. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15901. '("Tbl"
  15902. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15903. ["Next Field" org-cycle (org-at-table-p)]
  15904. ["Previous Field" org-shifttab (org-at-table-p)]
  15905. ["Next Row" org-return (org-at-table-p)]
  15906. "--"
  15907. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15908. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15909. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15910. "--"
  15911. ("Column"
  15912. ["Move Column Left" org-metaleft (org-at-table-p)]
  15913. ["Move Column Right" org-metaright (org-at-table-p)]
  15914. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15915. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15916. ("Row"
  15917. ["Move Row Up" org-metaup (org-at-table-p)]
  15918. ["Move Row Down" org-metadown (org-at-table-p)]
  15919. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15920. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15921. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15922. "--"
  15923. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15924. ("Rectangle"
  15925. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15926. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15927. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15928. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15929. "--"
  15930. ("Calculate"
  15931. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15932. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15933. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15934. "--"
  15935. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15936. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15937. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15938. "--"
  15939. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15940. "--"
  15941. ["Sum Column/Rectangle" org-table-sum
  15942. (or (org-at-table-p) (org-region-active-p))]
  15943. ["Which Column?" org-table-current-column (org-at-table-p)])
  15944. ["Debug Formulas"
  15945. org-table-toggle-formula-debugger
  15946. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15947. ["Show Col/Row Numbers"
  15948. org-table-toggle-coordinate-overlays
  15949. :style toggle
  15950. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15951. "--"
  15952. ["Create" org-table-create (and (not (org-at-table-p))
  15953. org-enable-table-editor)]
  15954. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15955. ["Import from File" org-table-import (not (org-at-table-p))]
  15956. ["Export to File" org-table-export (org-at-table-p)]
  15957. "--"
  15958. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15959. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15960. '("Org"
  15961. ("Show/Hide"
  15962. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15963. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15964. ["Sparse Tree..." org-sparse-tree t]
  15965. ["Reveal Context" org-reveal t]
  15966. ["Show All" show-all t]
  15967. "--"
  15968. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15969. "--"
  15970. ["New Heading" org-insert-heading t]
  15971. ("Navigate Headings"
  15972. ["Up" outline-up-heading t]
  15973. ["Next" outline-next-visible-heading t]
  15974. ["Previous" outline-previous-visible-heading t]
  15975. ["Next Same Level" outline-forward-same-level t]
  15976. ["Previous Same Level" outline-backward-same-level t]
  15977. "--"
  15978. ["Jump" org-goto t])
  15979. ("Edit Structure"
  15980. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15981. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15982. "--"
  15983. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15984. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15985. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15986. "--"
  15987. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15988. "--"
  15989. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15990. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15991. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15992. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15993. "--"
  15994. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15995. "--"
  15996. ["Convert to odd levels" org-convert-to-odd-levels t]
  15997. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15998. ("Editing"
  15999. ["Emphasis..." org-emphasize t]
  16000. ["Edit Source Example" org-edit-special t]
  16001. "--"
  16002. ["Footnote new/jump" org-footnote-action t]
  16003. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16004. ("Archive"
  16005. ["Archive (default method)" org-archive-subtree-default t]
  16006. "--"
  16007. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16008. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16009. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16010. )
  16011. "--"
  16012. ("Hyperlinks"
  16013. ["Store Link (Global)" org-store-link t]
  16014. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16015. ["Insert Link" org-insert-link t]
  16016. ["Follow Link" org-open-at-point t]
  16017. "--"
  16018. ["Next link" org-next-link t]
  16019. ["Previous link" org-previous-link t]
  16020. "--"
  16021. ["Descriptive Links"
  16022. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16023. :style radio
  16024. :selected (member '(org-link) buffer-invisibility-spec)]
  16025. ["Literal Links"
  16026. (progn
  16027. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16028. :style radio
  16029. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16030. "--"
  16031. ("TODO Lists"
  16032. ["TODO/DONE/-" org-todo t]
  16033. ("Select keyword"
  16034. ["Next keyword" org-shiftright (org-on-heading-p)]
  16035. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16036. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  16037. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16038. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16039. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16040. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16041. "--"
  16042. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16043. :selected org-enforce-todo-dependencies :style toggle :active t]
  16044. "Settings for tree at point"
  16045. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16046. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  16047. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16048. ["Do Children parallel" org-toggle-ordered-property :style radio
  16049. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  16050. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16051. "--"
  16052. ["Set Priority" org-priority t]
  16053. ["Priority Up" org-shiftup t]
  16054. ["Priority Down" org-shiftdown t]
  16055. "--"
  16056. ["Get news from all feeds" org-feed-update-all t]
  16057. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16058. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16059. ("TAGS and Properties"
  16060. ["Set Tags" org-set-tags-command t]
  16061. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16062. "--"
  16063. ["Set property" org-set-property t]
  16064. ["Column view of properties" org-columns t]
  16065. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16066. ("Dates and Scheduling"
  16067. ["Timestamp" org-time-stamp t]
  16068. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16069. ("Change Date"
  16070. ["1 Day Later" org-shiftright t]
  16071. ["1 Day Earlier" org-shiftleft t]
  16072. ["1 ... Later" org-shiftup t]
  16073. ["1 ... Earlier" org-shiftdown t])
  16074. ["Compute Time Range" org-evaluate-time-range t]
  16075. ["Schedule Item" org-schedule t]
  16076. ["Deadline" org-deadline t]
  16077. "--"
  16078. ["Custom time format" org-toggle-time-stamp-overlays
  16079. :style radio :selected org-display-custom-times]
  16080. "--"
  16081. ["Goto Calendar" org-goto-calendar t]
  16082. ["Date from Calendar" org-date-from-calendar t]
  16083. "--"
  16084. ["Start/Restart Timer" org-timer-start t]
  16085. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16086. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16087. ["Insert Timer String" org-timer t]
  16088. ["Insert Timer Item" org-timer-item t])
  16089. ("Logging work"
  16090. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16091. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16092. ["Clock out" org-clock-out t]
  16093. ["Clock cancel" org-clock-cancel t]
  16094. "--"
  16095. ["Mark as default task" org-clock-mark-default-task t]
  16096. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16097. ["Goto running clock" org-clock-goto t]
  16098. "--"
  16099. ["Display times" org-clock-display t]
  16100. ["Create clock table" org-clock-report t]
  16101. "--"
  16102. ["Record DONE time"
  16103. (progn (setq org-log-done (not org-log-done))
  16104. (message "Switching to %s will %s record a timestamp"
  16105. (car org-done-keywords)
  16106. (if org-log-done "automatically" "not")))
  16107. :style toggle :selected org-log-done])
  16108. "--"
  16109. ["Agenda Command..." org-agenda t]
  16110. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16111. ("File List for Agenda")
  16112. ("Special views current file"
  16113. ["TODO Tree" org-show-todo-tree t]
  16114. ["Check Deadlines" org-check-deadlines t]
  16115. ["Timeline" org-timeline t]
  16116. ["Tags/Property tree" org-match-sparse-tree t])
  16117. "--"
  16118. ["Export/Publish..." org-export t]
  16119. ("LaTeX"
  16120. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16121. :selected org-cdlatex-mode]
  16122. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16123. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16124. ["Modify math symbol" org-cdlatex-math-modify
  16125. (org-inside-LaTeX-fragment-p)]
  16126. ["Insert citation" org-reftex-citation t]
  16127. "--"
  16128. ["Export LaTeX fragments as images"
  16129. (if (featurep 'org-exp)
  16130. (setq org-export-with-LaTeX-fragments
  16131. (not org-export-with-LaTeX-fragments))
  16132. (require 'org-exp))
  16133. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  16134. org-export-with-LaTeX-fragments)]
  16135. "--"
  16136. ["Template for BEAMER" org-insert-beamer-options-template t])
  16137. "--"
  16138. ("MobileOrg"
  16139. ["Push Files and Views" org-mobile-push t]
  16140. ["Get Captured and Flagged" org-mobile-pull t]
  16141. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16142. "--"
  16143. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16144. "--"
  16145. ("Documentation"
  16146. ["Show Version" org-version t]
  16147. ["Info Documentation" org-info t])
  16148. ("Customize"
  16149. ["Browse Org Group" org-customize t]
  16150. "--"
  16151. ["Expand This Menu" org-create-customize-menu
  16152. (fboundp 'customize-menu-create)])
  16153. ["Send bug report" org-submit-bug-report t]
  16154. "--"
  16155. ("Refresh/Reload"
  16156. ["Refresh setup current buffer" org-mode-restart t]
  16157. ["Reload Org (after update)" org-reload t]
  16158. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16159. ))
  16160. (defun org-info (&optional node)
  16161. "Read documentation for Org-mode in the info system.
  16162. With optional NODE, go directly to that node."
  16163. (interactive)
  16164. (info (format "(org)%s" (or node ""))))
  16165. ;;;###autoload
  16166. (defun org-submit-bug-report ()
  16167. "Submit a bug report on Org-mode via mail.
  16168. Don't hesitate to report any problems or inaccurate documentation.
  16169. If you don't have setup sending mail from (X)Emacs, please copy the
  16170. output buffer into your mail program, as it gives us important
  16171. information about your Org-mode version and configuration."
  16172. (interactive)
  16173. (require 'reporter)
  16174. (org-load-modules-maybe)
  16175. (org-require-autoloaded-modules)
  16176. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16177. (reporter-submit-bug-report
  16178. "emacs-orgmode@gnu.org"
  16179. (org-version)
  16180. (let (list)
  16181. (save-window-excursion
  16182. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16183. (delete-other-windows)
  16184. (erase-buffer)
  16185. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16186. We would like to add your full Org-mode and Outline configuration to the
  16187. bug report. This greatly simplifies the work of the maintainer and
  16188. other experts on the mailing list.
  16189. HOWEVER, some variables you have customized may contain private
  16190. information. The names of customers, colleagues, or friends, might
  16191. appear in the form of file names, tags, todo states, or search strings.
  16192. If you answer yes to the prompt, you might want to check and remove
  16193. such private information before sending the email.")
  16194. (add-text-properties (point-min) (point-max) '(face org-warning))
  16195. (when (yes-or-no-p "Include your Org-mode configuration ")
  16196. (mapatoms
  16197. (lambda (v)
  16198. (and (boundp v)
  16199. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16200. (or (and (symbol-value v)
  16201. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16202. (and
  16203. (get v 'custom-type) (get v 'standard-value)
  16204. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16205. (push v list)))))
  16206. (kill-buffer (get-buffer "*Warn about privacy*"))
  16207. list))
  16208. nil nil
  16209. "Remember to cover the basics, that is, what you expected to happen and
  16210. what in fact did happen. You don't know how to make a good report? See
  16211. http://orgmode.org/manual/Feedback.html#Feedback
  16212. Your bug report will be posted to the Org-mode mailing list.
  16213. ------------------------------------------------------------------------")
  16214. (save-excursion
  16215. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16216. (replace-match "\\1Bug: \\3 [\\2]")))))
  16217. (defun org-install-agenda-files-menu ()
  16218. (let ((bl (buffer-list)))
  16219. (save-excursion
  16220. (while bl
  16221. (set-buffer (pop bl))
  16222. (if (org-mode-p) (setq bl nil)))
  16223. (when (org-mode-p)
  16224. (easy-menu-change
  16225. '("Org") "File List for Agenda"
  16226. (append
  16227. (list
  16228. ["Edit File List" (org-edit-agenda-file-list) t]
  16229. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16230. ["Remove Current File from List" org-remove-file t]
  16231. ["Cycle through agenda files" org-cycle-agenda-files t]
  16232. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16233. "--")
  16234. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16235. ;;;; Documentation
  16236. ;;;###autoload
  16237. (defun org-require-autoloaded-modules ()
  16238. (interactive)
  16239. (mapc 'require
  16240. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16241. org-docbook org-exp org-html org-icalendar
  16242. org-id org-latex
  16243. org-publish org-remember org-table
  16244. org-timer org-xoxo)))
  16245. ;;;###autoload
  16246. (defun org-reload (&optional uncompiled)
  16247. "Reload all org lisp files.
  16248. With prefix arg UNCOMPILED, load the uncompiled versions."
  16249. (interactive "P")
  16250. (require 'find-func)
  16251. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16252. (dir-org (file-name-directory (org-find-library-name "org")))
  16253. (dir-org-contrib (ignore-errors
  16254. (file-name-directory
  16255. (org-find-library-name "org-contribdir"))))
  16256. (babel-files
  16257. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16258. (append (list nil "comint" "eval" "exp" "keys"
  16259. "lob" "ref" "table" "tangle")
  16260. (delq nil
  16261. (mapcar
  16262. (lambda (lang)
  16263. (when (cdr lang) (symbol-name (car lang))))
  16264. org-babel-load-languages)))))
  16265. (files
  16266. (append (directory-files dir-org t file-re)
  16267. babel-files
  16268. (and dir-org-contrib
  16269. (directory-files dir-org-contrib t file-re))))
  16270. (remove-re (concat (if (featurep 'xemacs)
  16271. "org-colview" "org-colview-xemacs")
  16272. "\\'")))
  16273. (setq files (mapcar 'file-name-sans-extension files))
  16274. (setq files (mapcar
  16275. (lambda (x) (if (string-match remove-re x) nil x))
  16276. files))
  16277. (setq files (delq nil files))
  16278. (mapc
  16279. (lambda (f)
  16280. (when (featurep (intern (file-name-nondirectory f)))
  16281. (if (and (not uncompiled)
  16282. (file-exists-p (concat f ".elc")))
  16283. (load (concat f ".elc") nil nil t)
  16284. (load (concat f ".el") nil nil t))))
  16285. files))
  16286. (org-version))
  16287. ;;;###autoload
  16288. (defun org-customize ()
  16289. "Call the customize function with org as argument."
  16290. (interactive)
  16291. (org-load-modules-maybe)
  16292. (org-require-autoloaded-modules)
  16293. (customize-browse 'org))
  16294. (defun org-create-customize-menu ()
  16295. "Create a full customization menu for Org-mode, insert it into the menu."
  16296. (interactive)
  16297. (org-load-modules-maybe)
  16298. (org-require-autoloaded-modules)
  16299. (if (fboundp 'customize-menu-create)
  16300. (progn
  16301. (easy-menu-change
  16302. '("Org") "Customize"
  16303. `(["Browse Org group" org-customize t]
  16304. "--"
  16305. ,(customize-menu-create 'org)
  16306. ["Set" Custom-set t]
  16307. ["Save" Custom-save t]
  16308. ["Reset to Current" Custom-reset-current t]
  16309. ["Reset to Saved" Custom-reset-saved t]
  16310. ["Reset to Standard Settings" Custom-reset-standard t]))
  16311. (message "\"Org\"-menu now contains full customization menu"))
  16312. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16313. ;;;; Miscellaneous stuff
  16314. ;;; Generally useful functions
  16315. (defun org-get-at-bol (property)
  16316. "Get text property PROPERTY at beginning of line."
  16317. (get-text-property (point-at-bol) property))
  16318. (defun org-find-text-property-in-string (prop s)
  16319. "Return the first non-nil value of property PROP in string S."
  16320. (or (get-text-property 0 prop s)
  16321. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16322. prop s)))
  16323. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16324. "Display the given MESSAGE as a warning."
  16325. (if (fboundp 'display-warning)
  16326. (display-warning 'org message
  16327. (if (featurep 'xemacs) 'warning :warning))
  16328. (let ((buf (get-buffer-create "*Org warnings*")))
  16329. (with-current-buffer buf
  16330. (goto-char (point-max))
  16331. (insert "Warning (Org): " message)
  16332. (unless (bolp)
  16333. (newline)))
  16334. (display-buffer buf)
  16335. (sit-for 0))))
  16336. (defun org-in-commented-line ()
  16337. "Is point in a line starting with `#'?"
  16338. (equal (char-after (point-at-bol)) ?#))
  16339. (defun org-in-indented-comment-line ()
  16340. "Is point in a line starting with `#' after some white space?"
  16341. (save-excursion
  16342. (save-match-data
  16343. (goto-char (point-at-bol))
  16344. (looking-at "[ \t]*#"))))
  16345. (defun org-in-verbatim-emphasis ()
  16346. (save-match-data
  16347. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16348. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16349. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16350. (if (and marker (marker-buffer marker)
  16351. (buffer-live-p (marker-buffer marker)))
  16352. (progn
  16353. (switch-to-buffer (marker-buffer marker))
  16354. (if (or (> marker (point-max)) (< marker (point-min)))
  16355. (widen))
  16356. (goto-char marker)
  16357. (org-show-context 'org-goto))
  16358. (if bookmark
  16359. (bookmark-jump bookmark)
  16360. (error "Cannot find location"))))
  16361. (defun org-quote-csv-field (s)
  16362. "Quote field for inclusion in CSV material."
  16363. (if (string-match "[\",]" s)
  16364. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16365. s))
  16366. (defun org-plist-delete (plist property)
  16367. "Delete PROPERTY from PLIST.
  16368. This is in contrast to merely setting it to 0."
  16369. (let (p)
  16370. (while plist
  16371. (if (not (eq property (car plist)))
  16372. (setq p (plist-put p (car plist) (nth 1 plist))))
  16373. (setq plist (cddr plist)))
  16374. p))
  16375. (defun org-force-self-insert (N)
  16376. "Needed to enforce self-insert under remapping."
  16377. (interactive "p")
  16378. (self-insert-command N))
  16379. (defun org-string-width (s)
  16380. "Compute width of string, ignoring invisible characters.
  16381. This ignores character with invisibility property `org-link', and also
  16382. characters with property `org-cwidth', because these will become invisible
  16383. upon the next fontification round."
  16384. (let (b l)
  16385. (when (or (eq t buffer-invisibility-spec)
  16386. (assq 'org-link buffer-invisibility-spec))
  16387. (while (setq b (text-property-any 0 (length s)
  16388. 'invisible 'org-link s))
  16389. (setq s (concat (substring s 0 b)
  16390. (substring s (or (next-single-property-change
  16391. b 'invisible s) (length s)))))))
  16392. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16393. (setq s (concat (substring s 0 b)
  16394. (substring s (or (next-single-property-change
  16395. b 'org-cwidth s) (length s))))))
  16396. (setq l (string-width s) b -1)
  16397. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16398. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16399. l))
  16400. (defun org-get-indentation (&optional line)
  16401. "Get the indentation of the current line, interpreting tabs.
  16402. When LINE is given, assume it represents a line and compute its indentation."
  16403. (if line
  16404. (if (string-match "^ *" (org-remove-tabs line))
  16405. (match-end 0))
  16406. (save-excursion
  16407. (beginning-of-line 1)
  16408. (skip-chars-forward " \t")
  16409. (current-column))))
  16410. (defun org-remove-tabs (s &optional width)
  16411. "Replace tabulators in S with spaces.
  16412. Assumes that s is a single line, starting in column 0."
  16413. (setq width (or width tab-width))
  16414. (while (string-match "\t" s)
  16415. (setq s (replace-match
  16416. (make-string
  16417. (- (* width (/ (+ (match-beginning 0) width) width))
  16418. (match-beginning 0)) ?\ )
  16419. t t s)))
  16420. s)
  16421. (defun org-fix-indentation (line ind)
  16422. "Fix indentation in LINE.
  16423. IND is a cons cell with target and minimum indentation.
  16424. If the current indentation in LINE is smaller than the minimum,
  16425. leave it alone. If it is larger than ind, set it to the target."
  16426. (let* ((l (org-remove-tabs line))
  16427. (i (org-get-indentation l))
  16428. (i1 (car ind)) (i2 (cdr ind)))
  16429. (if (>= i i2) (setq l (substring line i2)))
  16430. (if (> i1 0)
  16431. (concat (make-string i1 ?\ ) l)
  16432. l)))
  16433. (defun org-remove-indentation (code &optional n)
  16434. "Remove the maximum common indentation from the lines in CODE.
  16435. N may optionally be the number of spaces to remove."
  16436. (with-temp-buffer
  16437. (insert code)
  16438. (org-do-remove-indentation n)
  16439. (buffer-string)))
  16440. (defun org-do-remove-indentation (&optional n)
  16441. "Remove the maximum common indentation from the buffer."
  16442. (untabify (point-min) (point-max))
  16443. (let ((min 10000) re)
  16444. (if n
  16445. (setq min n)
  16446. (goto-char (point-min))
  16447. (while (re-search-forward "^ *[^ \n]" nil t)
  16448. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16449. (unless (or (= min 0) (= min 10000))
  16450. (setq re (format "^ \\{%d\\}" min))
  16451. (goto-char (point-min))
  16452. (while (re-search-forward re nil t)
  16453. (replace-match "")
  16454. (end-of-line 1))
  16455. min)))
  16456. (defun org-fill-template (template alist)
  16457. "Find each %key of ALIST in TEMPLATE and replace it."
  16458. (let ((case-fold-search nil)
  16459. entry key value)
  16460. (setq alist (sort (copy-sequence alist)
  16461. (lambda (a b) (< (length (car a)) (length (car b))))))
  16462. (while (setq entry (pop alist))
  16463. (setq template
  16464. (replace-regexp-in-string
  16465. (concat "%" (regexp-quote (car entry)))
  16466. (cdr entry) template t t)))
  16467. template))
  16468. (defun org-base-buffer (buffer)
  16469. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16470. (if (not buffer)
  16471. buffer
  16472. (or (buffer-base-buffer buffer)
  16473. buffer)))
  16474. (defun org-trim (s)
  16475. "Remove whitespace at beginning and end of string."
  16476. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16477. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16478. s)
  16479. (defun org-wrap (string &optional width lines)
  16480. "Wrap string to either a number of lines, or a width in characters.
  16481. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16482. that costs. If there is a word longer than WIDTH, the text is actually
  16483. wrapped to the length of that word.
  16484. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16485. many lines, whatever width that takes.
  16486. The return value is a list of lines, without newlines at the end."
  16487. (let* ((words (org-split-string string "[ \t\n]+"))
  16488. (maxword (apply 'max (mapcar 'org-string-width words)))
  16489. w ll)
  16490. (cond (width
  16491. (org-do-wrap words (max maxword width)))
  16492. (lines
  16493. (setq w maxword)
  16494. (setq ll (org-do-wrap words maxword))
  16495. (if (<= (length ll) lines)
  16496. ll
  16497. (setq ll words)
  16498. (while (> (length ll) lines)
  16499. (setq w (1+ w))
  16500. (setq ll (org-do-wrap words w)))
  16501. ll))
  16502. (t (error "Cannot wrap this")))))
  16503. (defun org-do-wrap (words width)
  16504. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  16505. (let (lines line)
  16506. (while words
  16507. (setq line (pop words))
  16508. (while (and words (< (+ (length line) (length (car words))) width))
  16509. (setq line (concat line " " (pop words))))
  16510. (setq lines (push line lines)))
  16511. (nreverse lines)))
  16512. (defun org-split-string (string &optional separators)
  16513. "Splits STRING into substrings at SEPARATORS.
  16514. No empty strings are returned if there are matches at the beginning
  16515. and end of string."
  16516. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  16517. (start 0)
  16518. notfirst
  16519. (list nil))
  16520. (while (and (string-match rexp string
  16521. (if (and notfirst
  16522. (= start (match-beginning 0))
  16523. (< start (length string)))
  16524. (1+ start) start))
  16525. (< (match-beginning 0) (length string)))
  16526. (setq notfirst t)
  16527. (or (eq (match-beginning 0) 0)
  16528. (and (eq (match-beginning 0) (match-end 0))
  16529. (eq (match-beginning 0) start))
  16530. (setq list
  16531. (cons (substring string start (match-beginning 0))
  16532. list)))
  16533. (setq start (match-end 0)))
  16534. (or (eq start (length string))
  16535. (setq list
  16536. (cons (substring string start)
  16537. list)))
  16538. (nreverse list)))
  16539. (defun org-quote-vert (s)
  16540. "Replace \"|\" with \"\\vert\"."
  16541. (while (string-match "|" s)
  16542. (setq s (replace-match "\\vert" t t s)))
  16543. s)
  16544. (defun org-uuidgen-p (s)
  16545. "Is S an ID created by UUIDGEN?"
  16546. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  16547. (defun org-context ()
  16548. "Return a list of contexts of the current cursor position.
  16549. If several contexts apply, all are returned.
  16550. Each context entry is a list with a symbol naming the context, and
  16551. two positions indicating start and end of the context. Possible
  16552. contexts are:
  16553. :headline anywhere in a headline
  16554. :headline-stars on the leading stars in a headline
  16555. :todo-keyword on a TODO keyword (including DONE) in a headline
  16556. :tags on the TAGS in a headline
  16557. :priority on the priority cookie in a headline
  16558. :item on the first line of a plain list item
  16559. :item-bullet on the bullet/number of a plain list item
  16560. :checkbox on the checkbox in a plain list item
  16561. :table in an org-mode table
  16562. :table-special on a special filed in a table
  16563. :table-table in a table.el table
  16564. :link on a hyperlink
  16565. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  16566. :target on a <<target>>
  16567. :radio-target on a <<<radio-target>>>
  16568. :latex-fragment on a LaTeX fragment
  16569. :latex-preview on a LaTeX fragment with overlayed preview image
  16570. This function expects the position to be visible because it uses font-lock
  16571. faces as a help to recognize the following contexts: :table-special, :link,
  16572. and :keyword."
  16573. (let* ((f (get-text-property (point) 'face))
  16574. (faces (if (listp f) f (list f)))
  16575. (p (point)) clist o)
  16576. ;; First the large context
  16577. (cond
  16578. ((org-on-heading-p t)
  16579. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  16580. (when (progn
  16581. (beginning-of-line 1)
  16582. (looking-at org-todo-line-tags-regexp))
  16583. (push (org-point-in-group p 1 :headline-stars) clist)
  16584. (push (org-point-in-group p 2 :todo-keyword) clist)
  16585. (push (org-point-in-group p 4 :tags) clist))
  16586. (goto-char p)
  16587. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  16588. (if (looking-at "\\[#[A-Z0-9]\\]")
  16589. (push (org-point-in-group p 0 :priority) clist)))
  16590. ((org-at-item-p)
  16591. (push (org-point-in-group p 2 :item-bullet) clist)
  16592. (push (list :item (point-at-bol)
  16593. (save-excursion (org-end-of-item) (point)))
  16594. clist)
  16595. (and (org-at-item-checkbox-p)
  16596. (push (org-point-in-group p 0 :checkbox) clist)))
  16597. ((org-at-table-p)
  16598. (push (list :table (org-table-begin) (org-table-end)) clist)
  16599. (if (memq 'org-formula faces)
  16600. (push (list :table-special
  16601. (previous-single-property-change p 'face)
  16602. (next-single-property-change p 'face)) clist)))
  16603. ((org-at-table-p 'any)
  16604. (push (list :table-table) clist)))
  16605. (goto-char p)
  16606. ;; Now the small context
  16607. (cond
  16608. ((org-at-timestamp-p)
  16609. (push (org-point-in-group p 0 :timestamp) clist))
  16610. ((memq 'org-link faces)
  16611. (push (list :link
  16612. (previous-single-property-change p 'face)
  16613. (next-single-property-change p 'face)) clist))
  16614. ((memq 'org-special-keyword faces)
  16615. (push (list :keyword
  16616. (previous-single-property-change p 'face)
  16617. (next-single-property-change p 'face)) clist))
  16618. ((org-on-target-p)
  16619. (push (org-point-in-group p 0 :target) clist)
  16620. (goto-char (1- (match-beginning 0)))
  16621. (if (looking-at org-radio-target-regexp)
  16622. (push (org-point-in-group p 0 :radio-target) clist))
  16623. (goto-char p))
  16624. ((setq o (car (delq nil
  16625. (mapcar
  16626. (lambda (x)
  16627. (if (memq x org-latex-fragment-image-overlays) x))
  16628. (overlays-at (point))))))
  16629. (push (list :latex-fragment
  16630. (overlay-start o) (overlay-end o)) clist)
  16631. (push (list :latex-preview
  16632. (overlay-start o) (overlay-end o)) clist))
  16633. ((org-inside-LaTeX-fragment-p)
  16634. ;; FIXME: positions wrong.
  16635. (push (list :latex-fragment (point) (point)) clist)))
  16636. (setq clist (nreverse (delq nil clist)))
  16637. clist))
  16638. ;; FIXME: Compare with at-regexp-p Do we need both?
  16639. (defun org-in-regexp (re &optional nlines visually)
  16640. "Check if point is inside a match of regexp.
  16641. Normally only the current line is checked, but you can include NLINES extra
  16642. lines both before and after point into the search.
  16643. If VISUALLY is set, require that the cursor is not after the match but
  16644. really on, so that the block visually is on the match."
  16645. (catch 'exit
  16646. (let ((pos (point))
  16647. (eol (point-at-eol (+ 1 (or nlines 0))))
  16648. (inc (if visually 1 0)))
  16649. (save-excursion
  16650. (beginning-of-line (- 1 (or nlines 0)))
  16651. (while (re-search-forward re eol t)
  16652. (if (and (<= (match-beginning 0) pos)
  16653. (>= (+ inc (match-end 0)) pos))
  16654. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16655. (defun org-at-regexp-p (regexp)
  16656. "Is point inside a match of REGEXP in the current line?"
  16657. (catch 'exit
  16658. (save-excursion
  16659. (let ((pos (point)) (end (point-at-eol)))
  16660. (beginning-of-line 1)
  16661. (while (re-search-forward regexp end t)
  16662. (if (and (<= (match-beginning 0) pos)
  16663. (>= (match-end 0) pos))
  16664. (throw 'exit t)))
  16665. nil))))
  16666. (defun org-in-regexps-block-p (start-re end-re)
  16667. "Return t if the current point is between matches of START-RE and END-RE.
  16668. This will also return to if point is on one of the two matches."
  16669. (interactive)
  16670. (let ((p (point)))
  16671. (save-excursion
  16672. (and (or (org-at-regexp-p start-re)
  16673. (re-search-backward start-re nil t))
  16674. (re-search-forward end-re nil t)
  16675. (>= (point) p)))))
  16676. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16677. "Call `multi-occur' with buffers for all agenda files."
  16678. (interactive "sOrg-files matching: \np")
  16679. (let* ((files (org-agenda-files))
  16680. (tnames (mapcar 'file-truename files))
  16681. (extra org-agenda-text-search-extra-files)
  16682. f)
  16683. (when (eq (car extra) 'agenda-archives)
  16684. (setq extra (cdr extra))
  16685. (setq files (org-add-archive-files files)))
  16686. (while (setq f (pop extra))
  16687. (unless (member (file-truename f) tnames)
  16688. (add-to-list 'files f 'append)
  16689. (add-to-list 'tnames (file-truename f) 'append)))
  16690. (multi-occur
  16691. (mapcar (lambda (x)
  16692. (with-current-buffer
  16693. (or (get-file-buffer x) (find-file-noselect x))
  16694. (widen)
  16695. (current-buffer)))
  16696. files)
  16697. regexp)))
  16698. (if (boundp 'occur-mode-find-occurrence-hook)
  16699. ;; Emacs 23
  16700. (add-hook 'occur-mode-find-occurrence-hook
  16701. (lambda ()
  16702. (when (org-mode-p)
  16703. (org-reveal))))
  16704. ;; Emacs 22
  16705. (defadvice occur-mode-goto-occurrence
  16706. (after org-occur-reveal activate)
  16707. (and (org-mode-p) (org-reveal)))
  16708. (defadvice occur-mode-goto-occurrence-other-window
  16709. (after org-occur-reveal activate)
  16710. (and (org-mode-p) (org-reveal)))
  16711. (defadvice occur-mode-display-occurrence
  16712. (after org-occur-reveal activate)
  16713. (when (org-mode-p)
  16714. (let ((pos (occur-mode-find-occurrence)))
  16715. (with-current-buffer (marker-buffer pos)
  16716. (save-excursion
  16717. (goto-char pos)
  16718. (org-reveal)))))))
  16719. (defun org-occur-link-in-agenda-files ()
  16720. "Create a link and search for it in the agendas.
  16721. The link is not stored in `org-stored-links', it is just created
  16722. for the search purpose."
  16723. (interactive)
  16724. (let ((link (condition-case nil
  16725. (org-store-link nil)
  16726. (error "Unable to create a link to here"))))
  16727. (org-occur-in-agenda-files (regexp-quote link))))
  16728. (defun org-uniquify (list)
  16729. "Remove duplicate elements from LIST."
  16730. (let (res)
  16731. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16732. res))
  16733. (defun org-delete-all (elts list)
  16734. "Remove all elements in ELTS from LIST."
  16735. (while elts
  16736. (setq list (delete (pop elts) list)))
  16737. list)
  16738. (defun org-count (cl-item cl-seq)
  16739. "Count the number of occurrences of ITEM in SEQ.
  16740. Taken from `count' in cl-seq.el with all keyword arguments removed."
  16741. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  16742. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  16743. (while (< cl-start cl-end)
  16744. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  16745. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  16746. (setq cl-start (1+ cl-start)))
  16747. cl-count))
  16748. (defun org-remove-if (predicate seq)
  16749. "Remove everything from SEQ that fulfills PREDICATE."
  16750. (let (res e)
  16751. (while seq
  16752. (setq e (pop seq))
  16753. (if (not (funcall predicate e)) (push e res)))
  16754. (nreverse res)))
  16755. (defun org-remove-if-not (predicate seq)
  16756. "Remove everything from SEQ that does not fulfill PREDICATE."
  16757. (let (res e)
  16758. (while seq
  16759. (setq e (pop seq))
  16760. (if (funcall predicate e) (push e res)))
  16761. (nreverse res)))
  16762. (defun org-back-over-empty-lines ()
  16763. "Move backwards over whitespace, to the beginning of the first empty line.
  16764. Returns the number of empty lines passed."
  16765. (let ((pos (point)))
  16766. (skip-chars-backward " \t\n\r")
  16767. (beginning-of-line 2)
  16768. (goto-char (min (point) pos))
  16769. (count-lines (point) pos)))
  16770. (defun org-skip-whitespace ()
  16771. (skip-chars-forward " \t\n\r"))
  16772. (defun org-point-in-group (point group &optional context)
  16773. "Check if POINT is in match-group GROUP.
  16774. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16775. match. If the match group does not exist or point is not inside it,
  16776. return nil."
  16777. (and (match-beginning group)
  16778. (>= point (match-beginning group))
  16779. (<= point (match-end group))
  16780. (if context
  16781. (list context (match-beginning group) (match-end group))
  16782. t)))
  16783. (defun org-switch-to-buffer-other-window (&rest args)
  16784. "Switch to buffer in a second window on the current frame.
  16785. In particular, do not allow pop-up frames.
  16786. Returns the newly created buffer."
  16787. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16788. special-display-function)
  16789. (apply 'switch-to-buffer-other-window args)))
  16790. (defun org-combine-plists (&rest plists)
  16791. "Create a single property list from all plists in PLISTS.
  16792. The process starts by copying the first list, and then setting properties
  16793. from the other lists. Settings in the last list are the most significant
  16794. ones and overrule settings in the other lists."
  16795. (let ((rtn (copy-sequence (pop plists)))
  16796. p v ls)
  16797. (while plists
  16798. (setq ls (pop plists))
  16799. (while ls
  16800. (setq p (pop ls) v (pop ls))
  16801. (setq rtn (plist-put rtn p v))))
  16802. rtn))
  16803. (defun org-move-line-down (arg)
  16804. "Move the current line down. With prefix argument, move it past ARG lines."
  16805. (interactive "p")
  16806. (let ((col (current-column))
  16807. beg end pos)
  16808. (beginning-of-line 1) (setq beg (point))
  16809. (beginning-of-line 2) (setq end (point))
  16810. (beginning-of-line (+ 1 arg))
  16811. (setq pos (move-marker (make-marker) (point)))
  16812. (insert (delete-and-extract-region beg end))
  16813. (goto-char pos)
  16814. (org-move-to-column col)))
  16815. (defun org-move-line-up (arg)
  16816. "Move the current line up. With prefix argument, move it past ARG lines."
  16817. (interactive "p")
  16818. (let ((col (current-column))
  16819. beg end pos)
  16820. (beginning-of-line 1) (setq beg (point))
  16821. (beginning-of-line 2) (setq end (point))
  16822. (beginning-of-line (- arg))
  16823. (setq pos (move-marker (make-marker) (point)))
  16824. (insert (delete-and-extract-region beg end))
  16825. (goto-char pos)
  16826. (org-move-to-column col)))
  16827. (defun org-replace-escapes (string table)
  16828. "Replace %-escapes in STRING with values in TABLE.
  16829. TABLE is an association list with keys like \"%a\" and string values.
  16830. The sequences in STRING may contain normal field width and padding information,
  16831. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16832. so values can contain further %-escapes if they are define later in TABLE."
  16833. (let ((tbl (copy-alist table))
  16834. (case-fold-search nil)
  16835. (pchg 0)
  16836. e re rpl)
  16837. (while (setq e (pop tbl))
  16838. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16839. (when (and (cdr e) (string-match re (cdr e)))
  16840. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16841. (safe "SREF"))
  16842. (add-text-properties 0 3 (list 'sref sref) safe)
  16843. (setcdr e (replace-match safe t t (cdr e)))))
  16844. (while (string-match re string)
  16845. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16846. (cdr e)))
  16847. (setq string (replace-match rpl t t string))))
  16848. (while (setq pchg (next-property-change pchg string))
  16849. (let ((sref (get-text-property pchg 'sref string)))
  16850. (when (and sref (string-match "SREF" string pchg))
  16851. (setq string (replace-match sref t t string)))))
  16852. string))
  16853. (defun org-sublist (list start end)
  16854. "Return a section of LIST, from START to END.
  16855. Counting starts at 1."
  16856. (let (rtn (c start))
  16857. (setq list (nthcdr (1- start) list))
  16858. (while (and list (<= c end))
  16859. (push (pop list) rtn)
  16860. (setq c (1+ c)))
  16861. (nreverse rtn)))
  16862. (defun org-find-base-buffer-visiting (file)
  16863. "Like `find-buffer-visiting' but always return the base buffer and
  16864. not an indirect buffer."
  16865. (let ((buf (or (get-file-buffer file)
  16866. (find-buffer-visiting file))))
  16867. (if buf
  16868. (or (buffer-base-buffer buf) buf)
  16869. nil)))
  16870. (defun org-image-file-name-regexp (&optional extensions)
  16871. "Return regexp matching the file names of images.
  16872. If EXTENSIONS is given, only match these."
  16873. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16874. (image-file-name-regexp)
  16875. (let ((image-file-name-extensions
  16876. (or extensions
  16877. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16878. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16879. (concat "\\."
  16880. (regexp-opt (nconc (mapcar 'upcase
  16881. image-file-name-extensions)
  16882. image-file-name-extensions)
  16883. t)
  16884. "\\'"))))
  16885. (defun org-file-image-p (file &optional extensions)
  16886. "Return non-nil if FILE is an image."
  16887. (save-match-data
  16888. (string-match (org-image-file-name-regexp extensions) file)))
  16889. (defun org-get-cursor-date ()
  16890. "Return the date at cursor in as a time.
  16891. This works in the calendar and in the agenda, anywhere else it just
  16892. returns the current time."
  16893. (let (date day defd)
  16894. (cond
  16895. ((eq major-mode 'calendar-mode)
  16896. (setq date (calendar-cursor-to-date)
  16897. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16898. ((eq major-mode 'org-agenda-mode)
  16899. (setq day (get-text-property (point) 'day))
  16900. (if day
  16901. (setq date (calendar-gregorian-from-absolute day)
  16902. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16903. (nth 2 date))))))
  16904. (or defd (current-time))))
  16905. (defvar org-agenda-action-marker (make-marker)
  16906. "Marker pointing to the entry for the next agenda action.")
  16907. (defun org-mark-entry-for-agenda-action ()
  16908. "Mark the current entry as target of an agenda action.
  16909. Agenda actions are actions executed from the agenda with the key `k',
  16910. which make use of the date at the cursor."
  16911. (interactive)
  16912. (move-marker org-agenda-action-marker
  16913. (save-excursion (org-back-to-heading t) (point))
  16914. (current-buffer))
  16915. (message
  16916. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16917. ;;; Paragraph filling stuff.
  16918. ;; We want this to be just right, so use the full arsenal.
  16919. (defun org-indent-line-function ()
  16920. "Indent line like previous, but further if previous was headline or item."
  16921. (interactive)
  16922. (let* ((pos (point))
  16923. (itemp (org-at-item-p))
  16924. (case-fold-search t)
  16925. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16926. column bpos bcol tpos tcol bullet btype bullet-type)
  16927. ;; Find the previous relevant line
  16928. (beginning-of-line 1)
  16929. (cond
  16930. ((looking-at "#") (setq column 0))
  16931. ((looking-at "\\*+ ") (setq column 0))
  16932. ((and (looking-at "[ \t]*:END:")
  16933. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16934. (save-excursion
  16935. (goto-char (1- (match-beginning 1)))
  16936. (setq column (current-column))))
  16937. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16938. (save-excursion
  16939. (re-search-backward
  16940. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16941. (setq column (org-get-indentation (match-string 0))))
  16942. (t
  16943. (beginning-of-line 0)
  16944. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16945. (not (looking-at "[ \t]*:END:"))
  16946. (not (looking-at org-drawer-regexp)))
  16947. (beginning-of-line 0))
  16948. (cond
  16949. ((looking-at "\\*+[ \t]+")
  16950. (if (not org-adapt-indentation)
  16951. (setq column 0)
  16952. (goto-char (match-end 0))
  16953. (setq column (current-column))))
  16954. ((looking-at org-drawer-regexp)
  16955. (goto-char (1- (match-beginning 1)))
  16956. (setq column (current-column)))
  16957. ((looking-at "\\([ \t]*\\):END:")
  16958. (goto-char (match-end 1))
  16959. (setq column (current-column)))
  16960. ((org-in-item-p)
  16961. (org-beginning-of-item)
  16962. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16963. (setq bpos (match-beginning 1) tpos (match-end 0)
  16964. bcol (progn (goto-char bpos) (current-column))
  16965. tcol (progn (goto-char tpos) (current-column))
  16966. bullet (match-string 1)
  16967. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16968. (if (> tcol (+ bcol org-description-max-indent))
  16969. (setq tcol (+ bcol 5)))
  16970. (if (not itemp)
  16971. (setq column tcol)
  16972. (goto-char pos)
  16973. (beginning-of-line 1)
  16974. (if (looking-at "\\S-")
  16975. (progn
  16976. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16977. (setq bullet (match-string 1)
  16978. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16979. (setq column (if (equal btype bullet-type) bcol tcol)))
  16980. (setq column (org-get-indentation)))))
  16981. (t (setq column (org-get-indentation))))))
  16982. (goto-char pos)
  16983. (if (<= (current-column) (current-indentation))
  16984. (org-indent-line-to column)
  16985. (save-excursion (org-indent-line-to column)))
  16986. (setq column (current-column))
  16987. (beginning-of-line 1)
  16988. (if (looking-at
  16989. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16990. (replace-match (concat (match-string 1)
  16991. (format org-property-format
  16992. (match-string 2) (match-string 3)))
  16993. t t))
  16994. (org-move-to-column column)))
  16995. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16996. "Variable to store copy of `adaptive-fill-regexp'.
  16997. Since `adaptive-fill-regexp' is set to never match, we need to
  16998. store a backup of its value before entering `org-mode' so that
  16999. the functionality can be provided as a fall-back.")
  17000. (defun org-set-autofill-regexps ()
  17001. (interactive)
  17002. ;; In the paragraph separator we include headlines, because filling
  17003. ;; text in a line directly attached to a headline would otherwise
  17004. ;; fill the headline as well.
  17005. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17006. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17007. ;; The paragraph starter includes hand-formatted lists.
  17008. (org-set-local
  17009. 'paragraph-start
  17010. (concat
  17011. "\f" "\\|"
  17012. "[ ]*$" "\\|"
  17013. "\\*+ " "\\|"
  17014. "[ \t]*#" "\\|"
  17015. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  17016. "[ \t]*[:|]" "\\|"
  17017. "\\$\\$" "\\|"
  17018. "\\\\\\(begin\\|end\\|[][]\\)"))
  17019. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17020. ;; But only if the user has not turned off tables or fixed-width regions
  17021. (org-set-local
  17022. 'auto-fill-inhibit-regexp
  17023. (concat "\\*+ \\|#\\+"
  17024. "\\|[ \t]*" org-keyword-time-regexp
  17025. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17026. (concat
  17027. "\\|[ \t]*["
  17028. (if org-enable-table-editor "|" "")
  17029. (if org-enable-fixed-width-editor ":" "")
  17030. "]"))))
  17031. ;; We use our own fill-paragraph function, to make sure that tables
  17032. ;; and fixed-width regions are not wrapped. That function will pass
  17033. ;; through to `fill-paragraph' when appropriate.
  17034. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17035. ;; Adaptive filling: To get full control, first make sure that
  17036. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17037. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17038. (org-set-local 'org-adaptive-fill-regexp-backup
  17039. adaptive-fill-regexp))
  17040. (org-set-local 'adaptive-fill-regexp "\000")
  17041. (org-set-local 'adaptive-fill-function
  17042. 'org-adaptive-fill-function)
  17043. (org-set-local
  17044. 'align-mode-rules-list
  17045. '((org-in-buffer-settings
  17046. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17047. (modes . '(org-mode))))))
  17048. (defun org-fill-paragraph (&optional justify)
  17049. "Re-align a table, pass through to fill-paragraph if no table."
  17050. (let ((table-p (org-at-table-p))
  17051. (table.el-p (org-at-table.el-p)))
  17052. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17053. (save-excursion (goto-char (point-at-bol))
  17054. (looking-at outline-regexp)))
  17055. t) ; skip headlines
  17056. (table.el-p t) ; skip table.el tables
  17057. (table-p (org-table-align) t) ; align org-mode tables
  17058. (t nil)))) ; call paragraph-fill
  17059. ;; For reference, this is the default value of adaptive-fill-regexp
  17060. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17061. (defun org-adaptive-fill-function ()
  17062. "Return a fill prefix for org-mode files.
  17063. In particular, this makes sure hanging paragraphs for hand-formatted lists
  17064. work correctly."
  17065. (cond
  17066. ;; Comment line
  17067. ((looking-at "#[ \t]+")
  17068. (match-string-no-properties 0))
  17069. ;; Description list
  17070. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  17071. (save-excursion
  17072. (if (> (match-end 1) (+ (match-beginning 1)
  17073. org-description-max-indent))
  17074. (goto-char (+ (match-beginning 1) 5))
  17075. (goto-char (match-end 0)))
  17076. (make-string (current-column) ?\ )))
  17077. ;; Ordered or unordered list
  17078. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  17079. (save-excursion
  17080. (goto-char (match-end 0))
  17081. (make-string (current-column) ?\ )))
  17082. ;; Other text
  17083. ((looking-at org-adaptive-fill-regexp-backup)
  17084. (match-string-no-properties 0))))
  17085. ;;; Other stuff.
  17086. (defun org-toggle-fixed-width-section (arg)
  17087. "Toggle the fixed-width export.
  17088. If there is no active region, the QUOTE keyword at the current headline is
  17089. inserted or removed. When present, it causes the text between this headline
  17090. and the next to be exported as fixed-width text, and unmodified.
  17091. If there is an active region, this command adds or removes a colon as the
  17092. first character of this line. If the first character of a line is a colon,
  17093. this line is also exported in fixed-width font."
  17094. (interactive "P")
  17095. (let* ((cc 0)
  17096. (regionp (org-region-active-p))
  17097. (beg (if regionp (region-beginning) (point)))
  17098. (end (if regionp (region-end)))
  17099. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17100. (case-fold-search nil)
  17101. (re "[ \t]*\\(: \\)")
  17102. off)
  17103. (if regionp
  17104. (save-excursion
  17105. (goto-char beg)
  17106. (setq cc (current-column))
  17107. (beginning-of-line 1)
  17108. (setq off (looking-at re))
  17109. (while (> nlines 0)
  17110. (setq nlines (1- nlines))
  17111. (beginning-of-line 1)
  17112. (cond
  17113. (arg
  17114. (org-move-to-column cc t)
  17115. (insert ": \n")
  17116. (forward-line -1))
  17117. ((and off (looking-at re))
  17118. (replace-match "" t t nil 1))
  17119. ((not off) (org-move-to-column cc t) (insert ": ")))
  17120. (forward-line 1)))
  17121. (save-excursion
  17122. (org-back-to-heading)
  17123. (if (looking-at (concat outline-regexp
  17124. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17125. (replace-match "" t t nil 1)
  17126. (if (looking-at outline-regexp)
  17127. (progn
  17128. (goto-char (match-end 0))
  17129. (insert org-quote-string " "))))))))
  17130. (defun org-reftex-citation ()
  17131. "Use reftex-citation to insert a citation into the buffer.
  17132. This looks for a line like
  17133. #+BIBLIOGRAPHY: foo plain option:-d
  17134. and derives from it that foo.bib is the bibliography file relevant
  17135. for this document. It then installs the necessary environment for RefTeX
  17136. to work in this buffer and calls `reftex-citation' to insert a citation
  17137. into the buffer.
  17138. Export of such citations to both LaTeX and HTML is handled by the contributed
  17139. package org-exp-bibtex by Taru Karttunen."
  17140. (interactive)
  17141. (let ((reftex-docstruct-symbol 'rds)
  17142. (reftex-cite-format "\\cite{%l}")
  17143. rds bib)
  17144. (save-excursion
  17145. (save-restriction
  17146. (widen)
  17147. (let ((case-fold-search t)
  17148. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17149. (if (not (save-excursion
  17150. (or (re-search-forward re nil t)
  17151. (re-search-backward re nil t))))
  17152. (error "No bibliography defined in file")
  17153. (setq bib (concat (match-string 1) ".bib")
  17154. rds (list (list 'bib bib)))))))
  17155. (call-interactively 'reftex-citation)))
  17156. ;;;; Functions extending outline functionality
  17157. (defun org-beginning-of-line (&optional arg)
  17158. "Go to the beginning of the current line. If that is invisible, continue
  17159. to a visible line beginning. This makes the function of C-a more intuitive.
  17160. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17161. first attempt, and only move to after the tags when the cursor is already
  17162. beyond the end of the headline."
  17163. (interactive "P")
  17164. (let ((pos (point))
  17165. (special (if (consp org-special-ctrl-a/e)
  17166. (car org-special-ctrl-a/e)
  17167. org-special-ctrl-a/e))
  17168. refpos)
  17169. (if (org-bound-and-true-p line-move-visual)
  17170. (beginning-of-visual-line 1)
  17171. (beginning-of-line 1))
  17172. (if (and arg (fboundp 'move-beginning-of-line))
  17173. (call-interactively 'move-beginning-of-line)
  17174. (if (bobp)
  17175. nil
  17176. (backward-char 1)
  17177. (if (org-truely-invisible-p)
  17178. (while (and (not (bobp)) (org-truely-invisible-p))
  17179. (backward-char 1)
  17180. (beginning-of-line 1))
  17181. (forward-char 1))))
  17182. (when special
  17183. (cond
  17184. ((and (looking-at org-complex-heading-regexp)
  17185. (= (char-after (match-end 1)) ?\ ))
  17186. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17187. (point-at-eol)))
  17188. (goto-char
  17189. (if (eq special t)
  17190. (cond ((> pos refpos) refpos)
  17191. ((= pos (point)) refpos)
  17192. (t (point)))
  17193. (cond ((> pos (point)) (point))
  17194. ((not (eq last-command this-command)) (point))
  17195. (t refpos)))))
  17196. ((org-at-item-p)
  17197. (goto-char
  17198. (if (eq special t)
  17199. (cond ((> pos (match-end 4)) (match-end 4))
  17200. ((= pos (point)) (match-end 4))
  17201. (t (point)))
  17202. (cond ((> pos (point)) (point))
  17203. ((not (eq last-command this-command)) (point))
  17204. (t (match-end 4))))))))
  17205. (org-no-warnings
  17206. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17207. (defun org-end-of-line (&optional arg)
  17208. "Go to the end of the line.
  17209. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17210. first attempt, and only move to after the tags when the cursor is already
  17211. beyond the end of the headline."
  17212. (interactive "P")
  17213. (let ((special (if (consp org-special-ctrl-a/e)
  17214. (cdr org-special-ctrl-a/e)
  17215. org-special-ctrl-a/e)))
  17216. (if (or (not special)
  17217. (not (org-on-heading-p))
  17218. arg)
  17219. (call-interactively
  17220. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17221. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17222. (t 'end-of-line)))
  17223. (let ((pos (point)))
  17224. (beginning-of-line 1)
  17225. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  17226. (if (eq special t)
  17227. (if (or (< pos (match-beginning 1))
  17228. (= pos (match-end 0)))
  17229. (goto-char (match-beginning 1))
  17230. (goto-char (match-end 0)))
  17231. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17232. (goto-char (match-end 0))
  17233. (goto-char (match-beginning 1))))
  17234. (call-interactively (if (fboundp 'move-end-of-line)
  17235. 'move-end-of-line
  17236. 'end-of-line)))))
  17237. (org-no-warnings
  17238. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17239. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17240. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17241. (define-key org-mode-map [home] 'org-beginning-of-line)
  17242. (define-key org-mode-map [end] 'org-end-of-line)
  17243. (defun org-backward-sentence (&optional arg)
  17244. "Go to beginning of sentence, or beginning of table field.
  17245. This will call `backward-sentence' or `org-table-beginning-of-field',
  17246. depending on context."
  17247. (interactive "P")
  17248. (cond
  17249. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17250. (t (call-interactively 'backward-sentence))))
  17251. (defun org-forward-sentence (&optional arg)
  17252. "Go to end of sentence, or end of table field.
  17253. This will call `forward-sentence' or `org-table-end-of-field',
  17254. depending on context."
  17255. (interactive "P")
  17256. (cond
  17257. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17258. (t (call-interactively 'forward-sentence))))
  17259. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17260. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17261. (defun org-kill-line (&optional arg)
  17262. "Kill line, to tags or end of line."
  17263. (interactive "P")
  17264. (cond
  17265. ((or (not org-special-ctrl-k)
  17266. (bolp)
  17267. (not (org-on-heading-p)))
  17268. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17269. org-ctrl-k-protect-subtree)
  17270. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17271. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17272. (error "C-k aborted - would kill hidden subtree")))
  17273. (call-interactively 'kill-line))
  17274. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  17275. (kill-region (point) (match-beginning 1))
  17276. (org-set-tags nil t))
  17277. (t (kill-region (point) (point-at-eol)))))
  17278. (define-key org-mode-map "\C-k" 'org-kill-line)
  17279. (defun org-yank (&optional arg)
  17280. "Yank. If the kill is a subtree, treat it specially.
  17281. This command will look at the current kill and check if is a single
  17282. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17283. cursor is at the beginning of a line or after the stars of a currently
  17284. empty headline, then the yank is handled specially. How exactly depends
  17285. on the value of the following variables, both set by default.
  17286. org-yank-folded-subtrees
  17287. When set, the subtree(s) will be folded after insertion, but only
  17288. if doing so would now swallow text after the yanked text.
  17289. org-yank-adjusted-subtrees
  17290. When set, the subtree will be promoted or demoted in order to
  17291. fit into the local outline tree structure, which means that the level
  17292. will be adjusted so that it becomes the smaller one of the two
  17293. *visible* surrounding headings.
  17294. Any prefix to this command will cause `yank' to be called directly with
  17295. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17296. will just
  17297. plainly yank the text as it is.
  17298. \[1] The test checks if the first non-white line is a heading
  17299. and if there are no other headings with fewer stars."
  17300. (interactive "P")
  17301. (org-yank-generic 'yank arg))
  17302. (defun org-yank-generic (command arg)
  17303. "Perform some yank-like command.
  17304. This function implements the behavior described in the `org-yank'
  17305. documentation. However, it has been generalized to work for any
  17306. interactive command with similar behavior."
  17307. ;; pretend to be command COMMAND
  17308. (setq this-command command)
  17309. (if arg
  17310. (call-interactively command)
  17311. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17312. (and (org-kill-is-subtree-p)
  17313. (or (bolp)
  17314. (and (looking-at "[ \t]*$")
  17315. (string-match
  17316. "\\`\\*+\\'"
  17317. (buffer-substring (point-at-bol) (point)))))))
  17318. swallowp)
  17319. (cond
  17320. ((and subtreep org-yank-folded-subtrees)
  17321. (let ((beg (point))
  17322. end)
  17323. (if (and subtreep org-yank-adjusted-subtrees)
  17324. (org-paste-subtree nil nil 'for-yank)
  17325. (call-interactively command))
  17326. (setq end (point))
  17327. (goto-char beg)
  17328. (when (and (bolp) subtreep
  17329. (not (setq swallowp
  17330. (org-yank-folding-would-swallow-text beg end))))
  17331. (or (looking-at outline-regexp)
  17332. (re-search-forward (concat "^" outline-regexp) end t))
  17333. (while (and (< (point) end) (looking-at outline-regexp))
  17334. (hide-subtree)
  17335. (org-cycle-show-empty-lines 'folded)
  17336. (condition-case nil
  17337. (outline-forward-same-level 1)
  17338. (error (goto-char end)))))
  17339. (when swallowp
  17340. (message
  17341. "Inserted text not folded because that would swallow text"))
  17342. (goto-char end)
  17343. (skip-chars-forward " \t\n\r")
  17344. (beginning-of-line 1)
  17345. (push-mark beg 'nomsg)))
  17346. ((and subtreep org-yank-adjusted-subtrees)
  17347. (let ((beg (point-at-bol)))
  17348. (org-paste-subtree nil nil 'for-yank)
  17349. (push-mark beg 'nomsg)))
  17350. (t
  17351. (call-interactively command))))))
  17352. (defun org-yank-folding-would-swallow-text (beg end)
  17353. "Would hide-subtree at BEG swallow any text after END?"
  17354. (let (level)
  17355. (save-excursion
  17356. (goto-char beg)
  17357. (when (or (looking-at outline-regexp)
  17358. (re-search-forward (concat "^" outline-regexp) end t))
  17359. (setq level (org-outline-level)))
  17360. (goto-char end)
  17361. (skip-chars-forward " \t\r\n\v\f")
  17362. (if (or (eobp)
  17363. (and (bolp) (looking-at org-outline-regexp)
  17364. (<= (org-outline-level) level)))
  17365. nil ; Nothing would be swallowed
  17366. t)))) ; something would swallow
  17367. (define-key org-mode-map "\C-y" 'org-yank)
  17368. (defun org-invisible-p ()
  17369. "Check if point is at a character currently not visible."
  17370. ;; Early versions of noutline don't have `outline-invisible-p'.
  17371. (if (fboundp 'outline-invisible-p)
  17372. (outline-invisible-p)
  17373. (get-char-property (point) 'invisible)))
  17374. (defun org-truely-invisible-p ()
  17375. "Check if point is at a character currently not visible.
  17376. This version does not only check the character property, but also
  17377. `visible-mode'."
  17378. ;; Early versions of noutline don't have `outline-invisible-p'.
  17379. (if (org-bound-and-true-p visible-mode)
  17380. nil
  17381. (if (fboundp 'outline-invisible-p)
  17382. (outline-invisible-p)
  17383. (get-char-property (point) 'invisible))))
  17384. (defun org-invisible-p2 ()
  17385. "Check if point is at a character currently not visible."
  17386. (save-excursion
  17387. (if (and (eolp) (not (bobp))) (backward-char 1))
  17388. ;; Early versions of noutline don't have `outline-invisible-p'.
  17389. (if (fboundp 'outline-invisible-p)
  17390. (outline-invisible-p)
  17391. (get-char-property (point) 'invisible))))
  17392. (defun org-back-to-heading (&optional invisible-ok)
  17393. "Call `outline-back-to-heading', but provide a better error message."
  17394. (condition-case nil
  17395. (outline-back-to-heading invisible-ok)
  17396. (error (error "Before first headline at position %d in buffer %s"
  17397. (point) (current-buffer)))))
  17398. (defun org-beginning-of-defun ()
  17399. "Go to the beginning of the subtree, i.e. back to the heading."
  17400. (org-back-to-heading))
  17401. (defun org-end-of-defun ()
  17402. "Go to the end of the subtree."
  17403. (org-end-of-subtree nil t))
  17404. (defun org-before-first-heading-p ()
  17405. "Before first heading?"
  17406. (save-excursion
  17407. (null (re-search-backward "^\\*+ " nil t))))
  17408. (defun org-on-heading-p (&optional ignored)
  17409. (outline-on-heading-p t))
  17410. (defun org-at-heading-p (&optional ignored)
  17411. (outline-on-heading-p t))
  17412. (defun org-point-at-end-of-empty-headline ()
  17413. "If point is at the end of an empty headline, return t, else nil.
  17414. If the heading only contains a TODO keyword, it is still still considered
  17415. empty."
  17416. (and (looking-at "[ \t]*$")
  17417. (save-excursion
  17418. (beginning-of-line 1)
  17419. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  17420. "\\)?[ \t]*$")))))
  17421. (defun org-at-heading-or-item-p ()
  17422. (or (org-on-heading-p) (org-at-item-p)))
  17423. (defun org-on-target-p ()
  17424. (or (org-in-regexp org-radio-target-regexp)
  17425. (org-in-regexp org-target-regexp)))
  17426. (defun org-up-heading-all (arg)
  17427. "Move to the heading line of which the present line is a subheading.
  17428. This function considers both visible and invisible heading lines.
  17429. With argument, move up ARG levels."
  17430. (if (fboundp 'outline-up-heading-all)
  17431. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  17432. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  17433. (defun org-up-heading-safe ()
  17434. "Move to the heading line of which the present line is a subheading.
  17435. This version will not throw an error. It will return the level of the
  17436. headline found, or nil if no higher level is found.
  17437. Also, this function will be a lot faster than `outline-up-heading',
  17438. because it relies on stars being the outline starters. This can really
  17439. make a significant difference in outlines with very many siblings."
  17440. (let (start-level re)
  17441. (org-back-to-heading t)
  17442. (setq start-level (funcall outline-level))
  17443. (if (equal start-level 1)
  17444. nil
  17445. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  17446. (if (re-search-backward re nil t)
  17447. (funcall outline-level)))))
  17448. (defun org-first-sibling-p ()
  17449. "Is this heading the first child of its parents?"
  17450. (interactive)
  17451. (let ((re (concat "^" outline-regexp))
  17452. level l)
  17453. (unless (org-at-heading-p t)
  17454. (error "Not at a heading"))
  17455. (setq level (funcall outline-level))
  17456. (save-excursion
  17457. (if (not (re-search-backward re nil t))
  17458. t
  17459. (setq l (funcall outline-level))
  17460. (< l level)))))
  17461. (defun org-goto-sibling (&optional previous)
  17462. "Goto the next sibling, even if it is invisible.
  17463. When PREVIOUS is set, go to the previous sibling instead. Returns t
  17464. when a sibling was found. When none is found, return nil and don't
  17465. move point."
  17466. (let ((fun (if previous 're-search-backward 're-search-forward))
  17467. (pos (point))
  17468. (re (concat "^" outline-regexp))
  17469. level l)
  17470. (when (condition-case nil (org-back-to-heading t) (error nil))
  17471. (setq level (funcall outline-level))
  17472. (catch 'exit
  17473. (or previous (forward-char 1))
  17474. (while (funcall fun re nil t)
  17475. (setq l (funcall outline-level))
  17476. (when (< l level) (goto-char pos) (throw 'exit nil))
  17477. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  17478. (goto-char pos)
  17479. nil))))
  17480. (defun org-show-siblings ()
  17481. "Show all siblings of the current headline."
  17482. (save-excursion
  17483. (while (org-goto-sibling) (org-flag-heading nil)))
  17484. (save-excursion
  17485. (while (org-goto-sibling 'previous)
  17486. (org-flag-heading nil))))
  17487. (defun org-show-hidden-entry ()
  17488. "Show an entry where even the heading is hidden."
  17489. (save-excursion
  17490. (org-show-entry)))
  17491. (defun org-flag-heading (flag &optional entry)
  17492. "Flag the current heading. FLAG non-nil means make invisible.
  17493. When ENTRY is non-nil, show the entire entry."
  17494. (save-excursion
  17495. (org-back-to-heading t)
  17496. ;; Check if we should show the entire entry
  17497. (if entry
  17498. (progn
  17499. (org-show-entry)
  17500. (save-excursion
  17501. (and (outline-next-heading)
  17502. (org-flag-heading nil))))
  17503. (outline-flag-region (max (point-min) (1- (point)))
  17504. (save-excursion (outline-end-of-heading) (point))
  17505. flag))))
  17506. (defun org-get-next-sibling ()
  17507. "Move to next heading of the same level, and return point.
  17508. If there is no such heading, return nil.
  17509. This is like outline-next-sibling, but invisible headings are ok."
  17510. (let ((level (funcall outline-level)))
  17511. (outline-next-heading)
  17512. (while (and (not (eobp)) (> (funcall outline-level) level))
  17513. (outline-next-heading))
  17514. (if (or (eobp) (< (funcall outline-level) level))
  17515. nil
  17516. (point))))
  17517. (defun org-get-last-sibling ()
  17518. "Move to previous heading of the same level, and return point.
  17519. If there is no such heading, return nil."
  17520. (let ((opoint (point))
  17521. (level (funcall outline-level)))
  17522. (outline-previous-heading)
  17523. (when (and (/= (point) opoint) (outline-on-heading-p t))
  17524. (while (and (> (funcall outline-level) level)
  17525. (not (bobp)))
  17526. (outline-previous-heading))
  17527. (if (< (funcall outline-level) level)
  17528. nil
  17529. (point)))))
  17530. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  17531. ;; This contains an exact copy of the original function, but it uses
  17532. ;; `org-back-to-heading', to make it work also in invisible
  17533. ;; trees. And is uses an invisible-OK argument.
  17534. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  17535. ;; Furthermore, when used inside Org, finding the end of a large subtree
  17536. ;; with many children and grandchildren etc, this can be much faster
  17537. ;; than the outline version.
  17538. (org-back-to-heading invisible-OK)
  17539. (let ((first t)
  17540. (level (funcall outline-level)))
  17541. (if (and (org-mode-p) (< level 1000))
  17542. ;; A true heading (not a plain list item), in Org-mode
  17543. ;; This means we can easily find the end by looking
  17544. ;; only for the right number of stars. Using a regexp to do
  17545. ;; this is so much faster than using a Lisp loop.
  17546. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  17547. (forward-char 1)
  17548. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  17549. ;; something else, do it the slow way
  17550. (while (and (not (eobp))
  17551. (or first (> (funcall outline-level) level)))
  17552. (setq first nil)
  17553. (outline-next-heading)))
  17554. (unless to-heading
  17555. (if (memq (preceding-char) '(?\n ?\^M))
  17556. (progn
  17557. ;; Go to end of line before heading
  17558. (forward-char -1)
  17559. (if (memq (preceding-char) '(?\n ?\^M))
  17560. ;; leave blank line before heading
  17561. (forward-char -1))))))
  17562. (point))
  17563. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  17564. "Use Org version in org-mode, for dramatic speed-up."
  17565. (if (eq major-mode 'org-mode)
  17566. (progn
  17567. (org-end-of-subtree nil t)
  17568. (unless (eobp) (backward-char 1)))
  17569. ad-do-it))
  17570. (defun org-forward-same-level (arg &optional invisible-ok)
  17571. "Move forward to the arg'th subheading at same level as this one.
  17572. Stop at the first and last subheadings of a superior heading."
  17573. (interactive "p")
  17574. (org-back-to-heading invisible-ok)
  17575. (org-on-heading-p)
  17576. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17577. (re (format "^\\*\\{1,%d\\} " level))
  17578. l)
  17579. (forward-char 1)
  17580. (while (> arg 0)
  17581. (while (and (re-search-forward re nil 'move)
  17582. (setq l (- (match-end 0) (match-beginning 0) 1))
  17583. (= l level)
  17584. (not invisible-ok)
  17585. (progn (backward-char 1) (org-invisible-p)))
  17586. (if (< l level) (setq arg 1)))
  17587. (setq arg (1- arg)))
  17588. (beginning-of-line 1)))
  17589. (defun org-backward-same-level (arg &optional invisible-ok)
  17590. "Move backward to the arg'th subheading at same level as this one.
  17591. Stop at the first and last subheadings of a superior heading."
  17592. (interactive "p")
  17593. (org-back-to-heading)
  17594. (org-on-heading-p)
  17595. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  17596. (re (format "^\\*\\{1,%d\\} " level))
  17597. l)
  17598. (while (> arg 0)
  17599. (while (and (re-search-backward re nil 'move)
  17600. (setq l (- (match-end 0) (match-beginning 0) 1))
  17601. (= l level)
  17602. (not invisible-ok)
  17603. (org-invisible-p))
  17604. (if (< l level) (setq arg 1)))
  17605. (setq arg (1- arg)))))
  17606. (defun org-show-subtree ()
  17607. "Show everything after this heading at deeper levels."
  17608. (outline-flag-region
  17609. (point)
  17610. (save-excursion
  17611. (org-end-of-subtree t t))
  17612. nil))
  17613. (defun org-show-entry ()
  17614. "Show the body directly following this heading.
  17615. Show the heading too, if it is currently invisible."
  17616. (interactive)
  17617. (save-excursion
  17618. (condition-case nil
  17619. (progn
  17620. (org-back-to-heading t)
  17621. (outline-flag-region
  17622. (max (point-min) (1- (point)))
  17623. (save-excursion
  17624. (if (re-search-forward
  17625. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  17626. (match-beginning 1)
  17627. (point-max)))
  17628. nil)
  17629. (org-cycle-hide-drawers 'children))
  17630. (error nil))))
  17631. (defun org-make-options-regexp (kwds &optional extra)
  17632. "Make a regular expression for keyword lines."
  17633. (concat
  17634. "^"
  17635. "#?[ \t]*\\+\\("
  17636. (mapconcat 'regexp-quote kwds "\\|")
  17637. (if extra (concat "\\|" extra))
  17638. "\\):[ \t]*"
  17639. "\\(.*\\)"))
  17640. ;; Make isearch reveal the necessary context
  17641. (defun org-isearch-end ()
  17642. "Reveal context after isearch exits."
  17643. (when isearch-success ; only if search was successful
  17644. (if (featurep 'xemacs)
  17645. ;; Under XEmacs, the hook is run in the correct place,
  17646. ;; we directly show the context.
  17647. (org-show-context 'isearch)
  17648. ;; In Emacs the hook runs *before* restoring the overlays.
  17649. ;; So we have to use a one-time post-command-hook to do this.
  17650. ;; (Emacs 22 has a special variable, see function `org-mode')
  17651. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17652. isearch-mode-end-hook-quit)
  17653. ;; Only when the isearch was not quitted.
  17654. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17655. 'append 'local)))))
  17656. (defun org-isearch-post-command ()
  17657. "Remove self from hook, and show context."
  17658. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17659. (org-show-context 'isearch))
  17660. ;;;; Integration with and fixes for other packages
  17661. ;;; Imenu support
  17662. (defvar org-imenu-markers nil
  17663. "All markers currently used by Imenu.")
  17664. (make-variable-buffer-local 'org-imenu-markers)
  17665. (defun org-imenu-new-marker (&optional pos)
  17666. "Return a new marker for use by Imenu, and remember the marker."
  17667. (let ((m (make-marker)))
  17668. (move-marker m (or pos (point)))
  17669. (push m org-imenu-markers)
  17670. m))
  17671. (defun org-imenu-get-tree ()
  17672. "Produce the index for Imenu."
  17673. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17674. (setq org-imenu-markers nil)
  17675. (let* ((n org-imenu-depth)
  17676. (re (concat "^" outline-regexp))
  17677. (subs (make-vector (1+ n) nil))
  17678. (last-level 0)
  17679. m level head)
  17680. (save-excursion
  17681. (save-restriction
  17682. (widen)
  17683. (goto-char (point-max))
  17684. (while (re-search-backward re nil t)
  17685. (setq level (org-reduced-level (funcall outline-level)))
  17686. (when (<= level n)
  17687. (looking-at org-complex-heading-regexp)
  17688. (setq head (org-link-display-format
  17689. (org-match-string-no-properties 4))
  17690. m (org-imenu-new-marker))
  17691. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17692. (if (>= level last-level)
  17693. (push (cons head m) (aref subs level))
  17694. (push (cons head (aref subs (1+ level))) (aref subs level))
  17695. (loop for i from (1+ level) to n do (aset subs i nil)))
  17696. (setq last-level level)))))
  17697. (aref subs 1)))
  17698. (eval-after-load "imenu"
  17699. '(progn
  17700. (add-hook 'imenu-after-jump-hook
  17701. (lambda ()
  17702. (if (eq major-mode 'org-mode)
  17703. (org-show-context 'org-goto))))))
  17704. (defun org-link-display-format (link)
  17705. "Replace a link with either the description, or the link target
  17706. if no description is present"
  17707. (save-match-data
  17708. (if (string-match org-bracket-link-analytic-regexp link)
  17709. (replace-match (if (match-end 5)
  17710. (match-string 5 link)
  17711. (concat (match-string 1 link)
  17712. (match-string 3 link)))
  17713. nil t link)
  17714. link)))
  17715. ;; Speedbar support
  17716. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17717. "Overlay marking the agenda restriction line in speedbar.")
  17718. (overlay-put org-speedbar-restriction-lock-overlay
  17719. 'face 'org-agenda-restriction-lock)
  17720. (overlay-put org-speedbar-restriction-lock-overlay
  17721. 'help-echo "Agendas are currently limited to this item.")
  17722. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17723. (defun org-speedbar-set-agenda-restriction ()
  17724. "Restrict future agenda commands to the location at point in speedbar.
  17725. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17726. (interactive)
  17727. (require 'org-agenda)
  17728. (let (p m tp np dir txt)
  17729. (cond
  17730. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17731. 'org-imenu t))
  17732. (setq m (get-text-property p 'org-imenu-marker))
  17733. (with-current-buffer (marker-buffer m)
  17734. (goto-char m)
  17735. (org-agenda-set-restriction-lock 'subtree)))
  17736. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17737. 'speedbar-function 'speedbar-find-file))
  17738. (setq tp (previous-single-property-change
  17739. (1+ p) 'speedbar-function)
  17740. np (next-single-property-change
  17741. tp 'speedbar-function)
  17742. dir (speedbar-line-directory)
  17743. txt (buffer-substring-no-properties (or tp (point-min))
  17744. (or np (point-max))))
  17745. (with-current-buffer (find-file-noselect
  17746. (let ((default-directory dir))
  17747. (expand-file-name txt)))
  17748. (unless (org-mode-p)
  17749. (error "Cannot restrict to non-Org-mode file"))
  17750. (org-agenda-set-restriction-lock 'file)))
  17751. (t (error "Don't know how to restrict Org-mode's agenda")))
  17752. (move-overlay org-speedbar-restriction-lock-overlay
  17753. (point-at-bol) (point-at-eol))
  17754. (setq current-prefix-arg nil)
  17755. (org-agenda-maybe-redo)))
  17756. (eval-after-load "speedbar"
  17757. '(progn
  17758. (speedbar-add-supported-extension ".org")
  17759. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17760. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17761. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17762. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17763. (add-hook 'speedbar-visiting-tag-hook
  17764. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17765. ;;; Fixes and Hacks for problems with other packages
  17766. ;; Make flyspell not check words in links, to not mess up our keymap
  17767. (defun org-mode-flyspell-verify ()
  17768. "Don't let flyspell put overlays at active buttons."
  17769. (and (not (get-text-property (point) 'keymap))
  17770. (not (get-text-property (point) 'org-no-flyspell))))
  17771. (defun org-remove-flyspell-overlays-in (beg end)
  17772. "Remove flyspell overlays in region."
  17773. (and (org-bound-and-true-p flyspell-mode)
  17774. (fboundp 'flyspell-delete-region-overlays)
  17775. (flyspell-delete-region-overlays beg end))
  17776. (add-text-properties beg end '(org-no-flyspell t)))
  17777. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17778. (eval-after-load "bookmark"
  17779. '(if (boundp 'bookmark-after-jump-hook)
  17780. ;; We can use the hook
  17781. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17782. ;; Hook not available, use advice
  17783. (defadvice bookmark-jump (after org-make-visible activate)
  17784. "Make the position visible."
  17785. (org-bookmark-jump-unhide))))
  17786. ;; Make sure saveplace shows the location if it was hidden
  17787. (eval-after-load "saveplace"
  17788. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17789. "Make the position visible."
  17790. (org-bookmark-jump-unhide)))
  17791. ;; Make sure ecb shows the location if it was hidden
  17792. (eval-after-load "ecb"
  17793. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17794. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17795. (if (eq major-mode 'org-mode)
  17796. (org-show-context))))
  17797. (defun org-bookmark-jump-unhide ()
  17798. "Unhide the current position, to show the bookmark location."
  17799. (and (org-mode-p)
  17800. (or (org-invisible-p)
  17801. (save-excursion (goto-char (max (point-min) (1- (point))))
  17802. (org-invisible-p)))
  17803. (org-show-context 'bookmark-jump)))
  17804. ;; Make session.el ignore our circular variable
  17805. (eval-after-load "session"
  17806. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17807. ;;;; Experimental code
  17808. (defun org-closed-in-range ()
  17809. "Sparse tree of items closed in a certain time range.
  17810. Still experimental, may disappear in the future."
  17811. (interactive)
  17812. ;; Get the time interval from the user.
  17813. (let* ((time1 (org-float-time
  17814. (org-read-date nil 'to-time nil "Starting date: ")))
  17815. (time2 (org-float-time
  17816. (org-read-date nil 'to-time nil "End date:")))
  17817. ;; callback function
  17818. (callback (lambda ()
  17819. (let ((time
  17820. (org-float-time
  17821. (apply 'encode-time
  17822. (org-parse-time-string
  17823. (match-string 1))))))
  17824. ;; check if time in interval
  17825. (and (>= time time1) (<= time time2))))))
  17826. ;; make tree, check each match with the callback
  17827. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17828. ;;;; Finish up
  17829. (provide 'org)
  17830. (run-hooks 'org-load-hook)
  17831. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17832. ;;; org.el ends here