org.el 520 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.03
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.03"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-align-all-tables nil
  196. "Non-nil means, align all tables when visiting a file.
  197. This is useful when the column width in tables is forced with <N> cookies
  198. in table fields. Such tables will look correct only after the first re-align.
  199. This can also be configured on a per-file basis by adding one of
  200. the following lines anywhere in the buffer:
  201. #+STARTUP: align
  202. #+STARTUP: noalign"
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-insert-mode-line-in-empty-file nil
  206. "Non-nil means insert the first line setting Org-mode in empty files.
  207. When the function `org-mode' is called interactively in an empty file, this
  208. normally means that the file name does not automatically trigger Org-mode.
  209. To ensure that the file will always be in Org-mode in the future, a
  210. line enforcing Org-mode will be inserted into the buffer, if this option
  211. has been set."
  212. :group 'org-startup
  213. :type 'boolean)
  214. (defcustom org-replace-disputed-keys nil
  215. "Non-nil means use alternative key bindings for some keys.
  216. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  217. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  218. If you want to use Org-mode together with one of these other modes,
  219. or more generally if you would like to move some Org-mode commands to
  220. other keys, set this variable and configure the keys with the variable
  221. `org-disputed-keys'.
  222. This option is only relevant at load-time of Org-mode, and must be set
  223. *before* org.el is loaded. Changing it requires a restart of Emacs to
  224. become effective."
  225. :group 'org-startup
  226. :type 'boolean)
  227. (if (fboundp 'defvaralias)
  228. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  229. (defcustom org-disputed-keys
  230. '(([(shift up)] . [(meta p)])
  231. ([(shift down)] . [(meta n)])
  232. ([(shift left)] . [(meta -)])
  233. ([(shift right)] . [(meta +)])
  234. ([(control shift right)] . [(meta shift +)])
  235. ([(control shift left)] . [(meta shift -)]))
  236. "Keys for which Org-mode and other modes compete.
  237. This is an alist, cars are the default keys, second element specifies
  238. the alternative to use when `org-replace-disputed-keys' is t.
  239. Keys can be specified in any syntax supported by `define-key'.
  240. The value of this option takes effect only at Org-mode's startup,
  241. therefore you'll have to restart Emacs to apply it after changing."
  242. :group 'org-startup
  243. :type 'alist)
  244. (defun org-key (key)
  245. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  246. Or return the original if not disputed."
  247. (if org-replace-disputed-keys
  248. (let* ((nkey (key-description key))
  249. (x (org-find-if (lambda (x)
  250. (equal (key-description (car x)) nkey))
  251. org-disputed-keys)))
  252. (if x (cdr x) key))
  253. key))
  254. (defun org-find-if (predicate seq)
  255. (catch 'exit
  256. (while seq
  257. (if (funcall predicate (car seq))
  258. (throw 'exit (car seq))
  259. (pop seq)))))
  260. (defun org-defkey (keymap key def)
  261. "Define a key, possibly translated, as returned by `org-key'."
  262. (define-key keymap (org-key key) def))
  263. (defcustom org-ellipsis nil
  264. "The ellipsis to use in the Org-mode outline.
  265. When nil, just use the standard three dots. When a string, use that instead,
  266. When a face, use the standart 3 dots, but with the specified face.
  267. The change affects only Org-mode (which will then use its own display table).
  268. Changing this requires executing `M-x org-mode' in a buffer to become
  269. effective."
  270. :group 'org-startup
  271. :type '(choice (const :tag "Default" nil)
  272. (face :tag "Face" :value org-warning)
  273. (string :tag "String" :value "...#")))
  274. (defvar org-display-table nil
  275. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  276. (defgroup org-keywords nil
  277. "Keywords in Org-mode."
  278. :tag "Org Keywords"
  279. :group 'org)
  280. (defcustom org-deadline-string "DEADLINE:"
  281. "String to mark deadline entries.
  282. A deadline is this string, followed by a time stamp. Should be a word,
  283. terminated by a colon. You can insert a schedule keyword and
  284. a timestamp with \\[org-deadline].
  285. Changes become only effective after restarting Emacs."
  286. :group 'org-keywords
  287. :type 'string)
  288. (defcustom org-scheduled-string "SCHEDULED:"
  289. "String to mark scheduled TODO entries.
  290. A schedule is this string, followed by a time stamp. Should be a word,
  291. terminated by a colon. You can insert a schedule keyword and
  292. a timestamp with \\[org-schedule].
  293. Changes become only effective after restarting Emacs."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-closed-string "CLOSED:"
  297. "String used as the prefix for timestamps logging closing a TODO entry."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-clock-string "CLOCK:"
  301. "String used as prefix for timestamps clocking work hours on an item."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-comment-string "COMMENT"
  305. "Entries starting with this keyword will never be exported.
  306. An entry can be toggled between COMMENT and normal with
  307. \\[org-toggle-comment].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-quote-string "QUOTE"
  312. "Entries starting with this keyword will be exported in fixed-width font.
  313. Quoting applies only to the text in the entry following the headline, and does
  314. not extend beyond the next headline, even if that is lower level.
  315. An entry can be toggled between QUOTE and normal with
  316. \\[org-toggle-fixed-width-section]."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defconst org-repeat-re
  320. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  321. "Regular expression for specifying repeated events.
  322. After a match, group 1 contains the repeat expression.")
  323. (defgroup org-structure nil
  324. "Options concerning the general structure of Org-mode files."
  325. :tag "Org Structure"
  326. :group 'org)
  327. (defgroup org-reveal-location nil
  328. "Options about how to make context of a location visible."
  329. :tag "Org Reveal Location"
  330. :group 'org-structure)
  331. (defconst org-context-choice
  332. '(choice
  333. (const :tag "Always" t)
  334. (const :tag "Never" nil)
  335. (repeat :greedy t :tag "Individual contexts"
  336. (cons
  337. (choice :tag "Context"
  338. (const agenda)
  339. (const org-goto)
  340. (const occur-tree)
  341. (const tags-tree)
  342. (const link-search)
  343. (const mark-goto)
  344. (const bookmark-jump)
  345. (const isearch)
  346. (const default))
  347. (boolean))))
  348. "Contexts for the reveal options.")
  349. (defcustom org-show-hierarchy-above '((default . t))
  350. "Non-nil means, show full hierarchy when revealing a location.
  351. Org-mode often shows locations in an org-mode file which might have
  352. been invisible before. When this is set, the hierarchy of headings
  353. above the exposed location is shown.
  354. Turning this off for example for sparse trees makes them very compact.
  355. Instead of t, this can also be an alist specifying this option for different
  356. contexts. Valid contexts are
  357. agenda when exposing an entry from the agenda
  358. org-goto when using the command `org-goto' on key C-c C-j
  359. occur-tree when using the command `org-occur' on key C-c /
  360. tags-tree when constructing a sparse tree based on tags matches
  361. link-search when exposing search matches associated with a link
  362. mark-goto when exposing the jump goal of a mark
  363. bookmark-jump when exposing a bookmark location
  364. isearch when exiting from an incremental search
  365. default default for all contexts not set explicitly"
  366. :group 'org-reveal-location
  367. :type org-context-choice)
  368. (defcustom org-show-following-heading '((default . nil))
  369. "Non-nil means, show following heading when revealing a location.
  370. Org-mode often shows locations in an org-mode file which might have
  371. been invisible before. When this is set, the heading following the
  372. match is shown.
  373. Turning this off for example for sparse trees makes them very compact,
  374. but makes it harder to edit the location of the match. In such a case,
  375. use the command \\[org-reveal] to show more context.
  376. Instead of t, this can also be an alist specifying this option for different
  377. contexts. See `org-show-hierarchy-above' for valid contexts."
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-siblings '((default . nil) (isearch t))
  381. "Non-nil means, show all sibling heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the sibling of the current entry
  384. heading are all made visible. If `org-show-hierarchy-above' is t,
  385. the same happens on each level of the hierarchy above the current entry.
  386. By default this is on for the isearch context, off for all other contexts.
  387. Turning this off for example for sparse trees makes them very compact,
  388. but makes it harder to edit the location of the match. In such a case,
  389. use the command \\[org-reveal] to show more context.
  390. Instead of t, this can also be an alist specifying this option for different
  391. contexts. See `org-show-hierarchy-above' for valid contexts."
  392. :group 'org-reveal-location
  393. :type org-context-choice)
  394. (defcustom org-show-entry-below '((default . nil))
  395. "Non-nil means, show the entry below a headline when revealing a location.
  396. Org-mode often shows locations in an org-mode file which might have
  397. been invisible before. When this is set, the text below the headline that is
  398. exposed is also shown.
  399. By default this is off for all contexts.
  400. Instead of t, this can also be an alist specifying this option for different
  401. contexts. See `org-show-hierarchy-above' for valid contexts."
  402. :group 'org-reveal-location
  403. :type org-context-choice)
  404. (defcustom org-indirect-buffer-display 'other-window
  405. "How should indirect tree buffers be displayed?
  406. This applies to indirect buffers created with the commands
  407. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  408. Valid values are:
  409. current-window Display in the current window
  410. other-window Just display in another window.
  411. dedicated-frame Create one new frame, and re-use it each time.
  412. new-frame Make a new frame each time. Note that in this case
  413. previously-made indirect buffers are kept, and you need to
  414. kill these buffers yourself."
  415. :group 'org-structure
  416. :group 'org-agenda-windows
  417. :type '(choice
  418. (const :tag "In current window" current-window)
  419. (const :tag "In current frame, other window" other-window)
  420. (const :tag "Each time a new frame" new-frame)
  421. (const :tag "One dedicated frame" dedicated-frame)))
  422. (defgroup org-cycle nil
  423. "Options concerning visibility cycling in Org-mode."
  424. :tag "Org Cycle"
  425. :group 'org-structure)
  426. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  427. "Names of drawers. Drawers are not opened by cycling on the headline above.
  428. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  429. this:
  430. :DRAWERNAME:
  431. .....
  432. :END:
  433. The drawer \"PROPERTIES\" is special for capturing properties through
  434. the property API.
  435. Drawers can be defined on the per-file basis with a line like:
  436. #+DRAWERS: HIDDEN STATE PROPERTIES"
  437. :group 'org-structure
  438. :type '(repeat (string :tag "Drawer Name")))
  439. (defcustom org-cycle-global-at-bob nil
  440. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  441. This makes it possible to do global cycling without having to use S-TAB or
  442. C-u TAB. For this special case to work, the first line of the buffer
  443. must not be a headline - it may be empty ot some other text. When used in
  444. this way, `org-cycle-hook' is disables temporarily, to make sure the
  445. cursor stays at the beginning of the buffer.
  446. When this option is nil, don't do anything special at the beginning
  447. of the buffer."
  448. :group 'org-cycle
  449. :type 'boolean)
  450. (defcustom org-cycle-emulate-tab t
  451. "Where should `org-cycle' emulate TAB.
  452. nil Never
  453. white Only in completely white lines
  454. whitestart Only at the beginning of lines, before the first non-white char
  455. t Everywhere except in headlines
  456. exc-hl-bol Everywhere except at the start of a headline
  457. If TAB is used in a place where it does not emulate TAB, the current subtree
  458. visibility is cycled."
  459. :group 'org-cycle
  460. :type '(choice (const :tag "Never" nil)
  461. (const :tag "Only in completely white lines" white)
  462. (const :tag "Before first char in a line" whitestart)
  463. (const :tag "Everywhere except in headlines" t)
  464. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  465. ))
  466. (defcustom org-cycle-separator-lines 2
  467. "Number of empty lines needed to keep an empty line between collapsed trees.
  468. If you leave an empty line between the end of a subtree and the following
  469. headline, this empty line is hidden when the subtree is folded.
  470. Org-mode will leave (exactly) one empty line visible if the number of
  471. empty lines is equal or larger to the number given in this variable.
  472. So the default 2 means, at least 2 empty lines after the end of a subtree
  473. are needed to produce free space between a collapsed subtree and the
  474. following headline.
  475. Special case: when 0, never leave empty lines in collapsed view."
  476. :group 'org-cycle
  477. :type 'integer)
  478. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  479. org-cycle-hide-drawers
  480. org-cycle-show-empty-lines
  481. org-optimize-window-after-visibility-change)
  482. "Hook that is run after `org-cycle' has changed the buffer visibility.
  483. The function(s) in this hook must accept a single argument which indicates
  484. the new state that was set by the most recent `org-cycle' command. The
  485. argument is a symbol. After a global state change, it can have the values
  486. `overview', `content', or `all'. After a local state change, it can have
  487. the values `folded', `children', or `subtree'."
  488. :group 'org-cycle
  489. :type 'hook)
  490. (defgroup org-edit-structure nil
  491. "Options concerning structure editing in Org-mode."
  492. :tag "Org Edit Structure"
  493. :group 'org-structure)
  494. (defcustom org-odd-levels-only nil
  495. "Non-nil means, skip even levels and only use odd levels for the outline.
  496. This has the effect that two stars are being added/taken away in
  497. promotion/demotion commands. It also influences how levels are
  498. handled by the exporters.
  499. Changing it requires restart of `font-lock-mode' to become effective
  500. for fontification also in regions already fontified.
  501. You may also set this on a per-file basis by adding one of the following
  502. lines to the buffer:
  503. #+STARTUP: odd
  504. #+STARTUP: oddeven"
  505. :group 'org-edit-structure
  506. :group 'org-font-lock
  507. :type 'boolean)
  508. (defcustom org-adapt-indentation t
  509. "Non-nil means, adapt indentation when promoting and demoting.
  510. When this is set and the *entire* text in an entry is indented, the
  511. indentation is increased by one space in a demotion command, and
  512. decreased by one in a promotion command. If any line in the entry
  513. body starts at column 0, indentation is not changed at all."
  514. :group 'org-edit-structure
  515. :type 'boolean)
  516. (defcustom org-special-ctrl-a/e nil
  517. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  518. When t, `C-a' will bring back the cursor to the beginning of the
  519. headline text, i.e. after the stars and after a possible TODO keyword.
  520. In an item, this will be the position after the bullet.
  521. When the cursor is already at that position, another `C-a' will bring
  522. it to the beginning of the line.
  523. `C-e' will jump to the end of the headline, ignoring the presence of tags
  524. in the headline. A second `C-e' will then jump to the true end of the
  525. line, after any tags.
  526. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  527. and only a directly following, identical keypress will bring the cursor
  528. to the special positions."
  529. :group 'org-edit-structure
  530. :type '(choice
  531. (const :tag "off" nil)
  532. (const :tag "after bullet first" t)
  533. (const :tag "border first" reversed)))
  534. (if (fboundp 'defvaralias)
  535. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  536. (defcustom org-special-ctrl-k nil
  537. "Non-nil means `C-k' will behave specially in headlines.
  538. When nil, `C-k' will call the default `kill-line' command.
  539. When t, the following will happen while the cursor is in the headline:
  540. - When the cursor is at the beginning of a headline, kill the entire
  541. line and possible the folded subtree below the line.
  542. - When in the middle of the headline text, kill the headline up to the tags.
  543. - When after the headline text, kill the tags."
  544. :group 'org-edit-structure
  545. :type 'boolean)
  546. (defcustom org-M-RET-may-split-line '((default . t))
  547. "Non-nil means, M-RET will split the line at the cursor position.
  548. When nil, it will go to the end of the line before making a
  549. new line.
  550. You may also set this option in a different way for different
  551. contexts. Valid contexts are:
  552. headline when creating a new headline
  553. item when creating a new item
  554. table in a table field
  555. default the value to be used for all contexts not explicitly
  556. customized"
  557. :group 'org-structure
  558. :group 'org-table
  559. :type '(choice
  560. (const :tag "Always" t)
  561. (const :tag "Never" nil)
  562. (repeat :greedy t :tag "Individual contexts"
  563. (cons
  564. (choice :tag "Context"
  565. (const headline)
  566. (const item)
  567. (const table)
  568. (const default))
  569. (boolean)))))
  570. (defcustom org-blank-before-new-entry '((heading . nil)
  571. (plain-list-item . nil))
  572. "Should `org-insert-heading' leave a blank line before new heading/item?
  573. The value is an alist, with `heading' and `plain-list-item' as car,
  574. and a boolean flag as cdr."
  575. :group 'org-edit-structure
  576. :type '(list
  577. (cons (const heading) (boolean))
  578. (cons (const plain-list-item) (boolean))))
  579. (defcustom org-insert-heading-hook nil
  580. "Hook being run after inserting a new heading."
  581. :group 'org-edit-structure
  582. :type 'hook)
  583. (defcustom org-enable-fixed-width-editor t
  584. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  585. This currently only means, they are never auto-wrapped.
  586. When nil, such lines will be treated like ordinary lines.
  587. See also the QUOTE keyword."
  588. :group 'org-edit-structure
  589. :type 'boolean)
  590. (defcustom org-goto-auto-isearch t
  591. "Non-nil means, typing characters in org-goto starts incremental search."
  592. :group 'org-edit-structure
  593. :type 'boolean)
  594. (defgroup org-sparse-trees nil
  595. "Options concerning sparse trees in Org-mode."
  596. :tag "Org Sparse Trees"
  597. :group 'org-structure)
  598. (defcustom org-highlight-sparse-tree-matches t
  599. "Non-nil means, highlight all matches that define a sparse tree.
  600. The highlights will automatically disappear the next time the buffer is
  601. changed by an edit command."
  602. :group 'org-sparse-trees
  603. :type 'boolean)
  604. (defcustom org-remove-highlights-with-change t
  605. "Non-nil means, any change to the buffer will remove temporary highlights.
  606. Such highlights are created by `org-occur' and `org-clock-display'.
  607. When nil, `C-c C-c needs to be used to get rid of the highlights.
  608. The highlights created by `org-preview-latex-fragment' always need
  609. `C-c C-c' to be removed."
  610. :group 'org-sparse-trees
  611. :group 'org-time
  612. :type 'boolean)
  613. (defcustom org-occur-hook '(org-first-headline-recenter)
  614. "Hook that is run after `org-occur' has constructed a sparse tree.
  615. This can be used to recenter the window to show as much of the structure
  616. as possible."
  617. :group 'org-sparse-trees
  618. :type 'hook)
  619. (defgroup org-plain-lists nil
  620. "Options concerning plain lists in Org-mode."
  621. :tag "Org Plain lists"
  622. :group 'org-structure)
  623. (defcustom org-cycle-include-plain-lists nil
  624. "Non-nil means, include plain lists into visibility cycling.
  625. This means that during cycling, plain list items will *temporarily* be
  626. interpreted as outline headlines with a level given by 1000+i where i is the
  627. indentation of the bullet. In all other operations, plain list items are
  628. not seen as headlines. For example, you cannot assign a TODO keyword to
  629. such an item."
  630. :group 'org-plain-lists
  631. :type 'boolean)
  632. (defcustom org-plain-list-ordered-item-terminator t
  633. "The character that makes a line with leading number an ordered list item.
  634. Valid values are ?. and ?\). To get both terminators, use t. While
  635. ?. may look nicer, it creates the danger that a line with leading
  636. number may be incorrectly interpreted as an item. ?\) therefore is
  637. the safe choice."
  638. :group 'org-plain-lists
  639. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  640. (const :tag "paren like in \"2)\"" ?\))
  641. (const :tab "both" t)))
  642. (defcustom org-empty-line-terminates-plain-lists nil
  643. "Non-nil means, an empty line ends all plain list levels.
  644. When nil, empty lines are part of the preceeding item."
  645. :group 'org-plain-lists
  646. :type 'boolean)
  647. (defcustom org-auto-renumber-ordered-lists t
  648. "Non-nil means, automatically renumber ordered plain lists.
  649. Renumbering happens when the sequence have been changed with
  650. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  651. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  652. :group 'org-plain-lists
  653. :type 'boolean)
  654. (defcustom org-provide-checkbox-statistics t
  655. "Non-nil means, update checkbox statistics after insert and toggle.
  656. When this is set, checkbox statistics is updated each time you either insert
  657. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  658. with \\[org-ctrl-c-ctrl-c\\]."
  659. :group 'org-plain-lists
  660. :type 'boolean)
  661. (defcustom org-description-max-indent 20
  662. "Maximum indentation for the second line of a description list.
  663. When the indentation would be larger than this, it will become
  664. 5 characters instead."
  665. :group 'org-plain-lists
  666. :type 'integer)
  667. (defgroup org-imenu-and-speedbar nil
  668. "Options concerning imenu and speedbar in Org-mode."
  669. :tag "Org Imenu and Speedbar"
  670. :group 'org-structure)
  671. (defcustom org-imenu-depth 2
  672. "The maximum level for Imenu access to Org-mode headlines.
  673. This also applied for speedbar access."
  674. :group 'org-imenu-and-speedbar
  675. :type 'number)
  676. (defgroup org-table nil
  677. "Options concerning tables in Org-mode."
  678. :tag "Org Table"
  679. :group 'org)
  680. (defcustom org-enable-table-editor 'optimized
  681. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  682. When nil, such lines will be treated like ordinary lines.
  683. When equal to the symbol `optimized', the table editor will be optimized to
  684. do the following:
  685. - Automatic overwrite mode in front of whitespace in table fields.
  686. This makes the structure of the table stay in tact as long as the edited
  687. field does not exceed the column width.
  688. - Minimize the number of realigns. Normally, the table is aligned each time
  689. TAB or RET are pressed to move to another field. With optimization this
  690. happens only if changes to a field might have changed the column width.
  691. Optimization requires replacing the functions `self-insert-command',
  692. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  693. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  694. very good at guessing when a re-align will be necessary, but you can always
  695. force one with \\[org-ctrl-c-ctrl-c].
  696. If you would like to use the optimized version in Org-mode, but the
  697. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  698. This variable can be used to turn on and off the table editor during a session,
  699. but in order to toggle optimization, a restart is required.
  700. See also the variable `org-table-auto-blank-field'."
  701. :group 'org-table
  702. :type '(choice
  703. (const :tag "off" nil)
  704. (const :tag "on" t)
  705. (const :tag "on, optimized" optimized)))
  706. (defcustom org-table-tab-recognizes-table.el t
  707. "Non-nil means, TAB will automatically notice a table.el table.
  708. When it sees such a table, it moves point into it and - if necessary -
  709. calls `table-recognize-table'."
  710. :group 'org-table-editing
  711. :type 'boolean)
  712. (defgroup org-link nil
  713. "Options concerning links in Org-mode."
  714. :tag "Org Link"
  715. :group 'org)
  716. (defvar org-link-abbrev-alist-local nil
  717. "Buffer-local version of `org-link-abbrev-alist', which see.
  718. The value of this is taken from the #+LINK lines.")
  719. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  720. (defcustom org-link-abbrev-alist nil
  721. "Alist of link abbreviations.
  722. The car of each element is a string, to be replaced at the start of a link.
  723. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  724. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  725. [[linkkey:tag][description]]
  726. If REPLACE is a string, the tag will simply be appended to create the link.
  727. If the string contains \"%s\", the tag will be inserted there.
  728. REPLACE may also be a function that will be called with the tag as the
  729. only argument to create the link, which should be returned as a string.
  730. See the manual for examples."
  731. :group 'org-link
  732. :type 'alist)
  733. (defcustom org-descriptive-links t
  734. "Non-nil means, hide link part and only show description of bracket links.
  735. Bracket links are like [[link][descritpion]]. This variable sets the initial
  736. state in new org-mode buffers. The setting can then be toggled on a
  737. per-buffer basis from the Org->Hyperlinks menu."
  738. :group 'org-link
  739. :type 'boolean)
  740. (defcustom org-link-file-path-type 'adaptive
  741. "How the path name in file links should be stored.
  742. Valid values are:
  743. relative Relative to the current directory, i.e. the directory of the file
  744. into which the link is being inserted.
  745. absolute Absolute path, if possible with ~ for home directory.
  746. noabbrev Absolute path, no abbreviation of home directory.
  747. adaptive Use relative path for files in the current directory and sub-
  748. directories of it. For other files, use an absolute path."
  749. :group 'org-link
  750. :type '(choice
  751. (const relative)
  752. (const absolute)
  753. (const noabbrev)
  754. (const adaptive)))
  755. (defcustom org-activate-links '(bracket angle plain radio tag date)
  756. "Types of links that should be activated in Org-mode files.
  757. This is a list of symbols, each leading to the activation of a certain link
  758. type. In principle, it does not hurt to turn on most link types - there may
  759. be a small gain when turning off unused link types. The types are:
  760. bracket The recommended [[link][description]] or [[link]] links with hiding.
  761. angular Links in angular brackes that may contain whitespace like
  762. <bbdb:Carsten Dominik>.
  763. plain Plain links in normal text, no whitespace, like http://google.com.
  764. radio Text that is matched by a radio target, see manual for details.
  765. tag Tag settings in a headline (link to tag search).
  766. date Time stamps (link to calendar).
  767. Changing this variable requires a restart of Emacs to become effective."
  768. :group 'org-link
  769. :type '(set (const :tag "Double bracket links (new style)" bracket)
  770. (const :tag "Angular bracket links (old style)" angular)
  771. (const :tag "Plain text links" plain)
  772. (const :tag "Radio target matches" radio)
  773. (const :tag "Tags" tag)
  774. (const :tag "Timestamps" date)))
  775. (defcustom org-make-link-description-function nil
  776. "Function to use to generate link descriptions from links. If
  777. nil the link location will be used. This function must take two
  778. parameters; the first is the link and the second the description
  779. org-insert-link has generated, and should return the description
  780. to use."
  781. :group 'org-link
  782. :type 'function)
  783. (defgroup org-link-store nil
  784. "Options concerning storing links in Org-mode."
  785. :tag "Org Store Link"
  786. :group 'org-link)
  787. (defcustom org-email-link-description-format "Email %c: %.30s"
  788. "Format of the description part of a link to an email or usenet message.
  789. The following %-excapes will be replaced by corresponding information:
  790. %F full \"From\" field
  791. %f name, taken from \"From\" field, address if no name
  792. %T full \"To\" field
  793. %t first name in \"To\" field, address if no name
  794. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  795. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  796. %s subject
  797. %m message-id.
  798. You may use normal field width specification between the % and the letter.
  799. This is for example useful to limit the length of the subject.
  800. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  801. :group 'org-link-store
  802. :type 'string)
  803. (defcustom org-from-is-user-regexp
  804. (let (r1 r2)
  805. (when (and user-mail-address (not (string= user-mail-address "")))
  806. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  807. (when (and user-full-name (not (string= user-full-name "")))
  808. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  809. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  810. "Regexp mached against the \"From:\" header of an email or usenet message.
  811. It should match if the message is from the user him/herself."
  812. :group 'org-link-store
  813. :type 'regexp)
  814. (defcustom org-context-in-file-links t
  815. "Non-nil means, file links from `org-store-link' contain context.
  816. A search string will be added to the file name with :: as separator and
  817. used to find the context when the link is activated by the command
  818. `org-open-at-point'.
  819. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  820. negates this setting for the duration of the command."
  821. :group 'org-link-store
  822. :type 'boolean)
  823. (defcustom org-keep-stored-link-after-insertion nil
  824. "Non-nil means, keep link in list for entire session.
  825. The command `org-store-link' adds a link pointing to the current
  826. location to an internal list. These links accumulate during a session.
  827. The command `org-insert-link' can be used to insert links into any
  828. Org-mode file (offering completion for all stored links). When this
  829. option is nil, every link which has been inserted once using \\[org-insert-link]
  830. will be removed from the list, to make completing the unused links
  831. more efficient."
  832. :group 'org-link-store
  833. :type 'boolean)
  834. (defgroup org-link-follow nil
  835. "Options concerning following links in Org-mode."
  836. :tag "Org Follow Link"
  837. :group 'org-link)
  838. (defcustom org-follow-link-hook nil
  839. "Hook that is run after a link has been followed."
  840. :group 'org-link-follow
  841. :type 'hook)
  842. (defcustom org-tab-follows-link nil
  843. "Non-nil means, on links TAB will follow the link.
  844. Needs to be set before org.el is loaded."
  845. :group 'org-link-follow
  846. :type 'boolean)
  847. (defcustom org-return-follows-link nil
  848. "Non-nil means, on links RET will follow the link.
  849. Needs to be set before org.el is loaded."
  850. :group 'org-link-follow
  851. :type 'boolean)
  852. (defcustom org-mouse-1-follows-link
  853. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  854. "Non-nil means, mouse-1 on a link will follow the link.
  855. A longer mouse click will still set point. Does not work on XEmacs.
  856. Needs to be set before org.el is loaded."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-mark-ring-length 4
  860. "Number of different positions to be recorded in the ring
  861. Changing this requires a restart of Emacs to work correctly."
  862. :group 'org-link-follow
  863. :type 'interger)
  864. (defcustom org-link-frame-setup
  865. '((vm . vm-visit-folder-other-frame)
  866. (gnus . gnus-other-frame)
  867. (file . find-file-other-window))
  868. "Setup the frame configuration for following links.
  869. When following a link with Emacs, it may often be useful to display
  870. this link in another window or frame. This variable can be used to
  871. set this up for the different types of links.
  872. For VM, use any of
  873. `vm-visit-folder'
  874. `vm-visit-folder-other-frame'
  875. For Gnus, use any of
  876. `gnus'
  877. `gnus-other-frame'
  878. For FILE, use any of
  879. `find-file'
  880. `find-file-other-window'
  881. `find-file-other-frame'
  882. For the calendar, use the variable `calendar-setup'.
  883. For BBDB, it is currently only possible to display the matches in
  884. another window."
  885. :group 'org-link-follow
  886. :type '(list
  887. (cons (const vm)
  888. (choice
  889. (const vm-visit-folder)
  890. (const vm-visit-folder-other-window)
  891. (const vm-visit-folder-other-frame)))
  892. (cons (const gnus)
  893. (choice
  894. (const gnus)
  895. (const gnus-other-frame)))
  896. (cons (const file)
  897. (choice
  898. (const find-file)
  899. (const find-file-other-window)
  900. (const find-file-other-frame)))))
  901. (defcustom org-display-internal-link-with-indirect-buffer nil
  902. "Non-nil means, use indirect buffer to display infile links.
  903. Activating internal links (from one location in a file to another location
  904. in the same file) normally just jumps to the location. When the link is
  905. activated with a C-u prefix (or with mouse-3), the link is displayed in
  906. another window. When this option is set, the other window actually displays
  907. an indirect buffer clone of the current buffer, to avoid any visibility
  908. changes to the current buffer."
  909. :group 'org-link-follow
  910. :type 'boolean)
  911. (defcustom org-open-non-existing-files nil
  912. "Non-nil means, `org-open-file' will open non-existing files.
  913. When nil, an error will be generated."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  917. "Function and arguments to call for following mailto links.
  918. This is a list with the first element being a lisp function, and the
  919. remaining elements being arguments to the function. In string arguments,
  920. %a will be replaced by the address, and %s will be replaced by the subject
  921. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  922. :group 'org-link-follow
  923. :type '(choice
  924. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  925. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  926. (const :tag "message-mail" (message-mail "%a" "%s"))
  927. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  928. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  929. "Non-nil means, ask for confirmation before executing shell links.
  930. Shell links can be dangerous: just think about a link
  931. [[shell:rm -rf ~/*][Google Search]]
  932. This link would show up in your Org-mode document as \"Google Search\",
  933. but really it would remove your entire home directory.
  934. Therefore we advise against setting this variable to nil.
  935. Just change it to `y-or-n-p' of you want to confirm with a
  936. single keystroke rather than having to type \"yes\"."
  937. :group 'org-link-follow
  938. :type '(choice
  939. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  940. (const :tag "with y-or-n (faster)" y-or-n-p)
  941. (const :tag "no confirmation (dangerous)" nil)))
  942. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  943. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  944. Elisp links can be dangerous: just think about a link
  945. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  946. This link would show up in your Org-mode document as \"Google Search\",
  947. but really it would remove your entire home directory.
  948. Therefore we advise against setting this variable to nil.
  949. Just change it to `y-or-n-p' of you want to confirm with a
  950. single keystroke rather than having to type \"yes\"."
  951. :group 'org-link-follow
  952. :type '(choice
  953. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  954. (const :tag "with y-or-n (faster)" y-or-n-p)
  955. (const :tag "no confirmation (dangerous)" nil)))
  956. (defconst org-file-apps-defaults-gnu
  957. '((remote . emacs)
  958. (t . mailcap))
  959. "Default file applications on a UNIX or GNU/Linux system.
  960. See `org-file-apps'.")
  961. (defconst org-file-apps-defaults-macosx
  962. '((remote . emacs)
  963. (t . "open %s")
  964. ("ps" . "gv %s")
  965. ("ps.gz" . "gv %s")
  966. ("eps" . "gv %s")
  967. ("eps.gz" . "gv %s")
  968. ("dvi" . "xdvi %s")
  969. ("fig" . "xfig %s"))
  970. "Default file applications on a MacOS X system.
  971. The system \"open\" is known as a default, but we use X11 applications
  972. for some files for which the OS does not have a good default.
  973. See `org-file-apps'.")
  974. (defconst org-file-apps-defaults-windowsnt
  975. (list
  976. '(remote . emacs)
  977. (cons t
  978. (list (if (featurep 'xemacs)
  979. 'mswindows-shell-execute
  980. 'w32-shell-execute)
  981. "open" 'file)))
  982. "Default file applications on a Windows NT system.
  983. The system \"open\" is used for most files.
  984. See `org-file-apps'.")
  985. (defcustom org-file-apps
  986. '(
  987. ("txt" . emacs)
  988. ("tex" . emacs)
  989. ("ltx" . emacs)
  990. ("org" . emacs)
  991. ("el" . emacs)
  992. ("bib" . emacs)
  993. )
  994. "External applications for opening `file:path' items in a document.
  995. Org-mode uses system defaults for different file types, but
  996. you can use this variable to set the application for a given file
  997. extension. The entries in this list are cons cells where the car identifies
  998. files and the cdr the corresponding command. Possible values for the
  999. file identifier are
  1000. \"ext\" A string identifying an extension
  1001. `directory' Matches a directory
  1002. `remote' Matches a remote file, accessible through tramp or efs.
  1003. Remote files most likely should be visited through Emacs
  1004. because external applications cannot handle such paths.
  1005. t Default for all remaining files
  1006. Possible values for the command are:
  1007. `emacs' The file will be visited by the current Emacs process.
  1008. `default' Use the default application for this file type.
  1009. string A command to be executed by a shell; %s will be replaced
  1010. by the path to the file.
  1011. sexp A Lisp form which will be evaluated. The file path will
  1012. be available in the Lisp variable `file'.
  1013. For more examples, see the system specific constants
  1014. `org-file-apps-defaults-macosx'
  1015. `org-file-apps-defaults-windowsnt'
  1016. `org-file-apps-defaults-gnu'."
  1017. :group 'org-link-follow
  1018. :type '(repeat
  1019. (cons (choice :value ""
  1020. (string :tag "Extension")
  1021. (const :tag "Default for unrecognized files" t)
  1022. (const :tag "Remote file" remote)
  1023. (const :tag "Links to a directory" directory))
  1024. (choice :value ""
  1025. (const :tag "Visit with Emacs" emacs)
  1026. (const :tag "Use system default" default)
  1027. (string :tag "Command")
  1028. (sexp :tag "Lisp form")))))
  1029. (defgroup org-refile nil
  1030. "Options concerning refiling entries in Org-mode."
  1031. :tag "Org Remember"
  1032. :group 'org)
  1033. (defcustom org-directory "~/org"
  1034. "Directory with org files.
  1035. This directory will be used as default to prompt for org files.
  1036. Used by the hooks for remember.el."
  1037. :group 'org-refile
  1038. :group 'org-remember
  1039. :type 'directory)
  1040. (defcustom org-default-notes-file "~/.notes"
  1041. "Default target for storing notes.
  1042. Used by the hooks for remember.el. This can be a string, or nil to mean
  1043. the value of `remember-data-file'.
  1044. You can set this on a per-template basis with the variable
  1045. `org-remember-templates'."
  1046. :group 'org-refile
  1047. :group 'org-remember
  1048. :type '(choice
  1049. (const :tag "Default from remember-data-file" nil)
  1050. file))
  1051. (defcustom org-goto-interface 'outline
  1052. "The default interface to be used for `org-goto'.
  1053. Allowed vaues are:
  1054. outline The interface shows an outline of the relevant file
  1055. and the correct heading is found by moving through
  1056. the outline or by searching with incremental search.
  1057. outline-path-completion Headlines in the current buffer are offered via
  1058. completion."
  1059. :group 'org-refile
  1060. :type '(choice
  1061. (const :tag "Outline" outline)
  1062. (const :tag "Outline-path-completion" outline-path-completion)))
  1063. (defcustom org-reverse-note-order nil
  1064. "Non-nil means, store new notes at the beginning of a file or entry.
  1065. When nil, new notes will be filed to the end of a file or entry.
  1066. This can also be a list with cons cells of regular expressions that
  1067. are matched against file names, and values."
  1068. :group 'org-remember
  1069. :type '(choice
  1070. (const :tag "Reverse always" t)
  1071. (const :tag "Reverse never" nil)
  1072. (repeat :tag "By file name regexp"
  1073. (cons regexp boolean))))
  1074. (defcustom org-refile-targets nil
  1075. "Targets for refiling entries with \\[org-refile].
  1076. This is list of cons cells. Each cell contains:
  1077. - a specification of the files to be considered, either a list of files,
  1078. or a symbol whose function or variable value will be used to retrieve
  1079. a file name or a list of file names. Nil means, refile to a different
  1080. heading in the current buffer.
  1081. - A specification of how to find candidate refile targets. This may be
  1082. any of
  1083. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1084. This tag has to be present in all target headlines, inheritance will
  1085. not be considered.
  1086. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1087. todo keyword.
  1088. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1089. headlines that are refiling targets.
  1090. - a cons cell (:level . N). Any headline of level N is considered a target.
  1091. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1092. :group 'org-remember
  1093. :type '(repeat
  1094. (cons
  1095. (choice :value org-agenda-files
  1096. (const :tag "All agenda files" org-agenda-files)
  1097. (const :tag "Current buffer" nil)
  1098. (function) (variable) (file))
  1099. (choice :tag "Identify target headline by"
  1100. (cons :tag "Specific tag" (const :tag) (string))
  1101. (cons :tag "TODO keyword" (const :todo) (string))
  1102. (cons :tag "Regular expression" (const :regexp) (regexp))
  1103. (cons :tag "Level number" (const :level) (integer))
  1104. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1105. (defcustom org-refile-use-outline-path nil
  1106. "Non-nil means, provide refile targets as paths.
  1107. So a level 3 headline will be available as level1/level2/level3.
  1108. When the value is `file', also include the file name (without directory)
  1109. into the path. When `full-file-path', include the full file path."
  1110. :group 'org-remember
  1111. :type '(choice
  1112. (const :tag "Not" nil)
  1113. (const :tag "Yes" t)
  1114. (const :tag "Start with file name" file)
  1115. (const :tag "Start with full file path" full-file-path)))
  1116. (defgroup org-todo nil
  1117. "Options concerning TODO items in Org-mode."
  1118. :tag "Org TODO"
  1119. :group 'org)
  1120. (defgroup org-progress nil
  1121. "Options concerning Progress logging in Org-mode."
  1122. :tag "Org Progress"
  1123. :group 'org-time)
  1124. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1125. "List of TODO entry keyword sequences and their interpretation.
  1126. \\<org-mode-map>This is a list of sequences.
  1127. Each sequence starts with a symbol, either `sequence' or `type',
  1128. indicating if the keywords should be interpreted as a sequence of
  1129. action steps, or as different types of TODO items. The first
  1130. keywords are states requiring action - these states will select a headline
  1131. for inclusion into the global TODO list Org-mode produces. If one of
  1132. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1133. signify that no further action is necessary. If \"|\" is not found,
  1134. the last keyword is treated as the only DONE state of the sequence.
  1135. The command \\[org-todo] cycles an entry through these states, and one
  1136. additional state where no keyword is present. For details about this
  1137. cycling, see the manual.
  1138. TODO keywords and interpretation can also be set on a per-file basis with
  1139. the special #+SEQ_TODO and #+TYP_TODO lines.
  1140. Each keyword can optionally specify a character for fast state selection
  1141. \(in combination with the variable `org-use-fast-todo-selection')
  1142. and specifiers for state change logging, using the same syntax
  1143. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1144. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1145. indicates to record a time stamp each time this state is selected.
  1146. Each keyword may also specify if a timestamp or a note should be
  1147. recorded when entering or leaving the state, by adding additional
  1148. characters in the parenthesis after the keyword. This looks like this:
  1149. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1150. record only the time of the state change. With X and Y being either
  1151. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1152. Y when leaving the state if and only if the *target* state does not
  1153. define X. You may omit any of the fast-selection key or X or /Y,
  1154. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1155. For backward compatibility, this variable may also be just a list
  1156. of keywords - in this case the interptetation (sequence or type) will be
  1157. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1158. :group 'org-todo
  1159. :group 'org-keywords
  1160. :type '(choice
  1161. (repeat :tag "Old syntax, just keywords"
  1162. (string :tag "Keyword"))
  1163. (repeat :tag "New syntax"
  1164. (cons
  1165. (choice
  1166. :tag "Interpretation"
  1167. (const :tag "Sequence (cycling hits every state)" sequence)
  1168. (const :tag "Type (cycling directly to DONE)" type))
  1169. (repeat
  1170. (string :tag "Keyword"))))))
  1171. (defvar org-todo-keywords-1 nil
  1172. "All TODO and DONE keywords active in a buffer.")
  1173. (make-variable-buffer-local 'org-todo-keywords-1)
  1174. (defvar org-todo-keywords-for-agenda nil)
  1175. (defvar org-done-keywords-for-agenda nil)
  1176. (defvar org-agenda-contributing-files nil)
  1177. (defvar org-not-done-keywords nil)
  1178. (make-variable-buffer-local 'org-not-done-keywords)
  1179. (defvar org-done-keywords nil)
  1180. (make-variable-buffer-local 'org-done-keywords)
  1181. (defvar org-todo-heads nil)
  1182. (make-variable-buffer-local 'org-todo-heads)
  1183. (defvar org-todo-sets nil)
  1184. (make-variable-buffer-local 'org-todo-sets)
  1185. (defvar org-todo-log-states nil)
  1186. (make-variable-buffer-local 'org-todo-log-states)
  1187. (defvar org-todo-kwd-alist nil)
  1188. (make-variable-buffer-local 'org-todo-kwd-alist)
  1189. (defvar org-todo-key-alist nil)
  1190. (make-variable-buffer-local 'org-todo-key-alist)
  1191. (defvar org-todo-key-trigger nil)
  1192. (make-variable-buffer-local 'org-todo-key-trigger)
  1193. (defcustom org-todo-interpretation 'sequence
  1194. "Controls how TODO keywords are interpreted.
  1195. This variable is in principle obsolete and is only used for
  1196. backward compatibility, if the interpretation of todo keywords is
  1197. not given already in `org-todo-keywords'. See that variable for
  1198. more information."
  1199. :group 'org-todo
  1200. :group 'org-keywords
  1201. :type '(choice (const sequence)
  1202. (const type)))
  1203. (defcustom org-use-fast-todo-selection 'prefix
  1204. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1205. This variable describes if and under what circumstances the cycling
  1206. mechanism for TODO keywords will be replaced by a single-key, direct
  1207. selection scheme.
  1208. When nil, fast selection is never used.
  1209. When the symbol `prefix', it will be used when `org-todo' is called with
  1210. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1211. in an agenda buffer.
  1212. When t, fast selection is used by default. In this case, the prefix
  1213. argument forces cycling instead.
  1214. In all cases, the special interface is only used if access keys have actually
  1215. been assigned by the user, i.e. if keywords in the configuration are followed
  1216. by a letter in parenthesis, like TODO(t)."
  1217. :group 'org-todo
  1218. :type '(choice
  1219. (const :tag "Never" nil)
  1220. (const :tag "By default" t)
  1221. (const :tag "Only with C-u C-c C-t" prefix)))
  1222. (defcustom org-provide-todo-statistics t
  1223. "Non-nil means, update todo statistics after insert and toggle.
  1224. When this is set, todo statistics is updated in the parent of the current
  1225. entry each time a todo state is changed."
  1226. :group 'org-todo
  1227. :type 'boolean)
  1228. (defcustom org-after-todo-state-change-hook nil
  1229. "Hook which is run after the state of a TODO item was changed.
  1230. The new state (a string with a TODO keyword, or nil) is available in the
  1231. Lisp variable `state'."
  1232. :group 'org-todo
  1233. :type 'hook)
  1234. (defcustom org-log-done nil
  1235. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1236. When equal to the list (done), also prompt for a closing note.
  1237. This can also be configured on a per-file basis by adding one of
  1238. the following lines anywhere in the buffer:
  1239. #+STARTUP: logdone
  1240. #+STARTUP: lognotedone
  1241. #+STARTUP: nologdone"
  1242. :group 'org-todo
  1243. :group 'org-progress
  1244. :type '(choice
  1245. (const :tag "No logging" nil)
  1246. (const :tag "Record CLOSED timestamp" time)
  1247. (const :tag "Record CLOSED timestamp with closing note." note)))
  1248. ;; Normalize old uses of org-log-done.
  1249. (cond
  1250. ((eq org-log-done t) (setq org-log-done 'time))
  1251. ((and (listp org-log-done) (memq 'done org-log-done))
  1252. (setq org-log-done 'note)))
  1253. (defcustom org-log-note-clock-out nil
  1254. "Non-nil means, recored a note when clocking out of an item.
  1255. This can also be configured on a per-file basis by adding one of
  1256. the following lines anywhere in the buffer:
  1257. #+STARTUP: lognoteclock-out
  1258. #+STARTUP: nolognoteclock-out"
  1259. :group 'org-todo
  1260. :group 'org-progress
  1261. :type 'boolean)
  1262. (defcustom org-log-done-with-time t
  1263. "Non-nil means, the CLOSED time stamp will contain date and time.
  1264. When nil, only the date will be recorded."
  1265. :group 'org-progress
  1266. :type 'boolean)
  1267. (defcustom org-log-note-headings
  1268. '((done . "CLOSING NOTE %t")
  1269. (state . "State %-12s %t")
  1270. (note . "Note taken on %t")
  1271. (clock-out . ""))
  1272. "Headings for notes added to entries.
  1273. The value is an alist, with the car being a symbol indicating the note
  1274. context, and the cdr is the heading to be used. The heading may also be the
  1275. empty string.
  1276. %t in the heading will be replaced by a time stamp.
  1277. %s will be replaced by the new TODO state, in double quotes.
  1278. %u will be replaced by the user name.
  1279. %U will be replaced by the full user name."
  1280. :group 'org-todo
  1281. :group 'org-progress
  1282. :type '(list :greedy t
  1283. (cons (const :tag "Heading when closing an item" done) string)
  1284. (cons (const :tag
  1285. "Heading when changing todo state (todo sequence only)"
  1286. state) string)
  1287. (cons (const :tag "Heading when just taking a note" note) string)
  1288. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1289. (unless (assq 'note org-log-note-headings)
  1290. (push '(note . "%t") org-log-note-headings))
  1291. (defcustom org-log-states-order-reversed t
  1292. "Non-nil means, the latest state change note will be directly after heading.
  1293. When nil, the notes will be orderer according to time."
  1294. :group 'org-todo
  1295. :group 'org-progress
  1296. :type 'boolean)
  1297. (defcustom org-log-repeat 'time
  1298. "Non-nil means, record moving through the DONE state when triggering repeat.
  1299. An auto-repeating tasks is immediately switched back to TODO when marked
  1300. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1301. the TODO keyword definition, or recording a closing note by setting
  1302. `org-log-done', there will be no record of the task moving through DONE.
  1303. This variable forces taking a note anyway. Possible values are:
  1304. nil Don't force a record
  1305. time Record a time stamp
  1306. note Record a note
  1307. This option can also be set with on a per-file-basis with
  1308. #+STARTUP: logrepeat
  1309. #+STARTUP: lognoterepeat
  1310. #+STARTUP: nologrepeat
  1311. You can have local logging settings for a subtree by setting the LOGGING
  1312. property to one or more of these keywords."
  1313. :group 'org-todo
  1314. :group 'org-progress
  1315. :type '(choice
  1316. (const :tag "Don't force a record" nil)
  1317. (const :tag "Force recording the DONE state" time)
  1318. (const :tag "Force recording a note with the DONE state" note)))
  1319. (defgroup org-priorities nil
  1320. "Priorities in Org-mode."
  1321. :tag "Org Priorities"
  1322. :group 'org-todo)
  1323. (defcustom org-highest-priority ?A
  1324. "The highest priority of TODO items. A character like ?A, ?B etc.
  1325. Must have a smaller ASCII number than `org-lowest-priority'."
  1326. :group 'org-priorities
  1327. :type 'character)
  1328. (defcustom org-lowest-priority ?C
  1329. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1330. Must have a larger ASCII number than `org-highest-priority'."
  1331. :group 'org-priorities
  1332. :type 'character)
  1333. (defcustom org-default-priority ?B
  1334. "The default priority of TODO items.
  1335. This is the priority an item get if no explicit priority is given."
  1336. :group 'org-priorities
  1337. :type 'character)
  1338. (defcustom org-priority-start-cycle-with-default t
  1339. "Non-nil means, start with default priority when starting to cycle.
  1340. When this is nil, the first step in the cycle will be (depending on the
  1341. command used) one higher or lower that the default priority."
  1342. :group 'org-priorities
  1343. :type 'boolean)
  1344. (defgroup org-time nil
  1345. "Options concerning time stamps and deadlines in Org-mode."
  1346. :tag "Org Time"
  1347. :group 'org)
  1348. (defcustom org-insert-labeled-timestamps-at-point nil
  1349. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1350. When nil, these labeled time stamps are forces into the second line of an
  1351. entry, just after the headline. When scheduling from the global TODO list,
  1352. the time stamp will always be forced into the second line."
  1353. :group 'org-time
  1354. :type 'boolean)
  1355. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1356. "Formats for `format-time-string' which are used for time stamps.
  1357. It is not recommended to change this constant.")
  1358. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1359. "Number of minutes to round time stamps to.
  1360. These are two values, the first applies when first creating a time stamp.
  1361. The second applies when changing it with the commands `S-up' and `S-down'.
  1362. When changing the time stamp, this means that it will change in steps
  1363. of N minutes, as given by the second value.
  1364. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1365. numbers should be factors of 60, so for example 5, 10, 15.
  1366. When this is larger than 1, you can still force an exact time-stamp by using
  1367. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1368. and by using a prefix arg to `S-up/down' to specify the exact number
  1369. of minutes to shift."
  1370. :group 'org-time
  1371. :get '(lambda (var) ; Make sure all entries have 5 elements
  1372. (if (integerp (default-value var))
  1373. (list (default-value var) 5)
  1374. (default-value var)))
  1375. :type '(list
  1376. (integer :tag "when inserting times")
  1377. (integer :tag "when modifying times")))
  1378. ;; Normalize old customizations of this variable.
  1379. (when (integerp org-time-stamp-rounding-minutes)
  1380. (setq org-time-stamp-rounding-minutes
  1381. (list org-time-stamp-rounding-minutes
  1382. org-time-stamp-rounding-minutes)))
  1383. (defcustom org-display-custom-times nil
  1384. "Non-nil means, overlay custom formats over all time stamps.
  1385. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1386. To turn this on on a per-file basis, insert anywhere in the file:
  1387. #+STARTUP: customtime"
  1388. :group 'org-time
  1389. :set 'set-default
  1390. :type 'sexp)
  1391. (make-variable-buffer-local 'org-display-custom-times)
  1392. (defcustom org-time-stamp-custom-formats
  1393. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1394. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1395. These are overlayed over the default ISO format if the variable
  1396. `org-display-custom-times' is set. Time like %H:%M should be at the
  1397. end of the second format."
  1398. :group 'org-time
  1399. :type 'sexp)
  1400. (defun org-time-stamp-format (&optional long inactive)
  1401. "Get the right format for a time string."
  1402. (let ((f (if long (cdr org-time-stamp-formats)
  1403. (car org-time-stamp-formats))))
  1404. (if inactive
  1405. (concat "[" (substring f 1 -1) "]")
  1406. f)))
  1407. (defcustom org-time-clocksum-format "%d:%02d"
  1408. "The format string used when creating CLOCKSUM lines, or when
  1409. org-mode generates a time duration."
  1410. :group 'org-time
  1411. :type 'string)
  1412. (defcustom org-deadline-warning-days 14
  1413. "No. of days before expiration during which a deadline becomes active.
  1414. This variable governs the display in sparse trees and in the agenda.
  1415. When 0 or negative, it means use this number (the absolute value of it)
  1416. even if a deadline has a different individual lead time specified."
  1417. :group 'org-time
  1418. :group 'org-agenda-daily/weekly
  1419. :type 'number)
  1420. (defcustom org-read-date-prefer-future t
  1421. "Non-nil means, assume future for incomplete date input from user.
  1422. This affects the following situations:
  1423. 1. The user gives a day, but no month.
  1424. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1425. read this as the third of *next* month. However, if you enter \"17\",
  1426. it will be considered as *this* month.
  1427. 2. The user gives a month but not a year.
  1428. For example, if it is april and you enter \"feb 2\", this will be read
  1429. as feb 2, *next* year. \"May 5\", however, will be this year.
  1430. Currently this does not work for ISO week specifications.
  1431. When this option is nil, the current month and year will always be used
  1432. as defaults."
  1433. :group 'org-time
  1434. :type 'boolean)
  1435. (defcustom org-read-date-display-live t
  1436. "Non-nil means, display current interpretation of date prompt live.
  1437. This display will be in an overlay, in the minibuffer."
  1438. :group 'org-time
  1439. :type 'boolean)
  1440. (defcustom org-read-date-popup-calendar t
  1441. "Non-nil means, pop up a calendar when prompting for a date.
  1442. In the calendar, the date can be selected with mouse-1. However, the
  1443. minibuffer will also be active, and you can simply enter the date as well.
  1444. When nil, only the minibuffer will be available."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (if (fboundp 'defvaralias)
  1448. (defvaralias 'org-popup-calendar-for-date-prompt
  1449. 'org-read-date-popup-calendar))
  1450. (defcustom org-extend-today-until 0
  1451. "The hour when your day really ends.
  1452. This has influence for the following applications:
  1453. - When switching the agenda to \"today\". It it is still earlier than
  1454. the time given here, the day recognized as TODAY is actually yesterday.
  1455. - When a date is read from the user and it is still before the time given
  1456. here, the current date and time will be assumed to be yesterday, 23:59.
  1457. FIXME:
  1458. IMPORTANT: This is still a very experimental feature, it may disappear
  1459. again or it may be extended to mean more things."
  1460. :group 'org-time
  1461. :type 'number)
  1462. (defcustom org-edit-timestamp-down-means-later nil
  1463. "Non-nil means, S-down will increase the time in a time stamp.
  1464. When nil, S-up will increase."
  1465. :group 'org-time
  1466. :type 'boolean)
  1467. (defcustom org-calendar-follow-timestamp-change t
  1468. "Non-nil means, make the calendar window follow timestamp changes.
  1469. When a timestamp is modified and the calendar window is visible, it will be
  1470. moved to the new date."
  1471. :group 'org-time
  1472. :type 'boolean)
  1473. (defgroup org-tags nil
  1474. "Options concerning tags in Org-mode."
  1475. :tag "Org Tags"
  1476. :group 'org)
  1477. (defcustom org-tag-alist nil
  1478. "List of tags allowed in Org-mode files.
  1479. When this list is nil, Org-mode will base TAG input on what is already in the
  1480. buffer.
  1481. The value of this variable is an alist, the car of each entry must be a
  1482. keyword as a string, the cdr may be a character that is used to select
  1483. that tag through the fast-tag-selection interface.
  1484. See the manual for details."
  1485. :group 'org-tags
  1486. :type '(repeat
  1487. (choice
  1488. (cons (string :tag "Tag name")
  1489. (character :tag "Access char"))
  1490. (const :tag "Start radio group" (:startgroup))
  1491. (const :tag "End radio group" (:endgroup)))))
  1492. (defvar org-file-tags nil
  1493. "List of tags that can be inherited by all entries in the file.
  1494. The tags will be inherited if the variable `org-use-tag-inheritance'
  1495. says they should be.
  1496. This variable is populated from #+TAG lines.")
  1497. (defcustom org-use-fast-tag-selection 'auto
  1498. "Non-nil means, use fast tag selection scheme.
  1499. This is a special interface to select and deselect tags with single keys.
  1500. When nil, fast selection is never used.
  1501. When the symbol `auto', fast selection is used if and only if selection
  1502. characters for tags have been configured, either through the variable
  1503. `org-tag-alist' or through a #+TAGS line in the buffer.
  1504. When t, fast selection is always used and selection keys are assigned
  1505. automatically if necessary."
  1506. :group 'org-tags
  1507. :type '(choice
  1508. (const :tag "Always" t)
  1509. (const :tag "Never" nil)
  1510. (const :tag "When selection characters are configured" 'auto)))
  1511. (defcustom org-fast-tag-selection-single-key nil
  1512. "Non-nil means, fast tag selection exits after first change.
  1513. When nil, you have to press RET to exit it.
  1514. During fast tag selection, you can toggle this flag with `C-c'.
  1515. This variable can also have the value `expert'. In this case, the window
  1516. displaying the tags menu is not even shown, until you press C-c again."
  1517. :group 'org-tags
  1518. :type '(choice
  1519. (const :tag "No" nil)
  1520. (const :tag "Yes" t)
  1521. (const :tag "Expert" expert)))
  1522. (defvar org-fast-tag-selection-include-todo nil
  1523. "Non-nil means, fast tags selection interface will also offer TODO states.
  1524. This is an undocumented feature, you should not rely on it.")
  1525. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1526. "The column to which tags should be indented in a headline.
  1527. If this number is positive, it specifies the column. If it is negative,
  1528. it means that the tags should be flushright to that column. For example,
  1529. -80 works well for a normal 80 character screen."
  1530. :group 'org-tags
  1531. :type 'integer)
  1532. (defcustom org-auto-align-tags t
  1533. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1534. These operations change the length of a headline and therefore shift
  1535. the tags around. With this options turned on, after each such operation
  1536. the tags are again aligned to `org-tags-column'."
  1537. :group 'org-tags
  1538. :type 'boolean)
  1539. (defcustom org-use-tag-inheritance t
  1540. "Non-nil means, tags in levels apply also for sublevels.
  1541. When nil, only the tags directly given in a specific line apply there.
  1542. If this option is t, a match early-on in a tree can lead to a large
  1543. number of matches in the subtree. If you only want to see the first
  1544. match in a tree during a search, check out the variable
  1545. `org-tags-match-list-sublevels'.
  1546. This may also be a list of tags that should be inherited, or a regexp that
  1547. matches tags that should be inherited."
  1548. :group 'org-tags
  1549. :type '(choice
  1550. (const :tag "Not" nil)
  1551. (const :tag "Always" t)
  1552. (repeat :tag "Specific tags" (string :tag "Tag"))
  1553. (regexp :tag "Tags matched by regexp")))
  1554. (defun org-tag-inherit-p (tag)
  1555. "Check if TAG is one that should be inherited."
  1556. (cond
  1557. ((eq org-use-tag-inheritance t) t)
  1558. ((not org-use-tag-inheritance) nil)
  1559. ((stringp org-use-tag-inheritance)
  1560. (string-match org-use-tag-inheritance tag))
  1561. ((listp org-use-tag-inheritance)
  1562. (member tag org-use-tag-inheritance))
  1563. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1564. (defcustom org-tags-match-list-sublevels t
  1565. "Non-nil means list also sublevels of headlines matching tag search.
  1566. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1567. the sublevels of a headline matching a tag search often also match
  1568. the same search. Listing all of them can create very long lists.
  1569. Setting this variable to nil causes subtrees of a match to be skipped.
  1570. This option is off by default, because inheritance in on. If you turn
  1571. inheritance off, you very likely want to turn this option on.
  1572. As a special case, if the tag search is restricted to TODO items, the
  1573. value of this variable is ignored and sublevels are always checked, to
  1574. make sure all corresponding TODO items find their way into the list."
  1575. :group 'org-tags
  1576. :type 'boolean)
  1577. (defvar org-tags-history nil
  1578. "History of minibuffer reads for tags.")
  1579. (defvar org-last-tags-completion-table nil
  1580. "The last used completion table for tags.")
  1581. (defvar org-after-tags-change-hook nil
  1582. "Hook that is run after the tags in a line have changed.")
  1583. (defgroup org-properties nil
  1584. "Options concerning properties in Org-mode."
  1585. :tag "Org Properties"
  1586. :group 'org)
  1587. (defcustom org-property-format "%-10s %s"
  1588. "How property key/value pairs should be formatted by `indent-line'.
  1589. When `indent-line' hits a property definition, it will format the line
  1590. according to this format, mainly to make sure that the values are
  1591. lined-up with respect to each other."
  1592. :group 'org-properties
  1593. :type 'string)
  1594. (defcustom org-use-property-inheritance nil
  1595. "Non-nil means, properties apply also for sublevels.
  1596. This setting is chiefly used during property searches. Turning it on can
  1597. cause significant overhead when doing a search, which is why it is not
  1598. on by default.
  1599. When nil, only the properties directly given in the current entry count.
  1600. When t, every property is inherited. The value may also be a list of
  1601. properties that should have inheritance, or a regular expression matching
  1602. properties that should be inherited.
  1603. However, note that some special properties use inheritance under special
  1604. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1605. and the properties ending in \"_ALL\" when they are used as descriptor
  1606. for valid values of a property.
  1607. Note for programmers:
  1608. When querying an entry with `org-entry-get', you can control if inheritance
  1609. should be used. By default, `org-entry-get' looks only at the local
  1610. properties. You can request inheritance by setting the inherit argument
  1611. to t (to force inheritance) or to `selective' (to respect the setting
  1612. in this variable)."
  1613. :group 'org-properties
  1614. :type '(choice
  1615. (const :tag "Not" nil)
  1616. (const :tag "Always" t)
  1617. (repeat :tag "Specific properties" (string :tag "Property"))
  1618. (regexp :tag "Properties matched by regexp")))
  1619. (defun org-property-inherit-p (property)
  1620. "Check if PROPERTY is one that should be inherited."
  1621. (cond
  1622. ((eq org-use-property-inheritance t) t)
  1623. ((not org-use-property-inheritance) nil)
  1624. ((stringp org-use-property-inheritance)
  1625. (string-match org-use-property-inheritance property))
  1626. ((listp org-use-property-inheritance)
  1627. (member property org-use-property-inheritance))
  1628. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1629. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1630. "The default column format, if no other format has been defined.
  1631. This variable can be set on the per-file basis by inserting a line
  1632. #+COLUMNS: %25ITEM ....."
  1633. :group 'org-properties
  1634. :type 'string)
  1635. (defcustom org-effort-property "Effort"
  1636. "The property that is being used to keep track of effort estimates.
  1637. Effort estimates given in this property need to have the format H:MM."
  1638. :group 'org-properties
  1639. :group 'org-progress
  1640. :type '(string :tag "Property"))
  1641. (defconst org-global-properties-fixed
  1642. '(("VISIBILITY_ALL" . "folded children content all"))
  1643. "List of property/value pairs that can be inherited by any entry.
  1644. These are fixed values, for the preset properties.")
  1645. (defcustom org-global-properties nil
  1646. "List of property/value pairs that can be inherited by any entry.
  1647. You can set buffer-local values for this by adding lines like
  1648. #+PROPERTY: NAME VALUE"
  1649. :group 'org-properties
  1650. :type '(repeat
  1651. (cons (string :tag "Property")
  1652. (string :tag "Value"))))
  1653. (defvar org-file-properties nil
  1654. "List of property/value pairs that can be inherited by any entry.
  1655. Valid for the current buffer.
  1656. This variable is populated from #+PROPERTY lines.")
  1657. (defgroup org-agenda nil
  1658. "Options concerning agenda views in Org-mode."
  1659. :tag "Org Agenda"
  1660. :group 'org)
  1661. (defvar org-category nil
  1662. "Variable used by org files to set a category for agenda display.
  1663. Such files should use a file variable to set it, for example
  1664. # -*- mode: org; org-category: \"ELisp\"
  1665. or contain a special line
  1666. #+CATEGORY: ELisp
  1667. If the file does not specify a category, then file's base name
  1668. is used instead.")
  1669. (make-variable-buffer-local 'org-category)
  1670. (defcustom org-agenda-files nil
  1671. "The files to be used for agenda display.
  1672. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1673. \\[org-remove-file]. You can also use customize to edit the list.
  1674. If an entry is a directory, all files in that directory that are matched by
  1675. `org-agenda-file-regexp' will be part of the file list.
  1676. If the value of the variable is not a list but a single file name, then
  1677. the list of agenda files is actually stored and maintained in that file, one
  1678. agenda file per line."
  1679. :group 'org-agenda
  1680. :type '(choice
  1681. (repeat :tag "List of files and directories" file)
  1682. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1683. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1684. "Regular expression to match files for `org-agenda-files'.
  1685. If any element in the list in that variable contains a directory instead
  1686. of a normal file, all files in that directory that are matched by this
  1687. regular expression will be included."
  1688. :group 'org-agenda
  1689. :type 'regexp)
  1690. (defcustom org-agenda-text-search-extra-files nil
  1691. "List of extra files to be searched by text search commands.
  1692. These files will be search in addition to the agenda files by the
  1693. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1694. Note that these files will only be searched for text search commands,
  1695. not for the other agenda views like todo lists, tag searches or the weekly
  1696. agenda. This variable is intended to list notes and possibly archive files
  1697. that should also be searched by these two commands.
  1698. In fact, if the first element in the list is the symbol `agenda-archives',
  1699. than all archive files of all agenda files will be added to the search
  1700. scope."
  1701. :group 'org-agenda
  1702. :type '(set :greedy t
  1703. (const :tag "Agenda Archives" agenda-archives)
  1704. (repeat :inline t (file))))
  1705. (if (fboundp 'defvaralias)
  1706. (defvaralias 'org-agenda-multi-occur-extra-files
  1707. 'org-agenda-text-search-extra-files))
  1708. (defcustom org-agenda-skip-unavailable-files nil
  1709. "t means to just skip non-reachable files in `org-agenda-files'.
  1710. Nil means to remove them, after a query, from the list."
  1711. :group 'org-agenda
  1712. :type 'boolean)
  1713. (defcustom org-calendar-to-agenda-key [?c]
  1714. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1715. The command `org-calendar-goto-agenda' will be bound to this key. The
  1716. default is the character `c' because then `c' can be used to switch back and
  1717. forth between agenda and calendar."
  1718. :group 'org-agenda
  1719. :type 'sexp)
  1720. (eval-after-load "calendar"
  1721. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1722. 'org-calendar-goto-agenda))
  1723. (defgroup org-latex nil
  1724. "Options for embedding LaTeX code into Org-mode."
  1725. :tag "Org LaTeX"
  1726. :group 'org)
  1727. (defcustom org-format-latex-options
  1728. '(:foreground default :background default :scale 1.0
  1729. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1730. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1731. "Options for creating images from LaTeX fragments.
  1732. This is a property list with the following properties:
  1733. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1734. `default' means use the foreground of the default face.
  1735. :background the background color, or \"Transparent\".
  1736. `default' means use the background of the default face.
  1737. :scale a scaling factor for the size of the images.
  1738. :html-foreground, :html-background, :html-scale
  1739. the same numbers for HTML export.
  1740. :matchers a list indicating which matchers should be used to
  1741. find LaTeX fragments. Valid members of this list are:
  1742. \"begin\" find environments
  1743. \"$\" find math expressions surrounded by $...$
  1744. \"$$\" find math expressions surrounded by $$....$$
  1745. \"\\(\" find math expressions surrounded by \\(...\\)
  1746. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1747. :group 'org-latex
  1748. :type 'plist)
  1749. (defcustom org-format-latex-header "\\documentclass{article}
  1750. \\usepackage{fullpage} % do not remove
  1751. \\usepackage{amssymb}
  1752. \\usepackage[usenames]{color}
  1753. \\usepackage{amsmath}
  1754. \\usepackage{latexsym}
  1755. \\usepackage[mathscr]{eucal}
  1756. \\pagestyle{empty} % do not remove"
  1757. "The document header used for processing LaTeX fragments."
  1758. :group 'org-latex
  1759. :type 'string)
  1760. (defgroup org-font-lock nil
  1761. "Font-lock settings for highlighting in Org-mode."
  1762. :tag "Org Font Lock"
  1763. :group 'org)
  1764. (defcustom org-level-color-stars-only nil
  1765. "Non-nil means fontify only the stars in each headline.
  1766. When nil, the entire headline is fontified.
  1767. Changing it requires restart of `font-lock-mode' to become effective
  1768. also in regions already fontified."
  1769. :group 'org-font-lock
  1770. :type 'boolean)
  1771. (defcustom org-hide-leading-stars nil
  1772. "Non-nil means, hide the first N-1 stars in a headline.
  1773. This works by using the face `org-hide' for these stars. This
  1774. face is white for a light background, and black for a dark
  1775. background. You may have to customize the face `org-hide' to
  1776. make this work.
  1777. Changing it requires restart of `font-lock-mode' to become effective
  1778. also in regions already fontified.
  1779. You may also set this on a per-file basis by adding one of the following
  1780. lines to the buffer:
  1781. #+STARTUP: hidestars
  1782. #+STARTUP: showstars"
  1783. :group 'org-font-lock
  1784. :type 'boolean)
  1785. (defcustom org-fontify-done-headline nil
  1786. "Non-nil means, change the face of a headline if it is marked DONE.
  1787. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1788. When this is non-nil, the headline after the keyword is set to the
  1789. `org-headline-done' as an additional indication."
  1790. :group 'org-font-lock
  1791. :type 'boolean)
  1792. (defcustom org-fontify-emphasized-text t
  1793. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1794. Changing this variable requires a restart of Emacs to take effect."
  1795. :group 'org-font-lock
  1796. :type 'boolean)
  1797. (defcustom org-highlight-latex-fragments-and-specials nil
  1798. "Non-nil means, fontify what is treated specially by the exporters."
  1799. :group 'org-font-lock
  1800. :type 'boolean)
  1801. (defcustom org-hide-emphasis-markers nil
  1802. "Non-nil mean font-lock should hide the emphasis marker characters."
  1803. :group 'org-font-lock
  1804. :type 'boolean)
  1805. (defvar org-emph-re nil
  1806. "Regular expression for matching emphasis.")
  1807. (defvar org-verbatim-re nil
  1808. "Regular expression for matching verbatim text.")
  1809. (defvar org-emphasis-regexp-components) ; defined just below
  1810. (defvar org-emphasis-alist) ; defined just below
  1811. (defun org-set-emph-re (var val)
  1812. "Set variable and compute the emphasis regular expression."
  1813. (set var val)
  1814. (when (and (boundp 'org-emphasis-alist)
  1815. (boundp 'org-emphasis-regexp-components)
  1816. org-emphasis-alist org-emphasis-regexp-components)
  1817. (let* ((e org-emphasis-regexp-components)
  1818. (pre (car e))
  1819. (post (nth 1 e))
  1820. (border (nth 2 e))
  1821. (body (nth 3 e))
  1822. (nl (nth 4 e))
  1823. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1824. (body1 (concat body "*?"))
  1825. (markers (mapconcat 'car org-emphasis-alist ""))
  1826. (vmarkers (mapconcat
  1827. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1828. org-emphasis-alist "")))
  1829. ;; make sure special characters appear at the right position in the class
  1830. (if (string-match "\\^" markers)
  1831. (setq markers (concat (replace-match "" t t markers) "^")))
  1832. (if (string-match "-" markers)
  1833. (setq markers (concat (replace-match "" t t markers) "-")))
  1834. (if (string-match "\\^" vmarkers)
  1835. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1836. (if (string-match "-" vmarkers)
  1837. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1838. (if (> nl 0)
  1839. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1840. (int-to-string nl) "\\}")))
  1841. ;; Make the regexp
  1842. (setq org-emph-re
  1843. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1844. "\\("
  1845. "\\([" markers "]\\)"
  1846. "\\("
  1847. "[^" border "]\\|"
  1848. "[^" border (if (and nil stacked) markers) "]"
  1849. body1
  1850. "[^" border (if (and nil stacked) markers) "]"
  1851. "\\)"
  1852. "\\3\\)"
  1853. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1854. (setq org-verbatim-re
  1855. (concat "\\([" pre "]\\|^\\)"
  1856. "\\("
  1857. "\\([" vmarkers "]\\)"
  1858. "\\("
  1859. "[^" border "]\\|"
  1860. "[^" border "]"
  1861. body1
  1862. "[^" border "]"
  1863. "\\)"
  1864. "\\3\\)"
  1865. "\\([" post "]\\|$\\)")))))
  1866. (defcustom org-emphasis-regexp-components
  1867. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1868. "Components used to build the regular expression for emphasis.
  1869. This is a list with 6 entries. Terminology: In an emphasis string
  1870. like \" *strong word* \", we call the initial space PREMATCH, the final
  1871. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1872. and \"trong wor\" is the body. The different components in this variable
  1873. specify what is allowed/forbidden in each part:
  1874. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1875. post Chars allowed as postmatch. End of line will be allowed too.
  1876. border The chars *forbidden* as border characters.
  1877. body-regexp A regexp like \".\" to match a body character. Don't use
  1878. non-shy groups here, and don't allow newline here.
  1879. newline The maximum number of newlines allowed in an emphasis exp.
  1880. Use customize to modify this, or restart Emacs after changing it."
  1881. :group 'org-font-lock
  1882. :set 'org-set-emph-re
  1883. :type '(list
  1884. (sexp :tag "Allowed chars in pre ")
  1885. (sexp :tag "Allowed chars in post ")
  1886. (sexp :tag "Forbidden chars in border ")
  1887. (sexp :tag "Regexp for body ")
  1888. (integer :tag "number of newlines allowed")
  1889. (option (boolean :tag "Stacking (DISABLED) "))))
  1890. (defcustom org-emphasis-alist
  1891. `(("*" bold "<b>" "</b>")
  1892. ("/" italic "<i>" "</i>")
  1893. ("_" underline "<u>" "</u>")
  1894. ("=" org-code "<code>" "</code>" verbatim)
  1895. ("~" org-verbatim "" "" verbatim)
  1896. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1897. "<del>" "</del>")
  1898. )
  1899. "Special syntax for emphasized text.
  1900. Text starting and ending with a special character will be emphasized, for
  1901. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1902. characters, the face to be used by font-lock for highlighting in Org-mode
  1903. Emacs buffers, and the HTML tags to be used for this.
  1904. Use customize to modify this, or restart Emacs after changing it."
  1905. :group 'org-font-lock
  1906. :set 'org-set-emph-re
  1907. :type '(repeat
  1908. (list
  1909. (string :tag "Marker character")
  1910. (choice
  1911. (face :tag "Font-lock-face")
  1912. (plist :tag "Face property list"))
  1913. (string :tag "HTML start tag")
  1914. (string :tag "HTML end tag")
  1915. (option (const verbatim)))))
  1916. ;;; Miscellaneous options
  1917. (defgroup org-completion nil
  1918. "Completion in Org-mode."
  1919. :tag "Org Completion"
  1920. :group 'org)
  1921. (defcustom org-completion-fallback-command 'hippie-expand
  1922. "The expansion command called by \\[org-complete] in normal context.
  1923. Normal means, no org-mode-specific context."
  1924. :group 'org-completion
  1925. :type 'function)
  1926. ;;; Functions and variables from ther packages
  1927. ;; Declared here to avoid compiler warnings
  1928. ;; XEmacs only
  1929. (defvar outline-mode-menu-heading)
  1930. (defvar outline-mode-menu-show)
  1931. (defvar outline-mode-menu-hide)
  1932. (defvar zmacs-regions) ; XEmacs regions
  1933. ;; Emacs only
  1934. (defvar mark-active)
  1935. ;; Various packages
  1936. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1937. (declare-function calendar-forward-day "cal-move" (arg))
  1938. (declare-function calendar-goto-date "cal-move" (date))
  1939. (declare-function calendar-goto-today "cal-move" ())
  1940. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1941. (defvar calc-embedded-close-formula)
  1942. (defvar calc-embedded-open-formula)
  1943. (declare-function cdlatex-tab "ext:cdlatex" ())
  1944. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1945. (defvar font-lock-unfontify-region-function)
  1946. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1947. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1948. (defvar iswitchb-temp-buflist)
  1949. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1950. (declare-function org-agenda-skip "org-agenda" ())
  1951. (declare-function org-format-agenda-item "org-agenda"
  1952. (extra txt &optional category tags dotime noprefix remove-re))
  1953. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1954. (declare-function org-agenda-change-all-lines "org-agenda"
  1955. (newhead hdmarker &optional fixface))
  1956. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1957. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1958. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1959. (beg end))
  1960. (declare-function parse-time-string "parse-time" (string))
  1961. (declare-function remember "remember" (&optional initial))
  1962. (declare-function remember-buffer-desc "remember" ())
  1963. (declare-function remember-finalize "remember" ())
  1964. (defvar remember-save-after-remembering)
  1965. (defvar remember-data-file)
  1966. (defvar remember-register)
  1967. (defvar remember-buffer)
  1968. (defvar remember-handler-functions)
  1969. (defvar remember-annotation-functions)
  1970. (defvar texmathp-why)
  1971. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1972. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1973. (defvar w3m-current-url)
  1974. (defvar w3m-current-title)
  1975. (defvar org-latex-regexps)
  1976. ;;; Autoload and prepare some org modules
  1977. ;; Some table stuff that needs to be defined here, because it is used
  1978. ;; by the functions setting up org-mode or checking for table context.
  1979. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1980. "Detects an org-type or table-type table.")
  1981. (defconst org-table-line-regexp "^[ \t]*|"
  1982. "Detects an org-type table line.")
  1983. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1984. "Detects an org-type table line.")
  1985. (defconst org-table-hline-regexp "^[ \t]*|-"
  1986. "Detects an org-type table hline.")
  1987. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1988. "Detects a table-type table hline.")
  1989. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1990. "Searching from within a table (any type) this finds the first line
  1991. outside the table.")
  1992. ;; Autoload the functions in org-table.el that are needed by functions here.
  1993. (eval-and-compile
  1994. (org-autoload "org-table"
  1995. '(org-table-align org-table-begin org-table-blank-field
  1996. org-table-convert org-table-convert-region org-table-copy-down
  1997. org-table-copy-region org-table-create
  1998. org-table-create-or-convert-from-region
  1999. org-table-create-with-table.el org-table-current-dline
  2000. org-table-cut-region org-table-delete-column org-table-edit-field
  2001. org-table-edit-formulas org-table-end org-table-eval-formula
  2002. org-table-export org-table-field-info
  2003. org-table-get-stored-formulas org-table-goto-column
  2004. org-table-hline-and-move org-table-import org-table-insert-column
  2005. org-table-insert-hline org-table-insert-row org-table-iterate
  2006. org-table-justify-field-maybe org-table-kill-row
  2007. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2008. org-table-move-column org-table-move-column-left
  2009. org-table-move-column-right org-table-move-row
  2010. org-table-move-row-down org-table-move-row-up
  2011. org-table-next-field org-table-next-row org-table-paste-rectangle
  2012. org-table-previous-field org-table-recalculate
  2013. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2014. org-table-toggle-coordinate-overlays
  2015. org-table-toggle-formula-debugger org-table-wrap-region
  2016. orgtbl-mode turn-on-orgtbl)))
  2017. (defun org-at-table-p (&optional table-type)
  2018. "Return t if the cursor is inside an org-type table.
  2019. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2020. (if org-enable-table-editor
  2021. (save-excursion
  2022. (beginning-of-line 1)
  2023. (looking-at (if table-type org-table-any-line-regexp
  2024. org-table-line-regexp)))
  2025. nil))
  2026. (defsubst org-table-p () (org-at-table-p))
  2027. (defun org-at-table.el-p ()
  2028. "Return t if and only if we are at a table.el table."
  2029. (and (org-at-table-p 'any)
  2030. (save-excursion
  2031. (goto-char (org-table-begin 'any))
  2032. (looking-at org-table1-hline-regexp))))
  2033. (defun org-table-recognize-table.el ()
  2034. "If there is a table.el table nearby, recognize it and move into it."
  2035. (if org-table-tab-recognizes-table.el
  2036. (if (org-at-table.el-p)
  2037. (progn
  2038. (beginning-of-line 1)
  2039. (if (looking-at org-table-dataline-regexp)
  2040. nil
  2041. (if (looking-at org-table1-hline-regexp)
  2042. (progn
  2043. (beginning-of-line 2)
  2044. (if (looking-at org-table-any-border-regexp)
  2045. (beginning-of-line -1)))))
  2046. (if (re-search-forward "|" (org-table-end t) t)
  2047. (progn
  2048. (require 'table)
  2049. (if (table--at-cell-p (point))
  2050. t
  2051. (message "recognizing table.el table...")
  2052. (table-recognize-table)
  2053. (message "recognizing table.el table...done")))
  2054. (error "This should not happen..."))
  2055. t)
  2056. nil)
  2057. nil))
  2058. (defun org-at-table-hline-p ()
  2059. "Return t if the cursor is inside a hline in a table."
  2060. (if org-enable-table-editor
  2061. (save-excursion
  2062. (beginning-of-line 1)
  2063. (looking-at org-table-hline-regexp))
  2064. nil))
  2065. (defvar org-table-clean-did-remove-column nil)
  2066. (defun org-table-map-tables (function)
  2067. "Apply FUNCTION to the start of all tables in the buffer."
  2068. (save-excursion
  2069. (save-restriction
  2070. (widen)
  2071. (goto-char (point-min))
  2072. (while (re-search-forward org-table-any-line-regexp nil t)
  2073. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2074. (beginning-of-line 1)
  2075. (if (looking-at org-table-line-regexp)
  2076. (save-excursion (funcall function)))
  2077. (re-search-forward org-table-any-border-regexp nil 1))))
  2078. (message "Mapping tables: done"))
  2079. ;; Declare and autoload functions from org-exp.el
  2080. (declare-function org-default-export-plist "org-exp")
  2081. (declare-function org-infile-export-plist "org-exp")
  2082. (declare-function org-get-current-options "org-exp")
  2083. (eval-and-compile
  2084. (org-autoload "org-exp"
  2085. '(org-export org-export-as-ascii org-export-visible
  2086. org-insert-export-options-template org-export-as-html-and-open
  2087. org-export-as-html-batch org-export-as-html-to-buffer
  2088. org-replace-region-by-html org-export-region-as-html
  2089. org-export-as-html org-export-icalendar-this-file
  2090. org-export-icalendar-all-agenda-files
  2091. org-table-clean-before-export
  2092. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2093. ;; Declare and autoload functions from org-exp.el
  2094. (eval-and-compile
  2095. (org-autoload "org-exp"
  2096. '(org-agenda org-agenda-list org-search-view
  2097. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2098. org-diary org-agenda-to-appt)))
  2099. ;; Autoload org-remember
  2100. (eval-and-compile
  2101. (org-autoload "org-remember"
  2102. '(org-remember-insinuate org-remember-annotation
  2103. org-remember-apply-template org-remember org-remember-handler)))
  2104. ;; Autoload org-clock.el
  2105. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2106. (beg end))
  2107. (declare-function org-update-mode-line "org-clock" ())
  2108. (defvar org-clock-start-time)
  2109. (defvar org-clock-marker (make-marker)
  2110. "Marker recording the last clock-in.")
  2111. (eval-and-compile
  2112. (org-autoload
  2113. "org-clock"
  2114. '(org-clock-in org-clock-out org-clock-cancel
  2115. org-clock-goto org-clock-sum org-clock-display
  2116. org-remove-clock-overlays org-clock-report
  2117. org-clocktable-shift org-dblock-write:clocktable
  2118. org-get-clocktable)))
  2119. (defun org-clock-update-time-maybe ()
  2120. "If this is a CLOCK line, update it and return t.
  2121. Otherwise, return nil."
  2122. (interactive)
  2123. (save-excursion
  2124. (beginning-of-line 1)
  2125. (skip-chars-forward " \t")
  2126. (when (looking-at org-clock-string)
  2127. (let ((re (concat "[ \t]*" org-clock-string
  2128. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2129. "\\([ \t]*=>.*\\)?\\)?"))
  2130. ts te h m s)
  2131. (cond
  2132. ((not (looking-at re))
  2133. nil)
  2134. ((not (match-end 2))
  2135. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2136. (> org-clock-marker (point))
  2137. (<= org-clock-marker (point-at-eol)))
  2138. ;; The clock is running here
  2139. (setq org-clock-start-time
  2140. (apply 'encode-time
  2141. (org-parse-time-string (match-string 1))))
  2142. (org-update-mode-line)))
  2143. (t
  2144. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2145. (end-of-line 1)
  2146. (setq ts (match-string 1)
  2147. te (match-string 3))
  2148. (setq s (- (time-to-seconds
  2149. (apply 'encode-time (org-parse-time-string te)))
  2150. (time-to-seconds
  2151. (apply 'encode-time (org-parse-time-string ts))))
  2152. h (floor (/ s 3600))
  2153. s (- s (* 3600 h))
  2154. m (floor (/ s 60))
  2155. s (- s (* 60 s)))
  2156. (insert " => " (format "%2d:%02d" h m))
  2157. t))))))
  2158. (defun org-check-running-clock ()
  2159. "Check if the current buffer contains the running clock.
  2160. If yes, offer to stop it and to save the buffer with the changes."
  2161. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2162. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2163. (buffer-name))))
  2164. (org-clock-out)
  2165. (when (y-or-n-p "Save changed buffer?")
  2166. (save-buffer))))
  2167. (defun org-clocktable-try-shift (dir n)
  2168. "Check if this line starts a clock table, if yes, shift the time block."
  2169. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2170. (org-clocktable-shift dir n)))
  2171. ;; Autoload archiving code
  2172. ;; The stuff that is needed for cycling and tags has to be defined here.
  2173. (defgroup org-archive nil
  2174. "Options concerning archiving in Org-mode."
  2175. :tag "Org Archive"
  2176. :group 'org-structure)
  2177. (defcustom org-archive-location "%s_archive::"
  2178. "The location where subtrees should be archived.
  2179. Otherwise, the value of this variable is a string, consisting of two
  2180. parts, separated by a double-colon.
  2181. The first part is a file name - when omitted, archiving happens in the same
  2182. file. %s will be replaced by the current file name (without directory part).
  2183. Archiving to a different file is useful to keep archived entries from
  2184. contributing to the Org-mode Agenda.
  2185. The part after the double colon is a headline. The archived entries will be
  2186. filed under that headline. When omitted, the subtrees are simply filed away
  2187. at the end of the file, as top-level entries.
  2188. Here are a few examples:
  2189. \"%s_archive::\"
  2190. If the current file is Projects.org, archive in file
  2191. Projects.org_archive, as top-level trees. This is the default.
  2192. \"::* Archived Tasks\"
  2193. Archive in the current file, under the top-level headline
  2194. \"* Archived Tasks\".
  2195. \"~/org/archive.org::\"
  2196. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2197. \"basement::** Finished Tasks\"
  2198. Archive in file ./basement (relative path), as level 3 trees
  2199. below the level 2 heading \"** Finished Tasks\".
  2200. You may set this option on a per-file basis by adding to the buffer a
  2201. line like
  2202. #+ARCHIVE: basement::** Finished Tasks
  2203. You may also define it locally for a subtree by setting an ARCHIVE property
  2204. in the entry. If such a property is found in an entry, or anywhere up
  2205. the hierarchy, it will be used."
  2206. :group 'org-archive
  2207. :type 'string)
  2208. (defcustom org-archive-tag "ARCHIVE"
  2209. "The tag that marks a subtree as archived.
  2210. An archived subtree does not open during visibility cycling, and does
  2211. not contribute to the agenda listings.
  2212. After changing this, font-lock must be restarted in the relevant buffers to
  2213. get the proper fontification."
  2214. :group 'org-archive
  2215. :group 'org-keywords
  2216. :type 'string)
  2217. (defcustom org-agenda-skip-archived-trees t
  2218. "Non-nil means, the agenda will skip any items located in archived trees.
  2219. An archived tree is a tree marked with the tag ARCHIVE."
  2220. :group 'org-archive
  2221. :group 'org-agenda-skip
  2222. :type 'boolean)
  2223. (defcustom org-cycle-open-archived-trees nil
  2224. "Non-nil means, `org-cycle' will open archived trees.
  2225. An archived tree is a tree marked with the tag ARCHIVE.
  2226. When nil, archived trees will stay folded. You can still open them with
  2227. normal outline commands like `show-all', but not with the cycling commands."
  2228. :group 'org-archive
  2229. :group 'org-cycle
  2230. :type 'boolean)
  2231. (defcustom org-sparse-tree-open-archived-trees nil
  2232. "Non-nil means sparse tree construction shows matches in archived trees.
  2233. When nil, matches in these trees are highlighted, but the trees are kept in
  2234. collapsed state."
  2235. :group 'org-archive
  2236. :group 'org-sparse-trees
  2237. :type 'boolean)
  2238. (defun org-cycle-hide-archived-subtrees (state)
  2239. "Re-hide all archived subtrees after a visibility state change."
  2240. (when (and (not org-cycle-open-archived-trees)
  2241. (not (memq state '(overview folded))))
  2242. (save-excursion
  2243. (let* ((globalp (memq state '(contents all)))
  2244. (beg (if globalp (point-min) (point)))
  2245. (end (if globalp (point-max) (org-end-of-subtree t))))
  2246. (org-hide-archived-subtrees beg end)
  2247. (goto-char beg)
  2248. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2249. (message "%s" (substitute-command-keys
  2250. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2251. (defun org-force-cycle-archived ()
  2252. "Cycle subtree even if it is archived."
  2253. (interactive)
  2254. (setq this-command 'org-cycle)
  2255. (let ((org-cycle-open-archived-trees t))
  2256. (call-interactively 'org-cycle)))
  2257. (defun org-hide-archived-subtrees (beg end)
  2258. "Re-hide all archived subtrees after a visibility state change."
  2259. (save-excursion
  2260. (let* ((re (concat ":" org-archive-tag ":")))
  2261. (goto-char beg)
  2262. (while (re-search-forward re end t)
  2263. (and (org-on-heading-p) (hide-subtree))
  2264. (org-end-of-subtree t)))))
  2265. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2266. (eval-and-compile
  2267. (org-autoload "org-archive"
  2268. '(org-add-archive-files org-archive-subtree
  2269. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2270. ;; Autoload Column View Code
  2271. (declare-function org-columns-number-to-string "org-colview")
  2272. (declare-function org-columns-get-format-and-top-level "org-colview")
  2273. (declare-function org-columns-compute "org-colview")
  2274. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2275. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2276. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2277. org-columns org-insert-columns-dblock))
  2278. ;; Autoload ID code
  2279. (org-autoload "org-id"
  2280. '(org-id-get-create org-id-new org-id-copy org-id-get
  2281. org-id-get-with-outline-path-completion
  2282. org-id-get-with-outline-drilling
  2283. org-id-goto org-id-find))
  2284. ;;; Variables for pre-computed regular expressions, all buffer local
  2285. (defvar org-drawer-regexp nil
  2286. "Matches first line of a hidden block.")
  2287. (make-variable-buffer-local 'org-drawer-regexp)
  2288. (defvar org-todo-regexp nil
  2289. "Matches any of the TODO state keywords.")
  2290. (make-variable-buffer-local 'org-todo-regexp)
  2291. (defvar org-not-done-regexp nil
  2292. "Matches any of the TODO state keywords except the last one.")
  2293. (make-variable-buffer-local 'org-not-done-regexp)
  2294. (defvar org-todo-line-regexp nil
  2295. "Matches a headline and puts TODO state into group 2 if present.")
  2296. (make-variable-buffer-local 'org-todo-line-regexp)
  2297. (defvar org-complex-heading-regexp nil
  2298. "Matches a headline and puts everything into groups:
  2299. group 1: the stars
  2300. group 2: The todo keyword, maybe
  2301. group 3: Priority cookie
  2302. group 4: True headline
  2303. group 5: Tags")
  2304. (make-variable-buffer-local 'org-complex-heading-regexp)
  2305. (defvar org-todo-line-tags-regexp nil
  2306. "Matches a headline and puts TODO state into group 2 if present.
  2307. Also put tags into group 4 if tags are present.")
  2308. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2309. (defvar org-nl-done-regexp nil
  2310. "Matches newline followed by a headline with the DONE keyword.")
  2311. (make-variable-buffer-local 'org-nl-done-regexp)
  2312. (defvar org-looking-at-done-regexp nil
  2313. "Matches the DONE keyword a point.")
  2314. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2315. (defvar org-ds-keyword-length 12
  2316. "Maximum length of the Deadline and SCHEDULED keywords.")
  2317. (make-variable-buffer-local 'org-ds-keyword-length)
  2318. (defvar org-deadline-regexp nil
  2319. "Matches the DEADLINE keyword.")
  2320. (make-variable-buffer-local 'org-deadline-regexp)
  2321. (defvar org-deadline-time-regexp nil
  2322. "Matches the DEADLINE keyword together with a time stamp.")
  2323. (make-variable-buffer-local 'org-deadline-time-regexp)
  2324. (defvar org-deadline-line-regexp nil
  2325. "Matches the DEADLINE keyword and the rest of the line.")
  2326. (make-variable-buffer-local 'org-deadline-line-regexp)
  2327. (defvar org-scheduled-regexp nil
  2328. "Matches the SCHEDULED keyword.")
  2329. (make-variable-buffer-local 'org-scheduled-regexp)
  2330. (defvar org-scheduled-time-regexp nil
  2331. "Matches the SCHEDULED keyword together with a time stamp.")
  2332. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2333. (defvar org-closed-time-regexp nil
  2334. "Matches the CLOSED keyword together with a time stamp.")
  2335. (make-variable-buffer-local 'org-closed-time-regexp)
  2336. (defvar org-keyword-time-regexp nil
  2337. "Matches any of the 4 keywords, together with the time stamp.")
  2338. (make-variable-buffer-local 'org-keyword-time-regexp)
  2339. (defvar org-keyword-time-not-clock-regexp nil
  2340. "Matches any of the 3 keywords, together with the time stamp.")
  2341. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2342. (defvar org-maybe-keyword-time-regexp nil
  2343. "Matches a timestamp, possibly preceeded by a keyword.")
  2344. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2345. (defvar org-planning-or-clock-line-re nil
  2346. "Matches a line with planning or clock info.")
  2347. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2348. (defconst org-plain-time-of-day-regexp
  2349. (concat
  2350. "\\(\\<[012]?[0-9]"
  2351. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2352. "\\(--?"
  2353. "\\(\\<[012]?[0-9]"
  2354. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2355. "\\)?")
  2356. "Regular expression to match a plain time or time range.
  2357. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2358. groups carry important information:
  2359. 0 the full match
  2360. 1 the first time, range or not
  2361. 8 the second time, if it is a range.")
  2362. (defconst org-plain-time-extension-regexp
  2363. (concat
  2364. "\\(\\<[012]?[0-9]"
  2365. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2366. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2367. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2368. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2369. groups carry important information:
  2370. 0 the full match
  2371. 7 hours of duration
  2372. 9 minutes of duration")
  2373. (defconst org-stamp-time-of-day-regexp
  2374. (concat
  2375. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2376. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2377. "\\(--?"
  2378. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2379. "Regular expression to match a timestamp time or time range.
  2380. After a match, the following groups carry important information:
  2381. 0 the full match
  2382. 1 date plus weekday, for backreferencing to make sure both times on same day
  2383. 2 the first time, range or not
  2384. 4 the second time, if it is a range.")
  2385. (defconst org-startup-options
  2386. '(("fold" org-startup-folded t)
  2387. ("overview" org-startup-folded t)
  2388. ("nofold" org-startup-folded nil)
  2389. ("showall" org-startup-folded nil)
  2390. ("content" org-startup-folded content)
  2391. ("hidestars" org-hide-leading-stars t)
  2392. ("showstars" org-hide-leading-stars nil)
  2393. ("odd" org-odd-levels-only t)
  2394. ("oddeven" org-odd-levels-only nil)
  2395. ("align" org-startup-align-all-tables t)
  2396. ("noalign" org-startup-align-all-tables nil)
  2397. ("customtime" org-display-custom-times t)
  2398. ("logdone" org-log-done time)
  2399. ("lognotedone" org-log-done note)
  2400. ("nologdone" org-log-done nil)
  2401. ("lognoteclock-out" org-log-note-clock-out t)
  2402. ("nolognoteclock-out" org-log-note-clock-out nil)
  2403. ("logrepeat" org-log-repeat state)
  2404. ("lognoterepeat" org-log-repeat note)
  2405. ("nologrepeat" org-log-repeat nil)
  2406. ("constcgs" constants-unit-system cgs)
  2407. ("constSI" constants-unit-system SI))
  2408. "Variable associated with STARTUP options for org-mode.
  2409. Each element is a list of three items: The startup options as written
  2410. in the #+STARTUP line, the corresponding variable, and the value to
  2411. set this variable to if the option is found. An optional forth element PUSH
  2412. means to push this value onto the list in the variable.")
  2413. (defun org-set-regexps-and-options ()
  2414. "Precompute regular expressions for current buffer."
  2415. (when (org-mode-p)
  2416. (org-set-local 'org-todo-kwd-alist nil)
  2417. (org-set-local 'org-todo-key-alist nil)
  2418. (org-set-local 'org-todo-key-trigger nil)
  2419. (org-set-local 'org-todo-keywords-1 nil)
  2420. (org-set-local 'org-done-keywords nil)
  2421. (org-set-local 'org-todo-heads nil)
  2422. (org-set-local 'org-todo-sets nil)
  2423. (org-set-local 'org-todo-log-states nil)
  2424. (let ((re (org-make-options-regexp
  2425. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2426. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2427. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2428. (splitre "[ \t]+")
  2429. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2430. tail sep kws1 prio props ftags drawers
  2431. ext-setup-or-nil setup-contents (start 0))
  2432. (save-excursion
  2433. (save-restriction
  2434. (widen)
  2435. (goto-char (point-min))
  2436. (while (or (and ext-setup-or-nil
  2437. (string-match re ext-setup-or-nil start)
  2438. (setq start (match-end 0)))
  2439. (and (setq ext-setup-or-nil nil start 0)
  2440. (re-search-forward re nil t)))
  2441. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2442. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2443. (cond
  2444. ((equal key "CATEGORY")
  2445. (if (string-match "[ \t]+$" value)
  2446. (setq value (replace-match "" t t value)))
  2447. (setq cat value))
  2448. ((member key '("SEQ_TODO" "TODO"))
  2449. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2450. ((equal key "TYP_TODO")
  2451. (push (cons 'type (org-split-string value splitre)) kwds))
  2452. ((equal key "TAGS")
  2453. (setq tags (append tags (org-split-string value splitre))))
  2454. ((equal key "COLUMNS")
  2455. (org-set-local 'org-columns-default-format value))
  2456. ((equal key "LINK")
  2457. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2458. (push (cons (match-string 1 value)
  2459. (org-trim (match-string 2 value)))
  2460. links)))
  2461. ((equal key "PRIORITIES")
  2462. (setq prio (org-split-string value " +")))
  2463. ((equal key "PROPERTY")
  2464. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2465. (push (cons (match-string 1 value) (match-string 2 value))
  2466. props)))
  2467. ((equal key "FILETAGS")
  2468. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2469. (setq ftags
  2470. (append
  2471. ftags
  2472. (apply 'append
  2473. (mapcar (lambda (x) (org-split-string x ":"))
  2474. (org-split-string value)))))))
  2475. ((equal key "DRAWERS")
  2476. (setq drawers (org-split-string value splitre)))
  2477. ((equal key "CONSTANTS")
  2478. (setq const (append const (org-split-string value splitre))))
  2479. ((equal key "STARTUP")
  2480. (let ((opts (org-split-string value splitre))
  2481. l var val)
  2482. (while (setq l (pop opts))
  2483. (when (setq l (assoc l org-startup-options))
  2484. (setq var (nth 1 l) val (nth 2 l))
  2485. (if (not (nth 3 l))
  2486. (set (make-local-variable var) val)
  2487. (if (not (listp (symbol-value var)))
  2488. (set (make-local-variable var) nil))
  2489. (set (make-local-variable var) (symbol-value var))
  2490. (add-to-list var val))))))
  2491. ((equal key "ARCHIVE")
  2492. (string-match " *$" value)
  2493. (setq arch (replace-match "" t t value))
  2494. (remove-text-properties 0 (length arch)
  2495. '(face t fontified t) arch))
  2496. ((equal key "SETUPFILE")
  2497. (setq setup-contents (org-file-contents
  2498. (expand-file-name
  2499. (org-remove-double-quotes value))
  2500. 'noerror))
  2501. (if (not ext-setup-or-nil)
  2502. (setq ext-setup-or-nil setup-contents start 0)
  2503. (setq ext-setup-or-nil
  2504. (concat (substring ext-setup-or-nil 0 start)
  2505. "\n" setup-contents "\n"
  2506. (substring ext-setup-or-nil start)))))
  2507. ))))
  2508. (when cat
  2509. (org-set-local 'org-category (intern cat))
  2510. (push (cons "CATEGORY" cat) props))
  2511. (when prio
  2512. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2513. (setq prio (mapcar 'string-to-char prio))
  2514. (org-set-local 'org-highest-priority (nth 0 prio))
  2515. (org-set-local 'org-lowest-priority (nth 1 prio))
  2516. (org-set-local 'org-default-priority (nth 2 prio)))
  2517. (and props (org-set-local 'org-file-properties (nreverse props)))
  2518. (and ftags (org-set-local 'org-file-tags ftags))
  2519. (and drawers (org-set-local 'org-drawers drawers))
  2520. (and arch (org-set-local 'org-archive-location arch))
  2521. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2522. ;; Process the TODO keywords
  2523. (unless kwds
  2524. ;; Use the global values as if they had been given locally.
  2525. (setq kwds (default-value 'org-todo-keywords))
  2526. (if (stringp (car kwds))
  2527. (setq kwds (list (cons org-todo-interpretation
  2528. (default-value 'org-todo-keywords)))))
  2529. (setq kwds (reverse kwds)))
  2530. (setq kwds (nreverse kwds))
  2531. (let (inter kws kw)
  2532. (while (setq kws (pop kwds))
  2533. (setq inter (pop kws) sep (member "|" kws)
  2534. kws0 (delete "|" (copy-sequence kws))
  2535. kwsa nil
  2536. kws1 (mapcar
  2537. (lambda (x)
  2538. ;; 1 2
  2539. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2540. (progn
  2541. (setq kw (match-string 1 x)
  2542. key (and (match-end 2) (match-string 2 x))
  2543. log (org-extract-log-state-settings x))
  2544. (push (cons kw (and key (string-to-char key))) kwsa)
  2545. (and log (push log org-todo-log-states))
  2546. kw)
  2547. (error "Invalid TODO keyword %s" x)))
  2548. kws0)
  2549. kwsa (if kwsa (append '((:startgroup))
  2550. (nreverse kwsa)
  2551. '((:endgroup))))
  2552. hw (car kws1)
  2553. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2554. tail (list inter hw (car dws) (org-last dws)))
  2555. (add-to-list 'org-todo-heads hw 'append)
  2556. (push kws1 org-todo-sets)
  2557. (setq org-done-keywords (append org-done-keywords dws nil))
  2558. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2559. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2560. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2561. (setq org-todo-sets (nreverse org-todo-sets)
  2562. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2563. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2564. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2565. ;; Process the constants
  2566. (when const
  2567. (let (e cst)
  2568. (while (setq e (pop const))
  2569. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2570. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2571. (setq org-table-formula-constants-local cst)))
  2572. ;; Process the tags.
  2573. (when tags
  2574. (let (e tgs)
  2575. (while (setq e (pop tags))
  2576. (cond
  2577. ((equal e "{") (push '(:startgroup) tgs))
  2578. ((equal e "}") (push '(:endgroup) tgs))
  2579. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2580. (push (cons (match-string 1 e)
  2581. (string-to-char (match-string 2 e)))
  2582. tgs))
  2583. (t (push (list e) tgs))))
  2584. (org-set-local 'org-tag-alist nil)
  2585. (while (setq e (pop tgs))
  2586. (or (and (stringp (car e))
  2587. (assoc (car e) org-tag-alist))
  2588. (push e org-tag-alist))))))
  2589. ;; Compute the regular expressions and other local variables
  2590. (if (not org-done-keywords)
  2591. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2592. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2593. (length org-scheduled-string)
  2594. (length org-clock-string)
  2595. (length org-closed-string)))
  2596. org-drawer-regexp
  2597. (concat "^[ \t]*:\\("
  2598. (mapconcat 'regexp-quote org-drawers "\\|")
  2599. "\\):[ \t]*$")
  2600. org-not-done-keywords
  2601. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2602. org-todo-regexp
  2603. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2604. "\\|") "\\)\\>")
  2605. org-not-done-regexp
  2606. (concat "\\<\\("
  2607. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2608. "\\)\\>")
  2609. org-todo-line-regexp
  2610. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2611. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2612. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2613. org-complex-heading-regexp
  2614. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2615. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2616. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2617. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2618. org-nl-done-regexp
  2619. (concat "\n\\*+[ \t]+"
  2620. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2621. "\\)" "\\>")
  2622. org-todo-line-tags-regexp
  2623. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2624. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2625. (org-re
  2626. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2627. org-looking-at-done-regexp
  2628. (concat "^" "\\(?:"
  2629. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2630. "\\>")
  2631. org-deadline-regexp (concat "\\<" org-deadline-string)
  2632. org-deadline-time-regexp
  2633. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2634. org-deadline-line-regexp
  2635. (concat "\\<\\(" org-deadline-string "\\).*")
  2636. org-scheduled-regexp
  2637. (concat "\\<" org-scheduled-string)
  2638. org-scheduled-time-regexp
  2639. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2640. org-closed-time-regexp
  2641. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2642. org-keyword-time-regexp
  2643. (concat "\\<\\(" org-scheduled-string
  2644. "\\|" org-deadline-string
  2645. "\\|" org-closed-string
  2646. "\\|" org-clock-string "\\)"
  2647. " *[[<]\\([^]>]+\\)[]>]")
  2648. org-keyword-time-not-clock-regexp
  2649. (concat "\\<\\(" org-scheduled-string
  2650. "\\|" org-deadline-string
  2651. "\\|" org-closed-string
  2652. "\\)"
  2653. " *[[<]\\([^]>]+\\)[]>]")
  2654. org-maybe-keyword-time-regexp
  2655. (concat "\\(\\<\\(" org-scheduled-string
  2656. "\\|" org-deadline-string
  2657. "\\|" org-closed-string
  2658. "\\|" org-clock-string "\\)\\)?"
  2659. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2660. org-planning-or-clock-line-re
  2661. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2662. "\\|" org-deadline-string
  2663. "\\|" org-closed-string "\\|" org-clock-string
  2664. "\\)\\>\\)")
  2665. )
  2666. (org-compute-latex-and-specials-regexp)
  2667. (org-set-font-lock-defaults)))
  2668. (defun org-file-contents (file &optional noerror)
  2669. "Return the contents of FILE, as a string."
  2670. (if (or (not file)
  2671. (not (file-readable-p file)))
  2672. (if noerror
  2673. (progn
  2674. (message "Cannot read file %s" file)
  2675. (ding) (sit-for 2)
  2676. "")
  2677. (error "Cannot read file %s" file))
  2678. (with-temp-buffer
  2679. (insert-file-contents file)
  2680. (buffer-string))))
  2681. (defun org-extract-log-state-settings (x)
  2682. "Extract the log state setting from a TODO keyword string.
  2683. This will extract info from a string like \"WAIT(w@/!)\"."
  2684. (let (kw key log1 log2)
  2685. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2686. (setq kw (match-string 1 x)
  2687. key (and (match-end 2) (match-string 2 x))
  2688. log1 (and (match-end 3) (match-string 3 x))
  2689. log2 (and (match-end 4) (match-string 4 x)))
  2690. (and (or log1 log2)
  2691. (list kw
  2692. (and log1 (if (equal log1 "!") 'time 'note))
  2693. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2694. (defun org-remove-keyword-keys (list)
  2695. "Remove a pair of parenthesis at the end of each string in LIST."
  2696. (mapcar (lambda (x)
  2697. (if (string-match "(.*)$" x)
  2698. (substring x 0 (match-beginning 0))
  2699. x))
  2700. list))
  2701. ;; FIXME: this could be done much better, using second characters etc.
  2702. (defun org-assign-fast-keys (alist)
  2703. "Assign fast keys to a keyword-key alist.
  2704. Respect keys that are already there."
  2705. (let (new e k c c1 c2 (char ?a))
  2706. (while (setq e (pop alist))
  2707. (cond
  2708. ((equal e '(:startgroup)) (push e new))
  2709. ((equal e '(:endgroup)) (push e new))
  2710. (t
  2711. (setq k (car e) c2 nil)
  2712. (if (cdr e)
  2713. (setq c (cdr e))
  2714. ;; automatically assign a character.
  2715. (setq c1 (string-to-char
  2716. (downcase (substring
  2717. k (if (= (string-to-char k) ?@) 1 0)))))
  2718. (if (or (rassoc c1 new) (rassoc c1 alist))
  2719. (while (or (rassoc char new) (rassoc char alist))
  2720. (setq char (1+ char)))
  2721. (setq c2 c1))
  2722. (setq c (or c2 char)))
  2723. (push (cons k c) new))))
  2724. (nreverse new)))
  2725. ;;; Some variables used in various places
  2726. (defvar org-window-configuration nil
  2727. "Used in various places to store a window configuration.")
  2728. (defvar org-finish-function nil
  2729. "Function to be called when `C-c C-c' is used.
  2730. This is for getting out of special buffers like remember.")
  2731. ;; FIXME: Occasionally check by commenting these, to make sure
  2732. ;; no other functions uses these, forgetting to let-bind them.
  2733. (defvar entry)
  2734. (defvar state)
  2735. (defvar last-state)
  2736. (defvar date)
  2737. (defvar description)
  2738. ;; Defined somewhere in this file, but used before definition.
  2739. (defvar org-html-entities)
  2740. (defvar org-struct-menu)
  2741. (defvar org-org-menu)
  2742. (defvar org-tbl-menu)
  2743. (defvar org-agenda-keymap)
  2744. ;;;; Define the Org-mode
  2745. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2746. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2747. ;; We use a before-change function to check if a table might need
  2748. ;; an update.
  2749. (defvar org-table-may-need-update t
  2750. "Indicates that a table might need an update.
  2751. This variable is set by `org-before-change-function'.
  2752. `org-table-align' sets it back to nil.")
  2753. (defun org-before-change-function (beg end)
  2754. "Every change indicates that a table might need an update."
  2755. (setq org-table-may-need-update t))
  2756. (defvar org-mode-map)
  2757. (defvar org-mode-hook nil)
  2758. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2759. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2760. (defvar org-table-buffer-is-an nil)
  2761. (defconst org-outline-regexp "\\*+ ")
  2762. ;;;###autoload
  2763. (define-derived-mode org-mode outline-mode "Org"
  2764. "Outline-based notes management and organizer, alias
  2765. \"Carsten's outline-mode for keeping track of everything.\"
  2766. Org-mode develops organizational tasks around a NOTES file which
  2767. contains information about projects as plain text. Org-mode is
  2768. implemented on top of outline-mode, which is ideal to keep the content
  2769. of large files well structured. It supports ToDo items, deadlines and
  2770. time stamps, which magically appear in the diary listing of the Emacs
  2771. calendar. Tables are easily created with a built-in table editor.
  2772. Plain text URL-like links connect to websites, emails (VM), Usenet
  2773. messages (Gnus), BBDB entries, and any files related to the project.
  2774. For printing and sharing of notes, an Org-mode file (or a part of it)
  2775. can be exported as a structured ASCII or HTML file.
  2776. The following commands are available:
  2777. \\{org-mode-map}"
  2778. ;; Get rid of Outline menus, they are not needed
  2779. ;; Need to do this here because define-derived-mode sets up
  2780. ;; the keymap so late. Still, it is a waste to call this each time
  2781. ;; we switch another buffer into org-mode.
  2782. (if (featurep 'xemacs)
  2783. (when (boundp 'outline-mode-menu-heading)
  2784. ;; Assume this is Greg's port, it used easymenu
  2785. (easy-menu-remove outline-mode-menu-heading)
  2786. (easy-menu-remove outline-mode-menu-show)
  2787. (easy-menu-remove outline-mode-menu-hide))
  2788. (define-key org-mode-map [menu-bar headings] 'undefined)
  2789. (define-key org-mode-map [menu-bar hide] 'undefined)
  2790. (define-key org-mode-map [menu-bar show] 'undefined))
  2791. (org-load-modules-maybe)
  2792. (easy-menu-add org-org-menu)
  2793. (easy-menu-add org-tbl-menu)
  2794. (org-install-agenda-files-menu)
  2795. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2796. (org-add-to-invisibility-spec '(org-cwidth))
  2797. (when (featurep 'xemacs)
  2798. (org-set-local 'line-move-ignore-invisible t))
  2799. (org-set-local 'outline-regexp org-outline-regexp)
  2800. (org-set-local 'outline-level 'org-outline-level)
  2801. (when (and org-ellipsis
  2802. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2803. (fboundp 'make-glyph-code))
  2804. (unless org-display-table
  2805. (setq org-display-table (make-display-table)))
  2806. (set-display-table-slot
  2807. org-display-table 4
  2808. (vconcat (mapcar
  2809. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2810. org-ellipsis)))
  2811. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2812. (setq buffer-display-table org-display-table))
  2813. (org-set-regexps-and-options)
  2814. ;; Calc embedded
  2815. (org-set-local 'calc-embedded-open-mode "# ")
  2816. (modify-syntax-entry ?# "<")
  2817. (modify-syntax-entry ?@ "w")
  2818. (if org-startup-truncated (setq truncate-lines t))
  2819. (org-set-local 'font-lock-unfontify-region-function
  2820. 'org-unfontify-region)
  2821. ;; Activate before-change-function
  2822. (org-set-local 'org-table-may-need-update t)
  2823. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2824. 'local)
  2825. ;; Check for running clock before killing a buffer
  2826. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2827. ;; Paragraphs and auto-filling
  2828. (org-set-autofill-regexps)
  2829. (setq indent-line-function 'org-indent-line-function)
  2830. (org-update-radio-target-regexp)
  2831. ;; Comment characters
  2832. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2833. (org-set-local 'comment-padding " ")
  2834. ;; Align options lines
  2835. (org-set-local
  2836. 'align-mode-rules-list
  2837. '((org-in-buffer-settings
  2838. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2839. (modes . '(org-mode)))))
  2840. ;; Imenu
  2841. (org-set-local 'imenu-create-index-function
  2842. 'org-imenu-get-tree)
  2843. ;; Make isearch reveal context
  2844. (if (or (featurep 'xemacs)
  2845. (not (boundp 'outline-isearch-open-invisible-function)))
  2846. ;; Emacs 21 and XEmacs make use of the hook
  2847. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2848. ;; Emacs 22 deals with this through a special variable
  2849. (org-set-local 'outline-isearch-open-invisible-function
  2850. (lambda (&rest ignore) (org-show-context 'isearch))))
  2851. ;; If empty file that did not turn on org-mode automatically, make it to.
  2852. (if (and org-insert-mode-line-in-empty-file
  2853. (interactive-p)
  2854. (= (point-min) (point-max)))
  2855. (insert "# -*- mode: org -*-\n\n"))
  2856. (unless org-inhibit-startup
  2857. (when org-startup-align-all-tables
  2858. (let ((bmp (buffer-modified-p)))
  2859. (org-table-map-tables 'org-table-align)
  2860. (set-buffer-modified-p bmp)))
  2861. (org-set-startup-visibility)))
  2862. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2863. (defun org-current-time ()
  2864. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2865. (if (> (car org-time-stamp-rounding-minutes) 1)
  2866. (let ((r (car org-time-stamp-rounding-minutes))
  2867. (time (decode-time)))
  2868. (apply 'encode-time
  2869. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2870. (nthcdr 2 time))))
  2871. (current-time)))
  2872. ;;;; Font-Lock stuff, including the activators
  2873. (defvar org-mouse-map (make-sparse-keymap))
  2874. (org-defkey org-mouse-map
  2875. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2876. (org-defkey org-mouse-map
  2877. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2878. (when org-mouse-1-follows-link
  2879. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2880. (when org-tab-follows-link
  2881. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2882. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2883. (when org-return-follows-link
  2884. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2885. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2886. (require 'font-lock)
  2887. (defconst org-non-link-chars "]\t\n\r<>")
  2888. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2889. "shell" "elisp"))
  2890. (defvar org-link-types-re nil
  2891. "Matches a link that has a url-like prefix like \"http:\"")
  2892. (defvar org-link-re-with-space nil
  2893. "Matches a link with spaces, optional angular brackets around it.")
  2894. (defvar org-link-re-with-space2 nil
  2895. "Matches a link with spaces, optional angular brackets around it.")
  2896. (defvar org-angle-link-re nil
  2897. "Matches link with angular brackets, spaces are allowed.")
  2898. (defvar org-plain-link-re nil
  2899. "Matches plain link, without spaces.")
  2900. (defvar org-bracket-link-regexp nil
  2901. "Matches a link in double brackets.")
  2902. (defvar org-bracket-link-analytic-regexp nil
  2903. "Regular expression used to analyze links.
  2904. Here is what the match groups contain after a match:
  2905. 1: http:
  2906. 2: http
  2907. 3: path
  2908. 4: [desc]
  2909. 5: desc")
  2910. (defvar org-any-link-re nil
  2911. "Regular expression matching any link.")
  2912. (defun org-make-link-regexps ()
  2913. "Update the link regular expressions.
  2914. This should be called after the variable `org-link-types' has changed."
  2915. (setq org-link-types-re
  2916. (concat
  2917. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2918. org-link-re-with-space
  2919. (concat
  2920. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2921. "\\([^" org-non-link-chars " ]"
  2922. "[^" org-non-link-chars "]*"
  2923. "[^" org-non-link-chars " ]\\)>?")
  2924. org-link-re-with-space2
  2925. (concat
  2926. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2927. "\\([^" org-non-link-chars " ]"
  2928. "[^]\t\n\r]*"
  2929. "[^" org-non-link-chars " ]\\)>?")
  2930. org-angle-link-re
  2931. (concat
  2932. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2933. "\\([^" org-non-link-chars " ]"
  2934. "[^" org-non-link-chars "]*"
  2935. "\\)>")
  2936. org-plain-link-re
  2937. (concat
  2938. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2939. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2940. org-bracket-link-regexp
  2941. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2942. org-bracket-link-analytic-regexp
  2943. (concat
  2944. "\\[\\["
  2945. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2946. "\\([^]]+\\)"
  2947. "\\]"
  2948. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2949. "\\]")
  2950. org-any-link-re
  2951. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2952. org-angle-link-re "\\)\\|\\("
  2953. org-plain-link-re "\\)")))
  2954. (org-make-link-regexps)
  2955. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2956. "Regular expression for fast time stamp matching.")
  2957. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2958. "Regular expression for fast time stamp matching.")
  2959. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2960. "Regular expression matching time strings for analysis.
  2961. This one does not require the space after the date, so it can be used
  2962. on a string that terminates immediately after the date.")
  2963. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2964. "Regular expression matching time strings for analysis.")
  2965. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2966. "Regular expression matching time stamps, with groups.")
  2967. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2968. "Regular expression matching time stamps (also [..]), with groups.")
  2969. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2970. "Regular expression matching a time stamp range.")
  2971. (defconst org-tr-regexp-both
  2972. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2973. "Regular expression matching a time stamp range.")
  2974. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2975. org-ts-regexp "\\)?")
  2976. "Regular expression matching a time stamp or time stamp range.")
  2977. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2978. org-ts-regexp-both "\\)?")
  2979. "Regular expression matching a time stamp or time stamp range.
  2980. The time stamps may be either active or inactive.")
  2981. (defvar org-emph-face nil)
  2982. (defun org-do-emphasis-faces (limit)
  2983. "Run through the buffer and add overlays to links."
  2984. (let (rtn)
  2985. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2986. (if (not (= (char-after (match-beginning 3))
  2987. (char-after (match-beginning 4))))
  2988. (progn
  2989. (setq rtn t)
  2990. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2991. 'face
  2992. (nth 1 (assoc (match-string 3)
  2993. org-emphasis-alist)))
  2994. (add-text-properties (match-beginning 2) (match-end 2)
  2995. '(font-lock-multiline t))
  2996. (when org-hide-emphasis-markers
  2997. (add-text-properties (match-end 4) (match-beginning 5)
  2998. '(invisible org-link))
  2999. (add-text-properties (match-beginning 3) (match-end 3)
  3000. '(invisible org-link)))))
  3001. (backward-char 1))
  3002. rtn))
  3003. (defun org-emphasize (&optional char)
  3004. "Insert or change an emphasis, i.e. a font like bold or italic.
  3005. If there is an active region, change that region to a new emphasis.
  3006. If there is no region, just insert the marker characters and position
  3007. the cursor between them.
  3008. CHAR should be either the marker character, or the first character of the
  3009. HTML tag associated with that emphasis. If CHAR is a space, the means
  3010. to remove the emphasis of the selected region.
  3011. If char is not given (for example in an interactive call) it
  3012. will be prompted for."
  3013. (interactive)
  3014. (let ((eal org-emphasis-alist) e det
  3015. (erc org-emphasis-regexp-components)
  3016. (prompt "")
  3017. (string "") beg end move tag c s)
  3018. (if (org-region-active-p)
  3019. (setq beg (region-beginning) end (region-end)
  3020. string (buffer-substring beg end))
  3021. (setq move t))
  3022. (while (setq e (pop eal))
  3023. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3024. c (aref tag 0))
  3025. (push (cons c (string-to-char (car e))) det)
  3026. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3027. (substring tag 1)))))
  3028. (unless char
  3029. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3030. (setq char (read-char-exclusive)))
  3031. (setq char (or (cdr (assoc char det)) char))
  3032. (if (equal char ?\ )
  3033. (setq s "" move nil)
  3034. (unless (assoc (char-to-string char) org-emphasis-alist)
  3035. (error "No such emphasis marker: \"%c\"" char))
  3036. (setq s (char-to-string char)))
  3037. (while (and (> (length string) 1)
  3038. (equal (substring string 0 1) (substring string -1))
  3039. (assoc (substring string 0 1) org-emphasis-alist))
  3040. (setq string (substring string 1 -1)))
  3041. (setq string (concat s string s))
  3042. (if beg (delete-region beg end))
  3043. (unless (or (bolp)
  3044. (string-match (concat "[" (nth 0 erc) "\n]")
  3045. (char-to-string (char-before (point)))))
  3046. (insert " "))
  3047. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3048. (char-to-string (char-after (point))))
  3049. (insert " ") (backward-char 1))
  3050. (insert string)
  3051. (and move (backward-char 1))))
  3052. (defconst org-nonsticky-props
  3053. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3054. (defun org-activate-plain-links (limit)
  3055. "Run through the buffer and add overlays to links."
  3056. (catch 'exit
  3057. (let (f)
  3058. (while (re-search-forward org-plain-link-re limit t)
  3059. (setq f (get-text-property (match-beginning 0) 'face))
  3060. (if (or (eq f 'org-tag)
  3061. (and (listp f) (memq 'org-tag f)))
  3062. nil
  3063. (add-text-properties (match-beginning 0) (match-end 0)
  3064. (list 'mouse-face 'highlight
  3065. 'rear-nonsticky org-nonsticky-props
  3066. 'keymap org-mouse-map
  3067. ))
  3068. (throw 'exit t))))))
  3069. (defun org-activate-code (limit)
  3070. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3071. (unless (get-text-property (match-beginning 1) 'face)
  3072. (remove-text-properties (match-beginning 0) (match-end 0)
  3073. '(display t invisible t intangible t))
  3074. t)))
  3075. (defun org-activate-angle-links (limit)
  3076. "Run through the buffer and add overlays to links."
  3077. (if (re-search-forward org-angle-link-re limit t)
  3078. (progn
  3079. (add-text-properties (match-beginning 0) (match-end 0)
  3080. (list 'mouse-face 'highlight
  3081. 'rear-nonsticky org-nonsticky-props
  3082. 'keymap org-mouse-map
  3083. ))
  3084. t)))
  3085. (defun org-activate-bracket-links (limit)
  3086. "Run through the buffer and add overlays to bracketed links."
  3087. (if (re-search-forward org-bracket-link-regexp limit t)
  3088. (let* ((help (concat "LINK: "
  3089. (org-match-string-no-properties 1)))
  3090. ;; FIXME: above we should remove the escapes.
  3091. ;; but that requires another match, protecting match data,
  3092. ;; a lot of overhead for font-lock.
  3093. (ip (org-maybe-intangible
  3094. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3095. 'keymap org-mouse-map 'mouse-face 'highlight
  3096. 'font-lock-multiline t 'help-echo help)))
  3097. (vp (list 'rear-nonsticky org-nonsticky-props
  3098. 'keymap org-mouse-map 'mouse-face 'highlight
  3099. ' font-lock-multiline t 'help-echo help)))
  3100. ;; We need to remove the invisible property here. Table narrowing
  3101. ;; may have made some of this invisible.
  3102. (remove-text-properties (match-beginning 0) (match-end 0)
  3103. '(invisible nil))
  3104. (if (match-end 3)
  3105. (progn
  3106. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3107. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3108. (add-text-properties (match-end 3) (match-end 0) ip))
  3109. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3110. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3111. (add-text-properties (match-end 1) (match-end 0) ip))
  3112. t)))
  3113. (defun org-activate-dates (limit)
  3114. "Run through the buffer and add overlays to dates."
  3115. (if (re-search-forward org-tsr-regexp-both limit t)
  3116. (progn
  3117. (add-text-properties (match-beginning 0) (match-end 0)
  3118. (list 'mouse-face 'highlight
  3119. 'rear-nonsticky org-nonsticky-props
  3120. 'keymap org-mouse-map))
  3121. (when org-display-custom-times
  3122. (if (match-end 3)
  3123. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3124. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3125. t)))
  3126. (defvar org-target-link-regexp nil
  3127. "Regular expression matching radio targets in plain text.")
  3128. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3129. "Regular expression matching a link target.")
  3130. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3131. "Regular expression matching a radio target.")
  3132. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3133. "Regular expression matching any target.")
  3134. (defun org-activate-target-links (limit)
  3135. "Run through the buffer and add overlays to target matches."
  3136. (when org-target-link-regexp
  3137. (let ((case-fold-search t))
  3138. (if (re-search-forward org-target-link-regexp limit t)
  3139. (progn
  3140. (add-text-properties (match-beginning 0) (match-end 0)
  3141. (list 'mouse-face 'highlight
  3142. 'rear-nonsticky org-nonsticky-props
  3143. 'keymap org-mouse-map
  3144. 'help-echo "Radio target link"
  3145. 'org-linked-text t))
  3146. t)))))
  3147. (defun org-update-radio-target-regexp ()
  3148. "Find all radio targets in this file and update the regular expression."
  3149. (interactive)
  3150. (when (memq 'radio org-activate-links)
  3151. (setq org-target-link-regexp
  3152. (org-make-target-link-regexp (org-all-targets 'radio)))
  3153. (org-restart-font-lock)))
  3154. (defun org-hide-wide-columns (limit)
  3155. (let (s e)
  3156. (setq s (text-property-any (point) (or limit (point-max))
  3157. 'org-cwidth t))
  3158. (when s
  3159. (setq e (next-single-property-change s 'org-cwidth))
  3160. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3161. (goto-char e)
  3162. t)))
  3163. (defvar org-latex-and-specials-regexp nil
  3164. "Regular expression for highlighting export special stuff.")
  3165. (defvar org-match-substring-regexp)
  3166. (defvar org-match-substring-with-braces-regexp)
  3167. (defvar org-export-html-special-string-regexps)
  3168. (defun org-compute-latex-and-specials-regexp ()
  3169. "Compute regular expression for stuff treated specially by exporters."
  3170. (if (not org-highlight-latex-fragments-and-specials)
  3171. (org-set-local 'org-latex-and-specials-regexp nil)
  3172. (require 'org-exp)
  3173. (let*
  3174. ((matchers (plist-get org-format-latex-options :matchers))
  3175. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3176. org-latex-regexps)))
  3177. (options (org-combine-plists (org-default-export-plist)
  3178. (org-infile-export-plist)))
  3179. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3180. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3181. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3182. (org-export-html-expand (plist-get options :expand-quoted-html))
  3183. (org-export-with-special-strings (plist-get options :special-strings))
  3184. (re-sub
  3185. (cond
  3186. ((equal org-export-with-sub-superscripts '{})
  3187. (list org-match-substring-with-braces-regexp))
  3188. (org-export-with-sub-superscripts
  3189. (list org-match-substring-regexp))
  3190. (t nil)))
  3191. (re-latex
  3192. (if org-export-with-LaTeX-fragments
  3193. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3194. (re-macros
  3195. (if org-export-with-TeX-macros
  3196. (list (concat "\\\\"
  3197. (regexp-opt
  3198. (append (mapcar 'car org-html-entities)
  3199. (if (boundp 'org-latex-entities)
  3200. org-latex-entities nil))
  3201. 'words))) ; FIXME
  3202. ))
  3203. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3204. (re-special (if org-export-with-special-strings
  3205. (mapcar (lambda (x) (car x))
  3206. org-export-html-special-string-regexps)))
  3207. (re-rest
  3208. (delq nil
  3209. (list
  3210. (if org-export-html-expand "@<[^>\n]+>")
  3211. ))))
  3212. (org-set-local
  3213. 'org-latex-and-specials-regexp
  3214. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3215. re-rest) "\\|")))))
  3216. (defun org-do-latex-and-special-faces (limit)
  3217. "Run through the buffer and add overlays to links."
  3218. (when org-latex-and-specials-regexp
  3219. (let (rtn d)
  3220. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3221. limit t))
  3222. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3223. 'face))
  3224. '(org-code org-verbatim underline)))
  3225. (progn
  3226. (setq rtn t
  3227. d (cond ((member (char-after (1+ (match-beginning 0)))
  3228. '(?_ ?^)) 1)
  3229. (t 0)))
  3230. (font-lock-prepend-text-property
  3231. (+ d (match-beginning 0)) (match-end 0)
  3232. 'face 'org-latex-and-export-specials)
  3233. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3234. '(font-lock-multiline t)))))
  3235. rtn)))
  3236. (defun org-restart-font-lock ()
  3237. "Restart font-lock-mode, to force refontification."
  3238. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3239. (font-lock-mode -1)
  3240. (font-lock-mode 1)))
  3241. (defun org-all-targets (&optional radio)
  3242. "Return a list of all targets in this file.
  3243. With optional argument RADIO, only find radio targets."
  3244. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3245. rtn)
  3246. (save-excursion
  3247. (goto-char (point-min))
  3248. (while (re-search-forward re nil t)
  3249. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3250. rtn)))
  3251. (defun org-make-target-link-regexp (targets)
  3252. "Make regular expression matching all strings in TARGETS.
  3253. The regular expression finds the targets also if there is a line break
  3254. between words."
  3255. (and targets
  3256. (concat
  3257. "\\<\\("
  3258. (mapconcat
  3259. (lambda (x)
  3260. (while (string-match " +" x)
  3261. (setq x (replace-match "\\s-+" t t x)))
  3262. x)
  3263. targets
  3264. "\\|")
  3265. "\\)\\>")))
  3266. (defun org-activate-tags (limit)
  3267. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3268. (progn
  3269. (add-text-properties (match-beginning 1) (match-end 1)
  3270. (list 'mouse-face 'highlight
  3271. 'rear-nonsticky org-nonsticky-props
  3272. 'keymap org-mouse-map))
  3273. t)))
  3274. (defun org-outline-level ()
  3275. (save-excursion
  3276. (looking-at outline-regexp)
  3277. (if (match-beginning 1)
  3278. (+ (org-get-string-indentation (match-string 1)) 1000)
  3279. (1- (- (match-end 0) (match-beginning 0))))))
  3280. (defvar org-font-lock-keywords nil)
  3281. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3282. "Regular expression matching a property line.")
  3283. (defvar org-font-lock-hook nil
  3284. "Functions to be called for special font lock stuff.")
  3285. (defun org-font-lock-hook (limit)
  3286. (run-hook-with-args 'org-font-lock-hook limit))
  3287. (defun org-set-font-lock-defaults ()
  3288. (let* ((em org-fontify-emphasized-text)
  3289. (lk org-activate-links)
  3290. (org-font-lock-extra-keywords
  3291. (list
  3292. ;; Call the hook
  3293. '(org-font-lock-hook)
  3294. ;; Headlines
  3295. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3296. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3297. ;; Table lines
  3298. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3299. (1 'org-table t))
  3300. ;; Table internals
  3301. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3302. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3303. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3304. ;; Drawers
  3305. (list org-drawer-regexp '(0 'org-special-keyword t))
  3306. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3307. ;; Properties
  3308. (list org-property-re
  3309. '(1 'org-special-keyword t)
  3310. '(3 'org-property-value t))
  3311. (if org-format-transports-properties-p
  3312. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3313. ;; Links
  3314. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3315. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3316. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3317. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3318. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3319. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3320. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3321. '(org-hide-wide-columns (0 nil append))
  3322. ;; TODO lines
  3323. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3324. '(1 (org-get-todo-face 1) t))
  3325. ;; DONE
  3326. (if org-fontify-done-headline
  3327. (list (concat "^[*]+ +\\<\\("
  3328. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3329. "\\)\\(.*\\)")
  3330. '(2 'org-headline-done t))
  3331. nil)
  3332. ;; Priorities
  3333. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3334. ;; Special keywords
  3335. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3336. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3337. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3338. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3339. ;; Emphasis
  3340. (if em
  3341. (if (featurep 'xemacs)
  3342. '(org-do-emphasis-faces (0 nil append))
  3343. '(org-do-emphasis-faces)))
  3344. ;; Checkboxes
  3345. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3346. 2 'bold prepend)
  3347. (if org-provide-checkbox-statistics
  3348. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3349. (0 (org-get-checkbox-statistics-face) t)))
  3350. ;; Description list items
  3351. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3352. 2 'bold prepend)
  3353. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3354. '(1 'org-archived prepend))
  3355. ;; Specials
  3356. '(org-do-latex-and-special-faces)
  3357. ;; Code
  3358. '(org-activate-code (1 'org-code t))
  3359. ;; COMMENT
  3360. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3361. "\\|" org-quote-string "\\)\\>")
  3362. '(1 'org-special-keyword t))
  3363. '("^#.*" (0 'font-lock-comment-face t))
  3364. )))
  3365. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3366. ;; Now set the full font-lock-keywords
  3367. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3368. (org-set-local 'font-lock-defaults
  3369. '(org-font-lock-keywords t nil nil backward-paragraph))
  3370. (kill-local-variable 'font-lock-keywords) nil))
  3371. (defvar org-m nil)
  3372. (defvar org-l nil)
  3373. (defvar org-f nil)
  3374. (defun org-get-level-face (n)
  3375. "Get the right face for match N in font-lock matching of healdines."
  3376. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3377. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3378. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3379. (cond
  3380. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3381. ((eq n 2) org-f)
  3382. (t (if org-level-color-stars-only nil org-f))))
  3383. (defun org-get-todo-face (kwd)
  3384. "Get the right face for a TODO keyword KWD.
  3385. If KWD is a number, get the corresponding match group."
  3386. (if (numberp kwd) (setq kwd (match-string kwd)))
  3387. (or (cdr (assoc kwd org-todo-keyword-faces))
  3388. (and (member kwd org-done-keywords) 'org-done)
  3389. 'org-todo))
  3390. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3391. "Remove fontification and activation overlays from links."
  3392. (font-lock-default-unfontify-region beg end)
  3393. (let* ((buffer-undo-list t)
  3394. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3395. (inhibit-modification-hooks t)
  3396. deactivate-mark buffer-file-name buffer-file-truename)
  3397. (remove-text-properties beg end
  3398. '(mouse-face t keymap t org-linked-text t
  3399. invisible t intangible t))))
  3400. ;;;; Visibility cycling, including org-goto and indirect buffer
  3401. ;;; Cycling
  3402. (defvar org-cycle-global-status nil)
  3403. (make-variable-buffer-local 'org-cycle-global-status)
  3404. (defvar org-cycle-subtree-status nil)
  3405. (make-variable-buffer-local 'org-cycle-subtree-status)
  3406. ;;;###autoload
  3407. (defun org-cycle (&optional arg)
  3408. "Visibility cycling for Org-mode.
  3409. - When this function is called with a prefix argument, rotate the entire
  3410. buffer through 3 states (global cycling)
  3411. 1. OVERVIEW: Show only top-level headlines.
  3412. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3413. 3. SHOW ALL: Show everything.
  3414. When called with two C-c C-u prefixes, switch to the startup visibility,
  3415. determined by the variable `org-startup-folded', and by any VISIBILITY
  3416. properties in the buffer.
  3417. - When point is at the beginning of a headline, rotate the subtree started
  3418. by this line through 3 different states (local cycling)
  3419. 1. FOLDED: Only the main headline is shown.
  3420. 2. CHILDREN: The main headline and the direct children are shown.
  3421. From this state, you can move to one of the children
  3422. and zoom in further.
  3423. 3. SUBTREE: Show the entire subtree, including body text.
  3424. - When there is a numeric prefix, go up to a heading with level ARG, do
  3425. a `show-subtree' and return to the previous cursor position. If ARG
  3426. is negative, go up that many levels.
  3427. - When point is not at the beginning of a headline, execute the global
  3428. binding for TAB, which is re-indenting the line. See the option
  3429. `org-cycle-emulate-tab' for details.
  3430. - Special case: if point is at the beginning of the buffer and there is
  3431. no headline in line 1, this function will act as if called with prefix arg.
  3432. But only if also the variable `org-cycle-global-at-bob' is t."
  3433. (interactive "P")
  3434. (org-load-modules-maybe)
  3435. (let* ((outline-regexp
  3436. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3437. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3438. outline-regexp))
  3439. (bob-special (and org-cycle-global-at-bob (bobp)
  3440. (not (looking-at outline-regexp))))
  3441. (org-cycle-hook
  3442. (if bob-special
  3443. (delq 'org-optimize-window-after-visibility-change
  3444. (copy-sequence org-cycle-hook))
  3445. org-cycle-hook))
  3446. (pos (point)))
  3447. (if (or bob-special (equal arg '(4)))
  3448. ;; special case: use global cycling
  3449. (setq arg t))
  3450. (cond
  3451. ((equal arg '(16))
  3452. (org-set-startup-visibility)
  3453. (message "Startup visibility, plus VISIBILITY properties."))
  3454. ((org-at-table-p 'any)
  3455. ;; Enter the table or move to the next field in the table
  3456. (or (org-table-recognize-table.el)
  3457. (progn
  3458. (if arg (org-table-edit-field t)
  3459. (org-table-justify-field-maybe)
  3460. (call-interactively 'org-table-next-field)))))
  3461. ((eq arg t) ;; Global cycling
  3462. (cond
  3463. ((and (eq last-command this-command)
  3464. (eq org-cycle-global-status 'overview))
  3465. ;; We just created the overview - now do table of contents
  3466. ;; This can be slow in very large buffers, so indicate action
  3467. (message "CONTENTS...")
  3468. (org-content)
  3469. (message "CONTENTS...done")
  3470. (setq org-cycle-global-status 'contents)
  3471. (run-hook-with-args 'org-cycle-hook 'contents))
  3472. ((and (eq last-command this-command)
  3473. (eq org-cycle-global-status 'contents))
  3474. ;; We just showed the table of contents - now show everything
  3475. (show-all)
  3476. (message "SHOW ALL")
  3477. (setq org-cycle-global-status 'all)
  3478. (run-hook-with-args 'org-cycle-hook 'all))
  3479. (t
  3480. ;; Default action: go to overview
  3481. (org-overview)
  3482. (message "OVERVIEW")
  3483. (setq org-cycle-global-status 'overview)
  3484. (run-hook-with-args 'org-cycle-hook 'overview))))
  3485. ((and org-drawers org-drawer-regexp
  3486. (save-excursion
  3487. (beginning-of-line 1)
  3488. (looking-at org-drawer-regexp)))
  3489. ;; Toggle block visibility
  3490. (org-flag-drawer
  3491. (not (get-char-property (match-end 0) 'invisible))))
  3492. ((integerp arg)
  3493. ;; Show-subtree, ARG levels up from here.
  3494. (save-excursion
  3495. (org-back-to-heading)
  3496. (outline-up-heading (if (< arg 0) (- arg)
  3497. (- (funcall outline-level) arg)))
  3498. (org-show-subtree)))
  3499. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3500. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3501. ;; At a heading: rotate between three different views
  3502. (org-back-to-heading)
  3503. (let ((goal-column 0) eoh eol eos)
  3504. ;; First, some boundaries
  3505. (save-excursion
  3506. (org-back-to-heading)
  3507. (save-excursion
  3508. (beginning-of-line 2)
  3509. (while (and (not (eobp)) ;; this is like `next-line'
  3510. (get-char-property (1- (point)) 'invisible))
  3511. (beginning-of-line 2)) (setq eol (point)))
  3512. (outline-end-of-heading) (setq eoh (point))
  3513. (org-end-of-subtree t)
  3514. (unless (eobp)
  3515. (skip-chars-forward " \t\n")
  3516. (beginning-of-line 1) ; in case this is an item
  3517. )
  3518. (setq eos (1- (point))))
  3519. ;; Find out what to do next and set `this-command'
  3520. (cond
  3521. ((= eos eoh)
  3522. ;; Nothing is hidden behind this heading
  3523. (message "EMPTY ENTRY")
  3524. (setq org-cycle-subtree-status nil)
  3525. (save-excursion
  3526. (goto-char eos)
  3527. (outline-next-heading)
  3528. (if (org-invisible-p) (org-flag-heading nil))))
  3529. ((or (>= eol eos)
  3530. (not (string-match "\\S-" (buffer-substring eol eos))))
  3531. ;; Entire subtree is hidden in one line: open it
  3532. (org-show-entry)
  3533. (show-children)
  3534. (message "CHILDREN")
  3535. (save-excursion
  3536. (goto-char eos)
  3537. (outline-next-heading)
  3538. (if (org-invisible-p) (org-flag-heading nil)))
  3539. (setq org-cycle-subtree-status 'children)
  3540. (run-hook-with-args 'org-cycle-hook 'children))
  3541. ((and (eq last-command this-command)
  3542. (eq org-cycle-subtree-status 'children))
  3543. ;; We just showed the children, now show everything.
  3544. (org-show-subtree)
  3545. (message "SUBTREE")
  3546. (setq org-cycle-subtree-status 'subtree)
  3547. (run-hook-with-args 'org-cycle-hook 'subtree))
  3548. (t
  3549. ;; Default action: hide the subtree.
  3550. (hide-subtree)
  3551. (message "FOLDED")
  3552. (setq org-cycle-subtree-status 'folded)
  3553. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3554. ;; TAB emulation and template completion
  3555. (buffer-read-only (org-back-to-heading))
  3556. ((org-try-structure-completion))
  3557. ((org-try-cdlatex-tab))
  3558. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3559. (or (not (bolp))
  3560. (not (looking-at outline-regexp))))
  3561. (call-interactively (global-key-binding "\t")))
  3562. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3563. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3564. (or (and (eq org-cycle-emulate-tab 'white)
  3565. (= (match-end 0) (point-at-eol)))
  3566. (and (eq org-cycle-emulate-tab 'whitestart)
  3567. (>= (match-end 0) pos))))
  3568. t
  3569. (eq org-cycle-emulate-tab t))
  3570. (call-interactively (global-key-binding "\t")))
  3571. (t (save-excursion
  3572. (org-back-to-heading)
  3573. (org-cycle))))))
  3574. ;;;###autoload
  3575. (defun org-global-cycle (&optional arg)
  3576. "Cycle the global visibility. For details see `org-cycle'.
  3577. With C-u prefix arg, switch to startup visibility.
  3578. With a numeric prefix, show all headlines up to that level."
  3579. (interactive "P")
  3580. (let ((org-cycle-include-plain-lists
  3581. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3582. (cond
  3583. ((integerp arg)
  3584. (show-all)
  3585. (hide-sublevels arg)
  3586. (setq org-cycle-global-status 'contents))
  3587. ((equal arg '(4))
  3588. (org-set-startup-visibility)
  3589. (message "Startup visibility, plus VISIBILITY properties."))
  3590. (t
  3591. (org-cycle '(4))))))
  3592. (defun org-set-startup-visibility ()
  3593. "Set the visibility required by startup options and properties."
  3594. (cond
  3595. ((eq org-startup-folded t)
  3596. (org-cycle '(4)))
  3597. ((eq org-startup-folded 'content)
  3598. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3599. (org-cycle '(4)) (org-cycle '(4)))))
  3600. (org-set-visibility-according-to-property 'no-cleanup)
  3601. (org-cycle-hide-archived-subtrees 'all)
  3602. (org-cycle-hide-drawers 'all)
  3603. (org-cycle-show-empty-lines 'all))
  3604. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3605. "Switch subtree visibilities according to :VISIBILITY: property."
  3606. (interactive)
  3607. (let (state)
  3608. (save-excursion
  3609. (goto-char (point-min))
  3610. (while (re-search-forward
  3611. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3612. nil t)
  3613. (setq state (match-string 1))
  3614. (save-excursion
  3615. (org-back-to-heading t)
  3616. (hide-subtree)
  3617. (org-reveal)
  3618. (cond
  3619. ((equal state '("fold" "folded"))
  3620. (hide-subtree))
  3621. ((equal state "children")
  3622. (org-show-hidden-entry)
  3623. (show-children))
  3624. ((equal state "content")
  3625. (save-excursion
  3626. (save-restriction
  3627. (org-narrow-to-subtree)
  3628. (org-content))))
  3629. ((member state '("all" "showall"))
  3630. (show-subtree)))))
  3631. (unless no-cleanup
  3632. (org-cycle-hide-archived-subtrees 'all)
  3633. (org-cycle-hide-drawers 'all)
  3634. (org-cycle-show-empty-lines 'all)))))
  3635. (defun org-overview ()
  3636. "Switch to overview mode, shoing only top-level headlines.
  3637. Really, this shows all headlines with level equal or greater than the level
  3638. of the first headline in the buffer. This is important, because if the
  3639. first headline is not level one, then (hide-sublevels 1) gives confusing
  3640. results."
  3641. (interactive)
  3642. (let ((level (save-excursion
  3643. (goto-char (point-min))
  3644. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3645. (progn
  3646. (goto-char (match-beginning 0))
  3647. (funcall outline-level))))))
  3648. (and level (hide-sublevels level))))
  3649. (defun org-content (&optional arg)
  3650. "Show all headlines in the buffer, like a table of contents.
  3651. With numerical argument N, show content up to level N."
  3652. (interactive "P")
  3653. (save-excursion
  3654. ;; Visit all headings and show their offspring
  3655. (and (integerp arg) (org-overview))
  3656. (goto-char (point-max))
  3657. (catch 'exit
  3658. (while (and (progn (condition-case nil
  3659. (outline-previous-visible-heading 1)
  3660. (error (goto-char (point-min))))
  3661. t)
  3662. (looking-at outline-regexp))
  3663. (if (integerp arg)
  3664. (show-children (1- arg))
  3665. (show-branches))
  3666. (if (bobp) (throw 'exit nil))))))
  3667. (defun org-optimize-window-after-visibility-change (state)
  3668. "Adjust the window after a change in outline visibility.
  3669. This function is the default value of the hook `org-cycle-hook'."
  3670. (when (get-buffer-window (current-buffer))
  3671. (cond
  3672. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3673. ; ((eq state 'overview) (org-beginning-of-line))
  3674. ((eq state 'content) nil)
  3675. ((eq state 'all) nil)
  3676. ((eq state 'folded) nil)
  3677. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3678. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3679. (defun org-compact-display-after-subtree-move ()
  3680. (let (beg end)
  3681. (save-excursion
  3682. (if (org-up-heading-safe)
  3683. (progn
  3684. (hide-subtree)
  3685. (show-entry)
  3686. (show-children)
  3687. (org-cycle-show-empty-lines 'children)
  3688. (org-cycle-hide-drawers 'children))
  3689. (org-overview)))))
  3690. (defun org-cycle-show-empty-lines (state)
  3691. "Show empty lines above all visible headlines.
  3692. The region to be covered depends on STATE when called through
  3693. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3694. entire buffer covered. Note that an empty line is only shown if there
  3695. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3696. (when (> org-cycle-separator-lines 0)
  3697. (save-excursion
  3698. (let* ((n org-cycle-separator-lines)
  3699. (re (cond
  3700. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3701. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3702. (t (let ((ns (number-to-string (- n 2))))
  3703. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3704. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3705. beg end)
  3706. (cond
  3707. ((memq state '(overview contents t))
  3708. (setq beg (point-min) end (point-max)))
  3709. ((memq state '(children folded))
  3710. (setq beg (point) end (progn (org-end-of-subtree t t)
  3711. (beginning-of-line 2)
  3712. (point)))))
  3713. (when beg
  3714. (goto-char beg)
  3715. (while (re-search-forward re end t)
  3716. (if (not (get-char-property (match-end 1) 'invisible))
  3717. (outline-flag-region
  3718. (match-beginning 1) (match-end 1) nil)))))))
  3719. ;; Never hide empty lines at the end of the file.
  3720. (save-excursion
  3721. (goto-char (point-max))
  3722. (outline-previous-heading)
  3723. (outline-end-of-heading)
  3724. (if (and (looking-at "[ \t\n]+")
  3725. (= (match-end 0) (point-max)))
  3726. (outline-flag-region (point) (match-end 0) nil))))
  3727. (defun org-cycle-hide-drawers (state)
  3728. "Re-hide all drawers after a visibility state change."
  3729. (when (and (org-mode-p)
  3730. (not (memq state '(overview folded))))
  3731. (save-excursion
  3732. (let* ((globalp (memq state '(contents all)))
  3733. (beg (if globalp (point-min) (point)))
  3734. (end (if globalp (point-max) (org-end-of-subtree t))))
  3735. (goto-char beg)
  3736. (while (re-search-forward org-drawer-regexp end t)
  3737. (org-flag-drawer t))))))
  3738. (defun org-flag-drawer (flag)
  3739. (save-excursion
  3740. (beginning-of-line 1)
  3741. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3742. (let ((b (match-end 0))
  3743. (outline-regexp org-outline-regexp))
  3744. (if (re-search-forward
  3745. "^[ \t]*:END:"
  3746. (save-excursion (outline-next-heading) (point)) t)
  3747. (outline-flag-region b (point-at-eol) flag)
  3748. (error ":END: line missing"))))))
  3749. (defun org-subtree-end-visible-p ()
  3750. "Is the end of the current subtree visible?"
  3751. (pos-visible-in-window-p
  3752. (save-excursion (org-end-of-subtree t) (point))))
  3753. (defun org-first-headline-recenter (&optional N)
  3754. "Move cursor to the first headline and recenter the headline.
  3755. Optional argument N means, put the headline into the Nth line of the window."
  3756. (goto-char (point-min))
  3757. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3758. (beginning-of-line)
  3759. (recenter (prefix-numeric-value N))))
  3760. ;;; Org-goto
  3761. (defvar org-goto-window-configuration nil)
  3762. (defvar org-goto-marker nil)
  3763. (defvar org-goto-map
  3764. (let ((map (make-sparse-keymap)))
  3765. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3766. (while (setq cmd (pop cmds))
  3767. (substitute-key-definition cmd cmd map global-map)))
  3768. (suppress-keymap map)
  3769. (org-defkey map "\C-m" 'org-goto-ret)
  3770. (org-defkey map [(return)] 'org-goto-ret)
  3771. (org-defkey map [(left)] 'org-goto-left)
  3772. (org-defkey map [(right)] 'org-goto-right)
  3773. (org-defkey map [(control ?g)] 'org-goto-quit)
  3774. (org-defkey map "\C-i" 'org-cycle)
  3775. (org-defkey map [(tab)] 'org-cycle)
  3776. (org-defkey map [(down)] 'outline-next-visible-heading)
  3777. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3778. (if org-goto-auto-isearch
  3779. (if (fboundp 'define-key-after)
  3780. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3781. nil)
  3782. (org-defkey map "q" 'org-goto-quit)
  3783. (org-defkey map "n" 'outline-next-visible-heading)
  3784. (org-defkey map "p" 'outline-previous-visible-heading)
  3785. (org-defkey map "f" 'outline-forward-same-level)
  3786. (org-defkey map "b" 'outline-backward-same-level)
  3787. (org-defkey map "u" 'outline-up-heading))
  3788. (org-defkey map "/" 'org-occur)
  3789. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3790. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3791. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3792. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3793. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3794. map))
  3795. (defconst org-goto-help
  3796. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3797. RET=jump to location [Q]uit and return to previous location
  3798. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3799. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3800. ;; FIXME: Docstring doe not mention both interfaces
  3801. (defun org-goto (&optional alternative-interface)
  3802. "Look up a different location in the current file, keeping current visibility.
  3803. When you want look-up or go to a different location in a document, the
  3804. fastest way is often to fold the entire buffer and then dive into the tree.
  3805. This method has the disadvantage, that the previous location will be folded,
  3806. which may not be what you want.
  3807. This command works around this by showing a copy of the current buffer
  3808. in an indirect buffer, in overview mode. You can dive into the tree in
  3809. that copy, use org-occur and incremental search to find a location.
  3810. When pressing RET or `Q', the command returns to the original buffer in
  3811. which the visibility is still unchanged. After RET is will also jump to
  3812. the location selected in the indirect buffer and expose the
  3813. the headline hierarchy above."
  3814. (interactive "P")
  3815. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3816. (org-refile-use-outline-path t)
  3817. (interface
  3818. (if (not alternative-interface)
  3819. org-goto-interface
  3820. (if (eq org-goto-interface 'outline)
  3821. 'outline-path-completion
  3822. 'outline)))
  3823. (org-goto-start-pos (point))
  3824. (selected-point
  3825. (if (eq interface 'outline)
  3826. (car (org-get-location (current-buffer) org-goto-help))
  3827. (nth 3 (org-refile-get-location "Goto: ")))))
  3828. (if selected-point
  3829. (progn
  3830. (org-mark-ring-push org-goto-start-pos)
  3831. (goto-char selected-point)
  3832. (if (or (org-invisible-p) (org-invisible-p2))
  3833. (org-show-context 'org-goto)))
  3834. (message "Quit"))))
  3835. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3836. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3837. (defvar org-goto-local-auto-isearch-map) ; defined below
  3838. (defun org-get-location (buf help)
  3839. "Let the user select a location in the Org-mode buffer BUF.
  3840. This function uses a recursive edit. It returns the selected position
  3841. or nil."
  3842. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3843. (isearch-hide-immediately nil)
  3844. (isearch-search-fun-function
  3845. (lambda () 'org-goto-local-search-forward-headings))
  3846. (org-goto-selected-point org-goto-exit-command))
  3847. (save-excursion
  3848. (save-window-excursion
  3849. (delete-other-windows)
  3850. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3851. (switch-to-buffer
  3852. (condition-case nil
  3853. (make-indirect-buffer (current-buffer) "*org-goto*")
  3854. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3855. (with-output-to-temp-buffer "*Help*"
  3856. (princ help))
  3857. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3858. (setq buffer-read-only nil)
  3859. (let ((org-startup-truncated t)
  3860. (org-startup-folded nil)
  3861. (org-startup-align-all-tables nil))
  3862. (org-mode)
  3863. (org-overview))
  3864. (setq buffer-read-only t)
  3865. (if (and (boundp 'org-goto-start-pos)
  3866. (integer-or-marker-p org-goto-start-pos))
  3867. (let ((org-show-hierarchy-above t)
  3868. (org-show-siblings t)
  3869. (org-show-following-heading t))
  3870. (goto-char org-goto-start-pos)
  3871. (and (org-invisible-p) (org-show-context)))
  3872. (goto-char (point-min)))
  3873. (org-beginning-of-line)
  3874. (message "Select location and press RET")
  3875. (use-local-map org-goto-map)
  3876. (recursive-edit)
  3877. ))
  3878. (kill-buffer "*org-goto*")
  3879. (cons org-goto-selected-point org-goto-exit-command)))
  3880. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3881. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3882. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3883. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3884. (defun org-goto-local-search-forward-headings (string bound noerror)
  3885. "Search and make sure that anu matches are in headlines."
  3886. (catch 'return
  3887. (while (search-forward string bound noerror)
  3888. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3889. (and (member :headline context)
  3890. (not (member :tags context))))
  3891. (throw 'return (point))))))
  3892. (defun org-goto-local-auto-isearch ()
  3893. "Start isearch."
  3894. (interactive)
  3895. (goto-char (point-min))
  3896. (let ((keys (this-command-keys)))
  3897. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3898. (isearch-mode t)
  3899. (isearch-process-search-char (string-to-char keys)))))
  3900. (defun org-goto-ret (&optional arg)
  3901. "Finish `org-goto' by going to the new location."
  3902. (interactive "P")
  3903. (setq org-goto-selected-point (point)
  3904. org-goto-exit-command 'return)
  3905. (throw 'exit nil))
  3906. (defun org-goto-left ()
  3907. "Finish `org-goto' by going to the new location."
  3908. (interactive)
  3909. (if (org-on-heading-p)
  3910. (progn
  3911. (beginning-of-line 1)
  3912. (setq org-goto-selected-point (point)
  3913. org-goto-exit-command 'left)
  3914. (throw 'exit nil))
  3915. (error "Not on a heading")))
  3916. (defun org-goto-right ()
  3917. "Finish `org-goto' by going to the new location."
  3918. (interactive)
  3919. (if (org-on-heading-p)
  3920. (progn
  3921. (setq org-goto-selected-point (point)
  3922. org-goto-exit-command 'right)
  3923. (throw 'exit nil))
  3924. (error "Not on a heading")))
  3925. (defun org-goto-quit ()
  3926. "Finish `org-goto' without cursor motion."
  3927. (interactive)
  3928. (setq org-goto-selected-point nil)
  3929. (setq org-goto-exit-command 'quit)
  3930. (throw 'exit nil))
  3931. ;;; Indirect buffer display of subtrees
  3932. (defvar org-indirect-dedicated-frame nil
  3933. "This is the frame being used for indirect tree display.")
  3934. (defvar org-last-indirect-buffer nil)
  3935. (defun org-tree-to-indirect-buffer (&optional arg)
  3936. "Create indirect buffer and narrow it to current subtree.
  3937. With numerical prefix ARG, go up to this level and then take that tree.
  3938. If ARG is negative, go up that many levels.
  3939. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3940. indirect buffer previously made with this command, to avoid proliferation of
  3941. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3942. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3943. is kept so that you can work with several indirect buffers at the same time.
  3944. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3945. requests that a new frame be made for the new buffer, so that the dedicated
  3946. frame is not changed."
  3947. (interactive "P")
  3948. (let ((cbuf (current-buffer))
  3949. (cwin (selected-window))
  3950. (pos (point))
  3951. beg end level heading ibuf)
  3952. (save-excursion
  3953. (org-back-to-heading t)
  3954. (when (numberp arg)
  3955. (setq level (org-outline-level))
  3956. (if (< arg 0) (setq arg (+ level arg)))
  3957. (while (> (setq level (org-outline-level)) arg)
  3958. (outline-up-heading 1 t)))
  3959. (setq beg (point)
  3960. heading (org-get-heading))
  3961. (org-end-of-subtree t) (setq end (point)))
  3962. (if (and (buffer-live-p org-last-indirect-buffer)
  3963. (not (eq org-indirect-buffer-display 'new-frame))
  3964. (not arg))
  3965. (kill-buffer org-last-indirect-buffer))
  3966. (setq ibuf (org-get-indirect-buffer cbuf)
  3967. org-last-indirect-buffer ibuf)
  3968. (cond
  3969. ((or (eq org-indirect-buffer-display 'new-frame)
  3970. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3971. (select-frame (make-frame))
  3972. (delete-other-windows)
  3973. (switch-to-buffer ibuf)
  3974. (org-set-frame-title heading))
  3975. ((eq org-indirect-buffer-display 'dedicated-frame)
  3976. (raise-frame
  3977. (select-frame (or (and org-indirect-dedicated-frame
  3978. (frame-live-p org-indirect-dedicated-frame)
  3979. org-indirect-dedicated-frame)
  3980. (setq org-indirect-dedicated-frame (make-frame)))))
  3981. (delete-other-windows)
  3982. (switch-to-buffer ibuf)
  3983. (org-set-frame-title (concat "Indirect: " heading)))
  3984. ((eq org-indirect-buffer-display 'current-window)
  3985. (switch-to-buffer ibuf))
  3986. ((eq org-indirect-buffer-display 'other-window)
  3987. (pop-to-buffer ibuf))
  3988. (t (error "Invalid value.")))
  3989. (if (featurep 'xemacs)
  3990. (save-excursion (org-mode) (turn-on-font-lock)))
  3991. (narrow-to-region beg end)
  3992. (show-all)
  3993. (goto-char pos)
  3994. (and (window-live-p cwin) (select-window cwin))))
  3995. (defun org-get-indirect-buffer (&optional buffer)
  3996. (setq buffer (or buffer (current-buffer)))
  3997. (let ((n 1) (base (buffer-name buffer)) bname)
  3998. (while (buffer-live-p
  3999. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4000. (setq n (1+ n)))
  4001. (condition-case nil
  4002. (make-indirect-buffer buffer bname 'clone)
  4003. (error (make-indirect-buffer buffer bname)))))
  4004. (defun org-set-frame-title (title)
  4005. "Set the title of the current frame to the string TITLE."
  4006. ;; FIXME: how to name a single frame in XEmacs???
  4007. (unless (featurep 'xemacs)
  4008. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4009. ;;;; Structure editing
  4010. ;;; Inserting headlines
  4011. (defun org-insert-heading (&optional force-heading)
  4012. "Insert a new heading or item with same depth at point.
  4013. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4014. If point is at the beginning of a headline, insert a sibling before the
  4015. current headline. If point is not at the beginning, do not split the line,
  4016. but create the new hedline after the current line."
  4017. (interactive "P")
  4018. (if (= (buffer-size) 0)
  4019. (insert "\n* ")
  4020. (when (or force-heading (not (org-insert-item)))
  4021. (let* ((head (save-excursion
  4022. (condition-case nil
  4023. (progn
  4024. (org-back-to-heading)
  4025. (match-string 0))
  4026. (error "*"))))
  4027. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4028. pos)
  4029. (cond
  4030. ((and (org-on-heading-p) (bolp)
  4031. (or (bobp)
  4032. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4033. ;; insert before the current line
  4034. (open-line (if blank 2 1)))
  4035. ((and (bolp)
  4036. (or (bobp)
  4037. (save-excursion
  4038. (backward-char 1) (not (org-invisible-p)))))
  4039. ;; insert right here
  4040. nil)
  4041. (t
  4042. ;; in the middle of the line
  4043. (org-show-entry)
  4044. (let ((split
  4045. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4046. tags pos)
  4047. (if (org-on-heading-p)
  4048. (progn
  4049. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4050. (setq tags (and (match-end 2) (match-string 2)))
  4051. (and (match-end 1)
  4052. (delete-region (match-beginning 1) (match-end 1)))
  4053. (setq pos (point-at-bol))
  4054. (or split (end-of-line 1))
  4055. (delete-horizontal-space)
  4056. (newline (if blank 2 1))
  4057. (when tags
  4058. (save-excursion
  4059. (goto-char pos)
  4060. (end-of-line 1)
  4061. (insert " " tags)
  4062. (org-set-tags nil 'align))))
  4063. (or split (end-of-line 1))
  4064. (newline (if blank 2 1))))))
  4065. (insert head) (just-one-space)
  4066. (setq pos (point))
  4067. (end-of-line 1)
  4068. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4069. (run-hooks 'org-insert-heading-hook)))))
  4070. (defun org-get-heading (&optional no-tags)
  4071. "Return the heading of the current entry, without the stars."
  4072. (save-excursion
  4073. (org-back-to-heading t)
  4074. (if (looking-at
  4075. (if no-tags
  4076. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4077. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4078. (match-string 1) "")))
  4079. (defun org-insert-heading-after-current ()
  4080. "Insert a new heading with same level as current, after current subtree."
  4081. (interactive)
  4082. (org-back-to-heading)
  4083. (org-insert-heading)
  4084. (org-move-subtree-down)
  4085. (end-of-line 1))
  4086. (defun org-insert-todo-heading (arg)
  4087. "Insert a new heading with the same level and TODO state as current heading.
  4088. If the heading has no TODO state, or if the state is DONE, use the first
  4089. state (TODO by default). Also with prefix arg, force first state."
  4090. (interactive "P")
  4091. (when (not (org-insert-item 'checkbox))
  4092. (org-insert-heading)
  4093. (save-excursion
  4094. (org-back-to-heading)
  4095. (outline-previous-heading)
  4096. (looking-at org-todo-line-regexp))
  4097. (if (or arg
  4098. (not (match-beginning 2))
  4099. (member (match-string 2) org-done-keywords))
  4100. (insert (car org-todo-keywords-1) " ")
  4101. (insert (match-string 2) " "))
  4102. (when org-provide-todo-statistics
  4103. (org-update-parent-todo-statistics))))
  4104. (defun org-insert-subheading (arg)
  4105. "Insert a new subheading and demote it.
  4106. Works for outline headings and for plain lists alike."
  4107. (interactive "P")
  4108. (org-insert-heading arg)
  4109. (cond
  4110. ((org-on-heading-p) (org-do-demote))
  4111. ((org-at-item-p) (org-indent-item 1))))
  4112. (defun org-insert-todo-subheading (arg)
  4113. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4114. Works for outline headings and for plain lists alike."
  4115. (interactive "P")
  4116. (org-insert-todo-heading arg)
  4117. (cond
  4118. ((org-on-heading-p) (org-do-demote))
  4119. ((org-at-item-p) (org-indent-item 1))))
  4120. ;;; Promotion and Demotion
  4121. (defun org-promote-subtree ()
  4122. "Promote the entire subtree.
  4123. See also `org-promote'."
  4124. (interactive)
  4125. (save-excursion
  4126. (org-map-tree 'org-promote))
  4127. (org-fix-position-after-promote))
  4128. (defun org-demote-subtree ()
  4129. "Demote the entire subtree. See `org-demote'.
  4130. See also `org-promote'."
  4131. (interactive)
  4132. (save-excursion
  4133. (org-map-tree 'org-demote))
  4134. (org-fix-position-after-promote))
  4135. (defun org-do-promote ()
  4136. "Promote the current heading higher up the tree.
  4137. If the region is active in `transient-mark-mode', promote all headings
  4138. in the region."
  4139. (interactive)
  4140. (save-excursion
  4141. (if (org-region-active-p)
  4142. (org-map-region 'org-promote (region-beginning) (region-end))
  4143. (org-promote)))
  4144. (org-fix-position-after-promote))
  4145. (defun org-do-demote ()
  4146. "Demote the current heading lower down the tree.
  4147. If the region is active in `transient-mark-mode', demote all headings
  4148. in the region."
  4149. (interactive)
  4150. (save-excursion
  4151. (if (org-region-active-p)
  4152. (org-map-region 'org-demote (region-beginning) (region-end))
  4153. (org-demote)))
  4154. (org-fix-position-after-promote))
  4155. (defun org-fix-position-after-promote ()
  4156. "Make sure that after pro/demotion cursor position is right."
  4157. (let ((pos (point)))
  4158. (when (save-excursion
  4159. (beginning-of-line 1)
  4160. (looking-at org-todo-line-regexp)
  4161. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4162. (cond ((eobp) (insert " "))
  4163. ((eolp) (insert " "))
  4164. ((equal (char-after) ?\ ) (forward-char 1))))))
  4165. (defun org-reduced-level (l)
  4166. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4167. (defun org-get-valid-level (level &optional change)
  4168. "Rectify a level change under the influence of `org-odd-levels-only'
  4169. LEVEL is a current level, CHANGE is by how much the level should be
  4170. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4171. even level numbers will become the next higher odd number."
  4172. (if org-odd-levels-only
  4173. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4174. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4175. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4176. (max 1 (+ level change))))
  4177. (if (boundp 'define-obsolete-function-alias)
  4178. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4179. (define-obsolete-function-alias 'org-get-legal-level
  4180. 'org-get-valid-level)
  4181. (define-obsolete-function-alias 'org-get-legal-level
  4182. 'org-get-valid-level "23.1")))
  4183. (defun org-promote ()
  4184. "Promote the current heading higher up the tree.
  4185. If the region is active in `transient-mark-mode', promote all headings
  4186. in the region."
  4187. (org-back-to-heading t)
  4188. (let* ((level (save-match-data (funcall outline-level)))
  4189. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4190. (diff (abs (- level (length up-head) -1))))
  4191. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4192. (replace-match up-head nil t)
  4193. ;; Fixup tag positioning
  4194. (and org-auto-align-tags (org-set-tags nil t))
  4195. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4196. (defun org-demote ()
  4197. "Demote the current heading lower down the tree.
  4198. If the region is active in `transient-mark-mode', demote all headings
  4199. in the region."
  4200. (org-back-to-heading t)
  4201. (let* ((level (save-match-data (funcall outline-level)))
  4202. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4203. (diff (abs (- level (length down-head) -1))))
  4204. (replace-match down-head nil t)
  4205. ;; Fixup tag positioning
  4206. (and org-auto-align-tags (org-set-tags nil t))
  4207. (if org-adapt-indentation (org-fixup-indentation diff))))
  4208. (defun org-map-tree (fun)
  4209. "Call FUN for every heading underneath the current one."
  4210. (org-back-to-heading)
  4211. (let ((level (funcall outline-level)))
  4212. (save-excursion
  4213. (funcall fun)
  4214. (while (and (progn
  4215. (outline-next-heading)
  4216. (> (funcall outline-level) level))
  4217. (not (eobp)))
  4218. (funcall fun)))))
  4219. (defun org-map-region (fun beg end)
  4220. "Call FUN for every heading between BEG and END."
  4221. (let ((org-ignore-region t))
  4222. (save-excursion
  4223. (setq end (copy-marker end))
  4224. (goto-char beg)
  4225. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4226. (< (point) end))
  4227. (funcall fun))
  4228. (while (and (progn
  4229. (outline-next-heading)
  4230. (< (point) end))
  4231. (not (eobp)))
  4232. (funcall fun)))))
  4233. (defun org-fixup-indentation (diff)
  4234. "Change the indentation in the current entry by DIFF
  4235. However, if any line in the current entry has no indentation, or if it
  4236. would end up with no indentation after the change, nothing at all is done."
  4237. (save-excursion
  4238. (let ((end (save-excursion (outline-next-heading)
  4239. (point-marker)))
  4240. (prohibit (if (> diff 0)
  4241. "^\\S-"
  4242. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4243. col)
  4244. (unless (save-excursion (end-of-line 1)
  4245. (re-search-forward prohibit end t))
  4246. (while (and (< (point) end)
  4247. (re-search-forward "^[ \t]+" end t))
  4248. (goto-char (match-end 0))
  4249. (setq col (current-column))
  4250. (if (< diff 0) (replace-match ""))
  4251. (indent-to (+ diff col))))
  4252. (move-marker end nil))))
  4253. (defun org-convert-to-odd-levels ()
  4254. "Convert an org-mode file with all levels allowed to one with odd levels.
  4255. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4256. level 5 etc."
  4257. (interactive)
  4258. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4259. (let ((org-odd-levels-only nil) n)
  4260. (save-excursion
  4261. (goto-char (point-min))
  4262. (while (re-search-forward "^\\*\\*+ " nil t)
  4263. (setq n (- (length (match-string 0)) 2))
  4264. (while (>= (setq n (1- n)) 0)
  4265. (org-demote))
  4266. (end-of-line 1))))))
  4267. (defun org-convert-to-oddeven-levels ()
  4268. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4269. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4270. section with an even level, conversion would destroy the structure of the file. An error
  4271. is signaled in this case."
  4272. (interactive)
  4273. (goto-char (point-min))
  4274. ;; First check if there are no even levels
  4275. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4276. (org-show-context t)
  4277. (error "Not all levels are odd in this file. Conversion not possible."))
  4278. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4279. (let ((org-odd-levels-only nil) n)
  4280. (save-excursion
  4281. (goto-char (point-min))
  4282. (while (re-search-forward "^\\*\\*+ " nil t)
  4283. (setq n (/ (1- (length (match-string 0))) 2))
  4284. (while (>= (setq n (1- n)) 0)
  4285. (org-promote))
  4286. (end-of-line 1))))))
  4287. (defun org-tr-level (n)
  4288. "Make N odd if required."
  4289. (if org-odd-levels-only (1+ (/ n 2)) n))
  4290. ;;; Vertical tree motion, cutting and pasting of subtrees
  4291. (defun org-move-subtree-up (&optional arg)
  4292. "Move the current subtree up past ARG headlines of the same level."
  4293. (interactive "p")
  4294. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4295. (defun org-move-subtree-down (&optional arg)
  4296. "Move the current subtree down past ARG headlines of the same level."
  4297. (interactive "p")
  4298. (setq arg (prefix-numeric-value arg))
  4299. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4300. 'outline-get-last-sibling))
  4301. (ins-point (make-marker))
  4302. (cnt (abs arg))
  4303. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4304. ;; Select the tree
  4305. (org-back-to-heading)
  4306. (setq beg0 (point))
  4307. (save-excursion
  4308. (setq ne-beg (org-back-over-empty-lines))
  4309. (setq beg (point)))
  4310. (save-match-data
  4311. (save-excursion (outline-end-of-heading)
  4312. (setq folded (org-invisible-p)))
  4313. (outline-end-of-subtree))
  4314. (outline-next-heading)
  4315. (setq ne-end (org-back-over-empty-lines))
  4316. (setq end (point))
  4317. (goto-char beg0)
  4318. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4319. ;; include less whitespace
  4320. (save-excursion
  4321. (goto-char beg)
  4322. (forward-line (- ne-beg ne-end))
  4323. (setq beg (point))))
  4324. ;; Find insertion point, with error handling
  4325. (while (> cnt 0)
  4326. (or (and (funcall movfunc) (looking-at outline-regexp))
  4327. (progn (goto-char beg0)
  4328. (error "Cannot move past superior level or buffer limit")))
  4329. (setq cnt (1- cnt)))
  4330. (if (> arg 0)
  4331. ;; Moving forward - still need to move over subtree
  4332. (progn (org-end-of-subtree t t)
  4333. (save-excursion
  4334. (org-back-over-empty-lines)
  4335. (or (bolp) (newline)))))
  4336. (setq ne-ins (org-back-over-empty-lines))
  4337. (move-marker ins-point (point))
  4338. (setq txt (buffer-substring beg end))
  4339. (org-save-markers-in-region beg end)
  4340. (delete-region beg end)
  4341. (outline-flag-region (1- beg) beg nil)
  4342. (outline-flag-region (1- (point)) (point) nil)
  4343. (let ((bbb (point)))
  4344. (insert-before-markers txt)
  4345. (org-reinstall-markers-in-region bbb)
  4346. (move-marker ins-point bbb))
  4347. (or (bolp) (insert "\n"))
  4348. (setq ins-end (point))
  4349. (goto-char ins-point)
  4350. (org-skip-whitespace)
  4351. (when (and (< arg 0)
  4352. (org-first-sibling-p)
  4353. (> ne-ins ne-beg))
  4354. ;; Move whitespace back to beginning
  4355. (save-excursion
  4356. (goto-char ins-end)
  4357. (let ((kill-whole-line t))
  4358. (kill-line (- ne-ins ne-beg)) (point)))
  4359. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4360. (move-marker ins-point nil)
  4361. (org-compact-display-after-subtree-move)
  4362. (unless folded
  4363. (org-show-entry)
  4364. (show-children)
  4365. (org-cycle-hide-drawers 'children))))
  4366. (defvar org-subtree-clip ""
  4367. "Clipboard for cut and paste of subtrees.
  4368. This is actually only a copy of the kill, because we use the normal kill
  4369. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4370. (defvar org-subtree-clip-folded nil
  4371. "Was the last copied subtree folded?
  4372. This is used to fold the tree back after pasting.")
  4373. (defun org-cut-subtree (&optional n)
  4374. "Cut the current subtree into the clipboard.
  4375. With prefix arg N, cut this many sequential subtrees.
  4376. This is a short-hand for marking the subtree and then cutting it."
  4377. (interactive "p")
  4378. (org-copy-subtree n 'cut))
  4379. (defun org-copy-subtree (&optional n cut force-store-markers)
  4380. "Cut the current subtree into the clipboard.
  4381. With prefix arg N, cut this many sequential subtrees.
  4382. This is a short-hand for marking the subtree and then copying it.
  4383. If CUT is non-nil, actually cut the subtree.
  4384. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4385. of some markers in the region, even if CUT is non-nil. This is
  4386. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4387. (interactive "p")
  4388. (let (beg end folded (beg0 (point)))
  4389. (if (interactive-p)
  4390. (org-back-to-heading nil) ; take what looks like a subtree
  4391. (org-back-to-heading t)) ; take what is really there
  4392. (org-back-over-empty-lines)
  4393. (setq beg (point))
  4394. (skip-chars-forward " \t\r\n")
  4395. (save-match-data
  4396. (save-excursion (outline-end-of-heading)
  4397. (setq folded (org-invisible-p)))
  4398. (condition-case nil
  4399. (outline-forward-same-level (1- n))
  4400. (error nil))
  4401. (org-end-of-subtree t t))
  4402. (org-back-over-empty-lines)
  4403. (setq end (point))
  4404. (goto-char beg0)
  4405. (when (> end beg)
  4406. (setq org-subtree-clip-folded folded)
  4407. (when (or cut force-store-markers)
  4408. (org-save-markers-in-region beg end))
  4409. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4410. (setq org-subtree-clip (current-kill 0))
  4411. (message "%s: Subtree(s) with %d characters"
  4412. (if cut "Cut" "Copied")
  4413. (length org-subtree-clip)))))
  4414. (defun org-paste-subtree (&optional level tree)
  4415. "Paste the clipboard as a subtree, with modification of headline level.
  4416. The entire subtree is promoted or demoted in order to match a new headline
  4417. level. By default, the new level is derived from the visible headings
  4418. before and after the insertion point, and taken to be the inferior headline
  4419. level of the two. So if the previous visible heading is level 3 and the
  4420. next is level 4 (or vice versa), level 4 will be used for insertion.
  4421. This makes sure that the subtree remains an independent subtree and does
  4422. not swallow low level entries.
  4423. You can also force a different level, either by using a numeric prefix
  4424. argument, or by inserting the heading marker by hand. For example, if the
  4425. cursor is after \"*****\", then the tree will be shifted to level 5.
  4426. If you want to insert the tree as is, just use \\[yank].
  4427. If optional TREE is given, use this text instead of the kill ring."
  4428. (interactive "P")
  4429. (unless (org-kill-is-subtree-p tree)
  4430. (error "%s"
  4431. (substitute-command-keys
  4432. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4433. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4434. (^re (concat "^\\(" outline-regexp "\\)"))
  4435. (re (concat "\\(" outline-regexp "\\)"))
  4436. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4437. (old-level (if (string-match ^re txt)
  4438. (- (match-end 0) (match-beginning 0) 1)
  4439. -1))
  4440. (force-level (cond (level (prefix-numeric-value level))
  4441. ((string-match
  4442. ^re_ (buffer-substring (point-at-bol) (point)))
  4443. (- (match-end 1) (match-beginning 1)))
  4444. (t nil)))
  4445. (previous-level (save-excursion
  4446. (condition-case nil
  4447. (progn
  4448. (outline-previous-visible-heading 1)
  4449. (if (looking-at re)
  4450. (- (match-end 0) (match-beginning 0) 1)
  4451. 1))
  4452. (error 1))))
  4453. (next-level (save-excursion
  4454. (condition-case nil
  4455. (progn
  4456. (or (looking-at outline-regexp)
  4457. (outline-next-visible-heading 1))
  4458. (if (looking-at re)
  4459. (- (match-end 0) (match-beginning 0) 1)
  4460. 1))
  4461. (error 1))))
  4462. (new-level (or force-level (max previous-level next-level)))
  4463. (shift (if (or (= old-level -1)
  4464. (= new-level -1)
  4465. (= old-level new-level))
  4466. 0
  4467. (- new-level old-level)))
  4468. (delta (if (> shift 0) -1 1))
  4469. (func (if (> shift 0) 'org-demote 'org-promote))
  4470. (org-odd-levels-only nil)
  4471. beg end)
  4472. ;; Remove the forced level indicator
  4473. (if force-level
  4474. (delete-region (point-at-bol) (point)))
  4475. ;; Paste
  4476. (beginning-of-line 1)
  4477. (org-back-over-empty-lines)
  4478. (setq beg (point))
  4479. (insert-before-markers txt)
  4480. (unless (string-match "\n\\'" txt) (insert "\n"))
  4481. (org-reinstall-markers-in-region beg)
  4482. (setq end (point))
  4483. (goto-char beg)
  4484. (skip-chars-forward " \t\n\r")
  4485. (setq beg (point))
  4486. ;; Shift if necessary
  4487. (unless (= shift 0)
  4488. (save-restriction
  4489. (narrow-to-region beg end)
  4490. (while (not (= shift 0))
  4491. (org-map-region func (point-min) (point-max))
  4492. (setq shift (+ delta shift)))
  4493. (goto-char (point-min))))
  4494. (when (interactive-p)
  4495. (message "Clipboard pasted as level %d subtree" new-level))
  4496. (if (and kill-ring
  4497. (eq org-subtree-clip (current-kill 0))
  4498. org-subtree-clip-folded)
  4499. ;; The tree was folded before it was killed/copied
  4500. (hide-subtree))))
  4501. (defun org-kill-is-subtree-p (&optional txt)
  4502. "Check if the current kill is an outline subtree, or a set of trees.
  4503. Returns nil if kill does not start with a headline, or if the first
  4504. headline level is not the largest headline level in the tree.
  4505. So this will actually accept several entries of equal levels as well,
  4506. which is OK for `org-paste-subtree'.
  4507. If optional TXT is given, check this string instead of the current kill."
  4508. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4509. (start-level (and kill
  4510. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4511. org-outline-regexp "\\)")
  4512. kill)
  4513. (- (match-end 2) (match-beginning 2) 1)))
  4514. (re (concat "^" org-outline-regexp))
  4515. (start (1+ (match-beginning 2))))
  4516. (if (not start-level)
  4517. (progn
  4518. nil) ;; does not even start with a heading
  4519. (catch 'exit
  4520. (while (setq start (string-match re kill (1+ start)))
  4521. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4522. (throw 'exit nil)))
  4523. t))))
  4524. (defvar org-markers-to-move nil
  4525. "Markers that should be moved with a cut-and-paste operation.
  4526. Those markers are stored together with their positions relative to
  4527. the start of the region.")
  4528. (defun org-save-markers-in-region (beg end)
  4529. "Check markers in region.
  4530. If these markers are between BEG and END, record their position relative
  4531. to BEG, so that after moving the block of text, we can put the markers back
  4532. into place.
  4533. This function gets called just before an entry or tree gets cut from the
  4534. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4535. called immediately, to move the markers with the entries."
  4536. (setq org-markers-to-move nil)
  4537. (when (featurep 'org-clock)
  4538. (org-clock-save-markers-for-cut-and-paste beg end))
  4539. (when (featurep 'org-agenda)
  4540. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4541. (defun org-check-and-save-marker (marker beg end)
  4542. "Check if MARKER is between BEG and END.
  4543. If yes, remember the marker and the distance to BEG."
  4544. (when (and (marker-buffer marker)
  4545. (equal (marker-buffer marker) (current-buffer)))
  4546. (if (and (>= marker beg) (< marker end))
  4547. (push (cons marker (- marker beg)) org-markers-to-move))))
  4548. (defun org-reinstall-markers-in-region (beg)
  4549. "Move all remembered markers to their position relative to BEG."
  4550. (mapc (lambda (x)
  4551. (move-marker (car x) (+ beg (cdr x))))
  4552. org-markers-to-move)
  4553. (setq org-markers-to-move nil))
  4554. (defun org-narrow-to-subtree ()
  4555. "Narrow buffer to the current subtree."
  4556. (interactive)
  4557. (save-excursion
  4558. (save-match-data
  4559. (narrow-to-region
  4560. (progn (org-back-to-heading) (point))
  4561. (progn (org-end-of-subtree t t) (point))))))
  4562. ;;; Outline Sorting
  4563. (defun org-sort (with-case)
  4564. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4565. Optional argument WITH-CASE means sort case-sensitively."
  4566. (interactive "P")
  4567. (if (org-at-table-p)
  4568. (org-call-with-arg 'org-table-sort-lines with-case)
  4569. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4570. (defun org-sort-remove-invisible (s)
  4571. (remove-text-properties 0 (length s) org-rm-props s)
  4572. (while (string-match org-bracket-link-regexp s)
  4573. (setq s (replace-match (if (match-end 2)
  4574. (match-string 3 s)
  4575. (match-string 1 s)) t t s)))
  4576. s)
  4577. (defvar org-priority-regexp) ; defined later in the file
  4578. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4579. "Sort entries on a certain level of an outline tree.
  4580. If there is an active region, the entries in the region are sorted.
  4581. Else, if the cursor is before the first entry, sort the top-level items.
  4582. Else, the children of the entry at point are sorted.
  4583. Sorting can be alphabetically, numerically, and by date/time as given by
  4584. the first time stamp in the entry. The command prompts for the sorting
  4585. type unless it has been given to the function through the SORTING-TYPE
  4586. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4587. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4588. called with point at the beginning of the record. It must return either
  4589. a string or a number that should serve as the sorting key for that record.
  4590. Comparing entries ignores case by default. However, with an optional argument
  4591. WITH-CASE, the sorting considers case as well."
  4592. (interactive "P")
  4593. (let ((case-func (if with-case 'identity 'downcase))
  4594. start beg end stars re re2
  4595. txt what tmp plain-list-p)
  4596. ;; Find beginning and end of region to sort
  4597. (cond
  4598. ((org-region-active-p)
  4599. ;; we will sort the region
  4600. (setq end (region-end)
  4601. what "region")
  4602. (goto-char (region-beginning))
  4603. (if (not (org-on-heading-p)) (outline-next-heading))
  4604. (setq start (point)))
  4605. ((org-at-item-p)
  4606. ;; we will sort this plain list
  4607. (org-beginning-of-item-list) (setq start (point))
  4608. (org-end-of-item-list) (setq end (point))
  4609. (goto-char start)
  4610. (setq plain-list-p t
  4611. what "plain list"))
  4612. ((or (org-on-heading-p)
  4613. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4614. ;; we will sort the children of the current headline
  4615. (org-back-to-heading)
  4616. (setq start (point)
  4617. end (progn (org-end-of-subtree t t)
  4618. (org-back-over-empty-lines)
  4619. (point))
  4620. what "children")
  4621. (goto-char start)
  4622. (show-subtree)
  4623. (outline-next-heading))
  4624. (t
  4625. ;; we will sort the top-level entries in this file
  4626. (goto-char (point-min))
  4627. (or (org-on-heading-p) (outline-next-heading))
  4628. (setq start (point) end (point-max) what "top-level")
  4629. (goto-char start)
  4630. (show-all)))
  4631. (setq beg (point))
  4632. (if (>= beg end) (error "Nothing to sort"))
  4633. (unless plain-list-p
  4634. (looking-at "\\(\\*+\\)")
  4635. (setq stars (match-string 1)
  4636. re (concat "^" (regexp-quote stars) " +")
  4637. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4638. txt (buffer-substring beg end))
  4639. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4640. (if (and (not (equal stars "*")) (string-match re2 txt))
  4641. (error "Region to sort contains a level above the first entry")))
  4642. (unless sorting-type
  4643. (message
  4644. (if plain-list-p
  4645. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4646. "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:")
  4647. what)
  4648. (setq sorting-type (read-char-exclusive))
  4649. (and (= (downcase sorting-type) ?f)
  4650. (setq getkey-func
  4651. (completing-read "Sort using function: "
  4652. obarray 'fboundp t nil nil))
  4653. (setq getkey-func (intern getkey-func)))
  4654. (and (= (downcase sorting-type) ?r)
  4655. (setq property
  4656. (completing-read "Property: "
  4657. (mapcar 'list (org-buffer-property-keys t))
  4658. nil t))))
  4659. (message "Sorting entries...")
  4660. (save-restriction
  4661. (narrow-to-region start end)
  4662. (let ((dcst (downcase sorting-type))
  4663. (now (current-time)))
  4664. (sort-subr
  4665. (/= dcst sorting-type)
  4666. ;; This function moves to the beginning character of the "record" to
  4667. ;; be sorted.
  4668. (if plain-list-p
  4669. (lambda nil
  4670. (if (org-at-item-p) t (goto-char (point-max))))
  4671. (lambda nil
  4672. (if (re-search-forward re nil t)
  4673. (goto-char (match-beginning 0))
  4674. (goto-char (point-max)))))
  4675. ;; This function moves to the last character of the "record" being
  4676. ;; sorted.
  4677. (if plain-list-p
  4678. 'org-end-of-item
  4679. (lambda nil
  4680. (save-match-data
  4681. (condition-case nil
  4682. (outline-forward-same-level 1)
  4683. (error
  4684. (goto-char (point-max)))))))
  4685. ;; This function returns the value that gets sorted against.
  4686. (if plain-list-p
  4687. (lambda nil
  4688. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4689. (cond
  4690. ((= dcst ?n)
  4691. (string-to-number (buffer-substring (match-end 0)
  4692. (point-at-eol))))
  4693. ((= dcst ?a)
  4694. (buffer-substring (match-end 0) (point-at-eol)))
  4695. ((= dcst ?t)
  4696. (if (re-search-forward org-ts-regexp
  4697. (point-at-eol) t)
  4698. (org-time-string-to-time (match-string 0))
  4699. now))
  4700. ((= dcst ?f)
  4701. (if getkey-func
  4702. (progn
  4703. (setq tmp (funcall getkey-func))
  4704. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4705. tmp)
  4706. (error "Invalid key function `%s'" getkey-func)))
  4707. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4708. (lambda nil
  4709. (cond
  4710. ((= dcst ?n)
  4711. (if (looking-at outline-regexp)
  4712. (string-to-number (buffer-substring (match-end 0)
  4713. (point-at-eol)))
  4714. nil))
  4715. ((= dcst ?a)
  4716. (funcall case-func (buffer-substring (point-at-bol)
  4717. (point-at-eol))))
  4718. ((= dcst ?t)
  4719. (if (re-search-forward org-ts-regexp
  4720. (save-excursion
  4721. (forward-line 2)
  4722. (point)) t)
  4723. (org-time-string-to-time (match-string 0))
  4724. now))
  4725. ((= dcst ?p)
  4726. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4727. (string-to-char (match-string 2))
  4728. org-default-priority))
  4729. ((= dcst ?r)
  4730. (or (org-entry-get nil property) ""))
  4731. ((= dcst ?o)
  4732. (if (looking-at org-complex-heading-regexp)
  4733. (- 9999 (length (member (match-string 2)
  4734. org-todo-keywords-1)))))
  4735. ((= dcst ?f)
  4736. (if getkey-func
  4737. (progn
  4738. (setq tmp (funcall getkey-func))
  4739. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4740. tmp)
  4741. (error "Invalid key function `%s'" getkey-func)))
  4742. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4743. nil
  4744. (cond
  4745. ((= dcst ?a) 'string<)
  4746. ((= dcst ?t) 'time-less-p)
  4747. (t nil)))))
  4748. (message "Sorting entries...done")))
  4749. (defun org-do-sort (table what &optional with-case sorting-type)
  4750. "Sort TABLE of WHAT according to SORTING-TYPE.
  4751. The user will be prompted for the SORTING-TYPE if the call to this
  4752. function does not specify it. WHAT is only for the prompt, to indicate
  4753. what is being sorted. The sorting key will be extracted from
  4754. the car of the elements of the table.
  4755. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4756. (unless sorting-type
  4757. (message
  4758. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4759. what)
  4760. (setq sorting-type (read-char-exclusive)))
  4761. (let ((dcst (downcase sorting-type))
  4762. extractfun comparefun)
  4763. ;; Define the appropriate functions
  4764. (cond
  4765. ((= dcst ?n)
  4766. (setq extractfun 'string-to-number
  4767. comparefun (if (= dcst sorting-type) '< '>)))
  4768. ((= dcst ?a)
  4769. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4770. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4771. comparefun (if (= dcst sorting-type)
  4772. 'string<
  4773. (lambda (a b) (and (not (string< a b))
  4774. (not (string= a b)))))))
  4775. ((= dcst ?t)
  4776. (setq extractfun
  4777. (lambda (x)
  4778. (if (string-match org-ts-regexp x)
  4779. (time-to-seconds
  4780. (org-time-string-to-time (match-string 0 x)))
  4781. 0))
  4782. comparefun (if (= dcst sorting-type) '< '>)))
  4783. (t (error "Invalid sorting type `%c'" sorting-type)))
  4784. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4785. table)
  4786. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4787. ;;;; Plain list items, including checkboxes
  4788. ;;; Plain list items
  4789. (defun org-at-item-p ()
  4790. "Is point in a line starting a hand-formatted item?"
  4791. (let ((llt org-plain-list-ordered-item-terminator))
  4792. (save-excursion
  4793. (goto-char (point-at-bol))
  4794. (looking-at
  4795. (cond
  4796. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4797. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4798. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4799. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4800. (defun org-in-item-p ()
  4801. "It the cursor inside a plain list item.
  4802. Does not have to be the first line."
  4803. (save-excursion
  4804. (condition-case nil
  4805. (progn
  4806. (org-beginning-of-item)
  4807. (org-at-item-p)
  4808. t)
  4809. (error nil))))
  4810. (defun org-insert-item (&optional checkbox)
  4811. "Insert a new item at the current level.
  4812. Return t when things worked, nil when we are not in an item."
  4813. (when (save-excursion
  4814. (condition-case nil
  4815. (progn
  4816. (org-beginning-of-item)
  4817. (org-at-item-p)
  4818. (if (org-invisible-p) (error "Invisible item"))
  4819. t)
  4820. (error nil)))
  4821. (let* ((bul (match-string 0))
  4822. (descp (save-excursion (goto-char (match-beginning 0))
  4823. (beginning-of-line 1)
  4824. (save-match-data
  4825. (looking-at "[ \t]*.*? ::"))))
  4826. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4827. (match-end 0)))
  4828. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4829. pos)
  4830. (if descp (setq checkbox nil))
  4831. (cond
  4832. ((and (org-at-item-p) (<= (point) eow))
  4833. ;; before the bullet
  4834. (beginning-of-line 1)
  4835. (open-line (if blank 2 1)))
  4836. ((<= (point) eow)
  4837. (beginning-of-line 1))
  4838. (t
  4839. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4840. (end-of-line 1)
  4841. (delete-horizontal-space))
  4842. (newline (if blank 2 1))))
  4843. (insert bul
  4844. (if checkbox "[ ]" "")
  4845. (if descp (concat (if checkbox " " "")
  4846. (read-string "Term: ") " :: ") ""))
  4847. (just-one-space)
  4848. (setq pos (point))
  4849. (end-of-line 1)
  4850. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4851. (org-maybe-renumber-ordered-list)
  4852. (and checkbox (org-update-checkbox-count-maybe))
  4853. t))
  4854. ;;; Checkboxes
  4855. (defun org-at-item-checkbox-p ()
  4856. "Is point at a line starting a plain-list item with a checklet?"
  4857. (and (org-at-item-p)
  4858. (save-excursion
  4859. (goto-char (match-end 0))
  4860. (skip-chars-forward " \t")
  4861. (looking-at "\\[[- X]\\]"))))
  4862. (defun org-toggle-checkbox (&optional arg)
  4863. "Toggle the checkbox in the current line."
  4864. (interactive "P")
  4865. (catch 'exit
  4866. (let (beg end status (firstnew 'unknown))
  4867. (cond
  4868. ((org-region-active-p)
  4869. (setq beg (region-beginning) end (region-end)))
  4870. ((org-on-heading-p)
  4871. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4872. ((org-at-item-checkbox-p)
  4873. (let ((pos (point)))
  4874. (replace-match
  4875. (cond (arg "[-]")
  4876. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4877. (t "[ ]"))
  4878. t t)
  4879. (goto-char pos))
  4880. (throw 'exit t))
  4881. (t (error "Not at a checkbox or heading, and no active region")))
  4882. (save-excursion
  4883. (goto-char beg)
  4884. (while (< (point) end)
  4885. (when (org-at-item-checkbox-p)
  4886. (setq status (equal (match-string 0) "[X]"))
  4887. (when (eq firstnew 'unknown)
  4888. (setq firstnew (not status)))
  4889. (replace-match
  4890. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4891. (beginning-of-line 2)))))
  4892. (org-update-checkbox-count-maybe))
  4893. (defun org-update-checkbox-count-maybe ()
  4894. "Update checkbox statistics unless turned off by user."
  4895. (when org-provide-checkbox-statistics
  4896. (org-update-checkbox-count)))
  4897. (defun org-update-checkbox-count (&optional all)
  4898. "Update the checkbox statistics in the current section.
  4899. This will find all statistic cookies like [57%] and [6/12] and update them
  4900. with the current numbers. With optional prefix argument ALL, do this for
  4901. the whole buffer."
  4902. (interactive "P")
  4903. (save-excursion
  4904. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4905. (beg (condition-case nil
  4906. (progn (outline-back-to-heading) (point))
  4907. (error (point-min))))
  4908. (end (move-marker (make-marker)
  4909. (progn (outline-next-heading) (point))))
  4910. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4911. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4912. (re-find (concat re "\\|" re-box))
  4913. beg-cookie end-cookie is-percent c-on c-off lim
  4914. eline curr-ind next-ind continue-from startsearch
  4915. (cstat 0)
  4916. )
  4917. (when all
  4918. (goto-char (point-min))
  4919. (outline-next-heading)
  4920. (setq beg (point) end (point-max)))
  4921. (goto-char end)
  4922. ;; find each statistic cookie
  4923. (while (re-search-backward re-find beg t)
  4924. (setq beg-cookie (match-beginning 1)
  4925. end-cookie (match-end 1)
  4926. cstat (+ cstat (if end-cookie 1 0))
  4927. startsearch (point-at-eol)
  4928. continue-from (point-at-bol)
  4929. is-percent (match-beginning 2)
  4930. lim (cond
  4931. ((org-on-heading-p) (outline-next-heading) (point))
  4932. ((org-at-item-p) (org-end-of-item) (point))
  4933. (t nil))
  4934. c-on 0
  4935. c-off 0)
  4936. (when lim
  4937. ;; find first checkbox for this cookie and gather
  4938. ;; statistics from all that are at this indentation level
  4939. (goto-char startsearch)
  4940. (if (re-search-forward re-box lim t)
  4941. (progn
  4942. (org-beginning-of-item)
  4943. (setq curr-ind (org-get-indentation))
  4944. (setq next-ind curr-ind)
  4945. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4946. (save-excursion (end-of-line) (setq eline (point)))
  4947. (if (re-search-forward re-box eline t)
  4948. (if (member (match-string 2) '("[ ]" "[-]"))
  4949. (setq c-off (1+ c-off))
  4950. (setq c-on (1+ c-on))
  4951. )
  4952. )
  4953. (org-end-of-item)
  4954. (setq next-ind (org-get-indentation))
  4955. )))
  4956. (goto-char continue-from)
  4957. ;; update cookie
  4958. (when end-cookie
  4959. (delete-region beg-cookie end-cookie)
  4960. (goto-char beg-cookie)
  4961. (insert
  4962. (if is-percent
  4963. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4964. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4965. ;; update items checkbox if it has one
  4966. (when (org-at-item-p)
  4967. (org-beginning-of-item)
  4968. (when (and (> (+ c-on c-off) 0)
  4969. (re-search-forward re-box (point-at-eol) t))
  4970. (setq beg-cookie (match-beginning 2)
  4971. end-cookie (match-end 2))
  4972. (delete-region beg-cookie end-cookie)
  4973. (goto-char beg-cookie)
  4974. (cond ((= c-off 0) (insert "[X]"))
  4975. ((= c-on 0) (insert "[ ]"))
  4976. (t (insert "[-]")))
  4977. )))
  4978. (goto-char continue-from))
  4979. (when (interactive-p)
  4980. (message "Checkbox satistics updated %s (%d places)"
  4981. (if all "in entire file" "in current outline entry") cstat)))))
  4982. (defun org-get-checkbox-statistics-face ()
  4983. "Select the face for checkbox statistics.
  4984. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4985. it will be `org-todo'."
  4986. (if (match-end 1)
  4987. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4988. (if (and (> (match-end 2) (match-beginning 2))
  4989. (equal (match-string 2) (match-string 3)))
  4990. 'org-done
  4991. 'org-todo)))
  4992. (defun org-get-indentation (&optional line)
  4993. "Get the indentation of the current line, interpreting tabs.
  4994. When LINE is given, assume it represents a line and compute its indentation."
  4995. (if line
  4996. (if (string-match "^ *" (org-remove-tabs line))
  4997. (match-end 0))
  4998. (save-excursion
  4999. (beginning-of-line 1)
  5000. (skip-chars-forward " \t")
  5001. (current-column))))
  5002. (defun org-remove-tabs (s &optional width)
  5003. "Replace tabulators in S with spaces.
  5004. Assumes that s is a single line, starting in column 0."
  5005. (setq width (or width tab-width))
  5006. (while (string-match "\t" s)
  5007. (setq s (replace-match
  5008. (make-string
  5009. (- (* width (/ (+ (match-beginning 0) width) width))
  5010. (match-beginning 0)) ?\ )
  5011. t t s)))
  5012. s)
  5013. (defun org-fix-indentation (line ind)
  5014. "Fix indentation in LINE.
  5015. IND is a cons cell with target and minimum indentation.
  5016. If the current indenation in LINE is smaller than the minimum,
  5017. leave it alone. If it is larger than ind, set it to the target."
  5018. (let* ((l (org-remove-tabs line))
  5019. (i (org-get-indentation l))
  5020. (i1 (car ind)) (i2 (cdr ind)))
  5021. (if (>= i i2) (setq l (substring line i2)))
  5022. (if (> i1 0)
  5023. (concat (make-string i1 ?\ ) l)
  5024. l)))
  5025. (defun org-beginning-of-item ()
  5026. "Go to the beginning of the current hand-formatted item.
  5027. If the cursor is not in an item, throw an error."
  5028. (interactive)
  5029. (let ((pos (point))
  5030. (limit (save-excursion
  5031. (condition-case nil
  5032. (progn
  5033. (org-back-to-heading)
  5034. (beginning-of-line 2) (point))
  5035. (error (point-min)))))
  5036. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5037. ind ind1)
  5038. (if (org-at-item-p)
  5039. (beginning-of-line 1)
  5040. (beginning-of-line 1)
  5041. (skip-chars-forward " \t")
  5042. (setq ind (current-column))
  5043. (if (catch 'exit
  5044. (while t
  5045. (beginning-of-line 0)
  5046. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5047. (if (looking-at "[ \t]*$")
  5048. (setq ind1 ind-empty)
  5049. (skip-chars-forward " \t")
  5050. (setq ind1 (current-column)))
  5051. (if (< ind1 ind)
  5052. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5053. nil
  5054. (goto-char pos)
  5055. (error "Not in an item")))))
  5056. (defun org-end-of-item ()
  5057. "Go to the end of the current hand-formatted item.
  5058. If the cursor is not in an item, throw an error."
  5059. (interactive)
  5060. (let* ((pos (point))
  5061. ind1
  5062. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5063. (limit (save-excursion (outline-next-heading) (point)))
  5064. (ind (save-excursion
  5065. (org-beginning-of-item)
  5066. (skip-chars-forward " \t")
  5067. (current-column)))
  5068. (end (catch 'exit
  5069. (while t
  5070. (beginning-of-line 2)
  5071. (if (eobp) (throw 'exit (point)))
  5072. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5073. (if (looking-at "[ \t]*$")
  5074. (setq ind1 ind-empty)
  5075. (skip-chars-forward " \t")
  5076. (setq ind1 (current-column)))
  5077. (if (<= ind1 ind)
  5078. (throw 'exit (point-at-bol)))))))
  5079. (if end
  5080. (goto-char end)
  5081. (goto-char pos)
  5082. (error "Not in an item"))))
  5083. (defun org-next-item ()
  5084. "Move to the beginning of the next item in the current plain list.
  5085. Error if not at a plain list, or if this is the last item in the list."
  5086. (interactive)
  5087. (let (ind ind1 (pos (point)))
  5088. (org-beginning-of-item)
  5089. (setq ind (org-get-indentation))
  5090. (org-end-of-item)
  5091. (setq ind1 (org-get-indentation))
  5092. (unless (and (org-at-item-p) (= ind ind1))
  5093. (goto-char pos)
  5094. (error "On last item"))))
  5095. (defun org-previous-item ()
  5096. "Move to the beginning of the previous item in the current plain list.
  5097. Error if not at a plain list, or if this is the first item in the list."
  5098. (interactive)
  5099. (let (beg ind ind1 (pos (point)))
  5100. (org-beginning-of-item)
  5101. (setq beg (point))
  5102. (setq ind (org-get-indentation))
  5103. (goto-char beg)
  5104. (catch 'exit
  5105. (while t
  5106. (beginning-of-line 0)
  5107. (if (looking-at "[ \t]*$")
  5108. nil
  5109. (if (<= (setq ind1 (org-get-indentation)) ind)
  5110. (throw 'exit t)))))
  5111. (condition-case nil
  5112. (if (or (not (org-at-item-p))
  5113. (< ind1 (1- ind)))
  5114. (error "")
  5115. (org-beginning-of-item))
  5116. (error (goto-char pos)
  5117. (error "On first item")))))
  5118. (defun org-first-list-item-p ()
  5119. "Is this heading the item in a plain list?"
  5120. (unless (org-at-item-p)
  5121. (error "Not at a plain list item"))
  5122. (org-beginning-of-item)
  5123. (= (point) (save-excursion (org-beginning-of-item-list))))
  5124. (defun org-move-item-down ()
  5125. "Move the plain list item at point down, i.e. swap with following item.
  5126. Subitems (items with larger indentation) are considered part of the item,
  5127. so this really moves item trees."
  5128. (interactive)
  5129. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5130. (org-beginning-of-item)
  5131. (setq beg0 (point))
  5132. (save-excursion
  5133. (setq ne-beg (org-back-over-empty-lines))
  5134. (setq beg (point)))
  5135. (goto-char beg0)
  5136. (setq ind (org-get-indentation))
  5137. (org-end-of-item)
  5138. (setq end0 (point))
  5139. (setq ind1 (org-get-indentation))
  5140. (setq ne-end (org-back-over-empty-lines))
  5141. (setq end (point))
  5142. (goto-char beg0)
  5143. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5144. ;; include less whitespace
  5145. (save-excursion
  5146. (goto-char beg)
  5147. (forward-line (- ne-beg ne-end))
  5148. (setq beg (point))))
  5149. (goto-char end0)
  5150. (if (and (org-at-item-p) (= ind ind1))
  5151. (progn
  5152. (org-end-of-item)
  5153. (org-back-over-empty-lines)
  5154. (setq txt (buffer-substring beg end))
  5155. (save-excursion
  5156. (delete-region beg end))
  5157. (setq pos (point))
  5158. (insert txt)
  5159. (goto-char pos) (org-skip-whitespace)
  5160. (org-maybe-renumber-ordered-list))
  5161. (goto-char pos)
  5162. (error "Cannot move this item further down"))))
  5163. (defun org-move-item-up (arg)
  5164. "Move the plain list item at point up, i.e. swap with previous item.
  5165. Subitems (items with larger indentation) are considered part of the item,
  5166. so this really moves item trees."
  5167. (interactive "p")
  5168. (let (beg beg0 end ind ind1 (pos (point)) txt
  5169. ne-beg ne-ins ins-end)
  5170. (org-beginning-of-item)
  5171. (setq beg0 (point))
  5172. (setq ind (org-get-indentation))
  5173. (save-excursion
  5174. (setq ne-beg (org-back-over-empty-lines))
  5175. (setq beg (point)))
  5176. (goto-char beg0)
  5177. (org-end-of-item)
  5178. (setq end (point))
  5179. (goto-char beg0)
  5180. (catch 'exit
  5181. (while t
  5182. (beginning-of-line 0)
  5183. (if (looking-at "[ \t]*$")
  5184. (if org-empty-line-terminates-plain-lists
  5185. (progn
  5186. (goto-char pos)
  5187. (error "Cannot move this item further up"))
  5188. nil)
  5189. (if (<= (setq ind1 (org-get-indentation)) ind)
  5190. (throw 'exit t)))))
  5191. (condition-case nil
  5192. (org-beginning-of-item)
  5193. (error (goto-char beg)
  5194. (error "Cannot move this item further up")))
  5195. (setq ind1 (org-get-indentation))
  5196. (if (and (org-at-item-p) (= ind ind1))
  5197. (progn
  5198. (setq ne-ins (org-back-over-empty-lines))
  5199. (setq txt (buffer-substring beg end))
  5200. (save-excursion
  5201. (delete-region beg end))
  5202. (setq pos (point))
  5203. (insert txt)
  5204. (setq ins-end (point))
  5205. (goto-char pos) (org-skip-whitespace)
  5206. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5207. ;; Move whitespace back to beginning
  5208. (save-excursion
  5209. (goto-char ins-end)
  5210. (let ((kill-whole-line t))
  5211. (kill-line (- ne-ins ne-beg)) (point)))
  5212. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5213. (org-maybe-renumber-ordered-list))
  5214. (goto-char pos)
  5215. (error "Cannot move this item further up"))))
  5216. (defun org-maybe-renumber-ordered-list ()
  5217. "Renumber the ordered list at point if setup allows it.
  5218. This tests the user option `org-auto-renumber-ordered-lists' before
  5219. doing the renumbering."
  5220. (interactive)
  5221. (when (and org-auto-renumber-ordered-lists
  5222. (org-at-item-p))
  5223. (if (match-beginning 3)
  5224. (org-renumber-ordered-list 1)
  5225. (org-fix-bullet-type))))
  5226. (defun org-maybe-renumber-ordered-list-safe ()
  5227. (condition-case nil
  5228. (save-excursion
  5229. (org-maybe-renumber-ordered-list))
  5230. (error nil)))
  5231. (defun org-cycle-list-bullet (&optional which)
  5232. "Cycle through the different itemize/enumerate bullets.
  5233. This cycle the entire list level through the sequence:
  5234. `-' -> `+' -> `*' -> `1.' -> `1)'
  5235. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5236. 0 meand `-', 1 means `+' etc."
  5237. (interactive "P")
  5238. (org-preserve-lc
  5239. (org-beginning-of-item-list)
  5240. (org-at-item-p)
  5241. (beginning-of-line 1)
  5242. (let ((current (match-string 0))
  5243. (prevp (eq which 'previous))
  5244. new)
  5245. (setq new (cond
  5246. ((and (numberp which)
  5247. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5248. ((string-match "-" current) (if prevp "1)" "+"))
  5249. ((string-match "\\+" current)
  5250. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5251. ((string-match "\\*" current) (if prevp "+" "1."))
  5252. ((string-match "\\." current) (if prevp "*" "1)"))
  5253. ((string-match ")" current) (if prevp "1." "-"))
  5254. (t (error "This should not happen"))))
  5255. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5256. (org-fix-bullet-type)
  5257. (org-maybe-renumber-ordered-list))))
  5258. (defun org-get-string-indentation (s)
  5259. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5260. (let ((n -1) (i 0) (w tab-width) c)
  5261. (catch 'exit
  5262. (while (< (setq n (1+ n)) (length s))
  5263. (setq c (aref s n))
  5264. (cond ((= c ?\ ) (setq i (1+ i)))
  5265. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5266. (t (throw 'exit t)))))
  5267. i))
  5268. (defun org-renumber-ordered-list (arg)
  5269. "Renumber an ordered plain list.
  5270. Cursor needs to be in the first line of an item, the line that starts
  5271. with something like \"1.\" or \"2)\"."
  5272. (interactive "p")
  5273. (unless (and (org-at-item-p)
  5274. (match-beginning 3))
  5275. (error "This is not an ordered list"))
  5276. (let ((line (org-current-line))
  5277. (col (current-column))
  5278. (ind (org-get-string-indentation
  5279. (buffer-substring (point-at-bol) (match-beginning 3))))
  5280. ;; (term (substring (match-string 3) -1))
  5281. ind1 (n (1- arg))
  5282. fmt)
  5283. ;; find where this list begins
  5284. (org-beginning-of-item-list)
  5285. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5286. (setq fmt (concat "%d" (match-string 1)))
  5287. (beginning-of-line 0)
  5288. ;; walk forward and replace these numbers
  5289. (catch 'exit
  5290. (while t
  5291. (catch 'next
  5292. (beginning-of-line 2)
  5293. (if (eobp) (throw 'exit nil))
  5294. (if (looking-at "[ \t]*$") (throw 'next nil))
  5295. (skip-chars-forward " \t") (setq ind1 (current-column))
  5296. (if (> ind1 ind) (throw 'next t))
  5297. (if (< ind1 ind) (throw 'exit t))
  5298. (if (not (org-at-item-p)) (throw 'exit nil))
  5299. (delete-region (match-beginning 2) (match-end 2))
  5300. (goto-char (match-beginning 2))
  5301. (insert (format fmt (setq n (1+ n)))))))
  5302. (goto-line line)
  5303. (org-move-to-column col)))
  5304. (defun org-fix-bullet-type ()
  5305. "Make sure all items in this list have the same bullet as the firsst item."
  5306. (interactive)
  5307. (unless (org-at-item-p) (error "This is not a list"))
  5308. (let ((line (org-current-line))
  5309. (col (current-column))
  5310. (ind (current-indentation))
  5311. ind1 bullet)
  5312. ;; find where this list begins
  5313. (org-beginning-of-item-list)
  5314. (beginning-of-line 1)
  5315. ;; find out what the bullet type is
  5316. (looking-at "[ \t]*\\(\\S-+\\)")
  5317. (setq bullet (match-string 1))
  5318. ;; walk forward and replace these numbers
  5319. (beginning-of-line 0)
  5320. (catch 'exit
  5321. (while t
  5322. (catch 'next
  5323. (beginning-of-line 2)
  5324. (if (eobp) (throw 'exit nil))
  5325. (if (looking-at "[ \t]*$") (throw 'next nil))
  5326. (skip-chars-forward " \t") (setq ind1 (current-column))
  5327. (if (> ind1 ind) (throw 'next t))
  5328. (if (< ind1 ind) (throw 'exit t))
  5329. (if (not (org-at-item-p)) (throw 'exit nil))
  5330. (skip-chars-forward " \t")
  5331. (looking-at "\\S-+")
  5332. (replace-match bullet))))
  5333. (goto-line line)
  5334. (org-move-to-column col)
  5335. (if (string-match "[0-9]" bullet)
  5336. (org-renumber-ordered-list 1))))
  5337. (defun org-beginning-of-item-list ()
  5338. "Go to the beginning of the current item list.
  5339. I.e. to the first item in this list."
  5340. (interactive)
  5341. (org-beginning-of-item)
  5342. (let ((pos (point-at-bol))
  5343. (ind (org-get-indentation))
  5344. ind1)
  5345. ;; find where this list begins
  5346. (catch 'exit
  5347. (while t
  5348. (catch 'next
  5349. (beginning-of-line 0)
  5350. (if (looking-at "[ \t]*$")
  5351. (throw (if (bobp) 'exit 'next) t))
  5352. (skip-chars-forward " \t") (setq ind1 (current-column))
  5353. (if (or (< ind1 ind)
  5354. (and (= ind1 ind)
  5355. (not (org-at-item-p)))
  5356. (bobp))
  5357. (throw 'exit t)
  5358. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5359. (goto-char pos)))
  5360. (defun org-end-of-item-list ()
  5361. "Go to the end of the current item list.
  5362. I.e. to the text after the last item."
  5363. (interactive)
  5364. (org-beginning-of-item)
  5365. (let ((pos (point-at-bol))
  5366. (ind (org-get-indentation))
  5367. ind1)
  5368. ;; find where this list begins
  5369. (catch 'exit
  5370. (while t
  5371. (catch 'next
  5372. (beginning-of-line 2)
  5373. (if (looking-at "[ \t]*$")
  5374. (throw (if (eobp) 'exit 'next) t))
  5375. (skip-chars-forward " \t") (setq ind1 (current-column))
  5376. (if (or (< ind1 ind)
  5377. (and (= ind1 ind)
  5378. (not (org-at-item-p)))
  5379. (eobp))
  5380. (progn
  5381. (setq pos (point-at-bol))
  5382. (throw 'exit t))))))
  5383. (goto-char pos)))
  5384. (defvar org-last-indent-begin-marker (make-marker))
  5385. (defvar org-last-indent-end-marker (make-marker))
  5386. (defun org-outdent-item (arg)
  5387. "Outdent a local list item."
  5388. (interactive "p")
  5389. (org-indent-item (- arg)))
  5390. (defun org-indent-item (arg)
  5391. "Indent a local list item."
  5392. (interactive "p")
  5393. (unless (org-at-item-p)
  5394. (error "Not on an item"))
  5395. (save-excursion
  5396. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5397. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5398. (setq beg org-last-indent-begin-marker
  5399. end org-last-indent-end-marker)
  5400. (org-beginning-of-item)
  5401. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5402. (org-end-of-item)
  5403. (setq end (move-marker org-last-indent-end-marker (point))))
  5404. (goto-char beg)
  5405. (setq tmp (org-item-indent-positions)
  5406. ind (car tmp)
  5407. ind-down (nth 2 tmp)
  5408. ind-up (nth 1 tmp)
  5409. delta (if (> arg 0)
  5410. (if ind-down (- ind-down ind) 2)
  5411. (if ind-up (- ind-up ind) -2)))
  5412. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5413. (while (< (point) end)
  5414. (beginning-of-line 1)
  5415. (skip-chars-forward " \t") (setq ind1 (current-column))
  5416. (delete-region (point-at-bol) (point))
  5417. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5418. (beginning-of-line 2))))
  5419. (org-fix-bullet-type)
  5420. (org-maybe-renumber-ordered-list-safe)
  5421. (save-excursion
  5422. (beginning-of-line 0)
  5423. (condition-case nil (org-beginning-of-item) (error nil))
  5424. (org-maybe-renumber-ordered-list-safe)))
  5425. (defun org-item-indent-positions ()
  5426. "Return indentation for plain list items.
  5427. This returns a list with three values: The current indentation, the
  5428. parent indentation and the indentation a child should habe.
  5429. Assumes cursor in item line."
  5430. (let* ((bolpos (point-at-bol))
  5431. (ind (org-get-indentation))
  5432. ind-down ind-up pos)
  5433. (save-excursion
  5434. (org-beginning-of-item-list)
  5435. (skip-chars-backward "\n\r \t")
  5436. (when (org-in-item-p)
  5437. (org-beginning-of-item)
  5438. (setq ind-up (org-get-indentation))))
  5439. (setq pos (point))
  5440. (save-excursion
  5441. (cond
  5442. ((and (condition-case nil (progn (org-previous-item) t)
  5443. (error nil))
  5444. (or (forward-char 1) t)
  5445. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5446. (setq ind-down (org-get-indentation)))
  5447. ((and (goto-char pos)
  5448. (org-at-item-p))
  5449. (goto-char (match-end 0))
  5450. (skip-chars-forward " \t")
  5451. (setq ind-down (current-column)))))
  5452. (list ind ind-up ind-down)))
  5453. ;;; The orgstruct minor mode
  5454. ;; Define a minor mode which can be used in other modes in order to
  5455. ;; integrate the org-mode structure editing commands.
  5456. ;; This is really a hack, because the org-mode structure commands use
  5457. ;; keys which normally belong to the major mode. Here is how it
  5458. ;; works: The minor mode defines all the keys necessary to operate the
  5459. ;; structure commands, but wraps the commands into a function which
  5460. ;; tests if the cursor is currently at a headline or a plain list
  5461. ;; item. If that is the case, the structure command is used,
  5462. ;; temporarily setting many Org-mode variables like regular
  5463. ;; expressions for filling etc. However, when any of those keys is
  5464. ;; used at a different location, function uses `key-binding' to look
  5465. ;; up if the key has an associated command in another currently active
  5466. ;; keymap (minor modes, major mode, global), and executes that
  5467. ;; command. There might be problems if any of the keys is otherwise
  5468. ;; used as a prefix key.
  5469. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5470. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5471. ;; addresses this by checking explicitly for both bindings.
  5472. (defvar orgstruct-mode-map (make-sparse-keymap)
  5473. "Keymap for the minor `orgstruct-mode'.")
  5474. (defvar org-local-vars nil
  5475. "List of local variables, for use by `orgstruct-mode'")
  5476. ;;;###autoload
  5477. (define-minor-mode orgstruct-mode
  5478. "Toggle the minor more `orgstruct-mode'.
  5479. This mode is for using Org-mode structure commands in other modes.
  5480. The following key behave as if Org-mode was active, if the cursor
  5481. is on a headline, or on a plain list item (both in the definition
  5482. of Org-mode).
  5483. M-up Move entry/item up
  5484. M-down Move entry/item down
  5485. M-left Promote
  5486. M-right Demote
  5487. M-S-up Move entry/item up
  5488. M-S-down Move entry/item down
  5489. M-S-left Promote subtree
  5490. M-S-right Demote subtree
  5491. M-q Fill paragraph and items like in Org-mode
  5492. C-c ^ Sort entries
  5493. C-c - Cycle list bullet
  5494. TAB Cycle item visibility
  5495. M-RET Insert new heading/item
  5496. S-M-RET Insert new TODO heading / Chekbox item
  5497. C-c C-c Set tags / toggle checkbox"
  5498. nil " OrgStruct" nil
  5499. (org-load-modules-maybe)
  5500. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5501. ;;;###autoload
  5502. (defun turn-on-orgstruct ()
  5503. "Unconditionally turn on `orgstruct-mode'."
  5504. (orgstruct-mode 1))
  5505. ;;;###autoload
  5506. (defun turn-on-orgstruct++ ()
  5507. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5508. In addition to setting orgstruct-mode, this also exports all indentation and
  5509. autofilling variables from org-mode into the buffer. Note that turning
  5510. off orgstruct-mode will *not* remove these additional settings."
  5511. (orgstruct-mode 1)
  5512. (let (var val)
  5513. (mapc
  5514. (lambda (x)
  5515. (when (string-match
  5516. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5517. (symbol-name (car x)))
  5518. (setq var (car x) val (nth 1 x))
  5519. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5520. org-local-vars)))
  5521. (defun orgstruct-error ()
  5522. "Error when there is no default binding for a structure key."
  5523. (interactive)
  5524. (error "This key has no function outside structure elements"))
  5525. (defun orgstruct-setup ()
  5526. "Setup orgstruct keymaps."
  5527. (let ((nfunc 0)
  5528. (bindings
  5529. (list
  5530. '([(meta up)] org-metaup)
  5531. '([(meta down)] org-metadown)
  5532. '([(meta left)] org-metaleft)
  5533. '([(meta right)] org-metaright)
  5534. '([(meta shift up)] org-shiftmetaup)
  5535. '([(meta shift down)] org-shiftmetadown)
  5536. '([(meta shift left)] org-shiftmetaleft)
  5537. '([(meta shift right)] org-shiftmetaright)
  5538. '([(shift up)] org-shiftup)
  5539. '([(shift down)] org-shiftdown)
  5540. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5541. '("\M-q" fill-paragraph)
  5542. '("\C-c^" org-sort)
  5543. '("\C-c-" org-cycle-list-bullet)))
  5544. elt key fun cmd)
  5545. (while (setq elt (pop bindings))
  5546. (setq nfunc (1+ nfunc))
  5547. (setq key (org-key (car elt))
  5548. fun (nth 1 elt)
  5549. cmd (orgstruct-make-binding fun nfunc key))
  5550. (org-defkey orgstruct-mode-map key cmd))
  5551. ;; Special treatment needed for TAB and RET
  5552. (org-defkey orgstruct-mode-map [(tab)]
  5553. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5554. (org-defkey orgstruct-mode-map "\C-i"
  5555. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5556. (org-defkey orgstruct-mode-map "\M-\C-m"
  5557. (orgstruct-make-binding 'org-insert-heading 105
  5558. "\M-\C-m" [(meta return)]))
  5559. (org-defkey orgstruct-mode-map [(meta return)]
  5560. (orgstruct-make-binding 'org-insert-heading 106
  5561. [(meta return)] "\M-\C-m"))
  5562. (org-defkey orgstruct-mode-map [(shift meta return)]
  5563. (orgstruct-make-binding 'org-insert-todo-heading 107
  5564. [(meta return)] "\M-\C-m"))
  5565. (unless org-local-vars
  5566. (setq org-local-vars (org-get-local-variables)))
  5567. t))
  5568. (defun orgstruct-make-binding (fun n &rest keys)
  5569. "Create a function for binding in the structure minor mode.
  5570. FUN is the command to call inside a table. N is used to create a unique
  5571. command name. KEYS are keys that should be checked in for a command
  5572. to execute outside of tables."
  5573. (eval
  5574. (list 'defun
  5575. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5576. '(arg)
  5577. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5578. "Outside of structure, run the binding of `"
  5579. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5580. "'.")
  5581. '(interactive "p")
  5582. (list 'if
  5583. '(org-context-p 'headline 'item)
  5584. (list 'org-run-like-in-org-mode (list 'quote fun))
  5585. (list 'let '(orgstruct-mode)
  5586. (list 'call-interactively
  5587. (append '(or)
  5588. (mapcar (lambda (k)
  5589. (list 'key-binding k))
  5590. keys)
  5591. '('orgstruct-error))))))))
  5592. (defun org-context-p (&rest contexts)
  5593. "Check if local context is and of CONTEXTS.
  5594. Possible values in the list of contexts are `table', `headline', and `item'."
  5595. (let ((pos (point)))
  5596. (goto-char (point-at-bol))
  5597. (prog1 (or (and (memq 'table contexts)
  5598. (looking-at "[ \t]*|"))
  5599. (and (memq 'headline contexts)
  5600. (looking-at "\\*+"))
  5601. (and (memq 'item contexts)
  5602. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5603. (goto-char pos))))
  5604. (defun org-get-local-variables ()
  5605. "Return a list of all local variables in an org-mode buffer."
  5606. (let (varlist)
  5607. (with-current-buffer (get-buffer-create "*Org tmp*")
  5608. (erase-buffer)
  5609. (org-mode)
  5610. (setq varlist (buffer-local-variables)))
  5611. (kill-buffer "*Org tmp*")
  5612. (delq nil
  5613. (mapcar
  5614. (lambda (x)
  5615. (setq x
  5616. (if (symbolp x)
  5617. (list x)
  5618. (list (car x) (list 'quote (cdr x)))))
  5619. (if (string-match
  5620. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5621. (symbol-name (car x)))
  5622. x nil))
  5623. varlist))))
  5624. ;;;###autoload
  5625. (defun org-run-like-in-org-mode (cmd)
  5626. (org-load-modules-maybe)
  5627. (unless org-local-vars
  5628. (setq org-local-vars (org-get-local-variables)))
  5629. (eval (list 'let org-local-vars
  5630. (list 'call-interactively (list 'quote cmd)))))
  5631. ;;;; Archiving
  5632. (defun org-get-category (&optional pos)
  5633. "Get the category applying to position POS."
  5634. (get-text-property (or pos (point)) 'org-category))
  5635. (defun org-refresh-category-properties ()
  5636. "Refresh category text properties in the buffer."
  5637. (let ((def-cat (cond
  5638. ((null org-category)
  5639. (if buffer-file-name
  5640. (file-name-sans-extension
  5641. (file-name-nondirectory buffer-file-name))
  5642. "???"))
  5643. ((symbolp org-category) (symbol-name org-category))
  5644. (t org-category)))
  5645. beg end cat pos optionp)
  5646. (org-unmodified
  5647. (save-excursion
  5648. (save-restriction
  5649. (widen)
  5650. (goto-char (point-min))
  5651. (put-text-property (point) (point-max) 'org-category def-cat)
  5652. (while (re-search-forward
  5653. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5654. (setq pos (match-end 0)
  5655. optionp (equal (char-after (match-beginning 0)) ?#)
  5656. cat (org-trim (match-string 2)))
  5657. (if optionp
  5658. (setq beg (point-at-bol) end (point-max))
  5659. (org-back-to-heading t)
  5660. (setq beg (point) end (org-end-of-subtree t t)))
  5661. (put-text-property beg end 'org-category cat)
  5662. (goto-char pos)))))))
  5663. ;;;; Link Stuff
  5664. ;;; Link abbreviations
  5665. (defun org-link-expand-abbrev (link)
  5666. "Apply replacements as defined in `org-link-abbrev-alist."
  5667. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5668. (let* ((key (match-string 1 link))
  5669. (as (or (assoc key org-link-abbrev-alist-local)
  5670. (assoc key org-link-abbrev-alist)))
  5671. (tag (and (match-end 2) (match-string 3 link)))
  5672. rpl)
  5673. (if (not as)
  5674. link
  5675. (setq rpl (cdr as))
  5676. (cond
  5677. ((symbolp rpl) (funcall rpl tag))
  5678. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5679. (t (concat rpl tag)))))
  5680. link))
  5681. ;;; Storing and inserting links
  5682. (defvar org-insert-link-history nil
  5683. "Minibuffer history for links inserted with `org-insert-link'.")
  5684. (defvar org-stored-links nil
  5685. "Contains the links stored with `org-store-link'.")
  5686. (defvar org-store-link-plist nil
  5687. "Plist with info about the most recently link created with `org-store-link'.")
  5688. (defvar org-link-protocols nil
  5689. "Link protocols added to Org-mode using `org-add-link-type'.")
  5690. (defvar org-store-link-functions nil
  5691. "List of functions that are called to create and store a link.
  5692. Each function will be called in turn until one returns a non-nil
  5693. value. Each function should check if it is responsible for creating
  5694. this link (for example by looking at the major mode).
  5695. If not, it must exit and return nil.
  5696. If yes, it should return a non-nil value after a calling
  5697. `org-store-link-props' with a list of properties and values.
  5698. Special properties are:
  5699. :type The link prefix. like \"http\". This must be given.
  5700. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5701. This is obligatory as well.
  5702. :description Optional default description for the second pair
  5703. of brackets in an Org-mode link. The user can still change
  5704. this when inserting this link into an Org-mode buffer.
  5705. In addition to these, any additional properties can be specified
  5706. and then used in remember templates.")
  5707. (defun org-add-link-type (type &optional follow export)
  5708. "Add TYPE to the list of `org-link-types'.
  5709. Re-compute all regular expressions depending on `org-link-types'
  5710. FOLLOW and EXPORT are two functions.
  5711. FOLLOW should take the link path as the single argument and do whatever
  5712. is necessary to follow the link, for example find a file or display
  5713. a mail message.
  5714. EXPORT should format the link path for export to one of the export formats.
  5715. It should be a function accepting three arguments:
  5716. path the path of the link, the text after the prefix (like \"http:\")
  5717. desc the description of the link, if any, nil if there was no descripton
  5718. format the export format, a symbol like `html' or `latex'.
  5719. The function may use the FORMAT information to return different values
  5720. depending on the format. The return value will be put literally into
  5721. the exported file.
  5722. Org-mode has a built-in default for exporting links. If you are happy with
  5723. this default, there is no need to define an export function for the link
  5724. type. For a simple example of an export function, see `org-bbdb.el'."
  5725. (add-to-list 'org-link-types type t)
  5726. (org-make-link-regexps)
  5727. (if (assoc type org-link-protocols)
  5728. (setcdr (assoc type org-link-protocols) (list follow export))
  5729. (push (list type follow export) org-link-protocols)))
  5730. ;;;###autoload
  5731. (defun org-store-link (arg)
  5732. "\\<org-mode-map>Store an org-link to the current location.
  5733. This link is added to `org-stored-links' and can later be inserted
  5734. into an org-buffer with \\[org-insert-link].
  5735. For some link types, a prefix arg is interpreted:
  5736. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5737. For file links, arg negates `org-context-in-file-links'."
  5738. (interactive "P")
  5739. (org-load-modules-maybe)
  5740. (setq org-store-link-plist nil) ; reset
  5741. (let (link cpltxt desc description search txt)
  5742. (cond
  5743. ((run-hook-with-args-until-success 'org-store-link-functions)
  5744. (setq link (plist-get org-store-link-plist :link)
  5745. desc (or (plist-get org-store-link-plist :description) link)))
  5746. ((eq major-mode 'calendar-mode)
  5747. (let ((cd (calendar-cursor-to-date)))
  5748. (setq link
  5749. (format-time-string
  5750. (car org-time-stamp-formats)
  5751. (apply 'encode-time
  5752. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5753. nil nil nil))))
  5754. (org-store-link-props :type "calendar" :date cd)))
  5755. ((eq major-mode 'w3-mode)
  5756. (setq cpltxt (url-view-url t)
  5757. link (org-make-link cpltxt))
  5758. (org-store-link-props :type "w3" :url (url-view-url t)))
  5759. ((eq major-mode 'w3m-mode)
  5760. (setq cpltxt (or w3m-current-title w3m-current-url)
  5761. link (org-make-link w3m-current-url))
  5762. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5763. ((setq search (run-hook-with-args-until-success
  5764. 'org-create-file-search-functions))
  5765. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5766. "::" search))
  5767. (setq cpltxt (or description link)))
  5768. ((eq major-mode 'image-mode)
  5769. (setq cpltxt (concat "file:"
  5770. (abbreviate-file-name buffer-file-name))
  5771. link (org-make-link cpltxt))
  5772. (org-store-link-props :type "image" :file buffer-file-name))
  5773. ((eq major-mode 'dired-mode)
  5774. ;; link to the file in the current line
  5775. (setq cpltxt (concat "file:"
  5776. (abbreviate-file-name
  5777. (expand-file-name
  5778. (dired-get-filename nil t))))
  5779. link (org-make-link cpltxt)))
  5780. ((and buffer-file-name (org-mode-p))
  5781. ;; Just link to current headline
  5782. (setq cpltxt (concat "file:"
  5783. (abbreviate-file-name buffer-file-name)))
  5784. ;; Add a context search string
  5785. (when (org-xor org-context-in-file-links arg)
  5786. ;; Check if we are on a target
  5787. (if (org-in-regexp "<<\\(.*?\\)>>")
  5788. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5789. (setq txt (cond
  5790. ((org-on-heading-p) nil)
  5791. ((org-region-active-p)
  5792. (buffer-substring (region-beginning) (region-end)))
  5793. (t nil)))
  5794. (when (or (null txt) (string-match "\\S-" txt))
  5795. (setq cpltxt
  5796. (concat cpltxt "::"
  5797. (condition-case nil
  5798. (org-make-org-heading-search-string txt)
  5799. (error "")))
  5800. desc "NONE"))))
  5801. (if (string-match "::\\'" cpltxt)
  5802. (setq cpltxt (substring cpltxt 0 -2)))
  5803. (setq link (org-make-link cpltxt)))
  5804. ((buffer-file-name (buffer-base-buffer))
  5805. ;; Just link to this file here.
  5806. (setq cpltxt (concat "file:"
  5807. (abbreviate-file-name
  5808. (buffer-file-name (buffer-base-buffer)))))
  5809. ;; Add a context string
  5810. (when (org-xor org-context-in-file-links arg)
  5811. (setq txt (if (org-region-active-p)
  5812. (buffer-substring (region-beginning) (region-end))
  5813. (buffer-substring (point-at-bol) (point-at-eol))))
  5814. ;; Only use search option if there is some text.
  5815. (when (string-match "\\S-" txt)
  5816. (setq cpltxt
  5817. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5818. desc "NONE")))
  5819. (setq link (org-make-link cpltxt)))
  5820. ((interactive-p)
  5821. (error "Cannot link to a buffer which is not visiting a file"))
  5822. (t (setq link nil)))
  5823. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5824. (setq link (or link cpltxt)
  5825. desc (or desc cpltxt))
  5826. (if (equal desc "NONE") (setq desc nil))
  5827. (if (and (interactive-p) link)
  5828. (progn
  5829. (setq org-stored-links
  5830. (cons (list link desc) org-stored-links))
  5831. (message "Stored: %s" (or desc link)))
  5832. (and link (org-make-link-string link desc)))))
  5833. (defun org-store-link-props (&rest plist)
  5834. "Store link properties, extract names and addresses."
  5835. (let (x adr)
  5836. (when (setq x (plist-get plist :from))
  5837. (setq adr (mail-extract-address-components x))
  5838. (plist-put plist :fromname (car adr))
  5839. (plist-put plist :fromaddress (nth 1 adr)))
  5840. (when (setq x (plist-get plist :to))
  5841. (setq adr (mail-extract-address-components x))
  5842. (plist-put plist :toname (car adr))
  5843. (plist-put plist :toaddress (nth 1 adr))))
  5844. (let ((from (plist-get plist :from))
  5845. (to (plist-get plist :to)))
  5846. (when (and from to org-from-is-user-regexp)
  5847. (plist-put plist :fromto
  5848. (if (string-match org-from-is-user-regexp from)
  5849. (concat "to %t")
  5850. (concat "from %f")))))
  5851. (setq org-store-link-plist plist))
  5852. (defun org-add-link-props (&rest plist)
  5853. "Add these properties to the link property list."
  5854. (let (key value)
  5855. (while plist
  5856. (setq key (pop plist) value (pop plist))
  5857. (setq org-store-link-plist
  5858. (plist-put org-store-link-plist key value)))))
  5859. (defun org-email-link-description (&optional fmt)
  5860. "Return the description part of an email link.
  5861. This takes information from `org-store-link-plist' and formats it
  5862. according to FMT (default from `org-email-link-description-format')."
  5863. (setq fmt (or fmt org-email-link-description-format))
  5864. (let* ((p org-store-link-plist)
  5865. (to (plist-get p :toaddress))
  5866. (from (plist-get p :fromaddress))
  5867. (table
  5868. (list
  5869. (cons "%c" (plist-get p :fromto))
  5870. (cons "%F" (plist-get p :from))
  5871. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5872. (cons "%T" (plist-get p :to))
  5873. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5874. (cons "%s" (plist-get p :subject))
  5875. (cons "%m" (plist-get p :message-id)))))
  5876. (when (string-match "%c" fmt)
  5877. ;; Check if the user wrote this message
  5878. (if (and org-from-is-user-regexp from to
  5879. (save-match-data (string-match org-from-is-user-regexp from)))
  5880. (setq fmt (replace-match "to %t" t t fmt))
  5881. (setq fmt (replace-match "from %f" t t fmt))))
  5882. (org-replace-escapes fmt table)))
  5883. (defun org-make-org-heading-search-string (&optional string heading)
  5884. "Make search string for STRING or current headline."
  5885. (interactive)
  5886. (let ((s (or string (org-get-heading))))
  5887. (unless (and string (not heading))
  5888. ;; We are using a headline, clean up garbage in there.
  5889. (if (string-match org-todo-regexp s)
  5890. (setq s (replace-match "" t t s)))
  5891. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5892. (setq s (replace-match "" t t s)))
  5893. (setq s (org-trim s))
  5894. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5895. org-comment-string "\\)") s)
  5896. (setq s (replace-match "" t t s)))
  5897. (while (string-match org-ts-regexp s)
  5898. (setq s (replace-match "" t t s))))
  5899. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5900. (setq s (replace-match " " t t s)))
  5901. (or string (setq s (concat "*" s))) ; Add * for headlines
  5902. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5903. (defun org-make-link (&rest strings)
  5904. "Concatenate STRINGS."
  5905. (apply 'concat strings))
  5906. (defun org-make-link-string (link &optional description)
  5907. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5908. (unless (string-match "\\S-" link)
  5909. (error "Empty link"))
  5910. (when (stringp description)
  5911. ;; Remove brackets from the description, they are fatal.
  5912. (while (string-match "\\[" description)
  5913. (setq description (replace-match "{" t t description)))
  5914. (while (string-match "\\]" description)
  5915. (setq description (replace-match "}" t t description))))
  5916. (when (equal (org-link-escape link) description)
  5917. ;; No description needed, it is identical
  5918. (setq description nil))
  5919. (when (and (not description)
  5920. (not (equal link (org-link-escape link))))
  5921. (setq description link))
  5922. (concat "[[" (org-link-escape link) "]"
  5923. (if description (concat "[" description "]") "")
  5924. "]"))
  5925. (defconst org-link-escape-chars
  5926. '((?\ . "%20")
  5927. (?\[ . "%5B")
  5928. (?\] . "%5D")
  5929. (?\340 . "%E0") ; `a
  5930. (?\342 . "%E2") ; ^a
  5931. (?\347 . "%E7") ; ,c
  5932. (?\350 . "%E8") ; `e
  5933. (?\351 . "%E9") ; 'e
  5934. (?\352 . "%EA") ; ^e
  5935. (?\356 . "%EE") ; ^i
  5936. (?\364 . "%F4") ; ^o
  5937. (?\371 . "%F9") ; `u
  5938. (?\373 . "%FB") ; ^u
  5939. (?\; . "%3B")
  5940. (?? . "%3F")
  5941. (?= . "%3D")
  5942. (?+ . "%2B")
  5943. )
  5944. "Association list of escapes for some characters problematic in links.
  5945. This is the list that is used for internal purposes.")
  5946. (defconst org-link-escape-chars-browser
  5947. '((?\ . "%20")) ; 32 for the SPC char
  5948. "Association list of escapes for some characters problematic in links.
  5949. This is the list that is used before handing over to the browser.")
  5950. (defun org-link-escape (text &optional table)
  5951. "Escape charaters in TEXT that are problematic for links."
  5952. (setq table (or table org-link-escape-chars))
  5953. (when text
  5954. (let ((re (mapconcat (lambda (x) (regexp-quote
  5955. (char-to-string (car x))))
  5956. table "\\|")))
  5957. (while (string-match re text)
  5958. (setq text
  5959. (replace-match
  5960. (cdr (assoc (string-to-char (match-string 0 text))
  5961. table))
  5962. t t text)))
  5963. text)))
  5964. (defun org-link-unescape (text &optional table)
  5965. "Reverse the action of `org-link-escape'."
  5966. (setq table (or table org-link-escape-chars))
  5967. (when text
  5968. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5969. table "\\|")))
  5970. (while (string-match re text)
  5971. (setq text
  5972. (replace-match
  5973. (char-to-string (car (rassoc (match-string 0 text) table)))
  5974. t t text)))
  5975. text)))
  5976. (defun org-xor (a b)
  5977. "Exclusive or."
  5978. (if a (not b) b))
  5979. (defun org-get-header (header)
  5980. "Find a header field in the current buffer."
  5981. (save-excursion
  5982. (goto-char (point-min))
  5983. (let ((case-fold-search t) s)
  5984. (cond
  5985. ((eq header 'from)
  5986. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5987. (setq s (match-string 1)))
  5988. (while (string-match "\"" s)
  5989. (setq s (replace-match "" t t s)))
  5990. (if (string-match "[<(].*" s)
  5991. (setq s (replace-match "" t t s))))
  5992. ((eq header 'message-id)
  5993. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5994. (setq s (match-string 1))))
  5995. ((eq header 'subject)
  5996. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5997. (setq s (match-string 1)))))
  5998. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5999. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6000. s)))
  6001. (defun org-fixup-message-id-for-http (s)
  6002. "Replace special characters in a message id, so it can be used in an http query."
  6003. (while (string-match "<" s)
  6004. (setq s (replace-match "%3C" t t s)))
  6005. (while (string-match ">" s)
  6006. (setq s (replace-match "%3E" t t s)))
  6007. (while (string-match "@" s)
  6008. (setq s (replace-match "%40" t t s)))
  6009. s)
  6010. ;;;###autoload
  6011. (defun org-insert-link-global ()
  6012. "Insert a link like Org-mode does.
  6013. This command can be called in any mode to insert a link in Org-mode syntax."
  6014. (interactive)
  6015. (org-load-modules-maybe)
  6016. (org-run-like-in-org-mode 'org-insert-link))
  6017. (defun org-insert-link (&optional complete-file link-location)
  6018. "Insert a link. At the prompt, enter the link.
  6019. Completion can be used to select a link previously stored with
  6020. `org-store-link'. When the empty string is entered (i.e. if you just
  6021. press RET at the prompt), the link defaults to the most recently
  6022. stored link. As SPC triggers completion in the minibuffer, you need to
  6023. use M-SPC or C-q SPC to force the insertion of a space character.
  6024. You will also be prompted for a description, and if one is given, it will
  6025. be displayed in the buffer instead of the link.
  6026. If there is already a link at point, this command will allow you to edit link
  6027. and description parts.
  6028. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6029. be selected using completion. The path to the file will be relative to the
  6030. current directory if the file is in the current directory or a subdirectory.
  6031. Otherwise, the link will be the absolute path as completed in the minibuffer
  6032. \(i.e. normally ~/path/to/file).
  6033. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6034. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6035. of `org-keep-stored-link-after-insertion'.
  6036. If `org-make-link-description-function' is non-nil, this function will be
  6037. called with the link target, and the result will be the default
  6038. link description.
  6039. If the LINK-LOCATION parameter is non-nil, this value will be
  6040. used as the link location instead of reading one interactively."
  6041. (interactive "P")
  6042. (let* ((wcf (current-window-configuration))
  6043. (region (if (org-region-active-p)
  6044. (buffer-substring (region-beginning) (region-end))))
  6045. (remove (and region (list (region-beginning) (region-end))))
  6046. (desc region)
  6047. tmphist ; byte-compile incorrectly complains about this
  6048. (link link-location)
  6049. entry file)
  6050. (cond
  6051. (link-location) ; specified by arg, just use it.
  6052. ((org-in-regexp org-bracket-link-regexp 1)
  6053. ;; We do have a link at point, and we are going to edit it.
  6054. (setq remove (list (match-beginning 0) (match-end 0)))
  6055. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6056. (setq link (read-string "Link: "
  6057. (org-link-unescape
  6058. (org-match-string-no-properties 1)))))
  6059. ((or (org-in-regexp org-angle-link-re)
  6060. (org-in-regexp org-plain-link-re))
  6061. ;; Convert to bracket link
  6062. (setq remove (list (match-beginning 0) (match-end 0))
  6063. link (read-string "Link: "
  6064. (org-remove-angle-brackets (match-string 0)))))
  6065. ((equal complete-file '(4))
  6066. ;; Completing read for file names.
  6067. (setq file (read-file-name "File: "))
  6068. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6069. (pwd1 (file-name-as-directory (abbreviate-file-name
  6070. (expand-file-name ".")))))
  6071. (cond
  6072. ((equal complete-file '(16))
  6073. (setq link (org-make-link
  6074. "file:"
  6075. (abbreviate-file-name (expand-file-name file)))))
  6076. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6077. (setq link (org-make-link "file:" (match-string 1 file))))
  6078. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6079. (expand-file-name file))
  6080. (setq link (org-make-link
  6081. "file:" (match-string 1 (expand-file-name file)))))
  6082. (t (setq link (org-make-link "file:" file))))))
  6083. (t
  6084. ;; Read link, with completion for stored links.
  6085. (with-output-to-temp-buffer "*Org Links*"
  6086. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6087. (when org-stored-links
  6088. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6089. (princ (mapconcat
  6090. (lambda (x)
  6091. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6092. (reverse org-stored-links) "\n"))))
  6093. (let ((cw (selected-window)))
  6094. (select-window (get-buffer-window "*Org Links*"))
  6095. (shrink-window-if-larger-than-buffer)
  6096. (setq truncate-lines t)
  6097. (select-window cw))
  6098. ;; Fake a link history, containing the stored links.
  6099. (setq tmphist (append (mapcar 'car org-stored-links)
  6100. org-insert-link-history))
  6101. (unwind-protect
  6102. (setq link (org-completing-read
  6103. "Link: "
  6104. (append
  6105. (mapcar (lambda (x) (list (concat (car x) ":")))
  6106. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6107. (mapcar (lambda (x) (list (concat x ":")))
  6108. org-link-types))
  6109. nil nil nil
  6110. 'tmphist
  6111. (or (car (car org-stored-links)))))
  6112. (set-window-configuration wcf)
  6113. (kill-buffer "*Org Links*"))
  6114. (setq entry (assoc link org-stored-links))
  6115. (or entry (push link org-insert-link-history))
  6116. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6117. (not org-keep-stored-link-after-insertion))
  6118. (setq org-stored-links (delq (assoc link org-stored-links)
  6119. org-stored-links)))
  6120. (setq desc (or desc (nth 1 entry)))))
  6121. (if (string-match org-plain-link-re link)
  6122. ;; URL-like link, normalize the use of angular brackets.
  6123. (setq link (org-make-link (org-remove-angle-brackets link))))
  6124. ;; Check if we are linking to the current file with a search option
  6125. ;; If yes, simplify the link by using only the search option.
  6126. (when (and buffer-file-name
  6127. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6128. (let* ((path (match-string 1 link))
  6129. (case-fold-search nil)
  6130. (search (match-string 2 link)))
  6131. (save-match-data
  6132. (if (equal (file-truename buffer-file-name) (file-truename path))
  6133. ;; We are linking to this same file, with a search option
  6134. (setq link search)))))
  6135. ;; Check if we can/should use a relative path. If yes, simplify the link
  6136. (when (string-match "\\<file:\\(.*\\)" link)
  6137. (let* ((path (match-string 1 link))
  6138. (origpath path)
  6139. (case-fold-search nil))
  6140. (cond
  6141. ((eq org-link-file-path-type 'absolute)
  6142. (setq path (abbreviate-file-name (expand-file-name path))))
  6143. ((eq org-link-file-path-type 'noabbrev)
  6144. (setq path (expand-file-name path)))
  6145. ((eq org-link-file-path-type 'relative)
  6146. (setq path (file-relative-name path)))
  6147. (t
  6148. (save-match-data
  6149. (if (string-match (concat "^" (regexp-quote
  6150. (file-name-as-directory
  6151. (expand-file-name "."))))
  6152. (expand-file-name path))
  6153. ;; We are linking a file with relative path name.
  6154. (setq path (substring (expand-file-name path)
  6155. (match-end 0)))))))
  6156. (setq link (concat "file:" path))
  6157. (if (equal desc origpath)
  6158. (setq desc path))))
  6159. (if org-make-link-description-function
  6160. (setq desc (funcall org-make-link-description-function link desc)))
  6161. (setq desc (read-string "Description: " desc))
  6162. (unless (string-match "\\S-" desc) (setq desc nil))
  6163. (if remove (apply 'delete-region remove))
  6164. (insert (org-make-link-string link desc))))
  6165. (defun org-completing-read (&rest args)
  6166. (let ((minibuffer-local-completion-map
  6167. (copy-keymap minibuffer-local-completion-map)))
  6168. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6169. (apply 'completing-read args)))
  6170. ;;; Opening/following a link
  6171. (defvar org-link-search-failed nil)
  6172. (defun org-next-link ()
  6173. "Move forward to the next link.
  6174. If the link is in hidden text, expose it."
  6175. (interactive)
  6176. (when (and org-link-search-failed (eq this-command last-command))
  6177. (goto-char (point-min))
  6178. (message "Link search wrapped back to beginning of buffer"))
  6179. (setq org-link-search-failed nil)
  6180. (let* ((pos (point))
  6181. (ct (org-context))
  6182. (a (assoc :link ct)))
  6183. (if a (goto-char (nth 2 a)))
  6184. (if (re-search-forward org-any-link-re nil t)
  6185. (progn
  6186. (goto-char (match-beginning 0))
  6187. (if (org-invisible-p) (org-show-context)))
  6188. (goto-char pos)
  6189. (setq org-link-search-failed t)
  6190. (error "No further link found"))))
  6191. (defun org-previous-link ()
  6192. "Move backward to the previous link.
  6193. If the link is in hidden text, expose it."
  6194. (interactive)
  6195. (when (and org-link-search-failed (eq this-command last-command))
  6196. (goto-char (point-max))
  6197. (message "Link search wrapped back to end of buffer"))
  6198. (setq org-link-search-failed nil)
  6199. (let* ((pos (point))
  6200. (ct (org-context))
  6201. (a (assoc :link ct)))
  6202. (if a (goto-char (nth 1 a)))
  6203. (if (re-search-backward org-any-link-re nil t)
  6204. (progn
  6205. (goto-char (match-beginning 0))
  6206. (if (org-invisible-p) (org-show-context)))
  6207. (goto-char pos)
  6208. (setq org-link-search-failed t)
  6209. (error "No further link found"))))
  6210. (defun org-find-file-at-mouse (ev)
  6211. "Open file link or URL at mouse."
  6212. (interactive "e")
  6213. (mouse-set-point ev)
  6214. (org-open-at-point 'in-emacs))
  6215. (defun org-open-at-mouse (ev)
  6216. "Open file link or URL at mouse."
  6217. (interactive "e")
  6218. (mouse-set-point ev)
  6219. (org-open-at-point))
  6220. (defvar org-window-config-before-follow-link nil
  6221. "The window configuration before following a link.
  6222. This is saved in case the need arises to restore it.")
  6223. (defvar org-open-link-marker (make-marker)
  6224. "Marker pointing to the location where `org-open-at-point; was called.")
  6225. ;;;###autoload
  6226. (defun org-open-at-point-global ()
  6227. "Follow a link like Org-mode does.
  6228. This command can be called in any mode to follow a link that has
  6229. Org-mode syntax."
  6230. (interactive)
  6231. (org-run-like-in-org-mode 'org-open-at-point))
  6232. ;;;###autoload
  6233. (defun org-open-link-from-string (s &optional arg)
  6234. "Open a link in the string S, as if it was in Org-mode."
  6235. (interactive "sLink: \nP")
  6236. (with-temp-buffer
  6237. (let ((org-inhibit-startup t))
  6238. (org-mode)
  6239. (insert s)
  6240. (goto-char (point-min))
  6241. (org-open-at-point arg))))
  6242. (defun org-open-at-point (&optional in-emacs)
  6243. "Open link at or after point.
  6244. If there is no link at point, this function will search forward up to
  6245. the end of the current subtree.
  6246. Normally, files will be opened by an appropriate application. If the
  6247. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6248. (interactive "P")
  6249. (org-load-modules-maybe)
  6250. (move-marker org-open-link-marker (point))
  6251. (setq org-window-config-before-follow-link (current-window-configuration))
  6252. (org-remove-occur-highlights nil nil t)
  6253. (if (org-at-timestamp-p t)
  6254. (org-follow-timestamp-link)
  6255. (let (type path link line search (pos (point)))
  6256. (catch 'match
  6257. (save-excursion
  6258. (skip-chars-forward "^]\n\r")
  6259. (when (org-in-regexp org-bracket-link-regexp)
  6260. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6261. (while (string-match " *\n *" link)
  6262. (setq link (replace-match " " t t link)))
  6263. (setq link (org-link-expand-abbrev link))
  6264. (if (string-match org-link-re-with-space2 link)
  6265. (setq type (match-string 1 link) path (match-string 2 link))
  6266. (setq type "thisfile" path link))
  6267. (throw 'match t)))
  6268. (when (get-text-property (point) 'org-linked-text)
  6269. (setq type "thisfile"
  6270. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6271. (1+ (point)) (point))
  6272. path (buffer-substring
  6273. (previous-single-property-change pos 'org-linked-text)
  6274. (next-single-property-change pos 'org-linked-text)))
  6275. (throw 'match t))
  6276. (save-excursion
  6277. (when (or (org-in-regexp org-angle-link-re)
  6278. (org-in-regexp org-plain-link-re))
  6279. (setq type (match-string 1) path (match-string 2))
  6280. (throw 'match t)))
  6281. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6282. (setq type "tree-match"
  6283. path (match-string 1))
  6284. (throw 'match t))
  6285. (save-excursion
  6286. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6287. (setq type "tags"
  6288. path (match-string 1))
  6289. (while (string-match ":" path)
  6290. (setq path (replace-match "+" t t path)))
  6291. (throw 'match t))))
  6292. (unless path
  6293. (error "No link found"))
  6294. ;; Remove any trailing spaces in path
  6295. (if (string-match " +\\'" path)
  6296. (setq path (replace-match "" t t path)))
  6297. (cond
  6298. ((assoc type org-link-protocols)
  6299. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6300. ((equal type "mailto")
  6301. (let ((cmd (car org-link-mailto-program))
  6302. (args (cdr org-link-mailto-program)) args1
  6303. (address path) (subject "") a)
  6304. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6305. (setq address (match-string 1 path)
  6306. subject (org-link-escape (match-string 2 path))))
  6307. (while args
  6308. (cond
  6309. ((not (stringp (car args))) (push (pop args) args1))
  6310. (t (setq a (pop args))
  6311. (if (string-match "%a" a)
  6312. (setq a (replace-match address t t a)))
  6313. (if (string-match "%s" a)
  6314. (setq a (replace-match subject t t a)))
  6315. (push a args1))))
  6316. (apply cmd (nreverse args1))))
  6317. ((member type '("http" "https" "ftp" "news"))
  6318. (browse-url (concat type ":" (org-link-escape
  6319. path org-link-escape-chars-browser))))
  6320. ((member type '("message"))
  6321. (browse-url (concat type ":" path)))
  6322. ((string= type "tags")
  6323. (org-tags-view in-emacs path))
  6324. ((string= type "thisfile")
  6325. (if in-emacs
  6326. (switch-to-buffer-other-window
  6327. (org-get-buffer-for-internal-link (current-buffer)))
  6328. (org-mark-ring-push))
  6329. (let ((cmd `(org-link-search
  6330. ,path
  6331. ,(cond ((equal in-emacs '(4)) 'occur)
  6332. ((equal in-emacs '(16)) 'org-occur)
  6333. (t nil))
  6334. ,pos)))
  6335. (condition-case nil (eval cmd)
  6336. (error (progn (widen) (eval cmd))))))
  6337. ((string= type "tree-match")
  6338. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6339. ((string= type "file")
  6340. (if (string-match "::\\([0-9]+\\)\\'" path)
  6341. (setq line (string-to-number (match-string 1 path))
  6342. path (substring path 0 (match-beginning 0)))
  6343. (if (string-match "::\\(.+\\)\\'" path)
  6344. (setq search (match-string 1 path)
  6345. path (substring path 0 (match-beginning 0)))))
  6346. (if (string-match "[*?{]" (file-name-nondirectory path))
  6347. (dired path)
  6348. (org-open-file path in-emacs line search)))
  6349. ((string= type "news")
  6350. (require 'org-gnus)
  6351. (org-gnus-follow-link path))
  6352. ((string= type "shell")
  6353. (let ((cmd path))
  6354. (if (or (not org-confirm-shell-link-function)
  6355. (funcall org-confirm-shell-link-function
  6356. (format "Execute \"%s\" in shell? "
  6357. (org-add-props cmd nil
  6358. 'face 'org-warning))))
  6359. (progn
  6360. (message "Executing %s" cmd)
  6361. (shell-command cmd))
  6362. (error "Abort"))))
  6363. ((string= type "elisp")
  6364. (let ((cmd path))
  6365. (if (or (not org-confirm-elisp-link-function)
  6366. (funcall org-confirm-elisp-link-function
  6367. (format "Execute \"%s\" as elisp? "
  6368. (org-add-props cmd nil
  6369. 'face 'org-warning))))
  6370. (message "%s => %s" cmd (eval (read cmd)))
  6371. (error "Abort"))))
  6372. (t
  6373. (browse-url-at-point)))))
  6374. (move-marker org-open-link-marker nil)
  6375. (run-hook-with-args 'org-follow-link-hook))
  6376. ;;;; Time estimates
  6377. (defun org-get-effort (&optional pom)
  6378. "Get the effort estimate for the current entry."
  6379. (org-entry-get pom org-effort-property))
  6380. ;;; File search
  6381. (defvar org-create-file-search-functions nil
  6382. "List of functions to construct the right search string for a file link.
  6383. These functions are called in turn with point at the location to
  6384. which the link should point.
  6385. A function in the hook should first test if it would like to
  6386. handle this file type, for example by checking the major-mode or
  6387. the file extension. If it decides not to handle this file, it
  6388. should just return nil to give other functions a chance. If it
  6389. does handle the file, it must return the search string to be used
  6390. when following the link. The search string will be part of the
  6391. file link, given after a double colon, and `org-open-at-point'
  6392. will automatically search for it. If special measures must be
  6393. taken to make the search successful, another function should be
  6394. added to the companion hook `org-execute-file-search-functions',
  6395. which see.
  6396. A function in this hook may also use `setq' to set the variable
  6397. `description' to provide a suggestion for the descriptive text to
  6398. be used for this link when it gets inserted into an Org-mode
  6399. buffer with \\[org-insert-link].")
  6400. (defvar org-execute-file-search-functions nil
  6401. "List of functions to execute a file search triggered by a link.
  6402. Functions added to this hook must accept a single argument, the
  6403. search string that was part of the file link, the part after the
  6404. double colon. The function must first check if it would like to
  6405. handle this search, for example by checking the major-mode or the
  6406. file extension. If it decides not to handle this search, it
  6407. should just return nil to give other functions a chance. If it
  6408. does handle the search, it must return a non-nil value to keep
  6409. other functions from trying.
  6410. Each function can access the current prefix argument through the
  6411. variable `current-prefix-argument'. Note that a single prefix is
  6412. used to force opening a link in Emacs, so it may be good to only
  6413. use a numeric or double prefix to guide the search function.
  6414. In case this is needed, a function in this hook can also restore
  6415. the window configuration before `org-open-at-point' was called using:
  6416. (set-window-configuration org-window-config-before-follow-link)")
  6417. (defun org-link-search (s &optional type avoid-pos)
  6418. "Search for a link search option.
  6419. If S is surrounded by forward slashes, it is interpreted as a
  6420. regular expression. In org-mode files, this will create an `org-occur'
  6421. sparse tree. In ordinary files, `occur' will be used to list matches.
  6422. If the current buffer is in `dired-mode', grep will be used to search
  6423. in all files. If AVOID-POS is given, ignore matches near that position."
  6424. (let ((case-fold-search t)
  6425. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6426. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6427. (append '(("") (" ") ("\t") ("\n"))
  6428. org-emphasis-alist)
  6429. "\\|") "\\)"))
  6430. (pos (point))
  6431. (pre nil) (post nil)
  6432. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6433. (cond
  6434. ;; First check if there are any special
  6435. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6436. ;; Now try the builtin stuff
  6437. ((save-excursion
  6438. (goto-char (point-min))
  6439. (and
  6440. (re-search-forward
  6441. (concat "<<" (regexp-quote s0) ">>") nil t)
  6442. (setq type 'dedicated
  6443. pos (match-beginning 0))))
  6444. ;; There is an exact target for this
  6445. (goto-char pos))
  6446. ((string-match "^/\\(.*\\)/$" s)
  6447. ;; A regular expression
  6448. (cond
  6449. ((org-mode-p)
  6450. (org-occur (match-string 1 s)))
  6451. ;;((eq major-mode 'dired-mode)
  6452. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6453. (t (org-do-occur (match-string 1 s)))))
  6454. (t
  6455. ;; A normal search strings
  6456. (when (equal (string-to-char s) ?*)
  6457. ;; Anchor on headlines, post may include tags.
  6458. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6459. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6460. s (substring s 1)))
  6461. (remove-text-properties
  6462. 0 (length s)
  6463. '(face nil mouse-face nil keymap nil fontified nil) s)
  6464. ;; Make a series of regular expressions to find a match
  6465. (setq words (org-split-string s "[ \n\r\t]+")
  6466. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6467. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6468. "\\)" markers)
  6469. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6470. re2a (concat "[ \t\r\n]" re2a_)
  6471. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6472. re4 (concat "[^a-zA-Z_]" re4_)
  6473. re1 (concat pre re2 post)
  6474. re3 (concat pre (if pre re4_ re4) post)
  6475. re5 (concat pre ".*" re4)
  6476. re2 (concat pre re2)
  6477. re2a (concat pre (if pre re2a_ re2a))
  6478. re4 (concat pre (if pre re4_ re4))
  6479. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6480. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6481. re5 "\\)"
  6482. ))
  6483. (cond
  6484. ((eq type 'org-occur) (org-occur reall))
  6485. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6486. (t (goto-char (point-min))
  6487. (setq type 'fuzzy)
  6488. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6489. (org-search-not-self 1 re1 nil t)
  6490. (org-search-not-self 1 re2 nil t)
  6491. (org-search-not-self 1 re2a nil t)
  6492. (org-search-not-self 1 re3 nil t)
  6493. (org-search-not-self 1 re4 nil t)
  6494. (org-search-not-self 1 re5 nil t)
  6495. )
  6496. (goto-char (match-beginning 1))
  6497. (goto-char pos)
  6498. (error "No match")))))
  6499. (t
  6500. ;; Normal string-search
  6501. (goto-char (point-min))
  6502. (if (search-forward s nil t)
  6503. (goto-char (match-beginning 0))
  6504. (error "No match"))))
  6505. (and (org-mode-p) (org-show-context 'link-search))
  6506. type))
  6507. (defun org-search-not-self (group &rest args)
  6508. "Execute `re-search-forward', but only accept matches that do not
  6509. enclose the position of `org-open-link-marker'."
  6510. (let ((m org-open-link-marker))
  6511. (catch 'exit
  6512. (while (apply 're-search-forward args)
  6513. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6514. (goto-char (match-end group))
  6515. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6516. (> (match-beginning 0) (marker-position m))
  6517. (< (match-end 0) (marker-position m)))
  6518. (save-match-data
  6519. (or (not (org-in-regexp
  6520. org-bracket-link-analytic-regexp 1))
  6521. (not (match-end 4)) ; no description
  6522. (and (<= (match-beginning 4) (point))
  6523. (>= (match-end 4) (point))))))
  6524. (throw 'exit (point))))))))
  6525. (defun org-get-buffer-for-internal-link (buffer)
  6526. "Return a buffer to be used for displaying the link target of internal links."
  6527. (cond
  6528. ((not org-display-internal-link-with-indirect-buffer)
  6529. buffer)
  6530. ((string-match "(Clone)$" (buffer-name buffer))
  6531. (message "Buffer is already a clone, not making another one")
  6532. ;; we also do not modify visibility in this case
  6533. buffer)
  6534. (t ; make a new indirect buffer for displaying the link
  6535. (let* ((bn (buffer-name buffer))
  6536. (ibn (concat bn "(Clone)"))
  6537. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6538. (with-current-buffer ib (org-overview))
  6539. ib))))
  6540. (defun org-do-occur (regexp &optional cleanup)
  6541. "Call the Emacs command `occur'.
  6542. If CLEANUP is non-nil, remove the printout of the regular expression
  6543. in the *Occur* buffer. This is useful if the regex is long and not useful
  6544. to read."
  6545. (occur regexp)
  6546. (when cleanup
  6547. (let ((cwin (selected-window)) win beg end)
  6548. (when (setq win (get-buffer-window "*Occur*"))
  6549. (select-window win))
  6550. (goto-char (point-min))
  6551. (when (re-search-forward "match[a-z]+" nil t)
  6552. (setq beg (match-end 0))
  6553. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6554. (setq end (1- (match-beginning 0)))))
  6555. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6556. (goto-char (point-min))
  6557. (select-window cwin))))
  6558. ;;; The mark ring for links jumps
  6559. (defvar org-mark-ring nil
  6560. "Mark ring for positions before jumps in Org-mode.")
  6561. (defvar org-mark-ring-last-goto nil
  6562. "Last position in the mark ring used to go back.")
  6563. ;; Fill and close the ring
  6564. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6565. (loop for i from 1 to org-mark-ring-length do
  6566. (push (make-marker) org-mark-ring))
  6567. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6568. org-mark-ring)
  6569. (defun org-mark-ring-push (&optional pos buffer)
  6570. "Put the current position or POS into the mark ring and rotate it."
  6571. (interactive)
  6572. (setq pos (or pos (point)))
  6573. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6574. (move-marker (car org-mark-ring)
  6575. (or pos (point))
  6576. (or buffer (current-buffer)))
  6577. (message "%s"
  6578. (substitute-command-keys
  6579. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6580. (defun org-mark-ring-goto (&optional n)
  6581. "Jump to the previous position in the mark ring.
  6582. With prefix arg N, jump back that many stored positions. When
  6583. called several times in succession, walk through the entire ring.
  6584. Org-mode commands jumping to a different position in the current file,
  6585. or to another Org-mode file, automatically push the old position
  6586. onto the ring."
  6587. (interactive "p")
  6588. (let (p m)
  6589. (if (eq last-command this-command)
  6590. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6591. (setq p org-mark-ring))
  6592. (setq org-mark-ring-last-goto p)
  6593. (setq m (car p))
  6594. (switch-to-buffer (marker-buffer m))
  6595. (goto-char m)
  6596. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6597. (defun org-remove-angle-brackets (s)
  6598. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6599. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6600. s)
  6601. (defun org-add-angle-brackets (s)
  6602. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6603. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6604. s)
  6605. (defun org-remove-double-quotes (s)
  6606. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6607. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6608. s)
  6609. ;;; Following specific links
  6610. (defun org-follow-timestamp-link ()
  6611. (cond
  6612. ((org-at-date-range-p t)
  6613. (let ((org-agenda-start-on-weekday)
  6614. (t1 (match-string 1))
  6615. (t2 (match-string 2)))
  6616. (setq t1 (time-to-days (org-time-string-to-time t1))
  6617. t2 (time-to-days (org-time-string-to-time t2)))
  6618. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6619. ((org-at-timestamp-p t)
  6620. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6621. (substring (match-string 1) 0 10)))
  6622. 1))
  6623. (t (error "This should not happen"))))
  6624. ;;; Following file links
  6625. (defvar org-wait nil)
  6626. (defun org-open-file (path &optional in-emacs line search)
  6627. "Open the file at PATH.
  6628. First, this expands any special file name abbreviations. Then the
  6629. configuration variable `org-file-apps' is checked if it contains an
  6630. entry for this file type, and if yes, the corresponding command is launched.
  6631. If no application is found, Emacs simply visits the file.
  6632. With optional argument IN-EMACS, Emacs will visit the file.
  6633. Optional LINE specifies a line to go to, optional SEARCH a string to
  6634. search for. If LINE or SEARCH is given, the file will always be
  6635. opened in Emacs.
  6636. If the file does not exist, an error is thrown."
  6637. (setq in-emacs (or in-emacs line search))
  6638. (let* ((file (if (equal path "")
  6639. buffer-file-name
  6640. (substitute-in-file-name (expand-file-name path))))
  6641. (apps (append org-file-apps (org-default-apps)))
  6642. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6643. (dirp (if remp nil (file-directory-p file)))
  6644. (dfile (downcase file))
  6645. (old-buffer (current-buffer))
  6646. (old-pos (point))
  6647. (old-mode major-mode)
  6648. ext cmd)
  6649. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6650. (setq ext (match-string 1 dfile))
  6651. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6652. (setq ext (match-string 1 dfile))))
  6653. (if in-emacs
  6654. (setq cmd 'emacs)
  6655. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6656. (and dirp (cdr (assoc 'directory apps)))
  6657. (cdr (assoc ext apps))
  6658. (cdr (assoc t apps)))))
  6659. (when (eq cmd 'mailcap)
  6660. (require 'mailcap)
  6661. (mailcap-parse-mailcaps)
  6662. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6663. (command (mailcap-mime-info mime-type)))
  6664. (if (stringp command)
  6665. (setq cmd command)
  6666. (setq cmd 'emacs))))
  6667. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6668. (not (file-exists-p file))
  6669. (not org-open-non-existing-files))
  6670. (error "No such file: %s" file))
  6671. (cond
  6672. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6673. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6674. (while (string-match "['\"]%s['\"]" cmd)
  6675. (setq cmd (replace-match "%s" t t cmd)))
  6676. (while (string-match "%s" cmd)
  6677. (setq cmd (replace-match
  6678. (save-match-data (shell-quote-argument file))
  6679. t t cmd)))
  6680. (save-window-excursion
  6681. (start-process-shell-command cmd nil cmd)
  6682. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6683. ))
  6684. ((or (stringp cmd)
  6685. (eq cmd 'emacs))
  6686. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6687. (widen)
  6688. (if line (goto-line line)
  6689. (if search (org-link-search search))))
  6690. ((consp cmd)
  6691. (eval cmd))
  6692. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6693. (and (org-mode-p) (eq old-mode 'org-mode)
  6694. (or (not (equal old-buffer (current-buffer)))
  6695. (not (equal old-pos (point))))
  6696. (org-mark-ring-push old-pos old-buffer))))
  6697. (defun org-default-apps ()
  6698. "Return the default applications for this operating system."
  6699. (cond
  6700. ((eq system-type 'darwin)
  6701. org-file-apps-defaults-macosx)
  6702. ((eq system-type 'windows-nt)
  6703. org-file-apps-defaults-windowsnt)
  6704. (t org-file-apps-defaults-gnu)))
  6705. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6706. (defun org-file-remote-p (file)
  6707. "Test whether FILE specifies a location on a remote system.
  6708. Return non-nil if the location is indeed remote.
  6709. For example, the filename \"/user@host:/foo\" specifies a location
  6710. on the system \"/user@host:\"."
  6711. (cond ((fboundp 'file-remote-p)
  6712. (file-remote-p file))
  6713. ((fboundp 'tramp-handle-file-remote-p)
  6714. (tramp-handle-file-remote-p file))
  6715. ((and (boundp 'ange-ftp-name-format)
  6716. (string-match (car ange-ftp-name-format) file))
  6717. t)
  6718. (t nil)))
  6719. ;;;; Refiling
  6720. (defun org-get-org-file ()
  6721. "Read a filename, with default directory `org-directory'."
  6722. (let ((default (or org-default-notes-file remember-data-file)))
  6723. (read-file-name (format "File name [%s]: " default)
  6724. (file-name-as-directory org-directory)
  6725. default)))
  6726. (defun org-notes-order-reversed-p ()
  6727. "Check if the current file should receive notes in reversed order."
  6728. (cond
  6729. ((not org-reverse-note-order) nil)
  6730. ((eq t org-reverse-note-order) t)
  6731. ((not (listp org-reverse-note-order)) nil)
  6732. (t (catch 'exit
  6733. (let ((all org-reverse-note-order)
  6734. entry)
  6735. (while (setq entry (pop all))
  6736. (if (string-match (car entry) buffer-file-name)
  6737. (throw 'exit (cdr entry))))
  6738. nil)))))
  6739. (defvar org-refile-target-table nil
  6740. "The list of refile targets, created by `org-refile'.")
  6741. (defvar org-agenda-new-buffers nil
  6742. "Buffers created to visit agenda files.")
  6743. (defun org-get-refile-targets (&optional default-buffer)
  6744. "Produce a table with refile targets."
  6745. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6746. targets txt re files f desc descre)
  6747. (with-current-buffer (or default-buffer (current-buffer))
  6748. (while (setq entry (pop entries))
  6749. (setq files (car entry) desc (cdr entry))
  6750. (cond
  6751. ((null files) (setq files (list (current-buffer))))
  6752. ((eq files 'org-agenda-files)
  6753. (setq files (org-agenda-files 'unrestricted)))
  6754. ((and (symbolp files) (fboundp files))
  6755. (setq files (funcall files)))
  6756. ((and (symbolp files) (boundp files))
  6757. (setq files (symbol-value files))))
  6758. (if (stringp files) (setq files (list files)))
  6759. (cond
  6760. ((eq (car desc) :tag)
  6761. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6762. ((eq (car desc) :todo)
  6763. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6764. ((eq (car desc) :regexp)
  6765. (setq descre (cdr desc)))
  6766. ((eq (car desc) :level)
  6767. (setq descre (concat "^\\*\\{" (number-to-string
  6768. (if org-odd-levels-only
  6769. (1- (* 2 (cdr desc)))
  6770. (cdr desc)))
  6771. "\\}[ \t]")))
  6772. ((eq (car desc) :maxlevel)
  6773. (setq descre (concat "^\\*\\{1," (number-to-string
  6774. (if org-odd-levels-only
  6775. (1- (* 2 (cdr desc)))
  6776. (cdr desc)))
  6777. "\\}[ \t]")))
  6778. (t (error "Bad refiling target description %s" desc)))
  6779. (while (setq f (pop files))
  6780. (save-excursion
  6781. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6782. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6783. (save-excursion
  6784. (save-restriction
  6785. (widen)
  6786. (goto-char (point-min))
  6787. (while (re-search-forward descre nil t)
  6788. (goto-char (point-at-bol))
  6789. (when (looking-at org-complex-heading-regexp)
  6790. (setq txt (match-string 4)
  6791. re (concat "^" (regexp-quote
  6792. (buffer-substring (match-beginning 1)
  6793. (match-end 4)))))
  6794. (if (match-end 5) (setq re (concat re "[ \t]+"
  6795. (regexp-quote
  6796. (match-string 5)))))
  6797. (setq re (concat re "[ \t]*$"))
  6798. (when org-refile-use-outline-path
  6799. (setq txt (mapconcat 'identity
  6800. (append
  6801. (if (eq org-refile-use-outline-path 'file)
  6802. (list (file-name-nondirectory
  6803. (buffer-file-name (buffer-base-buffer))))
  6804. (if (eq org-refile-use-outline-path 'full-file-path)
  6805. (list (buffer-file-name (buffer-base-buffer)))))
  6806. (org-get-outline-path)
  6807. (list txt))
  6808. "/")))
  6809. (push (list txt f re (point)) targets))
  6810. (goto-char (point-at-eol))))))))
  6811. (nreverse targets))))
  6812. (defun org-get-outline-path ()
  6813. "Return the outline path to the current entry, as a list."
  6814. (let (rtn)
  6815. (save-excursion
  6816. (while (org-up-heading-safe)
  6817. (when (looking-at org-complex-heading-regexp)
  6818. (push (org-match-string-no-properties 4) rtn)))
  6819. rtn)))
  6820. (defvar org-refile-history nil
  6821. "History for refiling operations.")
  6822. (defun org-refile (&optional goto default-buffer)
  6823. "Move the entry at point to another heading.
  6824. The list of target headings is compiled using the information in
  6825. `org-refile-targets', which see. This list is created before each use
  6826. and will therefore always be up-to-date.
  6827. At the target location, the entry is filed as a subitem of the target heading.
  6828. Depending on `org-reverse-note-order', the new subitem will either be the
  6829. first of the last subitem.
  6830. With prefix arg GOTO, the command will only visit the target location,
  6831. not actually move anything.
  6832. With a double prefix `C-c C-c', go to the location where the last refiling
  6833. operation has put the subtree."
  6834. (interactive "P")
  6835. (let* ((cbuf (current-buffer))
  6836. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6837. pos it nbuf file re level reversed)
  6838. (if (equal goto '(16))
  6839. (org-refile-goto-last-stored)
  6840. (when (setq it (org-refile-get-location
  6841. (if goto "Goto: " "Refile to: ") default-buffer))
  6842. (setq file (nth 1 it)
  6843. re (nth 2 it)
  6844. pos (nth 3 it))
  6845. (setq nbuf (or (find-buffer-visiting file)
  6846. (find-file-noselect file)))
  6847. (if goto
  6848. (progn
  6849. (switch-to-buffer nbuf)
  6850. (goto-char pos)
  6851. (org-show-context 'org-goto))
  6852. (org-copy-subtree 1 nil t)
  6853. (save-excursion
  6854. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6855. (find-file-noselect file))))
  6856. (setq reversed (org-notes-order-reversed-p))
  6857. (save-excursion
  6858. (save-restriction
  6859. (widen)
  6860. (goto-char pos)
  6861. (looking-at outline-regexp)
  6862. (setq level (org-get-valid-level (funcall outline-level) 1))
  6863. (goto-char
  6864. (if reversed
  6865. (outline-next-heading)
  6866. (or (save-excursion (outline-get-next-sibling))
  6867. (org-end-of-subtree t t)
  6868. (point-max))))
  6869. (bookmark-set "org-refile-last-stored")
  6870. (org-paste-subtree level))))
  6871. (org-cut-subtree)
  6872. (setq org-markers-to-move nil)
  6873. (message "Entry refiled to \"%s\"" (car it)))))))
  6874. (defun org-refile-goto-last-stored ()
  6875. "Go to the location where the last refile was stored."
  6876. (interactive)
  6877. (bookmark-jump "org-refile-last-stored")
  6878. (message "This is the location of the last refile"))
  6879. (defun org-refile-get-location (&optional prompt default-buffer)
  6880. "Prompt the user for a refile location, using PROMPT."
  6881. (let ((org-refile-targets org-refile-targets)
  6882. (org-refile-use-outline-path org-refile-use-outline-path))
  6883. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6884. (unless org-refile-target-table
  6885. (error "No refile targets"))
  6886. (let* ((cbuf (current-buffer))
  6887. (cfunc (if org-refile-use-outline-path
  6888. 'org-olpath-completing-read
  6889. 'completing-read))
  6890. (extra (if org-refile-use-outline-path "/" ""))
  6891. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6892. (fname (and filename (file-truename filename)))
  6893. (tbl (mapcar
  6894. (lambda (x)
  6895. (if (not (equal fname (file-truename (nth 1 x))))
  6896. (cons (concat (car x) extra " ("
  6897. (file-name-nondirectory (nth 1 x)) ")")
  6898. (cdr x))
  6899. (cons (concat (car x) extra) (cdr x))))
  6900. org-refile-target-table))
  6901. (completion-ignore-case t))
  6902. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6903. tbl)))
  6904. (defun org-olpath-completing-read (prompt collection &rest args)
  6905. "Read an outline path like a file name."
  6906. (let ((thetable collection))
  6907. (apply
  6908. 'completing-read prompt
  6909. (lambda (string predicate &optional flag)
  6910. (let (rtn r s f (l (length string)))
  6911. (cond
  6912. ((eq flag nil)
  6913. ;; try completion
  6914. (try-completion string thetable))
  6915. ((eq flag t)
  6916. ;; all-completions
  6917. (setq rtn (all-completions string thetable predicate))
  6918. (mapcar
  6919. (lambda (x)
  6920. (setq r (substring x l))
  6921. (if (string-match " ([^)]*)$" x)
  6922. (setq f (match-string 0 x))
  6923. (setq f ""))
  6924. (if (string-match "/" r)
  6925. (concat string (substring r 0 (match-end 0)) f)
  6926. x))
  6927. rtn))
  6928. ((eq flag 'lambda)
  6929. ;; exact match?
  6930. (assoc string thetable)))
  6931. ))
  6932. args)))
  6933. ;;;; Dynamic blocks
  6934. (defun org-find-dblock (name)
  6935. "Find the first dynamic block with name NAME in the buffer.
  6936. If not found, stay at current position and return nil."
  6937. (let (pos)
  6938. (save-excursion
  6939. (goto-char (point-min))
  6940. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6941. nil t)
  6942. (match-beginning 0))))
  6943. (if pos (goto-char pos))
  6944. pos))
  6945. (defconst org-dblock-start-re
  6946. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6947. "Matches the startline of a dynamic block, with parameters.")
  6948. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6949. "Matches the end of a dyhamic block.")
  6950. (defun org-create-dblock (plist)
  6951. "Create a dynamic block section, with parameters taken from PLIST.
  6952. PLIST must containe a :name entry which is used as name of the block."
  6953. (unless (bolp) (newline))
  6954. (let ((name (plist-get plist :name)))
  6955. (insert "#+BEGIN: " name)
  6956. (while plist
  6957. (if (eq (car plist) :name)
  6958. (setq plist (cddr plist))
  6959. (insert " " (prin1-to-string (pop plist)))))
  6960. (insert "\n\n#+END:\n")
  6961. (beginning-of-line -2)))
  6962. (defun org-prepare-dblock ()
  6963. "Prepare dynamic block for refresh.
  6964. This empties the block, puts the cursor at the insert position and returns
  6965. the property list including an extra property :name with the block name."
  6966. (unless (looking-at org-dblock-start-re)
  6967. (error "Not at a dynamic block"))
  6968. (let* ((begdel (1+ (match-end 0)))
  6969. (name (org-no-properties (match-string 1)))
  6970. (params (append (list :name name)
  6971. (read (concat "(" (match-string 3) ")")))))
  6972. (unless (re-search-forward org-dblock-end-re nil t)
  6973. (error "Dynamic block not terminated"))
  6974. (setq params
  6975. (append params
  6976. (list :content (buffer-substring
  6977. begdel (match-beginning 0)))))
  6978. (delete-region begdel (match-beginning 0))
  6979. (goto-char begdel)
  6980. (open-line 1)
  6981. params))
  6982. (defun org-map-dblocks (&optional command)
  6983. "Apply COMMAND to all dynamic blocks in the current buffer.
  6984. If COMMAND is not given, use `org-update-dblock'."
  6985. (let ((cmd (or command 'org-update-dblock))
  6986. pos)
  6987. (save-excursion
  6988. (goto-char (point-min))
  6989. (while (re-search-forward org-dblock-start-re nil t)
  6990. (goto-char (setq pos (match-beginning 0)))
  6991. (condition-case nil
  6992. (funcall cmd)
  6993. (error (message "Error during update of dynamic block")))
  6994. (goto-char pos)
  6995. (unless (re-search-forward org-dblock-end-re nil t)
  6996. (error "Dynamic block not terminated"))))))
  6997. (defun org-dblock-update (&optional arg)
  6998. "User command for updating dynamic blocks.
  6999. Update the dynamic block at point. With prefix ARG, update all dynamic
  7000. blocks in the buffer."
  7001. (interactive "P")
  7002. (if arg
  7003. (org-update-all-dblocks)
  7004. (or (looking-at org-dblock-start-re)
  7005. (org-beginning-of-dblock))
  7006. (org-update-dblock)))
  7007. (defun org-update-dblock ()
  7008. "Update the dynamic block at point
  7009. This means to empty the block, parse for parameters and then call
  7010. the correct writing function."
  7011. (save-window-excursion
  7012. (let* ((pos (point))
  7013. (line (org-current-line))
  7014. (params (org-prepare-dblock))
  7015. (name (plist-get params :name))
  7016. (cmd (intern (concat "org-dblock-write:" name))))
  7017. (message "Updating dynamic block `%s' at line %d..." name line)
  7018. (funcall cmd params)
  7019. (message "Updating dynamic block `%s' at line %d...done" name line)
  7020. (goto-char pos))))
  7021. (defun org-beginning-of-dblock ()
  7022. "Find the beginning of the dynamic block at point.
  7023. Error if there is no scuh block at point."
  7024. (let ((pos (point))
  7025. beg)
  7026. (end-of-line 1)
  7027. (if (and (re-search-backward org-dblock-start-re nil t)
  7028. (setq beg (match-beginning 0))
  7029. (re-search-forward org-dblock-end-re nil t)
  7030. (> (match-end 0) pos))
  7031. (goto-char beg)
  7032. (goto-char pos)
  7033. (error "Not in a dynamic block"))))
  7034. (defun org-update-all-dblocks ()
  7035. "Update all dynamic blocks in the buffer.
  7036. This function can be used in a hook."
  7037. (when (org-mode-p)
  7038. (org-map-dblocks 'org-update-dblock)))
  7039. ;;;; Completion
  7040. (defconst org-additional-option-like-keywords
  7041. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7042. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7043. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7044. (defcustom org-structure-template-alist
  7045. '(
  7046. ("s" "#+begin_src ?\n\n#+end_src"
  7047. "<src lang=\"?\">\n\n</src>")
  7048. ("e" "#+begin_example\n?\n#+end_example"
  7049. "<example>\n?\n</example>")
  7050. ("q" "#+begin_quote\n?\n#+end_quote"
  7051. "<quote>\n?\n</quote>")
  7052. ("v" "#+begin_verse\n?\n#+end_verse"
  7053. "<verse>\n?\n/verse>")
  7054. ("l" "#+begin_latex\n?\n#+end_latex"
  7055. "<literal style=\"latex\">\n?\n</literal>")
  7056. ("L" "#+latex: "
  7057. "<literal style=\"latex\">?</literal>")
  7058. ("h" "#+begin_html\n?\n#+end_html"
  7059. "<literal style=\"html\">\n?\n</literal>")
  7060. ("H" "#+html: "
  7061. "<literal style=\"html\">?</literal>")
  7062. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7063. ("A" "#+ascii: ")
  7064. ("i" "#+include %file ?"
  7065. "<include file=%file markup=\"?\">")
  7066. )
  7067. "Structure completion elements.
  7068. This is a list of abbreviation keys and values. The value gets inserted
  7069. it you type @samp{.} followed by the key and then the completion key,
  7070. usually `M-TAB'. %file will be replaced by a file name after prompting
  7071. for the file uning completion.
  7072. There are two templates for each key, the first uses the original Org syntax,
  7073. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7074. the default when the /org-mtags.el/ module has been loaded. See also the
  7075. variable `org-mtags-prefere-muse-templates'.
  7076. This is an experimental feature, it is undecided if it is going to stay in."
  7077. :group 'org-completion
  7078. :type '(repeat
  7079. (string :tag "Key")
  7080. (string :tag "Template")
  7081. (string :tag "Muse Template")))
  7082. (defun org-try-structure-completion ()
  7083. "Try to complete a structure template before point.
  7084. This looks for strings like \"<e\" on an otherwise empty line and
  7085. expands them."
  7086. (let ((l (buffer-substring (point-at-bol) (point)))
  7087. a)
  7088. (when (and (looking-at "[ \t]*$")
  7089. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7090. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7091. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7092. (match-beginning 1)) a)
  7093. t)))
  7094. (defun org-complete-expand-structure-template (start cell)
  7095. "Expand a structure template."
  7096. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7097. (rpl (nth (if musep 2 1) cell)))
  7098. (delete-region start (point))
  7099. (when (string-match "\\`#\\+" rpl)
  7100. (cond
  7101. ((bolp))
  7102. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7103. (delete-region (point-at-bol) (point)))
  7104. (t (newline))))
  7105. (setq start (point))
  7106. (if (string-match "%file" rpl)
  7107. (setq rpl (replace-match
  7108. (concat
  7109. "\""
  7110. (save-match-data
  7111. (abbreviate-file-name (read-file-name "Include file: ")))
  7112. "\"")
  7113. t t rpl)))
  7114. (insert rpl)
  7115. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7116. (defun org-complete (&optional arg)
  7117. "Perform completion on word at point.
  7118. At the beginning of a headline, this completes TODO keywords as given in
  7119. `org-todo-keywords'.
  7120. If the current word is preceded by a backslash, completes the TeX symbols
  7121. that are supported for HTML support.
  7122. If the current word is preceded by \"#+\", completes special words for
  7123. setting file options.
  7124. In the line after \"#+STARTUP:, complete valid keywords.\"
  7125. At all other locations, this simply calls the value of
  7126. `org-completion-fallback-command'."
  7127. (interactive "P")
  7128. (org-without-partial-completion
  7129. (catch 'exit
  7130. (let* ((a nil)
  7131. (end (point))
  7132. (beg1 (save-excursion
  7133. (skip-chars-backward (org-re "[:alnum:]_@"))
  7134. (point)))
  7135. (beg (save-excursion
  7136. (skip-chars-backward "a-zA-Z0-9_:$")
  7137. (point)))
  7138. (confirm (lambda (x) (stringp (car x))))
  7139. (searchhead (equal (char-before beg) ?*))
  7140. (struct
  7141. (when (and (member (char-before beg1) '(?. ?<))
  7142. (setq a (assoc (buffer-substring beg1 (point))
  7143. org-structure-template-alist)))
  7144. (org-complete-expand-structure-template (1- beg1) a)
  7145. (throw 'exit t)))
  7146. (tag (and (equal (char-before beg1) ?:)
  7147. (equal (char-after (point-at-bol)) ?*)))
  7148. (prop (and (equal (char-before beg1) ?:)
  7149. (not (equal (char-after (point-at-bol)) ?*))))
  7150. (texp (equal (char-before beg) ?\\))
  7151. (link (equal (char-before beg) ?\[))
  7152. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7153. beg)
  7154. "#+"))
  7155. (startup (string-match "^#\\+STARTUP:.*"
  7156. (buffer-substring (point-at-bol) (point))))
  7157. (completion-ignore-case opt)
  7158. (type nil)
  7159. (tbl nil)
  7160. (table (cond
  7161. (opt
  7162. (setq type :opt)
  7163. (require 'org-exp)
  7164. (append
  7165. (mapcar
  7166. (lambda (x)
  7167. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7168. (cons (match-string 2 x) (match-string 1 x)))
  7169. (org-split-string (org-get-current-options) "\n"))
  7170. (mapcar 'list org-additional-option-like-keywords)))
  7171. (startup
  7172. (setq type :startup)
  7173. org-startup-options)
  7174. (link (append org-link-abbrev-alist-local
  7175. org-link-abbrev-alist))
  7176. (texp
  7177. (setq type :tex)
  7178. org-html-entities)
  7179. ((string-match "\\`\\*+[ \t]+\\'"
  7180. (buffer-substring (point-at-bol) beg))
  7181. (setq type :todo)
  7182. (mapcar 'list org-todo-keywords-1))
  7183. (searchhead
  7184. (setq type :searchhead)
  7185. (save-excursion
  7186. (goto-char (point-min))
  7187. (while (re-search-forward org-todo-line-regexp nil t)
  7188. (push (list
  7189. (org-make-org-heading-search-string
  7190. (match-string 3) t))
  7191. tbl)))
  7192. tbl)
  7193. (tag (setq type :tag beg beg1)
  7194. (or org-tag-alist (org-get-buffer-tags)))
  7195. (prop (setq type :prop beg beg1)
  7196. (mapcar 'list (org-buffer-property-keys nil t t)))
  7197. (t (progn
  7198. (call-interactively org-completion-fallback-command)
  7199. (throw 'exit nil)))))
  7200. (pattern (buffer-substring-no-properties beg end))
  7201. (completion (try-completion pattern table confirm)))
  7202. (cond ((eq completion t)
  7203. (if (not (assoc (upcase pattern) table))
  7204. (message "Already complete")
  7205. (if (and (equal type :opt)
  7206. (not (member (car (assoc (upcase pattern) table))
  7207. org-additional-option-like-keywords)))
  7208. (insert (substring (cdr (assoc (upcase pattern) table))
  7209. (length pattern)))
  7210. (if (memq type '(:tag :prop)) (insert ":")))))
  7211. ((null completion)
  7212. (message "Can't find completion for \"%s\"" pattern)
  7213. (ding))
  7214. ((not (string= pattern completion))
  7215. (delete-region beg end)
  7216. (if (string-match " +$" completion)
  7217. (setq completion (replace-match "" t t completion)))
  7218. (insert completion)
  7219. (if (get-buffer-window "*Completions*")
  7220. (delete-window (get-buffer-window "*Completions*")))
  7221. (if (assoc completion table)
  7222. (if (eq type :todo) (insert " ")
  7223. (if (memq type '(:tag :prop)) (insert ":"))))
  7224. (if (and (equal type :opt) (assoc completion table))
  7225. (message "%s" (substitute-command-keys
  7226. "Press \\[org-complete] again to insert example settings"))))
  7227. (t
  7228. (message "Making completion list...")
  7229. (let ((list (sort (all-completions pattern table confirm)
  7230. 'string<)))
  7231. (with-output-to-temp-buffer "*Completions*"
  7232. (condition-case nil
  7233. ;; Protection needed for XEmacs and emacs 21
  7234. (display-completion-list list pattern)
  7235. (error (display-completion-list list)))))
  7236. (message "Making completion list...%s" "done")))))))
  7237. ;;;; TODO, DEADLINE, Comments
  7238. (defun org-toggle-comment ()
  7239. "Change the COMMENT state of an entry."
  7240. (interactive)
  7241. (save-excursion
  7242. (org-back-to-heading)
  7243. (let (case-fold-search)
  7244. (if (looking-at (concat outline-regexp
  7245. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7246. (replace-match "" t t nil 1)
  7247. (if (looking-at outline-regexp)
  7248. (progn
  7249. (goto-char (match-end 0))
  7250. (insert org-comment-string " ")))))))
  7251. (defvar org-last-todo-state-is-todo nil
  7252. "This is non-nil when the last TODO state change led to a TODO state.
  7253. If the last change removed the TODO tag or switched to DONE, then
  7254. this is nil.")
  7255. (defvar org-setting-tags nil) ; dynamically skiped
  7256. (defun org-parse-local-options (string var)
  7257. "Parse STRING for startup setting relevant for variable VAR."
  7258. (let ((rtn (symbol-value var))
  7259. e opts)
  7260. (save-match-data
  7261. (if (or (not string) (not (string-match "\\S-" string)))
  7262. rtn
  7263. (setq opts (delq nil (mapcar (lambda (x)
  7264. (setq e (assoc x org-startup-options))
  7265. (if (eq (nth 1 e) var) e nil))
  7266. (org-split-string string "[ \t]+"))))
  7267. (if (not opts)
  7268. rtn
  7269. (setq rtn nil)
  7270. (while (setq e (pop opts))
  7271. (if (not (nth 3 e))
  7272. (setq rtn (nth 2 e))
  7273. (if (not (listp rtn)) (setq rtn nil))
  7274. (push (nth 2 e) rtn)))
  7275. rtn)))))
  7276. (defvar org-blocker-hook nil
  7277. "Hook for functions that are allowed to block a state change.
  7278. Each function gets as its single argument a property list, see
  7279. `org-trigger-hook' for more information about this list.
  7280. If any of the functions in this hook returns nil, the state change
  7281. is blocked.")
  7282. (defvar org-trigger-hook nil
  7283. "Hook for functions that are triggered by a state change.
  7284. Each function gets as its single argument a property list with at least
  7285. the following elements:
  7286. (:type type-of-change :position pos-at-entry-start
  7287. :from old-state :to new-state)
  7288. Depending on the type, more properties may be present.
  7289. This mechanism is currently implemented for:
  7290. TODO state changes
  7291. ------------------
  7292. :type todo-state-change
  7293. :from previous state (keyword as a string), or nil
  7294. :to new state (keyword as a string), or nil")
  7295. (defun org-todo (&optional arg)
  7296. "Change the TODO state of an item.
  7297. The state of an item is given by a keyword at the start of the heading,
  7298. like
  7299. *** TODO Write paper
  7300. *** DONE Call mom
  7301. The different keywords are specified in the variable `org-todo-keywords'.
  7302. By default the available states are \"TODO\" and \"DONE\".
  7303. So for this example: when the item starts with TODO, it is changed to DONE.
  7304. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7305. DONE are present, add TODO at the beginning of the heading.
  7306. With C-u prefix arg, use completion to determine the new state.
  7307. With numeric prefix arg, switch to that state.
  7308. For calling through lisp, arg is also interpreted in the following way:
  7309. 'none -> empty state
  7310. \"\"(empty string) -> switch to empty state
  7311. 'done -> switch to DONE
  7312. 'nextset -> switch to the next set of keywords
  7313. 'previousset -> switch to the previous set of keywords
  7314. \"WAITING\" -> switch to the specified keyword, but only if it
  7315. really is a member of `org-todo-keywords'."
  7316. (interactive "P")
  7317. (save-excursion
  7318. (catch 'exit
  7319. (org-back-to-heading)
  7320. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7321. (or (looking-at (concat " +" org-todo-regexp " *"))
  7322. (looking-at " *"))
  7323. (let* ((match-data (match-data))
  7324. (startpos (point-at-bol))
  7325. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7326. (org-log-done org-log-done)
  7327. (org-log-repeat org-log-repeat)
  7328. (org-todo-log-states org-todo-log-states)
  7329. (this (match-string 1))
  7330. (hl-pos (match-beginning 0))
  7331. (head (org-get-todo-sequence-head this))
  7332. (ass (assoc head org-todo-kwd-alist))
  7333. (interpret (nth 1 ass))
  7334. (done-word (nth 3 ass))
  7335. (final-done-word (nth 4 ass))
  7336. (last-state (or this ""))
  7337. (completion-ignore-case t)
  7338. (member (member this org-todo-keywords-1))
  7339. (tail (cdr member))
  7340. (state (cond
  7341. ((and org-todo-key-trigger
  7342. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7343. (and (not arg) org-use-fast-todo-selection
  7344. (not (eq org-use-fast-todo-selection 'prefix)))))
  7345. ;; Use fast selection
  7346. (org-fast-todo-selection))
  7347. ((and (equal arg '(4))
  7348. (or (not org-use-fast-todo-selection)
  7349. (not org-todo-key-trigger)))
  7350. ;; Read a state with completion
  7351. (completing-read "State: " (mapcar (lambda(x) (list x))
  7352. org-todo-keywords-1)
  7353. nil t))
  7354. ((eq arg 'right)
  7355. (if this
  7356. (if tail (car tail) nil)
  7357. (car org-todo-keywords-1)))
  7358. ((eq arg 'left)
  7359. (if (equal member org-todo-keywords-1)
  7360. nil
  7361. (if this
  7362. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7363. org-todo-keywords-1)
  7364. (org-last org-todo-keywords-1))))
  7365. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7366. (setq arg nil))) ; hack to fall back to cycling
  7367. (arg
  7368. ;; user or caller requests a specific state
  7369. (cond
  7370. ((equal arg "") nil)
  7371. ((eq arg 'none) nil)
  7372. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7373. ((eq arg 'nextset)
  7374. (or (car (cdr (member head org-todo-heads)))
  7375. (car org-todo-heads)))
  7376. ((eq arg 'previousset)
  7377. (let ((org-todo-heads (reverse org-todo-heads)))
  7378. (or (car (cdr (member head org-todo-heads)))
  7379. (car org-todo-heads))))
  7380. ((car (member arg org-todo-keywords-1)))
  7381. ((nth (1- (prefix-numeric-value arg))
  7382. org-todo-keywords-1))))
  7383. ((null member) (or head (car org-todo-keywords-1)))
  7384. ((equal this final-done-word) nil) ;; -> make empty
  7385. ((null tail) nil) ;; -> first entry
  7386. ((eq interpret 'sequence)
  7387. (car tail))
  7388. ((memq interpret '(type priority))
  7389. (if (eq this-command last-command)
  7390. (car tail)
  7391. (if (> (length tail) 0)
  7392. (or done-word (car org-done-keywords))
  7393. nil)))
  7394. (t nil)))
  7395. (next (if state (concat " " state " ") " "))
  7396. (change-plist (list :type 'todo-state-change :from this :to state
  7397. :position startpos))
  7398. dolog now-done-p)
  7399. (when org-blocker-hook
  7400. (unless (save-excursion
  7401. (save-match-data
  7402. (run-hook-with-args-until-failure
  7403. 'org-blocker-hook change-plist)))
  7404. (if (interactive-p)
  7405. (error "TODO state change from %s to %s blocked" this state)
  7406. ;; fail silently
  7407. (message "TODO state change from %s to %s blocked" this state)
  7408. (throw 'exit nil))))
  7409. (store-match-data match-data)
  7410. (replace-match next t t)
  7411. (unless (pos-visible-in-window-p hl-pos)
  7412. (message "TODO state changed to %s" (org-trim next)))
  7413. (unless head
  7414. (setq head (org-get-todo-sequence-head state)
  7415. ass (assoc head org-todo-kwd-alist)
  7416. interpret (nth 1 ass)
  7417. done-word (nth 3 ass)
  7418. final-done-word (nth 4 ass)))
  7419. (when (memq arg '(nextset previousset))
  7420. (message "Keyword-Set %d/%d: %s"
  7421. (- (length org-todo-sets) -1
  7422. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7423. (length org-todo-sets)
  7424. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7425. (setq org-last-todo-state-is-todo
  7426. (not (member state org-done-keywords)))
  7427. (setq now-done-p (and (member state org-done-keywords)
  7428. (not (member this org-done-keywords))))
  7429. (and logging (org-local-logging logging))
  7430. (when (and (or org-todo-log-states org-log-done)
  7431. (not (memq arg '(nextset previousset))))
  7432. ;; we need to look at recording a time and note
  7433. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7434. (nth 2 (assoc this org-todo-log-states))))
  7435. (when (and state
  7436. (member state org-not-done-keywords)
  7437. (not (member this org-not-done-keywords)))
  7438. ;; This is now a todo state and was not one before
  7439. ;; If there was a CLOSED time stamp, get rid of it.
  7440. (org-add-planning-info nil nil 'closed))
  7441. (when (and now-done-p org-log-done)
  7442. ;; It is now done, and it was not done before
  7443. (org-add-planning-info 'closed (org-current-time))
  7444. (if (and (not dolog) (eq 'note org-log-done))
  7445. (org-add-log-setup 'done state 'findpos 'note)))
  7446. (when (and state dolog)
  7447. ;; This is a non-nil state, and we need to log it
  7448. (org-add-log-setup 'state state 'findpos dolog)))
  7449. ;; Fixup tag positioning
  7450. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7451. (when org-provide-todo-statistics
  7452. (org-update-parent-todo-statistics))
  7453. (run-hooks 'org-after-todo-state-change-hook)
  7454. (if (and arg (not (member state org-done-keywords)))
  7455. (setq head (org-get-todo-sequence-head state)))
  7456. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7457. ;; Do we need to trigger a repeat?
  7458. (when now-done-p (org-auto-repeat-maybe state))
  7459. ;; Fixup cursor location if close to the keyword
  7460. (if (and (outline-on-heading-p)
  7461. (not (bolp))
  7462. (save-excursion (beginning-of-line 1)
  7463. (looking-at org-todo-line-regexp))
  7464. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7465. (progn
  7466. (goto-char (or (match-end 2) (match-end 1)))
  7467. (just-one-space)))
  7468. (when org-trigger-hook
  7469. (save-excursion
  7470. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7471. (defun org-update-parent-todo-statistics ()
  7472. "Update any statistics cookie in the parent of the current headline."
  7473. (interactive)
  7474. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7475. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7476. (catch 'exit
  7477. (save-excursion
  7478. (setq level (org-up-heading-safe))
  7479. (unless (and level
  7480. (re-search-forward box-re (point-at-eol) t))
  7481. (throw 'exit nil))
  7482. (setq is-percent (match-end 2))
  7483. (save-match-data
  7484. (unless (outline-next-heading) (throw 'exit nil))
  7485. (while (looking-at org-todo-line-regexp)
  7486. (setq kwd (match-string 2))
  7487. (and kwd (setq cnt-all (1+ cnt-all)))
  7488. (and (member kwd org-done-keywords)
  7489. (setq cnt-done (1+ cnt-done)))
  7490. (condition-case nil
  7491. (outline-forward-same-level 1)
  7492. (error (end-of-line 1)))))
  7493. (replace-match
  7494. (if is-percent
  7495. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7496. (format "[%d/%d]" cnt-done cnt-all)))
  7497. (run-hook-with-args 'org-after-todo-statistics-hook
  7498. cnt-done (- cnt-all cnt-done))))))
  7499. (defvar org-after-todo-statistics-hook nil
  7500. "Hook that is called after a TODO statistics cookie has been updated.
  7501. Each function is called with two arguments: the number of not-done entries
  7502. and the number of done entries.
  7503. For example, the following function, when added to this hook, will switch
  7504. an entry to DONE when all children are done, and back to TODO when new
  7505. entries are set to a TODO status. Note that this hook is only called
  7506. when there is a statistics cookie in the headline!
  7507. (defun org-summary-todo (n-done n-not-done)
  7508. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7509. (let (org-log-done org-log-states) ; turn off logging
  7510. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7511. ")
  7512. (defun org-local-logging (value)
  7513. "Get logging settings from a property VALUE."
  7514. (let* (words w a)
  7515. ;; directly set the variables, they are already local.
  7516. (setq org-log-done nil
  7517. org-log-repeat nil
  7518. org-todo-log-states nil)
  7519. (setq words (org-split-string value))
  7520. (while (setq w (pop words))
  7521. (cond
  7522. ((setq a (assoc w org-startup-options))
  7523. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7524. (set (nth 1 a) (nth 2 a))))
  7525. ((setq a (org-extract-log-state-settings w))
  7526. (and (member (car a) org-todo-keywords-1)
  7527. (push a org-todo-log-states)))))))
  7528. (defun org-get-todo-sequence-head (kwd)
  7529. "Return the head of the TODO sequence to which KWD belongs.
  7530. If KWD is not set, check if there is a text property remembering the
  7531. right sequence."
  7532. (let (p)
  7533. (cond
  7534. ((not kwd)
  7535. (or (get-text-property (point-at-bol) 'org-todo-head)
  7536. (progn
  7537. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7538. nil (point-at-eol)))
  7539. (get-text-property p 'org-todo-head))))
  7540. ((not (member kwd org-todo-keywords-1))
  7541. (car org-todo-keywords-1))
  7542. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7543. (defun org-fast-todo-selection ()
  7544. "Fast TODO keyword selection with single keys.
  7545. Returns the new TODO keyword, or nil if no state change should occur."
  7546. (let* ((fulltable org-todo-key-alist)
  7547. (done-keywords org-done-keywords) ;; needed for the faces.
  7548. (maxlen (apply 'max (mapcar
  7549. (lambda (x)
  7550. (if (stringp (car x)) (string-width (car x)) 0))
  7551. fulltable)))
  7552. (expert nil)
  7553. (fwidth (+ maxlen 3 1 3))
  7554. (ncol (/ (- (window-width) 4) fwidth))
  7555. tg cnt e c tbl
  7556. groups ingroup)
  7557. (save-window-excursion
  7558. (if expert
  7559. (set-buffer (get-buffer-create " *Org todo*"))
  7560. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7561. (erase-buffer)
  7562. (org-set-local 'org-done-keywords done-keywords)
  7563. (setq tbl fulltable cnt 0)
  7564. (while (setq e (pop tbl))
  7565. (cond
  7566. ((equal e '(:startgroup))
  7567. (push '() groups) (setq ingroup t)
  7568. (when (not (= cnt 0))
  7569. (setq cnt 0)
  7570. (insert "\n"))
  7571. (insert "{ "))
  7572. ((equal e '(:endgroup))
  7573. (setq ingroup nil cnt 0)
  7574. (insert "}\n"))
  7575. (t
  7576. (setq tg (car e) c (cdr e))
  7577. (if ingroup (push tg (car groups)))
  7578. (setq tg (org-add-props tg nil 'face
  7579. (org-get-todo-face tg)))
  7580. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7581. (insert "[" c "] " tg (make-string
  7582. (- fwidth 4 (length tg)) ?\ ))
  7583. (when (= (setq cnt (1+ cnt)) ncol)
  7584. (insert "\n")
  7585. (if ingroup (insert " "))
  7586. (setq cnt 0)))))
  7587. (insert "\n")
  7588. (goto-char (point-min))
  7589. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7590. (fit-window-to-buffer))
  7591. (message "[a-z..]:Set [SPC]:clear")
  7592. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7593. (cond
  7594. ((or (= c ?\C-g)
  7595. (and (= c ?q) (not (rassoc c fulltable))))
  7596. (setq quit-flag t))
  7597. ((= c ?\ ) nil)
  7598. ((setq e (rassoc c fulltable) tg (car e))
  7599. tg)
  7600. (t (setq quit-flag t))))))
  7601. (defun org-entry-is-todo-p ()
  7602. (member (org-get-todo-state) org-not-done-keywords))
  7603. (defun org-entry-is-done-p ()
  7604. (member (org-get-todo-state) org-done-keywords))
  7605. (defun org-get-todo-state ()
  7606. (save-excursion
  7607. (org-back-to-heading t)
  7608. (and (looking-at org-todo-line-regexp)
  7609. (match-end 2)
  7610. (match-string 2))))
  7611. (defun org-at-date-range-p (&optional inactive-ok)
  7612. "Is the cursor inside a date range?"
  7613. (interactive)
  7614. (save-excursion
  7615. (catch 'exit
  7616. (let ((pos (point)))
  7617. (skip-chars-backward "^[<\r\n")
  7618. (skip-chars-backward "<[")
  7619. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7620. (>= (match-end 0) pos)
  7621. (throw 'exit t))
  7622. (skip-chars-backward "^<[\r\n")
  7623. (skip-chars-backward "<[")
  7624. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7625. (>= (match-end 0) pos)
  7626. (throw 'exit t)))
  7627. nil)))
  7628. (defun org-get-repeat ()
  7629. "Check if tere is a deadline/schedule with repeater in this entry."
  7630. (save-match-data
  7631. (save-excursion
  7632. (org-back-to-heading t)
  7633. (if (re-search-forward
  7634. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7635. (match-string 1)))))
  7636. (defvar org-last-changed-timestamp)
  7637. (defvar org-log-post-message)
  7638. (defvar org-log-note-purpose)
  7639. (defvar org-log-note-how)
  7640. (defun org-auto-repeat-maybe (done-word)
  7641. "Check if the current headline contains a repeated deadline/schedule.
  7642. If yes, set TODO state back to what it was and change the base date
  7643. of repeating deadline/scheduled time stamps to new date.
  7644. This function is run automatically after each state change to a DONE state."
  7645. ;; last-state is dynamically scoped into this function
  7646. (let* ((repeat (org-get-repeat))
  7647. (aa (assoc last-state org-todo-kwd-alist))
  7648. (interpret (nth 1 aa))
  7649. (head (nth 2 aa))
  7650. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7651. (msg "Entry repeats: ")
  7652. (org-log-done nil)
  7653. (org-todo-log-states nil)
  7654. (nshiftmax 10) (nshift 0)
  7655. re type n what ts mb0 time)
  7656. (when repeat
  7657. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7658. (org-todo (if (eq interpret 'type) last-state head))
  7659. (when org-log-repeat
  7660. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7661. (memq 'org-add-log-note post-command-hook))
  7662. ;; OK, we are already setup for some record
  7663. (if (eq org-log-repeat 'note)
  7664. ;; make sure we take a note, not only a time stamp
  7665. (setq org-log-note-how 'note))
  7666. ;; Set up for taking a record
  7667. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7668. 'findpos org-log-repeat)))
  7669. (org-back-to-heading t)
  7670. (org-add-planning-info nil nil 'closed)
  7671. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7672. org-deadline-time-regexp "\\)\\|\\("
  7673. org-ts-regexp "\\)"))
  7674. (while (re-search-forward
  7675. re (save-excursion (outline-next-heading) (point)) t)
  7676. (setq type (if (match-end 1) org-scheduled-string
  7677. (if (match-end 3) org-deadline-string "Plain:"))
  7678. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7679. mb0 (match-beginning 0))
  7680. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7681. (setq n (string-to-number (match-string 2 ts))
  7682. what (match-string 3 ts))
  7683. (if (equal what "w") (setq n (* n 7) what "d"))
  7684. ;; Preparation, see if we need to modify the start date for the change
  7685. (when (match-end 1)
  7686. (setq time (save-match-data (org-time-string-to-time ts)))
  7687. (cond
  7688. ((equal (match-string 1 ts) ".")
  7689. ;; Shift starting date to today
  7690. (org-timestamp-change
  7691. (- (time-to-days (current-time)) (time-to-days time))
  7692. 'day))
  7693. ((equal (match-string 1 ts) "+")
  7694. (while (or (= nshift 0)
  7695. (<= (time-to-days time) (time-to-days (current-time))))
  7696. (when (= (incf nshift) nshiftmax)
  7697. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7698. (error "Abort")))
  7699. (org-timestamp-change n (cdr (assoc what whata)))
  7700. (org-at-timestamp-p t)
  7701. (setq ts (match-string 1))
  7702. (setq time (save-match-data (org-time-string-to-time ts))))
  7703. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7704. ;; rematch, so that we have everything in place for the real shift
  7705. (org-at-timestamp-p t)
  7706. (setq ts (match-string 1))
  7707. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7708. (org-timestamp-change n (cdr (assoc what whata)))
  7709. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7710. (setq org-log-post-message msg)
  7711. (message "%s" msg))))
  7712. (defun org-show-todo-tree (arg)
  7713. "Make a compact tree which shows all headlines marked with TODO.
  7714. The tree will show the lines where the regexp matches, and all higher
  7715. headlines above the match.
  7716. With a \\[universal-argument] prefix, also show the DONE entries.
  7717. With a numeric prefix N, construct a sparse tree for the Nth element
  7718. of `org-todo-keywords-1'."
  7719. (interactive "P")
  7720. (let ((case-fold-search nil)
  7721. (kwd-re
  7722. (cond ((null arg) org-not-done-regexp)
  7723. ((equal arg '(4))
  7724. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7725. (mapcar 'list org-todo-keywords-1))))
  7726. (concat "\\("
  7727. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7728. "\\)\\>")))
  7729. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7730. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7731. org-todo-keywords-1)))
  7732. (t (error "Invalid prefix argument: %s" arg)))))
  7733. (message "%d TODO entries found"
  7734. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7735. (defun org-deadline (&optional remove)
  7736. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7737. With argument REMOVE, remove any deadline from the item."
  7738. (interactive "P")
  7739. (if remove
  7740. (progn
  7741. (org-remove-timestamp-with-keyword org-deadline-string)
  7742. (message "Item no longer has a deadline."))
  7743. (org-add-planning-info 'deadline nil 'closed)))
  7744. (defun org-schedule (&optional remove)
  7745. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7746. With argument REMOVE, remove any scheduling date from the item."
  7747. (interactive "P")
  7748. (if remove
  7749. (progn
  7750. (org-remove-timestamp-with-keyword org-scheduled-string)
  7751. (message "Item is no longer scheduled."))
  7752. (org-add-planning-info 'scheduled nil 'closed)))
  7753. (defun org-remove-timestamp-with-keyword (keyword)
  7754. "Remove all time stamps with KEYWORD in the current entry."
  7755. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7756. beg)
  7757. (save-excursion
  7758. (org-back-to-heading t)
  7759. (setq beg (point))
  7760. (org-end-of-subtree t t)
  7761. (while (re-search-backward re beg t)
  7762. (replace-match "")
  7763. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7764. (equal (char-before) ?\ ))
  7765. (backward-delete-char 1)
  7766. (if (string-match "^[ \t]*$" (buffer-substring
  7767. (point-at-bol) (point-at-eol)))
  7768. (delete-region (point-at-bol)
  7769. (min (point-max) (1+ (point-at-eol))))))))))
  7770. (defun org-add-planning-info (what &optional time &rest remove)
  7771. "Insert new timestamp with keyword in the line directly after the headline.
  7772. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7773. If non is given, the user is prompted for a date.
  7774. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7775. be removed."
  7776. (interactive)
  7777. (let (org-time-was-given org-end-time-was-given ts
  7778. end default-time default-input)
  7779. (when (and (not time) (memq what '(scheduled deadline)))
  7780. ;; Try to get a default date/time from existing timestamp
  7781. (save-excursion
  7782. (org-back-to-heading t)
  7783. (setq end (save-excursion (outline-next-heading) (point)))
  7784. (when (re-search-forward (if (eq what 'scheduled)
  7785. org-scheduled-time-regexp
  7786. org-deadline-time-regexp)
  7787. end t)
  7788. (setq ts (match-string 1)
  7789. default-time
  7790. (apply 'encode-time (org-parse-time-string ts))
  7791. default-input (and ts (org-get-compact-tod ts))))))
  7792. (when what
  7793. ;; If necessary, get the time from the user
  7794. (setq time (or time (org-read-date nil 'to-time nil nil
  7795. default-time default-input))))
  7796. (when (and org-insert-labeled-timestamps-at-point
  7797. (member what '(scheduled deadline)))
  7798. (insert
  7799. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7800. (org-insert-time-stamp time org-time-was-given
  7801. nil nil nil (list org-end-time-was-given))
  7802. (setq what nil))
  7803. (save-excursion
  7804. (save-restriction
  7805. (let (col list elt ts buffer-invisibility-spec)
  7806. (org-back-to-heading t)
  7807. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7808. (goto-char (match-end 1))
  7809. (setq col (current-column))
  7810. (goto-char (match-end 0))
  7811. (if (eobp) (insert "\n") (forward-char 1))
  7812. (if (and (not (looking-at outline-regexp))
  7813. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7814. "[^\r\n]*"))
  7815. (not (equal (match-string 1) org-clock-string)))
  7816. (narrow-to-region (match-beginning 0) (match-end 0))
  7817. (insert-before-markers "\n")
  7818. (backward-char 1)
  7819. (narrow-to-region (point) (point))
  7820. (org-indent-to-column col))
  7821. ;; Check if we have to remove something.
  7822. (setq list (cons what remove))
  7823. (while list
  7824. (setq elt (pop list))
  7825. (goto-char (point-min))
  7826. (when (or (and (eq elt 'scheduled)
  7827. (re-search-forward org-scheduled-time-regexp nil t))
  7828. (and (eq elt 'deadline)
  7829. (re-search-forward org-deadline-time-regexp nil t))
  7830. (and (eq elt 'closed)
  7831. (re-search-forward org-closed-time-regexp nil t)))
  7832. (replace-match "")
  7833. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7834. (if (looking-at " +") (replace-match ""))))
  7835. (goto-char (point-max))
  7836. (when what
  7837. (insert
  7838. (if (not (equal (char-before) ?\ )) " " "")
  7839. (cond ((eq what 'scheduled) org-scheduled-string)
  7840. ((eq what 'deadline) org-deadline-string)
  7841. ((eq what 'closed) org-closed-string))
  7842. " ")
  7843. (setq ts (org-insert-time-stamp
  7844. time
  7845. (or org-time-was-given
  7846. (and (eq what 'closed) org-log-done-with-time))
  7847. (eq what 'closed)
  7848. nil nil (list org-end-time-was-given)))
  7849. (end-of-line 1))
  7850. (goto-char (point-min))
  7851. (widen)
  7852. (if (and (looking-at "[ \t]+\n")
  7853. (equal (char-before) ?\n))
  7854. (delete-region (1- (point)) (point-at-eol)))
  7855. ts)))))
  7856. (defvar org-log-note-marker (make-marker))
  7857. (defvar org-log-note-purpose nil)
  7858. (defvar org-log-note-state nil)
  7859. (defvar org-log-note-how nil)
  7860. (defvar org-log-note-window-configuration nil)
  7861. (defvar org-log-note-return-to (make-marker))
  7862. (defvar org-log-post-message nil
  7863. "Message to be displayed after a log note has been stored.
  7864. The auto-repeater uses this.")
  7865. (defun org-add-note ()
  7866. "Add a note to the current entry.
  7867. This is done in the same way as adding a state change note."
  7868. (interactive)
  7869. (org-add-log-setup 'note nil t nil))
  7870. (defun org-add-log-setup (&optional purpose state findpos how)
  7871. "Set up the post command hook to take a note.
  7872. If this is about to TODO state change, the new state is expected in STATE.
  7873. When FINDPOS is non-nil, find the correct position for the note in
  7874. the current entry. If not, assume that it can be inserted at point."
  7875. (save-excursion
  7876. (when findpos
  7877. (org-back-to-heading t)
  7878. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7879. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7880. "[^\r\n]*\\)?"))
  7881. (goto-char (match-end 0))
  7882. (unless org-log-states-order-reversed
  7883. (and (= (char-after) ?\n) (forward-char 1))
  7884. (org-skip-over-state-notes)
  7885. (skip-chars-backward " \t\n\r")))
  7886. (move-marker org-log-note-marker (point))
  7887. (setq org-log-note-purpose purpose
  7888. org-log-note-state state
  7889. org-log-note-how how)
  7890. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7891. (defun org-skip-over-state-notes ()
  7892. "Skip past the list of State notes in an entry."
  7893. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7894. (while (looking-at "[ \t]*- State")
  7895. (condition-case nil
  7896. (org-next-item)
  7897. (error (org-end-of-item)))))
  7898. (defun org-add-log-note (&optional purpose)
  7899. "Pop up a window for taking a note, and add this note later at point."
  7900. (remove-hook 'post-command-hook 'org-add-log-note)
  7901. (setq org-log-note-window-configuration (current-window-configuration))
  7902. (delete-other-windows)
  7903. (move-marker org-log-note-return-to (point))
  7904. (switch-to-buffer (marker-buffer org-log-note-marker))
  7905. (goto-char org-log-note-marker)
  7906. (org-switch-to-buffer-other-window "*Org Note*")
  7907. (erase-buffer)
  7908. (if (memq org-log-note-how '(time state))
  7909. (org-store-log-note)
  7910. (let ((org-inhibit-startup t)) (org-mode))
  7911. (insert (format "# Insert note for %s.
  7912. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7913. (cond
  7914. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7915. ((eq org-log-note-purpose 'done) "closed todo item")
  7916. ((eq org-log-note-purpose 'state)
  7917. (format "state change to \"%s\"" org-log-note-state))
  7918. ((eq org-log-note-purpose 'note)
  7919. "this entry")
  7920. (t (error "This should not happen")))))
  7921. (org-set-local 'org-finish-function 'org-store-log-note)))
  7922. (defvar org-note-abort nil) ; dynamically scoped
  7923. (defun org-store-log-note ()
  7924. "Finish taking a log note, and insert it to where it belongs."
  7925. (let ((txt (buffer-string))
  7926. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7927. lines ind)
  7928. (kill-buffer (current-buffer))
  7929. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7930. (setq txt (replace-match "" t t txt)))
  7931. (if (string-match "\\s-+\\'" txt)
  7932. (setq txt (replace-match "" t t txt)))
  7933. (setq lines (org-split-string txt "\n"))
  7934. (when (and note (string-match "\\S-" note))
  7935. (setq note
  7936. (org-replace-escapes
  7937. note
  7938. (list (cons "%u" (user-login-name))
  7939. (cons "%U" user-full-name)
  7940. (cons "%t" (format-time-string
  7941. (org-time-stamp-format 'long 'inactive)
  7942. (current-time)))
  7943. (cons "%s" (if org-log-note-state
  7944. (concat "\"" org-log-note-state "\"")
  7945. "")))))
  7946. (if lines (setq note (concat note " \\\\")))
  7947. (push note lines))
  7948. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7949. (when lines
  7950. (save-excursion
  7951. (set-buffer (marker-buffer org-log-note-marker))
  7952. (save-excursion
  7953. (goto-char org-log-note-marker)
  7954. (move-marker org-log-note-marker nil)
  7955. (end-of-line 1)
  7956. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7957. (indent-relative nil)
  7958. (insert "- " (pop lines))
  7959. (org-indent-line-function)
  7960. (beginning-of-line 1)
  7961. (looking-at "[ \t]*")
  7962. (setq ind (concat (match-string 0) " "))
  7963. (end-of-line 1)
  7964. (while lines (insert "\n" ind (pop lines)))))))
  7965. (set-window-configuration org-log-note-window-configuration)
  7966. (with-current-buffer (marker-buffer org-log-note-return-to)
  7967. (goto-char org-log-note-return-to))
  7968. (move-marker org-log-note-return-to nil)
  7969. (and org-log-post-message (message "%s" org-log-post-message)))
  7970. (defun org-sparse-tree (&optional arg)
  7971. "Create a sparse tree, prompt for the details.
  7972. This command can create sparse trees. You first need to select the type
  7973. of match used to create the tree:
  7974. t Show entries with a specific TODO keyword.
  7975. T Show entries selected by a tags match.
  7976. p Enter a property name and its value (both with completion on existing
  7977. names/values) and show entries with that property.
  7978. r Show entries matching a regular expression
  7979. d Show deadlines due within `org-deadline-warning-days'."
  7980. (interactive "P")
  7981. (let (ans kwd value)
  7982. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7983. (setq ans (read-char-exclusive))
  7984. (cond
  7985. ((equal ans ?d)
  7986. (call-interactively 'org-check-deadlines))
  7987. ((equal ans ?b)
  7988. (call-interactively 'org-check-before-date))
  7989. ((equal ans ?t)
  7990. (org-show-todo-tree '(4)))
  7991. ((equal ans ?T)
  7992. (call-interactively 'org-tags-sparse-tree))
  7993. ((member ans '(?p ?P))
  7994. (setq kwd (completing-read "Property: "
  7995. (mapcar 'list (org-buffer-property-keys))))
  7996. (setq value (completing-read "Value: "
  7997. (mapcar 'list (org-property-values kwd))))
  7998. (unless (string-match "\\`{.*}\\'" value)
  7999. (setq value (concat "\"" value "\"")))
  8000. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8001. ((member ans '(?r ?R ?/))
  8002. (call-interactively 'org-occur))
  8003. (t (error "No such sparse tree command \"%c\"" ans)))))
  8004. (defvar org-occur-highlights nil
  8005. "List of overlays used for occur matches.")
  8006. (make-variable-buffer-local 'org-occur-highlights)
  8007. (defvar org-occur-parameters nil
  8008. "Parameters of the active org-occur calls.
  8009. This is a list, each call to org-occur pushes as cons cell,
  8010. containing the regular expression and the callback, onto the list.
  8011. The list can contain several entries if `org-occur' has been called
  8012. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8013. will only contain one set of parameters. When the highlights are
  8014. removed (for example with `C-c C-c', or with the next edit (depending
  8015. on `org-remove-highlights-with-change'), this variable is emptied
  8016. as well.")
  8017. (make-variable-buffer-local 'org-occur-parameters)
  8018. (defun org-occur (regexp &optional keep-previous callback)
  8019. "Make a compact tree which shows all matches of REGEXP.
  8020. The tree will show the lines where the regexp matches, and all higher
  8021. headlines above the match. It will also show the heading after the match,
  8022. to make sure editing the matching entry is easy.
  8023. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8024. call to `org-occur' will be kept, to allow stacking of calls to this
  8025. command.
  8026. If CALLBACK is non-nil, it is a function which is called to confirm
  8027. that the match should indeed be shown."
  8028. (interactive "sRegexp: \nP")
  8029. (unless keep-previous
  8030. (org-remove-occur-highlights nil nil t))
  8031. (push (cons regexp callback) org-occur-parameters)
  8032. (let ((cnt 0))
  8033. (save-excursion
  8034. (goto-char (point-min))
  8035. (if (or (not keep-previous) ; do not want to keep
  8036. (not org-occur-highlights)) ; no previous matches
  8037. ;; hide everything
  8038. (org-overview))
  8039. (while (re-search-forward regexp nil t)
  8040. (when (or (not callback)
  8041. (save-match-data (funcall callback)))
  8042. (setq cnt (1+ cnt))
  8043. (when org-highlight-sparse-tree-matches
  8044. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8045. (org-show-context 'occur-tree))))
  8046. (when org-remove-highlights-with-change
  8047. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8048. nil 'local))
  8049. (unless org-sparse-tree-open-archived-trees
  8050. (org-hide-archived-subtrees (point-min) (point-max)))
  8051. (run-hooks 'org-occur-hook)
  8052. (if (interactive-p)
  8053. (message "%d match(es) for regexp %s" cnt regexp))
  8054. cnt))
  8055. (defun org-show-context (&optional key)
  8056. "Make sure point and context and visible.
  8057. How much context is shown depends upon the variables
  8058. `org-show-hierarchy-above', `org-show-following-heading'. and
  8059. `org-show-siblings'."
  8060. (let ((heading-p (org-on-heading-p t))
  8061. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8062. (following-p (org-get-alist-option org-show-following-heading key))
  8063. (entry-p (org-get-alist-option org-show-entry-below key))
  8064. (siblings-p (org-get-alist-option org-show-siblings key)))
  8065. (catch 'exit
  8066. ;; Show heading or entry text
  8067. (if (and heading-p (not entry-p))
  8068. (org-flag-heading nil) ; only show the heading
  8069. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8070. (org-show-hidden-entry))) ; show entire entry
  8071. (when following-p
  8072. ;; Show next sibling, or heading below text
  8073. (save-excursion
  8074. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8075. (org-flag-heading nil))))
  8076. (when siblings-p (org-show-siblings))
  8077. (when hierarchy-p
  8078. ;; show all higher headings, possibly with siblings
  8079. (save-excursion
  8080. (while (and (condition-case nil
  8081. (progn (org-up-heading-all 1) t)
  8082. (error nil))
  8083. (not (bobp)))
  8084. (org-flag-heading nil)
  8085. (when siblings-p (org-show-siblings))))))))
  8086. (defun org-reveal (&optional siblings)
  8087. "Show current entry, hierarchy above it, and the following headline.
  8088. This can be used to show a consistent set of context around locations
  8089. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8090. not t for the search context.
  8091. With optional argument SIBLINGS, on each level of the hierarchy all
  8092. siblings are shown. This repairs the tree structure to what it would
  8093. look like when opened with hierarchical calls to `org-cycle'."
  8094. (interactive "P")
  8095. (let ((org-show-hierarchy-above t)
  8096. (org-show-following-heading t)
  8097. (org-show-siblings (if siblings t org-show-siblings)))
  8098. (org-show-context nil)))
  8099. (defun org-highlight-new-match (beg end)
  8100. "Highlight from BEG to END and mark the highlight is an occur headline."
  8101. (let ((ov (org-make-overlay beg end)))
  8102. (org-overlay-put ov 'face 'secondary-selection)
  8103. (push ov org-occur-highlights)))
  8104. (defun org-remove-occur-highlights (&optional beg end noremove)
  8105. "Remove the occur highlights from the buffer.
  8106. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8107. from the `before-change-functions' in the current buffer."
  8108. (interactive)
  8109. (unless org-inhibit-highlight-removal
  8110. (mapc 'org-delete-overlay org-occur-highlights)
  8111. (setq org-occur-highlights nil)
  8112. (setq org-occur-parameters nil)
  8113. (unless noremove
  8114. (remove-hook 'before-change-functions
  8115. 'org-remove-occur-highlights 'local))))
  8116. ;;;; Priorities
  8117. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8118. "Regular expression matching the priority indicator.")
  8119. (defvar org-remove-priority-next-time nil)
  8120. (defun org-priority-up ()
  8121. "Increase the priority of the current item."
  8122. (interactive)
  8123. (org-priority 'up))
  8124. (defun org-priority-down ()
  8125. "Decrease the priority of the current item."
  8126. (interactive)
  8127. (org-priority 'down))
  8128. (defun org-priority (&optional action)
  8129. "Change the priority of an item by ARG.
  8130. ACTION can be `set', `up', `down', or a character."
  8131. (interactive)
  8132. (setq action (or action 'set))
  8133. (let (current new news have remove)
  8134. (save-excursion
  8135. (org-back-to-heading)
  8136. (if (looking-at org-priority-regexp)
  8137. (setq current (string-to-char (match-string 2))
  8138. have t)
  8139. (setq current org-default-priority))
  8140. (cond
  8141. ((or (eq action 'set)
  8142. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8143. (if (not (eq action 'set))
  8144. (setq new action)
  8145. (message "Priority %c-%c, SPC to remove: "
  8146. org-highest-priority org-lowest-priority)
  8147. (setq new (read-char-exclusive)))
  8148. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8149. (= (upcase org-lowest-priority) org-lowest-priority))
  8150. (setq new (upcase new)))
  8151. (cond ((equal new ?\ ) (setq remove t))
  8152. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8153. (error "Priority must be between `%c' and `%c'"
  8154. org-highest-priority org-lowest-priority))))
  8155. ((eq action 'up)
  8156. (if (and (not have) (eq last-command this-command))
  8157. (setq new org-lowest-priority)
  8158. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8159. org-default-priority (1- current)))))
  8160. ((eq action 'down)
  8161. (if (and (not have) (eq last-command this-command))
  8162. (setq new org-highest-priority)
  8163. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8164. org-default-priority (1+ current)))))
  8165. (t (error "Invalid action")))
  8166. (if (or (< (upcase new) org-highest-priority)
  8167. (> (upcase new) org-lowest-priority))
  8168. (setq remove t))
  8169. (setq news (format "%c" new))
  8170. (if have
  8171. (if remove
  8172. (replace-match "" t t nil 1)
  8173. (replace-match news t t nil 2))
  8174. (if remove
  8175. (error "No priority cookie found in line")
  8176. (looking-at org-todo-line-regexp)
  8177. (if (match-end 2)
  8178. (progn
  8179. (goto-char (match-end 2))
  8180. (insert " [#" news "]"))
  8181. (goto-char (match-beginning 3))
  8182. (insert "[#" news "] ")))))
  8183. (org-preserve-lc (org-set-tags nil 'align))
  8184. (if remove
  8185. (message "Priority removed")
  8186. (message "Priority of current item set to %s" news))))
  8187. (defun org-get-priority (s)
  8188. "Find priority cookie and return priority."
  8189. (save-match-data
  8190. (if (not (string-match org-priority-regexp s))
  8191. (* 1000 (- org-lowest-priority org-default-priority))
  8192. (* 1000 (- org-lowest-priority
  8193. (string-to-char (match-string 2 s)))))))
  8194. ;;;; Tags
  8195. (defun org-scan-tags (action matcher &optional todo-only)
  8196. "Scan headline tags with inheritance and produce output ACTION.
  8197. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8198. evaluated, testing if a given set of tags qualifies a headline for
  8199. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8200. are included in the output."
  8201. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8202. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8203. (org-re
  8204. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8205. (props (list 'face nil
  8206. 'done-face 'org-done
  8207. 'undone-face nil
  8208. 'mouse-face 'highlight
  8209. 'org-not-done-regexp org-not-done-regexp
  8210. 'org-todo-regexp org-todo-regexp
  8211. 'keymap org-agenda-keymap
  8212. 'help-echo
  8213. (format "mouse-2 or RET jump to org file %s"
  8214. (abbreviate-file-name
  8215. (or (buffer-file-name (buffer-base-buffer))
  8216. (buffer-name (buffer-base-buffer)))))))
  8217. (case-fold-search nil)
  8218. lspos tags tags-list
  8219. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8220. (llast 0) rtn level category i txt
  8221. todo marker entry priority)
  8222. (save-excursion
  8223. (goto-char (point-min))
  8224. (when (eq action 'sparse-tree)
  8225. (org-overview)
  8226. (org-remove-occur-highlights))
  8227. (while (re-search-forward re nil t)
  8228. (catch :skip
  8229. (setq todo (if (match-end 1) (match-string 2))
  8230. tags (if (match-end 4) (match-string 4)))
  8231. (goto-char (setq lspos (1+ (match-beginning 0))))
  8232. (setq level (org-reduced-level (funcall outline-level))
  8233. category (org-get-category))
  8234. (setq i llast llast level)
  8235. ;; remove tag lists from same and sublevels
  8236. (while (>= i level)
  8237. (when (setq entry (assoc i tags-alist))
  8238. (setq tags-alist (delete entry tags-alist)))
  8239. (setq i (1- i)))
  8240. ;; add the next tags
  8241. (when tags
  8242. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8243. tags-alist
  8244. (cons (cons level tags) tags-alist)))
  8245. ;; compile tags for current headline
  8246. (setq tags-list
  8247. (if org-use-tag-inheritance
  8248. (apply 'append (mapcar 'cdr tags-alist))
  8249. tags))
  8250. (when (and tags org-use-tag-inheritance
  8251. (not (eq t org-use-tag-inheritance)))
  8252. ;; selective inheritance, remove uninherited ones
  8253. (setcdr (car tags-alist)
  8254. (org-remove-uniherited-tags (cdar tags-alist))))
  8255. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8256. (eval matcher)
  8257. (or (not org-agenda-skip-archived-trees)
  8258. (not (member org-archive-tag tags-list))))
  8259. (and (eq action 'agenda) (org-agenda-skip))
  8260. ;; list this headline
  8261. (if (eq action 'sparse-tree)
  8262. (progn
  8263. (and org-highlight-sparse-tree-matches
  8264. (org-get-heading) (match-end 0)
  8265. (org-highlight-new-match
  8266. (match-beginning 0) (match-beginning 1)))
  8267. (org-show-context 'tags-tree))
  8268. (setq txt (org-format-agenda-item
  8269. ""
  8270. (concat
  8271. (if org-tags-match-list-sublevels
  8272. (make-string (1- level) ?.) "")
  8273. (org-get-heading))
  8274. category tags-list)
  8275. priority (org-get-priority txt))
  8276. (goto-char lspos)
  8277. (setq marker (org-agenda-new-marker))
  8278. (org-add-props txt props
  8279. 'org-marker marker 'org-hd-marker marker 'org-category category
  8280. 'priority priority 'type "tagsmatch")
  8281. (push txt rtn))
  8282. ;; if we are to skip sublevels, jump to end of subtree
  8283. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8284. (when (and (eq action 'sparse-tree)
  8285. (not org-sparse-tree-open-archived-trees))
  8286. (org-hide-archived-subtrees (point-min) (point-max)))
  8287. (nreverse rtn)))
  8288. (defun org-remove-uniherited-tags (tags)
  8289. "Remove all tags that are not inherited from the list TAGS."
  8290. (cond
  8291. ((eq org-use-tag-inheritance t) tags)
  8292. ((not org-use-tag-inheritance) nil)
  8293. ((stringp org-use-tag-inheritance)
  8294. (delq nil (mapcar
  8295. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8296. tags)))
  8297. ((listp org-use-tag-inheritance)
  8298. (org-delete-all org-use-tag-inheritance tags))))
  8299. (defvar todo-only) ;; dynamically scoped
  8300. (defun org-tags-sparse-tree (&optional todo-only match)
  8301. "Create a sparse tree according to tags string MATCH.
  8302. MATCH can contain positive and negative selection of tags, like
  8303. \"+WORK+URGENT-WITHBOSS\".
  8304. If optional argument TODO_ONLY is non-nil, only select lines that are
  8305. also TODO lines."
  8306. (interactive "P")
  8307. (org-prepare-agenda-buffers (list (current-buffer)))
  8308. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8309. (defvar org-cached-props nil)
  8310. (defun org-cached-entry-get (pom property)
  8311. (if (or (eq t org-use-property-inheritance)
  8312. (and (stringp org-use-property-inheritance)
  8313. (string-match org-use-property-inheritance property))
  8314. (and (listp org-use-property-inheritance)
  8315. (member property org-use-property-inheritance)))
  8316. ;; Caching is not possible, check it directly
  8317. (org-entry-get pom property 'inherit)
  8318. ;; Get all properties, so that we can do complicated checks easily
  8319. (cdr (assoc property (or org-cached-props
  8320. (setq org-cached-props
  8321. (org-entry-properties pom)))))))
  8322. (defun org-global-tags-completion-table (&optional files)
  8323. "Return the list of all tags in all agenda buffer/files."
  8324. (save-excursion
  8325. (org-uniquify
  8326. (delq nil
  8327. (apply 'append
  8328. (mapcar
  8329. (lambda (file)
  8330. (set-buffer (find-file-noselect file))
  8331. (append (org-get-buffer-tags)
  8332. (mapcar (lambda (x) (if (stringp (car-safe x))
  8333. (list (car-safe x)) nil))
  8334. org-tag-alist)))
  8335. (if (and files (car files))
  8336. files
  8337. (org-agenda-files))))))))
  8338. (defun org-make-tags-matcher (match)
  8339. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8340. ;; todo-only is scoped dynamically into this function, and the function
  8341. ;; may change it it the matcher asksk for it.
  8342. (unless match
  8343. ;; Get a new match request, with completion
  8344. (let ((org-last-tags-completion-table
  8345. (org-global-tags-completion-table)))
  8346. (setq match (completing-read
  8347. "Match: " 'org-tags-completion-function nil nil nil
  8348. 'org-tags-history))))
  8349. ;; Parse the string and create a lisp form
  8350. (let ((match0 match)
  8351. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8352. minus tag mm
  8353. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8354. orterms term orlist re-p str-p level-p level-op
  8355. prop-p pn pv po cat-p gv)
  8356. (if (string-match "/+" match)
  8357. ;; match contains also a todo-matching request
  8358. (progn
  8359. (setq tagsmatch (substring match 0 (match-beginning 0))
  8360. todomatch (substring match (match-end 0)))
  8361. (if (string-match "^!" todomatch)
  8362. (setq todo-only t todomatch (substring todomatch 1)))
  8363. (if (string-match "^\\s-*$" todomatch)
  8364. (setq todomatch nil)))
  8365. ;; only matching tags
  8366. (setq tagsmatch match todomatch nil))
  8367. ;; Make the tags matcher
  8368. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8369. (setq tagsmatcher t)
  8370. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8371. (while (setq term (pop orterms))
  8372. (while (and (equal (substring term -1) "\\") orterms)
  8373. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8374. (while (string-match re term)
  8375. (setq minus (and (match-end 1)
  8376. (equal (match-string 1 term) "-"))
  8377. tag (match-string 2 term)
  8378. re-p (equal (string-to-char tag) ?{)
  8379. level-p (match-end 4)
  8380. prop-p (match-end 5)
  8381. mm (cond
  8382. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8383. (level-p
  8384. (setq level-op (org-op-to-function (match-string 3 term)))
  8385. `(,level-op level ,(string-to-number
  8386. (match-string 4 term))))
  8387. (prop-p
  8388. (setq pn (match-string 5 term)
  8389. po (match-string 6 term)
  8390. pv (match-string 7 term)
  8391. cat-p (equal pn "CATEGORY")
  8392. re-p (equal (string-to-char pv) ?{)
  8393. str-p (equal (string-to-char pv) ?\")
  8394. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8395. (setq po (org-op-to-function po str-p))
  8396. (if (equal pn "CATEGORY")
  8397. (setq gv '(get-text-property (point) 'org-category))
  8398. (setq gv `(org-cached-entry-get nil ,pn)))
  8399. (if re-p
  8400. (if (eq po 'org<>)
  8401. `(not (string-match ,pv (or ,gv "")))
  8402. `(string-match ,pv (or ,gv "")))
  8403. (if str-p
  8404. `(,po (or ,gv "") ,pv)
  8405. `(,po (string-to-number (or ,gv ""))
  8406. ,(string-to-number pv) ))))
  8407. (t `(member ,(downcase tag) tags-list)))
  8408. mm (if minus (list 'not mm) mm)
  8409. term (substring term (match-end 0)))
  8410. (push mm tagsmatcher))
  8411. (push (if (> (length tagsmatcher) 1)
  8412. (cons 'and tagsmatcher)
  8413. (car tagsmatcher))
  8414. orlist)
  8415. (setq tagsmatcher nil))
  8416. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8417. (setq tagsmatcher
  8418. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8419. ;; Make the todo matcher
  8420. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8421. (setq todomatcher t)
  8422. (setq orterms (org-split-string todomatch "|") orlist nil)
  8423. (while (setq term (pop orterms))
  8424. (while (string-match re term)
  8425. (setq minus (and (match-end 1)
  8426. (equal (match-string 1 term) "-"))
  8427. kwd (match-string 2 term)
  8428. re-p (equal (string-to-char kwd) ?{)
  8429. term (substring term (match-end 0))
  8430. mm (if re-p
  8431. `(string-match ,(substring kwd 1 -1) todo)
  8432. (list 'equal 'todo kwd))
  8433. mm (if minus (list 'not mm) mm))
  8434. (push mm todomatcher))
  8435. (push (if (> (length todomatcher) 1)
  8436. (cons 'and todomatcher)
  8437. (car todomatcher))
  8438. orlist)
  8439. (setq todomatcher nil))
  8440. (setq todomatcher (if (> (length orlist) 1)
  8441. (cons 'or orlist) (car orlist))))
  8442. ;; Return the string and lisp forms of the matcher
  8443. (setq matcher (if todomatcher
  8444. (list 'and tagsmatcher todomatcher)
  8445. tagsmatcher))
  8446. (cons match0 matcher)))
  8447. (defun org-op-to-function (op &optional stringp)
  8448. (setq op
  8449. (cond
  8450. ((equal op "<" ) '(< string< ))
  8451. ((equal op ">" ) '(> org-string> ))
  8452. ((member op '("<=" "=<")) '(<= org-string<= ))
  8453. ((member op '(">=" "=>")) '(>= org-string>= ))
  8454. ((member op '("=" "==")) '(= string= ))
  8455. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8456. (nth (if stringp 1 0) op))
  8457. (defun org<> (a b) (not (= a b)))
  8458. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8459. (defun org-string>= (a b) (not (string< a b)))
  8460. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8461. (defun org-string<> (a b) (not (string= a b)))
  8462. (defun org-match-any-p (re list)
  8463. "Does re match any element of list?"
  8464. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8465. (delq nil list))
  8466. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8467. (defvar org-tags-overlay (org-make-overlay 1 1))
  8468. (org-detach-overlay org-tags-overlay)
  8469. (defun org-get-tags-at (&optional pos)
  8470. "Get a list of all headline tags applicable at POS.
  8471. POS defaults to point. If tags are inherited, the list contains
  8472. the targets in the same sequence as the headlines appear, i.e.
  8473. sthe tags of the current headline come last."
  8474. (interactive)
  8475. (let (tags ltags lastpos parent)
  8476. (save-excursion
  8477. (save-restriction
  8478. (widen)
  8479. (goto-char (or pos (point)))
  8480. (save-match-data
  8481. (condition-case nil
  8482. (progn
  8483. (org-back-to-heading t)
  8484. (while (not (equal lastpos (point)))
  8485. (setq lastpos (point))
  8486. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8487. (setq ltags (org-split-string
  8488. (org-match-string-no-properties 1) ":"))
  8489. (setq tags (append (org-remove-uniherited-tags ltags)
  8490. tags)))
  8491. (or org-use-tag-inheritance (error ""))
  8492. (org-up-heading-all 1)
  8493. (setq parent t)))
  8494. (error nil))))
  8495. tags)))
  8496. (defun org-toggle-tag (tag &optional onoff)
  8497. "Toggle the tag TAG for the current line.
  8498. If ONOFF is `on' or `off', don't toggle but set to this state."
  8499. (unless (org-on-heading-p t) (error "Not on headling"))
  8500. (let (res current)
  8501. (save-excursion
  8502. (beginning-of-line)
  8503. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8504. (point-at-eol) t)
  8505. (progn
  8506. (setq current (match-string 1))
  8507. (replace-match ""))
  8508. (setq current ""))
  8509. (setq current (nreverse (org-split-string current ":")))
  8510. (cond
  8511. ((eq onoff 'on)
  8512. (setq res t)
  8513. (or (member tag current) (push tag current)))
  8514. ((eq onoff 'off)
  8515. (or (not (member tag current)) (setq current (delete tag current))))
  8516. (t (if (member tag current)
  8517. (setq current (delete tag current))
  8518. (setq res t)
  8519. (push tag current))))
  8520. (end-of-line 1)
  8521. (if current
  8522. (progn
  8523. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8524. (org-set-tags nil t))
  8525. (delete-horizontal-space))
  8526. (run-hooks 'org-after-tags-change-hook))
  8527. res))
  8528. (defun org-align-tags-here (to-col)
  8529. ;; Assumes that this is a headline
  8530. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8531. (beginning-of-line 1)
  8532. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8533. (< pos (match-beginning 2)))
  8534. (progn
  8535. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8536. (goto-char (match-beginning 1))
  8537. (insert " ")
  8538. (delete-region (point) (1+ (match-beginning 2)))
  8539. (setq ncol (max (1+ (current-column))
  8540. (1+ col)
  8541. (if (> to-col 0)
  8542. to-col
  8543. (- (abs to-col) tags-l))))
  8544. (setq p (point))
  8545. (insert (make-string (- ncol (current-column)) ?\ ))
  8546. (setq ncol (current-column))
  8547. (tabify p (point-at-eol))
  8548. (org-move-to-column (min ncol col) t))
  8549. (goto-char pos))))
  8550. (defun org-set-tags (&optional arg just-align)
  8551. "Set the tags for the current headline.
  8552. With prefix ARG, realign all tags in headings in the current buffer."
  8553. (interactive "P")
  8554. (let* ((re (concat "^" outline-regexp))
  8555. (current (org-get-tags-string))
  8556. (col (current-column))
  8557. (org-setting-tags t)
  8558. table current-tags inherited-tags ; computed below when needed
  8559. tags p0 c0 c1 rpl)
  8560. (if arg
  8561. (save-excursion
  8562. (goto-char (point-min))
  8563. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8564. (while (re-search-forward re nil t)
  8565. (org-set-tags nil t)
  8566. (end-of-line 1)))
  8567. (message "All tags realigned to column %d" org-tags-column))
  8568. (if just-align
  8569. (setq tags current)
  8570. ;; Get a new set of tags from the user
  8571. (save-excursion
  8572. (setq table (or org-tag-alist (org-get-buffer-tags))
  8573. org-last-tags-completion-table table
  8574. current-tags (org-split-string current ":")
  8575. inherited-tags (nreverse
  8576. (nthcdr (length current-tags)
  8577. (nreverse (org-get-tags-at))))
  8578. tags
  8579. (if (or (eq t org-use-fast-tag-selection)
  8580. (and org-use-fast-tag-selection
  8581. (delq nil (mapcar 'cdr table))))
  8582. (org-fast-tag-selection
  8583. current-tags inherited-tags table
  8584. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8585. (let ((org-add-colon-after-tag-completion t))
  8586. (org-trim
  8587. (org-without-partial-completion
  8588. (completing-read "Tags: " 'org-tags-completion-function
  8589. nil nil current 'org-tags-history)))))))
  8590. (while (string-match "[-+&]+" tags)
  8591. ;; No boolean logic, just a list
  8592. (setq tags (replace-match ":" t t tags))))
  8593. (if (string-match "\\`[\t ]*\\'" tags)
  8594. (setq tags "")
  8595. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8596. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8597. ;; Insert new tags at the correct column
  8598. (beginning-of-line 1)
  8599. (cond
  8600. ((and (equal current "") (equal tags "")))
  8601. ((re-search-forward
  8602. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8603. (point-at-eol) t)
  8604. (if (equal tags "")
  8605. (setq rpl "")
  8606. (goto-char (match-beginning 0))
  8607. (setq c0 (current-column) p0 (point)
  8608. c1 (max (1+ c0) (if (> org-tags-column 0)
  8609. org-tags-column
  8610. (- (- org-tags-column) (length tags))))
  8611. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8612. (replace-match rpl t t)
  8613. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8614. tags)
  8615. (t (error "Tags alignment failed")))
  8616. (org-move-to-column col)
  8617. (unless just-align
  8618. (run-hooks 'org-after-tags-change-hook)))))
  8619. (defun org-change-tag-in-region (beg end tag off)
  8620. "Add or remove TAG for each entry in the region.
  8621. This works in the agenda, and also in an org-mode buffer."
  8622. (interactive
  8623. (list (region-beginning) (region-end)
  8624. (let ((org-last-tags-completion-table
  8625. (if (org-mode-p)
  8626. (org-get-buffer-tags)
  8627. (org-global-tags-completion-table))))
  8628. (completing-read
  8629. "Tag: " 'org-tags-completion-function nil nil nil
  8630. 'org-tags-history))
  8631. (progn
  8632. (message "[s]et or [r]emove? ")
  8633. (equal (read-char-exclusive) ?r))))
  8634. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8635. (let ((agendap (equal major-mode 'org-agenda-mode))
  8636. l1 l2 m buf pos newhead (cnt 0))
  8637. (goto-char end)
  8638. (setq l2 (1- (org-current-line)))
  8639. (goto-char beg)
  8640. (setq l1 (org-current-line))
  8641. (loop for l from l1 to l2 do
  8642. (goto-line l)
  8643. (setq m (get-text-property (point) 'org-hd-marker))
  8644. (when (or (and (org-mode-p) (org-on-heading-p))
  8645. (and agendap m))
  8646. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8647. pos (if agendap m (point)))
  8648. (with-current-buffer buf
  8649. (save-excursion
  8650. (save-restriction
  8651. (goto-char pos)
  8652. (setq cnt (1+ cnt))
  8653. (org-toggle-tag tag (if off 'off 'on))
  8654. (setq newhead (org-get-heading)))))
  8655. (and agendap (org-agenda-change-all-lines newhead m))))
  8656. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8657. (defun org-tags-completion-function (string predicate &optional flag)
  8658. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8659. (confirm (lambda (x) (stringp (car x)))))
  8660. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8661. (setq s1 (match-string 1 string)
  8662. s2 (match-string 2 string))
  8663. (setq s1 "" s2 string))
  8664. (cond
  8665. ((eq flag nil)
  8666. ;; try completion
  8667. (setq rtn (try-completion s2 ctable confirm))
  8668. (if (stringp rtn)
  8669. (setq rtn
  8670. (concat s1 s2 (substring rtn (length s2))
  8671. (if (and org-add-colon-after-tag-completion
  8672. (assoc rtn ctable))
  8673. ":" ""))))
  8674. rtn)
  8675. ((eq flag t)
  8676. ;; all-completions
  8677. (all-completions s2 ctable confirm)
  8678. )
  8679. ((eq flag 'lambda)
  8680. ;; exact match?
  8681. (assoc s2 ctable)))
  8682. ))
  8683. (defun org-fast-tag-insert (kwd tags face &optional end)
  8684. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8685. (insert (format "%-12s" (concat kwd ":"))
  8686. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8687. (or end "")))
  8688. (defun org-fast-tag-show-exit (flag)
  8689. (save-excursion
  8690. (goto-line 3)
  8691. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8692. (replace-match ""))
  8693. (when flag
  8694. (end-of-line 1)
  8695. (org-move-to-column (- (window-width) 19) t)
  8696. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8697. (defun org-set-current-tags-overlay (current prefix)
  8698. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8699. (if (featurep 'xemacs)
  8700. (org-overlay-display org-tags-overlay (concat prefix s)
  8701. 'secondary-selection)
  8702. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8703. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8704. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8705. "Fast tag selection with single keys.
  8706. CURRENT is the current list of tags in the headline, INHERITED is the
  8707. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8708. possibly with grouping information. TODO-TABLE is a similar table with
  8709. TODO keywords, should these have keys assigned to them.
  8710. If the keys are nil, a-z are automatically assigned.
  8711. Returns the new tags string, or nil to not change the current settings."
  8712. (let* ((fulltable (append table todo-table))
  8713. (maxlen (apply 'max (mapcar
  8714. (lambda (x)
  8715. (if (stringp (car x)) (string-width (car x)) 0))
  8716. fulltable)))
  8717. (buf (current-buffer))
  8718. (expert (eq org-fast-tag-selection-single-key 'expert))
  8719. (buffer-tags nil)
  8720. (fwidth (+ maxlen 3 1 3))
  8721. (ncol (/ (- (window-width) 4) fwidth))
  8722. (i-face 'org-done)
  8723. (c-face 'org-todo)
  8724. tg cnt e c char c1 c2 ntable tbl rtn
  8725. ov-start ov-end ov-prefix
  8726. (exit-after-next org-fast-tag-selection-single-key)
  8727. (done-keywords org-done-keywords)
  8728. groups ingroup)
  8729. (save-excursion
  8730. (beginning-of-line 1)
  8731. (if (looking-at
  8732. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8733. (setq ov-start (match-beginning 1)
  8734. ov-end (match-end 1)
  8735. ov-prefix "")
  8736. (setq ov-start (1- (point-at-eol))
  8737. ov-end (1+ ov-start))
  8738. (skip-chars-forward "^\n\r")
  8739. (setq ov-prefix
  8740. (concat
  8741. (buffer-substring (1- (point)) (point))
  8742. (if (> (current-column) org-tags-column)
  8743. " "
  8744. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8745. (org-move-overlay org-tags-overlay ov-start ov-end)
  8746. (save-window-excursion
  8747. (if expert
  8748. (set-buffer (get-buffer-create " *Org tags*"))
  8749. (delete-other-windows)
  8750. (split-window-vertically)
  8751. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8752. (erase-buffer)
  8753. (org-set-local 'org-done-keywords done-keywords)
  8754. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8755. (org-fast-tag-insert "Current" current c-face "\n\n")
  8756. (org-fast-tag-show-exit exit-after-next)
  8757. (org-set-current-tags-overlay current ov-prefix)
  8758. (setq tbl fulltable char ?a cnt 0)
  8759. (while (setq e (pop tbl))
  8760. (cond
  8761. ((equal e '(:startgroup))
  8762. (push '() groups) (setq ingroup t)
  8763. (when (not (= cnt 0))
  8764. (setq cnt 0)
  8765. (insert "\n"))
  8766. (insert "{ "))
  8767. ((equal e '(:endgroup))
  8768. (setq ingroup nil cnt 0)
  8769. (insert "}\n"))
  8770. (t
  8771. (setq tg (car e) c2 nil)
  8772. (if (cdr e)
  8773. (setq c (cdr e))
  8774. ;; automatically assign a character.
  8775. (setq c1 (string-to-char
  8776. (downcase (substring
  8777. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8778. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8779. (while (or (rassoc char ntable) (rassoc char table))
  8780. (setq char (1+ char)))
  8781. (setq c2 c1))
  8782. (setq c (or c2 char)))
  8783. (if ingroup (push tg (car groups)))
  8784. (setq tg (org-add-props tg nil 'face
  8785. (cond
  8786. ((not (assoc tg table))
  8787. (org-get-todo-face tg))
  8788. ((member tg current) c-face)
  8789. ((member tg inherited) i-face)
  8790. (t nil))))
  8791. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8792. (insert "[" c "] " tg (make-string
  8793. (- fwidth 4 (length tg)) ?\ ))
  8794. (push (cons tg c) ntable)
  8795. (when (= (setq cnt (1+ cnt)) ncol)
  8796. (insert "\n")
  8797. (if ingroup (insert " "))
  8798. (setq cnt 0)))))
  8799. (setq ntable (nreverse ntable))
  8800. (insert "\n")
  8801. (goto-char (point-min))
  8802. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8803. (fit-window-to-buffer))
  8804. (setq rtn
  8805. (catch 'exit
  8806. (while t
  8807. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8808. (if groups " [!] no groups" " [!]groups")
  8809. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8810. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8811. (cond
  8812. ((= c ?\r) (throw 'exit t))
  8813. ((= c ?!)
  8814. (setq groups (not groups))
  8815. (goto-char (point-min))
  8816. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8817. ((= c ?\C-c)
  8818. (if (not expert)
  8819. (org-fast-tag-show-exit
  8820. (setq exit-after-next (not exit-after-next)))
  8821. (setq expert nil)
  8822. (delete-other-windows)
  8823. (split-window-vertically)
  8824. (org-switch-to-buffer-other-window " *Org tags*")
  8825. (and (fboundp 'fit-window-to-buffer)
  8826. (fit-window-to-buffer))))
  8827. ((or (= c ?\C-g)
  8828. (and (= c ?q) (not (rassoc c ntable))))
  8829. (org-detach-overlay org-tags-overlay)
  8830. (setq quit-flag t))
  8831. ((= c ?\ )
  8832. (setq current nil)
  8833. (if exit-after-next (setq exit-after-next 'now)))
  8834. ((= c ?\t)
  8835. (condition-case nil
  8836. (setq tg (completing-read
  8837. "Tag: "
  8838. (or buffer-tags
  8839. (with-current-buffer buf
  8840. (org-get-buffer-tags)))))
  8841. (quit (setq tg "")))
  8842. (when (string-match "\\S-" tg)
  8843. (add-to-list 'buffer-tags (list tg))
  8844. (if (member tg current)
  8845. (setq current (delete tg current))
  8846. (push tg current)))
  8847. (if exit-after-next (setq exit-after-next 'now)))
  8848. ((setq e (rassoc c todo-table) tg (car e))
  8849. (with-current-buffer buf
  8850. (save-excursion (org-todo tg)))
  8851. (if exit-after-next (setq exit-after-next 'now)))
  8852. ((setq e (rassoc c ntable) tg (car e))
  8853. (if (member tg current)
  8854. (setq current (delete tg current))
  8855. (loop for g in groups do
  8856. (if (member tg g)
  8857. (mapc (lambda (x)
  8858. (setq current (delete x current)))
  8859. g)))
  8860. (push tg current))
  8861. (if exit-after-next (setq exit-after-next 'now))))
  8862. ;; Create a sorted list
  8863. (setq current
  8864. (sort current
  8865. (lambda (a b)
  8866. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8867. (if (eq exit-after-next 'now) (throw 'exit t))
  8868. (goto-char (point-min))
  8869. (beginning-of-line 2)
  8870. (delete-region (point) (point-at-eol))
  8871. (org-fast-tag-insert "Current" current c-face)
  8872. (org-set-current-tags-overlay current ov-prefix)
  8873. (while (re-search-forward
  8874. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8875. (setq tg (match-string 1))
  8876. (add-text-properties
  8877. (match-beginning 1) (match-end 1)
  8878. (list 'face
  8879. (cond
  8880. ((member tg current) c-face)
  8881. ((member tg inherited) i-face)
  8882. (t (get-text-property (match-beginning 1) 'face))))))
  8883. (goto-char (point-min)))))
  8884. (org-detach-overlay org-tags-overlay)
  8885. (if rtn
  8886. (mapconcat 'identity current ":")
  8887. nil))))
  8888. (defun org-get-tags-string ()
  8889. "Get the TAGS string in the current headline."
  8890. (unless (org-on-heading-p t)
  8891. (error "Not on a heading"))
  8892. (save-excursion
  8893. (beginning-of-line 1)
  8894. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8895. (org-match-string-no-properties 1)
  8896. "")))
  8897. (defun org-get-tags ()
  8898. "Get the list of tags specified in the current headline."
  8899. (org-split-string (org-get-tags-string) ":"))
  8900. (defun org-get-buffer-tags ()
  8901. "Get a table of all tags used in the buffer, for completion."
  8902. (let (tags)
  8903. (save-excursion
  8904. (goto-char (point-min))
  8905. (while (re-search-forward
  8906. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8907. (when (equal (char-after (point-at-bol 0)) ?*)
  8908. (mapc (lambda (x) (add-to-list 'tags x))
  8909. (org-split-string (org-match-string-no-properties 1) ":")))))
  8910. (mapcar 'list tags)))
  8911. ;;;; Properties
  8912. ;;; Setting and retrieving properties
  8913. (defconst org-special-properties
  8914. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8915. "TIMESTAMP" "TIMESTAMP_IA")
  8916. "The special properties valid in Org-mode.
  8917. These are properties that are not defined in the property drawer,
  8918. but in some other way.")
  8919. (defconst org-default-properties
  8920. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8921. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8922. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8923. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8924. "Some properties that are used by Org-mode for various purposes.
  8925. Being in this list makes sure that they are offered for completion.")
  8926. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8927. "Regular expression matching the first line of a property drawer.")
  8928. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8929. "Regular expression matching the first line of a property drawer.")
  8930. (defun org-property-action ()
  8931. "Do an action on properties."
  8932. (interactive)
  8933. (let (c)
  8934. (org-at-property-p)
  8935. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8936. (setq c (read-char-exclusive))
  8937. (cond
  8938. ((equal c ?s)
  8939. (call-interactively 'org-set-property))
  8940. ((equal c ?d)
  8941. (call-interactively 'org-delete-property))
  8942. ((equal c ?D)
  8943. (call-interactively 'org-delete-property-globally))
  8944. ((equal c ?c)
  8945. (call-interactively 'org-compute-property-at-point))
  8946. (t (error "No such property action %c" c)))))
  8947. (defun org-at-property-p ()
  8948. "Is the cursor in a property line?"
  8949. ;; FIXME: Does not check if we are actually in the drawer.
  8950. ;; FIXME: also returns true on any drawers.....
  8951. ;; This is used by C-c C-c for property action.
  8952. (save-excursion
  8953. (beginning-of-line 1)
  8954. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8955. (defun org-get-property-block (&optional beg end force)
  8956. "Return the (beg . end) range of the body of the property drawer.
  8957. BEG and END can be beginning and end of subtree, if not given
  8958. they will be found.
  8959. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8960. (catch 'exit
  8961. (save-excursion
  8962. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8963. (end (or end (progn (outline-next-heading) (point)))))
  8964. (goto-char beg)
  8965. (if (re-search-forward org-property-start-re end t)
  8966. (setq beg (1+ (match-end 0)))
  8967. (if force
  8968. (save-excursion
  8969. (org-insert-property-drawer)
  8970. (setq end (progn (outline-next-heading) (point))))
  8971. (throw 'exit nil))
  8972. (goto-char beg)
  8973. (if (re-search-forward org-property-start-re end t)
  8974. (setq beg (1+ (match-end 0)))))
  8975. (if (re-search-forward org-property-end-re end t)
  8976. (setq end (match-beginning 0))
  8977. (or force (throw 'exit nil))
  8978. (goto-char beg)
  8979. (setq end beg)
  8980. (org-indent-line-function)
  8981. (insert ":END:\n"))
  8982. (cons beg end)))))
  8983. (defun org-entry-properties (&optional pom which)
  8984. "Get all properties of the entry at point-or-marker POM.
  8985. This includes the TODO keyword, the tags, time strings for deadline,
  8986. scheduled, and clocking, and any additional properties defined in the
  8987. entry. The return value is an alist, keys may occur multiple times
  8988. if the property key was used several times.
  8989. POM may also be nil, in which case the current entry is used.
  8990. If WHICH is nil or `all', get all properties. If WHICH is
  8991. `special' or `standard', only get that subclass."
  8992. (setq which (or which 'all))
  8993. (org-with-point-at pom
  8994. (let ((clockstr (substring org-clock-string 0 -1))
  8995. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8996. beg end range props sum-props key value string clocksum)
  8997. (save-excursion
  8998. (when (condition-case nil (org-back-to-heading t) (error nil))
  8999. (setq beg (point))
  9000. (setq sum-props (get-text-property (point) 'org-summaries))
  9001. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9002. (outline-next-heading)
  9003. (setq end (point))
  9004. (when (memq which '(all special))
  9005. ;; Get the special properties, like TODO and tags
  9006. (goto-char beg)
  9007. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9008. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9009. (when (looking-at org-priority-regexp)
  9010. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9011. (when (and (setq value (org-get-tags-string))
  9012. (string-match "\\S-" value))
  9013. (push (cons "TAGS" value) props))
  9014. (when (setq value (org-get-tags-at))
  9015. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9016. props))
  9017. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9018. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9019. string (if (equal key clockstr)
  9020. (org-no-properties
  9021. (org-trim
  9022. (buffer-substring
  9023. (match-beginning 3) (goto-char (point-at-eol)))))
  9024. (substring (org-match-string-no-properties 3) 1 -1)))
  9025. (unless key
  9026. (if (= (char-after (match-beginning 3)) ?\[)
  9027. (setq key "TIMESTAMP_IA")
  9028. (setq key "TIMESTAMP")))
  9029. (when (or (equal key clockstr) (not (assoc key props)))
  9030. (push (cons key string) props)))
  9031. )
  9032. (when (memq which '(all standard))
  9033. ;; Get the standard properties, like :PORP: ...
  9034. (setq range (org-get-property-block beg end))
  9035. (when range
  9036. (goto-char (car range))
  9037. (while (re-search-forward
  9038. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9039. (cdr range) t)
  9040. (setq key (org-match-string-no-properties 1)
  9041. value (org-trim (or (org-match-string-no-properties 2) "")))
  9042. (unless (member key excluded)
  9043. (push (cons key (or value "")) props)))))
  9044. (if clocksum
  9045. (push (cons "CLOCKSUM"
  9046. (org-columns-number-to-string (/ (float clocksum) 60.)
  9047. 'add_times))
  9048. props))
  9049. (append sum-props (nreverse props)))))))
  9050. (defun org-entry-get (pom property &optional inherit)
  9051. "Get value of PROPERTY for entry at point-or-marker POM.
  9052. If INHERIT is non-nil and the entry does not have the property,
  9053. then also check higher levels of the hierarchy.
  9054. If INHERIT is the symbol `selective', use inheritance only if the setting
  9055. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9056. If the property is present but empty, the return value is the empty string.
  9057. If the property is not present at all, nil is returned."
  9058. (org-with-point-at pom
  9059. (if (and inherit (if (eq inherit 'selective)
  9060. (org-property-inherit-p property)
  9061. t))
  9062. (org-entry-get-with-inheritance property)
  9063. (if (member property org-special-properties)
  9064. ;; We need a special property. Use brute force, get all properties.
  9065. (cdr (assoc property (org-entry-properties nil 'special)))
  9066. (let ((range (org-get-property-block)))
  9067. (if (and range
  9068. (goto-char (car range))
  9069. (re-search-forward
  9070. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9071. (cdr range) t))
  9072. ;; Found the property, return it.
  9073. (if (match-end 1)
  9074. (org-match-string-no-properties 1)
  9075. "")))))))
  9076. (defun org-property-or-variable-value (var &optional inherit)
  9077. "Check if there is a property fixing the value of VAR.
  9078. If yes, return this value. If not, return the current value of the variable."
  9079. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9080. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9081. (read prop)
  9082. (symbol-value var))))
  9083. (defun org-entry-delete (pom property)
  9084. "Delete the property PROPERTY from entry at point-or-marker POM."
  9085. (org-with-point-at pom
  9086. (if (member property org-special-properties)
  9087. nil ; cannot delete these properties.
  9088. (let ((range (org-get-property-block)))
  9089. (if (and range
  9090. (goto-char (car range))
  9091. (re-search-forward
  9092. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9093. (cdr range) t))
  9094. (progn
  9095. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9096. t)
  9097. nil)))))
  9098. ;; Multi-values properties are properties that contain multiple values
  9099. ;; These values are assumed to be single words, separated by whitespace.
  9100. (defun org-entry-add-to-multivalued-property (pom property value)
  9101. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9102. (let* ((old (org-entry-get pom property))
  9103. (values (and old (org-split-string old "[ \t]"))))
  9104. (unless (member value values)
  9105. (setq values (cons value values))
  9106. (org-entry-put pom property
  9107. (mapconcat 'identity values " ")))))
  9108. (defun org-entry-remove-from-multivalued-property (pom property value)
  9109. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9110. (let* ((old (org-entry-get pom property))
  9111. (values (and old (org-split-string old "[ \t]"))))
  9112. (when (member value values)
  9113. (setq values (delete value values))
  9114. (org-entry-put pom property
  9115. (mapconcat 'identity values " ")))))
  9116. (defun org-entry-member-in-multivalued-property (pom property value)
  9117. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9118. (let* ((old (org-entry-get pom property))
  9119. (values (and old (org-split-string old "[ \t]"))))
  9120. (member value values)))
  9121. (defvar org-entry-property-inherited-from (make-marker))
  9122. (defun org-entry-get-with-inheritance (property)
  9123. "Get entry property, and search higher levels if not present."
  9124. (let (tmp)
  9125. (save-excursion
  9126. (save-restriction
  9127. (widen)
  9128. (catch 'ex
  9129. (while t
  9130. (when (setq tmp (org-entry-get nil property))
  9131. (org-back-to-heading t)
  9132. (move-marker org-entry-property-inherited-from (point))
  9133. (throw 'ex tmp))
  9134. (or (org-up-heading-safe) (throw 'ex nil)))))
  9135. (or tmp
  9136. (cdr (assoc property org-file-properties))
  9137. (cdr (assoc property org-global-properties))
  9138. (cdr (assoc property org-global-properties-fixed))))))
  9139. (defun org-entry-put (pom property value)
  9140. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9141. (org-with-point-at pom
  9142. (org-back-to-heading t)
  9143. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9144. range)
  9145. (cond
  9146. ((equal property "TODO")
  9147. (when (and (stringp value) (string-match "\\S-" value)
  9148. (not (member value org-todo-keywords-1)))
  9149. (error "\"%s\" is not a valid TODO state" value))
  9150. (if (or (not value)
  9151. (not (string-match "\\S-" value)))
  9152. (setq value 'none))
  9153. (org-todo value)
  9154. (org-set-tags nil 'align))
  9155. ((equal property "PRIORITY")
  9156. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9157. (string-to-char value) ?\ ))
  9158. (org-set-tags nil 'align))
  9159. ((equal property "SCHEDULED")
  9160. (if (re-search-forward org-scheduled-time-regexp end t)
  9161. (cond
  9162. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9163. ((eq value 'later) (org-timestamp-change 1 'day))
  9164. (t (call-interactively 'org-schedule)))
  9165. (call-interactively 'org-schedule)))
  9166. ((equal property "DEADLINE")
  9167. (if (re-search-forward org-deadline-time-regexp end t)
  9168. (cond
  9169. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9170. ((eq value 'later) (org-timestamp-change 1 'day))
  9171. (t (call-interactively 'org-deadline)))
  9172. (call-interactively 'org-deadline)))
  9173. ((member property org-special-properties)
  9174. (error "The %s property can not yet be set with `org-entry-put'"
  9175. property))
  9176. (t ; a non-special property
  9177. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9178. (setq range (org-get-property-block beg end 'force))
  9179. (goto-char (car range))
  9180. (if (re-search-forward
  9181. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9182. (progn
  9183. (delete-region (match-beginning 1) (match-end 1))
  9184. (goto-char (match-beginning 1)))
  9185. (goto-char (cdr range))
  9186. (insert "\n")
  9187. (backward-char 1)
  9188. (org-indent-line-function)
  9189. (insert ":" property ":"))
  9190. (and value (insert " " value))
  9191. (org-indent-line-function)))))))
  9192. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9193. "Get all property keys in the current buffer.
  9194. With INCLUDE-SPECIALS, also list the special properties that relect things
  9195. like tags and TODO state.
  9196. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9197. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9198. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9199. formats in the current buffer."
  9200. (let (rtn range cfmt cols s p)
  9201. (save-excursion
  9202. (save-restriction
  9203. (widen)
  9204. (goto-char (point-min))
  9205. (while (re-search-forward org-property-start-re nil t)
  9206. (setq range (org-get-property-block))
  9207. (goto-char (car range))
  9208. (while (re-search-forward
  9209. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9210. (cdr range) t)
  9211. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9212. (outline-next-heading))))
  9213. (when include-specials
  9214. (setq rtn (append org-special-properties rtn)))
  9215. (when include-defaults
  9216. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9217. (when include-columns
  9218. (save-excursion
  9219. (save-restriction
  9220. (widen)
  9221. (goto-char (point-min))
  9222. (while (re-search-forward
  9223. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9224. nil t)
  9225. (setq cfmt (match-string 2) s 0)
  9226. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9227. cfmt s)
  9228. (setq s (match-end 0)
  9229. p (match-string 1 cfmt))
  9230. (unless (or (equal p "ITEM")
  9231. (member p org-special-properties))
  9232. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9233. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9234. (defun org-property-values (key)
  9235. "Return a list of all values of property KEY."
  9236. (save-excursion
  9237. (save-restriction
  9238. (widen)
  9239. (goto-char (point-min))
  9240. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9241. values)
  9242. (while (re-search-forward re nil t)
  9243. (add-to-list 'values (org-trim (match-string 1))))
  9244. (delete "" values)))))
  9245. (defun org-insert-property-drawer ()
  9246. "Insert a property drawer into the current entry."
  9247. (interactive)
  9248. (org-back-to-heading t)
  9249. (looking-at outline-regexp)
  9250. (let ((indent (- (match-end 0)(match-beginning 0)))
  9251. (beg (point))
  9252. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9253. end hiddenp)
  9254. (outline-next-heading)
  9255. (setq end (point))
  9256. (goto-char beg)
  9257. (while (re-search-forward re end t))
  9258. (setq hiddenp (org-invisible-p))
  9259. (end-of-line 1)
  9260. (and (equal (char-after) ?\n) (forward-char 1))
  9261. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9262. (beginning-of-line 2))
  9263. (org-skip-over-state-notes)
  9264. (skip-chars-backward " \t\n\r")
  9265. (if (eq (char-before) ?*) (forward-char 1))
  9266. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9267. (beginning-of-line 0)
  9268. (org-indent-to-column indent)
  9269. (beginning-of-line 2)
  9270. (org-indent-to-column indent)
  9271. (beginning-of-line 0)
  9272. (if hiddenp
  9273. (save-excursion
  9274. (org-back-to-heading t)
  9275. (hide-entry))
  9276. (org-flag-drawer t))))
  9277. (defun org-set-property (property value)
  9278. "In the current entry, set PROPERTY to VALUE.
  9279. When called interactively, this will prompt for a property name, offering
  9280. completion on existing and default properties. And then it will prompt
  9281. for a value, offering competion either on allowed values (via an inherited
  9282. xxx_ALL property) or on existing values in other instances of this property
  9283. in the current file."
  9284. (interactive
  9285. (let* ((completion-ignore-case t)
  9286. (keys (org-buffer-property-keys nil t t))
  9287. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9288. (prop (if (member prop0 keys)
  9289. prop0
  9290. (or (cdr (assoc (downcase prop0)
  9291. (mapcar (lambda (x) (cons (downcase x) x))
  9292. keys)))
  9293. prop0)))
  9294. (cur (org-entry-get nil prop))
  9295. (allowed (org-property-get-allowed-values nil prop 'table))
  9296. (existing (mapcar 'list (org-property-values prop)))
  9297. (val (if allowed
  9298. (org-completing-read "Value: " allowed nil 'req-match)
  9299. (org-completing-read
  9300. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9301. (concat "[" cur "]") "")
  9302. ": ")
  9303. existing nil nil "" nil cur))))
  9304. (list prop (if (equal val "") cur val))))
  9305. (unless (equal (org-entry-get nil property) value)
  9306. (org-entry-put nil property value)))
  9307. (defun org-delete-property (property)
  9308. "In the current entry, delete PROPERTY."
  9309. (interactive
  9310. (let* ((completion-ignore-case t)
  9311. (prop (completing-read
  9312. "Property: " (org-entry-properties nil 'standard))))
  9313. (list prop)))
  9314. (message "Property %s %s" property
  9315. (if (org-entry-delete nil property)
  9316. "deleted"
  9317. "was not present in the entry")))
  9318. (defun org-delete-property-globally (property)
  9319. "Remove PROPERTY globally, from all entries."
  9320. (interactive
  9321. (let* ((completion-ignore-case t)
  9322. (prop (completing-read
  9323. "Globally remove property: "
  9324. (mapcar 'list (org-buffer-property-keys)))))
  9325. (list prop)))
  9326. (save-excursion
  9327. (save-restriction
  9328. (widen)
  9329. (goto-char (point-min))
  9330. (let ((cnt 0))
  9331. (while (re-search-forward
  9332. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9333. nil t)
  9334. (setq cnt (1+ cnt))
  9335. (replace-match ""))
  9336. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9337. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9338. (defun org-compute-property-at-point ()
  9339. "Compute the property at point.
  9340. This looks for an enclosing column format, extracts the operator and
  9341. then applies it to the proerty in the column format's scope."
  9342. (interactive)
  9343. (unless (org-at-property-p)
  9344. (error "Not at a property"))
  9345. (let ((prop (org-match-string-no-properties 2)))
  9346. (org-columns-get-format-and-top-level)
  9347. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9348. (error "No operator defined for property %s" prop))
  9349. (org-columns-compute prop)))
  9350. (defun org-property-get-allowed-values (pom property &optional table)
  9351. "Get allowed values for the property PROPERTY.
  9352. When TABLE is non-nil, return an alist that can directly be used for
  9353. completion."
  9354. (let (vals)
  9355. (cond
  9356. ((equal property "TODO")
  9357. (setq vals (org-with-point-at pom
  9358. (append org-todo-keywords-1 '("")))))
  9359. ((equal property "PRIORITY")
  9360. (let ((n org-lowest-priority))
  9361. (while (>= n org-highest-priority)
  9362. (push (char-to-string n) vals)
  9363. (setq n (1- n)))))
  9364. ((member property org-special-properties))
  9365. (t
  9366. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9367. (when (and vals (string-match "\\S-" vals))
  9368. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9369. (setq vals (mapcar (lambda (x)
  9370. (cond ((stringp x) x)
  9371. ((numberp x) (number-to-string x))
  9372. ((symbolp x) (symbol-name x))
  9373. (t "???")))
  9374. vals)))))
  9375. (if table (mapcar 'list vals) vals)))
  9376. (defun org-property-previous-allowed-value (&optional previous)
  9377. "Switch to the next allowed value for this property."
  9378. (interactive)
  9379. (org-property-next-allowed-value t))
  9380. (defun org-property-next-allowed-value (&optional previous)
  9381. "Switch to the next allowed value for this property."
  9382. (interactive)
  9383. (unless (org-at-property-p)
  9384. (error "Not at a property"))
  9385. (let* ((key (match-string 2))
  9386. (value (match-string 3))
  9387. (allowed (or (org-property-get-allowed-values (point) key)
  9388. (and (member value '("[ ]" "[-]" "[X]"))
  9389. '("[ ]" "[X]"))))
  9390. nval)
  9391. (unless allowed
  9392. (error "Allowed values for this property have not been defined"))
  9393. (if previous (setq allowed (reverse allowed)))
  9394. (if (member value allowed)
  9395. (setq nval (car (cdr (member value allowed)))))
  9396. (setq nval (or nval (car allowed)))
  9397. (if (equal nval value)
  9398. (error "Only one allowed value for this property"))
  9399. (org-at-property-p)
  9400. (replace-match (concat " :" key ": " nval) t t)
  9401. (org-indent-line-function)
  9402. (beginning-of-line 1)
  9403. (skip-chars-forward " \t")))
  9404. (defun org-find-entry-with-id (ident)
  9405. "Locate the entry that contains the ID property with exact value IDENT.
  9406. IDENT can be a string, a symbol or a number, this function will search for
  9407. the string representation of it.
  9408. Return the position where this entry starts, or nil if there is no such entry."
  9409. (let ((id (cond
  9410. ((stringp ident) ident)
  9411. ((symbol-name ident) (symbol-name ident))
  9412. ((numberp ident) (number-to-string ident))
  9413. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9414. (case-fold-search nil))
  9415. (save-excursion
  9416. (save-restriction
  9417. (widen)
  9418. (goto-char (point-min))
  9419. (when (re-search-forward
  9420. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9421. nil t)
  9422. (org-back-to-heading)
  9423. (point))))))
  9424. ;;;; Timestamps
  9425. (defvar org-last-changed-timestamp nil)
  9426. (defvar org-time-was-given) ; dynamically scoped parameter
  9427. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9428. (defvar org-ts-what) ; dynamically scoped parameter
  9429. (defun org-time-stamp (arg)
  9430. "Prompt for a date/time and insert a time stamp.
  9431. If the user specifies a time like HH:MM, or if this command is called
  9432. with a prefix argument, the time stamp will contain date and time.
  9433. Otherwise, only the date will be included. All parts of a date not
  9434. specified by the user will be filled in from the current date/time.
  9435. So if you press just return without typing anything, the time stamp
  9436. will represent the current date/time. If there is already a timestamp
  9437. at the cursor, it will be modified."
  9438. (interactive "P")
  9439. (let* ((ts nil)
  9440. (default-time
  9441. ;; Default time is either today, or, when entering a range,
  9442. ;; the range start.
  9443. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9444. (save-excursion
  9445. (re-search-backward
  9446. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9447. (- (point) 20) t)))
  9448. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9449. (current-time)))
  9450. (default-input (and ts (org-get-compact-tod ts)))
  9451. org-time-was-given org-end-time-was-given time)
  9452. (cond
  9453. ((and (org-at-timestamp-p)
  9454. (eq last-command 'org-time-stamp)
  9455. (eq this-command 'org-time-stamp))
  9456. (insert "--")
  9457. (setq time (let ((this-command this-command))
  9458. (org-read-date arg 'totime nil nil default-time default-input)))
  9459. (org-insert-time-stamp time (or org-time-was-given arg)))
  9460. ((org-at-timestamp-p)
  9461. (setq time (let ((this-command this-command))
  9462. (org-read-date arg 'totime nil nil default-time default-input)))
  9463. (when (org-at-timestamp-p) ; just to get the match data
  9464. (replace-match "")
  9465. (setq org-last-changed-timestamp
  9466. (org-insert-time-stamp
  9467. time (or org-time-was-given arg)
  9468. nil nil nil (list org-end-time-was-given))))
  9469. (message "Timestamp updated"))
  9470. (t
  9471. (setq time (let ((this-command this-command))
  9472. (org-read-date arg 'totime nil nil default-time default-input)))
  9473. (org-insert-time-stamp time (or org-time-was-given arg)
  9474. nil nil nil (list org-end-time-was-given))))))
  9475. ;; FIXME: can we use this for something else, like computing time differences?
  9476. (defun org-get-compact-tod (s)
  9477. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9478. (let* ((t1 (match-string 1 s))
  9479. (h1 (string-to-number (match-string 2 s)))
  9480. (m1 (string-to-number (match-string 3 s)))
  9481. (t2 (and (match-end 4) (match-string 5 s)))
  9482. (h2 (and t2 (string-to-number (match-string 6 s))))
  9483. (m2 (and t2 (string-to-number (match-string 7 s))))
  9484. dh dm)
  9485. (if (not t2)
  9486. t1
  9487. (setq dh (- h2 h1) dm (- m2 m1))
  9488. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9489. (concat t1 "+" (number-to-string dh)
  9490. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9491. (defun org-time-stamp-inactive (&optional arg)
  9492. "Insert an inactive time stamp.
  9493. An inactive time stamp is enclosed in square brackets instead of angle
  9494. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9495. does not link to the calendar and cannot be changed with the S-cursor keys.
  9496. So these are more for recording a certain time/date."
  9497. (interactive "P")
  9498. (let (org-time-was-given org-end-time-was-given time)
  9499. (setq time (org-read-date arg 'totime))
  9500. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9501. nil nil (list org-end-time-was-given))))
  9502. (defvar org-date-ovl (org-make-overlay 1 1))
  9503. (org-overlay-put org-date-ovl 'face 'org-warning)
  9504. (org-detach-overlay org-date-ovl)
  9505. (defvar org-ans1) ; dynamically scoped parameter
  9506. (defvar org-ans2) ; dynamically scoped parameter
  9507. (defvar org-plain-time-of-day-regexp) ; defined below
  9508. (defvar org-read-date-overlay nil)
  9509. (defvar org-dcst nil) ; dynamically scoped
  9510. (defun org-read-date (&optional with-time to-time from-string prompt
  9511. default-time default-input)
  9512. "Read a date, possibly a time, and make things smooth for the user.
  9513. The prompt will suggest to enter an ISO date, but you can also enter anything
  9514. which will at least partially be understood by `parse-time-string'.
  9515. Unrecognized parts of the date will default to the current day, month, year,
  9516. hour and minute. If this command is called to replace a timestamp at point,
  9517. of to enter the second timestamp of a range, the default time is taken from the
  9518. existing stamp. For example,
  9519. 3-2-5 --> 2003-02-05
  9520. feb 15 --> currentyear-02-15
  9521. sep 12 9 --> 2009-09-12
  9522. 12:45 --> today 12:45
  9523. 22 sept 0:34 --> currentyear-09-22 0:34
  9524. 12 --> currentyear-currentmonth-12
  9525. Fri --> nearest Friday (today or later)
  9526. etc.
  9527. Furthermore you can specify a relative date by giving, as the *first* thing
  9528. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9529. change in days weeks, months, years.
  9530. With a single plus or minus, the date is relative to today. With a double
  9531. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9532. +4d --> four days from today
  9533. +4 --> same as above
  9534. +2w --> two weeks from today
  9535. ++5 --> five days from default date
  9536. The function understands only English month and weekday abbreviations,
  9537. but this can be configured with the variables `parse-time-months' and
  9538. `parse-time-weekdays'.
  9539. While prompting, a calendar is popped up - you can also select the
  9540. date with the mouse (button 1). The calendar shows a period of three
  9541. months. To scroll it to other months, use the keys `>' and `<'.
  9542. If you don't like the calendar, turn it off with
  9543. \(setq org-read-date-popup-calendar nil)
  9544. With optional argument TO-TIME, the date will immediately be converted
  9545. to an internal time.
  9546. With an optional argument WITH-TIME, the prompt will suggest to also
  9547. insert a time. Note that when WITH-TIME is not set, you can still
  9548. enter a time, and this function will inform the calling routine about
  9549. this change. The calling routine may then choose to change the format
  9550. used to insert the time stamp into the buffer to include the time.
  9551. With optional argument FROM-STRING, read from this string instead from
  9552. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9553. the time/date that is used for everything that is not specified by the
  9554. user."
  9555. (require 'parse-time)
  9556. (let* ((org-time-stamp-rounding-minutes
  9557. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9558. (org-dcst org-display-custom-times)
  9559. (ct (org-current-time))
  9560. (def (or default-time ct))
  9561. (defdecode (decode-time def))
  9562. (dummy (progn
  9563. (when (< (nth 2 defdecode) org-extend-today-until)
  9564. (setcar (nthcdr 2 defdecode) -1)
  9565. (setcar (nthcdr 1 defdecode) 59)
  9566. (setq def (apply 'encode-time defdecode)
  9567. defdecode (decode-time def)))))
  9568. (calendar-move-hook nil)
  9569. (calendar-view-diary-initially-flag nil)
  9570. (view-diary-entries-initially nil)
  9571. (calendar-view-holidays-initially-flag nil)
  9572. (view-calendar-holidays-initially nil)
  9573. (timestr (format-time-string
  9574. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9575. (prompt (concat (if prompt (concat prompt " ") "")
  9576. (format "Date+time [%s]: " timestr)))
  9577. ans (org-ans0 "") org-ans1 org-ans2 final)
  9578. (cond
  9579. (from-string (setq ans from-string))
  9580. (org-read-date-popup-calendar
  9581. (save-excursion
  9582. (save-window-excursion
  9583. (calendar)
  9584. (calendar-forward-day (- (time-to-days def)
  9585. (calendar-absolute-from-gregorian
  9586. (calendar-current-date))))
  9587. (org-eval-in-calendar nil t)
  9588. (let* ((old-map (current-local-map))
  9589. (map (copy-keymap calendar-mode-map))
  9590. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9591. (org-defkey map (kbd "RET") 'org-calendar-select)
  9592. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9593. 'org-calendar-select-mouse)
  9594. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9595. 'org-calendar-select-mouse)
  9596. (org-defkey minibuffer-local-map [(meta shift left)]
  9597. (lambda () (interactive)
  9598. (org-eval-in-calendar '(calendar-backward-month 1))))
  9599. (org-defkey minibuffer-local-map [(meta shift right)]
  9600. (lambda () (interactive)
  9601. (org-eval-in-calendar '(calendar-forward-month 1))))
  9602. (org-defkey minibuffer-local-map [(meta shift up)]
  9603. (lambda () (interactive)
  9604. (org-eval-in-calendar '(calendar-backward-year 1))))
  9605. (org-defkey minibuffer-local-map [(meta shift down)]
  9606. (lambda () (interactive)
  9607. (org-eval-in-calendar '(calendar-forward-year 1))))
  9608. (org-defkey minibuffer-local-map [(shift up)]
  9609. (lambda () (interactive)
  9610. (org-eval-in-calendar '(calendar-backward-week 1))))
  9611. (org-defkey minibuffer-local-map [(shift down)]
  9612. (lambda () (interactive)
  9613. (org-eval-in-calendar '(calendar-forward-week 1))))
  9614. (org-defkey minibuffer-local-map [(shift left)]
  9615. (lambda () (interactive)
  9616. (org-eval-in-calendar '(calendar-backward-day 1))))
  9617. (org-defkey minibuffer-local-map [(shift right)]
  9618. (lambda () (interactive)
  9619. (org-eval-in-calendar '(calendar-forward-day 1))))
  9620. (org-defkey minibuffer-local-map ">"
  9621. (lambda () (interactive)
  9622. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9623. (org-defkey minibuffer-local-map "<"
  9624. (lambda () (interactive)
  9625. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9626. (unwind-protect
  9627. (progn
  9628. (use-local-map map)
  9629. (add-hook 'post-command-hook 'org-read-date-display)
  9630. (setq org-ans0 (read-string prompt default-input nil nil))
  9631. ;; org-ans0: from prompt
  9632. ;; org-ans1: from mouse click
  9633. ;; org-ans2: from calendar motion
  9634. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9635. (remove-hook 'post-command-hook 'org-read-date-display)
  9636. (use-local-map old-map)
  9637. (when org-read-date-overlay
  9638. (org-delete-overlay org-read-date-overlay)
  9639. (setq org-read-date-overlay nil)))))))
  9640. (t ; Naked prompt only
  9641. (unwind-protect
  9642. (setq ans (read-string prompt default-input nil timestr))
  9643. (when org-read-date-overlay
  9644. (org-delete-overlay org-read-date-overlay)
  9645. (setq org-read-date-overlay nil)))))
  9646. (setq final (org-read-date-analyze ans def defdecode))
  9647. (if to-time
  9648. (apply 'encode-time final)
  9649. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9650. (format "%04d-%02d-%02d %02d:%02d"
  9651. (nth 5 final) (nth 4 final) (nth 3 final)
  9652. (nth 2 final) (nth 1 final))
  9653. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9654. (defvar def)
  9655. (defvar defdecode)
  9656. (defvar with-time)
  9657. (defun org-read-date-display ()
  9658. "Display the currrent date prompt interpretation in the minibuffer."
  9659. (when org-read-date-display-live
  9660. (when org-read-date-overlay
  9661. (org-delete-overlay org-read-date-overlay))
  9662. (let ((p (point)))
  9663. (end-of-line 1)
  9664. (while (not (equal (buffer-substring
  9665. (max (point-min) (- (point) 4)) (point))
  9666. " "))
  9667. (insert " "))
  9668. (goto-char p))
  9669. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9670. " " (or org-ans1 org-ans2)))
  9671. (org-end-time-was-given nil)
  9672. (f (org-read-date-analyze ans def defdecode))
  9673. (fmts (if org-dcst
  9674. org-time-stamp-custom-formats
  9675. org-time-stamp-formats))
  9676. (fmt (if (or with-time
  9677. (and (boundp 'org-time-was-given) org-time-was-given))
  9678. (cdr fmts)
  9679. (car fmts)))
  9680. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9681. (when (and org-end-time-was-given
  9682. (string-match org-plain-time-of-day-regexp txt))
  9683. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9684. org-end-time-was-given
  9685. (substring txt (match-end 0)))))
  9686. (setq org-read-date-overlay
  9687. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9688. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9689. (defun org-read-date-analyze (ans def defdecode)
  9690. "Analyze the combined answer of the date prompt."
  9691. ;; FIXME: cleanup and comment
  9692. (let (delta deltan deltaw deltadef year month day
  9693. hour minute second wday pm h2 m2 tl wday1
  9694. iso-year iso-weekday iso-week iso-year iso-date)
  9695. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9696. (setq ans "+0"))
  9697. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9698. (setq ans (replace-match "" t t ans)
  9699. deltan (car delta)
  9700. deltaw (nth 1 delta)
  9701. deltadef (nth 2 delta)))
  9702. ;; Check if there is an iso week date in there
  9703. ;; If yes, sore the info and ostpone interpreting it until the rest
  9704. ;; of the parsing is done
  9705. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9706. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9707. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9708. iso-week (string-to-number (match-string 2 ans)))
  9709. (setq ans (replace-match "" t t ans)))
  9710. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9711. (when (string-match
  9712. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9713. (setq year (if (match-end 2)
  9714. (string-to-number (match-string 2 ans))
  9715. (string-to-number (format-time-string "%Y")))
  9716. month (string-to-number (match-string 3 ans))
  9717. day (string-to-number (match-string 4 ans)))
  9718. (if (< year 100) (setq year (+ 2000 year)))
  9719. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9720. t nil ans)))
  9721. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9722. ;; If there is a time with am/pm, and *no* time without it, we convert
  9723. ;; so that matching will be successful.
  9724. (loop for i from 1 to 2 do ; twice, for end time as well
  9725. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9726. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9727. (setq hour (string-to-number (match-string 1 ans))
  9728. minute (if (match-end 3)
  9729. (string-to-number (match-string 3 ans))
  9730. 0)
  9731. pm (equal ?p
  9732. (string-to-char (downcase (match-string 4 ans)))))
  9733. (if (and (= hour 12) (not pm))
  9734. (setq hour 0)
  9735. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9736. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9737. t t ans))))
  9738. ;; Check if a time range is given as a duration
  9739. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9740. (setq hour (string-to-number (match-string 1 ans))
  9741. h2 (+ hour (string-to-number (match-string 3 ans)))
  9742. minute (string-to-number (match-string 2 ans))
  9743. m2 (+ minute (if (match-end 5) (string-to-number
  9744. (match-string 5 ans))0)))
  9745. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9746. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9747. t t ans)))
  9748. ;; Check if there is a time range
  9749. (when (boundp 'org-end-time-was-given)
  9750. (setq org-time-was-given nil)
  9751. (when (and (string-match org-plain-time-of-day-regexp ans)
  9752. (match-end 8))
  9753. (setq org-end-time-was-given (match-string 8 ans))
  9754. (setq ans (concat (substring ans 0 (match-beginning 7))
  9755. (substring ans (match-end 7))))))
  9756. (setq tl (parse-time-string ans)
  9757. day (or (nth 3 tl) (nth 3 defdecode))
  9758. month (or (nth 4 tl)
  9759. (if (and org-read-date-prefer-future
  9760. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9761. (1+ (nth 4 defdecode))
  9762. (nth 4 defdecode)))
  9763. year (or (nth 5 tl)
  9764. (if (and org-read-date-prefer-future
  9765. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9766. (1+ (nth 5 defdecode))
  9767. (nth 5 defdecode)))
  9768. hour (or (nth 2 tl) (nth 2 defdecode))
  9769. minute (or (nth 1 tl) (nth 1 defdecode))
  9770. second (or (nth 0 tl) 0)
  9771. wday (nth 6 tl))
  9772. ;; Special date definitions below
  9773. (cond
  9774. (iso-week
  9775. ;; There was an iso week
  9776. (setq year (or iso-year year)
  9777. day (or iso-weekday wday 1)
  9778. wday nil ; to make sure that the trigger below does not match
  9779. iso-date (calendar-gregorian-from-absolute
  9780. (calendar-absolute-from-iso
  9781. (list iso-week day year))))
  9782. ; FIXME: Should we also push ISO weeks into the future?
  9783. ; (when (and org-read-date-prefer-future
  9784. ; (not iso-year)
  9785. ; (< (calendar-absolute-from-gregorian iso-date)
  9786. ; (time-to-days (current-time))))
  9787. ; (setq year (1+ year)
  9788. ; iso-date (calendar-gregorian-from-absolute
  9789. ; (calendar-absolute-from-iso
  9790. ; (list iso-week day year)))))
  9791. (setq month (car iso-date)
  9792. year (nth 2 iso-date)
  9793. day (nth 1 iso-date)))
  9794. (deltan
  9795. (unless deltadef
  9796. (let ((now (decode-time (current-time))))
  9797. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9798. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9799. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9800. ((equal deltaw "m") (setq month (+ month deltan)))
  9801. ((equal deltaw "y") (setq year (+ year deltan)))))
  9802. ((and wday (not (nth 3 tl)))
  9803. ;; Weekday was given, but no day, so pick that day in the week
  9804. ;; on or after the derived date.
  9805. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9806. (unless (equal wday wday1)
  9807. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9808. (if (and (boundp 'org-time-was-given)
  9809. (nth 2 tl))
  9810. (setq org-time-was-given t))
  9811. (if (< year 100) (setq year (+ 2000 year)))
  9812. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9813. (list second minute hour day month year)))
  9814. (defvar parse-time-weekdays)
  9815. (defun org-read-date-get-relative (s today default)
  9816. "Check string S for special relative date string.
  9817. TODAY and DEFAULT are internal times, for today and for a default.
  9818. Return shift list (N what def-flag)
  9819. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9820. N is the number of WHATs to shift.
  9821. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9822. the DEFAULT date rather than TODAY."
  9823. (when (and
  9824. (string-match
  9825. (concat
  9826. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9827. "\\([0-9]+\\)?"
  9828. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9829. "\\([ \t]\\|$\\)") s)
  9830. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9831. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9832. (string-to-char (substring (match-string 1 s) -1))
  9833. ?+))
  9834. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9835. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9836. (what (if (match-end 3) (match-string 3 s) "d"))
  9837. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9838. (date (if rel default today))
  9839. (wday (nth 6 (decode-time date)))
  9840. delta)
  9841. (if wday1
  9842. (progn
  9843. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9844. (if (= dir ?-) (setq delta (- delta 7)))
  9845. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9846. (list delta "d" rel))
  9847. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9848. (defun org-eval-in-calendar (form &optional keepdate)
  9849. "Eval FORM in the calendar window and return to current window.
  9850. Also, store the cursor date in variable org-ans2."
  9851. (let ((sw (selected-window)))
  9852. (select-window (get-buffer-window "*Calendar*"))
  9853. (eval form)
  9854. (when (and (not keepdate) (calendar-cursor-to-date))
  9855. (let* ((date (calendar-cursor-to-date))
  9856. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9857. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9858. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9859. (select-window sw)))
  9860. ; ;; Update the prompt to show new default date
  9861. ; (save-excursion
  9862. ; (goto-char (point-min))
  9863. ; (when (and org-ans2
  9864. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9865. ; (get-text-property (match-end 0) 'field))
  9866. ; (let ((inhibit-read-only t))
  9867. ; (replace-match (concat "[" org-ans2 "]") t t)
  9868. ; (add-text-properties (point-min) (1+ (match-end 0))
  9869. ; (text-properties-at (1+ (point-min)))))))))
  9870. (defun org-calendar-select ()
  9871. "Return to `org-read-date' with the date currently selected.
  9872. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9873. (interactive)
  9874. (when (calendar-cursor-to-date)
  9875. (let* ((date (calendar-cursor-to-date))
  9876. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9877. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9878. (if (active-minibuffer-window) (exit-minibuffer))))
  9879. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9880. "Insert a date stamp for the date given by the internal TIME.
  9881. WITH-HM means, use the stamp format that includes the time of the day.
  9882. INACTIVE means use square brackets instead of angular ones, so that the
  9883. stamp will not contribute to the agenda.
  9884. PRE and POST are optional strings to be inserted before and after the
  9885. stamp.
  9886. The command returns the inserted time stamp."
  9887. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9888. stamp)
  9889. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9890. (insert-before-markers (or pre ""))
  9891. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9892. (when (listp extra)
  9893. (setq extra (car extra))
  9894. (if (and (stringp extra)
  9895. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9896. (setq extra (format "-%02d:%02d"
  9897. (string-to-number (match-string 1 extra))
  9898. (string-to-number (match-string 2 extra))))
  9899. (setq extra nil)))
  9900. (when extra
  9901. (backward-char 1)
  9902. (insert-before-markers extra)
  9903. (forward-char 1))
  9904. (insert-before-markers (or post ""))
  9905. stamp))
  9906. (defun org-toggle-time-stamp-overlays ()
  9907. "Toggle the use of custom time stamp formats."
  9908. (interactive)
  9909. (setq org-display-custom-times (not org-display-custom-times))
  9910. (unless org-display-custom-times
  9911. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9912. (while (setq p (next-single-property-change p 'display))
  9913. (if (and (get-text-property p 'display)
  9914. (eq (get-text-property p 'face) 'org-date))
  9915. (remove-text-properties
  9916. p (setq p (next-single-property-change p 'display))
  9917. '(display t))))
  9918. (set-buffer-modified-p bmp)))
  9919. (if (featurep 'xemacs)
  9920. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9921. (org-restart-font-lock)
  9922. (setq org-table-may-need-update t)
  9923. (if org-display-custom-times
  9924. (message "Time stamps are overlayed with custom format")
  9925. (message "Time stamp overlays removed")))
  9926. (defun org-display-custom-time (beg end)
  9927. "Overlay modified time stamp format over timestamp between BEG and END."
  9928. (let* ((ts (buffer-substring beg end))
  9929. t1 w1 with-hm tf time str w2 (off 0))
  9930. (save-match-data
  9931. (setq t1 (org-parse-time-string ts t))
  9932. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9933. (setq off (- (match-end 0) (match-beginning 0)))))
  9934. (setq end (- end off))
  9935. (setq w1 (- end beg)
  9936. with-hm (and (nth 1 t1) (nth 2 t1))
  9937. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9938. time (org-fix-decoded-time t1)
  9939. str (org-add-props
  9940. (format-time-string
  9941. (substring tf 1 -1) (apply 'encode-time time))
  9942. nil 'mouse-face 'highlight)
  9943. w2 (length str))
  9944. (if (not (= w2 w1))
  9945. (add-text-properties (1+ beg) (+ 2 beg)
  9946. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9947. (if (featurep 'xemacs)
  9948. (progn
  9949. (put-text-property beg end 'invisible t)
  9950. (put-text-property beg end 'end-glyph (make-glyph str)))
  9951. (put-text-property beg end 'display str))))
  9952. (defun org-translate-time (string)
  9953. "Translate all timestamps in STRING to custom format.
  9954. But do this only if the variable `org-display-custom-times' is set."
  9955. (when org-display-custom-times
  9956. (save-match-data
  9957. (let* ((start 0)
  9958. (re org-ts-regexp-both)
  9959. t1 with-hm inactive tf time str beg end)
  9960. (while (setq start (string-match re string start))
  9961. (setq beg (match-beginning 0)
  9962. end (match-end 0)
  9963. t1 (save-match-data
  9964. (org-parse-time-string (substring string beg end) t))
  9965. with-hm (and (nth 1 t1) (nth 2 t1))
  9966. inactive (equal (substring string beg (1+ beg)) "[")
  9967. tf (funcall (if with-hm 'cdr 'car)
  9968. org-time-stamp-custom-formats)
  9969. time (org-fix-decoded-time t1)
  9970. str (format-time-string
  9971. (concat
  9972. (if inactive "[" "<") (substring tf 1 -1)
  9973. (if inactive "]" ">"))
  9974. (apply 'encode-time time))
  9975. string (replace-match str t t string)
  9976. start (+ start (length str)))))))
  9977. string)
  9978. (defun org-fix-decoded-time (time)
  9979. "Set 0 instead of nil for the first 6 elements of time.
  9980. Don't touch the rest."
  9981. (let ((n 0))
  9982. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9983. (defun org-days-to-time (timestamp-string)
  9984. "Difference between TIMESTAMP-STRING and now in days."
  9985. (- (time-to-days (org-time-string-to-time timestamp-string))
  9986. (time-to-days (current-time))))
  9987. (defun org-deadline-close (timestamp-string &optional ndays)
  9988. "Is the time in TIMESTAMP-STRING close to the current date?"
  9989. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9990. (and (< (org-days-to-time timestamp-string) ndays)
  9991. (not (org-entry-is-done-p))))
  9992. (defun org-get-wdays (ts)
  9993. "Get the deadline lead time appropriate for timestring TS."
  9994. (cond
  9995. ((<= org-deadline-warning-days 0)
  9996. ;; 0 or negative, enforce this value no matter what
  9997. (- org-deadline-warning-days))
  9998. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9999. ;; lead time is specified.
  10000. (floor (* (string-to-number (match-string 1 ts))
  10001. (cdr (assoc (match-string 2 ts)
  10002. '(("d" . 1) ("w" . 7)
  10003. ("m" . 30.4) ("y" . 365.25)))))))
  10004. ;; go for the default.
  10005. (t org-deadline-warning-days)))
  10006. (defun org-calendar-select-mouse (ev)
  10007. "Return to `org-read-date' with the date currently selected.
  10008. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10009. (interactive "e")
  10010. (mouse-set-point ev)
  10011. (when (calendar-cursor-to-date)
  10012. (let* ((date (calendar-cursor-to-date))
  10013. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10014. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10015. (if (active-minibuffer-window) (exit-minibuffer))))
  10016. (defun org-check-deadlines (ndays)
  10017. "Check if there are any deadlines due or past due.
  10018. A deadline is considered due if it happens within `org-deadline-warning-days'
  10019. days from today's date. If the deadline appears in an entry marked DONE,
  10020. it is not shown. The prefix arg NDAYS can be used to test that many
  10021. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10022. (interactive "P")
  10023. (let* ((org-warn-days
  10024. (cond
  10025. ((equal ndays '(4)) 100000)
  10026. (ndays (prefix-numeric-value ndays))
  10027. (t (abs org-deadline-warning-days))))
  10028. (case-fold-search nil)
  10029. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10030. (callback
  10031. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10032. (message "%d deadlines past-due or due within %d days"
  10033. (org-occur regexp nil callback)
  10034. org-warn-days)))
  10035. (defun org-check-before-date (date)
  10036. "Check if there are deadlines or scheduled entries before DATE."
  10037. (interactive (list (org-read-date)))
  10038. (let ((case-fold-search nil)
  10039. (regexp (concat "\\<\\(" org-deadline-string
  10040. "\\|" org-scheduled-string
  10041. "\\) *<\\([^>]+\\)>"))
  10042. (callback
  10043. (lambda () (time-less-p
  10044. (org-time-string-to-time (match-string 2))
  10045. (org-time-string-to-time date)))))
  10046. (message "%d entries before %s"
  10047. (org-occur regexp nil callback) date)))
  10048. (defun org-evaluate-time-range (&optional to-buffer)
  10049. "Evaluate a time range by computing the difference between start and end.
  10050. Normally the result is just printed in the echo area, but with prefix arg
  10051. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10052. If the time range is actually in a table, the result is inserted into the
  10053. next column.
  10054. For time difference computation, a year is assumed to be exactly 365
  10055. days in order to avoid rounding problems."
  10056. (interactive "P")
  10057. (or
  10058. (org-clock-update-time-maybe)
  10059. (save-excursion
  10060. (unless (org-at-date-range-p t)
  10061. (goto-char (point-at-bol))
  10062. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10063. (if (not (org-at-date-range-p t))
  10064. (error "Not at a time-stamp range, and none found in current line")))
  10065. (let* ((ts1 (match-string 1))
  10066. (ts2 (match-string 2))
  10067. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10068. (match-end (match-end 0))
  10069. (time1 (org-time-string-to-time ts1))
  10070. (time2 (org-time-string-to-time ts2))
  10071. (t1 (time-to-seconds time1))
  10072. (t2 (time-to-seconds time2))
  10073. (diff (abs (- t2 t1)))
  10074. (negative (< (- t2 t1) 0))
  10075. ;; (ys (floor (* 365 24 60 60)))
  10076. (ds (* 24 60 60))
  10077. (hs (* 60 60))
  10078. (fy "%dy %dd %02d:%02d")
  10079. (fy1 "%dy %dd")
  10080. (fd "%dd %02d:%02d")
  10081. (fd1 "%dd")
  10082. (fh "%02d:%02d")
  10083. y d h m align)
  10084. (if havetime
  10085. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10086. y 0
  10087. d (floor (/ diff ds)) diff (mod diff ds)
  10088. h (floor (/ diff hs)) diff (mod diff hs)
  10089. m (floor (/ diff 60)))
  10090. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10091. y 0
  10092. d (floor (+ (/ diff ds) 0.5))
  10093. h 0 m 0))
  10094. (if (not to-buffer)
  10095. (message "%s" (org-make-tdiff-string y d h m))
  10096. (if (org-at-table-p)
  10097. (progn
  10098. (goto-char match-end)
  10099. (setq align t)
  10100. (and (looking-at " *|") (goto-char (match-end 0))))
  10101. (goto-char match-end))
  10102. (if (looking-at
  10103. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10104. (replace-match ""))
  10105. (if negative (insert " -"))
  10106. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10107. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10108. (insert " " (format fh h m))))
  10109. (if align (org-table-align))
  10110. (message "Time difference inserted")))))
  10111. (defun org-make-tdiff-string (y d h m)
  10112. (let ((fmt "")
  10113. (l nil))
  10114. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10115. l (push y l)))
  10116. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10117. l (push d l)))
  10118. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10119. l (push h l)))
  10120. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10121. l (push m l)))
  10122. (apply 'format fmt (nreverse l))))
  10123. (defun org-time-string-to-time (s)
  10124. (apply 'encode-time (org-parse-time-string s)))
  10125. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10126. "Convert a time stamp to an absolute day number.
  10127. If there is a specifyer for a cyclic time stamp, get the closest date to
  10128. DAYNR.
  10129. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10130. (cond
  10131. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10132. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10133. daynr
  10134. (+ daynr 1000)))
  10135. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10136. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10137. (time-to-days (current-time))) (match-string 0 s)
  10138. prefer show-all))
  10139. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10140. (defun org-days-to-iso-week (days)
  10141. "Return the iso week number."
  10142. (require 'cal-iso)
  10143. (car (calendar-iso-from-absolute days)))
  10144. (defun org-small-year-to-year (year)
  10145. "Convert 2-digit years into 4-digit years.
  10146. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10147. The year 2000 cannot be abbreviated. Any year lager than 99
  10148. is retrned unchanged."
  10149. (if (< year 38)
  10150. (setq year (+ 2000 year))
  10151. (if (< year 100)
  10152. (setq year (+ 1900 year))))
  10153. year)
  10154. (defun org-time-from-absolute (d)
  10155. "Return the time corresponding to date D.
  10156. D may be an absolute day number, or a calendar-type list (month day year)."
  10157. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10158. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10159. (defun org-calendar-holiday ()
  10160. "List of holidays, for Diary display in Org-mode."
  10161. (require 'holidays)
  10162. (let ((hl (funcall
  10163. (if (fboundp 'calendar-check-holidays)
  10164. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10165. (if hl (mapconcat 'identity hl "; "))))
  10166. (defun org-diary-sexp-entry (sexp entry date)
  10167. "Process a SEXP diary ENTRY for DATE."
  10168. (require 'diary-lib)
  10169. (let ((result (if calendar-debug-sexp
  10170. (let ((stack-trace-on-error t))
  10171. (eval (car (read-from-string sexp))))
  10172. (condition-case nil
  10173. (eval (car (read-from-string sexp)))
  10174. (error
  10175. (beep)
  10176. (message "Bad sexp at line %d in %s: %s"
  10177. (org-current-line)
  10178. (buffer-file-name) sexp)
  10179. (sleep-for 2))))))
  10180. (cond ((stringp result) result)
  10181. ((and (consp result)
  10182. (stringp (cdr result))) (cdr result))
  10183. (result entry)
  10184. (t nil))))
  10185. (defun org-diary-to-ical-string (frombuf)
  10186. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10187. This uses the icalendar.el library."
  10188. (let* ((tmpdir (if (featurep 'xemacs)
  10189. (temp-directory)
  10190. temporary-file-directory))
  10191. (tmpfile (make-temp-name
  10192. (expand-file-name "orgics" tmpdir)))
  10193. buf rtn b e)
  10194. (save-excursion
  10195. (set-buffer frombuf)
  10196. (icalendar-export-region (point-min) (point-max) tmpfile)
  10197. (setq buf (find-buffer-visiting tmpfile))
  10198. (set-buffer buf)
  10199. (goto-char (point-min))
  10200. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10201. (setq b (match-beginning 0)))
  10202. (goto-char (point-max))
  10203. (if (re-search-backward "^END:VEVENT" nil t)
  10204. (setq e (match-end 0)))
  10205. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10206. (kill-buffer buf)
  10207. (kill-buffer frombuf)
  10208. (delete-file tmpfile)
  10209. rtn))
  10210. (defun org-closest-date (start current change prefer show-all)
  10211. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10212. When PREFER is `past' return a date that is either CURRENT or past.
  10213. When PREFER is `future', return a date that is either CURRENT or future.
  10214. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10215. ;; Make the proper lists from the dates
  10216. (catch 'exit
  10217. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10218. dn dw sday cday n1 n2
  10219. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10220. (setq start (org-date-to-gregorian start)
  10221. current (org-date-to-gregorian
  10222. (if show-all
  10223. current
  10224. (time-to-days (current-time))))
  10225. sday (calendar-absolute-from-gregorian start)
  10226. cday (calendar-absolute-from-gregorian current))
  10227. (if (<= cday sday) (throw 'exit sday))
  10228. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10229. (setq dn (string-to-number (match-string 1 change))
  10230. dw (cdr (assoc (match-string 2 change) a1)))
  10231. (error "Invalid change specifyer: %s" change))
  10232. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10233. (cond
  10234. ((eq dw 'day)
  10235. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10236. n2 (+ n1 dn)))
  10237. ((eq dw 'year)
  10238. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10239. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10240. (setq date1 (list m d y1)
  10241. n1 (calendar-absolute-from-gregorian date1)
  10242. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10243. n2 (calendar-absolute-from-gregorian date2)))
  10244. ((eq dw 'month)
  10245. ;; approx number of month between the tow dates
  10246. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10247. ;; How often does dn fit in there?
  10248. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10249. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10250. m (+ m nm)
  10251. ny (floor (/ m 12))
  10252. y (+ y ny)
  10253. m (- m (* ny 12)))
  10254. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10255. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10256. (setq m2 (+ m dn) y2 y)
  10257. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10258. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10259. (while (< n2 cday)
  10260. (setq n1 n2 m m2 y y2)
  10261. (setq m2 (+ m dn) y2 y)
  10262. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10263. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10264. (if show-all
  10265. (cond
  10266. ((eq prefer 'past) n1)
  10267. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10268. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10269. (cond
  10270. ((eq prefer 'past) n1)
  10271. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10272. (t (if (= cday n1) n1 n2)))))))
  10273. (defun org-date-to-gregorian (date)
  10274. "Turn any specification of DATE into a gregorian date for the calendar."
  10275. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10276. ((and (listp date) (= (length date) 3)) date)
  10277. ((stringp date)
  10278. (setq date (org-parse-time-string date))
  10279. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10280. ((listp date)
  10281. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10282. (defun org-parse-time-string (s &optional nodefault)
  10283. "Parse the standard Org-mode time string.
  10284. This should be a lot faster than the normal `parse-time-string'.
  10285. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10286. hour and minute fields will be nil if not given."
  10287. (if (string-match org-ts-regexp0 s)
  10288. (list 0
  10289. (if (or (match-beginning 8) (not nodefault))
  10290. (string-to-number (or (match-string 8 s) "0")))
  10291. (if (or (match-beginning 7) (not nodefault))
  10292. (string-to-number (or (match-string 7 s) "0")))
  10293. (string-to-number (match-string 4 s))
  10294. (string-to-number (match-string 3 s))
  10295. (string-to-number (match-string 2 s))
  10296. nil nil nil)
  10297. (make-list 9 0)))
  10298. (defun org-timestamp-up (&optional arg)
  10299. "Increase the date item at the cursor by one.
  10300. If the cursor is on the year, change the year. If it is on the month or
  10301. the day, change that.
  10302. With prefix ARG, change by that many units."
  10303. (interactive "p")
  10304. (org-timestamp-change (prefix-numeric-value arg)))
  10305. (defun org-timestamp-down (&optional arg)
  10306. "Decrease the date item at the cursor by one.
  10307. If the cursor is on the year, change the year. If it is on the month or
  10308. the day, change that.
  10309. With prefix ARG, change by that many units."
  10310. (interactive "p")
  10311. (org-timestamp-change (- (prefix-numeric-value arg))))
  10312. (defun org-timestamp-up-day (&optional arg)
  10313. "Increase the date in the time stamp by one day.
  10314. With prefix ARG, change that many days."
  10315. (interactive "p")
  10316. (if (and (not (org-at-timestamp-p t))
  10317. (org-on-heading-p))
  10318. (org-todo 'up)
  10319. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10320. (defun org-timestamp-down-day (&optional arg)
  10321. "Decrease the date in the time stamp by one day.
  10322. With prefix ARG, change that many days."
  10323. (interactive "p")
  10324. (if (and (not (org-at-timestamp-p t))
  10325. (org-on-heading-p))
  10326. (org-todo 'down)
  10327. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10328. (defun org-at-timestamp-p (&optional inactive-ok)
  10329. "Determine if the cursor is in or at a timestamp."
  10330. (interactive)
  10331. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10332. (pos (point))
  10333. (ans (or (looking-at tsr)
  10334. (save-excursion
  10335. (skip-chars-backward "^[<\n\r\t")
  10336. (if (> (point) (point-min)) (backward-char 1))
  10337. (and (looking-at tsr)
  10338. (> (- (match-end 0) pos) -1))))))
  10339. (and ans
  10340. (boundp 'org-ts-what)
  10341. (setq org-ts-what
  10342. (cond
  10343. ((= pos (match-beginning 0)) 'bracket)
  10344. ((= pos (1- (match-end 0))) 'bracket)
  10345. ((org-pos-in-match-range pos 2) 'year)
  10346. ((org-pos-in-match-range pos 3) 'month)
  10347. ((org-pos-in-match-range pos 7) 'hour)
  10348. ((org-pos-in-match-range pos 8) 'minute)
  10349. ((or (org-pos-in-match-range pos 4)
  10350. (org-pos-in-match-range pos 5)) 'day)
  10351. ((and (> pos (or (match-end 8) (match-end 5)))
  10352. (< pos (match-end 0)))
  10353. (- pos (or (match-end 8) (match-end 5))))
  10354. (t 'day))))
  10355. ans))
  10356. (defun org-toggle-timestamp-type ()
  10357. "Toggle the type (<active> or [inactive]) of a time stamp."
  10358. (interactive)
  10359. (when (org-at-timestamp-p t)
  10360. (save-excursion
  10361. (goto-char (match-beginning 0))
  10362. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10363. (goto-char (1- (match-end 0)))
  10364. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10365. (message "Timestamp is now %sactive"
  10366. (if (equal (char-before) ?>) "in" ""))))
  10367. (defun org-timestamp-change (n &optional what)
  10368. "Change the date in the time stamp at point.
  10369. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10370. `year', `minute', `second'. If WHAT is not given, the cursor position
  10371. in the timestamp determines what will be changed."
  10372. (let ((pos (point))
  10373. with-hm inactive
  10374. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10375. org-ts-what
  10376. extra rem
  10377. ts time time0)
  10378. (if (not (org-at-timestamp-p t))
  10379. (error "Not at a timestamp"))
  10380. (if (and (not what) (eq org-ts-what 'bracket))
  10381. (org-toggle-timestamp-type)
  10382. (if (and (not what) (not (eq org-ts-what 'day))
  10383. org-display-custom-times
  10384. (get-text-property (point) 'display)
  10385. (not (get-text-property (1- (point)) 'display)))
  10386. (setq org-ts-what 'day))
  10387. (setq org-ts-what (or what org-ts-what)
  10388. inactive (= (char-after (match-beginning 0)) ?\[)
  10389. ts (match-string 0))
  10390. (replace-match "")
  10391. (if (string-match
  10392. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10393. ts)
  10394. (setq extra (match-string 1 ts)))
  10395. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10396. (setq with-hm t))
  10397. (setq time0 (org-parse-time-string ts))
  10398. (when (and (eq org-ts-what 'minute)
  10399. (eq current-prefix-arg nil))
  10400. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10401. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10402. (setcar (cdr time0) (+ (nth 1 time0)
  10403. (if (> n 0) (- rem) (- dm rem))))))
  10404. (setq time
  10405. (encode-time (or (car time0) 0)
  10406. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10407. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10408. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10409. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10410. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10411. (nthcdr 6 time0)))
  10412. (when (integerp org-ts-what)
  10413. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10414. (if (eq what 'calendar)
  10415. (let ((cal-date (org-get-date-from-calendar)))
  10416. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10417. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10418. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10419. (setcar time0 (or (car time0) 0))
  10420. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10421. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10422. (setq time (apply 'encode-time time0))))
  10423. (setq org-last-changed-timestamp
  10424. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10425. (org-clock-update-time-maybe)
  10426. (goto-char pos)
  10427. ;; Try to recenter the calendar window, if any
  10428. (if (and org-calendar-follow-timestamp-change
  10429. (get-buffer-window "*Calendar*" t)
  10430. (memq org-ts-what '(day month year)))
  10431. (org-recenter-calendar (time-to-days time))))))
  10432. (defun org-modify-ts-extra (s pos n dm)
  10433. "Change the different parts of the lead-time and repeat fields in timestamp."
  10434. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10435. ng h m new rem)
  10436. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10437. (cond
  10438. ((or (org-pos-in-match-range pos 2)
  10439. (org-pos-in-match-range pos 3))
  10440. (setq m (string-to-number (match-string 3 s))
  10441. h (string-to-number (match-string 2 s)))
  10442. (if (org-pos-in-match-range pos 2)
  10443. (setq h (+ h n))
  10444. (setq n (* dm (org-no-warnings (signum n))))
  10445. (when (not (= 0 (setq rem (% m dm))))
  10446. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10447. (setq m (+ m n)))
  10448. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10449. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10450. (setq h (min 24 (max 0 h)))
  10451. (setq ng 1 new (format "-%02d:%02d" h m)))
  10452. ((org-pos-in-match-range pos 6)
  10453. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10454. ((org-pos-in-match-range pos 5)
  10455. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10456. ((org-pos-in-match-range pos 9)
  10457. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10458. ((org-pos-in-match-range pos 8)
  10459. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10460. (when ng
  10461. (setq s (concat
  10462. (substring s 0 (match-beginning ng))
  10463. new
  10464. (substring s (match-end ng))))))
  10465. s))
  10466. (defun org-recenter-calendar (date)
  10467. "If the calendar is visible, recenter it to DATE."
  10468. (let* ((win (selected-window))
  10469. (cwin (get-buffer-window "*Calendar*" t))
  10470. (calendar-move-hook nil))
  10471. (when cwin
  10472. (select-window cwin)
  10473. (calendar-goto-date (if (listp date) date
  10474. (calendar-gregorian-from-absolute date)))
  10475. (select-window win))))
  10476. (defun org-goto-calendar (&optional arg)
  10477. "Go to the Emacs calendar at the current date.
  10478. If there is a time stamp in the current line, go to that date.
  10479. A prefix ARG can be used to force the current date."
  10480. (interactive "P")
  10481. (let ((tsr org-ts-regexp) diff
  10482. (calendar-move-hook nil)
  10483. (calendar-view-holidays-initially-flag nil)
  10484. (view-calendar-holidays-initially nil)
  10485. (calendar-view-diary-initially-flag nil)
  10486. (view-diary-entries-initially nil))
  10487. (if (or (org-at-timestamp-p)
  10488. (save-excursion
  10489. (beginning-of-line 1)
  10490. (looking-at (concat ".*" tsr))))
  10491. (let ((d1 (time-to-days (current-time)))
  10492. (d2 (time-to-days
  10493. (org-time-string-to-time (match-string 1)))))
  10494. (setq diff (- d2 d1))))
  10495. (calendar)
  10496. (calendar-goto-today)
  10497. (if (and diff (not arg)) (calendar-forward-day diff))))
  10498. (defun org-get-date-from-calendar ()
  10499. "Return a list (month day year) of date at point in calendar."
  10500. (with-current-buffer "*Calendar*"
  10501. (save-match-data
  10502. (calendar-cursor-to-date))))
  10503. (defun org-date-from-calendar ()
  10504. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10505. If there is already a time stamp at the cursor position, update it."
  10506. (interactive)
  10507. (if (org-at-timestamp-p t)
  10508. (org-timestamp-change 0 'calendar)
  10509. (let ((cal-date (org-get-date-from-calendar)))
  10510. (org-insert-time-stamp
  10511. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10512. (defun org-minutes-to-hh:mm-string (m)
  10513. "Compute H:MM from a number of minutes."
  10514. (let ((h (/ m 60)))
  10515. (setq m (- m (* 60 h)))
  10516. (format org-time-clocksum-format h m)))
  10517. (defun org-hh:mm-string-to-minutes (s)
  10518. "Convert a string H:MM to a number of minutes."
  10519. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10520. (+ (* (string-to-number (match-string 1 s)) 60)
  10521. (string-to-number (match-string 2 s)))
  10522. 0))
  10523. ;;;; Agenda files
  10524. ;;;###autoload
  10525. (defun org-iswitchb (&optional arg)
  10526. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10527. With a prefix argument, restrict available to files.
  10528. With two prefix arguments, restrict available buffers to agenda files.
  10529. Due to some yet unresolved reason, global function
  10530. `iswitchb-mode' needs to be active for this function to work."
  10531. (interactive "P")
  10532. (require 'iswitchb)
  10533. (let ((enabled iswitchb-mode) blist)
  10534. (or enabled (iswitchb-mode 1))
  10535. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10536. ((equal arg '(16)) (org-buffer-list 'agenda))
  10537. (t (org-buffer-list))))
  10538. (unwind-protect
  10539. (let ((iswitchb-make-buflist-hook
  10540. (lambda ()
  10541. (setq iswitchb-temp-buflist
  10542. (mapcar 'buffer-name blist)))))
  10543. (switch-to-buffer
  10544. (iswitchb-read-buffer
  10545. "Switch-to: " nil t))
  10546. (or enabled (iswitchb-mode -1))))))
  10547. (defun org-buffer-list (&optional predicate tmp)
  10548. "Return a list of Org buffers.
  10549. PREDICATE can be either 'export, 'files or 'agenda.
  10550. 'export restrict the list to Export buffers.
  10551. 'files restrict the list to buffers visiting Org files.
  10552. 'agenda restrict the list to buffers visiting agenda files.
  10553. If TMP is non-nil, don't include temporary buffers."
  10554. (let (filter blist)
  10555. (setq filter
  10556. (cond ((eq predicate 'files) "\.org$")
  10557. ((eq predicate 'export) "\*Org .*Export")
  10558. (t "\*Org \\|\.org$")))
  10559. (setq blist
  10560. (mapcar
  10561. (lambda(b)
  10562. (let ((bname (buffer-name b))
  10563. (bfile (buffer-file-name b)))
  10564. (if (and (string-match filter bname)
  10565. (if (eq predicate 'agenda)
  10566. (member bfile
  10567. (mapcar (lambda(f) (file-truename f))
  10568. org-agenda-files)) t)
  10569. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10570. (buffer-list)))
  10571. (delete nil blist)))
  10572. (defun org-agenda-files (&optional unrestricted ext)
  10573. "Get the list of agenda files.
  10574. Optional UNRESTRICTED means return the full list even if a restriction
  10575. is currently in place.
  10576. When EXT is non-nil, try to add all files that are created by adding EXT
  10577. to the file nemes. Basically, this is a way to add the archive files
  10578. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10579. a string, \"_archive\" will be used."
  10580. (let ((files
  10581. (cond
  10582. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10583. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10584. ((listp org-agenda-files) org-agenda-files)
  10585. (t (error "Invalid value of `org-agenda-files'")))))
  10586. (setq files (apply 'append
  10587. (mapcar (lambda (f)
  10588. (if (file-directory-p f)
  10589. (directory-files
  10590. f t org-agenda-file-regexp)
  10591. (list f)))
  10592. files)))
  10593. (when org-agenda-skip-unavailable-files
  10594. (setq files (delq nil
  10595. (mapcar (function
  10596. (lambda (file)
  10597. (and (file-readable-p file) file)))
  10598. files))))
  10599. (when ext
  10600. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10601. ext "_archive"))
  10602. (setq files (apply 'append
  10603. (mapcar
  10604. (lambda (f)
  10605. (if (file-exists-p (concat f ext))
  10606. (list f (concat f ext))
  10607. (list f)))
  10608. files))))
  10609. files))
  10610. (defun org-edit-agenda-file-list ()
  10611. "Edit the list of agenda files.
  10612. Depending on setup, this either uses customize to edit the variable
  10613. `org-agenda-files', or it visits the file that is holding the list. In the
  10614. latter case, the buffer is set up in a way that saving it automatically kills
  10615. the buffer and restores the previous window configuration."
  10616. (interactive)
  10617. (if (stringp org-agenda-files)
  10618. (let ((cw (current-window-configuration)))
  10619. (find-file org-agenda-files)
  10620. (org-set-local 'org-window-configuration cw)
  10621. (org-add-hook 'after-save-hook
  10622. (lambda ()
  10623. (set-window-configuration
  10624. (prog1 org-window-configuration
  10625. (kill-buffer (current-buffer))))
  10626. (org-install-agenda-files-menu)
  10627. (message "New agenda file list installed"))
  10628. nil 'local)
  10629. (message "%s" (substitute-command-keys
  10630. "Edit list and finish with \\[save-buffer]")))
  10631. (customize-variable 'org-agenda-files)))
  10632. (defun org-store-new-agenda-file-list (list)
  10633. "Set new value for the agenda file list and save it correcly."
  10634. (if (stringp org-agenda-files)
  10635. (let ((f org-agenda-files) b)
  10636. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10637. (with-temp-file f
  10638. (insert (mapconcat 'identity list "\n") "\n")))
  10639. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10640. (setq org-agenda-files list)
  10641. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10642. (defun org-read-agenda-file-list ()
  10643. "Read the list of agenda files from a file."
  10644. (when (file-directory-p org-agenda-files)
  10645. (error "`org-agenda-files' cannot be a single directory"))
  10646. (when (stringp org-agenda-files)
  10647. (with-temp-buffer
  10648. (insert-file-contents org-agenda-files)
  10649. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10650. ;;;###autoload
  10651. (defun org-cycle-agenda-files ()
  10652. "Cycle through the files in `org-agenda-files'.
  10653. If the current buffer visits an agenda file, find the next one in the list.
  10654. If the current buffer does not, find the first agenda file."
  10655. (interactive)
  10656. (let* ((fs (org-agenda-files t))
  10657. (files (append fs (list (car fs))))
  10658. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10659. file)
  10660. (unless files (error "No agenda files"))
  10661. (catch 'exit
  10662. (while (setq file (pop files))
  10663. (if (equal (file-truename file) tcf)
  10664. (when (car files)
  10665. (find-file (car files))
  10666. (throw 'exit t))))
  10667. (find-file (car fs)))
  10668. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10669. (defun org-agenda-file-to-front (&optional to-end)
  10670. "Move/add the current file to the top of the agenda file list.
  10671. If the file is not present in the list, it is added to the front. If it is
  10672. present, it is moved there. With optional argument TO-END, add/move to the
  10673. end of the list."
  10674. (interactive "P")
  10675. (let ((org-agenda-skip-unavailable-files nil)
  10676. (file-alist (mapcar (lambda (x)
  10677. (cons (file-truename x) x))
  10678. (org-agenda-files t)))
  10679. (ctf (file-truename buffer-file-name))
  10680. x had)
  10681. (setq x (assoc ctf file-alist) had x)
  10682. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10683. (if to-end
  10684. (setq file-alist (append (delq x file-alist) (list x)))
  10685. (setq file-alist (cons x (delq x file-alist))))
  10686. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10687. (org-install-agenda-files-menu)
  10688. (message "File %s to %s of agenda file list"
  10689. (if had "moved" "added") (if to-end "end" "front"))))
  10690. (defun org-remove-file (&optional file)
  10691. "Remove current file from the list of files in variable `org-agenda-files'.
  10692. These are the files which are being checked for agenda entries.
  10693. Optional argument FILE means, use this file instead of the current."
  10694. (interactive)
  10695. (let* ((org-agenda-skip-unavailable-files nil)
  10696. (file (or file buffer-file-name))
  10697. (true-file (file-truename file))
  10698. (afile (abbreviate-file-name file))
  10699. (files (delq nil (mapcar
  10700. (lambda (x)
  10701. (if (equal true-file
  10702. (file-truename x))
  10703. nil x))
  10704. (org-agenda-files t)))))
  10705. (if (not (= (length files) (length (org-agenda-files t))))
  10706. (progn
  10707. (org-store-new-agenda-file-list files)
  10708. (org-install-agenda-files-menu)
  10709. (message "Removed file: %s" afile))
  10710. (message "File was not in list: %s (not removed)" afile))))
  10711. (defun org-file-menu-entry (file)
  10712. (vector file (list 'find-file file) t))
  10713. (defun org-check-agenda-file (file)
  10714. "Make sure FILE exists. If not, ask user what to do."
  10715. (when (not (file-exists-p file))
  10716. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10717. (abbreviate-file-name file))
  10718. (let ((r (downcase (read-char-exclusive))))
  10719. (cond
  10720. ((equal r ?r)
  10721. (org-remove-file file)
  10722. (throw 'nextfile t))
  10723. (t (error "Abort"))))))
  10724. (defun org-get-agenda-file-buffer (file)
  10725. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10726. it to the list of buffers which might be released later."
  10727. (let ((buf (org-find-base-buffer-visiting file)))
  10728. (if buf
  10729. buf ; just return it
  10730. ;; Make a new buffer and remember it
  10731. (setq buf (find-file-noselect file))
  10732. (if buf (push buf org-agenda-new-buffers))
  10733. buf)))
  10734. (defun org-release-buffers (blist)
  10735. "Release all buffers in list, asking the user for confirmation when needed.
  10736. When a buffer is unmodified, it is just killed. When modified, it is saved
  10737. \(if the user agrees) and then killed."
  10738. (let (buf file)
  10739. (while (setq buf (pop blist))
  10740. (setq file (buffer-file-name buf))
  10741. (when (and (buffer-modified-p buf)
  10742. file
  10743. (y-or-n-p (format "Save file %s? " file)))
  10744. (with-current-buffer buf (save-buffer)))
  10745. (kill-buffer buf))))
  10746. (defun org-prepare-agenda-buffers (files)
  10747. "Create buffers for all agenda files, protect archived trees and comments."
  10748. (interactive)
  10749. (let ((pa '(:org-archived t))
  10750. (pc '(:org-comment t))
  10751. (pall '(:org-archived t :org-comment t))
  10752. (inhibit-read-only t)
  10753. (rea (concat ":" org-archive-tag ":"))
  10754. bmp file re)
  10755. (save-excursion
  10756. (save-restriction
  10757. (while (setq file (pop files))
  10758. (if (bufferp file)
  10759. (set-buffer file)
  10760. (org-check-agenda-file file)
  10761. (set-buffer (org-get-agenda-file-buffer file)))
  10762. (widen)
  10763. (setq bmp (buffer-modified-p))
  10764. (org-refresh-category-properties)
  10765. (setq org-todo-keywords-for-agenda
  10766. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10767. (setq org-done-keywords-for-agenda
  10768. (append org-done-keywords-for-agenda org-done-keywords))
  10769. (save-excursion
  10770. (remove-text-properties (point-min) (point-max) pall)
  10771. (when org-agenda-skip-archived-trees
  10772. (goto-char (point-min))
  10773. (while (re-search-forward rea nil t)
  10774. (if (org-on-heading-p t)
  10775. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10776. (goto-char (point-min))
  10777. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10778. (while (re-search-forward re nil t)
  10779. (add-text-properties
  10780. (match-beginning 0) (org-end-of-subtree t) pc)))
  10781. (set-buffer-modified-p bmp))))))
  10782. ;;;; Embedded LaTeX
  10783. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10784. "Keymap for the minor `org-cdlatex-mode'.")
  10785. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10786. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10787. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10788. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10789. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10790. (defvar org-cdlatex-texmathp-advice-is-done nil
  10791. "Flag remembering if we have applied the advice to texmathp already.")
  10792. (define-minor-mode org-cdlatex-mode
  10793. "Toggle the minor `org-cdlatex-mode'.
  10794. This mode supports entering LaTeX environment and math in LaTeX fragments
  10795. in Org-mode.
  10796. \\{org-cdlatex-mode-map}"
  10797. nil " OCDL" nil
  10798. (when org-cdlatex-mode (require 'cdlatex))
  10799. (unless org-cdlatex-texmathp-advice-is-done
  10800. (setq org-cdlatex-texmathp-advice-is-done t)
  10801. (defadvice texmathp (around org-math-always-on activate)
  10802. "Always return t in org-mode buffers.
  10803. This is because we want to insert math symbols without dollars even outside
  10804. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10805. en embedded LaTeX fragement, let texmathp do its job.
  10806. \\[org-cdlatex-mode-map]"
  10807. (interactive)
  10808. (let (p)
  10809. (cond
  10810. ((not (org-mode-p)) ad-do-it)
  10811. ((eq this-command 'cdlatex-math-symbol)
  10812. (setq ad-return-value t
  10813. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10814. (t
  10815. (let ((p (org-inside-LaTeX-fragment-p)))
  10816. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10817. (setq ad-return-value t
  10818. texmathp-why '("Org-mode embedded math" . 0))
  10819. (if p ad-do-it)))))))))
  10820. (defun turn-on-org-cdlatex ()
  10821. "Unconditionally turn on `org-cdlatex-mode'."
  10822. (org-cdlatex-mode 1))
  10823. (defun org-inside-LaTeX-fragment-p ()
  10824. "Test if point is inside a LaTeX fragment.
  10825. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10826. sequence appearing also before point.
  10827. Even though the matchers for math are configurable, this function assumes
  10828. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10829. delimiters are skipped when they have been removed by customization.
  10830. The return value is nil, or a cons cell with the delimiter and
  10831. and the position of this delimiter.
  10832. This function does a reasonably good job, but can locally be fooled by
  10833. for example currency specifications. For example it will assume being in
  10834. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10835. fragments that are properly closed, but during editing, we have to live
  10836. with the uncertainty caused by missing closing delimiters. This function
  10837. looks only before point, not after."
  10838. (catch 'exit
  10839. (let ((pos (point))
  10840. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10841. (lim (progn
  10842. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10843. (point)))
  10844. dd-on str (start 0) m re)
  10845. (goto-char pos)
  10846. (when dodollar
  10847. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10848. re (nth 1 (assoc "$" org-latex-regexps)))
  10849. (while (string-match re str start)
  10850. (cond
  10851. ((= (match-end 0) (length str))
  10852. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10853. ((= (match-end 0) (- (length str) 5))
  10854. (throw 'exit nil))
  10855. (t (setq start (match-end 0))))))
  10856. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10857. (goto-char pos)
  10858. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10859. (and (match-beginning 2) (throw 'exit nil))
  10860. ;; count $$
  10861. (while (re-search-backward "\\$\\$" lim t)
  10862. (setq dd-on (not dd-on)))
  10863. (goto-char pos)
  10864. (if dd-on (cons "$$" m))))))
  10865. (defun org-try-cdlatex-tab ()
  10866. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10867. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10868. - inside a LaTeX fragment, or
  10869. - after the first word in a line, where an abbreviation expansion could
  10870. insert a LaTeX environment."
  10871. (when org-cdlatex-mode
  10872. (cond
  10873. ((save-excursion
  10874. (skip-chars-backward "a-zA-Z0-9*")
  10875. (skip-chars-backward " \t")
  10876. (bolp))
  10877. (cdlatex-tab) t)
  10878. ((org-inside-LaTeX-fragment-p)
  10879. (cdlatex-tab) t)
  10880. (t nil))))
  10881. (defun org-cdlatex-underscore-caret (&optional arg)
  10882. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10883. Revert to the normal definition outside of these fragments."
  10884. (interactive "P")
  10885. (if (org-inside-LaTeX-fragment-p)
  10886. (call-interactively 'cdlatex-sub-superscript)
  10887. (let (org-cdlatex-mode)
  10888. (call-interactively (key-binding (vector last-input-event))))))
  10889. (defun org-cdlatex-math-modify (&optional arg)
  10890. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10891. Revert to the normal definition outside of these fragments."
  10892. (interactive "P")
  10893. (if (org-inside-LaTeX-fragment-p)
  10894. (call-interactively 'cdlatex-math-modify)
  10895. (let (org-cdlatex-mode)
  10896. (call-interactively (key-binding (vector last-input-event))))))
  10897. (defvar org-latex-fragment-image-overlays nil
  10898. "List of overlays carrying the images of latex fragments.")
  10899. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10900. (defun org-remove-latex-fragment-image-overlays ()
  10901. "Remove all overlays with LaTeX fragment images in current buffer."
  10902. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10903. (setq org-latex-fragment-image-overlays nil))
  10904. (defun org-preview-latex-fragment (&optional subtree)
  10905. "Preview the LaTeX fragment at point, or all locally or globally.
  10906. If the cursor is in a LaTeX fragment, create the image and overlay
  10907. it over the source code. If there is no fragment at point, display
  10908. all fragments in the current text, from one headline to the next. With
  10909. prefix SUBTREE, display all fragments in the current subtree. With a
  10910. double prefix `C-u C-u', or when the cursor is before the first headline,
  10911. display all fragments in the buffer.
  10912. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10913. (interactive "P")
  10914. (org-remove-latex-fragment-image-overlays)
  10915. (save-excursion
  10916. (save-restriction
  10917. (let (beg end at msg)
  10918. (cond
  10919. ((or (equal subtree '(16))
  10920. (not (save-excursion
  10921. (re-search-backward (concat "^" outline-regexp) nil t))))
  10922. (setq beg (point-min) end (point-max)
  10923. msg "Creating images for buffer...%s"))
  10924. ((equal subtree '(4))
  10925. (org-back-to-heading)
  10926. (setq beg (point) end (org-end-of-subtree t)
  10927. msg "Creating images for subtree...%s"))
  10928. (t
  10929. (if (setq at (org-inside-LaTeX-fragment-p))
  10930. (goto-char (max (point-min) (- (cdr at) 2)))
  10931. (org-back-to-heading))
  10932. (setq beg (point) end (progn (outline-next-heading) (point))
  10933. msg (if at "Creating image...%s"
  10934. "Creating images for entry...%s"))))
  10935. (message msg "")
  10936. (narrow-to-region beg end)
  10937. (goto-char beg)
  10938. (org-format-latex
  10939. (concat "ltxpng/" (file-name-sans-extension
  10940. (file-name-nondirectory
  10941. buffer-file-name)))
  10942. default-directory 'overlays msg at 'forbuffer)
  10943. (message msg "done. Use `C-c C-c' to remove images.")))))
  10944. (defvar org-latex-regexps
  10945. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10946. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10947. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10948. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10949. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10950. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10951. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10952. "Regular expressions for matching embedded LaTeX.")
  10953. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10954. "Replace LaTeX fragments with links to an image, and produce images."
  10955. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10956. (let* ((prefixnodir (file-name-nondirectory prefix))
  10957. (absprefix (expand-file-name prefix dir))
  10958. (todir (file-name-directory absprefix))
  10959. (opt org-format-latex-options)
  10960. (matchers (plist-get opt :matchers))
  10961. (re-list org-latex-regexps)
  10962. (cnt 0) txt link beg end re e checkdir
  10963. m n block linkfile movefile ov)
  10964. ;; Check if there are old images files with this prefix, and remove them
  10965. (when (file-directory-p todir)
  10966. (mapc 'delete-file
  10967. (directory-files
  10968. todir 'full
  10969. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10970. ;; Check the different regular expressions
  10971. (while (setq e (pop re-list))
  10972. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10973. block (if (nth 3 e) "\n\n" ""))
  10974. (when (member m matchers)
  10975. (goto-char (point-min))
  10976. (while (re-search-forward re nil t)
  10977. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10978. (setq txt (match-string n)
  10979. beg (match-beginning n) end (match-end n)
  10980. cnt (1+ cnt)
  10981. linkfile (format "%s_%04d.png" prefix cnt)
  10982. movefile (format "%s_%04d.png" absprefix cnt)
  10983. link (concat block "[[file:" linkfile "]]" block))
  10984. (if msg (message msg cnt))
  10985. (goto-char beg)
  10986. (unless checkdir ; make sure the directory exists
  10987. (setq checkdir t)
  10988. (or (file-directory-p todir) (make-directory todir)))
  10989. (org-create-formula-image
  10990. txt movefile opt forbuffer)
  10991. (if overlays
  10992. (progn
  10993. (setq ov (org-make-overlay beg end))
  10994. (if (featurep 'xemacs)
  10995. (progn
  10996. (org-overlay-put ov 'invisible t)
  10997. (org-overlay-put
  10998. ov 'end-glyph
  10999. (make-glyph (vector 'png :file movefile))))
  11000. (org-overlay-put
  11001. ov 'display
  11002. (list 'image :type 'png :file movefile :ascent 'center)))
  11003. (push ov org-latex-fragment-image-overlays)
  11004. (goto-char end))
  11005. (delete-region beg end)
  11006. (insert link))))))))
  11007. ;; This function borrows from Ganesh Swami's latex2png.el
  11008. (defun org-create-formula-image (string tofile options buffer)
  11009. (let* ((tmpdir (if (featurep 'xemacs)
  11010. (temp-directory)
  11011. temporary-file-directory))
  11012. (texfilebase (make-temp-name
  11013. (expand-file-name "orgtex" tmpdir)))
  11014. (texfile (concat texfilebase ".tex"))
  11015. (dvifile (concat texfilebase ".dvi"))
  11016. (pngfile (concat texfilebase ".png"))
  11017. (fnh (if (featurep 'xemacs)
  11018. (font-height (get-face-font 'default))
  11019. (face-attribute 'default :height nil)))
  11020. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11021. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11022. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11023. "Black"))
  11024. (bg (or (plist-get options (if buffer :background :html-background))
  11025. "Transparent")))
  11026. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11027. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11028. (with-temp-file texfile
  11029. (insert org-format-latex-header
  11030. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11031. (let ((dir default-directory))
  11032. (condition-case nil
  11033. (progn
  11034. (cd tmpdir)
  11035. (call-process "latex" nil nil nil texfile))
  11036. (error nil))
  11037. (cd dir))
  11038. (if (not (file-exists-p dvifile))
  11039. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11040. (call-process "dvipng" nil nil nil
  11041. "-E" "-fg" fg "-bg" bg
  11042. "-D" dpi
  11043. ;;"-x" scale "-y" scale
  11044. "-T" "tight"
  11045. "-o" pngfile
  11046. dvifile)
  11047. (if (not (file-exists-p pngfile))
  11048. (progn (message "Failed to create png file from %s" texfile) nil)
  11049. ;; Use the requested file name and clean up
  11050. (copy-file pngfile tofile 'replace)
  11051. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11052. (delete-file (concat texfilebase e)))
  11053. pngfile))))
  11054. (defun org-dvipng-color (attr)
  11055. "Return an rgb color specification for dvipng."
  11056. (apply 'format "rgb %s %s %s"
  11057. (mapcar 'org-normalize-color
  11058. (color-values (face-attribute 'default attr nil)))))
  11059. (defun org-normalize-color (value)
  11060. "Return string to be used as color value for an RGB component."
  11061. (format "%g" (/ value 65535.0)))
  11062. ;;;; Key bindings
  11063. ;; Make `C-c C-x' a prefix key
  11064. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11065. ;; TAB key with modifiers
  11066. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11067. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11068. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11069. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11070. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11071. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11072. ;; The following line is necessary under Suse GNU/Linux
  11073. (unless (featurep 'xemacs)
  11074. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11075. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11076. (define-key org-mode-map [backtab] 'org-shifttab)
  11077. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11078. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11079. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11080. ;; Cursor keys with modifiers
  11081. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11082. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11083. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11084. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11085. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11086. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11087. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11088. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11089. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11090. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11091. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11092. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11093. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11094. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11095. ;;; Extra keys for tty access.
  11096. ;; We only set them when really needed because otherwise the
  11097. ;; menus don't show the simple keys
  11098. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11099. (not window-system))
  11100. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11101. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11102. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11103. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11104. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11105. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11106. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11107. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11108. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11109. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11110. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11111. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11112. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11113. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11114. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11115. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11116. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11117. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11118. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11119. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11120. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11121. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11122. ;; All the other keys
  11123. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11124. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11125. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  11126. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11127. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11128. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11129. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11130. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11131. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11132. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11133. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11134. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11135. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11136. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11137. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11138. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11139. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11140. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11141. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11142. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11143. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11144. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11145. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11146. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11147. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11148. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11149. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11150. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11151. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11152. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11153. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11154. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11155. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11156. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11157. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11158. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11159. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11160. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11161. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11162. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11163. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11164. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11165. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11166. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11167. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11168. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11169. (org-defkey org-mode-map "\C-m" 'org-return)
  11170. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11171. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11172. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11173. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11174. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11175. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11176. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11177. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11178. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11179. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11180. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11181. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11182. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11183. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11184. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11185. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11186. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11187. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11188. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11189. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11190. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11191. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11192. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11193. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11194. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11195. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11196. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11197. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11198. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11199. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11200. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11201. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11202. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11203. (when (featurep 'xemacs)
  11204. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11205. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11206. (defun org-self-insert-command (N)
  11207. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11208. If the cursor is in a table looking at whitespace, the whitespace is
  11209. overwritten, and the table is not marked as requiring realignment."
  11210. (interactive "p")
  11211. (if (and (org-table-p)
  11212. (progn
  11213. ;; check if we blank the field, and if that triggers align
  11214. (and (featurep 'org-table) org-table-auto-blank-field
  11215. (member last-command
  11216. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11217. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11218. ;; got extra space, this field does not determine column width
  11219. (let (org-table-may-need-update) (org-table-blank-field))
  11220. ;; no extra space, this field may determine column width
  11221. (org-table-blank-field)))
  11222. t)
  11223. (eq N 1)
  11224. (looking-at "[^|\n]* |"))
  11225. (let (org-table-may-need-update)
  11226. (goto-char (1- (match-end 0)))
  11227. (delete-backward-char 1)
  11228. (goto-char (match-beginning 0))
  11229. (self-insert-command N))
  11230. (setq org-table-may-need-update t)
  11231. (self-insert-command N)
  11232. (org-fix-tags-on-the-fly)))
  11233. (defun org-fix-tags-on-the-fly ()
  11234. (when (and (equal (char-after (point-at-bol)) ?*)
  11235. (org-on-heading-p))
  11236. (org-align-tags-here org-tags-column)))
  11237. (defun org-delete-backward-char (N)
  11238. "Like `delete-backward-char', insert whitespace at field end in tables.
  11239. When deleting backwards, in tables this function will insert whitespace in
  11240. front of the next \"|\" separator, to keep the table aligned. The table will
  11241. still be marked for re-alignment if the field did fill the entire column,
  11242. because, in this case the deletion might narrow the column."
  11243. (interactive "p")
  11244. (if (and (org-table-p)
  11245. (eq N 1)
  11246. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11247. (looking-at ".*?|"))
  11248. (let ((pos (point))
  11249. (noalign (looking-at "[^|\n\r]* |"))
  11250. (c org-table-may-need-update))
  11251. (backward-delete-char N)
  11252. (skip-chars-forward "^|")
  11253. (insert " ")
  11254. (goto-char (1- pos))
  11255. ;; noalign: if there were two spaces at the end, this field
  11256. ;; does not determine the width of the column.
  11257. (if noalign (setq org-table-may-need-update c)))
  11258. (backward-delete-char N)
  11259. (org-fix-tags-on-the-fly)))
  11260. (defun org-delete-char (N)
  11261. "Like `delete-char', but insert whitespace at field end in tables.
  11262. When deleting characters, in tables this function will insert whitespace in
  11263. front of the next \"|\" separator, to keep the table aligned. The table will
  11264. still be marked for re-alignment if the field did fill the entire column,
  11265. because, in this case the deletion might narrow the column."
  11266. (interactive "p")
  11267. (if (and (org-table-p)
  11268. (not (bolp))
  11269. (not (= (char-after) ?|))
  11270. (eq N 1))
  11271. (if (looking-at ".*?|")
  11272. (let ((pos (point))
  11273. (noalign (looking-at "[^|\n\r]* |"))
  11274. (c org-table-may-need-update))
  11275. (replace-match (concat
  11276. (substring (match-string 0) 1 -1)
  11277. " |"))
  11278. (goto-char pos)
  11279. ;; noalign: if there were two spaces at the end, this field
  11280. ;; does not determine the width of the column.
  11281. (if noalign (setq org-table-may-need-update c)))
  11282. (delete-char N))
  11283. (delete-char N)
  11284. (org-fix-tags-on-the-fly)))
  11285. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11286. (put 'org-self-insert-command 'delete-selection t)
  11287. (put 'orgtbl-self-insert-command 'delete-selection t)
  11288. (put 'org-delete-char 'delete-selection 'supersede)
  11289. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11290. ;; Make `flyspell-mode' delay after some commands
  11291. (put 'org-self-insert-command 'flyspell-delayed t)
  11292. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11293. (put 'org-delete-char 'flyspell-delayed t)
  11294. (put 'org-delete-backward-char 'flyspell-delayed t)
  11295. ;; Make pabbrev-mode expand after org-mode commands
  11296. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11297. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11298. ;; How to do this: Measure non-white length of current string
  11299. ;; If equal to column width, we should realign.
  11300. (defun org-remap (map &rest commands)
  11301. "In MAP, remap the functions given in COMMANDS.
  11302. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11303. (let (new old)
  11304. (while commands
  11305. (setq old (pop commands) new (pop commands))
  11306. (if (fboundp 'command-remapping)
  11307. (org-defkey map (vector 'remap old) new)
  11308. (substitute-key-definition old new map global-map)))))
  11309. (when (eq org-enable-table-editor 'optimized)
  11310. ;; If the user wants maximum table support, we need to hijack
  11311. ;; some standard editing functions
  11312. (org-remap org-mode-map
  11313. 'self-insert-command 'org-self-insert-command
  11314. 'delete-char 'org-delete-char
  11315. 'delete-backward-char 'org-delete-backward-char)
  11316. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11317. (defun org-shiftcursor-error ()
  11318. "Throw an error because Shift-Cursor command was applied in wrong context."
  11319. (error "This command is active in special context like tables, headlines or timestamps"))
  11320. (defun org-shifttab (&optional arg)
  11321. "Global visibility cycling or move to previous table field.
  11322. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11323. on context.
  11324. See the individual commands for more information."
  11325. (interactive "P")
  11326. (cond
  11327. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11328. ((integerp arg)
  11329. (message "Content view to level: %d" arg)
  11330. (org-content (prefix-numeric-value arg))
  11331. (setq org-cycle-global-status 'overview))
  11332. (t (call-interactively 'org-global-cycle))))
  11333. (defun org-shiftmetaleft ()
  11334. "Promote subtree or delete table column.
  11335. Calls `org-promote-subtree', `org-outdent-item',
  11336. or `org-table-delete-column', depending on context.
  11337. See the individual commands for more information."
  11338. (interactive)
  11339. (cond
  11340. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11341. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11342. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11343. (t (org-shiftcursor-error))))
  11344. (defun org-shiftmetaright ()
  11345. "Demote subtree or insert table column.
  11346. Calls `org-demote-subtree', `org-indent-item',
  11347. or `org-table-insert-column', depending on context.
  11348. See the individual commands for more information."
  11349. (interactive)
  11350. (cond
  11351. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11352. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11353. ((org-at-item-p) (call-interactively 'org-indent-item))
  11354. (t (org-shiftcursor-error))))
  11355. (defun org-shiftmetaup (&optional arg)
  11356. "Move subtree up or kill table row.
  11357. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11358. `org-move-item-up' depending on context. See the individual commands
  11359. for more information."
  11360. (interactive "P")
  11361. (cond
  11362. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11363. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11364. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11365. (t (org-shiftcursor-error))))
  11366. (defun org-shiftmetadown (&optional arg)
  11367. "Move subtree down or insert table row.
  11368. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11369. `org-move-item-down', depending on context. See the individual
  11370. commands for more information."
  11371. (interactive "P")
  11372. (cond
  11373. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11374. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11375. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11376. (t (org-shiftcursor-error))))
  11377. (defun org-metaleft (&optional arg)
  11378. "Promote heading or move table column to left.
  11379. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11380. With no specific context, calls the Emacs default `backward-word'.
  11381. See the individual commands for more information."
  11382. (interactive "P")
  11383. (cond
  11384. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11385. ((or (org-on-heading-p) (org-region-active-p))
  11386. (call-interactively 'org-do-promote))
  11387. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11388. (t (call-interactively 'backward-word))))
  11389. (defun org-metaright (&optional arg)
  11390. "Demote subtree or move table column to right.
  11391. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11392. With no specific context, calls the Emacs default `forward-word'.
  11393. See the individual commands for more information."
  11394. (interactive "P")
  11395. (cond
  11396. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11397. ((or (org-on-heading-p) (org-region-active-p))
  11398. (call-interactively 'org-do-demote))
  11399. ((org-at-item-p) (call-interactively 'org-indent-item))
  11400. (t (call-interactively 'forward-word))))
  11401. (defun org-metaup (&optional arg)
  11402. "Move subtree up or move table row up.
  11403. Calls `org-move-subtree-up' or `org-table-move-row' or
  11404. `org-move-item-up', depending on context. See the individual commands
  11405. for more information."
  11406. (interactive "P")
  11407. (cond
  11408. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11409. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11410. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11411. (t (transpose-lines 1) (beginning-of-line -1))))
  11412. (defun org-metadown (&optional arg)
  11413. "Move subtree down or move table row down.
  11414. Calls `org-move-subtree-down' or `org-table-move-row' or
  11415. `org-move-item-down', depending on context. See the individual
  11416. commands for more information."
  11417. (interactive "P")
  11418. (cond
  11419. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11420. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11421. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11422. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11423. (defun org-shiftup (&optional arg)
  11424. "Increase item in timestamp or increase priority of current headline.
  11425. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11426. depending on context. See the individual commands for more information."
  11427. (interactive "P")
  11428. (cond
  11429. ((org-at-timestamp-p t)
  11430. (call-interactively (if org-edit-timestamp-down-means-later
  11431. 'org-timestamp-down 'org-timestamp-up)))
  11432. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11433. ((org-at-item-p) (call-interactively 'org-previous-item))
  11434. ((org-clocktable-try-shift 'up arg))
  11435. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11436. (defun org-shiftdown (&optional arg)
  11437. "Decrease item in timestamp or decrease priority of current headline.
  11438. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11439. depending on context. See the individual commands for more information."
  11440. (interactive "P")
  11441. (cond
  11442. ((org-at-timestamp-p t)
  11443. (call-interactively (if org-edit-timestamp-down-means-later
  11444. 'org-timestamp-up 'org-timestamp-down)))
  11445. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11446. ((org-clocktable-try-shift 'down arg))
  11447. (t (call-interactively 'org-next-item))))
  11448. (defun org-shiftright (&optional arg)
  11449. "Next TODO keyword or timestamp one day later, depending on context."
  11450. (interactive "P")
  11451. (cond
  11452. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11453. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11454. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11455. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11456. ((org-clocktable-try-shift 'right arg))
  11457. (t (org-shiftcursor-error))))
  11458. (defun org-shiftleft (&optional arg)
  11459. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11460. (interactive "P")
  11461. (cond
  11462. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11463. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11464. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11465. ((org-at-property-p)
  11466. (call-interactively 'org-property-previous-allowed-value))
  11467. ((org-clocktable-try-shift 'left arg))
  11468. (t (org-shiftcursor-error))))
  11469. (defun org-shiftcontrolright ()
  11470. "Switch to next TODO set."
  11471. (interactive)
  11472. (cond
  11473. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11474. (t (org-shiftcursor-error))))
  11475. (defun org-shiftcontrolleft ()
  11476. "Switch to previous TODO set."
  11477. (interactive)
  11478. (cond
  11479. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11480. (t (org-shiftcursor-error))))
  11481. (defun org-ctrl-c-ret ()
  11482. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11483. (interactive)
  11484. (cond
  11485. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11486. (t (call-interactively 'org-insert-heading))))
  11487. (defun org-copy-special ()
  11488. "Copy region in table or copy current subtree.
  11489. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11490. See the individual commands for more information."
  11491. (interactive)
  11492. (call-interactively
  11493. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11494. (defun org-cut-special ()
  11495. "Cut region in table or cut current subtree.
  11496. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11497. See the individual commands for more information."
  11498. (interactive)
  11499. (call-interactively
  11500. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11501. (defun org-paste-special (arg)
  11502. "Paste rectangular region into table, or past subtree relative to level.
  11503. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11504. See the individual commands for more information."
  11505. (interactive "P")
  11506. (if (org-at-table-p)
  11507. (org-table-paste-rectangle)
  11508. (org-paste-subtree arg)))
  11509. (defun org-ctrl-c-ctrl-c (&optional arg)
  11510. "Set tags in headline, or update according to changed information at point.
  11511. This command does many different things, depending on context:
  11512. - If the cursor is in a headline, prompt for tags and insert them
  11513. into the current line, aligned to `org-tags-column'. When called
  11514. with prefix arg, realign all tags in the current buffer.
  11515. - If the cursor is in one of the special #+KEYWORD lines, this
  11516. triggers scanning the buffer for these lines and updating the
  11517. information.
  11518. - If the cursor is inside a table, realign the table. This command
  11519. works even if the automatic table editor has been turned off.
  11520. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11521. the entire table.
  11522. - If the cursor is a the beginning of a dynamic block, update it.
  11523. - If the cursor is inside a table created by the table.el package,
  11524. activate that table.
  11525. - If the current buffer is a remember buffer, close note and file it.
  11526. with a prefix argument, file it without further interaction to the default
  11527. location.
  11528. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11529. links in this buffer.
  11530. - If the cursor is on a numbered item in a plain list, renumber the
  11531. ordered list.
  11532. - If the cursor is on a checkbox, toggle it."
  11533. (interactive "P")
  11534. (let ((org-enable-table-editor t))
  11535. (cond
  11536. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11537. org-occur-highlights
  11538. org-latex-fragment-image-overlays)
  11539. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11540. (org-remove-occur-highlights)
  11541. (org-remove-latex-fragment-image-overlays)
  11542. (message "Temporary highlights/overlays removed from current buffer"))
  11543. ((and (local-variable-p 'org-finish-function (current-buffer))
  11544. (fboundp org-finish-function))
  11545. (funcall org-finish-function))
  11546. ((org-at-property-p)
  11547. (call-interactively 'org-property-action))
  11548. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11549. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11550. ((org-at-table.el-p)
  11551. (require 'table)
  11552. (beginning-of-line 1)
  11553. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11554. (call-interactively 'table-recognize-table))
  11555. ((org-at-table-p)
  11556. (org-table-maybe-eval-formula)
  11557. (if arg
  11558. (call-interactively 'org-table-recalculate)
  11559. (org-table-maybe-recalculate-line))
  11560. (call-interactively 'org-table-align))
  11561. ((org-at-item-checkbox-p)
  11562. (call-interactively 'org-toggle-checkbox))
  11563. ((org-at-item-p)
  11564. (call-interactively 'org-maybe-renumber-ordered-list))
  11565. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11566. ;; Dynamic block
  11567. (beginning-of-line 1)
  11568. (org-update-dblock))
  11569. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11570. (cond
  11571. ((equal (match-string 1) "TBLFM")
  11572. ;; Recalculate the table before this line
  11573. (save-excursion
  11574. (beginning-of-line 1)
  11575. (skip-chars-backward " \r\n\t")
  11576. (if (org-at-table-p)
  11577. (org-call-with-arg 'org-table-recalculate t))))
  11578. (t
  11579. ; (org-set-regexps-and-options)
  11580. ; (org-restart-font-lock)
  11581. (let ((org-inhibit-startup t)) (org-mode-restart))
  11582. (message "Local setup has been refreshed"))))
  11583. (t (error "C-c C-c can do nothing useful at this location.")))))
  11584. (defun org-mode-restart ()
  11585. "Restart Org-mode, to scan again for special lines.
  11586. Also updates the keyword regular expressions."
  11587. (interactive)
  11588. (org-mode)
  11589. (message "Org-mode restarted"))
  11590. (defun org-kill-note-or-show-branches ()
  11591. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11592. (interactive)
  11593. (if (not org-finish-function)
  11594. (call-interactively 'show-branches)
  11595. (let ((org-note-abort t))
  11596. (funcall org-finish-function))))
  11597. (defun org-return (&optional indent)
  11598. "Goto next table row or insert a newline.
  11599. Calls `org-table-next-row' or `newline', depending on context.
  11600. See the individual commands for more information."
  11601. (interactive)
  11602. (cond
  11603. ((bobp) (if indent (newline-and-indent) (newline)))
  11604. ((and (org-at-heading-p)
  11605. (looking-at
  11606. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11607. (org-show-entry)
  11608. (end-of-line 1)
  11609. (newline))
  11610. ((org-at-table-p)
  11611. (org-table-justify-field-maybe)
  11612. (call-interactively 'org-table-next-row))
  11613. (t (if indent (newline-and-indent) (newline)))))
  11614. (defun org-return-indent ()
  11615. "Goto next table row or insert a newline and indent.
  11616. Calls `org-table-next-row' or `newline-and-indent', depending on
  11617. context. See the individual commands for more information."
  11618. (interactive)
  11619. (org-return t))
  11620. (defun org-ctrl-c-star ()
  11621. "Compute table, or change heading status of lines.
  11622. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11623. depending on context. This will also turn a plain list item or a normal
  11624. line into a subheading."
  11625. (interactive)
  11626. (cond
  11627. ((org-at-table-p)
  11628. (call-interactively 'org-table-recalculate))
  11629. ((org-region-active-p)
  11630. ;; Convert all lines in region to list items
  11631. (call-interactively 'org-toggle-region-headings))
  11632. ((org-on-heading-p)
  11633. (org-toggle-region-headings (point-at-bol)
  11634. (min (1+ (point-at-eol)) (point-max))))
  11635. ((org-at-item-p)
  11636. ;; Convert to heading
  11637. (let ((level (save-match-data
  11638. (save-excursion
  11639. (condition-case nil
  11640. (progn
  11641. (org-back-to-heading t)
  11642. (funcall outline-level))
  11643. (error 0))))))
  11644. (replace-match
  11645. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11646. (t (org-toggle-region-headings (point-at-bol)
  11647. (min (1+ (point-at-eol)) (point-max))))))
  11648. (defun org-ctrl-c-minus ()
  11649. "Insert separator line in table or modify bullet status of line.
  11650. Also turns a plain line or a region of lines into list items.
  11651. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11652. `org-cycle-list-bullet', depending on context."
  11653. (interactive)
  11654. (cond
  11655. ((org-at-table-p)
  11656. (call-interactively 'org-table-insert-hline))
  11657. ((org-on-heading-p)
  11658. ;; Convert to item
  11659. (save-excursion
  11660. (beginning-of-line 1)
  11661. (if (looking-at "\\*+ ")
  11662. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11663. ((org-region-active-p)
  11664. ;; Convert all lines in region to list items
  11665. (call-interactively 'org-toggle-region-items))
  11666. ((org-in-item-p)
  11667. (call-interactively 'org-cycle-list-bullet))
  11668. (t (org-toggle-region-items (point-at-bol)
  11669. (min (1+ (point-at-eol)) (point-max))))))
  11670. (defun org-toggle-region-items (beg end)
  11671. "Convert all lines in region to list items.
  11672. If the first line is already an item, convert all list items in the region
  11673. to normal lines."
  11674. (interactive "r")
  11675. (let (l2 l)
  11676. (save-excursion
  11677. (goto-char end)
  11678. (setq l2 (org-current-line))
  11679. (goto-char beg)
  11680. (beginning-of-line 1)
  11681. (setq l (1- (org-current-line)))
  11682. (if (org-at-item-p)
  11683. ;; We already have items, de-itemize
  11684. (while (< (setq l (1+ l)) l2)
  11685. (when (org-at-item-p)
  11686. (goto-char (match-beginning 2))
  11687. (delete-region (match-beginning 2) (match-end 2))
  11688. (and (looking-at "[ \t]+") (replace-match "")))
  11689. (beginning-of-line 2))
  11690. (while (< (setq l (1+ l)) l2)
  11691. (unless (org-at-item-p)
  11692. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11693. (replace-match "\\1- \\2")))
  11694. (beginning-of-line 2))))))
  11695. (defun org-toggle-region-headings (beg end)
  11696. "Convert all lines in region to list items.
  11697. If the first line is already an item, convert all list items in the region
  11698. to normal lines."
  11699. (interactive "r")
  11700. (let (l2 l)
  11701. (save-excursion
  11702. (goto-char end)
  11703. (setq l2 (org-current-line))
  11704. (goto-char beg)
  11705. (beginning-of-line 1)
  11706. (setq l (1- (org-current-line)))
  11707. (if (org-on-heading-p)
  11708. ;; We already have headlines, de-star them
  11709. (while (< (setq l (1+ l)) l2)
  11710. (when (org-on-heading-p t)
  11711. (and (looking-at outline-regexp) (replace-match "")))
  11712. (beginning-of-line 2))
  11713. (let* ((stars (save-excursion
  11714. (re-search-backward org-complex-heading-regexp nil t)
  11715. (or (match-string 1) "*")))
  11716. (add-stars (if org-odd-levels-only "**" "*"))
  11717. (rpl (concat stars add-stars " \\2")))
  11718. (while (< (setq l (1+ l)) l2)
  11719. (unless (org-on-heading-p)
  11720. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11721. (replace-match rpl)))
  11722. (beginning-of-line 2)))))))
  11723. (defun org-meta-return (&optional arg)
  11724. "Insert a new heading or wrap a region in a table.
  11725. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11726. See the individual commands for more information."
  11727. (interactive "P")
  11728. (cond
  11729. ((org-at-table-p)
  11730. (call-interactively 'org-table-wrap-region))
  11731. (t (call-interactively 'org-insert-heading))))
  11732. ;;; Menu entries
  11733. ;; Define the Org-mode menus
  11734. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11735. '("Tbl"
  11736. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11737. ["Next Field" org-cycle (org-at-table-p)]
  11738. ["Previous Field" org-shifttab (org-at-table-p)]
  11739. ["Next Row" org-return (org-at-table-p)]
  11740. "--"
  11741. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11742. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11743. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11744. "--"
  11745. ("Column"
  11746. ["Move Column Left" org-metaleft (org-at-table-p)]
  11747. ["Move Column Right" org-metaright (org-at-table-p)]
  11748. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11749. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11750. ("Row"
  11751. ["Move Row Up" org-metaup (org-at-table-p)]
  11752. ["Move Row Down" org-metadown (org-at-table-p)]
  11753. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11754. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11755. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11756. "--"
  11757. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11758. ("Rectangle"
  11759. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11760. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11761. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11762. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11763. "--"
  11764. ("Calculate"
  11765. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11766. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11767. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11768. "--"
  11769. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11770. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11771. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11772. "--"
  11773. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11774. "--"
  11775. ["Sum Column/Rectangle" org-table-sum
  11776. (or (org-at-table-p) (org-region-active-p))]
  11777. ["Which Column?" org-table-current-column (org-at-table-p)])
  11778. ["Debug Formulas"
  11779. org-table-toggle-formula-debugger
  11780. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11781. ["Show Col/Row Numbers"
  11782. org-table-toggle-coordinate-overlays
  11783. :style toggle
  11784. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11785. "--"
  11786. ["Create" org-table-create (and (not (org-at-table-p))
  11787. org-enable-table-editor)]
  11788. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11789. ["Import from File" org-table-import (not (org-at-table-p))]
  11790. ["Export to File" org-table-export (org-at-table-p)]
  11791. "--"
  11792. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11793. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11794. '("Org"
  11795. ("Show/Hide"
  11796. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11797. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11798. ["Sparse Tree..." org-sparse-tree t]
  11799. ["Reveal Context" org-reveal t]
  11800. ["Show All" show-all t]
  11801. "--"
  11802. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11803. "--"
  11804. ["New Heading" org-insert-heading t]
  11805. ("Navigate Headings"
  11806. ["Up" outline-up-heading t]
  11807. ["Next" outline-next-visible-heading t]
  11808. ["Previous" outline-previous-visible-heading t]
  11809. ["Next Same Level" outline-forward-same-level t]
  11810. ["Previous Same Level" outline-backward-same-level t]
  11811. "--"
  11812. ["Jump" org-goto t])
  11813. ("Edit Structure"
  11814. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11815. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11816. "--"
  11817. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11818. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11819. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11820. "--"
  11821. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11822. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11823. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11824. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11825. "--"
  11826. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11827. "--"
  11828. ["Convert to odd levels" org-convert-to-odd-levels t]
  11829. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11830. ("Editing"
  11831. ["Emphasis..." org-emphasize t])
  11832. ("Archive"
  11833. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11834. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11835. ; :active t :keys "C-u C-c C-x C-a"]
  11836. ["Sparse trees open ARCHIVE trees"
  11837. (setq org-sparse-tree-open-archived-trees
  11838. (not org-sparse-tree-open-archived-trees))
  11839. :style toggle :selected org-sparse-tree-open-archived-trees]
  11840. ["Cycling opens ARCHIVE trees"
  11841. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11842. :style toggle :selected org-cycle-open-archived-trees]
  11843. ["Agenda includes ARCHIVE trees"
  11844. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11845. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11846. "--"
  11847. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11848. ; ["Check and Move Children" (org-archive-subtree '(4))
  11849. ; :active t :keys "C-u C-c C-x C-s"]
  11850. )
  11851. "--"
  11852. ("TODO Lists"
  11853. ["TODO/DONE/-" org-todo t]
  11854. ("Select keyword"
  11855. ["Next keyword" org-shiftright (org-on-heading-p)]
  11856. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11857. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11858. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11859. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11860. ["Show TODO Tree" org-show-todo-tree t]
  11861. ["Global TODO list" org-todo-list t]
  11862. "--"
  11863. ["Set Priority" org-priority t]
  11864. ["Priority Up" org-shiftup t]
  11865. ["Priority Down" org-shiftdown t])
  11866. ("TAGS and Properties"
  11867. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11868. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11869. "--"
  11870. ["Set property" 'org-set-property t]
  11871. ["Column view of properties" org-columns t]
  11872. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11873. ("Dates and Scheduling"
  11874. ["Timestamp" org-time-stamp t]
  11875. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11876. ("Change Date"
  11877. ["1 Day Later" org-shiftright t]
  11878. ["1 Day Earlier" org-shiftleft t]
  11879. ["1 ... Later" org-shiftup t]
  11880. ["1 ... Earlier" org-shiftdown t])
  11881. ["Compute Time Range" org-evaluate-time-range t]
  11882. ["Schedule Item" org-schedule t]
  11883. ["Deadline" org-deadline t]
  11884. "--"
  11885. ["Custom time format" org-toggle-time-stamp-overlays
  11886. :style radio :selected org-display-custom-times]
  11887. "--"
  11888. ["Goto Calendar" org-goto-calendar t]
  11889. ["Date from Calendar" org-date-from-calendar t])
  11890. ("Logging work"
  11891. ["Clock in" org-clock-in t]
  11892. ["Clock out" org-clock-out t]
  11893. ["Clock cancel" org-clock-cancel t]
  11894. ["Goto running clock" org-clock-goto t]
  11895. ["Display times" org-clock-display t]
  11896. ["Create clock table" org-clock-report t]
  11897. "--"
  11898. ["Record DONE time"
  11899. (progn (setq org-log-done (not org-log-done))
  11900. (message "Switching to %s will %s record a timestamp"
  11901. (car org-done-keywords)
  11902. (if org-log-done "automatically" "not")))
  11903. :style toggle :selected org-log-done])
  11904. "--"
  11905. ["Agenda Command..." org-agenda t]
  11906. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11907. ("File List for Agenda")
  11908. ("Special views current file"
  11909. ["TODO Tree" org-show-todo-tree t]
  11910. ["Check Deadlines" org-check-deadlines t]
  11911. ["Timeline" org-timeline t]
  11912. ["Tags Tree" org-tags-sparse-tree t])
  11913. "--"
  11914. ("Hyperlinks"
  11915. ["Store Link (Global)" org-store-link t]
  11916. ["Insert Link" org-insert-link t]
  11917. ["Follow Link" org-open-at-point t]
  11918. "--"
  11919. ["Next link" org-next-link t]
  11920. ["Previous link" org-previous-link t]
  11921. "--"
  11922. ["Descriptive Links"
  11923. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11924. :style radio
  11925. :selected (member '(org-link) buffer-invisibility-spec)]
  11926. ["Literal Links"
  11927. (progn
  11928. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11929. :style radio
  11930. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11931. "--"
  11932. ["Export/Publish..." org-export t]
  11933. ("LaTeX"
  11934. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11935. :selected org-cdlatex-mode]
  11936. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11937. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11938. ["Modify math symbol" org-cdlatex-math-modify
  11939. (org-inside-LaTeX-fragment-p)]
  11940. ["Export LaTeX fragments as images"
  11941. (if (featurep 'org-exp)
  11942. (setq org-export-with-LaTeX-fragments
  11943. (not org-export-with-LaTeX-fragments))
  11944. (require 'org-exp))
  11945. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11946. org-export-with-LaTeX-fragments)])
  11947. "--"
  11948. ("Documentation"
  11949. ["Show Version" org-version t]
  11950. ["Info Documentation" org-info t])
  11951. ("Customize"
  11952. ["Browse Org Group" org-customize t]
  11953. "--"
  11954. ["Expand This Menu" org-create-customize-menu
  11955. (fboundp 'customize-menu-create)])
  11956. "--"
  11957. ["Refresh setup" org-mode-restart t]
  11958. ))
  11959. (defun org-info (&optional node)
  11960. "Read documentation for Org-mode in the info system.
  11961. With optional NODE, go directly to that node."
  11962. (interactive)
  11963. (info (format "(org)%s" (or node ""))))
  11964. (defun org-install-agenda-files-menu ()
  11965. (let ((bl (buffer-list)))
  11966. (save-excursion
  11967. (while bl
  11968. (set-buffer (pop bl))
  11969. (if (org-mode-p) (setq bl nil)))
  11970. (when (org-mode-p)
  11971. (easy-menu-change
  11972. '("Org") "File List for Agenda"
  11973. (append
  11974. (list
  11975. ["Edit File List" (org-edit-agenda-file-list) t]
  11976. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11977. ["Remove Current File from List" org-remove-file t]
  11978. ["Cycle through agenda files" org-cycle-agenda-files t]
  11979. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11980. "--")
  11981. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11982. ;;;; Documentation
  11983. (defun org-require-autoloaded-modules ()
  11984. (interactive)
  11985. (mapc 'require
  11986. '(org-agenda org-archive org-clock org-colview
  11987. org-exp org-export-latex org-publish
  11988. org-remember org-table)))
  11989. (defun org-customize ()
  11990. "Call the customize function with org as argument."
  11991. (interactive)
  11992. (org-load-modules-maybe)
  11993. (org-require-autoloaded-modules)
  11994. (customize-browse 'org))
  11995. (defun org-create-customize-menu ()
  11996. "Create a full customization menu for Org-mode, insert it into the menu."
  11997. (interactive)
  11998. (org-load-modules-maybe)
  11999. (org-require-autoloaded-modules)
  12000. (if (fboundp 'customize-menu-create)
  12001. (progn
  12002. (easy-menu-change
  12003. '("Org") "Customize"
  12004. `(["Browse Org group" org-customize t]
  12005. "--"
  12006. ,(customize-menu-create 'org)
  12007. ["Set" Custom-set t]
  12008. ["Save" Custom-save t]
  12009. ["Reset to Current" Custom-reset-current t]
  12010. ["Reset to Saved" Custom-reset-saved t]
  12011. ["Reset to Standard Settings" Custom-reset-standard t]))
  12012. (message "\"Org\"-menu now contains full customization menu"))
  12013. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12014. ;;;; Miscellaneous stuff
  12015. ;;; Generally useful functions
  12016. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12017. "Display the given MESSAGE as a warning."
  12018. (if (fboundp 'display-warning)
  12019. (display-warning 'org message
  12020. (if (featurep 'xemacs)
  12021. 'warning
  12022. :warning))
  12023. (let ((buf (get-buffer-create "*Org warnings*")))
  12024. (with-current-buffer buf
  12025. (goto-char (point-max))
  12026. (insert "Warning (Org): " message)
  12027. (unless (bolp)
  12028. (newline)))
  12029. (display-buffer buf)
  12030. (sit-for 0))))
  12031. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12032. "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
  12033. (if (and marker (marker-buffer marker)
  12034. (buffer-live-p (marker-buffer marker)))
  12035. (progn
  12036. (switch-to-buffer (marker-buffer marker))
  12037. (if (or (> marker (point-max)) (< marker (point-min)))
  12038. (widen))
  12039. (goto-char marker))
  12040. (if bookmark
  12041. (bookmark-jump bookmark)
  12042. (error "Cannot find location"))))
  12043. (defun org-quote-csv-field (s)
  12044. "Quote field for inclusion in CSV material."
  12045. (if (string-match "[\",]" s)
  12046. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12047. s))
  12048. (defun org-plist-delete (plist property)
  12049. "Delete PROPERTY from PLIST.
  12050. This is in contrast to merely setting it to 0."
  12051. (let (p)
  12052. (while plist
  12053. (if (not (eq property (car plist)))
  12054. (setq p (plist-put p (car plist) (nth 1 plist))))
  12055. (setq plist (cddr plist)))
  12056. p))
  12057. (defun org-force-self-insert (N)
  12058. "Needed to enforce self-insert under remapping."
  12059. (interactive "p")
  12060. (self-insert-command N))
  12061. (defun org-string-width (s)
  12062. "Compute width of string, ignoring invisible characters.
  12063. This ignores character with invisibility property `org-link', and also
  12064. characters with property `org-cwidth', because these will become invisible
  12065. upon the next fontification round."
  12066. (let (b l)
  12067. (when (or (eq t buffer-invisibility-spec)
  12068. (assq 'org-link buffer-invisibility-spec))
  12069. (while (setq b (text-property-any 0 (length s)
  12070. 'invisible 'org-link s))
  12071. (setq s (concat (substring s 0 b)
  12072. (substring s (or (next-single-property-change
  12073. b 'invisible s) (length s)))))))
  12074. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12075. (setq s (concat (substring s 0 b)
  12076. (substring s (or (next-single-property-change
  12077. b 'org-cwidth s) (length s))))))
  12078. (setq l (string-width s) b -1)
  12079. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12080. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12081. l))
  12082. (defun org-base-buffer (buffer)
  12083. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12084. (if (not buffer)
  12085. buffer
  12086. (or (buffer-base-buffer buffer)
  12087. buffer)))
  12088. (defun org-trim (s)
  12089. "Remove whitespace at beginning and end of string."
  12090. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12091. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12092. s)
  12093. (defun org-wrap (string &optional width lines)
  12094. "Wrap string to either a number of lines, or a width in characters.
  12095. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12096. that costs. If there is a word longer than WIDTH, the text is actually
  12097. wrapped to the length of that word.
  12098. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12099. many lines, whatever width that takes.
  12100. The return value is a list of lines, without newlines at the end."
  12101. (let* ((words (org-split-string string "[ \t\n]+"))
  12102. (maxword (apply 'max (mapcar 'org-string-width words)))
  12103. w ll)
  12104. (cond (width
  12105. (org-do-wrap words (max maxword width)))
  12106. (lines
  12107. (setq w maxword)
  12108. (setq ll (org-do-wrap words maxword))
  12109. (if (<= (length ll) lines)
  12110. ll
  12111. (setq ll words)
  12112. (while (> (length ll) lines)
  12113. (setq w (1+ w))
  12114. (setq ll (org-do-wrap words w)))
  12115. ll))
  12116. (t (error "Cannot wrap this")))))
  12117. (defun org-do-wrap (words width)
  12118. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12119. (let (lines line)
  12120. (while words
  12121. (setq line (pop words))
  12122. (while (and words (< (+ (length line) (length (car words))) width))
  12123. (setq line (concat line " " (pop words))))
  12124. (setq lines (push line lines)))
  12125. (nreverse lines)))
  12126. (defun org-split-string (string &optional separators)
  12127. "Splits STRING into substrings at SEPARATORS.
  12128. No empty strings are returned if there are matches at the beginning
  12129. and end of string."
  12130. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12131. (start 0)
  12132. notfirst
  12133. (list nil))
  12134. (while (and (string-match rexp string
  12135. (if (and notfirst
  12136. (= start (match-beginning 0))
  12137. (< start (length string)))
  12138. (1+ start) start))
  12139. (< (match-beginning 0) (length string)))
  12140. (setq notfirst t)
  12141. (or (eq (match-beginning 0) 0)
  12142. (and (eq (match-beginning 0) (match-end 0))
  12143. (eq (match-beginning 0) start))
  12144. (setq list
  12145. (cons (substring string start (match-beginning 0))
  12146. list)))
  12147. (setq start (match-end 0)))
  12148. (or (eq start (length string))
  12149. (setq list
  12150. (cons (substring string start)
  12151. list)))
  12152. (nreverse list)))
  12153. (defun org-context ()
  12154. "Return a list of contexts of the current cursor position.
  12155. If several contexts apply, all are returned.
  12156. Each context entry is a list with a symbol naming the context, and
  12157. two positions indicating start and end of the context. Possible
  12158. contexts are:
  12159. :headline anywhere in a headline
  12160. :headline-stars on the leading stars in a headline
  12161. :todo-keyword on a TODO keyword (including DONE) in a headline
  12162. :tags on the TAGS in a headline
  12163. :priority on the priority cookie in a headline
  12164. :item on the first line of a plain list item
  12165. :item-bullet on the bullet/number of a plain list item
  12166. :checkbox on the checkbox in a plain list item
  12167. :table in an org-mode table
  12168. :table-special on a special filed in a table
  12169. :table-table in a table.el table
  12170. :link on a hyperlink
  12171. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12172. :target on a <<target>>
  12173. :radio-target on a <<<radio-target>>>
  12174. :latex-fragment on a LaTeX fragment
  12175. :latex-preview on a LaTeX fragment with overlayed preview image
  12176. This function expects the position to be visible because it uses font-lock
  12177. faces as a help to recognize the following contexts: :table-special, :link,
  12178. and :keyword."
  12179. (let* ((f (get-text-property (point) 'face))
  12180. (faces (if (listp f) f (list f)))
  12181. (p (point)) clist o)
  12182. ;; First the large context
  12183. (cond
  12184. ((org-on-heading-p t)
  12185. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12186. (when (progn
  12187. (beginning-of-line 1)
  12188. (looking-at org-todo-line-tags-regexp))
  12189. (push (org-point-in-group p 1 :headline-stars) clist)
  12190. (push (org-point-in-group p 2 :todo-keyword) clist)
  12191. (push (org-point-in-group p 4 :tags) clist))
  12192. (goto-char p)
  12193. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12194. (if (looking-at "\\[#[A-Z0-9]\\]")
  12195. (push (org-point-in-group p 0 :priority) clist)))
  12196. ((org-at-item-p)
  12197. (push (org-point-in-group p 2 :item-bullet) clist)
  12198. (push (list :item (point-at-bol)
  12199. (save-excursion (org-end-of-item) (point)))
  12200. clist)
  12201. (and (org-at-item-checkbox-p)
  12202. (push (org-point-in-group p 0 :checkbox) clist)))
  12203. ((org-at-table-p)
  12204. (push (list :table (org-table-begin) (org-table-end)) clist)
  12205. (if (memq 'org-formula faces)
  12206. (push (list :table-special
  12207. (previous-single-property-change p 'face)
  12208. (next-single-property-change p 'face)) clist)))
  12209. ((org-at-table-p 'any)
  12210. (push (list :table-table) clist)))
  12211. (goto-char p)
  12212. ;; Now the small context
  12213. (cond
  12214. ((org-at-timestamp-p)
  12215. (push (org-point-in-group p 0 :timestamp) clist))
  12216. ((memq 'org-link faces)
  12217. (push (list :link
  12218. (previous-single-property-change p 'face)
  12219. (next-single-property-change p 'face)) clist))
  12220. ((memq 'org-special-keyword faces)
  12221. (push (list :keyword
  12222. (previous-single-property-change p 'face)
  12223. (next-single-property-change p 'face)) clist))
  12224. ((org-on-target-p)
  12225. (push (org-point-in-group p 0 :target) clist)
  12226. (goto-char (1- (match-beginning 0)))
  12227. (if (looking-at org-radio-target-regexp)
  12228. (push (org-point-in-group p 0 :radio-target) clist))
  12229. (goto-char p))
  12230. ((setq o (car (delq nil
  12231. (mapcar
  12232. (lambda (x)
  12233. (if (memq x org-latex-fragment-image-overlays) x))
  12234. (org-overlays-at (point))))))
  12235. (push (list :latex-fragment
  12236. (org-overlay-start o) (org-overlay-end o)) clist)
  12237. (push (list :latex-preview
  12238. (org-overlay-start o) (org-overlay-end o)) clist))
  12239. ((org-inside-LaTeX-fragment-p)
  12240. ;; FIXME: positions wrong.
  12241. (push (list :latex-fragment (point) (point)) clist)))
  12242. (setq clist (nreverse (delq nil clist)))
  12243. clist))
  12244. ;; FIXME: Compare with at-regexp-p Do we need both?
  12245. (defun org-in-regexp (re &optional nlines visually)
  12246. "Check if point is inside a match of regexp.
  12247. Normally only the current line is checked, but you can include NLINES extra
  12248. lines both before and after point into the search.
  12249. If VISUALLY is set, require that the cursor is not after the match but
  12250. really on, so that the block visually is on the match."
  12251. (catch 'exit
  12252. (let ((pos (point))
  12253. (eol (point-at-eol (+ 1 (or nlines 0))))
  12254. (inc (if visually 1 0)))
  12255. (save-excursion
  12256. (beginning-of-line (- 1 (or nlines 0)))
  12257. (while (re-search-forward re eol t)
  12258. (if (and (<= (match-beginning 0) pos)
  12259. (>= (+ inc (match-end 0)) pos))
  12260. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12261. (defun org-at-regexp-p (regexp)
  12262. "Is point inside a match of REGEXP in the current line?"
  12263. (catch 'exit
  12264. (save-excursion
  12265. (let ((pos (point)) (end (point-at-eol)))
  12266. (beginning-of-line 1)
  12267. (while (re-search-forward regexp end t)
  12268. (if (and (<= (match-beginning 0) pos)
  12269. (>= (match-end 0) pos))
  12270. (throw 'exit t)))
  12271. nil))))
  12272. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12273. "Call `multi-occur' with buffers for all agenda files."
  12274. (interactive "sOrg-files matching: \np")
  12275. (let* ((files (org-agenda-files))
  12276. (tnames (mapcar 'file-truename files))
  12277. (extra org-agenda-text-search-extra-files)
  12278. f)
  12279. (when (eq (car extra) 'agenda-archives)
  12280. (setq extra (cdr extra))
  12281. (setq files (org-add-archive-files files)))
  12282. (while (setq f (pop extra))
  12283. (unless (member (file-truename f) tnames)
  12284. (add-to-list 'files f 'append)
  12285. (add-to-list 'tnames (file-truename f) 'append)))
  12286. (multi-occur
  12287. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12288. regexp)))
  12289. (if (boundp 'occur-mode-find-occurrence-hook)
  12290. ;; Emacs 23
  12291. (add-hook 'occur-mode-find-occurrence-hook
  12292. (lambda ()
  12293. (when (org-mode-p)
  12294. (org-reveal))))
  12295. ;; Emacs 22
  12296. (defadvice occur-mode-goto-occurrence
  12297. (after org-occur-reveal activate)
  12298. (and (org-mode-p) (org-reveal)))
  12299. (defadvice occur-mode-goto-occurrence-other-window
  12300. (after org-occur-reveal activate)
  12301. (and (org-mode-p) (org-reveal)))
  12302. (defadvice occur-mode-display-occurrence
  12303. (after org-occur-reveal activate)
  12304. (when (org-mode-p)
  12305. (let ((pos (occur-mode-find-occurrence)))
  12306. (with-current-buffer (marker-buffer pos)
  12307. (save-excursion
  12308. (goto-char pos)
  12309. (org-reveal)))))))
  12310. (defun org-uniquify (list)
  12311. "Remove duplicate elements from LIST."
  12312. (let (res)
  12313. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12314. res))
  12315. (defun org-delete-all (elts list)
  12316. "Remove all elements in ELTS from LIST."
  12317. (while elts
  12318. (setq list (delete (pop elts) list)))
  12319. list)
  12320. (defun org-back-over-empty-lines ()
  12321. "Move backwards over witespace, to the beginning of the first empty line.
  12322. Returns the number of empty lines passed."
  12323. (let ((pos (point)))
  12324. (skip-chars-backward " \t\n\r")
  12325. (beginning-of-line 2)
  12326. (goto-char (min (point) pos))
  12327. (count-lines (point) pos)))
  12328. (defun org-skip-whitespace ()
  12329. (skip-chars-forward " \t\n\r"))
  12330. (defun org-point-in-group (point group &optional context)
  12331. "Check if POINT is in match-group GROUP.
  12332. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12333. match. If the match group does ot exist or point is not inside it,
  12334. return nil."
  12335. (and (match-beginning group)
  12336. (>= point (match-beginning group))
  12337. (<= point (match-end group))
  12338. (if context
  12339. (list context (match-beginning group) (match-end group))
  12340. t)))
  12341. (defun org-switch-to-buffer-other-window (&rest args)
  12342. "Switch to buffer in a second window on the current frame.
  12343. In particular, do not allow pop-up frames."
  12344. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12345. special-display-function)
  12346. (apply 'switch-to-buffer-other-window args)))
  12347. (defun org-combine-plists (&rest plists)
  12348. "Create a single property list from all plists in PLISTS.
  12349. The process starts by copying the first list, and then setting properties
  12350. from the other lists. Settings in the last list are the most significant
  12351. ones and overrule settings in the other lists."
  12352. (let ((rtn (copy-sequence (pop plists)))
  12353. p v ls)
  12354. (while plists
  12355. (setq ls (pop plists))
  12356. (while ls
  12357. (setq p (pop ls) v (pop ls))
  12358. (setq rtn (plist-put rtn p v))))
  12359. rtn))
  12360. (defun org-move-line-down (arg)
  12361. "Move the current line down. With prefix argument, move it past ARG lines."
  12362. (interactive "p")
  12363. (let ((col (current-column))
  12364. beg end pos)
  12365. (beginning-of-line 1) (setq beg (point))
  12366. (beginning-of-line 2) (setq end (point))
  12367. (beginning-of-line (+ 1 arg))
  12368. (setq pos (move-marker (make-marker) (point)))
  12369. (insert (delete-and-extract-region beg end))
  12370. (goto-char pos)
  12371. (org-move-to-column col)))
  12372. (defun org-move-line-up (arg)
  12373. "Move the current line up. With prefix argument, move it past ARG lines."
  12374. (interactive "p")
  12375. (let ((col (current-column))
  12376. beg end pos)
  12377. (beginning-of-line 1) (setq beg (point))
  12378. (beginning-of-line 2) (setq end (point))
  12379. (beginning-of-line (- arg))
  12380. (setq pos (move-marker (make-marker) (point)))
  12381. (insert (delete-and-extract-region beg end))
  12382. (goto-char pos)
  12383. (org-move-to-column col)))
  12384. (defun org-replace-escapes (string table)
  12385. "Replace %-escapes in STRING with values in TABLE.
  12386. TABLE is an association list with keys like \"%a\" and string values.
  12387. The sequences in STRING may contain normal field width and padding information,
  12388. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12389. so values can contain further %-escapes if they are define later in TABLE."
  12390. (let ((case-fold-search nil)
  12391. e re rpl)
  12392. (while (setq e (pop table))
  12393. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12394. (while (string-match re string)
  12395. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12396. (cdr e)))
  12397. (setq string (replace-match rpl t t string))))
  12398. string))
  12399. (defun org-sublist (list start end)
  12400. "Return a section of LIST, from START to END.
  12401. Counting starts at 1."
  12402. (let (rtn (c start))
  12403. (setq list (nthcdr (1- start) list))
  12404. (while (and list (<= c end))
  12405. (push (pop list) rtn)
  12406. (setq c (1+ c)))
  12407. (nreverse rtn)))
  12408. (defun org-find-base-buffer-visiting (file)
  12409. "Like `find-buffer-visiting' but alway return the base buffer and
  12410. not an indirect buffer."
  12411. (let ((buf (find-buffer-visiting file)))
  12412. (if buf
  12413. (or (buffer-base-buffer buf) buf)
  12414. nil)))
  12415. (defun org-image-file-name-regexp ()
  12416. "Return regexp matching the file names of images."
  12417. (if (fboundp 'image-file-name-regexp)
  12418. (image-file-name-regexp)
  12419. (let ((image-file-name-extensions
  12420. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12421. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12422. (concat "\\."
  12423. (regexp-opt (nconc (mapcar 'upcase
  12424. image-file-name-extensions)
  12425. image-file-name-extensions)
  12426. t)
  12427. "\\'"))))
  12428. (defun org-file-image-p (file)
  12429. "Return non-nil if FILE is an image."
  12430. (save-match-data
  12431. (string-match (org-image-file-name-regexp) file)))
  12432. ;;; Paragraph filling stuff.
  12433. ;; We want this to be just right, so use the full arsenal.
  12434. (defun org-indent-line-function ()
  12435. "Indent line like previous, but further if previous was headline or item."
  12436. (interactive)
  12437. (let* ((pos (point))
  12438. (itemp (org-at-item-p))
  12439. column bpos bcol tpos tcol bullet btype bullet-type)
  12440. ;; Find the previous relevant line
  12441. (beginning-of-line 1)
  12442. (cond
  12443. ((looking-at "#") (setq column 0))
  12444. ((looking-at "\\*+ ") (setq column 0))
  12445. (t
  12446. (beginning-of-line 0)
  12447. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12448. (beginning-of-line 0))
  12449. (cond
  12450. ((looking-at "\\*+[ \t]+")
  12451. (goto-char (match-end 0))
  12452. (setq column (current-column)))
  12453. ((org-in-item-p)
  12454. (org-beginning-of-item)
  12455. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12456. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12457. (setq bpos (match-beginning 1) tpos (match-end 0)
  12458. bcol (progn (goto-char bpos) (current-column))
  12459. tcol (progn (goto-char tpos) (current-column))
  12460. bullet (match-string 1)
  12461. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12462. (if (> tcol (+ bcol org-description-max-indent))
  12463. (setq tcol (+ bcol 5)))
  12464. (if (not itemp)
  12465. (setq column tcol)
  12466. (goto-char pos)
  12467. (beginning-of-line 1)
  12468. (if (looking-at "\\S-")
  12469. (progn
  12470. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12471. (setq bullet (match-string 1)
  12472. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12473. (setq column (if (equal btype bullet-type) bcol tcol)))
  12474. (setq column (org-get-indentation)))))
  12475. (t (setq column (org-get-indentation))))))
  12476. (goto-char pos)
  12477. (if (<= (current-column) (current-indentation))
  12478. (org-indent-line-to column)
  12479. (save-excursion (org-indent-line-to column)))
  12480. (setq column (current-column))
  12481. (beginning-of-line 1)
  12482. (if (looking-at
  12483. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12484. (replace-match (concat "\\1" (format org-property-format
  12485. (match-string 2) (match-string 3)))
  12486. t nil))
  12487. (org-move-to-column column)))
  12488. (defun org-set-autofill-regexps ()
  12489. (interactive)
  12490. ;; In the paragraph separator we include headlines, because filling
  12491. ;; text in a line directly attached to a headline would otherwise
  12492. ;; fill the headline as well.
  12493. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12494. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12495. ;; The paragraph starter includes hand-formatted lists.
  12496. (org-set-local 'paragraph-start
  12497. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12498. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12499. ;; But only if the user has not turned off tables or fixed-width regions
  12500. (org-set-local
  12501. 'auto-fill-inhibit-regexp
  12502. (concat "\\*+ \\|#\\+"
  12503. "\\|[ \t]*" org-keyword-time-regexp
  12504. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12505. (concat
  12506. "\\|[ \t]*["
  12507. (if org-enable-table-editor "|" "")
  12508. (if org-enable-fixed-width-editor ":" "")
  12509. "]"))))
  12510. ;; We use our own fill-paragraph function, to make sure that tables
  12511. ;; and fixed-width regions are not wrapped. That function will pass
  12512. ;; through to `fill-paragraph' when appropriate.
  12513. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12514. ; Adaptive filling: To get full control, first make sure that
  12515. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12516. (org-set-local 'adaptive-fill-regexp "\000")
  12517. (org-set-local 'adaptive-fill-function
  12518. 'org-adaptive-fill-function)
  12519. (org-set-local
  12520. 'align-mode-rules-list
  12521. '((org-in-buffer-settings
  12522. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12523. (modes . '(org-mode))))))
  12524. (defun org-fill-paragraph (&optional justify)
  12525. "Re-align a table, pass through to fill-paragraph if no table."
  12526. (let ((table-p (org-at-table-p))
  12527. (table.el-p (org-at-table.el-p)))
  12528. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12529. (save-excursion (goto-char (point-at-bol))
  12530. (looking-at outline-regexp)))
  12531. t) ; skip headlines
  12532. (table.el-p t) ; skip table.el tables
  12533. (table-p (org-table-align) t) ; align org-mode tables
  12534. (t nil)))) ; call paragraph-fill
  12535. ;; For reference, this is the default value of adaptive-fill-regexp
  12536. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12537. (defun org-adaptive-fill-function ()
  12538. "Return a fill prefix for org-mode files.
  12539. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12540. work correctly."
  12541. (cond ((looking-at "#[ \t]+")
  12542. (match-string 0))
  12543. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12544. (save-excursion
  12545. (if (> (match-end 1) (+ (match-beginning 1)
  12546. org-description-max-indent))
  12547. (goto-char (+ (match-beginning 1) 5))
  12548. (goto-char (match-end 0)))
  12549. (make-string (current-column) ?\ )))
  12550. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12551. (save-excursion
  12552. (goto-char (match-end 0))
  12553. (make-string (current-column) ?\ )))
  12554. (t nil)))
  12555. ;;; Other stuff.
  12556. (defun org-toggle-fixed-width-section (arg)
  12557. "Toggle the fixed-width export.
  12558. If there is no active region, the QUOTE keyword at the current headline is
  12559. inserted or removed. When present, it causes the text between this headline
  12560. and the next to be exported as fixed-width text, and unmodified.
  12561. If there is an active region, this command adds or removes a colon as the
  12562. first character of this line. If the first character of a line is a colon,
  12563. this line is also exported in fixed-width font."
  12564. (interactive "P")
  12565. (let* ((cc 0)
  12566. (regionp (org-region-active-p))
  12567. (beg (if regionp (region-beginning) (point)))
  12568. (end (if regionp (region-end)))
  12569. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12570. (case-fold-search nil)
  12571. (re "[ \t]*\\(:\\)")
  12572. off)
  12573. (if regionp
  12574. (save-excursion
  12575. (goto-char beg)
  12576. (setq cc (current-column))
  12577. (beginning-of-line 1)
  12578. (setq off (looking-at re))
  12579. (while (> nlines 0)
  12580. (setq nlines (1- nlines))
  12581. (beginning-of-line 1)
  12582. (cond
  12583. (arg
  12584. (org-move-to-column cc t)
  12585. (insert ":\n")
  12586. (forward-line -1))
  12587. ((and off (looking-at re))
  12588. (replace-match "" t t nil 1))
  12589. ((not off) (org-move-to-column cc t) (insert ":")))
  12590. (forward-line 1)))
  12591. (save-excursion
  12592. (org-back-to-heading)
  12593. (if (looking-at (concat outline-regexp
  12594. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12595. (replace-match "" t t nil 1)
  12596. (if (looking-at outline-regexp)
  12597. (progn
  12598. (goto-char (match-end 0))
  12599. (insert org-quote-string " "))))))))
  12600. ;;;; Functions extending outline functionality
  12601. (defun org-beginning-of-line (&optional arg)
  12602. "Go to the beginning of the current line. If that is invisible, continue
  12603. to a visible line beginning. This makes the function of C-a more intuitive.
  12604. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12605. first attempt, and only move to after the tags when the cursor is already
  12606. beyond the end of the headline."
  12607. (interactive "P")
  12608. (let ((pos (point)))
  12609. (beginning-of-line 1)
  12610. (if (bobp)
  12611. nil
  12612. (backward-char 1)
  12613. (if (org-invisible-p)
  12614. (while (and (not (bobp)) (org-invisible-p))
  12615. (backward-char 1)
  12616. (beginning-of-line 1))
  12617. (forward-char 1)))
  12618. (when org-special-ctrl-a/e
  12619. (cond
  12620. ((and (looking-at org-todo-line-regexp)
  12621. (= (char-after (match-end 1)) ?\ ))
  12622. (goto-char
  12623. (if (eq org-special-ctrl-a/e t)
  12624. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12625. ((= pos (point)) (match-beginning 3))
  12626. (t (point)))
  12627. (cond ((> pos (point)) (point))
  12628. ((not (eq last-command this-command)) (point))
  12629. (t (match-beginning 3))))))
  12630. ((org-at-item-p)
  12631. (goto-char
  12632. (if (eq org-special-ctrl-a/e t)
  12633. (cond ((> pos (match-end 4)) (match-end 4))
  12634. ((= pos (point)) (match-end 4))
  12635. (t (point)))
  12636. (cond ((> pos (point)) (point))
  12637. ((not (eq last-command this-command)) (point))
  12638. (t (match-end 4))))))))))
  12639. (defun org-end-of-line (&optional arg)
  12640. "Go to the end of the line.
  12641. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12642. first attempt, and only move to after the tags when the cursor is already
  12643. beyond the end of the headline."
  12644. (interactive "P")
  12645. (if (or (not org-special-ctrl-a/e)
  12646. (not (org-on-heading-p)))
  12647. (end-of-line arg)
  12648. (let ((pos (point)))
  12649. (beginning-of-line 1)
  12650. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12651. (if (eq org-special-ctrl-a/e t)
  12652. (if (or (< pos (match-beginning 1))
  12653. (= pos (match-end 0)))
  12654. (goto-char (match-beginning 1))
  12655. (goto-char (match-end 0)))
  12656. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12657. (goto-char (match-end 0))
  12658. (goto-char (match-beginning 1))))
  12659. (end-of-line arg)))))
  12660. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12661. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12662. (defun org-kill-line (&optional arg)
  12663. "Kill line, to tags or end of line."
  12664. (interactive "P")
  12665. (cond
  12666. ((or (not org-special-ctrl-k)
  12667. (bolp)
  12668. (not (org-on-heading-p)))
  12669. (call-interactively 'kill-line))
  12670. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12671. (kill-region (point) (match-beginning 1))
  12672. (org-set-tags nil t))
  12673. (t (kill-region (point) (point-at-eol)))))
  12674. (define-key org-mode-map "\C-k" 'org-kill-line)
  12675. (defun org-invisible-p ()
  12676. "Check if point is at a character currently not visible."
  12677. ;; Early versions of noutline don't have `outline-invisible-p'.
  12678. (if (fboundp 'outline-invisible-p)
  12679. (outline-invisible-p)
  12680. (get-char-property (point) 'invisible)))
  12681. (defun org-invisible-p2 ()
  12682. "Check if point is at a character currently not visible."
  12683. (save-excursion
  12684. (if (and (eolp) (not (bobp))) (backward-char 1))
  12685. ;; Early versions of noutline don't have `outline-invisible-p'.
  12686. (if (fboundp 'outline-invisible-p)
  12687. (outline-invisible-p)
  12688. (get-char-property (point) 'invisible))))
  12689. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12690. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12691. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12692. (defun org-at-heading-or-item-p ()
  12693. (or (org-on-heading-p) (org-at-item-p)))
  12694. (defun org-on-target-p ()
  12695. (or (org-in-regexp org-radio-target-regexp)
  12696. (org-in-regexp org-target-regexp)))
  12697. (defun org-up-heading-all (arg)
  12698. "Move to the heading line of which the present line is a subheading.
  12699. This function considers both visible and invisible heading lines.
  12700. With argument, move up ARG levels."
  12701. (if (fboundp 'outline-up-heading-all)
  12702. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12703. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12704. (defun org-up-heading-safe ()
  12705. "Move to the heading line of which the present line is a subheading.
  12706. This version will not throw an error. It will return the level of the
  12707. headline found, or nil if no higher level is found."
  12708. (let ((pos (point)) start-level level
  12709. (re (concat "^" outline-regexp)))
  12710. (catch 'exit
  12711. (outline-back-to-heading t)
  12712. (setq start-level (funcall outline-level))
  12713. (if (equal start-level 1) (throw 'exit nil))
  12714. (while (re-search-backward re nil t)
  12715. (setq level (funcall outline-level))
  12716. (if (< level start-level) (throw 'exit level)))
  12717. nil)))
  12718. (defun org-first-sibling-p ()
  12719. "Is this heading the first child of its parents?"
  12720. (interactive)
  12721. (let ((re (concat "^" outline-regexp))
  12722. level l)
  12723. (unless (org-at-heading-p t)
  12724. (error "Not at a heading"))
  12725. (setq level (funcall outline-level))
  12726. (save-excursion
  12727. (if (not (re-search-backward re nil t))
  12728. t
  12729. (setq l (funcall outline-level))
  12730. (< l level)))))
  12731. (defun org-goto-sibling (&optional previous)
  12732. "Goto the next sibling, even if it is invisible.
  12733. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12734. when a sibling was found. When none is found, return nil and don't
  12735. move point."
  12736. (let ((fun (if previous 're-search-backward 're-search-forward))
  12737. (pos (point))
  12738. (re (concat "^" outline-regexp))
  12739. level l)
  12740. (when (condition-case nil (org-back-to-heading t) (error nil))
  12741. (setq level (funcall outline-level))
  12742. (catch 'exit
  12743. (or previous (forward-char 1))
  12744. (while (funcall fun re nil t)
  12745. (setq l (funcall outline-level))
  12746. (when (< l level) (goto-char pos) (throw 'exit nil))
  12747. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12748. (goto-char pos)
  12749. nil))))
  12750. (defun org-show-siblings ()
  12751. "Show all siblings of the current headline."
  12752. (save-excursion
  12753. (while (org-goto-sibling) (org-flag-heading nil)))
  12754. (save-excursion
  12755. (while (org-goto-sibling 'previous)
  12756. (org-flag-heading nil))))
  12757. (defun org-show-hidden-entry ()
  12758. "Show an entry where even the heading is hidden."
  12759. (save-excursion
  12760. (org-show-entry)))
  12761. (defun org-flag-heading (flag &optional entry)
  12762. "Flag the current heading. FLAG non-nil means make invisible.
  12763. When ENTRY is non-nil, show the entire entry."
  12764. (save-excursion
  12765. (org-back-to-heading t)
  12766. ;; Check if we should show the entire entry
  12767. (if entry
  12768. (progn
  12769. (org-show-entry)
  12770. (save-excursion
  12771. (and (outline-next-heading)
  12772. (org-flag-heading nil))))
  12773. (outline-flag-region (max (point-min) (1- (point)))
  12774. (save-excursion (outline-end-of-heading) (point))
  12775. flag))))
  12776. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12777. ;; This is an exact copy of the original function, but it uses
  12778. ;; `org-back-to-heading', to make it work also in invisible
  12779. ;; trees. And is uses an invisible-OK argument.
  12780. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12781. (org-back-to-heading invisible-OK)
  12782. (let ((first t)
  12783. (level (funcall outline-level)))
  12784. (while (and (not (eobp))
  12785. (or first (> (funcall outline-level) level)))
  12786. (setq first nil)
  12787. (outline-next-heading))
  12788. (unless to-heading
  12789. (if (memq (preceding-char) '(?\n ?\^M))
  12790. (progn
  12791. ;; Go to end of line before heading
  12792. (forward-char -1)
  12793. (if (memq (preceding-char) '(?\n ?\^M))
  12794. ;; leave blank line before heading
  12795. (forward-char -1))))))
  12796. (point))
  12797. (defun org-show-subtree ()
  12798. "Show everything after this heading at deeper levels."
  12799. (outline-flag-region
  12800. (point)
  12801. (save-excursion
  12802. (outline-end-of-subtree) (outline-next-heading) (point))
  12803. nil))
  12804. (defun org-show-entry ()
  12805. "Show the body directly following this heading.
  12806. Show the heading too, if it is currently invisible."
  12807. (interactive)
  12808. (save-excursion
  12809. (condition-case nil
  12810. (progn
  12811. (org-back-to-heading t)
  12812. (outline-flag-region
  12813. (max (point-min) (1- (point)))
  12814. (save-excursion
  12815. (re-search-forward
  12816. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12817. (or (match-beginning 1) (point-max)))
  12818. nil))
  12819. (error nil))))
  12820. (defun org-make-options-regexp (kwds)
  12821. "Make a regular expression for keyword lines."
  12822. (concat
  12823. "^"
  12824. "#?[ \t]*\\+\\("
  12825. (mapconcat 'regexp-quote kwds "\\|")
  12826. "\\):[ \t]*"
  12827. "\\(.+\\)"))
  12828. ;; Make isearch reveal the necessary context
  12829. (defun org-isearch-end ()
  12830. "Reveal context after isearch exits."
  12831. (when isearch-success ; only if search was successful
  12832. (if (featurep 'xemacs)
  12833. ;; Under XEmacs, the hook is run in the correct place,
  12834. ;; we directly show the context.
  12835. (org-show-context 'isearch)
  12836. ;; In Emacs the hook runs *before* restoring the overlays.
  12837. ;; So we have to use a one-time post-command-hook to do this.
  12838. ;; (Emacs 22 has a special variable, see function `org-mode')
  12839. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12840. isearch-mode-end-hook-quit)
  12841. ;; Only when the isearch was not quitted.
  12842. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12843. 'append 'local)))))
  12844. (defun org-isearch-post-command ()
  12845. "Remove self from hook, and show context."
  12846. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12847. (org-show-context 'isearch))
  12848. ;;;; Integration with and fixes for other packages
  12849. ;;; Imenu support
  12850. (defvar org-imenu-markers nil
  12851. "All markers currently used by Imenu.")
  12852. (make-variable-buffer-local 'org-imenu-markers)
  12853. (defun org-imenu-new-marker (&optional pos)
  12854. "Return a new marker for use by Imenu, and remember the marker."
  12855. (let ((m (make-marker)))
  12856. (move-marker m (or pos (point)))
  12857. (push m org-imenu-markers)
  12858. m))
  12859. (defun org-imenu-get-tree ()
  12860. "Produce the index for Imenu."
  12861. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12862. (setq org-imenu-markers nil)
  12863. (let* ((n org-imenu-depth)
  12864. (re (concat "^" outline-regexp))
  12865. (subs (make-vector (1+ n) nil))
  12866. (last-level 0)
  12867. m tree level head)
  12868. (save-excursion
  12869. (save-restriction
  12870. (widen)
  12871. (goto-char (point-max))
  12872. (while (re-search-backward re nil t)
  12873. (setq level (org-reduced-level (funcall outline-level)))
  12874. (when (<= level n)
  12875. (looking-at org-complex-heading-regexp)
  12876. (setq head (org-match-string-no-properties 4)
  12877. m (org-imenu-new-marker))
  12878. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12879. (if (>= level last-level)
  12880. (push (cons head m) (aref subs level))
  12881. (push (cons head (aref subs (1+ level))) (aref subs level))
  12882. (loop for i from (1+ level) to n do (aset subs i nil)))
  12883. (setq last-level level)))))
  12884. (aref subs 1)))
  12885. (eval-after-load "imenu"
  12886. '(progn
  12887. (add-hook 'imenu-after-jump-hook
  12888. (lambda () (org-show-context 'org-goto)))))
  12889. ;; Speedbar support
  12890. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12891. "Overlay marking the agenda restriction line in speedbar.")
  12892. (org-overlay-put org-speedbar-restriction-lock-overlay
  12893. 'face 'org-agenda-restriction-lock)
  12894. (org-overlay-put org-speedbar-restriction-lock-overlay
  12895. 'help-echo "Agendas are currently limited to this item.")
  12896. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12897. (defun org-speedbar-set-agenda-restriction ()
  12898. "Restrict future agenda commands to the location at point in speedbar.
  12899. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12900. (interactive)
  12901. (require 'org-agenda)
  12902. (let (p m tp np dir txt w)
  12903. (cond
  12904. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12905. 'org-imenu t))
  12906. (setq m (get-text-property p 'org-imenu-marker))
  12907. (save-excursion
  12908. (save-restriction
  12909. (set-buffer (marker-buffer m))
  12910. (goto-char m)
  12911. (org-agenda-set-restriction-lock 'subtree))))
  12912. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12913. 'speedbar-function 'speedbar-find-file))
  12914. (setq tp (previous-single-property-change
  12915. (1+ p) 'speedbar-function)
  12916. np (next-single-property-change
  12917. tp 'speedbar-function)
  12918. dir (speedbar-line-directory)
  12919. txt (buffer-substring-no-properties (or tp (point-min))
  12920. (or np (point-max))))
  12921. (save-excursion
  12922. (save-restriction
  12923. (set-buffer (find-file-noselect
  12924. (let ((default-directory dir))
  12925. (expand-file-name txt))))
  12926. (unless (org-mode-p)
  12927. (error "Cannot restrict to non-Org-mode file"))
  12928. (org-agenda-set-restriction-lock 'file))))
  12929. (t (error "Don't know how to restrict Org-mode's agenda")))
  12930. (org-move-overlay org-speedbar-restriction-lock-overlay
  12931. (point-at-bol) (point-at-eol))
  12932. (setq current-prefix-arg nil)
  12933. (org-agenda-maybe-redo)))
  12934. (eval-after-load "speedbar"
  12935. '(progn
  12936. (speedbar-add-supported-extension ".org")
  12937. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12938. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12939. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12940. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12941. (add-hook 'speedbar-visiting-tag-hook
  12942. (lambda () (org-show-context 'org-goto)))))
  12943. ;;; Fixes and Hacks for problems with other packages
  12944. ;; Make flyspell not check words in links, to not mess up our keymap
  12945. (defun org-mode-flyspell-verify ()
  12946. "Don't let flyspell put overlays at active buttons."
  12947. (not (get-text-property (point) 'keymap)))
  12948. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12949. (eval-after-load "bookmark"
  12950. '(if (boundp 'bookmark-after-jump-hook)
  12951. ;; We can use the hook
  12952. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12953. ;; Hook not available, use advice
  12954. (defadvice bookmark-jump (after org-make-visible activate)
  12955. "Make the position visible."
  12956. (org-bookmark-jump-unhide))))
  12957. (defun org-bookmark-jump-unhide ()
  12958. "Unhide the current position, to show the bookmark location."
  12959. (and (org-mode-p)
  12960. (or (org-invisible-p)
  12961. (save-excursion (goto-char (max (point-min) (1- (point))))
  12962. (org-invisible-p)))
  12963. (org-show-context 'bookmark-jump)))
  12964. ;; Make session.el ignore our circular variable
  12965. (eval-after-load "session"
  12966. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12967. ;;;; Experimental code
  12968. (defun org-closed-in-range ()
  12969. "Sparse tree of items closed in a certain time range.
  12970. Still experimental, may disappear in the future."
  12971. (interactive)
  12972. ;; Get the time interval from the user.
  12973. (let* ((time1 (time-to-seconds
  12974. (org-read-date nil 'to-time nil "Starting date: ")))
  12975. (time2 (time-to-seconds
  12976. (org-read-date nil 'to-time nil "End date:")))
  12977. ;; callback function
  12978. (callback (lambda ()
  12979. (let ((time
  12980. (time-to-seconds
  12981. (apply 'encode-time
  12982. (org-parse-time-string
  12983. (match-string 1))))))
  12984. ;; check if time in interval
  12985. (and (>= time time1) (<= time time2))))))
  12986. ;; make tree, check each match with the callback
  12987. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12988. ;;;; Finish up
  12989. (provide 'org)
  12990. (run-hooks 'org-load-hook)
  12991. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12992. ;;; org.el ends here