org.el 597 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071
  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
  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: 6.24trans
  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. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.24trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  155. (const :tag " vm: Links to VM folders/messages" org-vm)
  156. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  157. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  158. (const :tag " mouse: Additional mouse support" org-mouse)
  159. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  160. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  161. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  162. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  163. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  164. (const :tag "C collector: Collect properties into tables" org-collector)
  165. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  166. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  167. (const :tag "C eval: Include command output as text" org-eval)
  168. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  169. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  170. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  171. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  172. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  173. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  174. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  175. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  176. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  177. (const :tag "C R: Computation using the R language" org-R)
  178. (const :tag "C registry: A registry for Org links" org-registry)
  179. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  180. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  181. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  182. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  183. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  184. (defcustom org-support-shift-select nil
  185. "Non-nil means, make shift-cursor commands select text when possible.
  186. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  187. selecting a region, or enlarge thusly regions started in this way.
  188. In Org-mode, in special contexts, these same keys are used for other
  189. purposes, important enough to compete with shift selection. Org tries
  190. to balance these needs by supporting `shift-select-mode' outside these
  191. special contexts, under control of this variable.
  192. The default of this variable is nil, to avoid confusing behavior. Shifted
  193. cursor keys will then execute Org commands in the following contexts:
  194. - on a headline, changing TODO state (left/right) and priority (up/down)
  195. - on a time stamp, changing the time
  196. - in a plain list item, changing the bullet type
  197. - in a property definition line, switching between allowed values
  198. - in the BEGIN line of a clock table (changing the time block).
  199. Outside these contexts, the commands will throw an error.
  200. When this variable is t and the cursor is not in a special context,
  201. Org-mode will support shift-selection for making and enlarging regions.
  202. To make this more effective, the bullet cycling will no longer happen
  203. anywhere in an item line, but only if the cursor is exactly on the bullet.
  204. If you set this variable to the symbol `always', then the keys
  205. will not be special in headlines, property lines, and item lines, to make
  206. shift selection work there as well. If this is what you want, you can
  207. use the following alternative commands: `C-c C-t' and `C-c ,' to
  208. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  209. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  210. edited by hand or in column view.
  211. However, when the cursor is on a timestamp, shift-cursor commands
  212. will still edit the time stamp - this is just too good to give up.
  213. XEmacs user should have this variable set to nil, because shift-select-mode
  214. is Emacs 23 only."
  215. :group 'org
  216. :type '(choice
  217. (const :tag "Never" nil)
  218. (const :tag "When outside special context" t)
  219. (const :tag "Everywhere except timestamps" always)))
  220. (defgroup org-startup nil
  221. "Options concerning startup of Org-mode."
  222. :tag "Org Startup"
  223. :group 'org)
  224. (defcustom org-startup-folded t
  225. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  226. This can also be configured on a per-file basis by adding one of
  227. the following lines anywhere in the buffer:
  228. #+STARTUP: fold
  229. #+STARTUP: nofold
  230. #+STARTUP: content"
  231. :group 'org-startup
  232. :type '(choice
  233. (const :tag "nofold: show all" nil)
  234. (const :tag "fold: overview" t)
  235. (const :tag "content: all headlines" content)))
  236. (defcustom org-startup-truncated t
  237. "Non-nil means, entering Org-mode will set `truncate-lines'.
  238. This is useful since some lines containing links can be very long and
  239. uninteresting. Also tables look terrible when wrapped."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (defcustom org-startup-align-all-tables nil
  243. "Non-nil means, align all tables when visiting a file.
  244. This is useful when the column width in tables is forced with <N> cookies
  245. in table fields. Such tables will look correct only after the first re-align.
  246. This can also be configured on a per-file basis by adding one of
  247. the following lines anywhere in the buffer:
  248. #+STARTUP: align
  249. #+STARTUP: noalign"
  250. :group 'org-startup
  251. :type 'boolean)
  252. (defcustom org-insert-mode-line-in-empty-file nil
  253. "Non-nil means insert the first line setting Org-mode in empty files.
  254. When the function `org-mode' is called interactively in an empty file, this
  255. normally means that the file name does not automatically trigger Org-mode.
  256. To ensure that the file will always be in Org-mode in the future, a
  257. line enforcing Org-mode will be inserted into the buffer, if this option
  258. has been set."
  259. :group 'org-startup
  260. :type 'boolean)
  261. (defcustom org-replace-disputed-keys nil
  262. "Non-nil means use alternative key bindings for some keys.
  263. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  264. These keys are also used by other packages like shift-selection-mode'
  265. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  266. If you want to use Org-mode together with one of these other modes,
  267. or more generally if you would like to move some Org-mode commands to
  268. other keys, set this variable and configure the keys with the variable
  269. `org-disputed-keys'.
  270. This option is only relevant at load-time of Org-mode, and must be set
  271. *before* org.el is loaded. Changing it requires a restart of Emacs to
  272. become effective."
  273. :group 'org-startup
  274. :type 'boolean)
  275. (defcustom org-use-extra-keys nil
  276. "Non-nil means use extra key sequence definitions for certain
  277. commands. This happens automatically if you run XEmacs or if
  278. window-system is nil. This variable lets you do the same
  279. manually. You must set it before loading org.
  280. Example: on Carbon Emacs 22 running graphically, with an external
  281. keyboard on a Powerbook, the default way of setting M-left might
  282. not work for either Alt or ESC. Setting this variable will make
  283. it work for ESC."
  284. :group 'org-startup
  285. :type 'boolean)
  286. (if (fboundp 'defvaralias)
  287. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  288. (defcustom org-disputed-keys
  289. '(([(shift up)] . [(meta p)])
  290. ([(shift down)] . [(meta n)])
  291. ([(shift left)] . [(meta -)])
  292. ([(shift right)] . [(meta +)])
  293. ([(control shift right)] . [(meta shift +)])
  294. ([(control shift left)] . [(meta shift -)]))
  295. "Keys for which Org-mode and other modes compete.
  296. This is an alist, cars are the default keys, second element specifies
  297. the alternative to use when `org-replace-disputed-keys' is t.
  298. Keys can be specified in any syntax supported by `define-key'.
  299. The value of this option takes effect only at Org-mode's startup,
  300. therefore you'll have to restart Emacs to apply it after changing."
  301. :group 'org-startup
  302. :type 'alist)
  303. (defun org-key (key)
  304. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  305. Or return the original if not disputed."
  306. (if org-replace-disputed-keys
  307. (let* ((nkey (key-description key))
  308. (x (org-find-if (lambda (x)
  309. (equal (key-description (car x)) nkey))
  310. org-disputed-keys)))
  311. (if x (cdr x) key))
  312. key))
  313. (defun org-find-if (predicate seq)
  314. (catch 'exit
  315. (while seq
  316. (if (funcall predicate (car seq))
  317. (throw 'exit (car seq))
  318. (pop seq)))))
  319. (defun org-defkey (keymap key def)
  320. "Define a key, possibly translated, as returned by `org-key'."
  321. (define-key keymap (org-key key) def))
  322. (defcustom org-ellipsis nil
  323. "The ellipsis to use in the Org-mode outline.
  324. When nil, just use the standard three dots. When a string, use that instead,
  325. When a face, use the standard 3 dots, but with the specified face.
  326. The change affects only Org-mode (which will then use its own display table).
  327. Changing this requires executing `M-x org-mode' in a buffer to become
  328. effective."
  329. :group 'org-startup
  330. :type '(choice (const :tag "Default" nil)
  331. (face :tag "Face" :value org-warning)
  332. (string :tag "String" :value "...#")))
  333. (defvar org-display-table nil
  334. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  335. (defgroup org-keywords nil
  336. "Keywords in Org-mode."
  337. :tag "Org Keywords"
  338. :group 'org)
  339. (defcustom org-deadline-string "DEADLINE:"
  340. "String to mark deadline entries.
  341. A deadline is this string, followed by a time stamp. Should be a word,
  342. terminated by a colon. You can insert a schedule keyword and
  343. a timestamp with \\[org-deadline].
  344. Changes become only effective after restarting Emacs."
  345. :group 'org-keywords
  346. :type 'string)
  347. (defcustom org-scheduled-string "SCHEDULED:"
  348. "String to mark scheduled TODO entries.
  349. A schedule is this string, followed by a time stamp. Should be a word,
  350. terminated by a colon. You can insert a schedule keyword and
  351. a timestamp with \\[org-schedule].
  352. Changes become only effective after restarting Emacs."
  353. :group 'org-keywords
  354. :type 'string)
  355. (defcustom org-closed-string "CLOSED:"
  356. "String used as the prefix for timestamps logging closing a TODO entry."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-clock-string "CLOCK:"
  360. "String used as prefix for timestamps clocking work hours on an item."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-comment-string "COMMENT"
  364. "Entries starting with this keyword will never be exported.
  365. An entry can be toggled between COMMENT and normal with
  366. \\[org-toggle-comment].
  367. Changes become only effective after restarting Emacs."
  368. :group 'org-keywords
  369. :type 'string)
  370. (defcustom org-quote-string "QUOTE"
  371. "Entries starting with this keyword will be exported in fixed-width font.
  372. Quoting applies only to the text in the entry following the headline, and does
  373. not extend beyond the next headline, even if that is lower level.
  374. An entry can be toggled between QUOTE and normal with
  375. \\[org-toggle-fixed-width-section]."
  376. :group 'org-keywords
  377. :type 'string)
  378. (defconst org-repeat-re
  379. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  380. "Regular expression for specifying repeated events.
  381. After a match, group 1 contains the repeat expression.")
  382. (defgroup org-structure nil
  383. "Options concerning the general structure of Org-mode files."
  384. :tag "Org Structure"
  385. :group 'org)
  386. (defgroup org-reveal-location nil
  387. "Options about how to make context of a location visible."
  388. :tag "Org Reveal Location"
  389. :group 'org-structure)
  390. (defconst org-context-choice
  391. '(choice
  392. (const :tag "Always" t)
  393. (const :tag "Never" nil)
  394. (repeat :greedy t :tag "Individual contexts"
  395. (cons
  396. (choice :tag "Context"
  397. (const agenda)
  398. (const org-goto)
  399. (const occur-tree)
  400. (const tags-tree)
  401. (const link-search)
  402. (const mark-goto)
  403. (const bookmark-jump)
  404. (const isearch)
  405. (const default))
  406. (boolean))))
  407. "Contexts for the reveal options.")
  408. (defcustom org-show-hierarchy-above '((default . t))
  409. "Non-nil means, show full hierarchy when revealing a location.
  410. Org-mode often shows locations in an org-mode file which might have
  411. been invisible before. When this is set, the hierarchy of headings
  412. above the exposed location is shown.
  413. Turning this off for example for sparse trees makes them very compact.
  414. Instead of t, this can also be an alist specifying this option for different
  415. contexts. Valid contexts are
  416. agenda when exposing an entry from the agenda
  417. org-goto when using the command `org-goto' on key C-c C-j
  418. occur-tree when using the command `org-occur' on key C-c /
  419. tags-tree when constructing a sparse tree based on tags matches
  420. link-search when exposing search matches associated with a link
  421. mark-goto when exposing the jump goal of a mark
  422. bookmark-jump when exposing a bookmark location
  423. isearch when exiting from an incremental search
  424. default default for all contexts not set explicitly"
  425. :group 'org-reveal-location
  426. :type org-context-choice)
  427. (defcustom org-show-following-heading '((default . nil))
  428. "Non-nil means, show following heading when revealing a location.
  429. Org-mode often shows locations in an org-mode file which might have
  430. been invisible before. When this is set, the heading following the
  431. match is shown.
  432. Turning this off for example for sparse trees makes them very compact,
  433. but makes it harder to edit the location of the match. In such a case,
  434. use the command \\[org-reveal] to show more context.
  435. Instead of t, this can also be an alist specifying this option for different
  436. contexts. See `org-show-hierarchy-above' for valid contexts."
  437. :group 'org-reveal-location
  438. :type org-context-choice)
  439. (defcustom org-show-siblings '((default . nil) (isearch t))
  440. "Non-nil means, show all sibling heading when revealing a location.
  441. Org-mode often shows locations in an org-mode file which might have
  442. been invisible before. When this is set, the sibling of the current entry
  443. heading are all made visible. If `org-show-hierarchy-above' is t,
  444. the same happens on each level of the hierarchy above the current entry.
  445. By default this is on for the isearch context, off for all other contexts.
  446. Turning this off for example for sparse trees makes them very compact,
  447. but makes it harder to edit the location of the match. In such a case,
  448. use the command \\[org-reveal] to show more context.
  449. Instead of t, this can also be an alist specifying this option for different
  450. contexts. See `org-show-hierarchy-above' for valid contexts."
  451. :group 'org-reveal-location
  452. :type org-context-choice)
  453. (defcustom org-show-entry-below '((default . nil))
  454. "Non-nil means, show the entry below a headline when revealing a location.
  455. Org-mode often shows locations in an org-mode file which might have
  456. been invisible before. When this is set, the text below the headline that is
  457. exposed is also shown.
  458. By default this is off for all contexts.
  459. Instead of t, this can also be an alist specifying this option for different
  460. contexts. See `org-show-hierarchy-above' for valid contexts."
  461. :group 'org-reveal-location
  462. :type org-context-choice)
  463. (defcustom org-indirect-buffer-display 'other-window
  464. "How should indirect tree buffers be displayed?
  465. This applies to indirect buffers created with the commands
  466. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  467. Valid values are:
  468. current-window Display in the current window
  469. other-window Just display in another window.
  470. dedicated-frame Create one new frame, and re-use it each time.
  471. new-frame Make a new frame each time. Note that in this case
  472. previously-made indirect buffers are kept, and you need to
  473. kill these buffers yourself."
  474. :group 'org-structure
  475. :group 'org-agenda-windows
  476. :type '(choice
  477. (const :tag "In current window" current-window)
  478. (const :tag "In current frame, other window" other-window)
  479. (const :tag "Each time a new frame" new-frame)
  480. (const :tag "One dedicated frame" dedicated-frame)))
  481. (defgroup org-cycle nil
  482. "Options concerning visibility cycling in Org-mode."
  483. :tag "Org Cycle"
  484. :group 'org-structure)
  485. (defcustom org-cycle-max-level nil
  486. "Maximum level which should still be subject to visibility cycling.
  487. Levels higher than this will, for cycling, be treated as text, not a headline.
  488. When `org-odd-levels-only' is set, a value of N in this variable actually
  489. means 2N-1 stars as the limiting headline.
  490. When nil, cycle all levels.
  491. Note that the limiting level of cycling is also influenced by
  492. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  493. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  494. than its value."
  495. :group 'org-cycle
  496. :type '(choice
  497. (const :tag "No limit" nil)
  498. (integer :tag "Maximum level")))
  499. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  500. "Names of drawers. Drawers are not opened by cycling on the headline above.
  501. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  502. this:
  503. :DRAWERNAME:
  504. .....
  505. :END:
  506. The drawer \"PROPERTIES\" is special for capturing properties through
  507. the property API.
  508. Drawers can be defined on the per-file basis with a line like:
  509. #+DRAWERS: HIDDEN STATE PROPERTIES"
  510. :group 'org-structure
  511. :group 'org-cycle
  512. :type '(repeat (string :tag "Drawer Name")))
  513. (defcustom org-cycle-global-at-bob nil
  514. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  515. This makes it possible to do global cycling without having to use S-TAB or
  516. C-u TAB. For this special case to work, the first line of the buffer
  517. must not be a headline - it may be empty or some other text. When used in
  518. this way, `org-cycle-hook' is disables temporarily, to make sure the
  519. cursor stays at the beginning of the buffer.
  520. When this option is nil, don't do anything special at the beginning
  521. of the buffer."
  522. :group 'org-cycle
  523. :type 'boolean)
  524. (defcustom org-cycle-emulate-tab t
  525. "Where should `org-cycle' emulate TAB.
  526. nil Never
  527. white Only in completely white lines
  528. whitestart Only at the beginning of lines, before the first non-white char
  529. t Everywhere except in headlines
  530. exc-hl-bol Everywhere except at the start of a headline
  531. If TAB is used in a place where it does not emulate TAB, the current subtree
  532. visibility is cycled."
  533. :group 'org-cycle
  534. :type '(choice (const :tag "Never" nil)
  535. (const :tag "Only in completely white lines" white)
  536. (const :tag "Before first char in a line" whitestart)
  537. (const :tag "Everywhere except in headlines" t)
  538. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  539. ))
  540. (defcustom org-cycle-separator-lines 2
  541. "Number of empty lines needed to keep an empty line between collapsed trees.
  542. If you leave an empty line between the end of a subtree and the following
  543. headline, this empty line is hidden when the subtree is folded.
  544. Org-mode will leave (exactly) one empty line visible if the number of
  545. empty lines is equal or larger to the number given in this variable.
  546. So the default 2 means, at least 2 empty lines after the end of a subtree
  547. are needed to produce free space between a collapsed subtree and the
  548. following headline.
  549. Special case: when 0, never leave empty lines in collapsed view."
  550. :group 'org-cycle
  551. :type 'integer)
  552. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  553. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  554. org-cycle-hide-drawers
  555. org-cycle-show-empty-lines
  556. org-optimize-window-after-visibility-change)
  557. "Hook that is run after `org-cycle' has changed the buffer visibility.
  558. The function(s) in this hook must accept a single argument which indicates
  559. the new state that was set by the most recent `org-cycle' command. The
  560. argument is a symbol. After a global state change, it can have the values
  561. `overview', `content', or `all'. After a local state change, it can have
  562. the values `folded', `children', or `subtree'."
  563. :group 'org-cycle
  564. :type 'hook)
  565. (defgroup org-edit-structure nil
  566. "Options concerning structure editing in Org-mode."
  567. :tag "Org Edit Structure"
  568. :group 'org-structure)
  569. (defcustom org-odd-levels-only nil
  570. "Non-nil means, skip even levels and only use odd levels for the outline.
  571. This has the effect that two stars are being added/taken away in
  572. promotion/demotion commands. It also influences how levels are
  573. handled by the exporters.
  574. Changing it requires restart of `font-lock-mode' to become effective
  575. for fontification also in regions already fontified.
  576. You may also set this on a per-file basis by adding one of the following
  577. lines to the buffer:
  578. #+STARTUP: odd
  579. #+STARTUP: oddeven"
  580. :group 'org-edit-structure
  581. :group 'org-font-lock
  582. :type 'boolean)
  583. (defcustom org-adapt-indentation t
  584. "Non-nil means, adapt indentation when promoting and demoting.
  585. When this is set and the *entire* text in an entry is indented, the
  586. indentation is increased by one space in a demotion command, and
  587. decreased by one in a promotion command. If any line in the entry
  588. body starts at column 0, indentation is not changed at all."
  589. :group 'org-edit-structure
  590. :type 'boolean)
  591. (defcustom org-special-ctrl-a/e nil
  592. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  593. When t, `C-a' will bring back the cursor to the beginning of the
  594. headline text, i.e. after the stars and after a possible TODO keyword.
  595. In an item, this will be the position after the bullet.
  596. When the cursor is already at that position, another `C-a' will bring
  597. it to the beginning of the line.
  598. `C-e' will jump to the end of the headline, ignoring the presence of tags
  599. in the headline. A second `C-e' will then jump to the true end of the
  600. line, after any tags.
  601. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  602. and only a directly following, identical keypress will bring the cursor
  603. to the special positions.
  604. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  605. set separately."
  606. :group 'org-edit-structure
  607. :type '(choice
  608. (const :tag "off" nil)
  609. (const :tag "after stars/bullet and before tags first" t)
  610. (const :tag "true line boundary first" reversed)
  611. (cons :tag "Set C-a and C-e separately"
  612. (choice :tag "Special C-a"
  613. (const :tag "off" nil)
  614. (const :tag "after stars/bullet first" t)
  615. (const :tag "before stars/bullet first" reversed))
  616. (choice :tag "Special C-e"
  617. (const :tag "off" nil)
  618. (const :tag "before tags first" t)
  619. (const :tag "after tags first" reversed)))))
  620. (if (fboundp 'defvaralias)
  621. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  622. (defcustom org-special-ctrl-k nil
  623. "Non-nil means `C-k' will behave specially in headlines.
  624. When nil, `C-k' will call the default `kill-line' command.
  625. When t, the following will happen while the cursor is in the headline:
  626. - When the cursor is at the beginning of a headline, kill the entire
  627. line and possible the folded subtree below the line.
  628. - When in the middle of the headline text, kill the headline up to the tags.
  629. - When after the headline text, kill the tags."
  630. :group 'org-edit-structure
  631. :type 'boolean)
  632. (defcustom org-yank-folded-subtrees t
  633. "Non-nil means, when yanking subtrees, fold them.
  634. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  635. it starts with a heading and all other headings in it are either children
  636. or siblings, then fold all the subtrees. However, do this only if no
  637. text after the yank would be swallowed into a folded tree by this action."
  638. :group 'org-edit-structure
  639. :type 'boolean)
  640. (defcustom org-yank-adjusted-subtrees nil
  641. "Non-nil means, when yanking subtrees, adjust the level.
  642. With this setting, `org-paste-subtree' is used to insert the subtree, see
  643. this function for details."
  644. :group 'org-edit-structure
  645. :type 'boolean)
  646. (defcustom org-M-RET-may-split-line '((default . t))
  647. "Non-nil means, M-RET will split the line at the cursor position.
  648. When nil, it will go to the end of the line before making a
  649. new line.
  650. You may also set this option in a different way for different
  651. contexts. Valid contexts are:
  652. headline when creating a new headline
  653. item when creating a new item
  654. table in a table field
  655. default the value to be used for all contexts not explicitly
  656. customized"
  657. :group 'org-structure
  658. :group 'org-table
  659. :type '(choice
  660. (const :tag "Always" t)
  661. (const :tag "Never" nil)
  662. (repeat :greedy t :tag "Individual contexts"
  663. (cons
  664. (choice :tag "Context"
  665. (const headline)
  666. (const item)
  667. (const table)
  668. (const default))
  669. (boolean)))))
  670. (defcustom org-insert-heading-respect-content nil
  671. "Non-nil means, insert new headings after the current subtree.
  672. When nil, the new heading is created directly after the current line.
  673. The commands \\[org-insert-heading-respect-content] and
  674. \\[org-insert-todo-heading-respect-content] turn this variable on
  675. for the duration of the command."
  676. :group 'org-structure
  677. :type 'boolean)
  678. (defcustom org-blank-before-new-entry '((heading . auto)
  679. (plain-list-item . auto))
  680. "Should `org-insert-heading' leave a blank line before new heading/item?
  681. The value is an alist, with `heading' and `plain-list-item' as car,
  682. and a boolean flag as cdr. For plain lists, if the variable
  683. `org-empty-line-terminates-plain-lists' is set, the setting here
  684. is ignored and no empty line is inserted, to keep the list in tact."
  685. :group 'org-edit-structure
  686. :type '(list
  687. (cons (const heading)
  688. (choice (const :tag "Never" nil)
  689. (const :tag "Always" t)
  690. (const :tag "Auto" auto)))
  691. (cons (const plain-list-item)
  692. (choice (const :tag "Never" nil)
  693. (const :tag "Always" t)
  694. (const :tag "Auto" auto)))))
  695. (defcustom org-insert-heading-hook nil
  696. "Hook being run after inserting a new heading."
  697. :group 'org-edit-structure
  698. :type 'hook)
  699. (defcustom org-enable-fixed-width-editor t
  700. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  701. This currently only means, they are never auto-wrapped.
  702. When nil, such lines will be treated like ordinary lines.
  703. See also the QUOTE keyword."
  704. :group 'org-edit-structure
  705. :type 'boolean)
  706. (defcustom org-edit-src-region-extra nil
  707. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  708. For examples see the function `org-edit-src-find-region-and-lang'.
  709. The regular expression identifying the begin marker should end with a newline,
  710. and the regexp marking the end line should start with a newline, to make sure
  711. there are kept outside the narrowed region."
  712. :group 'org-edit-structure
  713. :type '(repeat
  714. (list
  715. (regexp :tag "begin regexp")
  716. (regexp :tag "end regexp")
  717. (choice :tag "language"
  718. (string :tag "specify")
  719. (integer :tag "from match group")
  720. (const :tag "from `lang' element")
  721. (const :tag "from `style' element")))))
  722. (defcustom org-coderef-label-format "(ref:%s)"
  723. "The default coderef format.
  724. This format string will be used to search for coderef labels in literal
  725. examples (EXAMPLE and SRC blocks). The format can be overwritten
  726. an individual literal example with the -f option, like
  727. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  728. ...
  729. #+END_SRC
  730. If you want to use this for HTML export, make sure that the format does
  731. not introduce special font-locking, and avoid the HTML special
  732. characters `<', `>', and `&'. The reason for this restriction is that
  733. the labels are searched for only after htmlize has done its job."
  734. :group 'org-edit-structure ; FIXME this is not in the right group
  735. :type 'string)
  736. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  737. "The mode that should be used to edit fixed-width regions.
  738. These are the regions where each line starts with a colon."
  739. :group 'org-edit-structure
  740. :type '(choice
  741. (const artist-mode)
  742. (const picture-mode)
  743. (const fundamental-mode)
  744. (function :tag "Other (specify)")))
  745. (defcustom org-goto-auto-isearch t
  746. "Non-nil means, typing characters in org-goto starts incremental search."
  747. :group 'org-edit-structure
  748. :type 'boolean)
  749. (defgroup org-sparse-trees nil
  750. "Options concerning sparse trees in Org-mode."
  751. :tag "Org Sparse Trees"
  752. :group 'org-structure)
  753. (defcustom org-highlight-sparse-tree-matches t
  754. "Non-nil means, highlight all matches that define a sparse tree.
  755. The highlights will automatically disappear the next time the buffer is
  756. changed by an edit command."
  757. :group 'org-sparse-trees
  758. :type 'boolean)
  759. (defcustom org-remove-highlights-with-change t
  760. "Non-nil means, any change to the buffer will remove temporary highlights.
  761. Such highlights are created by `org-occur' and `org-clock-display'.
  762. When nil, `C-c C-c needs to be used to get rid of the highlights.
  763. The highlights created by `org-preview-latex-fragment' always need
  764. `C-c C-c' to be removed."
  765. :group 'org-sparse-trees
  766. :group 'org-time
  767. :type 'boolean)
  768. (defcustom org-occur-hook '(org-first-headline-recenter)
  769. "Hook that is run after `org-occur' has constructed a sparse tree.
  770. This can be used to recenter the window to show as much of the structure
  771. as possible."
  772. :group 'org-sparse-trees
  773. :type 'hook)
  774. (defgroup org-imenu-and-speedbar nil
  775. "Options concerning imenu and speedbar in Org-mode."
  776. :tag "Org Imenu and Speedbar"
  777. :group 'org-structure)
  778. (defcustom org-imenu-depth 2
  779. "The maximum level for Imenu access to Org-mode headlines.
  780. This also applied for speedbar access."
  781. :group 'org-imenu-and-speedbar
  782. :type 'number)
  783. (defgroup org-table nil
  784. "Options concerning tables in Org-mode."
  785. :tag "Org Table"
  786. :group 'org)
  787. (defcustom org-enable-table-editor 'optimized
  788. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  789. When nil, such lines will be treated like ordinary lines.
  790. When equal to the symbol `optimized', the table editor will be optimized to
  791. do the following:
  792. - Automatic overwrite mode in front of whitespace in table fields.
  793. This makes the structure of the table stay in tact as long as the edited
  794. field does not exceed the column width.
  795. - Minimize the number of realigns. Normally, the table is aligned each time
  796. TAB or RET are pressed to move to another field. With optimization this
  797. happens only if changes to a field might have changed the column width.
  798. Optimization requires replacing the functions `self-insert-command',
  799. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  800. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  801. very good at guessing when a re-align will be necessary, but you can always
  802. force one with \\[org-ctrl-c-ctrl-c].
  803. If you would like to use the optimized version in Org-mode, but the
  804. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  805. This variable can be used to turn on and off the table editor during a session,
  806. but in order to toggle optimization, a restart is required.
  807. See also the variable `org-table-auto-blank-field'."
  808. :group 'org-table
  809. :type '(choice
  810. (const :tag "off" nil)
  811. (const :tag "on" t)
  812. (const :tag "on, optimized" optimized)))
  813. (defcustom org-self-insert-cluster-for-undo t
  814. "Non-nil means cluster self-insert commands for undo when possible.
  815. If this is set, then, like in the Emacs command loop, 20 consequtive
  816. characters will be undone together.
  817. This is configurable, because there is some impact on typing performance."
  818. :group 'org-table
  819. :type 'boolean)
  820. (defcustom org-table-tab-recognizes-table.el t
  821. "Non-nil means, TAB will automatically notice a table.el table.
  822. When it sees such a table, it moves point into it and - if necessary -
  823. calls `table-recognize-table'."
  824. :group 'org-table-editing
  825. :type 'boolean)
  826. (defgroup org-link nil
  827. "Options concerning links in Org-mode."
  828. :tag "Org Link"
  829. :group 'org)
  830. (defvar org-link-abbrev-alist-local nil
  831. "Buffer-local version of `org-link-abbrev-alist', which see.
  832. The value of this is taken from the #+LINK lines.")
  833. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  834. (defcustom org-link-abbrev-alist nil
  835. "Alist of link abbreviations.
  836. The car of each element is a string, to be replaced at the start of a link.
  837. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  838. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  839. [[linkkey:tag][description]]
  840. The 'linkkey' must be a word word, starting with a letter, followed
  841. by letters, numbers, '-' or '_'.
  842. If REPLACE is a string, the tag will simply be appended to create the link.
  843. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  844. the placeholder \"%h\" will cause a url-encoded version of the tag to
  845. be inserted at that point (see the function `url-hexify-string').
  846. REPLACE may also be a function that will be called with the tag as the
  847. only argument to create the link, which should be returned as a string.
  848. See the manual for examples."
  849. :group 'org-link
  850. :type '(repeat
  851. (cons
  852. (string :tag "Protocol")
  853. (choice
  854. (string :tag "Format")
  855. (function)))))
  856. (defcustom org-descriptive-links t
  857. "Non-nil means, hide link part and only show description of bracket links.
  858. Bracket links are like [[link][description]]. This variable sets the initial
  859. state in new org-mode buffers. The setting can then be toggled on a
  860. per-buffer basis from the Org->Hyperlinks menu."
  861. :group 'org-link
  862. :type 'boolean)
  863. (defcustom org-link-file-path-type 'adaptive
  864. "How the path name in file links should be stored.
  865. Valid values are:
  866. relative Relative to the current directory, i.e. the directory of the file
  867. into which the link is being inserted.
  868. absolute Absolute path, if possible with ~ for home directory.
  869. noabbrev Absolute path, no abbreviation of home directory.
  870. adaptive Use relative path for files in the current directory and sub-
  871. directories of it. For other files, use an absolute path."
  872. :group 'org-link
  873. :type '(choice
  874. (const relative)
  875. (const absolute)
  876. (const noabbrev)
  877. (const adaptive)))
  878. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  879. "Types of links that should be activated in Org-mode files.
  880. This is a list of symbols, each leading to the activation of a certain link
  881. type. In principle, it does not hurt to turn on most link types - there may
  882. be a small gain when turning off unused link types. The types are:
  883. bracket The recommended [[link][description]] or [[link]] links with hiding.
  884. angular Links in angular brackets that may contain whitespace like
  885. <bbdb:Carsten Dominik>.
  886. plain Plain links in normal text, no whitespace, like http://google.com.
  887. radio Text that is matched by a radio target, see manual for details.
  888. tag Tag settings in a headline (link to tag search).
  889. date Time stamps (link to calendar).
  890. footnote Footnote labels.
  891. Changing this variable requires a restart of Emacs to become effective."
  892. :group 'org-link
  893. :type '(set :greedy t
  894. (const :tag "Double bracket links (new style)" bracket)
  895. (const :tag "Angular bracket links (old style)" angular)
  896. (const :tag "Plain text links" plain)
  897. (const :tag "Radio target matches" radio)
  898. (const :tag "Tags" tag)
  899. (const :tag "Timestamps" date)
  900. (const :tag "Footnotes" footnote)))
  901. (defcustom org-make-link-description-function nil
  902. "Function to use to generate link descriptions from links. If
  903. nil the link location will be used. This function must take two
  904. parameters; the first is the link and the second the description
  905. org-insert-link has generated, and should return the description
  906. to use."
  907. :group 'org-link
  908. :type 'function)
  909. (defgroup org-link-store nil
  910. "Options concerning storing links in Org-mode."
  911. :tag "Org Store Link"
  912. :group 'org-link)
  913. (defcustom org-email-link-description-format "Email %c: %.30s"
  914. "Format of the description part of a link to an email or usenet message.
  915. The following %-escapes will be replaced by corresponding information:
  916. %F full \"From\" field
  917. %f name, taken from \"From\" field, address if no name
  918. %T full \"To\" field
  919. %t first name in \"To\" field, address if no name
  920. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  921. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  922. %s subject
  923. %m message-id.
  924. You may use normal field width specification between the % and the letter.
  925. This is for example useful to limit the length of the subject.
  926. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  927. :group 'org-link-store
  928. :type 'string)
  929. (defcustom org-from-is-user-regexp
  930. (let (r1 r2)
  931. (when (and user-mail-address (not (string= user-mail-address "")))
  932. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  933. (when (and user-full-name (not (string= user-full-name "")))
  934. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  935. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  936. "Regexp matched against the \"From:\" header of an email or usenet message.
  937. It should match if the message is from the user him/herself."
  938. :group 'org-link-store
  939. :type 'regexp)
  940. (defcustom org-link-to-org-use-id 'create-if-interactive
  941. "Non-nil means, storing a link to an Org file will use entry IDs.
  942. Note that before this variable is even considered, org-id must be loaded,
  943. to please customize `org-modules' and turn it on.
  944. The variable can have the following values:
  945. t Create an ID if needed to make a link to the current entry.
  946. create-if-interactive
  947. If `org-store-link' is called directly (interactively, as a user
  948. command), do create an ID to support the link. But when doing the
  949. job for remember, only use the ID if it already exists. The
  950. purpose of this setting is to avoid proliferation of unwanted
  951. IDs, just because you happen to be in an Org file when you
  952. call `org-remember' that automatically and preemptively
  953. creates a link. If you do want to get an ID link in a remember
  954. template to an entry not having an ID, create it first by
  955. explicitly creating a link to it, using `C-c C-l' first.
  956. use-existing
  957. Use existing ID, do not create one.
  958. nil Never use an ID to make a link, instead link using a text search for
  959. the headline text."
  960. :group 'org-link-store
  961. :type '(choice
  962. (const :tag "Create ID to make link" t)
  963. (const :tag "Create if string link interactively"
  964. 'create-if-interactive)
  965. (const :tag "Only use existing" 'use-existing)
  966. (const :tag "Do not use ID to create link" nil)))
  967. (defcustom org-context-in-file-links t
  968. "Non-nil means, file links from `org-store-link' contain context.
  969. A search string will be added to the file name with :: as separator and
  970. used to find the context when the link is activated by the command
  971. `org-open-at-point'.
  972. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  973. negates this setting for the duration of the command."
  974. :group 'org-link-store
  975. :type 'boolean)
  976. (defcustom org-keep-stored-link-after-insertion nil
  977. "Non-nil means, keep link in list for entire session.
  978. The command `org-store-link' adds a link pointing to the current
  979. location to an internal list. These links accumulate during a session.
  980. The command `org-insert-link' can be used to insert links into any
  981. Org-mode file (offering completion for all stored links). When this
  982. option is nil, every link which has been inserted once using \\[org-insert-link]
  983. will be removed from the list, to make completing the unused links
  984. more efficient."
  985. :group 'org-link-store
  986. :type 'boolean)
  987. (defgroup org-link-follow nil
  988. "Options concerning following links in Org-mode."
  989. :tag "Org Follow Link"
  990. :group 'org-link)
  991. (defcustom org-link-translation-function nil
  992. "Function to translate links with different syntax to Org syntax.
  993. This can be used to translate links created for example by the Planner
  994. or emacs-wiki packages to Org syntax.
  995. The function must accept two parameters, a TYPE containing the link
  996. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  997. which is everything after the link protocol. It should return a cons
  998. with possibly modified values of type and path.
  999. Org contains a function for this, so if you set this variable to
  1000. `org-translate-link-from-planner', you should be able follow many
  1001. links created by planner."
  1002. :group 'org-link-follow
  1003. :type 'function)
  1004. (defcustom org-follow-link-hook nil
  1005. "Hook that is run after a link has been followed."
  1006. :group 'org-link-follow
  1007. :type 'hook)
  1008. (defcustom org-tab-follows-link nil
  1009. "Non-nil means, on links TAB will follow the link.
  1010. Needs to be set before org.el is loaded.
  1011. This really should not be used, it does not make sense, and the
  1012. implementation is bad."
  1013. :group 'org-link-follow
  1014. :type 'boolean)
  1015. (defcustom org-return-follows-link nil
  1016. "Non-nil means, on links RET will follow the link.
  1017. Needs to be set before org.el is loaded."
  1018. :group 'org-link-follow
  1019. :type 'boolean)
  1020. (defcustom org-mouse-1-follows-link
  1021. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1022. "Non-nil means, mouse-1 on a link will follow the link.
  1023. A longer mouse click will still set point. Does not work on XEmacs.
  1024. Needs to be set before org.el is loaded."
  1025. :group 'org-link-follow
  1026. :type 'boolean)
  1027. (defcustom org-mark-ring-length 4
  1028. "Number of different positions to be recorded in the ring
  1029. Changing this requires a restart of Emacs to work correctly."
  1030. :group 'org-link-follow
  1031. :type 'integer)
  1032. (defcustom org-link-frame-setup
  1033. '((vm . vm-visit-folder-other-frame)
  1034. (gnus . gnus-other-frame)
  1035. (file . find-file-other-window))
  1036. "Setup the frame configuration for following links.
  1037. When following a link with Emacs, it may often be useful to display
  1038. this link in another window or frame. This variable can be used to
  1039. set this up for the different types of links.
  1040. For VM, use any of
  1041. `vm-visit-folder'
  1042. `vm-visit-folder-other-frame'
  1043. For Gnus, use any of
  1044. `gnus'
  1045. `gnus-other-frame'
  1046. `org-gnus-no-new-news'
  1047. For FILE, use any of
  1048. `find-file'
  1049. `find-file-other-window'
  1050. `find-file-other-frame'
  1051. For the calendar, use the variable `calendar-setup'.
  1052. For BBDB, it is currently only possible to display the matches in
  1053. another window."
  1054. :group 'org-link-follow
  1055. :type '(list
  1056. (cons (const vm)
  1057. (choice
  1058. (const vm-visit-folder)
  1059. (const vm-visit-folder-other-window)
  1060. (const vm-visit-folder-other-frame)))
  1061. (cons (const gnus)
  1062. (choice
  1063. (const gnus)
  1064. (const gnus-other-frame)
  1065. (const org-gnus-no-new-news)))
  1066. (cons (const file)
  1067. (choice
  1068. (const find-file)
  1069. (const find-file-other-window)
  1070. (const find-file-other-frame)))))
  1071. (defcustom org-display-internal-link-with-indirect-buffer nil
  1072. "Non-nil means, use indirect buffer to display infile links.
  1073. Activating internal links (from one location in a file to another location
  1074. in the same file) normally just jumps to the location. When the link is
  1075. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1076. another window. When this option is set, the other window actually displays
  1077. an indirect buffer clone of the current buffer, to avoid any visibility
  1078. changes to the current buffer."
  1079. :group 'org-link-follow
  1080. :type 'boolean)
  1081. (defcustom org-open-non-existing-files nil
  1082. "Non-nil means, `org-open-file' will open non-existing files.
  1083. When nil, an error will be generated."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-open-directory-means-index-dot-org nil
  1087. "Non-nil means, a link to a directory really means to index.org.
  1088. When nil, following a directory link will run dired or open a finder/explorer
  1089. window on that directory."
  1090. :group 'org-link-follow
  1091. :type 'boolean)
  1092. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1093. "Function and arguments to call for following mailto links.
  1094. This is a list with the first element being a lisp function, and the
  1095. remaining elements being arguments to the function. In string arguments,
  1096. %a will be replaced by the address, and %s will be replaced by the subject
  1097. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1098. :group 'org-link-follow
  1099. :type '(choice
  1100. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1101. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1102. (const :tag "message-mail" (message-mail "%a" "%s"))
  1103. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1104. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1105. "Non-nil means, ask for confirmation before executing shell links.
  1106. Shell links can be dangerous: just think about a link
  1107. [[shell:rm -rf ~/*][Google Search]]
  1108. This link would show up in your Org-mode document as \"Google Search\",
  1109. but really it would remove your entire home directory.
  1110. Therefore we advise against setting this variable to nil.
  1111. Just change it to `y-or-n-p' if you want to confirm with a
  1112. single keystroke rather than having to type \"yes\"."
  1113. :group 'org-link-follow
  1114. :type '(choice
  1115. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1116. (const :tag "with y-or-n (faster)" y-or-n-p)
  1117. (const :tag "no confirmation (dangerous)" nil)))
  1118. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1119. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1120. Elisp links can be dangerous: just think about a link
  1121. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1122. This link would show up in your Org-mode document as \"Google Search\",
  1123. but really it would remove your entire home directory.
  1124. Therefore we advise against setting this variable to nil.
  1125. Just change it to `y-or-n-p' if you want to confirm with a
  1126. single keystroke rather than having to type \"yes\"."
  1127. :group 'org-link-follow
  1128. :type '(choice
  1129. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1130. (const :tag "with y-or-n (faster)" y-or-n-p)
  1131. (const :tag "no confirmation (dangerous)" nil)))
  1132. (defconst org-file-apps-defaults-gnu
  1133. '((remote . emacs)
  1134. (system . mailcap)
  1135. (t . mailcap))
  1136. "Default file applications on a UNIX or GNU/Linux system.
  1137. See `org-file-apps'.")
  1138. (defconst org-file-apps-defaults-macosx
  1139. '((remote . emacs)
  1140. (t . "open %s")
  1141. (system . "open %s")
  1142. ("ps.gz" . "gv %s")
  1143. ("eps.gz" . "gv %s")
  1144. ("dvi" . "xdvi %s")
  1145. ("fig" . "xfig %s"))
  1146. "Default file applications on a MacOS X system.
  1147. The system \"open\" is known as a default, but we use X11 applications
  1148. for some files for which the OS does not have a good default.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-windowsnt
  1151. (list
  1152. '(remote . emacs)
  1153. (cons t
  1154. (list (if (featurep 'xemacs)
  1155. 'mswindows-shell-execute
  1156. 'w32-shell-execute)
  1157. "open" 'file))
  1158. (cons 'system
  1159. (list (if (featurep 'xemacs)
  1160. 'mswindows-shell-execute
  1161. 'w32-shell-execute)
  1162. "open" 'file)))
  1163. "Default file applications on a Windows NT system.
  1164. The system \"open\" is used for most files.
  1165. See `org-file-apps'.")
  1166. (defcustom org-file-apps
  1167. '(
  1168. (auto-mode . emacs)
  1169. ("\\.x?html?\\'" . default)
  1170. ("\\.pdf\\'" . default)
  1171. )
  1172. "External applications for opening `file:path' items in a document.
  1173. Org-mode uses system defaults for different file types, but
  1174. you can use this variable to set the application for a given file
  1175. extension. The entries in this list are cons cells where the car identifies
  1176. files and the cdr the corresponding command. Possible values for the
  1177. file identifier are
  1178. \"regex\" Regular expression matched against the file name. For backward
  1179. compatibility, this can also be a string with only alphanumeric
  1180. characters, which is then interpreted as an extension.
  1181. `directory' Matches a directory
  1182. `remote' Matches a remote file, accessible through tramp or efs.
  1183. Remote files most likely should be visited through Emacs
  1184. because external applications cannot handle such paths.
  1185. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1186. so all files Emacs knows how to handle. Using this with
  1187. command `emacs' will open most files in Emacs. Beware that this
  1188. will also open html files inside Emacs, unless you add
  1189. (\"html\" . default) to the list as well.
  1190. t Default for files not matched by any of the other options.
  1191. `system' The system command to open files, like `open' on Windows
  1192. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1193. that will be selected if you call `C-c C-o' with a double
  1194. `C-u C-u' prefix.
  1195. Possible values for the command are:
  1196. `emacs' The file will be visited by the current Emacs process.
  1197. `default' Use the default application for this file type, which is the
  1198. association for t in the list, most likely in the system-specific
  1199. part.
  1200. This can be used to overrule an unwanted setting in the
  1201. system-specific variable.
  1202. `system' Use the system command for opening files, like \"open\".
  1203. This command is specified by the entry whose car is `system'.
  1204. Most likely, the system-specific version of this variable
  1205. does define this command, but you can overrule/replace it
  1206. here.
  1207. string A command to be executed by a shell; %s will be replaced
  1208. by the path to the file.
  1209. sexp A Lisp form which will be evaluated. The file path will
  1210. be available in the Lisp variable `file'.
  1211. For more examples, see the system specific constants
  1212. `org-file-apps-defaults-macosx'
  1213. `org-file-apps-defaults-windowsnt'
  1214. `org-file-apps-defaults-gnu'."
  1215. :group 'org-link-follow
  1216. :type '(repeat
  1217. (cons (choice :value ""
  1218. (string :tag "Extension")
  1219. (const :tag "System command to open files" system)
  1220. (const :tag "Default for unrecognized files" t)
  1221. (const :tag "Remote file" remote)
  1222. (const :tag "Links to a directory" directory)
  1223. (const :tag "Any files that have Emacs modes"
  1224. auto-mode))
  1225. (choice :value ""
  1226. (const :tag "Visit with Emacs" emacs)
  1227. (const :tag "Use default" default)
  1228. (const :tag "Use the system command" system)
  1229. (string :tag "Command")
  1230. (sexp :tag "Lisp form")))))
  1231. (defgroup org-refile nil
  1232. "Options concerning refiling entries in Org-mode."
  1233. :tag "Org Refile"
  1234. :group 'org)
  1235. (defcustom org-directory "~/org"
  1236. "Directory with org files.
  1237. This is just a default location to look for Org files. There is no need
  1238. at all to put your files into this directory. It is only used in the
  1239. following situations:
  1240. 1. When a remember template specifies a target file that is not an
  1241. absolute path. The path will then be interpreted relative to
  1242. `org-directory'
  1243. 2. When a remember note is filed away in an interactive way (when exiting the
  1244. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1245. with `org-directory' as the default path."
  1246. :group 'org-refile
  1247. :group 'org-remember
  1248. :type 'directory)
  1249. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1250. "Default target for storing notes.
  1251. Used by the hooks for remember.el. This can be a string, or nil to mean
  1252. the value of `remember-data-file'.
  1253. You can set this on a per-template basis with the variable
  1254. `org-remember-templates'."
  1255. :group 'org-refile
  1256. :group 'org-remember
  1257. :type '(choice
  1258. (const :tag "Default from remember-data-file" nil)
  1259. file))
  1260. (defcustom org-goto-interface 'outline
  1261. "The default interface to be used for `org-goto'.
  1262. Allowed values are:
  1263. outline The interface shows an outline of the relevant file
  1264. and the correct heading is found by moving through
  1265. the outline or by searching with incremental search.
  1266. outline-path-completion Headlines in the current buffer are offered via
  1267. completion. This is the interface also used by
  1268. the refile command."
  1269. :group 'org-refile
  1270. :type '(choice
  1271. (const :tag "Outline" outline)
  1272. (const :tag "Outline-path-completion" outline-path-completion)))
  1273. (defcustom org-goto-max-level 5
  1274. "Maximum level to be considered when running org-goto with refile interface."
  1275. :group 'org-refile
  1276. :type 'number)
  1277. (defcustom org-reverse-note-order nil
  1278. "Non-nil means, store new notes at the beginning of a file or entry.
  1279. When nil, new notes will be filed to the end of a file or entry.
  1280. This can also be a list with cons cells of regular expressions that
  1281. are matched against file names, and values."
  1282. :group 'org-remember
  1283. :group 'org-refile
  1284. :type '(choice
  1285. (const :tag "Reverse always" t)
  1286. (const :tag "Reverse never" nil)
  1287. (repeat :tag "By file name regexp"
  1288. (cons regexp boolean))))
  1289. (defcustom org-refile-targets nil
  1290. "Targets for refiling entries with \\[org-refile].
  1291. This is list of cons cells. Each cell contains:
  1292. - a specification of the files to be considered, either a list of files,
  1293. or a symbol whose function or variable value will be used to retrieve
  1294. a file name or a list of file names. If you use `org-agenda-files' for
  1295. that, all agenda files will be scanned for targets. Nil means, consider
  1296. headings in the current buffer.
  1297. - A specification of how to find candidate refile targets. This may be
  1298. any of:
  1299. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1300. This tag has to be present in all target headlines, inheritance will
  1301. not be considered.
  1302. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1303. todo keyword.
  1304. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1305. headlines that are refiling targets.
  1306. - a cons cell (:level . N). Any headline of level N is considered a target.
  1307. Note that, when `org-odd-levels-only' is set, level corresponds to
  1308. order in hierarchy, not to the number of stars.
  1309. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1310. Note that, when `org-odd-levels-only' is set, level corresponds to
  1311. order in hierarchy, not to the number of stars.
  1312. When this variable is nil, all top-level headlines in the current buffer
  1313. are used, equivalent to the value `((nil . (:level . 1))'."
  1314. :group 'org-refile
  1315. :type '(repeat
  1316. (cons
  1317. (choice :value org-agenda-files
  1318. (const :tag "All agenda files" org-agenda-files)
  1319. (const :tag "Current buffer" nil)
  1320. (function) (variable) (file))
  1321. (choice :tag "Identify target headline by"
  1322. (cons :tag "Specific tag" (const :value :tag) (string))
  1323. (cons :tag "TODO keyword" (const :value :todo) (string))
  1324. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1325. (cons :tag "Level number" (const :value :level) (integer))
  1326. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1327. (defcustom org-refile-use-outline-path nil
  1328. "Non-nil means, provide refile targets as paths.
  1329. So a level 3 headline will be available as level1/level2/level3.
  1330. When the value is `file', also include the file name (without directory)
  1331. into the path. When `full-file-path', include the full file path."
  1332. :group 'org-refile
  1333. :type '(choice
  1334. (const :tag "Not" nil)
  1335. (const :tag "Yes" t)
  1336. (const :tag "Start with file name" file)
  1337. (const :tag "Start with full file path" full-file-path)))
  1338. (defcustom org-outline-path-complete-in-steps t
  1339. "Non-nil means, complete the outline path in hierarchical steps.
  1340. When Org-mode uses the refile interface to select an outline path
  1341. \(see variable `org-refile-use-outline-path'), the completion of
  1342. the path can be done is a single go, or if can be done in steps down
  1343. the headline hierarchy. Going in steps is probably the best if you
  1344. do not use a special completion package like `ido' or `icicles'.
  1345. However, when using these packages, going in one step can be very
  1346. fast, while still showing the whole path to the entry."
  1347. :group 'org-refile
  1348. :type 'boolean)
  1349. (defgroup org-todo nil
  1350. "Options concerning TODO items in Org-mode."
  1351. :tag "Org TODO"
  1352. :group 'org)
  1353. (defgroup org-progress nil
  1354. "Options concerning Progress logging in Org-mode."
  1355. :tag "Org Progress"
  1356. :group 'org-time)
  1357. (defvar org-todo-interpretation-widgets
  1358. '(
  1359. (:tag "Sequence (cycling hits every state)" sequence)
  1360. (:tag "Type (cycling directly to DONE)" type))
  1361. "The available interpretation symbols for customizing
  1362. `org-todo-keywords'.
  1363. Interested libraries should add to this list.")
  1364. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1365. "List of TODO entry keyword sequences and their interpretation.
  1366. \\<org-mode-map>This is a list of sequences.
  1367. Each sequence starts with a symbol, either `sequence' or `type',
  1368. indicating if the keywords should be interpreted as a sequence of
  1369. action steps, or as different types of TODO items. The first
  1370. keywords are states requiring action - these states will select a headline
  1371. for inclusion into the global TODO list Org-mode produces. If one of
  1372. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1373. signify that no further action is necessary. If \"|\" is not found,
  1374. the last keyword is treated as the only DONE state of the sequence.
  1375. The command \\[org-todo] cycles an entry through these states, and one
  1376. additional state where no keyword is present. For details about this
  1377. cycling, see the manual.
  1378. TODO keywords and interpretation can also be set on a per-file basis with
  1379. the special #+SEQ_TODO and #+TYP_TODO lines.
  1380. Each keyword can optionally specify a character for fast state selection
  1381. \(in combination with the variable `org-use-fast-todo-selection')
  1382. and specifiers for state change logging, using the same syntax
  1383. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1384. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1385. indicates to record a time stamp each time this state is selected.
  1386. Each keyword may also specify if a timestamp or a note should be
  1387. recorded when entering or leaving the state, by adding additional
  1388. characters in the parenthesis after the keyword. This looks like this:
  1389. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1390. record only the time of the state change. With X and Y being either
  1391. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1392. Y when leaving the state if and only if the *target* state does not
  1393. define X. You may omit any of the fast-selection key or X or /Y,
  1394. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1395. For backward compatibility, this variable may also be just a list
  1396. of keywords - in this case the interpretation (sequence or type) will be
  1397. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1398. :group 'org-todo
  1399. :group 'org-keywords
  1400. :type '(choice
  1401. (repeat :tag "Old syntax, just keywords"
  1402. (string :tag "Keyword"))
  1403. (repeat :tag "New syntax"
  1404. (cons
  1405. (choice
  1406. :tag "Interpretation"
  1407. ;;Quick and dirty way to see
  1408. ;;`org-todo-interpretations'. This takes the
  1409. ;;place of item arguments
  1410. :convert-widget
  1411. (lambda (widget)
  1412. (widget-put widget
  1413. :args (mapcar
  1414. #'(lambda (x)
  1415. (widget-convert
  1416. (cons 'const x)))
  1417. org-todo-interpretation-widgets))
  1418. widget))
  1419. (repeat
  1420. (string :tag "Keyword"))))))
  1421. (defvar org-todo-keywords-1 nil
  1422. "All TODO and DONE keywords active in a buffer.")
  1423. (make-variable-buffer-local 'org-todo-keywords-1)
  1424. (defvar org-todo-keywords-for-agenda nil)
  1425. (defvar org-done-keywords-for-agenda nil)
  1426. (defvar org-todo-keyword-alist-for-agenda nil)
  1427. (defvar org-tag-alist-for-agenda nil)
  1428. (defvar org-agenda-contributing-files nil)
  1429. (defvar org-not-done-keywords nil)
  1430. (make-variable-buffer-local 'org-not-done-keywords)
  1431. (defvar org-done-keywords nil)
  1432. (make-variable-buffer-local 'org-done-keywords)
  1433. (defvar org-todo-heads nil)
  1434. (make-variable-buffer-local 'org-todo-heads)
  1435. (defvar org-todo-sets nil)
  1436. (make-variable-buffer-local 'org-todo-sets)
  1437. (defvar org-todo-log-states nil)
  1438. (make-variable-buffer-local 'org-todo-log-states)
  1439. (defvar org-todo-kwd-alist nil)
  1440. (make-variable-buffer-local 'org-todo-kwd-alist)
  1441. (defvar org-todo-key-alist nil)
  1442. (make-variable-buffer-local 'org-todo-key-alist)
  1443. (defvar org-todo-key-trigger nil)
  1444. (make-variable-buffer-local 'org-todo-key-trigger)
  1445. (defcustom org-todo-interpretation 'sequence
  1446. "Controls how TODO keywords are interpreted.
  1447. This variable is in principle obsolete and is only used for
  1448. backward compatibility, if the interpretation of todo keywords is
  1449. not given already in `org-todo-keywords'. See that variable for
  1450. more information."
  1451. :group 'org-todo
  1452. :group 'org-keywords
  1453. :type '(choice (const sequence)
  1454. (const type)))
  1455. (defcustom org-use-fast-todo-selection t
  1456. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1457. This variable describes if and under what circumstances the cycling
  1458. mechanism for TODO keywords will be replaced by a single-key, direct
  1459. selection scheme.
  1460. When nil, fast selection is never used.
  1461. When the symbol `prefix', it will be used when `org-todo' is called with
  1462. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1463. in an agenda buffer.
  1464. When t, fast selection is used by default. In this case, the prefix
  1465. argument forces cycling instead.
  1466. In all cases, the special interface is only used if access keys have actually
  1467. been assigned by the user, i.e. if keywords in the configuration are followed
  1468. by a letter in parenthesis, like TODO(t)."
  1469. :group 'org-todo
  1470. :type '(choice
  1471. (const :tag "Never" nil)
  1472. (const :tag "By default" t)
  1473. (const :tag "Only with C-u C-c C-t" prefix)))
  1474. (defcustom org-provide-todo-statistics t
  1475. "Non-nil means, update todo statistics after insert and toggle.
  1476. When this is set, todo statistics is updated in the parent of the current
  1477. entry each time a todo state is changed."
  1478. :group 'org-todo
  1479. :type 'boolean)
  1480. (defcustom org-after-todo-state-change-hook nil
  1481. "Hook which is run after the state of a TODO item was changed.
  1482. The new state (a string with a TODO keyword, or nil) is available in the
  1483. Lisp variable `state'."
  1484. :group 'org-todo
  1485. :type 'hook)
  1486. (defvar org-blocker-hook nil
  1487. "Hook for functions that are allowed to block a state change.
  1488. Each function gets as its single argument a property list, see
  1489. `org-trigger-hook' for more information about this list.
  1490. If any of the functions in this hook returns nil, the state change
  1491. is blocked.")
  1492. (defvar org-trigger-hook nil
  1493. "Hook for functions that are triggered by a state change.
  1494. Each function gets as its single argument a property list with at least
  1495. the following elements:
  1496. (:type type-of-change :position pos-at-entry-start
  1497. :from old-state :to new-state)
  1498. Depending on the type, more properties may be present.
  1499. This mechanism is currently implemented for:
  1500. TODO state changes
  1501. ------------------
  1502. :type todo-state-change
  1503. :from previous state (keyword as a string), or nil, or a symbol
  1504. 'todo' or 'done', to indicate the general type of state.
  1505. :to new state, like in :from")
  1506. (defcustom org-enforce-todo-dependencies nil
  1507. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1508. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1509. be blocked if any prior sibling is not yet done.
  1510. This variable needs to be set before org.el is loaded, and you need to
  1511. restart Emacs after a change to make the change effective. The only way
  1512. to change is while Emacs is running is through the customize interface."
  1513. :set (lambda (var val)
  1514. (set var val)
  1515. (if val
  1516. (add-hook 'org-blocker-hook
  1517. 'org-block-todo-from-children-or-siblings)
  1518. (remove-hook 'org-blocker-hook
  1519. 'org-block-todo-from-children-or-siblings)))
  1520. :group 'org-todo
  1521. :type 'boolean)
  1522. (defcustom org-enforce-todo-checkbox-dependencies nil
  1523. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1524. When this is nil, checkboxes have no influence on switching TODO states.
  1525. When non-nil, you first need to check off all check boxes before the TODO
  1526. entry can be switched to DONE.
  1527. This variable needs to be set before org.el is loaded, and you need to
  1528. restart Emacs after a change to make the change effective. The only way
  1529. to change is while Emacs is running is through the customize interface."
  1530. :set (lambda (var val)
  1531. (set var val)
  1532. (if val
  1533. (add-hook 'org-blocker-hook
  1534. 'org-block-todo-from-checkboxes)
  1535. (remove-hook 'org-blocker-hook
  1536. 'org-block-todo-from-checkboxes)))
  1537. :group 'org-todo
  1538. :type 'boolean)
  1539. (defcustom org-todo-state-tags-triggers nil
  1540. "Tag changes that should be triggered by TODO state changes.
  1541. This is a list. Each entry is
  1542. (state-change (tag . flag) .......)
  1543. State-change can be a string with a state, and empty string to indicate the
  1544. state that has no TODO keyword, or it can be one of the symbols `todo'
  1545. or `done', meaning any not-done or done state, respectively."
  1546. :group 'org-todo
  1547. :group 'org-tags
  1548. :type '(repeat
  1549. (cons (choice :tag "When changing to"
  1550. (const :tag "Not-done state" todo)
  1551. (const :tag "Done state" done)
  1552. (string :tag "State"))
  1553. (repeat
  1554. (cons :tag "Tag action"
  1555. (string :tag "Tag")
  1556. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1557. (defcustom org-log-done nil
  1558. "Information to record when a task moves to the DONE state.
  1559. Possible values are:
  1560. nil Don't add anything, just change the keyword
  1561. time Add a time stamp to the task
  1562. note Prompt a closing note and add it with template `org-log-note-headings'
  1563. This option can also be set with on a per-file-basis with
  1564. #+STARTUP: nologdone
  1565. #+STARTUP: logdone
  1566. #+STARTUP: lognotedone
  1567. You can have local logging settings for a subtree by setting the LOGGING
  1568. property to one or more of these keywords."
  1569. :group 'org-todo
  1570. :group 'org-progress
  1571. :type '(choice
  1572. (const :tag "No logging" nil)
  1573. (const :tag "Record CLOSED timestamp" time)
  1574. (const :tag "Record CLOSED timestamp with closing note." note)))
  1575. ;; Normalize old uses of org-log-done.
  1576. (cond
  1577. ((eq org-log-done t) (setq org-log-done 'time))
  1578. ((and (listp org-log-done) (memq 'done org-log-done))
  1579. (setq org-log-done 'note)))
  1580. (defcustom org-log-note-clock-out nil
  1581. "Non-nil means, record a note when clocking out of an item.
  1582. This can also be configured on a per-file basis by adding one of
  1583. the following lines anywhere in the buffer:
  1584. #+STARTUP: lognoteclock-out
  1585. #+STARTUP: nolognoteclock-out"
  1586. :group 'org-todo
  1587. :group 'org-progress
  1588. :type 'boolean)
  1589. (defcustom org-log-done-with-time t
  1590. "Non-nil means, the CLOSED time stamp will contain date and time.
  1591. When nil, only the date will be recorded."
  1592. :group 'org-progress
  1593. :type 'boolean)
  1594. (defcustom org-log-note-headings
  1595. '((done . "CLOSING NOTE %t")
  1596. (state . "State %-12s from %-12S %t")
  1597. (note . "Note taken on %t")
  1598. (clock-out . ""))
  1599. "Headings for notes added to entries.
  1600. The value is an alist, with the car being a symbol indicating the note
  1601. context, and the cdr is the heading to be used. The heading may also be the
  1602. empty string.
  1603. %t in the heading will be replaced by a time stamp.
  1604. %s will be replaced by the new TODO state, in double quotes.
  1605. %S will be replaced by the old TODO state, in double quotes.
  1606. %u will be replaced by the user name.
  1607. %U will be replaced by the full user name."
  1608. :group 'org-todo
  1609. :group 'org-progress
  1610. :type '(list :greedy t
  1611. (cons (const :tag "Heading when closing an item" done) string)
  1612. (cons (const :tag
  1613. "Heading when changing todo state (todo sequence only)"
  1614. state) string)
  1615. (cons (const :tag "Heading when just taking a note" note) string)
  1616. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1617. (unless (assq 'note org-log-note-headings)
  1618. (push '(note . "%t") org-log-note-headings))
  1619. (defcustom org-log-into-drawer nil
  1620. "Non-nil means, insert state change notes and time stamps into a drawer.
  1621. When nil, state changes notes will be inserted after the headline and
  1622. any scheduling and clock lines, but not inside a drawer.
  1623. The value of this variable should be the name of the drawer to use.
  1624. LOGBOOK is proposed at the default drawer for this purpose, you can
  1625. also set this to a string to define the drawer of your choice.
  1626. A value of t is also allowed, representing \"LOGBOOK\".
  1627. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1628. will be ignored."
  1629. :group 'org-todo
  1630. :group 'org-progress
  1631. :type '(choice
  1632. (const :tag "Not into a drawer" nil)
  1633. (const :tag "LOGBOOK" t)
  1634. (string :tag "Other")))
  1635. (if (fboundp 'defvaralias)
  1636. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1637. (defcustom org-log-state-notes-insert-after-drawers nil
  1638. "Non-nil means, insert state change notes after any drawers in entry.
  1639. Only the drawers that *immediately* follow the headline and the
  1640. deadline/scheduled line are skipped.
  1641. When nil, insert notes right after the heading and perhaps the line
  1642. with deadline/scheduling if present.
  1643. This variable will have no effect if `org-log-into-drawer' is
  1644. set."
  1645. :group 'org-todo
  1646. :group 'org-progress
  1647. :type 'boolean)
  1648. (defcustom org-log-states-order-reversed t
  1649. "Non-nil means, the latest state change note will be directly after heading.
  1650. When nil, the notes will be orderer according to time."
  1651. :group 'org-todo
  1652. :group 'org-progress
  1653. :type 'boolean)
  1654. (defcustom org-log-repeat 'time
  1655. "Non-nil means, record moving through the DONE state when triggering repeat.
  1656. An auto-repeating tasks is immediately switched back to TODO when marked
  1657. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1658. the TODO keyword definition, or recording a closing note by setting
  1659. `org-log-done', there will be no record of the task moving through DONE.
  1660. This variable forces taking a note anyway. Possible values are:
  1661. nil Don't force a record
  1662. time Record a time stamp
  1663. note Record a note
  1664. This option can also be set with on a per-file-basis with
  1665. #+STARTUP: logrepeat
  1666. #+STARTUP: lognoterepeat
  1667. #+STARTUP: nologrepeat
  1668. You can have local logging settings for a subtree by setting the LOGGING
  1669. property to one or more of these keywords."
  1670. :group 'org-todo
  1671. :group 'org-progress
  1672. :type '(choice
  1673. (const :tag "Don't force a record" nil)
  1674. (const :tag "Force recording the DONE state" time)
  1675. (const :tag "Force recording a note with the DONE state" note)))
  1676. (defgroup org-priorities nil
  1677. "Priorities in Org-mode."
  1678. :tag "Org Priorities"
  1679. :group 'org-todo)
  1680. (defcustom org-highest-priority ?A
  1681. "The highest priority of TODO items. A character like ?A, ?B etc.
  1682. Must have a smaller ASCII number than `org-lowest-priority'."
  1683. :group 'org-priorities
  1684. :type 'character)
  1685. (defcustom org-lowest-priority ?C
  1686. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1687. Must have a larger ASCII number than `org-highest-priority'."
  1688. :group 'org-priorities
  1689. :type 'character)
  1690. (defcustom org-default-priority ?B
  1691. "The default priority of TODO items.
  1692. This is the priority an item get if no explicit priority is given."
  1693. :group 'org-priorities
  1694. :type 'character)
  1695. (defcustom org-priority-start-cycle-with-default t
  1696. "Non-nil means, start with default priority when starting to cycle.
  1697. When this is nil, the first step in the cycle will be (depending on the
  1698. command used) one higher or lower that the default priority."
  1699. :group 'org-priorities
  1700. :type 'boolean)
  1701. (defgroup org-time nil
  1702. "Options concerning time stamps and deadlines in Org-mode."
  1703. :tag "Org Time"
  1704. :group 'org)
  1705. (defcustom org-insert-labeled-timestamps-at-point nil
  1706. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1707. When nil, these labeled time stamps are forces into the second line of an
  1708. entry, just after the headline. When scheduling from the global TODO list,
  1709. the time stamp will always be forced into the second line."
  1710. :group 'org-time
  1711. :type 'boolean)
  1712. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1713. "Formats for `format-time-string' which are used for time stamps.
  1714. It is not recommended to change this constant.")
  1715. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1716. "Number of minutes to round time stamps to.
  1717. These are two values, the first applies when first creating a time stamp.
  1718. The second applies when changing it with the commands `S-up' and `S-down'.
  1719. When changing the time stamp, this means that it will change in steps
  1720. of N minutes, as given by the second value.
  1721. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1722. numbers should be factors of 60, so for example 5, 10, 15.
  1723. When this is larger than 1, you can still force an exact time-stamp by using
  1724. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1725. and by using a prefix arg to `S-up/down' to specify the exact number
  1726. of minutes to shift."
  1727. :group 'org-time
  1728. :get '(lambda (var) ; Make sure all entries have 5 elements
  1729. (if (integerp (default-value var))
  1730. (list (default-value var) 5)
  1731. (default-value var)))
  1732. :type '(list
  1733. (integer :tag "when inserting times")
  1734. (integer :tag "when modifying times")))
  1735. ;; Normalize old customizations of this variable.
  1736. (when (integerp org-time-stamp-rounding-minutes)
  1737. (setq org-time-stamp-rounding-minutes
  1738. (list org-time-stamp-rounding-minutes
  1739. org-time-stamp-rounding-minutes)))
  1740. (defcustom org-display-custom-times nil
  1741. "Non-nil means, overlay custom formats over all time stamps.
  1742. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1743. To turn this on on a per-file basis, insert anywhere in the file:
  1744. #+STARTUP: customtime"
  1745. :group 'org-time
  1746. :set 'set-default
  1747. :type 'sexp)
  1748. (make-variable-buffer-local 'org-display-custom-times)
  1749. (defcustom org-time-stamp-custom-formats
  1750. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1751. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1752. These are overlayed over the default ISO format if the variable
  1753. `org-display-custom-times' is set. Time like %H:%M should be at the
  1754. end of the second format."
  1755. :group 'org-time
  1756. :type 'sexp)
  1757. (defun org-time-stamp-format (&optional long inactive)
  1758. "Get the right format for a time string."
  1759. (let ((f (if long (cdr org-time-stamp-formats)
  1760. (car org-time-stamp-formats))))
  1761. (if inactive
  1762. (concat "[" (substring f 1 -1) "]")
  1763. f)))
  1764. (defcustom org-time-clocksum-format "%d:%02d"
  1765. "The format string used when creating CLOCKSUM lines, or when
  1766. org-mode generates a time duration."
  1767. :group 'org-time
  1768. :type 'string)
  1769. (defcustom org-deadline-warning-days 14
  1770. "No. of days before expiration during which a deadline becomes active.
  1771. This variable governs the display in sparse trees and in the agenda.
  1772. When 0 or negative, it means use this number (the absolute value of it)
  1773. even if a deadline has a different individual lead time specified.
  1774. Custom commands can set this variable in the options section."
  1775. :group 'org-time
  1776. :group 'org-agenda-daily/weekly
  1777. :type 'number)
  1778. (defcustom org-read-date-prefer-future t
  1779. "Non-nil means, assume future for incomplete date input from user.
  1780. This affects the following situations:
  1781. 1. The user gives a day, but no month.
  1782. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1783. read this as the third of *next* month. However, if you enter \"17\",
  1784. it will be considered as *this* month.
  1785. 2. The user gives a month but not a year.
  1786. For example, if it is april and you enter \"feb 2\", this will be read
  1787. as feb 2, *next* year. \"May 5\", however, will be this year.
  1788. Currently this does not work for ISO week specifications.
  1789. When this option is nil, the current month and year will always be used
  1790. as defaults."
  1791. :group 'org-time
  1792. :type 'boolean)
  1793. (defcustom org-read-date-display-live t
  1794. "Non-nil means, display current interpretation of date prompt live.
  1795. This display will be in an overlay, in the minibuffer."
  1796. :group 'org-time
  1797. :type 'boolean)
  1798. (defcustom org-read-date-popup-calendar t
  1799. "Non-nil means, pop up a calendar when prompting for a date.
  1800. In the calendar, the date can be selected with mouse-1. However, the
  1801. minibuffer will also be active, and you can simply enter the date as well.
  1802. When nil, only the minibuffer will be available."
  1803. :group 'org-time
  1804. :type 'boolean)
  1805. (if (fboundp 'defvaralias)
  1806. (defvaralias 'org-popup-calendar-for-date-prompt
  1807. 'org-read-date-popup-calendar))
  1808. (defcustom org-read-date-minibuffer-setup-hook nil
  1809. "Hook to be used to set up keys for the date/time interface.
  1810. Add key definitions to `minibuffer-local-map', which will be a temporary
  1811. copy."
  1812. :group 'org-time
  1813. :type 'hook)
  1814. (defcustom org-extend-today-until 0
  1815. "The hour when your day really ends. Must be an integer.
  1816. This has influence for the following applications:
  1817. - When switching the agenda to \"today\". It it is still earlier than
  1818. the time given here, the day recognized as TODAY is actually yesterday.
  1819. - When a date is read from the user and it is still before the time given
  1820. here, the current date and time will be assumed to be yesterday, 23:59.
  1821. Also, timestamps inserted in remember templates follow this rule.
  1822. IMPORTANT: This is a feature whose implementation is and likely will
  1823. remain incomplete. Really, it is only here because past midnight seems to
  1824. be the favorite working time of John Wiegley :-)"
  1825. :group 'org-time
  1826. :type 'number)
  1827. (defcustom org-edit-timestamp-down-means-later nil
  1828. "Non-nil means, S-down will increase the time in a time stamp.
  1829. When nil, S-up will increase."
  1830. :group 'org-time
  1831. :type 'boolean)
  1832. (defcustom org-calendar-follow-timestamp-change t
  1833. "Non-nil means, make the calendar window follow timestamp changes.
  1834. When a timestamp is modified and the calendar window is visible, it will be
  1835. moved to the new date."
  1836. :group 'org-time
  1837. :type 'boolean)
  1838. (defgroup org-tags nil
  1839. "Options concerning tags in Org-mode."
  1840. :tag "Org Tags"
  1841. :group 'org)
  1842. (defcustom org-tag-alist nil
  1843. "List of tags allowed in Org-mode files.
  1844. When this list is nil, Org-mode will base TAG input on what is already in the
  1845. buffer.
  1846. The value of this variable is an alist, the car of each entry must be a
  1847. keyword as a string, the cdr may be a character that is used to select
  1848. that tag through the fast-tag-selection interface.
  1849. See the manual for details."
  1850. :group 'org-tags
  1851. :type '(repeat
  1852. (choice
  1853. (cons (string :tag "Tag name")
  1854. (character :tag "Access char"))
  1855. (const :tag "Start radio group" (:startgroup))
  1856. (const :tag "End radio group" (:endgroup))
  1857. (const :tag "New line" (:newline)))))
  1858. (defcustom org-tag-persistent-alist nil
  1859. "List of tags that will always appear in all Org-mode files.
  1860. This is in addition to any in buffer settings or customizations
  1861. of `org-tag-alist'.
  1862. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1863. The value of this variable is an alist, the car of each entry must be a
  1864. keyword as a string, the cdr may be a character that is used to select
  1865. that tag through the fast-tag-selection interface.
  1866. See the manual for details.
  1867. To disable these tags on a per-file basis, insert anywhere in the file:
  1868. #+STARTUP: noptag"
  1869. :group 'org-tags
  1870. :type '(repeat
  1871. (choice
  1872. (cons (string :tag "Tag name")
  1873. (character :tag "Access char"))
  1874. (const :tag "Start radio group" (:startgroup))
  1875. (const :tag "End radio group" (:endgroup))
  1876. (const :tag "New line" (:newline)))))
  1877. (defvar org-file-tags nil
  1878. "List of tags that can be inherited by all entries in the file.
  1879. The tags will be inherited if the variable `org-use-tag-inheritance'
  1880. says they should be.
  1881. This variable is populated from #+TAG lines.")
  1882. (defcustom org-use-fast-tag-selection 'auto
  1883. "Non-nil means, use fast tag selection scheme.
  1884. This is a special interface to select and deselect tags with single keys.
  1885. When nil, fast selection is never used.
  1886. When the symbol `auto', fast selection is used if and only if selection
  1887. characters for tags have been configured, either through the variable
  1888. `org-tag-alist' or through a #+TAGS line in the buffer.
  1889. When t, fast selection is always used and selection keys are assigned
  1890. automatically if necessary."
  1891. :group 'org-tags
  1892. :type '(choice
  1893. (const :tag "Always" t)
  1894. (const :tag "Never" nil)
  1895. (const :tag "When selection characters are configured" 'auto)))
  1896. (defcustom org-fast-tag-selection-single-key nil
  1897. "Non-nil means, fast tag selection exits after first change.
  1898. When nil, you have to press RET to exit it.
  1899. During fast tag selection, you can toggle this flag with `C-c'.
  1900. This variable can also have the value `expert'. In this case, the window
  1901. displaying the tags menu is not even shown, until you press C-c again."
  1902. :group 'org-tags
  1903. :type '(choice
  1904. (const :tag "No" nil)
  1905. (const :tag "Yes" t)
  1906. (const :tag "Expert" expert)))
  1907. (defvar org-fast-tag-selection-include-todo nil
  1908. "Non-nil means, fast tags selection interface will also offer TODO states.
  1909. This is an undocumented feature, you should not rely on it.")
  1910. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1911. "The column to which tags should be indented in a headline.
  1912. If this number is positive, it specifies the column. If it is negative,
  1913. it means that the tags should be flushright to that column. For example,
  1914. -80 works well for a normal 80 character screen."
  1915. :group 'org-tags
  1916. :type 'integer)
  1917. (defcustom org-auto-align-tags t
  1918. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1919. These operations change the length of a headline and therefore shift
  1920. the tags around. With this options turned on, after each such operation
  1921. the tags are again aligned to `org-tags-column'."
  1922. :group 'org-tags
  1923. :type 'boolean)
  1924. (defcustom org-use-tag-inheritance t
  1925. "Non-nil means, tags in levels apply also for sublevels.
  1926. When nil, only the tags directly given in a specific line apply there.
  1927. This may also be a list of tags that should be inherited, or a regexp that
  1928. matches tags that should be inherited. Additional control is possible
  1929. with the variable `org-tags-exclude-from-inheritance' which gives an
  1930. explicit list of tags to be excluded from inheritance., even if the value of
  1931. `org-use-tag-inheritance' would select it for inheritance.
  1932. If this option is t, a match early-on in a tree can lead to a large
  1933. number of matches in the subtree when constructing the agenda or creating
  1934. a sparse tree. If you only want to see the first match in a tree during
  1935. a search, check out the variable `org-tags-match-list-sublevels'."
  1936. :group 'org-tags
  1937. :type '(choice
  1938. (const :tag "Not" nil)
  1939. (const :tag "Always" t)
  1940. (repeat :tag "Specific tags" (string :tag "Tag"))
  1941. (regexp :tag "Tags matched by regexp")))
  1942. (defcustom org-tags-exclude-from-inheritance nil
  1943. "List of tags that should never be inherited.
  1944. This is a way to exclude a few tags from inheritance. For way to do
  1945. the opposite, to actively allow inheritance for selected tags,
  1946. see the variable `org-use-tag-inheritance'."
  1947. :group 'org-tags
  1948. :type '(repeat (string :tag "Tag")))
  1949. (defun org-tag-inherit-p (tag)
  1950. "Check if TAG is one that should be inherited."
  1951. (cond
  1952. ((member tag org-tags-exclude-from-inheritance) nil)
  1953. ((eq org-use-tag-inheritance t) t)
  1954. ((not org-use-tag-inheritance) nil)
  1955. ((stringp org-use-tag-inheritance)
  1956. (string-match org-use-tag-inheritance tag))
  1957. ((listp org-use-tag-inheritance)
  1958. (member tag org-use-tag-inheritance))
  1959. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1960. (defcustom org-tags-match-list-sublevels t
  1961. "Non-nil means list also sublevels of headlines matching a search.
  1962. This variable applies to tags/property searches, and also to stuck
  1963. projects because this search is based on a tags match as well.
  1964. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1965. the sublevels of a headline matching a tag search often also match
  1966. the same search. Listing all of them can create very long lists.
  1967. Setting this variable to nil causes subtrees of a match to be skipped.
  1968. As a special case, if the tag search is restricted to TODO items, the
  1969. value of this variable is ignored and sublevels are always checked, to
  1970. make sure all corresponding TODO items find their way into the list.
  1971. This variable is semi-obsolete and probably should always be true. It
  1972. is better to limit inheritance to certain tags using the variables
  1973. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1974. :group 'org-tags
  1975. :type '(choice
  1976. (const :tag "No, don't list them" nil)
  1977. (const :tag "Yes, do list them" t)
  1978. (const :tag "List them, indented with leading dots" indented)))
  1979. (defvar org-tags-history nil
  1980. "History of minibuffer reads for tags.")
  1981. (defvar org-last-tags-completion-table nil
  1982. "The last used completion table for tags.")
  1983. (defvar org-after-tags-change-hook nil
  1984. "Hook that is run after the tags in a line have changed.")
  1985. (defgroup org-properties nil
  1986. "Options concerning properties in Org-mode."
  1987. :tag "Org Properties"
  1988. :group 'org)
  1989. (defcustom org-property-format "%-10s %s"
  1990. "How property key/value pairs should be formatted by `indent-line'.
  1991. When `indent-line' hits a property definition, it will format the line
  1992. according to this format, mainly to make sure that the values are
  1993. lined-up with respect to each other."
  1994. :group 'org-properties
  1995. :type 'string)
  1996. (defcustom org-use-property-inheritance nil
  1997. "Non-nil means, properties apply also for sublevels.
  1998. This setting is chiefly used during property searches. Turning it on can
  1999. cause significant overhead when doing a search, which is why it is not
  2000. on by default.
  2001. When nil, only the properties directly given in the current entry count.
  2002. When t, every property is inherited. The value may also be a list of
  2003. properties that should have inheritance, or a regular expression matching
  2004. properties that should be inherited.
  2005. However, note that some special properties use inheritance under special
  2006. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2007. and the properties ending in \"_ALL\" when they are used as descriptor
  2008. for valid values of a property.
  2009. Note for programmers:
  2010. When querying an entry with `org-entry-get', you can control if inheritance
  2011. should be used. By default, `org-entry-get' looks only at the local
  2012. properties. You can request inheritance by setting the inherit argument
  2013. to t (to force inheritance) or to `selective' (to respect the setting
  2014. in this variable)."
  2015. :group 'org-properties
  2016. :type '(choice
  2017. (const :tag "Not" nil)
  2018. (const :tag "Always" t)
  2019. (repeat :tag "Specific properties" (string :tag "Property"))
  2020. (regexp :tag "Properties matched by regexp")))
  2021. (defun org-property-inherit-p (property)
  2022. "Check if PROPERTY is one that should be inherited."
  2023. (cond
  2024. ((eq org-use-property-inheritance t) t)
  2025. ((not org-use-property-inheritance) nil)
  2026. ((stringp org-use-property-inheritance)
  2027. (string-match org-use-property-inheritance property))
  2028. ((listp org-use-property-inheritance)
  2029. (member property org-use-property-inheritance))
  2030. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2031. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2032. "The default column format, if no other format has been defined.
  2033. This variable can be set on the per-file basis by inserting a line
  2034. #+COLUMNS: %25ITEM ....."
  2035. :group 'org-properties
  2036. :type 'string)
  2037. (defcustom org-columns-ellipses ".."
  2038. "The ellipses to be used when a field in column view is truncated.
  2039. When this is the empty string, as many characters as possible are shown,
  2040. but then there will be no visual indication that the field has been truncated.
  2041. When this is a string of length N, the last N characters of a truncated
  2042. field are replaced by this string. If the column is narrower than the
  2043. ellipses string, only part of the ellipses string will be shown."
  2044. :group 'org-properties
  2045. :type 'string)
  2046. (defcustom org-columns-modify-value-for-display-function nil
  2047. "Function that modifies values for display in column view.
  2048. For example, it can be used to cut out a certain part from a time stamp.
  2049. The function must take 2 arguments:
  2050. column-title The title of the column (*not* the property name)
  2051. value The value that should be modified.
  2052. The function should return the value that should be displayed,
  2053. or nil if the normal value should be used."
  2054. :group 'org-properties
  2055. :type 'function)
  2056. (defcustom org-effort-property "Effort"
  2057. "The property that is being used to keep track of effort estimates.
  2058. Effort estimates given in this property need to have the format H:MM."
  2059. :group 'org-properties
  2060. :group 'org-progress
  2061. :type '(string :tag "Property"))
  2062. (defconst org-global-properties-fixed
  2063. '(("VISIBILITY_ALL" . "folded children content all"))
  2064. "List of property/value pairs that can be inherited by any entry.
  2065. These are fixed values, for the preset properties. The user variable
  2066. that can be used to add to this list is `org-global-properties'.
  2067. The entries in this list are cons cells where the car is a property
  2068. name and cdr is a string with the value. If the value represents
  2069. multiple items like an \"_ALL\" property, separate the items by
  2070. spaces.")
  2071. (defcustom org-global-properties nil
  2072. "List of property/value pairs that can be inherited by any entry.
  2073. This list will be combined with the constant `org-global-properties-fixed'.
  2074. The entries in this list are cons cells where the car is a property
  2075. name and cdr is a string with the value.
  2076. You can set buffer-local values for the same purpose in the variable
  2077. `org-file-properties' this by adding lines like
  2078. #+PROPERTY: NAME VALUE"
  2079. :group 'org-properties
  2080. :type '(repeat
  2081. (cons (string :tag "Property")
  2082. (string :tag "Value"))))
  2083. (defvar org-file-properties nil
  2084. "List of property/value pairs that can be inherited by any entry.
  2085. Valid for the current buffer.
  2086. This variable is populated from #+PROPERTY lines.")
  2087. (make-variable-buffer-local 'org-file-properties)
  2088. (defgroup org-agenda nil
  2089. "Options concerning agenda views in Org-mode."
  2090. :tag "Org Agenda"
  2091. :group 'org)
  2092. (defvar org-category nil
  2093. "Variable used by org files to set a category for agenda display.
  2094. Such files should use a file variable to set it, for example
  2095. # -*- mode: org; org-category: \"ELisp\"
  2096. or contain a special line
  2097. #+CATEGORY: ELisp
  2098. If the file does not specify a category, then file's base name
  2099. is used instead.")
  2100. (make-variable-buffer-local 'org-category)
  2101. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2102. (defcustom org-agenda-files nil
  2103. "The files to be used for agenda display.
  2104. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2105. \\[org-remove-file]. You can also use customize to edit the list.
  2106. If an entry is a directory, all files in that directory that are matched by
  2107. `org-agenda-file-regexp' will be part of the file list.
  2108. If the value of the variable is not a list but a single file name, then
  2109. the list of agenda files is actually stored and maintained in that file, one
  2110. agenda file per line."
  2111. :group 'org-agenda
  2112. :type '(choice
  2113. (repeat :tag "List of files and directories" file)
  2114. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2115. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2116. "Regular expression to match files for `org-agenda-files'.
  2117. If any element in the list in that variable contains a directory instead
  2118. of a normal file, all files in that directory that are matched by this
  2119. regular expression will be included."
  2120. :group 'org-agenda
  2121. :type 'regexp)
  2122. (defcustom org-agenda-text-search-extra-files nil
  2123. "List of extra files to be searched by text search commands.
  2124. These files will be search in addition to the agenda files by the
  2125. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2126. Note that these files will only be searched for text search commands,
  2127. not for the other agenda views like todo lists, tag searches or the weekly
  2128. agenda. This variable is intended to list notes and possibly archive files
  2129. that should also be searched by these two commands.
  2130. In fact, if the first element in the list is the symbol `agenda-archives',
  2131. than all archive files of all agenda files will be added to the search
  2132. scope."
  2133. :group 'org-agenda
  2134. :type '(set :greedy t
  2135. (const :tag "Agenda Archives" agenda-archives)
  2136. (repeat :inline t (file))))
  2137. (if (fboundp 'defvaralias)
  2138. (defvaralias 'org-agenda-multi-occur-extra-files
  2139. 'org-agenda-text-search-extra-files))
  2140. (defcustom org-agenda-skip-unavailable-files nil
  2141. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2142. A nil value means to remove them, after a query, from the list."
  2143. :group 'org-agenda
  2144. :type 'boolean)
  2145. (defcustom org-calendar-to-agenda-key [?c]
  2146. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2147. The command `org-calendar-goto-agenda' will be bound to this key. The
  2148. default is the character `c' because then `c' can be used to switch back and
  2149. forth between agenda and calendar."
  2150. :group 'org-agenda
  2151. :type 'sexp)
  2152. (defcustom org-calendar-agenda-action-key [?k]
  2153. "The key to be installed in `calendar-mode-map' for agenda-action.
  2154. The command `org-agenda-action' will be bound to this key. The
  2155. default is the character `k' because we use the same key in the agenda."
  2156. :group 'org-agenda
  2157. :type 'sexp)
  2158. (eval-after-load "calendar"
  2159. '(progn
  2160. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2161. 'org-calendar-goto-agenda)
  2162. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2163. 'org-agenda-action)))
  2164. (defgroup org-latex nil
  2165. "Options for embedding LaTeX code into Org-mode."
  2166. :tag "Org LaTeX"
  2167. :group 'org)
  2168. (defcustom org-format-latex-options
  2169. '(:foreground default :background default :scale 1.0
  2170. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2171. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2172. "Options for creating images from LaTeX fragments.
  2173. This is a property list with the following properties:
  2174. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2175. `default' means use the foreground of the default face.
  2176. :background the background color, or \"Transparent\".
  2177. `default' means use the background of the default face.
  2178. :scale a scaling factor for the size of the images.
  2179. :html-foreground, :html-background, :html-scale
  2180. the same numbers for HTML export.
  2181. :matchers a list indicating which matchers should be used to
  2182. find LaTeX fragments. Valid members of this list are:
  2183. \"begin\" find environments
  2184. \"$1\" find single characters surrounded by $.$
  2185. \"$\" find math expressions surrounded by $...$
  2186. \"$$\" find math expressions surrounded by $$....$$
  2187. \"\\(\" find math expressions surrounded by \\(...\\)
  2188. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2189. :group 'org-latex
  2190. :type 'plist)
  2191. (defcustom org-format-latex-header "\\documentclass{article}
  2192. \\usepackage{fullpage} % do not remove
  2193. \\usepackage{amssymb}
  2194. \\usepackage[usenames]{color}
  2195. \\usepackage{amsmath}
  2196. \\usepackage{latexsym}
  2197. \\usepackage[mathscr]{eucal}
  2198. \\pagestyle{empty} % do not remove"
  2199. "The document header used for processing LaTeX fragments."
  2200. :group 'org-latex
  2201. :type 'string)
  2202. (defgroup org-font-lock nil
  2203. "Font-lock settings for highlighting in Org-mode."
  2204. :tag "Org Font Lock"
  2205. :group 'org)
  2206. (defcustom org-level-color-stars-only nil
  2207. "Non-nil means fontify only the stars in each headline.
  2208. When nil, the entire headline is fontified.
  2209. Changing it requires restart of `font-lock-mode' to become effective
  2210. also in regions already fontified."
  2211. :group 'org-font-lock
  2212. :type 'boolean)
  2213. (defcustom org-hide-leading-stars nil
  2214. "Non-nil means, hide the first N-1 stars in a headline.
  2215. This works by using the face `org-hide' for these stars. This
  2216. face is white for a light background, and black for a dark
  2217. background. You may have to customize the face `org-hide' to
  2218. make this work.
  2219. Changing it requires restart of `font-lock-mode' to become effective
  2220. also in regions already fontified.
  2221. You may also set this on a per-file basis by adding one of the following
  2222. lines to the buffer:
  2223. #+STARTUP: hidestars
  2224. #+STARTUP: showstars"
  2225. :group 'org-font-lock
  2226. :type 'boolean)
  2227. (defcustom org-fontify-done-headline nil
  2228. "Non-nil means, change the face of a headline if it is marked DONE.
  2229. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2230. When this is non-nil, the headline after the keyword is set to the
  2231. `org-headline-done' as an additional indication."
  2232. :group 'org-font-lock
  2233. :type 'boolean)
  2234. (defcustom org-fontify-emphasized-text t
  2235. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2236. Changing this variable requires a restart of Emacs to take effect."
  2237. :group 'org-font-lock
  2238. :type 'boolean)
  2239. (defcustom org-highlight-latex-fragments-and-specials nil
  2240. "Non-nil means, fontify what is treated specially by the exporters."
  2241. :group 'org-font-lock
  2242. :type 'boolean)
  2243. (defcustom org-hide-emphasis-markers nil
  2244. "Non-nil mean font-lock should hide the emphasis marker characters."
  2245. :group 'org-font-lock
  2246. :type 'boolean)
  2247. (defvar org-emph-re nil
  2248. "Regular expression for matching emphasis.")
  2249. (defvar org-verbatim-re nil
  2250. "Regular expression for matching verbatim text.")
  2251. (defvar org-emphasis-regexp-components) ; defined just below
  2252. (defvar org-emphasis-alist) ; defined just below
  2253. (defun org-set-emph-re (var val)
  2254. "Set variable and compute the emphasis regular expression."
  2255. (set var val)
  2256. (when (and (boundp 'org-emphasis-alist)
  2257. (boundp 'org-emphasis-regexp-components)
  2258. org-emphasis-alist org-emphasis-regexp-components)
  2259. (let* ((e org-emphasis-regexp-components)
  2260. (pre (car e))
  2261. (post (nth 1 e))
  2262. (border (nth 2 e))
  2263. (body (nth 3 e))
  2264. (nl (nth 4 e))
  2265. (body1 (concat body "*?"))
  2266. (markers (mapconcat 'car org-emphasis-alist ""))
  2267. (vmarkers (mapconcat
  2268. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2269. org-emphasis-alist "")))
  2270. ;; make sure special characters appear at the right position in the class
  2271. (if (string-match "\\^" markers)
  2272. (setq markers (concat (replace-match "" t t markers) "^")))
  2273. (if (string-match "-" markers)
  2274. (setq markers (concat (replace-match "" t t markers) "-")))
  2275. (if (string-match "\\^" vmarkers)
  2276. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2277. (if (string-match "-" vmarkers)
  2278. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2279. (if (> nl 0)
  2280. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2281. (int-to-string nl) "\\}")))
  2282. ;; Make the regexp
  2283. (setq org-emph-re
  2284. (concat "\\([" pre "]\\|^\\)"
  2285. "\\("
  2286. "\\([" markers "]\\)"
  2287. "\\("
  2288. "[^" border "]\\|"
  2289. "[^" border "]"
  2290. body1
  2291. "[^" border "]"
  2292. "\\)"
  2293. "\\3\\)"
  2294. "\\([" post "]\\|$\\)"))
  2295. (setq org-verbatim-re
  2296. (concat "\\([" pre "]\\|^\\)"
  2297. "\\("
  2298. "\\([" vmarkers "]\\)"
  2299. "\\("
  2300. "[^" border "]\\|"
  2301. "[^" border "]"
  2302. body1
  2303. "[^" border "]"
  2304. "\\)"
  2305. "\\3\\)"
  2306. "\\([" post "]\\|$\\)")))))
  2307. (defcustom org-emphasis-regexp-components
  2308. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2309. "Components used to build the regular expression for emphasis.
  2310. This is a list with 6 entries. Terminology: In an emphasis string
  2311. like \" *strong word* \", we call the initial space PREMATCH, the final
  2312. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2313. and \"trong wor\" is the body. The different components in this variable
  2314. specify what is allowed/forbidden in each part:
  2315. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2316. post Chars allowed as postmatch. End of line will be allowed too.
  2317. border The chars *forbidden* as border characters.
  2318. body-regexp A regexp like \".\" to match a body character. Don't use
  2319. non-shy groups here, and don't allow newline here.
  2320. newline The maximum number of newlines allowed in an emphasis exp.
  2321. Use customize to modify this, or restart Emacs after changing it."
  2322. :group 'org-font-lock
  2323. :set 'org-set-emph-re
  2324. :type '(list
  2325. (sexp :tag "Allowed chars in pre ")
  2326. (sexp :tag "Allowed chars in post ")
  2327. (sexp :tag "Forbidden chars in border ")
  2328. (sexp :tag "Regexp for body ")
  2329. (integer :tag "number of newlines allowed")
  2330. (option (boolean :tag "Please ignore this button"))))
  2331. (defcustom org-emphasis-alist
  2332. `(("*" bold "<b>" "</b>")
  2333. ("/" italic "<i>" "</i>")
  2334. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2335. ("=" org-code "<code>" "</code>" verbatim)
  2336. ("~" org-verbatim "<code>" "</code>" verbatim)
  2337. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2338. "<del>" "</del>")
  2339. )
  2340. "Special syntax for emphasized text.
  2341. Text starting and ending with a special character will be emphasized, for
  2342. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2343. characters, the face to be used by font-lock for highlighting in Org-mode
  2344. Emacs buffers, and the HTML tags to be used for this.
  2345. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2346. Use customize to modify this, or restart Emacs after changing it."
  2347. :group 'org-font-lock
  2348. :set 'org-set-emph-re
  2349. :type '(repeat
  2350. (list
  2351. (string :tag "Marker character")
  2352. (choice
  2353. (face :tag "Font-lock-face")
  2354. (plist :tag "Face property list"))
  2355. (string :tag "HTML start tag")
  2356. (string :tag "HTML end tag")
  2357. (option (const verbatim)))))
  2358. ;;; Miscellaneous options
  2359. (defgroup org-completion nil
  2360. "Completion in Org-mode."
  2361. :tag "Org Completion"
  2362. :group 'org)
  2363. (defcustom org-completion-use-ido nil
  2364. "Non-nil means, use ido completion wherever possible.
  2365. Note that `ido-mode' must be active for this variable to be relevant.
  2366. If you decide to turn this variable on, you might well want to turn off
  2367. `org-outline-path-complete-in-steps'."
  2368. :group 'org-completion
  2369. :type 'boolean)
  2370. (defcustom org-completion-fallback-command 'hippie-expand
  2371. "The expansion command called by \\[org-complete] in normal context.
  2372. Normal means, no org-mode-specific context."
  2373. :group 'org-completion
  2374. :type 'function)
  2375. ;;; Functions and variables from ther packages
  2376. ;; Declared here to avoid compiler warnings
  2377. ;; XEmacs only
  2378. (defvar outline-mode-menu-heading)
  2379. (defvar outline-mode-menu-show)
  2380. (defvar outline-mode-menu-hide)
  2381. (defvar zmacs-regions) ; XEmacs regions
  2382. ;; Emacs only
  2383. (defvar mark-active)
  2384. ;; Various packages
  2385. (declare-function find-library-name "find-func" (library))
  2386. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2387. (declare-function calendar-forward-day "cal-move" (arg))
  2388. (declare-function calendar-goto-date "cal-move" (date))
  2389. (declare-function calendar-goto-today "cal-move" ())
  2390. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2391. (defvar calc-embedded-close-formula)
  2392. (defvar calc-embedded-open-formula)
  2393. (declare-function cdlatex-tab "ext:cdlatex" ())
  2394. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2395. (defvar font-lock-unfontify-region-function)
  2396. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2397. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2398. (defvar iswitchb-temp-buflist)
  2399. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2400. (defvar org-agenda-tags-todo-honor-ignore-options)
  2401. (declare-function org-agenda-skip "org-agenda" ())
  2402. (declare-function org-format-agenda-item "org-agenda"
  2403. (extra txt &optional category tags dotime noprefix remove-re))
  2404. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2405. (declare-function org-agenda-change-all-lines "org-agenda"
  2406. (newhead hdmarker &optional fixface just-this))
  2407. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2408. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2409. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2410. (beg end))
  2411. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2412. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2413. "org-agenda" (&optional end))
  2414. (declare-function parse-time-string "parse-time" (string))
  2415. (declare-function remember "remember" (&optional initial))
  2416. (declare-function remember-buffer-desc "remember" ())
  2417. (declare-function remember-finalize "remember" ())
  2418. (defvar remember-save-after-remembering)
  2419. (defvar remember-data-file)
  2420. (defvar remember-register)
  2421. (defvar remember-buffer)
  2422. (defvar remember-handler-functions)
  2423. (defvar remember-annotation-functions)
  2424. (defvar texmathp-why)
  2425. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2426. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2427. (defvar w3m-current-url)
  2428. (defvar w3m-current-title)
  2429. (defvar org-latex-regexps)
  2430. ;;; Autoload and prepare some org modules
  2431. ;; Some table stuff that needs to be defined here, because it is used
  2432. ;; by the functions setting up org-mode or checking for table context.
  2433. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2434. "Detects an org-type or table-type table.")
  2435. (defconst org-table-line-regexp "^[ \t]*|"
  2436. "Detects an org-type table line.")
  2437. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2438. "Detects an org-type table line.")
  2439. (defconst org-table-hline-regexp "^[ \t]*|-"
  2440. "Detects an org-type table hline.")
  2441. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2442. "Detects a table-type table hline.")
  2443. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2444. "Searching from within a table (any type) this finds the first line
  2445. outside the table.")
  2446. ;; Autoload the functions in org-table.el that are needed by functions here.
  2447. (eval-and-compile
  2448. (org-autoload "org-table"
  2449. '(org-table-align org-table-begin org-table-blank-field
  2450. org-table-convert org-table-convert-region org-table-copy-down
  2451. org-table-copy-region org-table-create
  2452. org-table-create-or-convert-from-region
  2453. org-table-create-with-table.el org-table-current-dline
  2454. org-table-cut-region org-table-delete-column org-table-edit-field
  2455. org-table-edit-formulas org-table-end org-table-eval-formula
  2456. org-table-export org-table-field-info
  2457. org-table-get-stored-formulas org-table-goto-column
  2458. org-table-hline-and-move org-table-import org-table-insert-column
  2459. org-table-insert-hline org-table-insert-row org-table-iterate
  2460. org-table-justify-field-maybe org-table-kill-row
  2461. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2462. org-table-move-column org-table-move-column-left
  2463. org-table-move-column-right org-table-move-row
  2464. org-table-move-row-down org-table-move-row-up
  2465. org-table-next-field org-table-next-row org-table-paste-rectangle
  2466. org-table-previous-field org-table-recalculate
  2467. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2468. org-table-toggle-coordinate-overlays
  2469. org-table-toggle-formula-debugger org-table-wrap-region
  2470. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2471. (defun org-at-table-p (&optional table-type)
  2472. "Return t if the cursor is inside an org-type table.
  2473. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2474. (if org-enable-table-editor
  2475. (save-excursion
  2476. (beginning-of-line 1)
  2477. (looking-at (if table-type org-table-any-line-regexp
  2478. org-table-line-regexp)))
  2479. nil))
  2480. (defsubst org-table-p () (org-at-table-p))
  2481. (defun org-at-table.el-p ()
  2482. "Return t if and only if we are at a table.el table."
  2483. (and (org-at-table-p 'any)
  2484. (save-excursion
  2485. (goto-char (org-table-begin 'any))
  2486. (looking-at org-table1-hline-regexp))))
  2487. (defun org-table-recognize-table.el ()
  2488. "If there is a table.el table nearby, recognize it and move into it."
  2489. (if org-table-tab-recognizes-table.el
  2490. (if (org-at-table.el-p)
  2491. (progn
  2492. (beginning-of-line 1)
  2493. (if (looking-at org-table-dataline-regexp)
  2494. nil
  2495. (if (looking-at org-table1-hline-regexp)
  2496. (progn
  2497. (beginning-of-line 2)
  2498. (if (looking-at org-table-any-border-regexp)
  2499. (beginning-of-line -1)))))
  2500. (if (re-search-forward "|" (org-table-end t) t)
  2501. (progn
  2502. (require 'table)
  2503. (if (table--at-cell-p (point))
  2504. t
  2505. (message "recognizing table.el table...")
  2506. (table-recognize-table)
  2507. (message "recognizing table.el table...done")))
  2508. (error "This should not happen..."))
  2509. t)
  2510. nil)
  2511. nil))
  2512. (defun org-at-table-hline-p ()
  2513. "Return t if the cursor is inside a hline in a table."
  2514. (if org-enable-table-editor
  2515. (save-excursion
  2516. (beginning-of-line 1)
  2517. (looking-at org-table-hline-regexp))
  2518. nil))
  2519. (defvar org-table-clean-did-remove-column nil)
  2520. (defun org-table-map-tables (function)
  2521. "Apply FUNCTION to the start of all tables in the buffer."
  2522. (save-excursion
  2523. (save-restriction
  2524. (widen)
  2525. (goto-char (point-min))
  2526. (while (re-search-forward org-table-any-line-regexp nil t)
  2527. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2528. (beginning-of-line 1)
  2529. (if (looking-at org-table-line-regexp)
  2530. (save-excursion (funcall function)))
  2531. (re-search-forward org-table-any-border-regexp nil 1))))
  2532. (message "Mapping tables: done"))
  2533. ;; Declare and autoload functions from org-exp.el
  2534. (declare-function org-default-export-plist "org-exp")
  2535. (declare-function org-infile-export-plist "org-exp")
  2536. (declare-function org-get-current-options "org-exp")
  2537. (eval-and-compile
  2538. (org-autoload "org-exp"
  2539. '(org-export org-export-as-ascii org-export-visible
  2540. org-insert-export-options-template org-export-as-html-and-open
  2541. org-export-as-html-batch org-export-as-html-to-buffer
  2542. org-replace-region-by-html org-export-region-as-html
  2543. org-export-as-html org-export-icalendar-this-file
  2544. org-export-icalendar-all-agenda-files
  2545. org-table-clean-before-export
  2546. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2547. ;; Declare and autoload functions from org-agenda.el
  2548. (eval-and-compile
  2549. (org-autoload "org-agenda"
  2550. '(org-agenda org-agenda-list org-search-view
  2551. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2552. org-diary org-agenda-to-appt
  2553. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2554. ;; Autoload org-remember
  2555. (eval-and-compile
  2556. (org-autoload "org-remember"
  2557. '(org-remember-insinuate org-remember-annotation
  2558. org-remember-apply-template org-remember org-remember-handler)))
  2559. ;; Autoload org-clock.el
  2560. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2561. (beg end))
  2562. (declare-function org-clock-update-mode-line "org-clock" ())
  2563. (defvar org-clock-start-time)
  2564. (defvar org-clock-marker (make-marker)
  2565. "Marker recording the last clock-in.")
  2566. (eval-and-compile
  2567. (org-autoload
  2568. "org-clock"
  2569. '(org-clock-in org-clock-out org-clock-cancel
  2570. org-clock-goto org-clock-sum org-clock-display
  2571. org-clock-remove-overlays org-clock-report
  2572. org-clocktable-shift org-dblock-write:clocktable
  2573. org-get-clocktable)))
  2574. (defun org-clock-update-time-maybe ()
  2575. "If this is a CLOCK line, update it and return t.
  2576. Otherwise, return nil."
  2577. (interactive)
  2578. (save-excursion
  2579. (beginning-of-line 1)
  2580. (skip-chars-forward " \t")
  2581. (when (looking-at org-clock-string)
  2582. (let ((re (concat "[ \t]*" org-clock-string
  2583. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2584. "\\([ \t]*=>.*\\)?\\)?"))
  2585. ts te h m s neg)
  2586. (cond
  2587. ((not (looking-at re))
  2588. nil)
  2589. ((not (match-end 2))
  2590. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2591. (> org-clock-marker (point))
  2592. (<= org-clock-marker (point-at-eol)))
  2593. ;; The clock is running here
  2594. (setq org-clock-start-time
  2595. (apply 'encode-time
  2596. (org-parse-time-string (match-string 1))))
  2597. (org-clock-update-mode-line)))
  2598. (t
  2599. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2600. (end-of-line 1)
  2601. (setq ts (match-string 1)
  2602. te (match-string 3))
  2603. (setq s (- (time-to-seconds
  2604. (apply 'encode-time (org-parse-time-string te)))
  2605. (time-to-seconds
  2606. (apply 'encode-time (org-parse-time-string ts))))
  2607. neg (< s 0)
  2608. s (abs s)
  2609. h (floor (/ s 3600))
  2610. s (- s (* 3600 h))
  2611. m (floor (/ s 60))
  2612. s (- s (* 60 s)))
  2613. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2614. t))))))
  2615. (defun org-check-running-clock ()
  2616. "Check if the current buffer contains the running clock.
  2617. If yes, offer to stop it and to save the buffer with the changes."
  2618. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2619. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2620. (buffer-name))))
  2621. (org-clock-out)
  2622. (when (y-or-n-p "Save changed buffer?")
  2623. (save-buffer))))
  2624. (defun org-clocktable-try-shift (dir n)
  2625. "Check if this line starts a clock table, if yes, shift the time block."
  2626. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2627. (org-clocktable-shift dir n)))
  2628. ;; Autoload org-timer.el
  2629. (eval-and-compile
  2630. (org-autoload
  2631. "org-timer"
  2632. '(org-timer-start org-timer org-timer-item
  2633. org-timer-change-times-in-region)))
  2634. ;; Autoload org-feed.el
  2635. (eval-and-compile
  2636. (org-autoload
  2637. "org-feed"
  2638. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2639. ;; Autoload archiving code
  2640. ;; The stuff that is needed for cycling and tags has to be defined here.
  2641. (defgroup org-archive nil
  2642. "Options concerning archiving in Org-mode."
  2643. :tag "Org Archive"
  2644. :group 'org-structure)
  2645. (defcustom org-archive-location "%s_archive::"
  2646. "The location where subtrees should be archived.
  2647. The value of this variable is a string, consisting of two parts,
  2648. separated by a double-colon. The first part is a filename and
  2649. the second part is a headline.
  2650. When the filename is omitted, archiving happens in the same file.
  2651. %s in the filename will be replaced by the current file
  2652. name (without the directory part). Archiving to a different file
  2653. is useful to keep archived entries from contributing to the
  2654. Org-mode Agenda.
  2655. The archived entries will be filed as subtrees of the specified
  2656. headline. When the headline is omitted, the subtrees are simply
  2657. filed away at the end of the file, as top-level entries. Also in
  2658. the heading you can use %s to represent the file name, this can be
  2659. useful when using the same archive for a number of different files.
  2660. Here are a few examples:
  2661. \"%s_archive::\"
  2662. If the current file is Projects.org, archive in file
  2663. Projects.org_archive, as top-level trees. This is the default.
  2664. \"::* Archived Tasks\"
  2665. Archive in the current file, under the top-level headline
  2666. \"* Archived Tasks\".
  2667. \"~/org/archive.org::\"
  2668. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2669. \"~/org/archive.org::From %s\"
  2670. Archive in file ~/org/archive.org (absolute path), und headlines
  2671. \"From FILENAME\" where file name is the current file name.
  2672. \"basement::** Finished Tasks\"
  2673. Archive in file ./basement (relative path), as level 3 trees
  2674. below the level 2 heading \"** Finished Tasks\".
  2675. You may set this option on a per-file basis by adding to the buffer a
  2676. line like
  2677. #+ARCHIVE: basement::** Finished Tasks
  2678. You may also define it locally for a subtree by setting an ARCHIVE property
  2679. in the entry. If such a property is found in an entry, or anywhere up
  2680. the hierarchy, it will be used."
  2681. :group 'org-archive
  2682. :type 'string)
  2683. (defcustom org-archive-tag "ARCHIVE"
  2684. "The tag that marks a subtree as archived.
  2685. An archived subtree does not open during visibility cycling, and does
  2686. not contribute to the agenda listings.
  2687. After changing this, font-lock must be restarted in the relevant buffers to
  2688. get the proper fontification."
  2689. :group 'org-archive
  2690. :group 'org-keywords
  2691. :type 'string)
  2692. (defcustom org-agenda-skip-archived-trees t
  2693. "Non-nil means, the agenda will skip any items located in archived trees.
  2694. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2695. variable is no longer recommended, you should leave it at the value t.
  2696. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2697. :group 'org-archive
  2698. :group 'org-agenda-skip
  2699. :type 'boolean)
  2700. (defcustom org-columns-skip-arrchived-trees t
  2701. "Non-nil means, irgnore archived trees when creating column view."
  2702. :group 'org-archive
  2703. :group 'org-properties
  2704. :type 'boolean)
  2705. (defcustom org-cycle-open-archived-trees nil
  2706. "Non-nil means, `org-cycle' will open archived trees.
  2707. An archived tree is a tree marked with the tag ARCHIVE.
  2708. When nil, archived trees will stay folded. You can still open them with
  2709. normal outline commands like `show-all', but not with the cycling commands."
  2710. :group 'org-archive
  2711. :group 'org-cycle
  2712. :type 'boolean)
  2713. (defcustom org-sparse-tree-open-archived-trees nil
  2714. "Non-nil means sparse tree construction shows matches in archived trees.
  2715. When nil, matches in these trees are highlighted, but the trees are kept in
  2716. collapsed state."
  2717. :group 'org-archive
  2718. :group 'org-sparse-trees
  2719. :type 'boolean)
  2720. (defun org-cycle-hide-archived-subtrees (state)
  2721. "Re-hide all archived subtrees after a visibility state change."
  2722. (when (and (not org-cycle-open-archived-trees)
  2723. (not (memq state '(overview folded))))
  2724. (save-excursion
  2725. (let* ((globalp (memq state '(contents all)))
  2726. (beg (if globalp (point-min) (point)))
  2727. (end (if globalp (point-max) (org-end-of-subtree t))))
  2728. (org-hide-archived-subtrees beg end)
  2729. (goto-char beg)
  2730. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2731. (message "%s" (substitute-command-keys
  2732. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2733. (defun org-force-cycle-archived ()
  2734. "Cycle subtree even if it is archived."
  2735. (interactive)
  2736. (setq this-command 'org-cycle)
  2737. (let ((org-cycle-open-archived-trees t))
  2738. (call-interactively 'org-cycle)))
  2739. (defun org-hide-archived-subtrees (beg end)
  2740. "Re-hide all archived subtrees after a visibility state change."
  2741. (save-excursion
  2742. (let* ((re (concat ":" org-archive-tag ":")))
  2743. (goto-char beg)
  2744. (while (re-search-forward re end t)
  2745. (and (org-on-heading-p) (hide-subtree))
  2746. (org-end-of-subtree t)))))
  2747. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2748. (eval-and-compile
  2749. (org-autoload "org-archive"
  2750. '(org-add-archive-files org-archive-subtree
  2751. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2752. ;; Autoload Column View Code
  2753. (declare-function org-columns-number-to-string "org-colview")
  2754. (declare-function org-columns-get-format-and-top-level "org-colview")
  2755. (declare-function org-columns-compute "org-colview")
  2756. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2757. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2758. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2759. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2760. ;; Autoload ID code
  2761. (declare-function org-id-store-link "org-id")
  2762. (declare-function org-id-locations-load "org-id")
  2763. (declare-function org-id-locations-save "org-id")
  2764. (defvar org-id-track-globally)
  2765. (org-autoload "org-id"
  2766. '(org-id-get-create org-id-new org-id-copy org-id-get
  2767. org-id-get-with-outline-path-completion
  2768. org-id-get-with-outline-drilling
  2769. org-id-goto org-id-find org-id-store-link))
  2770. ;; Autoload Plotting Code
  2771. (org-autoload "org-plot"
  2772. '(org-plot/gnuplot))
  2773. ;;; Variables for pre-computed regular expressions, all buffer local
  2774. (defvar org-drawer-regexp nil
  2775. "Matches first line of a hidden block.")
  2776. (make-variable-buffer-local 'org-drawer-regexp)
  2777. (defvar org-todo-regexp nil
  2778. "Matches any of the TODO state keywords.")
  2779. (make-variable-buffer-local 'org-todo-regexp)
  2780. (defvar org-not-done-regexp nil
  2781. "Matches any of the TODO state keywords except the last one.")
  2782. (make-variable-buffer-local 'org-not-done-regexp)
  2783. (defvar org-todo-line-regexp nil
  2784. "Matches a headline and puts TODO state into group 2 if present.")
  2785. (make-variable-buffer-local 'org-todo-line-regexp)
  2786. (defvar org-complex-heading-regexp nil
  2787. "Matches a headline and puts everything into groups:
  2788. group 1: the stars
  2789. group 2: The todo keyword, maybe
  2790. group 3: Priority cookie
  2791. group 4: True headline
  2792. group 5: Tags")
  2793. (make-variable-buffer-local 'org-complex-heading-regexp)
  2794. (defvar org-todo-line-tags-regexp nil
  2795. "Matches a headline and puts TODO state into group 2 if present.
  2796. Also put tags into group 4 if tags are present.")
  2797. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2798. (defvar org-nl-done-regexp nil
  2799. "Matches newline followed by a headline with the DONE keyword.")
  2800. (make-variable-buffer-local 'org-nl-done-regexp)
  2801. (defvar org-looking-at-done-regexp nil
  2802. "Matches the DONE keyword a point.")
  2803. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2804. (defvar org-ds-keyword-length 12
  2805. "Maximum length of the Deadline and SCHEDULED keywords.")
  2806. (make-variable-buffer-local 'org-ds-keyword-length)
  2807. (defvar org-deadline-regexp nil
  2808. "Matches the DEADLINE keyword.")
  2809. (make-variable-buffer-local 'org-deadline-regexp)
  2810. (defvar org-deadline-time-regexp nil
  2811. "Matches the DEADLINE keyword together with a time stamp.")
  2812. (make-variable-buffer-local 'org-deadline-time-regexp)
  2813. (defvar org-deadline-line-regexp nil
  2814. "Matches the DEADLINE keyword and the rest of the line.")
  2815. (make-variable-buffer-local 'org-deadline-line-regexp)
  2816. (defvar org-scheduled-regexp nil
  2817. "Matches the SCHEDULED keyword.")
  2818. (make-variable-buffer-local 'org-scheduled-regexp)
  2819. (defvar org-scheduled-time-regexp nil
  2820. "Matches the SCHEDULED keyword together with a time stamp.")
  2821. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2822. (defvar org-closed-time-regexp nil
  2823. "Matches the CLOSED keyword together with a time stamp.")
  2824. (make-variable-buffer-local 'org-closed-time-regexp)
  2825. (defvar org-keyword-time-regexp nil
  2826. "Matches any of the 4 keywords, together with the time stamp.")
  2827. (make-variable-buffer-local 'org-keyword-time-regexp)
  2828. (defvar org-keyword-time-not-clock-regexp nil
  2829. "Matches any of the 3 keywords, together with the time stamp.")
  2830. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2831. (defvar org-maybe-keyword-time-regexp nil
  2832. "Matches a timestamp, possibly preceeded by a keyword.")
  2833. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2834. (defvar org-planning-or-clock-line-re nil
  2835. "Matches a line with planning or clock info.")
  2836. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2837. (defconst org-plain-time-of-day-regexp
  2838. (concat
  2839. "\\(\\<[012]?[0-9]"
  2840. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2841. "\\(--?"
  2842. "\\(\\<[012]?[0-9]"
  2843. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2844. "\\)?")
  2845. "Regular expression to match a plain time or time range.
  2846. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2847. groups carry important information:
  2848. 0 the full match
  2849. 1 the first time, range or not
  2850. 8 the second time, if it is a range.")
  2851. (defconst org-plain-time-extension-regexp
  2852. (concat
  2853. "\\(\\<[012]?[0-9]"
  2854. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2855. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2856. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2857. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2858. groups carry important information:
  2859. 0 the full match
  2860. 7 hours of duration
  2861. 9 minutes of duration")
  2862. (defconst org-stamp-time-of-day-regexp
  2863. (concat
  2864. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2865. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2866. "\\(--?"
  2867. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2868. "Regular expression to match a timestamp time or time range.
  2869. After a match, the following groups carry important information:
  2870. 0 the full match
  2871. 1 date plus weekday, for backreferencing to make sure both times on same day
  2872. 2 the first time, range or not
  2873. 4 the second time, if it is a range.")
  2874. (defconst org-startup-options
  2875. '(("fold" org-startup-folded t)
  2876. ("overview" org-startup-folded t)
  2877. ("nofold" org-startup-folded nil)
  2878. ("showall" org-startup-folded nil)
  2879. ("content" org-startup-folded content)
  2880. ("hidestars" org-hide-leading-stars t)
  2881. ("showstars" org-hide-leading-stars nil)
  2882. ("odd" org-odd-levels-only t)
  2883. ("oddeven" org-odd-levels-only nil)
  2884. ("align" org-startup-align-all-tables t)
  2885. ("noalign" org-startup-align-all-tables nil)
  2886. ("customtime" org-display-custom-times t)
  2887. ("logdone" org-log-done time)
  2888. ("lognotedone" org-log-done note)
  2889. ("nologdone" org-log-done nil)
  2890. ("lognoteclock-out" org-log-note-clock-out t)
  2891. ("nolognoteclock-out" org-log-note-clock-out nil)
  2892. ("logrepeat" org-log-repeat state)
  2893. ("lognoterepeat" org-log-repeat note)
  2894. ("nologrepeat" org-log-repeat nil)
  2895. ("fninline" org-footnote-define-inline t)
  2896. ("nofninline" org-footnote-define-inline nil)
  2897. ("fnlocal" org-footnote-section nil)
  2898. ("fnauto" org-footnote-auto-label t)
  2899. ("fnprompt" org-footnote-auto-label nil)
  2900. ("fnconfirm" org-footnote-auto-label confirm)
  2901. ("fnplain" org-footnote-auto-label plain)
  2902. ("constcgs" constants-unit-system cgs)
  2903. ("constSI" constants-unit-system SI)
  2904. ("noptag" org-tag-persistent-alist nil))
  2905. "Variable associated with STARTUP options for org-mode.
  2906. Each element is a list of three items: The startup options as written
  2907. in the #+STARTUP line, the corresponding variable, and the value to
  2908. set this variable to if the option is found. An optional forth element PUSH
  2909. means to push this value onto the list in the variable.")
  2910. (defun org-set-regexps-and-options ()
  2911. "Precompute regular expressions for current buffer."
  2912. (when (org-mode-p)
  2913. (org-set-local 'org-todo-kwd-alist nil)
  2914. (org-set-local 'org-todo-key-alist nil)
  2915. (org-set-local 'org-todo-key-trigger nil)
  2916. (org-set-local 'org-todo-keywords-1 nil)
  2917. (org-set-local 'org-done-keywords nil)
  2918. (org-set-local 'org-todo-heads nil)
  2919. (org-set-local 'org-todo-sets nil)
  2920. (org-set-local 'org-todo-log-states nil)
  2921. (org-set-local 'org-file-properties nil)
  2922. (org-set-local 'org-file-tags nil)
  2923. (let ((re (org-make-options-regexp
  2924. '("CATEGORY" "TODO" "COLUMNS"
  2925. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2926. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2927. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2928. (splitre "[ \t]+")
  2929. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2930. tail sep kws1 prio props ftags drawers
  2931. ext-setup-or-nil setup-contents (start 0))
  2932. (save-excursion
  2933. (save-restriction
  2934. (widen)
  2935. (goto-char (point-min))
  2936. (while (or (and ext-setup-or-nil
  2937. (string-match re ext-setup-or-nil start)
  2938. (setq start (match-end 0)))
  2939. (and (setq ext-setup-or-nil nil start 0)
  2940. (re-search-forward re nil t)))
  2941. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2942. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2943. (cond
  2944. ((equal key "CATEGORY")
  2945. (if (string-match "[ \t]+$" value)
  2946. (setq value (replace-match "" t t value)))
  2947. (setq cat value))
  2948. ((member key '("SEQ_TODO" "TODO"))
  2949. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2950. ((equal key "TYP_TODO")
  2951. (push (cons 'type (org-split-string value splitre)) kwds))
  2952. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2953. ;; general TODO-like setup
  2954. (push (cons (intern (downcase (match-string 1 key)))
  2955. (org-split-string value splitre)) kwds))
  2956. ((equal key "TAGS")
  2957. (setq tags (append tags (if tags '("\\n") nil)
  2958. (org-split-string value splitre))))
  2959. ((equal key "COLUMNS")
  2960. (org-set-local 'org-columns-default-format value))
  2961. ((equal key "LINK")
  2962. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2963. (push (cons (match-string 1 value)
  2964. (org-trim (match-string 2 value)))
  2965. links)))
  2966. ((equal key "PRIORITIES")
  2967. (setq prio (org-split-string value " +")))
  2968. ((equal key "PROPERTY")
  2969. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2970. (push (cons (match-string 1 value) (match-string 2 value))
  2971. props)))
  2972. ((equal key "FILETAGS")
  2973. (when (string-match "\\S-" value)
  2974. (setq ftags
  2975. (append
  2976. ftags
  2977. (apply 'append
  2978. (mapcar (lambda (x) (org-split-string x ":"))
  2979. (org-split-string value)))))))
  2980. ((equal key "DRAWERS")
  2981. (setq drawers (org-split-string value splitre)))
  2982. ((equal key "CONSTANTS")
  2983. (setq const (append const (org-split-string value splitre))))
  2984. ((equal key "STARTUP")
  2985. (let ((opts (org-split-string value splitre))
  2986. l var val)
  2987. (while (setq l (pop opts))
  2988. (when (setq l (assoc l org-startup-options))
  2989. (setq var (nth 1 l) val (nth 2 l))
  2990. (if (not (nth 3 l))
  2991. (set (make-local-variable var) val)
  2992. (if (not (listp (symbol-value var)))
  2993. (set (make-local-variable var) nil))
  2994. (set (make-local-variable var) (symbol-value var))
  2995. (add-to-list var val))))))
  2996. ((equal key "ARCHIVE")
  2997. (string-match " *$" value)
  2998. (setq arch (replace-match "" t t value))
  2999. (remove-text-properties 0 (length arch)
  3000. '(face t fontified t) arch))
  3001. ((equal key "SETUPFILE")
  3002. (setq setup-contents (org-file-contents
  3003. (expand-file-name
  3004. (org-remove-double-quotes value))
  3005. 'noerror))
  3006. (if (not ext-setup-or-nil)
  3007. (setq ext-setup-or-nil setup-contents start 0)
  3008. (setq ext-setup-or-nil
  3009. (concat (substring ext-setup-or-nil 0 start)
  3010. "\n" setup-contents "\n"
  3011. (substring ext-setup-or-nil start)))))
  3012. ))))
  3013. (when cat
  3014. (org-set-local 'org-category (intern cat))
  3015. (push (cons "CATEGORY" cat) props))
  3016. (when prio
  3017. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3018. (setq prio (mapcar 'string-to-char prio))
  3019. (org-set-local 'org-highest-priority (nth 0 prio))
  3020. (org-set-local 'org-lowest-priority (nth 1 prio))
  3021. (org-set-local 'org-default-priority (nth 2 prio)))
  3022. (and props (org-set-local 'org-file-properties (nreverse props)))
  3023. (and ftags (org-set-local 'org-file-tags
  3024. (mapcar 'org-add-prop-inherited ftags)))
  3025. (and drawers (org-set-local 'org-drawers drawers))
  3026. (and arch (org-set-local 'org-archive-location arch))
  3027. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3028. ;; Process the TODO keywords
  3029. (unless kwds
  3030. ;; Use the global values as if they had been given locally.
  3031. (setq kwds (default-value 'org-todo-keywords))
  3032. (if (stringp (car kwds))
  3033. (setq kwds (list (cons org-todo-interpretation
  3034. (default-value 'org-todo-keywords)))))
  3035. (setq kwds (reverse kwds)))
  3036. (setq kwds (nreverse kwds))
  3037. (let (inter kws kw)
  3038. (while (setq kws (pop kwds))
  3039. (let ((kws (or
  3040. (run-hook-with-args-until-success
  3041. 'org-todo-setup-filter-hook kws)
  3042. kws)))
  3043. (setq inter (pop kws) sep (member "|" kws)
  3044. kws0 (delete "|" (copy-sequence kws))
  3045. kwsa nil
  3046. kws1 (mapcar
  3047. (lambda (x)
  3048. ;; 1 2
  3049. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3050. (progn
  3051. (setq kw (match-string 1 x)
  3052. key (and (match-end 2) (match-string 2 x))
  3053. log (org-extract-log-state-settings x))
  3054. (push (cons kw (and key (string-to-char key))) kwsa)
  3055. (and log (push log org-todo-log-states))
  3056. kw)
  3057. (error "Invalid TODO keyword %s" x)))
  3058. kws0)
  3059. kwsa (if kwsa (append '((:startgroup))
  3060. (nreverse kwsa)
  3061. '((:endgroup))))
  3062. hw (car kws1)
  3063. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3064. tail (list inter hw (car dws) (org-last dws))))
  3065. (add-to-list 'org-todo-heads hw 'append)
  3066. (push kws1 org-todo-sets)
  3067. (setq org-done-keywords (append org-done-keywords dws nil))
  3068. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3069. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3070. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3071. (setq org-todo-sets (nreverse org-todo-sets)
  3072. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3073. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3074. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3075. ;; Process the constants
  3076. (when const
  3077. (let (e cst)
  3078. (while (setq e (pop const))
  3079. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3080. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3081. (setq org-table-formula-constants-local cst)))
  3082. ;; Process the tags.
  3083. (when tags
  3084. (let (e tgs)
  3085. (while (setq e (pop tags))
  3086. (cond
  3087. ((equal e "{") (push '(:startgroup) tgs))
  3088. ((equal e "}") (push '(:endgroup) tgs))
  3089. ((equal e "\\n") (push '(:newline) tgs))
  3090. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3091. (push (cons (match-string 1 e)
  3092. (string-to-char (match-string 2 e)))
  3093. tgs))
  3094. (t (push (list e) tgs))))
  3095. (org-set-local 'org-tag-alist nil)
  3096. (while (setq e (pop tgs))
  3097. (or (and (stringp (car e))
  3098. (assoc (car e) org-tag-alist))
  3099. (push e org-tag-alist)))))
  3100. ;; Compute the regular expressions and other local variables
  3101. (if (not org-done-keywords)
  3102. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3103. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3104. (length org-scheduled-string)
  3105. (length org-clock-string)
  3106. (length org-closed-string)))
  3107. org-drawer-regexp
  3108. (concat "^[ \t]*:\\("
  3109. (mapconcat 'regexp-quote org-drawers "\\|")
  3110. "\\):[ \t]*$")
  3111. org-not-done-keywords
  3112. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3113. org-todo-regexp
  3114. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3115. "\\|") "\\)\\>")
  3116. org-not-done-regexp
  3117. (concat "\\<\\("
  3118. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3119. "\\)\\>")
  3120. org-todo-line-regexp
  3121. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3122. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3123. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3124. org-complex-heading-regexp
  3125. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3126. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3127. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3128. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3129. org-nl-done-regexp
  3130. (concat "\n\\*+[ \t]+"
  3131. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3132. "\\)" "\\>")
  3133. org-todo-line-tags-regexp
  3134. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3135. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3136. (org-re
  3137. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3138. org-looking-at-done-regexp
  3139. (concat "^" "\\(?:"
  3140. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3141. "\\>")
  3142. org-deadline-regexp (concat "\\<" org-deadline-string)
  3143. org-deadline-time-regexp
  3144. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3145. org-deadline-line-regexp
  3146. (concat "\\<\\(" org-deadline-string "\\).*")
  3147. org-scheduled-regexp
  3148. (concat "\\<" org-scheduled-string)
  3149. org-scheduled-time-regexp
  3150. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3151. org-closed-time-regexp
  3152. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3153. org-keyword-time-regexp
  3154. (concat "\\<\\(" org-scheduled-string
  3155. "\\|" org-deadline-string
  3156. "\\|" org-closed-string
  3157. "\\|" org-clock-string "\\)"
  3158. " *[[<]\\([^]>]+\\)[]>]")
  3159. org-keyword-time-not-clock-regexp
  3160. (concat "\\<\\(" org-scheduled-string
  3161. "\\|" org-deadline-string
  3162. "\\|" org-closed-string
  3163. "\\)"
  3164. " *[[<]\\([^]>]+\\)[]>]")
  3165. org-maybe-keyword-time-regexp
  3166. (concat "\\(\\<\\(" org-scheduled-string
  3167. "\\|" org-deadline-string
  3168. "\\|" org-closed-string
  3169. "\\|" org-clock-string "\\)\\)?"
  3170. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3171. org-planning-or-clock-line-re
  3172. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3173. "\\|" org-deadline-string
  3174. "\\|" org-closed-string "\\|" org-clock-string
  3175. "\\)\\>\\)")
  3176. )
  3177. (org-compute-latex-and-specials-regexp)
  3178. (org-set-font-lock-defaults))))
  3179. (defun org-file-contents (file &optional noerror)
  3180. "Return the contents of FILE, as a string."
  3181. (if (or (not file)
  3182. (not (file-readable-p file)))
  3183. (if noerror
  3184. (progn
  3185. (message "Cannot read file %s" file)
  3186. (ding) (sit-for 2)
  3187. "")
  3188. (error "Cannot read file %s" file))
  3189. (with-temp-buffer
  3190. (insert-file-contents file)
  3191. (buffer-string))))
  3192. (defun org-extract-log-state-settings (x)
  3193. "Extract the log state setting from a TODO keyword string.
  3194. This will extract info from a string like \"WAIT(w@/!)\"."
  3195. (let (kw key log1 log2)
  3196. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3197. (setq kw (match-string 1 x)
  3198. key (and (match-end 2) (match-string 2 x))
  3199. log1 (and (match-end 3) (match-string 3 x))
  3200. log2 (and (match-end 4) (match-string 4 x)))
  3201. (and (or log1 log2)
  3202. (list kw
  3203. (and log1 (if (equal log1 "!") 'time 'note))
  3204. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3205. (defun org-remove-keyword-keys (list)
  3206. "Remove a pair of parenthesis at the end of each string in LIST."
  3207. (mapcar (lambda (x)
  3208. (if (string-match "(.*)$" x)
  3209. (substring x 0 (match-beginning 0))
  3210. x))
  3211. list))
  3212. ;; FIXME: this could be done much better, using second characters etc.
  3213. (defun org-assign-fast-keys (alist)
  3214. "Assign fast keys to a keyword-key alist.
  3215. Respect keys that are already there."
  3216. (let (new e k c c1 c2 (char ?a))
  3217. (while (setq e (pop alist))
  3218. (cond
  3219. ((equal e '(:startgroup)) (push e new))
  3220. ((equal e '(:endgroup)) (push e new))
  3221. ((equal e '(:newline)) (push e new))
  3222. (t
  3223. (setq k (car e) c2 nil)
  3224. (if (cdr e)
  3225. (setq c (cdr e))
  3226. ;; automatically assign a character.
  3227. (setq c1 (string-to-char
  3228. (downcase (substring
  3229. k (if (= (string-to-char k) ?@) 1 0)))))
  3230. (if (or (rassoc c1 new) (rassoc c1 alist))
  3231. (while (or (rassoc char new) (rassoc char alist))
  3232. (setq char (1+ char)))
  3233. (setq c2 c1))
  3234. (setq c (or c2 char)))
  3235. (push (cons k c) new))))
  3236. (nreverse new)))
  3237. ;;; Some variables used in various places
  3238. (defvar org-window-configuration nil
  3239. "Used in various places to store a window configuration.")
  3240. (defvar org-finish-function nil
  3241. "Function to be called when `C-c C-c' is used.
  3242. This is for getting out of special buffers like remember.")
  3243. ;; FIXME: Occasionally check by commenting these, to make sure
  3244. ;; no other functions uses these, forgetting to let-bind them.
  3245. (defvar entry)
  3246. (defvar state)
  3247. (defvar last-state)
  3248. (defvar date)
  3249. (defvar description)
  3250. ;; Defined somewhere in this file, but used before definition.
  3251. (defvar org-html-entities)
  3252. (defvar org-struct-menu)
  3253. (defvar org-org-menu)
  3254. (defvar org-tbl-menu)
  3255. (defvar org-agenda-keymap)
  3256. ;;;; Define the Org-mode
  3257. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3258. (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."))
  3259. ;; We use a before-change function to check if a table might need
  3260. ;; an update.
  3261. (defvar org-table-may-need-update t
  3262. "Indicates that a table might need an update.
  3263. This variable is set by `org-before-change-function'.
  3264. `org-table-align' sets it back to nil.")
  3265. (defun org-before-change-function (beg end)
  3266. "Every change indicates that a table might need an update."
  3267. (setq org-table-may-need-update t))
  3268. (defvar org-mode-map)
  3269. (defvar org-mode-hook nil
  3270. "Mode hook for Org-mode, run after the mode was turned on.")
  3271. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3272. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3273. (defvar org-table-buffer-is-an nil)
  3274. (defconst org-outline-regexp "\\*+ ")
  3275. ;;;###autoload
  3276. (define-derived-mode org-mode outline-mode "Org"
  3277. "Outline-based notes management and organizer, alias
  3278. \"Carsten's outline-mode for keeping track of everything.\"
  3279. Org-mode develops organizational tasks around a NOTES file which
  3280. contains information about projects as plain text. Org-mode is
  3281. implemented on top of outline-mode, which is ideal to keep the content
  3282. of large files well structured. It supports ToDo items, deadlines and
  3283. time stamps, which magically appear in the diary listing of the Emacs
  3284. calendar. Tables are easily created with a built-in table editor.
  3285. Plain text URL-like links connect to websites, emails (VM), Usenet
  3286. messages (Gnus), BBDB entries, and any files related to the project.
  3287. For printing and sharing of notes, an Org-mode file (or a part of it)
  3288. can be exported as a structured ASCII or HTML file.
  3289. The following commands are available:
  3290. \\{org-mode-map}"
  3291. ;; Get rid of Outline menus, they are not needed
  3292. ;; Need to do this here because define-derived-mode sets up
  3293. ;; the keymap so late. Still, it is a waste to call this each time
  3294. ;; we switch another buffer into org-mode.
  3295. (if (featurep 'xemacs)
  3296. (when (boundp 'outline-mode-menu-heading)
  3297. ;; Assume this is Greg's port, it used easymenu
  3298. (easy-menu-remove outline-mode-menu-heading)
  3299. (easy-menu-remove outline-mode-menu-show)
  3300. (easy-menu-remove outline-mode-menu-hide))
  3301. (define-key org-mode-map [menu-bar headings] 'undefined)
  3302. (define-key org-mode-map [menu-bar hide] 'undefined)
  3303. (define-key org-mode-map [menu-bar show] 'undefined))
  3304. (org-load-modules-maybe)
  3305. (easy-menu-add org-org-menu)
  3306. (easy-menu-add org-tbl-menu)
  3307. (org-install-agenda-files-menu)
  3308. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3309. (org-add-to-invisibility-spec '(org-cwidth))
  3310. (when (featurep 'xemacs)
  3311. (org-set-local 'line-move-ignore-invisible t))
  3312. (org-set-local 'outline-regexp org-outline-regexp)
  3313. (org-set-local 'outline-level 'org-outline-level)
  3314. (when (and org-ellipsis
  3315. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3316. (fboundp 'make-glyph-code))
  3317. (unless org-display-table
  3318. (setq org-display-table (make-display-table)))
  3319. (set-display-table-slot
  3320. org-display-table 4
  3321. (vconcat (mapcar
  3322. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3323. org-ellipsis)))
  3324. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3325. (setq buffer-display-table org-display-table))
  3326. (org-set-regexps-and-options)
  3327. (when (and org-tag-faces (not org-tags-special-faces-re))
  3328. ;; tag faces set outside customize.... force initialization.
  3329. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3330. ;; Calc embedded
  3331. (org-set-local 'calc-embedded-open-mode "# ")
  3332. (modify-syntax-entry ?# "<")
  3333. (modify-syntax-entry ?@ "w")
  3334. (if org-startup-truncated (setq truncate-lines t))
  3335. (org-set-local 'font-lock-unfontify-region-function
  3336. 'org-unfontify-region)
  3337. ;; Activate before-change-function
  3338. (org-set-local 'org-table-may-need-update t)
  3339. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3340. 'local)
  3341. ;; Check for running clock before killing a buffer
  3342. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3343. ;; Paragraphs and auto-filling
  3344. (org-set-autofill-regexps)
  3345. (setq indent-line-function 'org-indent-line-function)
  3346. (org-update-radio-target-regexp)
  3347. ;; Make sure dependence stuff works reliably, even for users who set it
  3348. ;; too late :-(
  3349. (if org-enforce-todo-dependencies
  3350. (add-hook 'org-blocker-hook
  3351. 'org-block-todo-from-children-or-siblings)
  3352. (remove-hook 'org-blocker-hook
  3353. 'org-block-todo-from-children-or-siblings))
  3354. (if org-enforce-todo-checkbox-dependencies
  3355. (add-hook 'org-blocker-hook
  3356. 'org-block-todo-from-checkboxes)
  3357. (remove-hook 'org-blocker-hook
  3358. 'org-block-todo-from-checkboxes))
  3359. ;; Comment characters
  3360. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3361. (org-set-local 'comment-padding " ")
  3362. ;; Align options lines
  3363. (org-set-local
  3364. 'align-mode-rules-list
  3365. '((org-in-buffer-settings
  3366. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3367. (modes . '(org-mode)))))
  3368. ;; Imenu
  3369. (org-set-local 'imenu-create-index-function
  3370. 'org-imenu-get-tree)
  3371. ;; Make isearch reveal context
  3372. (if (or (featurep 'xemacs)
  3373. (not (boundp 'outline-isearch-open-invisible-function)))
  3374. ;; Emacs 21 and XEmacs make use of the hook
  3375. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3376. ;; Emacs 22 deals with this through a special variable
  3377. (org-set-local 'outline-isearch-open-invisible-function
  3378. (lambda (&rest ignore) (org-show-context 'isearch))))
  3379. ;; If empty file that did not turn on org-mode automatically, make it to.
  3380. (if (and org-insert-mode-line-in-empty-file
  3381. (interactive-p)
  3382. (= (point-min) (point-max)))
  3383. (insert "# -*- mode: org -*-\n\n"))
  3384. (unless org-inhibit-startup
  3385. (when org-startup-align-all-tables
  3386. (let ((bmp (buffer-modified-p)))
  3387. (org-table-map-tables 'org-table-align)
  3388. (set-buffer-modified-p bmp)))
  3389. (org-set-startup-visibility)))
  3390. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3391. (defun org-current-time ()
  3392. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3393. (if (> (car org-time-stamp-rounding-minutes) 1)
  3394. (let ((r (car org-time-stamp-rounding-minutes))
  3395. (time (decode-time)))
  3396. (apply 'encode-time
  3397. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3398. (nthcdr 2 time))))
  3399. (current-time)))
  3400. ;;;; Font-Lock stuff, including the activators
  3401. (defvar org-mouse-map (make-sparse-keymap))
  3402. (org-defkey org-mouse-map
  3403. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3404. (org-defkey org-mouse-map
  3405. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3406. (when org-mouse-1-follows-link
  3407. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3408. (when org-tab-follows-link
  3409. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3410. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3411. (require 'font-lock)
  3412. (defconst org-non-link-chars "]\t\n\r<>")
  3413. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3414. "shell" "elisp"))
  3415. (defvar org-link-types-re nil
  3416. "Matches a link that has a url-like prefix like \"http:\"")
  3417. (defvar org-link-re-with-space nil
  3418. "Matches a link with spaces, optional angular brackets around it.")
  3419. (defvar org-link-re-with-space2 nil
  3420. "Matches a link with spaces, optional angular brackets around it.")
  3421. (defvar org-link-re-with-space3 nil
  3422. "Matches a link with spaces, only for internal part in bracket links.")
  3423. (defvar org-angle-link-re nil
  3424. "Matches link with angular brackets, spaces are allowed.")
  3425. (defvar org-plain-link-re nil
  3426. "Matches plain link, without spaces.")
  3427. (defvar org-bracket-link-regexp nil
  3428. "Matches a link in double brackets.")
  3429. (defvar org-bracket-link-analytic-regexp nil
  3430. "Regular expression used to analyze links.
  3431. Here is what the match groups contain after a match:
  3432. 1: http:
  3433. 2: http
  3434. 3: path
  3435. 4: [desc]
  3436. 5: desc")
  3437. (defvar org-bracket-link-analytic-regexp++ nil
  3438. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3439. (defvar org-any-link-re nil
  3440. "Regular expression matching any link.")
  3441. (defun org-make-link-regexps ()
  3442. "Update the link regular expressions.
  3443. This should be called after the variable `org-link-types' has changed."
  3444. (setq org-link-types-re
  3445. (concat
  3446. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3447. org-link-re-with-space
  3448. (concat
  3449. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3450. "\\([^" org-non-link-chars " ]"
  3451. "[^" org-non-link-chars "]*"
  3452. "[^" org-non-link-chars " ]\\)>?")
  3453. org-link-re-with-space2
  3454. (concat
  3455. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3456. "\\([^" org-non-link-chars " ]"
  3457. "[^\t\n\r]*"
  3458. "[^" org-non-link-chars " ]\\)>?")
  3459. org-link-re-with-space3
  3460. (concat
  3461. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3462. "\\([^" org-non-link-chars " ]"
  3463. "[^\t\n\r]*\\)")
  3464. org-angle-link-re
  3465. (concat
  3466. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3467. "\\([^" org-non-link-chars " ]"
  3468. "[^" org-non-link-chars "]*"
  3469. "\\)>")
  3470. org-plain-link-re
  3471. (concat
  3472. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3473. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3474. org-bracket-link-regexp
  3475. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3476. org-bracket-link-analytic-regexp
  3477. (concat
  3478. "\\[\\["
  3479. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3480. "\\([^]]+\\)"
  3481. "\\]"
  3482. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3483. "\\]")
  3484. org-bracket-link-analytic-regexp++
  3485. (concat
  3486. "\\[\\["
  3487. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3488. "\\([^]]+\\)"
  3489. "\\]"
  3490. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3491. "\\]")
  3492. org-any-link-re
  3493. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3494. org-angle-link-re "\\)\\|\\("
  3495. org-plain-link-re "\\)")))
  3496. (org-make-link-regexps)
  3497. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3498. "Regular expression for fast time stamp matching.")
  3499. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3500. "Regular expression for fast time stamp matching.")
  3501. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3502. "Regular expression matching time strings for analysis.
  3503. This one does not require the space after the date, so it can be used
  3504. on a string that terminates immediately after the date.")
  3505. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3506. "Regular expression matching time strings for analysis.")
  3507. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3508. "Regular expression matching time stamps, with groups.")
  3509. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3510. "Regular expression matching time stamps (also [..]), with groups.")
  3511. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3512. "Regular expression matching a time stamp range.")
  3513. (defconst org-tr-regexp-both
  3514. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3515. "Regular expression matching a time stamp range.")
  3516. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3517. org-ts-regexp "\\)?")
  3518. "Regular expression matching a time stamp or time stamp range.")
  3519. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3520. org-ts-regexp-both "\\)?")
  3521. "Regular expression matching a time stamp or time stamp range.
  3522. The time stamps may be either active or inactive.")
  3523. (defvar org-emph-face nil)
  3524. (defun org-do-emphasis-faces (limit)
  3525. "Run through the buffer and add overlays to links."
  3526. (let (rtn)
  3527. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3528. (if (not (= (char-after (match-beginning 3))
  3529. (char-after (match-beginning 4))))
  3530. (progn
  3531. (setq rtn t)
  3532. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3533. 'face
  3534. (nth 1 (assoc (match-string 3)
  3535. org-emphasis-alist)))
  3536. (add-text-properties (match-beginning 2) (match-end 2)
  3537. '(font-lock-multiline t))
  3538. (when org-hide-emphasis-markers
  3539. (add-text-properties (match-end 4) (match-beginning 5)
  3540. '(invisible org-link))
  3541. (add-text-properties (match-beginning 3) (match-end 3)
  3542. '(invisible org-link)))))
  3543. (backward-char 1))
  3544. rtn))
  3545. (defun org-emphasize (&optional char)
  3546. "Insert or change an emphasis, i.e. a font like bold or italic.
  3547. If there is an active region, change that region to a new emphasis.
  3548. If there is no region, just insert the marker characters and position
  3549. the cursor between them.
  3550. CHAR should be either the marker character, or the first character of the
  3551. HTML tag associated with that emphasis. If CHAR is a space, the means
  3552. to remove the emphasis of the selected region.
  3553. If char is not given (for example in an interactive call) it
  3554. will be prompted for."
  3555. (interactive)
  3556. (let ((eal org-emphasis-alist) e det
  3557. (erc org-emphasis-regexp-components)
  3558. (prompt "")
  3559. (string "") beg end move tag c s)
  3560. (if (org-region-active-p)
  3561. (setq beg (region-beginning) end (region-end)
  3562. string (buffer-substring beg end))
  3563. (setq move t))
  3564. (while (setq e (pop eal))
  3565. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3566. c (aref tag 0))
  3567. (push (cons c (string-to-char (car e))) det)
  3568. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3569. (substring tag 1)))))
  3570. (setq det (nreverse det))
  3571. (unless char
  3572. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3573. (setq char (read-char-exclusive)))
  3574. (setq char (or (cdr (assoc char det)) char))
  3575. (if (equal char ?\ )
  3576. (setq s "" move nil)
  3577. (unless (assoc (char-to-string char) org-emphasis-alist)
  3578. (error "No such emphasis marker: \"%c\"" char))
  3579. (setq s (char-to-string char)))
  3580. (while (and (> (length string) 1)
  3581. (equal (substring string 0 1) (substring string -1))
  3582. (assoc (substring string 0 1) org-emphasis-alist))
  3583. (setq string (substring string 1 -1)))
  3584. (setq string (concat s string s))
  3585. (if beg (delete-region beg end))
  3586. (unless (or (bolp)
  3587. (string-match (concat "[" (nth 0 erc) "\n]")
  3588. (char-to-string (char-before (point)))))
  3589. (insert " "))
  3590. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3591. (char-to-string (char-after (point))))
  3592. (insert " ") (backward-char 1))
  3593. (insert string)
  3594. (and move (backward-char 1))))
  3595. (defconst org-nonsticky-props
  3596. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3597. (defun org-activate-plain-links (limit)
  3598. "Run through the buffer and add overlays to links."
  3599. (catch 'exit
  3600. (let (f)
  3601. (while (re-search-forward org-plain-link-re limit t)
  3602. (setq f (get-text-property (match-beginning 0) 'face))
  3603. (if (or (eq f 'org-tag)
  3604. (and (listp f) (memq 'org-tag f)))
  3605. nil
  3606. (add-text-properties (match-beginning 0) (match-end 0)
  3607. (list 'mouse-face 'highlight
  3608. 'rear-nonsticky org-nonsticky-props
  3609. 'keymap org-mouse-map
  3610. ))
  3611. (throw 'exit t))))))
  3612. (defun org-activate-code (limit)
  3613. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3614. (progn
  3615. (remove-text-properties (match-beginning 0) (match-end 0)
  3616. '(display t invisible t intangible t))
  3617. t)))
  3618. (defun org-activate-angle-links (limit)
  3619. "Run through the buffer and add overlays to links."
  3620. (if (re-search-forward org-angle-link-re limit t)
  3621. (progn
  3622. (add-text-properties (match-beginning 0) (match-end 0)
  3623. (list 'mouse-face 'highlight
  3624. 'rear-nonsticky org-nonsticky-props
  3625. 'keymap org-mouse-map
  3626. ))
  3627. t)))
  3628. (defun org-activate-footnote-links (limit)
  3629. "Run through the buffer and add overlays to links."
  3630. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3631. limit t)
  3632. (progn
  3633. (add-text-properties (match-beginning 2) (match-end 2)
  3634. (list 'mouse-face 'highlight
  3635. 'rear-nonsticky org-nonsticky-props
  3636. 'keymap org-mouse-map
  3637. 'help-echo
  3638. (if (= (point-at-bol) (match-beginning 2))
  3639. "Footnote definition"
  3640. "Footnote reference")
  3641. ))
  3642. t)))
  3643. (defun org-activate-bracket-links (limit)
  3644. "Run through the buffer and add overlays to bracketed links."
  3645. (if (re-search-forward org-bracket-link-regexp limit t)
  3646. (let* ((help (concat "LINK: "
  3647. (org-match-string-no-properties 1)))
  3648. ;; FIXME: above we should remove the escapes.
  3649. ;; but that requires another match, protecting match data,
  3650. ;; a lot of overhead for font-lock.
  3651. (ip (org-maybe-intangible
  3652. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3653. 'keymap org-mouse-map 'mouse-face 'highlight
  3654. 'font-lock-multiline t 'help-echo help)))
  3655. (vp (list 'rear-nonsticky org-nonsticky-props
  3656. 'keymap org-mouse-map 'mouse-face 'highlight
  3657. 'font-lock-multiline t 'help-echo help)))
  3658. ;; We need to remove the invisible property here. Table narrowing
  3659. ;; may have made some of this invisible.
  3660. (remove-text-properties (match-beginning 0) (match-end 0)
  3661. '(invisible nil))
  3662. (if (match-end 3)
  3663. (progn
  3664. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3665. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3666. (add-text-properties (match-end 3) (match-end 0) ip))
  3667. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3668. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3669. (add-text-properties (match-end 1) (match-end 0) ip))
  3670. t)))
  3671. (defun org-activate-dates (limit)
  3672. "Run through the buffer and add overlays to dates."
  3673. (if (re-search-forward org-tsr-regexp-both limit t)
  3674. (progn
  3675. (add-text-properties (match-beginning 0) (match-end 0)
  3676. (list 'mouse-face 'highlight
  3677. 'rear-nonsticky org-nonsticky-props
  3678. 'keymap org-mouse-map))
  3679. (when org-display-custom-times
  3680. (if (match-end 3)
  3681. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3682. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3683. t)))
  3684. (defvar org-target-link-regexp nil
  3685. "Regular expression matching radio targets in plain text.")
  3686. (make-variable-buffer-local 'org-target-link-regexp)
  3687. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3688. "Regular expression matching a link target.")
  3689. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3690. "Regular expression matching a radio target.")
  3691. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3692. "Regular expression matching any target.")
  3693. (defun org-activate-target-links (limit)
  3694. "Run through the buffer and add overlays to target matches."
  3695. (when org-target-link-regexp
  3696. (let ((case-fold-search t))
  3697. (if (re-search-forward org-target-link-regexp limit t)
  3698. (progn
  3699. (add-text-properties (match-beginning 0) (match-end 0)
  3700. (list 'mouse-face 'highlight
  3701. 'rear-nonsticky org-nonsticky-props
  3702. 'keymap org-mouse-map
  3703. 'help-echo "Radio target link"
  3704. 'org-linked-text t))
  3705. t)))))
  3706. (defun org-update-radio-target-regexp ()
  3707. "Find all radio targets in this file and update the regular expression."
  3708. (interactive)
  3709. (when (memq 'radio org-activate-links)
  3710. (setq org-target-link-regexp
  3711. (org-make-target-link-regexp (org-all-targets 'radio)))
  3712. (org-restart-font-lock)))
  3713. (defun org-hide-wide-columns (limit)
  3714. (let (s e)
  3715. (setq s (text-property-any (point) (or limit (point-max))
  3716. 'org-cwidth t))
  3717. (when s
  3718. (setq e (next-single-property-change s 'org-cwidth))
  3719. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3720. (goto-char e)
  3721. t)))
  3722. (defvar org-latex-and-specials-regexp nil
  3723. "Regular expression for highlighting export special stuff.")
  3724. (defvar org-match-substring-regexp)
  3725. (defvar org-match-substring-with-braces-regexp)
  3726. (defvar org-export-html-special-string-regexps)
  3727. (defun org-compute-latex-and-specials-regexp ()
  3728. "Compute regular expression for stuff treated specially by exporters."
  3729. (if (not org-highlight-latex-fragments-and-specials)
  3730. (org-set-local 'org-latex-and-specials-regexp nil)
  3731. (require 'org-exp)
  3732. (let*
  3733. ((matchers (plist-get org-format-latex-options :matchers))
  3734. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3735. org-latex-regexps)))
  3736. (options (org-combine-plists (org-default-export-plist)
  3737. (org-infile-export-plist)))
  3738. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3739. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3740. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3741. (org-export-html-expand (plist-get options :expand-quoted-html))
  3742. (org-export-with-special-strings (plist-get options :special-strings))
  3743. (re-sub
  3744. (cond
  3745. ((equal org-export-with-sub-superscripts '{})
  3746. (list org-match-substring-with-braces-regexp))
  3747. (org-export-with-sub-superscripts
  3748. (list org-match-substring-regexp))
  3749. (t nil)))
  3750. (re-latex
  3751. (if org-export-with-LaTeX-fragments
  3752. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3753. (re-macros
  3754. (if org-export-with-TeX-macros
  3755. (list (concat "\\\\"
  3756. (regexp-opt
  3757. (append (mapcar 'car org-html-entities)
  3758. (if (boundp 'org-latex-entities)
  3759. org-latex-entities nil))
  3760. 'words))) ; FIXME
  3761. ))
  3762. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3763. (re-special (if org-export-with-special-strings
  3764. (mapcar (lambda (x) (car x))
  3765. org-export-html-special-string-regexps)))
  3766. (re-rest
  3767. (delq nil
  3768. (list
  3769. (if org-export-html-expand "@<[^>\n]+>")
  3770. ))))
  3771. (org-set-local
  3772. 'org-latex-and-specials-regexp
  3773. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3774. re-rest) "\\|")))))
  3775. (defun org-do-latex-and-special-faces (limit)
  3776. "Run through the buffer and add overlays to links."
  3777. (when org-latex-and-specials-regexp
  3778. (let (rtn d)
  3779. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3780. limit t))
  3781. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3782. 'face))
  3783. '(org-code org-verbatim underline)))
  3784. (progn
  3785. (setq rtn t
  3786. d (cond ((member (char-after (1+ (match-beginning 0)))
  3787. '(?_ ?^)) 1)
  3788. (t 0)))
  3789. (font-lock-prepend-text-property
  3790. (+ d (match-beginning 0)) (match-end 0)
  3791. 'face 'org-latex-and-export-specials)
  3792. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3793. '(font-lock-multiline t)))))
  3794. rtn)))
  3795. (defun org-restart-font-lock ()
  3796. "Restart font-lock-mode, to force refontification."
  3797. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3798. (font-lock-mode -1)
  3799. (font-lock-mode 1)))
  3800. (defun org-all-targets (&optional radio)
  3801. "Return a list of all targets in this file.
  3802. With optional argument RADIO, only find radio targets."
  3803. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3804. rtn)
  3805. (save-excursion
  3806. (goto-char (point-min))
  3807. (while (re-search-forward re nil t)
  3808. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3809. rtn)))
  3810. (defun org-make-target-link-regexp (targets)
  3811. "Make regular expression matching all strings in TARGETS.
  3812. The regular expression finds the targets also if there is a line break
  3813. between words."
  3814. (and targets
  3815. (concat
  3816. "\\<\\("
  3817. (mapconcat
  3818. (lambda (x)
  3819. (while (string-match " +" x)
  3820. (setq x (replace-match "\\s-+" t t x)))
  3821. x)
  3822. targets
  3823. "\\|")
  3824. "\\)\\>")))
  3825. (defun org-activate-tags (limit)
  3826. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3827. (progn
  3828. (add-text-properties (match-beginning 1) (match-end 1)
  3829. (list 'mouse-face 'highlight
  3830. 'rear-nonsticky org-nonsticky-props
  3831. 'keymap org-mouse-map))
  3832. t)))
  3833. (defun org-outline-level ()
  3834. (save-excursion
  3835. (looking-at outline-regexp)
  3836. (if (match-beginning 1)
  3837. (+ (org-get-string-indentation (match-string 1)) 1000)
  3838. (1- (- (match-end 0) (match-beginning 0))))))
  3839. (defvar org-font-lock-keywords nil)
  3840. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3841. "Regular expression matching a property line.")
  3842. (defvar org-font-lock-hook nil
  3843. "Functions to be called for special font lock stuff.")
  3844. (defun org-font-lock-hook (limit)
  3845. (run-hook-with-args 'org-font-lock-hook limit))
  3846. (defun org-set-font-lock-defaults ()
  3847. (let* ((em org-fontify-emphasized-text)
  3848. (lk org-activate-links)
  3849. (org-font-lock-extra-keywords
  3850. (list
  3851. ;; Call the hook
  3852. '(org-font-lock-hook)
  3853. ;; Headlines
  3854. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3855. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3856. ;; Table lines
  3857. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3858. (1 'org-table t))
  3859. ;; Table internals
  3860. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3861. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3862. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3863. ;; Drawers
  3864. (list org-drawer-regexp '(0 'org-special-keyword t))
  3865. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3866. ;; Properties
  3867. (list org-property-re
  3868. '(1 'org-special-keyword t)
  3869. '(3 'org-property-value t))
  3870. (if org-format-transports-properties-p
  3871. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3872. ;; Links
  3873. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3874. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3875. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3876. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3877. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3878. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3879. (if (memq 'footnote lk) '(org-activate-footnote-links
  3880. (2 'org-footnote t)))
  3881. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3882. '(org-hide-wide-columns (0 nil append))
  3883. ;; TODO lines
  3884. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3885. '(1 (org-get-todo-face 1) t))
  3886. ;; DONE
  3887. (if org-fontify-done-headline
  3888. (list (concat "^[*]+ +\\<\\("
  3889. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3890. "\\)\\(.*\\)")
  3891. '(2 'org-headline-done t))
  3892. nil)
  3893. ;; Priorities
  3894. '(org-font-lock-add-priority-faces)
  3895. ;; Tags
  3896. '(org-font-lock-add-tag-faces)
  3897. ;; Special keywords
  3898. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3899. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3900. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3901. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3902. ;; Emphasis
  3903. (if em
  3904. (if (featurep 'xemacs)
  3905. '(org-do-emphasis-faces (0 nil append))
  3906. '(org-do-emphasis-faces)))
  3907. ;; Checkboxes
  3908. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3909. 2 'org-checkbox prepend)
  3910. (if org-provide-checkbox-statistics
  3911. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3912. (0 (org-get-checkbox-statistics-face) t)))
  3913. ;; Description list items
  3914. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3915. 2 'bold prepend)
  3916. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3917. '(1 'org-archived prepend))
  3918. ;; Specials
  3919. '(org-do-latex-and-special-faces)
  3920. ;; Code
  3921. '(org-activate-code (1 'org-code t))
  3922. ;; COMMENT
  3923. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3924. "\\|" org-quote-string "\\)\\>")
  3925. '(1 'org-special-keyword t))
  3926. '("^#.*" (0 'font-lock-comment-face t))
  3927. )))
  3928. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3929. ;; Now set the full font-lock-keywords
  3930. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3931. (org-set-local 'font-lock-defaults
  3932. '(org-font-lock-keywords t nil nil backward-paragraph))
  3933. (kill-local-variable 'font-lock-keywords) nil))
  3934. (defvar org-m nil)
  3935. (defvar org-l nil)
  3936. (defvar org-f nil)
  3937. (defun org-get-level-face (n)
  3938. "Get the right face for match N in font-lock matching of headlines."
  3939. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3940. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3941. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3942. (cond
  3943. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3944. ((eq n 2) org-f)
  3945. (t (if org-level-color-stars-only nil org-f))))
  3946. (defun org-get-todo-face (kwd)
  3947. "Get the right face for a TODO keyword KWD.
  3948. If KWD is a number, get the corresponding match group."
  3949. (if (numberp kwd) (setq kwd (match-string kwd)))
  3950. (or (cdr (assoc kwd org-todo-keyword-faces))
  3951. (and (member kwd org-done-keywords) 'org-done)
  3952. 'org-todo))
  3953. (defun org-font-lock-add-tag-faces (limit)
  3954. "Add the special tag faces."
  3955. (when (and org-tag-faces org-tags-special-faces-re)
  3956. (while (re-search-forward org-tags-special-faces-re limit t)
  3957. (add-text-properties (match-beginning 1) (match-end 1)
  3958. (list 'face (org-get-tag-face 1)
  3959. 'font-lock-fontified t))
  3960. (backward-char 1))))
  3961. (defun org-font-lock-add-priority-faces (limit)
  3962. "Add the special priority faces."
  3963. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3964. (add-text-properties
  3965. (match-beginning 0) (match-end 0)
  3966. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3967. org-priority-faces))
  3968. 'org-special-keyword)
  3969. 'font-lock-fontified t))))
  3970. (defun org-get-tag-face (kwd)
  3971. "Get the right face for a TODO keyword KWD.
  3972. If KWD is a number, get the corresponding match group."
  3973. (if (numberp kwd) (setq kwd (match-string kwd)))
  3974. (or (cdr (assoc kwd org-tag-faces))
  3975. 'org-tag))
  3976. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3977. "Remove fontification and activation overlays from links."
  3978. (font-lock-default-unfontify-region beg end)
  3979. (let* ((buffer-undo-list t)
  3980. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3981. (inhibit-modification-hooks t)
  3982. deactivate-mark buffer-file-name buffer-file-truename)
  3983. (remove-text-properties beg end
  3984. '(mouse-face t keymap t org-linked-text t
  3985. invisible t intangible t))))
  3986. ;;;; Visibility cycling, including org-goto and indirect buffer
  3987. ;;; Cycling
  3988. (defvar org-cycle-global-status nil)
  3989. (make-variable-buffer-local 'org-cycle-global-status)
  3990. (defvar org-cycle-subtree-status nil)
  3991. (make-variable-buffer-local 'org-cycle-subtree-status)
  3992. ;;;###autoload
  3993. (defvar org-inlinetask-min-level)
  3994. (defun org-cycle (&optional arg)
  3995. "Visibility cycling for Org-mode.
  3996. - When this function is called with a prefix argument, rotate the entire
  3997. buffer through 3 states (global cycling)
  3998. 1. OVERVIEW: Show only top-level headlines.
  3999. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4000. 3. SHOW ALL: Show everything.
  4001. When called with two C-u C-u prefixes, switch to the startup visibility,
  4002. determined by the variable `org-startup-folded', and by any VISIBILITY
  4003. properties in the buffer.
  4004. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4005. including drawers.
  4006. - When point is at the beginning of a headline, rotate the subtree started
  4007. by this line through 3 different states (local cycling)
  4008. 1. FOLDED: Only the main headline is shown.
  4009. 2. CHILDREN: The main headline and the direct children are shown.
  4010. From this state, you can move to one of the children
  4011. and zoom in further.
  4012. 3. SUBTREE: Show the entire subtree, including body text.
  4013. - When there is a numeric prefix, go up to a heading with level ARG, do
  4014. a `show-subtree' and return to the previous cursor position. If ARG
  4015. is negative, go up that many levels.
  4016. - When point is not at the beginning of a headline, execute the global
  4017. binding for TAB, which is re-indenting the line. See the option
  4018. `org-cycle-emulate-tab' for details.
  4019. - Special case: if point is at the beginning of the buffer and there is
  4020. no headline in line 1, this function will act as if called with prefix arg.
  4021. But only if also the variable `org-cycle-global-at-bob' is t."
  4022. (interactive "P")
  4023. (org-load-modules-maybe)
  4024. (let* ((limit-level
  4025. (or org-cycle-max-level
  4026. (and (boundp 'org-inlinetask-min-level)
  4027. org-inlinetask-min-level
  4028. (1- org-inlinetask-min-level))))
  4029. (nstars (and limit-level
  4030. (if org-odd-levels-only
  4031. (and limit-level (1- (* limit 2)))
  4032. limit-level)))
  4033. (outline-regexp
  4034. (cond
  4035. ((not (org-mode-p)) outline-regexp)
  4036. (org-cycle-include-plain-lists
  4037. (concat "\\(?:\\*"
  4038. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4039. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4040. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4041. (bob-special (and org-cycle-global-at-bob (bobp)
  4042. (not (looking-at outline-regexp))))
  4043. (org-cycle-hook
  4044. (if bob-special
  4045. (delq 'org-optimize-window-after-visibility-change
  4046. (copy-sequence org-cycle-hook))
  4047. org-cycle-hook))
  4048. (pos (point)))
  4049. (if (or bob-special (equal arg '(4)))
  4050. ;; special case: use global cycling
  4051. (setq arg t))
  4052. (cond
  4053. ((equal arg '(16))
  4054. (org-set-startup-visibility)
  4055. (message "Startup visibility, plus VISIBILITY properties"))
  4056. ((equal arg '(64))
  4057. (show-all)
  4058. (message "Entire buffer visible, including drawers"))
  4059. ((org-at-table-p 'any)
  4060. ;; Enter the table or move to the next field in the table
  4061. (or (org-table-recognize-table.el)
  4062. (progn
  4063. (if arg (org-table-edit-field t)
  4064. (org-table-justify-field-maybe)
  4065. (call-interactively 'org-table-next-field)))))
  4066. ((eq arg t) ;; Global cycling
  4067. (cond
  4068. ((and (eq last-command this-command)
  4069. (eq org-cycle-global-status 'overview))
  4070. ;; We just created the overview - now do table of contents
  4071. ;; This can be slow in very large buffers, so indicate action
  4072. (message "CONTENTS...")
  4073. (org-content)
  4074. (message "CONTENTS...done")
  4075. (setq org-cycle-global-status 'contents)
  4076. (run-hook-with-args 'org-cycle-hook 'contents))
  4077. ((and (eq last-command this-command)
  4078. (eq org-cycle-global-status 'contents))
  4079. ;; We just showed the table of contents - now show everything
  4080. (show-all)
  4081. (message "SHOW ALL")
  4082. (setq org-cycle-global-status 'all)
  4083. (run-hook-with-args 'org-cycle-hook 'all))
  4084. (t
  4085. ;; Default action: go to overview
  4086. (org-overview)
  4087. (message "OVERVIEW")
  4088. (setq org-cycle-global-status 'overview)
  4089. (run-hook-with-args 'org-cycle-hook 'overview))))
  4090. ((and org-drawers org-drawer-regexp
  4091. (save-excursion
  4092. (beginning-of-line 1)
  4093. (looking-at org-drawer-regexp)))
  4094. ;; Toggle block visibility
  4095. (org-flag-drawer
  4096. (not (get-char-property (match-end 0) 'invisible))))
  4097. ((integerp arg)
  4098. ;; Show-subtree, ARG levels up from here.
  4099. (save-excursion
  4100. (org-back-to-heading)
  4101. (outline-up-heading (if (< arg 0) (- arg)
  4102. (- (funcall outline-level) arg)))
  4103. (org-show-subtree)))
  4104. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4105. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4106. ;; At a heading: rotate between three different views
  4107. (org-back-to-heading)
  4108. (let ((goal-column 0) eoh eol eos)
  4109. ;; First, some boundaries
  4110. (save-excursion
  4111. (org-back-to-heading)
  4112. (save-excursion
  4113. (beginning-of-line 2)
  4114. (while (and (not (eobp)) ;; this is like `next-line'
  4115. (get-char-property (1- (point)) 'invisible))
  4116. (beginning-of-line 2)) (setq eol (point)))
  4117. (outline-end-of-heading) (setq eoh (point))
  4118. (org-end-of-subtree t)
  4119. (unless (eobp)
  4120. (skip-chars-forward " \t\n")
  4121. (beginning-of-line 1) ; in case this is an item
  4122. )
  4123. (setq eos (1- (point))))
  4124. ;; Find out what to do next and set `this-command'
  4125. (cond
  4126. ((= eos eoh)
  4127. ;; Nothing is hidden behind this heading
  4128. (message "EMPTY ENTRY")
  4129. (setq org-cycle-subtree-status nil)
  4130. (save-excursion
  4131. (goto-char eos)
  4132. (outline-next-heading)
  4133. (if (org-invisible-p) (org-flag-heading nil))))
  4134. ((or (>= eol eos)
  4135. (not (string-match "\\S-" (buffer-substring eol eos))))
  4136. ;; Entire subtree is hidden in one line: open it
  4137. (org-show-entry)
  4138. (show-children)
  4139. (message "CHILDREN")
  4140. (save-excursion
  4141. (goto-char eos)
  4142. (outline-next-heading)
  4143. (if (org-invisible-p) (org-flag-heading nil)))
  4144. (setq org-cycle-subtree-status 'children)
  4145. (run-hook-with-args 'org-cycle-hook 'children))
  4146. ((and (eq last-command this-command)
  4147. (eq org-cycle-subtree-status 'children))
  4148. ;; We just showed the children, now show everything.
  4149. (org-show-subtree)
  4150. (message "SUBTREE")
  4151. (setq org-cycle-subtree-status 'subtree)
  4152. (run-hook-with-args 'org-cycle-hook 'subtree))
  4153. (t
  4154. ;; Default action: hide the subtree.
  4155. (hide-subtree)
  4156. (message "FOLDED")
  4157. (setq org-cycle-subtree-status 'folded)
  4158. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4159. ;; TAB emulation and template completion
  4160. (buffer-read-only (org-back-to-heading))
  4161. ((org-try-structure-completion))
  4162. ((org-try-cdlatex-tab))
  4163. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4164. (or (not (bolp))
  4165. (not (looking-at outline-regexp))))
  4166. (call-interactively (global-key-binding "\t")))
  4167. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4168. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4169. (or (and (eq org-cycle-emulate-tab 'white)
  4170. (= (match-end 0) (point-at-eol)))
  4171. (and (eq org-cycle-emulate-tab 'whitestart)
  4172. (>= (match-end 0) pos))))
  4173. t
  4174. (eq org-cycle-emulate-tab t))
  4175. (call-interactively (global-key-binding "\t")))
  4176. (t (save-excursion
  4177. (org-back-to-heading)
  4178. (org-cycle))))))
  4179. ;;;###autoload
  4180. (defun org-global-cycle (&optional arg)
  4181. "Cycle the global visibility. For details see `org-cycle'.
  4182. With C-u prefix arg, switch to startup visibility.
  4183. With a numeric prefix, show all headlines up to that level."
  4184. (interactive "P")
  4185. (let ((org-cycle-include-plain-lists
  4186. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4187. (cond
  4188. ((integerp arg)
  4189. (show-all)
  4190. (hide-sublevels arg)
  4191. (setq org-cycle-global-status 'contents))
  4192. ((equal arg '(4))
  4193. (org-set-startup-visibility)
  4194. (message "Startup visibility, plus VISIBILITY properties."))
  4195. (t
  4196. (org-cycle '(4))))))
  4197. (defun org-set-startup-visibility ()
  4198. "Set the visibility required by startup options and properties."
  4199. (cond
  4200. ((eq org-startup-folded t)
  4201. (org-cycle '(4)))
  4202. ((eq org-startup-folded 'content)
  4203. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4204. (org-cycle '(4)) (org-cycle '(4)))))
  4205. (org-set-visibility-according-to-property 'no-cleanup)
  4206. (org-cycle-hide-archived-subtrees 'all)
  4207. (org-cycle-hide-drawers 'all)
  4208. (org-cycle-show-empty-lines 'all))
  4209. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4210. "Switch subtree visibilities according to :VISIBILITY: property."
  4211. (interactive)
  4212. (let (org-show-entry-below state)
  4213. (save-excursion
  4214. (goto-char (point-min))
  4215. (while (re-search-forward
  4216. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4217. nil t)
  4218. (setq state (match-string 1))
  4219. (save-excursion
  4220. (org-back-to-heading t)
  4221. (hide-subtree)
  4222. (org-reveal)
  4223. (cond
  4224. ((equal state '("fold" "folded"))
  4225. (hide-subtree))
  4226. ((equal state "children")
  4227. (org-show-hidden-entry)
  4228. (show-children))
  4229. ((equal state "content")
  4230. (save-excursion
  4231. (save-restriction
  4232. (org-narrow-to-subtree)
  4233. (org-content))))
  4234. ((member state '("all" "showall"))
  4235. (show-subtree)))))
  4236. (unless no-cleanup
  4237. (org-cycle-hide-archived-subtrees 'all)
  4238. (org-cycle-hide-drawers 'all)
  4239. (org-cycle-show-empty-lines 'all)))))
  4240. (defun org-overview ()
  4241. "Switch to overview mode, showing only top-level headlines.
  4242. Really, this shows all headlines with level equal or greater than the level
  4243. of the first headline in the buffer. This is important, because if the
  4244. first headline is not level one, then (hide-sublevels 1) gives confusing
  4245. results."
  4246. (interactive)
  4247. (let ((level (save-excursion
  4248. (goto-char (point-min))
  4249. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4250. (progn
  4251. (goto-char (match-beginning 0))
  4252. (funcall outline-level))))))
  4253. (and level (hide-sublevels level))))
  4254. (defun org-content (&optional arg)
  4255. "Show all headlines in the buffer, like a table of contents.
  4256. With numerical argument N, show content up to level N."
  4257. (interactive "P")
  4258. (save-excursion
  4259. ;; Visit all headings and show their offspring
  4260. (and (integerp arg) (org-overview))
  4261. (goto-char (point-max))
  4262. (catch 'exit
  4263. (while (and (progn (condition-case nil
  4264. (outline-previous-visible-heading 1)
  4265. (error (goto-char (point-min))))
  4266. t)
  4267. (looking-at outline-regexp))
  4268. (if (integerp arg)
  4269. (show-children (1- arg))
  4270. (show-branches))
  4271. (if (bobp) (throw 'exit nil))))))
  4272. (defun org-optimize-window-after-visibility-change (state)
  4273. "Adjust the window after a change in outline visibility.
  4274. This function is the default value of the hook `org-cycle-hook'."
  4275. (when (get-buffer-window (current-buffer))
  4276. (cond
  4277. ((eq state 'content) nil)
  4278. ((eq state 'all) nil)
  4279. ((eq state 'folded) nil)
  4280. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4281. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4282. (defun org-compact-display-after-subtree-move ()
  4283. "Show a compacter version of the tree of the entry's parent."
  4284. (save-excursion
  4285. (if (org-up-heading-safe)
  4286. (progn
  4287. (hide-subtree)
  4288. (show-entry)
  4289. (show-children)
  4290. (org-cycle-show-empty-lines 'children)
  4291. (org-cycle-hide-drawers 'children))
  4292. (org-overview))))
  4293. (defun org-cycle-show-empty-lines (state)
  4294. "Show empty lines above all visible headlines.
  4295. The region to be covered depends on STATE when called through
  4296. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4297. entire buffer covered. Note that an empty line is only shown if there
  4298. are at least `org-cycle-separator-lines' empty lines before the headline."
  4299. (when (> org-cycle-separator-lines 0)
  4300. (save-excursion
  4301. (let* ((n org-cycle-separator-lines)
  4302. (re (cond
  4303. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4304. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4305. (t (let ((ns (number-to-string (- n 2))))
  4306. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4307. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4308. beg end)
  4309. (cond
  4310. ((memq state '(overview contents t))
  4311. (setq beg (point-min) end (point-max)))
  4312. ((memq state '(children folded))
  4313. (setq beg (point) end (progn (org-end-of-subtree t t)
  4314. (beginning-of-line 2)
  4315. (point)))))
  4316. (when beg
  4317. (goto-char beg)
  4318. (while (re-search-forward re end t)
  4319. (if (not (get-char-property (match-end 1) 'invisible))
  4320. (outline-flag-region
  4321. (match-beginning 1) (match-end 1) nil)))))))
  4322. ;; Never hide empty lines at the end of the file.
  4323. (save-excursion
  4324. (goto-char (point-max))
  4325. (outline-previous-heading)
  4326. (outline-end-of-heading)
  4327. (if (and (looking-at "[ \t\n]+")
  4328. (= (match-end 0) (point-max)))
  4329. (outline-flag-region (point) (match-end 0) nil))))
  4330. (defun org-show-empty-lines-in-parent ()
  4331. "Move to the parent and re-show empty lines before visible headlines."
  4332. (save-excursion
  4333. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4334. (org-cycle-show-empty-lines context))))
  4335. (defun org-cycle-hide-drawers (state)
  4336. "Re-hide all drawers after a visibility state change."
  4337. (when (and (org-mode-p)
  4338. (not (memq state '(overview folded))))
  4339. (save-excursion
  4340. (let* ((globalp (memq state '(contents all)))
  4341. (beg (if globalp (point-min) (point)))
  4342. (end (if globalp (point-max) (org-end-of-subtree t))))
  4343. (goto-char beg)
  4344. (while (re-search-forward org-drawer-regexp end t)
  4345. (org-flag-drawer t))))))
  4346. (defun org-flag-drawer (flag)
  4347. (save-excursion
  4348. (beginning-of-line 1)
  4349. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4350. (let ((b (match-end 0))
  4351. (outline-regexp org-outline-regexp))
  4352. (if (re-search-forward
  4353. "^[ \t]*:END:"
  4354. (save-excursion (outline-next-heading) (point)) t)
  4355. (outline-flag-region b (point-at-eol) flag)
  4356. (error ":END: line missing"))))))
  4357. (defun org-subtree-end-visible-p ()
  4358. "Is the end of the current subtree visible?"
  4359. (pos-visible-in-window-p
  4360. (save-excursion (org-end-of-subtree t) (point))))
  4361. (defun org-first-headline-recenter (&optional N)
  4362. "Move cursor to the first headline and recenter the headline.
  4363. Optional argument N means, put the headline into the Nth line of the window."
  4364. (goto-char (point-min))
  4365. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4366. (beginning-of-line)
  4367. (recenter (prefix-numeric-value N))))
  4368. ;;; Org-goto
  4369. (defvar org-goto-window-configuration nil)
  4370. (defvar org-goto-marker nil)
  4371. (defvar org-goto-map
  4372. (let ((map (make-sparse-keymap)))
  4373. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4374. (while (setq cmd (pop cmds))
  4375. (substitute-key-definition cmd cmd map global-map)))
  4376. (suppress-keymap map)
  4377. (org-defkey map "\C-m" 'org-goto-ret)
  4378. (org-defkey map [(return)] 'org-goto-ret)
  4379. (org-defkey map [(left)] 'org-goto-left)
  4380. (org-defkey map [(right)] 'org-goto-right)
  4381. (org-defkey map [(control ?g)] 'org-goto-quit)
  4382. (org-defkey map "\C-i" 'org-cycle)
  4383. (org-defkey map [(tab)] 'org-cycle)
  4384. (org-defkey map [(down)] 'outline-next-visible-heading)
  4385. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4386. (if org-goto-auto-isearch
  4387. (if (fboundp 'define-key-after)
  4388. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4389. nil)
  4390. (org-defkey map "q" 'org-goto-quit)
  4391. (org-defkey map "n" 'outline-next-visible-heading)
  4392. (org-defkey map "p" 'outline-previous-visible-heading)
  4393. (org-defkey map "f" 'outline-forward-same-level)
  4394. (org-defkey map "b" 'outline-backward-same-level)
  4395. (org-defkey map "u" 'outline-up-heading))
  4396. (org-defkey map "/" 'org-occur)
  4397. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4398. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4399. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4400. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4401. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4402. map))
  4403. (defconst org-goto-help
  4404. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4405. RET=jump to location [Q]uit and return to previous location
  4406. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4407. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4408. ;; FIXME: Docstring doe not mention both interfaces
  4409. (defun org-goto (&optional alternative-interface)
  4410. "Look up a different location in the current file, keeping current visibility.
  4411. When you want look-up or go to a different location in a document, the
  4412. fastest way is often to fold the entire buffer and then dive into the tree.
  4413. This method has the disadvantage, that the previous location will be folded,
  4414. which may not be what you want.
  4415. This command works around this by showing a copy of the current buffer
  4416. in an indirect buffer, in overview mode. You can dive into the tree in
  4417. that copy, use org-occur and incremental search to find a location.
  4418. When pressing RET or `Q', the command returns to the original buffer in
  4419. which the visibility is still unchanged. After RET is will also jump to
  4420. the location selected in the indirect buffer and expose the
  4421. the headline hierarchy above."
  4422. (interactive "P")
  4423. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4424. (org-refile-use-outline-path t)
  4425. (interface
  4426. (if (not alternative-interface)
  4427. org-goto-interface
  4428. (if (eq org-goto-interface 'outline)
  4429. 'outline-path-completion
  4430. 'outline)))
  4431. (org-goto-start-pos (point))
  4432. (selected-point
  4433. (if (eq interface 'outline)
  4434. (car (org-get-location (current-buffer) org-goto-help))
  4435. (nth 3 (org-refile-get-location "Goto: ")))))
  4436. (if selected-point
  4437. (progn
  4438. (org-mark-ring-push org-goto-start-pos)
  4439. (goto-char selected-point)
  4440. (if (or (org-invisible-p) (org-invisible-p2))
  4441. (org-show-context 'org-goto)))
  4442. (message "Quit"))))
  4443. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4444. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4445. (defvar org-goto-local-auto-isearch-map) ; defined below
  4446. (defun org-get-location (buf help)
  4447. "Let the user select a location in the Org-mode buffer BUF.
  4448. This function uses a recursive edit. It returns the selected position
  4449. or nil."
  4450. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4451. (isearch-hide-immediately nil)
  4452. (isearch-search-fun-function
  4453. (lambda () 'org-goto-local-search-headings))
  4454. (org-goto-selected-point org-goto-exit-command))
  4455. (save-excursion
  4456. (save-window-excursion
  4457. (delete-other-windows)
  4458. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4459. (switch-to-buffer
  4460. (condition-case nil
  4461. (make-indirect-buffer (current-buffer) "*org-goto*")
  4462. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4463. (with-output-to-temp-buffer "*Help*"
  4464. (princ help))
  4465. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4466. (setq buffer-read-only nil)
  4467. (let ((org-startup-truncated t)
  4468. (org-startup-folded nil)
  4469. (org-startup-align-all-tables nil))
  4470. (org-mode)
  4471. (org-overview))
  4472. (setq buffer-read-only t)
  4473. (if (and (boundp 'org-goto-start-pos)
  4474. (integer-or-marker-p org-goto-start-pos))
  4475. (let ((org-show-hierarchy-above t)
  4476. (org-show-siblings t)
  4477. (org-show-following-heading t))
  4478. (goto-char org-goto-start-pos)
  4479. (and (org-invisible-p) (org-show-context)))
  4480. (goto-char (point-min)))
  4481. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4482. (message "Select location and press RET")
  4483. (use-local-map org-goto-map)
  4484. (recursive-edit)
  4485. ))
  4486. (kill-buffer "*org-goto*")
  4487. (cons org-goto-selected-point org-goto-exit-command)))
  4488. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4489. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4490. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4491. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4492. (defun org-goto-local-search-headings (string bound noerror)
  4493. "Search and make sure that any matches are in headlines."
  4494. (catch 'return
  4495. (while (if isearch-forward
  4496. (search-forward string bound noerror)
  4497. (search-backward string bound noerror))
  4498. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4499. (and (member :headline context)
  4500. (not (member :tags context))))
  4501. (throw 'return (point))))))
  4502. (defun org-goto-local-auto-isearch ()
  4503. "Start isearch."
  4504. (interactive)
  4505. (goto-char (point-min))
  4506. (let ((keys (this-command-keys)))
  4507. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4508. (isearch-mode t)
  4509. (isearch-process-search-char (string-to-char keys)))))
  4510. (defun org-goto-ret (&optional arg)
  4511. "Finish `org-goto' by going to the new location."
  4512. (interactive "P")
  4513. (setq org-goto-selected-point (point)
  4514. org-goto-exit-command 'return)
  4515. (throw 'exit nil))
  4516. (defun org-goto-left ()
  4517. "Finish `org-goto' by going to the new location."
  4518. (interactive)
  4519. (if (org-on-heading-p)
  4520. (progn
  4521. (beginning-of-line 1)
  4522. (setq org-goto-selected-point (point)
  4523. org-goto-exit-command 'left)
  4524. (throw 'exit nil))
  4525. (error "Not on a heading")))
  4526. (defun org-goto-right ()
  4527. "Finish `org-goto' by going to the new location."
  4528. (interactive)
  4529. (if (org-on-heading-p)
  4530. (progn
  4531. (setq org-goto-selected-point (point)
  4532. org-goto-exit-command 'right)
  4533. (throw 'exit nil))
  4534. (error "Not on a heading")))
  4535. (defun org-goto-quit ()
  4536. "Finish `org-goto' without cursor motion."
  4537. (interactive)
  4538. (setq org-goto-selected-point nil)
  4539. (setq org-goto-exit-command 'quit)
  4540. (throw 'exit nil))
  4541. ;;; Indirect buffer display of subtrees
  4542. (defvar org-indirect-dedicated-frame nil
  4543. "This is the frame being used for indirect tree display.")
  4544. (defvar org-last-indirect-buffer nil)
  4545. (defun org-tree-to-indirect-buffer (&optional arg)
  4546. "Create indirect buffer and narrow it to current subtree.
  4547. With numerical prefix ARG, go up to this level and then take that tree.
  4548. If ARG is negative, go up that many levels.
  4549. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4550. indirect buffer previously made with this command, to avoid proliferation of
  4551. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4552. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4553. is kept so that you can work with several indirect buffers at the same time.
  4554. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4555. requests that a new frame be made for the new buffer, so that the dedicated
  4556. frame is not changed."
  4557. (interactive "P")
  4558. (let ((cbuf (current-buffer))
  4559. (cwin (selected-window))
  4560. (pos (point))
  4561. beg end level heading ibuf)
  4562. (save-excursion
  4563. (org-back-to-heading t)
  4564. (when (numberp arg)
  4565. (setq level (org-outline-level))
  4566. (if (< arg 0) (setq arg (+ level arg)))
  4567. (while (> (setq level (org-outline-level)) arg)
  4568. (outline-up-heading 1 t)))
  4569. (setq beg (point)
  4570. heading (org-get-heading))
  4571. (org-end-of-subtree t) (setq end (point)))
  4572. (if (and (buffer-live-p org-last-indirect-buffer)
  4573. (not (eq org-indirect-buffer-display 'new-frame))
  4574. (not arg))
  4575. (kill-buffer org-last-indirect-buffer))
  4576. (setq ibuf (org-get-indirect-buffer cbuf)
  4577. org-last-indirect-buffer ibuf)
  4578. (cond
  4579. ((or (eq org-indirect-buffer-display 'new-frame)
  4580. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4581. (select-frame (make-frame))
  4582. (delete-other-windows)
  4583. (switch-to-buffer ibuf)
  4584. (org-set-frame-title heading))
  4585. ((eq org-indirect-buffer-display 'dedicated-frame)
  4586. (raise-frame
  4587. (select-frame (or (and org-indirect-dedicated-frame
  4588. (frame-live-p org-indirect-dedicated-frame)
  4589. org-indirect-dedicated-frame)
  4590. (setq org-indirect-dedicated-frame (make-frame)))))
  4591. (delete-other-windows)
  4592. (switch-to-buffer ibuf)
  4593. (org-set-frame-title (concat "Indirect: " heading)))
  4594. ((eq org-indirect-buffer-display 'current-window)
  4595. (switch-to-buffer ibuf))
  4596. ((eq org-indirect-buffer-display 'other-window)
  4597. (pop-to-buffer ibuf))
  4598. (t (error "Invalid value.")))
  4599. (if (featurep 'xemacs)
  4600. (save-excursion (org-mode) (turn-on-font-lock)))
  4601. (narrow-to-region beg end)
  4602. (show-all)
  4603. (goto-char pos)
  4604. (and (window-live-p cwin) (select-window cwin))))
  4605. (defun org-get-indirect-buffer (&optional buffer)
  4606. (setq buffer (or buffer (current-buffer)))
  4607. (let ((n 1) (base (buffer-name buffer)) bname)
  4608. (while (buffer-live-p
  4609. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4610. (setq n (1+ n)))
  4611. (condition-case nil
  4612. (make-indirect-buffer buffer bname 'clone)
  4613. (error (make-indirect-buffer buffer bname)))))
  4614. (defun org-set-frame-title (title)
  4615. "Set the title of the current frame to the string TITLE."
  4616. ;; FIXME: how to name a single frame in XEmacs???
  4617. (unless (featurep 'xemacs)
  4618. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4619. ;;;; Structure editing
  4620. ;;; Inserting headlines
  4621. (defun org-previous-line-empty-p ()
  4622. (save-excursion
  4623. (and (not (bobp))
  4624. (or (beginning-of-line 0) t)
  4625. (save-match-data
  4626. (looking-at "[ \t]*$")))))
  4627. (defun org-insert-heading (&optional force-heading)
  4628. "Insert a new heading or item with same depth at point.
  4629. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4630. If point is at the beginning of a headline, insert a sibling before the
  4631. current headline. If point is not at the beginning, do not split the line,
  4632. but create the new headline after the current line."
  4633. (interactive "P")
  4634. (if (= (buffer-size) 0)
  4635. (insert "\n* ")
  4636. (when (or force-heading (not (org-insert-item)))
  4637. (let* ((empty-line-p nil)
  4638. (head (save-excursion
  4639. (condition-case nil
  4640. (progn
  4641. (org-back-to-heading)
  4642. (setq empty-line-p (org-previous-line-empty-p))
  4643. (match-string 0))
  4644. (error "*"))))
  4645. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4646. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4647. pos hide-previous previous-pos)
  4648. (cond
  4649. ((and (org-on-heading-p) (bolp)
  4650. (or (bobp)
  4651. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4652. ;; insert before the current line
  4653. (open-line (if blank 2 1)))
  4654. ((and (bolp)
  4655. (or (bobp)
  4656. (save-excursion
  4657. (backward-char 1) (not (org-invisible-p)))))
  4658. ;; insert right here
  4659. nil)
  4660. (t
  4661. ;; somewhere in the line
  4662. (save-excursion
  4663. (setq previous-pos (point-at-bol))
  4664. (end-of-line)
  4665. (setq hide-previous (org-invisible-p)))
  4666. (and org-insert-heading-respect-content (org-show-subtree))
  4667. (let ((split
  4668. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4669. (save-excursion
  4670. (let ((p (point)))
  4671. (goto-char (point-at-bol))
  4672. (and (looking-at org-complex-heading-regexp)
  4673. (> p (match-beginning 4)))))))
  4674. tags pos)
  4675. (cond
  4676. (org-insert-heading-respect-content
  4677. (org-end-of-subtree nil t)
  4678. (or (bolp) (newline))
  4679. (or (org-previous-line-empty-p)
  4680. (and blank (newline)))
  4681. (open-line 1))
  4682. ((org-on-heading-p)
  4683. (when hide-previous
  4684. (show-children)
  4685. (org-show-entry))
  4686. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4687. (setq tags (and (match-end 2) (match-string 2)))
  4688. (and (match-end 1)
  4689. (delete-region (match-beginning 1) (match-end 1)))
  4690. (setq pos (point-at-bol))
  4691. (or split (end-of-line 1))
  4692. (delete-horizontal-space)
  4693. (newline (if blank 2 1))
  4694. (when tags
  4695. (save-excursion
  4696. (goto-char pos)
  4697. (end-of-line 1)
  4698. (insert " " tags)
  4699. (org-set-tags nil 'align))))
  4700. (t
  4701. (or split (end-of-line 1))
  4702. (newline (if blank 2 1)))))))
  4703. (insert head) (just-one-space)
  4704. (setq pos (point))
  4705. (end-of-line 1)
  4706. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4707. (when (and org-insert-heading-respect-content hide-previous)
  4708. (save-excursion
  4709. (goto-char previous-pos)
  4710. (hide-subtree)))
  4711. (run-hooks 'org-insert-heading-hook)))))
  4712. (defun org-get-heading (&optional no-tags)
  4713. "Return the heading of the current entry, without the stars."
  4714. (save-excursion
  4715. (org-back-to-heading t)
  4716. (if (looking-at
  4717. (if no-tags
  4718. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4719. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4720. (match-string 1) "")))
  4721. (defun org-heading-components ()
  4722. "Return the components of the current heading.
  4723. This is a list with the following elements:
  4724. - the level as an integer
  4725. - the reduced level, different if `org-odd-levels-only' is set.
  4726. - the TODO keyword, or nil
  4727. - the priority character, like ?A, or nil if no priority is given
  4728. - the headline text itself, or the tags string if no headline text
  4729. - the tags string, or nil."
  4730. (save-excursion
  4731. (org-back-to-heading t)
  4732. (if (looking-at org-complex-heading-regexp)
  4733. (list (length (match-string 1))
  4734. (org-reduced-level (length (match-string 1)))
  4735. (org-match-string-no-properties 2)
  4736. (and (match-end 3) (aref (match-string 3) 2))
  4737. (org-match-string-no-properties 4)
  4738. (org-match-string-no-properties 5)))))
  4739. (defun org-insert-heading-after-current ()
  4740. "Insert a new heading with same level as current, after current subtree."
  4741. (interactive)
  4742. (org-back-to-heading)
  4743. (org-insert-heading)
  4744. (org-move-subtree-down)
  4745. (end-of-line 1))
  4746. (defun org-insert-heading-respect-content ()
  4747. (interactive)
  4748. (let ((org-insert-heading-respect-content t))
  4749. (org-insert-heading t)))
  4750. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4751. (interactive "P")
  4752. (let ((org-insert-heading-respect-content t))
  4753. (org-insert-todo-heading force-state t)))
  4754. (defun org-insert-todo-heading (arg &optional force-heading)
  4755. "Insert a new heading with the same level and TODO state as current heading.
  4756. If the heading has no TODO state, or if the state is DONE, use the first
  4757. state (TODO by default). Also with prefix arg, force first state."
  4758. (interactive "P")
  4759. (when (or force-heading (not (org-insert-item 'checkbox)))
  4760. (org-insert-heading force-heading)
  4761. (save-excursion
  4762. (org-back-to-heading)
  4763. (outline-previous-heading)
  4764. (looking-at org-todo-line-regexp))
  4765. (let*
  4766. ((new-mark-x
  4767. (if (or arg
  4768. (not (match-beginning 2))
  4769. (member (match-string 2) org-done-keywords))
  4770. (car org-todo-keywords-1)
  4771. (match-string 2)))
  4772. (new-mark
  4773. (or
  4774. (run-hook-with-args-until-success
  4775. 'org-todo-get-default-hook new-mark-x nil)
  4776. new-mark-x)))
  4777. (beginning-of-line 1)
  4778. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4779. (insert new-mark " ")))
  4780. (when org-provide-todo-statistics
  4781. (org-update-parent-todo-statistics))))
  4782. (defun org-insert-subheading (arg)
  4783. "Insert a new subheading and demote it.
  4784. Works for outline headings and for plain lists alike."
  4785. (interactive "P")
  4786. (org-insert-heading arg)
  4787. (cond
  4788. ((org-on-heading-p) (org-do-demote))
  4789. ((org-at-item-p) (org-indent-item 1))))
  4790. (defun org-insert-todo-subheading (arg)
  4791. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4792. Works for outline headings and for plain lists alike."
  4793. (interactive "P")
  4794. (org-insert-todo-heading arg)
  4795. (cond
  4796. ((org-on-heading-p) (org-do-demote))
  4797. ((org-at-item-p) (org-indent-item 1))))
  4798. ;;; Promotion and Demotion
  4799. (defun org-promote-subtree ()
  4800. "Promote the entire subtree.
  4801. See also `org-promote'."
  4802. (interactive)
  4803. (save-excursion
  4804. (org-map-tree 'org-promote))
  4805. (org-fix-position-after-promote))
  4806. (defun org-demote-subtree ()
  4807. "Demote the entire subtree. See `org-demote'.
  4808. See also `org-promote'."
  4809. (interactive)
  4810. (save-excursion
  4811. (org-map-tree 'org-demote))
  4812. (org-fix-position-after-promote))
  4813. (defun org-do-promote ()
  4814. "Promote the current heading higher up the tree.
  4815. If the region is active in `transient-mark-mode', promote all headings
  4816. in the region."
  4817. (interactive)
  4818. (save-excursion
  4819. (if (org-region-active-p)
  4820. (org-map-region 'org-promote (region-beginning) (region-end))
  4821. (org-promote)))
  4822. (org-fix-position-after-promote))
  4823. (defun org-do-demote ()
  4824. "Demote the current heading lower down the tree.
  4825. If the region is active in `transient-mark-mode', demote all headings
  4826. in the region."
  4827. (interactive)
  4828. (save-excursion
  4829. (if (org-region-active-p)
  4830. (org-map-region 'org-demote (region-beginning) (region-end))
  4831. (org-demote)))
  4832. (org-fix-position-after-promote))
  4833. (defun org-fix-position-after-promote ()
  4834. "Make sure that after pro/demotion cursor position is right."
  4835. (let ((pos (point)))
  4836. (when (save-excursion
  4837. (beginning-of-line 1)
  4838. (looking-at org-todo-line-regexp)
  4839. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4840. (cond ((eobp) (insert " "))
  4841. ((eolp) (insert " "))
  4842. ((equal (char-after) ?\ ) (forward-char 1))))))
  4843. (defun org-reduced-level (l)
  4844. "Compute the effective level of a heading.
  4845. This takes into account the setting of `org-odd-levels-only'."
  4846. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4847. (defun org-get-valid-level (level &optional change)
  4848. "Rectify a level change under the influence of `org-odd-levels-only'
  4849. LEVEL is a current level, CHANGE is by how much the level should be
  4850. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4851. even level numbers will become the next higher odd number."
  4852. (if org-odd-levels-only
  4853. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4854. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4855. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4856. (max 1 (+ level (or change 0)))))
  4857. (if (boundp 'define-obsolete-function-alias)
  4858. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4859. (define-obsolete-function-alias 'org-get-legal-level
  4860. 'org-get-valid-level)
  4861. (define-obsolete-function-alias 'org-get-legal-level
  4862. 'org-get-valid-level "23.1")))
  4863. (defun org-promote ()
  4864. "Promote the current heading higher up the tree.
  4865. If the region is active in `transient-mark-mode', promote all headings
  4866. in the region."
  4867. (org-back-to-heading t)
  4868. (let* ((level (save-match-data (funcall outline-level)))
  4869. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4870. (diff (abs (- level (length up-head) -1))))
  4871. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4872. (replace-match up-head nil t)
  4873. ;; Fixup tag positioning
  4874. (and org-auto-align-tags (org-set-tags nil t))
  4875. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4876. (defun org-demote ()
  4877. "Demote the current heading lower down the tree.
  4878. If the region is active in `transient-mark-mode', demote all headings
  4879. in the region."
  4880. (org-back-to-heading t)
  4881. (let* ((level (save-match-data (funcall outline-level)))
  4882. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4883. (diff (abs (- level (length down-head) -1))))
  4884. (replace-match down-head nil t)
  4885. ;; Fixup tag positioning
  4886. (and org-auto-align-tags (org-set-tags nil t))
  4887. (if org-adapt-indentation (org-fixup-indentation diff))))
  4888. (defun org-map-tree (fun)
  4889. "Call FUN for every heading underneath the current one."
  4890. (org-back-to-heading)
  4891. (let ((level (funcall outline-level)))
  4892. (save-excursion
  4893. (funcall fun)
  4894. (while (and (progn
  4895. (outline-next-heading)
  4896. (> (funcall outline-level) level))
  4897. (not (eobp)))
  4898. (funcall fun)))))
  4899. (defun org-map-region (fun beg end)
  4900. "Call FUN for every heading between BEG and END."
  4901. (let ((org-ignore-region t))
  4902. (save-excursion
  4903. (setq end (copy-marker end))
  4904. (goto-char beg)
  4905. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4906. (< (point) end))
  4907. (funcall fun))
  4908. (while (and (progn
  4909. (outline-next-heading)
  4910. (< (point) end))
  4911. (not (eobp)))
  4912. (funcall fun)))))
  4913. (defun org-fixup-indentation (diff)
  4914. "Change the indentation in the current entry by DIFF
  4915. However, if any line in the current entry has no indentation, or if it
  4916. would end up with no indentation after the change, nothing at all is done."
  4917. (save-excursion
  4918. (let ((end (save-excursion (outline-next-heading)
  4919. (point-marker)))
  4920. (prohibit (if (> diff 0)
  4921. "^\\S-"
  4922. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4923. col)
  4924. (unless (save-excursion (end-of-line 1)
  4925. (re-search-forward prohibit end t))
  4926. (while (and (< (point) end)
  4927. (re-search-forward "^[ \t]+" end t))
  4928. (goto-char (match-end 0))
  4929. (setq col (current-column))
  4930. (if (< diff 0) (replace-match ""))
  4931. (org-indent-to-column (+ diff col))))
  4932. (move-marker end nil))))
  4933. (defun org-convert-to-odd-levels ()
  4934. "Convert an org-mode file with all levels allowed to one with odd levels.
  4935. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4936. level 5 etc."
  4937. (interactive)
  4938. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4939. (let ((org-odd-levels-only nil) n)
  4940. (save-excursion
  4941. (goto-char (point-min))
  4942. (while (re-search-forward "^\\*\\*+ " nil t)
  4943. (setq n (- (length (match-string 0)) 2))
  4944. (while (>= (setq n (1- n)) 0)
  4945. (org-demote))
  4946. (end-of-line 1))))))
  4947. (defun org-convert-to-oddeven-levels ()
  4948. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4949. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4950. section with an even level, conversion would destroy the structure of the file. An error
  4951. is signaled in this case."
  4952. (interactive)
  4953. (goto-char (point-min))
  4954. ;; First check if there are no even levels
  4955. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4956. (org-show-context t)
  4957. (error "Not all levels are odd in this file. Conversion not possible."))
  4958. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4959. (let ((org-odd-levels-only nil) n)
  4960. (save-excursion
  4961. (goto-char (point-min))
  4962. (while (re-search-forward "^\\*\\*+ " nil t)
  4963. (setq n (/ (1- (length (match-string 0))) 2))
  4964. (while (>= (setq n (1- n)) 0)
  4965. (org-promote))
  4966. (end-of-line 1))))))
  4967. (defun org-tr-level (n)
  4968. "Make N odd if required."
  4969. (if org-odd-levels-only (1+ (/ n 2)) n))
  4970. ;;; Vertical tree motion, cutting and pasting of subtrees
  4971. (defun org-move-subtree-up (&optional arg)
  4972. "Move the current subtree up past ARG headlines of the same level."
  4973. (interactive "p")
  4974. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4975. (defun org-move-subtree-down (&optional arg)
  4976. "Move the current subtree down past ARG headlines of the same level."
  4977. (interactive "p")
  4978. (setq arg (prefix-numeric-value arg))
  4979. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4980. 'outline-get-last-sibling))
  4981. (ins-point (make-marker))
  4982. (cnt (abs arg))
  4983. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4984. ;; Select the tree
  4985. (org-back-to-heading)
  4986. (setq beg0 (point))
  4987. (save-excursion
  4988. (setq ne-beg (org-back-over-empty-lines))
  4989. (setq beg (point)))
  4990. (save-match-data
  4991. (save-excursion (outline-end-of-heading)
  4992. (setq folded (org-invisible-p)))
  4993. (outline-end-of-subtree))
  4994. (outline-next-heading)
  4995. (setq ne-end (org-back-over-empty-lines))
  4996. (setq end (point))
  4997. (goto-char beg0)
  4998. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4999. ;; include less whitespace
  5000. (save-excursion
  5001. (goto-char beg)
  5002. (forward-line (- ne-beg ne-end))
  5003. (setq beg (point))))
  5004. ;; Find insertion point, with error handling
  5005. (while (> cnt 0)
  5006. (or (and (funcall movfunc) (looking-at outline-regexp))
  5007. (progn (goto-char beg0)
  5008. (error "Cannot move past superior level or buffer limit")))
  5009. (setq cnt (1- cnt)))
  5010. (if (> arg 0)
  5011. ;; Moving forward - still need to move over subtree
  5012. (progn (org-end-of-subtree t t)
  5013. (save-excursion
  5014. (org-back-over-empty-lines)
  5015. (or (bolp) (newline)))))
  5016. (setq ne-ins (org-back-over-empty-lines))
  5017. (move-marker ins-point (point))
  5018. (setq txt (buffer-substring beg end))
  5019. (org-save-markers-in-region beg end)
  5020. (delete-region beg end)
  5021. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5022. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5023. (let ((bbb (point)))
  5024. (insert-before-markers txt)
  5025. (org-reinstall-markers-in-region bbb)
  5026. (move-marker ins-point bbb))
  5027. (or (bolp) (insert "\n"))
  5028. (setq ins-end (point))
  5029. (goto-char ins-point)
  5030. (org-skip-whitespace)
  5031. (when (and (< arg 0)
  5032. (org-first-sibling-p)
  5033. (> ne-ins ne-beg))
  5034. ;; Move whitespace back to beginning
  5035. (save-excursion
  5036. (goto-char ins-end)
  5037. (let ((kill-whole-line t))
  5038. (kill-line (- ne-ins ne-beg)) (point)))
  5039. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5040. (move-marker ins-point nil)
  5041. (org-compact-display-after-subtree-move)
  5042. (org-show-empty-lines-in-parent)
  5043. (unless folded
  5044. (org-show-entry)
  5045. (show-children)
  5046. (org-cycle-hide-drawers 'children))))
  5047. (defvar org-subtree-clip ""
  5048. "Clipboard for cut and paste of subtrees.
  5049. This is actually only a copy of the kill, because we use the normal kill
  5050. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5051. (defvar org-subtree-clip-folded nil
  5052. "Was the last copied subtree folded?
  5053. This is used to fold the tree back after pasting.")
  5054. (defun org-cut-subtree (&optional n)
  5055. "Cut the current subtree into the clipboard.
  5056. With prefix arg N, cut this many sequential subtrees.
  5057. This is a short-hand for marking the subtree and then cutting it."
  5058. (interactive "p")
  5059. (org-copy-subtree n 'cut))
  5060. (defun org-copy-subtree (&optional n cut force-store-markers)
  5061. "Cut the current subtree into the clipboard.
  5062. With prefix arg N, cut this many sequential subtrees.
  5063. This is a short-hand for marking the subtree and then copying it.
  5064. If CUT is non-nil, actually cut the subtree.
  5065. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5066. of some markers in the region, even if CUT is non-nil. This is
  5067. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5068. (interactive "p")
  5069. (let (beg end folded (beg0 (point)))
  5070. (if (interactive-p)
  5071. (org-back-to-heading nil) ; take what looks like a subtree
  5072. (org-back-to-heading t)) ; take what is really there
  5073. (org-back-over-empty-lines)
  5074. (setq beg (point))
  5075. (skip-chars-forward " \t\r\n")
  5076. (save-match-data
  5077. (save-excursion (outline-end-of-heading)
  5078. (setq folded (org-invisible-p)))
  5079. (condition-case nil
  5080. (outline-forward-same-level (1- n))
  5081. (error nil))
  5082. (org-end-of-subtree t t))
  5083. (org-back-over-empty-lines)
  5084. (setq end (point))
  5085. (goto-char beg0)
  5086. (when (> end beg)
  5087. (setq org-subtree-clip-folded folded)
  5088. (when (or cut force-store-markers)
  5089. (org-save-markers-in-region beg end))
  5090. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5091. (setq org-subtree-clip (current-kill 0))
  5092. (message "%s: Subtree(s) with %d characters"
  5093. (if cut "Cut" "Copied")
  5094. (length org-subtree-clip)))))
  5095. (defun org-paste-subtree (&optional level tree for-yank)
  5096. "Paste the clipboard as a subtree, with modification of headline level.
  5097. The entire subtree is promoted or demoted in order to match a new headline
  5098. level.
  5099. If the cursor is at the beginning of a headline, the same level as
  5100. that headline is used to paste the tree
  5101. If not, the new level is derived from the *visible* headings
  5102. before and after the insertion point, and taken to be the inferior headline
  5103. level of the two. So if the previous visible heading is level 3 and the
  5104. next is level 4 (or vice versa), level 4 will be used for insertion.
  5105. This makes sure that the subtree remains an independent subtree and does
  5106. not swallow low level entries.
  5107. You can also force a different level, either by using a numeric prefix
  5108. argument, or by inserting the heading marker by hand. For example, if the
  5109. cursor is after \"*****\", then the tree will be shifted to level 5.
  5110. If optional TREE is given, use this text instead of the kill ring.
  5111. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5112. move back over whitespace before inserting, and move point to the end of
  5113. the inserted text when done."
  5114. (interactive "P")
  5115. (unless (org-kill-is-subtree-p tree)
  5116. (error "%s"
  5117. (substitute-command-keys
  5118. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5119. (let* ((visp (not (org-invisible-p)))
  5120. (txt (or tree (and kill-ring (current-kill 0))))
  5121. (^re (concat "^\\(" outline-regexp "\\)"))
  5122. (re (concat "\\(" outline-regexp "\\)"))
  5123. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5124. (old-level (if (string-match ^re txt)
  5125. (- (match-end 0) (match-beginning 0) 1)
  5126. -1))
  5127. (force-level (cond (level (prefix-numeric-value level))
  5128. ((and (looking-at "[ \t]*$")
  5129. (string-match
  5130. ^re_ (buffer-substring
  5131. (point-at-bol) (point))))
  5132. (- (match-end 1) (match-beginning 1)))
  5133. ((and (bolp)
  5134. (looking-at org-outline-regexp))
  5135. (- (match-end 0) (point) 1))
  5136. (t nil)))
  5137. (previous-level (save-excursion
  5138. (condition-case nil
  5139. (progn
  5140. (outline-previous-visible-heading 1)
  5141. (if (looking-at re)
  5142. (- (match-end 0) (match-beginning 0) 1)
  5143. 1))
  5144. (error 1))))
  5145. (next-level (save-excursion
  5146. (condition-case nil
  5147. (progn
  5148. (or (looking-at outline-regexp)
  5149. (outline-next-visible-heading 1))
  5150. (if (looking-at re)
  5151. (- (match-end 0) (match-beginning 0) 1)
  5152. 1))
  5153. (error 1))))
  5154. (new-level (or force-level (max previous-level next-level)))
  5155. (shift (if (or (= old-level -1)
  5156. (= new-level -1)
  5157. (= old-level new-level))
  5158. 0
  5159. (- new-level old-level)))
  5160. (delta (if (> shift 0) -1 1))
  5161. (func (if (> shift 0) 'org-demote 'org-promote))
  5162. (org-odd-levels-only nil)
  5163. beg end newend)
  5164. ;; Remove the forced level indicator
  5165. (if force-level
  5166. (delete-region (point-at-bol) (point)))
  5167. ;; Paste
  5168. (beginning-of-line 1)
  5169. (unless for-yank (org-back-over-empty-lines))
  5170. (setq beg (point))
  5171. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5172. (insert-before-markers txt)
  5173. (unless (string-match "\n\\'" txt) (insert "\n"))
  5174. (setq newend (point))
  5175. (org-reinstall-markers-in-region beg)
  5176. (setq end (point))
  5177. (goto-char beg)
  5178. (skip-chars-forward " \t\n\r")
  5179. (setq beg (point))
  5180. (if (and (org-invisible-p) visp)
  5181. (save-excursion (outline-show-heading)))
  5182. ;; Shift if necessary
  5183. (unless (= shift 0)
  5184. (save-restriction
  5185. (narrow-to-region beg end)
  5186. (while (not (= shift 0))
  5187. (org-map-region func (point-min) (point-max))
  5188. (setq shift (+ delta shift)))
  5189. (goto-char (point-min))
  5190. (setq newend (point-max))))
  5191. (when (or (interactive-p) for-yank)
  5192. (message "Clipboard pasted as level %d subtree" new-level))
  5193. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5194. kill-ring
  5195. (eq org-subtree-clip (current-kill 0))
  5196. org-subtree-clip-folded)
  5197. ;; The tree was folded before it was killed/copied
  5198. (hide-subtree))
  5199. (and for-yank (goto-char newend))))
  5200. (defun org-kill-is-subtree-p (&optional txt)
  5201. "Check if the current kill is an outline subtree, or a set of trees.
  5202. Returns nil if kill does not start with a headline, or if the first
  5203. headline level is not the largest headline level in the tree.
  5204. So this will actually accept several entries of equal levels as well,
  5205. which is OK for `org-paste-subtree'.
  5206. If optional TXT is given, check this string instead of the current kill."
  5207. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5208. (start-level (and kill
  5209. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5210. org-outline-regexp "\\)")
  5211. kill)
  5212. (- (match-end 2) (match-beginning 2) 1)))
  5213. (re (concat "^" org-outline-regexp))
  5214. (start (1+ (or (match-beginning 2) -1))))
  5215. (if (not start-level)
  5216. (progn
  5217. nil) ;; does not even start with a heading
  5218. (catch 'exit
  5219. (while (setq start (string-match re kill (1+ start)))
  5220. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5221. (throw 'exit nil)))
  5222. t))))
  5223. (defvar org-markers-to-move nil
  5224. "Markers that should be moved with a cut-and-paste operation.
  5225. Those markers are stored together with their positions relative to
  5226. the start of the region.")
  5227. (defun org-save-markers-in-region (beg end)
  5228. "Check markers in region.
  5229. If these markers are between BEG and END, record their position relative
  5230. to BEG, so that after moving the block of text, we can put the markers back
  5231. into place.
  5232. This function gets called just before an entry or tree gets cut from the
  5233. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5234. called immediately, to move the markers with the entries."
  5235. (setq org-markers-to-move nil)
  5236. (when (featurep 'org-clock)
  5237. (org-clock-save-markers-for-cut-and-paste beg end))
  5238. (when (featurep 'org-agenda)
  5239. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5240. (defun org-check-and-save-marker (marker beg end)
  5241. "Check if MARKER is between BEG and END.
  5242. If yes, remember the marker and the distance to BEG."
  5243. (when (and (marker-buffer marker)
  5244. (equal (marker-buffer marker) (current-buffer)))
  5245. (if (and (>= marker beg) (< marker end))
  5246. (push (cons marker (- marker beg)) org-markers-to-move))))
  5247. (defun org-reinstall-markers-in-region (beg)
  5248. "Move all remembered markers to their position relative to BEG."
  5249. (mapc (lambda (x)
  5250. (move-marker (car x) (+ beg (cdr x))))
  5251. org-markers-to-move)
  5252. (setq org-markers-to-move nil))
  5253. (defun org-narrow-to-subtree ()
  5254. "Narrow buffer to the current subtree."
  5255. (interactive)
  5256. (save-excursion
  5257. (save-match-data
  5258. (narrow-to-region
  5259. (progn (org-back-to-heading) (point))
  5260. (progn (org-end-of-subtree t) (point))))))
  5261. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5262. "Clone the task (subtree) at point N times.
  5263. The clones will be inserted as siblings.
  5264. In interactive use, the user will be prompted for the number of clones
  5265. to be produced, and for a time SHIFT, which may be a repeater as used
  5266. in time stamps, for example `+3d'.
  5267. When a valid repeater is given and the entry contains any time stamps,
  5268. the clones will become a sequence in time, with time stamps in the
  5269. subtree shifted for each clone produced. If SHIFT is nil or the
  5270. empty string, time stamps will be left alone.
  5271. If the original subtree did contain time stamps with a repeater,
  5272. the following will happen:
  5273. - the repeater will be removed in each clone
  5274. - an additional clone will be produced, with the current, unshifted
  5275. date(s) in the entry.
  5276. - the original entry will be placed *after* all the clones, with
  5277. repeater intact.
  5278. - the start days in the repeater in the original entry will be shifted
  5279. to past the last clone.
  5280. I this way you can spell out a number of instances of a repeating task,
  5281. and still retain the repeater to cover future instances of the task."
  5282. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5283. (let (beg end template task
  5284. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5285. (if (not (and (integerp n) (> n 0)))
  5286. (error "Invalid number of replications %s" n))
  5287. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5288. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5289. shift)))
  5290. (error "Invalid shift specification %s" shift))
  5291. (when doshift
  5292. (setq shift-n (string-to-number (match-string 1 shift))
  5293. shift-what (cdr (assoc (match-string 2 shift)
  5294. '(("d" . day) ("w" . week)
  5295. ("m" . month) ("y" . year))))))
  5296. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5297. (setq nmin 1 nmax n)
  5298. (org-back-to-heading t)
  5299. (setq beg (point))
  5300. (org-end-of-subtree t t)
  5301. (setq end (point))
  5302. (setq template (buffer-substring beg end))
  5303. (when (and doshift
  5304. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5305. (delete-region beg end)
  5306. (setq end beg)
  5307. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5308. (goto-char end)
  5309. (loop for n from nmin to nmax do
  5310. (if (not doshift)
  5311. (setq task template)
  5312. (with-temp-buffer
  5313. (insert template)
  5314. (org-mode)
  5315. (goto-char (point-min))
  5316. (while (re-search-forward org-ts-regexp nil t)
  5317. (org-timestamp-change (* n shift-n) shift-what))
  5318. (unless (= n n-no-remove)
  5319. (goto-char (point-min))
  5320. (while (re-search-forward org-ts-regexp nil t)
  5321. (save-excursion
  5322. (goto-char (match-beginning 0))
  5323. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5324. (delete-region (match-beginning 1) (match-end 1))))))
  5325. (setq task (buffer-string))))
  5326. (insert task))
  5327. (goto-char beg)))
  5328. ;;; Outline Sorting
  5329. (defun org-sort (with-case)
  5330. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5331. Optional argument WITH-CASE means sort case-sensitively.
  5332. With a double prefix argument, also remove duplicate entries."
  5333. (interactive "P")
  5334. (if (org-at-table-p)
  5335. (org-call-with-arg 'org-table-sort-lines with-case)
  5336. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5337. (defun org-sort-remove-invisible (s)
  5338. (remove-text-properties 0 (length s) org-rm-props s)
  5339. (while (string-match org-bracket-link-regexp s)
  5340. (setq s (replace-match (if (match-end 2)
  5341. (match-string 3 s)
  5342. (match-string 1 s)) t t s)))
  5343. s)
  5344. (defvar org-priority-regexp) ; defined later in the file
  5345. (defun org-sort-entries-or-items
  5346. (&optional with-case sorting-type getkey-func compare-func property)
  5347. "Sort entries on a certain level of an outline tree.
  5348. If there is an active region, the entries in the region are sorted.
  5349. Else, if the cursor is before the first entry, sort the top-level items.
  5350. Else, the children of the entry at point are sorted.
  5351. Sorting can be alphabetically, numerically, and by date/time as given by
  5352. the first time stamp in the entry. The command prompts for the sorting
  5353. type unless it has been given to the function through the SORTING-TYPE
  5354. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5355. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5356. called with point at the beginning of the record. It must return either
  5357. a string or a number that should serve as the sorting key for that record.
  5358. Comparing entries ignores case by default. However, with an optional argument
  5359. WITH-CASE, the sorting considers case as well."
  5360. (interactive "P")
  5361. (let ((case-func (if with-case 'identity 'downcase))
  5362. start beg end stars re re2
  5363. txt what tmp plain-list-p)
  5364. ;; Find beginning and end of region to sort
  5365. (cond
  5366. ((org-region-active-p)
  5367. ;; we will sort the region
  5368. (setq end (region-end)
  5369. what "region")
  5370. (goto-char (region-beginning))
  5371. (if (not (org-on-heading-p)) (outline-next-heading))
  5372. (setq start (point)))
  5373. ((org-at-item-p)
  5374. ;; we will sort this plain list
  5375. (org-beginning-of-item-list) (setq start (point))
  5376. (org-end-of-item-list) (setq end (point))
  5377. (goto-char start)
  5378. (setq plain-list-p t
  5379. what "plain list"))
  5380. ((or (org-on-heading-p)
  5381. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5382. ;; we will sort the children of the current headline
  5383. (org-back-to-heading)
  5384. (setq start (point)
  5385. end (progn (org-end-of-subtree t t)
  5386. (org-back-over-empty-lines)
  5387. (point))
  5388. what "children")
  5389. (goto-char start)
  5390. (show-subtree)
  5391. (outline-next-heading))
  5392. (t
  5393. ;; we will sort the top-level entries in this file
  5394. (goto-char (point-min))
  5395. (or (org-on-heading-p) (outline-next-heading))
  5396. (setq start (point) end (point-max) what "top-level")
  5397. (goto-char start)
  5398. (show-all)))
  5399. (setq beg (point))
  5400. (if (>= beg end) (error "Nothing to sort"))
  5401. (unless plain-list-p
  5402. (looking-at "\\(\\*+\\)")
  5403. (setq stars (match-string 1)
  5404. re (concat "^" (regexp-quote stars) " +")
  5405. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5406. txt (buffer-substring beg end))
  5407. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5408. (if (and (not (equal stars "*")) (string-match re2 txt))
  5409. (error "Region to sort contains a level above the first entry")))
  5410. (unless sorting-type
  5411. (message
  5412. (if plain-list-p
  5413. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5414. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5415. what)
  5416. (setq sorting-type (read-char-exclusive))
  5417. (and (= (downcase sorting-type) ?f)
  5418. (setq getkey-func
  5419. (org-ido-completing-read "Sort using function: "
  5420. obarray 'fboundp t nil nil))
  5421. (setq getkey-func (intern getkey-func)))
  5422. (and (= (downcase sorting-type) ?r)
  5423. (setq property
  5424. (org-ido-completing-read "Property: "
  5425. (mapcar 'list (org-buffer-property-keys t))
  5426. nil t))))
  5427. (message "Sorting entries...")
  5428. (save-restriction
  5429. (narrow-to-region start end)
  5430. (let ((dcst (downcase sorting-type))
  5431. (now (current-time)))
  5432. (sort-subr
  5433. (/= dcst sorting-type)
  5434. ;; This function moves to the beginning character of the "record" to
  5435. ;; be sorted.
  5436. (if plain-list-p
  5437. (lambda nil
  5438. (if (org-at-item-p) t (goto-char (point-max))))
  5439. (lambda nil
  5440. (if (re-search-forward re nil t)
  5441. (goto-char (match-beginning 0))
  5442. (goto-char (point-max)))))
  5443. ;; This function moves to the last character of the "record" being
  5444. ;; sorted.
  5445. (if plain-list-p
  5446. 'org-end-of-item
  5447. (lambda nil
  5448. (save-match-data
  5449. (condition-case nil
  5450. (outline-forward-same-level 1)
  5451. (error
  5452. (goto-char (point-max)))))))
  5453. ;; This function returns the value that gets sorted against.
  5454. (if plain-list-p
  5455. (lambda nil
  5456. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5457. (cond
  5458. ((= dcst ?n)
  5459. (string-to-number (buffer-substring (match-end 0)
  5460. (point-at-eol))))
  5461. ((= dcst ?a)
  5462. (buffer-substring (match-end 0) (point-at-eol)))
  5463. ((= dcst ?t)
  5464. (if (re-search-forward org-ts-regexp
  5465. (point-at-eol) t)
  5466. (org-time-string-to-time (match-string 0))
  5467. now))
  5468. ((= dcst ?f)
  5469. (if getkey-func
  5470. (progn
  5471. (setq tmp (funcall getkey-func))
  5472. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5473. tmp)
  5474. (error "Invalid key function `%s'" getkey-func)))
  5475. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5476. (lambda nil
  5477. (cond
  5478. ((= dcst ?n)
  5479. (if (looking-at org-complex-heading-regexp)
  5480. (string-to-number (match-string 4))
  5481. nil))
  5482. ((= dcst ?a)
  5483. (if (looking-at org-complex-heading-regexp)
  5484. (funcall case-func (match-string 4))
  5485. nil))
  5486. ((= dcst ?t)
  5487. (if (re-search-forward org-ts-regexp
  5488. (save-excursion
  5489. (forward-line 2)
  5490. (point)) t)
  5491. (org-time-string-to-time (match-string 0))
  5492. now))
  5493. ((= dcst ?p)
  5494. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5495. (string-to-char (match-string 2))
  5496. org-default-priority))
  5497. ((= dcst ?r)
  5498. (or (org-entry-get nil property) ""))
  5499. ((= dcst ?o)
  5500. (if (looking-at org-complex-heading-regexp)
  5501. (- 9999 (length (member (match-string 2)
  5502. org-todo-keywords-1)))))
  5503. ((= dcst ?f)
  5504. (if getkey-func
  5505. (progn
  5506. (setq tmp (funcall getkey-func))
  5507. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5508. tmp)
  5509. (error "Invalid key function `%s'" getkey-func)))
  5510. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5511. nil
  5512. (cond
  5513. ((= dcst ?a) 'string<)
  5514. ((= dcst ?t) 'time-less-p)
  5515. ((= dcst ?f) compare-func)
  5516. (t nil)))))
  5517. (message "Sorting entries...done")))
  5518. (defun org-do-sort (table what &optional with-case sorting-type)
  5519. "Sort TABLE of WHAT according to SORTING-TYPE.
  5520. The user will be prompted for the SORTING-TYPE if the call to this
  5521. function does not specify it. WHAT is only for the prompt, to indicate
  5522. what is being sorted. The sorting key will be extracted from
  5523. the car of the elements of the table.
  5524. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5525. (unless sorting-type
  5526. (message
  5527. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5528. what)
  5529. (setq sorting-type (read-char-exclusive)))
  5530. (let ((dcst (downcase sorting-type))
  5531. extractfun comparefun)
  5532. ;; Define the appropriate functions
  5533. (cond
  5534. ((= dcst ?n)
  5535. (setq extractfun 'string-to-number
  5536. comparefun (if (= dcst sorting-type) '< '>)))
  5537. ((= dcst ?a)
  5538. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5539. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5540. comparefun (if (= dcst sorting-type)
  5541. 'string<
  5542. (lambda (a b) (and (not (string< a b))
  5543. (not (string= a b)))))))
  5544. ((= dcst ?t)
  5545. (setq extractfun
  5546. (lambda (x)
  5547. (if (or (string-match org-ts-regexp x)
  5548. (string-match org-ts-regexp-both x))
  5549. (time-to-seconds
  5550. (org-time-string-to-time (match-string 0 x)))
  5551. 0))
  5552. comparefun (if (= dcst sorting-type) '< '>)))
  5553. (t (error "Invalid sorting type `%c'" sorting-type)))
  5554. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5555. table)
  5556. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5557. ;;; Editing source examples
  5558. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5559. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5560. (defvar org-edit-src-force-single-line nil)
  5561. (defvar org-edit-src-from-org-mode nil)
  5562. (defvar org-edit-src-picture nil)
  5563. (define-minor-mode org-exit-edit-mode
  5564. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5565. (defun org-edit-src-code ()
  5566. "Edit the source code example at point.
  5567. An indirect buffer is created, and that buffer is then narrowed to the
  5568. example at point and switched to the correct language mode. When done,
  5569. exit by killing the buffer with \\[org-edit-src-exit]."
  5570. (interactive)
  5571. (let ((line (org-current-line))
  5572. (case-fold-search t)
  5573. (msg (substitute-command-keys
  5574. "Edit, then exit with C-c ' (C-c and single quote)"))
  5575. (info (org-edit-src-find-region-and-lang))
  5576. (org-mode-p (eq major-mode 'org-mode))
  5577. beg end lang lang-f single lfmt)
  5578. (if (not info)
  5579. nil
  5580. (setq beg (nth 0 info)
  5581. end (nth 1 info)
  5582. lang (nth 2 info)
  5583. single (nth 3 info)
  5584. lfmt (nth 4 info)
  5585. lang-f (intern (concat lang "-mode")))
  5586. (unless (functionp lang-f)
  5587. (error "No such language mode: %s" lang-f))
  5588. (goto-line line)
  5589. (if (get-buffer "*Org Edit Src Example*")
  5590. (kill-buffer "*Org Edit Src Example*"))
  5591. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5592. "*Org Edit Src Example*"))
  5593. (narrow-to-region beg end)
  5594. (remove-text-properties beg end '(display nil invisible nil
  5595. intangible nil))
  5596. (let ((org-inhibit-startup t))
  5597. (funcall lang-f))
  5598. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5599. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5600. (when lfmt
  5601. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5602. (when org-mode-p
  5603. (goto-char (point-min))
  5604. (while (re-search-forward "^," nil t)
  5605. (replace-match "")))
  5606. (goto-line line)
  5607. (org-exit-edit-mode)
  5608. (org-set-local 'header-line-format msg)
  5609. (message "%s" msg)
  5610. t)))
  5611. (defun org-edit-fixed-width-region ()
  5612. "Edit the fixed-width ascii drawing at point.
  5613. This must be a region where each line starts with a colon followed by
  5614. a space character.
  5615. An indirect buffer is created, and that buffer is then narrowed to the
  5616. example at point and switched to artist-mode. When done,
  5617. exit by killing the buffer with \\[org-edit-src-exit]."
  5618. (interactive)
  5619. (let ((line (org-current-line))
  5620. (case-fold-search t)
  5621. (msg (substitute-command-keys
  5622. "Edit, then exit with C-c ' (C-c and single quote)"))
  5623. (org-mode-p (eq major-mode 'org-mode))
  5624. beg end)
  5625. (beginning-of-line 1)
  5626. (if (looking-at "[ \t]*[^:\n \t]")
  5627. nil
  5628. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5629. (setq beg (point) end beg)
  5630. (save-excursion
  5631. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5632. (setq beg (point-at-bol 2))
  5633. (setq beg (point))))
  5634. (save-excursion
  5635. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5636. (setq end (1- (match-beginning 0)))
  5637. (setq end (point))))
  5638. (goto-line line))
  5639. (if (get-buffer "*Org Edit Picture*")
  5640. (kill-buffer "*Org Edit Picture*"))
  5641. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5642. "*Org Edit Picture*"))
  5643. (narrow-to-region beg end)
  5644. (remove-text-properties beg end '(display nil invisible nil
  5645. intangible nil))
  5646. (when (fboundp 'font-lock-unfontify-region)
  5647. (font-lock-unfontify-region (point-min) (point-max)))
  5648. (cond
  5649. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5650. (fundamental-mode)
  5651. (artist-mode 1))
  5652. (t (funcall org-edit-fixed-width-region-mode)))
  5653. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5654. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5655. (set (make-local-variable 'org-edit-src-picture) t)
  5656. (goto-char (point-min))
  5657. (while (re-search-forward "^[ \t]*: ?" nil t)
  5658. (replace-match ""))
  5659. (goto-line line)
  5660. (org-exit-edit-mode)
  5661. (org-set-local 'header-line-format msg)
  5662. (message "%s" msg)
  5663. t)))
  5664. (defun org-edit-src-find-region-and-lang ()
  5665. "Find the region and language for a local edit.
  5666. Return a list with beginning and end of the region, a string representing
  5667. the language, a switch telling of the content should be in a single line."
  5668. (let ((re-list
  5669. (append
  5670. org-edit-src-region-extra
  5671. '(
  5672. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5673. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5674. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5675. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5676. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5677. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5678. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5679. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5680. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5681. ("^#\\+html:" "\n" "html" single-line)
  5682. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5683. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5684. ("^#\\+latex:" "\n" "latex" single-line)
  5685. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5686. ("^#\\+ascii:" "\n" "ascii" single-line)
  5687. )))
  5688. (pos (point))
  5689. re1 re2 single beg end lang lfmt match-re1)
  5690. (catch 'exit
  5691. (while (setq entry (pop re-list))
  5692. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5693. single (nth 3 entry))
  5694. (save-excursion
  5695. (if (or (looking-at re1)
  5696. (re-search-backward re1 nil t))
  5697. (progn
  5698. (setq match-re1 (match-string 0))
  5699. (setq beg (match-end 0)
  5700. lang (org-edit-src-get-lang lang)
  5701. lfmt (org-edit-src-get-label-format match-re1))
  5702. (if (and (re-search-forward re2 nil t)
  5703. (>= (match-end 0) pos))
  5704. (throw 'exit (list beg (match-beginning 0)
  5705. lang single lfmt))))
  5706. (if (or (looking-at re2)
  5707. (re-search-forward re2 nil t))
  5708. (progn
  5709. (setq end (match-beginning 0))
  5710. (if (and (re-search-backward re1 nil t)
  5711. (<= (match-beginning 0) pos))
  5712. (progn
  5713. (setq lfmt (org-edit-src-get-label-format
  5714. (match-string 0)))
  5715. (throw 'exit
  5716. (list (match-end 0) end
  5717. (org-edit-src-get-lang lang)
  5718. single lfmt))))))))))))
  5719. (defun org-edit-src-get-lang (lang)
  5720. "Extract the src language."
  5721. (let ((m (match-string 0)))
  5722. (cond
  5723. ((stringp lang) lang)
  5724. ((integerp lang) (match-string lang))
  5725. ((and (eq lang 'lang)
  5726. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5727. (match-string 1 m))
  5728. ((and (eq lang 'style)
  5729. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5730. (match-string 1 m))
  5731. (t "fundamental"))))
  5732. (defun org-edit-src-get-label-format (s)
  5733. "Extract the label format."
  5734. (save-match-data
  5735. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5736. (match-string 1 s))))
  5737. (defun org-edit-src-exit ()
  5738. "Exit special edit and protect problematic lines."
  5739. (interactive)
  5740. (unless (buffer-base-buffer (current-buffer))
  5741. (error "This is not an indirect buffer, something is wrong..."))
  5742. (unless (> (point-min) 1)
  5743. (error "This buffer is not narrowed, something is wrong..."))
  5744. (goto-char (point-min))
  5745. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5746. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5747. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5748. (goto-char (point-min))
  5749. (while (re-search-forward "\n" nil t)
  5750. (replace-match " "))
  5751. (goto-char (point-min))
  5752. (if (looking-at "\\s-*") (replace-match " "))
  5753. (if (re-search-forward "\\s-+\\'" nil t)
  5754. (replace-match "")))
  5755. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5756. (goto-char (point-min))
  5757. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5758. (replace-match ",\\1"))
  5759. (when font-lock-mode
  5760. (font-lock-unfontify-region (point-min) (point-max)))
  5761. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5762. (when (org-bound-and-true-p org-edit-src-picture)
  5763. (untabify (point-min) (point-max))
  5764. (goto-char (point-min))
  5765. (while (re-search-forward "^" nil t)
  5766. (replace-match ": "))
  5767. (when font-lock-mode
  5768. (font-lock-unfontify-region (point-min) (point-max)))
  5769. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5770. (kill-buffer (current-buffer))
  5771. (and (org-mode-p) (org-restart-font-lock)))
  5772. ;;; The orgstruct minor mode
  5773. ;; Define a minor mode which can be used in other modes in order to
  5774. ;; integrate the org-mode structure editing commands.
  5775. ;; This is really a hack, because the org-mode structure commands use
  5776. ;; keys which normally belong to the major mode. Here is how it
  5777. ;; works: The minor mode defines all the keys necessary to operate the
  5778. ;; structure commands, but wraps the commands into a function which
  5779. ;; tests if the cursor is currently at a headline or a plain list
  5780. ;; item. If that is the case, the structure command is used,
  5781. ;; temporarily setting many Org-mode variables like regular
  5782. ;; expressions for filling etc. However, when any of those keys is
  5783. ;; used at a different location, function uses `key-binding' to look
  5784. ;; up if the key has an associated command in another currently active
  5785. ;; keymap (minor modes, major mode, global), and executes that
  5786. ;; command. There might be problems if any of the keys is otherwise
  5787. ;; used as a prefix key.
  5788. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5789. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5790. ;; addresses this by checking explicitly for both bindings.
  5791. (defvar orgstruct-mode-map (make-sparse-keymap)
  5792. "Keymap for the minor `orgstruct-mode'.")
  5793. (defvar org-local-vars nil
  5794. "List of local variables, for use by `orgstruct-mode'")
  5795. ;;;###autoload
  5796. (define-minor-mode orgstruct-mode
  5797. "Toggle the minor more `orgstruct-mode'.
  5798. This mode is for using Org-mode structure commands in other modes.
  5799. The following key behave as if Org-mode was active, if the cursor
  5800. is on a headline, or on a plain list item (both in the definition
  5801. of Org-mode).
  5802. M-up Move entry/item up
  5803. M-down Move entry/item down
  5804. M-left Promote
  5805. M-right Demote
  5806. M-S-up Move entry/item up
  5807. M-S-down Move entry/item down
  5808. M-S-left Promote subtree
  5809. M-S-right Demote subtree
  5810. M-q Fill paragraph and items like in Org-mode
  5811. C-c ^ Sort entries
  5812. C-c - Cycle list bullet
  5813. TAB Cycle item visibility
  5814. M-RET Insert new heading/item
  5815. S-M-RET Insert new TODO heading / Checkbox item
  5816. C-c C-c Set tags / toggle checkbox"
  5817. nil " OrgStruct" nil
  5818. (org-load-modules-maybe)
  5819. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5820. ;;;###autoload
  5821. (defun turn-on-orgstruct ()
  5822. "Unconditionally turn on `orgstruct-mode'."
  5823. (orgstruct-mode 1))
  5824. (defun orgstruct++-mode (&optional arg)
  5825. "Toggle `orgstruct-mode', the enhanced version of it.
  5826. In addition to setting orgstruct-mode, this also exports all indentation and
  5827. autofilling variables from org-mode into the buffer. It will also
  5828. recognize item context in multiline items.
  5829. Note that turning off orgstruct-mode will *not* remove the
  5830. indentation/paragraph settings. This can only be done by refreshing the
  5831. major mode, for example with \[normal-mode]."
  5832. (interactive "P")
  5833. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5834. (if (< arg 1)
  5835. (orgstruct-mode -1)
  5836. (orgstruct-mode 1)
  5837. (let (var val)
  5838. (mapc
  5839. (lambda (x)
  5840. (when (string-match
  5841. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5842. (symbol-name (car x)))
  5843. (setq var (car x) val (nth 1 x))
  5844. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5845. org-local-vars)
  5846. (org-set-local 'orgstruct-is-++ t))))
  5847. (defvar orgstruct-is-++ nil
  5848. "Is orgstruct-mode in ++ version in the current-buffer?")
  5849. (make-variable-buffer-local 'orgstruct-is-++)
  5850. ;;;###autoload
  5851. (defun turn-on-orgstruct++ ()
  5852. "Unconditionally turn on `orgstruct++-mode'."
  5853. (orgstruct++-mode 1))
  5854. (defun orgstruct-error ()
  5855. "Error when there is no default binding for a structure key."
  5856. (interactive)
  5857. (error "This key has no function outside structure elements"))
  5858. (defun orgstruct-setup ()
  5859. "Setup orgstruct keymaps."
  5860. (let ((nfunc 0)
  5861. (bindings
  5862. (list
  5863. '([(meta up)] org-metaup)
  5864. '([(meta down)] org-metadown)
  5865. '([(meta left)] org-metaleft)
  5866. '([(meta right)] org-metaright)
  5867. '([(meta shift up)] org-shiftmetaup)
  5868. '([(meta shift down)] org-shiftmetadown)
  5869. '([(meta shift left)] org-shiftmetaleft)
  5870. '([(meta shift right)] org-shiftmetaright)
  5871. '([(shift up)] org-shiftup)
  5872. '([(shift down)] org-shiftdown)
  5873. '([(shift left)] org-shiftleft)
  5874. '([(shift right)] org-shiftright)
  5875. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5876. '("\M-q" fill-paragraph)
  5877. '("\C-c^" org-sort)
  5878. '("\C-c-" org-cycle-list-bullet)))
  5879. elt key fun cmd)
  5880. (while (setq elt (pop bindings))
  5881. (setq nfunc (1+ nfunc))
  5882. (setq key (org-key (car elt))
  5883. fun (nth 1 elt)
  5884. cmd (orgstruct-make-binding fun nfunc key))
  5885. (org-defkey orgstruct-mode-map key cmd))
  5886. ;; Special treatment needed for TAB and RET
  5887. (org-defkey orgstruct-mode-map [(tab)]
  5888. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5889. (org-defkey orgstruct-mode-map "\C-i"
  5890. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5891. (org-defkey orgstruct-mode-map "\M-\C-m"
  5892. (orgstruct-make-binding 'org-insert-heading 105
  5893. "\M-\C-m" [(meta return)]))
  5894. (org-defkey orgstruct-mode-map [(meta return)]
  5895. (orgstruct-make-binding 'org-insert-heading 106
  5896. [(meta return)] "\M-\C-m"))
  5897. (org-defkey orgstruct-mode-map [(shift meta return)]
  5898. (orgstruct-make-binding 'org-insert-todo-heading 107
  5899. [(meta return)] "\M-\C-m"))
  5900. (unless org-local-vars
  5901. (setq org-local-vars (org-get-local-variables)))
  5902. t))
  5903. (defun orgstruct-make-binding (fun n &rest keys)
  5904. "Create a function for binding in the structure minor mode.
  5905. FUN is the command to call inside a table. N is used to create a unique
  5906. command name. KEYS are keys that should be checked in for a command
  5907. to execute outside of tables."
  5908. (eval
  5909. (list 'defun
  5910. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5911. '(arg)
  5912. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5913. "Outside of structure, run the binding of `"
  5914. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5915. "'.")
  5916. '(interactive "p")
  5917. (list 'if
  5918. `(org-context-p 'headline 'item
  5919. (and orgstruct-is-++
  5920. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5921. 'item-body))
  5922. (list 'org-run-like-in-org-mode (list 'quote fun))
  5923. (list 'let '(orgstruct-mode)
  5924. (list 'call-interactively
  5925. (append '(or)
  5926. (mapcar (lambda (k)
  5927. (list 'key-binding k))
  5928. keys)
  5929. '('orgstruct-error))))))))
  5930. (defun org-context-p (&rest contexts)
  5931. "Check if local context is any of CONTEXTS.
  5932. Possible values in the list of contexts are `table', `headline', and `item'."
  5933. (let ((pos (point)))
  5934. (goto-char (point-at-bol))
  5935. (prog1 (or (and (memq 'table contexts)
  5936. (looking-at "[ \t]*|"))
  5937. (and (memq 'headline contexts)
  5938. ;;????????? (looking-at "\\*+"))
  5939. (looking-at outline-regexp))
  5940. (and (memq 'item contexts)
  5941. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5942. (and (memq 'item-body contexts)
  5943. (org-in-item-p)))
  5944. (goto-char pos))))
  5945. (defun org-get-local-variables ()
  5946. "Return a list of all local variables in an org-mode buffer."
  5947. (let (varlist)
  5948. (with-current-buffer (get-buffer-create "*Org tmp*")
  5949. (erase-buffer)
  5950. (org-mode)
  5951. (setq varlist (buffer-local-variables)))
  5952. (kill-buffer "*Org tmp*")
  5953. (delq nil
  5954. (mapcar
  5955. (lambda (x)
  5956. (setq x
  5957. (if (symbolp x)
  5958. (list x)
  5959. (list (car x) (list 'quote (cdr x)))))
  5960. (if (string-match
  5961. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5962. (symbol-name (car x)))
  5963. x nil))
  5964. varlist))))
  5965. ;;;###autoload
  5966. (defun org-run-like-in-org-mode (cmd)
  5967. "Run a command, pretending that the current buffer is in Org-mode.
  5968. This will temporarily bind local variables that are typically bound in
  5969. Org-mode to the values they have in Org-mode, and then interactively
  5970. call CMD."
  5971. (org-load-modules-maybe)
  5972. (unless org-local-vars
  5973. (setq org-local-vars (org-get-local-variables)))
  5974. (eval (list 'let org-local-vars
  5975. (list 'call-interactively (list 'quote cmd)))))
  5976. ;;;; Archiving
  5977. (defun org-get-category (&optional pos)
  5978. "Get the category applying to position POS."
  5979. (get-text-property (or pos (point)) 'org-category))
  5980. (defun org-refresh-category-properties ()
  5981. "Refresh category text properties in the buffer."
  5982. (let ((def-cat (cond
  5983. ((null org-category)
  5984. (if buffer-file-name
  5985. (file-name-sans-extension
  5986. (file-name-nondirectory buffer-file-name))
  5987. "???"))
  5988. ((symbolp org-category) (symbol-name org-category))
  5989. (t org-category)))
  5990. beg end cat pos optionp)
  5991. (org-unmodified
  5992. (save-excursion
  5993. (save-restriction
  5994. (widen)
  5995. (goto-char (point-min))
  5996. (put-text-property (point) (point-max) 'org-category def-cat)
  5997. (while (re-search-forward
  5998. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5999. (setq pos (match-end 0)
  6000. optionp (equal (char-after (match-beginning 0)) ?#)
  6001. cat (org-trim (match-string 2)))
  6002. (if optionp
  6003. (setq beg (point-at-bol) end (point-max))
  6004. (org-back-to-heading t)
  6005. (setq beg (point) end (org-end-of-subtree t t)))
  6006. (put-text-property beg end 'org-category cat)
  6007. (goto-char pos)))))))
  6008. ;;;; Link Stuff
  6009. ;;; Link abbreviations
  6010. (defun org-link-expand-abbrev (link)
  6011. "Apply replacements as defined in `org-link-abbrev-alist."
  6012. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6013. (let* ((key (match-string 1 link))
  6014. (as (or (assoc key org-link-abbrev-alist-local)
  6015. (assoc key org-link-abbrev-alist)))
  6016. (tag (and (match-end 2) (match-string 3 link)))
  6017. rpl)
  6018. (if (not as)
  6019. link
  6020. (setq rpl (cdr as))
  6021. (cond
  6022. ((symbolp rpl) (funcall rpl tag))
  6023. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6024. ((string-match "%h" rpl)
  6025. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6026. (t (concat rpl tag)))))
  6027. link))
  6028. ;;; Storing and inserting links
  6029. (defvar org-insert-link-history nil
  6030. "Minibuffer history for links inserted with `org-insert-link'.")
  6031. (defvar org-stored-links nil
  6032. "Contains the links stored with `org-store-link'.")
  6033. (defvar org-store-link-plist nil
  6034. "Plist with info about the most recently link created with `org-store-link'.")
  6035. (defvar org-link-protocols nil
  6036. "Link protocols added to Org-mode using `org-add-link-type'.")
  6037. (defvar org-store-link-functions nil
  6038. "List of functions that are called to create and store a link.
  6039. Each function will be called in turn until one returns a non-nil
  6040. value. Each function should check if it is responsible for creating
  6041. this link (for example by looking at the major mode).
  6042. If not, it must exit and return nil.
  6043. If yes, it should return a non-nil value after a calling
  6044. `org-store-link-props' with a list of properties and values.
  6045. Special properties are:
  6046. :type The link prefix. like \"http\". This must be given.
  6047. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6048. This is obligatory as well.
  6049. :description Optional default description for the second pair
  6050. of brackets in an Org-mode link. The user can still change
  6051. this when inserting this link into an Org-mode buffer.
  6052. In addition to these, any additional properties can be specified
  6053. and then used in remember templates.")
  6054. (defun org-add-link-type (type &optional follow export)
  6055. "Add TYPE to the list of `org-link-types'.
  6056. Re-compute all regular expressions depending on `org-link-types'
  6057. FOLLOW and EXPORT are two functions.
  6058. FOLLOW should take the link path as the single argument and do whatever
  6059. is necessary to follow the link, for example find a file or display
  6060. a mail message.
  6061. EXPORT should format the link path for export to one of the export formats.
  6062. It should be a function accepting three arguments:
  6063. path the path of the link, the text after the prefix (like \"http:\")
  6064. desc the description of the link, if any, nil if there was no description
  6065. format the export format, a symbol like `html' or `latex'.
  6066. The function may use the FORMAT information to return different values
  6067. depending on the format. The return value will be put literally into
  6068. the exported file.
  6069. Org-mode has a built-in default for exporting links. If you are happy with
  6070. this default, there is no need to define an export function for the link
  6071. type. For a simple example of an export function, see `org-bbdb.el'."
  6072. (add-to-list 'org-link-types type t)
  6073. (org-make-link-regexps)
  6074. (if (assoc type org-link-protocols)
  6075. (setcdr (assoc type org-link-protocols) (list follow export))
  6076. (push (list type follow export) org-link-protocols)))
  6077. ;;;###autoload
  6078. (defun org-store-link (arg)
  6079. "\\<org-mode-map>Store an org-link to the current location.
  6080. This link is added to `org-stored-links' and can later be inserted
  6081. into an org-buffer with \\[org-insert-link].
  6082. For some link types, a prefix arg is interpreted:
  6083. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6084. For file links, arg negates `org-context-in-file-links'."
  6085. (interactive "P")
  6086. (org-load-modules-maybe)
  6087. (setq org-store-link-plist nil) ; reset
  6088. (let (link cpltxt desc description search txt)
  6089. (cond
  6090. ((run-hook-with-args-until-success 'org-store-link-functions)
  6091. (setq link (plist-get org-store-link-plist :link)
  6092. desc (or (plist-get org-store-link-plist :description) link)))
  6093. ((equal (buffer-name) "*Org Edit Src Example*")
  6094. (let (label gc)
  6095. (while (or (not label)
  6096. (save-excursion
  6097. (save-restriction
  6098. (widen)
  6099. (goto-char (point-min))
  6100. (re-search-forward
  6101. (regexp-quote (format org-coderef-label-format label))
  6102. nil t))))
  6103. (when label (message "Label exists already") (sit-for 2))
  6104. (setq label (read-string "Code line label: " label)))
  6105. (end-of-line 1)
  6106. (setq link (format org-coderef-label-format label))
  6107. (setq gc (- 79 (length link)))
  6108. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6109. (insert link)
  6110. (setq link (concat "(" label ")") desc nil)))
  6111. ((eq major-mode 'calendar-mode)
  6112. (let ((cd (calendar-cursor-to-date)))
  6113. (setq link
  6114. (format-time-string
  6115. (car org-time-stamp-formats)
  6116. (apply 'encode-time
  6117. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6118. nil nil nil))))
  6119. (org-store-link-props :type "calendar" :date cd)))
  6120. ((eq major-mode 'w3-mode)
  6121. (setq cpltxt (if (and (buffer-name)
  6122. (not (string-match "Untitled" (buffer-name))))
  6123. (buffer-name)
  6124. (url-view-url t))
  6125. link (org-make-link (url-view-url t)))
  6126. (org-store-link-props :type "w3" :url (url-view-url t)))
  6127. ((eq major-mode 'w3m-mode)
  6128. (setq cpltxt (or w3m-current-title w3m-current-url)
  6129. link (org-make-link w3m-current-url))
  6130. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6131. ((setq search (run-hook-with-args-until-success
  6132. 'org-create-file-search-functions))
  6133. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6134. "::" search))
  6135. (setq cpltxt (or description link)))
  6136. ((eq major-mode 'image-mode)
  6137. (setq cpltxt (concat "file:"
  6138. (abbreviate-file-name buffer-file-name))
  6139. link (org-make-link cpltxt))
  6140. (org-store-link-props :type "image" :file buffer-file-name))
  6141. ((eq major-mode 'dired-mode)
  6142. ;; link to the file in the current line
  6143. (setq cpltxt (concat "file:"
  6144. (abbreviate-file-name
  6145. (expand-file-name
  6146. (dired-get-filename nil t))))
  6147. link (org-make-link cpltxt)))
  6148. ((and buffer-file-name (org-mode-p))
  6149. (cond
  6150. ((org-in-regexp "<<\\(.*?\\)>>")
  6151. (setq cpltxt
  6152. (concat "file:"
  6153. (abbreviate-file-name buffer-file-name)
  6154. "::" (match-string 1))
  6155. link (org-make-link cpltxt)))
  6156. ((and (featurep 'org-id)
  6157. (or (eq org-link-to-org-use-id t)
  6158. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6159. (interactive-p))
  6160. (and org-link-to-org-use-id
  6161. (condition-case nil
  6162. (org-entry-get nil "ID")
  6163. (error nil)))))
  6164. ;; We can make a link using the ID.
  6165. (setq link (condition-case nil
  6166. (prog1 (org-id-store-link)
  6167. (setq desc (plist-get org-store-link-plist
  6168. :description)))
  6169. (error
  6170. ;; probably before first headline, link to file only
  6171. (concat "file:"
  6172. (abbreviate-file-name buffer-file-name))))))
  6173. (t
  6174. ;; Just link to current headline
  6175. (setq cpltxt (concat "file:"
  6176. (abbreviate-file-name buffer-file-name)))
  6177. ;; Add a context search string
  6178. (when (org-xor org-context-in-file-links arg)
  6179. (setq txt (cond
  6180. ((org-on-heading-p) nil)
  6181. ((org-region-active-p)
  6182. (buffer-substring (region-beginning) (region-end)))
  6183. (t nil)))
  6184. (when (or (null txt) (string-match "\\S-" txt))
  6185. (setq cpltxt
  6186. (concat cpltxt "::"
  6187. (condition-case nil
  6188. (org-make-org-heading-search-string txt)
  6189. (error "")))
  6190. desc "NONE")))
  6191. (if (string-match "::\\'" cpltxt)
  6192. (setq cpltxt (substring cpltxt 0 -2)))
  6193. (setq link (org-make-link cpltxt)))))
  6194. ((buffer-file-name (buffer-base-buffer))
  6195. ;; Just link to this file here.
  6196. (setq cpltxt (concat "file:"
  6197. (abbreviate-file-name
  6198. (buffer-file-name (buffer-base-buffer)))))
  6199. ;; Add a context string
  6200. (when (org-xor org-context-in-file-links arg)
  6201. (setq txt (if (org-region-active-p)
  6202. (buffer-substring (region-beginning) (region-end))
  6203. (buffer-substring (point-at-bol) (point-at-eol))))
  6204. ;; Only use search option if there is some text.
  6205. (when (string-match "\\S-" txt)
  6206. (setq cpltxt
  6207. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6208. desc "NONE")))
  6209. (setq link (org-make-link cpltxt)))
  6210. ((interactive-p)
  6211. (error "Cannot link to a buffer which is not visiting a file"))
  6212. (t (setq link nil)))
  6213. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6214. (setq link (or link cpltxt)
  6215. desc (or desc cpltxt))
  6216. (if (equal desc "NONE") (setq desc nil))
  6217. (if (and (interactive-p) link)
  6218. (progn
  6219. (setq org-stored-links
  6220. (cons (list link desc) org-stored-links))
  6221. (message "Stored: %s" (or desc link)))
  6222. (and link (org-make-link-string link desc)))))
  6223. (defun org-store-link-props (&rest plist)
  6224. "Store link properties, extract names and addresses."
  6225. (let (x adr)
  6226. (when (setq x (plist-get plist :from))
  6227. (setq adr (mail-extract-address-components x))
  6228. (setq plist (plist-put plist :fromname (car adr)))
  6229. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6230. (when (setq x (plist-get plist :to))
  6231. (setq adr (mail-extract-address-components x))
  6232. (setq plist (plist-put plist :toname (car adr)))
  6233. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6234. (let ((from (plist-get plist :from))
  6235. (to (plist-get plist :to)))
  6236. (when (and from to org-from-is-user-regexp)
  6237. (setq plist
  6238. (plist-put plist :fromto
  6239. (if (string-match org-from-is-user-regexp from)
  6240. (concat "to %t")
  6241. (concat "from %f"))))))
  6242. (setq org-store-link-plist plist))
  6243. (defun org-add-link-props (&rest plist)
  6244. "Add these properties to the link property list."
  6245. (let (key value)
  6246. (while plist
  6247. (setq key (pop plist) value (pop plist))
  6248. (setq org-store-link-plist
  6249. (plist-put org-store-link-plist key value)))))
  6250. (defun org-email-link-description (&optional fmt)
  6251. "Return the description part of an email link.
  6252. This takes information from `org-store-link-plist' and formats it
  6253. according to FMT (default from `org-email-link-description-format')."
  6254. (setq fmt (or fmt org-email-link-description-format))
  6255. (let* ((p org-store-link-plist)
  6256. (to (plist-get p :toaddress))
  6257. (from (plist-get p :fromaddress))
  6258. (table
  6259. (list
  6260. (cons "%c" (plist-get p :fromto))
  6261. (cons "%F" (plist-get p :from))
  6262. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6263. (cons "%T" (plist-get p :to))
  6264. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6265. (cons "%s" (plist-get p :subject))
  6266. (cons "%m" (plist-get p :message-id)))))
  6267. (when (string-match "%c" fmt)
  6268. ;; Check if the user wrote this message
  6269. (if (and org-from-is-user-regexp from to
  6270. (save-match-data (string-match org-from-is-user-regexp from)))
  6271. (setq fmt (replace-match "to %t" t t fmt))
  6272. (setq fmt (replace-match "from %f" t t fmt))))
  6273. (org-replace-escapes fmt table)))
  6274. (defun org-make-org-heading-search-string (&optional string heading)
  6275. "Make search string for STRING or current headline."
  6276. (interactive)
  6277. (let ((s (or string (org-get-heading))))
  6278. (unless (and string (not heading))
  6279. ;; We are using a headline, clean up garbage in there.
  6280. (if (string-match org-todo-regexp s)
  6281. (setq s (replace-match "" t t s)))
  6282. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6283. (setq s (replace-match "" t t s)))
  6284. (setq s (org-trim s))
  6285. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6286. org-comment-string "\\)") s)
  6287. (setq s (replace-match "" t t s)))
  6288. (while (string-match org-ts-regexp s)
  6289. (setq s (replace-match "" t t s))))
  6290. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6291. (setq s (replace-match " " t t s)))
  6292. (or string (setq s (concat "*" s))) ; Add * for headlines
  6293. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6294. (defun org-make-link (&rest strings)
  6295. "Concatenate STRINGS."
  6296. (apply 'concat strings))
  6297. (defun org-make-link-string (link &optional description)
  6298. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6299. (unless (string-match "\\S-" link)
  6300. (error "Empty link"))
  6301. (when (stringp description)
  6302. ;; Remove brackets from the description, they are fatal.
  6303. (while (string-match "\\[" description)
  6304. (setq description (replace-match "{" t t description)))
  6305. (while (string-match "\\]" description)
  6306. (setq description (replace-match "}" t t description))))
  6307. (when (equal (org-link-escape link) description)
  6308. ;; No description needed, it is identical
  6309. (setq description nil))
  6310. (when (and (not description)
  6311. (not (equal link (org-link-escape link))))
  6312. (setq description (org-extract-attributes link)))
  6313. (concat "[[" (org-link-escape link) "]"
  6314. (if description (concat "[" description "]") "")
  6315. "]"))
  6316. (defconst org-link-escape-chars
  6317. '((?\ . "%20")
  6318. (?\[ . "%5B")
  6319. (?\] . "%5D")
  6320. (?\340 . "%E0") ; `a
  6321. (?\342 . "%E2") ; ^a
  6322. (?\347 . "%E7") ; ,c
  6323. (?\350 . "%E8") ; `e
  6324. (?\351 . "%E9") ; 'e
  6325. (?\352 . "%EA") ; ^e
  6326. (?\356 . "%EE") ; ^i
  6327. (?\364 . "%F4") ; ^o
  6328. (?\371 . "%F9") ; `u
  6329. (?\373 . "%FB") ; ^u
  6330. (?\; . "%3B")
  6331. (?? . "%3F")
  6332. (?= . "%3D")
  6333. (?+ . "%2B")
  6334. )
  6335. "Association list of escapes for some characters problematic in links.
  6336. This is the list that is used for internal purposes.")
  6337. (defconst org-link-escape-chars-browser
  6338. '((?\ . "%20")) ; 32 for the SPC char
  6339. "Association list of escapes for some characters problematic in links.
  6340. This is the list that is used before handing over to the browser.")
  6341. (defun org-link-escape (text &optional table)
  6342. "Escape characters in TEXT that are problematic for links."
  6343. (setq table (or table org-link-escape-chars))
  6344. (when text
  6345. (let ((re (mapconcat (lambda (x) (regexp-quote
  6346. (char-to-string (car x))))
  6347. table "\\|")))
  6348. (while (string-match re text)
  6349. (setq text
  6350. (replace-match
  6351. (cdr (assoc (string-to-char (match-string 0 text))
  6352. table))
  6353. t t text)))
  6354. text)))
  6355. (defun org-link-unescape (text &optional table)
  6356. "Reverse the action of `org-link-escape'."
  6357. (setq table (or table org-link-escape-chars))
  6358. (when text
  6359. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6360. table "\\|")))
  6361. (while (string-match re text)
  6362. (setq text
  6363. (replace-match
  6364. (char-to-string (car (rassoc (match-string 0 text) table)))
  6365. t t text)))
  6366. text)))
  6367. (defun org-xor (a b)
  6368. "Exclusive or."
  6369. (if a (not b) b))
  6370. (defun org-fixup-message-id-for-http (s)
  6371. "Replace special characters in a message id, so it can be used in an http query."
  6372. (while (string-match "<" s)
  6373. (setq s (replace-match "%3C" t t s)))
  6374. (while (string-match ">" s)
  6375. (setq s (replace-match "%3E" t t s)))
  6376. (while (string-match "@" s)
  6377. (setq s (replace-match "%40" t t s)))
  6378. s)
  6379. ;;;###autoload
  6380. (defun org-insert-link-global ()
  6381. "Insert a link like Org-mode does.
  6382. This command can be called in any mode to insert a link in Org-mode syntax."
  6383. (interactive)
  6384. (org-load-modules-maybe)
  6385. (org-run-like-in-org-mode 'org-insert-link))
  6386. (defun org-insert-link (&optional complete-file link-location)
  6387. "Insert a link. At the prompt, enter the link.
  6388. Completion can be used to insert any of the link protocol prefixes like
  6389. http or ftp in use.
  6390. The history can be used to select a link previously stored with
  6391. `org-store-link'. When the empty string is entered (i.e. if you just
  6392. press RET at the prompt), the link defaults to the most recently
  6393. stored link. As SPC triggers completion in the minibuffer, you need to
  6394. use M-SPC or C-q SPC to force the insertion of a space character.
  6395. You will also be prompted for a description, and if one is given, it will
  6396. be displayed in the buffer instead of the link.
  6397. If there is already a link at point, this command will allow you to edit link
  6398. and description parts.
  6399. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6400. be selected using completion. The path to the file will be relative to the
  6401. current directory if the file is in the current directory or a subdirectory.
  6402. Otherwise, the link will be the absolute path as completed in the minibuffer
  6403. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6404. option `org-link-file-path-type'.
  6405. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6406. the current directory or below.
  6407. With three \\[universal-argument] prefixes, negate the meaning of
  6408. `org-keep-stored-link-after-insertion'.
  6409. If `org-make-link-description-function' is non-nil, this function will be
  6410. called with the link target, and the result will be the default
  6411. link description.
  6412. If the LINK-LOCATION parameter is non-nil, this value will be
  6413. used as the link location instead of reading one interactively."
  6414. (interactive "P")
  6415. (let* ((wcf (current-window-configuration))
  6416. (region (if (org-region-active-p)
  6417. (buffer-substring (region-beginning) (region-end))))
  6418. (remove (and region (list (region-beginning) (region-end))))
  6419. (desc region)
  6420. tmphist ; byte-compile incorrectly complains about this
  6421. (link link-location)
  6422. entry file)
  6423. (cond
  6424. (link-location) ; specified by arg, just use it.
  6425. ((org-in-regexp org-bracket-link-regexp 1)
  6426. ;; We do have a link at point, and we are going to edit it.
  6427. (setq remove (list (match-beginning 0) (match-end 0)))
  6428. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6429. (setq link (read-string "Link: "
  6430. (org-link-unescape
  6431. (org-match-string-no-properties 1)))))
  6432. ((or (org-in-regexp org-angle-link-re)
  6433. (org-in-regexp org-plain-link-re))
  6434. ;; Convert to bracket link
  6435. (setq remove (list (match-beginning 0) (match-end 0))
  6436. link (read-string "Link: "
  6437. (org-remove-angle-brackets (match-string 0)))))
  6438. ((member complete-file '((4) (16)))
  6439. ;; Completing read for file names.
  6440. (setq file (read-file-name "File: "))
  6441. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6442. (pwd1 (file-name-as-directory (abbreviate-file-name
  6443. (expand-file-name ".")))))
  6444. (cond
  6445. ((equal complete-file '(16))
  6446. (setq link (org-make-link
  6447. "file:"
  6448. (abbreviate-file-name (expand-file-name file)))))
  6449. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6450. (setq link (org-make-link "file:" (match-string 1 file))))
  6451. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6452. (expand-file-name file))
  6453. (setq link (org-make-link
  6454. "file:" (match-string 1 (expand-file-name file)))))
  6455. (t (setq link (org-make-link "file:" file))))))
  6456. (t
  6457. ;; Read link, with completion for stored links.
  6458. (with-output-to-temp-buffer "*Org Links*"
  6459. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6460. (when org-stored-links
  6461. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6462. (princ (mapconcat
  6463. (lambda (x)
  6464. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6465. (reverse org-stored-links) "\n"))))
  6466. (let ((cw (selected-window)))
  6467. (select-window (get-buffer-window "*Org Links*"))
  6468. (setq truncate-lines t)
  6469. (org-fit-window-to-buffer)
  6470. (select-window cw))
  6471. ;; Fake a link history, containing the stored links.
  6472. (setq tmphist (append (mapcar 'car org-stored-links)
  6473. org-insert-link-history))
  6474. (unwind-protect
  6475. (setq link
  6476. (let ((org-completion-use-ido nil))
  6477. (org-completing-read
  6478. "Link: "
  6479. (append
  6480. (mapcar (lambda (x) (list (concat (car x) ":")))
  6481. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6482. (mapcar (lambda (x) (list (concat x ":")))
  6483. org-link-types))
  6484. nil nil nil
  6485. 'tmphist
  6486. (or (car (car org-stored-links))))))
  6487. (set-window-configuration wcf)
  6488. (kill-buffer "*Org Links*"))
  6489. (setq entry (assoc link org-stored-links))
  6490. (or entry (push link org-insert-link-history))
  6491. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6492. (not org-keep-stored-link-after-insertion))
  6493. (setq org-stored-links (delq (assoc link org-stored-links)
  6494. org-stored-links)))
  6495. (setq desc (or desc (nth 1 entry)))))
  6496. (if (string-match org-plain-link-re link)
  6497. ;; URL-like link, normalize the use of angular brackets.
  6498. (setq link (org-make-link (org-remove-angle-brackets link))))
  6499. ;; Check if we are linking to the current file with a search option
  6500. ;; If yes, simplify the link by using only the search option.
  6501. (when (and buffer-file-name
  6502. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6503. (let* ((path (match-string 1 link))
  6504. (case-fold-search nil)
  6505. (search (match-string 2 link)))
  6506. (save-match-data
  6507. (if (equal (file-truename buffer-file-name) (file-truename path))
  6508. ;; We are linking to this same file, with a search option
  6509. (setq link search)))))
  6510. ;; Check if we can/should use a relative path. If yes, simplify the link
  6511. (when (string-match "^file:\\(.*\\)" link)
  6512. (let* ((path (match-string 1 link))
  6513. (origpath path)
  6514. (case-fold-search nil))
  6515. (cond
  6516. ((or (eq org-link-file-path-type 'absolute)
  6517. (equal complete-file '(16)))
  6518. (setq path (abbreviate-file-name (expand-file-name path))))
  6519. ((eq org-link-file-path-type 'noabbrev)
  6520. (setq path (expand-file-name path)))
  6521. ((eq org-link-file-path-type 'relative)
  6522. (setq path (file-relative-name path)))
  6523. (t
  6524. (save-match-data
  6525. (if (string-match (concat "^" (regexp-quote
  6526. (file-name-as-directory
  6527. (expand-file-name "."))))
  6528. (expand-file-name path))
  6529. ;; We are linking a file with relative path name.
  6530. (setq path (substring (expand-file-name path)
  6531. (match-end 0)))
  6532. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6533. (setq link (concat "file:" path))
  6534. (if (equal desc origpath)
  6535. (setq desc path))))
  6536. (if org-make-link-description-function
  6537. (setq desc (funcall org-make-link-description-function link desc)))
  6538. (setq desc (read-string "Description: " desc))
  6539. (unless (string-match "\\S-" desc) (setq desc nil))
  6540. (if remove (apply 'delete-region remove))
  6541. (insert (org-make-link-string link desc))))
  6542. (defun org-completing-read (&rest args)
  6543. "Completing-read with SPACE being a normal character."
  6544. (let ((minibuffer-local-completion-map
  6545. (copy-keymap minibuffer-local-completion-map)))
  6546. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6547. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6548. (apply 'org-ido-completing-read args)))
  6549. (defun org-completing-read-no-ido (&rest args)
  6550. (let (org-completion-use-ido)
  6551. (apply 'org-completing-read args)))
  6552. (defun org-ido-completing-read (&rest args)
  6553. "Completing-read using `ido-mode' speedups if available"
  6554. (if (and org-completion-use-ido
  6555. (fboundp 'ido-completing-read)
  6556. (boundp 'ido-mode) ido-mode
  6557. (listp (second args)))
  6558. (let ((ido-enter-matching-directory nil))
  6559. (apply 'ido-completing-read (concat (car args))
  6560. (mapcar (lambda (x) (car x)) (nth 1 args))
  6561. (cddr args)))
  6562. (apply 'completing-read args)))
  6563. (defun org-extract-attributes (s)
  6564. "Extract the attributes cookie from a string and set as text property."
  6565. (let (a attr (start 0) key value)
  6566. (save-match-data
  6567. (when (string-match "{{\\([^}]+\\)}}$" s)
  6568. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6569. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6570. (setq key (match-string 1 a) value (match-string 2 a)
  6571. start (match-end 0)
  6572. attr (plist-put attr (intern key) value))))
  6573. (org-add-props s nil 'org-attr attr))
  6574. s))
  6575. (defun org-attributes-to-string (plist)
  6576. "Format a property list into an HTML attribute list."
  6577. (let ((s "") key value)
  6578. (while plist
  6579. (setq key (pop plist) value (pop plist))
  6580. (and value
  6581. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6582. s))
  6583. ;;; Opening/following a link
  6584. (defvar org-link-search-failed nil)
  6585. (defun org-next-link ()
  6586. "Move forward to the next link.
  6587. If the link is in hidden text, expose it."
  6588. (interactive)
  6589. (when (and org-link-search-failed (eq this-command last-command))
  6590. (goto-char (point-min))
  6591. (message "Link search wrapped back to beginning of buffer"))
  6592. (setq org-link-search-failed nil)
  6593. (let* ((pos (point))
  6594. (ct (org-context))
  6595. (a (assoc :link ct)))
  6596. (if a (goto-char (nth 2 a)))
  6597. (if (re-search-forward org-any-link-re nil t)
  6598. (progn
  6599. (goto-char (match-beginning 0))
  6600. (if (org-invisible-p) (org-show-context)))
  6601. (goto-char pos)
  6602. (setq org-link-search-failed t)
  6603. (error "No further link found"))))
  6604. (defun org-previous-link ()
  6605. "Move backward to the previous link.
  6606. If the link is in hidden text, expose it."
  6607. (interactive)
  6608. (when (and org-link-search-failed (eq this-command last-command))
  6609. (goto-char (point-max))
  6610. (message "Link search wrapped back to end of buffer"))
  6611. (setq org-link-search-failed nil)
  6612. (let* ((pos (point))
  6613. (ct (org-context))
  6614. (a (assoc :link ct)))
  6615. (if a (goto-char (nth 1 a)))
  6616. (if (re-search-backward org-any-link-re nil t)
  6617. (progn
  6618. (goto-char (match-beginning 0))
  6619. (if (org-invisible-p) (org-show-context)))
  6620. (goto-char pos)
  6621. (setq org-link-search-failed t)
  6622. (error "No further link found"))))
  6623. (defun org-translate-link (s)
  6624. "Translate a link string if a translation function has been defined."
  6625. (if (and org-link-translation-function
  6626. (fboundp org-link-translation-function)
  6627. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6628. (progn
  6629. (setq s (funcall org-link-translation-function
  6630. (match-string 1) (match-string 2)))
  6631. (concat (car s) ":" (cdr s)))
  6632. s))
  6633. (defun org-translate-link-from-planner (type path)
  6634. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6635. This is still an experimental function, your mileage may vary."
  6636. (cond
  6637. ((member type '("http" "https" "news" "ftp"))
  6638. ;; standard Internet links are the same.
  6639. nil)
  6640. ((and (equal type "irc") (string-match "^//" path))
  6641. ;; Planner has two / at the beginning of an irc link, we have 1.
  6642. ;; We should have zero, actually....
  6643. (setq path (substring path 1)))
  6644. ((and (equal type "lisp") (string-match "^/" path))
  6645. ;; Planner has a slash, we do not.
  6646. (setq type "elisp" path (substring path 1)))
  6647. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6648. ;; A typical message link. Planner has the id after the fina slash,
  6649. ;; we separate it with a hash mark
  6650. (setq path (concat (match-string 1 path) "#"
  6651. (org-remove-angle-brackets (match-string 2 path)))))
  6652. )
  6653. (cons type path))
  6654. (defun org-find-file-at-mouse (ev)
  6655. "Open file link or URL at mouse."
  6656. (interactive "e")
  6657. (mouse-set-point ev)
  6658. (org-open-at-point 'in-emacs))
  6659. (defun org-open-at-mouse (ev)
  6660. "Open file link or URL at mouse."
  6661. (interactive "e")
  6662. (mouse-set-point ev)
  6663. (if (eq major-mode 'org-agenda-mode)
  6664. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6665. (org-open-at-point))
  6666. (defvar org-window-config-before-follow-link nil
  6667. "The window configuration before following a link.
  6668. This is saved in case the need arises to restore it.")
  6669. (defvar org-open-link-marker (make-marker)
  6670. "Marker pointing to the location where `org-open-at-point; was called.")
  6671. ;;;###autoload
  6672. (defun org-open-at-point-global ()
  6673. "Follow a link like Org-mode does.
  6674. This command can be called in any mode to follow a link that has
  6675. Org-mode syntax."
  6676. (interactive)
  6677. (org-run-like-in-org-mode 'org-open-at-point))
  6678. ;;;###autoload
  6679. (defun org-open-link-from-string (s &optional arg)
  6680. "Open a link in the string S, as if it was in Org-mode."
  6681. (interactive "sLink: \nP")
  6682. (with-temp-buffer
  6683. (let ((org-inhibit-startup t))
  6684. (org-mode)
  6685. (insert s)
  6686. (goto-char (point-min))
  6687. (org-open-at-point arg))))
  6688. (defun org-open-at-point (&optional in-emacs)
  6689. "Open link at or after point.
  6690. If there is no link at point, this function will search forward up to
  6691. the end of the current subtree.
  6692. Normally, files will be opened by an appropriate application. If the
  6693. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6694. With a double prefix argument, try to open outside of Emacs, in the
  6695. application the system uses for this file type."
  6696. (interactive "P")
  6697. (org-load-modules-maybe)
  6698. (move-marker org-open-link-marker (point))
  6699. (setq org-window-config-before-follow-link (current-window-configuration))
  6700. (org-remove-occur-highlights nil nil t)
  6701. (cond
  6702. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6703. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6704. (org-footnote-action))
  6705. (t
  6706. (let (type path link line search (pos (point)))
  6707. (catch 'match
  6708. (save-excursion
  6709. (skip-chars-forward "^]\n\r")
  6710. (when (org-in-regexp org-bracket-link-regexp)
  6711. (setq link (org-extract-attributes
  6712. (org-link-unescape (org-match-string-no-properties 1))))
  6713. (while (string-match " *\n *" link)
  6714. (setq link (replace-match " " t t link)))
  6715. (setq link (org-link-expand-abbrev link))
  6716. (cond
  6717. ((or (file-name-absolute-p link)
  6718. (string-match "^\\.\\.?/" link))
  6719. (setq type "file" path link))
  6720. ((string-match org-link-re-with-space3 link)
  6721. (setq type (match-string 1 link) path (match-string 2 link)))
  6722. (t (setq type "thisfile" path link)))
  6723. (throw 'match t)))
  6724. (when (get-text-property (point) 'org-linked-text)
  6725. (setq type "thisfile"
  6726. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6727. (1+ (point)) (point))
  6728. path (buffer-substring
  6729. (previous-single-property-change pos 'org-linked-text)
  6730. (next-single-property-change pos 'org-linked-text)))
  6731. (throw 'match t))
  6732. (save-excursion
  6733. (when (or (org-in-regexp org-angle-link-re)
  6734. (org-in-regexp org-plain-link-re))
  6735. (setq type (match-string 1) path (match-string 2))
  6736. (throw 'match t)))
  6737. (save-excursion
  6738. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6739. (setq type "tags"
  6740. path (match-string 1))
  6741. (while (string-match ":" path)
  6742. (setq path (replace-match "+" t t path)))
  6743. (throw 'match t)))
  6744. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6745. (setq type "tree-match"
  6746. path (match-string 1))
  6747. (throw 'match t)))
  6748. (unless path
  6749. (error "No link found"))
  6750. ;; Remove any trailing spaces in path
  6751. (if (string-match " +\\'" path)
  6752. (setq path (replace-match "" t t path)))
  6753. (if (and org-link-translation-function
  6754. (fboundp org-link-translation-function))
  6755. ;; Check if we need to translate the link
  6756. (let ((tmp (funcall org-link-translation-function type path)))
  6757. (setq type (car tmp) path (cdr tmp))))
  6758. (cond
  6759. ((assoc type org-link-protocols)
  6760. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6761. ((equal type "mailto")
  6762. (let ((cmd (car org-link-mailto-program))
  6763. (args (cdr org-link-mailto-program)) args1
  6764. (address path) (subject "") a)
  6765. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6766. (setq address (match-string 1 path)
  6767. subject (org-link-escape (match-string 2 path))))
  6768. (while args
  6769. (cond
  6770. ((not (stringp (car args))) (push (pop args) args1))
  6771. (t (setq a (pop args))
  6772. (if (string-match "%a" a)
  6773. (setq a (replace-match address t t a)))
  6774. (if (string-match "%s" a)
  6775. (setq a (replace-match subject t t a)))
  6776. (push a args1))))
  6777. (apply cmd (nreverse args1))))
  6778. ((member type '("http" "https" "ftp" "news"))
  6779. (browse-url (concat type ":" (org-link-escape
  6780. path org-link-escape-chars-browser))))
  6781. ((member type '("message"))
  6782. (browse-url (concat type ":" path)))
  6783. ((string= type "tags")
  6784. (org-tags-view in-emacs path))
  6785. ((string= type "thisfile")
  6786. (if in-emacs
  6787. (switch-to-buffer-other-window
  6788. (org-get-buffer-for-internal-link (current-buffer)))
  6789. (org-mark-ring-push))
  6790. (let ((cmd `(org-link-search
  6791. ,path
  6792. ,(cond ((equal in-emacs '(4)) 'occur)
  6793. ((equal in-emacs '(16)) 'org-occur)
  6794. (t nil))
  6795. ,pos)))
  6796. (condition-case nil (eval cmd)
  6797. (error (progn (widen) (eval cmd))))))
  6798. ((string= type "tree-match")
  6799. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6800. ((string= type "file")
  6801. (if (string-match "::\\([0-9]+\\)\\'" path)
  6802. (setq line (string-to-number (match-string 1 path))
  6803. path (substring path 0 (match-beginning 0)))
  6804. (if (string-match "::\\(.+\\)\\'" path)
  6805. (setq search (match-string 1 path)
  6806. path (substring path 0 (match-beginning 0)))))
  6807. (if (string-match "[*?{]" (file-name-nondirectory path))
  6808. (dired path)
  6809. (org-open-file path in-emacs line search)))
  6810. ((string= type "news")
  6811. (require 'org-gnus)
  6812. (org-gnus-follow-link path))
  6813. ((string= type "shell")
  6814. (let ((cmd path))
  6815. (if (or (not org-confirm-shell-link-function)
  6816. (funcall org-confirm-shell-link-function
  6817. (format "Execute \"%s\" in shell? "
  6818. (org-add-props cmd nil
  6819. 'face 'org-warning))))
  6820. (progn
  6821. (message "Executing %s" cmd)
  6822. (shell-command cmd))
  6823. (error "Abort"))))
  6824. ((string= type "elisp")
  6825. (let ((cmd path))
  6826. (if (or (not org-confirm-elisp-link-function)
  6827. (funcall org-confirm-elisp-link-function
  6828. (format "Execute \"%s\" as elisp? "
  6829. (org-add-props cmd nil
  6830. 'face 'org-warning))))
  6831. (message "%s => %s" cmd
  6832. (if (equal (string-to-char cmd) ?\()
  6833. (eval (read cmd))
  6834. (call-interactively (read cmd))))
  6835. (error "Abort"))))
  6836. (t
  6837. (browse-url-at-point))))))
  6838. (move-marker org-open-link-marker nil)
  6839. (run-hook-with-args 'org-follow-link-hook))
  6840. ;;;; Time estimates
  6841. (defun org-get-effort (&optional pom)
  6842. "Get the effort estimate for the current entry."
  6843. (org-entry-get pom org-effort-property))
  6844. ;;; File search
  6845. (defvar org-create-file-search-functions nil
  6846. "List of functions to construct the right search string for a file link.
  6847. These functions are called in turn with point at the location to
  6848. which the link should point.
  6849. A function in the hook should first test if it would like to
  6850. handle this file type, for example by checking the major-mode or
  6851. the file extension. If it decides not to handle this file, it
  6852. should just return nil to give other functions a chance. If it
  6853. does handle the file, it must return the search string to be used
  6854. when following the link. The search string will be part of the
  6855. file link, given after a double colon, and `org-open-at-point'
  6856. will automatically search for it. If special measures must be
  6857. taken to make the search successful, another function should be
  6858. added to the companion hook `org-execute-file-search-functions',
  6859. which see.
  6860. A function in this hook may also use `setq' to set the variable
  6861. `description' to provide a suggestion for the descriptive text to
  6862. be used for this link when it gets inserted into an Org-mode
  6863. buffer with \\[org-insert-link].")
  6864. (defvar org-execute-file-search-functions nil
  6865. "List of functions to execute a file search triggered by a link.
  6866. Functions added to this hook must accept a single argument, the
  6867. search string that was part of the file link, the part after the
  6868. double colon. The function must first check if it would like to
  6869. handle this search, for example by checking the major-mode or the
  6870. file extension. If it decides not to handle this search, it
  6871. should just return nil to give other functions a chance. If it
  6872. does handle the search, it must return a non-nil value to keep
  6873. other functions from trying.
  6874. Each function can access the current prefix argument through the
  6875. variable `current-prefix-argument'. Note that a single prefix is
  6876. used to force opening a link in Emacs, so it may be good to only
  6877. use a numeric or double prefix to guide the search function.
  6878. In case this is needed, a function in this hook can also restore
  6879. the window configuration before `org-open-at-point' was called using:
  6880. (set-window-configuration org-window-config-before-follow-link)")
  6881. (defun org-link-search (s &optional type avoid-pos)
  6882. "Search for a link search option.
  6883. If S is surrounded by forward slashes, it is interpreted as a
  6884. regular expression. In org-mode files, this will create an `org-occur'
  6885. sparse tree. In ordinary files, `occur' will be used to list matches.
  6886. If the current buffer is in `dired-mode', grep will be used to search
  6887. in all files. If AVOID-POS is given, ignore matches near that position."
  6888. (let ((case-fold-search t)
  6889. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6890. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6891. (append '(("") (" ") ("\t") ("\n"))
  6892. org-emphasis-alist)
  6893. "\\|") "\\)"))
  6894. (pos (point))
  6895. (pre nil) (post nil)
  6896. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6897. (cond
  6898. ;; First check if there are any special
  6899. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6900. ;; Now try the builtin stuff
  6901. ((save-excursion
  6902. (goto-char (point-min))
  6903. (and
  6904. (re-search-forward
  6905. (concat "<<" (regexp-quote s0) ">>") nil t)
  6906. (setq type 'dedicated
  6907. pos (match-beginning 0))))
  6908. ;; There is an exact target for this
  6909. (goto-char pos))
  6910. ((and (string-match "^(\\(.*\\))$" s0)
  6911. (save-excursion
  6912. (goto-char (point-min))
  6913. (and
  6914. (re-search-forward
  6915. (concat "[^[]" (regexp-quote
  6916. (format org-coderef-label-format
  6917. (match-string 1 s0))))
  6918. nil t)
  6919. (setq type 'dedicated
  6920. pos (1+ (match-beginning 0))))))
  6921. ;; There is a coderef target for this
  6922. (goto-char pos))
  6923. ((string-match "^/\\(.*\\)/$" s)
  6924. ;; A regular expression
  6925. (cond
  6926. ((org-mode-p)
  6927. (org-occur (match-string 1 s)))
  6928. ;;((eq major-mode 'dired-mode)
  6929. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6930. (t (org-do-occur (match-string 1 s)))))
  6931. (t
  6932. ;; A normal search strings
  6933. (when (equal (string-to-char s) ?*)
  6934. ;; Anchor on headlines, post may include tags.
  6935. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6936. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6937. s (substring s 1)))
  6938. (remove-text-properties
  6939. 0 (length s)
  6940. '(face nil mouse-face nil keymap nil fontified nil) s)
  6941. ;; Make a series of regular expressions to find a match
  6942. (setq words (org-split-string s "[ \n\r\t]+")
  6943. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6944. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6945. "\\)" markers)
  6946. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6947. re2a (concat "[ \t\r\n]" re2a_)
  6948. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6949. re4 (concat "[^a-zA-Z_]" re4_)
  6950. re1 (concat pre re2 post)
  6951. re3 (concat pre (if pre re4_ re4) post)
  6952. re5 (concat pre ".*" re4)
  6953. re2 (concat pre re2)
  6954. re2a (concat pre (if pre re2a_ re2a))
  6955. re4 (concat pre (if pre re4_ re4))
  6956. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6957. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6958. re5 "\\)"
  6959. ))
  6960. (cond
  6961. ((eq type 'org-occur) (org-occur reall))
  6962. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6963. (t (goto-char (point-min))
  6964. (setq type 'fuzzy)
  6965. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6966. (org-search-not-self 1 re1 nil t)
  6967. (org-search-not-self 1 re2 nil t)
  6968. (org-search-not-self 1 re2a nil t)
  6969. (org-search-not-self 1 re3 nil t)
  6970. (org-search-not-self 1 re4 nil t)
  6971. (org-search-not-self 1 re5 nil t)
  6972. )
  6973. (goto-char (match-beginning 1))
  6974. (goto-char pos)
  6975. (error "No match")))))
  6976. (t
  6977. ;; Normal string-search
  6978. (goto-char (point-min))
  6979. (if (search-forward s nil t)
  6980. (goto-char (match-beginning 0))
  6981. (error "No match"))))
  6982. (and (org-mode-p) (org-show-context 'link-search))
  6983. type))
  6984. (defun org-search-not-self (group &rest args)
  6985. "Execute `re-search-forward', but only accept matches that do not
  6986. enclose the position of `org-open-link-marker'."
  6987. (let ((m org-open-link-marker))
  6988. (catch 'exit
  6989. (while (apply 're-search-forward args)
  6990. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6991. (goto-char (match-end group))
  6992. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6993. (> (match-beginning 0) (marker-position m))
  6994. (< (match-end 0) (marker-position m)))
  6995. (save-match-data
  6996. (or (not (org-in-regexp
  6997. org-bracket-link-analytic-regexp 1))
  6998. (not (match-end 4)) ; no description
  6999. (and (<= (match-beginning 4) (point))
  7000. (>= (match-end 4) (point))))))
  7001. (throw 'exit (point))))))))
  7002. (defun org-get-buffer-for-internal-link (buffer)
  7003. "Return a buffer to be used for displaying the link target of internal links."
  7004. (cond
  7005. ((not org-display-internal-link-with-indirect-buffer)
  7006. buffer)
  7007. ((string-match "(Clone)$" (buffer-name buffer))
  7008. (message "Buffer is already a clone, not making another one")
  7009. ;; we also do not modify visibility in this case
  7010. buffer)
  7011. (t ; make a new indirect buffer for displaying the link
  7012. (let* ((bn (buffer-name buffer))
  7013. (ibn (concat bn "(Clone)"))
  7014. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7015. (with-current-buffer ib (org-overview))
  7016. ib))))
  7017. (defun org-do-occur (regexp &optional cleanup)
  7018. "Call the Emacs command `occur'.
  7019. If CLEANUP is non-nil, remove the printout of the regular expression
  7020. in the *Occur* buffer. This is useful if the regex is long and not useful
  7021. to read."
  7022. (occur regexp)
  7023. (when cleanup
  7024. (let ((cwin (selected-window)) win beg end)
  7025. (when (setq win (get-buffer-window "*Occur*"))
  7026. (select-window win))
  7027. (goto-char (point-min))
  7028. (when (re-search-forward "match[a-z]+" nil t)
  7029. (setq beg (match-end 0))
  7030. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7031. (setq end (1- (match-beginning 0)))))
  7032. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7033. (goto-char (point-min))
  7034. (select-window cwin))))
  7035. ;;; The mark ring for links jumps
  7036. (defvar org-mark-ring nil
  7037. "Mark ring for positions before jumps in Org-mode.")
  7038. (defvar org-mark-ring-last-goto nil
  7039. "Last position in the mark ring used to go back.")
  7040. ;; Fill and close the ring
  7041. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7042. (loop for i from 1 to org-mark-ring-length do
  7043. (push (make-marker) org-mark-ring))
  7044. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7045. org-mark-ring)
  7046. (defun org-mark-ring-push (&optional pos buffer)
  7047. "Put the current position or POS into the mark ring and rotate it."
  7048. (interactive)
  7049. (setq pos (or pos (point)))
  7050. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7051. (move-marker (car org-mark-ring)
  7052. (or pos (point))
  7053. (or buffer (current-buffer)))
  7054. (message "%s"
  7055. (substitute-command-keys
  7056. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7057. (defun org-mark-ring-goto (&optional n)
  7058. "Jump to the previous position in the mark ring.
  7059. With prefix arg N, jump back that many stored positions. When
  7060. called several times in succession, walk through the entire ring.
  7061. Org-mode commands jumping to a different position in the current file,
  7062. or to another Org-mode file, automatically push the old position
  7063. onto the ring."
  7064. (interactive "p")
  7065. (let (p m)
  7066. (if (eq last-command this-command)
  7067. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7068. (setq p org-mark-ring))
  7069. (setq org-mark-ring-last-goto p)
  7070. (setq m (car p))
  7071. (switch-to-buffer (marker-buffer m))
  7072. (goto-char m)
  7073. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7074. (defun org-remove-angle-brackets (s)
  7075. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7076. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7077. s)
  7078. (defun org-add-angle-brackets (s)
  7079. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7080. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7081. s)
  7082. (defun org-remove-double-quotes (s)
  7083. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7084. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7085. s)
  7086. ;;; Following specific links
  7087. (defun org-follow-timestamp-link ()
  7088. (cond
  7089. ((org-at-date-range-p t)
  7090. (let ((org-agenda-start-on-weekday)
  7091. (t1 (match-string 1))
  7092. (t2 (match-string 2)))
  7093. (setq t1 (time-to-days (org-time-string-to-time t1))
  7094. t2 (time-to-days (org-time-string-to-time t2)))
  7095. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7096. ((org-at-timestamp-p t)
  7097. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7098. (substring (match-string 1) 0 10)))
  7099. 1))
  7100. (t (error "This should not happen"))))
  7101. ;;; Following file links
  7102. (defvar org-wait nil)
  7103. (defun org-open-file (path &optional in-emacs line search)
  7104. "Open the file at PATH.
  7105. First, this expands any special file name abbreviations. Then the
  7106. configuration variable `org-file-apps' is checked if it contains an
  7107. entry for this file type, and if yes, the corresponding command is launched.
  7108. If no application is found, Emacs simply visits the file.
  7109. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7110. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7111. and o use an external application to visit the file.
  7112. Optional LINE specifies a line to go to, optional SEARCH a string to
  7113. search for. If LINE or SEARCH is given, the file will always be
  7114. opened in Emacs.
  7115. If the file does not exist, an error is thrown."
  7116. (setq in-emacs (or in-emacs line search))
  7117. (let* ((file (if (equal path "")
  7118. buffer-file-name
  7119. (substitute-in-file-name (expand-file-name path))))
  7120. (apps (append org-file-apps (org-default-apps)))
  7121. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7122. (dirp (if remp nil (file-directory-p file)))
  7123. (file (if (and dirp org-open-directory-means-index-dot-org)
  7124. (concat (file-name-as-directory file) "index.org")
  7125. file))
  7126. (a-m-a-p (assq 'auto-mode apps))
  7127. (dfile (downcase file))
  7128. (old-buffer (current-buffer))
  7129. (old-pos (point))
  7130. (old-mode major-mode)
  7131. ext cmd)
  7132. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7133. (setq ext (match-string 1 dfile))
  7134. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7135. (setq ext (match-string 1 dfile))))
  7136. (cond
  7137. ((equal in-emacs '(16))
  7138. (setq cmd (cdr (assoc 'system apps))))
  7139. (in-emacs (setq cmd 'emacs))
  7140. (t
  7141. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7142. (and dirp (cdr (assoc 'directory apps)))
  7143. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7144. 'string-match)
  7145. (cdr (assoc ext apps))
  7146. (cdr (assoc t apps))))))
  7147. (when (eq cmd 'system)
  7148. (setq cmd (cdr (assoc 'system apps))))
  7149. (when (eq cmd 'default)
  7150. (setq cmd (cdr (assoc t apps))))
  7151. (when (eq cmd 'mailcap)
  7152. (require 'mailcap)
  7153. (mailcap-parse-mailcaps)
  7154. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7155. (command (mailcap-mime-info mime-type)))
  7156. (if (stringp command)
  7157. (setq cmd command)
  7158. (setq cmd 'emacs))))
  7159. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7160. (not (file-exists-p file))
  7161. (not org-open-non-existing-files))
  7162. (error "No such file: %s" file))
  7163. (cond
  7164. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7165. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7166. (while (string-match "['\"]%s['\"]" cmd)
  7167. (setq cmd (replace-match "%s" t t cmd)))
  7168. (while (string-match "%s" cmd)
  7169. (setq cmd (replace-match
  7170. (save-match-data
  7171. (shell-quote-argument
  7172. (convert-standard-filename file)))
  7173. t t cmd)))
  7174. (save-window-excursion
  7175. (start-process-shell-command cmd nil cmd)
  7176. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7177. ))
  7178. ((or (stringp cmd)
  7179. (eq cmd 'emacs))
  7180. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7181. (widen)
  7182. (if line (goto-line line)
  7183. (if search (org-link-search search))))
  7184. ((consp cmd)
  7185. (let ((file (convert-standard-filename file)))
  7186. (eval cmd)))
  7187. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7188. (and (org-mode-p) (eq old-mode 'org-mode)
  7189. (or (not (equal old-buffer (current-buffer)))
  7190. (not (equal old-pos (point))))
  7191. (org-mark-ring-push old-pos old-buffer))))
  7192. (defun org-default-apps ()
  7193. "Return the default applications for this operating system."
  7194. (cond
  7195. ((eq system-type 'darwin)
  7196. org-file-apps-defaults-macosx)
  7197. ((eq system-type 'windows-nt)
  7198. org-file-apps-defaults-windowsnt)
  7199. (t org-file-apps-defaults-gnu)))
  7200. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7201. "Convert extensions to regular expressions in the cars of LIST.
  7202. Also, weed out any non-string entries, because the return value is used
  7203. only for regexp matching.
  7204. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7205. point to the symbol `emacs', indicating that the file should
  7206. be opened in Emacs."
  7207. (append
  7208. (delq nil
  7209. (mapcar (lambda (x)
  7210. (if (not (stringp (car x)))
  7211. nil
  7212. (if (string-match "\\W" (car x))
  7213. x
  7214. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7215. list))
  7216. (if add-auto-mode
  7217. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7218. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7219. (defun org-file-remote-p (file)
  7220. "Test whether FILE specifies a location on a remote system.
  7221. Return non-nil if the location is indeed remote.
  7222. For example, the filename \"/user@host:/foo\" specifies a location
  7223. on the system \"/user@host:\"."
  7224. (cond ((fboundp 'file-remote-p)
  7225. (file-remote-p file))
  7226. ((fboundp 'tramp-handle-file-remote-p)
  7227. (tramp-handle-file-remote-p file))
  7228. ((and (boundp 'ange-ftp-name-format)
  7229. (string-match (car ange-ftp-name-format) file))
  7230. t)
  7231. (t nil)))
  7232. ;;;; Refiling
  7233. (defun org-get-org-file ()
  7234. "Read a filename, with default directory `org-directory'."
  7235. (let ((default (or org-default-notes-file remember-data-file)))
  7236. (read-file-name (format "File name [%s]: " default)
  7237. (file-name-as-directory org-directory)
  7238. default)))
  7239. (defun org-notes-order-reversed-p ()
  7240. "Check if the current file should receive notes in reversed order."
  7241. (cond
  7242. ((not org-reverse-note-order) nil)
  7243. ((eq t org-reverse-note-order) t)
  7244. ((not (listp org-reverse-note-order)) nil)
  7245. (t (catch 'exit
  7246. (let ((all org-reverse-note-order)
  7247. entry)
  7248. (while (setq entry (pop all))
  7249. (if (string-match (car entry) buffer-file-name)
  7250. (throw 'exit (cdr entry))))
  7251. nil)))))
  7252. (defvar org-refile-target-table nil
  7253. "The list of refile targets, created by `org-refile'.")
  7254. (defvar org-agenda-new-buffers nil
  7255. "Buffers created to visit agenda files.")
  7256. (defun org-get-refile-targets (&optional default-buffer)
  7257. "Produce a table with refile targets."
  7258. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7259. targets txt re files f desc descre fast-path-p level)
  7260. (message "Getting targets...")
  7261. (with-current-buffer (or default-buffer (current-buffer))
  7262. (while (setq entry (pop entries))
  7263. (setq files (car entry) desc (cdr entry))
  7264. (setq fast-path-p nil)
  7265. (cond
  7266. ((null files) (setq files (list (current-buffer))))
  7267. ((eq files 'org-agenda-files)
  7268. (setq files (org-agenda-files 'unrestricted)))
  7269. ((and (symbolp files) (fboundp files))
  7270. (setq files (funcall files)))
  7271. ((and (symbolp files) (boundp files))
  7272. (setq files (symbol-value files))))
  7273. (if (stringp files) (setq files (list files)))
  7274. (cond
  7275. ((eq (car desc) :tag)
  7276. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7277. ((eq (car desc) :todo)
  7278. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7279. ((eq (car desc) :regexp)
  7280. (setq descre (cdr desc)))
  7281. ((eq (car desc) :level)
  7282. (setq descre (concat "^\\*\\{" (number-to-string
  7283. (if org-odd-levels-only
  7284. (1- (* 2 (cdr desc)))
  7285. (cdr desc)))
  7286. "\\}[ \t]")))
  7287. ((eq (car desc) :maxlevel)
  7288. (setq fast-path-p t)
  7289. (setq descre (concat "^\\*\\{1," (number-to-string
  7290. (if org-odd-levels-only
  7291. (1- (* 2 (cdr desc)))
  7292. (cdr desc)))
  7293. "\\}[ \t]")))
  7294. (t (error "Bad refiling target description %s" desc)))
  7295. (while (setq f (pop files))
  7296. (save-excursion
  7297. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7298. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7299. (setq f (expand-file-name f))
  7300. (save-excursion
  7301. (save-restriction
  7302. (widen)
  7303. (goto-char (point-min))
  7304. (while (re-search-forward descre nil t)
  7305. (goto-char (point-at-bol))
  7306. (when (looking-at org-complex-heading-regexp)
  7307. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7308. txt (org-link-display-format (match-string 4))
  7309. re (concat "^" (regexp-quote
  7310. (buffer-substring (match-beginning 1)
  7311. (match-end 4)))))
  7312. (if (match-end 5) (setq re (concat re "[ \t]+"
  7313. (regexp-quote
  7314. (match-string 5)))))
  7315. (setq re (concat re "[ \t]*$"))
  7316. (when org-refile-use-outline-path
  7317. (setq txt (mapconcat 'org-protect-slash
  7318. (append
  7319. (if (eq org-refile-use-outline-path 'file)
  7320. (list (file-name-nondirectory
  7321. (buffer-file-name (buffer-base-buffer))))
  7322. (if (eq org-refile-use-outline-path 'full-file-path)
  7323. (list (buffer-file-name (buffer-base-buffer)))))
  7324. (org-get-outline-path fast-path-p level txt)
  7325. (list txt))
  7326. "/")))
  7327. (push (list txt f re (point)) targets))
  7328. (goto-char (point-at-eol))))))))
  7329. (message "Getting targets...done")
  7330. (nreverse targets))))
  7331. (defun org-protect-slash (s)
  7332. (while (string-match "/" s)
  7333. (setq s (replace-match "\\" t t s)))
  7334. s)
  7335. (defvar org-olpa (make-vector 20 nil))
  7336. (defun org-get-outline-path (&optional fastp level heading)
  7337. "Return the outline path to the current entry, as a list."
  7338. (if fastp
  7339. (progn
  7340. (if (> level 19)
  7341. (error "Outline path failure, more than 19 levels."))
  7342. (loop for i from level upto 19 do
  7343. (aset org-olpa i nil))
  7344. (prog1
  7345. (delq nil (append org-olpa nil))
  7346. (aset org-olpa level heading)))
  7347. (let (rtn)
  7348. (save-excursion
  7349. (while (org-up-heading-safe)
  7350. (when (looking-at org-complex-heading-regexp)
  7351. (push (org-match-string-no-properties 4) rtn)))
  7352. rtn))))
  7353. (defvar org-refile-history nil
  7354. "History for refiling operations.")
  7355. (defun org-refile (&optional goto default-buffer)
  7356. "Move the entry at point to another heading.
  7357. The list of target headings is compiled using the information in
  7358. `org-refile-targets', which see. This list is created before each use
  7359. and will therefore always be up-to-date.
  7360. At the target location, the entry is filed as a subitem of the target heading.
  7361. Depending on `org-reverse-note-order', the new subitem will either be the
  7362. first or the last subitem.
  7363. If there is an active region, all entries in that region will be moved.
  7364. However, the region must fulfil the requirement that the first heading
  7365. is the first one sets the top-level of the moved text - at most siblings
  7366. below it are allowed.
  7367. With prefix arg GOTO, the command will only visit the target location,
  7368. not actually move anything.
  7369. With a double prefix `C-u C-u', go to the location where the last refiling
  7370. operation has put the subtree."
  7371. (interactive "P")
  7372. (let* ((cbuf (current-buffer))
  7373. (regionp (org-region-active-p))
  7374. (region-start (and regionp (region-beginning)))
  7375. (region-end (and regionp (region-end)))
  7376. (region-length (and regionp (- region-end region-start)))
  7377. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7378. pos it nbuf file re level reversed)
  7379. (when regionp (goto-char region-start)
  7380. (unless (org-kill-is-subtree-p
  7381. (buffer-substring region-start region-end))
  7382. (error "The region is not a (sequence of) subtree(s)")))
  7383. (if (equal goto '(16))
  7384. (org-refile-goto-last-stored)
  7385. (when (setq it (org-refile-get-location
  7386. (if goto "Goto: " "Refile to: ") default-buffer))
  7387. (setq file (nth 1 it)
  7388. re (nth 2 it)
  7389. pos (nth 3 it))
  7390. (if (and (not goto)
  7391. (equal (buffer-file-name) file)
  7392. (if regionp
  7393. (and (>= pos region-start)
  7394. (<= pos region-end))
  7395. (and (>= pos (point))
  7396. (< pos (save-excursion
  7397. (org-end-of-subtree t t))))))
  7398. (error "Cannot refile to position inside the tree or region"))
  7399. (setq nbuf (or (find-buffer-visiting file)
  7400. (find-file-noselect file)))
  7401. (if goto
  7402. (progn
  7403. (switch-to-buffer nbuf)
  7404. (goto-char pos)
  7405. (org-show-context 'org-goto))
  7406. (if regionp
  7407. (progn
  7408. (kill-new (buffer-substring region-start region-end))
  7409. (org-save-markers-in-region region-start region-end))
  7410. (org-copy-subtree 1 nil t))
  7411. (save-excursion
  7412. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7413. (find-file-noselect file))))
  7414. (setq reversed (org-notes-order-reversed-p))
  7415. (save-excursion
  7416. (save-restriction
  7417. (widen)
  7418. (goto-char pos)
  7419. (looking-at outline-regexp)
  7420. (setq level (org-get-valid-level (funcall outline-level) 1))
  7421. (goto-char
  7422. (if reversed
  7423. (or (outline-next-heading) (point-max))
  7424. (or (save-excursion (outline-get-next-sibling))
  7425. (org-end-of-subtree t t)
  7426. (point-max))))
  7427. (if (not (bolp)) (newline))
  7428. (bookmark-set "org-refile-last-stored")
  7429. (org-paste-subtree level))))
  7430. (if regionp
  7431. (delete-region (point) (+ (point) region-length))
  7432. (org-cut-subtree))
  7433. (setq org-markers-to-move nil)
  7434. (message "Refiled to \"%s\"" (car it)))))))
  7435. (defun org-refile-goto-last-stored ()
  7436. "Go to the location where the last refile was stored."
  7437. (interactive)
  7438. (bookmark-jump "org-refile-last-stored")
  7439. (message "This is the location of the last refile"))
  7440. (defun org-refile-get-location (&optional prompt default-buffer)
  7441. "Prompt the user for a refile location, using PROMPT."
  7442. (let ((org-refile-targets org-refile-targets)
  7443. (org-refile-use-outline-path org-refile-use-outline-path))
  7444. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7445. (unless org-refile-target-table
  7446. (error "No refile targets"))
  7447. (let* ((cbuf (current-buffer))
  7448. (partial-completion-mode nil)
  7449. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7450. (cfunc (if (and org-refile-use-outline-path
  7451. org-outline-path-complete-in-steps)
  7452. 'org-olpath-completing-read
  7453. 'org-ido-completing-read))
  7454. (extra (if org-refile-use-outline-path "/" ""))
  7455. (filename (and cfn (expand-file-name cfn)))
  7456. (tbl (mapcar
  7457. (lambda (x)
  7458. (if (not (equal filename (nth 1 x)))
  7459. (cons (concat (car x) extra " ("
  7460. (file-name-nondirectory (nth 1 x)) ")")
  7461. (cdr x))
  7462. (cons (concat (car x) extra) (cdr x))))
  7463. org-refile-target-table))
  7464. (completion-ignore-case t))
  7465. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7466. tbl)))
  7467. (defun org-olpath-completing-read (prompt collection &rest args)
  7468. "Read an outline path like a file name."
  7469. (let ((thetable collection)
  7470. (org-completion-use-ido nil)) ; does not work with ido.
  7471. (apply
  7472. 'org-ido-completing-read prompt
  7473. (lambda (string predicate &optional flag)
  7474. (let (rtn r f (l (length string)))
  7475. (cond
  7476. ((eq flag nil)
  7477. ;; try completion
  7478. (try-completion string thetable))
  7479. ((eq flag t)
  7480. ;; all-completions
  7481. (setq rtn (all-completions string thetable predicate))
  7482. (mapcar
  7483. (lambda (x)
  7484. (setq r (substring x l))
  7485. (if (string-match " ([^)]*)$" x)
  7486. (setq f (match-string 0 x))
  7487. (setq f ""))
  7488. (if (string-match "/" r)
  7489. (concat string (substring r 0 (match-end 0)) f)
  7490. x))
  7491. rtn))
  7492. ((eq flag 'lambda)
  7493. ;; exact match?
  7494. (assoc string thetable)))
  7495. ))
  7496. args)))
  7497. ;;;; Dynamic blocks
  7498. (defun org-find-dblock (name)
  7499. "Find the first dynamic block with name NAME in the buffer.
  7500. If not found, stay at current position and return nil."
  7501. (let (pos)
  7502. (save-excursion
  7503. (goto-char (point-min))
  7504. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7505. nil t)
  7506. (match-beginning 0))))
  7507. (if pos (goto-char pos))
  7508. pos))
  7509. (defconst org-dblock-start-re
  7510. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7511. "Matches the startline of a dynamic block, with parameters.")
  7512. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7513. "Matches the end of a dynamic block.")
  7514. (defun org-create-dblock (plist)
  7515. "Create a dynamic block section, with parameters taken from PLIST.
  7516. PLIST must contain a :name entry which is used as name of the block."
  7517. (unless (bolp) (newline))
  7518. (let ((name (plist-get plist :name)))
  7519. (insert "#+BEGIN: " name)
  7520. (while plist
  7521. (if (eq (car plist) :name)
  7522. (setq plist (cddr plist))
  7523. (insert " " (prin1-to-string (pop plist)))))
  7524. (insert "\n\n#+END:\n")
  7525. (beginning-of-line -2)))
  7526. (defun org-prepare-dblock ()
  7527. "Prepare dynamic block for refresh.
  7528. This empties the block, puts the cursor at the insert position and returns
  7529. the property list including an extra property :name with the block name."
  7530. (unless (looking-at org-dblock-start-re)
  7531. (error "Not at a dynamic block"))
  7532. (let* ((begdel (1+ (match-end 0)))
  7533. (name (org-no-properties (match-string 1)))
  7534. (params (append (list :name name)
  7535. (read (concat "(" (match-string 3) ")")))))
  7536. (unless (re-search-forward org-dblock-end-re nil t)
  7537. (error "Dynamic block not terminated"))
  7538. (setq params
  7539. (append params
  7540. (list :content (buffer-substring
  7541. begdel (match-beginning 0)))))
  7542. (delete-region begdel (match-beginning 0))
  7543. (goto-char begdel)
  7544. (open-line 1)
  7545. params))
  7546. (defun org-map-dblocks (&optional command)
  7547. "Apply COMMAND to all dynamic blocks in the current buffer.
  7548. If COMMAND is not given, use `org-update-dblock'."
  7549. (let ((cmd (or command 'org-update-dblock))
  7550. pos)
  7551. (save-excursion
  7552. (goto-char (point-min))
  7553. (while (re-search-forward org-dblock-start-re nil t)
  7554. (goto-char (setq pos (match-beginning 0)))
  7555. (condition-case nil
  7556. (funcall cmd)
  7557. (error (message "Error during update of dynamic block")))
  7558. (goto-char pos)
  7559. (unless (re-search-forward org-dblock-end-re nil t)
  7560. (error "Dynamic block not terminated"))))))
  7561. (defun org-dblock-update (&optional arg)
  7562. "User command for updating dynamic blocks.
  7563. Update the dynamic block at point. With prefix ARG, update all dynamic
  7564. blocks in the buffer."
  7565. (interactive "P")
  7566. (if arg
  7567. (org-update-all-dblocks)
  7568. (or (looking-at org-dblock-start-re)
  7569. (org-beginning-of-dblock))
  7570. (org-update-dblock)))
  7571. (defun org-update-dblock ()
  7572. "Update the dynamic block at point
  7573. This means to empty the block, parse for parameters and then call
  7574. the correct writing function."
  7575. (save-window-excursion
  7576. (let* ((pos (point))
  7577. (line (org-current-line))
  7578. (params (org-prepare-dblock))
  7579. (name (plist-get params :name))
  7580. (cmd (intern (concat "org-dblock-write:" name))))
  7581. (message "Updating dynamic block `%s' at line %d..." name line)
  7582. (funcall cmd params)
  7583. (message "Updating dynamic block `%s' at line %d...done" name line)
  7584. (goto-char pos))))
  7585. (defun org-beginning-of-dblock ()
  7586. "Find the beginning of the dynamic block at point.
  7587. Error if there is no such block at point."
  7588. (let ((pos (point))
  7589. beg)
  7590. (end-of-line 1)
  7591. (if (and (re-search-backward org-dblock-start-re nil t)
  7592. (setq beg (match-beginning 0))
  7593. (re-search-forward org-dblock-end-re nil t)
  7594. (> (match-end 0) pos))
  7595. (goto-char beg)
  7596. (goto-char pos)
  7597. (error "Not in a dynamic block"))))
  7598. (defun org-update-all-dblocks ()
  7599. "Update all dynamic blocks in the buffer.
  7600. This function can be used in a hook."
  7601. (when (org-mode-p)
  7602. (org-map-dblocks 'org-update-dblock)))
  7603. ;;;; Completion
  7604. (defconst org-additional-option-like-keywords
  7605. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7606. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7607. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7608. "BEGIN_QUOTE" "END_QUOTE"
  7609. "BEGIN_VERSE" "END_VERSE"
  7610. "BEGIN_CENTER" "END_CENTER"
  7611. "BEGIN_SRC" "END_SRC"
  7612. "CATEGORY" "COLUMNS"
  7613. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7614. (defcustom org-structure-template-alist
  7615. '(
  7616. ("s" "#+begin_src ?\n\n#+end_src"
  7617. "<src lang=\"?\">\n\n</src>")
  7618. ("e" "#+begin_example\n?\n#+end_example"
  7619. "<example>\n?\n</example>")
  7620. ("q" "#+begin_quote\n?\n#+end_quote"
  7621. "<quote>\n?\n</quote>")
  7622. ("v" "#+begin_verse\n?\n#+end_verse"
  7623. "<verse>\n?\n/verse>")
  7624. ("c" "#+begin_center\n?\n#+end_center"
  7625. "<center>\n?\n/center>")
  7626. ("l" "#+begin_latex\n?\n#+end_latex"
  7627. "<literal style=\"latex\">\n?\n</literal>")
  7628. ("L" "#+latex: "
  7629. "<literal style=\"latex\">?</literal>")
  7630. ("h" "#+begin_html\n?\n#+end_html"
  7631. "<literal style=\"html\">\n?\n</literal>")
  7632. ("H" "#+html: "
  7633. "<literal style=\"html\">?</literal>")
  7634. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7635. ("A" "#+ascii: ")
  7636. ("i" "#+include %file ?"
  7637. "<include file=%file markup=\"?\">")
  7638. )
  7639. "Structure completion elements.
  7640. This is a list of abbreviation keys and values. The value gets inserted
  7641. it you type @samp{.} followed by the key and then the completion key,
  7642. usually `M-TAB'. %file will be replaced by a file name after prompting
  7643. for the file using completion.
  7644. There are two templates for each key, the first uses the original Org syntax,
  7645. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7646. the default when the /org-mtags.el/ module has been loaded. See also the
  7647. variable `org-mtags-prefer-muse-templates'.
  7648. This is an experimental feature, it is undecided if it is going to stay in."
  7649. :group 'org-completion
  7650. :type '(repeat
  7651. (string :tag "Key")
  7652. (string :tag "Template")
  7653. (string :tag "Muse Template")))
  7654. (defun org-try-structure-completion ()
  7655. "Try to complete a structure template before point.
  7656. This looks for strings like \"<e\" on an otherwise empty line and
  7657. expands them."
  7658. (let ((l (buffer-substring (point-at-bol) (point)))
  7659. a)
  7660. (when (and (looking-at "[ \t]*$")
  7661. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7662. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7663. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7664. (match-beginning 1)) a)
  7665. t)))
  7666. (defun org-complete-expand-structure-template (start cell)
  7667. "Expand a structure template."
  7668. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7669. (rpl (nth (if musep 2 1) cell)))
  7670. (delete-region start (point))
  7671. (when (string-match "\\`#\\+" rpl)
  7672. (cond
  7673. ((bolp))
  7674. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7675. (delete-region (point-at-bol) (point)))
  7676. (t (newline))))
  7677. (setq start (point))
  7678. (if (string-match "%file" rpl)
  7679. (setq rpl (replace-match
  7680. (concat
  7681. "\""
  7682. (save-match-data
  7683. (abbreviate-file-name (read-file-name "Include file: ")))
  7684. "\"")
  7685. t t rpl)))
  7686. (insert rpl)
  7687. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7688. (defun org-complete (&optional arg)
  7689. "Perform completion on word at point.
  7690. At the beginning of a headline, this completes TODO keywords as given in
  7691. `org-todo-keywords'.
  7692. If the current word is preceded by a backslash, completes the TeX symbols
  7693. that are supported for HTML support.
  7694. If the current word is preceded by \"#+\", completes special words for
  7695. setting file options.
  7696. In the line after \"#+STARTUP:, complete valid keywords.\"
  7697. At all other locations, this simply calls the value of
  7698. `org-completion-fallback-command'."
  7699. (interactive "P")
  7700. (org-without-partial-completion
  7701. (catch 'exit
  7702. (let* ((a nil)
  7703. (end (point))
  7704. (beg1 (save-excursion
  7705. (skip-chars-backward (org-re "[:alnum:]_@"))
  7706. (point)))
  7707. (beg (save-excursion
  7708. (skip-chars-backward "a-zA-Z0-9_:$")
  7709. (point)))
  7710. (confirm (lambda (x) (stringp (car x))))
  7711. (searchhead (equal (char-before beg) ?*))
  7712. (struct
  7713. (when (and (member (char-before beg1) '(?. ?<))
  7714. (setq a (assoc (buffer-substring beg1 (point))
  7715. org-structure-template-alist)))
  7716. (org-complete-expand-structure-template (1- beg1) a)
  7717. (throw 'exit t)))
  7718. (tag (and (equal (char-before beg1) ?:)
  7719. (equal (char-after (point-at-bol)) ?*)))
  7720. (prop (and (equal (char-before beg1) ?:)
  7721. (not (equal (char-after (point-at-bol)) ?*))))
  7722. (texp (equal (char-before beg) ?\\))
  7723. (link (equal (char-before beg) ?\[))
  7724. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7725. beg)
  7726. "#+"))
  7727. (startup (string-match "^#\\+STARTUP:.*"
  7728. (buffer-substring (point-at-bol) (point))))
  7729. (completion-ignore-case opt)
  7730. (type nil)
  7731. (tbl nil)
  7732. (table (cond
  7733. (opt
  7734. (setq type :opt)
  7735. (require 'org-exp)
  7736. (append
  7737. (mapcar
  7738. (lambda (x)
  7739. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7740. (cons (match-string 2 x) (match-string 1 x)))
  7741. (org-split-string (org-get-current-options) "\n"))
  7742. (mapcar 'list org-additional-option-like-keywords)))
  7743. (startup
  7744. (setq type :startup)
  7745. org-startup-options)
  7746. (link (append org-link-abbrev-alist-local
  7747. org-link-abbrev-alist))
  7748. (texp
  7749. (setq type :tex)
  7750. org-html-entities)
  7751. ((string-match "\\`\\*+[ \t]+\\'"
  7752. (buffer-substring (point-at-bol) beg))
  7753. (setq type :todo)
  7754. (mapcar 'list org-todo-keywords-1))
  7755. (searchhead
  7756. (setq type :searchhead)
  7757. (save-excursion
  7758. (goto-char (point-min))
  7759. (while (re-search-forward org-todo-line-regexp nil t)
  7760. (push (list
  7761. (org-make-org-heading-search-string
  7762. (match-string 3) t))
  7763. tbl)))
  7764. tbl)
  7765. (tag (setq type :tag beg beg1)
  7766. (or org-tag-alist (org-get-buffer-tags)))
  7767. (prop (setq type :prop beg beg1)
  7768. (mapcar 'list (org-buffer-property-keys nil t t)))
  7769. (t (progn
  7770. (call-interactively org-completion-fallback-command)
  7771. (throw 'exit nil)))))
  7772. (pattern (buffer-substring-no-properties beg end))
  7773. (completion (try-completion pattern table confirm)))
  7774. (cond ((eq completion t)
  7775. (if (not (assoc (upcase pattern) table))
  7776. (message "Already complete")
  7777. (if (and (equal type :opt)
  7778. (not (member (car (assoc (upcase pattern) table))
  7779. org-additional-option-like-keywords)))
  7780. (insert (substring (cdr (assoc (upcase pattern) table))
  7781. (length pattern)))
  7782. (if (memq type '(:tag :prop)) (insert ":")))))
  7783. ((null completion)
  7784. (message "Can't find completion for \"%s\"" pattern)
  7785. (ding))
  7786. ((not (string= pattern completion))
  7787. (delete-region beg end)
  7788. (if (string-match " +$" completion)
  7789. (setq completion (replace-match "" t t completion)))
  7790. (insert completion)
  7791. (if (get-buffer-window "*Completions*")
  7792. (delete-window (get-buffer-window "*Completions*")))
  7793. (if (assoc completion table)
  7794. (if (eq type :todo) (insert " ")
  7795. (if (memq type '(:tag :prop)) (insert ":"))))
  7796. (if (and (equal type :opt) (assoc completion table))
  7797. (message "%s" (substitute-command-keys
  7798. "Press \\[org-complete] again to insert example settings"))))
  7799. (t
  7800. (message "Making completion list...")
  7801. (let ((list (sort (all-completions pattern table confirm)
  7802. 'string<)))
  7803. (with-output-to-temp-buffer "*Completions*"
  7804. (condition-case nil
  7805. ;; Protection needed for XEmacs and emacs 21
  7806. (display-completion-list list pattern)
  7807. (error (display-completion-list list)))))
  7808. (message "Making completion list...%s" "done")))))))
  7809. ;;;; TODO, DEADLINE, Comments
  7810. (defun org-toggle-comment ()
  7811. "Change the COMMENT state of an entry."
  7812. (interactive)
  7813. (save-excursion
  7814. (org-back-to-heading)
  7815. (let (case-fold-search)
  7816. (if (looking-at (concat outline-regexp
  7817. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7818. (replace-match "" t t nil 1)
  7819. (if (looking-at outline-regexp)
  7820. (progn
  7821. (goto-char (match-end 0))
  7822. (insert org-comment-string " ")))))))
  7823. (defvar org-last-todo-state-is-todo nil
  7824. "This is non-nil when the last TODO state change led to a TODO state.
  7825. If the last change removed the TODO tag or switched to DONE, then
  7826. this is nil.")
  7827. (defvar org-setting-tags nil) ; dynamically skipped
  7828. (defun org-parse-local-options (string var)
  7829. "Parse STRING for startup setting relevant for variable VAR."
  7830. (let ((rtn (symbol-value var))
  7831. e opts)
  7832. (save-match-data
  7833. (if (or (not string) (not (string-match "\\S-" string)))
  7834. rtn
  7835. (setq opts (delq nil (mapcar (lambda (x)
  7836. (setq e (assoc x org-startup-options))
  7837. (if (eq (nth 1 e) var) e nil))
  7838. (org-split-string string "[ \t]+"))))
  7839. (if (not opts)
  7840. rtn
  7841. (setq rtn nil)
  7842. (while (setq e (pop opts))
  7843. (if (not (nth 3 e))
  7844. (setq rtn (nth 2 e))
  7845. (if (not (listp rtn)) (setq rtn nil))
  7846. (push (nth 2 e) rtn)))
  7847. rtn)))))
  7848. (defvar org-todo-setup-filter-hook nil
  7849. "Hook for functions that pre-filter todo specs.
  7850. Each function takes a todo spec and returns either `nil' or the spec
  7851. transformed into canonical form." )
  7852. (defvar org-todo-get-default-hook nil
  7853. "Hook for functions that get a default item for todo.
  7854. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7855. `nil' or a string to be used for the todo mark." )
  7856. (defvar org-agenda-headline-snapshot-before-repeat)
  7857. (defun org-todo (&optional arg)
  7858. "Change the TODO state of an item.
  7859. The state of an item is given by a keyword at the start of the heading,
  7860. like
  7861. *** TODO Write paper
  7862. *** DONE Call mom
  7863. The different keywords are specified in the variable `org-todo-keywords'.
  7864. By default the available states are \"TODO\" and \"DONE\".
  7865. So for this example: when the item starts with TODO, it is changed to DONE.
  7866. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7867. DONE are present, add TODO at the beginning of the heading.
  7868. With C-u prefix arg, use completion to determine the new state.
  7869. With numeric prefix arg, switch to that state.
  7870. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7871. With a tripple C-u prefix, circumvent any state blocking.
  7872. For calling through lisp, arg is also interpreted in the following way:
  7873. 'none -> empty state
  7874. \"\"(empty string) -> switch to empty state
  7875. 'done -> switch to DONE
  7876. 'nextset -> switch to the next set of keywords
  7877. 'previousset -> switch to the previous set of keywords
  7878. \"WAITING\" -> switch to the specified keyword, but only if it
  7879. really is a member of `org-todo-keywords'."
  7880. (interactive "P")
  7881. (if (equal arg '(16)) (setq arg 'nextset))
  7882. (let ((org-blocker-hook org-blocker-hook))
  7883. (when (equal arg '(64))
  7884. (setq arg nil org-blocker-hook nil))
  7885. (save-excursion
  7886. (catch 'exit
  7887. (org-back-to-heading)
  7888. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7889. (or (looking-at (concat " +" org-todo-regexp " *"))
  7890. (looking-at " *"))
  7891. (let* ((match-data (match-data))
  7892. (startpos (point-at-bol))
  7893. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7894. (org-log-done org-log-done)
  7895. (org-log-repeat org-log-repeat)
  7896. (org-todo-log-states org-todo-log-states)
  7897. (this (match-string 1))
  7898. (hl-pos (match-beginning 0))
  7899. (head (org-get-todo-sequence-head this))
  7900. (ass (assoc head org-todo-kwd-alist))
  7901. (interpret (nth 1 ass))
  7902. (done-word (nth 3 ass))
  7903. (final-done-word (nth 4 ass))
  7904. (last-state (or this ""))
  7905. (completion-ignore-case t)
  7906. (member (member this org-todo-keywords-1))
  7907. (tail (cdr member))
  7908. (state (cond
  7909. ((and org-todo-key-trigger
  7910. (or (and (equal arg '(4))
  7911. (eq org-use-fast-todo-selection 'prefix))
  7912. (and (not arg) org-use-fast-todo-selection
  7913. (not (eq org-use-fast-todo-selection
  7914. 'prefix)))))
  7915. ;; Use fast selection
  7916. (org-fast-todo-selection))
  7917. ((and (equal arg '(4))
  7918. (or (not org-use-fast-todo-selection)
  7919. (not org-todo-key-trigger)))
  7920. ;; Read a state with completion
  7921. (org-ido-completing-read
  7922. "State: " (mapcar (lambda(x) (list x))
  7923. org-todo-keywords-1)
  7924. nil t))
  7925. ((eq arg 'right)
  7926. (if this
  7927. (if tail (car tail) nil)
  7928. (car org-todo-keywords-1)))
  7929. ((eq arg 'left)
  7930. (if (equal member org-todo-keywords-1)
  7931. nil
  7932. (if this
  7933. (nth (- (length org-todo-keywords-1)
  7934. (length tail) 2)
  7935. org-todo-keywords-1)
  7936. (org-last org-todo-keywords-1))))
  7937. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7938. (setq arg nil))) ; hack to fall back to cycling
  7939. (arg
  7940. ;; user or caller requests a specific state
  7941. (cond
  7942. ((equal arg "") nil)
  7943. ((eq arg 'none) nil)
  7944. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7945. ((eq arg 'nextset)
  7946. (or (car (cdr (member head org-todo-heads)))
  7947. (car org-todo-heads)))
  7948. ((eq arg 'previousset)
  7949. (let ((org-todo-heads (reverse org-todo-heads)))
  7950. (or (car (cdr (member head org-todo-heads)))
  7951. (car org-todo-heads))))
  7952. ((car (member arg org-todo-keywords-1)))
  7953. ((nth (1- (prefix-numeric-value arg))
  7954. org-todo-keywords-1))))
  7955. ((null member) (or head (car org-todo-keywords-1)))
  7956. ((equal this final-done-word) nil) ;; -> make empty
  7957. ((null tail) nil) ;; -> first entry
  7958. ((memq interpret '(type priority))
  7959. (if (eq this-command last-command)
  7960. (car tail)
  7961. (if (> (length tail) 0)
  7962. (or done-word (car org-done-keywords))
  7963. nil)))
  7964. (t
  7965. (car tail))))
  7966. (state (or
  7967. (run-hook-with-args-until-success
  7968. 'org-todo-get-default-hook state last-state)
  7969. state))
  7970. (next (if state (concat " " state " ") " "))
  7971. (change-plist (list :type 'todo-state-change :from this :to state
  7972. :position startpos))
  7973. dolog now-done-p)
  7974. (when org-blocker-hook
  7975. (setq org-last-todo-state-is-todo
  7976. (not (member this org-done-keywords)))
  7977. (unless (save-excursion
  7978. (save-match-data
  7979. (run-hook-with-args-until-failure
  7980. 'org-blocker-hook change-plist)))
  7981. (if (interactive-p)
  7982. (error "TODO state change from %s to %s blocked" this state)
  7983. ;; fail silently
  7984. (message "TODO state change from %s to %s blocked" this state)
  7985. (throw 'exit nil))))
  7986. (store-match-data match-data)
  7987. (replace-match next t t)
  7988. (unless (pos-visible-in-window-p hl-pos)
  7989. (message "TODO state changed to %s" (org-trim next)))
  7990. (unless head
  7991. (setq head (org-get-todo-sequence-head state)
  7992. ass (assoc head org-todo-kwd-alist)
  7993. interpret (nth 1 ass)
  7994. done-word (nth 3 ass)
  7995. final-done-word (nth 4 ass)))
  7996. (when (memq arg '(nextset previousset))
  7997. (message "Keyword-Set %d/%d: %s"
  7998. (- (length org-todo-sets) -1
  7999. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8000. (length org-todo-sets)
  8001. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8002. (setq org-last-todo-state-is-todo
  8003. (not (member state org-done-keywords)))
  8004. (setq now-done-p (and (member state org-done-keywords)
  8005. (not (member this org-done-keywords))))
  8006. (and logging (org-local-logging logging))
  8007. (when (and (or org-todo-log-states org-log-done)
  8008. (not (memq arg '(nextset previousset))))
  8009. ;; we need to look at recording a time and note
  8010. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8011. (nth 2 (assoc this org-todo-log-states))))
  8012. (when (and state
  8013. (member state org-not-done-keywords)
  8014. (not (member this org-not-done-keywords)))
  8015. ;; This is now a todo state and was not one before
  8016. ;; If there was a CLOSED time stamp, get rid of it.
  8017. (org-add-planning-info nil nil 'closed))
  8018. (when (and now-done-p org-log-done)
  8019. ;; It is now done, and it was not done before
  8020. (org-add-planning-info 'closed (org-current-time))
  8021. (if (and (not dolog) (eq 'note org-log-done))
  8022. (org-add-log-setup 'done state this 'findpos 'note)))
  8023. (when (and state dolog)
  8024. ;; This is a non-nil state, and we need to log it
  8025. (org-add-log-setup 'state state this 'findpos dolog)))
  8026. ;; Fixup tag positioning
  8027. (org-todo-trigger-tag-changes state)
  8028. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8029. (when org-provide-todo-statistics
  8030. (org-update-parent-todo-statistics))
  8031. (run-hooks 'org-after-todo-state-change-hook)
  8032. (if (and arg (not (member state org-done-keywords)))
  8033. (setq head (org-get-todo-sequence-head state)))
  8034. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8035. ;; Do we need to trigger a repeat?
  8036. (when now-done-p
  8037. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8038. ;; This is for the agenda, take a snapshot of the headline.
  8039. (save-match-data
  8040. (setq org-agenda-headline-snapshot-before-repeat
  8041. (org-get-heading))))
  8042. (org-auto-repeat-maybe state))
  8043. ;; Fixup cursor location if close to the keyword
  8044. (if (and (outline-on-heading-p)
  8045. (not (bolp))
  8046. (save-excursion (beginning-of-line 1)
  8047. (looking-at org-todo-line-regexp))
  8048. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8049. (progn
  8050. (goto-char (or (match-end 2) (match-end 1)))
  8051. (just-one-space)))
  8052. (when org-trigger-hook
  8053. (save-excursion
  8054. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8055. (defun org-block-todo-from-children-or-siblings (change-plist)
  8056. "Block turning an entry into a TODO, using the hierarchy.
  8057. This checks whether the current task should be blocked from state
  8058. changes. Such blocking occurs when:
  8059. 1. The task has children which are not all in a completed state.
  8060. 2. A task has a parent with the property :ORDERED:, and there
  8061. are siblings prior to the current task with incomplete
  8062. status."
  8063. (catch 'dont-block
  8064. ;; If this is not a todo state change, or if this entry is already DONE,
  8065. ;; do not block
  8066. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8067. (member (plist-get change-plist :from)
  8068. (cons 'done org-done-keywords))
  8069. (member (plist-get change-plist :to)
  8070. (cons 'todo org-not-done-keywords)))
  8071. (throw 'dont-block t))
  8072. ;; If this task has children, and any are undone, it's blocked
  8073. (save-excursion
  8074. (org-back-to-heading t)
  8075. (let ((this-level (funcall outline-level)))
  8076. (outline-next-heading)
  8077. (let ((child-level (funcall outline-level)))
  8078. (while (and (not (eobp))
  8079. (> child-level this-level))
  8080. ;; this todo has children, check whether they are all
  8081. ;; completed
  8082. (if (and (not (org-entry-is-done-p))
  8083. (org-entry-is-todo-p))
  8084. (throw 'dont-block nil))
  8085. (outline-next-heading)
  8086. (setq child-level (funcall outline-level))))))
  8087. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8088. ;; any previous siblings are undone, it's blocked
  8089. (save-excursion
  8090. (org-back-to-heading t)
  8091. (when (save-excursion
  8092. (ignore-errors
  8093. (org-up-heading-all 1)
  8094. (org-entry-get (point) "ORDERED")))
  8095. (let* ((this-level (funcall outline-level))
  8096. (current-level this-level))
  8097. (while (and (not (bobp))
  8098. (>= current-level this-level))
  8099. (outline-previous-heading)
  8100. (setq current-level (funcall outline-level))
  8101. (if (= current-level this-level)
  8102. ;; This is a younger sibling, check if it is completed
  8103. (if (and (not (org-entry-is-done-p))
  8104. (org-entry-is-todo-p))
  8105. (throw 'dont-block nil)))))))
  8106. t)) ; don't block
  8107. (defcustom org-track-ordered-property-with-tag nil
  8108. "Should the ORDERED property also be shown as a tag?
  8109. The ORDERED property decides if an entry should require subtasks to be
  8110. completed in sequence. Since a property is not very visible, setting
  8111. this option means that toggling the ORDERED property with the command
  8112. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8113. not relevant for the behavior, but it makes things more visible.
  8114. Note that toggling the tag with tags commands will not change the property
  8115. and therefore not influence behavior!
  8116. This can be t, meaning the tag ORDERED should be used, It can also be a
  8117. string to select a different tag for this task."
  8118. :group 'org-todo
  8119. :type '(choice
  8120. (const :tag "No tracking" nil)
  8121. (const :tag "Track with ORDERED tag" t)
  8122. (string :tag "Use other tag")))
  8123. (defun org-toggle-ordered-property ()
  8124. "Toggle the ORDERED property of the current entry.
  8125. For better visibility, you can track the value of this property with a tag.
  8126. See variable `org-track-ordered-property-with-tag'."
  8127. (interactive)
  8128. (let* ((t1 org-track-ordered-property-with-tag)
  8129. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8130. (save-excursion
  8131. (org-back-to-heading)
  8132. (if (org-entry-get nil "ORDERED")
  8133. (progn
  8134. (org-delete-property "ORDERED")
  8135. (and tag (org-toggle-tag tag 'off))
  8136. (message "Subtasks can be completed in arbitrary order"))
  8137. (org-entry-put nil "ORDERED" "t")
  8138. (and tag (org-toggle-tag tag 'on))
  8139. (message "Subtasks must be completed in sequence")))))
  8140. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8141. (defun org-block-todo-from-checkboxes (change-plist)
  8142. "Block turning an entry into a TODO, using checkboxes.
  8143. This checks whether the current task should be blocked from state
  8144. changes because there are uncheckd boxes in this entry."
  8145. (catch 'dont-block
  8146. ;; If this is not a todo state change, or if this entry is already DONE,
  8147. ;; do not block
  8148. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8149. (member (plist-get change-plist :from)
  8150. (cons 'done org-done-keywords))
  8151. (member (plist-get change-plist :to)
  8152. (cons 'todo org-not-done-keywords)))
  8153. (throw 'dont-block t))
  8154. ;; If this task has checkboxes that are not checked, it's blocked
  8155. (save-excursion
  8156. (org-back-to-heading t)
  8157. (let ((beg (point)) end)
  8158. (outline-next-heading)
  8159. (setq end (point))
  8160. (goto-char beg)
  8161. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8162. end t)
  8163. (progn
  8164. (if (boundp 'org-blocked-by-checkboxes)
  8165. (setq org-blocked-by-checkboxes t))
  8166. (throw 'dont-block nil)))))
  8167. t)) ; do not block
  8168. (defun org-update-parent-todo-statistics ()
  8169. "Update any statistics cookie in the parent of the current headline."
  8170. (interactive)
  8171. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8172. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8173. (catch 'exit
  8174. (save-excursion
  8175. (setq level (org-up-heading-safe))
  8176. (unless level
  8177. (throw 'exit nil))
  8178. (while (re-search-forward box-re (point-at-eol) t)
  8179. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8180. (setq is-percent (match-end 2))
  8181. (save-match-data
  8182. (unless (outline-next-heading) (throw 'exit nil))
  8183. (while (looking-at org-todo-line-regexp)
  8184. (setq kwd (match-string 2))
  8185. (and kwd (setq cnt-all (1+ cnt-all)))
  8186. (and (member kwd org-done-keywords)
  8187. (setq cnt-done (1+ cnt-done)))
  8188. (condition-case nil
  8189. (org-forward-same-level 1)
  8190. (error (end-of-line 1)))))
  8191. (replace-match
  8192. (if is-percent
  8193. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8194. (format "[%d/%d]" cnt-done cnt-all))))
  8195. (when cookie-present
  8196. (run-hook-with-args 'org-after-todo-statistics-hook
  8197. cnt-done (- cnt-all cnt-done)))))))
  8198. (defvar org-after-todo-statistics-hook nil
  8199. "Hook that is called after a TODO statistics cookie has been updated.
  8200. Each function is called with two arguments: the number of not-done entries
  8201. and the number of done entries.
  8202. For example, the following function, when added to this hook, will switch
  8203. an entry to DONE when all children are done, and back to TODO when new
  8204. entries are set to a TODO status. Note that this hook is only called
  8205. when there is a statistics cookie in the headline!
  8206. (defun org-summary-todo (n-done n-not-done)
  8207. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8208. (let (org-log-done org-log-states) ; turn off logging
  8209. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8210. ")
  8211. (defun org-todo-trigger-tag-changes (state)
  8212. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8213. (let ((l org-todo-state-tags-triggers)
  8214. changes)
  8215. (when (or (not state) (equal state ""))
  8216. (setq changes (append changes (cdr (assoc "" l)))))
  8217. (when (and (stringp state) (> (length state) 0))
  8218. (setq changes (append changes (cdr (assoc state l)))))
  8219. (when (member state org-not-done-keywords)
  8220. (setq changes (append changes (cdr (assoc 'todo l)))))
  8221. (when (member state org-done-keywords)
  8222. (setq changes (append changes (cdr (assoc 'done l)))))
  8223. (dolist (c changes)
  8224. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8225. (defun org-local-logging (value)
  8226. "Get logging settings from a property VALUE."
  8227. (let* (words w a)
  8228. ;; directly set the variables, they are already local.
  8229. (setq org-log-done nil
  8230. org-log-repeat nil
  8231. org-todo-log-states nil)
  8232. (setq words (org-split-string value))
  8233. (while (setq w (pop words))
  8234. (cond
  8235. ((setq a (assoc w org-startup-options))
  8236. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8237. (set (nth 1 a) (nth 2 a))))
  8238. ((setq a (org-extract-log-state-settings w))
  8239. (and (member (car a) org-todo-keywords-1)
  8240. (push a org-todo-log-states)))))))
  8241. (defun org-get-todo-sequence-head (kwd)
  8242. "Return the head of the TODO sequence to which KWD belongs.
  8243. If KWD is not set, check if there is a text property remembering the
  8244. right sequence."
  8245. (let (p)
  8246. (cond
  8247. ((not kwd)
  8248. (or (get-text-property (point-at-bol) 'org-todo-head)
  8249. (progn
  8250. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8251. nil (point-at-eol)))
  8252. (get-text-property p 'org-todo-head))))
  8253. ((not (member kwd org-todo-keywords-1))
  8254. (car org-todo-keywords-1))
  8255. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8256. (defun org-fast-todo-selection ()
  8257. "Fast TODO keyword selection with single keys.
  8258. Returns the new TODO keyword, or nil if no state change should occur."
  8259. (let* ((fulltable org-todo-key-alist)
  8260. (done-keywords org-done-keywords) ;; needed for the faces.
  8261. (maxlen (apply 'max (mapcar
  8262. (lambda (x)
  8263. (if (stringp (car x)) (string-width (car x)) 0))
  8264. fulltable)))
  8265. (expert nil)
  8266. (fwidth (+ maxlen 3 1 3))
  8267. (ncol (/ (- (window-width) 4) fwidth))
  8268. tg cnt e c tbl
  8269. groups ingroup)
  8270. (save-excursion
  8271. (save-window-excursion
  8272. (if expert
  8273. (set-buffer (get-buffer-create " *Org todo*"))
  8274. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8275. (erase-buffer)
  8276. (org-set-local 'org-done-keywords done-keywords)
  8277. (setq tbl fulltable cnt 0)
  8278. (while (setq e (pop tbl))
  8279. (cond
  8280. ((equal e '(:startgroup))
  8281. (push '() groups) (setq ingroup t)
  8282. (when (not (= cnt 0))
  8283. (setq cnt 0)
  8284. (insert "\n"))
  8285. (insert "{ "))
  8286. ((equal e '(:endgroup))
  8287. (setq ingroup nil cnt 0)
  8288. (insert "}\n"))
  8289. ((equal e '(:newline))
  8290. (when (not (= cnt 0))
  8291. (setq cnt 0)
  8292. (insert "\n")
  8293. (setq e (car tbl))
  8294. (while (equal (car tbl) '(:newline))
  8295. (insert "\n")
  8296. (setq tbl (cdr tbl)))))
  8297. (t
  8298. (setq tg (car e) c (cdr e))
  8299. (if ingroup (push tg (car groups)))
  8300. (setq tg (org-add-props tg nil 'face
  8301. (org-get-todo-face tg)))
  8302. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8303. (insert "[" c "] " tg (make-string
  8304. (- fwidth 4 (length tg)) ?\ ))
  8305. (when (= (setq cnt (1+ cnt)) ncol)
  8306. (insert "\n")
  8307. (if ingroup (insert " "))
  8308. (setq cnt 0)))))
  8309. (insert "\n")
  8310. (goto-char (point-min))
  8311. (if (not expert) (org-fit-window-to-buffer))
  8312. (message "[a-z..]:Set [SPC]:clear")
  8313. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8314. (cond
  8315. ((or (= c ?\C-g)
  8316. (and (= c ?q) (not (rassoc c fulltable))))
  8317. (setq quit-flag t))
  8318. ((= c ?\ ) nil)
  8319. ((setq e (rassoc c fulltable) tg (car e))
  8320. tg)
  8321. (t (setq quit-flag t)))))))
  8322. (defun org-entry-is-todo-p ()
  8323. (member (org-get-todo-state) org-not-done-keywords))
  8324. (defun org-entry-is-done-p ()
  8325. (member (org-get-todo-state) org-done-keywords))
  8326. (defun org-get-todo-state ()
  8327. (save-excursion
  8328. (org-back-to-heading t)
  8329. (and (looking-at org-todo-line-regexp)
  8330. (match-end 2)
  8331. (match-string 2))))
  8332. (defun org-at-date-range-p (&optional inactive-ok)
  8333. "Is the cursor inside a date range?"
  8334. (interactive)
  8335. (save-excursion
  8336. (catch 'exit
  8337. (let ((pos (point)))
  8338. (skip-chars-backward "^[<\r\n")
  8339. (skip-chars-backward "<[")
  8340. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8341. (>= (match-end 0) pos)
  8342. (throw 'exit t))
  8343. (skip-chars-backward "^<[\r\n")
  8344. (skip-chars-backward "<[")
  8345. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8346. (>= (match-end 0) pos)
  8347. (throw 'exit t)))
  8348. nil)))
  8349. (defun org-get-repeat ()
  8350. "Check if there is a deadline/schedule with repeater in this entry."
  8351. (save-match-data
  8352. (save-excursion
  8353. (org-back-to-heading t)
  8354. (if (re-search-forward
  8355. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8356. (match-string 1)))))
  8357. (defvar org-last-changed-timestamp)
  8358. (defvar org-last-inserted-timestamp)
  8359. (defvar org-log-post-message)
  8360. (defvar org-log-note-purpose)
  8361. (defvar org-log-note-how)
  8362. (defvar org-log-note-extra)
  8363. (defun org-auto-repeat-maybe (done-word)
  8364. "Check if the current headline contains a repeated deadline/schedule.
  8365. If yes, set TODO state back to what it was and change the base date
  8366. of repeating deadline/scheduled time stamps to new date.
  8367. This function is run automatically after each state change to a DONE state."
  8368. ;; last-state is dynamically scoped into this function
  8369. (let* ((repeat (org-get-repeat))
  8370. (aa (assoc last-state org-todo-kwd-alist))
  8371. (interpret (nth 1 aa))
  8372. (head (nth 2 aa))
  8373. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8374. (msg "Entry repeats: ")
  8375. (org-log-done nil)
  8376. (org-todo-log-states nil)
  8377. (nshiftmax 10) (nshift 0)
  8378. re type n what ts time)
  8379. (when repeat
  8380. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8381. (org-todo (if (eq interpret 'type) last-state head))
  8382. (when org-log-repeat
  8383. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8384. (memq 'org-add-log-note post-command-hook))
  8385. ;; OK, we are already setup for some record
  8386. (if (eq org-log-repeat 'note)
  8387. ;; make sure we take a note, not only a time stamp
  8388. (setq org-log-note-how 'note))
  8389. ;; Set up for taking a record
  8390. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8391. last-state
  8392. 'findpos org-log-repeat)))
  8393. (org-back-to-heading t)
  8394. (org-add-planning-info nil nil 'closed)
  8395. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8396. org-deadline-time-regexp "\\)\\|\\("
  8397. org-ts-regexp "\\)"))
  8398. (while (re-search-forward
  8399. re (save-excursion (outline-next-heading) (point)) t)
  8400. (setq type (if (match-end 1) org-scheduled-string
  8401. (if (match-end 3) org-deadline-string "Plain:"))
  8402. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8403. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8404. (setq n (string-to-number (match-string 2 ts))
  8405. what (match-string 3 ts))
  8406. (if (equal what "w") (setq n (* n 7) what "d"))
  8407. ;; Preparation, see if we need to modify the start date for the change
  8408. (when (match-end 1)
  8409. (setq time (save-match-data (org-time-string-to-time ts)))
  8410. (cond
  8411. ((equal (match-string 1 ts) ".")
  8412. ;; Shift starting date to today
  8413. (org-timestamp-change
  8414. (- (time-to-days (current-time)) (time-to-days time))
  8415. 'day))
  8416. ((equal (match-string 1 ts) "+")
  8417. (while (or (= nshift 0)
  8418. (<= (time-to-days time) (time-to-days (current-time))))
  8419. (when (= (incf nshift) nshiftmax)
  8420. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8421. (error "Abort")))
  8422. (org-timestamp-change n (cdr (assoc what whata)))
  8423. (org-at-timestamp-p t)
  8424. (setq ts (match-string 1))
  8425. (setq time (save-match-data (org-time-string-to-time ts))))
  8426. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8427. ;; rematch, so that we have everything in place for the real shift
  8428. (org-at-timestamp-p t)
  8429. (setq ts (match-string 1))
  8430. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8431. (org-timestamp-change n (cdr (assoc what whata)))
  8432. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8433. (setq org-log-post-message msg)
  8434. (message "%s" msg))))
  8435. (defun org-show-todo-tree (arg)
  8436. "Make a compact tree which shows all headlines marked with TODO.
  8437. The tree will show the lines where the regexp matches, and all higher
  8438. headlines above the match.
  8439. With a \\[universal-argument] prefix, also show the DONE entries.
  8440. With a numeric prefix N, construct a sparse tree for the Nth element
  8441. of `org-todo-keywords-1'."
  8442. (interactive "P")
  8443. (let ((case-fold-search nil)
  8444. (kwd-re
  8445. (cond ((null arg) org-not-done-regexp)
  8446. ((equal arg '(4))
  8447. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8448. (mapcar 'list org-todo-keywords-1))))
  8449. (concat "\\("
  8450. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8451. "\\)\\>")))
  8452. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8453. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8454. org-todo-keywords-1)))
  8455. (t (error "Invalid prefix argument: %s" arg)))))
  8456. (message "%d TODO entries found"
  8457. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8458. (defun org-deadline (&optional remove time)
  8459. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8460. With argument REMOVE, remove any deadline from the item.
  8461. When TIME is set, it should be an internal time specification, and the
  8462. scheduling will use the corresponding date."
  8463. (interactive "P")
  8464. (if remove
  8465. (progn
  8466. (org-remove-timestamp-with-keyword org-deadline-string)
  8467. (message "Item no longer has a deadline."))
  8468. (if (org-get-repeat)
  8469. (error "Cannot change deadline on task with repeater, please do that by hand")
  8470. (org-add-planning-info 'deadline time 'closed)
  8471. (message "Deadline on %s" org-last-inserted-timestamp))))
  8472. (defun org-schedule (&optional remove time)
  8473. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8474. With argument REMOVE, remove any scheduling date from the item.
  8475. When TIME is set, it should be an internal time specification, and the
  8476. scheduling will use the corresponding date."
  8477. (interactive "P")
  8478. (if remove
  8479. (progn
  8480. (org-remove-timestamp-with-keyword org-scheduled-string)
  8481. (message "Item is no longer scheduled."))
  8482. (if (org-get-repeat)
  8483. (error "Cannot reschedule task with repeater, please do that by hand")
  8484. (org-add-planning-info 'scheduled time 'closed)
  8485. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8486. (defun org-get-scheduled-time (pom &optional inherit)
  8487. "Get the scheduled time as a time tuple, of a format suitable
  8488. for calling org-schedule with, or if there is no scheduling,
  8489. returns nil."
  8490. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8491. (when time
  8492. (apply 'encode-time (org-parse-time-string time)))))
  8493. (defun org-get-deadline-time (pom &optional inherit)
  8494. "Get the deadine as a time tuple, of a format suitable for
  8495. calling org-deadlin with, or if there is no scheduling, returns
  8496. nil."
  8497. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8498. (when time
  8499. (apply 'encode-time (org-parse-time-string time)))))
  8500. (defun org-remove-timestamp-with-keyword (keyword)
  8501. "Remove all time stamps with KEYWORD in the current entry."
  8502. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8503. beg)
  8504. (save-excursion
  8505. (org-back-to-heading t)
  8506. (setq beg (point))
  8507. (org-end-of-subtree t t)
  8508. (while (re-search-backward re beg t)
  8509. (replace-match "")
  8510. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8511. (equal (char-before) ?\ ))
  8512. (backward-delete-char 1)
  8513. (if (string-match "^[ \t]*$" (buffer-substring
  8514. (point-at-bol) (point-at-eol)))
  8515. (delete-region (point-at-bol)
  8516. (min (point-max) (1+ (point-at-eol))))))))))
  8517. (defun org-add-planning-info (what &optional time &rest remove)
  8518. "Insert new timestamp with keyword in the line directly after the headline.
  8519. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8520. If non is given, the user is prompted for a date.
  8521. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8522. be removed."
  8523. (interactive)
  8524. (let (org-time-was-given org-end-time-was-given ts
  8525. end default-time default-input)
  8526. (catch 'exit
  8527. (when (and (not time) (memq what '(scheduled deadline)))
  8528. ;; Try to get a default date/time from existing timestamp
  8529. (save-excursion
  8530. (org-back-to-heading t)
  8531. (setq end (save-excursion (outline-next-heading) (point)))
  8532. (when (re-search-forward (if (eq what 'scheduled)
  8533. org-scheduled-time-regexp
  8534. org-deadline-time-regexp)
  8535. end t)
  8536. (setq ts (match-string 1)
  8537. default-time
  8538. (apply 'encode-time (org-parse-time-string ts))
  8539. default-input (and ts (org-get-compact-tod ts))))))
  8540. (when what
  8541. ;; If necessary, get the time from the user
  8542. (setq time (or time (org-read-date nil 'to-time nil nil
  8543. default-time default-input))))
  8544. (when (and org-insert-labeled-timestamps-at-point
  8545. (member what '(scheduled deadline)))
  8546. (insert
  8547. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8548. (org-insert-time-stamp time org-time-was-given
  8549. nil nil nil (list org-end-time-was-given))
  8550. (setq what nil))
  8551. (save-excursion
  8552. (save-restriction
  8553. (let (col list elt ts buffer-invisibility-spec)
  8554. (org-back-to-heading t)
  8555. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8556. (goto-char (match-end 1))
  8557. (setq col (current-column))
  8558. (goto-char (match-end 0))
  8559. (if (eobp) (insert "\n") (forward-char 1))
  8560. (when (and (not what)
  8561. (not (looking-at
  8562. (concat "[ \t]*"
  8563. org-keyword-time-not-clock-regexp))))
  8564. ;; Nothing to add, nothing to remove...... :-)
  8565. (throw 'exit nil))
  8566. (if (and (not (looking-at outline-regexp))
  8567. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8568. "[^\r\n]*"))
  8569. (not (equal (match-string 1) org-clock-string)))
  8570. (narrow-to-region (match-beginning 0) (match-end 0))
  8571. (insert-before-markers "\n")
  8572. (backward-char 1)
  8573. (narrow-to-region (point) (point))
  8574. (and org-adapt-indentation (org-indent-to-column col)))
  8575. ;; Check if we have to remove something.
  8576. (setq list (cons what remove))
  8577. (while list
  8578. (setq elt (pop list))
  8579. (goto-char (point-min))
  8580. (when (or (and (eq elt 'scheduled)
  8581. (re-search-forward org-scheduled-time-regexp nil t))
  8582. (and (eq elt 'deadline)
  8583. (re-search-forward org-deadline-time-regexp nil t))
  8584. (and (eq elt 'closed)
  8585. (re-search-forward org-closed-time-regexp nil t)))
  8586. (replace-match "")
  8587. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8588. (if (looking-at " +") (replace-match ""))))
  8589. (goto-char (point-max))
  8590. (when what
  8591. (insert
  8592. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8593. (cond ((eq what 'scheduled) org-scheduled-string)
  8594. ((eq what 'deadline) org-deadline-string)
  8595. ((eq what 'closed) org-closed-string))
  8596. " ")
  8597. (setq ts (org-insert-time-stamp
  8598. time
  8599. (or org-time-was-given
  8600. (and (eq what 'closed) org-log-done-with-time))
  8601. (eq what 'closed)
  8602. nil nil (list org-end-time-was-given)))
  8603. (end-of-line 1))
  8604. (goto-char (point-min))
  8605. (widen)
  8606. (if (and (looking-at "[ \t]+\n")
  8607. (equal (char-before) ?\n))
  8608. (delete-region (1- (point)) (point-at-eol)))
  8609. ts))))))
  8610. (defvar org-log-note-marker (make-marker))
  8611. (defvar org-log-note-purpose nil)
  8612. (defvar org-log-note-state nil)
  8613. (defvar org-log-note-previous-state nil)
  8614. (defvar org-log-note-how nil)
  8615. (defvar org-log-note-extra nil)
  8616. (defvar org-log-note-window-configuration nil)
  8617. (defvar org-log-note-return-to (make-marker))
  8618. (defvar org-log-post-message nil
  8619. "Message to be displayed after a log note has been stored.
  8620. The auto-repeater uses this.")
  8621. (defun org-add-note ()
  8622. "Add a note to the current entry.
  8623. This is done in the same way as adding a state change note."
  8624. (interactive)
  8625. (org-add-log-setup 'note nil nil 'findpos nil))
  8626. (defvar org-property-end-re)
  8627. (defun org-add-log-setup (&optional purpose state prev-state
  8628. findpos how &optional extra)
  8629. "Set up the post command hook to take a note.
  8630. If this is about to TODO state change, the new state is expected in STATE.
  8631. When FINDPOS is non-nil, find the correct position for the note in
  8632. the current entry. If not, assume that it can be inserted at point.
  8633. HOW is an indicator what kind of note should be created.
  8634. EXTRA is additional text that will be inserted into the notes buffer."
  8635. (let ((drawer (cond ((stringp org-log-into-drawer)
  8636. org-log-into-drawer)
  8637. (org-log-into-drawer "LOGBOOK")
  8638. (t nil))))
  8639. (save-restriction
  8640. (save-excursion
  8641. (when findpos
  8642. (org-back-to-heading t)
  8643. (narrow-to-region (point) (save-excursion
  8644. (outline-next-heading) (point)))
  8645. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8646. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8647. "[^\r\n]*\\)?"))
  8648. (goto-char (match-end 0))
  8649. (cond
  8650. (drawer
  8651. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8652. nil t)
  8653. (progn
  8654. (goto-char (match-end 0))
  8655. (or org-log-states-order-reversed
  8656. (and (re-search-forward org-property-end-re nil t)
  8657. (goto-char (1- (match-beginning 0))))))
  8658. (insert "\n:" drawer ":\n:END:")
  8659. (beginning-of-line 0)
  8660. (org-indent-line-function)
  8661. (beginning-of-line 2)
  8662. (org-indent-line-function)
  8663. (end-of-line 0)))
  8664. ((and org-log-state-notes-insert-after-drawers
  8665. (save-excursion
  8666. (forward-line) (looking-at org-drawer-regexp)))
  8667. (forward-line)
  8668. (while (looking-at org-drawer-regexp)
  8669. (goto-char (match-end 0))
  8670. (re-search-forward org-property-end-re (point-max) t)
  8671. (forward-line))
  8672. (forward-line -1)))
  8673. (unless org-log-states-order-reversed
  8674. (and (= (char-after) ?\n) (forward-char 1))
  8675. (org-skip-over-state-notes)
  8676. (skip-chars-backward " \t\n\r")))
  8677. (move-marker org-log-note-marker (point))
  8678. (setq org-log-note-purpose purpose
  8679. org-log-note-state state
  8680. org-log-note-previous-state prev-state
  8681. org-log-note-how how
  8682. org-log-note-extra extra)
  8683. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8684. (defun org-skip-over-state-notes ()
  8685. "Skip past the list of State notes in an entry."
  8686. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8687. (while (looking-at "[ \t]*- State")
  8688. (condition-case nil
  8689. (org-next-item)
  8690. (error (org-end-of-item)))))
  8691. (defun org-add-log-note (&optional purpose)
  8692. "Pop up a window for taking a note, and add this note later at point."
  8693. (remove-hook 'post-command-hook 'org-add-log-note)
  8694. (setq org-log-note-window-configuration (current-window-configuration))
  8695. (delete-other-windows)
  8696. (move-marker org-log-note-return-to (point))
  8697. (switch-to-buffer (marker-buffer org-log-note-marker))
  8698. (goto-char org-log-note-marker)
  8699. (org-switch-to-buffer-other-window "*Org Note*")
  8700. (erase-buffer)
  8701. (if (memq org-log-note-how '(time state))
  8702. (let (current-prefix-arg) (org-store-log-note))
  8703. (let ((org-inhibit-startup t)) (org-mode))
  8704. (insert (format "# Insert note for %s.
  8705. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8706. (cond
  8707. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8708. ((eq org-log-note-purpose 'done) "closed todo item")
  8709. ((eq org-log-note-purpose 'state)
  8710. (format "state change from \"%s\" to \"%s\""
  8711. (or org-log-note-previous-state "")
  8712. (or org-log-note-state "")))
  8713. ((eq org-log-note-purpose 'note)
  8714. "this entry")
  8715. (t (error "This should not happen")))))
  8716. (if org-log-note-extra (insert org-log-note-extra))
  8717. (org-set-local 'org-finish-function 'org-store-log-note)))
  8718. (defvar org-note-abort nil) ; dynamically scoped
  8719. (defun org-store-log-note ()
  8720. "Finish taking a log note, and insert it to where it belongs."
  8721. (let ((txt (buffer-string))
  8722. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8723. lines ind)
  8724. (kill-buffer (current-buffer))
  8725. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8726. (setq txt (replace-match "" t t txt)))
  8727. (if (string-match "\\s-+\\'" txt)
  8728. (setq txt (replace-match "" t t txt)))
  8729. (setq lines (org-split-string txt "\n"))
  8730. (when (and note (string-match "\\S-" note))
  8731. (setq note
  8732. (org-replace-escapes
  8733. note
  8734. (list (cons "%u" (user-login-name))
  8735. (cons "%U" user-full-name)
  8736. (cons "%t" (format-time-string
  8737. (org-time-stamp-format 'long 'inactive)
  8738. (current-time)))
  8739. (cons "%s" (if org-log-note-state
  8740. (concat "\"" org-log-note-state "\"")
  8741. ""))
  8742. (cons "%S" (if org-log-note-previous-state
  8743. (concat "\"" org-log-note-previous-state "\"")
  8744. "\"\"")))))
  8745. (if lines (setq note (concat note " \\\\")))
  8746. (push note lines))
  8747. (when (or current-prefix-arg org-note-abort)
  8748. (when org-log-into-drawer
  8749. (org-remove-empty-drawer-at
  8750. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8751. org-log-note-marker))
  8752. (setq lines nil))
  8753. (when lines
  8754. (save-excursion
  8755. (set-buffer (marker-buffer org-log-note-marker))
  8756. (save-excursion
  8757. (goto-char org-log-note-marker)
  8758. (move-marker org-log-note-marker nil)
  8759. (end-of-line 1)
  8760. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8761. (insert "- " (pop lines))
  8762. (org-indent-line-function)
  8763. (beginning-of-line 1)
  8764. (looking-at "[ \t]*")
  8765. (setq ind (concat (match-string 0) " "))
  8766. (end-of-line 1)
  8767. (while lines (insert "\n" ind (pop lines)))
  8768. (message "Note stored")
  8769. (org-back-to-heading t)
  8770. (org-cycle-hide-drawers 'children)))))
  8771. (set-window-configuration org-log-note-window-configuration)
  8772. (with-current-buffer (marker-buffer org-log-note-return-to)
  8773. (goto-char org-log-note-return-to))
  8774. (move-marker org-log-note-return-to nil)
  8775. (and org-log-post-message (message "%s" org-log-post-message)))
  8776. (defun org-remove-empty-drawer-at (drawer pos)
  8777. "Remove an emptyr DARWER drawer at position POS.
  8778. POS may also be a marker."
  8779. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8780. (save-excursion
  8781. (save-restriction
  8782. (widen)
  8783. (goto-char pos)
  8784. (if (org-in-regexp
  8785. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8786. (replace-match ""))))))
  8787. (defun org-sparse-tree (&optional arg)
  8788. "Create a sparse tree, prompt for the details.
  8789. This command can create sparse trees. You first need to select the type
  8790. of match used to create the tree:
  8791. t Show entries with a specific TODO keyword.
  8792. m Show entries selected by a tags/property match.
  8793. p Enter a property name and its value (both with completion on existing
  8794. names/values) and show entries with that property.
  8795. r Show entries matching a regular expression.
  8796. d Show deadlines due within `org-deadline-warning-days'.
  8797. b Show deadlines and scheduled items before a date.
  8798. a Show deadlines and scheduled items after a date."
  8799. (interactive "P")
  8800. (let (ans kwd value)
  8801. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8802. (setq ans (read-char-exclusive))
  8803. (cond
  8804. ((equal ans ?d)
  8805. (call-interactively 'org-check-deadlines))
  8806. ((equal ans ?b)
  8807. (call-interactively 'org-check-before-date))
  8808. ((equal ans ?a)
  8809. (call-interactively 'org-check-after-date))
  8810. ((equal ans ?t)
  8811. (org-show-todo-tree '(4)))
  8812. ((member ans '(?T ?m))
  8813. (call-interactively 'org-match-sparse-tree))
  8814. ((member ans '(?p ?P))
  8815. (setq kwd (org-ido-completing-read "Property: "
  8816. (mapcar 'list (org-buffer-property-keys))))
  8817. (setq value (org-ido-completing-read "Value: "
  8818. (mapcar 'list (org-property-values kwd))))
  8819. (unless (string-match "\\`{.*}\\'" value)
  8820. (setq value (concat "\"" value "\"")))
  8821. (org-match-sparse-tree arg (concat kwd "=" value)))
  8822. ((member ans '(?r ?R ?/))
  8823. (call-interactively 'org-occur))
  8824. (t (error "No such sparse tree command \"%c\"" ans)))))
  8825. (defvar org-occur-highlights nil
  8826. "List of overlays used for occur matches.")
  8827. (make-variable-buffer-local 'org-occur-highlights)
  8828. (defvar org-occur-parameters nil
  8829. "Parameters of the active org-occur calls.
  8830. This is a list, each call to org-occur pushes as cons cell,
  8831. containing the regular expression and the callback, onto the list.
  8832. The list can contain several entries if `org-occur' has been called
  8833. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8834. will only contain one set of parameters. When the highlights are
  8835. removed (for example with `C-c C-c', or with the next edit (depending
  8836. on `org-remove-highlights-with-change'), this variable is emptied
  8837. as well.")
  8838. (make-variable-buffer-local 'org-occur-parameters)
  8839. (defun org-occur (regexp &optional keep-previous callback)
  8840. "Make a compact tree which shows all matches of REGEXP.
  8841. The tree will show the lines where the regexp matches, and all higher
  8842. headlines above the match. It will also show the heading after the match,
  8843. to make sure editing the matching entry is easy.
  8844. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8845. call to `org-occur' will be kept, to allow stacking of calls to this
  8846. command.
  8847. If CALLBACK is non-nil, it is a function which is called to confirm
  8848. that the match should indeed be shown."
  8849. (interactive "sRegexp: \nP")
  8850. (unless keep-previous
  8851. (org-remove-occur-highlights nil nil t))
  8852. (push (cons regexp callback) org-occur-parameters)
  8853. (let ((cnt 0))
  8854. (save-excursion
  8855. (goto-char (point-min))
  8856. (if (or (not keep-previous) ; do not want to keep
  8857. (not org-occur-highlights)) ; no previous matches
  8858. ;; hide everything
  8859. (org-overview))
  8860. (while (re-search-forward regexp nil t)
  8861. (when (or (not callback)
  8862. (save-match-data (funcall callback)))
  8863. (setq cnt (1+ cnt))
  8864. (when org-highlight-sparse-tree-matches
  8865. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8866. (org-show-context 'occur-tree))))
  8867. (when org-remove-highlights-with-change
  8868. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8869. nil 'local))
  8870. (unless org-sparse-tree-open-archived-trees
  8871. (org-hide-archived-subtrees (point-min) (point-max)))
  8872. (run-hooks 'org-occur-hook)
  8873. (if (interactive-p)
  8874. (message "%d match(es) for regexp %s" cnt regexp))
  8875. cnt))
  8876. (defun org-show-context (&optional key)
  8877. "Make sure point and context and visible.
  8878. How much context is shown depends upon the variables
  8879. `org-show-hierarchy-above', `org-show-following-heading'. and
  8880. `org-show-siblings'."
  8881. (let ((heading-p (org-on-heading-p t))
  8882. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8883. (following-p (org-get-alist-option org-show-following-heading key))
  8884. (entry-p (org-get-alist-option org-show-entry-below key))
  8885. (siblings-p (org-get-alist-option org-show-siblings key)))
  8886. (catch 'exit
  8887. ;; Show heading or entry text
  8888. (if (and heading-p (not entry-p))
  8889. (org-flag-heading nil) ; only show the heading
  8890. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8891. (org-show-hidden-entry))) ; show entire entry
  8892. (when following-p
  8893. ;; Show next sibling, or heading below text
  8894. (save-excursion
  8895. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8896. (org-flag-heading nil))))
  8897. (when siblings-p (org-show-siblings))
  8898. (when hierarchy-p
  8899. ;; show all higher headings, possibly with siblings
  8900. (save-excursion
  8901. (while (and (condition-case nil
  8902. (progn (org-up-heading-all 1) t)
  8903. (error nil))
  8904. (not (bobp)))
  8905. (org-flag-heading nil)
  8906. (when siblings-p (org-show-siblings))))))))
  8907. (defun org-reveal (&optional siblings)
  8908. "Show current entry, hierarchy above it, and the following headline.
  8909. This can be used to show a consistent set of context around locations
  8910. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8911. not t for the search context.
  8912. With optional argument SIBLINGS, on each level of the hierarchy all
  8913. siblings are shown. This repairs the tree structure to what it would
  8914. look like when opened with hierarchical calls to `org-cycle'."
  8915. (interactive "P")
  8916. (let ((org-show-hierarchy-above t)
  8917. (org-show-following-heading t)
  8918. (org-show-siblings (if siblings t org-show-siblings)))
  8919. (org-show-context nil)))
  8920. (defun org-highlight-new-match (beg end)
  8921. "Highlight from BEG to END and mark the highlight is an occur headline."
  8922. (let ((ov (org-make-overlay beg end)))
  8923. (org-overlay-put ov 'face 'secondary-selection)
  8924. (push ov org-occur-highlights)))
  8925. (defun org-remove-occur-highlights (&optional beg end noremove)
  8926. "Remove the occur highlights from the buffer.
  8927. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8928. from the `before-change-functions' in the current buffer."
  8929. (interactive)
  8930. (unless org-inhibit-highlight-removal
  8931. (mapc 'org-delete-overlay org-occur-highlights)
  8932. (setq org-occur-highlights nil)
  8933. (setq org-occur-parameters nil)
  8934. (unless noremove
  8935. (remove-hook 'before-change-functions
  8936. 'org-remove-occur-highlights 'local))))
  8937. ;;;; Priorities
  8938. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8939. "Regular expression matching the priority indicator.")
  8940. (defvar org-remove-priority-next-time nil)
  8941. (defun org-priority-up ()
  8942. "Increase the priority of the current item."
  8943. (interactive)
  8944. (org-priority 'up))
  8945. (defun org-priority-down ()
  8946. "Decrease the priority of the current item."
  8947. (interactive)
  8948. (org-priority 'down))
  8949. (defun org-priority (&optional action)
  8950. "Change the priority of an item by ARG.
  8951. ACTION can be `set', `up', `down', or a character."
  8952. (interactive)
  8953. (setq action (or action 'set))
  8954. (let (current new news have remove)
  8955. (save-excursion
  8956. (org-back-to-heading t)
  8957. (if (looking-at org-priority-regexp)
  8958. (setq current (string-to-char (match-string 2))
  8959. have t)
  8960. (setq current org-default-priority))
  8961. (cond
  8962. ((or (eq action 'set)
  8963. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8964. (if (not (eq action 'set))
  8965. (setq new action)
  8966. (message "Priority %c-%c, SPC to remove: "
  8967. org-highest-priority org-lowest-priority)
  8968. (setq new (read-char-exclusive)))
  8969. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8970. (= (upcase org-lowest-priority) org-lowest-priority))
  8971. (setq new (upcase new)))
  8972. (cond ((equal new ?\ ) (setq remove t))
  8973. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8974. (error "Priority must be between `%c' and `%c'"
  8975. org-highest-priority org-lowest-priority))))
  8976. ((eq action 'up)
  8977. (if (and (not have) (eq last-command this-command))
  8978. (setq new org-lowest-priority)
  8979. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8980. org-default-priority (1- current)))))
  8981. ((eq action 'down)
  8982. (if (and (not have) (eq last-command this-command))
  8983. (setq new org-highest-priority)
  8984. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8985. org-default-priority (1+ current)))))
  8986. (t (error "Invalid action")))
  8987. (if (or (< (upcase new) org-highest-priority)
  8988. (> (upcase new) org-lowest-priority))
  8989. (setq remove t))
  8990. (setq news (format "%c" new))
  8991. (if have
  8992. (if remove
  8993. (replace-match "" t t nil 1)
  8994. (replace-match news t t nil 2))
  8995. (if remove
  8996. (error "No priority cookie found in line")
  8997. (looking-at org-todo-line-regexp)
  8998. (if (match-end 2)
  8999. (progn
  9000. (goto-char (match-end 2))
  9001. (insert " [#" news "]"))
  9002. (goto-char (match-beginning 3))
  9003. (insert "[#" news "] ")))))
  9004. (org-preserve-lc (org-set-tags nil 'align))
  9005. (if remove
  9006. (message "Priority removed")
  9007. (message "Priority of current item set to %s" news))))
  9008. (defun org-get-priority (s)
  9009. "Find priority cookie and return priority."
  9010. (save-match-data
  9011. (if (not (string-match org-priority-regexp s))
  9012. (* 1000 (- org-lowest-priority org-default-priority))
  9013. (* 1000 (- org-lowest-priority
  9014. (string-to-char (match-string 2 s)))))))
  9015. ;;;; Tags
  9016. (defvar org-agenda-archives-mode)
  9017. (defvar org-scanner-tags nil
  9018. "The current tag list while the tags scanner is running.")
  9019. (defvar org-trust-scanner-tags nil
  9020. "Should `org-get-tags-at' use the tags fro the scanner.
  9021. This is for internal dynamical scoping only.
  9022. When this is non-nil, the function `org-get-tags-at' will return the value
  9023. of `org-scanner-tags' instead of building the list by itself. This
  9024. can lead to large speed-ups when the tags scanner is used in a file with
  9025. many entries, and when the list of tags is retrieved, for example to
  9026. obtain a list of properties. Building the tags list for each entry in such
  9027. a file becomes an N^2 operation - but with this variable set, it scales
  9028. as N.")
  9029. (defun org-scan-tags (action matcher &optional todo-only)
  9030. "Scan headline tags with inheritance and produce output ACTION.
  9031. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9032. or `agenda' to produce an entry list for an agenda view. It can also be
  9033. a Lisp form or a function that should be called at each matched headline, in
  9034. this case the return value is a list of all return values from these calls.
  9035. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9036. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9037. only lines with a TODO keyword are included in the output."
  9038. (require 'org-agenda)
  9039. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  9040. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9041. (org-re
  9042. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9043. (props (list 'face 'default
  9044. 'done-face 'org-agenda-done
  9045. 'undone-face 'default
  9046. 'mouse-face 'highlight
  9047. 'org-not-done-regexp org-not-done-regexp
  9048. 'org-todo-regexp org-todo-regexp
  9049. 'keymap org-agenda-keymap
  9050. 'help-echo
  9051. (format "mouse-2 or RET jump to org file %s"
  9052. (abbreviate-file-name
  9053. (or (buffer-file-name (buffer-base-buffer))
  9054. (buffer-name (buffer-base-buffer)))))))
  9055. (case-fold-search nil)
  9056. lspos tags tags-list
  9057. (tags-alist (list (cons 0 org-file-tags)))
  9058. (llast 0) rtn rtn1 level category i txt
  9059. todo marker entry priority)
  9060. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9061. (setq action (list 'lambda nil action)))
  9062. (save-excursion
  9063. (goto-char (point-min))
  9064. (when (eq action 'sparse-tree)
  9065. (org-overview)
  9066. (org-remove-occur-highlights))
  9067. (while (re-search-forward re nil t)
  9068. (catch :skip
  9069. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9070. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9071. (goto-char (setq lspos (1+ (match-beginning 0))))
  9072. (setq level (org-reduced-level (funcall outline-level))
  9073. category (org-get-category))
  9074. (setq i llast llast level)
  9075. ;; remove tag lists from same and sublevels
  9076. (while (>= i level)
  9077. (when (setq entry (assoc i tags-alist))
  9078. (setq tags-alist (delete entry tags-alist)))
  9079. (setq i (1- i)))
  9080. ;; add the next tags
  9081. (when tags
  9082. (setq tags (org-split-string tags ":")
  9083. tags-alist
  9084. (cons (cons level tags) tags-alist)))
  9085. ;; compile tags for current headline
  9086. (setq tags-list
  9087. (if org-use-tag-inheritance
  9088. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9089. tags)
  9090. org-scanner-tags tags-list)
  9091. (when org-use-tag-inheritance
  9092. (setcdr (car tags-alist)
  9093. (mapcar (lambda (x)
  9094. (setq x (copy-sequence x))
  9095. (org-add-prop-inherited x))
  9096. (cdar tags-alist))))
  9097. (when (and tags org-use-tag-inheritance
  9098. (or (not (eq t org-use-tag-inheritance))
  9099. org-tags-exclude-from-inheritance))
  9100. ;; selective inheritance, remove uninherited ones
  9101. (setcdr (car tags-alist)
  9102. (org-remove-uniherited-tags (cdar tags-alist))))
  9103. (when (and (or (not todo-only)
  9104. (and (member todo org-not-done-keywords)
  9105. (or (not org-agenda-tags-todo-honor-ignore-options)
  9106. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9107. (let ((case-fold-search t)) (eval matcher))
  9108. (or
  9109. (not (member org-archive-tag tags-list))
  9110. ;; we have an archive tag, should we use this anyway?
  9111. (or (not org-agenda-skip-archived-trees)
  9112. (and (eq action 'agenda) org-agenda-archives-mode))))
  9113. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9114. ;; select this headline
  9115. (cond
  9116. ((eq action 'sparse-tree)
  9117. (and org-highlight-sparse-tree-matches
  9118. (org-get-heading) (match-end 0)
  9119. (org-highlight-new-match
  9120. (match-beginning 0) (match-beginning 1)))
  9121. (org-show-context 'tags-tree))
  9122. ((eq action 'agenda)
  9123. (setq txt (org-format-agenda-item
  9124. ""
  9125. (concat
  9126. (if (eq org-tags-match-list-sublevels 'indented)
  9127. (make-string (1- level) ?.) "")
  9128. (org-get-heading))
  9129. category
  9130. tags-list
  9131. )
  9132. priority (org-get-priority txt))
  9133. (goto-char lspos)
  9134. (setq marker (org-agenda-new-marker))
  9135. (org-add-props txt props
  9136. 'org-marker marker 'org-hd-marker marker 'org-category category
  9137. 'todo-state todo
  9138. 'priority priority 'type "tagsmatch")
  9139. (push txt rtn))
  9140. ((functionp action)
  9141. (save-excursion
  9142. (setq rtn1 (funcall action))
  9143. (push rtn1 rtn))
  9144. (goto-char (point-at-eol)))
  9145. (t (error "Invalid action")))
  9146. ;; if we are to skip sublevels, jump to end of subtree
  9147. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  9148. (when (and (eq action 'sparse-tree)
  9149. (not org-sparse-tree-open-archived-trees))
  9150. (org-hide-archived-subtrees (point-min) (point-max)))
  9151. (nreverse rtn)))
  9152. (defun org-remove-uniherited-tags (tags)
  9153. "Remove all tags that are not inherited from the list TAGS."
  9154. (cond
  9155. ((eq org-use-tag-inheritance t)
  9156. (if org-tags-exclude-from-inheritance
  9157. (org-delete-all org-tags-exclude-from-inheritance tags)
  9158. tags))
  9159. ((not org-use-tag-inheritance) nil)
  9160. ((stringp org-use-tag-inheritance)
  9161. (delq nil (mapcar
  9162. (lambda (x)
  9163. (if (and (string-match org-use-tag-inheritance x)
  9164. (not (member x org-tags-exclude-from-inheritance)))
  9165. x nil))
  9166. tags)))
  9167. ((listp org-use-tag-inheritance)
  9168. (delq nil (mapcar
  9169. (lambda (x)
  9170. (if (member x org-use-tag-inheritance) x nil))
  9171. tags)))))
  9172. (defvar todo-only) ;; dynamically scoped
  9173. (defun org-match-sparse-tree (&optional todo-only match)
  9174. "Create a sparse tree according to tags string MATCH.
  9175. MATCH can contain positive and negative selection of tags, like
  9176. \"+WORK+URGENT-WITHBOSS\".
  9177. If optional argument TODO-ONLY is non-nil, only select lines that are
  9178. also TODO lines."
  9179. (interactive "P")
  9180. (org-prepare-agenda-buffers (list (current-buffer)))
  9181. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9182. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9183. (defvar org-cached-props nil)
  9184. (defun org-cached-entry-get (pom property)
  9185. (if (or (eq t org-use-property-inheritance)
  9186. (and (stringp org-use-property-inheritance)
  9187. (string-match org-use-property-inheritance property))
  9188. (and (listp org-use-property-inheritance)
  9189. (member property org-use-property-inheritance)))
  9190. ;; Caching is not possible, check it directly
  9191. (org-entry-get pom property 'inherit)
  9192. ;; Get all properties, so that we can do complicated checks easily
  9193. (cdr (assoc property (or org-cached-props
  9194. (setq org-cached-props
  9195. (org-entry-properties pom)))))))
  9196. (defun org-global-tags-completion-table (&optional files)
  9197. "Return the list of all tags in all agenda buffer/files."
  9198. (save-excursion
  9199. (org-uniquify
  9200. (delq nil
  9201. (apply 'append
  9202. (mapcar
  9203. (lambda (file)
  9204. (set-buffer (find-file-noselect file))
  9205. (append (org-get-buffer-tags)
  9206. (mapcar (lambda (x) (if (stringp (car-safe x))
  9207. (list (car-safe x)) nil))
  9208. org-tag-alist)))
  9209. (if (and files (car files))
  9210. files
  9211. (org-agenda-files))))))))
  9212. (defun org-make-tags-matcher (match)
  9213. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9214. ;; todo-only is scoped dynamically into this function, and the function
  9215. ;; may change it if the matcher asks for it.
  9216. (unless match
  9217. ;; Get a new match request, with completion
  9218. (let ((org-last-tags-completion-table
  9219. (org-global-tags-completion-table)))
  9220. (setq match (org-completing-read-no-ido
  9221. "Match: " 'org-tags-completion-function nil nil nil
  9222. 'org-tags-history))))
  9223. ;; Parse the string and create a lisp form
  9224. (let ((match0 match)
  9225. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9226. minus tag mm
  9227. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9228. orterms term orlist re-p str-p level-p level-op time-p
  9229. prop-p pn pv po cat-p gv rest)
  9230. (if (string-match "/+" match)
  9231. ;; match contains also a todo-matching request
  9232. (progn
  9233. (setq tagsmatch (substring match 0 (match-beginning 0))
  9234. todomatch (substring match (match-end 0)))
  9235. (if (string-match "^!" todomatch)
  9236. (setq todo-only t todomatch (substring todomatch 1)))
  9237. (if (string-match "^\\s-*$" todomatch)
  9238. (setq todomatch nil)))
  9239. ;; only matching tags
  9240. (setq tagsmatch match todomatch nil))
  9241. ;; Make the tags matcher
  9242. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9243. (setq tagsmatcher t)
  9244. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9245. (while (setq term (pop orterms))
  9246. (while (and (equal (substring term -1) "\\") orterms)
  9247. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9248. (while (string-match re term)
  9249. (setq rest (substring term (match-end 0))
  9250. minus (and (match-end 1)
  9251. (equal (match-string 1 term) "-"))
  9252. tag (match-string 2 term)
  9253. re-p (equal (string-to-char tag) ?{)
  9254. level-p (match-end 4)
  9255. prop-p (match-end 5)
  9256. mm (cond
  9257. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9258. (level-p
  9259. (setq level-op (org-op-to-function (match-string 3 term)))
  9260. `(,level-op level ,(string-to-number
  9261. (match-string 4 term))))
  9262. (prop-p
  9263. (setq pn (match-string 5 term)
  9264. po (match-string 6 term)
  9265. pv (match-string 7 term)
  9266. cat-p (equal pn "CATEGORY")
  9267. re-p (equal (string-to-char pv) ?{)
  9268. str-p (equal (string-to-char pv) ?\")
  9269. time-p (save-match-data
  9270. (string-match "^\"[[<].*[]>]\"$" pv))
  9271. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9272. (if time-p (setq pv (org-matcher-time pv)))
  9273. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9274. (cond
  9275. ((equal pn "CATEGORY")
  9276. (setq gv '(get-text-property (point) 'org-category)))
  9277. ((equal pn "TODO")
  9278. (setq gv 'todo))
  9279. (t
  9280. (setq gv `(org-cached-entry-get nil ,pn))))
  9281. (if re-p
  9282. (if (eq po 'org<>)
  9283. `(not (string-match ,pv (or ,gv "")))
  9284. `(string-match ,pv (or ,gv "")))
  9285. (if str-p
  9286. `(,po (or ,gv "") ,pv)
  9287. `(,po (string-to-number (or ,gv ""))
  9288. ,(string-to-number pv) ))))
  9289. (t `(member ,tag tags-list)))
  9290. mm (if minus (list 'not mm) mm)
  9291. term rest)
  9292. (push mm tagsmatcher))
  9293. (push (if (> (length tagsmatcher) 1)
  9294. (cons 'and tagsmatcher)
  9295. (car tagsmatcher))
  9296. orlist)
  9297. (setq tagsmatcher nil))
  9298. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9299. (setq tagsmatcher
  9300. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9301. ;; Make the todo matcher
  9302. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9303. (setq todomatcher t)
  9304. (setq orterms (org-split-string todomatch "|") orlist nil)
  9305. (while (setq term (pop orterms))
  9306. (while (string-match re term)
  9307. (setq minus (and (match-end 1)
  9308. (equal (match-string 1 term) "-"))
  9309. kwd (match-string 2 term)
  9310. re-p (equal (string-to-char kwd) ?{)
  9311. term (substring term (match-end 0))
  9312. mm (if re-p
  9313. `(string-match ,(substring kwd 1 -1) todo)
  9314. (list 'equal 'todo kwd))
  9315. mm (if minus (list 'not mm) mm))
  9316. (push mm todomatcher))
  9317. (push (if (> (length todomatcher) 1)
  9318. (cons 'and todomatcher)
  9319. (car todomatcher))
  9320. orlist)
  9321. (setq todomatcher nil))
  9322. (setq todomatcher (if (> (length orlist) 1)
  9323. (cons 'or orlist) (car orlist))))
  9324. ;; Return the string and lisp forms of the matcher
  9325. (setq matcher (if todomatcher
  9326. (list 'and tagsmatcher todomatcher)
  9327. tagsmatcher))
  9328. (cons match0 matcher)))
  9329. (defun org-op-to-function (op &optional stringp)
  9330. "Turn an operator into the appropriate function."
  9331. (setq op
  9332. (cond
  9333. ((equal op "<" ) '(< string< org-time<))
  9334. ((equal op ">" ) '(> org-string> org-time>))
  9335. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9336. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9337. ((member op '("=" "==")) '(= string= org-time=))
  9338. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9339. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9340. (defun org<> (a b) (not (= a b)))
  9341. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9342. (defun org-string>= (a b) (not (string< a b)))
  9343. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9344. (defun org-string<> (a b) (not (string= a b)))
  9345. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9346. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9347. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9348. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9349. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9350. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9351. (defun org-2ft (s)
  9352. "Convert S to a floating point time.
  9353. If S is already a number, just return it. If it is a string, parse
  9354. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9355. (cond
  9356. ((numberp s) s)
  9357. ((stringp s)
  9358. (condition-case nil
  9359. (float-time (apply 'encode-time (org-parse-time-string s)))
  9360. (error 0.)))
  9361. (t 0.)))
  9362. (defun org-time-today ()
  9363. "Time in seconds today at 0:00.
  9364. Returns the float number of seconds since the beginning of the
  9365. epoch to the beginning of today (00:00)."
  9366. (float-time (apply 'encode-time
  9367. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9368. (defun org-matcher-time (s)
  9369. "Interpret a time comparison value."
  9370. (save-match-data
  9371. (cond
  9372. ((string= s "<now>") (float-time))
  9373. ((string= s "<today>") (org-time-today))
  9374. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9375. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9376. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9377. (+ (org-time-today)
  9378. (* (string-to-number (match-string 1 s))
  9379. (cdr (assoc (match-string 2 s)
  9380. '(("d" . 86400.0) ("w" . 604800.0)
  9381. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9382. (t (org-2ft s)))))
  9383. (defun org-match-any-p (re list)
  9384. "Does re match any element of list?"
  9385. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9386. (delq nil list))
  9387. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9388. (defvar org-tags-overlay (org-make-overlay 1 1))
  9389. (org-detach-overlay org-tags-overlay)
  9390. (defun org-get-local-tags-at (&optional pos)
  9391. "Get a list of tags defined in the current headline."
  9392. (org-get-tags-at pos 'local))
  9393. (defun org-get-local-tags ()
  9394. "Get a list of tags defined in the current headline."
  9395. (org-get-tags-at nil 'local))
  9396. (defun org-get-tags-at (&optional pos local)
  9397. "Get a list of all headline tags applicable at POS.
  9398. POS defaults to point. If tags are inherited, the list contains
  9399. the targets in the same sequence as the headlines appear, i.e.
  9400. the tags of the current headline come last.
  9401. When LOCAL is non-nil, only return tags from the current headline,
  9402. ignore inherited ones."
  9403. (interactive)
  9404. (if (and org-trust-scanner-tags
  9405. (or (not pos) (equal pos (point)))
  9406. (not local))
  9407. org-scanner-tags
  9408. (let (tags ltags lastpos parent)
  9409. (save-excursion
  9410. (save-restriction
  9411. (widen)
  9412. (goto-char (or pos (point)))
  9413. (save-match-data
  9414. (catch 'done
  9415. (condition-case nil
  9416. (progn
  9417. (org-back-to-heading t)
  9418. (while (not (equal lastpos (point)))
  9419. (setq lastpos (point))
  9420. (when (looking-at
  9421. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9422. (setq ltags (org-split-string
  9423. (org-match-string-no-properties 1) ":"))
  9424. (when parent
  9425. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9426. (setq tags (append
  9427. (if parent
  9428. (org-remove-uniherited-tags ltags)
  9429. ltags)
  9430. tags)))
  9431. (or org-use-tag-inheritance (throw 'done t))
  9432. (if local (throw 'done t))
  9433. (org-up-heading-all 1)
  9434. (setq parent t)))
  9435. (error nil)))))
  9436. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9437. (defun org-add-prop-inherited (s)
  9438. (add-text-properties 0 (length s) '(inherited t) s)
  9439. s)
  9440. (defun org-toggle-tag (tag &optional onoff)
  9441. "Toggle the tag TAG for the current line.
  9442. If ONOFF is `on' or `off', don't toggle but set to this state."
  9443. (let (res current)
  9444. (save-excursion
  9445. (org-back-to-heading t)
  9446. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9447. (point-at-eol) t)
  9448. (progn
  9449. (setq current (match-string 1))
  9450. (replace-match ""))
  9451. (setq current ""))
  9452. (setq current (nreverse (org-split-string current ":")))
  9453. (cond
  9454. ((eq onoff 'on)
  9455. (setq res t)
  9456. (or (member tag current) (push tag current)))
  9457. ((eq onoff 'off)
  9458. (or (not (member tag current)) (setq current (delete tag current))))
  9459. (t (if (member tag current)
  9460. (setq current (delete tag current))
  9461. (setq res t)
  9462. (push tag current))))
  9463. (end-of-line 1)
  9464. (if current
  9465. (progn
  9466. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9467. (org-set-tags nil t))
  9468. (delete-horizontal-space))
  9469. (run-hooks 'org-after-tags-change-hook))
  9470. res))
  9471. (defun org-align-tags-here (to-col)
  9472. ;; Assumes that this is a headline
  9473. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9474. (beginning-of-line 1)
  9475. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9476. (< pos (match-beginning 2)))
  9477. (progn
  9478. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9479. (goto-char (match-beginning 1))
  9480. (insert " ")
  9481. (delete-region (point) (1+ (match-beginning 2)))
  9482. (setq ncol (max (1+ (current-column))
  9483. (1+ col)
  9484. (if (> to-col 0)
  9485. to-col
  9486. (- (abs to-col) tags-l))))
  9487. (setq p (point))
  9488. (insert (make-string (- ncol (current-column)) ?\ ))
  9489. (setq ncol (current-column))
  9490. (when indent-tabs-mode (tabify p (point-at-eol)))
  9491. (org-move-to-column (min ncol col) t))
  9492. (goto-char pos))))
  9493. (defun org-set-tags-command (&optional arg just-align)
  9494. "Call the set-tags command for the current entry."
  9495. (interactive "P")
  9496. (if (org-on-heading-p)
  9497. (org-set-tags arg just-align)
  9498. (save-excursion
  9499. (org-back-to-heading t)
  9500. (org-set-tags arg just-align))))
  9501. (defun org-set-tags (&optional arg just-align)
  9502. "Set the tags for the current headline.
  9503. With prefix ARG, realign all tags in headings in the current buffer."
  9504. (interactive "P")
  9505. (let* ((re (concat "^" outline-regexp))
  9506. (current (org-get-tags-string))
  9507. (col (current-column))
  9508. (org-setting-tags t)
  9509. table current-tags inherited-tags ; computed below when needed
  9510. tags p0 c0 c1 rpl)
  9511. (if arg
  9512. (save-excursion
  9513. (goto-char (point-min))
  9514. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9515. (while (re-search-forward re nil t)
  9516. (org-set-tags nil t)
  9517. (end-of-line 1)))
  9518. (message "All tags realigned to column %d" org-tags-column))
  9519. (if just-align
  9520. (setq tags current)
  9521. ;; Get a new set of tags from the user
  9522. (save-excursion
  9523. (setq table (append org-tag-persistent-alist
  9524. (or org-tag-alist (org-get-buffer-tags)))
  9525. org-last-tags-completion-table table
  9526. current-tags (org-split-string current ":")
  9527. inherited-tags (nreverse
  9528. (nthcdr (length current-tags)
  9529. (nreverse (org-get-tags-at))))
  9530. tags
  9531. (if (or (eq t org-use-fast-tag-selection)
  9532. (and org-use-fast-tag-selection
  9533. (delq nil (mapcar 'cdr table))))
  9534. (org-fast-tag-selection
  9535. current-tags inherited-tags table
  9536. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9537. (let ((org-add-colon-after-tag-completion t))
  9538. (org-trim
  9539. (org-without-partial-completion
  9540. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9541. nil nil current 'org-tags-history)))))))
  9542. (while (string-match "[-+&]+" tags)
  9543. ;; No boolean logic, just a list
  9544. (setq tags (replace-match ":" t t tags))))
  9545. (if (string-match "\\`[\t ]*\\'" tags)
  9546. (setq tags "")
  9547. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9548. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9549. ;; Insert new tags at the correct column
  9550. (beginning-of-line 1)
  9551. (cond
  9552. ((and (equal current "") (equal tags "")))
  9553. ((re-search-forward
  9554. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9555. (point-at-eol) t)
  9556. (if (equal tags "")
  9557. (setq rpl "")
  9558. (goto-char (match-beginning 0))
  9559. (setq c0 (current-column) p0 (point)
  9560. c1 (max (1+ c0) (if (> org-tags-column 0)
  9561. org-tags-column
  9562. (- (- org-tags-column) (length tags))))
  9563. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9564. (replace-match rpl t t)
  9565. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9566. tags)
  9567. (t (error "Tags alignment failed")))
  9568. (org-move-to-column col)
  9569. (unless just-align
  9570. (run-hooks 'org-after-tags-change-hook)))))
  9571. (defun org-change-tag-in-region (beg end tag off)
  9572. "Add or remove TAG for each entry in the region.
  9573. This works in the agenda, and also in an org-mode buffer."
  9574. (interactive
  9575. (list (region-beginning) (region-end)
  9576. (let ((org-last-tags-completion-table
  9577. (if (org-mode-p)
  9578. (org-get-buffer-tags)
  9579. (org-global-tags-completion-table))))
  9580. (org-ido-completing-read
  9581. "Tag: " 'org-tags-completion-function nil nil nil
  9582. 'org-tags-history))
  9583. (progn
  9584. (message "[s]et or [r]emove? ")
  9585. (equal (read-char-exclusive) ?r))))
  9586. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9587. (let ((agendap (equal major-mode 'org-agenda-mode))
  9588. l1 l2 m buf pos newhead (cnt 0))
  9589. (goto-char end)
  9590. (setq l2 (1- (org-current-line)))
  9591. (goto-char beg)
  9592. (setq l1 (org-current-line))
  9593. (loop for l from l1 to l2 do
  9594. (goto-line l)
  9595. (setq m (get-text-property (point) 'org-hd-marker))
  9596. (when (or (and (org-mode-p) (org-on-heading-p))
  9597. (and agendap m))
  9598. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9599. pos (if agendap m (point)))
  9600. (with-current-buffer buf
  9601. (save-excursion
  9602. (save-restriction
  9603. (goto-char pos)
  9604. (setq cnt (1+ cnt))
  9605. (org-toggle-tag tag (if off 'off 'on))
  9606. (setq newhead (org-get-heading)))))
  9607. (and agendap (org-agenda-change-all-lines newhead m))))
  9608. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9609. (defun org-tags-completion-function (string predicate &optional flag)
  9610. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9611. (confirm (lambda (x) (stringp (car x)))))
  9612. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9613. (setq s1 (match-string 1 string)
  9614. s2 (match-string 2 string))
  9615. (setq s1 "" s2 string))
  9616. (cond
  9617. ((eq flag nil)
  9618. ;; try completion
  9619. (setq rtn (try-completion s2 ctable confirm))
  9620. (if (stringp rtn)
  9621. (setq rtn
  9622. (concat s1 s2 (substring rtn (length s2))
  9623. (if (and org-add-colon-after-tag-completion
  9624. (assoc rtn ctable))
  9625. ":" ""))))
  9626. rtn)
  9627. ((eq flag t)
  9628. ;; all-completions
  9629. (all-completions s2 ctable confirm)
  9630. )
  9631. ((eq flag 'lambda)
  9632. ;; exact match?
  9633. (assoc s2 ctable)))
  9634. ))
  9635. (defun org-fast-tag-insert (kwd tags face &optional end)
  9636. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9637. (insert (format "%-12s" (concat kwd ":"))
  9638. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9639. (or end "")))
  9640. (defun org-fast-tag-show-exit (flag)
  9641. (save-excursion
  9642. (goto-line 3)
  9643. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9644. (replace-match ""))
  9645. (when flag
  9646. (end-of-line 1)
  9647. (org-move-to-column (- (window-width) 19) t)
  9648. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9649. (defun org-set-current-tags-overlay (current prefix)
  9650. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9651. (if (featurep 'xemacs)
  9652. (org-overlay-display org-tags-overlay (concat prefix s)
  9653. 'secondary-selection)
  9654. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9655. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9656. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9657. "Fast tag selection with single keys.
  9658. CURRENT is the current list of tags in the headline, INHERITED is the
  9659. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9660. possibly with grouping information. TODO-TABLE is a similar table with
  9661. TODO keywords, should these have keys assigned to them.
  9662. If the keys are nil, a-z are automatically assigned.
  9663. Returns the new tags string, or nil to not change the current settings."
  9664. (let* ((fulltable (append table todo-table))
  9665. (maxlen (apply 'max (mapcar
  9666. (lambda (x)
  9667. (if (stringp (car x)) (string-width (car x)) 0))
  9668. fulltable)))
  9669. (buf (current-buffer))
  9670. (expert (eq org-fast-tag-selection-single-key 'expert))
  9671. (buffer-tags nil)
  9672. (fwidth (+ maxlen 3 1 3))
  9673. (ncol (/ (- (window-width) 4) fwidth))
  9674. (i-face 'org-done)
  9675. (c-face 'org-todo)
  9676. tg cnt e c char c1 c2 ntable tbl rtn
  9677. ov-start ov-end ov-prefix
  9678. (exit-after-next org-fast-tag-selection-single-key)
  9679. (done-keywords org-done-keywords)
  9680. groups ingroup)
  9681. (save-excursion
  9682. (beginning-of-line 1)
  9683. (if (looking-at
  9684. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9685. (setq ov-start (match-beginning 1)
  9686. ov-end (match-end 1)
  9687. ov-prefix "")
  9688. (setq ov-start (1- (point-at-eol))
  9689. ov-end (1+ ov-start))
  9690. (skip-chars-forward "^\n\r")
  9691. (setq ov-prefix
  9692. (concat
  9693. (buffer-substring (1- (point)) (point))
  9694. (if (> (current-column) org-tags-column)
  9695. " "
  9696. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9697. (org-move-overlay org-tags-overlay ov-start ov-end)
  9698. (save-window-excursion
  9699. (if expert
  9700. (set-buffer (get-buffer-create " *Org tags*"))
  9701. (delete-other-windows)
  9702. (split-window-vertically)
  9703. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9704. (erase-buffer)
  9705. (org-set-local 'org-done-keywords done-keywords)
  9706. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9707. (org-fast-tag-insert "Current" current c-face "\n\n")
  9708. (org-fast-tag-show-exit exit-after-next)
  9709. (org-set-current-tags-overlay current ov-prefix)
  9710. (setq tbl fulltable char ?a cnt 0)
  9711. (while (setq e (pop tbl))
  9712. (cond
  9713. ((equal e '(:startgroup))
  9714. (push '() groups) (setq ingroup t)
  9715. (when (not (= cnt 0))
  9716. (setq cnt 0)
  9717. (insert "\n"))
  9718. (insert "{ "))
  9719. ((equal e '(:endgroup))
  9720. (setq ingroup nil cnt 0)
  9721. (insert "}\n"))
  9722. ((equal e '(:newline))
  9723. (when (not (= cnt 0))
  9724. (setq cnt 0)
  9725. (insert "\n")
  9726. (setq e (car tbl))
  9727. (while (equal (car tbl) '(:newline))
  9728. (insert "\n")
  9729. (setq tbl (cdr tbl)))))
  9730. (t
  9731. (setq tg (car e) c2 nil)
  9732. (if (cdr e)
  9733. (setq c (cdr e))
  9734. ;; automatically assign a character.
  9735. (setq c1 (string-to-char
  9736. (downcase (substring
  9737. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9738. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9739. (while (or (rassoc char ntable) (rassoc char table))
  9740. (setq char (1+ char)))
  9741. (setq c2 c1))
  9742. (setq c (or c2 char)))
  9743. (if ingroup (push tg (car groups)))
  9744. (setq tg (org-add-props tg nil 'face
  9745. (cond
  9746. ((not (assoc tg table))
  9747. (org-get-todo-face tg))
  9748. ((member tg current) c-face)
  9749. ((member tg inherited) i-face)
  9750. (t nil))))
  9751. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9752. (insert "[" c "] " tg (make-string
  9753. (- fwidth 4 (length tg)) ?\ ))
  9754. (push (cons tg c) ntable)
  9755. (when (= (setq cnt (1+ cnt)) ncol)
  9756. (insert "\n")
  9757. (if ingroup (insert " "))
  9758. (setq cnt 0)))))
  9759. (setq ntable (nreverse ntable))
  9760. (insert "\n")
  9761. (goto-char (point-min))
  9762. (if (not expert) (org-fit-window-to-buffer))
  9763. (setq rtn
  9764. (catch 'exit
  9765. (while t
  9766. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9767. (if groups " [!] no groups" " [!]groups")
  9768. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9769. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9770. (cond
  9771. ((= c ?\r) (throw 'exit t))
  9772. ((= c ?!)
  9773. (setq groups (not groups))
  9774. (goto-char (point-min))
  9775. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9776. ((= c ?\C-c)
  9777. (if (not expert)
  9778. (org-fast-tag-show-exit
  9779. (setq exit-after-next (not exit-after-next)))
  9780. (setq expert nil)
  9781. (delete-other-windows)
  9782. (split-window-vertically)
  9783. (org-switch-to-buffer-other-window " *Org tags*")
  9784. (org-fit-window-to-buffer)))
  9785. ((or (= c ?\C-g)
  9786. (and (= c ?q) (not (rassoc c ntable))))
  9787. (org-detach-overlay org-tags-overlay)
  9788. (setq quit-flag t))
  9789. ((= c ?\ )
  9790. (setq current nil)
  9791. (if exit-after-next (setq exit-after-next 'now)))
  9792. ((= c ?\t)
  9793. (condition-case nil
  9794. (setq tg (org-ido-completing-read
  9795. "Tag: "
  9796. (or buffer-tags
  9797. (with-current-buffer buf
  9798. (org-get-buffer-tags)))))
  9799. (quit (setq tg "")))
  9800. (when (string-match "\\S-" tg)
  9801. (add-to-list 'buffer-tags (list tg))
  9802. (if (member tg current)
  9803. (setq current (delete tg current))
  9804. (push tg current)))
  9805. (if exit-after-next (setq exit-after-next 'now)))
  9806. ((setq e (rassoc c todo-table) tg (car e))
  9807. (with-current-buffer buf
  9808. (save-excursion (org-todo tg)))
  9809. (if exit-after-next (setq exit-after-next 'now)))
  9810. ((setq e (rassoc c ntable) tg (car e))
  9811. (if (member tg current)
  9812. (setq current (delete tg current))
  9813. (loop for g in groups do
  9814. (if (member tg g)
  9815. (mapc (lambda (x)
  9816. (setq current (delete x current)))
  9817. g)))
  9818. (push tg current))
  9819. (if exit-after-next (setq exit-after-next 'now))))
  9820. ;; Create a sorted list
  9821. (setq current
  9822. (sort current
  9823. (lambda (a b)
  9824. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9825. (if (eq exit-after-next 'now) (throw 'exit t))
  9826. (goto-char (point-min))
  9827. (beginning-of-line 2)
  9828. (delete-region (point) (point-at-eol))
  9829. (org-fast-tag-insert "Current" current c-face)
  9830. (org-set-current-tags-overlay current ov-prefix)
  9831. (while (re-search-forward
  9832. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9833. (setq tg (match-string 1))
  9834. (add-text-properties
  9835. (match-beginning 1) (match-end 1)
  9836. (list 'face
  9837. (cond
  9838. ((member tg current) c-face)
  9839. ((member tg inherited) i-face)
  9840. (t (get-text-property (match-beginning 1) 'face))))))
  9841. (goto-char (point-min)))))
  9842. (org-detach-overlay org-tags-overlay)
  9843. (if rtn
  9844. (mapconcat 'identity current ":")
  9845. nil))))
  9846. (defun org-get-tags-string ()
  9847. "Get the TAGS string in the current headline."
  9848. (unless (org-on-heading-p t)
  9849. (error "Not on a heading"))
  9850. (save-excursion
  9851. (beginning-of-line 1)
  9852. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9853. (org-match-string-no-properties 1)
  9854. "")))
  9855. (defun org-get-tags ()
  9856. "Get the list of tags specified in the current headline."
  9857. (org-split-string (org-get-tags-string) ":"))
  9858. (defun org-get-buffer-tags ()
  9859. "Get a table of all tags used in the buffer, for completion."
  9860. (let (tags)
  9861. (save-excursion
  9862. (goto-char (point-min))
  9863. (while (re-search-forward
  9864. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9865. (when (equal (char-after (point-at-bol 0)) ?*)
  9866. (mapc (lambda (x) (add-to-list 'tags x))
  9867. (org-split-string (org-match-string-no-properties 1) ":")))))
  9868. (mapcar 'list tags)))
  9869. ;;;; The mapping API
  9870. ;;;###autoload
  9871. (defun org-map-entries (func &optional match scope &rest skip)
  9872. "Call FUNC at each headline selected by MATCH in SCOPE.
  9873. FUNC is a function or a lisp form. The function will be called without
  9874. arguments, with the cursor positioned at the beginning of the headline.
  9875. The return values of all calls to the function will be collected and
  9876. returned as a list.
  9877. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9878. Only headlines that are matched by this query will be considered during
  9879. the iteration. When MATCH is nil or t, all headlines will be
  9880. visited by the iteration.
  9881. SCOPE determines the scope of this command. It can be any of:
  9882. nil The current buffer, respecting the restriction if any
  9883. tree The subtree started with the entry at point
  9884. file The current buffer, without restriction
  9885. file-with-archives
  9886. The current buffer, and any archives associated with it
  9887. agenda All agenda files
  9888. agenda-with-archives
  9889. All agenda files with any archive files associated with them
  9890. \(file1 file2 ...)
  9891. If this is a list, all files in the list will be scanned
  9892. The remaining args are treated as settings for the skipping facilities of
  9893. the scanner. The following items can be given here:
  9894. archive skip trees with the archive tag.
  9895. comment skip trees with the COMMENT keyword
  9896. function or Emacs Lisp form:
  9897. will be used as value for `org-agenda-skip-function', so whenever
  9898. the the function returns t, FUNC will not be called for that
  9899. entry and search will continue from the point where the
  9900. function leaves it.
  9901. If your function needs to retrieve the tags including inherited tags
  9902. at the *current* entry, you can use the value of the variable
  9903. `org-scanner-tags' which will be much faster than getting the value
  9904. with `org-get-tags-at'. If your function gets properties with
  9905. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9906. to t around the call to `org-entry-properties' to get the same speedup.
  9907. Note that if your function moves around to retrieve tags and properties at
  9908. a *different* entry, you cannot use these techniques."
  9909. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9910. (org-agenda-skip-archived-trees (memq 'archive skip))
  9911. (org-agenda-skip-comment-trees (memq 'comment skip))
  9912. (org-agenda-skip-function
  9913. (car (org-delete-all '(comment archive) skip)))
  9914. (org-tags-match-list-sublevels t)
  9915. matcher file res
  9916. org-todo-keywords-for-agenda
  9917. org-done-keywords-for-agenda
  9918. org-todo-keyword-alist-for-agenda
  9919. org-tag-alist-for-agenda)
  9920. (cond
  9921. ((eq match t) (setq matcher t))
  9922. ((eq match nil) (setq matcher t))
  9923. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9924. (save-excursion
  9925. (save-restriction
  9926. (when (eq scope 'tree)
  9927. (org-back-to-heading t)
  9928. (org-narrow-to-subtree)
  9929. (setq scope nil))
  9930. (if (not scope)
  9931. (progn
  9932. (org-prepare-agenda-buffers
  9933. (list (buffer-file-name (current-buffer))))
  9934. (setq res (org-scan-tags func matcher)))
  9935. ;; Get the right scope
  9936. (cond
  9937. ((and scope (listp scope) (symbolp (car scope)))
  9938. (setq scope (eval scope)))
  9939. ((eq scope 'agenda)
  9940. (setq scope (org-agenda-files t)))
  9941. ((eq scope 'agenda-with-archives)
  9942. (setq scope (org-agenda-files t))
  9943. (setq scope (org-add-archive-files scope)))
  9944. ((eq scope 'file)
  9945. (setq scope (list (buffer-file-name))))
  9946. ((eq scope 'file-with-archives)
  9947. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9948. (org-prepare-agenda-buffers scope)
  9949. (while (setq file (pop scope))
  9950. (with-current-buffer (org-find-base-buffer-visiting file)
  9951. (save-excursion
  9952. (save-restriction
  9953. (widen)
  9954. (goto-char (point-min))
  9955. (setq res (append res (org-scan-tags func matcher))))))))))
  9956. res))
  9957. ;;;; Properties
  9958. ;;; Setting and retrieving properties
  9959. (defconst org-special-properties
  9960. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9961. "TIMESTAMP" "TIMESTAMP_IA")
  9962. "The special properties valid in Org-mode.
  9963. These are properties that are not defined in the property drawer,
  9964. but in some other way.")
  9965. (defconst org-default-properties
  9966. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9967. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9968. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9969. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9970. "Some properties that are used by Org-mode for various purposes.
  9971. Being in this list makes sure that they are offered for completion.")
  9972. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9973. "Regular expression matching the first line of a property drawer.")
  9974. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9975. "Regular expression matching the first line of a property drawer.")
  9976. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9977. "Regular expression matching the first line of a property drawer.")
  9978. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9979. "Regular expression matching the first line of a property drawer.")
  9980. (defconst org-property-drawer-re
  9981. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9982. org-property-end-re "\\)\n?")
  9983. "Matches an entire property drawer.")
  9984. (defconst org-clock-drawer-re
  9985. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9986. org-property-end-re "\\)\n?")
  9987. "Matches an entire clock drawer.")
  9988. (defun org-property-action ()
  9989. "Do an action on properties."
  9990. (interactive)
  9991. (let (c)
  9992. (org-at-property-p)
  9993. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9994. (setq c (read-char-exclusive))
  9995. (cond
  9996. ((equal c ?s)
  9997. (call-interactively 'org-set-property))
  9998. ((equal c ?d)
  9999. (call-interactively 'org-delete-property))
  10000. ((equal c ?D)
  10001. (call-interactively 'org-delete-property-globally))
  10002. ((equal c ?c)
  10003. (call-interactively 'org-compute-property-at-point))
  10004. (t (error "No such property action %c" c)))))
  10005. (defun org-at-property-p ()
  10006. "Is the cursor in a property line?"
  10007. ;; FIXME: Does not check if we are actually in the drawer.
  10008. ;; FIXME: also returns true on any drawers.....
  10009. ;; This is used by C-c C-c for property action.
  10010. (save-excursion
  10011. (beginning-of-line 1)
  10012. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10013. (defun org-get-property-block (&optional beg end force)
  10014. "Return the (beg . end) range of the body of the property drawer.
  10015. BEG and END can be beginning and end of subtree, if not given
  10016. they will be found.
  10017. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10018. (catch 'exit
  10019. (save-excursion
  10020. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10021. (end (or end (progn (outline-next-heading) (point)))))
  10022. (goto-char beg)
  10023. (if (re-search-forward org-property-start-re end t)
  10024. (setq beg (1+ (match-end 0)))
  10025. (if force
  10026. (save-excursion
  10027. (org-insert-property-drawer)
  10028. (setq end (progn (outline-next-heading) (point))))
  10029. (throw 'exit nil))
  10030. (goto-char beg)
  10031. (if (re-search-forward org-property-start-re end t)
  10032. (setq beg (1+ (match-end 0)))))
  10033. (if (re-search-forward org-property-end-re end t)
  10034. (setq end (match-beginning 0))
  10035. (or force (throw 'exit nil))
  10036. (goto-char beg)
  10037. (setq end beg)
  10038. (org-indent-line-function)
  10039. (insert ":END:\n"))
  10040. (cons beg end)))))
  10041. (defun org-entry-properties (&optional pom which)
  10042. "Get all properties of the entry at point-or-marker POM.
  10043. This includes the TODO keyword, the tags, time strings for deadline,
  10044. scheduled, and clocking, and any additional properties defined in the
  10045. entry. The return value is an alist, keys may occur multiple times
  10046. if the property key was used several times.
  10047. POM may also be nil, in which case the current entry is used.
  10048. If WHICH is nil or `all', get all properties. If WHICH is
  10049. `special' or `standard', only get that subclass."
  10050. (setq which (or which 'all))
  10051. (org-with-point-at pom
  10052. (let ((clockstr (substring org-clock-string 0 -1))
  10053. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10054. beg end range props sum-props key value string clocksum)
  10055. (save-excursion
  10056. (when (condition-case nil
  10057. (and (org-mode-p) (org-back-to-heading t))
  10058. (error nil))
  10059. (setq beg (point))
  10060. (setq sum-props (get-text-property (point) 'org-summaries))
  10061. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10062. (outline-next-heading)
  10063. (setq end (point))
  10064. (when (memq which '(all special))
  10065. ;; Get the special properties, like TODO and tags
  10066. (goto-char beg)
  10067. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10068. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10069. (when (looking-at org-priority-regexp)
  10070. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10071. (when (and (setq value (org-get-tags-string))
  10072. (string-match "\\S-" value))
  10073. (push (cons "TAGS" value) props))
  10074. (when (setq value (org-get-tags-at))
  10075. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10076. props))
  10077. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10078. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10079. string (if (equal key clockstr)
  10080. (org-no-properties
  10081. (org-trim
  10082. (buffer-substring
  10083. (match-beginning 3) (goto-char (point-at-eol)))))
  10084. (substring (org-match-string-no-properties 3) 1 -1)))
  10085. (unless key
  10086. (if (= (char-after (match-beginning 3)) ?\[)
  10087. (setq key "TIMESTAMP_IA")
  10088. (setq key "TIMESTAMP")))
  10089. (when (or (equal key clockstr) (not (assoc key props)))
  10090. (push (cons key string) props)))
  10091. )
  10092. (when (memq which '(all standard))
  10093. ;; Get the standard properties, like :PROP: ...
  10094. (setq range (org-get-property-block beg end))
  10095. (when range
  10096. (goto-char (car range))
  10097. (while (re-search-forward
  10098. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10099. (cdr range) t)
  10100. (setq key (org-match-string-no-properties 1)
  10101. value (org-trim (or (org-match-string-no-properties 2) "")))
  10102. (unless (member key excluded)
  10103. (push (cons key (or value "")) props)))))
  10104. (if clocksum
  10105. (push (cons "CLOCKSUM"
  10106. (org-columns-number-to-string (/ (float clocksum) 60.)
  10107. 'add_times))
  10108. props))
  10109. (unless (assoc "CATEGORY" props)
  10110. (setq value (or (org-get-category)
  10111. (progn (org-refresh-category-properties)
  10112. (org-get-category))))
  10113. (push (cons "CATEGORY" value) props))
  10114. (append sum-props (nreverse props)))))))
  10115. (defun org-entry-get (pom property &optional inherit)
  10116. "Get value of PROPERTY for entry at point-or-marker POM.
  10117. If INHERIT is non-nil and the entry does not have the property,
  10118. then also check higher levels of the hierarchy.
  10119. If INHERIT is the symbol `selective', use inheritance only if the setting
  10120. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10121. If the property is present but empty, the return value is the empty string.
  10122. If the property is not present at all, nil is returned."
  10123. (org-with-point-at pom
  10124. (if (and inherit (if (eq inherit 'selective)
  10125. (org-property-inherit-p property)
  10126. t))
  10127. (org-entry-get-with-inheritance property)
  10128. (if (member property org-special-properties)
  10129. ;; We need a special property. Use brute force, get all properties.
  10130. (cdr (assoc property (org-entry-properties nil 'special)))
  10131. (let ((range (org-get-property-block)))
  10132. (if (and range
  10133. (goto-char (car range))
  10134. (re-search-forward
  10135. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10136. (cdr range) t))
  10137. ;; Found the property, return it.
  10138. (if (match-end 1)
  10139. (org-match-string-no-properties 1)
  10140. "")))))))
  10141. (defun org-property-or-variable-value (var &optional inherit)
  10142. "Check if there is a property fixing the value of VAR.
  10143. If yes, return this value. If not, return the current value of the variable."
  10144. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10145. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10146. (read prop)
  10147. (symbol-value var))))
  10148. (defun org-entry-delete (pom property)
  10149. "Delete the property PROPERTY from entry at point-or-marker POM."
  10150. (org-with-point-at pom
  10151. (if (member property org-special-properties)
  10152. nil ; cannot delete these properties.
  10153. (let ((range (org-get-property-block)))
  10154. (if (and range
  10155. (goto-char (car range))
  10156. (re-search-forward
  10157. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10158. (cdr range) t))
  10159. (progn
  10160. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10161. t)
  10162. nil)))))
  10163. ;; Multi-values properties are properties that contain multiple values
  10164. ;; These values are assumed to be single words, separated by whitespace.
  10165. (defun org-entry-add-to-multivalued-property (pom property value)
  10166. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10167. (let* ((old (org-entry-get pom property))
  10168. (values (and old (org-split-string old "[ \t]"))))
  10169. (setq value (org-entry-protect-space value))
  10170. (unless (member value values)
  10171. (setq values (cons value values))
  10172. (org-entry-put pom property
  10173. (mapconcat 'identity values " ")))))
  10174. (defun org-entry-remove-from-multivalued-property (pom property value)
  10175. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10176. (let* ((old (org-entry-get pom property))
  10177. (values (and old (org-split-string old "[ \t]"))))
  10178. (setq value (org-entry-protect-space value))
  10179. (when (member value values)
  10180. (setq values (delete value values))
  10181. (org-entry-put pom property
  10182. (mapconcat 'identity values " ")))))
  10183. (defun org-entry-member-in-multivalued-property (pom property value)
  10184. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10185. (let* ((old (org-entry-get pom property))
  10186. (values (and old (org-split-string old "[ \t]"))))
  10187. (setq value (org-entry-protect-space value))
  10188. (member value values)))
  10189. (defun org-entry-get-multivalued-property (pom property)
  10190. "Return a list of values in a multivalued property."
  10191. (let* ((value (org-entry-get pom property))
  10192. (values (and value (org-split-string value "[ \t]"))))
  10193. (mapcar 'org-entry-restore-space values)))
  10194. (defun org-entry-put-multivalued-property (pom property &rest values)
  10195. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10196. VALUES should be a list of strings. Spaces will be protected."
  10197. (org-entry-put pom property
  10198. (mapconcat 'org-entry-protect-space values " "))
  10199. (let* ((value (org-entry-get pom property))
  10200. (values (and value (org-split-string value "[ \t]"))))
  10201. (mapcar 'org-entry-restore-space values)))
  10202. (defun org-entry-protect-space (s)
  10203. "Protect spaces and newline in string S."
  10204. (while (string-match " " s)
  10205. (setq s (replace-match "%20" t t s)))
  10206. (while (string-match "\n" s)
  10207. (setq s (replace-match "%0A" t t s)))
  10208. s)
  10209. (defun org-entry-restore-space (s)
  10210. "Restore spaces and newline in string S."
  10211. (while (string-match "%20" s)
  10212. (setq s (replace-match " " t t s)))
  10213. (while (string-match "%0A" s)
  10214. (setq s (replace-match "\n" t t s)))
  10215. s)
  10216. (defvar org-entry-property-inherited-from (make-marker)
  10217. "Marker pointing to the entry from where a property was inherited.
  10218. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10219. location of the entry where the inheritance search matched. If there was
  10220. no match, the marker will point nowhere.
  10221. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10222. is set.")
  10223. (defun org-entry-get-with-inheritance (property)
  10224. "Get entry property, and search higher levels if not present."
  10225. (move-marker org-entry-property-inherited-from nil)
  10226. (let (tmp)
  10227. (save-excursion
  10228. (save-restriction
  10229. (widen)
  10230. (catch 'ex
  10231. (while t
  10232. (when (setq tmp (org-entry-get nil property))
  10233. (org-back-to-heading t)
  10234. (move-marker org-entry-property-inherited-from (point))
  10235. (throw 'ex tmp))
  10236. (or (org-up-heading-safe) (throw 'ex nil)))))
  10237. (or tmp
  10238. (cdr (assoc property org-file-properties))
  10239. (cdr (assoc property org-global-properties))
  10240. (cdr (assoc property org-global-properties-fixed))))))
  10241. (defun org-entry-put (pom property value)
  10242. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10243. (org-with-point-at pom
  10244. (org-back-to-heading t)
  10245. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10246. range)
  10247. (cond
  10248. ((equal property "TODO")
  10249. (when (and (stringp value) (string-match "\\S-" value)
  10250. (not (member value org-todo-keywords-1)))
  10251. (error "\"%s\" is not a valid TODO state" value))
  10252. (if (or (not value)
  10253. (not (string-match "\\S-" value)))
  10254. (setq value 'none))
  10255. (org-todo value)
  10256. (org-set-tags nil 'align))
  10257. ((equal property "PRIORITY")
  10258. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10259. (string-to-char value) ?\ ))
  10260. (org-set-tags nil 'align))
  10261. ((equal property "SCHEDULED")
  10262. (if (re-search-forward org-scheduled-time-regexp end t)
  10263. (cond
  10264. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10265. ((eq value 'later) (org-timestamp-change 1 'day))
  10266. (t (call-interactively 'org-schedule)))
  10267. (call-interactively 'org-schedule)))
  10268. ((equal property "DEADLINE")
  10269. (if (re-search-forward org-deadline-time-regexp end t)
  10270. (cond
  10271. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10272. ((eq value 'later) (org-timestamp-change 1 'day))
  10273. (t (call-interactively 'org-deadline)))
  10274. (call-interactively 'org-deadline)))
  10275. ((member property org-special-properties)
  10276. (error "The %s property can not yet be set with `org-entry-put'"
  10277. property))
  10278. (t ; a non-special property
  10279. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10280. (setq range (org-get-property-block beg end 'force))
  10281. (goto-char (car range))
  10282. (if (re-search-forward
  10283. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10284. (progn
  10285. (delete-region (match-beginning 1) (match-end 1))
  10286. (goto-char (match-beginning 1)))
  10287. (goto-char (cdr range))
  10288. (insert "\n")
  10289. (backward-char 1)
  10290. (org-indent-line-function)
  10291. (insert ":" property ":"))
  10292. (and value (insert " " value))
  10293. (org-indent-line-function)))))))
  10294. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10295. "Get all property keys in the current buffer.
  10296. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10297. like tags and TODO state.
  10298. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10299. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10300. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10301. formats in the current buffer."
  10302. (let (rtn range cfmt s p)
  10303. (save-excursion
  10304. (save-restriction
  10305. (widen)
  10306. (goto-char (point-min))
  10307. (while (re-search-forward org-property-start-re nil t)
  10308. (setq range (org-get-property-block))
  10309. (goto-char (car range))
  10310. (while (re-search-forward
  10311. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10312. (cdr range) t)
  10313. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10314. (outline-next-heading))))
  10315. (when include-specials
  10316. (setq rtn (append org-special-properties rtn)))
  10317. (when include-defaults
  10318. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10319. (when include-columns
  10320. (save-excursion
  10321. (save-restriction
  10322. (widen)
  10323. (goto-char (point-min))
  10324. (while (re-search-forward
  10325. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10326. nil t)
  10327. (setq cfmt (match-string 2) s 0)
  10328. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10329. cfmt s)
  10330. (setq s (match-end 0)
  10331. p (match-string 1 cfmt))
  10332. (unless (or (equal p "ITEM")
  10333. (member p org-special-properties))
  10334. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10335. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10336. (defun org-property-values (key)
  10337. "Return a list of all values of property KEY."
  10338. (save-excursion
  10339. (save-restriction
  10340. (widen)
  10341. (goto-char (point-min))
  10342. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10343. values)
  10344. (while (re-search-forward re nil t)
  10345. (add-to-list 'values (org-trim (match-string 1))))
  10346. (delete "" values)))))
  10347. (defun org-insert-property-drawer ()
  10348. "Insert a property drawer into the current entry."
  10349. (interactive)
  10350. (org-back-to-heading t)
  10351. (looking-at outline-regexp)
  10352. (let ((indent (- (match-end 0)(match-beginning 0)))
  10353. (beg (point))
  10354. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10355. end hiddenp)
  10356. (outline-next-heading)
  10357. (setq end (point))
  10358. (goto-char beg)
  10359. (while (re-search-forward re end t))
  10360. (setq hiddenp (org-invisible-p))
  10361. (end-of-line 1)
  10362. (and (equal (char-after) ?\n) (forward-char 1))
  10363. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10364. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10365. ;; just skip this line
  10366. (beginning-of-line 2)
  10367. ;; Drawer start, find the end
  10368. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10369. (beginning-of-line 1)))
  10370. (org-skip-over-state-notes)
  10371. (skip-chars-backward " \t\n\r")
  10372. (if (eq (char-before) ?*) (forward-char 1))
  10373. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10374. (beginning-of-line 0)
  10375. (org-indent-to-column indent)
  10376. (beginning-of-line 2)
  10377. (org-indent-to-column indent)
  10378. (beginning-of-line 0)
  10379. (if hiddenp
  10380. (save-excursion
  10381. (org-back-to-heading t)
  10382. (hide-entry))
  10383. (org-flag-drawer t))))
  10384. (defun org-set-property (property value)
  10385. "In the current entry, set PROPERTY to VALUE.
  10386. When called interactively, this will prompt for a property name, offering
  10387. completion on existing and default properties. And then it will prompt
  10388. for a value, offering completion either on allowed values (via an inherited
  10389. xxx_ALL property) or on existing values in other instances of this property
  10390. in the current file."
  10391. (interactive
  10392. (let* ((completion-ignore-case t)
  10393. (keys (org-buffer-property-keys nil t t))
  10394. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10395. (prop (if (member prop0 keys)
  10396. prop0
  10397. (or (cdr (assoc (downcase prop0)
  10398. (mapcar (lambda (x) (cons (downcase x) x))
  10399. keys)))
  10400. prop0)))
  10401. (cur (org-entry-get nil prop))
  10402. (allowed (org-property-get-allowed-values nil prop 'table))
  10403. (existing (mapcar 'list (org-property-values prop)))
  10404. (val (if allowed
  10405. (org-completing-read "Value: " allowed nil 'req-match)
  10406. (let (org-completion-use-ido)
  10407. (org-completing-read
  10408. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10409. (concat "[" cur "]") "")
  10410. ": ")
  10411. existing nil nil "" nil cur)))))
  10412. (list prop (if (equal val "") cur val))))
  10413. (unless (equal (org-entry-get nil property) value)
  10414. (org-entry-put nil property value)))
  10415. (defun org-delete-property (property)
  10416. "In the current entry, delete PROPERTY."
  10417. (interactive
  10418. (let* ((completion-ignore-case t)
  10419. (prop (org-ido-completing-read
  10420. "Property: " (org-entry-properties nil 'standard))))
  10421. (list prop)))
  10422. (message "Property %s %s" property
  10423. (if (org-entry-delete nil property)
  10424. "deleted"
  10425. "was not present in the entry")))
  10426. (defun org-delete-property-globally (property)
  10427. "Remove PROPERTY globally, from all entries."
  10428. (interactive
  10429. (let* ((completion-ignore-case t)
  10430. (prop (org-ido-completing-read
  10431. "Globally remove property: "
  10432. (mapcar 'list (org-buffer-property-keys)))))
  10433. (list prop)))
  10434. (save-excursion
  10435. (save-restriction
  10436. (widen)
  10437. (goto-char (point-min))
  10438. (let ((cnt 0))
  10439. (while (re-search-forward
  10440. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10441. nil t)
  10442. (setq cnt (1+ cnt))
  10443. (replace-match ""))
  10444. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10445. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10446. (defun org-compute-property-at-point ()
  10447. "Compute the property at point.
  10448. This looks for an enclosing column format, extracts the operator and
  10449. then applies it to the property in the column format's scope."
  10450. (interactive)
  10451. (unless (org-at-property-p)
  10452. (error "Not at a property"))
  10453. (let ((prop (org-match-string-no-properties 2)))
  10454. (org-columns-get-format-and-top-level)
  10455. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10456. (error "No operator defined for property %s" prop))
  10457. (org-columns-compute prop)))
  10458. (defun org-property-get-allowed-values (pom property &optional table)
  10459. "Get allowed values for the property PROPERTY.
  10460. When TABLE is non-nil, return an alist that can directly be used for
  10461. completion."
  10462. (let (vals)
  10463. (cond
  10464. ((equal property "TODO")
  10465. (setq vals (org-with-point-at pom
  10466. (append org-todo-keywords-1 '("")))))
  10467. ((equal property "PRIORITY")
  10468. (let ((n org-lowest-priority))
  10469. (while (>= n org-highest-priority)
  10470. (push (char-to-string n) vals)
  10471. (setq n (1- n)))))
  10472. ((member property org-special-properties))
  10473. (t
  10474. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10475. (when (and vals (string-match "\\S-" vals))
  10476. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10477. (setq vals (mapcar (lambda (x)
  10478. (cond ((stringp x) x)
  10479. ((numberp x) (number-to-string x))
  10480. ((symbolp x) (symbol-name x))
  10481. (t "???")))
  10482. vals)))))
  10483. (if table (mapcar 'list vals) vals)))
  10484. (defun org-property-previous-allowed-value (&optional previous)
  10485. "Switch to the next allowed value for this property."
  10486. (interactive)
  10487. (org-property-next-allowed-value t))
  10488. (defun org-property-next-allowed-value (&optional previous)
  10489. "Switch to the next allowed value for this property."
  10490. (interactive)
  10491. (unless (org-at-property-p)
  10492. (error "Not at a property"))
  10493. (let* ((key (match-string 2))
  10494. (value (match-string 3))
  10495. (allowed (or (org-property-get-allowed-values (point) key)
  10496. (and (member value '("[ ]" "[-]" "[X]"))
  10497. '("[ ]" "[X]"))))
  10498. nval)
  10499. (unless allowed
  10500. (error "Allowed values for this property have not been defined"))
  10501. (if previous (setq allowed (reverse allowed)))
  10502. (if (member value allowed)
  10503. (setq nval (car (cdr (member value allowed)))))
  10504. (setq nval (or nval (car allowed)))
  10505. (if (equal nval value)
  10506. (error "Only one allowed value for this property"))
  10507. (org-at-property-p)
  10508. (replace-match (concat " :" key ": " nval) t t)
  10509. (org-indent-line-function)
  10510. (beginning-of-line 1)
  10511. (skip-chars-forward " \t")))
  10512. (defun org-find-entry-with-id (ident)
  10513. "Locate the entry that contains the ID property with exact value IDENT.
  10514. IDENT can be a string, a symbol or a number, this function will search for
  10515. the string representation of it.
  10516. Return the position where this entry starts, or nil if there is no such entry."
  10517. (interactive "sID: ")
  10518. (let ((id (cond
  10519. ((stringp ident) ident)
  10520. ((symbol-name ident) (symbol-name ident))
  10521. ((numberp ident) (number-to-string ident))
  10522. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10523. (case-fold-search nil))
  10524. (save-excursion
  10525. (save-restriction
  10526. (widen)
  10527. (goto-char (point-min))
  10528. (when (re-search-forward
  10529. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10530. nil t)
  10531. (org-back-to-heading)
  10532. (point))))))
  10533. ;;;; Timestamps
  10534. (defvar org-last-changed-timestamp nil)
  10535. (defvar org-last-inserted-timestamp nil
  10536. "The last time stamp inserted with `org-insert-time-stamp'.")
  10537. (defvar org-time-was-given) ; dynamically scoped parameter
  10538. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10539. (defvar org-ts-what) ; dynamically scoped parameter
  10540. (defun org-time-stamp (arg &optional inactive)
  10541. "Prompt for a date/time and insert a time stamp.
  10542. If the user specifies a time like HH:MM, or if this command is called
  10543. with a prefix argument, the time stamp will contain date and time.
  10544. Otherwise, only the date will be included. All parts of a date not
  10545. specified by the user will be filled in from the current date/time.
  10546. So if you press just return without typing anything, the time stamp
  10547. will represent the current date/time. If there is already a timestamp
  10548. at the cursor, it will be modified."
  10549. (interactive "P")
  10550. (let* ((ts nil)
  10551. (default-time
  10552. ;; Default time is either today, or, when entering a range,
  10553. ;; the range start.
  10554. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10555. (save-excursion
  10556. (re-search-backward
  10557. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10558. (- (point) 20) t)))
  10559. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10560. (current-time)))
  10561. (default-input (and ts (org-get-compact-tod ts)))
  10562. org-time-was-given org-end-time-was-given time)
  10563. (cond
  10564. ((and (org-at-timestamp-p t)
  10565. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10566. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10567. (insert "--")
  10568. (setq time (let ((this-command this-command))
  10569. (org-read-date arg 'totime nil nil
  10570. default-time default-input)))
  10571. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10572. ((org-at-timestamp-p t)
  10573. (setq time (let ((this-command this-command))
  10574. (org-read-date arg 'totime nil nil default-time default-input)))
  10575. (when (org-at-timestamp-p t) ; just to get the match data
  10576. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10577. (replace-match "")
  10578. (setq org-last-changed-timestamp
  10579. (org-insert-time-stamp
  10580. time (or org-time-was-given arg)
  10581. inactive nil nil (list org-end-time-was-given))))
  10582. (message "Timestamp updated"))
  10583. (t
  10584. (setq time (let ((this-command this-command))
  10585. (org-read-date arg 'totime nil nil default-time default-input)))
  10586. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10587. nil nil (list org-end-time-was-given))))))
  10588. ;; FIXME: can we use this for something else, like computing time differences?
  10589. (defun org-get-compact-tod (s)
  10590. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10591. (let* ((t1 (match-string 1 s))
  10592. (h1 (string-to-number (match-string 2 s)))
  10593. (m1 (string-to-number (match-string 3 s)))
  10594. (t2 (and (match-end 4) (match-string 5 s)))
  10595. (h2 (and t2 (string-to-number (match-string 6 s))))
  10596. (m2 (and t2 (string-to-number (match-string 7 s))))
  10597. dh dm)
  10598. (if (not t2)
  10599. t1
  10600. (setq dh (- h2 h1) dm (- m2 m1))
  10601. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10602. (concat t1 "+" (number-to-string dh)
  10603. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10604. (defun org-time-stamp-inactive (&optional arg)
  10605. "Insert an inactive time stamp.
  10606. An inactive time stamp is enclosed in square brackets instead of angle
  10607. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10608. does not link to the calendar and cannot be changed with the S-cursor keys.
  10609. So these are more for recording a certain time/date."
  10610. (interactive "P")
  10611. (org-time-stamp arg 'inactive))
  10612. (defvar org-date-ovl (org-make-overlay 1 1))
  10613. (org-overlay-put org-date-ovl 'face 'org-warning)
  10614. (org-detach-overlay org-date-ovl)
  10615. (defvar org-ans1) ; dynamically scoped parameter
  10616. (defvar org-ans2) ; dynamically scoped parameter
  10617. (defvar org-plain-time-of-day-regexp) ; defined below
  10618. (defvar org-overriding-default-time nil) ; dynamically scoped
  10619. (defvar org-read-date-overlay nil)
  10620. (defvar org-dcst nil) ; dynamically scoped
  10621. (defvar org-read-date-history nil)
  10622. (defun org-read-date (&optional with-time to-time from-string prompt
  10623. default-time default-input)
  10624. "Read a date, possibly a time, and make things smooth for the user.
  10625. The prompt will suggest to enter an ISO date, but you can also enter anything
  10626. which will at least partially be understood by `parse-time-string'.
  10627. Unrecognized parts of the date will default to the current day, month, year,
  10628. hour and minute. If this command is called to replace a timestamp at point,
  10629. of to enter the second timestamp of a range, the default time is taken from the
  10630. existing stamp. For example,
  10631. 3-2-5 --> 2003-02-05
  10632. feb 15 --> currentyear-02-15
  10633. sep 12 9 --> 2009-09-12
  10634. 12:45 --> today 12:45
  10635. 22 sept 0:34 --> currentyear-09-22 0:34
  10636. 12 --> currentyear-currentmonth-12
  10637. Fri --> nearest Friday (today or later)
  10638. etc.
  10639. Furthermore you can specify a relative date by giving, as the *first* thing
  10640. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10641. change in days weeks, months, years.
  10642. With a single plus or minus, the date is relative to today. With a double
  10643. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10644. +4d --> four days from today
  10645. +4 --> same as above
  10646. +2w --> two weeks from today
  10647. ++5 --> five days from default date
  10648. The function understands only English month and weekday abbreviations,
  10649. but this can be configured with the variables `parse-time-months' and
  10650. `parse-time-weekdays'.
  10651. While prompting, a calendar is popped up - you can also select the
  10652. date with the mouse (button 1). The calendar shows a period of three
  10653. months. To scroll it to other months, use the keys `>' and `<'.
  10654. If you don't like the calendar, turn it off with
  10655. \(setq org-read-date-popup-calendar nil)
  10656. With optional argument TO-TIME, the date will immediately be converted
  10657. to an internal time.
  10658. With an optional argument WITH-TIME, the prompt will suggest to also
  10659. insert a time. Note that when WITH-TIME is not set, you can still
  10660. enter a time, and this function will inform the calling routine about
  10661. this change. The calling routine may then choose to change the format
  10662. used to insert the time stamp into the buffer to include the time.
  10663. With optional argument FROM-STRING, read from this string instead from
  10664. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10665. the time/date that is used for everything that is not specified by the
  10666. user."
  10667. (require 'parse-time)
  10668. (let* ((org-time-stamp-rounding-minutes
  10669. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10670. (org-dcst org-display-custom-times)
  10671. (ct (org-current-time))
  10672. (def (or org-overriding-default-time default-time ct))
  10673. (defdecode (decode-time def))
  10674. (dummy (progn
  10675. (when (< (nth 2 defdecode) org-extend-today-until)
  10676. (setcar (nthcdr 2 defdecode) -1)
  10677. (setcar (nthcdr 1 defdecode) 59)
  10678. (setq def (apply 'encode-time defdecode)
  10679. defdecode (decode-time def)))))
  10680. (calendar-move-hook nil)
  10681. (calendar-view-diary-initially-flag nil)
  10682. (view-diary-entries-initially nil)
  10683. (calendar-view-holidays-initially-flag nil)
  10684. (view-calendar-holidays-initially nil)
  10685. (timestr (format-time-string
  10686. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10687. (prompt (concat (if prompt (concat prompt " ") "")
  10688. (format "Date+time [%s]: " timestr)))
  10689. ans (org-ans0 "") org-ans1 org-ans2 final)
  10690. (cond
  10691. (from-string (setq ans from-string))
  10692. (org-read-date-popup-calendar
  10693. (save-excursion
  10694. (save-window-excursion
  10695. (calendar)
  10696. (calendar-forward-day (- (time-to-days def)
  10697. (calendar-absolute-from-gregorian
  10698. (calendar-current-date))))
  10699. (org-eval-in-calendar nil t)
  10700. (let* ((old-map (current-local-map))
  10701. (map (copy-keymap calendar-mode-map))
  10702. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10703. (org-defkey map (kbd "RET") 'org-calendar-select)
  10704. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10705. 'org-calendar-select-mouse)
  10706. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10707. 'org-calendar-select-mouse)
  10708. (org-defkey minibuffer-local-map [(meta shift left)]
  10709. (lambda () (interactive)
  10710. (org-eval-in-calendar '(calendar-backward-month 1))))
  10711. (org-defkey minibuffer-local-map [(meta shift right)]
  10712. (lambda () (interactive)
  10713. (org-eval-in-calendar '(calendar-forward-month 1))))
  10714. (org-defkey minibuffer-local-map [(meta shift up)]
  10715. (lambda () (interactive)
  10716. (org-eval-in-calendar '(calendar-backward-year 1))))
  10717. (org-defkey minibuffer-local-map [(meta shift down)]
  10718. (lambda () (interactive)
  10719. (org-eval-in-calendar '(calendar-forward-year 1))))
  10720. (org-defkey minibuffer-local-map [(shift up)]
  10721. (lambda () (interactive)
  10722. (org-eval-in-calendar '(calendar-backward-week 1))))
  10723. (org-defkey minibuffer-local-map [(shift down)]
  10724. (lambda () (interactive)
  10725. (org-eval-in-calendar '(calendar-forward-week 1))))
  10726. (org-defkey minibuffer-local-map [(shift left)]
  10727. (lambda () (interactive)
  10728. (org-eval-in-calendar '(calendar-backward-day 1))))
  10729. (org-defkey minibuffer-local-map [(shift right)]
  10730. (lambda () (interactive)
  10731. (org-eval-in-calendar '(calendar-forward-day 1))))
  10732. (org-defkey minibuffer-local-map ">"
  10733. (lambda () (interactive)
  10734. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10735. (org-defkey minibuffer-local-map "<"
  10736. (lambda () (interactive)
  10737. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10738. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10739. (unwind-protect
  10740. (progn
  10741. (use-local-map map)
  10742. (add-hook 'post-command-hook 'org-read-date-display)
  10743. (setq org-ans0 (read-string prompt default-input
  10744. 'org-read-date-history nil))
  10745. ;; org-ans0: from prompt
  10746. ;; org-ans1: from mouse click
  10747. ;; org-ans2: from calendar motion
  10748. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10749. (remove-hook 'post-command-hook 'org-read-date-display)
  10750. (use-local-map old-map)
  10751. (when org-read-date-overlay
  10752. (org-delete-overlay org-read-date-overlay)
  10753. (setq org-read-date-overlay nil)))))))
  10754. (t ; Naked prompt only
  10755. (unwind-protect
  10756. (setq ans (read-string prompt default-input
  10757. 'org-read-date-history timestr))
  10758. (when org-read-date-overlay
  10759. (org-delete-overlay org-read-date-overlay)
  10760. (setq org-read-date-overlay nil)))))
  10761. (setq final (org-read-date-analyze ans def defdecode))
  10762. (if to-time
  10763. (apply 'encode-time final)
  10764. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10765. (format "%04d-%02d-%02d %02d:%02d"
  10766. (nth 5 final) (nth 4 final) (nth 3 final)
  10767. (nth 2 final) (nth 1 final))
  10768. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10769. (defvar def)
  10770. (defvar defdecode)
  10771. (defvar with-time)
  10772. (defun org-read-date-display ()
  10773. "Display the current date prompt interpretation in the minibuffer."
  10774. (when org-read-date-display-live
  10775. (when org-read-date-overlay
  10776. (org-delete-overlay org-read-date-overlay))
  10777. (let ((p (point)))
  10778. (end-of-line 1)
  10779. (while (not (equal (buffer-substring
  10780. (max (point-min) (- (point) 4)) (point))
  10781. " "))
  10782. (insert " "))
  10783. (goto-char p))
  10784. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10785. " " (or org-ans1 org-ans2)))
  10786. (org-end-time-was-given nil)
  10787. (f (org-read-date-analyze ans def defdecode))
  10788. (fmts (if org-dcst
  10789. org-time-stamp-custom-formats
  10790. org-time-stamp-formats))
  10791. (fmt (if (or with-time
  10792. (and (boundp 'org-time-was-given) org-time-was-given))
  10793. (cdr fmts)
  10794. (car fmts)))
  10795. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10796. (when (and org-end-time-was-given
  10797. (string-match org-plain-time-of-day-regexp txt))
  10798. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10799. org-end-time-was-given
  10800. (substring txt (match-end 0)))))
  10801. (setq org-read-date-overlay
  10802. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10803. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10804. (defun org-read-date-analyze (ans def defdecode)
  10805. "Analyse the combined answer of the date prompt."
  10806. ;; FIXME: cleanup and comment
  10807. (let (delta deltan deltaw deltadef year month day
  10808. hour minute second wday pm h2 m2 tl wday1
  10809. iso-year iso-weekday iso-week iso-year iso-date)
  10810. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10811. (setq ans "+0"))
  10812. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10813. (setq ans (replace-match "" t t ans)
  10814. deltan (car delta)
  10815. deltaw (nth 1 delta)
  10816. deltadef (nth 2 delta)))
  10817. ;; Check if there is an iso week date in there
  10818. ;; If yes, sore the info and postpone interpreting it until the rest
  10819. ;; of the parsing is done
  10820. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10821. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10822. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10823. iso-week (string-to-number (match-string 2 ans)))
  10824. (setq ans (replace-match "" t t ans)))
  10825. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10826. (when (string-match
  10827. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10828. (setq year (if (match-end 2)
  10829. (string-to-number (match-string 2 ans))
  10830. (string-to-number (format-time-string "%Y")))
  10831. month (string-to-number (match-string 3 ans))
  10832. day (string-to-number (match-string 4 ans)))
  10833. (if (< year 100) (setq year (+ 2000 year)))
  10834. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10835. t nil ans)))
  10836. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10837. ;; If there is a time with am/pm, and *no* time without it, we convert
  10838. ;; so that matching will be successful.
  10839. (loop for i from 1 to 2 do ; twice, for end time as well
  10840. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10841. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10842. (setq hour (string-to-number (match-string 1 ans))
  10843. minute (if (match-end 3)
  10844. (string-to-number (match-string 3 ans))
  10845. 0)
  10846. pm (equal ?p
  10847. (string-to-char (downcase (match-string 4 ans)))))
  10848. (if (and (= hour 12) (not pm))
  10849. (setq hour 0)
  10850. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10851. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10852. t t ans))))
  10853. ;; Check if a time range is given as a duration
  10854. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10855. (setq hour (string-to-number (match-string 1 ans))
  10856. h2 (+ hour (string-to-number (match-string 3 ans)))
  10857. minute (string-to-number (match-string 2 ans))
  10858. m2 (+ minute (if (match-end 5) (string-to-number
  10859. (match-string 5 ans))0)))
  10860. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10861. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10862. t t ans)))
  10863. ;; Check if there is a time range
  10864. (when (boundp 'org-end-time-was-given)
  10865. (setq org-time-was-given nil)
  10866. (when (and (string-match org-plain-time-of-day-regexp ans)
  10867. (match-end 8))
  10868. (setq org-end-time-was-given (match-string 8 ans))
  10869. (setq ans (concat (substring ans 0 (match-beginning 7))
  10870. (substring ans (match-end 7))))))
  10871. (setq tl (parse-time-string ans)
  10872. day (or (nth 3 tl) (nth 3 defdecode))
  10873. month (or (nth 4 tl)
  10874. (if (and org-read-date-prefer-future
  10875. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10876. (1+ (nth 4 defdecode))
  10877. (nth 4 defdecode)))
  10878. year (or (nth 5 tl)
  10879. (if (and org-read-date-prefer-future
  10880. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10881. (1+ (nth 5 defdecode))
  10882. (nth 5 defdecode)))
  10883. hour (or (nth 2 tl) (nth 2 defdecode))
  10884. minute (or (nth 1 tl) (nth 1 defdecode))
  10885. second (or (nth 0 tl) 0)
  10886. wday (nth 6 tl))
  10887. ;; Special date definitions below
  10888. (cond
  10889. (iso-week
  10890. ;; There was an iso week
  10891. (setq year (or iso-year year)
  10892. day (or iso-weekday wday 1)
  10893. wday nil ; to make sure that the trigger below does not match
  10894. iso-date (calendar-gregorian-from-absolute
  10895. (calendar-absolute-from-iso
  10896. (list iso-week day year))))
  10897. ; FIXME: Should we also push ISO weeks into the future?
  10898. ; (when (and org-read-date-prefer-future
  10899. ; (not iso-year)
  10900. ; (< (calendar-absolute-from-gregorian iso-date)
  10901. ; (time-to-days (current-time))))
  10902. ; (setq year (1+ year)
  10903. ; iso-date (calendar-gregorian-from-absolute
  10904. ; (calendar-absolute-from-iso
  10905. ; (list iso-week day year)))))
  10906. (setq month (car iso-date)
  10907. year (nth 2 iso-date)
  10908. day (nth 1 iso-date)))
  10909. (deltan
  10910. (unless deltadef
  10911. (let ((now (decode-time (current-time))))
  10912. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10913. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10914. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10915. ((equal deltaw "m") (setq month (+ month deltan)))
  10916. ((equal deltaw "y") (setq year (+ year deltan)))))
  10917. ((and wday (not (nth 3 tl)))
  10918. ;; Weekday was given, but no day, so pick that day in the week
  10919. ;; on or after the derived date.
  10920. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10921. (unless (equal wday wday1)
  10922. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10923. (if (and (boundp 'org-time-was-given)
  10924. (nth 2 tl))
  10925. (setq org-time-was-given t))
  10926. (if (< year 100) (setq year (+ 2000 year)))
  10927. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10928. (list second minute hour day month year)))
  10929. (defvar parse-time-weekdays)
  10930. (defun org-read-date-get-relative (s today default)
  10931. "Check string S for special relative date string.
  10932. TODAY and DEFAULT are internal times, for today and for a default.
  10933. Return shift list (N what def-flag)
  10934. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10935. N is the number of WHATs to shift.
  10936. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10937. the DEFAULT date rather than TODAY."
  10938. (when (and
  10939. (string-match
  10940. (concat
  10941. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10942. "\\([0-9]+\\)?"
  10943. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10944. "\\([ \t]\\|$\\)") s)
  10945. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10946. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10947. (string-to-char (substring (match-string 1 s) -1))
  10948. ?+))
  10949. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10950. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10951. (what (if (match-end 3) (match-string 3 s) "d"))
  10952. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10953. (date (if rel default today))
  10954. (wday (nth 6 (decode-time date)))
  10955. delta)
  10956. (if wday1
  10957. (progn
  10958. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10959. (if (= dir ?-) (setq delta (- delta 7)))
  10960. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10961. (list delta "d" rel))
  10962. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10963. (defun org-eval-in-calendar (form &optional keepdate)
  10964. "Eval FORM in the calendar window and return to current window.
  10965. Also, store the cursor date in variable org-ans2."
  10966. (let ((sw (selected-window)))
  10967. (select-window (get-buffer-window "*Calendar*"))
  10968. (eval form)
  10969. (when (and (not keepdate) (calendar-cursor-to-date))
  10970. (let* ((date (calendar-cursor-to-date))
  10971. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10972. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10973. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10974. (select-window sw)))
  10975. (defun org-calendar-select ()
  10976. "Return to `org-read-date' with the date currently selected.
  10977. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10978. (interactive)
  10979. (when (calendar-cursor-to-date)
  10980. (let* ((date (calendar-cursor-to-date))
  10981. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10982. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10983. (if (active-minibuffer-window) (exit-minibuffer))))
  10984. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10985. "Insert a date stamp for the date given by the internal TIME.
  10986. WITH-HM means, use the stamp format that includes the time of the day.
  10987. INACTIVE means use square brackets instead of angular ones, so that the
  10988. stamp will not contribute to the agenda.
  10989. PRE and POST are optional strings to be inserted before and after the
  10990. stamp.
  10991. The command returns the inserted time stamp."
  10992. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10993. stamp)
  10994. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10995. (insert-before-markers (or pre ""))
  10996. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10997. (when (listp extra)
  10998. (setq extra (car extra))
  10999. (if (and (stringp extra)
  11000. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11001. (setq extra (format "-%02d:%02d"
  11002. (string-to-number (match-string 1 extra))
  11003. (string-to-number (match-string 2 extra))))
  11004. (setq extra nil)))
  11005. (when extra
  11006. (backward-char 1)
  11007. (insert-before-markers extra)
  11008. (forward-char 1))
  11009. (insert-before-markers (or post ""))
  11010. (setq org-last-inserted-timestamp stamp)))
  11011. (defun org-toggle-time-stamp-overlays ()
  11012. "Toggle the use of custom time stamp formats."
  11013. (interactive)
  11014. (setq org-display-custom-times (not org-display-custom-times))
  11015. (unless org-display-custom-times
  11016. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11017. (while (setq p (next-single-property-change p 'display))
  11018. (if (and (get-text-property p 'display)
  11019. (eq (get-text-property p 'face) 'org-date))
  11020. (remove-text-properties
  11021. p (setq p (next-single-property-change p 'display))
  11022. '(display t))))
  11023. (set-buffer-modified-p bmp)))
  11024. (if (featurep 'xemacs)
  11025. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11026. (org-restart-font-lock)
  11027. (setq org-table-may-need-update t)
  11028. (if org-display-custom-times
  11029. (message "Time stamps are overlayed with custom format")
  11030. (message "Time stamp overlays removed")))
  11031. (defun org-display-custom-time (beg end)
  11032. "Overlay modified time stamp format over timestamp between BEG and END."
  11033. (let* ((ts (buffer-substring beg end))
  11034. t1 w1 with-hm tf time str w2 (off 0))
  11035. (save-match-data
  11036. (setq t1 (org-parse-time-string ts t))
  11037. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11038. (setq off (- (match-end 0) (match-beginning 0)))))
  11039. (setq end (- end off))
  11040. (setq w1 (- end beg)
  11041. with-hm (and (nth 1 t1) (nth 2 t1))
  11042. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11043. time (org-fix-decoded-time t1)
  11044. str (org-add-props
  11045. (format-time-string
  11046. (substring tf 1 -1) (apply 'encode-time time))
  11047. nil 'mouse-face 'highlight)
  11048. w2 (length str))
  11049. (if (not (= w2 w1))
  11050. (add-text-properties (1+ beg) (+ 2 beg)
  11051. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11052. (if (featurep 'xemacs)
  11053. (progn
  11054. (put-text-property beg end 'invisible t)
  11055. (put-text-property beg end 'end-glyph (make-glyph str)))
  11056. (put-text-property beg end 'display str))))
  11057. (defun org-translate-time (string)
  11058. "Translate all timestamps in STRING to custom format.
  11059. But do this only if the variable `org-display-custom-times' is set."
  11060. (when org-display-custom-times
  11061. (save-match-data
  11062. (let* ((start 0)
  11063. (re org-ts-regexp-both)
  11064. t1 with-hm inactive tf time str beg end)
  11065. (while (setq start (string-match re string start))
  11066. (setq beg (match-beginning 0)
  11067. end (match-end 0)
  11068. t1 (save-match-data
  11069. (org-parse-time-string (substring string beg end) t))
  11070. with-hm (and (nth 1 t1) (nth 2 t1))
  11071. inactive (equal (substring string beg (1+ beg)) "[")
  11072. tf (funcall (if with-hm 'cdr 'car)
  11073. org-time-stamp-custom-formats)
  11074. time (org-fix-decoded-time t1)
  11075. str (format-time-string
  11076. (concat
  11077. (if inactive "[" "<") (substring tf 1 -1)
  11078. (if inactive "]" ">"))
  11079. (apply 'encode-time time))
  11080. string (replace-match str t t string)
  11081. start (+ start (length str)))))))
  11082. string)
  11083. (defun org-fix-decoded-time (time)
  11084. "Set 0 instead of nil for the first 6 elements of time.
  11085. Don't touch the rest."
  11086. (let ((n 0))
  11087. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11088. (defun org-days-to-time (timestamp-string)
  11089. "Difference between TIMESTAMP-STRING and now in days."
  11090. (- (time-to-days (org-time-string-to-time timestamp-string))
  11091. (time-to-days (current-time))))
  11092. (defun org-deadline-close (timestamp-string &optional ndays)
  11093. "Is the time in TIMESTAMP-STRING close to the current date?"
  11094. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11095. (and (< (org-days-to-time timestamp-string) ndays)
  11096. (not (org-entry-is-done-p))))
  11097. (defun org-get-wdays (ts)
  11098. "Get the deadline lead time appropriate for timestring TS."
  11099. (cond
  11100. ((<= org-deadline-warning-days 0)
  11101. ;; 0 or negative, enforce this value no matter what
  11102. (- org-deadline-warning-days))
  11103. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11104. ;; lead time is specified.
  11105. (floor (* (string-to-number (match-string 1 ts))
  11106. (cdr (assoc (match-string 2 ts)
  11107. '(("d" . 1) ("w" . 7)
  11108. ("m" . 30.4) ("y" . 365.25)))))))
  11109. ;; go for the default.
  11110. (t org-deadline-warning-days)))
  11111. (defun org-calendar-select-mouse (ev)
  11112. "Return to `org-read-date' with the date currently selected.
  11113. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11114. (interactive "e")
  11115. (mouse-set-point ev)
  11116. (when (calendar-cursor-to-date)
  11117. (let* ((date (calendar-cursor-to-date))
  11118. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11119. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11120. (if (active-minibuffer-window) (exit-minibuffer))))
  11121. (defun org-check-deadlines (ndays)
  11122. "Check if there are any deadlines due or past due.
  11123. A deadline is considered due if it happens within `org-deadline-warning-days'
  11124. days from today's date. If the deadline appears in an entry marked DONE,
  11125. it is not shown. The prefix arg NDAYS can be used to test that many
  11126. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11127. (interactive "P")
  11128. (let* ((org-warn-days
  11129. (cond
  11130. ((equal ndays '(4)) 100000)
  11131. (ndays (prefix-numeric-value ndays))
  11132. (t (abs org-deadline-warning-days))))
  11133. (case-fold-search nil)
  11134. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11135. (callback
  11136. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11137. (message "%d deadlines past-due or due within %d days"
  11138. (org-occur regexp nil callback)
  11139. org-warn-days)))
  11140. (defun org-check-before-date (date)
  11141. "Check if there are deadlines or scheduled entries before DATE."
  11142. (interactive (list (org-read-date)))
  11143. (let ((case-fold-search nil)
  11144. (regexp (concat "\\<\\(" org-deadline-string
  11145. "\\|" org-scheduled-string
  11146. "\\) *<\\([^>]+\\)>"))
  11147. (callback
  11148. (lambda () (time-less-p
  11149. (org-time-string-to-time (match-string 2))
  11150. (org-time-string-to-time date)))))
  11151. (message "%d entries before %s"
  11152. (org-occur regexp nil callback) date)))
  11153. (defun org-check-after-date (date)
  11154. "Check if there are deadlines or scheduled entries after DATE."
  11155. (interactive (list (org-read-date)))
  11156. (let ((case-fold-search nil)
  11157. (regexp (concat "\\<\\(" org-deadline-string
  11158. "\\|" org-scheduled-string
  11159. "\\) *<\\([^>]+\\)>"))
  11160. (callback
  11161. (lambda () (not
  11162. (time-less-p
  11163. (org-time-string-to-time (match-string 2))
  11164. (org-time-string-to-time date))))))
  11165. (message "%d entries after %s"
  11166. (org-occur regexp nil callback) date)))
  11167. (defun org-evaluate-time-range (&optional to-buffer)
  11168. "Evaluate a time range by computing the difference between start and end.
  11169. Normally the result is just printed in the echo area, but with prefix arg
  11170. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11171. If the time range is actually in a table, the result is inserted into the
  11172. next column.
  11173. For time difference computation, a year is assumed to be exactly 365
  11174. days in order to avoid rounding problems."
  11175. (interactive "P")
  11176. (or
  11177. (org-clock-update-time-maybe)
  11178. (save-excursion
  11179. (unless (org-at-date-range-p t)
  11180. (goto-char (point-at-bol))
  11181. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11182. (if (not (org-at-date-range-p t))
  11183. (error "Not at a time-stamp range, and none found in current line")))
  11184. (let* ((ts1 (match-string 1))
  11185. (ts2 (match-string 2))
  11186. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11187. (match-end (match-end 0))
  11188. (time1 (org-time-string-to-time ts1))
  11189. (time2 (org-time-string-to-time ts2))
  11190. (t1 (time-to-seconds time1))
  11191. (t2 (time-to-seconds time2))
  11192. (diff (abs (- t2 t1)))
  11193. (negative (< (- t2 t1) 0))
  11194. ;; (ys (floor (* 365 24 60 60)))
  11195. (ds (* 24 60 60))
  11196. (hs (* 60 60))
  11197. (fy "%dy %dd %02d:%02d")
  11198. (fy1 "%dy %dd")
  11199. (fd "%dd %02d:%02d")
  11200. (fd1 "%dd")
  11201. (fh "%02d:%02d")
  11202. y d h m align)
  11203. (if havetime
  11204. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11205. y 0
  11206. d (floor (/ diff ds)) diff (mod diff ds)
  11207. h (floor (/ diff hs)) diff (mod diff hs)
  11208. m (floor (/ diff 60)))
  11209. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11210. y 0
  11211. d (floor (+ (/ diff ds) 0.5))
  11212. h 0 m 0))
  11213. (if (not to-buffer)
  11214. (message "%s" (org-make-tdiff-string y d h m))
  11215. (if (org-at-table-p)
  11216. (progn
  11217. (goto-char match-end)
  11218. (setq align t)
  11219. (and (looking-at " *|") (goto-char (match-end 0))))
  11220. (goto-char match-end))
  11221. (if (looking-at
  11222. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11223. (replace-match ""))
  11224. (if negative (insert " -"))
  11225. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11226. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11227. (insert " " (format fh h m))))
  11228. (if align (org-table-align))
  11229. (message "Time difference inserted")))))
  11230. (defun org-make-tdiff-string (y d h m)
  11231. (let ((fmt "")
  11232. (l nil))
  11233. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11234. l (push y l)))
  11235. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11236. l (push d l)))
  11237. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11238. l (push h l)))
  11239. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11240. l (push m l)))
  11241. (apply 'format fmt (nreverse l))))
  11242. (defun org-time-string-to-time (s)
  11243. (apply 'encode-time (org-parse-time-string s)))
  11244. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11245. "Convert a time stamp to an absolute day number.
  11246. If there is a specifyer for a cyclic time stamp, get the closest date to
  11247. DAYNR.
  11248. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11249. (cond
  11250. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11251. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11252. daynr
  11253. (+ daynr 1000)))
  11254. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11255. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11256. (time-to-days (current-time))) (match-string 0 s)
  11257. prefer show-all))
  11258. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11259. (defun org-days-to-iso-week (days)
  11260. "Return the iso week number."
  11261. (require 'cal-iso)
  11262. (car (calendar-iso-from-absolute days)))
  11263. (defun org-small-year-to-year (year)
  11264. "Convert 2-digit years into 4-digit years.
  11265. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11266. The year 2000 cannot be abbreviated. Any year larger than 99
  11267. is returned unchanged."
  11268. (if (< year 38)
  11269. (setq year (+ 2000 year))
  11270. (if (< year 100)
  11271. (setq year (+ 1900 year))))
  11272. year)
  11273. (defun org-time-from-absolute (d)
  11274. "Return the time corresponding to date D.
  11275. D may be an absolute day number, or a calendar-type list (month day year)."
  11276. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11277. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11278. (defun org-calendar-holiday ()
  11279. "List of holidays, for Diary display in Org-mode."
  11280. (require 'holidays)
  11281. (let ((hl (funcall
  11282. (if (fboundp 'calendar-check-holidays)
  11283. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11284. (if hl (mapconcat 'identity hl "; "))))
  11285. (defun org-diary-sexp-entry (sexp entry date)
  11286. "Process a SEXP diary ENTRY for DATE."
  11287. (require 'diary-lib)
  11288. (let ((result (if calendar-debug-sexp
  11289. (let ((stack-trace-on-error t))
  11290. (eval (car (read-from-string sexp))))
  11291. (condition-case nil
  11292. (eval (car (read-from-string sexp)))
  11293. (error
  11294. (beep)
  11295. (message "Bad sexp at line %d in %s: %s"
  11296. (org-current-line)
  11297. (buffer-file-name) sexp)
  11298. (sleep-for 2))))))
  11299. (cond ((stringp result) result)
  11300. ((and (consp result)
  11301. (stringp (cdr result))) (cdr result))
  11302. (result entry)
  11303. (t nil))))
  11304. (defun org-diary-to-ical-string (frombuf)
  11305. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11306. This uses the icalendar.el library."
  11307. (let* ((tmpdir (if (featurep 'xemacs)
  11308. (temp-directory)
  11309. temporary-file-directory))
  11310. (tmpfile (make-temp-name
  11311. (expand-file-name "orgics" tmpdir)))
  11312. buf rtn b e)
  11313. (save-excursion
  11314. (set-buffer frombuf)
  11315. (icalendar-export-region (point-min) (point-max) tmpfile)
  11316. (setq buf (find-buffer-visiting tmpfile))
  11317. (set-buffer buf)
  11318. (goto-char (point-min))
  11319. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11320. (setq b (match-beginning 0)))
  11321. (goto-char (point-max))
  11322. (if (re-search-backward "^END:VEVENT" nil t)
  11323. (setq e (match-end 0)))
  11324. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11325. (kill-buffer buf)
  11326. (delete-file tmpfile)
  11327. rtn))
  11328. (defun org-closest-date (start current change prefer show-all)
  11329. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11330. When PREFER is `past' return a date that is either CURRENT or past.
  11331. When PREFER is `future', return a date that is either CURRENT or future.
  11332. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11333. ;; Make the proper lists from the dates
  11334. (catch 'exit
  11335. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11336. dn dw sday cday n1 n2 n0
  11337. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11338. (setq start (org-date-to-gregorian start)
  11339. current (org-date-to-gregorian
  11340. (if show-all
  11341. current
  11342. (time-to-days (current-time))))
  11343. sday (calendar-absolute-from-gregorian start)
  11344. cday (calendar-absolute-from-gregorian current))
  11345. (if (<= cday sday) (throw 'exit sday))
  11346. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11347. (setq dn (string-to-number (match-string 1 change))
  11348. dw (cdr (assoc (match-string 2 change) a1)))
  11349. (error "Invalid change specifyer: %s" change))
  11350. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11351. (cond
  11352. ((eq dw 'day)
  11353. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11354. n2 (+ n1 dn)))
  11355. ((eq dw 'year)
  11356. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11357. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11358. (setq date1 (list m d y1)
  11359. n1 (calendar-absolute-from-gregorian date1)
  11360. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11361. n2 (calendar-absolute-from-gregorian date2)))
  11362. ((eq dw 'month)
  11363. ;; approx number of month between the two dates
  11364. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11365. ;; How often does dn fit in there?
  11366. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11367. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11368. m (+ m nm)
  11369. ny (floor (/ m 12))
  11370. y (+ y ny)
  11371. m (- m (* ny 12)))
  11372. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11373. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11374. (setq m2 (+ m dn) y2 y)
  11375. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11376. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11377. (while (<= n2 cday)
  11378. (setq n1 n2 m m2 y y2)
  11379. (setq m2 (+ m dn) y2 y)
  11380. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11381. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11382. ;; Make sure n1 is the earlier date
  11383. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11384. (if show-all
  11385. (cond
  11386. ((eq prefer 'past) n1)
  11387. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11388. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11389. (cond
  11390. ((eq prefer 'past) n1)
  11391. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11392. (t (if (= cday n1) n1 n2)))))))
  11393. (defun org-date-to-gregorian (date)
  11394. "Turn any specification of DATE into a gregorian date for the calendar."
  11395. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11396. ((and (listp date) (= (length date) 3)) date)
  11397. ((stringp date)
  11398. (setq date (org-parse-time-string date))
  11399. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11400. ((listp date)
  11401. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11402. (defun org-parse-time-string (s &optional nodefault)
  11403. "Parse the standard Org-mode time string.
  11404. This should be a lot faster than the normal `parse-time-string'.
  11405. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11406. hour and minute fields will be nil if not given."
  11407. (if (string-match org-ts-regexp0 s)
  11408. (list 0
  11409. (if (or (match-beginning 8) (not nodefault))
  11410. (string-to-number (or (match-string 8 s) "0")))
  11411. (if (or (match-beginning 7) (not nodefault))
  11412. (string-to-number (or (match-string 7 s) "0")))
  11413. (string-to-number (match-string 4 s))
  11414. (string-to-number (match-string 3 s))
  11415. (string-to-number (match-string 2 s))
  11416. nil nil nil)
  11417. (make-list 9 0)))
  11418. (defun org-timestamp-up (&optional arg)
  11419. "Increase the date item at the cursor by one.
  11420. If the cursor is on the year, change the year. If it is on the month or
  11421. the day, change that.
  11422. With prefix ARG, change by that many units."
  11423. (interactive "p")
  11424. (org-timestamp-change (prefix-numeric-value arg)))
  11425. (defun org-timestamp-down (&optional arg)
  11426. "Decrease the date item at the cursor by one.
  11427. If the cursor is on the year, change the year. If it is on the month or
  11428. the day, change that.
  11429. With prefix ARG, change by that many units."
  11430. (interactive "p")
  11431. (org-timestamp-change (- (prefix-numeric-value arg))))
  11432. (defun org-timestamp-up-day (&optional arg)
  11433. "Increase the date in the time stamp by one day.
  11434. With prefix ARG, change that many days."
  11435. (interactive "p")
  11436. (if (and (not (org-at-timestamp-p t))
  11437. (org-on-heading-p))
  11438. (org-todo 'up)
  11439. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11440. (defun org-timestamp-down-day (&optional arg)
  11441. "Decrease the date in the time stamp by one day.
  11442. With prefix ARG, change that many days."
  11443. (interactive "p")
  11444. (if (and (not (org-at-timestamp-p t))
  11445. (org-on-heading-p))
  11446. (org-todo 'down)
  11447. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11448. (defun org-at-timestamp-p (&optional inactive-ok)
  11449. "Determine if the cursor is in or at a timestamp."
  11450. (interactive)
  11451. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11452. (pos (point))
  11453. (ans (or (looking-at tsr)
  11454. (save-excursion
  11455. (skip-chars-backward "^[<\n\r\t")
  11456. (if (> (point) (point-min)) (backward-char 1))
  11457. (and (looking-at tsr)
  11458. (> (- (match-end 0) pos) -1))))))
  11459. (and ans
  11460. (boundp 'org-ts-what)
  11461. (setq org-ts-what
  11462. (cond
  11463. ((= pos (match-beginning 0)) 'bracket)
  11464. ((= pos (1- (match-end 0))) 'bracket)
  11465. ((org-pos-in-match-range pos 2) 'year)
  11466. ((org-pos-in-match-range pos 3) 'month)
  11467. ((org-pos-in-match-range pos 7) 'hour)
  11468. ((org-pos-in-match-range pos 8) 'minute)
  11469. ((or (org-pos-in-match-range pos 4)
  11470. (org-pos-in-match-range pos 5)) 'day)
  11471. ((and (> pos (or (match-end 8) (match-end 5)))
  11472. (< pos (match-end 0)))
  11473. (- pos (or (match-end 8) (match-end 5))))
  11474. (t 'day))))
  11475. ans))
  11476. (defun org-toggle-timestamp-type ()
  11477. "Toggle the type (<active> or [inactive]) of a time stamp."
  11478. (interactive)
  11479. (when (org-at-timestamp-p t)
  11480. (let ((beg (match-beginning 0)) (end (match-end 0))
  11481. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11482. (save-excursion
  11483. (goto-char beg)
  11484. (while (re-search-forward "[][<>]" end t)
  11485. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11486. t t)))
  11487. (message "Timestamp is now %sactive"
  11488. (if (equal (char-after beg) ?<) "" "in")))))
  11489. (defun org-timestamp-change (n &optional what)
  11490. "Change the date in the time stamp at point.
  11491. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11492. `year', `minute', `second'. If WHAT is not given, the cursor position
  11493. in the timestamp determines what will be changed."
  11494. (let ((pos (point))
  11495. with-hm inactive
  11496. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11497. org-ts-what
  11498. extra rem
  11499. ts time time0)
  11500. (if (not (org-at-timestamp-p t))
  11501. (error "Not at a timestamp"))
  11502. (if (and (not what) (eq org-ts-what 'bracket))
  11503. (org-toggle-timestamp-type)
  11504. (if (and (not what) (not (eq org-ts-what 'day))
  11505. org-display-custom-times
  11506. (get-text-property (point) 'display)
  11507. (not (get-text-property (1- (point)) 'display)))
  11508. (setq org-ts-what 'day))
  11509. (setq org-ts-what (or what org-ts-what)
  11510. inactive (= (char-after (match-beginning 0)) ?\[)
  11511. ts (match-string 0))
  11512. (replace-match "")
  11513. (if (string-match
  11514. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11515. ts)
  11516. (setq extra (match-string 1 ts)))
  11517. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11518. (setq with-hm t))
  11519. (setq time0 (org-parse-time-string ts))
  11520. (when (and (eq org-ts-what 'minute)
  11521. (eq current-prefix-arg nil))
  11522. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11523. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11524. (setcar (cdr time0) (+ (nth 1 time0)
  11525. (if (> n 0) (- rem) (- dm rem))))))
  11526. (setq time
  11527. (encode-time (or (car time0) 0)
  11528. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11529. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11530. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11531. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11532. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11533. (nthcdr 6 time0)))
  11534. (when (and (member org-ts-what '(hour minute))
  11535. extra
  11536. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11537. (setq extra (org-modify-ts-extra
  11538. extra
  11539. (if (eq org-ts-what 'hour) 2 5)
  11540. n dm)))
  11541. (when (integerp org-ts-what)
  11542. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11543. (if (eq what 'calendar)
  11544. (let ((cal-date (org-get-date-from-calendar)))
  11545. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11546. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11547. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11548. (setcar time0 (or (car time0) 0))
  11549. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11550. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11551. (setq time (apply 'encode-time time0))))
  11552. (setq org-last-changed-timestamp
  11553. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11554. (org-clock-update-time-maybe)
  11555. (goto-char pos)
  11556. ;; Try to recenter the calendar window, if any
  11557. (if (and org-calendar-follow-timestamp-change
  11558. (get-buffer-window "*Calendar*" t)
  11559. (memq org-ts-what '(day month year)))
  11560. (org-recenter-calendar (time-to-days time))))))
  11561. (defun org-modify-ts-extra (s pos n dm)
  11562. "Change the different parts of the lead-time and repeat fields in timestamp."
  11563. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11564. ng h m new rem)
  11565. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11566. (cond
  11567. ((or (org-pos-in-match-range pos 2)
  11568. (org-pos-in-match-range pos 3))
  11569. (setq m (string-to-number (match-string 3 s))
  11570. h (string-to-number (match-string 2 s)))
  11571. (if (org-pos-in-match-range pos 2)
  11572. (setq h (+ h n))
  11573. (setq n (* dm (org-no-warnings (signum n))))
  11574. (when (not (= 0 (setq rem (% m dm))))
  11575. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11576. (setq m (+ m n)))
  11577. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11578. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11579. (setq h (min 24 (max 0 h)))
  11580. (setq ng 1 new (format "-%02d:%02d" h m)))
  11581. ((org-pos-in-match-range pos 6)
  11582. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11583. ((org-pos-in-match-range pos 5)
  11584. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11585. ((org-pos-in-match-range pos 9)
  11586. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11587. ((org-pos-in-match-range pos 8)
  11588. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11589. (when ng
  11590. (setq s (concat
  11591. (substring s 0 (match-beginning ng))
  11592. new
  11593. (substring s (match-end ng))))))
  11594. s))
  11595. (defun org-recenter-calendar (date)
  11596. "If the calendar is visible, recenter it to DATE."
  11597. (let* ((win (selected-window))
  11598. (cwin (get-buffer-window "*Calendar*" t))
  11599. (calendar-move-hook nil))
  11600. (when cwin
  11601. (select-window cwin)
  11602. (calendar-goto-date (if (listp date) date
  11603. (calendar-gregorian-from-absolute date)))
  11604. (select-window win))))
  11605. (defun org-goto-calendar (&optional arg)
  11606. "Go to the Emacs calendar at the current date.
  11607. If there is a time stamp in the current line, go to that date.
  11608. A prefix ARG can be used to force the current date."
  11609. (interactive "P")
  11610. (let ((tsr org-ts-regexp) diff
  11611. (calendar-move-hook nil)
  11612. (calendar-view-holidays-initially-flag nil)
  11613. (view-calendar-holidays-initially nil)
  11614. (calendar-view-diary-initially-flag nil)
  11615. (view-diary-entries-initially nil))
  11616. (if (or (org-at-timestamp-p)
  11617. (save-excursion
  11618. (beginning-of-line 1)
  11619. (looking-at (concat ".*" tsr))))
  11620. (let ((d1 (time-to-days (current-time)))
  11621. (d2 (time-to-days
  11622. (org-time-string-to-time (match-string 1)))))
  11623. (setq diff (- d2 d1))))
  11624. (calendar)
  11625. (calendar-goto-today)
  11626. (if (and diff (not arg)) (calendar-forward-day diff))))
  11627. (defun org-get-date-from-calendar ()
  11628. "Return a list (month day year) of date at point in calendar."
  11629. (with-current-buffer "*Calendar*"
  11630. (save-match-data
  11631. (calendar-cursor-to-date))))
  11632. (defun org-date-from-calendar ()
  11633. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11634. If there is already a time stamp at the cursor position, update it."
  11635. (interactive)
  11636. (if (org-at-timestamp-p t)
  11637. (org-timestamp-change 0 'calendar)
  11638. (let ((cal-date (org-get-date-from-calendar)))
  11639. (org-insert-time-stamp
  11640. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11641. (defun org-minutes-to-hh:mm-string (m)
  11642. "Compute H:MM from a number of minutes."
  11643. (let ((h (/ m 60)))
  11644. (setq m (- m (* 60 h)))
  11645. (format org-time-clocksum-format h m)))
  11646. (defun org-hh:mm-string-to-minutes (s)
  11647. "Convert a string H:MM to a number of minutes."
  11648. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11649. (+ (* (string-to-number (match-string 1 s)) 60)
  11650. (string-to-number (match-string 2 s)))
  11651. 0))
  11652. ;;;; Files
  11653. (defun org-save-all-org-buffers ()
  11654. "Save all Org-mode buffers without user confirmation."
  11655. (interactive)
  11656. (message "Saving all Org-mode buffers...")
  11657. (save-some-buffers t 'org-mode-p)
  11658. (when (featurep 'org-id) (org-id-locations-save))
  11659. (message "Saving all Org-mode buffers... done"))
  11660. (defun org-revert-all-org-buffers ()
  11661. "Revert all Org-mode buffers.
  11662. Prompt for confirmation when there are unsaved changes.
  11663. Be sure you know what you are doing before letting this function
  11664. overwrite your changes.
  11665. This function is useful in a setup where one tracks org files
  11666. with a version control system, to revert on one machine after pulling
  11667. changes from another. I believe the procedure must be like this:
  11668. 1. M-x org-save-all-org-buffers
  11669. 2. Pull changes from the other machine, resolve conflicts
  11670. 3. M-x org-revert-all-org-buffers"
  11671. (interactive)
  11672. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11673. (error "Abort"))
  11674. (save-excursion
  11675. (save-window-excursion
  11676. (mapc
  11677. (lambda (b)
  11678. (when (and (with-current-buffer b (org-mode-p))
  11679. (with-current-buffer b buffer-file-name))
  11680. (switch-to-buffer b)
  11681. (revert-buffer t 'no-confirm)))
  11682. (buffer-list))
  11683. (when (and (featurep 'org-id) org-id-track-globally)
  11684. (org-id-locations-load)))))
  11685. ;;;; Agenda files
  11686. ;;;###autoload
  11687. (defun org-iswitchb (&optional arg)
  11688. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11689. With a prefix argument, restrict available to files.
  11690. With two prefix arguments, restrict available buffers to agenda files.
  11691. Due to some yet unresolved reason, the global function
  11692. `iswitchb-mode' needs to be active for this function to work."
  11693. (interactive "P")
  11694. (require 'iswitchb)
  11695. (let ((enabled iswitchb-mode) blist)
  11696. (or enabled (iswitchb-mode 1))
  11697. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11698. ((equal arg '(16)) (org-buffer-list 'agenda))
  11699. (t (org-buffer-list))))
  11700. (unwind-protect
  11701. (let ((iswitchb-make-buflist-hook
  11702. (lambda ()
  11703. (setq iswitchb-temp-buflist
  11704. (mapcar 'buffer-name blist)))))
  11705. (switch-to-buffer
  11706. (iswitchb-read-buffer
  11707. "Switch-to: " nil t))
  11708. (or enabled (iswitchb-mode -1))))))
  11709. ;;;###autoload
  11710. (defun org-ido-switchb (&optional arg)
  11711. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11712. With a prefix argument, restrict available to files.
  11713. With two prefix arguments, restrict available buffers to agenda files."
  11714. (interactive "P")
  11715. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11716. ((equal arg '(16)) (org-buffer-list 'agenda))
  11717. (t (org-buffer-list)))))
  11718. (switch-to-buffer
  11719. (org-ido-completing-read "Org buffer: "
  11720. (mapcar 'list (mapcar 'buffer-name blist))
  11721. nil t))))
  11722. (defun org-buffer-list (&optional predicate exclude-tmp)
  11723. "Return a list of Org buffers.
  11724. PREDICATE can be `export', `files' or `agenda'.
  11725. export restrict the list to Export buffers.
  11726. files restrict the list to buffers visiting Org files.
  11727. agenda restrict the list to buffers visiting agenda files.
  11728. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11729. (let* ((bfn nil)
  11730. (agenda-files (and (eq predicate 'agenda)
  11731. (mapcar 'file-truename (org-agenda-files t))))
  11732. (filter
  11733. (cond
  11734. ((eq predicate 'files)
  11735. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11736. ((eq predicate 'export)
  11737. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11738. ((eq predicate 'agenda)
  11739. (lambda (b)
  11740. (with-current-buffer b
  11741. (and (eq major-mode 'org-mode)
  11742. (setq bfn (buffer-file-name b))
  11743. (member (file-truename bfn) agenda-files)))))
  11744. (t (lambda (b) (with-current-buffer b
  11745. (or (eq major-mode 'org-mode)
  11746. (string-match "\*Org .*Export"
  11747. (buffer-name b)))))))))
  11748. (delq nil
  11749. (mapcar
  11750. (lambda(b)
  11751. (if (and (funcall filter b)
  11752. (or (not exclude-tmp)
  11753. (not (string-match "tmp" (buffer-name b)))))
  11754. b
  11755. nil))
  11756. (buffer-list)))))
  11757. (defun org-agenda-files (&optional unrestricted archives)
  11758. "Get the list of agenda files.
  11759. Optional UNRESTRICTED means return the full list even if a restriction
  11760. is currently in place.
  11761. When ARCHIVES is t, include all archive files hat are really being
  11762. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11763. `org-agenda-archives-mode' is t."
  11764. (let ((files
  11765. (cond
  11766. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11767. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11768. ((listp org-agenda-files) org-agenda-files)
  11769. (t (error "Invalid value of `org-agenda-files'")))))
  11770. (setq files (apply 'append
  11771. (mapcar (lambda (f)
  11772. (if (file-directory-p f)
  11773. (directory-files
  11774. f t org-agenda-file-regexp)
  11775. (list f)))
  11776. files)))
  11777. (when org-agenda-skip-unavailable-files
  11778. (setq files (delq nil
  11779. (mapcar (function
  11780. (lambda (file)
  11781. (and (file-readable-p file) file)))
  11782. files))))
  11783. (when (or (eq archives t)
  11784. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11785. (setq files (org-add-archive-files files)))
  11786. files))
  11787. (defun org-edit-agenda-file-list ()
  11788. "Edit the list of agenda files.
  11789. Depending on setup, this either uses customize to edit the variable
  11790. `org-agenda-files', or it visits the file that is holding the list. In the
  11791. latter case, the buffer is set up in a way that saving it automatically kills
  11792. the buffer and restores the previous window configuration."
  11793. (interactive)
  11794. (if (stringp org-agenda-files)
  11795. (let ((cw (current-window-configuration)))
  11796. (find-file org-agenda-files)
  11797. (org-set-local 'org-window-configuration cw)
  11798. (org-add-hook 'after-save-hook
  11799. (lambda ()
  11800. (set-window-configuration
  11801. (prog1 org-window-configuration
  11802. (kill-buffer (current-buffer))))
  11803. (org-install-agenda-files-menu)
  11804. (message "New agenda file list installed"))
  11805. nil 'local)
  11806. (message "%s" (substitute-command-keys
  11807. "Edit list and finish with \\[save-buffer]")))
  11808. (customize-variable 'org-agenda-files)))
  11809. (defun org-store-new-agenda-file-list (list)
  11810. "Set new value for the agenda file list and save it correctly."
  11811. (if (stringp org-agenda-files)
  11812. (let ((f org-agenda-files) b)
  11813. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11814. (with-temp-file f
  11815. (insert (mapconcat 'identity list "\n") "\n")))
  11816. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11817. (setq org-agenda-files list)
  11818. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11819. (defun org-read-agenda-file-list ()
  11820. "Read the list of agenda files from a file."
  11821. (when (file-directory-p org-agenda-files)
  11822. (error "`org-agenda-files' cannot be a single directory"))
  11823. (when (stringp org-agenda-files)
  11824. (with-temp-buffer
  11825. (insert-file-contents org-agenda-files)
  11826. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11827. ;;;###autoload
  11828. (defun org-cycle-agenda-files ()
  11829. "Cycle through the files in `org-agenda-files'.
  11830. If the current buffer visits an agenda file, find the next one in the list.
  11831. If the current buffer does not, find the first agenda file."
  11832. (interactive)
  11833. (let* ((fs (org-agenda-files t))
  11834. (files (append fs (list (car fs))))
  11835. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11836. file)
  11837. (unless files (error "No agenda files"))
  11838. (catch 'exit
  11839. (while (setq file (pop files))
  11840. (if (equal (file-truename file) tcf)
  11841. (when (car files)
  11842. (find-file (car files))
  11843. (throw 'exit t))))
  11844. (find-file (car fs)))
  11845. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11846. (defun org-agenda-file-to-front (&optional to-end)
  11847. "Move/add the current file to the top of the agenda file list.
  11848. If the file is not present in the list, it is added to the front. If it is
  11849. present, it is moved there. With optional argument TO-END, add/move to the
  11850. end of the list."
  11851. (interactive "P")
  11852. (let ((org-agenda-skip-unavailable-files nil)
  11853. (file-alist (mapcar (lambda (x)
  11854. (cons (file-truename x) x))
  11855. (org-agenda-files t)))
  11856. (ctf (file-truename buffer-file-name))
  11857. x had)
  11858. (setq x (assoc ctf file-alist) had x)
  11859. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11860. (if to-end
  11861. (setq file-alist (append (delq x file-alist) (list x)))
  11862. (setq file-alist (cons x (delq x file-alist))))
  11863. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11864. (org-install-agenda-files-menu)
  11865. (message "File %s to %s of agenda file list"
  11866. (if had "moved" "added") (if to-end "end" "front"))))
  11867. (defun org-remove-file (&optional file)
  11868. "Remove current file from the list of files in variable `org-agenda-files'.
  11869. These are the files which are being checked for agenda entries.
  11870. Optional argument FILE means, use this file instead of the current."
  11871. (interactive)
  11872. (let* ((org-agenda-skip-unavailable-files nil)
  11873. (file (or file buffer-file-name))
  11874. (true-file (file-truename file))
  11875. (afile (abbreviate-file-name file))
  11876. (files (delq nil (mapcar
  11877. (lambda (x)
  11878. (if (equal true-file
  11879. (file-truename x))
  11880. nil x))
  11881. (org-agenda-files t)))))
  11882. (if (not (= (length files) (length (org-agenda-files t))))
  11883. (progn
  11884. (org-store-new-agenda-file-list files)
  11885. (org-install-agenda-files-menu)
  11886. (message "Removed file: %s" afile))
  11887. (message "File was not in list: %s (not removed)" afile))))
  11888. (defun org-file-menu-entry (file)
  11889. (vector file (list 'find-file file) t))
  11890. (defun org-check-agenda-file (file)
  11891. "Make sure FILE exists. If not, ask user what to do."
  11892. (when (not (file-exists-p file))
  11893. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11894. (abbreviate-file-name file))
  11895. (let ((r (downcase (read-char-exclusive))))
  11896. (cond
  11897. ((equal r ?r)
  11898. (org-remove-file file)
  11899. (throw 'nextfile t))
  11900. (t (error "Abort"))))))
  11901. (defun org-get-agenda-file-buffer (file)
  11902. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11903. it to the list of buffers which might be released later."
  11904. (let ((buf (org-find-base-buffer-visiting file)))
  11905. (if buf
  11906. buf ; just return it
  11907. ;; Make a new buffer and remember it
  11908. (setq buf (find-file-noselect file))
  11909. (if buf (push buf org-agenda-new-buffers))
  11910. buf)))
  11911. (defun org-release-buffers (blist)
  11912. "Release all buffers in list, asking the user for confirmation when needed.
  11913. When a buffer is unmodified, it is just killed. When modified, it is saved
  11914. \(if the user agrees) and then killed."
  11915. (let (buf file)
  11916. (while (setq buf (pop blist))
  11917. (setq file (buffer-file-name buf))
  11918. (when (and (buffer-modified-p buf)
  11919. file
  11920. (y-or-n-p (format "Save file %s? " file)))
  11921. (with-current-buffer buf (save-buffer)))
  11922. (kill-buffer buf))))
  11923. (defun org-prepare-agenda-buffers (files)
  11924. "Create buffers for all agenda files, protect archived trees and comments."
  11925. (interactive)
  11926. (let ((pa '(:org-archived t))
  11927. (pc '(:org-comment t))
  11928. (pall '(:org-archived t :org-comment t))
  11929. (inhibit-read-only t)
  11930. (rea (concat ":" org-archive-tag ":"))
  11931. bmp file re)
  11932. (save-excursion
  11933. (save-restriction
  11934. (while (setq file (pop files))
  11935. (if (bufferp file)
  11936. (set-buffer file)
  11937. (org-check-agenda-file file)
  11938. (set-buffer (org-get-agenda-file-buffer file)))
  11939. (widen)
  11940. (setq bmp (buffer-modified-p))
  11941. (org-refresh-category-properties)
  11942. (setq org-todo-keywords-for-agenda
  11943. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11944. (setq org-done-keywords-for-agenda
  11945. (append org-done-keywords-for-agenda org-done-keywords))
  11946. (setq org-todo-keyword-alist-for-agenda
  11947. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11948. (setq org-tag-alist-for-agenda
  11949. (append org-tag-alist-for-agenda org-tag-alist))
  11950. (save-excursion
  11951. (remove-text-properties (point-min) (point-max) pall)
  11952. (when org-agenda-skip-archived-trees
  11953. (goto-char (point-min))
  11954. (while (re-search-forward rea nil t)
  11955. (if (org-on-heading-p t)
  11956. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11957. (goto-char (point-min))
  11958. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11959. (while (re-search-forward re nil t)
  11960. (add-text-properties
  11961. (match-beginning 0) (org-end-of-subtree t) pc)))
  11962. (set-buffer-modified-p bmp))))
  11963. (setq org-todo-keyword-alist-for-agenda
  11964. (org-uniquify org-todo-keyword-alist-for-agenda)
  11965. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11966. ;;;; Embedded LaTeX
  11967. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11968. "Keymap for the minor `org-cdlatex-mode'.")
  11969. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11970. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11971. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11972. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11973. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11974. (defvar org-cdlatex-texmathp-advice-is-done nil
  11975. "Flag remembering if we have applied the advice to texmathp already.")
  11976. (define-minor-mode org-cdlatex-mode
  11977. "Toggle the minor `org-cdlatex-mode'.
  11978. This mode supports entering LaTeX environment and math in LaTeX fragments
  11979. in Org-mode.
  11980. \\{org-cdlatex-mode-map}"
  11981. nil " OCDL" nil
  11982. (when org-cdlatex-mode (require 'cdlatex))
  11983. (unless org-cdlatex-texmathp-advice-is-done
  11984. (setq org-cdlatex-texmathp-advice-is-done t)
  11985. (defadvice texmathp (around org-math-always-on activate)
  11986. "Always return t in org-mode buffers.
  11987. This is because we want to insert math symbols without dollars even outside
  11988. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11989. an embedded LaTeX fragment, let texmathp do its job.
  11990. \\[org-cdlatex-mode-map]"
  11991. (interactive)
  11992. (let (p)
  11993. (cond
  11994. ((not (org-mode-p)) ad-do-it)
  11995. ((eq this-command 'cdlatex-math-symbol)
  11996. (setq ad-return-value t
  11997. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11998. (t
  11999. (let ((p (org-inside-LaTeX-fragment-p)))
  12000. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12001. (setq ad-return-value t
  12002. texmathp-why '("Org-mode embedded math" . 0))
  12003. (if p ad-do-it)))))))))
  12004. (defun turn-on-org-cdlatex ()
  12005. "Unconditionally turn on `org-cdlatex-mode'."
  12006. (org-cdlatex-mode 1))
  12007. (defun org-inside-LaTeX-fragment-p ()
  12008. "Test if point is inside a LaTeX fragment.
  12009. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12010. sequence appearing also before point.
  12011. Even though the matchers for math are configurable, this function assumes
  12012. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12013. delimiters are skipped when they have been removed by customization.
  12014. The return value is nil, or a cons cell with the delimiter and
  12015. and the position of this delimiter.
  12016. This function does a reasonably good job, but can locally be fooled by
  12017. for example currency specifications. For example it will assume being in
  12018. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12019. fragments that are properly closed, but during editing, we have to live
  12020. with the uncertainty caused by missing closing delimiters. This function
  12021. looks only before point, not after."
  12022. (catch 'exit
  12023. (let ((pos (point))
  12024. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12025. (lim (progn
  12026. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12027. (point)))
  12028. dd-on str (start 0) m re)
  12029. (goto-char pos)
  12030. (when dodollar
  12031. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12032. re (nth 1 (assoc "$" org-latex-regexps)))
  12033. (while (string-match re str start)
  12034. (cond
  12035. ((= (match-end 0) (length str))
  12036. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12037. ((= (match-end 0) (- (length str) 5))
  12038. (throw 'exit nil))
  12039. (t (setq start (match-end 0))))))
  12040. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12041. (goto-char pos)
  12042. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12043. (and (match-beginning 2) (throw 'exit nil))
  12044. ;; count $$
  12045. (while (re-search-backward "\\$\\$" lim t)
  12046. (setq dd-on (not dd-on)))
  12047. (goto-char pos)
  12048. (if dd-on (cons "$$" m))))))
  12049. (defun org-try-cdlatex-tab ()
  12050. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12051. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12052. - inside a LaTeX fragment, or
  12053. - after the first word in a line, where an abbreviation expansion could
  12054. insert a LaTeX environment."
  12055. (when org-cdlatex-mode
  12056. (cond
  12057. ((save-excursion
  12058. (skip-chars-backward "a-zA-Z0-9*")
  12059. (skip-chars-backward " \t")
  12060. (bolp))
  12061. (cdlatex-tab) t)
  12062. ((org-inside-LaTeX-fragment-p)
  12063. (cdlatex-tab) t)
  12064. (t nil))))
  12065. (defun org-cdlatex-underscore-caret (&optional arg)
  12066. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12067. Revert to the normal definition outside of these fragments."
  12068. (interactive "P")
  12069. (if (org-inside-LaTeX-fragment-p)
  12070. (call-interactively 'cdlatex-sub-superscript)
  12071. (let (org-cdlatex-mode)
  12072. (call-interactively (key-binding (vector last-input-event))))))
  12073. (defun org-cdlatex-math-modify (&optional arg)
  12074. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12075. Revert to the normal definition outside of these fragments."
  12076. (interactive "P")
  12077. (if (org-inside-LaTeX-fragment-p)
  12078. (call-interactively 'cdlatex-math-modify)
  12079. (let (org-cdlatex-mode)
  12080. (call-interactively (key-binding (vector last-input-event))))))
  12081. (defvar org-latex-fragment-image-overlays nil
  12082. "List of overlays carrying the images of latex fragments.")
  12083. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12084. (defun org-remove-latex-fragment-image-overlays ()
  12085. "Remove all overlays with LaTeX fragment images in current buffer."
  12086. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12087. (setq org-latex-fragment-image-overlays nil))
  12088. (defun org-preview-latex-fragment (&optional subtree)
  12089. "Preview the LaTeX fragment at point, or all locally or globally.
  12090. If the cursor is in a LaTeX fragment, create the image and overlay
  12091. it over the source code. If there is no fragment at point, display
  12092. all fragments in the current text, from one headline to the next. With
  12093. prefix SUBTREE, display all fragments in the current subtree. With a
  12094. double prefix `C-u C-u', or when the cursor is before the first headline,
  12095. display all fragments in the buffer.
  12096. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12097. (interactive "P")
  12098. (org-remove-latex-fragment-image-overlays)
  12099. (save-excursion
  12100. (save-restriction
  12101. (let (beg end at msg)
  12102. (cond
  12103. ((or (equal subtree '(16))
  12104. (not (save-excursion
  12105. (re-search-backward (concat "^" outline-regexp) nil t))))
  12106. (setq beg (point-min) end (point-max)
  12107. msg "Creating images for buffer...%s"))
  12108. ((equal subtree '(4))
  12109. (org-back-to-heading)
  12110. (setq beg (point) end (org-end-of-subtree t)
  12111. msg "Creating images for subtree...%s"))
  12112. (t
  12113. (if (setq at (org-inside-LaTeX-fragment-p))
  12114. (goto-char (max (point-min) (- (cdr at) 2)))
  12115. (org-back-to-heading))
  12116. (setq beg (point) end (progn (outline-next-heading) (point))
  12117. msg (if at "Creating image...%s"
  12118. "Creating images for entry...%s"))))
  12119. (message msg "")
  12120. (narrow-to-region beg end)
  12121. (goto-char beg)
  12122. (org-format-latex
  12123. (concat "ltxpng/" (file-name-sans-extension
  12124. (file-name-nondirectory
  12125. buffer-file-name)))
  12126. default-directory 'overlays msg at 'forbuffer)
  12127. (message msg "done. Use `C-c C-c' to remove images.")))))
  12128. (defvar org-latex-regexps
  12129. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12130. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12131. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12132. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12133. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12134. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12135. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12136. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12137. "Regular expressions for matching embedded LaTeX.")
  12138. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12139. "Replace LaTeX fragments with links to an image, and produce images."
  12140. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12141. (let* ((prefixnodir (file-name-nondirectory prefix))
  12142. (absprefix (expand-file-name prefix dir))
  12143. (todir (file-name-directory absprefix))
  12144. (opt org-format-latex-options)
  12145. (matchers (plist-get opt :matchers))
  12146. (re-list org-latex-regexps)
  12147. (cnt 0) txt link beg end re e checkdir
  12148. m n block linkfile movefile ov)
  12149. ;; Check if there are old images files with this prefix, and remove them
  12150. (when (file-directory-p todir)
  12151. (mapc 'delete-file
  12152. (directory-files
  12153. todir 'full
  12154. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12155. ;; Check the different regular expressions
  12156. (while (setq e (pop re-list))
  12157. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12158. block (if (nth 3 e) "\n\n" ""))
  12159. (when (member m matchers)
  12160. (goto-char (point-min))
  12161. (while (re-search-forward re nil t)
  12162. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12163. (not (get-text-property (match-beginning n)
  12164. 'org-protected)))
  12165. (setq txt (match-string n)
  12166. beg (match-beginning n) end (match-end n)
  12167. cnt (1+ cnt)
  12168. linkfile (format "%s_%04d.png" prefix cnt)
  12169. movefile (format "%s_%04d.png" absprefix cnt)
  12170. link (concat block "[[file:" linkfile "]]" block))
  12171. (if msg (message msg cnt))
  12172. (goto-char beg)
  12173. (unless checkdir ; make sure the directory exists
  12174. (setq checkdir t)
  12175. (or (file-directory-p todir) (make-directory todir)))
  12176. (org-create-formula-image
  12177. txt movefile opt forbuffer)
  12178. (if overlays
  12179. (progn
  12180. (setq ov (org-make-overlay beg end))
  12181. (if (featurep 'xemacs)
  12182. (progn
  12183. (org-overlay-put ov 'invisible t)
  12184. (org-overlay-put
  12185. ov 'end-glyph
  12186. (make-glyph (vector 'png :file movefile))))
  12187. (org-overlay-put
  12188. ov 'display
  12189. (list 'image :type 'png :file movefile :ascent 'center)))
  12190. (push ov org-latex-fragment-image-overlays)
  12191. (goto-char end))
  12192. (delete-region beg end)
  12193. (insert link))))))))
  12194. ;; This function borrows from Ganesh Swami's latex2png.el
  12195. (defun org-create-formula-image (string tofile options buffer)
  12196. (let* ((tmpdir (if (featurep 'xemacs)
  12197. (temp-directory)
  12198. temporary-file-directory))
  12199. (texfilebase (make-temp-name
  12200. (expand-file-name "orgtex" tmpdir)))
  12201. (texfile (concat texfilebase ".tex"))
  12202. (dvifile (concat texfilebase ".dvi"))
  12203. (pngfile (concat texfilebase ".png"))
  12204. (fnh (if (featurep 'xemacs)
  12205. (font-height (get-face-font 'default))
  12206. (face-attribute 'default :height nil)))
  12207. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12208. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12209. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12210. "Black"))
  12211. (bg (or (plist-get options (if buffer :background :html-background))
  12212. "Transparent")))
  12213. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12214. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12215. (with-temp-file texfile
  12216. (insert org-format-latex-header
  12217. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12218. (let ((dir default-directory))
  12219. (condition-case nil
  12220. (progn
  12221. (cd tmpdir)
  12222. (call-process "latex" nil nil nil texfile))
  12223. (error nil))
  12224. (cd dir))
  12225. (if (not (file-exists-p dvifile))
  12226. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12227. (condition-case nil
  12228. (call-process "dvipng" nil nil nil
  12229. "-E" "-fg" fg "-bg" bg
  12230. "-D" dpi
  12231. ;;"-x" scale "-y" scale
  12232. "-T" "tight"
  12233. "-o" pngfile
  12234. dvifile)
  12235. (error nil))
  12236. (if (not (file-exists-p pngfile))
  12237. (progn (message "Failed to create png file from %s" texfile) nil)
  12238. ;; Use the requested file name and clean up
  12239. (copy-file pngfile tofile 'replace)
  12240. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12241. (delete-file (concat texfilebase e)))
  12242. pngfile))))
  12243. (defun org-dvipng-color (attr)
  12244. "Return an rgb color specification for dvipng."
  12245. (apply 'format "rgb %s %s %s"
  12246. (mapcar 'org-normalize-color
  12247. (color-values (face-attribute 'default attr nil)))))
  12248. (defun org-normalize-color (value)
  12249. "Return string to be used as color value for an RGB component."
  12250. (format "%g" (/ value 65535.0)))
  12251. ;;;; Key bindings
  12252. ;; Make `C-c C-x' a prefix key
  12253. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12254. ;; TAB key with modifiers
  12255. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12256. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12257. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12258. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12259. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12260. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12261. ;; The following line is necessary under Suse GNU/Linux
  12262. (unless (featurep 'xemacs)
  12263. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12264. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12265. (define-key org-mode-map [backtab] 'org-shifttab)
  12266. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12267. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12268. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12269. ;; Cursor keys with modifiers
  12270. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12271. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12272. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12273. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12274. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12275. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12276. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12277. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12278. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12279. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12280. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12281. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12282. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12283. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12284. ;;; Extra keys for tty access.
  12285. ;; We only set them when really needed because otherwise the
  12286. ;; menus don't show the simple keys
  12287. (when (or org-use-extra-keys
  12288. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12289. (not window-system))
  12290. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12291. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12292. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12293. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12294. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12295. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12296. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12297. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12298. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12299. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12300. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12301. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12302. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12303. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12304. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12305. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12306. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12307. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12308. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12309. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12310. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12311. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12312. ;; All the other keys
  12313. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12314. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12315. (if (boundp 'narrow-map)
  12316. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12317. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12318. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12319. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12320. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12321. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12322. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12323. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12324. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12325. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12326. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12327. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12328. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12329. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12330. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12331. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12332. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12333. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12334. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12335. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12336. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12337. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12338. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12339. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12340. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12341. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12342. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12343. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12344. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12345. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12346. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12347. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12348. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12349. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12350. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12351. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12352. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12353. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12354. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12355. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12356. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12357. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12358. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12359. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12360. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12361. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12362. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12363. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12364. (org-defkey org-mode-map "\C-m" 'org-return)
  12365. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12366. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12367. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12368. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12369. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12370. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12371. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12372. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12373. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12374. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12375. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12376. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12377. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12378. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12379. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12380. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12381. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12382. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12383. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12384. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12385. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12386. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12387. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12388. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12389. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12390. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12391. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12392. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12393. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12394. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12395. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12396. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12397. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12398. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12399. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12400. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12401. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12402. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12403. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12404. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12405. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12406. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12407. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12408. (when (featurep 'xemacs)
  12409. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12410. (defvar org-self-insert-command-undo-counter 0)
  12411. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12412. (defun org-self-insert-command (N)
  12413. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12414. If the cursor is in a table looking at whitespace, the whitespace is
  12415. overwritten, and the table is not marked as requiring realignment."
  12416. (interactive "p")
  12417. (if (and
  12418. (org-table-p)
  12419. (progn
  12420. ;; check if we blank the field, and if that triggers align
  12421. (and (featurep 'org-table) org-table-auto-blank-field
  12422. (member last-command
  12423. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12424. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12425. ;; got extra space, this field does not determine column width
  12426. (let (org-table-may-need-update) (org-table-blank-field))
  12427. ;; no extra space, this field may determine column width
  12428. (org-table-blank-field)))
  12429. t)
  12430. (eq N 1)
  12431. (looking-at "[^|\n]* |"))
  12432. (let (org-table-may-need-update)
  12433. (goto-char (1- (match-end 0)))
  12434. (delete-backward-char 1)
  12435. (goto-char (match-beginning 0))
  12436. (self-insert-command N))
  12437. (setq org-table-may-need-update t)
  12438. (self-insert-command N)
  12439. (org-fix-tags-on-the-fly)
  12440. (if org-self-insert-cluster-for-undo
  12441. (if (not (eq last-command 'org-self-insert-command))
  12442. (setq org-self-insert-command-undo-counter 1)
  12443. (if (>= org-self-insert-command-undo-counter 20)
  12444. (setq org-self-insert-command-undo-counter 1)
  12445. (and (> org-self-insert-command-undo-counter 0)
  12446. buffer-undo-list
  12447. (not (cadr buffer-undo-list)) ; remove nil entry
  12448. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12449. (setq org-self-insert-command-undo-counter
  12450. (1+ org-self-insert-command-undo-counter)))))))
  12451. (defun org-fix-tags-on-the-fly ()
  12452. (when (and (equal (char-after (point-at-bol)) ?*)
  12453. (org-on-heading-p))
  12454. (org-align-tags-here org-tags-column)))
  12455. (defun org-delete-backward-char (N)
  12456. "Like `delete-backward-char', insert whitespace at field end in tables.
  12457. When deleting backwards, in tables this function will insert whitespace in
  12458. front of the next \"|\" separator, to keep the table aligned. The table will
  12459. still be marked for re-alignment if the field did fill the entire column,
  12460. because, in this case the deletion might narrow the column."
  12461. (interactive "p")
  12462. (if (and (org-table-p)
  12463. (eq N 1)
  12464. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12465. (looking-at ".*?|"))
  12466. (let ((pos (point))
  12467. (noalign (looking-at "[^|\n\r]* |"))
  12468. (c org-table-may-need-update))
  12469. (backward-delete-char N)
  12470. (skip-chars-forward "^|")
  12471. (insert " ")
  12472. (goto-char (1- pos))
  12473. ;; noalign: if there were two spaces at the end, this field
  12474. ;; does not determine the width of the column.
  12475. (if noalign (setq org-table-may-need-update c)))
  12476. (backward-delete-char N)
  12477. (org-fix-tags-on-the-fly)))
  12478. (defun org-delete-char (N)
  12479. "Like `delete-char', but insert whitespace at field end in tables.
  12480. When deleting characters, in tables this function will insert whitespace in
  12481. front of the next \"|\" separator, to keep the table aligned. The table will
  12482. still be marked for re-alignment if the field did fill the entire column,
  12483. because, in this case the deletion might narrow the column."
  12484. (interactive "p")
  12485. (if (and (org-table-p)
  12486. (not (bolp))
  12487. (not (= (char-after) ?|))
  12488. (eq N 1))
  12489. (if (looking-at ".*?|")
  12490. (let ((pos (point))
  12491. (noalign (looking-at "[^|\n\r]* |"))
  12492. (c org-table-may-need-update))
  12493. (replace-match (concat
  12494. (substring (match-string 0) 1 -1)
  12495. " |"))
  12496. (goto-char pos)
  12497. ;; noalign: if there were two spaces at the end, this field
  12498. ;; does not determine the width of the column.
  12499. (if noalign (setq org-table-may-need-update c)))
  12500. (delete-char N))
  12501. (delete-char N)
  12502. (org-fix-tags-on-the-fly)))
  12503. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12504. (put 'org-self-insert-command 'delete-selection t)
  12505. (put 'orgtbl-self-insert-command 'delete-selection t)
  12506. (put 'org-delete-char 'delete-selection 'supersede)
  12507. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12508. (put 'org-yank 'delete-selection 'yank)
  12509. ;; Make `flyspell-mode' delay after some commands
  12510. (put 'org-self-insert-command 'flyspell-delayed t)
  12511. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12512. (put 'org-delete-char 'flyspell-delayed t)
  12513. (put 'org-delete-backward-char 'flyspell-delayed t)
  12514. ;; Make pabbrev-mode expand after org-mode commands
  12515. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12516. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12517. ;; How to do this: Measure non-white length of current string
  12518. ;; If equal to column width, we should realign.
  12519. (defun org-remap (map &rest commands)
  12520. "In MAP, remap the functions given in COMMANDS.
  12521. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12522. (let (new old)
  12523. (while commands
  12524. (setq old (pop commands) new (pop commands))
  12525. (if (fboundp 'command-remapping)
  12526. (org-defkey map (vector 'remap old) new)
  12527. (substitute-key-definition old new map global-map)))))
  12528. (when (eq org-enable-table-editor 'optimized)
  12529. ;; If the user wants maximum table support, we need to hijack
  12530. ;; some standard editing functions
  12531. (org-remap org-mode-map
  12532. 'self-insert-command 'org-self-insert-command
  12533. 'delete-char 'org-delete-char
  12534. 'delete-backward-char 'org-delete-backward-char)
  12535. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12536. (defvar org-ctrl-c-ctrl-c-hook nil
  12537. "Hook for functions attaching themselves to `C-c C-c'.
  12538. This can be used to add additional functionality to the C-c C-c key which
  12539. executes context-dependent commands.
  12540. Each function will be called with no arguments. The function must check
  12541. if the context is appropriate for it to act. If yes, it should do its
  12542. thing and then return a non-nil value. If the context is wrong,
  12543. just do nothing.")
  12544. (defvar org-metaleft-hook nil
  12545. "Hook for functions attaching themselves to `M-left'.
  12546. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12547. (defvar org-metaright-hook nil
  12548. "Hook for functions attaching themselves to `M-right'.
  12549. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12550. (defvar org-metaup-hook nil
  12551. "Hook for functions attaching themselves to `M-up'.
  12552. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12553. (defvar org-metadown-hook nil
  12554. "Hook for functions attaching themselves to `M-down'.
  12555. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12556. (defvar org-shiftmetaleft-hook nil
  12557. "Hook for functions attaching themselves to `M-S-left'.
  12558. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12559. (defvar org-shiftmetaright-hook nil
  12560. "Hook for functions attaching themselves to `M-S-right'.
  12561. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12562. (defvar org-shiftmetaup-hook nil
  12563. "Hook for functions attaching themselves to `M-S-up'.
  12564. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12565. (defvar org-shiftmetadown-hook nil
  12566. "Hook for functions attaching themselves to `M-S-down'.
  12567. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12568. (defvar org-metareturn-hook nil
  12569. "Hook for functions attaching themselves to `M-RET'.
  12570. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12571. (defun org-modifier-cursor-error ()
  12572. "Throw an error, a modified cursor command was applied in wrong context."
  12573. (error "This command is active in special context like tables, headlines or items"))
  12574. (defun org-shiftselect-error ()
  12575. "Throw an error because Shift-Cursor command was applied in wrong context."
  12576. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12577. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12578. (error "This command works only in special context like headlines or timestamps.")))
  12579. (defun org-call-for-shift-select (cmd)
  12580. (let ((this-command-keys-shift-translated t))
  12581. (call-interactively cmd)))
  12582. (defun org-shifttab (&optional arg)
  12583. "Global visibility cycling or move to previous table field.
  12584. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12585. on context.
  12586. See the individual commands for more information."
  12587. (interactive "P")
  12588. (cond
  12589. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12590. ((integerp arg)
  12591. (message "Content view to level: %d" arg)
  12592. (org-content (prefix-numeric-value arg))
  12593. (setq org-cycle-global-status 'overview))
  12594. (t (call-interactively 'org-global-cycle))))
  12595. (defun org-shiftmetaleft ()
  12596. "Promote subtree or delete table column.
  12597. Calls `org-promote-subtree', `org-outdent-item',
  12598. or `org-table-delete-column', depending on context.
  12599. See the individual commands for more information."
  12600. (interactive)
  12601. (cond
  12602. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12603. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12604. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12605. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12606. (t (org-modifier-cursor-error))))
  12607. (defun org-shiftmetaright ()
  12608. "Demote subtree or insert table column.
  12609. Calls `org-demote-subtree', `org-indent-item',
  12610. or `org-table-insert-column', depending on context.
  12611. See the individual commands for more information."
  12612. (interactive)
  12613. (cond
  12614. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12615. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12616. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12617. ((org-at-item-p) (call-interactively 'org-indent-item))
  12618. (t (org-modifier-cursor-error))))
  12619. (defun org-shiftmetaup (&optional arg)
  12620. "Move subtree up or kill table row.
  12621. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12622. `org-move-item-up' depending on context. See the individual commands
  12623. for more information."
  12624. (interactive "P")
  12625. (cond
  12626. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12627. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12628. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12629. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12630. (t (org-modifier-cursor-error))))
  12631. (defun org-shiftmetadown (&optional arg)
  12632. "Move subtree down or insert table row.
  12633. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12634. `org-move-item-down', depending on context. See the individual
  12635. commands for more information."
  12636. (interactive "P")
  12637. (cond
  12638. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12639. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12640. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12641. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12642. (t (org-modifier-cursor-error))))
  12643. (defun org-metaleft (&optional arg)
  12644. "Promote heading or move table column to left.
  12645. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12646. With no specific context, calls the Emacs default `backward-word'.
  12647. See the individual commands for more information."
  12648. (interactive "P")
  12649. (cond
  12650. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12651. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12652. ((or (org-on-heading-p)
  12653. (and (org-region-active-p)
  12654. (save-excursion
  12655. (goto-char (region-beginning))
  12656. (org-on-heading-p))))
  12657. (call-interactively 'org-do-promote))
  12658. ((or (org-at-item-p)
  12659. (and (org-region-active-p)
  12660. (save-excursion
  12661. (goto-char (region-beginning))
  12662. (org-at-item-p))))
  12663. (call-interactively 'org-outdent-item))
  12664. (t (call-interactively 'backward-word))))
  12665. (defun org-metaright (&optional arg)
  12666. "Demote subtree or move table column to right.
  12667. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12668. With no specific context, calls the Emacs default `forward-word'.
  12669. See the individual commands for more information."
  12670. (interactive "P")
  12671. (cond
  12672. ((run-hook-with-args-until-success 'org-metaright-hook))
  12673. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12674. ((or (org-on-heading-p)
  12675. (and (org-region-active-p)
  12676. (save-excursion
  12677. (goto-char (region-beginning))
  12678. (org-on-heading-p))))
  12679. (call-interactively 'org-do-demote))
  12680. ((or (org-at-item-p)
  12681. (and (org-region-active-p)
  12682. (save-excursion
  12683. (goto-char (region-beginning))
  12684. (org-at-item-p))))
  12685. (call-interactively 'org-indent-item))
  12686. (t (call-interactively 'forward-word))))
  12687. (defun org-metaup (&optional arg)
  12688. "Move subtree up or move table row up.
  12689. Calls `org-move-subtree-up' or `org-table-move-row' or
  12690. `org-move-item-up', depending on context. See the individual commands
  12691. for more information."
  12692. (interactive "P")
  12693. (cond
  12694. ((run-hook-with-args-until-success 'org-metaup-hook))
  12695. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12696. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12697. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12698. (t (transpose-lines 1) (beginning-of-line -1))))
  12699. (defun org-metadown (&optional arg)
  12700. "Move subtree down or move table row down.
  12701. Calls `org-move-subtree-down' or `org-table-move-row' or
  12702. `org-move-item-down', depending on context. See the individual
  12703. commands for more information."
  12704. (interactive "P")
  12705. (cond
  12706. ((run-hook-with-args-until-success 'org-metadown-hook))
  12707. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12708. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12709. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12710. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12711. (defun org-shiftup (&optional arg)
  12712. "Increase item in timestamp or increase priority of current headline.
  12713. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12714. depending on context. See the individual commands for more information."
  12715. (interactive "P")
  12716. (cond
  12717. ((and org-support-shift-select (org-region-active-p))
  12718. (org-call-for-shift-select 'previous-line))
  12719. ((org-at-timestamp-p t)
  12720. (call-interactively (if org-edit-timestamp-down-means-later
  12721. 'org-timestamp-down 'org-timestamp-up)))
  12722. ((and (not (eq org-support-shift-select 'always))
  12723. (org-on-heading-p))
  12724. (call-interactively 'org-priority-up))
  12725. ((and (not org-support-shift-select) (org-at-item-p))
  12726. (call-interactively 'org-previous-item))
  12727. ((org-clocktable-try-shift 'up arg))
  12728. (org-support-shift-select
  12729. (org-call-for-shift-select 'previous-line))
  12730. (t (org-shiftselect-error))))
  12731. (defun org-shiftdown (&optional arg)
  12732. "Decrease item in timestamp or decrease priority of current headline.
  12733. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12734. depending on context. See the individual commands for more information."
  12735. (interactive "P")
  12736. (cond
  12737. ((and org-support-shift-select (org-region-active-p))
  12738. (org-call-for-shift-select 'next-line))
  12739. ((org-at-timestamp-p t)
  12740. (call-interactively (if org-edit-timestamp-down-means-later
  12741. 'org-timestamp-up 'org-timestamp-down)))
  12742. ((and (not (eq org-support-shift-select 'always))
  12743. (org-on-heading-p))
  12744. (call-interactively 'org-priority-down))
  12745. ((and (not org-support-shift-select) (org-at-item-p))
  12746. (call-interactively 'org-next-item))
  12747. ((org-clocktable-try-shift 'down arg))
  12748. (org-support-shift-select
  12749. (org-call-for-shift-select 'next-line))
  12750. (t (org-shiftselect-error))))
  12751. (defun org-shiftright (&optional arg)
  12752. "Cycle the thing at point or in the current line, depending on context.
  12753. Depending on context, this does one of the following:
  12754. - switch a timestamp at point one day into the future
  12755. - on a headline, switch to the next TODO keyword.
  12756. - on an item, switch entire list to the next bullet type
  12757. - on a property line, switch to the next allowed value
  12758. - on a clocktable definition line, move time block into the future"
  12759. (interactive "P")
  12760. (cond
  12761. ((and org-support-shift-select (org-region-active-p))
  12762. (org-call-for-shift-select 'forward-char))
  12763. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12764. ((and (not (eq org-support-shift-select 'always))
  12765. (org-on-heading-p))
  12766. (org-call-with-arg 'org-todo 'right))
  12767. ((or (and org-support-shift-select
  12768. (not (eq org-support-shift-select 'always))
  12769. (org-at-item-bullet-p))
  12770. (and (not org-support-shift-select) (org-at-item-p)))
  12771. (org-call-with-arg 'org-cycle-list-bullet nil))
  12772. ((and (not (eq org-support-shift-select 'always))
  12773. (org-at-property-p))
  12774. (call-interactively 'org-property-next-allowed-value))
  12775. ((org-clocktable-try-shift 'right arg))
  12776. (org-support-shift-select
  12777. (org-call-for-shift-select 'forward-char))
  12778. (t (org-shiftselect-error))))
  12779. (defun org-shiftleft (&optional arg)
  12780. "Cycle the thing at point or in the current line, depending on context.
  12781. Depending on context, this does one of the following:
  12782. - switch a timestamp at point one day into the past
  12783. - on a headline, switch to the previous TODO keyword.
  12784. - on an item, switch entire list to the previous bullet type
  12785. - on a property line, switch to the previous allowed value
  12786. - on a clocktable definition line, move time block into the past"
  12787. (interactive "P")
  12788. (cond
  12789. ((and org-support-shift-select (org-region-active-p))
  12790. (org-call-for-shift-select 'backward-char))
  12791. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12792. ((and (not (eq org-support-shift-select 'always))
  12793. (org-on-heading-p))
  12794. (org-call-with-arg 'org-todo 'left))
  12795. ((or (and org-support-shift-select
  12796. (not (eq org-support-shift-select 'always))
  12797. (org-at-item-bullet-p))
  12798. (and (not org-support-shift-select) (org-at-item-p)))
  12799. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12800. ((and (not (eq org-support-shift-select 'always))
  12801. (org-at-property-p))
  12802. (call-interactively 'org-property-previous-allowed-value))
  12803. ((org-clocktable-try-shift 'left arg))
  12804. (org-support-shift-select
  12805. (org-call-for-shift-select 'backward-char))
  12806. (t (org-shiftselect-error))))
  12807. (defun org-shiftcontrolright ()
  12808. "Switch to next TODO set."
  12809. (interactive)
  12810. (cond
  12811. ((and org-support-shift-select (org-region-active-p))
  12812. (org-call-for-shift-select 'forward-word))
  12813. ((and (not (eq org-support-shift-select 'always))
  12814. (org-on-heading-p))
  12815. (org-call-with-arg 'org-todo 'nextset))
  12816. (org-support-shift-select
  12817. (org-call-for-shift-select 'forward-word))
  12818. (t (org-shiftselect-error))))
  12819. (defun org-shiftcontrolleft ()
  12820. "Switch to previous TODO set."
  12821. (interactive)
  12822. (cond
  12823. ((and org-support-shift-select (org-region-active-p))
  12824. (org-call-for-shift-select 'backward-word))
  12825. ((and (not (eq org-support-shift-select 'always))
  12826. (org-on-heading-p))
  12827. (org-call-with-arg 'org-todo 'previousset))
  12828. (org-support-shift-select
  12829. (org-call-for-shift-select 'backward-word))
  12830. (t (org-shiftselect-error))))
  12831. (defun org-ctrl-c-ret ()
  12832. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12833. (interactive)
  12834. (cond
  12835. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12836. (t (call-interactively 'org-insert-heading))))
  12837. (defun org-copy-special ()
  12838. "Copy region in table or copy current subtree.
  12839. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12840. See the individual commands for more information."
  12841. (interactive)
  12842. (call-interactively
  12843. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12844. (defun org-cut-special ()
  12845. "Cut region in table or cut current subtree.
  12846. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12847. See the individual commands for more information."
  12848. (interactive)
  12849. (call-interactively
  12850. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12851. (defun org-paste-special (arg)
  12852. "Paste rectangular region into table, or past subtree relative to level.
  12853. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12854. See the individual commands for more information."
  12855. (interactive "P")
  12856. (if (org-at-table-p)
  12857. (org-table-paste-rectangle)
  12858. (org-paste-subtree arg)))
  12859. (defun org-edit-special ()
  12860. "Call a special editor for the stuff at point.
  12861. When at a table, call the formula editor with `org-table-edit-formulas'.
  12862. When at the first line of an src example, call `org-edit-src-code'.
  12863. When in an #+include line, visit the include file. Otherwise call
  12864. `ffap' to visit the file at point."
  12865. (interactive)
  12866. (cond
  12867. ((org-at-table-p)
  12868. (call-interactively 'org-table-edit-formulas))
  12869. ((save-excursion
  12870. (beginning-of-line 1)
  12871. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12872. (find-file (org-trim (match-string 1))))
  12873. ((org-edit-src-code))
  12874. ((org-edit-fixed-width-region))
  12875. (t (call-interactively 'ffap))))
  12876. (defun org-ctrl-c-ctrl-c (&optional arg)
  12877. "Set tags in headline, or update according to changed information at point.
  12878. This command does many different things, depending on context:
  12879. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12880. this is what we do.
  12881. - If the cursor is in a headline, prompt for tags and insert them
  12882. into the current line, aligned to `org-tags-column'. When called
  12883. with prefix arg, realign all tags in the current buffer.
  12884. - If the cursor is in one of the special #+KEYWORD lines, this
  12885. triggers scanning the buffer for these lines and updating the
  12886. information.
  12887. - If the cursor is inside a table, realign the table. This command
  12888. works even if the automatic table editor has been turned off.
  12889. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12890. the entire table.
  12891. - If the cursor is at a footnote reference or definition, jump to
  12892. the corresponding definition or references, respectively.
  12893. - If the cursor is a the beginning of a dynamic block, update it.
  12894. - If the cursor is inside a table created by the table.el package,
  12895. activate that table.
  12896. - If the current buffer is a remember buffer, close note and file
  12897. it. A prefix argument of 1 files to the default location
  12898. without further interaction. A prefix argument of 2 files to
  12899. the currently clocking task.
  12900. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12901. links in this buffer.
  12902. - If the cursor is on a numbered item in a plain list, renumber the
  12903. ordered list.
  12904. - If the cursor is on a checkbox, toggle it."
  12905. (interactive "P")
  12906. (let ((org-enable-table-editor t))
  12907. (cond
  12908. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12909. org-occur-highlights
  12910. org-latex-fragment-image-overlays)
  12911. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12912. (org-remove-occur-highlights)
  12913. (org-remove-latex-fragment-image-overlays)
  12914. (message "Temporary highlights/overlays removed from current buffer"))
  12915. ((and (local-variable-p 'org-finish-function (current-buffer))
  12916. (fboundp org-finish-function))
  12917. (funcall org-finish-function))
  12918. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12919. ((org-at-property-p)
  12920. (call-interactively 'org-property-action))
  12921. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12922. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12923. ((org-at-table.el-p)
  12924. (require 'table)
  12925. (beginning-of-line 1)
  12926. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12927. (call-interactively 'table-recognize-table))
  12928. ((org-at-table-p)
  12929. (org-table-maybe-eval-formula)
  12930. (if arg
  12931. (call-interactively 'org-table-recalculate)
  12932. (org-table-maybe-recalculate-line))
  12933. (call-interactively 'org-table-align))
  12934. ((or (org-footnote-at-reference-p)
  12935. (org-footnote-at-definition-p))
  12936. (call-interactively 'org-footnote-action))
  12937. ((org-at-item-checkbox-p)
  12938. (call-interactively 'org-toggle-checkbox))
  12939. ((org-at-item-p)
  12940. (if arg
  12941. (call-interactively 'org-toggle-checkbox)
  12942. (call-interactively 'org-maybe-renumber-ordered-list)))
  12943. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12944. ;; Dynamic block
  12945. (beginning-of-line 1)
  12946. (save-excursion (org-update-dblock)))
  12947. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12948. (cond
  12949. ((equal (match-string 1) "TBLFM")
  12950. ;; Recalculate the table before this line
  12951. (save-excursion
  12952. (beginning-of-line 1)
  12953. (skip-chars-backward " \r\n\t")
  12954. (if (org-at-table-p)
  12955. (org-call-with-arg 'org-table-recalculate t))))
  12956. (t
  12957. ; (org-set-regexps-and-options)
  12958. ; (org-restart-font-lock)
  12959. (let ((org-inhibit-startup t)) (org-mode-restart))
  12960. (message "Local setup has been refreshed"))))
  12961. (t (error "C-c C-c can do nothing useful at this location.")))))
  12962. (defun org-mode-restart ()
  12963. "Restart Org-mode, to scan again for special lines.
  12964. Also updates the keyword regular expressions."
  12965. (interactive)
  12966. (org-mode)
  12967. (message "Org-mode restarted"))
  12968. (defun org-kill-note-or-show-branches ()
  12969. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12970. (interactive)
  12971. (if (not org-finish-function)
  12972. (call-interactively 'show-branches)
  12973. (let ((org-note-abort t))
  12974. (funcall org-finish-function))))
  12975. (defun org-return (&optional indent)
  12976. "Goto next table row or insert a newline.
  12977. Calls `org-table-next-row' or `newline', depending on context.
  12978. See the individual commands for more information."
  12979. (interactive)
  12980. (cond
  12981. ((bobp) (if indent (newline-and-indent) (newline)))
  12982. ((org-at-table-p)
  12983. (org-table-justify-field-maybe)
  12984. (call-interactively 'org-table-next-row))
  12985. ((and org-return-follows-link
  12986. (eq (get-text-property (point) 'face) 'org-link))
  12987. (call-interactively 'org-open-at-point))
  12988. ((and (org-at-heading-p)
  12989. (looking-at
  12990. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12991. (org-show-entry)
  12992. (end-of-line 1)
  12993. (newline))
  12994. (t (if indent (newline-and-indent) (newline)))))
  12995. (defun org-return-indent ()
  12996. "Goto next table row or insert a newline and indent.
  12997. Calls `org-table-next-row' or `newline-and-indent', depending on
  12998. context. See the individual commands for more information."
  12999. (interactive)
  13000. (org-return t))
  13001. (defun org-ctrl-c-star ()
  13002. "Compute table, or change heading status of lines.
  13003. Calls `org-table-recalculate' or `org-toggle-heading',
  13004. depending on context."
  13005. (interactive)
  13006. (cond
  13007. ((org-at-table-p)
  13008. (call-interactively 'org-table-recalculate))
  13009. (t
  13010. ;; Convert all lines in region to list items
  13011. (call-interactively 'org-toggle-heading))))
  13012. (defun org-ctrl-c-minus ()
  13013. "Insert separator line in table or modify bullet status of line.
  13014. Also turns a plain line or a region of lines into list items.
  13015. Calls `org-table-insert-hline', `org-toggle-item', or
  13016. `org-cycle-list-bullet', depending on context."
  13017. (interactive)
  13018. (cond
  13019. ((org-at-table-p)
  13020. (call-interactively 'org-table-insert-hline))
  13021. ((org-region-active-p)
  13022. (call-interactively 'org-toggle-item))
  13023. ((org-in-item-p)
  13024. (call-interactively 'org-cycle-list-bullet))
  13025. (t
  13026. (call-interactively 'org-toggle-item))))
  13027. (defun org-toggle-item ()
  13028. "Convert headings or normal lines to items, items to normal lines.
  13029. If there is no active region, only the current line is considered.
  13030. If the first line in the region is a headline, convert all headlines to items.
  13031. If the first line in the region is an item, convert all items to normal lines.
  13032. If the first line is normal text, add an item bullet to each line."
  13033. (interactive)
  13034. (let (l2 l beg end)
  13035. (if (org-region-active-p)
  13036. (setq beg (region-beginning) end (region-end))
  13037. (setq beg (point-at-bol)
  13038. end (min (1+ (point-at-eol)) (point-max))))
  13039. (save-excursion
  13040. (goto-char end)
  13041. (setq l2 (org-current-line))
  13042. (goto-char beg)
  13043. (beginning-of-line 1)
  13044. (setq l (1- (org-current-line)))
  13045. (if (org-at-item-p)
  13046. ;; We already have items, de-itemize
  13047. (while (< (setq l (1+ l)) l2)
  13048. (when (org-at-item-p)
  13049. (goto-char (match-beginning 2))
  13050. (delete-region (match-beginning 2) (match-end 2))
  13051. (and (looking-at "[ \t]+") (replace-match "")))
  13052. (beginning-of-line 2))
  13053. (if (org-on-heading-p)
  13054. ;; Headings, convert to items
  13055. (while (< (setq l (1+ l)) l2)
  13056. (if (looking-at org-outline-regexp)
  13057. (replace-match "- " t t))
  13058. (beginning-of-line 2))
  13059. ;; normal lines, turn them into items
  13060. (while (< (setq l (1+ l)) l2)
  13061. (unless (org-at-item-p)
  13062. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13063. (replace-match "\\1- \\2")))
  13064. (beginning-of-line 2)))))))
  13065. (defun org-toggle-heading (&optional nstars)
  13066. "Convert headings to normal text, or items or text to headings.
  13067. If there is no active region, only the current line is considered.
  13068. If the first line is a heading, remove the stars from all headlines
  13069. in the region.
  13070. If the first line is a plain list item, turn all plain list items into
  13071. headings.
  13072. If the first line is a normal line, turn each and every line in the region
  13073. into a heading.
  13074. When converting a line into a heading, the number of stars is chosen
  13075. such that the lines become children of the current entry. However, when
  13076. a prefix argument is given, its value determines the number of stars to add."
  13077. (interactive "P")
  13078. (let (l2 l itemp beg end)
  13079. (if (org-region-active-p)
  13080. (setq beg (region-beginning) end (region-end))
  13081. (setq beg (point-at-bol)
  13082. end (min (1+ (point-at-eol)) (point-max))))
  13083. (save-excursion
  13084. (goto-char end)
  13085. (setq l2 (org-current-line))
  13086. (goto-char beg)
  13087. (beginning-of-line 1)
  13088. (setq l (1- (org-current-line)))
  13089. (if (org-on-heading-p)
  13090. ;; We already have headlines, de-star them
  13091. (while (< (setq l (1+ l)) l2)
  13092. (when (org-on-heading-p t)
  13093. (and (looking-at outline-regexp) (replace-match "")))
  13094. (beginning-of-line 2))
  13095. (setq itemp (org-at-item-p))
  13096. (let* ((stars
  13097. (if nstars
  13098. (make-string (prefix-numeric-value current-prefix-arg)
  13099. ?*)
  13100. (save-excursion
  13101. (re-search-backward org-complex-heading-regexp nil t)
  13102. (or (match-string 1) ""))))
  13103. (add-stars (cond (nstars "")
  13104. ((equal stars "") "*")
  13105. (org-odd-levels-only "**")
  13106. (t "*")))
  13107. (rpl (concat stars add-stars " ")))
  13108. (while (< (setq l (1+ l)) l2)
  13109. (if itemp
  13110. (and (org-at-item-p) (replace-match rpl t t))
  13111. (unless (org-on-heading-p)
  13112. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13113. (replace-match (concat rpl (match-string 2))))))
  13114. (beginning-of-line 2)))))))
  13115. (defun org-meta-return (&optional arg)
  13116. "Insert a new heading or wrap a region in a table.
  13117. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13118. See the individual commands for more information."
  13119. (interactive "P")
  13120. (cond
  13121. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13122. ((org-at-table-p)
  13123. (call-interactively 'org-table-wrap-region))
  13124. (t (call-interactively 'org-insert-heading))))
  13125. ;;; Menu entries
  13126. ;; Define the Org-mode menus
  13127. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13128. '("Tbl"
  13129. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13130. ["Next Field" org-cycle (org-at-table-p)]
  13131. ["Previous Field" org-shifttab (org-at-table-p)]
  13132. ["Next Row" org-return (org-at-table-p)]
  13133. "--"
  13134. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13135. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13136. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13137. "--"
  13138. ("Column"
  13139. ["Move Column Left" org-metaleft (org-at-table-p)]
  13140. ["Move Column Right" org-metaright (org-at-table-p)]
  13141. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13142. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13143. ("Row"
  13144. ["Move Row Up" org-metaup (org-at-table-p)]
  13145. ["Move Row Down" org-metadown (org-at-table-p)]
  13146. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13147. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13148. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13149. "--"
  13150. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13151. ("Rectangle"
  13152. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13153. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13154. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13155. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13156. "--"
  13157. ("Calculate"
  13158. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13159. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13160. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13161. "--"
  13162. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13163. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13164. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13165. "--"
  13166. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13167. "--"
  13168. ["Sum Column/Rectangle" org-table-sum
  13169. (or (org-at-table-p) (org-region-active-p))]
  13170. ["Which Column?" org-table-current-column (org-at-table-p)])
  13171. ["Debug Formulas"
  13172. org-table-toggle-formula-debugger
  13173. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13174. ["Show Col/Row Numbers"
  13175. org-table-toggle-coordinate-overlays
  13176. :style toggle
  13177. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13178. "--"
  13179. ["Create" org-table-create (and (not (org-at-table-p))
  13180. org-enable-table-editor)]
  13181. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13182. ["Import from File" org-table-import (not (org-at-table-p))]
  13183. ["Export to File" org-table-export (org-at-table-p)]
  13184. "--"
  13185. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13186. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13187. '("Org"
  13188. ("Show/Hide"
  13189. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13190. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13191. ["Sparse Tree..." org-sparse-tree t]
  13192. ["Reveal Context" org-reveal t]
  13193. ["Show All" show-all t]
  13194. "--"
  13195. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13196. "--"
  13197. ["New Heading" org-insert-heading t]
  13198. ("Navigate Headings"
  13199. ["Up" outline-up-heading t]
  13200. ["Next" outline-next-visible-heading t]
  13201. ["Previous" outline-previous-visible-heading t]
  13202. ["Next Same Level" outline-forward-same-level t]
  13203. ["Previous Same Level" outline-backward-same-level t]
  13204. "--"
  13205. ["Jump" org-goto t])
  13206. ("Edit Structure"
  13207. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13208. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13209. "--"
  13210. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13211. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13212. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13213. "--"
  13214. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13215. "--"
  13216. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13217. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13218. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13219. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13220. "--"
  13221. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13222. "--"
  13223. ["Convert to odd levels" org-convert-to-odd-levels t]
  13224. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13225. ("Editing"
  13226. ["Emphasis..." org-emphasize t]
  13227. ["Edit Source Example" org-edit-special t]
  13228. "--"
  13229. ["Footnote new/jump" org-footnote-action t]
  13230. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13231. ("Archive"
  13232. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13233. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13234. ; :active t :keys "C-u C-c C-x C-a"]
  13235. ["Sparse trees open ARCHIVE trees"
  13236. (setq org-sparse-tree-open-archived-trees
  13237. (not org-sparse-tree-open-archived-trees))
  13238. :style toggle :selected org-sparse-tree-open-archived-trees]
  13239. ["Cycling opens ARCHIVE trees"
  13240. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13241. :style toggle :selected org-cycle-open-archived-trees]
  13242. "--"
  13243. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13244. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13245. ; ["Check and Move Children" (org-archive-subtree '(4))
  13246. ; :active t :keys "C-u C-c C-x C-s"]
  13247. )
  13248. "--"
  13249. ("TODO Lists"
  13250. ["TODO/DONE/-" org-todo t]
  13251. ("Select keyword"
  13252. ["Next keyword" org-shiftright (org-on-heading-p)]
  13253. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13254. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13255. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13256. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13257. ["Show TODO Tree" org-show-todo-tree t]
  13258. ["Global TODO list" org-todo-list t]
  13259. "--"
  13260. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13261. :selected org-enforce-todo-dependencies :style toggle :active t]
  13262. "Settings for tree at point"
  13263. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13264. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13265. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13266. ["Do Children parallel" org-toggle-ordered-property :style radio
  13267. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13268. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13269. "--"
  13270. ["Set Priority" org-priority t]
  13271. ["Priority Up" org-shiftup t]
  13272. ["Priority Down" org-shiftdown t]
  13273. "--"
  13274. ["Get news from all feeds" org-feed-update-all t]
  13275. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13276. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13277. ("TAGS and Properties"
  13278. ["Set Tags" org-set-tags-command t]
  13279. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13280. "--"
  13281. ["Set property" org-set-property t]
  13282. ["Column view of properties" org-columns t]
  13283. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13284. ("Dates and Scheduling"
  13285. ["Timestamp" org-time-stamp t]
  13286. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13287. ("Change Date"
  13288. ["1 Day Later" org-shiftright t]
  13289. ["1 Day Earlier" org-shiftleft t]
  13290. ["1 ... Later" org-shiftup t]
  13291. ["1 ... Earlier" org-shiftdown t])
  13292. ["Compute Time Range" org-evaluate-time-range t]
  13293. ["Schedule Item" org-schedule t]
  13294. ["Deadline" org-deadline t]
  13295. "--"
  13296. ["Custom time format" org-toggle-time-stamp-overlays
  13297. :style radio :selected org-display-custom-times]
  13298. "--"
  13299. ["Goto Calendar" org-goto-calendar t]
  13300. ["Date from Calendar" org-date-from-calendar t]
  13301. "--"
  13302. ["Start/Restart Timer" org-timer-start t]
  13303. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13304. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13305. ["Insert Timer String" org-timer t]
  13306. ["Insert Timer Item" org-timer-item t])
  13307. ("Logging work"
  13308. ["Clock in" org-clock-in t]
  13309. ["Clock out" org-clock-out t]
  13310. ["Clock cancel" org-clock-cancel t]
  13311. ["Goto running clock" org-clock-goto t]
  13312. ["Display times" org-clock-display t]
  13313. ["Create clock table" org-clock-report t]
  13314. "--"
  13315. ["Record DONE time"
  13316. (progn (setq org-log-done (not org-log-done))
  13317. (message "Switching to %s will %s record a timestamp"
  13318. (car org-done-keywords)
  13319. (if org-log-done "automatically" "not")))
  13320. :style toggle :selected org-log-done])
  13321. "--"
  13322. ["Agenda Command..." org-agenda t]
  13323. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13324. ("File List for Agenda")
  13325. ("Special views current file"
  13326. ["TODO Tree" org-show-todo-tree t]
  13327. ["Check Deadlines" org-check-deadlines t]
  13328. ["Timeline" org-timeline t]
  13329. ["Tags/Property tree" org-match-sparse-tree t])
  13330. "--"
  13331. ("Hyperlinks"
  13332. ["Store Link (Global)" org-store-link t]
  13333. ["Insert Link" org-insert-link t]
  13334. ["Follow Link" org-open-at-point t]
  13335. "--"
  13336. ["Next link" org-next-link t]
  13337. ["Previous link" org-previous-link t]
  13338. "--"
  13339. ["Descriptive Links"
  13340. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13341. :style radio
  13342. :selected (member '(org-link) buffer-invisibility-spec)]
  13343. ["Literal Links"
  13344. (progn
  13345. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13346. :style radio
  13347. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13348. "--"
  13349. ["Export/Publish..." org-export t]
  13350. ("LaTeX"
  13351. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13352. :selected org-cdlatex-mode]
  13353. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13354. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13355. ["Modify math symbol" org-cdlatex-math-modify
  13356. (org-inside-LaTeX-fragment-p)]
  13357. ["Export LaTeX fragments as images"
  13358. (if (featurep 'org-exp)
  13359. (setq org-export-with-LaTeX-fragments
  13360. (not org-export-with-LaTeX-fragments))
  13361. (require 'org-exp))
  13362. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13363. org-export-with-LaTeX-fragments)])
  13364. "--"
  13365. ("Documentation"
  13366. ["Show Version" org-version t]
  13367. ["Info Documentation" org-info t])
  13368. ("Customize"
  13369. ["Browse Org Group" org-customize t]
  13370. "--"
  13371. ["Expand This Menu" org-create-customize-menu
  13372. (fboundp 'customize-menu-create)])
  13373. "--"
  13374. ("Refresh/Reload"
  13375. ["Refresh setup current buffer" org-mode-restart t]
  13376. ["Reload Org (after update)" org-reload t]
  13377. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13378. ))
  13379. (defun org-info (&optional node)
  13380. "Read documentation for Org-mode in the info system.
  13381. With optional NODE, go directly to that node."
  13382. (interactive)
  13383. (info (format "(org)%s" (or node ""))))
  13384. (defun org-install-agenda-files-menu ()
  13385. (let ((bl (buffer-list)))
  13386. (save-excursion
  13387. (while bl
  13388. (set-buffer (pop bl))
  13389. (if (org-mode-p) (setq bl nil)))
  13390. (when (org-mode-p)
  13391. (easy-menu-change
  13392. '("Org") "File List for Agenda"
  13393. (append
  13394. (list
  13395. ["Edit File List" (org-edit-agenda-file-list) t]
  13396. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13397. ["Remove Current File from List" org-remove-file t]
  13398. ["Cycle through agenda files" org-cycle-agenda-files t]
  13399. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13400. "--")
  13401. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13402. ;;;; Documentation
  13403. ;;;###autoload
  13404. (defun org-require-autoloaded-modules ()
  13405. (interactive)
  13406. (mapc 'require
  13407. '(org-agenda org-archive org-attach org-clock org-colview
  13408. org-exp org-id org-export-latex org-docbook.el
  13409. org-publish org-remember org-table org-timer)))
  13410. ;;;###autoload
  13411. (defun org-reload (&optional uncompiled)
  13412. "Reload all org lisp files.
  13413. With prefix arg UNCOMPILED, load the uncompiled versions."
  13414. (interactive "P")
  13415. (require 'find-func)
  13416. (let* ((dir (file-name-directory (find-library-name "org")))
  13417. (files (directory-files dir t "\\.el\\'"))
  13418. (remove-re (concat (if (featurep 'xemacs)
  13419. "org-colview" "org-colview-xemacs")
  13420. "\\'")))
  13421. (setq files (mapcar 'file-name-sans-extension files))
  13422. (setq files (mapcar
  13423. (lambda (x) (if (string-match remove-re x) nil x))
  13424. files))
  13425. (setq files (delq nil files))
  13426. (mapc
  13427. (lambda (f)
  13428. (if (and (not uncompiled)
  13429. (file-exists-p (concat f ".elc")))
  13430. (load (concat f ".elc") nil nil t)
  13431. (load (concat f ".el") nil nil t)))
  13432. files)))
  13433. ;;;###autoload
  13434. (defun org-customize ()
  13435. "Call the customize function with org as argument."
  13436. (interactive)
  13437. (org-load-modules-maybe)
  13438. (org-require-autoloaded-modules)
  13439. (customize-browse 'org))
  13440. (defun org-create-customize-menu ()
  13441. "Create a full customization menu for Org-mode, insert it into the menu."
  13442. (interactive)
  13443. (org-load-modules-maybe)
  13444. (org-require-autoloaded-modules)
  13445. (if (fboundp 'customize-menu-create)
  13446. (progn
  13447. (easy-menu-change
  13448. '("Org") "Customize"
  13449. `(["Browse Org group" org-customize t]
  13450. "--"
  13451. ,(customize-menu-create 'org)
  13452. ["Set" Custom-set t]
  13453. ["Save" Custom-save t]
  13454. ["Reset to Current" Custom-reset-current t]
  13455. ["Reset to Saved" Custom-reset-saved t]
  13456. ["Reset to Standard Settings" Custom-reset-standard t]))
  13457. (message "\"Org\"-menu now contains full customization menu"))
  13458. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13459. ;;;; Miscellaneous stuff
  13460. ;;; Generally useful functions
  13461. (defun org-find-text-property-in-string (prop s)
  13462. "Return the first non-nil value of property PROP in string S."
  13463. (or (get-text-property 0 prop s)
  13464. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13465. prop s)))
  13466. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13467. "Display the given MESSAGE as a warning."
  13468. (if (fboundp 'display-warning)
  13469. (display-warning 'org message
  13470. (if (featurep 'xemacs)
  13471. 'warning
  13472. :warning))
  13473. (let ((buf (get-buffer-create "*Org warnings*")))
  13474. (with-current-buffer buf
  13475. (goto-char (point-max))
  13476. (insert "Warning (Org): " message)
  13477. (unless (bolp)
  13478. (newline)))
  13479. (display-buffer buf)
  13480. (sit-for 0))))
  13481. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13482. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13483. (if (and marker (marker-buffer marker)
  13484. (buffer-live-p (marker-buffer marker)))
  13485. (progn
  13486. (switch-to-buffer (marker-buffer marker))
  13487. (if (or (> marker (point-max)) (< marker (point-min)))
  13488. (widen))
  13489. (goto-char marker)
  13490. (org-show-context 'org-goto))
  13491. (if bookmark
  13492. (bookmark-jump bookmark)
  13493. (error "Cannot find location"))))
  13494. (defun org-quote-csv-field (s)
  13495. "Quote field for inclusion in CSV material."
  13496. (if (string-match "[\",]" s)
  13497. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13498. s))
  13499. (defun org-plist-delete (plist property)
  13500. "Delete PROPERTY from PLIST.
  13501. This is in contrast to merely setting it to 0."
  13502. (let (p)
  13503. (while plist
  13504. (if (not (eq property (car plist)))
  13505. (setq p (plist-put p (car plist) (nth 1 plist))))
  13506. (setq plist (cddr plist)))
  13507. p))
  13508. (defun org-force-self-insert (N)
  13509. "Needed to enforce self-insert under remapping."
  13510. (interactive "p")
  13511. (self-insert-command N))
  13512. (defun org-string-width (s)
  13513. "Compute width of string, ignoring invisible characters.
  13514. This ignores character with invisibility property `org-link', and also
  13515. characters with property `org-cwidth', because these will become invisible
  13516. upon the next fontification round."
  13517. (let (b l)
  13518. (when (or (eq t buffer-invisibility-spec)
  13519. (assq 'org-link buffer-invisibility-spec))
  13520. (while (setq b (text-property-any 0 (length s)
  13521. 'invisible 'org-link s))
  13522. (setq s (concat (substring s 0 b)
  13523. (substring s (or (next-single-property-change
  13524. b 'invisible s) (length s)))))))
  13525. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13526. (setq s (concat (substring s 0 b)
  13527. (substring s (or (next-single-property-change
  13528. b 'org-cwidth s) (length s))))))
  13529. (setq l (string-width s) b -1)
  13530. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13531. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13532. l))
  13533. (defun org-get-indentation (&optional line)
  13534. "Get the indentation of the current line, interpreting tabs.
  13535. When LINE is given, assume it represents a line and compute its indentation."
  13536. (if line
  13537. (if (string-match "^ *" (org-remove-tabs line))
  13538. (match-end 0))
  13539. (save-excursion
  13540. (beginning-of-line 1)
  13541. (skip-chars-forward " \t")
  13542. (current-column))))
  13543. (defun org-remove-tabs (s &optional width)
  13544. "Replace tabulators in S with spaces.
  13545. Assumes that s is a single line, starting in column 0."
  13546. (setq width (or width tab-width))
  13547. (while (string-match "\t" s)
  13548. (setq s (replace-match
  13549. (make-string
  13550. (- (* width (/ (+ (match-beginning 0) width) width))
  13551. (match-beginning 0)) ?\ )
  13552. t t s)))
  13553. s)
  13554. (defun org-fix-indentation (line ind)
  13555. "Fix indentation in LINE.
  13556. IND is a cons cell with target and minimum indentation.
  13557. If the current indentation in LINE is smaller than the minimum,
  13558. leave it alone. If it is larger than ind, set it to the target."
  13559. (let* ((l (org-remove-tabs line))
  13560. (i (org-get-indentation l))
  13561. (i1 (car ind)) (i2 (cdr ind)))
  13562. (if (>= i i2) (setq l (substring line i2)))
  13563. (if (> i1 0)
  13564. (concat (make-string i1 ?\ ) l)
  13565. l)))
  13566. (defun org-base-buffer (buffer)
  13567. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13568. (if (not buffer)
  13569. buffer
  13570. (or (buffer-base-buffer buffer)
  13571. buffer)))
  13572. (defun org-trim (s)
  13573. "Remove whitespace at beginning and end of string."
  13574. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13575. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13576. s)
  13577. (defun org-wrap (string &optional width lines)
  13578. "Wrap string to either a number of lines, or a width in characters.
  13579. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13580. that costs. If there is a word longer than WIDTH, the text is actually
  13581. wrapped to the length of that word.
  13582. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13583. many lines, whatever width that takes.
  13584. The return value is a list of lines, without newlines at the end."
  13585. (let* ((words (org-split-string string "[ \t\n]+"))
  13586. (maxword (apply 'max (mapcar 'org-string-width words)))
  13587. w ll)
  13588. (cond (width
  13589. (org-do-wrap words (max maxword width)))
  13590. (lines
  13591. (setq w maxword)
  13592. (setq ll (org-do-wrap words maxword))
  13593. (if (<= (length ll) lines)
  13594. ll
  13595. (setq ll words)
  13596. (while (> (length ll) lines)
  13597. (setq w (1+ w))
  13598. (setq ll (org-do-wrap words w)))
  13599. ll))
  13600. (t (error "Cannot wrap this")))))
  13601. (defun org-do-wrap (words width)
  13602. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13603. (let (lines line)
  13604. (while words
  13605. (setq line (pop words))
  13606. (while (and words (< (+ (length line) (length (car words))) width))
  13607. (setq line (concat line " " (pop words))))
  13608. (setq lines (push line lines)))
  13609. (nreverse lines)))
  13610. (defun org-split-string (string &optional separators)
  13611. "Splits STRING into substrings at SEPARATORS.
  13612. No empty strings are returned if there are matches at the beginning
  13613. and end of string."
  13614. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13615. (start 0)
  13616. notfirst
  13617. (list nil))
  13618. (while (and (string-match rexp string
  13619. (if (and notfirst
  13620. (= start (match-beginning 0))
  13621. (< start (length string)))
  13622. (1+ start) start))
  13623. (< (match-beginning 0) (length string)))
  13624. (setq notfirst t)
  13625. (or (eq (match-beginning 0) 0)
  13626. (and (eq (match-beginning 0) (match-end 0))
  13627. (eq (match-beginning 0) start))
  13628. (setq list
  13629. (cons (substring string start (match-beginning 0))
  13630. list)))
  13631. (setq start (match-end 0)))
  13632. (or (eq start (length string))
  13633. (setq list
  13634. (cons (substring string start)
  13635. list)))
  13636. (nreverse list)))
  13637. (defun org-uuidgen-p (s)
  13638. "Is S an ID created by UUIDGEN?"
  13639. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13640. (defun org-context ()
  13641. "Return a list of contexts of the current cursor position.
  13642. If several contexts apply, all are returned.
  13643. Each context entry is a list with a symbol naming the context, and
  13644. two positions indicating start and end of the context. Possible
  13645. contexts are:
  13646. :headline anywhere in a headline
  13647. :headline-stars on the leading stars in a headline
  13648. :todo-keyword on a TODO keyword (including DONE) in a headline
  13649. :tags on the TAGS in a headline
  13650. :priority on the priority cookie in a headline
  13651. :item on the first line of a plain list item
  13652. :item-bullet on the bullet/number of a plain list item
  13653. :checkbox on the checkbox in a plain list item
  13654. :table in an org-mode table
  13655. :table-special on a special filed in a table
  13656. :table-table in a table.el table
  13657. :link on a hyperlink
  13658. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13659. :target on a <<target>>
  13660. :radio-target on a <<<radio-target>>>
  13661. :latex-fragment on a LaTeX fragment
  13662. :latex-preview on a LaTeX fragment with overlayed preview image
  13663. This function expects the position to be visible because it uses font-lock
  13664. faces as a help to recognize the following contexts: :table-special, :link,
  13665. and :keyword."
  13666. (let* ((f (get-text-property (point) 'face))
  13667. (faces (if (listp f) f (list f)))
  13668. (p (point)) clist o)
  13669. ;; First the large context
  13670. (cond
  13671. ((org-on-heading-p t)
  13672. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13673. (when (progn
  13674. (beginning-of-line 1)
  13675. (looking-at org-todo-line-tags-regexp))
  13676. (push (org-point-in-group p 1 :headline-stars) clist)
  13677. (push (org-point-in-group p 2 :todo-keyword) clist)
  13678. (push (org-point-in-group p 4 :tags) clist))
  13679. (goto-char p)
  13680. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13681. (if (looking-at "\\[#[A-Z0-9]\\]")
  13682. (push (org-point-in-group p 0 :priority) clist)))
  13683. ((org-at-item-p)
  13684. (push (org-point-in-group p 2 :item-bullet) clist)
  13685. (push (list :item (point-at-bol)
  13686. (save-excursion (org-end-of-item) (point)))
  13687. clist)
  13688. (and (org-at-item-checkbox-p)
  13689. (push (org-point-in-group p 0 :checkbox) clist)))
  13690. ((org-at-table-p)
  13691. (push (list :table (org-table-begin) (org-table-end)) clist)
  13692. (if (memq 'org-formula faces)
  13693. (push (list :table-special
  13694. (previous-single-property-change p 'face)
  13695. (next-single-property-change p 'face)) clist)))
  13696. ((org-at-table-p 'any)
  13697. (push (list :table-table) clist)))
  13698. (goto-char p)
  13699. ;; Now the small context
  13700. (cond
  13701. ((org-at-timestamp-p)
  13702. (push (org-point-in-group p 0 :timestamp) clist))
  13703. ((memq 'org-link faces)
  13704. (push (list :link
  13705. (previous-single-property-change p 'face)
  13706. (next-single-property-change p 'face)) clist))
  13707. ((memq 'org-special-keyword faces)
  13708. (push (list :keyword
  13709. (previous-single-property-change p 'face)
  13710. (next-single-property-change p 'face)) clist))
  13711. ((org-on-target-p)
  13712. (push (org-point-in-group p 0 :target) clist)
  13713. (goto-char (1- (match-beginning 0)))
  13714. (if (looking-at org-radio-target-regexp)
  13715. (push (org-point-in-group p 0 :radio-target) clist))
  13716. (goto-char p))
  13717. ((setq o (car (delq nil
  13718. (mapcar
  13719. (lambda (x)
  13720. (if (memq x org-latex-fragment-image-overlays) x))
  13721. (org-overlays-at (point))))))
  13722. (push (list :latex-fragment
  13723. (org-overlay-start o) (org-overlay-end o)) clist)
  13724. (push (list :latex-preview
  13725. (org-overlay-start o) (org-overlay-end o)) clist))
  13726. ((org-inside-LaTeX-fragment-p)
  13727. ;; FIXME: positions wrong.
  13728. (push (list :latex-fragment (point) (point)) clist)))
  13729. (setq clist (nreverse (delq nil clist)))
  13730. clist))
  13731. ;; FIXME: Compare with at-regexp-p Do we need both?
  13732. (defun org-in-regexp (re &optional nlines visually)
  13733. "Check if point is inside a match of regexp.
  13734. Normally only the current line is checked, but you can include NLINES extra
  13735. lines both before and after point into the search.
  13736. If VISUALLY is set, require that the cursor is not after the match but
  13737. really on, so that the block visually is on the match."
  13738. (catch 'exit
  13739. (let ((pos (point))
  13740. (eol (point-at-eol (+ 1 (or nlines 0))))
  13741. (inc (if visually 1 0)))
  13742. (save-excursion
  13743. (beginning-of-line (- 1 (or nlines 0)))
  13744. (while (re-search-forward re eol t)
  13745. (if (and (<= (match-beginning 0) pos)
  13746. (>= (+ inc (match-end 0)) pos))
  13747. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13748. (defun org-at-regexp-p (regexp)
  13749. "Is point inside a match of REGEXP in the current line?"
  13750. (catch 'exit
  13751. (save-excursion
  13752. (let ((pos (point)) (end (point-at-eol)))
  13753. (beginning-of-line 1)
  13754. (while (re-search-forward regexp end t)
  13755. (if (and (<= (match-beginning 0) pos)
  13756. (>= (match-end 0) pos))
  13757. (throw 'exit t)))
  13758. nil))))
  13759. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13760. "Call `multi-occur' with buffers for all agenda files."
  13761. (interactive "sOrg-files matching: \np")
  13762. (let* ((files (org-agenda-files))
  13763. (tnames (mapcar 'file-truename files))
  13764. (extra org-agenda-text-search-extra-files)
  13765. f)
  13766. (when (eq (car extra) 'agenda-archives)
  13767. (setq extra (cdr extra))
  13768. (setq files (org-add-archive-files files)))
  13769. (while (setq f (pop extra))
  13770. (unless (member (file-truename f) tnames)
  13771. (add-to-list 'files f 'append)
  13772. (add-to-list 'tnames (file-truename f) 'append)))
  13773. (multi-occur
  13774. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13775. regexp)))
  13776. (if (boundp 'occur-mode-find-occurrence-hook)
  13777. ;; Emacs 23
  13778. (add-hook 'occur-mode-find-occurrence-hook
  13779. (lambda ()
  13780. (when (org-mode-p)
  13781. (org-reveal))))
  13782. ;; Emacs 22
  13783. (defadvice occur-mode-goto-occurrence
  13784. (after org-occur-reveal activate)
  13785. (and (org-mode-p) (org-reveal)))
  13786. (defadvice occur-mode-goto-occurrence-other-window
  13787. (after org-occur-reveal activate)
  13788. (and (org-mode-p) (org-reveal)))
  13789. (defadvice occur-mode-display-occurrence
  13790. (after org-occur-reveal activate)
  13791. (when (org-mode-p)
  13792. (let ((pos (occur-mode-find-occurrence)))
  13793. (with-current-buffer (marker-buffer pos)
  13794. (save-excursion
  13795. (goto-char pos)
  13796. (org-reveal)))))))
  13797. (defun org-uniquify (list)
  13798. "Remove duplicate elements from LIST."
  13799. (let (res)
  13800. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13801. res))
  13802. (defun org-delete-all (elts list)
  13803. "Remove all elements in ELTS from LIST."
  13804. (while elts
  13805. (setq list (delete (pop elts) list)))
  13806. list)
  13807. (defun org-back-over-empty-lines ()
  13808. "Move backwards over whitespace, to the beginning of the first empty line.
  13809. Returns the number of empty lines passed."
  13810. (let ((pos (point)))
  13811. (skip-chars-backward " \t\n\r")
  13812. (beginning-of-line 2)
  13813. (goto-char (min (point) pos))
  13814. (count-lines (point) pos)))
  13815. (defun org-skip-whitespace ()
  13816. (skip-chars-forward " \t\n\r"))
  13817. (defun org-point-in-group (point group &optional context)
  13818. "Check if POINT is in match-group GROUP.
  13819. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13820. match. If the match group does ot exist or point is not inside it,
  13821. return nil."
  13822. (and (match-beginning group)
  13823. (>= point (match-beginning group))
  13824. (<= point (match-end group))
  13825. (if context
  13826. (list context (match-beginning group) (match-end group))
  13827. t)))
  13828. (defun org-switch-to-buffer-other-window (&rest args)
  13829. "Switch to buffer in a second window on the current frame.
  13830. In particular, do not allow pop-up frames."
  13831. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13832. special-display-function)
  13833. (apply 'switch-to-buffer-other-window args)))
  13834. (defun org-combine-plists (&rest plists)
  13835. "Create a single property list from all plists in PLISTS.
  13836. The process starts by copying the first list, and then setting properties
  13837. from the other lists. Settings in the last list are the most significant
  13838. ones and overrule settings in the other lists."
  13839. (let ((rtn (copy-sequence (pop plists)))
  13840. p v ls)
  13841. (while plists
  13842. (setq ls (pop plists))
  13843. (while ls
  13844. (setq p (pop ls) v (pop ls))
  13845. (setq rtn (plist-put rtn p v))))
  13846. rtn))
  13847. (defun org-move-line-down (arg)
  13848. "Move the current line down. With prefix argument, move it past ARG lines."
  13849. (interactive "p")
  13850. (let ((col (current-column))
  13851. beg end pos)
  13852. (beginning-of-line 1) (setq beg (point))
  13853. (beginning-of-line 2) (setq end (point))
  13854. (beginning-of-line (+ 1 arg))
  13855. (setq pos (move-marker (make-marker) (point)))
  13856. (insert (delete-and-extract-region beg end))
  13857. (goto-char pos)
  13858. (org-move-to-column col)))
  13859. (defun org-move-line-up (arg)
  13860. "Move the current line up. With prefix argument, move it past ARG lines."
  13861. (interactive "p")
  13862. (let ((col (current-column))
  13863. beg end pos)
  13864. (beginning-of-line 1) (setq beg (point))
  13865. (beginning-of-line 2) (setq end (point))
  13866. (beginning-of-line (- arg))
  13867. (setq pos (move-marker (make-marker) (point)))
  13868. (insert (delete-and-extract-region beg end))
  13869. (goto-char pos)
  13870. (org-move-to-column col)))
  13871. (defun org-replace-escapes (string table)
  13872. "Replace %-escapes in STRING with values in TABLE.
  13873. TABLE is an association list with keys like \"%a\" and string values.
  13874. The sequences in STRING may contain normal field width and padding information,
  13875. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13876. so values can contain further %-escapes if they are define later in TABLE."
  13877. (let ((case-fold-search nil)
  13878. e re rpl)
  13879. (while (setq e (pop table))
  13880. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13881. (while (string-match re string)
  13882. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13883. (cdr e)))
  13884. (setq string (replace-match rpl t t string))))
  13885. string))
  13886. (defun org-sublist (list start end)
  13887. "Return a section of LIST, from START to END.
  13888. Counting starts at 1."
  13889. (let (rtn (c start))
  13890. (setq list (nthcdr (1- start) list))
  13891. (while (and list (<= c end))
  13892. (push (pop list) rtn)
  13893. (setq c (1+ c)))
  13894. (nreverse rtn)))
  13895. (defun org-find-base-buffer-visiting (file)
  13896. "Like `find-buffer-visiting' but alway return the base buffer and
  13897. not an indirect buffer."
  13898. (let ((buf (find-buffer-visiting file)))
  13899. (if buf
  13900. (or (buffer-base-buffer buf) buf)
  13901. nil)))
  13902. (defun org-image-file-name-regexp (&optional extensions)
  13903. "Return regexp matching the file names of images.
  13904. If EXTENSIONS is given, only match these."
  13905. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13906. (image-file-name-regexp)
  13907. (let ((image-file-name-extensions
  13908. (or extensions
  13909. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13910. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13911. (concat "\\."
  13912. (regexp-opt (nconc (mapcar 'upcase
  13913. image-file-name-extensions)
  13914. image-file-name-extensions)
  13915. t)
  13916. "\\'"))))
  13917. (defun org-file-image-p (file &optional extensions)
  13918. "Return non-nil if FILE is an image."
  13919. (save-match-data
  13920. (string-match (org-image-file-name-regexp extensions) file)))
  13921. (defun org-get-cursor-date ()
  13922. "Return the date at cursor in as a time.
  13923. This works in the calendar and in the agenda, anywhere else it just
  13924. returns the current time."
  13925. (let (date day defd)
  13926. (cond
  13927. ((eq major-mode 'calendar-mode)
  13928. (setq date (calendar-cursor-to-date)
  13929. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13930. ((eq major-mode 'org-agenda-mode)
  13931. (setq day (get-text-property (point) 'day))
  13932. (if day
  13933. (setq date (calendar-gregorian-from-absolute day)
  13934. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13935. (nth 2 date))))))
  13936. (or defd (current-time))))
  13937. (defvar org-agenda-action-marker (make-marker)
  13938. "Marker pointing to the entry for the next agenda action.")
  13939. (defun org-mark-entry-for-agenda-action ()
  13940. "Mark the current entry as target of an agenda action.
  13941. Agenda actions are actions executed from the agenda with the key `k',
  13942. which make use of the date at the cursor."
  13943. (interactive)
  13944. (move-marker org-agenda-action-marker
  13945. (save-excursion (org-back-to-heading t) (point))
  13946. (current-buffer))
  13947. (message
  13948. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13949. ;;; Paragraph filling stuff.
  13950. ;; We want this to be just right, so use the full arsenal.
  13951. (defun org-indent-line-function ()
  13952. "Indent line like previous, but further if previous was headline or item."
  13953. (interactive)
  13954. (let* ((pos (point))
  13955. (itemp (org-at-item-p))
  13956. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13957. column bpos bcol tpos tcol bullet btype bullet-type)
  13958. ;; Find the previous relevant line
  13959. (beginning-of-line 1)
  13960. (cond
  13961. ((looking-at "#") (setq column 0))
  13962. ((looking-at "\\*+ ") (setq column 0))
  13963. ((and (looking-at "[ \t]*:END:")
  13964. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13965. (save-excursion
  13966. (goto-char (1- (match-beginning 1)))
  13967. (setq column (current-column))))
  13968. (t
  13969. (beginning-of-line 0)
  13970. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13971. (not (looking-at "[ \t]*:END:"))
  13972. (not (looking-at org-drawer-regexp)))
  13973. (beginning-of-line 0))
  13974. (cond
  13975. ((looking-at "\\*+[ \t]+")
  13976. (if (not org-adapt-indentation)
  13977. (setq column 0)
  13978. (goto-char (match-end 0))
  13979. (setq column (current-column))))
  13980. ((looking-at org-drawer-regexp)
  13981. (goto-char (1- (match-beginning 1)))
  13982. (setq column (current-column)))
  13983. ((looking-at "\\([ \t]*\\):END:")
  13984. (goto-char (match-end 1))
  13985. (setq column (current-column)))
  13986. ((org-in-item-p)
  13987. (org-beginning-of-item)
  13988. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13989. (setq bpos (match-beginning 1) tpos (match-end 0)
  13990. bcol (progn (goto-char bpos) (current-column))
  13991. tcol (progn (goto-char tpos) (current-column))
  13992. bullet (match-string 1)
  13993. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13994. (if (> tcol (+ bcol org-description-max-indent))
  13995. (setq tcol (+ bcol 5)))
  13996. (if (not itemp)
  13997. (setq column tcol)
  13998. (goto-char pos)
  13999. (beginning-of-line 1)
  14000. (if (looking-at "\\S-")
  14001. (progn
  14002. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14003. (setq bullet (match-string 1)
  14004. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14005. (setq column (if (equal btype bullet-type) bcol tcol)))
  14006. (setq column (org-get-indentation)))))
  14007. (t (setq column (org-get-indentation))))))
  14008. (goto-char pos)
  14009. (if (<= (current-column) (current-indentation))
  14010. (org-indent-line-to column)
  14011. (save-excursion (org-indent-line-to column)))
  14012. (setq column (current-column))
  14013. (beginning-of-line 1)
  14014. (if (looking-at
  14015. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14016. (replace-match (concat "\\1" (format org-property-format
  14017. (match-string 2) (match-string 3)))
  14018. t nil))
  14019. (org-move-to-column column)))
  14020. (defun org-set-autofill-regexps ()
  14021. (interactive)
  14022. ;; In the paragraph separator we include headlines, because filling
  14023. ;; text in a line directly attached to a headline would otherwise
  14024. ;; fill the headline as well.
  14025. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14026. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14027. ;; The paragraph starter includes hand-formatted lists.
  14028. (org-set-local 'paragraph-start
  14029. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14030. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14031. ;; But only if the user has not turned off tables or fixed-width regions
  14032. (org-set-local
  14033. 'auto-fill-inhibit-regexp
  14034. (concat "\\*+ \\|#\\+"
  14035. "\\|[ \t]*" org-keyword-time-regexp
  14036. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14037. (concat
  14038. "\\|[ \t]*["
  14039. (if org-enable-table-editor "|" "")
  14040. (if org-enable-fixed-width-editor ":" "")
  14041. "]"))))
  14042. ;; We use our own fill-paragraph function, to make sure that tables
  14043. ;; and fixed-width regions are not wrapped. That function will pass
  14044. ;; through to `fill-paragraph' when appropriate.
  14045. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14046. ; Adaptive filling: To get full control, first make sure that
  14047. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14048. (org-set-local 'adaptive-fill-regexp "\000")
  14049. (org-set-local 'adaptive-fill-function
  14050. 'org-adaptive-fill-function)
  14051. (org-set-local
  14052. 'align-mode-rules-list
  14053. '((org-in-buffer-settings
  14054. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14055. (modes . '(org-mode))))))
  14056. (defun org-fill-paragraph (&optional justify)
  14057. "Re-align a table, pass through to fill-paragraph if no table."
  14058. (let ((table-p (org-at-table-p))
  14059. (table.el-p (org-at-table.el-p)))
  14060. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14061. (save-excursion (goto-char (point-at-bol))
  14062. (looking-at outline-regexp)))
  14063. t) ; skip headlines
  14064. (table.el-p t) ; skip table.el tables
  14065. (table-p (org-table-align) t) ; align org-mode tables
  14066. (t nil)))) ; call paragraph-fill
  14067. ;; For reference, this is the default value of adaptive-fill-regexp
  14068. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14069. (defun org-adaptive-fill-function ()
  14070. "Return a fill prefix for org-mode files.
  14071. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14072. work correctly."
  14073. (cond ((looking-at "#[ \t]+")
  14074. (match-string 0))
  14075. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14076. (save-excursion
  14077. (if (> (match-end 1) (+ (match-beginning 1)
  14078. org-description-max-indent))
  14079. (goto-char (+ (match-beginning 1) 5))
  14080. (goto-char (match-end 0)))
  14081. (make-string (current-column) ?\ )))
  14082. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14083. (save-excursion
  14084. (goto-char (match-end 0))
  14085. (make-string (current-column) ?\ )))
  14086. (t nil)))
  14087. ;;; Other stuff.
  14088. (defun org-toggle-fixed-width-section (arg)
  14089. "Toggle the fixed-width export.
  14090. If there is no active region, the QUOTE keyword at the current headline is
  14091. inserted or removed. When present, it causes the text between this headline
  14092. and the next to be exported as fixed-width text, and unmodified.
  14093. If there is an active region, this command adds or removes a colon as the
  14094. first character of this line. If the first character of a line is a colon,
  14095. this line is also exported in fixed-width font."
  14096. (interactive "P")
  14097. (let* ((cc 0)
  14098. (regionp (org-region-active-p))
  14099. (beg (if regionp (region-beginning) (point)))
  14100. (end (if regionp (region-end)))
  14101. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14102. (case-fold-search nil)
  14103. (re "[ \t]*\\(: \\)")
  14104. off)
  14105. (if regionp
  14106. (save-excursion
  14107. (goto-char beg)
  14108. (setq cc (current-column))
  14109. (beginning-of-line 1)
  14110. (setq off (looking-at re))
  14111. (while (> nlines 0)
  14112. (setq nlines (1- nlines))
  14113. (beginning-of-line 1)
  14114. (cond
  14115. (arg
  14116. (org-move-to-column cc t)
  14117. (insert ": \n")
  14118. (forward-line -1))
  14119. ((and off (looking-at re))
  14120. (replace-match "" t t nil 1))
  14121. ((not off) (org-move-to-column cc t) (insert ": ")))
  14122. (forward-line 1)))
  14123. (save-excursion
  14124. (org-back-to-heading)
  14125. (if (looking-at (concat outline-regexp
  14126. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14127. (replace-match "" t t nil 1)
  14128. (if (looking-at outline-regexp)
  14129. (progn
  14130. (goto-char (match-end 0))
  14131. (insert org-quote-string " "))))))))
  14132. (defun org-reftex-citation ()
  14133. "Use reftex-citation to insert a citation into the buffer.
  14134. This looks for a line like
  14135. #+BIBLIOGRAPHY: foo plain option:-d
  14136. and derives from it that foo.bib is the bbliography file relevant
  14137. for this document. It then installs the necessary environment for RefTeX
  14138. to work in this buffer and calls `reftex-citation' to insert a citation
  14139. into the buffer.
  14140. Export of such citations to both LaTeX and HTML is handled by the contributed
  14141. package org-exp-bibtex by Taru Karttunen."
  14142. (interactive)
  14143. (let ((reftex-docstruct-symbol 'rds)
  14144. (reftex-cite-format "\\cite{%l}")
  14145. rds bib)
  14146. (save-excursion
  14147. (save-restriction
  14148. (widen)
  14149. (let ((case-fold-search t)
  14150. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14151. (if (not (save-excursion
  14152. (or (re-search-forward re nil t)
  14153. (re-search-backward re nil t))))
  14154. (error "No bibliography defined in file")
  14155. (setq bib (concat (match-string 1) ".bib")
  14156. rds (list (list 'bib bib)))))))
  14157. (call-interactively 'reftex-citation)))
  14158. ;;;; Functions extending outline functionality
  14159. (defun org-beginning-of-line (&optional arg)
  14160. "Go to the beginning of the current line. If that is invisible, continue
  14161. to a visible line beginning. This makes the function of C-a more intuitive.
  14162. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14163. first attempt, and only move to after the tags when the cursor is already
  14164. beyond the end of the headline."
  14165. (interactive "P")
  14166. (let ((pos (point))
  14167. (special (if (consp org-special-ctrl-a/e)
  14168. (car org-special-ctrl-a/e)
  14169. org-special-ctrl-a/e))
  14170. refpos)
  14171. (beginning-of-line 1)
  14172. (if (and arg (fboundp 'move-beginning-of-line))
  14173. (call-interactively 'move-beginning-of-line)
  14174. (if (bobp)
  14175. nil
  14176. (backward-char 1)
  14177. (if (org-invisible-p)
  14178. (while (and (not (bobp)) (org-invisible-p))
  14179. (backward-char 1)
  14180. (beginning-of-line 1))
  14181. (forward-char 1))))
  14182. (when special
  14183. (cond
  14184. ((and (looking-at org-complex-heading-regexp)
  14185. (= (char-after (match-end 1)) ?\ ))
  14186. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14187. (point-at-eol)))
  14188. (goto-char
  14189. (if (eq special t)
  14190. (cond ((> pos refpos) refpos)
  14191. ((= pos (point)) refpos)
  14192. (t (point)))
  14193. (cond ((> pos (point)) (point))
  14194. ((not (eq last-command this-command)) (point))
  14195. (t refpos)))))
  14196. ((org-at-item-p)
  14197. (goto-char
  14198. (if (eq special t)
  14199. (cond ((> pos (match-end 4)) (match-end 4))
  14200. ((= pos (point)) (match-end 4))
  14201. (t (point)))
  14202. (cond ((> pos (point)) (point))
  14203. ((not (eq last-command this-command)) (point))
  14204. (t (match-end 4))))))))
  14205. (org-no-warnings
  14206. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14207. (defun org-end-of-line (&optional arg)
  14208. "Go to the end of the line.
  14209. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14210. first attempt, and only move to after the tags when the cursor is already
  14211. beyond the end of the headline."
  14212. (interactive "P")
  14213. (let ((special (if (consp org-special-ctrl-a/e)
  14214. (cdr org-special-ctrl-a/e)
  14215. org-special-ctrl-a/e)))
  14216. (if (or (not special)
  14217. (not (org-on-heading-p))
  14218. arg)
  14219. (call-interactively (if (fboundp 'move-end-of-line)
  14220. 'move-end-of-line
  14221. 'end-of-line))
  14222. (let ((pos (point)))
  14223. (beginning-of-line 1)
  14224. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14225. (if (eq special t)
  14226. (if (or (< pos (match-beginning 1))
  14227. (= pos (match-end 0)))
  14228. (goto-char (match-beginning 1))
  14229. (goto-char (match-end 0)))
  14230. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14231. (goto-char (match-end 0))
  14232. (goto-char (match-beginning 1))))
  14233. (call-interactively (if (fboundp 'move-end-of-line)
  14234. 'move-end-of-line
  14235. 'end-of-line)))))
  14236. (org-no-warnings
  14237. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14238. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14239. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14240. (defun org-backward-sentence (&optional arg)
  14241. "Go to beginning of sentence, or beginning of table field.
  14242. This will call `backward-sentence' or `org-table-beginning-of-field',
  14243. depending on context."
  14244. (interactive "P")
  14245. (cond
  14246. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14247. (t (call-interactively 'backward-sentence))))
  14248. (defun org-forward-sentence (&optional arg)
  14249. "Go to end of sentence, or end of table field.
  14250. This will call `forward-sentence' or `org-table-end-of-field',
  14251. depending on context."
  14252. (interactive "P")
  14253. (cond
  14254. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14255. (t (call-interactively 'forward-sentence))))
  14256. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14257. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14258. (defun org-kill-line (&optional arg)
  14259. "Kill line, to tags or end of line."
  14260. (interactive "P")
  14261. (cond
  14262. ((or (not org-special-ctrl-k)
  14263. (bolp)
  14264. (not (org-on-heading-p)))
  14265. (call-interactively 'kill-line))
  14266. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14267. (kill-region (point) (match-beginning 1))
  14268. (org-set-tags nil t))
  14269. (t (kill-region (point) (point-at-eol)))))
  14270. (define-key org-mode-map "\C-k" 'org-kill-line)
  14271. (defun org-yank (&optional arg)
  14272. "Yank. If the kill is a subtree, treat it specially.
  14273. This command will look at the current kill and check if is a single
  14274. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14275. cursor is at the beginning of a line or after the stars of a currently
  14276. empty headline, then the yank is handled specially. How exactly depends
  14277. on the value of the following variables, both set by default.
  14278. org-yank-folded-subtrees
  14279. When set, the subtree(s) will be folded after insertion, but only
  14280. if doing so would now swallow text after the yanked text.
  14281. org-yank-adjusted-subtrees
  14282. When set, the subtree will be promoted or demoted in order to
  14283. fit into the local outline tree structure, which means that the level
  14284. will be adjusted so that it becomes the smaller one of the two
  14285. *visible* surrounding headings.
  14286. Any prefix to this command will cause `yank' to be called directly with
  14287. no special treatment. In particular, a simple `C-u' prefix will just
  14288. plainly yank the text as it is.
  14289. \[1] Basically, the test checks if the first non-white line is a heading
  14290. and if there are no other headings with fewer stars."
  14291. (interactive "P")
  14292. (setq this-command 'yank)
  14293. (if arg
  14294. (call-interactively 'yank)
  14295. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14296. (and (org-kill-is-subtree-p)
  14297. (or (bolp)
  14298. (and (looking-at "[ \t]*$")
  14299. (string-match
  14300. "\\`\\*+\\'"
  14301. (buffer-substring (point-at-bol) (point)))))))
  14302. swallowp)
  14303. (cond
  14304. ((and subtreep org-yank-folded-subtrees)
  14305. (let ((beg (point))
  14306. end)
  14307. (if (and subtreep org-yank-adjusted-subtrees)
  14308. (org-paste-subtree nil nil 'for-yank)
  14309. (call-interactively 'yank))
  14310. (setq end (point))
  14311. (goto-char beg)
  14312. (when (and (bolp) subtreep
  14313. (not (setq swallowp
  14314. (org-yank-folding-would-swallow-text beg end))))
  14315. (or (looking-at outline-regexp)
  14316. (re-search-forward (concat "^" outline-regexp) end t))
  14317. (while (and (< (point) end) (looking-at outline-regexp))
  14318. (hide-subtree)
  14319. (org-cycle-show-empty-lines 'folded)
  14320. (condition-case nil
  14321. (outline-forward-same-level 1)
  14322. (error (goto-char end)))))
  14323. (when swallowp
  14324. (message
  14325. "Yanked text not folded because that would swallow text"))
  14326. (goto-char end)
  14327. (skip-chars-forward " \t\n\r")
  14328. (beginning-of-line 1)
  14329. (push-mark beg 'nomsg)))
  14330. ((and subtreep org-yank-adjusted-subtrees)
  14331. (let ((beg (point-at-bol)))
  14332. (org-paste-subtree nil nil 'for-yank)
  14333. (push-mark beg 'nomsg)))
  14334. (t
  14335. (call-interactively 'yank))))))
  14336. (defun org-yank-folding-would-swallow-text (beg end)
  14337. "Would hide-subtree at BEG swallow any text after END?"
  14338. (let (level)
  14339. (save-excursion
  14340. (goto-char beg)
  14341. (when (or (looking-at outline-regexp)
  14342. (re-search-forward (concat "^" outline-regexp) end t))
  14343. (setq level (org-outline-level)))
  14344. (goto-char end)
  14345. (skip-chars-forward " \t\r\n\v\f")
  14346. (if (or (eobp)
  14347. (and (bolp) (looking-at org-outline-regexp)
  14348. (<= (org-outline-level) level)))
  14349. nil ; Nothing would be swallowed
  14350. t)))) ; something would swallow
  14351. (define-key org-mode-map "\C-y" 'org-yank)
  14352. (defun org-invisible-p ()
  14353. "Check if point is at a character currently not visible."
  14354. ;; Early versions of noutline don't have `outline-invisible-p'.
  14355. (if (fboundp 'outline-invisible-p)
  14356. (outline-invisible-p)
  14357. (get-char-property (point) 'invisible)))
  14358. (defun org-invisible-p2 ()
  14359. "Check if point is at a character currently not visible."
  14360. (save-excursion
  14361. (if (and (eolp) (not (bobp))) (backward-char 1))
  14362. ;; Early versions of noutline don't have `outline-invisible-p'.
  14363. (if (fboundp 'outline-invisible-p)
  14364. (outline-invisible-p)
  14365. (get-char-property (point) 'invisible))))
  14366. (defun org-back-to-heading (&optional invisible-ok)
  14367. "Call `outline-back-to-heading', but provide a better error message."
  14368. (condition-case nil
  14369. (outline-back-to-heading invisible-ok)
  14370. (error (error "Before first headline at position %d in buffer %s"
  14371. (point) (current-buffer)))))
  14372. (defun org-before-first-heading-p ()
  14373. "Before first heading?"
  14374. (save-excursion
  14375. (null (re-search-backward "^\\*+ " nil t))))
  14376. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14377. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14378. (defun org-at-heading-or-item-p ()
  14379. (or (org-on-heading-p) (org-at-item-p)))
  14380. (defun org-on-target-p ()
  14381. (or (org-in-regexp org-radio-target-regexp)
  14382. (org-in-regexp org-target-regexp)))
  14383. (defun org-up-heading-all (arg)
  14384. "Move to the heading line of which the present line is a subheading.
  14385. This function considers both visible and invisible heading lines.
  14386. With argument, move up ARG levels."
  14387. (if (fboundp 'outline-up-heading-all)
  14388. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14389. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14390. (defun org-up-heading-safe ()
  14391. "Move to the heading line of which the present line is a subheading.
  14392. This version will not throw an error. It will return the level of the
  14393. headline found, or nil if no higher level is found."
  14394. (let (start-level re)
  14395. (org-back-to-heading t)
  14396. (setq start-level (funcall outline-level))
  14397. (if (equal start-level 1)
  14398. nil
  14399. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14400. (if (re-search-backward re nil t)
  14401. (funcall outline-level)))))
  14402. (defun org-first-sibling-p ()
  14403. "Is this heading the first child of its parents?"
  14404. (interactive)
  14405. (let ((re (concat "^" outline-regexp))
  14406. level l)
  14407. (unless (org-at-heading-p t)
  14408. (error "Not at a heading"))
  14409. (setq level (funcall outline-level))
  14410. (save-excursion
  14411. (if (not (re-search-backward re nil t))
  14412. t
  14413. (setq l (funcall outline-level))
  14414. (< l level)))))
  14415. (defun org-goto-sibling (&optional previous)
  14416. "Goto the next sibling, even if it is invisible.
  14417. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14418. when a sibling was found. When none is found, return nil and don't
  14419. move point."
  14420. (let ((fun (if previous 're-search-backward 're-search-forward))
  14421. (pos (point))
  14422. (re (concat "^" outline-regexp))
  14423. level l)
  14424. (when (condition-case nil (org-back-to-heading t) (error nil))
  14425. (setq level (funcall outline-level))
  14426. (catch 'exit
  14427. (or previous (forward-char 1))
  14428. (while (funcall fun re nil t)
  14429. (setq l (funcall outline-level))
  14430. (when (< l level) (goto-char pos) (throw 'exit nil))
  14431. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14432. (goto-char pos)
  14433. nil))))
  14434. (defun org-show-siblings ()
  14435. "Show all siblings of the current headline."
  14436. (save-excursion
  14437. (while (org-goto-sibling) (org-flag-heading nil)))
  14438. (save-excursion
  14439. (while (org-goto-sibling 'previous)
  14440. (org-flag-heading nil))))
  14441. (defun org-show-hidden-entry ()
  14442. "Show an entry where even the heading is hidden."
  14443. (save-excursion
  14444. (org-show-entry)))
  14445. (defun org-flag-heading (flag &optional entry)
  14446. "Flag the current heading. FLAG non-nil means make invisible.
  14447. When ENTRY is non-nil, show the entire entry."
  14448. (save-excursion
  14449. (org-back-to-heading t)
  14450. ;; Check if we should show the entire entry
  14451. (if entry
  14452. (progn
  14453. (org-show-entry)
  14454. (save-excursion
  14455. (and (outline-next-heading)
  14456. (org-flag-heading nil))))
  14457. (outline-flag-region (max (point-min) (1- (point)))
  14458. (save-excursion (outline-end-of-heading) (point))
  14459. flag))))
  14460. (defun org-forward-same-level (arg)
  14461. "Move forward to the ARG'th subheading at same level as this one.
  14462. Stop at the first and last subheadings of a superior heading.
  14463. This is like outline-forward-same-level, but invisible headings are ok."
  14464. (interactive "p")
  14465. (org-back-to-heading t)
  14466. (while (> arg 0)
  14467. (let ((point-to-move-to (save-excursion
  14468. (org-get-next-sibling))))
  14469. (if point-to-move-to
  14470. (progn
  14471. (goto-char point-to-move-to)
  14472. (setq arg (1- arg)))
  14473. (progn
  14474. (setq arg 0)
  14475. (error "No following same-level heading"))))))
  14476. (defun org-get-next-sibling ()
  14477. "Move to next heading of the same level, and return point.
  14478. If there is no such heading, return nil.
  14479. This is like outline-next-sibling, but invisible headings are ok."
  14480. (let ((level (funcall outline-level)))
  14481. (outline-next-heading)
  14482. (while (and (not (eobp)) (> (funcall outline-level) level))
  14483. (outline-next-heading))
  14484. (if (or (eobp) (< (funcall outline-level) level))
  14485. nil
  14486. (point))))
  14487. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14488. ;; This is an exact copy of the original function, but it uses
  14489. ;; `org-back-to-heading', to make it work also in invisible
  14490. ;; trees. And is uses an invisible-OK argument.
  14491. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14492. (org-back-to-heading invisible-OK)
  14493. (let ((first t)
  14494. (level (funcall outline-level)))
  14495. (while (and (not (eobp))
  14496. (or first (> (funcall outline-level) level)))
  14497. (setq first nil)
  14498. (outline-next-heading))
  14499. (unless to-heading
  14500. (if (memq (preceding-char) '(?\n ?\^M))
  14501. (progn
  14502. ;; Go to end of line before heading
  14503. (forward-char -1)
  14504. (if (memq (preceding-char) '(?\n ?\^M))
  14505. ;; leave blank line before heading
  14506. (forward-char -1))))))
  14507. (point))
  14508. (defun org-show-subtree ()
  14509. "Show everything after this heading at deeper levels."
  14510. (outline-flag-region
  14511. (point)
  14512. (save-excursion
  14513. (outline-end-of-subtree) (outline-next-heading) (point))
  14514. nil))
  14515. (defun org-show-entry ()
  14516. "Show the body directly following this heading.
  14517. Show the heading too, if it is currently invisible."
  14518. (interactive)
  14519. (save-excursion
  14520. (condition-case nil
  14521. (progn
  14522. (org-back-to-heading t)
  14523. (outline-flag-region
  14524. (max (point-min) (1- (point)))
  14525. (save-excursion
  14526. (if (re-search-forward
  14527. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14528. (match-beginning 1)
  14529. (point-max)))
  14530. nil))
  14531. (error nil))))
  14532. (defun org-make-options-regexp (kwds &optional extra)
  14533. "Make a regular expression for keyword lines."
  14534. (concat
  14535. "^"
  14536. "#?[ \t]*\\+\\("
  14537. (mapconcat 'regexp-quote kwds "\\|")
  14538. (if extra (concat "\\|" extra))
  14539. "\\):[ \t]*"
  14540. "\\(.+\\)"))
  14541. ;; Make isearch reveal the necessary context
  14542. (defun org-isearch-end ()
  14543. "Reveal context after isearch exits."
  14544. (when isearch-success ; only if search was successful
  14545. (if (featurep 'xemacs)
  14546. ;; Under XEmacs, the hook is run in the correct place,
  14547. ;; we directly show the context.
  14548. (org-show-context 'isearch)
  14549. ;; In Emacs the hook runs *before* restoring the overlays.
  14550. ;; So we have to use a one-time post-command-hook to do this.
  14551. ;; (Emacs 22 has a special variable, see function `org-mode')
  14552. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14553. isearch-mode-end-hook-quit)
  14554. ;; Only when the isearch was not quitted.
  14555. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14556. 'append 'local)))))
  14557. (defun org-isearch-post-command ()
  14558. "Remove self from hook, and show context."
  14559. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14560. (org-show-context 'isearch))
  14561. ;;;; Integration with and fixes for other packages
  14562. ;;; Imenu support
  14563. (defvar org-imenu-markers nil
  14564. "All markers currently used by Imenu.")
  14565. (make-variable-buffer-local 'org-imenu-markers)
  14566. (defun org-imenu-new-marker (&optional pos)
  14567. "Return a new marker for use by Imenu, and remember the marker."
  14568. (let ((m (make-marker)))
  14569. (move-marker m (or pos (point)))
  14570. (push m org-imenu-markers)
  14571. m))
  14572. (defun org-imenu-get-tree ()
  14573. "Produce the index for Imenu."
  14574. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14575. (setq org-imenu-markers nil)
  14576. (let* ((n org-imenu-depth)
  14577. (re (concat "^" outline-regexp))
  14578. (subs (make-vector (1+ n) nil))
  14579. (last-level 0)
  14580. m level head)
  14581. (save-excursion
  14582. (save-restriction
  14583. (widen)
  14584. (goto-char (point-max))
  14585. (while (re-search-backward re nil t)
  14586. (setq level (org-reduced-level (funcall outline-level)))
  14587. (when (<= level n)
  14588. (looking-at org-complex-heading-regexp)
  14589. (setq head (org-link-display-format
  14590. (org-match-string-no-properties 4))
  14591. m (org-imenu-new-marker))
  14592. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14593. (if (>= level last-level)
  14594. (push (cons head m) (aref subs level))
  14595. (push (cons head (aref subs (1+ level))) (aref subs level))
  14596. (loop for i from (1+ level) to n do (aset subs i nil)))
  14597. (setq last-level level)))))
  14598. (aref subs 1)))
  14599. (eval-after-load "imenu"
  14600. '(progn
  14601. (add-hook 'imenu-after-jump-hook
  14602. (lambda ()
  14603. (if (eq major-mode 'org-mode)
  14604. (org-show-context 'org-goto))))))
  14605. (defun org-link-display-format (link)
  14606. "Replace a link with either the description, or the link target
  14607. if no description is present"
  14608. (save-match-data
  14609. (if (string-match org-bracket-link-analytic-regexp link)
  14610. (replace-match (or (match-string 5 link)
  14611. (concat (match-string 1 link)
  14612. (match-string 3 link)))
  14613. nil nil link)
  14614. link)))
  14615. ;; Speedbar support
  14616. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14617. "Overlay marking the agenda restriction line in speedbar.")
  14618. (org-overlay-put org-speedbar-restriction-lock-overlay
  14619. 'face 'org-agenda-restriction-lock)
  14620. (org-overlay-put org-speedbar-restriction-lock-overlay
  14621. 'help-echo "Agendas are currently limited to this item.")
  14622. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14623. (defun org-speedbar-set-agenda-restriction ()
  14624. "Restrict future agenda commands to the location at point in speedbar.
  14625. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14626. (interactive)
  14627. (require 'org-agenda)
  14628. (let (p m tp np dir txt)
  14629. (cond
  14630. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14631. 'org-imenu t))
  14632. (setq m (get-text-property p 'org-imenu-marker))
  14633. (save-excursion
  14634. (save-restriction
  14635. (set-buffer (marker-buffer m))
  14636. (goto-char m)
  14637. (org-agenda-set-restriction-lock 'subtree))))
  14638. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14639. 'speedbar-function 'speedbar-find-file))
  14640. (setq tp (previous-single-property-change
  14641. (1+ p) 'speedbar-function)
  14642. np (next-single-property-change
  14643. tp 'speedbar-function)
  14644. dir (speedbar-line-directory)
  14645. txt (buffer-substring-no-properties (or tp (point-min))
  14646. (or np (point-max))))
  14647. (save-excursion
  14648. (save-restriction
  14649. (set-buffer (find-file-noselect
  14650. (let ((default-directory dir))
  14651. (expand-file-name txt))))
  14652. (unless (org-mode-p)
  14653. (error "Cannot restrict to non-Org-mode file"))
  14654. (org-agenda-set-restriction-lock 'file))))
  14655. (t (error "Don't know how to restrict Org-mode's agenda")))
  14656. (org-move-overlay org-speedbar-restriction-lock-overlay
  14657. (point-at-bol) (point-at-eol))
  14658. (setq current-prefix-arg nil)
  14659. (org-agenda-maybe-redo)))
  14660. (eval-after-load "speedbar"
  14661. '(progn
  14662. (speedbar-add-supported-extension ".org")
  14663. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14664. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14665. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14666. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14667. (add-hook 'speedbar-visiting-tag-hook
  14668. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14669. ;;; Fixes and Hacks for problems with other packages
  14670. ;; Make flyspell not check words in links, to not mess up our keymap
  14671. (defun org-mode-flyspell-verify ()
  14672. "Don't let flyspell put overlays at active buttons."
  14673. (not (get-text-property (point) 'keymap)))
  14674. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14675. (eval-after-load "bookmark"
  14676. '(if (boundp 'bookmark-after-jump-hook)
  14677. ;; We can use the hook
  14678. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14679. ;; Hook not available, use advice
  14680. (defadvice bookmark-jump (after org-make-visible activate)
  14681. "Make the position visible."
  14682. (org-bookmark-jump-unhide))))
  14683. ;; Make sure saveplace show the location if it was hidden
  14684. (eval-after-load "saveplace"
  14685. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14686. "Make the position visible."
  14687. (org-bookmark-jump-unhide)))
  14688. (defun org-bookmark-jump-unhide ()
  14689. "Unhide the current position, to show the bookmark location."
  14690. (and (org-mode-p)
  14691. (or (org-invisible-p)
  14692. (save-excursion (goto-char (max (point-min) (1- (point))))
  14693. (org-invisible-p)))
  14694. (org-show-context 'bookmark-jump)))
  14695. ;; Make session.el ignore our circular variable
  14696. (eval-after-load "session"
  14697. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14698. ;;;; Experimental code
  14699. (defun org-closed-in-range ()
  14700. "Sparse tree of items closed in a certain time range.
  14701. Still experimental, may disappear in the future."
  14702. (interactive)
  14703. ;; Get the time interval from the user.
  14704. (let* ((time1 (time-to-seconds
  14705. (org-read-date nil 'to-time nil "Starting date: ")))
  14706. (time2 (time-to-seconds
  14707. (org-read-date nil 'to-time nil "End date:")))
  14708. ;; callback function
  14709. (callback (lambda ()
  14710. (let ((time
  14711. (time-to-seconds
  14712. (apply 'encode-time
  14713. (org-parse-time-string
  14714. (match-string 1))))))
  14715. ;; check if time in interval
  14716. (and (>= time time1) (<= time time2))))))
  14717. ;; make tree, check each match with the callback
  14718. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14719. ;;;; Finish up
  14720. (provide 'org)
  14721. (run-hooks 'org-load-hook)
  14722. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14723. ;;; org.el ends here