org.el 520 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183
  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-indented nil
  196. "Non-nil means, turn on `org-indent-mode' on startup.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: localindent
  200. #+STARTUP: indent
  201. #+STARTUP: noindent"
  202. :group 'org-structure
  203. :type '(choice
  204. (const :tag "Not" nil)
  205. (const :tag "Locally" local)
  206. (const :tag "Globally (slow on startup in large files)" t)))
  207. (defcustom org-startup-align-all-tables nil
  208. "Non-nil means, align all tables when visiting a file.
  209. This is useful when the column width in tables is forced with <N> cookies
  210. in table fields. Such tables will look correct only after the first re-align.
  211. This can also be configured on a per-file basis by adding one of
  212. the following lines anywhere in the buffer:
  213. #+STARTUP: align
  214. #+STARTUP: noalign"
  215. :group 'org-startup
  216. :type 'boolean)
  217. (defcustom org-insert-mode-line-in-empty-file nil
  218. "Non-nil means insert the first line setting Org-mode in empty files.
  219. When the function `org-mode' is called interactively in an empty file, this
  220. normally means that the file name does not automatically trigger Org-mode.
  221. To ensure that the file will always be in Org-mode in the future, a
  222. line enforcing Org-mode will be inserted into the buffer, if this option
  223. has been set."
  224. :group 'org-startup
  225. :type 'boolean)
  226. (defcustom org-replace-disputed-keys nil
  227. "Non-nil means use alternative key bindings for some keys.
  228. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  229. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  230. If you want to use Org-mode together with one of these other modes,
  231. or more generally if you would like to move some Org-mode commands to
  232. other keys, set this variable and configure the keys with the variable
  233. `org-disputed-keys'.
  234. This option is only relevant at load-time of Org-mode, and must be set
  235. *before* org.el is loaded. Changing it requires a restart of Emacs to
  236. become effective."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following 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 heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  491. org-cycle-hide-drawers
  492. org-cycle-show-empty-lines
  493. org-optimize-window-after-visibility-change)
  494. "Hook that is run after `org-cycle' has changed the buffer visibility.
  495. The function(s) in this hook must accept a single argument which indicates
  496. the new state that was set by the most recent `org-cycle' command. The
  497. argument is a symbol. After a global state change, it can have the values
  498. `overview', `content', or `all'. After a local state change, it can have
  499. the values `folded', `children', or `subtree'."
  500. :group 'org-cycle
  501. :type 'hook)
  502. (defgroup org-edit-structure nil
  503. "Options concerning structure editing in Org-mode."
  504. :tag "Org Edit Structure"
  505. :group 'org-structure)
  506. (defcustom org-odd-levels-only nil
  507. "Non-nil means, skip even levels and only use odd levels for the outline.
  508. This has the effect that two stars are being added/taken away in
  509. promotion/demotion commands. It also influences how levels are
  510. handled by the exporters.
  511. Changing it requires restart of `font-lock-mode' to become effective
  512. for fontification also in regions already fontified.
  513. You may also set this on a per-file basis by adding one of the following
  514. lines to the buffer:
  515. #+STARTUP: odd
  516. #+STARTUP: oddeven"
  517. :group 'org-edit-structure
  518. :group 'org-font-lock
  519. :type 'boolean)
  520. (defcustom org-adapt-indentation t
  521. "Non-nil means, adapt indentation when promoting and demoting.
  522. When this is set and the *entire* text in an entry is indented, the
  523. indentation is increased by one space in a demotion command, and
  524. decreased by one in a promotion command. If any line in the entry
  525. body starts at column 0, indentation is not changed at all."
  526. :group 'org-edit-structure
  527. :type 'boolean)
  528. (defcustom org-special-ctrl-a/e nil
  529. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  530. When t, `C-a' will bring back the cursor to the beginning of the
  531. headline text, i.e. after the stars and after a possible TODO keyword.
  532. In an item, this will be the position after the bullet.
  533. When the cursor is already at that position, another `C-a' will bring
  534. it to the beginning of the line.
  535. `C-e' will jump to the end of the headline, ignoring the presence of tags
  536. in the headline. A second `C-e' will then jump to the true end of the
  537. line, after any tags.
  538. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  539. and only a directly following, identical keypress will bring the cursor
  540. to the special positions."
  541. :group 'org-edit-structure
  542. :type '(choice
  543. (const :tag "off" nil)
  544. (const :tag "after bullet first" t)
  545. (const :tag "border first" reversed)))
  546. (if (fboundp 'defvaralias)
  547. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  548. (defcustom org-special-ctrl-k nil
  549. "Non-nil means `C-k' will behave specially in headlines.
  550. When nil, `C-k' will call the default `kill-line' command.
  551. When t, the following will happen while the cursor is in the headline:
  552. - When the cursor is at the beginning of a headline, kill the entire
  553. line and possible the folded subtree below the line.
  554. - When in the middle of the headline text, kill the headline up to the tags.
  555. - When after the headline text, kill the tags."
  556. :group 'org-edit-structure
  557. :type 'boolean)
  558. (defcustom org-M-RET-may-split-line '((default . t))
  559. "Non-nil means, M-RET will split the line at the cursor position.
  560. When nil, it will go to the end of the line before making a
  561. new line.
  562. You may also set this option in a different way for different
  563. contexts. Valid contexts are:
  564. headline when creating a new headline
  565. item when creating a new item
  566. table in a table field
  567. default the value to be used for all contexts not explicitly
  568. customized"
  569. :group 'org-structure
  570. :group 'org-table
  571. :type '(choice
  572. (const :tag "Always" t)
  573. (const :tag "Never" nil)
  574. (repeat :greedy t :tag "Individual contexts"
  575. (cons
  576. (choice :tag "Context"
  577. (const headline)
  578. (const item)
  579. (const table)
  580. (const default))
  581. (boolean)))))
  582. (defcustom org-blank-before-new-entry '((heading . nil)
  583. (plain-list-item . nil))
  584. "Should `org-insert-heading' leave a blank line before new heading/item?
  585. The value is an alist, with `heading' and `plain-list-item' as car,
  586. and a boolean flag as cdr."
  587. :group 'org-edit-structure
  588. :type '(list
  589. (cons (const heading) (boolean))
  590. (cons (const plain-list-item) (boolean))))
  591. (defcustom org-insert-heading-hook nil
  592. "Hook being run after inserting a new heading."
  593. :group 'org-edit-structure
  594. :type 'hook)
  595. (defcustom org-enable-fixed-width-editor t
  596. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  597. This currently only means, they are never auto-wrapped.
  598. When nil, such lines will be treated like ordinary lines.
  599. See also the QUOTE keyword."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-goto-auto-isearch t
  603. "Non-nil means, typing characters in org-goto starts incremental search."
  604. :group 'org-edit-structure
  605. :type 'boolean)
  606. (defgroup org-sparse-trees nil
  607. "Options concerning sparse trees in Org-mode."
  608. :tag "Org Sparse Trees"
  609. :group 'org-structure)
  610. (defcustom org-highlight-sparse-tree-matches t
  611. "Non-nil means, highlight all matches that define a sparse tree.
  612. The highlights will automatically disappear the next time the buffer is
  613. changed by an edit command."
  614. :group 'org-sparse-trees
  615. :type 'boolean)
  616. (defcustom org-remove-highlights-with-change t
  617. "Non-nil means, any change to the buffer will remove temporary highlights.
  618. Such highlights are created by `org-occur' and `org-clock-display'.
  619. When nil, `C-c C-c needs to be used to get rid of the highlights.
  620. The highlights created by `org-preview-latex-fragment' always need
  621. `C-c C-c' to be removed."
  622. :group 'org-sparse-trees
  623. :group 'org-time
  624. :type 'boolean)
  625. (defcustom org-occur-hook '(org-first-headline-recenter)
  626. "Hook that is run after `org-occur' has constructed a sparse tree.
  627. This can be used to recenter the window to show as much of the structure
  628. as possible."
  629. :group 'org-sparse-trees
  630. :type 'hook)
  631. (defgroup org-plain-lists nil
  632. "Options concerning plain lists in Org-mode."
  633. :tag "Org Plain lists"
  634. :group 'org-structure)
  635. (defcustom org-cycle-include-plain-lists nil
  636. "Non-nil means, include plain lists into visibility cycling.
  637. This means that during cycling, plain list items will *temporarily* be
  638. interpreted as outline headlines with a level given by 1000+i where i is the
  639. indentation of the bullet. In all other operations, plain list items are
  640. not seen as headlines. For example, you cannot assign a TODO keyword to
  641. such an item."
  642. :group 'org-plain-lists
  643. :type 'boolean)
  644. (defcustom org-plain-list-ordered-item-terminator t
  645. "The character that makes a line with leading number an ordered list item.
  646. Valid values are ?. and ?\). To get both terminators, use t. While
  647. ?. may look nicer, it creates the danger that a line with leading
  648. number may be incorrectly interpreted as an item. ?\) therefore is
  649. the safe choice."
  650. :group 'org-plain-lists
  651. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  652. (const :tag "paren like in \"2)\"" ?\))
  653. (const :tab "both" t)))
  654. (defcustom org-empty-line-terminates-plain-lists nil
  655. "Non-nil means, an empty line ends all plain list levels.
  656. When nil, empty lines are part of the preceeding item."
  657. :group 'org-plain-lists
  658. :type 'boolean)
  659. (defcustom org-auto-renumber-ordered-lists t
  660. "Non-nil means, automatically renumber ordered plain lists.
  661. Renumbering happens when the sequence have been changed with
  662. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  663. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  664. :group 'org-plain-lists
  665. :type 'boolean)
  666. (defcustom org-provide-checkbox-statistics t
  667. "Non-nil means, update checkbox statistics after insert and toggle.
  668. When this is set, checkbox statistics is updated each time you either insert
  669. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  670. with \\[org-ctrl-c-ctrl-c\\]."
  671. :group 'org-plain-lists
  672. :type 'boolean)
  673. (defcustom org-description-max-indent 20
  674. "Maximum indentation for the second line of a description list.
  675. When the indentation would be larger than this, it will become
  676. 5 characters instead."
  677. :group 'org-plain-lists
  678. :type 'integer)
  679. (defgroup org-imenu-and-speedbar nil
  680. "Options concerning imenu and speedbar in Org-mode."
  681. :tag "Org Imenu and Speedbar"
  682. :group 'org-structure)
  683. (defcustom org-imenu-depth 2
  684. "The maximum level for Imenu access to Org-mode headlines.
  685. This also applied for speedbar access."
  686. :group 'org-imenu-and-speedbar
  687. :type 'number)
  688. (defgroup org-table nil
  689. "Options concerning tables in Org-mode."
  690. :tag "Org Table"
  691. :group 'org)
  692. (defcustom org-enable-table-editor 'optimized
  693. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  694. When nil, such lines will be treated like ordinary lines.
  695. When equal to the symbol `optimized', the table editor will be optimized to
  696. do the following:
  697. - Automatic overwrite mode in front of whitespace in table fields.
  698. This makes the structure of the table stay in tact as long as the edited
  699. field does not exceed the column width.
  700. - Minimize the number of realigns. Normally, the table is aligned each time
  701. TAB or RET are pressed to move to another field. With optimization this
  702. happens only if changes to a field might have changed the column width.
  703. Optimization requires replacing the functions `self-insert-command',
  704. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  705. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  706. very good at guessing when a re-align will be necessary, but you can always
  707. force one with \\[org-ctrl-c-ctrl-c].
  708. If you would like to use the optimized version in Org-mode, but the
  709. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  710. This variable can be used to turn on and off the table editor during a session,
  711. but in order to toggle optimization, a restart is required.
  712. See also the variable `org-table-auto-blank-field'."
  713. :group 'org-table
  714. :type '(choice
  715. (const :tag "off" nil)
  716. (const :tag "on" t)
  717. (const :tag "on, optimized" optimized)))
  718. (defcustom org-table-tab-recognizes-table.el t
  719. "Non-nil means, TAB will automatically notice a table.el table.
  720. When it sees such a table, it moves point into it and - if necessary -
  721. calls `table-recognize-table'."
  722. :group 'org-table-editing
  723. :type 'boolean)
  724. (defgroup org-link nil
  725. "Options concerning links in Org-mode."
  726. :tag "Org Link"
  727. :group 'org)
  728. (defvar org-link-abbrev-alist-local nil
  729. "Buffer-local version of `org-link-abbrev-alist', which see.
  730. The value of this is taken from the #+LINK lines.")
  731. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  732. (defcustom org-link-abbrev-alist nil
  733. "Alist of link abbreviations.
  734. The car of each element is a string, to be replaced at the start of a link.
  735. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  736. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  737. [[linkkey:tag][description]]
  738. If REPLACE is a string, the tag will simply be appended to create the link.
  739. If the string contains \"%s\", the tag will be inserted there.
  740. REPLACE may also be a function that will be called with the tag as the
  741. only argument to create the link, which should be returned as a string.
  742. See the manual for examples."
  743. :group 'org-link
  744. :type 'alist)
  745. (defcustom org-descriptive-links t
  746. "Non-nil means, hide link part and only show description of bracket links.
  747. Bracket links are like [[link][descritpion]]. This variable sets the initial
  748. state in new org-mode buffers. The setting can then be toggled on a
  749. per-buffer basis from the Org->Hyperlinks menu."
  750. :group 'org-link
  751. :type 'boolean)
  752. (defcustom org-link-file-path-type 'adaptive
  753. "How the path name in file links should be stored.
  754. Valid values are:
  755. relative Relative to the current directory, i.e. the directory of the file
  756. into which the link is being inserted.
  757. absolute Absolute path, if possible with ~ for home directory.
  758. noabbrev Absolute path, no abbreviation of home directory.
  759. adaptive Use relative path for files in the current directory and sub-
  760. directories of it. For other files, use an absolute path."
  761. :group 'org-link
  762. :type '(choice
  763. (const relative)
  764. (const absolute)
  765. (const noabbrev)
  766. (const adaptive)))
  767. (defcustom org-activate-links '(bracket angle plain radio tag date)
  768. "Types of links that should be activated in Org-mode files.
  769. This is a list of symbols, each leading to the activation of a certain link
  770. type. In principle, it does not hurt to turn on most link types - there may
  771. be a small gain when turning off unused link types. The types are:
  772. bracket The recommended [[link][description]] or [[link]] links with hiding.
  773. angular Links in angular brackes that may contain whitespace like
  774. <bbdb:Carsten Dominik>.
  775. plain Plain links in normal text, no whitespace, like http://google.com.
  776. radio Text that is matched by a radio target, see manual for details.
  777. tag Tag settings in a headline (link to tag search).
  778. date Time stamps (link to calendar).
  779. Changing this variable requires a restart of Emacs to become effective."
  780. :group 'org-link
  781. :type '(set (const :tag "Double bracket links (new style)" bracket)
  782. (const :tag "Angular bracket links (old style)" angular)
  783. (const :tag "Plain text links" plain)
  784. (const :tag "Radio target matches" radio)
  785. (const :tag "Tags" tag)
  786. (const :tag "Timestamps" date)))
  787. (defcustom org-make-link-description-function nil
  788. "Function to use to generate link descriptions from links. If
  789. nil the link location will be used. This function must take two
  790. parameters; the first is the link and the second the description
  791. org-insert-link has generated, and should return the description
  792. to use."
  793. :group 'org-link
  794. :type 'function)
  795. (defgroup org-link-store nil
  796. "Options concerning storing links in Org-mode."
  797. :tag "Org Store Link"
  798. :group 'org-link)
  799. (defcustom org-email-link-description-format "Email %c: %.30s"
  800. "Format of the description part of a link to an email or usenet message.
  801. The following %-excapes will be replaced by corresponding information:
  802. %F full \"From\" field
  803. %f name, taken from \"From\" field, address if no name
  804. %T full \"To\" field
  805. %t first name in \"To\" field, address if no name
  806. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  807. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  808. %s subject
  809. %m message-id.
  810. You may use normal field width specification between the % and the letter.
  811. This is for example useful to limit the length of the subject.
  812. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  813. :group 'org-link-store
  814. :type 'string)
  815. (defcustom org-from-is-user-regexp
  816. (let (r1 r2)
  817. (when (and user-mail-address (not (string= user-mail-address "")))
  818. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  819. (when (and user-full-name (not (string= user-full-name "")))
  820. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  821. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  822. "Regexp mached against the \"From:\" header of an email or usenet message.
  823. It should match if the message is from the user him/herself."
  824. :group 'org-link-store
  825. :type 'regexp)
  826. (defcustom org-context-in-file-links t
  827. "Non-nil means, file links from `org-store-link' contain context.
  828. A search string will be added to the file name with :: as separator and
  829. used to find the context when the link is activated by the command
  830. `org-open-at-point'.
  831. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  832. negates this setting for the duration of the command."
  833. :group 'org-link-store
  834. :type 'boolean)
  835. (defcustom org-keep-stored-link-after-insertion nil
  836. "Non-nil means, keep link in list for entire session.
  837. The command `org-store-link' adds a link pointing to the current
  838. location to an internal list. These links accumulate during a session.
  839. The command `org-insert-link' can be used to insert links into any
  840. Org-mode file (offering completion for all stored links). When this
  841. option is nil, every link which has been inserted once using \\[org-insert-link]
  842. will be removed from the list, to make completing the unused links
  843. more efficient."
  844. :group 'org-link-store
  845. :type 'boolean)
  846. (defgroup org-link-follow nil
  847. "Options concerning following links in Org-mode."
  848. :tag "Org Follow Link"
  849. :group 'org-link)
  850. (defcustom org-follow-link-hook nil
  851. "Hook that is run after a link has been followed."
  852. :group 'org-link-follow
  853. :type 'hook)
  854. (defcustom org-tab-follows-link nil
  855. "Non-nil means, on links TAB will follow the link.
  856. Needs to be set before org.el is loaded."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-return-follows-link nil
  860. "Non-nil means, on links RET will follow the link.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mouse-1-follows-link
  865. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  866. "Non-nil means, mouse-1 on a link will follow the link.
  867. A longer mouse click will still set point. Does not work on XEmacs.
  868. Needs to be set before org.el is loaded."
  869. :group 'org-link-follow
  870. :type 'boolean)
  871. (defcustom org-mark-ring-length 4
  872. "Number of different positions to be recorded in the ring
  873. Changing this requires a restart of Emacs to work correctly."
  874. :group 'org-link-follow
  875. :type 'interger)
  876. (defcustom org-link-frame-setup
  877. '((vm . vm-visit-folder-other-frame)
  878. (gnus . gnus-other-frame)
  879. (file . find-file-other-window))
  880. "Setup the frame configuration for following links.
  881. When following a link with Emacs, it may often be useful to display
  882. this link in another window or frame. This variable can be used to
  883. set this up for the different types of links.
  884. For VM, use any of
  885. `vm-visit-folder'
  886. `vm-visit-folder-other-frame'
  887. For Gnus, use any of
  888. `gnus'
  889. `gnus-other-frame'
  890. For FILE, use any of
  891. `find-file'
  892. `find-file-other-window'
  893. `find-file-other-frame'
  894. For the calendar, use the variable `calendar-setup'.
  895. For BBDB, it is currently only possible to display the matches in
  896. another window."
  897. :group 'org-link-follow
  898. :type '(list
  899. (cons (const vm)
  900. (choice
  901. (const vm-visit-folder)
  902. (const vm-visit-folder-other-window)
  903. (const vm-visit-folder-other-frame)))
  904. (cons (const gnus)
  905. (choice
  906. (const gnus)
  907. (const gnus-other-frame)))
  908. (cons (const file)
  909. (choice
  910. (const find-file)
  911. (const find-file-other-window)
  912. (const find-file-other-frame)))))
  913. (defcustom org-display-internal-link-with-indirect-buffer nil
  914. "Non-nil means, use indirect buffer to display infile links.
  915. Activating internal links (from one location in a file to another location
  916. in the same file) normally just jumps to the location. When the link is
  917. activated with a C-u prefix (or with mouse-3), the link is displayed in
  918. another window. When this option is set, the other window actually displays
  919. an indirect buffer clone of the current buffer, to avoid any visibility
  920. changes to the current buffer."
  921. :group 'org-link-follow
  922. :type 'boolean)
  923. (defcustom org-open-non-existing-files nil
  924. "Non-nil means, `org-open-file' will open non-existing files.
  925. When nil, an error will be generated."
  926. :group 'org-link-follow
  927. :type 'boolean)
  928. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  929. "Function and arguments to call for following mailto links.
  930. This is a list with the first element being a lisp function, and the
  931. remaining elements being arguments to the function. In string arguments,
  932. %a will be replaced by the address, and %s will be replaced by the subject
  933. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  934. :group 'org-link-follow
  935. :type '(choice
  936. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  937. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  938. (const :tag "message-mail" (message-mail "%a" "%s"))
  939. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  940. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  941. "Non-nil means, ask for confirmation before executing shell links.
  942. Shell links can be dangerous: just think about a link
  943. [[shell:rm -rf ~/*][Google Search]]
  944. This link would show up in your Org-mode document as \"Google Search\",
  945. but really it would remove your entire home directory.
  946. Therefore we advise against setting this variable to nil.
  947. Just change it to `y-or-n-p' of you want to confirm with a
  948. single keystroke rather than having to type \"yes\"."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  952. (const :tag "with y-or-n (faster)" y-or-n-p)
  953. (const :tag "no confirmation (dangerous)" nil)))
  954. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  955. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  956. Elisp links can be dangerous: just think about a link
  957. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  958. This link would show up in your Org-mode document as \"Google Search\",
  959. but really it would remove your entire home directory.
  960. Therefore we advise against setting this variable to nil.
  961. Just change it to `y-or-n-p' of you want to confirm with a
  962. single keystroke rather than having to type \"yes\"."
  963. :group 'org-link-follow
  964. :type '(choice
  965. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  966. (const :tag "with y-or-n (faster)" y-or-n-p)
  967. (const :tag "no confirmation (dangerous)" nil)))
  968. (defconst org-file-apps-defaults-gnu
  969. '((remote . emacs)
  970. (t . mailcap))
  971. "Default file applications on a UNIX or GNU/Linux system.
  972. See `org-file-apps'.")
  973. (defconst org-file-apps-defaults-macosx
  974. '((remote . emacs)
  975. (t . "open %s")
  976. ("ps" . "gv %s")
  977. ("ps.gz" . "gv %s")
  978. ("eps" . "gv %s")
  979. ("eps.gz" . "gv %s")
  980. ("dvi" . "xdvi %s")
  981. ("fig" . "xfig %s"))
  982. "Default file applications on a MacOS X system.
  983. The system \"open\" is known as a default, but we use X11 applications
  984. for some files for which the OS does not have a good default.
  985. See `org-file-apps'.")
  986. (defconst org-file-apps-defaults-windowsnt
  987. (list
  988. '(remote . emacs)
  989. (cons t
  990. (list (if (featurep 'xemacs)
  991. 'mswindows-shell-execute
  992. 'w32-shell-execute)
  993. "open" 'file)))
  994. "Default file applications on a Windows NT system.
  995. The system \"open\" is used for most files.
  996. See `org-file-apps'.")
  997. (defcustom org-file-apps
  998. '(
  999. ("txt" . emacs)
  1000. ("tex" . emacs)
  1001. ("ltx" . emacs)
  1002. ("org" . emacs)
  1003. ("el" . emacs)
  1004. ("bib" . emacs)
  1005. )
  1006. "External applications for opening `file:path' items in a document.
  1007. Org-mode uses system defaults for different file types, but
  1008. you can use this variable to set the application for a given file
  1009. extension. The entries in this list are cons cells where the car identifies
  1010. files and the cdr the corresponding command. Possible values for the
  1011. file identifier are
  1012. \"ext\" A string identifying an extension
  1013. `directory' Matches a directory
  1014. `remote' Matches a remote file, accessible through tramp or efs.
  1015. Remote files most likely should be visited through Emacs
  1016. because external applications cannot handle such paths.
  1017. t Default for all remaining files
  1018. Possible values for the command are:
  1019. `emacs' The file will be visited by the current Emacs process.
  1020. `default' Use the default application for this file type.
  1021. string A command to be executed by a shell; %s will be replaced
  1022. by the path to the file.
  1023. sexp A Lisp form which will be evaluated. The file path will
  1024. be available in the Lisp variable `file'.
  1025. For more examples, see the system specific constants
  1026. `org-file-apps-defaults-macosx'
  1027. `org-file-apps-defaults-windowsnt'
  1028. `org-file-apps-defaults-gnu'."
  1029. :group 'org-link-follow
  1030. :type '(repeat
  1031. (cons (choice :value ""
  1032. (string :tag "Extension")
  1033. (const :tag "Default for unrecognized files" t)
  1034. (const :tag "Remote file" remote)
  1035. (const :tag "Links to a directory" directory))
  1036. (choice :value ""
  1037. (const :tag "Visit with Emacs" emacs)
  1038. (const :tag "Use system default" default)
  1039. (string :tag "Command")
  1040. (sexp :tag "Lisp form")))))
  1041. (defgroup org-refile nil
  1042. "Options concerning refiling entries in Org-mode."
  1043. :tag "Org Remember"
  1044. :group 'org)
  1045. (defcustom org-directory "~/org"
  1046. "Directory with org files.
  1047. This directory will be used as default to prompt for org files.
  1048. Used by the hooks for remember.el."
  1049. :group 'org-refile
  1050. :group 'org-remember
  1051. :type 'directory)
  1052. (defcustom org-default-notes-file "~/.notes"
  1053. "Default target for storing notes.
  1054. Used by the hooks for remember.el. This can be a string, or nil to mean
  1055. the value of `remember-data-file'.
  1056. You can set this on a per-template basis with the variable
  1057. `org-remember-templates'."
  1058. :group 'org-refile
  1059. :group 'org-remember
  1060. :type '(choice
  1061. (const :tag "Default from remember-data-file" nil)
  1062. file))
  1063. (defcustom org-goto-interface 'outline
  1064. "The default interface to be used for `org-goto'.
  1065. Allowed vaues are:
  1066. outline The interface shows an outline of the relevant file
  1067. and the correct heading is found by moving through
  1068. the outline or by searching with incremental search.
  1069. outline-path-completion Headlines in the current buffer are offered via
  1070. completion."
  1071. :group 'org-refile
  1072. :type '(choice
  1073. (const :tag "Outline" outline)
  1074. (const :tag "Outline-path-completion" outline-path-completion)))
  1075. (defcustom org-reverse-note-order nil
  1076. "Non-nil means, store new notes at the beginning of a file or entry.
  1077. When nil, new notes will be filed to the end of a file or entry.
  1078. This can also be a list with cons cells of regular expressions that
  1079. are matched against file names, and values."
  1080. :group 'org-remember
  1081. :type '(choice
  1082. (const :tag "Reverse always" t)
  1083. (const :tag "Reverse never" nil)
  1084. (repeat :tag "By file name regexp"
  1085. (cons regexp boolean))))
  1086. (defcustom org-refile-targets nil
  1087. "Targets for refiling entries with \\[org-refile].
  1088. This is list of cons cells. Each cell contains:
  1089. - a specification of the files to be considered, either a list of files,
  1090. or a symbol whose function or variable value will be used to retrieve
  1091. a file name or a list of file names. Nil means, refile to a different
  1092. heading in the current buffer.
  1093. - A specification of how to find candidate refile targets. This may be
  1094. any of
  1095. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1096. This tag has to be present in all target headlines, inheritance will
  1097. not be considered.
  1098. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1099. todo keyword.
  1100. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1101. headlines that are refiling targets.
  1102. - a cons cell (:level . N). Any headline of level N is considered a target.
  1103. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1104. :group 'org-remember
  1105. :type '(repeat
  1106. (cons
  1107. (choice :value org-agenda-files
  1108. (const :tag "All agenda files" org-agenda-files)
  1109. (const :tag "Current buffer" nil)
  1110. (function) (variable) (file))
  1111. (choice :tag "Identify target headline by"
  1112. (cons :tag "Specific tag" (const :tag) (string))
  1113. (cons :tag "TODO keyword" (const :todo) (string))
  1114. (cons :tag "Regular expression" (const :regexp) (regexp))
  1115. (cons :tag "Level number" (const :level) (integer))
  1116. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1117. (defcustom org-refile-use-outline-path nil
  1118. "Non-nil means, provide refile targets as paths.
  1119. So a level 3 headline will be available as level1/level2/level3.
  1120. When the value is `file', also include the file name (without directory)
  1121. into the path. When `full-file-path', include the full file path."
  1122. :group 'org-remember
  1123. :type '(choice
  1124. (const :tag "Not" nil)
  1125. (const :tag "Yes" t)
  1126. (const :tag "Start with file name" file)
  1127. (const :tag "Start with full file path" full-file-path)))
  1128. (defgroup org-todo nil
  1129. "Options concerning TODO items in Org-mode."
  1130. :tag "Org TODO"
  1131. :group 'org)
  1132. (defgroup org-progress nil
  1133. "Options concerning Progress logging in Org-mode."
  1134. :tag "Org Progress"
  1135. :group 'org-time)
  1136. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1137. "List of TODO entry keyword sequences and their interpretation.
  1138. \\<org-mode-map>This is a list of sequences.
  1139. Each sequence starts with a symbol, either `sequence' or `type',
  1140. indicating if the keywords should be interpreted as a sequence of
  1141. action steps, or as different types of TODO items. The first
  1142. keywords are states requiring action - these states will select a headline
  1143. for inclusion into the global TODO list Org-mode produces. If one of
  1144. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1145. signify that no further action is necessary. If \"|\" is not found,
  1146. the last keyword is treated as the only DONE state of the sequence.
  1147. The command \\[org-todo] cycles an entry through these states, and one
  1148. additional state where no keyword is present. For details about this
  1149. cycling, see the manual.
  1150. TODO keywords and interpretation can also be set on a per-file basis with
  1151. the special #+SEQ_TODO and #+TYP_TODO lines.
  1152. Each keyword can optionally specify a character for fast state selection
  1153. \(in combination with the variable `org-use-fast-todo-selection')
  1154. and specifiers for state change logging, using the same syntax
  1155. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1156. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1157. indicates to record a time stamp each time this state is selected.
  1158. Each keyword may also specify if a timestamp or a note should be
  1159. recorded when entering or leaving the state, by adding additional
  1160. characters in the parenthesis after the keyword. This looks like this:
  1161. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1162. record only the time of the state change. With X and Y being either
  1163. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1164. Y when leaving the state if and only if the *target* state does not
  1165. define X. You may omit any of the fast-selection key or X or /Y,
  1166. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1167. For backward compatibility, this variable may also be just a list
  1168. of keywords - in this case the interptetation (sequence or type) will be
  1169. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1170. :group 'org-todo
  1171. :group 'org-keywords
  1172. :type '(choice
  1173. (repeat :tag "Old syntax, just keywords"
  1174. (string :tag "Keyword"))
  1175. (repeat :tag "New syntax"
  1176. (cons
  1177. (choice
  1178. :tag "Interpretation"
  1179. (const :tag "Sequence (cycling hits every state)" sequence)
  1180. (const :tag "Type (cycling directly to DONE)" type))
  1181. (repeat
  1182. (string :tag "Keyword"))))))
  1183. (defvar org-todo-keywords-1 nil
  1184. "All TODO and DONE keywords active in a buffer.")
  1185. (make-variable-buffer-local 'org-todo-keywords-1)
  1186. (defvar org-todo-keywords-for-agenda nil)
  1187. (defvar org-done-keywords-for-agenda nil)
  1188. (defvar org-agenda-contributing-files nil)
  1189. (defvar org-not-done-keywords nil)
  1190. (make-variable-buffer-local 'org-not-done-keywords)
  1191. (defvar org-done-keywords nil)
  1192. (make-variable-buffer-local 'org-done-keywords)
  1193. (defvar org-todo-heads nil)
  1194. (make-variable-buffer-local 'org-todo-heads)
  1195. (defvar org-todo-sets nil)
  1196. (make-variable-buffer-local 'org-todo-sets)
  1197. (defvar org-todo-log-states nil)
  1198. (make-variable-buffer-local 'org-todo-log-states)
  1199. (defvar org-todo-kwd-alist nil)
  1200. (make-variable-buffer-local 'org-todo-kwd-alist)
  1201. (defvar org-todo-key-alist nil)
  1202. (make-variable-buffer-local 'org-todo-key-alist)
  1203. (defvar org-todo-key-trigger nil)
  1204. (make-variable-buffer-local 'org-todo-key-trigger)
  1205. (defcustom org-todo-interpretation 'sequence
  1206. "Controls how TODO keywords are interpreted.
  1207. This variable is in principle obsolete and is only used for
  1208. backward compatibility, if the interpretation of todo keywords is
  1209. not given already in `org-todo-keywords'. See that variable for
  1210. more information."
  1211. :group 'org-todo
  1212. :group 'org-keywords
  1213. :type '(choice (const sequence)
  1214. (const type)))
  1215. (defcustom org-use-fast-todo-selection 'prefix
  1216. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1217. This variable describes if and under what circumstances the cycling
  1218. mechanism for TODO keywords will be replaced by a single-key, direct
  1219. selection scheme.
  1220. When nil, fast selection is never used.
  1221. When the symbol `prefix', it will be used when `org-todo' is called with
  1222. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1223. in an agenda buffer.
  1224. When t, fast selection is used by default. In this case, the prefix
  1225. argument forces cycling instead.
  1226. In all cases, the special interface is only used if access keys have actually
  1227. been assigned by the user, i.e. if keywords in the configuration are followed
  1228. by a letter in parenthesis, like TODO(t)."
  1229. :group 'org-todo
  1230. :type '(choice
  1231. (const :tag "Never" nil)
  1232. (const :tag "By default" t)
  1233. (const :tag "Only with C-u C-c C-t" prefix)))
  1234. (defcustom org-provide-todo-statistics t
  1235. "Non-nil means, update todo statistics after insert and toggle.
  1236. When this is set, todo statistics is updated in the parent of the current
  1237. entry each time a todo state is changed."
  1238. :group 'org-todo
  1239. :type 'boolean)
  1240. (defcustom org-after-todo-state-change-hook nil
  1241. "Hook which is run after the state of a TODO item was changed.
  1242. The new state (a string with a TODO keyword, or nil) is available in the
  1243. Lisp variable `state'."
  1244. :group 'org-todo
  1245. :type 'hook)
  1246. (defcustom org-log-done nil
  1247. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1248. When equal to the list (done), also prompt for a closing note.
  1249. This can also be configured on a per-file basis by adding one of
  1250. the following lines anywhere in the buffer:
  1251. #+STARTUP: logdone
  1252. #+STARTUP: lognotedone
  1253. #+STARTUP: nologdone"
  1254. :group 'org-todo
  1255. :group 'org-progress
  1256. :type '(choice
  1257. (const :tag "No logging" nil)
  1258. (const :tag "Record CLOSED timestamp" time)
  1259. (const :tag "Record CLOSED timestamp with closing note." note)))
  1260. ;; Normalize old uses of org-log-done.
  1261. (cond
  1262. ((eq org-log-done t) (setq org-log-done 'time))
  1263. ((and (listp org-log-done) (memq 'done org-log-done))
  1264. (setq org-log-done 'note)))
  1265. (defcustom org-log-note-clock-out nil
  1266. "Non-nil means, recored a note when clocking out of an item.
  1267. This can also be configured on a per-file basis by adding one of
  1268. the following lines anywhere in the buffer:
  1269. #+STARTUP: lognoteclock-out
  1270. #+STARTUP: nolognoteclock-out"
  1271. :group 'org-todo
  1272. :group 'org-progress
  1273. :type 'boolean)
  1274. (defcustom org-log-done-with-time t
  1275. "Non-nil means, the CLOSED time stamp will contain date and time.
  1276. When nil, only the date will be recorded."
  1277. :group 'org-progress
  1278. :type 'boolean)
  1279. (defcustom org-log-note-headings
  1280. '((done . "CLOSING NOTE %t")
  1281. (state . "State %-12s %t")
  1282. (note . "Note taken on %t")
  1283. (clock-out . ""))
  1284. "Headings for notes added to entries.
  1285. The value is an alist, with the car being a symbol indicating the note
  1286. context, and the cdr is the heading to be used. The heading may also be the
  1287. empty string.
  1288. %t in the heading will be replaced by a time stamp.
  1289. %s will be replaced by the new TODO state, in double quotes.
  1290. %u will be replaced by the user name.
  1291. %U will be replaced by the full user name."
  1292. :group 'org-todo
  1293. :group 'org-progress
  1294. :type '(list :greedy t
  1295. (cons (const :tag "Heading when closing an item" done) string)
  1296. (cons (const :tag
  1297. "Heading when changing todo state (todo sequence only)"
  1298. state) string)
  1299. (cons (const :tag "Heading when just taking a note" note) string)
  1300. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1301. (unless (assq 'note org-log-note-headings)
  1302. (push '(note . "%t") org-log-note-headings))
  1303. (defcustom org-log-states-order-reversed t
  1304. "Non-nil means, the latest state change note will be directly after heading.
  1305. When nil, the notes will be orderer according to time."
  1306. :group 'org-todo
  1307. :group 'org-progress
  1308. :type 'boolean)
  1309. (defcustom org-log-repeat 'time
  1310. "Non-nil means, record moving through the DONE state when triggering repeat.
  1311. An auto-repeating tasks is immediately switched back to TODO when marked
  1312. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1313. the TODO keyword definition, or recording a closing note by setting
  1314. `org-log-done', there will be no record of the task moving through DONE.
  1315. This variable forces taking a note anyway. Possible values are:
  1316. nil Don't force a record
  1317. time Record a time stamp
  1318. note Record a note
  1319. This option can also be set with on a per-file-basis with
  1320. #+STARTUP: logrepeat
  1321. #+STARTUP: lognoterepeat
  1322. #+STARTUP: nologrepeat
  1323. You can have local logging settings for a subtree by setting the LOGGING
  1324. property to one or more of these keywords."
  1325. :group 'org-todo
  1326. :group 'org-progress
  1327. :type '(choice
  1328. (const :tag "Don't force a record" nil)
  1329. (const :tag "Force recording the DONE state" time)
  1330. (const :tag "Force recording a note with the DONE state" note)))
  1331. (defgroup org-priorities nil
  1332. "Priorities in Org-mode."
  1333. :tag "Org Priorities"
  1334. :group 'org-todo)
  1335. (defcustom org-highest-priority ?A
  1336. "The highest priority of TODO items. A character like ?A, ?B etc.
  1337. Must have a smaller ASCII number than `org-lowest-priority'."
  1338. :group 'org-priorities
  1339. :type 'character)
  1340. (defcustom org-lowest-priority ?C
  1341. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1342. Must have a larger ASCII number than `org-highest-priority'."
  1343. :group 'org-priorities
  1344. :type 'character)
  1345. (defcustom org-default-priority ?B
  1346. "The default priority of TODO items.
  1347. This is the priority an item get if no explicit priority is given."
  1348. :group 'org-priorities
  1349. :type 'character)
  1350. (defcustom org-priority-start-cycle-with-default t
  1351. "Non-nil means, start with default priority when starting to cycle.
  1352. When this is nil, the first step in the cycle will be (depending on the
  1353. command used) one higher or lower that the default priority."
  1354. :group 'org-priorities
  1355. :type 'boolean)
  1356. (defgroup org-time nil
  1357. "Options concerning time stamps and deadlines in Org-mode."
  1358. :tag "Org Time"
  1359. :group 'org)
  1360. (defcustom org-insert-labeled-timestamps-at-point nil
  1361. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1362. When nil, these labeled time stamps are forces into the second line of an
  1363. entry, just after the headline. When scheduling from the global TODO list,
  1364. the time stamp will always be forced into the second line."
  1365. :group 'org-time
  1366. :type 'boolean)
  1367. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1368. "Formats for `format-time-string' which are used for time stamps.
  1369. It is not recommended to change this constant.")
  1370. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1371. "Number of minutes to round time stamps to.
  1372. These are two values, the first applies when first creating a time stamp.
  1373. The second applies when changing it with the commands `S-up' and `S-down'.
  1374. When changing the time stamp, this means that it will change in steps
  1375. of N minutes, as given by the second value.
  1376. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1377. numbers should be factors of 60, so for example 5, 10, 15.
  1378. When this is larger than 1, you can still force an exact time-stamp by using
  1379. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1380. and by using a prefix arg to `S-up/down' to specify the exact number
  1381. of minutes to shift."
  1382. :group 'org-time
  1383. :get '(lambda (var) ; Make sure all entries have 5 elements
  1384. (if (integerp (default-value var))
  1385. (list (default-value var) 5)
  1386. (default-value var)))
  1387. :type '(list
  1388. (integer :tag "when inserting times")
  1389. (integer :tag "when modifying times")))
  1390. ;; Normalize old customizations of this variable.
  1391. (when (integerp org-time-stamp-rounding-minutes)
  1392. (setq org-time-stamp-rounding-minutes
  1393. (list org-time-stamp-rounding-minutes
  1394. org-time-stamp-rounding-minutes)))
  1395. (defcustom org-display-custom-times nil
  1396. "Non-nil means, overlay custom formats over all time stamps.
  1397. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1398. To turn this on on a per-file basis, insert anywhere in the file:
  1399. #+STARTUP: customtime"
  1400. :group 'org-time
  1401. :set 'set-default
  1402. :type 'sexp)
  1403. (make-variable-buffer-local 'org-display-custom-times)
  1404. (defcustom org-time-stamp-custom-formats
  1405. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1406. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1407. These are overlayed over the default ISO format if the variable
  1408. `org-display-custom-times' is set. Time like %H:%M should be at the
  1409. end of the second format."
  1410. :group 'org-time
  1411. :type 'sexp)
  1412. (defun org-time-stamp-format (&optional long inactive)
  1413. "Get the right format for a time string."
  1414. (let ((f (if long (cdr org-time-stamp-formats)
  1415. (car org-time-stamp-formats))))
  1416. (if inactive
  1417. (concat "[" (substring f 1 -1) "]")
  1418. f)))
  1419. (defcustom org-time-clocksum-format "%d:%02d"
  1420. "The format string used when creating CLOCKSUM lines, or when
  1421. org-mode generates a time duration."
  1422. :group 'org-time
  1423. :type 'string)
  1424. (defcustom org-deadline-warning-days 14
  1425. "No. of days before expiration during which a deadline becomes active.
  1426. This variable governs the display in sparse trees and in the agenda.
  1427. When 0 or negative, it means use this number (the absolute value of it)
  1428. even if a deadline has a different individual lead time specified."
  1429. :group 'org-time
  1430. :group 'org-agenda-daily/weekly
  1431. :type 'number)
  1432. (defcustom org-read-date-prefer-future t
  1433. "Non-nil means, assume future for incomplete date input from user.
  1434. This affects the following situations:
  1435. 1. The user gives a day, but no month.
  1436. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1437. read this as the third of *next* month. However, if you enter \"17\",
  1438. it will be considered as *this* month.
  1439. 2. The user gives a month but not a year.
  1440. For example, if it is april and you enter \"feb 2\", this will be read
  1441. as feb 2, *next* year. \"May 5\", however, will be this year.
  1442. Currently this does not work for ISO week specifications.
  1443. When this option is nil, the current month and year will always be used
  1444. as defaults."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (defcustom org-read-date-display-live t
  1448. "Non-nil means, display current interpretation of date prompt live.
  1449. This display will be in an overlay, in the minibuffer."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defcustom org-read-date-popup-calendar t
  1453. "Non-nil means, pop up a calendar when prompting for a date.
  1454. In the calendar, the date can be selected with mouse-1. However, the
  1455. minibuffer will also be active, and you can simply enter the date as well.
  1456. When nil, only the minibuffer will be available."
  1457. :group 'org-time
  1458. :type 'boolean)
  1459. (if (fboundp 'defvaralias)
  1460. (defvaralias 'org-popup-calendar-for-date-prompt
  1461. 'org-read-date-popup-calendar))
  1462. (defcustom org-extend-today-until 0
  1463. "The hour when your day really ends.
  1464. This has influence for the following applications:
  1465. - When switching the agenda to \"today\". It it is still earlier than
  1466. the time given here, the day recognized as TODAY is actually yesterday.
  1467. - When a date is read from the user and it is still before the time given
  1468. here, the current date and time will be assumed to be yesterday, 23:59.
  1469. FIXME:
  1470. IMPORTANT: This is still a very experimental feature, it may disappear
  1471. again or it may be extended to mean more things."
  1472. :group 'org-time
  1473. :type 'number)
  1474. (defcustom org-edit-timestamp-down-means-later nil
  1475. "Non-nil means, S-down will increase the time in a time stamp.
  1476. When nil, S-up will increase."
  1477. :group 'org-time
  1478. :type 'boolean)
  1479. (defcustom org-calendar-follow-timestamp-change t
  1480. "Non-nil means, make the calendar window follow timestamp changes.
  1481. When a timestamp is modified and the calendar window is visible, it will be
  1482. moved to the new date."
  1483. :group 'org-time
  1484. :type 'boolean)
  1485. (defgroup org-tags nil
  1486. "Options concerning tags in Org-mode."
  1487. :tag "Org Tags"
  1488. :group 'org)
  1489. (defcustom org-tag-alist nil
  1490. "List of tags allowed in Org-mode files.
  1491. When this list is nil, Org-mode will base TAG input on what is already in the
  1492. buffer.
  1493. The value of this variable is an alist, the car of each entry must be a
  1494. keyword as a string, the cdr may be a character that is used to select
  1495. that tag through the fast-tag-selection interface.
  1496. See the manual for details."
  1497. :group 'org-tags
  1498. :type '(repeat
  1499. (choice
  1500. (cons (string :tag "Tag name")
  1501. (character :tag "Access char"))
  1502. (const :tag "Start radio group" (:startgroup))
  1503. (const :tag "End radio group" (:endgroup)))))
  1504. (defvar org-file-tags nil
  1505. "List of tags that can be inherited by all entries in the file.
  1506. The tags will be inherited if the variable `org-use-tag-inheritance'
  1507. says they should be.
  1508. This variable is populated from #+TAG lines.")
  1509. (defcustom org-use-fast-tag-selection 'auto
  1510. "Non-nil means, use fast tag selection scheme.
  1511. This is a special interface to select and deselect tags with single keys.
  1512. When nil, fast selection is never used.
  1513. When the symbol `auto', fast selection is used if and only if selection
  1514. characters for tags have been configured, either through the variable
  1515. `org-tag-alist' or through a #+TAGS line in the buffer.
  1516. When t, fast selection is always used and selection keys are assigned
  1517. automatically if necessary."
  1518. :group 'org-tags
  1519. :type '(choice
  1520. (const :tag "Always" t)
  1521. (const :tag "Never" nil)
  1522. (const :tag "When selection characters are configured" 'auto)))
  1523. (defcustom org-fast-tag-selection-single-key nil
  1524. "Non-nil means, fast tag selection exits after first change.
  1525. When nil, you have to press RET to exit it.
  1526. During fast tag selection, you can toggle this flag with `C-c'.
  1527. This variable can also have the value `expert'. In this case, the window
  1528. displaying the tags menu is not even shown, until you press C-c again."
  1529. :group 'org-tags
  1530. :type '(choice
  1531. (const :tag "No" nil)
  1532. (const :tag "Yes" t)
  1533. (const :tag "Expert" expert)))
  1534. (defvar org-fast-tag-selection-include-todo nil
  1535. "Non-nil means, fast tags selection interface will also offer TODO states.
  1536. This is an undocumented feature, you should not rely on it.")
  1537. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1538. "The column to which tags should be indented in a headline.
  1539. If this number is positive, it specifies the column. If it is negative,
  1540. it means that the tags should be flushright to that column. For example,
  1541. -80 works well for a normal 80 character screen."
  1542. :group 'org-tags
  1543. :type 'integer)
  1544. (defcustom org-auto-align-tags t
  1545. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1546. These operations change the length of a headline and therefore shift
  1547. the tags around. With this options turned on, after each such operation
  1548. the tags are again aligned to `org-tags-column'."
  1549. :group 'org-tags
  1550. :type 'boolean)
  1551. (defcustom org-use-tag-inheritance t
  1552. "Non-nil means, tags in levels apply also for sublevels.
  1553. When nil, only the tags directly given in a specific line apply there.
  1554. If this option is t, a match early-on in a tree can lead to a large
  1555. number of matches in the subtree. If you only want to see the first
  1556. match in a tree during a search, check out the variable
  1557. `org-tags-match-list-sublevels'.
  1558. This may also be a list of tags that should be inherited, or a regexp that
  1559. matches tags that should be inherited."
  1560. :group 'org-tags
  1561. :type '(choice
  1562. (const :tag "Not" nil)
  1563. (const :tag "Always" t)
  1564. (repeat :tag "Specific tags" (string :tag "Tag"))
  1565. (regexp :tag "Tags matched by regexp")))
  1566. (defun org-tag-inherit-p (tag)
  1567. "Check if TAG is one that should be inherited."
  1568. (cond
  1569. ((eq org-use-tag-inheritance t) t)
  1570. ((not org-use-tag-inheritance) nil)
  1571. ((stringp org-use-tag-inheritance)
  1572. (string-match org-use-tag-inheritance tag))
  1573. ((listp org-use-tag-inheritance)
  1574. (member tag org-use-tag-inheritance))
  1575. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1576. (defcustom org-tags-match-list-sublevels t
  1577. "Non-nil means list also sublevels of headlines matching tag search.
  1578. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1579. the sublevels of a headline matching a tag search often also match
  1580. the same search. Listing all of them can create very long lists.
  1581. Setting this variable to nil causes subtrees of a match to be skipped.
  1582. This option is off by default, because inheritance in on. If you turn
  1583. inheritance off, you very likely want to turn this option on.
  1584. As a special case, if the tag search is restricted to TODO items, the
  1585. value of this variable is ignored and sublevels are always checked, to
  1586. make sure all corresponding TODO items find their way into the list."
  1587. :group 'org-tags
  1588. :type 'boolean)
  1589. (defvar org-tags-history nil
  1590. "History of minibuffer reads for tags.")
  1591. (defvar org-last-tags-completion-table nil
  1592. "The last used completion table for tags.")
  1593. (defvar org-after-tags-change-hook nil
  1594. "Hook that is run after the tags in a line have changed.")
  1595. (defgroup org-properties nil
  1596. "Options concerning properties in Org-mode."
  1597. :tag "Org Properties"
  1598. :group 'org)
  1599. (defcustom org-property-format "%-10s %s"
  1600. "How property key/value pairs should be formatted by `indent-line'.
  1601. When `indent-line' hits a property definition, it will format the line
  1602. according to this format, mainly to make sure that the values are
  1603. lined-up with respect to each other."
  1604. :group 'org-properties
  1605. :type 'string)
  1606. (defcustom org-use-property-inheritance nil
  1607. "Non-nil means, properties apply also for sublevels.
  1608. This setting is chiefly used during property searches. Turning it on can
  1609. cause significant overhead when doing a search, which is why it is not
  1610. on by default.
  1611. When nil, only the properties directly given in the current entry count.
  1612. When t, every property is inherited. The value may also be a list of
  1613. properties that should have inheritance, or a regular expression matching
  1614. properties that should be inherited.
  1615. However, note that some special properties use inheritance under special
  1616. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1617. and the properties ending in \"_ALL\" when they are used as descriptor
  1618. for valid values of a property.
  1619. Note for programmers:
  1620. When querying an entry with `org-entry-get', you can control if inheritance
  1621. should be used. By default, `org-entry-get' looks only at the local
  1622. properties. You can request inheritance by setting the inherit argument
  1623. to t (to force inheritance) or to `selective' (to respect the setting
  1624. in this variable)."
  1625. :group 'org-properties
  1626. :type '(choice
  1627. (const :tag "Not" nil)
  1628. (const :tag "Always" t)
  1629. (repeat :tag "Specific properties" (string :tag "Property"))
  1630. (regexp :tag "Properties matched by regexp")))
  1631. (defun org-property-inherit-p (property)
  1632. "Check if PROPERTY is one that should be inherited."
  1633. (cond
  1634. ((eq org-use-property-inheritance t) t)
  1635. ((not org-use-property-inheritance) nil)
  1636. ((stringp org-use-property-inheritance)
  1637. (string-match org-use-property-inheritance property))
  1638. ((listp org-use-property-inheritance)
  1639. (member property org-use-property-inheritance))
  1640. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1641. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1642. "The default column format, if no other format has been defined.
  1643. This variable can be set on the per-file basis by inserting a line
  1644. #+COLUMNS: %25ITEM ....."
  1645. :group 'org-properties
  1646. :type 'string)
  1647. (defcustom org-effort-property "Effort"
  1648. "The property that is being used to keep track of effort estimates.
  1649. Effort estimates given in this property need to have the format H:MM."
  1650. :group 'org-properties
  1651. :group 'org-progress
  1652. :type '(string :tag "Property"))
  1653. (defconst org-global-properties-fixed
  1654. '(("VISIBILITY_ALL" . "folded children content all"))
  1655. "List of property/value pairs that can be inherited by any entry.
  1656. These are fixed values, for the preset properties.")
  1657. (defcustom org-global-properties nil
  1658. "List of property/value pairs that can be inherited by any entry.
  1659. You can set buffer-local values for this by adding lines like
  1660. #+PROPERTY: NAME VALUE"
  1661. :group 'org-properties
  1662. :type '(repeat
  1663. (cons (string :tag "Property")
  1664. (string :tag "Value"))))
  1665. (defvar org-file-properties nil
  1666. "List of property/value pairs that can be inherited by any entry.
  1667. Valid for the current buffer.
  1668. This variable is populated from #+PROPERTY lines.")
  1669. (defgroup org-agenda nil
  1670. "Options concerning agenda views in Org-mode."
  1671. :tag "Org Agenda"
  1672. :group 'org)
  1673. (defvar org-category nil
  1674. "Variable used by org files to set a category for agenda display.
  1675. Such files should use a file variable to set it, for example
  1676. # -*- mode: org; org-category: \"ELisp\"
  1677. or contain a special line
  1678. #+CATEGORY: ELisp
  1679. If the file does not specify a category, then file's base name
  1680. is used instead.")
  1681. (make-variable-buffer-local 'org-category)
  1682. (defcustom org-agenda-files nil
  1683. "The files to be used for agenda display.
  1684. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1685. \\[org-remove-file]. You can also use customize to edit the list.
  1686. If an entry is a directory, all files in that directory that are matched by
  1687. `org-agenda-file-regexp' will be part of the file list.
  1688. If the value of the variable is not a list but a single file name, then
  1689. the list of agenda files is actually stored and maintained in that file, one
  1690. agenda file per line."
  1691. :group 'org-agenda
  1692. :type '(choice
  1693. (repeat :tag "List of files and directories" file)
  1694. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1695. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1696. "Regular expression to match files for `org-agenda-files'.
  1697. If any element in the list in that variable contains a directory instead
  1698. of a normal file, all files in that directory that are matched by this
  1699. regular expression will be included."
  1700. :group 'org-agenda
  1701. :type 'regexp)
  1702. (defcustom org-agenda-text-search-extra-files nil
  1703. "List of extra files to be searched by text search commands.
  1704. These files will be search in addition to the agenda files by the
  1705. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1706. Note that these files will only be searched for text search commands,
  1707. not for the other agenda views like todo lists, tag searches or the weekly
  1708. agenda. This variable is intended to list notes and possibly archive files
  1709. that should also be searched by these two commands.
  1710. In fact, if the first element in the list is the symbol `agenda-archives',
  1711. than all archive files of all agenda files will be added to the search
  1712. scope."
  1713. :group 'org-agenda
  1714. :type '(set :greedy t
  1715. (const :tag "Agenda Archives" agenda-archives)
  1716. (repeat :inline t (file))))
  1717. (if (fboundp 'defvaralias)
  1718. (defvaralias 'org-agenda-multi-occur-extra-files
  1719. 'org-agenda-text-search-extra-files))
  1720. (defcustom org-agenda-skip-unavailable-files nil
  1721. "t means to just skip non-reachable files in `org-agenda-files'.
  1722. Nil means to remove them, after a query, from the list."
  1723. :group 'org-agenda
  1724. :type 'boolean)
  1725. (defcustom org-calendar-to-agenda-key [?c]
  1726. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1727. The command `org-calendar-goto-agenda' will be bound to this key. The
  1728. default is the character `c' because then `c' can be used to switch back and
  1729. forth between agenda and calendar."
  1730. :group 'org-agenda
  1731. :type 'sexp)
  1732. (eval-after-load "calendar"
  1733. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1734. 'org-calendar-goto-agenda))
  1735. (defgroup org-latex nil
  1736. "Options for embedding LaTeX code into Org-mode."
  1737. :tag "Org LaTeX"
  1738. :group 'org)
  1739. (defcustom org-format-latex-options
  1740. '(:foreground default :background default :scale 1.0
  1741. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1742. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1743. "Options for creating images from LaTeX fragments.
  1744. This is a property list with the following properties:
  1745. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1746. `default' means use the foreground of the default face.
  1747. :background the background color, or \"Transparent\".
  1748. `default' means use the background of the default face.
  1749. :scale a scaling factor for the size of the images.
  1750. :html-foreground, :html-background, :html-scale
  1751. the same numbers for HTML export.
  1752. :matchers a list indicating which matchers should be used to
  1753. find LaTeX fragments. Valid members of this list are:
  1754. \"begin\" find environments
  1755. \"$\" find math expressions surrounded by $...$
  1756. \"$$\" find math expressions surrounded by $$....$$
  1757. \"\\(\" find math expressions surrounded by \\(...\\)
  1758. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1759. :group 'org-latex
  1760. :type 'plist)
  1761. (defcustom org-format-latex-header "\\documentclass{article}
  1762. \\usepackage{fullpage} % do not remove
  1763. \\usepackage{amssymb}
  1764. \\usepackage[usenames]{color}
  1765. \\usepackage{amsmath}
  1766. \\usepackage{latexsym}
  1767. \\usepackage[mathscr]{eucal}
  1768. \\pagestyle{empty} % do not remove"
  1769. "The document header used for processing LaTeX fragments."
  1770. :group 'org-latex
  1771. :type 'string)
  1772. (defgroup org-font-lock nil
  1773. "Font-lock settings for highlighting in Org-mode."
  1774. :tag "Org Font Lock"
  1775. :group 'org)
  1776. (defcustom org-level-color-stars-only nil
  1777. "Non-nil means fontify only the stars in each headline.
  1778. When nil, the entire headline is fontified.
  1779. Changing it requires restart of `font-lock-mode' to become effective
  1780. also in regions already fontified."
  1781. :group 'org-font-lock
  1782. :type 'boolean)
  1783. (defcustom org-hide-leading-stars nil
  1784. "Non-nil means, hide the first N-1 stars in a headline.
  1785. This works by using the face `org-hide' for these stars. This
  1786. face is white for a light background, and black for a dark
  1787. background. You may have to customize the face `org-hide' to
  1788. make this work.
  1789. Changing it requires restart of `font-lock-mode' to become effective
  1790. also in regions already fontified.
  1791. You may also set this on a per-file basis by adding one of the following
  1792. lines to the buffer:
  1793. #+STARTUP: hidestars
  1794. #+STARTUP: showstars"
  1795. :group 'org-font-lock
  1796. :type 'boolean)
  1797. (defcustom org-fontify-done-headline nil
  1798. "Non-nil means, change the face of a headline if it is marked DONE.
  1799. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1800. When this is non-nil, the headline after the keyword is set to the
  1801. `org-headline-done' as an additional indication."
  1802. :group 'org-font-lock
  1803. :type 'boolean)
  1804. (defcustom org-fontify-emphasized-text t
  1805. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1806. Changing this variable requires a restart of Emacs to take effect."
  1807. :group 'org-font-lock
  1808. :type 'boolean)
  1809. (defcustom org-highlight-latex-fragments-and-specials nil
  1810. "Non-nil means, fontify what is treated specially by the exporters."
  1811. :group 'org-font-lock
  1812. :type 'boolean)
  1813. (defcustom org-hide-emphasis-markers nil
  1814. "Non-nil mean font-lock should hide the emphasis marker characters."
  1815. :group 'org-font-lock
  1816. :type 'boolean)
  1817. (defvar org-emph-re nil
  1818. "Regular expression for matching emphasis.")
  1819. (defvar org-verbatim-re nil
  1820. "Regular expression for matching verbatim text.")
  1821. (defvar org-emphasis-regexp-components) ; defined just below
  1822. (defvar org-emphasis-alist) ; defined just below
  1823. (defun org-set-emph-re (var val)
  1824. "Set variable and compute the emphasis regular expression."
  1825. (set var val)
  1826. (when (and (boundp 'org-emphasis-alist)
  1827. (boundp 'org-emphasis-regexp-components)
  1828. org-emphasis-alist org-emphasis-regexp-components)
  1829. (let* ((e org-emphasis-regexp-components)
  1830. (pre (car e))
  1831. (post (nth 1 e))
  1832. (border (nth 2 e))
  1833. (body (nth 3 e))
  1834. (nl (nth 4 e))
  1835. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1836. (body1 (concat body "*?"))
  1837. (markers (mapconcat 'car org-emphasis-alist ""))
  1838. (vmarkers (mapconcat
  1839. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1840. org-emphasis-alist "")))
  1841. ;; make sure special characters appear at the right position in the class
  1842. (if (string-match "\\^" markers)
  1843. (setq markers (concat (replace-match "" t t markers) "^")))
  1844. (if (string-match "-" markers)
  1845. (setq markers (concat (replace-match "" t t markers) "-")))
  1846. (if (string-match "\\^" vmarkers)
  1847. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1848. (if (string-match "-" vmarkers)
  1849. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1850. (if (> nl 0)
  1851. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1852. (int-to-string nl) "\\}")))
  1853. ;; Make the regexp
  1854. (setq org-emph-re
  1855. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1856. "\\("
  1857. "\\([" markers "]\\)"
  1858. "\\("
  1859. "[^" border "]\\|"
  1860. "[^" border (if (and nil stacked) markers) "]"
  1861. body1
  1862. "[^" border (if (and nil stacked) markers) "]"
  1863. "\\)"
  1864. "\\3\\)"
  1865. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1866. (setq org-verbatim-re
  1867. (concat "\\([" pre "]\\|^\\)"
  1868. "\\("
  1869. "\\([" vmarkers "]\\)"
  1870. "\\("
  1871. "[^" border "]\\|"
  1872. "[^" border "]"
  1873. body1
  1874. "[^" border "]"
  1875. "\\)"
  1876. "\\3\\)"
  1877. "\\([" post "]\\|$\\)")))))
  1878. (defcustom org-emphasis-regexp-components
  1879. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1880. "Components used to build the regular expression for emphasis.
  1881. This is a list with 6 entries. Terminology: In an emphasis string
  1882. like \" *strong word* \", we call the initial space PREMATCH, the final
  1883. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1884. and \"trong wor\" is the body. The different components in this variable
  1885. specify what is allowed/forbidden in each part:
  1886. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1887. post Chars allowed as postmatch. End of line will be allowed too.
  1888. border The chars *forbidden* as border characters.
  1889. body-regexp A regexp like \".\" to match a body character. Don't use
  1890. non-shy groups here, and don't allow newline here.
  1891. newline The maximum number of newlines allowed in an emphasis exp.
  1892. Use customize to modify this, or restart Emacs after changing it."
  1893. :group 'org-font-lock
  1894. :set 'org-set-emph-re
  1895. :type '(list
  1896. (sexp :tag "Allowed chars in pre ")
  1897. (sexp :tag "Allowed chars in post ")
  1898. (sexp :tag "Forbidden chars in border ")
  1899. (sexp :tag "Regexp for body ")
  1900. (integer :tag "number of newlines allowed")
  1901. (option (boolean :tag "Stacking (DISABLED) "))))
  1902. (defcustom org-emphasis-alist
  1903. `(("*" bold "<b>" "</b>")
  1904. ("/" italic "<i>" "</i>")
  1905. ("_" underline "<u>" "</u>")
  1906. ("=" org-code "<code>" "</code>" verbatim)
  1907. ("~" org-verbatim "" "" verbatim)
  1908. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1909. "<del>" "</del>")
  1910. )
  1911. "Special syntax for emphasized text.
  1912. Text starting and ending with a special character will be emphasized, for
  1913. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1914. characters, the face to be used by font-lock for highlighting in Org-mode
  1915. Emacs buffers, and the HTML tags to be used for this.
  1916. Use customize to modify this, or restart Emacs after changing it."
  1917. :group 'org-font-lock
  1918. :set 'org-set-emph-re
  1919. :type '(repeat
  1920. (list
  1921. (string :tag "Marker character")
  1922. (choice
  1923. (face :tag "Font-lock-face")
  1924. (plist :tag "Face property list"))
  1925. (string :tag "HTML start tag")
  1926. (string :tag "HTML end tag")
  1927. (option (const verbatim)))))
  1928. ;;; Miscellaneous options
  1929. (defgroup org-completion nil
  1930. "Completion in Org-mode."
  1931. :tag "Org Completion"
  1932. :group 'org)
  1933. (defcustom org-completion-fallback-command 'hippie-expand
  1934. "The expansion command called by \\[org-complete] in normal context.
  1935. Normal means, no org-mode-specific context."
  1936. :group 'org-completion
  1937. :type 'function)
  1938. ;;; Functions and variables from ther packages
  1939. ;; Declared here to avoid compiler warnings
  1940. ;; XEmacs only
  1941. (defvar outline-mode-menu-heading)
  1942. (defvar outline-mode-menu-show)
  1943. (defvar outline-mode-menu-hide)
  1944. (defvar zmacs-regions) ; XEmacs regions
  1945. ;; Emacs only
  1946. (defvar mark-active)
  1947. ;; Various packages
  1948. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1949. (declare-function calendar-forward-day "cal-move" (arg))
  1950. (declare-function calendar-goto-date "cal-move" (date))
  1951. (declare-function calendar-goto-today "cal-move" ())
  1952. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1953. (defvar calc-embedded-close-formula)
  1954. (defvar calc-embedded-open-formula)
  1955. (declare-function cdlatex-tab "ext:cdlatex" ())
  1956. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1957. (defvar font-lock-unfontify-region-function)
  1958. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1959. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1960. (defvar iswitchb-temp-buflist)
  1961. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1962. (declare-function org-agenda-skip "org-agenda" ())
  1963. (declare-function org-format-agenda-item "org-agenda"
  1964. (extra txt &optional category tags dotime noprefix remove-re))
  1965. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1966. (declare-function org-agenda-change-all-lines "org-agenda"
  1967. (newhead hdmarker &optional fixface))
  1968. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1969. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1970. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1971. (beg end))
  1972. (declare-function parse-time-string "parse-time" (string))
  1973. (declare-function remember "remember" (&optional initial))
  1974. (declare-function remember-buffer-desc "remember" ())
  1975. (declare-function remember-finalize "remember" ())
  1976. (defvar remember-save-after-remembering)
  1977. (defvar remember-data-file)
  1978. (defvar remember-register)
  1979. (defvar remember-buffer)
  1980. (defvar remember-handler-functions)
  1981. (defvar remember-annotation-functions)
  1982. (defvar texmathp-why)
  1983. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1984. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1985. (defvar w3m-current-url)
  1986. (defvar w3m-current-title)
  1987. (defvar org-latex-regexps)
  1988. ;;; Autoload and prepare some org modules
  1989. ;; Some table stuff that needs to be defined here, because it is used
  1990. ;; by the functions setting up org-mode or checking for table context.
  1991. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1992. "Detects an org-type or table-type table.")
  1993. (defconst org-table-line-regexp "^[ \t]*|"
  1994. "Detects an org-type table line.")
  1995. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1996. "Detects an org-type table line.")
  1997. (defconst org-table-hline-regexp "^[ \t]*|-"
  1998. "Detects an org-type table hline.")
  1999. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2000. "Detects a table-type table hline.")
  2001. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2002. "Searching from within a table (any type) this finds the first line
  2003. outside the table.")
  2004. ;; Autoload the functions in org-table.el that are needed by functions here.
  2005. (eval-and-compile
  2006. (org-autoload "org-table"
  2007. '(org-table-align org-table-begin org-table-blank-field
  2008. org-table-convert org-table-convert-region org-table-copy-down
  2009. org-table-copy-region org-table-create
  2010. org-table-create-or-convert-from-region
  2011. org-table-create-with-table.el org-table-current-dline
  2012. org-table-cut-region org-table-delete-column org-table-edit-field
  2013. org-table-edit-formulas org-table-end org-table-eval-formula
  2014. org-table-export org-table-field-info
  2015. org-table-get-stored-formulas org-table-goto-column
  2016. org-table-hline-and-move org-table-import org-table-insert-column
  2017. org-table-insert-hline org-table-insert-row org-table-iterate
  2018. org-table-justify-field-maybe org-table-kill-row
  2019. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2020. org-table-move-column org-table-move-column-left
  2021. org-table-move-column-right org-table-move-row
  2022. org-table-move-row-down org-table-move-row-up
  2023. org-table-next-field org-table-next-row org-table-paste-rectangle
  2024. org-table-previous-field org-table-recalculate
  2025. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2026. org-table-toggle-coordinate-overlays
  2027. org-table-toggle-formula-debugger org-table-wrap-region
  2028. orgtbl-mode turn-on-orgtbl)))
  2029. (defun org-at-table-p (&optional table-type)
  2030. "Return t if the cursor is inside an org-type table.
  2031. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2032. (if org-enable-table-editor
  2033. (save-excursion
  2034. (beginning-of-line 1)
  2035. (looking-at (if table-type org-table-any-line-regexp
  2036. org-table-line-regexp)))
  2037. nil))
  2038. (defsubst org-table-p () (org-at-table-p))
  2039. (defun org-at-table.el-p ()
  2040. "Return t if and only if we are at a table.el table."
  2041. (and (org-at-table-p 'any)
  2042. (save-excursion
  2043. (goto-char (org-table-begin 'any))
  2044. (looking-at org-table1-hline-regexp))))
  2045. (defun org-table-recognize-table.el ()
  2046. "If there is a table.el table nearby, recognize it and move into it."
  2047. (if org-table-tab-recognizes-table.el
  2048. (if (org-at-table.el-p)
  2049. (progn
  2050. (beginning-of-line 1)
  2051. (if (looking-at org-table-dataline-regexp)
  2052. nil
  2053. (if (looking-at org-table1-hline-regexp)
  2054. (progn
  2055. (beginning-of-line 2)
  2056. (if (looking-at org-table-any-border-regexp)
  2057. (beginning-of-line -1)))))
  2058. (if (re-search-forward "|" (org-table-end t) t)
  2059. (progn
  2060. (require 'table)
  2061. (if (table--at-cell-p (point))
  2062. t
  2063. (message "recognizing table.el table...")
  2064. (table-recognize-table)
  2065. (message "recognizing table.el table...done")))
  2066. (error "This should not happen..."))
  2067. t)
  2068. nil)
  2069. nil))
  2070. (defun org-at-table-hline-p ()
  2071. "Return t if the cursor is inside a hline in a table."
  2072. (if org-enable-table-editor
  2073. (save-excursion
  2074. (beginning-of-line 1)
  2075. (looking-at org-table-hline-regexp))
  2076. nil))
  2077. (defvar org-table-clean-did-remove-column nil)
  2078. (defun org-table-map-tables (function)
  2079. "Apply FUNCTION to the start of all tables in the buffer."
  2080. (save-excursion
  2081. (save-restriction
  2082. (widen)
  2083. (goto-char (point-min))
  2084. (while (re-search-forward org-table-any-line-regexp nil t)
  2085. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2086. (beginning-of-line 1)
  2087. (if (looking-at org-table-line-regexp)
  2088. (save-excursion (funcall function)))
  2089. (re-search-forward org-table-any-border-regexp nil 1))))
  2090. (message "Mapping tables: done"))
  2091. ;; Declare and autoload functions from org-exp.el
  2092. (declare-function org-default-export-plist "org-exp")
  2093. (declare-function org-infile-export-plist "org-exp")
  2094. (declare-function org-get-current-options "org-exp")
  2095. (eval-and-compile
  2096. (org-autoload "org-exp"
  2097. '(org-export org-export-as-ascii org-export-visible
  2098. org-insert-export-options-template org-export-as-html-and-open
  2099. org-export-as-html-batch org-export-as-html-to-buffer
  2100. org-replace-region-by-html org-export-region-as-html
  2101. org-export-as-html org-export-icalendar-this-file
  2102. org-export-icalendar-all-agenda-files
  2103. org-table-clean-before-export
  2104. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2105. ;; Declare and autoload functions from org-exp.el
  2106. (eval-and-compile
  2107. (org-autoload "org-exp"
  2108. '(org-agenda org-agenda-list org-search-view
  2109. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2110. org-diary org-agenda-to-appt)))
  2111. ;; Autoload org-remember
  2112. (eval-and-compile
  2113. (org-autoload "org-remember"
  2114. '(org-remember-insinuate org-remember-annotation
  2115. org-remember-apply-template org-remember org-remember-handler)))
  2116. ;; Autoload org-clock.el
  2117. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2118. (beg end))
  2119. (declare-function org-update-mode-line "org-clock" ())
  2120. (defvar org-clock-start-time)
  2121. (defvar org-clock-marker (make-marker)
  2122. "Marker recording the last clock-in.")
  2123. (eval-and-compile
  2124. (org-autoload
  2125. "org-clock"
  2126. '(org-clock-in org-clock-out org-clock-cancel
  2127. org-clock-goto org-clock-sum org-clock-display
  2128. org-remove-clock-overlays org-clock-report
  2129. org-clocktable-shift org-dblock-write:clocktable
  2130. org-get-clocktable)))
  2131. (defun org-clock-update-time-maybe ()
  2132. "If this is a CLOCK line, update it and return t.
  2133. Otherwise, return nil."
  2134. (interactive)
  2135. (save-excursion
  2136. (beginning-of-line 1)
  2137. (skip-chars-forward " \t")
  2138. (when (looking-at org-clock-string)
  2139. (let ((re (concat "[ \t]*" org-clock-string
  2140. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2141. "\\([ \t]*=>.*\\)?\\)?"))
  2142. ts te h m s)
  2143. (cond
  2144. ((not (looking-at re))
  2145. nil)
  2146. ((not (match-end 2))
  2147. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2148. (> org-clock-marker (point))
  2149. (<= org-clock-marker (point-at-eol)))
  2150. ;; The clock is running here
  2151. (setq org-clock-start-time
  2152. (apply 'encode-time
  2153. (org-parse-time-string (match-string 1))))
  2154. (org-update-mode-line)))
  2155. (t
  2156. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2157. (end-of-line 1)
  2158. (setq ts (match-string 1)
  2159. te (match-string 3))
  2160. (setq s (- (time-to-seconds
  2161. (apply 'encode-time (org-parse-time-string te)))
  2162. (time-to-seconds
  2163. (apply 'encode-time (org-parse-time-string ts))))
  2164. h (floor (/ s 3600))
  2165. s (- s (* 3600 h))
  2166. m (floor (/ s 60))
  2167. s (- s (* 60 s)))
  2168. (insert " => " (format "%2d:%02d" h m))
  2169. t))))))
  2170. (defun org-check-running-clock ()
  2171. "Check if the current buffer contains the running clock.
  2172. If yes, offer to stop it and to save the buffer with the changes."
  2173. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2174. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2175. (buffer-name))))
  2176. (org-clock-out)
  2177. (when (y-or-n-p "Save changed buffer?")
  2178. (save-buffer))))
  2179. (defun org-clocktable-try-shift (dir n)
  2180. "Check if this line starts a clock table, if yes, shift the time block."
  2181. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2182. (org-clocktable-shift dir n)))
  2183. ;; Autoload archiving code
  2184. ;; The stuff that is needed for cycling and tags has to be defined here.
  2185. (defgroup org-archive nil
  2186. "Options concerning archiving in Org-mode."
  2187. :tag "Org Archive"
  2188. :group 'org-structure)
  2189. (defcustom org-archive-location "%s_archive::"
  2190. "The location where subtrees should be archived.
  2191. Otherwise, the value of this variable is a string, consisting of two
  2192. parts, separated by a double-colon.
  2193. The first part is a file name - when omitted, archiving happens in the same
  2194. file. %s will be replaced by the current file name (without directory part).
  2195. Archiving to a different file is useful to keep archived entries from
  2196. contributing to the Org-mode Agenda.
  2197. The part after the double colon is a headline. The archived entries will be
  2198. filed under that headline. When omitted, the subtrees are simply filed away
  2199. at the end of the file, as top-level entries.
  2200. Here are a few examples:
  2201. \"%s_archive::\"
  2202. If the current file is Projects.org, archive in file
  2203. Projects.org_archive, as top-level trees. This is the default.
  2204. \"::* Archived Tasks\"
  2205. Archive in the current file, under the top-level headline
  2206. \"* Archived Tasks\".
  2207. \"~/org/archive.org::\"
  2208. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2209. \"basement::** Finished Tasks\"
  2210. Archive in file ./basement (relative path), as level 3 trees
  2211. below the level 2 heading \"** Finished Tasks\".
  2212. You may set this option on a per-file basis by adding to the buffer a
  2213. line like
  2214. #+ARCHIVE: basement::** Finished Tasks
  2215. You may also define it locally for a subtree by setting an ARCHIVE property
  2216. in the entry. If such a property is found in an entry, or anywhere up
  2217. the hierarchy, it will be used."
  2218. :group 'org-archive
  2219. :type 'string)
  2220. (defcustom org-archive-tag "ARCHIVE"
  2221. "The tag that marks a subtree as archived.
  2222. An archived subtree does not open during visibility cycling, and does
  2223. not contribute to the agenda listings.
  2224. After changing this, font-lock must be restarted in the relevant buffers to
  2225. get the proper fontification."
  2226. :group 'org-archive
  2227. :group 'org-keywords
  2228. :type 'string)
  2229. (defcustom org-agenda-skip-archived-trees t
  2230. "Non-nil means, the agenda will skip any items located in archived trees.
  2231. An archived tree is a tree marked with the tag ARCHIVE."
  2232. :group 'org-archive
  2233. :group 'org-agenda-skip
  2234. :type 'boolean)
  2235. (defcustom org-cycle-open-archived-trees nil
  2236. "Non-nil means, `org-cycle' will open archived trees.
  2237. An archived tree is a tree marked with the tag ARCHIVE.
  2238. When nil, archived trees will stay folded. You can still open them with
  2239. normal outline commands like `show-all', but not with the cycling commands."
  2240. :group 'org-archive
  2241. :group 'org-cycle
  2242. :type 'boolean)
  2243. (defcustom org-sparse-tree-open-archived-trees nil
  2244. "Non-nil means sparse tree construction shows matches in archived trees.
  2245. When nil, matches in these trees are highlighted, but the trees are kept in
  2246. collapsed state."
  2247. :group 'org-archive
  2248. :group 'org-sparse-trees
  2249. :type 'boolean)
  2250. (defun org-cycle-hide-archived-subtrees (state)
  2251. "Re-hide all archived subtrees after a visibility state change."
  2252. (when (and (not org-cycle-open-archived-trees)
  2253. (not (memq state '(overview folded))))
  2254. (save-excursion
  2255. (let* ((globalp (memq state '(contents all)))
  2256. (beg (if globalp (point-min) (point)))
  2257. (end (if globalp (point-max) (org-end-of-subtree t))))
  2258. (org-hide-archived-subtrees beg end)
  2259. (goto-char beg)
  2260. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2261. (message "%s" (substitute-command-keys
  2262. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2263. (defun org-force-cycle-archived ()
  2264. "Cycle subtree even if it is archived."
  2265. (interactive)
  2266. (setq this-command 'org-cycle)
  2267. (let ((org-cycle-open-archived-trees t))
  2268. (call-interactively 'org-cycle)))
  2269. (defun org-hide-archived-subtrees (beg end)
  2270. "Re-hide all archived subtrees after a visibility state change."
  2271. (save-excursion
  2272. (let* ((re (concat ":" org-archive-tag ":")))
  2273. (goto-char beg)
  2274. (while (re-search-forward re end t)
  2275. (and (org-on-heading-p) (hide-subtree))
  2276. (org-end-of-subtree t)))))
  2277. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2278. (eval-and-compile
  2279. (org-autoload "org-archive"
  2280. '(org-add-archive-files org-archive-subtree
  2281. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2282. ;; Autoload Column View Code
  2283. (declare-function org-columns-number-to-string "org-colview")
  2284. (declare-function org-columns-get-format-and-top-level "org-colview")
  2285. (declare-function org-columns-compute "org-colview")
  2286. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2287. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2288. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2289. org-columns org-insert-columns-dblock))
  2290. ;; Autoload ID code
  2291. (org-autoload "org-id"
  2292. '(org-id-get-create org-id-new org-id-copy org-id-get
  2293. org-id-get-with-outline-path-completion
  2294. org-id-get-with-outline-drilling
  2295. org-id-goto org-id-find))
  2296. ;;; Variables for pre-computed regular expressions, all buffer local
  2297. (defvar org-drawer-regexp nil
  2298. "Matches first line of a hidden block.")
  2299. (make-variable-buffer-local 'org-drawer-regexp)
  2300. (defvar org-todo-regexp nil
  2301. "Matches any of the TODO state keywords.")
  2302. (make-variable-buffer-local 'org-todo-regexp)
  2303. (defvar org-not-done-regexp nil
  2304. "Matches any of the TODO state keywords except the last one.")
  2305. (make-variable-buffer-local 'org-not-done-regexp)
  2306. (defvar org-todo-line-regexp nil
  2307. "Matches a headline and puts TODO state into group 2 if present.")
  2308. (make-variable-buffer-local 'org-todo-line-regexp)
  2309. (defvar org-complex-heading-regexp nil
  2310. "Matches a headline and puts everything into groups:
  2311. group 1: the stars
  2312. group 2: The todo keyword, maybe
  2313. group 3: Priority cookie
  2314. group 4: True headline
  2315. group 5: Tags")
  2316. (make-variable-buffer-local 'org-complex-heading-regexp)
  2317. (defvar org-todo-line-tags-regexp nil
  2318. "Matches a headline and puts TODO state into group 2 if present.
  2319. Also put tags into group 4 if tags are present.")
  2320. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2321. (defvar org-nl-done-regexp nil
  2322. "Matches newline followed by a headline with the DONE keyword.")
  2323. (make-variable-buffer-local 'org-nl-done-regexp)
  2324. (defvar org-looking-at-done-regexp nil
  2325. "Matches the DONE keyword a point.")
  2326. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2327. (defvar org-ds-keyword-length 12
  2328. "Maximum length of the Deadline and SCHEDULED keywords.")
  2329. (make-variable-buffer-local 'org-ds-keyword-length)
  2330. (defvar org-deadline-regexp nil
  2331. "Matches the DEADLINE keyword.")
  2332. (make-variable-buffer-local 'org-deadline-regexp)
  2333. (defvar org-deadline-time-regexp nil
  2334. "Matches the DEADLINE keyword together with a time stamp.")
  2335. (make-variable-buffer-local 'org-deadline-time-regexp)
  2336. (defvar org-deadline-line-regexp nil
  2337. "Matches the DEADLINE keyword and the rest of the line.")
  2338. (make-variable-buffer-local 'org-deadline-line-regexp)
  2339. (defvar org-scheduled-regexp nil
  2340. "Matches the SCHEDULED keyword.")
  2341. (make-variable-buffer-local 'org-scheduled-regexp)
  2342. (defvar org-scheduled-time-regexp nil
  2343. "Matches the SCHEDULED keyword together with a time stamp.")
  2344. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2345. (defvar org-closed-time-regexp nil
  2346. "Matches the CLOSED keyword together with a time stamp.")
  2347. (make-variable-buffer-local 'org-closed-time-regexp)
  2348. (defvar org-keyword-time-regexp nil
  2349. "Matches any of the 4 keywords, together with the time stamp.")
  2350. (make-variable-buffer-local 'org-keyword-time-regexp)
  2351. (defvar org-keyword-time-not-clock-regexp nil
  2352. "Matches any of the 3 keywords, together with the time stamp.")
  2353. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2354. (defvar org-maybe-keyword-time-regexp nil
  2355. "Matches a timestamp, possibly preceeded by a keyword.")
  2356. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2357. (defvar org-planning-or-clock-line-re nil
  2358. "Matches a line with planning or clock info.")
  2359. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2360. (defconst org-plain-time-of-day-regexp
  2361. (concat
  2362. "\\(\\<[012]?[0-9]"
  2363. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2364. "\\(--?"
  2365. "\\(\\<[012]?[0-9]"
  2366. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2367. "\\)?")
  2368. "Regular expression to match a plain time or time range.
  2369. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2370. groups carry important information:
  2371. 0 the full match
  2372. 1 the first time, range or not
  2373. 8 the second time, if it is a range.")
  2374. (defconst org-plain-time-extension-regexp
  2375. (concat
  2376. "\\(\\<[012]?[0-9]"
  2377. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2378. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2379. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2380. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2381. groups carry important information:
  2382. 0 the full match
  2383. 7 hours of duration
  2384. 9 minutes of duration")
  2385. (defconst org-stamp-time-of-day-regexp
  2386. (concat
  2387. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2388. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2389. "\\(--?"
  2390. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2391. "Regular expression to match a timestamp time or time range.
  2392. After a match, the following groups carry important information:
  2393. 0 the full match
  2394. 1 date plus weekday, for backreferencing to make sure both times on same day
  2395. 2 the first time, range or not
  2396. 4 the second time, if it is a range.")
  2397. (defconst org-startup-options
  2398. '(("fold" org-startup-folded t)
  2399. ("overview" org-startup-folded t)
  2400. ("nofold" org-startup-folded nil)
  2401. ("showall" org-startup-folded nil)
  2402. ("content" org-startup-folded content)
  2403. ("hidestars" org-hide-leading-stars t)
  2404. ("showstars" org-hide-leading-stars nil)
  2405. ("indent" org-adapt-indentation t)
  2406. ("noindent" org-adapt-indentation nil)
  2407. ("odd" org-odd-levels-only t)
  2408. ("oddeven" org-odd-levels-only nil)
  2409. ("align" org-startup-align-all-tables t)
  2410. ("noalign" org-startup-align-all-tables nil)
  2411. ("customtime" org-display-custom-times t)
  2412. ("logdone" org-log-done time)
  2413. ("lognotedone" org-log-done note)
  2414. ("nologdone" org-log-done nil)
  2415. ("lognoteclock-out" org-log-note-clock-out t)
  2416. ("nolognoteclock-out" org-log-note-clock-out nil)
  2417. ("logrepeat" org-log-repeat state)
  2418. ("lognoterepeat" org-log-repeat note)
  2419. ("nologrepeat" org-log-repeat nil)
  2420. ("constcgs" constants-unit-system cgs)
  2421. ("constSI" constants-unit-system SI))
  2422. "Variable associated with STARTUP options for org-mode.
  2423. Each element is a list of three items: The startup options as written
  2424. in the #+STARTUP line, the corresponding variable, and the value to
  2425. set this variable to if the option is found. An optional forth element PUSH
  2426. means to push this value onto the list in the variable.")
  2427. (defun org-set-regexps-and-options ()
  2428. "Precompute regular expressions for current buffer."
  2429. (when (org-mode-p)
  2430. (org-set-local 'org-todo-kwd-alist nil)
  2431. (org-set-local 'org-todo-key-alist nil)
  2432. (org-set-local 'org-todo-key-trigger nil)
  2433. (org-set-local 'org-todo-keywords-1 nil)
  2434. (org-set-local 'org-done-keywords nil)
  2435. (org-set-local 'org-todo-heads nil)
  2436. (org-set-local 'org-todo-sets nil)
  2437. (org-set-local 'org-todo-log-states nil)
  2438. (let ((re (org-make-options-regexp
  2439. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2440. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2441. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2442. (splitre "[ \t]+")
  2443. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2444. tail sep kws1 prio props ftags drawers
  2445. ext-setup-or-nil setup-contents (start 0))
  2446. (save-excursion
  2447. (save-restriction
  2448. (widen)
  2449. (goto-char (point-min))
  2450. (while (or (and ext-setup-or-nil
  2451. (string-match re ext-setup-or-nil start)
  2452. (setq start (match-end 0)))
  2453. (and (setq ext-setup-or-nil nil start 0)
  2454. (re-search-forward re nil t)))
  2455. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2456. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2457. (cond
  2458. ((equal key "CATEGORY")
  2459. (if (string-match "[ \t]+$" value)
  2460. (setq value (replace-match "" t t value)))
  2461. (setq cat value))
  2462. ((member key '("SEQ_TODO" "TODO"))
  2463. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2464. ((equal key "TYP_TODO")
  2465. (push (cons 'type (org-split-string value splitre)) kwds))
  2466. ((equal key "TAGS")
  2467. (setq tags (append tags (org-split-string value splitre))))
  2468. ((equal key "COLUMNS")
  2469. (org-set-local 'org-columns-default-format value))
  2470. ((equal key "LINK")
  2471. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2472. (push (cons (match-string 1 value)
  2473. (org-trim (match-string 2 value)))
  2474. links)))
  2475. ((equal key "PRIORITIES")
  2476. (setq prio (org-split-string value " +")))
  2477. ((equal key "PROPERTY")
  2478. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2479. (push (cons (match-string 1 value) (match-string 2 value))
  2480. props)))
  2481. ((equal key "FILETAGS")
  2482. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2483. (setq ftags
  2484. (append
  2485. ftags
  2486. (apply 'append
  2487. (mapcar (lambda (x) (org-split-string x ":"))
  2488. (org-split-string value)))))))
  2489. ((equal key "DRAWERS")
  2490. (setq drawers (org-split-string value splitre)))
  2491. ((equal key "CONSTANTS")
  2492. (setq const (append const (org-split-string value splitre))))
  2493. ((equal key "STARTUP")
  2494. (let ((opts (org-split-string value splitre))
  2495. l var val)
  2496. (while (setq l (pop opts))
  2497. (when (setq l (assoc l org-startup-options))
  2498. (setq var (nth 1 l) val (nth 2 l))
  2499. (if (not (nth 3 l))
  2500. (set (make-local-variable var) val)
  2501. (if (not (listp (symbol-value var)))
  2502. (set (make-local-variable var) nil))
  2503. (set (make-local-variable var) (symbol-value var))
  2504. (add-to-list var val))))))
  2505. ((equal key "ARCHIVE")
  2506. (string-match " *$" value)
  2507. (setq arch (replace-match "" t t value))
  2508. (remove-text-properties 0 (length arch)
  2509. '(face t fontified t) arch))
  2510. ((equal key "SETUPFILE")
  2511. (setq setup-contents (org-file-contents
  2512. (expand-file-name
  2513. (org-remove-double-quotes value))
  2514. 'noerror))
  2515. (if (not ext-setup-or-nil)
  2516. (setq ext-setup-or-nil setup-contents start 0)
  2517. (setq ext-setup-or-nil
  2518. (concat (substring ext-setup-or-nil 0 start)
  2519. "\n" setup-contents "\n"
  2520. (substring ext-setup-or-nil start)))))
  2521. ))))
  2522. (when cat
  2523. (org-set-local 'org-category (intern cat))
  2524. (push (cons "CATEGORY" cat) props))
  2525. (when prio
  2526. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2527. (setq prio (mapcar 'string-to-char prio))
  2528. (org-set-local 'org-highest-priority (nth 0 prio))
  2529. (org-set-local 'org-lowest-priority (nth 1 prio))
  2530. (org-set-local 'org-default-priority (nth 2 prio)))
  2531. (and props (org-set-local 'org-file-properties (nreverse props)))
  2532. (and ftags (org-set-local 'org-file-tags ftags))
  2533. (and drawers (org-set-local 'org-drawers drawers))
  2534. (and arch (org-set-local 'org-archive-location arch))
  2535. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2536. ;; Process the TODO keywords
  2537. (unless kwds
  2538. ;; Use the global values as if they had been given locally.
  2539. (setq kwds (default-value 'org-todo-keywords))
  2540. (if (stringp (car kwds))
  2541. (setq kwds (list (cons org-todo-interpretation
  2542. (default-value 'org-todo-keywords)))))
  2543. (setq kwds (reverse kwds)))
  2544. (setq kwds (nreverse kwds))
  2545. (let (inter kws kw)
  2546. (while (setq kws (pop kwds))
  2547. (setq inter (pop kws) sep (member "|" kws)
  2548. kws0 (delete "|" (copy-sequence kws))
  2549. kwsa nil
  2550. kws1 (mapcar
  2551. (lambda (x)
  2552. ;; 1 2
  2553. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2554. (progn
  2555. (setq kw (match-string 1 x)
  2556. key (and (match-end 2) (match-string 2 x))
  2557. log (org-extract-log-state-settings x))
  2558. (push (cons kw (and key (string-to-char key))) kwsa)
  2559. (and log (push log org-todo-log-states))
  2560. kw)
  2561. (error "Invalid TODO keyword %s" x)))
  2562. kws0)
  2563. kwsa (if kwsa (append '((:startgroup))
  2564. (nreverse kwsa)
  2565. '((:endgroup))))
  2566. hw (car kws1)
  2567. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2568. tail (list inter hw (car dws) (org-last dws)))
  2569. (add-to-list 'org-todo-heads hw 'append)
  2570. (push kws1 org-todo-sets)
  2571. (setq org-done-keywords (append org-done-keywords dws nil))
  2572. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2573. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2574. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2575. (setq org-todo-sets (nreverse org-todo-sets)
  2576. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2577. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2578. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2579. ;; Process the constants
  2580. (when const
  2581. (let (e cst)
  2582. (while (setq e (pop const))
  2583. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2584. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2585. (setq org-table-formula-constants-local cst)))
  2586. ;; Process the tags.
  2587. (when tags
  2588. (let (e tgs)
  2589. (while (setq e (pop tags))
  2590. (cond
  2591. ((equal e "{") (push '(:startgroup) tgs))
  2592. ((equal e "}") (push '(:endgroup) tgs))
  2593. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2594. (push (cons (match-string 1 e)
  2595. (string-to-char (match-string 2 e)))
  2596. tgs))
  2597. (t (push (list e) tgs))))
  2598. (org-set-local 'org-tag-alist nil)
  2599. (while (setq e (pop tgs))
  2600. (or (and (stringp (car e))
  2601. (assoc (car e) org-tag-alist))
  2602. (push e org-tag-alist)))))
  2603. ;; Compute the regular expressions and other local variables
  2604. (if (not org-done-keywords)
  2605. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2606. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2607. (length org-scheduled-string)
  2608. (length org-clock-string)
  2609. (length org-closed-string)))
  2610. org-drawer-regexp
  2611. (concat "^[ \t]*:\\("
  2612. (mapconcat 'regexp-quote org-drawers "\\|")
  2613. "\\):[ \t]*$")
  2614. org-not-done-keywords
  2615. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2616. org-todo-regexp
  2617. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2618. "\\|") "\\)\\>")
  2619. org-not-done-regexp
  2620. (concat "\\<\\("
  2621. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2622. "\\)\\>")
  2623. org-todo-line-regexp
  2624. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2625. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2626. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2627. org-complex-heading-regexp
  2628. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2629. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2630. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2631. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2632. org-nl-done-regexp
  2633. (concat "\n\\*+[ \t]+"
  2634. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2635. "\\)" "\\>")
  2636. org-todo-line-tags-regexp
  2637. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2638. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2639. (org-re
  2640. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2641. org-looking-at-done-regexp
  2642. (concat "^" "\\(?:"
  2643. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2644. "\\>")
  2645. org-deadline-regexp (concat "\\<" org-deadline-string)
  2646. org-deadline-time-regexp
  2647. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2648. org-deadline-line-regexp
  2649. (concat "\\<\\(" org-deadline-string "\\).*")
  2650. org-scheduled-regexp
  2651. (concat "\\<" org-scheduled-string)
  2652. org-scheduled-time-regexp
  2653. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2654. org-closed-time-regexp
  2655. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2656. org-keyword-time-regexp
  2657. (concat "\\<\\(" org-scheduled-string
  2658. "\\|" org-deadline-string
  2659. "\\|" org-closed-string
  2660. "\\|" org-clock-string "\\)"
  2661. " *[[<]\\([^]>]+\\)[]>]")
  2662. org-keyword-time-not-clock-regexp
  2663. (concat "\\<\\(" org-scheduled-string
  2664. "\\|" org-deadline-string
  2665. "\\|" org-closed-string
  2666. "\\)"
  2667. " *[[<]\\([^]>]+\\)[]>]")
  2668. org-maybe-keyword-time-regexp
  2669. (concat "\\(\\<\\(" org-scheduled-string
  2670. "\\|" org-deadline-string
  2671. "\\|" org-closed-string
  2672. "\\|" org-clock-string "\\)\\)?"
  2673. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2674. org-planning-or-clock-line-re
  2675. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2676. "\\|" org-deadline-string
  2677. "\\|" org-closed-string "\\|" org-clock-string
  2678. "\\)\\>\\)")
  2679. )
  2680. (org-compute-latex-and-specials-regexp)
  2681. (org-set-font-lock-defaults))))
  2682. (defun org-file-contents (file &optional noerror)
  2683. "Return the contents of FILE, as a string."
  2684. (if (or (not file)
  2685. (not (file-readable-p file)))
  2686. (if noerror
  2687. (progn
  2688. (message "Cannot read file %s" file)
  2689. (ding) (sit-for 2)
  2690. "")
  2691. (error "Cannot read file %s" file))
  2692. (with-temp-buffer
  2693. (insert-file-contents file)
  2694. (buffer-string))))
  2695. (defun org-extract-log-state-settings (x)
  2696. "Extract the log state setting from a TODO keyword string.
  2697. This will extract info from a string like \"WAIT(w@/!)\"."
  2698. (let (kw key log1 log2)
  2699. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2700. (setq kw (match-string 1 x)
  2701. key (and (match-end 2) (match-string 2 x))
  2702. log1 (and (match-end 3) (match-string 3 x))
  2703. log2 (and (match-end 4) (match-string 4 x)))
  2704. (and (or log1 log2)
  2705. (list kw
  2706. (and log1 (if (equal log1 "!") 'time 'note))
  2707. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2708. (defun org-remove-keyword-keys (list)
  2709. "Remove a pair of parenthesis at the end of each string in LIST."
  2710. (mapcar (lambda (x)
  2711. (if (string-match "(.*)$" x)
  2712. (substring x 0 (match-beginning 0))
  2713. x))
  2714. list))
  2715. ;; FIXME: this could be done much better, using second characters etc.
  2716. (defun org-assign-fast-keys (alist)
  2717. "Assign fast keys to a keyword-key alist.
  2718. Respect keys that are already there."
  2719. (let (new e k c c1 c2 (char ?a))
  2720. (while (setq e (pop alist))
  2721. (cond
  2722. ((equal e '(:startgroup)) (push e new))
  2723. ((equal e '(:endgroup)) (push e new))
  2724. (t
  2725. (setq k (car e) c2 nil)
  2726. (if (cdr e)
  2727. (setq c (cdr e))
  2728. ;; automatically assign a character.
  2729. (setq c1 (string-to-char
  2730. (downcase (substring
  2731. k (if (= (string-to-char k) ?@) 1 0)))))
  2732. (if (or (rassoc c1 new) (rassoc c1 alist))
  2733. (while (or (rassoc char new) (rassoc char alist))
  2734. (setq char (1+ char)))
  2735. (setq c2 c1))
  2736. (setq c (or c2 char)))
  2737. (push (cons k c) new))))
  2738. (nreverse new)))
  2739. ;;; Some variables used in various places
  2740. (defvar org-window-configuration nil
  2741. "Used in various places to store a window configuration.")
  2742. (defvar org-finish-function nil
  2743. "Function to be called when `C-c C-c' is used.
  2744. This is for getting out of special buffers like remember.")
  2745. ;; FIXME: Occasionally check by commenting these, to make sure
  2746. ;; no other functions uses these, forgetting to let-bind them.
  2747. (defvar entry)
  2748. (defvar state)
  2749. (defvar last-state)
  2750. (defvar date)
  2751. (defvar description)
  2752. ;; Defined somewhere in this file, but used before definition.
  2753. (defvar org-html-entities)
  2754. (defvar org-struct-menu)
  2755. (defvar org-org-menu)
  2756. (defvar org-tbl-menu)
  2757. (defvar org-agenda-keymap)
  2758. ;;;; Define the Org-mode
  2759. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2760. (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."))
  2761. ;; We use a before-change function to check if a table might need
  2762. ;; an update.
  2763. (defvar org-table-may-need-update t
  2764. "Indicates that a table might need an update.
  2765. This variable is set by `org-before-change-function'.
  2766. `org-table-align' sets it back to nil.")
  2767. (defun org-before-change-function (beg end)
  2768. "Every change indicates that a table might need an update."
  2769. (setq org-table-may-need-update t))
  2770. (defvar org-mode-map)
  2771. (defvar org-mode-hook nil)
  2772. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2773. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2774. (defvar org-table-buffer-is-an nil)
  2775. (defconst org-outline-regexp "\\*+ ")
  2776. ;;;###autoload
  2777. (define-derived-mode org-mode outline-mode "Org"
  2778. "Outline-based notes management and organizer, alias
  2779. \"Carsten's outline-mode for keeping track of everything.\"
  2780. Org-mode develops organizational tasks around a NOTES file which
  2781. contains information about projects as plain text. Org-mode is
  2782. implemented on top of outline-mode, which is ideal to keep the content
  2783. of large files well structured. It supports ToDo items, deadlines and
  2784. time stamps, which magically appear in the diary listing of the Emacs
  2785. calendar. Tables are easily created with a built-in table editor.
  2786. Plain text URL-like links connect to websites, emails (VM), Usenet
  2787. messages (Gnus), BBDB entries, and any files related to the project.
  2788. For printing and sharing of notes, an Org-mode file (or a part of it)
  2789. can be exported as a structured ASCII or HTML file.
  2790. The following commands are available:
  2791. \\{org-mode-map}"
  2792. ;; Get rid of Outline menus, they are not needed
  2793. ;; Need to do this here because define-derived-mode sets up
  2794. ;; the keymap so late. Still, it is a waste to call this each time
  2795. ;; we switch another buffer into org-mode.
  2796. (if (featurep 'xemacs)
  2797. (when (boundp 'outline-mode-menu-heading)
  2798. ;; Assume this is Greg's port, it used easymenu
  2799. (easy-menu-remove outline-mode-menu-heading)
  2800. (easy-menu-remove outline-mode-menu-show)
  2801. (easy-menu-remove outline-mode-menu-hide))
  2802. (define-key org-mode-map [menu-bar headings] 'undefined)
  2803. (define-key org-mode-map [menu-bar hide] 'undefined)
  2804. (define-key org-mode-map [menu-bar show] 'undefined))
  2805. (org-load-modules-maybe)
  2806. (easy-menu-add org-org-menu)
  2807. (easy-menu-add org-tbl-menu)
  2808. (org-install-agenda-files-menu)
  2809. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2810. (org-add-to-invisibility-spec '(org-cwidth))
  2811. (when (featurep 'xemacs)
  2812. (org-set-local 'line-move-ignore-invisible t))
  2813. (org-set-local 'outline-regexp org-outline-regexp)
  2814. (org-set-local 'outline-level 'org-outline-level)
  2815. (when (and org-ellipsis
  2816. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2817. (fboundp 'make-glyph-code))
  2818. (unless org-display-table
  2819. (setq org-display-table (make-display-table)))
  2820. (set-display-table-slot
  2821. org-display-table 4
  2822. (vconcat (mapcar
  2823. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2824. org-ellipsis)))
  2825. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2826. (setq buffer-display-table org-display-table))
  2827. (org-set-regexps-and-options)
  2828. ;; Calc embedded
  2829. (org-set-local 'calc-embedded-open-mode "# ")
  2830. (modify-syntax-entry ?# "<")
  2831. (modify-syntax-entry ?@ "w")
  2832. (if org-startup-truncated (setq truncate-lines t))
  2833. (org-set-local 'font-lock-unfontify-region-function
  2834. 'org-unfontify-region)
  2835. ;; Activate before-change-function
  2836. (org-set-local 'org-table-may-need-update t)
  2837. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2838. 'local)
  2839. ;; Check for running clock before killing a buffer
  2840. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2841. ;; Paragraphs and auto-filling
  2842. (org-set-autofill-regexps)
  2843. (setq indent-line-function 'org-indent-line-function)
  2844. (org-update-radio-target-regexp)
  2845. ;; Comment characters
  2846. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2847. (org-set-local 'comment-padding " ")
  2848. ;; Align options lines
  2849. (org-set-local
  2850. 'align-mode-rules-list
  2851. '((org-in-buffer-settings
  2852. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2853. (modes . '(org-mode)))))
  2854. ;; Imenu
  2855. (org-set-local 'imenu-create-index-function
  2856. 'org-imenu-get-tree)
  2857. ;; Make isearch reveal context
  2858. (if (or (featurep 'xemacs)
  2859. (not (boundp 'outline-isearch-open-invisible-function)))
  2860. ;; Emacs 21 and XEmacs make use of the hook
  2861. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2862. ;; Emacs 22 deals with this through a special variable
  2863. (org-set-local 'outline-isearch-open-invisible-function
  2864. (lambda (&rest ignore) (org-show-context 'isearch))))
  2865. ;; If empty file that did not turn on org-mode automatically, make it to.
  2866. (if (and org-insert-mode-line-in-empty-file
  2867. (interactive-p)
  2868. (= (point-min) (point-max)))
  2869. (insert "# -*- mode: org -*-\n\n"))
  2870. (unless org-inhibit-startup
  2871. (when org-startup-align-all-tables
  2872. (let ((bmp (buffer-modified-p)))
  2873. (org-table-map-tables 'org-table-align)
  2874. (set-buffer-modified-p bmp)))
  2875. (org-set-startup-visibility)))
  2876. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2877. (defun org-current-time ()
  2878. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2879. (if (> (car org-time-stamp-rounding-minutes) 1)
  2880. (let ((r (car org-time-stamp-rounding-minutes))
  2881. (time (decode-time)))
  2882. (apply 'encode-time
  2883. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2884. (nthcdr 2 time))))
  2885. (current-time)))
  2886. ;;;; Font-Lock stuff, including the activators
  2887. (defvar org-mouse-map (make-sparse-keymap))
  2888. (org-defkey org-mouse-map
  2889. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2890. (org-defkey org-mouse-map
  2891. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2892. (when org-mouse-1-follows-link
  2893. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2894. (when org-tab-follows-link
  2895. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2896. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2897. (when org-return-follows-link
  2898. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2899. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2900. (require 'font-lock)
  2901. (defconst org-non-link-chars "]\t\n\r<>")
  2902. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2903. "shell" "elisp"))
  2904. (defvar org-link-types-re nil
  2905. "Matches a link that has a url-like prefix like \"http:\"")
  2906. (defvar org-link-re-with-space nil
  2907. "Matches a link with spaces, optional angular brackets around it.")
  2908. (defvar org-link-re-with-space2 nil
  2909. "Matches a link with spaces, optional angular brackets around it.")
  2910. (defvar org-angle-link-re nil
  2911. "Matches link with angular brackets, spaces are allowed.")
  2912. (defvar org-plain-link-re nil
  2913. "Matches plain link, without spaces.")
  2914. (defvar org-bracket-link-regexp nil
  2915. "Matches a link in double brackets.")
  2916. (defvar org-bracket-link-analytic-regexp nil
  2917. "Regular expression used to analyze links.
  2918. Here is what the match groups contain after a match:
  2919. 1: http:
  2920. 2: http
  2921. 3: path
  2922. 4: [desc]
  2923. 5: desc")
  2924. (defvar org-any-link-re nil
  2925. "Regular expression matching any link.")
  2926. (defun org-make-link-regexps ()
  2927. "Update the link regular expressions.
  2928. This should be called after the variable `org-link-types' has changed."
  2929. (setq org-link-types-re
  2930. (concat
  2931. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2932. org-link-re-with-space
  2933. (concat
  2934. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2935. "\\([^" org-non-link-chars " ]"
  2936. "[^" org-non-link-chars "]*"
  2937. "[^" org-non-link-chars " ]\\)>?")
  2938. org-link-re-with-space2
  2939. (concat
  2940. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2941. "\\([^" org-non-link-chars " ]"
  2942. "[^]\t\n\r]*"
  2943. "[^" org-non-link-chars " ]\\)>?")
  2944. org-angle-link-re
  2945. (concat
  2946. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2947. "\\([^" org-non-link-chars " ]"
  2948. "[^" org-non-link-chars "]*"
  2949. "\\)>")
  2950. org-plain-link-re
  2951. (concat
  2952. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2953. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2954. org-bracket-link-regexp
  2955. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2956. org-bracket-link-analytic-regexp
  2957. (concat
  2958. "\\[\\["
  2959. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2960. "\\([^]]+\\)"
  2961. "\\]"
  2962. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2963. "\\]")
  2964. org-any-link-re
  2965. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2966. org-angle-link-re "\\)\\|\\("
  2967. org-plain-link-re "\\)")))
  2968. (org-make-link-regexps)
  2969. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2970. "Regular expression for fast time stamp matching.")
  2971. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2972. "Regular expression for fast time stamp matching.")
  2973. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2974. "Regular expression matching time strings for analysis.
  2975. This one does not require the space after the date, so it can be used
  2976. on a string that terminates immediately after the date.")
  2977. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2978. "Regular expression matching time strings for analysis.")
  2979. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2980. "Regular expression matching time stamps, with groups.")
  2981. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2982. "Regular expression matching time stamps (also [..]), with groups.")
  2983. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2984. "Regular expression matching a time stamp range.")
  2985. (defconst org-tr-regexp-both
  2986. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2987. "Regular expression matching a time stamp range.")
  2988. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2989. org-ts-regexp "\\)?")
  2990. "Regular expression matching a time stamp or time stamp range.")
  2991. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2992. org-ts-regexp-both "\\)?")
  2993. "Regular expression matching a time stamp or time stamp range.
  2994. The time stamps may be either active or inactive.")
  2995. (defvar org-emph-face nil)
  2996. (defun org-do-emphasis-faces (limit)
  2997. "Run through the buffer and add overlays to links."
  2998. (let (rtn)
  2999. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3000. (if (not (= (char-after (match-beginning 3))
  3001. (char-after (match-beginning 4))))
  3002. (progn
  3003. (setq rtn t)
  3004. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3005. 'face
  3006. (nth 1 (assoc (match-string 3)
  3007. org-emphasis-alist)))
  3008. (add-text-properties (match-beginning 2) (match-end 2)
  3009. '(font-lock-multiline t))
  3010. (when org-hide-emphasis-markers
  3011. (add-text-properties (match-end 4) (match-beginning 5)
  3012. '(invisible org-link))
  3013. (add-text-properties (match-beginning 3) (match-end 3)
  3014. '(invisible org-link)))))
  3015. (backward-char 1))
  3016. rtn))
  3017. (defun org-emphasize (&optional char)
  3018. "Insert or change an emphasis, i.e. a font like bold or italic.
  3019. If there is an active region, change that region to a new emphasis.
  3020. If there is no region, just insert the marker characters and position
  3021. the cursor between them.
  3022. CHAR should be either the marker character, or the first character of the
  3023. HTML tag associated with that emphasis. If CHAR is a space, the means
  3024. to remove the emphasis of the selected region.
  3025. If char is not given (for example in an interactive call) it
  3026. will be prompted for."
  3027. (interactive)
  3028. (let ((eal org-emphasis-alist) e det
  3029. (erc org-emphasis-regexp-components)
  3030. (prompt "")
  3031. (string "") beg end move tag c s)
  3032. (if (org-region-active-p)
  3033. (setq beg (region-beginning) end (region-end)
  3034. string (buffer-substring beg end))
  3035. (setq move t))
  3036. (while (setq e (pop eal))
  3037. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3038. c (aref tag 0))
  3039. (push (cons c (string-to-char (car e))) det)
  3040. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3041. (substring tag 1)))))
  3042. (unless char
  3043. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3044. (setq char (read-char-exclusive)))
  3045. (setq char (or (cdr (assoc char det)) char))
  3046. (if (equal char ?\ )
  3047. (setq s "" move nil)
  3048. (unless (assoc (char-to-string char) org-emphasis-alist)
  3049. (error "No such emphasis marker: \"%c\"" char))
  3050. (setq s (char-to-string char)))
  3051. (while (and (> (length string) 1)
  3052. (equal (substring string 0 1) (substring string -1))
  3053. (assoc (substring string 0 1) org-emphasis-alist))
  3054. (setq string (substring string 1 -1)))
  3055. (setq string (concat s string s))
  3056. (if beg (delete-region beg end))
  3057. (unless (or (bolp)
  3058. (string-match (concat "[" (nth 0 erc) "\n]")
  3059. (char-to-string (char-before (point)))))
  3060. (insert " "))
  3061. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3062. (char-to-string (char-after (point))))
  3063. (insert " ") (backward-char 1))
  3064. (insert string)
  3065. (and move (backward-char 1))))
  3066. (defconst org-nonsticky-props
  3067. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3068. (defun org-activate-plain-links (limit)
  3069. "Run through the buffer and add overlays to links."
  3070. (catch 'exit
  3071. (let (f)
  3072. (while (re-search-forward org-plain-link-re limit t)
  3073. (setq f (get-text-property (match-beginning 0) 'face))
  3074. (if (or (eq f 'org-tag)
  3075. (and (listp f) (memq 'org-tag f)))
  3076. nil
  3077. (add-text-properties (match-beginning 0) (match-end 0)
  3078. (list 'mouse-face 'highlight
  3079. 'rear-nonsticky org-nonsticky-props
  3080. 'keymap org-mouse-map
  3081. ))
  3082. (throw 'exit t))))))
  3083. (defun org-activate-code (limit)
  3084. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3085. (unless (get-text-property (match-beginning 1) 'face)
  3086. (remove-text-properties (match-beginning 0) (match-end 0)
  3087. '(display t invisible t intangible t))
  3088. t)))
  3089. (defun org-activate-angle-links (limit)
  3090. "Run through the buffer and add overlays to links."
  3091. (if (re-search-forward org-angle-link-re limit t)
  3092. (progn
  3093. (add-text-properties (match-beginning 0) (match-end 0)
  3094. (list 'mouse-face 'highlight
  3095. 'rear-nonsticky org-nonsticky-props
  3096. 'keymap org-mouse-map
  3097. ))
  3098. t)))
  3099. (defun org-activate-bracket-links (limit)
  3100. "Run through the buffer and add overlays to bracketed links."
  3101. (if (re-search-forward org-bracket-link-regexp limit t)
  3102. (let* ((help (concat "LINK: "
  3103. (org-match-string-no-properties 1)))
  3104. ;; FIXME: above we should remove the escapes.
  3105. ;; but that requires another match, protecting match data,
  3106. ;; a lot of overhead for font-lock.
  3107. (ip (org-maybe-intangible
  3108. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3109. 'keymap org-mouse-map 'mouse-face 'highlight
  3110. 'font-lock-multiline t 'help-echo help)))
  3111. (vp (list 'rear-nonsticky org-nonsticky-props
  3112. 'keymap org-mouse-map 'mouse-face 'highlight
  3113. ' font-lock-multiline t 'help-echo help)))
  3114. ;; We need to remove the invisible property here. Table narrowing
  3115. ;; may have made some of this invisible.
  3116. (remove-text-properties (match-beginning 0) (match-end 0)
  3117. '(invisible nil))
  3118. (if (match-end 3)
  3119. (progn
  3120. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3121. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3122. (add-text-properties (match-end 3) (match-end 0) ip))
  3123. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3124. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3125. (add-text-properties (match-end 1) (match-end 0) ip))
  3126. t)))
  3127. (defun org-activate-dates (limit)
  3128. "Run through the buffer and add overlays to dates."
  3129. (if (re-search-forward org-tsr-regexp-both limit t)
  3130. (progn
  3131. (add-text-properties (match-beginning 0) (match-end 0)
  3132. (list 'mouse-face 'highlight
  3133. 'rear-nonsticky org-nonsticky-props
  3134. 'keymap org-mouse-map))
  3135. (when org-display-custom-times
  3136. (if (match-end 3)
  3137. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3138. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3139. t)))
  3140. (defvar org-target-link-regexp nil
  3141. "Regular expression matching radio targets in plain text.")
  3142. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3143. "Regular expression matching a link target.")
  3144. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3145. "Regular expression matching a radio target.")
  3146. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3147. "Regular expression matching any target.")
  3148. (defun org-activate-target-links (limit)
  3149. "Run through the buffer and add overlays to target matches."
  3150. (when org-target-link-regexp
  3151. (let ((case-fold-search t))
  3152. (if (re-search-forward org-target-link-regexp limit t)
  3153. (progn
  3154. (add-text-properties (match-beginning 0) (match-end 0)
  3155. (list 'mouse-face 'highlight
  3156. 'rear-nonsticky org-nonsticky-props
  3157. 'keymap org-mouse-map
  3158. 'help-echo "Radio target link"
  3159. 'org-linked-text t))
  3160. t)))))
  3161. (defun org-update-radio-target-regexp ()
  3162. "Find all radio targets in this file and update the regular expression."
  3163. (interactive)
  3164. (when (memq 'radio org-activate-links)
  3165. (setq org-target-link-regexp
  3166. (org-make-target-link-regexp (org-all-targets 'radio)))
  3167. (org-restart-font-lock)))
  3168. (defun org-hide-wide-columns (limit)
  3169. (let (s e)
  3170. (setq s (text-property-any (point) (or limit (point-max))
  3171. 'org-cwidth t))
  3172. (when s
  3173. (setq e (next-single-property-change s 'org-cwidth))
  3174. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3175. (goto-char e)
  3176. t)))
  3177. (defvar org-latex-and-specials-regexp nil
  3178. "Regular expression for highlighting export special stuff.")
  3179. (defvar org-match-substring-regexp)
  3180. (defvar org-match-substring-with-braces-regexp)
  3181. (defvar org-export-html-special-string-regexps)
  3182. (defun org-compute-latex-and-specials-regexp ()
  3183. "Compute regular expression for stuff treated specially by exporters."
  3184. (if (not org-highlight-latex-fragments-and-specials)
  3185. (org-set-local 'org-latex-and-specials-regexp nil)
  3186. (require 'org-exp)
  3187. (let*
  3188. ((matchers (plist-get org-format-latex-options :matchers))
  3189. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3190. org-latex-regexps)))
  3191. (options (org-combine-plists (org-default-export-plist)
  3192. (org-infile-export-plist)))
  3193. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3194. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3195. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3196. (org-export-html-expand (plist-get options :expand-quoted-html))
  3197. (org-export-with-special-strings (plist-get options :special-strings))
  3198. (re-sub
  3199. (cond
  3200. ((equal org-export-with-sub-superscripts '{})
  3201. (list org-match-substring-with-braces-regexp))
  3202. (org-export-with-sub-superscripts
  3203. (list org-match-substring-regexp))
  3204. (t nil)))
  3205. (re-latex
  3206. (if org-export-with-LaTeX-fragments
  3207. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3208. (re-macros
  3209. (if org-export-with-TeX-macros
  3210. (list (concat "\\\\"
  3211. (regexp-opt
  3212. (append (mapcar 'car org-html-entities)
  3213. (if (boundp 'org-latex-entities)
  3214. org-latex-entities nil))
  3215. 'words))) ; FIXME
  3216. ))
  3217. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3218. (re-special (if org-export-with-special-strings
  3219. (mapcar (lambda (x) (car x))
  3220. org-export-html-special-string-regexps)))
  3221. (re-rest
  3222. (delq nil
  3223. (list
  3224. (if org-export-html-expand "@<[^>\n]+>")
  3225. ))))
  3226. (org-set-local
  3227. 'org-latex-and-specials-regexp
  3228. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3229. re-rest) "\\|")))))
  3230. (defun org-do-latex-and-special-faces (limit)
  3231. "Run through the buffer and add overlays to links."
  3232. (when org-latex-and-specials-regexp
  3233. (let (rtn d)
  3234. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3235. limit t))
  3236. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3237. 'face))
  3238. '(org-code org-verbatim underline)))
  3239. (progn
  3240. (setq rtn t
  3241. d (cond ((member (char-after (1+ (match-beginning 0)))
  3242. '(?_ ?^)) 1)
  3243. (t 0)))
  3244. (font-lock-prepend-text-property
  3245. (+ d (match-beginning 0)) (match-end 0)
  3246. 'face 'org-latex-and-export-specials)
  3247. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3248. '(font-lock-multiline t)))))
  3249. rtn)))
  3250. (defun org-restart-font-lock ()
  3251. "Restart font-lock-mode, to force refontification."
  3252. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3253. (font-lock-mode -1)
  3254. (font-lock-mode 1)))
  3255. (defun org-all-targets (&optional radio)
  3256. "Return a list of all targets in this file.
  3257. With optional argument RADIO, only find radio targets."
  3258. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3259. rtn)
  3260. (save-excursion
  3261. (goto-char (point-min))
  3262. (while (re-search-forward re nil t)
  3263. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3264. rtn)))
  3265. (defun org-make-target-link-regexp (targets)
  3266. "Make regular expression matching all strings in TARGETS.
  3267. The regular expression finds the targets also if there is a line break
  3268. between words."
  3269. (and targets
  3270. (concat
  3271. "\\<\\("
  3272. (mapconcat
  3273. (lambda (x)
  3274. (while (string-match " +" x)
  3275. (setq x (replace-match "\\s-+" t t x)))
  3276. x)
  3277. targets
  3278. "\\|")
  3279. "\\)\\>")))
  3280. (defun org-activate-tags (limit)
  3281. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3282. (progn
  3283. (add-text-properties (match-beginning 1) (match-end 1)
  3284. (list 'mouse-face 'highlight
  3285. 'rear-nonsticky org-nonsticky-props
  3286. 'keymap org-mouse-map))
  3287. t)))
  3288. (defun org-outline-level ()
  3289. (save-excursion
  3290. (looking-at outline-regexp)
  3291. (if (match-beginning 1)
  3292. (+ (org-get-string-indentation (match-string 1)) 1000)
  3293. (1- (- (match-end 0) (match-beginning 0))))))
  3294. (defvar org-font-lock-keywords nil)
  3295. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3296. "Regular expression matching a property line.")
  3297. (defvar org-font-lock-hook nil
  3298. "Functions to be called for special font lock stuff.")
  3299. (defun org-font-lock-hook (limit)
  3300. (run-hook-with-args 'org-font-lock-hook limit))
  3301. (defun org-set-font-lock-defaults ()
  3302. (let* ((em org-fontify-emphasized-text)
  3303. (lk org-activate-links)
  3304. (org-font-lock-extra-keywords
  3305. (list
  3306. ;; Call the hook
  3307. '(org-font-lock-hook)
  3308. ;; Headlines
  3309. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3310. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3311. ;; Table lines
  3312. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3313. (1 'org-table t))
  3314. ;; Table internals
  3315. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3316. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3317. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3318. ;; Drawers
  3319. (list org-drawer-regexp '(0 'org-special-keyword t))
  3320. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3321. ;; Properties
  3322. (list org-property-re
  3323. '(1 'org-special-keyword t)
  3324. '(3 'org-property-value t))
  3325. (if org-format-transports-properties-p
  3326. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3327. ;; Links
  3328. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3329. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3330. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3331. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3332. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3333. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3334. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3335. '(org-hide-wide-columns (0 nil append))
  3336. ;; TODO lines
  3337. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3338. '(1 (org-get-todo-face 1) t))
  3339. ;; DONE
  3340. (if org-fontify-done-headline
  3341. (list (concat "^[*]+ +\\<\\("
  3342. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3343. "\\)\\(.*\\)")
  3344. '(2 'org-headline-done t))
  3345. nil)
  3346. ;; Priorities
  3347. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3348. ;; Special keywords
  3349. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3350. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3351. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3352. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3353. ;; Emphasis
  3354. (if em
  3355. (if (featurep 'xemacs)
  3356. '(org-do-emphasis-faces (0 nil append))
  3357. '(org-do-emphasis-faces)))
  3358. ;; Checkboxes
  3359. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3360. 2 'bold prepend)
  3361. (if org-provide-checkbox-statistics
  3362. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3363. (0 (org-get-checkbox-statistics-face) t)))
  3364. ;; Description list items
  3365. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3366. 2 'bold prepend)
  3367. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3368. '(1 'org-archived prepend))
  3369. ;; Specials
  3370. '(org-do-latex-and-special-faces)
  3371. ;; Code
  3372. '(org-activate-code (1 'org-code t))
  3373. ;; COMMENT
  3374. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3375. "\\|" org-quote-string "\\)\\>")
  3376. '(1 'org-special-keyword t))
  3377. '("^#.*" (0 'font-lock-comment-face t))
  3378. )))
  3379. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3380. ;; Now set the full font-lock-keywords
  3381. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3382. (org-set-local 'font-lock-defaults
  3383. '(org-font-lock-keywords t nil nil backward-paragraph))
  3384. (kill-local-variable 'font-lock-keywords) nil))
  3385. (defvar org-m nil)
  3386. (defvar org-l nil)
  3387. (defvar org-f nil)
  3388. (defun org-get-level-face (n)
  3389. "Get the right face for match N in font-lock matching of healdines."
  3390. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3391. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3392. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3393. (cond
  3394. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3395. ((eq n 2) org-f)
  3396. (t (if org-level-color-stars-only nil org-f))))
  3397. (defun org-get-todo-face (kwd)
  3398. "Get the right face for a TODO keyword KWD.
  3399. If KWD is a number, get the corresponding match group."
  3400. (if (numberp kwd) (setq kwd (match-string kwd)))
  3401. (or (cdr (assoc kwd org-todo-keyword-faces))
  3402. (and (member kwd org-done-keywords) 'org-done)
  3403. 'org-todo))
  3404. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3405. "Remove fontification and activation overlays from links."
  3406. (font-lock-default-unfontify-region beg end)
  3407. (let* ((buffer-undo-list t)
  3408. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3409. (inhibit-modification-hooks t)
  3410. deactivate-mark buffer-file-name buffer-file-truename)
  3411. (remove-text-properties beg end
  3412. '(mouse-face t keymap t org-linked-text t
  3413. invisible t intangible t))))
  3414. ;;;; Visibility cycling, including org-goto and indirect buffer
  3415. ;;; Cycling
  3416. (defvar org-cycle-global-status nil)
  3417. (make-variable-buffer-local 'org-cycle-global-status)
  3418. (defvar org-cycle-subtree-status nil)
  3419. (make-variable-buffer-local 'org-cycle-subtree-status)
  3420. ;;;###autoload
  3421. (defun org-cycle (&optional arg)
  3422. "Visibility cycling for Org-mode.
  3423. - When this function is called with a prefix argument, rotate the entire
  3424. buffer through 3 states (global cycling)
  3425. 1. OVERVIEW: Show only top-level headlines.
  3426. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3427. 3. SHOW ALL: Show everything.
  3428. When called with two C-c C-u prefixes, switch to the startup visibility,
  3429. determined by the variable `org-startup-folded', and by any VISIBILITY
  3430. properties in the buffer.
  3431. - When point is at the beginning of a headline, rotate the subtree started
  3432. by this line through 3 different states (local cycling)
  3433. 1. FOLDED: Only the main headline is shown.
  3434. 2. CHILDREN: The main headline and the direct children are shown.
  3435. From this state, you can move to one of the children
  3436. and zoom in further.
  3437. 3. SUBTREE: Show the entire subtree, including body text.
  3438. - When there is a numeric prefix, go up to a heading with level ARG, do
  3439. a `show-subtree' and return to the previous cursor position. If ARG
  3440. is negative, go up that many levels.
  3441. - When point is not at the beginning of a headline, execute the global
  3442. binding for TAB, which is re-indenting the line. See the option
  3443. `org-cycle-emulate-tab' for details.
  3444. - Special case: if point is at the beginning of the buffer and there is
  3445. no headline in line 1, this function will act as if called with prefix arg.
  3446. But only if also the variable `org-cycle-global-at-bob' is t."
  3447. (interactive "P")
  3448. (org-load-modules-maybe)
  3449. (let* ((outline-regexp
  3450. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3451. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3452. outline-regexp))
  3453. (bob-special (and org-cycle-global-at-bob (bobp)
  3454. (not (looking-at outline-regexp))))
  3455. (org-cycle-hook
  3456. (if bob-special
  3457. (delq 'org-optimize-window-after-visibility-change
  3458. (copy-sequence org-cycle-hook))
  3459. org-cycle-hook))
  3460. (pos (point)))
  3461. (if (or bob-special (equal arg '(4)))
  3462. ;; special case: use global cycling
  3463. (setq arg t))
  3464. (cond
  3465. ((equal arg '(16))
  3466. (org-set-startup-visibility)
  3467. (message "Startup visibility, plus VISIBILITY properties."))
  3468. ((org-at-table-p 'any)
  3469. ;; Enter the table or move to the next field in the table
  3470. (or (org-table-recognize-table.el)
  3471. (progn
  3472. (if arg (org-table-edit-field t)
  3473. (org-table-justify-field-maybe)
  3474. (call-interactively 'org-table-next-field)))))
  3475. ((eq arg t) ;; Global cycling
  3476. (cond
  3477. ((and (eq last-command this-command)
  3478. (eq org-cycle-global-status 'overview))
  3479. ;; We just created the overview - now do table of contents
  3480. ;; This can be slow in very large buffers, so indicate action
  3481. (message "CONTENTS...")
  3482. (org-content)
  3483. (message "CONTENTS...done")
  3484. (setq org-cycle-global-status 'contents)
  3485. (run-hook-with-args 'org-cycle-hook 'contents))
  3486. ((and (eq last-command this-command)
  3487. (eq org-cycle-global-status 'contents))
  3488. ;; We just showed the table of contents - now show everything
  3489. (show-all)
  3490. (message "SHOW ALL")
  3491. (setq org-cycle-global-status 'all)
  3492. (run-hook-with-args 'org-cycle-hook 'all))
  3493. (t
  3494. ;; Default action: go to overview
  3495. (org-overview)
  3496. (message "OVERVIEW")
  3497. (setq org-cycle-global-status 'overview)
  3498. (run-hook-with-args 'org-cycle-hook 'overview))))
  3499. ((and org-drawers org-drawer-regexp
  3500. (save-excursion
  3501. (beginning-of-line 1)
  3502. (looking-at org-drawer-regexp)))
  3503. ;; Toggle block visibility
  3504. (org-flag-drawer
  3505. (not (get-char-property (match-end 0) 'invisible))))
  3506. ((integerp arg)
  3507. ;; Show-subtree, ARG levels up from here.
  3508. (save-excursion
  3509. (org-back-to-heading)
  3510. (outline-up-heading (if (< arg 0) (- arg)
  3511. (- (funcall outline-level) arg)))
  3512. (org-show-subtree)))
  3513. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3514. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3515. ;; At a heading: rotate between three different views
  3516. (org-back-to-heading)
  3517. (let ((goal-column 0) eoh eol eos)
  3518. ;; First, some boundaries
  3519. (save-excursion
  3520. (org-back-to-heading)
  3521. (save-excursion
  3522. (beginning-of-line 2)
  3523. (while (and (not (eobp)) ;; this is like `next-line'
  3524. (get-char-property (1- (point)) 'invisible))
  3525. (beginning-of-line 2)) (setq eol (point)))
  3526. (outline-end-of-heading) (setq eoh (point))
  3527. (org-end-of-subtree t)
  3528. (unless (eobp)
  3529. (skip-chars-forward " \t\n")
  3530. (beginning-of-line 1) ; in case this is an item
  3531. )
  3532. (setq eos (1- (point))))
  3533. ;; Find out what to do next and set `this-command'
  3534. (cond
  3535. ((= eos eoh)
  3536. ;; Nothing is hidden behind this heading
  3537. (message "EMPTY ENTRY")
  3538. (setq org-cycle-subtree-status nil)
  3539. (save-excursion
  3540. (goto-char eos)
  3541. (outline-next-heading)
  3542. (if (org-invisible-p) (org-flag-heading nil))))
  3543. ((or (>= eol eos)
  3544. (not (string-match "\\S-" (buffer-substring eol eos))))
  3545. ;; Entire subtree is hidden in one line: open it
  3546. (org-show-entry)
  3547. (show-children)
  3548. (message "CHILDREN")
  3549. (save-excursion
  3550. (goto-char eos)
  3551. (outline-next-heading)
  3552. (if (org-invisible-p) (org-flag-heading nil)))
  3553. (setq org-cycle-subtree-status 'children)
  3554. (run-hook-with-args 'org-cycle-hook 'children))
  3555. ((and (eq last-command this-command)
  3556. (eq org-cycle-subtree-status 'children))
  3557. ;; We just showed the children, now show everything.
  3558. (org-show-subtree)
  3559. (message "SUBTREE")
  3560. (setq org-cycle-subtree-status 'subtree)
  3561. (run-hook-with-args 'org-cycle-hook 'subtree))
  3562. (t
  3563. ;; Default action: hide the subtree.
  3564. (hide-subtree)
  3565. (message "FOLDED")
  3566. (setq org-cycle-subtree-status 'folded)
  3567. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3568. ;; TAB emulation and template completion
  3569. (buffer-read-only (org-back-to-heading))
  3570. ((org-try-structure-completion))
  3571. ((org-try-cdlatex-tab))
  3572. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3573. (or (not (bolp))
  3574. (not (looking-at outline-regexp))))
  3575. (call-interactively (global-key-binding "\t")))
  3576. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3577. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3578. (or (and (eq org-cycle-emulate-tab 'white)
  3579. (= (match-end 0) (point-at-eol)))
  3580. (and (eq org-cycle-emulate-tab 'whitestart)
  3581. (>= (match-end 0) pos))))
  3582. t
  3583. (eq org-cycle-emulate-tab t))
  3584. (call-interactively (global-key-binding "\t")))
  3585. (t (save-excursion
  3586. (org-back-to-heading)
  3587. (org-cycle))))))
  3588. ;;;###autoload
  3589. (defun org-global-cycle (&optional arg)
  3590. "Cycle the global visibility. For details see `org-cycle'.
  3591. With C-u prefix arg, switch to startup visibility.
  3592. With a numeric prefix, show all headlines up to that level."
  3593. (interactive "P")
  3594. (let ((org-cycle-include-plain-lists
  3595. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3596. (cond
  3597. ((integerp arg)
  3598. (show-all)
  3599. (hide-sublevels arg)
  3600. (setq org-cycle-global-status 'contents))
  3601. ((equal arg '(4))
  3602. (org-set-startup-visibility)
  3603. (message "Startup visibility, plus VISIBILITY properties."))
  3604. (t
  3605. (org-cycle '(4))))))
  3606. (defun org-set-startup-visibility ()
  3607. "Set the visibility required by startup options and properties."
  3608. (cond
  3609. ((eq org-startup-folded t)
  3610. (org-cycle '(4)))
  3611. ((eq org-startup-folded 'content)
  3612. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3613. (org-cycle '(4)) (org-cycle '(4)))))
  3614. (org-set-visibility-according-to-property 'no-cleanup)
  3615. (org-cycle-hide-archived-subtrees 'all)
  3616. (org-cycle-hide-drawers 'all)
  3617. (org-cycle-show-empty-lines 'all))
  3618. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3619. "Switch subtree visibilities according to :VISIBILITY: property."
  3620. (interactive)
  3621. (let (state)
  3622. (save-excursion
  3623. (goto-char (point-min))
  3624. (while (re-search-forward
  3625. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3626. nil t)
  3627. (setq state (match-string 1))
  3628. (save-excursion
  3629. (org-back-to-heading t)
  3630. (hide-subtree)
  3631. (org-reveal)
  3632. (cond
  3633. ((equal state '("fold" "folded"))
  3634. (hide-subtree))
  3635. ((equal state "children")
  3636. (org-show-hidden-entry)
  3637. (show-children))
  3638. ((equal state "content")
  3639. (save-excursion
  3640. (save-restriction
  3641. (org-narrow-to-subtree)
  3642. (org-content))))
  3643. ((member state '("all" "showall"))
  3644. (show-subtree)))))
  3645. (unless no-cleanup
  3646. (org-cycle-hide-archived-subtrees 'all)
  3647. (org-cycle-hide-drawers 'all)
  3648. (org-cycle-show-empty-lines 'all)))))
  3649. (defun org-overview ()
  3650. "Switch to overview mode, shoing only top-level headlines.
  3651. Really, this shows all headlines with level equal or greater than the level
  3652. of the first headline in the buffer. This is important, because if the
  3653. first headline is not level one, then (hide-sublevels 1) gives confusing
  3654. results."
  3655. (interactive)
  3656. (let ((level (save-excursion
  3657. (goto-char (point-min))
  3658. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3659. (progn
  3660. (goto-char (match-beginning 0))
  3661. (funcall outline-level))))))
  3662. (and level (hide-sublevels level))))
  3663. (defun org-content (&optional arg)
  3664. "Show all headlines in the buffer, like a table of contents.
  3665. With numerical argument N, show content up to level N."
  3666. (interactive "P")
  3667. (save-excursion
  3668. ;; Visit all headings and show their offspring
  3669. (and (integerp arg) (org-overview))
  3670. (goto-char (point-max))
  3671. (catch 'exit
  3672. (while (and (progn (condition-case nil
  3673. (outline-previous-visible-heading 1)
  3674. (error (goto-char (point-min))))
  3675. t)
  3676. (looking-at outline-regexp))
  3677. (if (integerp arg)
  3678. (show-children (1- arg))
  3679. (show-branches))
  3680. (if (bobp) (throw 'exit nil))))))
  3681. (defun org-optimize-window-after-visibility-change (state)
  3682. "Adjust the window after a change in outline visibility.
  3683. This function is the default value of the hook `org-cycle-hook'."
  3684. (when (get-buffer-window (current-buffer))
  3685. (cond
  3686. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3687. ; ((eq state 'overview) (org-beginning-of-line))
  3688. ((eq state 'content) nil)
  3689. ((eq state 'all) nil)
  3690. ((eq state 'folded) nil)
  3691. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3692. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3693. (defun org-compact-display-after-subtree-move ()
  3694. (let (beg end)
  3695. (save-excursion
  3696. (if (org-up-heading-safe)
  3697. (progn
  3698. (hide-subtree)
  3699. (show-entry)
  3700. (show-children)
  3701. (org-cycle-show-empty-lines 'children)
  3702. (org-cycle-hide-drawers 'children))
  3703. (org-overview)))))
  3704. (defun org-cycle-show-empty-lines (state)
  3705. "Show empty lines above all visible headlines.
  3706. The region to be covered depends on STATE when called through
  3707. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3708. entire buffer covered. Note that an empty line is only shown if there
  3709. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3710. (when (> org-cycle-separator-lines 0)
  3711. (save-excursion
  3712. (let* ((n org-cycle-separator-lines)
  3713. (re (cond
  3714. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3715. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3716. (t (let ((ns (number-to-string (- n 2))))
  3717. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3718. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3719. beg end)
  3720. (cond
  3721. ((memq state '(overview contents t))
  3722. (setq beg (point-min) end (point-max)))
  3723. ((memq state '(children folded))
  3724. (setq beg (point) end (progn (org-end-of-subtree t t)
  3725. (beginning-of-line 2)
  3726. (point)))))
  3727. (when beg
  3728. (goto-char beg)
  3729. (while (re-search-forward re end t)
  3730. (if (not (get-char-property (match-end 1) 'invisible))
  3731. (outline-flag-region
  3732. (match-beginning 1) (match-end 1) nil)))))))
  3733. ;; Never hide empty lines at the end of the file.
  3734. (save-excursion
  3735. (goto-char (point-max))
  3736. (outline-previous-heading)
  3737. (outline-end-of-heading)
  3738. (if (and (looking-at "[ \t\n]+")
  3739. (= (match-end 0) (point-max)))
  3740. (outline-flag-region (point) (match-end 0) nil))))
  3741. (defun org-cycle-hide-drawers (state)
  3742. "Re-hide all drawers after a visibility state change."
  3743. (when (and (org-mode-p)
  3744. (not (memq state '(overview folded))))
  3745. (save-excursion
  3746. (let* ((globalp (memq state '(contents all)))
  3747. (beg (if globalp (point-min) (point)))
  3748. (end (if globalp (point-max) (org-end-of-subtree t))))
  3749. (goto-char beg)
  3750. (while (re-search-forward org-drawer-regexp end t)
  3751. (org-flag-drawer t))))))
  3752. (defun org-flag-drawer (flag)
  3753. (save-excursion
  3754. (beginning-of-line 1)
  3755. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3756. (let ((b (match-end 0))
  3757. (outline-regexp org-outline-regexp))
  3758. (if (re-search-forward
  3759. "^[ \t]*:END:"
  3760. (save-excursion (outline-next-heading) (point)) t)
  3761. (outline-flag-region b (point-at-eol) flag)
  3762. (error ":END: line missing"))))))
  3763. (defun org-subtree-end-visible-p ()
  3764. "Is the end of the current subtree visible?"
  3765. (pos-visible-in-window-p
  3766. (save-excursion (org-end-of-subtree t) (point))))
  3767. (defun org-first-headline-recenter (&optional N)
  3768. "Move cursor to the first headline and recenter the headline.
  3769. Optional argument N means, put the headline into the Nth line of the window."
  3770. (goto-char (point-min))
  3771. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3772. (beginning-of-line)
  3773. (recenter (prefix-numeric-value N))))
  3774. ;;; Org-goto
  3775. (defvar org-goto-window-configuration nil)
  3776. (defvar org-goto-marker nil)
  3777. (defvar org-goto-map
  3778. (let ((map (make-sparse-keymap)))
  3779. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3780. (while (setq cmd (pop cmds))
  3781. (substitute-key-definition cmd cmd map global-map)))
  3782. (suppress-keymap map)
  3783. (org-defkey map "\C-m" 'org-goto-ret)
  3784. (org-defkey map [(return)] 'org-goto-ret)
  3785. (org-defkey map [(left)] 'org-goto-left)
  3786. (org-defkey map [(right)] 'org-goto-right)
  3787. (org-defkey map [(control ?g)] 'org-goto-quit)
  3788. (org-defkey map "\C-i" 'org-cycle)
  3789. (org-defkey map [(tab)] 'org-cycle)
  3790. (org-defkey map [(down)] 'outline-next-visible-heading)
  3791. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3792. (if org-goto-auto-isearch
  3793. (if (fboundp 'define-key-after)
  3794. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3795. nil)
  3796. (org-defkey map "q" 'org-goto-quit)
  3797. (org-defkey map "n" 'outline-next-visible-heading)
  3798. (org-defkey map "p" 'outline-previous-visible-heading)
  3799. (org-defkey map "f" 'outline-forward-same-level)
  3800. (org-defkey map "b" 'outline-backward-same-level)
  3801. (org-defkey map "u" 'outline-up-heading))
  3802. (org-defkey map "/" 'org-occur)
  3803. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3804. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3805. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3806. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3807. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3808. map))
  3809. (defconst org-goto-help
  3810. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3811. RET=jump to location [Q]uit and return to previous location
  3812. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3813. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3814. ;; FIXME: Docstring doe not mention both interfaces
  3815. (defun org-goto (&optional alternative-interface)
  3816. "Look up a different location in the current file, keeping current visibility.
  3817. When you want look-up or go to a different location in a document, the
  3818. fastest way is often to fold the entire buffer and then dive into the tree.
  3819. This method has the disadvantage, that the previous location will be folded,
  3820. which may not be what you want.
  3821. This command works around this by showing a copy of the current buffer
  3822. in an indirect buffer, in overview mode. You can dive into the tree in
  3823. that copy, use org-occur and incremental search to find a location.
  3824. When pressing RET or `Q', the command returns to the original buffer in
  3825. which the visibility is still unchanged. After RET is will also jump to
  3826. the location selected in the indirect buffer and expose the
  3827. the headline hierarchy above."
  3828. (interactive "P")
  3829. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3830. (org-refile-use-outline-path t)
  3831. (interface
  3832. (if (not alternative-interface)
  3833. org-goto-interface
  3834. (if (eq org-goto-interface 'outline)
  3835. 'outline-path-completion
  3836. 'outline)))
  3837. (org-goto-start-pos (point))
  3838. (selected-point
  3839. (if (eq interface 'outline)
  3840. (car (org-get-location (current-buffer) org-goto-help))
  3841. (nth 3 (org-refile-get-location "Goto: ")))))
  3842. (if selected-point
  3843. (progn
  3844. (org-mark-ring-push org-goto-start-pos)
  3845. (goto-char selected-point)
  3846. (if (or (org-invisible-p) (org-invisible-p2))
  3847. (org-show-context 'org-goto)))
  3848. (message "Quit"))))
  3849. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3850. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3851. (defvar org-goto-local-auto-isearch-map) ; defined below
  3852. (defun org-get-location (buf help)
  3853. "Let the user select a location in the Org-mode buffer BUF.
  3854. This function uses a recursive edit. It returns the selected position
  3855. or nil."
  3856. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3857. (isearch-hide-immediately nil)
  3858. (isearch-search-fun-function
  3859. (lambda () 'org-goto-local-search-forward-headings))
  3860. (org-goto-selected-point org-goto-exit-command))
  3861. (save-excursion
  3862. (save-window-excursion
  3863. (delete-other-windows)
  3864. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3865. (switch-to-buffer
  3866. (condition-case nil
  3867. (make-indirect-buffer (current-buffer) "*org-goto*")
  3868. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3869. (with-output-to-temp-buffer "*Help*"
  3870. (princ help))
  3871. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3872. (setq buffer-read-only nil)
  3873. (let ((org-startup-truncated t)
  3874. (org-startup-folded nil)
  3875. (org-startup-align-all-tables nil))
  3876. (org-mode)
  3877. (org-overview))
  3878. (setq buffer-read-only t)
  3879. (if (and (boundp 'org-goto-start-pos)
  3880. (integer-or-marker-p org-goto-start-pos))
  3881. (let ((org-show-hierarchy-above t)
  3882. (org-show-siblings t)
  3883. (org-show-following-heading t))
  3884. (goto-char org-goto-start-pos)
  3885. (and (org-invisible-p) (org-show-context)))
  3886. (goto-char (point-min)))
  3887. (org-beginning-of-line)
  3888. (message "Select location and press RET")
  3889. (use-local-map org-goto-map)
  3890. (recursive-edit)
  3891. ))
  3892. (kill-buffer "*org-goto*")
  3893. (cons org-goto-selected-point org-goto-exit-command)))
  3894. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3895. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3896. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3897. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3898. (defun org-goto-local-search-forward-headings (string bound noerror)
  3899. "Search and make sure that anu matches are in headlines."
  3900. (catch 'return
  3901. (while (search-forward string bound noerror)
  3902. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3903. (and (member :headline context)
  3904. (not (member :tags context))))
  3905. (throw 'return (point))))))
  3906. (defun org-goto-local-auto-isearch ()
  3907. "Start isearch."
  3908. (interactive)
  3909. (goto-char (point-min))
  3910. (let ((keys (this-command-keys)))
  3911. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3912. (isearch-mode t)
  3913. (isearch-process-search-char (string-to-char keys)))))
  3914. (defun org-goto-ret (&optional arg)
  3915. "Finish `org-goto' by going to the new location."
  3916. (interactive "P")
  3917. (setq org-goto-selected-point (point)
  3918. org-goto-exit-command 'return)
  3919. (throw 'exit nil))
  3920. (defun org-goto-left ()
  3921. "Finish `org-goto' by going to the new location."
  3922. (interactive)
  3923. (if (org-on-heading-p)
  3924. (progn
  3925. (beginning-of-line 1)
  3926. (setq org-goto-selected-point (point)
  3927. org-goto-exit-command 'left)
  3928. (throw 'exit nil))
  3929. (error "Not on a heading")))
  3930. (defun org-goto-right ()
  3931. "Finish `org-goto' by going to the new location."
  3932. (interactive)
  3933. (if (org-on-heading-p)
  3934. (progn
  3935. (setq org-goto-selected-point (point)
  3936. org-goto-exit-command 'right)
  3937. (throw 'exit nil))
  3938. (error "Not on a heading")))
  3939. (defun org-goto-quit ()
  3940. "Finish `org-goto' without cursor motion."
  3941. (interactive)
  3942. (setq org-goto-selected-point nil)
  3943. (setq org-goto-exit-command 'quit)
  3944. (throw 'exit nil))
  3945. ;;; Indirect buffer display of subtrees
  3946. (defvar org-indirect-dedicated-frame nil
  3947. "This is the frame being used for indirect tree display.")
  3948. (defvar org-last-indirect-buffer nil)
  3949. (defun org-tree-to-indirect-buffer (&optional arg)
  3950. "Create indirect buffer and narrow it to current subtree.
  3951. With numerical prefix ARG, go up to this level and then take that tree.
  3952. If ARG is negative, go up that many levels.
  3953. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3954. indirect buffer previously made with this command, to avoid proliferation of
  3955. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3956. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3957. is kept so that you can work with several indirect buffers at the same time.
  3958. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3959. requests that a new frame be made for the new buffer, so that the dedicated
  3960. frame is not changed."
  3961. (interactive "P")
  3962. (let ((cbuf (current-buffer))
  3963. (cwin (selected-window))
  3964. (pos (point))
  3965. beg end level heading ibuf)
  3966. (save-excursion
  3967. (org-back-to-heading t)
  3968. (when (numberp arg)
  3969. (setq level (org-outline-level))
  3970. (if (< arg 0) (setq arg (+ level arg)))
  3971. (while (> (setq level (org-outline-level)) arg)
  3972. (outline-up-heading 1 t)))
  3973. (setq beg (point)
  3974. heading (org-get-heading))
  3975. (org-end-of-subtree t) (setq end (point)))
  3976. (if (and (buffer-live-p org-last-indirect-buffer)
  3977. (not (eq org-indirect-buffer-display 'new-frame))
  3978. (not arg))
  3979. (kill-buffer org-last-indirect-buffer))
  3980. (setq ibuf (org-get-indirect-buffer cbuf)
  3981. org-last-indirect-buffer ibuf)
  3982. (cond
  3983. ((or (eq org-indirect-buffer-display 'new-frame)
  3984. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3985. (select-frame (make-frame))
  3986. (delete-other-windows)
  3987. (switch-to-buffer ibuf)
  3988. (org-set-frame-title heading))
  3989. ((eq org-indirect-buffer-display 'dedicated-frame)
  3990. (raise-frame
  3991. (select-frame (or (and org-indirect-dedicated-frame
  3992. (frame-live-p org-indirect-dedicated-frame)
  3993. org-indirect-dedicated-frame)
  3994. (setq org-indirect-dedicated-frame (make-frame)))))
  3995. (delete-other-windows)
  3996. (switch-to-buffer ibuf)
  3997. (org-set-frame-title (concat "Indirect: " heading)))
  3998. ((eq org-indirect-buffer-display 'current-window)
  3999. (switch-to-buffer ibuf))
  4000. ((eq org-indirect-buffer-display 'other-window)
  4001. (pop-to-buffer ibuf))
  4002. (t (error "Invalid value.")))
  4003. (if (featurep 'xemacs)
  4004. (save-excursion (org-mode) (turn-on-font-lock)))
  4005. (narrow-to-region beg end)
  4006. (show-all)
  4007. (goto-char pos)
  4008. (and (window-live-p cwin) (select-window cwin))))
  4009. (defun org-get-indirect-buffer (&optional buffer)
  4010. (setq buffer (or buffer (current-buffer)))
  4011. (let ((n 1) (base (buffer-name buffer)) bname)
  4012. (while (buffer-live-p
  4013. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4014. (setq n (1+ n)))
  4015. (condition-case nil
  4016. (make-indirect-buffer buffer bname 'clone)
  4017. (error (make-indirect-buffer buffer bname)))))
  4018. (defun org-set-frame-title (title)
  4019. "Set the title of the current frame to the string TITLE."
  4020. ;; FIXME: how to name a single frame in XEmacs???
  4021. (unless (featurep 'xemacs)
  4022. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4023. ;;;; Structure editing
  4024. ;;; Inserting headlines
  4025. (defun org-insert-heading (&optional force-heading)
  4026. "Insert a new heading or item with same depth at point.
  4027. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4028. If point is at the beginning of a headline, insert a sibling before the
  4029. current headline. If point is not at the beginning, do not split the line,
  4030. but create the new hedline after the current line."
  4031. (interactive "P")
  4032. (if (= (buffer-size) 0)
  4033. (insert "\n* ")
  4034. (when (or force-heading (not (org-insert-item)))
  4035. (let* ((head (save-excursion
  4036. (condition-case nil
  4037. (progn
  4038. (org-back-to-heading)
  4039. (match-string 0))
  4040. (error "*"))))
  4041. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4042. pos)
  4043. (cond
  4044. ((and (org-on-heading-p) (bolp)
  4045. (or (bobp)
  4046. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4047. ;; insert before the current line
  4048. (open-line (if blank 2 1)))
  4049. ((and (bolp)
  4050. (or (bobp)
  4051. (save-excursion
  4052. (backward-char 1) (not (org-invisible-p)))))
  4053. ;; insert right here
  4054. nil)
  4055. (t
  4056. ;; in the middle of the line
  4057. (org-show-entry)
  4058. (let ((split
  4059. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4060. tags pos)
  4061. (if (org-on-heading-p)
  4062. (progn
  4063. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4064. (setq tags (and (match-end 2) (match-string 2)))
  4065. (and (match-end 1)
  4066. (delete-region (match-beginning 1) (match-end 1)))
  4067. (setq pos (point-at-bol))
  4068. (or split (end-of-line 1))
  4069. (delete-horizontal-space)
  4070. (newline (if blank 2 1))
  4071. (when tags
  4072. (save-excursion
  4073. (goto-char pos)
  4074. (end-of-line 1)
  4075. (insert " " tags)
  4076. (org-set-tags nil 'align))))
  4077. (or split (end-of-line 1))
  4078. (newline (if blank 2 1))))))
  4079. (insert head) (just-one-space)
  4080. (setq pos (point))
  4081. (end-of-line 1)
  4082. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4083. (run-hooks 'org-insert-heading-hook)))))
  4084. (defun org-get-heading (&optional no-tags)
  4085. "Return the heading of the current entry, without the stars."
  4086. (save-excursion
  4087. (org-back-to-heading t)
  4088. (if (looking-at
  4089. (if no-tags
  4090. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4091. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4092. (match-string 1) "")))
  4093. (defun org-insert-heading-after-current ()
  4094. "Insert a new heading with same level as current, after current subtree."
  4095. (interactive)
  4096. (org-back-to-heading)
  4097. (org-insert-heading)
  4098. (org-move-subtree-down)
  4099. (end-of-line 1))
  4100. (defun org-insert-todo-heading (arg)
  4101. "Insert a new heading with the same level and TODO state as current heading.
  4102. If the heading has no TODO state, or if the state is DONE, use the first
  4103. state (TODO by default). Also with prefix arg, force first state."
  4104. (interactive "P")
  4105. (when (not (org-insert-item 'checkbox))
  4106. (org-insert-heading)
  4107. (save-excursion
  4108. (org-back-to-heading)
  4109. (outline-previous-heading)
  4110. (looking-at org-todo-line-regexp))
  4111. (if (or arg
  4112. (not (match-beginning 2))
  4113. (member (match-string 2) org-done-keywords))
  4114. (insert (car org-todo-keywords-1) " ")
  4115. (insert (match-string 2) " "))
  4116. (when org-provide-todo-statistics
  4117. (org-update-parent-todo-statistics))))
  4118. (defun org-insert-subheading (arg)
  4119. "Insert a new subheading and demote it.
  4120. Works for outline headings and for plain lists alike."
  4121. (interactive "P")
  4122. (org-insert-heading arg)
  4123. (cond
  4124. ((org-on-heading-p) (org-do-demote))
  4125. ((org-at-item-p) (org-indent-item 1))))
  4126. (defun org-insert-todo-subheading (arg)
  4127. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4128. Works for outline headings and for plain lists alike."
  4129. (interactive "P")
  4130. (org-insert-todo-heading arg)
  4131. (cond
  4132. ((org-on-heading-p) (org-do-demote))
  4133. ((org-at-item-p) (org-indent-item 1))))
  4134. ;;; Promotion and Demotion
  4135. (defun org-promote-subtree ()
  4136. "Promote the entire subtree.
  4137. See also `org-promote'."
  4138. (interactive)
  4139. (save-excursion
  4140. (org-map-tree 'org-promote))
  4141. (org-fix-position-after-promote))
  4142. (defun org-demote-subtree ()
  4143. "Demote the entire subtree. See `org-demote'.
  4144. See also `org-promote'."
  4145. (interactive)
  4146. (save-excursion
  4147. (org-map-tree 'org-demote))
  4148. (org-fix-position-after-promote))
  4149. (defun org-do-promote ()
  4150. "Promote the current heading higher up the tree.
  4151. If the region is active in `transient-mark-mode', promote all headings
  4152. in the region."
  4153. (interactive)
  4154. (save-excursion
  4155. (if (org-region-active-p)
  4156. (org-map-region 'org-promote (region-beginning) (region-end))
  4157. (org-promote)))
  4158. (org-fix-position-after-promote))
  4159. (defun org-do-demote ()
  4160. "Demote the current heading lower down the tree.
  4161. If the region is active in `transient-mark-mode', demote all headings
  4162. in the region."
  4163. (interactive)
  4164. (save-excursion
  4165. (if (org-region-active-p)
  4166. (org-map-region 'org-demote (region-beginning) (region-end))
  4167. (org-demote)))
  4168. (org-fix-position-after-promote))
  4169. (defun org-fix-position-after-promote ()
  4170. "Make sure that after pro/demotion cursor position is right."
  4171. (let ((pos (point)))
  4172. (when (save-excursion
  4173. (beginning-of-line 1)
  4174. (looking-at org-todo-line-regexp)
  4175. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4176. (cond ((eobp) (insert " "))
  4177. ((eolp) (insert " "))
  4178. ((equal (char-after) ?\ ) (forward-char 1))))))
  4179. (defun org-reduced-level (l)
  4180. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4181. (defun org-get-valid-level (level &optional change)
  4182. "Rectify a level change under the influence of `org-odd-levels-only'
  4183. LEVEL is a current level, CHANGE is by how much the level should be
  4184. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4185. even level numbers will become the next higher odd number."
  4186. (if org-odd-levels-only
  4187. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4188. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4189. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4190. (max 1 (+ level change))))
  4191. (if (boundp 'define-obsolete-function-alias)
  4192. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4193. (define-obsolete-function-alias 'org-get-legal-level
  4194. 'org-get-valid-level)
  4195. (define-obsolete-function-alias 'org-get-legal-level
  4196. 'org-get-valid-level "23.1")))
  4197. (defun org-promote ()
  4198. "Promote the current heading higher up the tree.
  4199. If the region is active in `transient-mark-mode', promote all headings
  4200. in the region."
  4201. (org-back-to-heading t)
  4202. (let* ((level (save-match-data (funcall outline-level)))
  4203. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4204. (diff (abs (- level (length up-head) -1))))
  4205. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4206. (replace-match up-head nil t)
  4207. ;; Fixup tag positioning
  4208. (and org-auto-align-tags (org-set-tags nil t))
  4209. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4210. (defun org-demote ()
  4211. "Demote the current heading lower down the tree.
  4212. If the region is active in `transient-mark-mode', demote all headings
  4213. in the region."
  4214. (org-back-to-heading t)
  4215. (let* ((level (save-match-data (funcall outline-level)))
  4216. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4217. (diff (abs (- level (length down-head) -1))))
  4218. (replace-match down-head nil t)
  4219. ;; Fixup tag positioning
  4220. (and org-auto-align-tags (org-set-tags nil t))
  4221. (if org-adapt-indentation (org-fixup-indentation diff))))
  4222. (defun org-map-tree (fun)
  4223. "Call FUN for every heading underneath the current one."
  4224. (org-back-to-heading)
  4225. (let ((level (funcall outline-level)))
  4226. (save-excursion
  4227. (funcall fun)
  4228. (while (and (progn
  4229. (outline-next-heading)
  4230. (> (funcall outline-level) level))
  4231. (not (eobp)))
  4232. (funcall fun)))))
  4233. (defun org-map-region (fun beg end)
  4234. "Call FUN for every heading between BEG and END."
  4235. (let ((org-ignore-region t))
  4236. (save-excursion
  4237. (setq end (copy-marker end))
  4238. (goto-char beg)
  4239. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4240. (< (point) end))
  4241. (funcall fun))
  4242. (while (and (progn
  4243. (outline-next-heading)
  4244. (< (point) end))
  4245. (not (eobp)))
  4246. (funcall fun)))))
  4247. (defun org-fixup-indentation (diff)
  4248. "Change the indentation in the current entry by DIFF
  4249. However, if any line in the current entry has no indentation, or if it
  4250. would end up with no indentation after the change, nothing at all is done."
  4251. (save-excursion
  4252. (let ((end (save-excursion (outline-next-heading)
  4253. (point-marker)))
  4254. (prohibit (if (> diff 0)
  4255. "^\\S-"
  4256. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4257. col)
  4258. (unless (save-excursion (end-of-line 1)
  4259. (re-search-forward prohibit end t))
  4260. (while (and (< (point) end)
  4261. (re-search-forward "^[ \t]+" end t))
  4262. (goto-char (match-end 0))
  4263. (setq col (current-column))
  4264. (if (< diff 0) (replace-match ""))
  4265. (indent-to (+ diff col))))
  4266. (move-marker end nil))))
  4267. (defun org-convert-to-odd-levels ()
  4268. "Convert an org-mode file with all levels allowed to one with odd levels.
  4269. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4270. level 5 etc."
  4271. (interactive)
  4272. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4273. (let ((org-odd-levels-only nil) n)
  4274. (save-excursion
  4275. (goto-char (point-min))
  4276. (while (re-search-forward "^\\*\\*+ " nil t)
  4277. (setq n (- (length (match-string 0)) 2))
  4278. (while (>= (setq n (1- n)) 0)
  4279. (org-demote))
  4280. (end-of-line 1))))))
  4281. (defun org-convert-to-oddeven-levels ()
  4282. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4283. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4284. section with an even level, conversion would destroy the structure of the file. An error
  4285. is signaled in this case."
  4286. (interactive)
  4287. (goto-char (point-min))
  4288. ;; First check if there are no even levels
  4289. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4290. (org-show-context t)
  4291. (error "Not all levels are odd in this file. Conversion not possible."))
  4292. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4293. (let ((org-odd-levels-only nil) n)
  4294. (save-excursion
  4295. (goto-char (point-min))
  4296. (while (re-search-forward "^\\*\\*+ " nil t)
  4297. (setq n (/ (1- (length (match-string 0))) 2))
  4298. (while (>= (setq n (1- n)) 0)
  4299. (org-promote))
  4300. (end-of-line 1))))))
  4301. (defun org-tr-level (n)
  4302. "Make N odd if required."
  4303. (if org-odd-levels-only (1+ (/ n 2)) n))
  4304. ;;; Vertical tree motion, cutting and pasting of subtrees
  4305. (defun org-move-subtree-up (&optional arg)
  4306. "Move the current subtree up past ARG headlines of the same level."
  4307. (interactive "p")
  4308. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4309. (defun org-move-subtree-down (&optional arg)
  4310. "Move the current subtree down past ARG headlines of the same level."
  4311. (interactive "p")
  4312. (setq arg (prefix-numeric-value arg))
  4313. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4314. 'outline-get-last-sibling))
  4315. (ins-point (make-marker))
  4316. (cnt (abs arg))
  4317. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4318. ;; Select the tree
  4319. (org-back-to-heading)
  4320. (setq beg0 (point))
  4321. (save-excursion
  4322. (setq ne-beg (org-back-over-empty-lines))
  4323. (setq beg (point)))
  4324. (save-match-data
  4325. (save-excursion (outline-end-of-heading)
  4326. (setq folded (org-invisible-p)))
  4327. (outline-end-of-subtree))
  4328. (outline-next-heading)
  4329. (setq ne-end (org-back-over-empty-lines))
  4330. (setq end (point))
  4331. (goto-char beg0)
  4332. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4333. ;; include less whitespace
  4334. (save-excursion
  4335. (goto-char beg)
  4336. (forward-line (- ne-beg ne-end))
  4337. (setq beg (point))))
  4338. ;; Find insertion point, with error handling
  4339. (while (> cnt 0)
  4340. (or (and (funcall movfunc) (looking-at outline-regexp))
  4341. (progn (goto-char beg0)
  4342. (error "Cannot move past superior level or buffer limit")))
  4343. (setq cnt (1- cnt)))
  4344. (if (> arg 0)
  4345. ;; Moving forward - still need to move over subtree
  4346. (progn (org-end-of-subtree t t)
  4347. (save-excursion
  4348. (org-back-over-empty-lines)
  4349. (or (bolp) (newline)))))
  4350. (setq ne-ins (org-back-over-empty-lines))
  4351. (move-marker ins-point (point))
  4352. (setq txt (buffer-substring beg end))
  4353. (org-save-markers-in-region beg end)
  4354. (delete-region beg end)
  4355. (outline-flag-region (1- beg) beg nil)
  4356. (outline-flag-region (1- (point)) (point) nil)
  4357. (let ((bbb (point)))
  4358. (insert-before-markers txt)
  4359. (org-reinstall-markers-in-region bbb)
  4360. (move-marker ins-point bbb))
  4361. (or (bolp) (insert "\n"))
  4362. (setq ins-end (point))
  4363. (goto-char ins-point)
  4364. (org-skip-whitespace)
  4365. (when (and (< arg 0)
  4366. (org-first-sibling-p)
  4367. (> ne-ins ne-beg))
  4368. ;; Move whitespace back to beginning
  4369. (save-excursion
  4370. (goto-char ins-end)
  4371. (let ((kill-whole-line t))
  4372. (kill-line (- ne-ins ne-beg)) (point)))
  4373. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4374. (move-marker ins-point nil)
  4375. (org-compact-display-after-subtree-move)
  4376. (unless folded
  4377. (org-show-entry)
  4378. (show-children)
  4379. (org-cycle-hide-drawers 'children))))
  4380. (defvar org-subtree-clip ""
  4381. "Clipboard for cut and paste of subtrees.
  4382. This is actually only a copy of the kill, because we use the normal kill
  4383. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4384. (defvar org-subtree-clip-folded nil
  4385. "Was the last copied subtree folded?
  4386. This is used to fold the tree back after pasting.")
  4387. (defun org-cut-subtree (&optional n)
  4388. "Cut the current subtree into the clipboard.
  4389. With prefix arg N, cut this many sequential subtrees.
  4390. This is a short-hand for marking the subtree and then cutting it."
  4391. (interactive "p")
  4392. (org-copy-subtree n 'cut))
  4393. (defun org-copy-subtree (&optional n cut force-store-markers)
  4394. "Cut the current subtree into the clipboard.
  4395. With prefix arg N, cut this many sequential subtrees.
  4396. This is a short-hand for marking the subtree and then copying it.
  4397. If CUT is non-nil, actually cut the subtree.
  4398. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4399. of some markers in the region, even if CUT is non-nil. This is
  4400. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4401. (interactive "p")
  4402. (let (beg end folded (beg0 (point)))
  4403. (if (interactive-p)
  4404. (org-back-to-heading nil) ; take what looks like a subtree
  4405. (org-back-to-heading t)) ; take what is really there
  4406. (org-back-over-empty-lines)
  4407. (setq beg (point))
  4408. (skip-chars-forward " \t\r\n")
  4409. (save-match-data
  4410. (save-excursion (outline-end-of-heading)
  4411. (setq folded (org-invisible-p)))
  4412. (condition-case nil
  4413. (outline-forward-same-level (1- n))
  4414. (error nil))
  4415. (org-end-of-subtree t t))
  4416. (org-back-over-empty-lines)
  4417. (setq end (point))
  4418. (goto-char beg0)
  4419. (when (> end beg)
  4420. (setq org-subtree-clip-folded folded)
  4421. (when (or cut force-store-markers)
  4422. (org-save-markers-in-region beg end))
  4423. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4424. (setq org-subtree-clip (current-kill 0))
  4425. (message "%s: Subtree(s) with %d characters"
  4426. (if cut "Cut" "Copied")
  4427. (length org-subtree-clip)))))
  4428. (defun org-paste-subtree (&optional level tree)
  4429. "Paste the clipboard as a subtree, with modification of headline level.
  4430. The entire subtree is promoted or demoted in order to match a new headline
  4431. level. By default, the new level is derived from the visible headings
  4432. before and after the insertion point, and taken to be the inferior headline
  4433. level of the two. So if the previous visible heading is level 3 and the
  4434. next is level 4 (or vice versa), level 4 will be used for insertion.
  4435. This makes sure that the subtree remains an independent subtree and does
  4436. not swallow low level entries.
  4437. You can also force a different level, either by using a numeric prefix
  4438. argument, or by inserting the heading marker by hand. For example, if the
  4439. cursor is after \"*****\", then the tree will be shifted to level 5.
  4440. If you want to insert the tree as is, just use \\[yank].
  4441. If optional TREE is given, use this text instead of the kill ring."
  4442. (interactive "P")
  4443. (unless (org-kill-is-subtree-p tree)
  4444. (error "%s"
  4445. (substitute-command-keys
  4446. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4447. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4448. (^re (concat "^\\(" outline-regexp "\\)"))
  4449. (re (concat "\\(" outline-regexp "\\)"))
  4450. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4451. (old-level (if (string-match ^re txt)
  4452. (- (match-end 0) (match-beginning 0) 1)
  4453. -1))
  4454. (force-level (cond (level (prefix-numeric-value level))
  4455. ((string-match
  4456. ^re_ (buffer-substring (point-at-bol) (point)))
  4457. (- (match-end 1) (match-beginning 1)))
  4458. (t nil)))
  4459. (previous-level (save-excursion
  4460. (condition-case nil
  4461. (progn
  4462. (outline-previous-visible-heading 1)
  4463. (if (looking-at re)
  4464. (- (match-end 0) (match-beginning 0) 1)
  4465. 1))
  4466. (error 1))))
  4467. (next-level (save-excursion
  4468. (condition-case nil
  4469. (progn
  4470. (or (looking-at outline-regexp)
  4471. (outline-next-visible-heading 1))
  4472. (if (looking-at re)
  4473. (- (match-end 0) (match-beginning 0) 1)
  4474. 1))
  4475. (error 1))))
  4476. (new-level (or force-level (max previous-level next-level)))
  4477. (shift (if (or (= old-level -1)
  4478. (= new-level -1)
  4479. (= old-level new-level))
  4480. 0
  4481. (- new-level old-level)))
  4482. (delta (if (> shift 0) -1 1))
  4483. (func (if (> shift 0) 'org-demote 'org-promote))
  4484. (org-odd-levels-only nil)
  4485. beg end)
  4486. ;; Remove the forced level indicator
  4487. (if force-level
  4488. (delete-region (point-at-bol) (point)))
  4489. ;; Paste
  4490. (beginning-of-line 1)
  4491. (org-back-over-empty-lines)
  4492. (setq beg (point))
  4493. (insert-before-markers txt)
  4494. (unless (string-match "\n\\'" txt) (insert "\n"))
  4495. (org-reinstall-markers-in-region beg)
  4496. (setq end (point))
  4497. (goto-char beg)
  4498. (skip-chars-forward " \t\n\r")
  4499. (setq beg (point))
  4500. ;; Shift if necessary
  4501. (unless (= shift 0)
  4502. (save-restriction
  4503. (narrow-to-region beg end)
  4504. (while (not (= shift 0))
  4505. (org-map-region func (point-min) (point-max))
  4506. (setq shift (+ delta shift)))
  4507. (goto-char (point-min))))
  4508. (when (interactive-p)
  4509. (message "Clipboard pasted as level %d subtree" new-level))
  4510. (if (and kill-ring
  4511. (eq org-subtree-clip (current-kill 0))
  4512. org-subtree-clip-folded)
  4513. ;; The tree was folded before it was killed/copied
  4514. (hide-subtree))))
  4515. (defun org-kill-is-subtree-p (&optional txt)
  4516. "Check if the current kill is an outline subtree, or a set of trees.
  4517. Returns nil if kill does not start with a headline, or if the first
  4518. headline level is not the largest headline level in the tree.
  4519. So this will actually accept several entries of equal levels as well,
  4520. which is OK for `org-paste-subtree'.
  4521. If optional TXT is given, check this string instead of the current kill."
  4522. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4523. (start-level (and kill
  4524. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4525. org-outline-regexp "\\)")
  4526. kill)
  4527. (- (match-end 2) (match-beginning 2) 1)))
  4528. (re (concat "^" org-outline-regexp))
  4529. (start (1+ (match-beginning 2))))
  4530. (if (not start-level)
  4531. (progn
  4532. nil) ;; does not even start with a heading
  4533. (catch 'exit
  4534. (while (setq start (string-match re kill (1+ start)))
  4535. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4536. (throw 'exit nil)))
  4537. t))))
  4538. (defvar org-markers-to-move nil
  4539. "Markers that should be moved with a cut-and-paste operation.
  4540. Those markers are stored together with their positions relative to
  4541. the start of the region.")
  4542. (defun org-save-markers-in-region (beg end)
  4543. "Check markers in region.
  4544. If these markers are between BEG and END, record their position relative
  4545. to BEG, so that after moving the block of text, we can put the markers back
  4546. into place.
  4547. This function gets called just before an entry or tree gets cut from the
  4548. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4549. called immediately, to move the markers with the entries."
  4550. (setq org-markers-to-move nil)
  4551. (when (featurep 'org-clock)
  4552. (org-clock-save-markers-for-cut-and-paste beg end))
  4553. (when (featurep 'org-agenda)
  4554. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4555. (defun org-check-and-save-marker (marker beg end)
  4556. "Check if MARKER is between BEG and END.
  4557. If yes, remember the marker and the distance to BEG."
  4558. (when (and (marker-buffer marker)
  4559. (equal (marker-buffer marker) (current-buffer)))
  4560. (if (and (>= marker beg) (< marker end))
  4561. (push (cons marker (- marker beg)) org-markers-to-move))))
  4562. (defun org-reinstall-markers-in-region (beg)
  4563. "Move all remembered markers to their position relative to BEG."
  4564. (mapc (lambda (x)
  4565. (move-marker (car x) (+ beg (cdr x))))
  4566. org-markers-to-move)
  4567. (setq org-markers-to-move nil))
  4568. (defun org-narrow-to-subtree ()
  4569. "Narrow buffer to the current subtree."
  4570. (interactive)
  4571. (save-excursion
  4572. (save-match-data
  4573. (narrow-to-region
  4574. (progn (org-back-to-heading) (point))
  4575. (progn (org-end-of-subtree t t) (point))))))
  4576. ;;; Outline Sorting
  4577. (defun org-sort (with-case)
  4578. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4579. Optional argument WITH-CASE means sort case-sensitively."
  4580. (interactive "P")
  4581. (if (org-at-table-p)
  4582. (org-call-with-arg 'org-table-sort-lines with-case)
  4583. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4584. (defun org-sort-remove-invisible (s)
  4585. (remove-text-properties 0 (length s) org-rm-props s)
  4586. (while (string-match org-bracket-link-regexp s)
  4587. (setq s (replace-match (if (match-end 2)
  4588. (match-string 3 s)
  4589. (match-string 1 s)) t t s)))
  4590. s)
  4591. (defvar org-priority-regexp) ; defined later in the file
  4592. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4593. "Sort entries on a certain level of an outline tree.
  4594. If there is an active region, the entries in the region are sorted.
  4595. Else, if the cursor is before the first entry, sort the top-level items.
  4596. Else, the children of the entry at point are sorted.
  4597. Sorting can be alphabetically, numerically, and by date/time as given by
  4598. the first time stamp in the entry. The command prompts for the sorting
  4599. type unless it has been given to the function through the SORTING-TYPE
  4600. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4601. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4602. called with point at the beginning of the record. It must return either
  4603. a string or a number that should serve as the sorting key for that record.
  4604. Comparing entries ignores case by default. However, with an optional argument
  4605. WITH-CASE, the sorting considers case as well."
  4606. (interactive "P")
  4607. (let ((case-func (if with-case 'identity 'downcase))
  4608. start beg end stars re re2
  4609. txt what tmp plain-list-p)
  4610. ;; Find beginning and end of region to sort
  4611. (cond
  4612. ((org-region-active-p)
  4613. ;; we will sort the region
  4614. (setq end (region-end)
  4615. what "region")
  4616. (goto-char (region-beginning))
  4617. (if (not (org-on-heading-p)) (outline-next-heading))
  4618. (setq start (point)))
  4619. ((org-at-item-p)
  4620. ;; we will sort this plain list
  4621. (org-beginning-of-item-list) (setq start (point))
  4622. (org-end-of-item-list) (setq end (point))
  4623. (goto-char start)
  4624. (setq plain-list-p t
  4625. what "plain list"))
  4626. ((or (org-on-heading-p)
  4627. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4628. ;; we will sort the children of the current headline
  4629. (org-back-to-heading)
  4630. (setq start (point)
  4631. end (progn (org-end-of-subtree t t)
  4632. (org-back-over-empty-lines)
  4633. (point))
  4634. what "children")
  4635. (goto-char start)
  4636. (show-subtree)
  4637. (outline-next-heading))
  4638. (t
  4639. ;; we will sort the top-level entries in this file
  4640. (goto-char (point-min))
  4641. (or (org-on-heading-p) (outline-next-heading))
  4642. (setq start (point) end (point-max) what "top-level")
  4643. (goto-char start)
  4644. (show-all)))
  4645. (setq beg (point))
  4646. (if (>= beg end) (error "Nothing to sort"))
  4647. (unless plain-list-p
  4648. (looking-at "\\(\\*+\\)")
  4649. (setq stars (match-string 1)
  4650. re (concat "^" (regexp-quote stars) " +")
  4651. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4652. txt (buffer-substring beg end))
  4653. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4654. (if (and (not (equal stars "*")) (string-match re2 txt))
  4655. (error "Region to sort contains a level above the first entry")))
  4656. (unless sorting-type
  4657. (message
  4658. (if plain-list-p
  4659. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4660. "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:")
  4661. what)
  4662. (setq sorting-type (read-char-exclusive))
  4663. (and (= (downcase sorting-type) ?f)
  4664. (setq getkey-func
  4665. (completing-read "Sort using function: "
  4666. obarray 'fboundp t nil nil))
  4667. (setq getkey-func (intern getkey-func)))
  4668. (and (= (downcase sorting-type) ?r)
  4669. (setq property
  4670. (completing-read "Property: "
  4671. (mapcar 'list (org-buffer-property-keys t))
  4672. nil t))))
  4673. (message "Sorting entries...")
  4674. (save-restriction
  4675. (narrow-to-region start end)
  4676. (let ((dcst (downcase sorting-type))
  4677. (now (current-time)))
  4678. (sort-subr
  4679. (/= dcst sorting-type)
  4680. ;; This function moves to the beginning character of the "record" to
  4681. ;; be sorted.
  4682. (if plain-list-p
  4683. (lambda nil
  4684. (if (org-at-item-p) t (goto-char (point-max))))
  4685. (lambda nil
  4686. (if (re-search-forward re nil t)
  4687. (goto-char (match-beginning 0))
  4688. (goto-char (point-max)))))
  4689. ;; This function moves to the last character of the "record" being
  4690. ;; sorted.
  4691. (if plain-list-p
  4692. 'org-end-of-item
  4693. (lambda nil
  4694. (save-match-data
  4695. (condition-case nil
  4696. (outline-forward-same-level 1)
  4697. (error
  4698. (goto-char (point-max)))))))
  4699. ;; This function returns the value that gets sorted against.
  4700. (if plain-list-p
  4701. (lambda nil
  4702. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4703. (cond
  4704. ((= dcst ?n)
  4705. (string-to-number (buffer-substring (match-end 0)
  4706. (point-at-eol))))
  4707. ((= dcst ?a)
  4708. (buffer-substring (match-end 0) (point-at-eol)))
  4709. ((= dcst ?t)
  4710. (if (re-search-forward org-ts-regexp
  4711. (point-at-eol) t)
  4712. (org-time-string-to-time (match-string 0))
  4713. now))
  4714. ((= dcst ?f)
  4715. (if getkey-func
  4716. (progn
  4717. (setq tmp (funcall getkey-func))
  4718. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4719. tmp)
  4720. (error "Invalid key function `%s'" getkey-func)))
  4721. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4722. (lambda nil
  4723. (cond
  4724. ((= dcst ?n)
  4725. (if (looking-at outline-regexp)
  4726. (string-to-number (buffer-substring (match-end 0)
  4727. (point-at-eol)))
  4728. nil))
  4729. ((= dcst ?a)
  4730. (funcall case-func (buffer-substring (point-at-bol)
  4731. (point-at-eol))))
  4732. ((= dcst ?t)
  4733. (if (re-search-forward org-ts-regexp
  4734. (save-excursion
  4735. (forward-line 2)
  4736. (point)) t)
  4737. (org-time-string-to-time (match-string 0))
  4738. now))
  4739. ((= dcst ?p)
  4740. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4741. (string-to-char (match-string 2))
  4742. org-default-priority))
  4743. ((= dcst ?r)
  4744. (or (org-entry-get nil property) ""))
  4745. ((= dcst ?o)
  4746. (if (looking-at org-complex-heading-regexp)
  4747. (- 9999 (length (member (match-string 2)
  4748. org-todo-keywords-1)))))
  4749. ((= dcst ?f)
  4750. (if getkey-func
  4751. (progn
  4752. (setq tmp (funcall getkey-func))
  4753. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4754. tmp)
  4755. (error "Invalid key function `%s'" getkey-func)))
  4756. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4757. nil
  4758. (cond
  4759. ((= dcst ?a) 'string<)
  4760. ((= dcst ?t) 'time-less-p)
  4761. (t nil)))))
  4762. (message "Sorting entries...done")))
  4763. (defun org-do-sort (table what &optional with-case sorting-type)
  4764. "Sort TABLE of WHAT according to SORTING-TYPE.
  4765. The user will be prompted for the SORTING-TYPE if the call to this
  4766. function does not specify it. WHAT is only for the prompt, to indicate
  4767. what is being sorted. The sorting key will be extracted from
  4768. the car of the elements of the table.
  4769. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4770. (unless sorting-type
  4771. (message
  4772. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4773. what)
  4774. (setq sorting-type (read-char-exclusive)))
  4775. (let ((dcst (downcase sorting-type))
  4776. extractfun comparefun)
  4777. ;; Define the appropriate functions
  4778. (cond
  4779. ((= dcst ?n)
  4780. (setq extractfun 'string-to-number
  4781. comparefun (if (= dcst sorting-type) '< '>)))
  4782. ((= dcst ?a)
  4783. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4784. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4785. comparefun (if (= dcst sorting-type)
  4786. 'string<
  4787. (lambda (a b) (and (not (string< a b))
  4788. (not (string= a b)))))))
  4789. ((= dcst ?t)
  4790. (setq extractfun
  4791. (lambda (x)
  4792. (if (string-match org-ts-regexp x)
  4793. (time-to-seconds
  4794. (org-time-string-to-time (match-string 0 x)))
  4795. 0))
  4796. comparefun (if (= dcst sorting-type) '< '>)))
  4797. (t (error "Invalid sorting type `%c'" sorting-type)))
  4798. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4799. table)
  4800. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4801. ;;;; Plain list items, including checkboxes
  4802. ;;; Plain list items
  4803. (defun org-at-item-p ()
  4804. "Is point in a line starting a hand-formatted item?"
  4805. (let ((llt org-plain-list-ordered-item-terminator))
  4806. (save-excursion
  4807. (goto-char (point-at-bol))
  4808. (looking-at
  4809. (cond
  4810. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4811. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4812. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4813. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4814. (defun org-in-item-p ()
  4815. "It the cursor inside a plain list item.
  4816. Does not have to be the first line."
  4817. (save-excursion
  4818. (condition-case nil
  4819. (progn
  4820. (org-beginning-of-item)
  4821. (org-at-item-p)
  4822. t)
  4823. (error nil))))
  4824. (defun org-insert-item (&optional checkbox)
  4825. "Insert a new item at the current level.
  4826. Return t when things worked, nil when we are not in an item."
  4827. (when (save-excursion
  4828. (condition-case nil
  4829. (progn
  4830. (org-beginning-of-item)
  4831. (org-at-item-p)
  4832. (if (org-invisible-p) (error "Invisible item"))
  4833. t)
  4834. (error nil)))
  4835. (let* ((bul (match-string 0))
  4836. (descp (save-excursion (goto-char (match-beginning 0))
  4837. (beginning-of-line 1)
  4838. (save-match-data
  4839. (looking-at "[ \t]*.*? ::"))))
  4840. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4841. (match-end 0)))
  4842. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4843. pos)
  4844. (if descp (setq checkbox nil))
  4845. (cond
  4846. ((and (org-at-item-p) (<= (point) eow))
  4847. ;; before the bullet
  4848. (beginning-of-line 1)
  4849. (open-line (if blank 2 1)))
  4850. ((<= (point) eow)
  4851. (beginning-of-line 1))
  4852. (t
  4853. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4854. (end-of-line 1)
  4855. (delete-horizontal-space))
  4856. (newline (if blank 2 1))))
  4857. (insert bul
  4858. (if checkbox "[ ]" "")
  4859. (if descp (concat (if checkbox " " "")
  4860. (read-string "Term: ") " :: ") ""))
  4861. (just-one-space)
  4862. (setq pos (point))
  4863. (end-of-line 1)
  4864. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4865. (org-maybe-renumber-ordered-list)
  4866. (and checkbox (org-update-checkbox-count-maybe))
  4867. t))
  4868. ;;; Checkboxes
  4869. (defun org-at-item-checkbox-p ()
  4870. "Is point at a line starting a plain-list item with a checklet?"
  4871. (and (org-at-item-p)
  4872. (save-excursion
  4873. (goto-char (match-end 0))
  4874. (skip-chars-forward " \t")
  4875. (looking-at "\\[[- X]\\]"))))
  4876. (defun org-toggle-checkbox (&optional arg)
  4877. "Toggle the checkbox in the current line."
  4878. (interactive "P")
  4879. (catch 'exit
  4880. (let (beg end status (firstnew 'unknown))
  4881. (cond
  4882. ((org-region-active-p)
  4883. (setq beg (region-beginning) end (region-end)))
  4884. ((org-on-heading-p)
  4885. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4886. ((org-at-item-checkbox-p)
  4887. (let ((pos (point)))
  4888. (replace-match
  4889. (cond (arg "[-]")
  4890. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4891. (t "[ ]"))
  4892. t t)
  4893. (goto-char pos))
  4894. (throw 'exit t))
  4895. (t (error "Not at a checkbox or heading, and no active region")))
  4896. (save-excursion
  4897. (goto-char beg)
  4898. (while (< (point) end)
  4899. (when (org-at-item-checkbox-p)
  4900. (setq status (equal (match-string 0) "[X]"))
  4901. (when (eq firstnew 'unknown)
  4902. (setq firstnew (not status)))
  4903. (replace-match
  4904. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4905. (beginning-of-line 2)))))
  4906. (org-update-checkbox-count-maybe))
  4907. (defun org-update-checkbox-count-maybe ()
  4908. "Update checkbox statistics unless turned off by user."
  4909. (when org-provide-checkbox-statistics
  4910. (org-update-checkbox-count)))
  4911. (defun org-update-checkbox-count (&optional all)
  4912. "Update the checkbox statistics in the current section.
  4913. This will find all statistic cookies like [57%] and [6/12] and update them
  4914. with the current numbers. With optional prefix argument ALL, do this for
  4915. the whole buffer."
  4916. (interactive "P")
  4917. (save-excursion
  4918. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4919. (beg (condition-case nil
  4920. (progn (outline-back-to-heading) (point))
  4921. (error (point-min))))
  4922. (end (move-marker (make-marker)
  4923. (progn (outline-next-heading) (point))))
  4924. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4925. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4926. (re-find (concat re "\\|" re-box))
  4927. beg-cookie end-cookie is-percent c-on c-off lim
  4928. eline curr-ind next-ind continue-from startsearch
  4929. (cstat 0)
  4930. )
  4931. (when all
  4932. (goto-char (point-min))
  4933. (outline-next-heading)
  4934. (setq beg (point) end (point-max)))
  4935. (goto-char end)
  4936. ;; find each statistic cookie
  4937. (while (re-search-backward re-find beg t)
  4938. (setq beg-cookie (match-beginning 1)
  4939. end-cookie (match-end 1)
  4940. cstat (+ cstat (if end-cookie 1 0))
  4941. startsearch (point-at-eol)
  4942. continue-from (point-at-bol)
  4943. is-percent (match-beginning 2)
  4944. lim (cond
  4945. ((org-on-heading-p) (outline-next-heading) (point))
  4946. ((org-at-item-p) (org-end-of-item) (point))
  4947. (t nil))
  4948. c-on 0
  4949. c-off 0)
  4950. (when lim
  4951. ;; find first checkbox for this cookie and gather
  4952. ;; statistics from all that are at this indentation level
  4953. (goto-char startsearch)
  4954. (if (re-search-forward re-box lim t)
  4955. (progn
  4956. (org-beginning-of-item)
  4957. (setq curr-ind (org-get-indentation))
  4958. (setq next-ind curr-ind)
  4959. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4960. (save-excursion (end-of-line) (setq eline (point)))
  4961. (if (re-search-forward re-box eline t)
  4962. (if (member (match-string 2) '("[ ]" "[-]"))
  4963. (setq c-off (1+ c-off))
  4964. (setq c-on (1+ c-on))
  4965. )
  4966. )
  4967. (org-end-of-item)
  4968. (setq next-ind (org-get-indentation))
  4969. )))
  4970. (goto-char continue-from)
  4971. ;; update cookie
  4972. (when end-cookie
  4973. (delete-region beg-cookie end-cookie)
  4974. (goto-char beg-cookie)
  4975. (insert
  4976. (if is-percent
  4977. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4978. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4979. ;; update items checkbox if it has one
  4980. (when (org-at-item-p)
  4981. (org-beginning-of-item)
  4982. (when (and (> (+ c-on c-off) 0)
  4983. (re-search-forward re-box (point-at-eol) t))
  4984. (setq beg-cookie (match-beginning 2)
  4985. end-cookie (match-end 2))
  4986. (delete-region beg-cookie end-cookie)
  4987. (goto-char beg-cookie)
  4988. (cond ((= c-off 0) (insert "[X]"))
  4989. ((= c-on 0) (insert "[ ]"))
  4990. (t (insert "[-]")))
  4991. )))
  4992. (goto-char continue-from))
  4993. (when (interactive-p)
  4994. (message "Checkbox satistics updated %s (%d places)"
  4995. (if all "in entire file" "in current outline entry") cstat)))))
  4996. (defun org-get-checkbox-statistics-face ()
  4997. "Select the face for checkbox statistics.
  4998. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4999. it will be `org-todo'."
  5000. (if (match-end 1)
  5001. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5002. (if (and (> (match-end 2) (match-beginning 2))
  5003. (equal (match-string 2) (match-string 3)))
  5004. 'org-done
  5005. 'org-todo)))
  5006. (defun org-get-indentation (&optional line)
  5007. "Get the indentation of the current line, interpreting tabs.
  5008. When LINE is given, assume it represents a line and compute its indentation."
  5009. (if line
  5010. (if (string-match "^ *" (org-remove-tabs line))
  5011. (match-end 0))
  5012. (save-excursion
  5013. (beginning-of-line 1)
  5014. (skip-chars-forward " \t")
  5015. (current-column))))
  5016. (defun org-remove-tabs (s &optional width)
  5017. "Replace tabulators in S with spaces.
  5018. Assumes that s is a single line, starting in column 0."
  5019. (setq width (or width tab-width))
  5020. (while (string-match "\t" s)
  5021. (setq s (replace-match
  5022. (make-string
  5023. (- (* width (/ (+ (match-beginning 0) width) width))
  5024. (match-beginning 0)) ?\ )
  5025. t t s)))
  5026. s)
  5027. (defun org-fix-indentation (line ind)
  5028. "Fix indentation in LINE.
  5029. IND is a cons cell with target and minimum indentation.
  5030. If the current indenation in LINE is smaller than the minimum,
  5031. leave it alone. If it is larger than ind, set it to the target."
  5032. (let* ((l (org-remove-tabs line))
  5033. (i (org-get-indentation l))
  5034. (i1 (car ind)) (i2 (cdr ind)))
  5035. (if (>= i i2) (setq l (substring line i2)))
  5036. (if (> i1 0)
  5037. (concat (make-string i1 ?\ ) l)
  5038. l)))
  5039. (defun org-beginning-of-item ()
  5040. "Go to the beginning of the current hand-formatted item.
  5041. If the cursor is not in an item, throw an error."
  5042. (interactive)
  5043. (let ((pos (point))
  5044. (limit (save-excursion
  5045. (condition-case nil
  5046. (progn
  5047. (org-back-to-heading)
  5048. (beginning-of-line 2) (point))
  5049. (error (point-min)))))
  5050. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5051. ind ind1)
  5052. (if (org-at-item-p)
  5053. (beginning-of-line 1)
  5054. (beginning-of-line 1)
  5055. (skip-chars-forward " \t")
  5056. (setq ind (current-column))
  5057. (if (catch 'exit
  5058. (while t
  5059. (beginning-of-line 0)
  5060. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5061. (if (looking-at "[ \t]*$")
  5062. (setq ind1 ind-empty)
  5063. (skip-chars-forward " \t")
  5064. (setq ind1 (current-column)))
  5065. (if (< ind1 ind)
  5066. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5067. nil
  5068. (goto-char pos)
  5069. (error "Not in an item")))))
  5070. (defun org-end-of-item ()
  5071. "Go to the end of the current hand-formatted item.
  5072. If the cursor is not in an item, throw an error."
  5073. (interactive)
  5074. (let* ((pos (point))
  5075. ind1
  5076. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5077. (limit (save-excursion (outline-next-heading) (point)))
  5078. (ind (save-excursion
  5079. (org-beginning-of-item)
  5080. (skip-chars-forward " \t")
  5081. (current-column)))
  5082. (end (catch 'exit
  5083. (while t
  5084. (beginning-of-line 2)
  5085. (if (eobp) (throw 'exit (point)))
  5086. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5087. (if (looking-at "[ \t]*$")
  5088. (setq ind1 ind-empty)
  5089. (skip-chars-forward " \t")
  5090. (setq ind1 (current-column)))
  5091. (if (<= ind1 ind)
  5092. (throw 'exit (point-at-bol)))))))
  5093. (if end
  5094. (goto-char end)
  5095. (goto-char pos)
  5096. (error "Not in an item"))))
  5097. (defun org-next-item ()
  5098. "Move to the beginning of the next item in the current plain list.
  5099. Error if not at a plain list, or if this is the last item in the list."
  5100. (interactive)
  5101. (let (ind ind1 (pos (point)))
  5102. (org-beginning-of-item)
  5103. (setq ind (org-get-indentation))
  5104. (org-end-of-item)
  5105. (setq ind1 (org-get-indentation))
  5106. (unless (and (org-at-item-p) (= ind ind1))
  5107. (goto-char pos)
  5108. (error "On last item"))))
  5109. (defun org-previous-item ()
  5110. "Move to the beginning of the previous item in the current plain list.
  5111. Error if not at a plain list, or if this is the first item in the list."
  5112. (interactive)
  5113. (let (beg ind ind1 (pos (point)))
  5114. (org-beginning-of-item)
  5115. (setq beg (point))
  5116. (setq ind (org-get-indentation))
  5117. (goto-char beg)
  5118. (catch 'exit
  5119. (while t
  5120. (beginning-of-line 0)
  5121. (if (looking-at "[ \t]*$")
  5122. nil
  5123. (if (<= (setq ind1 (org-get-indentation)) ind)
  5124. (throw 'exit t)))))
  5125. (condition-case nil
  5126. (if (or (not (org-at-item-p))
  5127. (< ind1 (1- ind)))
  5128. (error "")
  5129. (org-beginning-of-item))
  5130. (error (goto-char pos)
  5131. (error "On first item")))))
  5132. (defun org-first-list-item-p ()
  5133. "Is this heading the item in a plain list?"
  5134. (unless (org-at-item-p)
  5135. (error "Not at a plain list item"))
  5136. (org-beginning-of-item)
  5137. (= (point) (save-excursion (org-beginning-of-item-list))))
  5138. (defun org-move-item-down ()
  5139. "Move the plain list item at point down, i.e. swap with following item.
  5140. Subitems (items with larger indentation) are considered part of the item,
  5141. so this really moves item trees."
  5142. (interactive)
  5143. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5144. (org-beginning-of-item)
  5145. (setq beg0 (point))
  5146. (save-excursion
  5147. (setq ne-beg (org-back-over-empty-lines))
  5148. (setq beg (point)))
  5149. (goto-char beg0)
  5150. (setq ind (org-get-indentation))
  5151. (org-end-of-item)
  5152. (setq end0 (point))
  5153. (setq ind1 (org-get-indentation))
  5154. (setq ne-end (org-back-over-empty-lines))
  5155. (setq end (point))
  5156. (goto-char beg0)
  5157. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5158. ;; include less whitespace
  5159. (save-excursion
  5160. (goto-char beg)
  5161. (forward-line (- ne-beg ne-end))
  5162. (setq beg (point))))
  5163. (goto-char end0)
  5164. (if (and (org-at-item-p) (= ind ind1))
  5165. (progn
  5166. (org-end-of-item)
  5167. (org-back-over-empty-lines)
  5168. (setq txt (buffer-substring beg end))
  5169. (save-excursion
  5170. (delete-region beg end))
  5171. (setq pos (point))
  5172. (insert txt)
  5173. (goto-char pos) (org-skip-whitespace)
  5174. (org-maybe-renumber-ordered-list))
  5175. (goto-char pos)
  5176. (error "Cannot move this item further down"))))
  5177. (defun org-move-item-up (arg)
  5178. "Move the plain list item at point up, i.e. swap with previous item.
  5179. Subitems (items with larger indentation) are considered part of the item,
  5180. so this really moves item trees."
  5181. (interactive "p")
  5182. (let (beg beg0 end ind ind1 (pos (point)) txt
  5183. ne-beg ne-ins ins-end)
  5184. (org-beginning-of-item)
  5185. (setq beg0 (point))
  5186. (setq ind (org-get-indentation))
  5187. (save-excursion
  5188. (setq ne-beg (org-back-over-empty-lines))
  5189. (setq beg (point)))
  5190. (goto-char beg0)
  5191. (org-end-of-item)
  5192. (setq end (point))
  5193. (goto-char beg0)
  5194. (catch 'exit
  5195. (while t
  5196. (beginning-of-line 0)
  5197. (if (looking-at "[ \t]*$")
  5198. (if org-empty-line-terminates-plain-lists
  5199. (progn
  5200. (goto-char pos)
  5201. (error "Cannot move this item further up"))
  5202. nil)
  5203. (if (<= (setq ind1 (org-get-indentation)) ind)
  5204. (throw 'exit t)))))
  5205. (condition-case nil
  5206. (org-beginning-of-item)
  5207. (error (goto-char beg)
  5208. (error "Cannot move this item further up")))
  5209. (setq ind1 (org-get-indentation))
  5210. (if (and (org-at-item-p) (= ind ind1))
  5211. (progn
  5212. (setq ne-ins (org-back-over-empty-lines))
  5213. (setq txt (buffer-substring beg end))
  5214. (save-excursion
  5215. (delete-region beg end))
  5216. (setq pos (point))
  5217. (insert txt)
  5218. (setq ins-end (point))
  5219. (goto-char pos) (org-skip-whitespace)
  5220. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5221. ;; Move whitespace back to beginning
  5222. (save-excursion
  5223. (goto-char ins-end)
  5224. (let ((kill-whole-line t))
  5225. (kill-line (- ne-ins ne-beg)) (point)))
  5226. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5227. (org-maybe-renumber-ordered-list))
  5228. (goto-char pos)
  5229. (error "Cannot move this item further up"))))
  5230. (defun org-maybe-renumber-ordered-list ()
  5231. "Renumber the ordered list at point if setup allows it.
  5232. This tests the user option `org-auto-renumber-ordered-lists' before
  5233. doing the renumbering."
  5234. (interactive)
  5235. (when (and org-auto-renumber-ordered-lists
  5236. (org-at-item-p))
  5237. (if (match-beginning 3)
  5238. (org-renumber-ordered-list 1)
  5239. (org-fix-bullet-type))))
  5240. (defun org-maybe-renumber-ordered-list-safe ()
  5241. (condition-case nil
  5242. (save-excursion
  5243. (org-maybe-renumber-ordered-list))
  5244. (error nil)))
  5245. (defun org-cycle-list-bullet (&optional which)
  5246. "Cycle through the different itemize/enumerate bullets.
  5247. This cycle the entire list level through the sequence:
  5248. `-' -> `+' -> `*' -> `1.' -> `1)'
  5249. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5250. 0 meand `-', 1 means `+' etc."
  5251. (interactive "P")
  5252. (org-preserve-lc
  5253. (org-beginning-of-item-list)
  5254. (org-at-item-p)
  5255. (beginning-of-line 1)
  5256. (let ((current (match-string 0))
  5257. (prevp (eq which 'previous))
  5258. new)
  5259. (setq new (cond
  5260. ((and (numberp which)
  5261. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5262. ((string-match "-" current) (if prevp "1)" "+"))
  5263. ((string-match "\\+" current)
  5264. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5265. ((string-match "\\*" current) (if prevp "+" "1."))
  5266. ((string-match "\\." current) (if prevp "*" "1)"))
  5267. ((string-match ")" current) (if prevp "1." "-"))
  5268. (t (error "This should not happen"))))
  5269. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5270. (org-fix-bullet-type)
  5271. (org-maybe-renumber-ordered-list))))
  5272. (defun org-get-string-indentation (s)
  5273. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5274. (let ((n -1) (i 0) (w tab-width) c)
  5275. (catch 'exit
  5276. (while (< (setq n (1+ n)) (length s))
  5277. (setq c (aref s n))
  5278. (cond ((= c ?\ ) (setq i (1+ i)))
  5279. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5280. (t (throw 'exit t)))))
  5281. i))
  5282. (defun org-renumber-ordered-list (arg)
  5283. "Renumber an ordered plain list.
  5284. Cursor needs to be in the first line of an item, the line that starts
  5285. with something like \"1.\" or \"2)\"."
  5286. (interactive "p")
  5287. (unless (and (org-at-item-p)
  5288. (match-beginning 3))
  5289. (error "This is not an ordered list"))
  5290. (let ((line (org-current-line))
  5291. (col (current-column))
  5292. (ind (org-get-string-indentation
  5293. (buffer-substring (point-at-bol) (match-beginning 3))))
  5294. ;; (term (substring (match-string 3) -1))
  5295. ind1 (n (1- arg))
  5296. fmt)
  5297. ;; find where this list begins
  5298. (org-beginning-of-item-list)
  5299. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5300. (setq fmt (concat "%d" (match-string 1)))
  5301. (beginning-of-line 0)
  5302. ;; walk forward and replace these numbers
  5303. (catch 'exit
  5304. (while t
  5305. (catch 'next
  5306. (beginning-of-line 2)
  5307. (if (eobp) (throw 'exit nil))
  5308. (if (looking-at "[ \t]*$") (throw 'next nil))
  5309. (skip-chars-forward " \t") (setq ind1 (current-column))
  5310. (if (> ind1 ind) (throw 'next t))
  5311. (if (< ind1 ind) (throw 'exit t))
  5312. (if (not (org-at-item-p)) (throw 'exit nil))
  5313. (delete-region (match-beginning 2) (match-end 2))
  5314. (goto-char (match-beginning 2))
  5315. (insert (format fmt (setq n (1+ n)))))))
  5316. (goto-line line)
  5317. (org-move-to-column col)))
  5318. (defun org-fix-bullet-type ()
  5319. "Make sure all items in this list have the same bullet as the firsst item."
  5320. (interactive)
  5321. (unless (org-at-item-p) (error "This is not a list"))
  5322. (let ((line (org-current-line))
  5323. (col (current-column))
  5324. (ind (current-indentation))
  5325. ind1 bullet)
  5326. ;; find where this list begins
  5327. (org-beginning-of-item-list)
  5328. (beginning-of-line 1)
  5329. ;; find out what the bullet type is
  5330. (looking-at "[ \t]*\\(\\S-+\\)")
  5331. (setq bullet (match-string 1))
  5332. ;; walk forward and replace these numbers
  5333. (beginning-of-line 0)
  5334. (catch 'exit
  5335. (while t
  5336. (catch 'next
  5337. (beginning-of-line 2)
  5338. (if (eobp) (throw 'exit nil))
  5339. (if (looking-at "[ \t]*$") (throw 'next nil))
  5340. (skip-chars-forward " \t") (setq ind1 (current-column))
  5341. (if (> ind1 ind) (throw 'next t))
  5342. (if (< ind1 ind) (throw 'exit t))
  5343. (if (not (org-at-item-p)) (throw 'exit nil))
  5344. (skip-chars-forward " \t")
  5345. (looking-at "\\S-+")
  5346. (replace-match bullet))))
  5347. (goto-line line)
  5348. (org-move-to-column col)
  5349. (if (string-match "[0-9]" bullet)
  5350. (org-renumber-ordered-list 1))))
  5351. (defun org-beginning-of-item-list ()
  5352. "Go to the beginning of the current item list.
  5353. I.e. to the first item in this list."
  5354. (interactive)
  5355. (org-beginning-of-item)
  5356. (let ((pos (point-at-bol))
  5357. (ind (org-get-indentation))
  5358. ind1)
  5359. ;; find where this list begins
  5360. (catch 'exit
  5361. (while t
  5362. (catch 'next
  5363. (beginning-of-line 0)
  5364. (if (looking-at "[ \t]*$")
  5365. (throw (if (bobp) 'exit 'next) t))
  5366. (skip-chars-forward " \t") (setq ind1 (current-column))
  5367. (if (or (< ind1 ind)
  5368. (and (= ind1 ind)
  5369. (not (org-at-item-p)))
  5370. (bobp))
  5371. (throw 'exit t)
  5372. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5373. (goto-char pos)))
  5374. (defun org-end-of-item-list ()
  5375. "Go to the end of the current item list.
  5376. I.e. to the text after the last item."
  5377. (interactive)
  5378. (org-beginning-of-item)
  5379. (let ((pos (point-at-bol))
  5380. (ind (org-get-indentation))
  5381. ind1)
  5382. ;; find where this list begins
  5383. (catch 'exit
  5384. (while t
  5385. (catch 'next
  5386. (beginning-of-line 2)
  5387. (if (looking-at "[ \t]*$")
  5388. (throw (if (eobp) 'exit 'next) t))
  5389. (skip-chars-forward " \t") (setq ind1 (current-column))
  5390. (if (or (< ind1 ind)
  5391. (and (= ind1 ind)
  5392. (not (org-at-item-p)))
  5393. (eobp))
  5394. (progn
  5395. (setq pos (point-at-bol))
  5396. (throw 'exit t))))))
  5397. (goto-char pos)))
  5398. (defvar org-last-indent-begin-marker (make-marker))
  5399. (defvar org-last-indent-end-marker (make-marker))
  5400. (defun org-outdent-item (arg)
  5401. "Outdent a local list item."
  5402. (interactive "p")
  5403. (org-indent-item (- arg)))
  5404. (defun org-indent-item (arg)
  5405. "Indent a local list item."
  5406. (interactive "p")
  5407. (unless (org-at-item-p)
  5408. (error "Not on an item"))
  5409. (save-excursion
  5410. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5411. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5412. (setq beg org-last-indent-begin-marker
  5413. end org-last-indent-end-marker)
  5414. (org-beginning-of-item)
  5415. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5416. (org-end-of-item)
  5417. (setq end (move-marker org-last-indent-end-marker (point))))
  5418. (goto-char beg)
  5419. (setq tmp (org-item-indent-positions)
  5420. ind (car tmp)
  5421. ind-down (nth 2 tmp)
  5422. ind-up (nth 1 tmp)
  5423. delta (if (> arg 0)
  5424. (if ind-down (- ind-down ind) 2)
  5425. (if ind-up (- ind-up ind) -2)))
  5426. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5427. (while (< (point) end)
  5428. (beginning-of-line 1)
  5429. (skip-chars-forward " \t") (setq ind1 (current-column))
  5430. (delete-region (point-at-bol) (point))
  5431. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5432. (beginning-of-line 2))))
  5433. (org-fix-bullet-type)
  5434. (org-maybe-renumber-ordered-list-safe)
  5435. (save-excursion
  5436. (beginning-of-line 0)
  5437. (condition-case nil (org-beginning-of-item) (error nil))
  5438. (org-maybe-renumber-ordered-list-safe)))
  5439. (defun org-item-indent-positions ()
  5440. "Return indentation for plain list items.
  5441. This returns a list with three values: The current indentation, the
  5442. parent indentation and the indentation a child should habe.
  5443. Assumes cursor in item line."
  5444. (let* ((bolpos (point-at-bol))
  5445. (ind (org-get-indentation))
  5446. ind-down ind-up pos)
  5447. (save-excursion
  5448. (org-beginning-of-item-list)
  5449. (skip-chars-backward "\n\r \t")
  5450. (when (org-in-item-p)
  5451. (org-beginning-of-item)
  5452. (setq ind-up (org-get-indentation))))
  5453. (setq pos (point))
  5454. (save-excursion
  5455. (cond
  5456. ((and (condition-case nil (progn (org-previous-item) t)
  5457. (error nil))
  5458. (or (forward-char 1) t)
  5459. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5460. (setq ind-down (org-get-indentation)))
  5461. ((and (goto-char pos)
  5462. (org-at-item-p))
  5463. (goto-char (match-end 0))
  5464. (skip-chars-forward " \t")
  5465. (setq ind-down (current-column)))))
  5466. (list ind ind-up ind-down)))
  5467. ;;; The orgstruct minor mode
  5468. ;; Define a minor mode which can be used in other modes in order to
  5469. ;; integrate the org-mode structure editing commands.
  5470. ;; This is really a hack, because the org-mode structure commands use
  5471. ;; keys which normally belong to the major mode. Here is how it
  5472. ;; works: The minor mode defines all the keys necessary to operate the
  5473. ;; structure commands, but wraps the commands into a function which
  5474. ;; tests if the cursor is currently at a headline or a plain list
  5475. ;; item. If that is the case, the structure command is used,
  5476. ;; temporarily setting many Org-mode variables like regular
  5477. ;; expressions for filling etc. However, when any of those keys is
  5478. ;; used at a different location, function uses `key-binding' to look
  5479. ;; up if the key has an associated command in another currently active
  5480. ;; keymap (minor modes, major mode, global), and executes that
  5481. ;; command. There might be problems if any of the keys is otherwise
  5482. ;; used as a prefix key.
  5483. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5484. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5485. ;; addresses this by checking explicitly for both bindings.
  5486. (defvar orgstruct-mode-map (make-sparse-keymap)
  5487. "Keymap for the minor `orgstruct-mode'.")
  5488. (defvar org-local-vars nil
  5489. "List of local variables, for use by `orgstruct-mode'")
  5490. ;;;###autoload
  5491. (define-minor-mode orgstruct-mode
  5492. "Toggle the minor more `orgstruct-mode'.
  5493. This mode is for using Org-mode structure commands in other modes.
  5494. The following key behave as if Org-mode was active, if the cursor
  5495. is on a headline, or on a plain list item (both in the definition
  5496. of Org-mode).
  5497. M-up Move entry/item up
  5498. M-down Move entry/item down
  5499. M-left Promote
  5500. M-right Demote
  5501. M-S-up Move entry/item up
  5502. M-S-down Move entry/item down
  5503. M-S-left Promote subtree
  5504. M-S-right Demote subtree
  5505. M-q Fill paragraph and items like in Org-mode
  5506. C-c ^ Sort entries
  5507. C-c - Cycle list bullet
  5508. TAB Cycle item visibility
  5509. M-RET Insert new heading/item
  5510. S-M-RET Insert new TODO heading / Chekbox item
  5511. C-c C-c Set tags / toggle checkbox"
  5512. nil " OrgStruct" nil
  5513. (org-load-modules-maybe)
  5514. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5515. ;;;###autoload
  5516. (defun turn-on-orgstruct ()
  5517. "Unconditionally turn on `orgstruct-mode'."
  5518. (orgstruct-mode 1))
  5519. ;;;###autoload
  5520. (defun turn-on-orgstruct++ ()
  5521. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5522. In addition to setting orgstruct-mode, this also exports all indentation and
  5523. autofilling variables from org-mode into the buffer. Note that turning
  5524. off orgstruct-mode will *not* remove these additional settings."
  5525. (orgstruct-mode 1)
  5526. (let (var val)
  5527. (mapc
  5528. (lambda (x)
  5529. (when (string-match
  5530. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5531. (symbol-name (car x)))
  5532. (setq var (car x) val (nth 1 x))
  5533. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5534. org-local-vars)))
  5535. (defun orgstruct-error ()
  5536. "Error when there is no default binding for a structure key."
  5537. (interactive)
  5538. (error "This key has no function outside structure elements"))
  5539. (defun orgstruct-setup ()
  5540. "Setup orgstruct keymaps."
  5541. (let ((nfunc 0)
  5542. (bindings
  5543. (list
  5544. '([(meta up)] org-metaup)
  5545. '([(meta down)] org-metadown)
  5546. '([(meta left)] org-metaleft)
  5547. '([(meta right)] org-metaright)
  5548. '([(meta shift up)] org-shiftmetaup)
  5549. '([(meta shift down)] org-shiftmetadown)
  5550. '([(meta shift left)] org-shiftmetaleft)
  5551. '([(meta shift right)] org-shiftmetaright)
  5552. '([(shift up)] org-shiftup)
  5553. '([(shift down)] org-shiftdown)
  5554. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5555. '("\M-q" fill-paragraph)
  5556. '("\C-c^" org-sort)
  5557. '("\C-c-" org-cycle-list-bullet)))
  5558. elt key fun cmd)
  5559. (while (setq elt (pop bindings))
  5560. (setq nfunc (1+ nfunc))
  5561. (setq key (org-key (car elt))
  5562. fun (nth 1 elt)
  5563. cmd (orgstruct-make-binding fun nfunc key))
  5564. (org-defkey orgstruct-mode-map key cmd))
  5565. ;; Special treatment needed for TAB and RET
  5566. (org-defkey orgstruct-mode-map [(tab)]
  5567. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5568. (org-defkey orgstruct-mode-map "\C-i"
  5569. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5570. (org-defkey orgstruct-mode-map "\M-\C-m"
  5571. (orgstruct-make-binding 'org-insert-heading 105
  5572. "\M-\C-m" [(meta return)]))
  5573. (org-defkey orgstruct-mode-map [(meta return)]
  5574. (orgstruct-make-binding 'org-insert-heading 106
  5575. [(meta return)] "\M-\C-m"))
  5576. (org-defkey orgstruct-mode-map [(shift meta return)]
  5577. (orgstruct-make-binding 'org-insert-todo-heading 107
  5578. [(meta return)] "\M-\C-m"))
  5579. (unless org-local-vars
  5580. (setq org-local-vars (org-get-local-variables)))
  5581. t))
  5582. (defun orgstruct-make-binding (fun n &rest keys)
  5583. "Create a function for binding in the structure minor mode.
  5584. FUN is the command to call inside a table. N is used to create a unique
  5585. command name. KEYS are keys that should be checked in for a command
  5586. to execute outside of tables."
  5587. (eval
  5588. (list 'defun
  5589. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5590. '(arg)
  5591. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5592. "Outside of structure, run the binding of `"
  5593. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5594. "'.")
  5595. '(interactive "p")
  5596. (list 'if
  5597. '(org-context-p 'headline 'item)
  5598. (list 'org-run-like-in-org-mode (list 'quote fun))
  5599. (list 'let '(orgstruct-mode)
  5600. (list 'call-interactively
  5601. (append '(or)
  5602. (mapcar (lambda (k)
  5603. (list 'key-binding k))
  5604. keys)
  5605. '('orgstruct-error))))))))
  5606. (defun org-context-p (&rest contexts)
  5607. "Check if local context is and of CONTEXTS.
  5608. Possible values in the list of contexts are `table', `headline', and `item'."
  5609. (let ((pos (point)))
  5610. (goto-char (point-at-bol))
  5611. (prog1 (or (and (memq 'table contexts)
  5612. (looking-at "[ \t]*|"))
  5613. (and (memq 'headline contexts)
  5614. (looking-at "\\*+"))
  5615. (and (memq 'item contexts)
  5616. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5617. (goto-char pos))))
  5618. (defun org-get-local-variables ()
  5619. "Return a list of all local variables in an org-mode buffer."
  5620. (let (varlist)
  5621. (with-current-buffer (get-buffer-create "*Org tmp*")
  5622. (erase-buffer)
  5623. (org-mode)
  5624. (setq varlist (buffer-local-variables)))
  5625. (kill-buffer "*Org tmp*")
  5626. (delq nil
  5627. (mapcar
  5628. (lambda (x)
  5629. (setq x
  5630. (if (symbolp x)
  5631. (list x)
  5632. (list (car x) (list 'quote (cdr x)))))
  5633. (if (string-match
  5634. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5635. (symbol-name (car x)))
  5636. x nil))
  5637. varlist))))
  5638. ;;;###autoload
  5639. (defun org-run-like-in-org-mode (cmd)
  5640. (org-load-modules-maybe)
  5641. (unless org-local-vars
  5642. (setq org-local-vars (org-get-local-variables)))
  5643. (eval (list 'let org-local-vars
  5644. (list 'call-interactively (list 'quote cmd)))))
  5645. ;;;; Archiving
  5646. (defun org-get-category (&optional pos)
  5647. "Get the category applying to position POS."
  5648. (get-text-property (or pos (point)) 'org-category))
  5649. (defun org-refresh-category-properties ()
  5650. "Refresh category text properties in the buffer."
  5651. (let ((def-cat (cond
  5652. ((null org-category)
  5653. (if buffer-file-name
  5654. (file-name-sans-extension
  5655. (file-name-nondirectory buffer-file-name))
  5656. "???"))
  5657. ((symbolp org-category) (symbol-name org-category))
  5658. (t org-category)))
  5659. beg end cat pos optionp)
  5660. (org-unmodified
  5661. (save-excursion
  5662. (save-restriction
  5663. (widen)
  5664. (goto-char (point-min))
  5665. (put-text-property (point) (point-max) 'org-category def-cat)
  5666. (while (re-search-forward
  5667. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5668. (setq pos (match-end 0)
  5669. optionp (equal (char-after (match-beginning 0)) ?#)
  5670. cat (org-trim (match-string 2)))
  5671. (if optionp
  5672. (setq beg (point-at-bol) end (point-max))
  5673. (org-back-to-heading t)
  5674. (setq beg (point) end (org-end-of-subtree t t)))
  5675. (put-text-property beg end 'org-category cat)
  5676. (goto-char pos)))))))
  5677. ;;;; Link Stuff
  5678. ;;; Link abbreviations
  5679. (defun org-link-expand-abbrev (link)
  5680. "Apply replacements as defined in `org-link-abbrev-alist."
  5681. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5682. (let* ((key (match-string 1 link))
  5683. (as (or (assoc key org-link-abbrev-alist-local)
  5684. (assoc key org-link-abbrev-alist)))
  5685. (tag (and (match-end 2) (match-string 3 link)))
  5686. rpl)
  5687. (if (not as)
  5688. link
  5689. (setq rpl (cdr as))
  5690. (cond
  5691. ((symbolp rpl) (funcall rpl tag))
  5692. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5693. (t (concat rpl tag)))))
  5694. link))
  5695. ;;; Storing and inserting links
  5696. (defvar org-insert-link-history nil
  5697. "Minibuffer history for links inserted with `org-insert-link'.")
  5698. (defvar org-stored-links nil
  5699. "Contains the links stored with `org-store-link'.")
  5700. (defvar org-store-link-plist nil
  5701. "Plist with info about the most recently link created with `org-store-link'.")
  5702. (defvar org-link-protocols nil
  5703. "Link protocols added to Org-mode using `org-add-link-type'.")
  5704. (defvar org-store-link-functions nil
  5705. "List of functions that are called to create and store a link.
  5706. Each function will be called in turn until one returns a non-nil
  5707. value. Each function should check if it is responsible for creating
  5708. this link (for example by looking at the major mode).
  5709. If not, it must exit and return nil.
  5710. If yes, it should return a non-nil value after a calling
  5711. `org-store-link-props' with a list of properties and values.
  5712. Special properties are:
  5713. :type The link prefix. like \"http\". This must be given.
  5714. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5715. This is obligatory as well.
  5716. :description Optional default description for the second pair
  5717. of brackets in an Org-mode link. The user can still change
  5718. this when inserting this link into an Org-mode buffer.
  5719. In addition to these, any additional properties can be specified
  5720. and then used in remember templates.")
  5721. (defun org-add-link-type (type &optional follow export)
  5722. "Add TYPE to the list of `org-link-types'.
  5723. Re-compute all regular expressions depending on `org-link-types'
  5724. FOLLOW and EXPORT are two functions.
  5725. FOLLOW should take the link path as the single argument and do whatever
  5726. is necessary to follow the link, for example find a file or display
  5727. a mail message.
  5728. EXPORT should format the link path for export to one of the export formats.
  5729. It should be a function accepting three arguments:
  5730. path the path of the link, the text after the prefix (like \"http:\")
  5731. desc the description of the link, if any, nil if there was no descripton
  5732. format the export format, a symbol like `html' or `latex'.
  5733. The function may use the FORMAT information to return different values
  5734. depending on the format. The return value will be put literally into
  5735. the exported file.
  5736. Org-mode has a built-in default for exporting links. If you are happy with
  5737. this default, there is no need to define an export function for the link
  5738. type. For a simple example of an export function, see `org-bbdb.el'."
  5739. (add-to-list 'org-link-types type t)
  5740. (org-make-link-regexps)
  5741. (if (assoc type org-link-protocols)
  5742. (setcdr (assoc type org-link-protocols) (list follow export))
  5743. (push (list type follow export) org-link-protocols)))
  5744. ;;;###autoload
  5745. (defun org-store-link (arg)
  5746. "\\<org-mode-map>Store an org-link to the current location.
  5747. This link is added to `org-stored-links' and can later be inserted
  5748. into an org-buffer with \\[org-insert-link].
  5749. For some link types, a prefix arg is interpreted:
  5750. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5751. For file links, arg negates `org-context-in-file-links'."
  5752. (interactive "P")
  5753. (org-load-modules-maybe)
  5754. (setq org-store-link-plist nil) ; reset
  5755. (let (link cpltxt desc description search txt)
  5756. (cond
  5757. ((run-hook-with-args-until-success 'org-store-link-functions)
  5758. (setq link (plist-get org-store-link-plist :link)
  5759. desc (or (plist-get org-store-link-plist :description) link)))
  5760. ((eq major-mode 'calendar-mode)
  5761. (let ((cd (calendar-cursor-to-date)))
  5762. (setq link
  5763. (format-time-string
  5764. (car org-time-stamp-formats)
  5765. (apply 'encode-time
  5766. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5767. nil nil nil))))
  5768. (org-store-link-props :type "calendar" :date cd)))
  5769. ((eq major-mode 'w3-mode)
  5770. (setq cpltxt (url-view-url t)
  5771. link (org-make-link cpltxt))
  5772. (org-store-link-props :type "w3" :url (url-view-url t)))
  5773. ((eq major-mode 'w3m-mode)
  5774. (setq cpltxt (or w3m-current-title w3m-current-url)
  5775. link (org-make-link w3m-current-url))
  5776. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5777. ((setq search (run-hook-with-args-until-success
  5778. 'org-create-file-search-functions))
  5779. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5780. "::" search))
  5781. (setq cpltxt (or description link)))
  5782. ((eq major-mode 'image-mode)
  5783. (setq cpltxt (concat "file:"
  5784. (abbreviate-file-name buffer-file-name))
  5785. link (org-make-link cpltxt))
  5786. (org-store-link-props :type "image" :file buffer-file-name))
  5787. ((eq major-mode 'dired-mode)
  5788. ;; link to the file in the current line
  5789. (setq cpltxt (concat "file:"
  5790. (abbreviate-file-name
  5791. (expand-file-name
  5792. (dired-get-filename nil t))))
  5793. link (org-make-link cpltxt)))
  5794. ((and buffer-file-name (org-mode-p))
  5795. ;; Just link to current headline
  5796. (setq cpltxt (concat "file:"
  5797. (abbreviate-file-name buffer-file-name)))
  5798. ;; Add a context search string
  5799. (when (org-xor org-context-in-file-links arg)
  5800. ;; Check if we are on a target
  5801. (if (org-in-regexp "<<\\(.*?\\)>>")
  5802. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5803. (setq txt (cond
  5804. ((org-on-heading-p) nil)
  5805. ((org-region-active-p)
  5806. (buffer-substring (region-beginning) (region-end)))
  5807. (t nil)))
  5808. (when (or (null txt) (string-match "\\S-" txt))
  5809. (setq cpltxt
  5810. (concat cpltxt "::"
  5811. (condition-case nil
  5812. (org-make-org-heading-search-string txt)
  5813. (error "")))
  5814. desc "NONE"))))
  5815. (if (string-match "::\\'" cpltxt)
  5816. (setq cpltxt (substring cpltxt 0 -2)))
  5817. (setq link (org-make-link cpltxt)))
  5818. ((buffer-file-name (buffer-base-buffer))
  5819. ;; Just link to this file here.
  5820. (setq cpltxt (concat "file:"
  5821. (abbreviate-file-name
  5822. (buffer-file-name (buffer-base-buffer)))))
  5823. ;; Add a context string
  5824. (when (org-xor org-context-in-file-links arg)
  5825. (setq txt (if (org-region-active-p)
  5826. (buffer-substring (region-beginning) (region-end))
  5827. (buffer-substring (point-at-bol) (point-at-eol))))
  5828. ;; Only use search option if there is some text.
  5829. (when (string-match "\\S-" txt)
  5830. (setq cpltxt
  5831. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5832. desc "NONE")))
  5833. (setq link (org-make-link cpltxt)))
  5834. ((interactive-p)
  5835. (error "Cannot link to a buffer which is not visiting a file"))
  5836. (t (setq link nil)))
  5837. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5838. (setq link (or link cpltxt)
  5839. desc (or desc cpltxt))
  5840. (if (equal desc "NONE") (setq desc nil))
  5841. (if (and (interactive-p) link)
  5842. (progn
  5843. (setq org-stored-links
  5844. (cons (list link desc) org-stored-links))
  5845. (message "Stored: %s" (or desc link)))
  5846. (and link (org-make-link-string link desc)))))
  5847. (defun org-store-link-props (&rest plist)
  5848. "Store link properties, extract names and addresses."
  5849. (let (x adr)
  5850. (when (setq x (plist-get plist :from))
  5851. (setq adr (mail-extract-address-components x))
  5852. (plist-put plist :fromname (car adr))
  5853. (plist-put plist :fromaddress (nth 1 adr)))
  5854. (when (setq x (plist-get plist :to))
  5855. (setq adr (mail-extract-address-components x))
  5856. (plist-put plist :toname (car adr))
  5857. (plist-put plist :toaddress (nth 1 adr))))
  5858. (let ((from (plist-get plist :from))
  5859. (to (plist-get plist :to)))
  5860. (when (and from to org-from-is-user-regexp)
  5861. (plist-put plist :fromto
  5862. (if (string-match org-from-is-user-regexp from)
  5863. (concat "to %t")
  5864. (concat "from %f")))))
  5865. (setq org-store-link-plist plist))
  5866. (defun org-add-link-props (&rest plist)
  5867. "Add these properties to the link property list."
  5868. (let (key value)
  5869. (while plist
  5870. (setq key (pop plist) value (pop plist))
  5871. (setq org-store-link-plist
  5872. (plist-put org-store-link-plist key value)))))
  5873. (defun org-email-link-description (&optional fmt)
  5874. "Return the description part of an email link.
  5875. This takes information from `org-store-link-plist' and formats it
  5876. according to FMT (default from `org-email-link-description-format')."
  5877. (setq fmt (or fmt org-email-link-description-format))
  5878. (let* ((p org-store-link-plist)
  5879. (to (plist-get p :toaddress))
  5880. (from (plist-get p :fromaddress))
  5881. (table
  5882. (list
  5883. (cons "%c" (plist-get p :fromto))
  5884. (cons "%F" (plist-get p :from))
  5885. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5886. (cons "%T" (plist-get p :to))
  5887. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5888. (cons "%s" (plist-get p :subject))
  5889. (cons "%m" (plist-get p :message-id)))))
  5890. (when (string-match "%c" fmt)
  5891. ;; Check if the user wrote this message
  5892. (if (and org-from-is-user-regexp from to
  5893. (save-match-data (string-match org-from-is-user-regexp from)))
  5894. (setq fmt (replace-match "to %t" t t fmt))
  5895. (setq fmt (replace-match "from %f" t t fmt))))
  5896. (org-replace-escapes fmt table)))
  5897. (defun org-make-org-heading-search-string (&optional string heading)
  5898. "Make search string for STRING or current headline."
  5899. (interactive)
  5900. (let ((s (or string (org-get-heading))))
  5901. (unless (and string (not heading))
  5902. ;; We are using a headline, clean up garbage in there.
  5903. (if (string-match org-todo-regexp s)
  5904. (setq s (replace-match "" t t s)))
  5905. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5906. (setq s (replace-match "" t t s)))
  5907. (setq s (org-trim s))
  5908. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5909. org-comment-string "\\)") s)
  5910. (setq s (replace-match "" t t s)))
  5911. (while (string-match org-ts-regexp s)
  5912. (setq s (replace-match "" t t s))))
  5913. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5914. (setq s (replace-match " " t t s)))
  5915. (or string (setq s (concat "*" s))) ; Add * for headlines
  5916. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5917. (defun org-make-link (&rest strings)
  5918. "Concatenate STRINGS."
  5919. (apply 'concat strings))
  5920. (defun org-make-link-string (link &optional description)
  5921. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5922. (unless (string-match "\\S-" link)
  5923. (error "Empty link"))
  5924. (when (stringp description)
  5925. ;; Remove brackets from the description, they are fatal.
  5926. (while (string-match "\\[" description)
  5927. (setq description (replace-match "{" t t description)))
  5928. (while (string-match "\\]" description)
  5929. (setq description (replace-match "}" t t description))))
  5930. (when (equal (org-link-escape link) description)
  5931. ;; No description needed, it is identical
  5932. (setq description nil))
  5933. (when (and (not description)
  5934. (not (equal link (org-link-escape link))))
  5935. (setq description link))
  5936. (concat "[[" (org-link-escape link) "]"
  5937. (if description (concat "[" description "]") "")
  5938. "]"))
  5939. (defconst org-link-escape-chars
  5940. '((?\ . "%20")
  5941. (?\[ . "%5B")
  5942. (?\] . "%5D")
  5943. (?\340 . "%E0") ; `a
  5944. (?\342 . "%E2") ; ^a
  5945. (?\347 . "%E7") ; ,c
  5946. (?\350 . "%E8") ; `e
  5947. (?\351 . "%E9") ; 'e
  5948. (?\352 . "%EA") ; ^e
  5949. (?\356 . "%EE") ; ^i
  5950. (?\364 . "%F4") ; ^o
  5951. (?\371 . "%F9") ; `u
  5952. (?\373 . "%FB") ; ^u
  5953. (?\; . "%3B")
  5954. (?? . "%3F")
  5955. (?= . "%3D")
  5956. (?+ . "%2B")
  5957. )
  5958. "Association list of escapes for some characters problematic in links.
  5959. This is the list that is used for internal purposes.")
  5960. (defconst org-link-escape-chars-browser
  5961. '((?\ . "%20")) ; 32 for the SPC char
  5962. "Association list of escapes for some characters problematic in links.
  5963. This is the list that is used before handing over to the browser.")
  5964. (defun org-link-escape (text &optional table)
  5965. "Escape charaters in TEXT that are problematic for links."
  5966. (setq table (or table org-link-escape-chars))
  5967. (when text
  5968. (let ((re (mapconcat (lambda (x) (regexp-quote
  5969. (char-to-string (car x))))
  5970. table "\\|")))
  5971. (while (string-match re text)
  5972. (setq text
  5973. (replace-match
  5974. (cdr (assoc (string-to-char (match-string 0 text))
  5975. table))
  5976. t t text)))
  5977. text)))
  5978. (defun org-link-unescape (text &optional table)
  5979. "Reverse the action of `org-link-escape'."
  5980. (setq table (or table org-link-escape-chars))
  5981. (when text
  5982. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5983. table "\\|")))
  5984. (while (string-match re text)
  5985. (setq text
  5986. (replace-match
  5987. (char-to-string (car (rassoc (match-string 0 text) table)))
  5988. t t text)))
  5989. text)))
  5990. (defun org-xor (a b)
  5991. "Exclusive or."
  5992. (if a (not b) b))
  5993. (defun org-get-header (header)
  5994. "Find a header field in the current buffer."
  5995. (save-excursion
  5996. (goto-char (point-min))
  5997. (let ((case-fold-search t) s)
  5998. (cond
  5999. ((eq header 'from)
  6000. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6001. (setq s (match-string 1)))
  6002. (while (string-match "\"" s)
  6003. (setq s (replace-match "" t t s)))
  6004. (if (string-match "[<(].*" s)
  6005. (setq s (replace-match "" t t s))))
  6006. ((eq header 'message-id)
  6007. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6008. (setq s (match-string 1))))
  6009. ((eq header 'subject)
  6010. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6011. (setq s (match-string 1)))))
  6012. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6013. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6014. s)))
  6015. (defun org-fixup-message-id-for-http (s)
  6016. "Replace special characters in a message id, so it can be used in an http query."
  6017. (while (string-match "<" s)
  6018. (setq s (replace-match "%3C" t t s)))
  6019. (while (string-match ">" s)
  6020. (setq s (replace-match "%3E" t t s)))
  6021. (while (string-match "@" s)
  6022. (setq s (replace-match "%40" t t s)))
  6023. s)
  6024. ;;;###autoload
  6025. (defun org-insert-link-global ()
  6026. "Insert a link like Org-mode does.
  6027. This command can be called in any mode to insert a link in Org-mode syntax."
  6028. (interactive)
  6029. (org-load-modules-maybe)
  6030. (org-run-like-in-org-mode 'org-insert-link))
  6031. (defun org-insert-link (&optional complete-file link-location)
  6032. "Insert a link. At the prompt, enter the link.
  6033. Completion can be used to select a link previously stored with
  6034. `org-store-link'. When the empty string is entered (i.e. if you just
  6035. press RET at the prompt), the link defaults to the most recently
  6036. stored link. As SPC triggers completion in the minibuffer, you need to
  6037. use M-SPC or C-q SPC to force the insertion of a space character.
  6038. You will also be prompted for a description, and if one is given, it will
  6039. be displayed in the buffer instead of the link.
  6040. If there is already a link at point, this command will allow you to edit link
  6041. and description parts.
  6042. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6043. be selected using completion. The path to the file will be relative to the
  6044. current directory if the file is in the current directory or a subdirectory.
  6045. Otherwise, the link will be the absolute path as completed in the minibuffer
  6046. \(i.e. normally ~/path/to/file).
  6047. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6048. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6049. of `org-keep-stored-link-after-insertion'.
  6050. If `org-make-link-description-function' is non-nil, this function will be
  6051. called with the link target, and the result will be the default
  6052. link description.
  6053. If the LINK-LOCATION parameter is non-nil, this value will be
  6054. used as the link location instead of reading one interactively."
  6055. (interactive "P")
  6056. (let* ((wcf (current-window-configuration))
  6057. (region (if (org-region-active-p)
  6058. (buffer-substring (region-beginning) (region-end))))
  6059. (remove (and region (list (region-beginning) (region-end))))
  6060. (desc region)
  6061. tmphist ; byte-compile incorrectly complains about this
  6062. (link link-location)
  6063. entry file)
  6064. (cond
  6065. (link-location) ; specified by arg, just use it.
  6066. ((org-in-regexp org-bracket-link-regexp 1)
  6067. ;; We do have a link at point, and we are going to edit it.
  6068. (setq remove (list (match-beginning 0) (match-end 0)))
  6069. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6070. (setq link (read-string "Link: "
  6071. (org-link-unescape
  6072. (org-match-string-no-properties 1)))))
  6073. ((or (org-in-regexp org-angle-link-re)
  6074. (org-in-regexp org-plain-link-re))
  6075. ;; Convert to bracket link
  6076. (setq remove (list (match-beginning 0) (match-end 0))
  6077. link (read-string "Link: "
  6078. (org-remove-angle-brackets (match-string 0)))))
  6079. ((equal complete-file '(4))
  6080. ;; Completing read for file names.
  6081. (setq file (read-file-name "File: "))
  6082. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6083. (pwd1 (file-name-as-directory (abbreviate-file-name
  6084. (expand-file-name ".")))))
  6085. (cond
  6086. ((equal complete-file '(16))
  6087. (setq link (org-make-link
  6088. "file:"
  6089. (abbreviate-file-name (expand-file-name file)))))
  6090. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6091. (setq link (org-make-link "file:" (match-string 1 file))))
  6092. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6093. (expand-file-name file))
  6094. (setq link (org-make-link
  6095. "file:" (match-string 1 (expand-file-name file)))))
  6096. (t (setq link (org-make-link "file:" file))))))
  6097. (t
  6098. ;; Read link, with completion for stored links.
  6099. (with-output-to-temp-buffer "*Org Links*"
  6100. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6101. (when org-stored-links
  6102. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6103. (princ (mapconcat
  6104. (lambda (x)
  6105. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6106. (reverse org-stored-links) "\n"))))
  6107. (let ((cw (selected-window)))
  6108. (select-window (get-buffer-window "*Org Links*"))
  6109. (shrink-window-if-larger-than-buffer)
  6110. (setq truncate-lines t)
  6111. (select-window cw))
  6112. ;; Fake a link history, containing the stored links.
  6113. (setq tmphist (append (mapcar 'car org-stored-links)
  6114. org-insert-link-history))
  6115. (unwind-protect
  6116. (setq link (org-completing-read
  6117. "Link: "
  6118. (append
  6119. (mapcar (lambda (x) (list (concat (car x) ":")))
  6120. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6121. (mapcar (lambda (x) (list (concat x ":")))
  6122. org-link-types))
  6123. nil nil nil
  6124. 'tmphist
  6125. (or (car (car org-stored-links)))))
  6126. (set-window-configuration wcf)
  6127. (kill-buffer "*Org Links*"))
  6128. (setq entry (assoc link org-stored-links))
  6129. (or entry (push link org-insert-link-history))
  6130. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6131. (not org-keep-stored-link-after-insertion))
  6132. (setq org-stored-links (delq (assoc link org-stored-links)
  6133. org-stored-links)))
  6134. (setq desc (or desc (nth 1 entry)))))
  6135. (if (string-match org-plain-link-re link)
  6136. ;; URL-like link, normalize the use of angular brackets.
  6137. (setq link (org-make-link (org-remove-angle-brackets link))))
  6138. ;; Check if we are linking to the current file with a search option
  6139. ;; If yes, simplify the link by using only the search option.
  6140. (when (and buffer-file-name
  6141. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6142. (let* ((path (match-string 1 link))
  6143. (case-fold-search nil)
  6144. (search (match-string 2 link)))
  6145. (save-match-data
  6146. (if (equal (file-truename buffer-file-name) (file-truename path))
  6147. ;; We are linking to this same file, with a search option
  6148. (setq link search)))))
  6149. ;; Check if we can/should use a relative path. If yes, simplify the link
  6150. (when (string-match "\\<file:\\(.*\\)" link)
  6151. (let* ((path (match-string 1 link))
  6152. (origpath path)
  6153. (case-fold-search nil))
  6154. (cond
  6155. ((eq org-link-file-path-type 'absolute)
  6156. (setq path (abbreviate-file-name (expand-file-name path))))
  6157. ((eq org-link-file-path-type 'noabbrev)
  6158. (setq path (expand-file-name path)))
  6159. ((eq org-link-file-path-type 'relative)
  6160. (setq path (file-relative-name path)))
  6161. (t
  6162. (save-match-data
  6163. (if (string-match (concat "^" (regexp-quote
  6164. (file-name-as-directory
  6165. (expand-file-name "."))))
  6166. (expand-file-name path))
  6167. ;; We are linking a file with relative path name.
  6168. (setq path (substring (expand-file-name path)
  6169. (match-end 0)))))))
  6170. (setq link (concat "file:" path))
  6171. (if (equal desc origpath)
  6172. (setq desc path))))
  6173. (if org-make-link-description-function
  6174. (setq desc (funcall org-make-link-description-function link desc)))
  6175. (setq desc (read-string "Description: " desc))
  6176. (unless (string-match "\\S-" desc) (setq desc nil))
  6177. (if remove (apply 'delete-region remove))
  6178. (insert (org-make-link-string link desc))))
  6179. (defun org-completing-read (&rest args)
  6180. (let ((minibuffer-local-completion-map
  6181. (copy-keymap minibuffer-local-completion-map)))
  6182. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6183. (apply 'completing-read args)))
  6184. ;;; Opening/following a link
  6185. (defvar org-link-search-failed nil)
  6186. (defun org-next-link ()
  6187. "Move forward to the next link.
  6188. If the link is in hidden text, expose it."
  6189. (interactive)
  6190. (when (and org-link-search-failed (eq this-command last-command))
  6191. (goto-char (point-min))
  6192. (message "Link search wrapped back to beginning of buffer"))
  6193. (setq org-link-search-failed nil)
  6194. (let* ((pos (point))
  6195. (ct (org-context))
  6196. (a (assoc :link ct)))
  6197. (if a (goto-char (nth 2 a)))
  6198. (if (re-search-forward org-any-link-re nil t)
  6199. (progn
  6200. (goto-char (match-beginning 0))
  6201. (if (org-invisible-p) (org-show-context)))
  6202. (goto-char pos)
  6203. (setq org-link-search-failed t)
  6204. (error "No further link found"))))
  6205. (defun org-previous-link ()
  6206. "Move backward to the previous link.
  6207. If the link is in hidden text, expose it."
  6208. (interactive)
  6209. (when (and org-link-search-failed (eq this-command last-command))
  6210. (goto-char (point-max))
  6211. (message "Link search wrapped back to end of buffer"))
  6212. (setq org-link-search-failed nil)
  6213. (let* ((pos (point))
  6214. (ct (org-context))
  6215. (a (assoc :link ct)))
  6216. (if a (goto-char (nth 1 a)))
  6217. (if (re-search-backward org-any-link-re nil t)
  6218. (progn
  6219. (goto-char (match-beginning 0))
  6220. (if (org-invisible-p) (org-show-context)))
  6221. (goto-char pos)
  6222. (setq org-link-search-failed t)
  6223. (error "No further link found"))))
  6224. (defun org-find-file-at-mouse (ev)
  6225. "Open file link or URL at mouse."
  6226. (interactive "e")
  6227. (mouse-set-point ev)
  6228. (org-open-at-point 'in-emacs))
  6229. (defun org-open-at-mouse (ev)
  6230. "Open file link or URL at mouse."
  6231. (interactive "e")
  6232. (mouse-set-point ev)
  6233. (org-open-at-point))
  6234. (defvar org-window-config-before-follow-link nil
  6235. "The window configuration before following a link.
  6236. This is saved in case the need arises to restore it.")
  6237. (defvar org-open-link-marker (make-marker)
  6238. "Marker pointing to the location where `org-open-at-point; was called.")
  6239. ;;;###autoload
  6240. (defun org-open-at-point-global ()
  6241. "Follow a link like Org-mode does.
  6242. This command can be called in any mode to follow a link that has
  6243. Org-mode syntax."
  6244. (interactive)
  6245. (org-run-like-in-org-mode 'org-open-at-point))
  6246. ;;;###autoload
  6247. (defun org-open-link-from-string (s &optional arg)
  6248. "Open a link in the string S, as if it was in Org-mode."
  6249. (interactive "sLink: \nP")
  6250. (with-temp-buffer
  6251. (let ((org-inhibit-startup t))
  6252. (org-mode)
  6253. (insert s)
  6254. (goto-char (point-min))
  6255. (org-open-at-point arg))))
  6256. (defun org-open-at-point (&optional in-emacs)
  6257. "Open link at or after point.
  6258. If there is no link at point, this function will search forward up to
  6259. the end of the current subtree.
  6260. Normally, files will be opened by an appropriate application. If the
  6261. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6262. (interactive "P")
  6263. (org-load-modules-maybe)
  6264. (move-marker org-open-link-marker (point))
  6265. (setq org-window-config-before-follow-link (current-window-configuration))
  6266. (org-remove-occur-highlights nil nil t)
  6267. (if (org-at-timestamp-p t)
  6268. (org-follow-timestamp-link)
  6269. (let (type path link line search (pos (point)))
  6270. (catch 'match
  6271. (save-excursion
  6272. (skip-chars-forward "^]\n\r")
  6273. (when (org-in-regexp org-bracket-link-regexp)
  6274. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6275. (while (string-match " *\n *" link)
  6276. (setq link (replace-match " " t t link)))
  6277. (setq link (org-link-expand-abbrev link))
  6278. (if (string-match org-link-re-with-space2 link)
  6279. (setq type (match-string 1 link) path (match-string 2 link))
  6280. (setq type "thisfile" path link))
  6281. (throw 'match t)))
  6282. (when (get-text-property (point) 'org-linked-text)
  6283. (setq type "thisfile"
  6284. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6285. (1+ (point)) (point))
  6286. path (buffer-substring
  6287. (previous-single-property-change pos 'org-linked-text)
  6288. (next-single-property-change pos 'org-linked-text)))
  6289. (throw 'match t))
  6290. (save-excursion
  6291. (when (or (org-in-regexp org-angle-link-re)
  6292. (org-in-regexp org-plain-link-re))
  6293. (setq type (match-string 1) path (match-string 2))
  6294. (throw 'match t)))
  6295. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6296. (setq type "tree-match"
  6297. path (match-string 1))
  6298. (throw 'match t))
  6299. (save-excursion
  6300. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6301. (setq type "tags"
  6302. path (match-string 1))
  6303. (while (string-match ":" path)
  6304. (setq path (replace-match "+" t t path)))
  6305. (throw 'match t))))
  6306. (unless path
  6307. (error "No link found"))
  6308. ;; Remove any trailing spaces in path
  6309. (if (string-match " +\\'" path)
  6310. (setq path (replace-match "" t t path)))
  6311. (cond
  6312. ((assoc type org-link-protocols)
  6313. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6314. ((equal type "mailto")
  6315. (let ((cmd (car org-link-mailto-program))
  6316. (args (cdr org-link-mailto-program)) args1
  6317. (address path) (subject "") a)
  6318. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6319. (setq address (match-string 1 path)
  6320. subject (org-link-escape (match-string 2 path))))
  6321. (while args
  6322. (cond
  6323. ((not (stringp (car args))) (push (pop args) args1))
  6324. (t (setq a (pop args))
  6325. (if (string-match "%a" a)
  6326. (setq a (replace-match address t t a)))
  6327. (if (string-match "%s" a)
  6328. (setq a (replace-match subject t t a)))
  6329. (push a args1))))
  6330. (apply cmd (nreverse args1))))
  6331. ((member type '("http" "https" "ftp" "news"))
  6332. (browse-url (concat type ":" (org-link-escape
  6333. path org-link-escape-chars-browser))))
  6334. ((member type '("message"))
  6335. (browse-url (concat type ":" path)))
  6336. ((string= type "tags")
  6337. (org-tags-view in-emacs path))
  6338. ((string= type "thisfile")
  6339. (if in-emacs
  6340. (switch-to-buffer-other-window
  6341. (org-get-buffer-for-internal-link (current-buffer)))
  6342. (org-mark-ring-push))
  6343. (let ((cmd `(org-link-search
  6344. ,path
  6345. ,(cond ((equal in-emacs '(4)) 'occur)
  6346. ((equal in-emacs '(16)) 'org-occur)
  6347. (t nil))
  6348. ,pos)))
  6349. (condition-case nil (eval cmd)
  6350. (error (progn (widen) (eval cmd))))))
  6351. ((string= type "tree-match")
  6352. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6353. ((string= type "file")
  6354. (if (string-match "::\\([0-9]+\\)\\'" path)
  6355. (setq line (string-to-number (match-string 1 path))
  6356. path (substring path 0 (match-beginning 0)))
  6357. (if (string-match "::\\(.+\\)\\'" path)
  6358. (setq search (match-string 1 path)
  6359. path (substring path 0 (match-beginning 0)))))
  6360. (if (string-match "[*?{]" (file-name-nondirectory path))
  6361. (dired path)
  6362. (org-open-file path in-emacs line search)))
  6363. ((string= type "news")
  6364. (require 'org-gnus)
  6365. (org-gnus-follow-link path))
  6366. ((string= type "shell")
  6367. (let ((cmd path))
  6368. (if (or (not org-confirm-shell-link-function)
  6369. (funcall org-confirm-shell-link-function
  6370. (format "Execute \"%s\" in shell? "
  6371. (org-add-props cmd nil
  6372. 'face 'org-warning))))
  6373. (progn
  6374. (message "Executing %s" cmd)
  6375. (shell-command cmd))
  6376. (error "Abort"))))
  6377. ((string= type "elisp")
  6378. (let ((cmd path))
  6379. (if (or (not org-confirm-elisp-link-function)
  6380. (funcall org-confirm-elisp-link-function
  6381. (format "Execute \"%s\" as elisp? "
  6382. (org-add-props cmd nil
  6383. 'face 'org-warning))))
  6384. (message "%s => %s" cmd (eval (read cmd)))
  6385. (error "Abort"))))
  6386. (t
  6387. (browse-url-at-point)))))
  6388. (move-marker org-open-link-marker nil)
  6389. (run-hook-with-args 'org-follow-link-hook))
  6390. ;;;; Time estimates
  6391. (defun org-get-effort (&optional pom)
  6392. "Get the effort estimate for the current entry."
  6393. (org-entry-get pom org-effort-property))
  6394. ;;; File search
  6395. (defvar org-create-file-search-functions nil
  6396. "List of functions to construct the right search string for a file link.
  6397. These functions are called in turn with point at the location to
  6398. which the link should point.
  6399. A function in the hook should first test if it would like to
  6400. handle this file type, for example by checking the major-mode or
  6401. the file extension. If it decides not to handle this file, it
  6402. should just return nil to give other functions a chance. If it
  6403. does handle the file, it must return the search string to be used
  6404. when following the link. The search string will be part of the
  6405. file link, given after a double colon, and `org-open-at-point'
  6406. will automatically search for it. If special measures must be
  6407. taken to make the search successful, another function should be
  6408. added to the companion hook `org-execute-file-search-functions',
  6409. which see.
  6410. A function in this hook may also use `setq' to set the variable
  6411. `description' to provide a suggestion for the descriptive text to
  6412. be used for this link when it gets inserted into an Org-mode
  6413. buffer with \\[org-insert-link].")
  6414. (defvar org-execute-file-search-functions nil
  6415. "List of functions to execute a file search triggered by a link.
  6416. Functions added to this hook must accept a single argument, the
  6417. search string that was part of the file link, the part after the
  6418. double colon. The function must first check if it would like to
  6419. handle this search, for example by checking the major-mode or the
  6420. file extension. If it decides not to handle this search, it
  6421. should just return nil to give other functions a chance. If it
  6422. does handle the search, it must return a non-nil value to keep
  6423. other functions from trying.
  6424. Each function can access the current prefix argument through the
  6425. variable `current-prefix-argument'. Note that a single prefix is
  6426. used to force opening a link in Emacs, so it may be good to only
  6427. use a numeric or double prefix to guide the search function.
  6428. In case this is needed, a function in this hook can also restore
  6429. the window configuration before `org-open-at-point' was called using:
  6430. (set-window-configuration org-window-config-before-follow-link)")
  6431. (defun org-link-search (s &optional type avoid-pos)
  6432. "Search for a link search option.
  6433. If S is surrounded by forward slashes, it is interpreted as a
  6434. regular expression. In org-mode files, this will create an `org-occur'
  6435. sparse tree. In ordinary files, `occur' will be used to list matches.
  6436. If the current buffer is in `dired-mode', grep will be used to search
  6437. in all files. If AVOID-POS is given, ignore matches near that position."
  6438. (let ((case-fold-search t)
  6439. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6440. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6441. (append '(("") (" ") ("\t") ("\n"))
  6442. org-emphasis-alist)
  6443. "\\|") "\\)"))
  6444. (pos (point))
  6445. (pre nil) (post nil)
  6446. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6447. (cond
  6448. ;; First check if there are any special
  6449. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6450. ;; Now try the builtin stuff
  6451. ((save-excursion
  6452. (goto-char (point-min))
  6453. (and
  6454. (re-search-forward
  6455. (concat "<<" (regexp-quote s0) ">>") nil t)
  6456. (setq type 'dedicated
  6457. pos (match-beginning 0))))
  6458. ;; There is an exact target for this
  6459. (goto-char pos))
  6460. ((string-match "^/\\(.*\\)/$" s)
  6461. ;; A regular expression
  6462. (cond
  6463. ((org-mode-p)
  6464. (org-occur (match-string 1 s)))
  6465. ;;((eq major-mode 'dired-mode)
  6466. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6467. (t (org-do-occur (match-string 1 s)))))
  6468. (t
  6469. ;; A normal search strings
  6470. (when (equal (string-to-char s) ?*)
  6471. ;; Anchor on headlines, post may include tags.
  6472. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6473. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6474. s (substring s 1)))
  6475. (remove-text-properties
  6476. 0 (length s)
  6477. '(face nil mouse-face nil keymap nil fontified nil) s)
  6478. ;; Make a series of regular expressions to find a match
  6479. (setq words (org-split-string s "[ \n\r\t]+")
  6480. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6481. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6482. "\\)" markers)
  6483. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6484. re2a (concat "[ \t\r\n]" re2a_)
  6485. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6486. re4 (concat "[^a-zA-Z_]" re4_)
  6487. re1 (concat pre re2 post)
  6488. re3 (concat pre (if pre re4_ re4) post)
  6489. re5 (concat pre ".*" re4)
  6490. re2 (concat pre re2)
  6491. re2a (concat pre (if pre re2a_ re2a))
  6492. re4 (concat pre (if pre re4_ re4))
  6493. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6494. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6495. re5 "\\)"
  6496. ))
  6497. (cond
  6498. ((eq type 'org-occur) (org-occur reall))
  6499. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6500. (t (goto-char (point-min))
  6501. (setq type 'fuzzy)
  6502. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6503. (org-search-not-self 1 re1 nil t)
  6504. (org-search-not-self 1 re2 nil t)
  6505. (org-search-not-self 1 re2a nil t)
  6506. (org-search-not-self 1 re3 nil t)
  6507. (org-search-not-self 1 re4 nil t)
  6508. (org-search-not-self 1 re5 nil t)
  6509. )
  6510. (goto-char (match-beginning 1))
  6511. (goto-char pos)
  6512. (error "No match")))))
  6513. (t
  6514. ;; Normal string-search
  6515. (goto-char (point-min))
  6516. (if (search-forward s nil t)
  6517. (goto-char (match-beginning 0))
  6518. (error "No match"))))
  6519. (and (org-mode-p) (org-show-context 'link-search))
  6520. type))
  6521. (defun org-search-not-self (group &rest args)
  6522. "Execute `re-search-forward', but only accept matches that do not
  6523. enclose the position of `org-open-link-marker'."
  6524. (let ((m org-open-link-marker))
  6525. (catch 'exit
  6526. (while (apply 're-search-forward args)
  6527. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6528. (goto-char (match-end group))
  6529. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6530. (> (match-beginning 0) (marker-position m))
  6531. (< (match-end 0) (marker-position m)))
  6532. (save-match-data
  6533. (or (not (org-in-regexp
  6534. org-bracket-link-analytic-regexp 1))
  6535. (not (match-end 4)) ; no description
  6536. (and (<= (match-beginning 4) (point))
  6537. (>= (match-end 4) (point))))))
  6538. (throw 'exit (point))))))))
  6539. (defun org-get-buffer-for-internal-link (buffer)
  6540. "Return a buffer to be used for displaying the link target of internal links."
  6541. (cond
  6542. ((not org-display-internal-link-with-indirect-buffer)
  6543. buffer)
  6544. ((string-match "(Clone)$" (buffer-name buffer))
  6545. (message "Buffer is already a clone, not making another one")
  6546. ;; we also do not modify visibility in this case
  6547. buffer)
  6548. (t ; make a new indirect buffer for displaying the link
  6549. (let* ((bn (buffer-name buffer))
  6550. (ibn (concat bn "(Clone)"))
  6551. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6552. (with-current-buffer ib (org-overview))
  6553. ib))))
  6554. (defun org-do-occur (regexp &optional cleanup)
  6555. "Call the Emacs command `occur'.
  6556. If CLEANUP is non-nil, remove the printout of the regular expression
  6557. in the *Occur* buffer. This is useful if the regex is long and not useful
  6558. to read."
  6559. (occur regexp)
  6560. (when cleanup
  6561. (let ((cwin (selected-window)) win beg end)
  6562. (when (setq win (get-buffer-window "*Occur*"))
  6563. (select-window win))
  6564. (goto-char (point-min))
  6565. (when (re-search-forward "match[a-z]+" nil t)
  6566. (setq beg (match-end 0))
  6567. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6568. (setq end (1- (match-beginning 0)))))
  6569. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6570. (goto-char (point-min))
  6571. (select-window cwin))))
  6572. ;;; The mark ring for links jumps
  6573. (defvar org-mark-ring nil
  6574. "Mark ring for positions before jumps in Org-mode.")
  6575. (defvar org-mark-ring-last-goto nil
  6576. "Last position in the mark ring used to go back.")
  6577. ;; Fill and close the ring
  6578. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6579. (loop for i from 1 to org-mark-ring-length do
  6580. (push (make-marker) org-mark-ring))
  6581. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6582. org-mark-ring)
  6583. (defun org-mark-ring-push (&optional pos buffer)
  6584. "Put the current position or POS into the mark ring and rotate it."
  6585. (interactive)
  6586. (setq pos (or pos (point)))
  6587. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6588. (move-marker (car org-mark-ring)
  6589. (or pos (point))
  6590. (or buffer (current-buffer)))
  6591. (message "%s"
  6592. (substitute-command-keys
  6593. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6594. (defun org-mark-ring-goto (&optional n)
  6595. "Jump to the previous position in the mark ring.
  6596. With prefix arg N, jump back that many stored positions. When
  6597. called several times in succession, walk through the entire ring.
  6598. Org-mode commands jumping to a different position in the current file,
  6599. or to another Org-mode file, automatically push the old position
  6600. onto the ring."
  6601. (interactive "p")
  6602. (let (p m)
  6603. (if (eq last-command this-command)
  6604. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6605. (setq p org-mark-ring))
  6606. (setq org-mark-ring-last-goto p)
  6607. (setq m (car p))
  6608. (switch-to-buffer (marker-buffer m))
  6609. (goto-char m)
  6610. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6611. (defun org-remove-angle-brackets (s)
  6612. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6613. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6614. s)
  6615. (defun org-add-angle-brackets (s)
  6616. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6617. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6618. s)
  6619. (defun org-remove-double-quotes (s)
  6620. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6621. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6622. s)
  6623. ;;; Following specific links
  6624. (defun org-follow-timestamp-link ()
  6625. (cond
  6626. ((org-at-date-range-p t)
  6627. (let ((org-agenda-start-on-weekday)
  6628. (t1 (match-string 1))
  6629. (t2 (match-string 2)))
  6630. (setq t1 (time-to-days (org-time-string-to-time t1))
  6631. t2 (time-to-days (org-time-string-to-time t2)))
  6632. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6633. ((org-at-timestamp-p t)
  6634. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6635. (substring (match-string 1) 0 10)))
  6636. 1))
  6637. (t (error "This should not happen"))))
  6638. ;;; Following file links
  6639. (defvar org-wait nil)
  6640. (defun org-open-file (path &optional in-emacs line search)
  6641. "Open the file at PATH.
  6642. First, this expands any special file name abbreviations. Then the
  6643. configuration variable `org-file-apps' is checked if it contains an
  6644. entry for this file type, and if yes, the corresponding command is launched.
  6645. If no application is found, Emacs simply visits the file.
  6646. With optional argument IN-EMACS, Emacs will visit the file.
  6647. Optional LINE specifies a line to go to, optional SEARCH a string to
  6648. search for. If LINE or SEARCH is given, the file will always be
  6649. opened in Emacs.
  6650. If the file does not exist, an error is thrown."
  6651. (setq in-emacs (or in-emacs line search))
  6652. (let* ((file (if (equal path "")
  6653. buffer-file-name
  6654. (substitute-in-file-name (expand-file-name path))))
  6655. (apps (append org-file-apps (org-default-apps)))
  6656. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6657. (dirp (if remp nil (file-directory-p file)))
  6658. (dfile (downcase file))
  6659. (old-buffer (current-buffer))
  6660. (old-pos (point))
  6661. (old-mode major-mode)
  6662. ext cmd)
  6663. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6664. (setq ext (match-string 1 dfile))
  6665. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6666. (setq ext (match-string 1 dfile))))
  6667. (if in-emacs
  6668. (setq cmd 'emacs)
  6669. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6670. (and dirp (cdr (assoc 'directory apps)))
  6671. (cdr (assoc ext apps))
  6672. (cdr (assoc t apps)))))
  6673. (when (eq cmd 'mailcap)
  6674. (require 'mailcap)
  6675. (mailcap-parse-mailcaps)
  6676. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6677. (command (mailcap-mime-info mime-type)))
  6678. (if (stringp command)
  6679. (setq cmd command)
  6680. (setq cmd 'emacs))))
  6681. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6682. (not (file-exists-p file))
  6683. (not org-open-non-existing-files))
  6684. (error "No such file: %s" file))
  6685. (cond
  6686. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6687. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6688. (while (string-match "['\"]%s['\"]" cmd)
  6689. (setq cmd (replace-match "%s" t t cmd)))
  6690. (while (string-match "%s" cmd)
  6691. (setq cmd (replace-match
  6692. (save-match-data
  6693. (shell-quote-argument
  6694. (convert-standard-filename file)))
  6695. t t cmd)))
  6696. (save-window-excursion
  6697. (start-process-shell-command cmd nil cmd)
  6698. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6699. ))
  6700. ((or (stringp cmd)
  6701. (eq cmd 'emacs))
  6702. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6703. (widen)
  6704. (if line (goto-line line)
  6705. (if search (org-link-search search))))
  6706. ((consp cmd)
  6707. (let ((file (convert-standard-filename file)))
  6708. (eval cmd)))
  6709. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6710. (and (org-mode-p) (eq old-mode 'org-mode)
  6711. (or (not (equal old-buffer (current-buffer)))
  6712. (not (equal old-pos (point))))
  6713. (org-mark-ring-push old-pos old-buffer))))
  6714. (defun org-default-apps ()
  6715. "Return the default applications for this operating system."
  6716. (cond
  6717. ((eq system-type 'darwin)
  6718. org-file-apps-defaults-macosx)
  6719. ((eq system-type 'windows-nt)
  6720. org-file-apps-defaults-windowsnt)
  6721. (t org-file-apps-defaults-gnu)))
  6722. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6723. (defun org-file-remote-p (file)
  6724. "Test whether FILE specifies a location on a remote system.
  6725. Return non-nil if the location is indeed remote.
  6726. For example, the filename \"/user@host:/foo\" specifies a location
  6727. on the system \"/user@host:\"."
  6728. (cond ((fboundp 'file-remote-p)
  6729. (file-remote-p file))
  6730. ((fboundp 'tramp-handle-file-remote-p)
  6731. (tramp-handle-file-remote-p file))
  6732. ((and (boundp 'ange-ftp-name-format)
  6733. (string-match (car ange-ftp-name-format) file))
  6734. t)
  6735. (t nil)))
  6736. ;;;; Refiling
  6737. (defun org-get-org-file ()
  6738. "Read a filename, with default directory `org-directory'."
  6739. (let ((default (or org-default-notes-file remember-data-file)))
  6740. (read-file-name (format "File name [%s]: " default)
  6741. (file-name-as-directory org-directory)
  6742. default)))
  6743. (defun org-notes-order-reversed-p ()
  6744. "Check if the current file should receive notes in reversed order."
  6745. (cond
  6746. ((not org-reverse-note-order) nil)
  6747. ((eq t org-reverse-note-order) t)
  6748. ((not (listp org-reverse-note-order)) nil)
  6749. (t (catch 'exit
  6750. (let ((all org-reverse-note-order)
  6751. entry)
  6752. (while (setq entry (pop all))
  6753. (if (string-match (car entry) buffer-file-name)
  6754. (throw 'exit (cdr entry))))
  6755. nil)))))
  6756. (defvar org-refile-target-table nil
  6757. "The list of refile targets, created by `org-refile'.")
  6758. (defvar org-agenda-new-buffers nil
  6759. "Buffers created to visit agenda files.")
  6760. (defun org-get-refile-targets (&optional default-buffer)
  6761. "Produce a table with refile targets."
  6762. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6763. targets txt re files f desc descre)
  6764. (with-current-buffer (or default-buffer (current-buffer))
  6765. (while (setq entry (pop entries))
  6766. (setq files (car entry) desc (cdr entry))
  6767. (cond
  6768. ((null files) (setq files (list (current-buffer))))
  6769. ((eq files 'org-agenda-files)
  6770. (setq files (org-agenda-files 'unrestricted)))
  6771. ((and (symbolp files) (fboundp files))
  6772. (setq files (funcall files)))
  6773. ((and (symbolp files) (boundp files))
  6774. (setq files (symbol-value files))))
  6775. (if (stringp files) (setq files (list files)))
  6776. (cond
  6777. ((eq (car desc) :tag)
  6778. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6779. ((eq (car desc) :todo)
  6780. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6781. ((eq (car desc) :regexp)
  6782. (setq descre (cdr desc)))
  6783. ((eq (car desc) :level)
  6784. (setq descre (concat "^\\*\\{" (number-to-string
  6785. (if org-odd-levels-only
  6786. (1- (* 2 (cdr desc)))
  6787. (cdr desc)))
  6788. "\\}[ \t]")))
  6789. ((eq (car desc) :maxlevel)
  6790. (setq descre (concat "^\\*\\{1," (number-to-string
  6791. (if org-odd-levels-only
  6792. (1- (* 2 (cdr desc)))
  6793. (cdr desc)))
  6794. "\\}[ \t]")))
  6795. (t (error "Bad refiling target description %s" desc)))
  6796. (while (setq f (pop files))
  6797. (save-excursion
  6798. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6799. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6800. (save-excursion
  6801. (save-restriction
  6802. (widen)
  6803. (goto-char (point-min))
  6804. (while (re-search-forward descre nil t)
  6805. (goto-char (point-at-bol))
  6806. (when (looking-at org-complex-heading-regexp)
  6807. (setq txt (match-string 4)
  6808. re (concat "^" (regexp-quote
  6809. (buffer-substring (match-beginning 1)
  6810. (match-end 4)))))
  6811. (if (match-end 5) (setq re (concat re "[ \t]+"
  6812. (regexp-quote
  6813. (match-string 5)))))
  6814. (setq re (concat re "[ \t]*$"))
  6815. (when org-refile-use-outline-path
  6816. (setq txt (mapconcat 'identity
  6817. (append
  6818. (if (eq org-refile-use-outline-path 'file)
  6819. (list (file-name-nondirectory
  6820. (buffer-file-name (buffer-base-buffer))))
  6821. (if (eq org-refile-use-outline-path 'full-file-path)
  6822. (list (buffer-file-name (buffer-base-buffer)))))
  6823. (org-get-outline-path)
  6824. (list txt))
  6825. "/")))
  6826. (push (list txt f re (point)) targets))
  6827. (goto-char (point-at-eol))))))))
  6828. (nreverse targets))))
  6829. (defun org-get-outline-path ()
  6830. "Return the outline path to the current entry, as a list."
  6831. (let (rtn)
  6832. (save-excursion
  6833. (while (org-up-heading-safe)
  6834. (when (looking-at org-complex-heading-regexp)
  6835. (push (org-match-string-no-properties 4) rtn)))
  6836. rtn)))
  6837. (defvar org-refile-history nil
  6838. "History for refiling operations.")
  6839. (defun org-refile (&optional goto default-buffer)
  6840. "Move the entry at point to another heading.
  6841. The list of target headings is compiled using the information in
  6842. `org-refile-targets', which see. This list is created before each use
  6843. and will therefore always be up-to-date.
  6844. At the target location, the entry is filed as a subitem of the target heading.
  6845. Depending on `org-reverse-note-order', the new subitem will either be the
  6846. first of the last subitem.
  6847. With prefix arg GOTO, the command will only visit the target location,
  6848. not actually move anything.
  6849. With a double prefix `C-c C-c', go to the location where the last refiling
  6850. operation has put the subtree."
  6851. (interactive "P")
  6852. (let* ((cbuf (current-buffer))
  6853. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6854. pos it nbuf file re level reversed)
  6855. (if (equal goto '(16))
  6856. (org-refile-goto-last-stored)
  6857. (when (setq it (org-refile-get-location
  6858. (if goto "Goto: " "Refile to: ") default-buffer))
  6859. (setq file (nth 1 it)
  6860. re (nth 2 it)
  6861. pos (nth 3 it))
  6862. (setq nbuf (or (find-buffer-visiting file)
  6863. (find-file-noselect file)))
  6864. (if goto
  6865. (progn
  6866. (switch-to-buffer nbuf)
  6867. (goto-char pos)
  6868. (org-show-context 'org-goto))
  6869. (org-copy-subtree 1 nil t)
  6870. (save-excursion
  6871. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6872. (find-file-noselect file))))
  6873. (setq reversed (org-notes-order-reversed-p))
  6874. (save-excursion
  6875. (save-restriction
  6876. (widen)
  6877. (goto-char pos)
  6878. (looking-at outline-regexp)
  6879. (setq level (org-get-valid-level (funcall outline-level) 1))
  6880. (goto-char
  6881. (if reversed
  6882. (outline-next-heading)
  6883. (or (save-excursion (outline-get-next-sibling))
  6884. (org-end-of-subtree t t)
  6885. (point-max))))
  6886. (bookmark-set "org-refile-last-stored")
  6887. (org-paste-subtree level))))
  6888. (org-cut-subtree)
  6889. (setq org-markers-to-move nil)
  6890. (message "Entry refiled to \"%s\"" (car it)))))))
  6891. (defun org-refile-goto-last-stored ()
  6892. "Go to the location where the last refile was stored."
  6893. (interactive)
  6894. (bookmark-jump "org-refile-last-stored")
  6895. (message "This is the location of the last refile"))
  6896. (defun org-refile-get-location (&optional prompt default-buffer)
  6897. "Prompt the user for a refile location, using PROMPT."
  6898. (let ((org-refile-targets org-refile-targets)
  6899. (org-refile-use-outline-path org-refile-use-outline-path))
  6900. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6901. (unless org-refile-target-table
  6902. (error "No refile targets"))
  6903. (let* ((cbuf (current-buffer))
  6904. (cfunc (if org-refile-use-outline-path
  6905. 'org-olpath-completing-read
  6906. 'completing-read))
  6907. (extra (if org-refile-use-outline-path "/" ""))
  6908. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6909. (fname (and filename (file-truename filename)))
  6910. (tbl (mapcar
  6911. (lambda (x)
  6912. (if (not (equal fname (file-truename (nth 1 x))))
  6913. (cons (concat (car x) extra " ("
  6914. (file-name-nondirectory (nth 1 x)) ")")
  6915. (cdr x))
  6916. (cons (concat (car x) extra) (cdr x))))
  6917. org-refile-target-table))
  6918. (completion-ignore-case t))
  6919. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6920. tbl)))
  6921. (defun org-olpath-completing-read (prompt collection &rest args)
  6922. "Read an outline path like a file name."
  6923. (let ((thetable collection))
  6924. (apply
  6925. 'completing-read prompt
  6926. (lambda (string predicate &optional flag)
  6927. (let (rtn r s f (l (length string)))
  6928. (cond
  6929. ((eq flag nil)
  6930. ;; try completion
  6931. (try-completion string thetable))
  6932. ((eq flag t)
  6933. ;; all-completions
  6934. (setq rtn (all-completions string thetable predicate))
  6935. (mapcar
  6936. (lambda (x)
  6937. (setq r (substring x l))
  6938. (if (string-match " ([^)]*)$" x)
  6939. (setq f (match-string 0 x))
  6940. (setq f ""))
  6941. (if (string-match "/" r)
  6942. (concat string (substring r 0 (match-end 0)) f)
  6943. x))
  6944. rtn))
  6945. ((eq flag 'lambda)
  6946. ;; exact match?
  6947. (assoc string thetable)))
  6948. ))
  6949. args)))
  6950. ;;;; Dynamic blocks
  6951. (defun org-find-dblock (name)
  6952. "Find the first dynamic block with name NAME in the buffer.
  6953. If not found, stay at current position and return nil."
  6954. (let (pos)
  6955. (save-excursion
  6956. (goto-char (point-min))
  6957. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6958. nil t)
  6959. (match-beginning 0))))
  6960. (if pos (goto-char pos))
  6961. pos))
  6962. (defconst org-dblock-start-re
  6963. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6964. "Matches the startline of a dynamic block, with parameters.")
  6965. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6966. "Matches the end of a dyhamic block.")
  6967. (defun org-create-dblock (plist)
  6968. "Create a dynamic block section, with parameters taken from PLIST.
  6969. PLIST must containe a :name entry which is used as name of the block."
  6970. (unless (bolp) (newline))
  6971. (let ((name (plist-get plist :name)))
  6972. (insert "#+BEGIN: " name)
  6973. (while plist
  6974. (if (eq (car plist) :name)
  6975. (setq plist (cddr plist))
  6976. (insert " " (prin1-to-string (pop plist)))))
  6977. (insert "\n\n#+END:\n")
  6978. (beginning-of-line -2)))
  6979. (defun org-prepare-dblock ()
  6980. "Prepare dynamic block for refresh.
  6981. This empties the block, puts the cursor at the insert position and returns
  6982. the property list including an extra property :name with the block name."
  6983. (unless (looking-at org-dblock-start-re)
  6984. (error "Not at a dynamic block"))
  6985. (let* ((begdel (1+ (match-end 0)))
  6986. (name (org-no-properties (match-string 1)))
  6987. (params (append (list :name name)
  6988. (read (concat "(" (match-string 3) ")")))))
  6989. (unless (re-search-forward org-dblock-end-re nil t)
  6990. (error "Dynamic block not terminated"))
  6991. (setq params
  6992. (append params
  6993. (list :content (buffer-substring
  6994. begdel (match-beginning 0)))))
  6995. (delete-region begdel (match-beginning 0))
  6996. (goto-char begdel)
  6997. (open-line 1)
  6998. params))
  6999. (defun org-map-dblocks (&optional command)
  7000. "Apply COMMAND to all dynamic blocks in the current buffer.
  7001. If COMMAND is not given, use `org-update-dblock'."
  7002. (let ((cmd (or command 'org-update-dblock))
  7003. pos)
  7004. (save-excursion
  7005. (goto-char (point-min))
  7006. (while (re-search-forward org-dblock-start-re nil t)
  7007. (goto-char (setq pos (match-beginning 0)))
  7008. (condition-case nil
  7009. (funcall cmd)
  7010. (error (message "Error during update of dynamic block")))
  7011. (goto-char pos)
  7012. (unless (re-search-forward org-dblock-end-re nil t)
  7013. (error "Dynamic block not terminated"))))))
  7014. (defun org-dblock-update (&optional arg)
  7015. "User command for updating dynamic blocks.
  7016. Update the dynamic block at point. With prefix ARG, update all dynamic
  7017. blocks in the buffer."
  7018. (interactive "P")
  7019. (if arg
  7020. (org-update-all-dblocks)
  7021. (or (looking-at org-dblock-start-re)
  7022. (org-beginning-of-dblock))
  7023. (org-update-dblock)))
  7024. (defun org-update-dblock ()
  7025. "Update the dynamic block at point
  7026. This means to empty the block, parse for parameters and then call
  7027. the correct writing function."
  7028. (save-window-excursion
  7029. (let* ((pos (point))
  7030. (line (org-current-line))
  7031. (params (org-prepare-dblock))
  7032. (name (plist-get params :name))
  7033. (cmd (intern (concat "org-dblock-write:" name))))
  7034. (message "Updating dynamic block `%s' at line %d..." name line)
  7035. (funcall cmd params)
  7036. (message "Updating dynamic block `%s' at line %d...done" name line)
  7037. (goto-char pos))))
  7038. (defun org-beginning-of-dblock ()
  7039. "Find the beginning of the dynamic block at point.
  7040. Error if there is no scuh block at point."
  7041. (let ((pos (point))
  7042. beg)
  7043. (end-of-line 1)
  7044. (if (and (re-search-backward org-dblock-start-re nil t)
  7045. (setq beg (match-beginning 0))
  7046. (re-search-forward org-dblock-end-re nil t)
  7047. (> (match-end 0) pos))
  7048. (goto-char beg)
  7049. (goto-char pos)
  7050. (error "Not in a dynamic block"))))
  7051. (defun org-update-all-dblocks ()
  7052. "Update all dynamic blocks in the buffer.
  7053. This function can be used in a hook."
  7054. (when (org-mode-p)
  7055. (org-map-dblocks 'org-update-dblock)))
  7056. ;;;; Completion
  7057. (defconst org-additional-option-like-keywords
  7058. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7059. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7060. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7061. (defcustom org-structure-template-alist
  7062. '(
  7063. ("s" "#+begin_src ?\n\n#+end_src"
  7064. "<src lang=\"?\">\n\n</src>")
  7065. ("e" "#+begin_example\n?\n#+end_example"
  7066. "<example>\n?\n</example>")
  7067. ("q" "#+begin_quote\n?\n#+end_quote"
  7068. "<quote>\n?\n</quote>")
  7069. ("v" "#+begin_verse\n?\n#+end_verse"
  7070. "<verse>\n?\n/verse>")
  7071. ("l" "#+begin_latex\n?\n#+end_latex"
  7072. "<literal style=\"latex\">\n?\n</literal>")
  7073. ("L" "#+latex: "
  7074. "<literal style=\"latex\">?</literal>")
  7075. ("h" "#+begin_html\n?\n#+end_html"
  7076. "<literal style=\"html\">\n?\n</literal>")
  7077. ("H" "#+html: "
  7078. "<literal style=\"html\">?</literal>")
  7079. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7080. ("A" "#+ascii: ")
  7081. ("i" "#+include %file ?"
  7082. "<include file=%file markup=\"?\">")
  7083. )
  7084. "Structure completion elements.
  7085. This is a list of abbreviation keys and values. The value gets inserted
  7086. it you type @samp{.} followed by the key and then the completion key,
  7087. usually `M-TAB'. %file will be replaced by a file name after prompting
  7088. for the file uning completion.
  7089. There are two templates for each key, the first uses the original Org syntax,
  7090. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7091. the default when the /org-mtags.el/ module has been loaded. See also the
  7092. variable `org-mtags-prefere-muse-templates'.
  7093. This is an experimental feature, it is undecided if it is going to stay in."
  7094. :group 'org-completion
  7095. :type '(repeat
  7096. (string :tag "Key")
  7097. (string :tag "Template")
  7098. (string :tag "Muse Template")))
  7099. (defun org-try-structure-completion ()
  7100. "Try to complete a structure template before point.
  7101. This looks for strings like \"<e\" on an otherwise empty line and
  7102. expands them."
  7103. (let ((l (buffer-substring (point-at-bol) (point)))
  7104. a)
  7105. (when (and (looking-at "[ \t]*$")
  7106. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7107. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7108. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7109. (match-beginning 1)) a)
  7110. t)))
  7111. (defun org-complete-expand-structure-template (start cell)
  7112. "Expand a structure template."
  7113. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7114. (rpl (nth (if musep 2 1) cell)))
  7115. (delete-region start (point))
  7116. (when (string-match "\\`#\\+" rpl)
  7117. (cond
  7118. ((bolp))
  7119. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7120. (delete-region (point-at-bol) (point)))
  7121. (t (newline))))
  7122. (setq start (point))
  7123. (if (string-match "%file" rpl)
  7124. (setq rpl (replace-match
  7125. (concat
  7126. "\""
  7127. (save-match-data
  7128. (abbreviate-file-name (read-file-name "Include file: ")))
  7129. "\"")
  7130. t t rpl)))
  7131. (insert rpl)
  7132. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7133. (defun org-complete (&optional arg)
  7134. "Perform completion on word at point.
  7135. At the beginning of a headline, this completes TODO keywords as given in
  7136. `org-todo-keywords'.
  7137. If the current word is preceded by a backslash, completes the TeX symbols
  7138. that are supported for HTML support.
  7139. If the current word is preceded by \"#+\", completes special words for
  7140. setting file options.
  7141. In the line after \"#+STARTUP:, complete valid keywords.\"
  7142. At all other locations, this simply calls the value of
  7143. `org-completion-fallback-command'."
  7144. (interactive "P")
  7145. (org-without-partial-completion
  7146. (catch 'exit
  7147. (let* ((a nil)
  7148. (end (point))
  7149. (beg1 (save-excursion
  7150. (skip-chars-backward (org-re "[:alnum:]_@"))
  7151. (point)))
  7152. (beg (save-excursion
  7153. (skip-chars-backward "a-zA-Z0-9_:$")
  7154. (point)))
  7155. (confirm (lambda (x) (stringp (car x))))
  7156. (searchhead (equal (char-before beg) ?*))
  7157. (struct
  7158. (when (and (member (char-before beg1) '(?. ?<))
  7159. (setq a (assoc (buffer-substring beg1 (point))
  7160. org-structure-template-alist)))
  7161. (org-complete-expand-structure-template (1- beg1) a)
  7162. (throw 'exit t)))
  7163. (tag (and (equal (char-before beg1) ?:)
  7164. (equal (char-after (point-at-bol)) ?*)))
  7165. (prop (and (equal (char-before beg1) ?:)
  7166. (not (equal (char-after (point-at-bol)) ?*))))
  7167. (texp (equal (char-before beg) ?\\))
  7168. (link (equal (char-before beg) ?\[))
  7169. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7170. beg)
  7171. "#+"))
  7172. (startup (string-match "^#\\+STARTUP:.*"
  7173. (buffer-substring (point-at-bol) (point))))
  7174. (completion-ignore-case opt)
  7175. (type nil)
  7176. (tbl nil)
  7177. (table (cond
  7178. (opt
  7179. (setq type :opt)
  7180. (require 'org-exp)
  7181. (append
  7182. (mapcar
  7183. (lambda (x)
  7184. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7185. (cons (match-string 2 x) (match-string 1 x)))
  7186. (org-split-string (org-get-current-options) "\n"))
  7187. (mapcar 'list org-additional-option-like-keywords)))
  7188. (startup
  7189. (setq type :startup)
  7190. org-startup-options)
  7191. (link (append org-link-abbrev-alist-local
  7192. org-link-abbrev-alist))
  7193. (texp
  7194. (setq type :tex)
  7195. org-html-entities)
  7196. ((string-match "\\`\\*+[ \t]+\\'"
  7197. (buffer-substring (point-at-bol) beg))
  7198. (setq type :todo)
  7199. (mapcar 'list org-todo-keywords-1))
  7200. (searchhead
  7201. (setq type :searchhead)
  7202. (save-excursion
  7203. (goto-char (point-min))
  7204. (while (re-search-forward org-todo-line-regexp nil t)
  7205. (push (list
  7206. (org-make-org-heading-search-string
  7207. (match-string 3) t))
  7208. tbl)))
  7209. tbl)
  7210. (tag (setq type :tag beg beg1)
  7211. (or org-tag-alist (org-get-buffer-tags)))
  7212. (prop (setq type :prop beg beg1)
  7213. (mapcar 'list (org-buffer-property-keys nil t t)))
  7214. (t (progn
  7215. (call-interactively org-completion-fallback-command)
  7216. (throw 'exit nil)))))
  7217. (pattern (buffer-substring-no-properties beg end))
  7218. (completion (try-completion pattern table confirm)))
  7219. (cond ((eq completion t)
  7220. (if (not (assoc (upcase pattern) table))
  7221. (message "Already complete")
  7222. (if (and (equal type :opt)
  7223. (not (member (car (assoc (upcase pattern) table))
  7224. org-additional-option-like-keywords)))
  7225. (insert (substring (cdr (assoc (upcase pattern) table))
  7226. (length pattern)))
  7227. (if (memq type '(:tag :prop)) (insert ":")))))
  7228. ((null completion)
  7229. (message "Can't find completion for \"%s\"" pattern)
  7230. (ding))
  7231. ((not (string= pattern completion))
  7232. (delete-region beg end)
  7233. (if (string-match " +$" completion)
  7234. (setq completion (replace-match "" t t completion)))
  7235. (insert completion)
  7236. (if (get-buffer-window "*Completions*")
  7237. (delete-window (get-buffer-window "*Completions*")))
  7238. (if (assoc completion table)
  7239. (if (eq type :todo) (insert " ")
  7240. (if (memq type '(:tag :prop)) (insert ":"))))
  7241. (if (and (equal type :opt) (assoc completion table))
  7242. (message "%s" (substitute-command-keys
  7243. "Press \\[org-complete] again to insert example settings"))))
  7244. (t
  7245. (message "Making completion list...")
  7246. (let ((list (sort (all-completions pattern table confirm)
  7247. 'string<)))
  7248. (with-output-to-temp-buffer "*Completions*"
  7249. (condition-case nil
  7250. ;; Protection needed for XEmacs and emacs 21
  7251. (display-completion-list list pattern)
  7252. (error (display-completion-list list)))))
  7253. (message "Making completion list...%s" "done")))))))
  7254. ;;;; TODO, DEADLINE, Comments
  7255. (defun org-toggle-comment ()
  7256. "Change the COMMENT state of an entry."
  7257. (interactive)
  7258. (save-excursion
  7259. (org-back-to-heading)
  7260. (let (case-fold-search)
  7261. (if (looking-at (concat outline-regexp
  7262. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7263. (replace-match "" t t nil 1)
  7264. (if (looking-at outline-regexp)
  7265. (progn
  7266. (goto-char (match-end 0))
  7267. (insert org-comment-string " ")))))))
  7268. (defvar org-last-todo-state-is-todo nil
  7269. "This is non-nil when the last TODO state change led to a TODO state.
  7270. If the last change removed the TODO tag or switched to DONE, then
  7271. this is nil.")
  7272. (defvar org-setting-tags nil) ; dynamically skiped
  7273. (defun org-parse-local-options (string var)
  7274. "Parse STRING for startup setting relevant for variable VAR."
  7275. (let ((rtn (symbol-value var))
  7276. e opts)
  7277. (save-match-data
  7278. (if (or (not string) (not (string-match "\\S-" string)))
  7279. rtn
  7280. (setq opts (delq nil (mapcar (lambda (x)
  7281. (setq e (assoc x org-startup-options))
  7282. (if (eq (nth 1 e) var) e nil))
  7283. (org-split-string string "[ \t]+"))))
  7284. (if (not opts)
  7285. rtn
  7286. (setq rtn nil)
  7287. (while (setq e (pop opts))
  7288. (if (not (nth 3 e))
  7289. (setq rtn (nth 2 e))
  7290. (if (not (listp rtn)) (setq rtn nil))
  7291. (push (nth 2 e) rtn)))
  7292. rtn)))))
  7293. (defvar org-blocker-hook nil
  7294. "Hook for functions that are allowed to block a state change.
  7295. Each function gets as its single argument a property list, see
  7296. `org-trigger-hook' for more information about this list.
  7297. If any of the functions in this hook returns nil, the state change
  7298. is blocked.")
  7299. (defvar org-trigger-hook nil
  7300. "Hook for functions that are triggered by a state change.
  7301. Each function gets as its single argument a property list with at least
  7302. the following elements:
  7303. (:type type-of-change :position pos-at-entry-start
  7304. :from old-state :to new-state)
  7305. Depending on the type, more properties may be present.
  7306. This mechanism is currently implemented for:
  7307. TODO state changes
  7308. ------------------
  7309. :type todo-state-change
  7310. :from previous state (keyword as a string), or nil
  7311. :to new state (keyword as a string), or nil")
  7312. (defun org-todo (&optional arg)
  7313. "Change the TODO state of an item.
  7314. The state of an item is given by a keyword at the start of the heading,
  7315. like
  7316. *** TODO Write paper
  7317. *** DONE Call mom
  7318. The different keywords are specified in the variable `org-todo-keywords'.
  7319. By default the available states are \"TODO\" and \"DONE\".
  7320. So for this example: when the item starts with TODO, it is changed to DONE.
  7321. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7322. DONE are present, add TODO at the beginning of the heading.
  7323. With C-u prefix arg, use completion to determine the new state.
  7324. With numeric prefix arg, switch to that state.
  7325. For calling through lisp, arg is also interpreted in the following way:
  7326. 'none -> empty state
  7327. \"\"(empty string) -> switch to empty state
  7328. 'done -> switch to DONE
  7329. 'nextset -> switch to the next set of keywords
  7330. 'previousset -> switch to the previous set of keywords
  7331. \"WAITING\" -> switch to the specified keyword, but only if it
  7332. really is a member of `org-todo-keywords'."
  7333. (interactive "P")
  7334. (save-excursion
  7335. (catch 'exit
  7336. (org-back-to-heading)
  7337. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7338. (or (looking-at (concat " +" org-todo-regexp " *"))
  7339. (looking-at " *"))
  7340. (let* ((match-data (match-data))
  7341. (startpos (point-at-bol))
  7342. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7343. (org-log-done org-log-done)
  7344. (org-log-repeat org-log-repeat)
  7345. (org-todo-log-states org-todo-log-states)
  7346. (this (match-string 1))
  7347. (hl-pos (match-beginning 0))
  7348. (head (org-get-todo-sequence-head this))
  7349. (ass (assoc head org-todo-kwd-alist))
  7350. (interpret (nth 1 ass))
  7351. (done-word (nth 3 ass))
  7352. (final-done-word (nth 4 ass))
  7353. (last-state (or this ""))
  7354. (completion-ignore-case t)
  7355. (member (member this org-todo-keywords-1))
  7356. (tail (cdr member))
  7357. (state (cond
  7358. ((and org-todo-key-trigger
  7359. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7360. (and (not arg) org-use-fast-todo-selection
  7361. (not (eq org-use-fast-todo-selection 'prefix)))))
  7362. ;; Use fast selection
  7363. (org-fast-todo-selection))
  7364. ((and (equal arg '(4))
  7365. (or (not org-use-fast-todo-selection)
  7366. (not org-todo-key-trigger)))
  7367. ;; Read a state with completion
  7368. (completing-read "State: " (mapcar (lambda(x) (list x))
  7369. org-todo-keywords-1)
  7370. nil t))
  7371. ((eq arg 'right)
  7372. (if this
  7373. (if tail (car tail) nil)
  7374. (car org-todo-keywords-1)))
  7375. ((eq arg 'left)
  7376. (if (equal member org-todo-keywords-1)
  7377. nil
  7378. (if this
  7379. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7380. org-todo-keywords-1)
  7381. (org-last org-todo-keywords-1))))
  7382. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7383. (setq arg nil))) ; hack to fall back to cycling
  7384. (arg
  7385. ;; user or caller requests a specific state
  7386. (cond
  7387. ((equal arg "") nil)
  7388. ((eq arg 'none) nil)
  7389. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7390. ((eq arg 'nextset)
  7391. (or (car (cdr (member head org-todo-heads)))
  7392. (car org-todo-heads)))
  7393. ((eq arg 'previousset)
  7394. (let ((org-todo-heads (reverse org-todo-heads)))
  7395. (or (car (cdr (member head org-todo-heads)))
  7396. (car org-todo-heads))))
  7397. ((car (member arg org-todo-keywords-1)))
  7398. ((nth (1- (prefix-numeric-value arg))
  7399. org-todo-keywords-1))))
  7400. ((null member) (or head (car org-todo-keywords-1)))
  7401. ((equal this final-done-word) nil) ;; -> make empty
  7402. ((null tail) nil) ;; -> first entry
  7403. ((eq interpret 'sequence)
  7404. (car tail))
  7405. ((memq interpret '(type priority))
  7406. (if (eq this-command last-command)
  7407. (car tail)
  7408. (if (> (length tail) 0)
  7409. (or done-word (car org-done-keywords))
  7410. nil)))
  7411. (t nil)))
  7412. (next (if state (concat " " state " ") " "))
  7413. (change-plist (list :type 'todo-state-change :from this :to state
  7414. :position startpos))
  7415. dolog now-done-p)
  7416. (when org-blocker-hook
  7417. (unless (save-excursion
  7418. (save-match-data
  7419. (run-hook-with-args-until-failure
  7420. 'org-blocker-hook change-plist)))
  7421. (if (interactive-p)
  7422. (error "TODO state change from %s to %s blocked" this state)
  7423. ;; fail silently
  7424. (message "TODO state change from %s to %s blocked" this state)
  7425. (throw 'exit nil))))
  7426. (store-match-data match-data)
  7427. (replace-match next t t)
  7428. (unless (pos-visible-in-window-p hl-pos)
  7429. (message "TODO state changed to %s" (org-trim next)))
  7430. (unless head
  7431. (setq head (org-get-todo-sequence-head state)
  7432. ass (assoc head org-todo-kwd-alist)
  7433. interpret (nth 1 ass)
  7434. done-word (nth 3 ass)
  7435. final-done-word (nth 4 ass)))
  7436. (when (memq arg '(nextset previousset))
  7437. (message "Keyword-Set %d/%d: %s"
  7438. (- (length org-todo-sets) -1
  7439. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7440. (length org-todo-sets)
  7441. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7442. (setq org-last-todo-state-is-todo
  7443. (not (member state org-done-keywords)))
  7444. (setq now-done-p (and (member state org-done-keywords)
  7445. (not (member this org-done-keywords))))
  7446. (and logging (org-local-logging logging))
  7447. (when (and (or org-todo-log-states org-log-done)
  7448. (not (memq arg '(nextset previousset))))
  7449. ;; we need to look at recording a time and note
  7450. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7451. (nth 2 (assoc this org-todo-log-states))))
  7452. (when (and state
  7453. (member state org-not-done-keywords)
  7454. (not (member this org-not-done-keywords)))
  7455. ;; This is now a todo state and was not one before
  7456. ;; If there was a CLOSED time stamp, get rid of it.
  7457. (org-add-planning-info nil nil 'closed))
  7458. (when (and now-done-p org-log-done)
  7459. ;; It is now done, and it was not done before
  7460. (org-add-planning-info 'closed (org-current-time))
  7461. (if (and (not dolog) (eq 'note org-log-done))
  7462. (org-add-log-setup 'done state 'findpos 'note)))
  7463. (when (and state dolog)
  7464. ;; This is a non-nil state, and we need to log it
  7465. (org-add-log-setup 'state state 'findpos dolog)))
  7466. ;; Fixup tag positioning
  7467. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7468. (when org-provide-todo-statistics
  7469. (org-update-parent-todo-statistics))
  7470. (run-hooks 'org-after-todo-state-change-hook)
  7471. (if (and arg (not (member state org-done-keywords)))
  7472. (setq head (org-get-todo-sequence-head state)))
  7473. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7474. ;; Do we need to trigger a repeat?
  7475. (when now-done-p (org-auto-repeat-maybe state))
  7476. ;; Fixup cursor location if close to the keyword
  7477. (if (and (outline-on-heading-p)
  7478. (not (bolp))
  7479. (save-excursion (beginning-of-line 1)
  7480. (looking-at org-todo-line-regexp))
  7481. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7482. (progn
  7483. (goto-char (or (match-end 2) (match-end 1)))
  7484. (just-one-space)))
  7485. (when org-trigger-hook
  7486. (save-excursion
  7487. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7488. (defun org-update-parent-todo-statistics ()
  7489. "Update any statistics cookie in the parent of the current headline."
  7490. (interactive)
  7491. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7492. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7493. (catch 'exit
  7494. (save-excursion
  7495. (setq level (org-up-heading-safe))
  7496. (unless (and level
  7497. (re-search-forward box-re (point-at-eol) t))
  7498. (throw 'exit nil))
  7499. (setq is-percent (match-end 2))
  7500. (save-match-data
  7501. (unless (outline-next-heading) (throw 'exit nil))
  7502. (while (looking-at org-todo-line-regexp)
  7503. (setq kwd (match-string 2))
  7504. (and kwd (setq cnt-all (1+ cnt-all)))
  7505. (and (member kwd org-done-keywords)
  7506. (setq cnt-done (1+ cnt-done)))
  7507. (condition-case nil
  7508. (outline-forward-same-level 1)
  7509. (error (end-of-line 1)))))
  7510. (replace-match
  7511. (if is-percent
  7512. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7513. (format "[%d/%d]" cnt-done cnt-all)))
  7514. (run-hook-with-args 'org-after-todo-statistics-hook
  7515. cnt-done (- cnt-all cnt-done))))))
  7516. (defvar org-after-todo-statistics-hook nil
  7517. "Hook that is called after a TODO statistics cookie has been updated.
  7518. Each function is called with two arguments: the number of not-done entries
  7519. and the number of done entries.
  7520. For example, the following function, when added to this hook, will switch
  7521. an entry to DONE when all children are done, and back to TODO when new
  7522. entries are set to a TODO status. Note that this hook is only called
  7523. when there is a statistics cookie in the headline!
  7524. (defun org-summary-todo (n-done n-not-done)
  7525. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7526. (let (org-log-done org-log-states) ; turn off logging
  7527. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7528. ")
  7529. (defun org-local-logging (value)
  7530. "Get logging settings from a property VALUE."
  7531. (let* (words w a)
  7532. ;; directly set the variables, they are already local.
  7533. (setq org-log-done nil
  7534. org-log-repeat nil
  7535. org-todo-log-states nil)
  7536. (setq words (org-split-string value))
  7537. (while (setq w (pop words))
  7538. (cond
  7539. ((setq a (assoc w org-startup-options))
  7540. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7541. (set (nth 1 a) (nth 2 a))))
  7542. ((setq a (org-extract-log-state-settings w))
  7543. (and (member (car a) org-todo-keywords-1)
  7544. (push a org-todo-log-states)))))))
  7545. (defun org-get-todo-sequence-head (kwd)
  7546. "Return the head of the TODO sequence to which KWD belongs.
  7547. If KWD is not set, check if there is a text property remembering the
  7548. right sequence."
  7549. (let (p)
  7550. (cond
  7551. ((not kwd)
  7552. (or (get-text-property (point-at-bol) 'org-todo-head)
  7553. (progn
  7554. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7555. nil (point-at-eol)))
  7556. (get-text-property p 'org-todo-head))))
  7557. ((not (member kwd org-todo-keywords-1))
  7558. (car org-todo-keywords-1))
  7559. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7560. (defun org-fast-todo-selection ()
  7561. "Fast TODO keyword selection with single keys.
  7562. Returns the new TODO keyword, or nil if no state change should occur."
  7563. (let* ((fulltable org-todo-key-alist)
  7564. (done-keywords org-done-keywords) ;; needed for the faces.
  7565. (maxlen (apply 'max (mapcar
  7566. (lambda (x)
  7567. (if (stringp (car x)) (string-width (car x)) 0))
  7568. fulltable)))
  7569. (expert nil)
  7570. (fwidth (+ maxlen 3 1 3))
  7571. (ncol (/ (- (window-width) 4) fwidth))
  7572. tg cnt e c tbl
  7573. groups ingroup)
  7574. (save-window-excursion
  7575. (if expert
  7576. (set-buffer (get-buffer-create " *Org todo*"))
  7577. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7578. (erase-buffer)
  7579. (org-set-local 'org-done-keywords done-keywords)
  7580. (setq tbl fulltable cnt 0)
  7581. (while (setq e (pop tbl))
  7582. (cond
  7583. ((equal e '(:startgroup))
  7584. (push '() groups) (setq ingroup t)
  7585. (when (not (= cnt 0))
  7586. (setq cnt 0)
  7587. (insert "\n"))
  7588. (insert "{ "))
  7589. ((equal e '(:endgroup))
  7590. (setq ingroup nil cnt 0)
  7591. (insert "}\n"))
  7592. (t
  7593. (setq tg (car e) c (cdr e))
  7594. (if ingroup (push tg (car groups)))
  7595. (setq tg (org-add-props tg nil 'face
  7596. (org-get-todo-face tg)))
  7597. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7598. (insert "[" c "] " tg (make-string
  7599. (- fwidth 4 (length tg)) ?\ ))
  7600. (when (= (setq cnt (1+ cnt)) ncol)
  7601. (insert "\n")
  7602. (if ingroup (insert " "))
  7603. (setq cnt 0)))))
  7604. (insert "\n")
  7605. (goto-char (point-min))
  7606. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7607. (fit-window-to-buffer))
  7608. (message "[a-z..]:Set [SPC]:clear")
  7609. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7610. (cond
  7611. ((or (= c ?\C-g)
  7612. (and (= c ?q) (not (rassoc c fulltable))))
  7613. (setq quit-flag t))
  7614. ((= c ?\ ) nil)
  7615. ((setq e (rassoc c fulltable) tg (car e))
  7616. tg)
  7617. (t (setq quit-flag t))))))
  7618. (defun org-entry-is-todo-p ()
  7619. (member (org-get-todo-state) org-not-done-keywords))
  7620. (defun org-entry-is-done-p ()
  7621. (member (org-get-todo-state) org-done-keywords))
  7622. (defun org-get-todo-state ()
  7623. (save-excursion
  7624. (org-back-to-heading t)
  7625. (and (looking-at org-todo-line-regexp)
  7626. (match-end 2)
  7627. (match-string 2))))
  7628. (defun org-at-date-range-p (&optional inactive-ok)
  7629. "Is the cursor inside a date range?"
  7630. (interactive)
  7631. (save-excursion
  7632. (catch 'exit
  7633. (let ((pos (point)))
  7634. (skip-chars-backward "^[<\r\n")
  7635. (skip-chars-backward "<[")
  7636. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7637. (>= (match-end 0) pos)
  7638. (throw 'exit t))
  7639. (skip-chars-backward "^<[\r\n")
  7640. (skip-chars-backward "<[")
  7641. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7642. (>= (match-end 0) pos)
  7643. (throw 'exit t)))
  7644. nil)))
  7645. (defun org-get-repeat ()
  7646. "Check if tere is a deadline/schedule with repeater in this entry."
  7647. (save-match-data
  7648. (save-excursion
  7649. (org-back-to-heading t)
  7650. (if (re-search-forward
  7651. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7652. (match-string 1)))))
  7653. (defvar org-last-changed-timestamp)
  7654. (defvar org-log-post-message)
  7655. (defvar org-log-note-purpose)
  7656. (defvar org-log-note-how)
  7657. (defun org-auto-repeat-maybe (done-word)
  7658. "Check if the current headline contains a repeated deadline/schedule.
  7659. If yes, set TODO state back to what it was and change the base date
  7660. of repeating deadline/scheduled time stamps to new date.
  7661. This function is run automatically after each state change to a DONE state."
  7662. ;; last-state is dynamically scoped into this function
  7663. (let* ((repeat (org-get-repeat))
  7664. (aa (assoc last-state org-todo-kwd-alist))
  7665. (interpret (nth 1 aa))
  7666. (head (nth 2 aa))
  7667. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7668. (msg "Entry repeats: ")
  7669. (org-log-done nil)
  7670. (org-todo-log-states nil)
  7671. (nshiftmax 10) (nshift 0)
  7672. re type n what ts mb0 time)
  7673. (when repeat
  7674. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7675. (org-todo (if (eq interpret 'type) last-state head))
  7676. (when org-log-repeat
  7677. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7678. (memq 'org-add-log-note post-command-hook))
  7679. ;; OK, we are already setup for some record
  7680. (if (eq org-log-repeat 'note)
  7681. ;; make sure we take a note, not only a time stamp
  7682. (setq org-log-note-how 'note))
  7683. ;; Set up for taking a record
  7684. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7685. 'findpos org-log-repeat)))
  7686. (org-back-to-heading t)
  7687. (org-add-planning-info nil nil 'closed)
  7688. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7689. org-deadline-time-regexp "\\)\\|\\("
  7690. org-ts-regexp "\\)"))
  7691. (while (re-search-forward
  7692. re (save-excursion (outline-next-heading) (point)) t)
  7693. (setq type (if (match-end 1) org-scheduled-string
  7694. (if (match-end 3) org-deadline-string "Plain:"))
  7695. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7696. mb0 (match-beginning 0))
  7697. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7698. (setq n (string-to-number (match-string 2 ts))
  7699. what (match-string 3 ts))
  7700. (if (equal what "w") (setq n (* n 7) what "d"))
  7701. ;; Preparation, see if we need to modify the start date for the change
  7702. (when (match-end 1)
  7703. (setq time (save-match-data (org-time-string-to-time ts)))
  7704. (cond
  7705. ((equal (match-string 1 ts) ".")
  7706. ;; Shift starting date to today
  7707. (org-timestamp-change
  7708. (- (time-to-days (current-time)) (time-to-days time))
  7709. 'day))
  7710. ((equal (match-string 1 ts) "+")
  7711. (while (or (= nshift 0)
  7712. (<= (time-to-days time) (time-to-days (current-time))))
  7713. (when (= (incf nshift) nshiftmax)
  7714. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7715. (error "Abort")))
  7716. (org-timestamp-change n (cdr (assoc what whata)))
  7717. (org-at-timestamp-p t)
  7718. (setq ts (match-string 1))
  7719. (setq time (save-match-data (org-time-string-to-time ts))))
  7720. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7721. ;; rematch, so that we have everything in place for the real shift
  7722. (org-at-timestamp-p t)
  7723. (setq ts (match-string 1))
  7724. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7725. (org-timestamp-change n (cdr (assoc what whata)))
  7726. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7727. (setq org-log-post-message msg)
  7728. (message "%s" msg))))
  7729. (defun org-show-todo-tree (arg)
  7730. "Make a compact tree which shows all headlines marked with TODO.
  7731. The tree will show the lines where the regexp matches, and all higher
  7732. headlines above the match.
  7733. With a \\[universal-argument] prefix, also show the DONE entries.
  7734. With a numeric prefix N, construct a sparse tree for the Nth element
  7735. of `org-todo-keywords-1'."
  7736. (interactive "P")
  7737. (let ((case-fold-search nil)
  7738. (kwd-re
  7739. (cond ((null arg) org-not-done-regexp)
  7740. ((equal arg '(4))
  7741. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7742. (mapcar 'list org-todo-keywords-1))))
  7743. (concat "\\("
  7744. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7745. "\\)\\>")))
  7746. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7747. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7748. org-todo-keywords-1)))
  7749. (t (error "Invalid prefix argument: %s" arg)))))
  7750. (message "%d TODO entries found"
  7751. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7752. (defun org-deadline (&optional remove)
  7753. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7754. With argument REMOVE, remove any deadline from the item."
  7755. (interactive "P")
  7756. (if remove
  7757. (progn
  7758. (org-remove-timestamp-with-keyword org-deadline-string)
  7759. (message "Item no longer has a deadline."))
  7760. (org-add-planning-info 'deadline nil 'closed)))
  7761. (defun org-schedule (&optional remove)
  7762. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7763. With argument REMOVE, remove any scheduling date from the item."
  7764. (interactive "P")
  7765. (if remove
  7766. (progn
  7767. (org-remove-timestamp-with-keyword org-scheduled-string)
  7768. (message "Item is no longer scheduled."))
  7769. (org-add-planning-info 'scheduled nil 'closed)))
  7770. (defun org-remove-timestamp-with-keyword (keyword)
  7771. "Remove all time stamps with KEYWORD in the current entry."
  7772. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7773. beg)
  7774. (save-excursion
  7775. (org-back-to-heading t)
  7776. (setq beg (point))
  7777. (org-end-of-subtree t t)
  7778. (while (re-search-backward re beg t)
  7779. (replace-match "")
  7780. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7781. (equal (char-before) ?\ ))
  7782. (backward-delete-char 1)
  7783. (if (string-match "^[ \t]*$" (buffer-substring
  7784. (point-at-bol) (point-at-eol)))
  7785. (delete-region (point-at-bol)
  7786. (min (point-max) (1+ (point-at-eol))))))))))
  7787. (defun org-add-planning-info (what &optional time &rest remove)
  7788. "Insert new timestamp with keyword in the line directly after the headline.
  7789. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7790. If non is given, the user is prompted for a date.
  7791. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7792. be removed."
  7793. (interactive)
  7794. (let (org-time-was-given org-end-time-was-given ts
  7795. end default-time default-input)
  7796. (when (and (not time) (memq what '(scheduled deadline)))
  7797. ;; Try to get a default date/time from existing timestamp
  7798. (save-excursion
  7799. (org-back-to-heading t)
  7800. (setq end (save-excursion (outline-next-heading) (point)))
  7801. (when (re-search-forward (if (eq what 'scheduled)
  7802. org-scheduled-time-regexp
  7803. org-deadline-time-regexp)
  7804. end t)
  7805. (setq ts (match-string 1)
  7806. default-time
  7807. (apply 'encode-time (org-parse-time-string ts))
  7808. default-input (and ts (org-get-compact-tod ts))))))
  7809. (when what
  7810. ;; If necessary, get the time from the user
  7811. (setq time (or time (org-read-date nil 'to-time nil nil
  7812. default-time default-input))))
  7813. (when (and org-insert-labeled-timestamps-at-point
  7814. (member what '(scheduled deadline)))
  7815. (insert
  7816. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7817. (org-insert-time-stamp time org-time-was-given
  7818. nil nil nil (list org-end-time-was-given))
  7819. (setq what nil))
  7820. (save-excursion
  7821. (save-restriction
  7822. (let (col list elt ts buffer-invisibility-spec)
  7823. (org-back-to-heading t)
  7824. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7825. (goto-char (match-end 1))
  7826. (setq col (current-column))
  7827. (goto-char (match-end 0))
  7828. (if (eobp) (insert "\n") (forward-char 1))
  7829. (if (and (not (looking-at outline-regexp))
  7830. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7831. "[^\r\n]*"))
  7832. (not (equal (match-string 1) org-clock-string)))
  7833. (narrow-to-region (match-beginning 0) (match-end 0))
  7834. (insert-before-markers "\n")
  7835. (backward-char 1)
  7836. (narrow-to-region (point) (point))
  7837. (and org-adapt-indentation (org-indent-to-column col)))
  7838. ;; Check if we have to remove something.
  7839. (setq list (cons what remove))
  7840. (while list
  7841. (setq elt (pop list))
  7842. (goto-char (point-min))
  7843. (when (or (and (eq elt 'scheduled)
  7844. (re-search-forward org-scheduled-time-regexp nil t))
  7845. (and (eq elt 'deadline)
  7846. (re-search-forward org-deadline-time-regexp nil t))
  7847. (and (eq elt 'closed)
  7848. (re-search-forward org-closed-time-regexp nil t)))
  7849. (replace-match "")
  7850. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7851. (if (looking-at " +") (replace-match ""))))
  7852. (goto-char (point-max))
  7853. (when what
  7854. (insert
  7855. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7856. (cond ((eq what 'scheduled) org-scheduled-string)
  7857. ((eq what 'deadline) org-deadline-string)
  7858. ((eq what 'closed) org-closed-string))
  7859. " ")
  7860. (setq ts (org-insert-time-stamp
  7861. time
  7862. (or org-time-was-given
  7863. (and (eq what 'closed) org-log-done-with-time))
  7864. (eq what 'closed)
  7865. nil nil (list org-end-time-was-given)))
  7866. (end-of-line 1))
  7867. (goto-char (point-min))
  7868. (widen)
  7869. (if (and (looking-at "[ \t]+\n")
  7870. (equal (char-before) ?\n))
  7871. (delete-region (1- (point)) (point-at-eol)))
  7872. ts)))))
  7873. (defvar org-log-note-marker (make-marker))
  7874. (defvar org-log-note-purpose nil)
  7875. (defvar org-log-note-state nil)
  7876. (defvar org-log-note-how nil)
  7877. (defvar org-log-note-window-configuration nil)
  7878. (defvar org-log-note-return-to (make-marker))
  7879. (defvar org-log-post-message nil
  7880. "Message to be displayed after a log note has been stored.
  7881. The auto-repeater uses this.")
  7882. (defun org-add-note ()
  7883. "Add a note to the current entry.
  7884. This is done in the same way as adding a state change note."
  7885. (interactive)
  7886. (org-add-log-setup 'note nil t nil))
  7887. (defun org-add-log-setup (&optional purpose state findpos how)
  7888. "Set up the post command hook to take a note.
  7889. If this is about to TODO state change, the new state is expected in STATE.
  7890. When FINDPOS is non-nil, find the correct position for the note in
  7891. the current entry. If not, assume that it can be inserted at point."
  7892. (save-excursion
  7893. (when findpos
  7894. (org-back-to-heading t)
  7895. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7896. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7897. "[^\r\n]*\\)?"))
  7898. (goto-char (match-end 0))
  7899. (unless org-log-states-order-reversed
  7900. (and (= (char-after) ?\n) (forward-char 1))
  7901. (org-skip-over-state-notes)
  7902. (skip-chars-backward " \t\n\r")))
  7903. (move-marker org-log-note-marker (point))
  7904. (setq org-log-note-purpose purpose
  7905. org-log-note-state state
  7906. org-log-note-how how)
  7907. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7908. (defun org-skip-over-state-notes ()
  7909. "Skip past the list of State notes in an entry."
  7910. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7911. (while (looking-at "[ \t]*- State")
  7912. (condition-case nil
  7913. (org-next-item)
  7914. (error (org-end-of-item)))))
  7915. (defun org-add-log-note (&optional purpose)
  7916. "Pop up a window for taking a note, and add this note later at point."
  7917. (remove-hook 'post-command-hook 'org-add-log-note)
  7918. (setq org-log-note-window-configuration (current-window-configuration))
  7919. (delete-other-windows)
  7920. (move-marker org-log-note-return-to (point))
  7921. (switch-to-buffer (marker-buffer org-log-note-marker))
  7922. (goto-char org-log-note-marker)
  7923. (org-switch-to-buffer-other-window "*Org Note*")
  7924. (erase-buffer)
  7925. (if (memq org-log-note-how '(time state))
  7926. (org-store-log-note)
  7927. (let ((org-inhibit-startup t)) (org-mode))
  7928. (insert (format "# Insert note for %s.
  7929. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7930. (cond
  7931. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7932. ((eq org-log-note-purpose 'done) "closed todo item")
  7933. ((eq org-log-note-purpose 'state)
  7934. (format "state change to \"%s\"" org-log-note-state))
  7935. ((eq org-log-note-purpose 'note)
  7936. "this entry")
  7937. (t (error "This should not happen")))))
  7938. (org-set-local 'org-finish-function 'org-store-log-note)))
  7939. (defvar org-note-abort nil) ; dynamically scoped
  7940. (defun org-store-log-note ()
  7941. "Finish taking a log note, and insert it to where it belongs."
  7942. (let ((txt (buffer-string))
  7943. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7944. lines ind)
  7945. (kill-buffer (current-buffer))
  7946. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7947. (setq txt (replace-match "" t t txt)))
  7948. (if (string-match "\\s-+\\'" txt)
  7949. (setq txt (replace-match "" t t txt)))
  7950. (setq lines (org-split-string txt "\n"))
  7951. (when (and note (string-match "\\S-" note))
  7952. (setq note
  7953. (org-replace-escapes
  7954. note
  7955. (list (cons "%u" (user-login-name))
  7956. (cons "%U" user-full-name)
  7957. (cons "%t" (format-time-string
  7958. (org-time-stamp-format 'long 'inactive)
  7959. (current-time)))
  7960. (cons "%s" (if org-log-note-state
  7961. (concat "\"" org-log-note-state "\"")
  7962. "")))))
  7963. (if lines (setq note (concat note " \\\\")))
  7964. (push note lines))
  7965. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7966. (when lines
  7967. (save-excursion
  7968. (set-buffer (marker-buffer org-log-note-marker))
  7969. (save-excursion
  7970. (goto-char org-log-note-marker)
  7971. (move-marker org-log-note-marker nil)
  7972. (end-of-line 1)
  7973. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7974. (indent-relative nil)
  7975. (insert "- " (pop lines))
  7976. (org-indent-line-function)
  7977. (beginning-of-line 1)
  7978. (looking-at "[ \t]*")
  7979. (setq ind (concat (match-string 0) " "))
  7980. (end-of-line 1)
  7981. (while lines (insert "\n" ind (pop lines)))))))
  7982. (set-window-configuration org-log-note-window-configuration)
  7983. (with-current-buffer (marker-buffer org-log-note-return-to)
  7984. (goto-char org-log-note-return-to))
  7985. (move-marker org-log-note-return-to nil)
  7986. (and org-log-post-message (message "%s" org-log-post-message)))
  7987. (defun org-sparse-tree (&optional arg)
  7988. "Create a sparse tree, prompt for the details.
  7989. This command can create sparse trees. You first need to select the type
  7990. of match used to create the tree:
  7991. t Show entries with a specific TODO keyword.
  7992. T Show entries selected by a tags match.
  7993. p Enter a property name and its value (both with completion on existing
  7994. names/values) and show entries with that property.
  7995. r Show entries matching a regular expression
  7996. d Show deadlines due within `org-deadline-warning-days'."
  7997. (interactive "P")
  7998. (let (ans kwd value)
  7999. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8000. (setq ans (read-char-exclusive))
  8001. (cond
  8002. ((equal ans ?d)
  8003. (call-interactively 'org-check-deadlines))
  8004. ((equal ans ?b)
  8005. (call-interactively 'org-check-before-date))
  8006. ((equal ans ?t)
  8007. (org-show-todo-tree '(4)))
  8008. ((equal ans ?T)
  8009. (call-interactively 'org-tags-sparse-tree))
  8010. ((member ans '(?p ?P))
  8011. (setq kwd (completing-read "Property: "
  8012. (mapcar 'list (org-buffer-property-keys))))
  8013. (setq value (completing-read "Value: "
  8014. (mapcar 'list (org-property-values kwd))))
  8015. (unless (string-match "\\`{.*}\\'" value)
  8016. (setq value (concat "\"" value "\"")))
  8017. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8018. ((member ans '(?r ?R ?/))
  8019. (call-interactively 'org-occur))
  8020. (t (error "No such sparse tree command \"%c\"" ans)))))
  8021. (defvar org-occur-highlights nil
  8022. "List of overlays used for occur matches.")
  8023. (make-variable-buffer-local 'org-occur-highlights)
  8024. (defvar org-occur-parameters nil
  8025. "Parameters of the active org-occur calls.
  8026. This is a list, each call to org-occur pushes as cons cell,
  8027. containing the regular expression and the callback, onto the list.
  8028. The list can contain several entries if `org-occur' has been called
  8029. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8030. will only contain one set of parameters. When the highlights are
  8031. removed (for example with `C-c C-c', or with the next edit (depending
  8032. on `org-remove-highlights-with-change'), this variable is emptied
  8033. as well.")
  8034. (make-variable-buffer-local 'org-occur-parameters)
  8035. (defun org-occur (regexp &optional keep-previous callback)
  8036. "Make a compact tree which shows all matches of REGEXP.
  8037. The tree will show the lines where the regexp matches, and all higher
  8038. headlines above the match. It will also show the heading after the match,
  8039. to make sure editing the matching entry is easy.
  8040. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8041. call to `org-occur' will be kept, to allow stacking of calls to this
  8042. command.
  8043. If CALLBACK is non-nil, it is a function which is called to confirm
  8044. that the match should indeed be shown."
  8045. (interactive "sRegexp: \nP")
  8046. (unless keep-previous
  8047. (org-remove-occur-highlights nil nil t))
  8048. (push (cons regexp callback) org-occur-parameters)
  8049. (let ((cnt 0))
  8050. (save-excursion
  8051. (goto-char (point-min))
  8052. (if (or (not keep-previous) ; do not want to keep
  8053. (not org-occur-highlights)) ; no previous matches
  8054. ;; hide everything
  8055. (org-overview))
  8056. (while (re-search-forward regexp nil t)
  8057. (when (or (not callback)
  8058. (save-match-data (funcall callback)))
  8059. (setq cnt (1+ cnt))
  8060. (when org-highlight-sparse-tree-matches
  8061. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8062. (org-show-context 'occur-tree))))
  8063. (when org-remove-highlights-with-change
  8064. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8065. nil 'local))
  8066. (unless org-sparse-tree-open-archived-trees
  8067. (org-hide-archived-subtrees (point-min) (point-max)))
  8068. (run-hooks 'org-occur-hook)
  8069. (if (interactive-p)
  8070. (message "%d match(es) for regexp %s" cnt regexp))
  8071. cnt))
  8072. (defun org-show-context (&optional key)
  8073. "Make sure point and context and visible.
  8074. How much context is shown depends upon the variables
  8075. `org-show-hierarchy-above', `org-show-following-heading'. and
  8076. `org-show-siblings'."
  8077. (let ((heading-p (org-on-heading-p t))
  8078. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8079. (following-p (org-get-alist-option org-show-following-heading key))
  8080. (entry-p (org-get-alist-option org-show-entry-below key))
  8081. (siblings-p (org-get-alist-option org-show-siblings key)))
  8082. (catch 'exit
  8083. ;; Show heading or entry text
  8084. (if (and heading-p (not entry-p))
  8085. (org-flag-heading nil) ; only show the heading
  8086. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8087. (org-show-hidden-entry))) ; show entire entry
  8088. (when following-p
  8089. ;; Show next sibling, or heading below text
  8090. (save-excursion
  8091. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8092. (org-flag-heading nil))))
  8093. (when siblings-p (org-show-siblings))
  8094. (when hierarchy-p
  8095. ;; show all higher headings, possibly with siblings
  8096. (save-excursion
  8097. (while (and (condition-case nil
  8098. (progn (org-up-heading-all 1) t)
  8099. (error nil))
  8100. (not (bobp)))
  8101. (org-flag-heading nil)
  8102. (when siblings-p (org-show-siblings))))))))
  8103. (defun org-reveal (&optional siblings)
  8104. "Show current entry, hierarchy above it, and the following headline.
  8105. This can be used to show a consistent set of context around locations
  8106. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8107. not t for the search context.
  8108. With optional argument SIBLINGS, on each level of the hierarchy all
  8109. siblings are shown. This repairs the tree structure to what it would
  8110. look like when opened with hierarchical calls to `org-cycle'."
  8111. (interactive "P")
  8112. (let ((org-show-hierarchy-above t)
  8113. (org-show-following-heading t)
  8114. (org-show-siblings (if siblings t org-show-siblings)))
  8115. (org-show-context nil)))
  8116. (defun org-highlight-new-match (beg end)
  8117. "Highlight from BEG to END and mark the highlight is an occur headline."
  8118. (let ((ov (org-make-overlay beg end)))
  8119. (org-overlay-put ov 'face 'secondary-selection)
  8120. (push ov org-occur-highlights)))
  8121. (defun org-remove-occur-highlights (&optional beg end noremove)
  8122. "Remove the occur highlights from the buffer.
  8123. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8124. from the `before-change-functions' in the current buffer."
  8125. (interactive)
  8126. (unless org-inhibit-highlight-removal
  8127. (mapc 'org-delete-overlay org-occur-highlights)
  8128. (setq org-occur-highlights nil)
  8129. (setq org-occur-parameters nil)
  8130. (unless noremove
  8131. (remove-hook 'before-change-functions
  8132. 'org-remove-occur-highlights 'local))))
  8133. ;;;; Priorities
  8134. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8135. "Regular expression matching the priority indicator.")
  8136. (defvar org-remove-priority-next-time nil)
  8137. (defun org-priority-up ()
  8138. "Increase the priority of the current item."
  8139. (interactive)
  8140. (org-priority 'up))
  8141. (defun org-priority-down ()
  8142. "Decrease the priority of the current item."
  8143. (interactive)
  8144. (org-priority 'down))
  8145. (defun org-priority (&optional action)
  8146. "Change the priority of an item by ARG.
  8147. ACTION can be `set', `up', `down', or a character."
  8148. (interactive)
  8149. (setq action (or action 'set))
  8150. (let (current new news have remove)
  8151. (save-excursion
  8152. (org-back-to-heading)
  8153. (if (looking-at org-priority-regexp)
  8154. (setq current (string-to-char (match-string 2))
  8155. have t)
  8156. (setq current org-default-priority))
  8157. (cond
  8158. ((or (eq action 'set)
  8159. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8160. (if (not (eq action 'set))
  8161. (setq new action)
  8162. (message "Priority %c-%c, SPC to remove: "
  8163. org-highest-priority org-lowest-priority)
  8164. (setq new (read-char-exclusive)))
  8165. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8166. (= (upcase org-lowest-priority) org-lowest-priority))
  8167. (setq new (upcase new)))
  8168. (cond ((equal new ?\ ) (setq remove t))
  8169. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8170. (error "Priority must be between `%c' and `%c'"
  8171. org-highest-priority org-lowest-priority))))
  8172. ((eq action 'up)
  8173. (if (and (not have) (eq last-command this-command))
  8174. (setq new org-lowest-priority)
  8175. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8176. org-default-priority (1- current)))))
  8177. ((eq action 'down)
  8178. (if (and (not have) (eq last-command this-command))
  8179. (setq new org-highest-priority)
  8180. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8181. org-default-priority (1+ current)))))
  8182. (t (error "Invalid action")))
  8183. (if (or (< (upcase new) org-highest-priority)
  8184. (> (upcase new) org-lowest-priority))
  8185. (setq remove t))
  8186. (setq news (format "%c" new))
  8187. (if have
  8188. (if remove
  8189. (replace-match "" t t nil 1)
  8190. (replace-match news t t nil 2))
  8191. (if remove
  8192. (error "No priority cookie found in line")
  8193. (looking-at org-todo-line-regexp)
  8194. (if (match-end 2)
  8195. (progn
  8196. (goto-char (match-end 2))
  8197. (insert " [#" news "]"))
  8198. (goto-char (match-beginning 3))
  8199. (insert "[#" news "] ")))))
  8200. (org-preserve-lc (org-set-tags nil 'align))
  8201. (if remove
  8202. (message "Priority removed")
  8203. (message "Priority of current item set to %s" news))))
  8204. (defun org-get-priority (s)
  8205. "Find priority cookie and return priority."
  8206. (save-match-data
  8207. (if (not (string-match org-priority-regexp s))
  8208. (* 1000 (- org-lowest-priority org-default-priority))
  8209. (* 1000 (- org-lowest-priority
  8210. (string-to-char (match-string 2 s)))))))
  8211. ;;;; Tags
  8212. (defun org-scan-tags (action matcher &optional todo-only)
  8213. "Scan headline tags with inheritance and produce output ACTION.
  8214. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8215. evaluated, testing if a given set of tags qualifies a headline for
  8216. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8217. are included in the output."
  8218. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8219. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8220. (org-re
  8221. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8222. (props (list 'face nil
  8223. 'done-face 'org-done
  8224. 'undone-face nil
  8225. 'mouse-face 'highlight
  8226. 'org-not-done-regexp org-not-done-regexp
  8227. 'org-todo-regexp org-todo-regexp
  8228. 'keymap org-agenda-keymap
  8229. 'help-echo
  8230. (format "mouse-2 or RET jump to org file %s"
  8231. (abbreviate-file-name
  8232. (or (buffer-file-name (buffer-base-buffer))
  8233. (buffer-name (buffer-base-buffer)))))))
  8234. (case-fold-search nil)
  8235. lspos tags tags-list
  8236. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8237. (llast 0) rtn level category i txt
  8238. todo marker entry priority)
  8239. (save-excursion
  8240. (goto-char (point-min))
  8241. (when (eq action 'sparse-tree)
  8242. (org-overview)
  8243. (org-remove-occur-highlights))
  8244. (while (re-search-forward re nil t)
  8245. (catch :skip
  8246. (setq todo (if (match-end 1) (match-string 2))
  8247. tags (if (match-end 4) (match-string 4)))
  8248. (goto-char (setq lspos (1+ (match-beginning 0))))
  8249. (setq level (org-reduced-level (funcall outline-level))
  8250. category (org-get-category))
  8251. (setq i llast llast level)
  8252. ;; remove tag lists from same and sublevels
  8253. (while (>= i level)
  8254. (when (setq entry (assoc i tags-alist))
  8255. (setq tags-alist (delete entry tags-alist)))
  8256. (setq i (1- i)))
  8257. ;; add the next tags
  8258. (when tags
  8259. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8260. tags-alist
  8261. (cons (cons level tags) tags-alist)))
  8262. ;; compile tags for current headline
  8263. (setq tags-list
  8264. (if org-use-tag-inheritance
  8265. (apply 'append (mapcar 'cdr tags-alist))
  8266. tags))
  8267. (when (and tags org-use-tag-inheritance
  8268. (not (eq t org-use-tag-inheritance)))
  8269. ;; selective inheritance, remove uninherited ones
  8270. (setcdr (car tags-alist)
  8271. (org-remove-uniherited-tags (cdar tags-alist))))
  8272. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8273. (eval matcher)
  8274. (or (not org-agenda-skip-archived-trees)
  8275. (not (member org-archive-tag tags-list))))
  8276. (and (eq action 'agenda) (org-agenda-skip))
  8277. ;; list this headline
  8278. (if (eq action 'sparse-tree)
  8279. (progn
  8280. (and org-highlight-sparse-tree-matches
  8281. (org-get-heading) (match-end 0)
  8282. (org-highlight-new-match
  8283. (match-beginning 0) (match-beginning 1)))
  8284. (org-show-context 'tags-tree))
  8285. (setq txt (org-format-agenda-item
  8286. ""
  8287. (concat
  8288. (if org-tags-match-list-sublevels
  8289. (make-string (1- level) ?.) "")
  8290. (org-get-heading))
  8291. category tags-list)
  8292. priority (org-get-priority txt))
  8293. (goto-char lspos)
  8294. (setq marker (org-agenda-new-marker))
  8295. (org-add-props txt props
  8296. 'org-marker marker 'org-hd-marker marker 'org-category category
  8297. 'priority priority 'type "tagsmatch")
  8298. (push txt rtn))
  8299. ;; if we are to skip sublevels, jump to end of subtree
  8300. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8301. (when (and (eq action 'sparse-tree)
  8302. (not org-sparse-tree-open-archived-trees))
  8303. (org-hide-archived-subtrees (point-min) (point-max)))
  8304. (nreverse rtn)))
  8305. (defun org-remove-uniherited-tags (tags)
  8306. "Remove all tags that are not inherited from the list TAGS."
  8307. (cond
  8308. ((eq org-use-tag-inheritance t) tags)
  8309. ((not org-use-tag-inheritance) nil)
  8310. ((stringp org-use-tag-inheritance)
  8311. (delq nil (mapcar
  8312. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8313. tags)))
  8314. ((listp org-use-tag-inheritance)
  8315. (org-delete-all org-use-tag-inheritance tags))))
  8316. (defvar todo-only) ;; dynamically scoped
  8317. (defun org-tags-sparse-tree (&optional todo-only match)
  8318. "Create a sparse tree according to tags string MATCH.
  8319. MATCH can contain positive and negative selection of tags, like
  8320. \"+WORK+URGENT-WITHBOSS\".
  8321. If optional argument TODO_ONLY is non-nil, only select lines that are
  8322. also TODO lines."
  8323. (interactive "P")
  8324. (org-prepare-agenda-buffers (list (current-buffer)))
  8325. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8326. (defvar org-cached-props nil)
  8327. (defun org-cached-entry-get (pom property)
  8328. (if (or (eq t org-use-property-inheritance)
  8329. (and (stringp org-use-property-inheritance)
  8330. (string-match org-use-property-inheritance property))
  8331. (and (listp org-use-property-inheritance)
  8332. (member property org-use-property-inheritance)))
  8333. ;; Caching is not possible, check it directly
  8334. (org-entry-get pom property 'inherit)
  8335. ;; Get all properties, so that we can do complicated checks easily
  8336. (cdr (assoc property (or org-cached-props
  8337. (setq org-cached-props
  8338. (org-entry-properties pom)))))))
  8339. (defun org-global-tags-completion-table (&optional files)
  8340. "Return the list of all tags in all agenda buffer/files."
  8341. (save-excursion
  8342. (org-uniquify
  8343. (delq nil
  8344. (apply 'append
  8345. (mapcar
  8346. (lambda (file)
  8347. (set-buffer (find-file-noselect file))
  8348. (append (org-get-buffer-tags)
  8349. (mapcar (lambda (x) (if (stringp (car-safe x))
  8350. (list (car-safe x)) nil))
  8351. org-tag-alist)))
  8352. (if (and files (car files))
  8353. files
  8354. (org-agenda-files))))))))
  8355. (defun org-make-tags-matcher (match)
  8356. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8357. ;; todo-only is scoped dynamically into this function, and the function
  8358. ;; may change it it the matcher asksk for it.
  8359. (unless match
  8360. ;; Get a new match request, with completion
  8361. (let ((org-last-tags-completion-table
  8362. (org-global-tags-completion-table)))
  8363. (setq match (completing-read
  8364. "Match: " 'org-tags-completion-function nil nil nil
  8365. 'org-tags-history))))
  8366. ;; Parse the string and create a lisp form
  8367. (let ((match0 match)
  8368. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8369. minus tag mm
  8370. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8371. orterms term orlist re-p str-p level-p level-op
  8372. prop-p pn pv po cat-p gv)
  8373. (if (string-match "/+" match)
  8374. ;; match contains also a todo-matching request
  8375. (progn
  8376. (setq tagsmatch (substring match 0 (match-beginning 0))
  8377. todomatch (substring match (match-end 0)))
  8378. (if (string-match "^!" todomatch)
  8379. (setq todo-only t todomatch (substring todomatch 1)))
  8380. (if (string-match "^\\s-*$" todomatch)
  8381. (setq todomatch nil)))
  8382. ;; only matching tags
  8383. (setq tagsmatch match todomatch nil))
  8384. ;; Make the tags matcher
  8385. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8386. (setq tagsmatcher t)
  8387. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8388. (while (setq term (pop orterms))
  8389. (while (and (equal (substring term -1) "\\") orterms)
  8390. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8391. (while (string-match re term)
  8392. (setq minus (and (match-end 1)
  8393. (equal (match-string 1 term) "-"))
  8394. tag (match-string 2 term)
  8395. re-p (equal (string-to-char tag) ?{)
  8396. level-p (match-end 4)
  8397. prop-p (match-end 5)
  8398. mm (cond
  8399. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8400. (level-p
  8401. (setq level-op (org-op-to-function (match-string 3 term)))
  8402. `(,level-op level ,(string-to-number
  8403. (match-string 4 term))))
  8404. (prop-p
  8405. (setq pn (match-string 5 term)
  8406. po (match-string 6 term)
  8407. pv (match-string 7 term)
  8408. cat-p (equal pn "CATEGORY")
  8409. re-p (equal (string-to-char pv) ?{)
  8410. str-p (equal (string-to-char pv) ?\")
  8411. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8412. (setq po (org-op-to-function po str-p))
  8413. (if (equal pn "CATEGORY")
  8414. (setq gv '(get-text-property (point) 'org-category))
  8415. (setq gv `(org-cached-entry-get nil ,pn)))
  8416. (if re-p
  8417. (if (eq po 'org<>)
  8418. `(not (string-match ,pv (or ,gv "")))
  8419. `(string-match ,pv (or ,gv "")))
  8420. (if str-p
  8421. `(,po (or ,gv "") ,pv)
  8422. `(,po (string-to-number (or ,gv ""))
  8423. ,(string-to-number pv) ))))
  8424. (t `(member ,(downcase tag) tags-list)))
  8425. mm (if minus (list 'not mm) mm)
  8426. term (substring term (match-end 0)))
  8427. (push mm tagsmatcher))
  8428. (push (if (> (length tagsmatcher) 1)
  8429. (cons 'and tagsmatcher)
  8430. (car tagsmatcher))
  8431. orlist)
  8432. (setq tagsmatcher nil))
  8433. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8434. (setq tagsmatcher
  8435. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8436. ;; Make the todo matcher
  8437. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8438. (setq todomatcher t)
  8439. (setq orterms (org-split-string todomatch "|") orlist nil)
  8440. (while (setq term (pop orterms))
  8441. (while (string-match re term)
  8442. (setq minus (and (match-end 1)
  8443. (equal (match-string 1 term) "-"))
  8444. kwd (match-string 2 term)
  8445. re-p (equal (string-to-char kwd) ?{)
  8446. term (substring term (match-end 0))
  8447. mm (if re-p
  8448. `(string-match ,(substring kwd 1 -1) todo)
  8449. (list 'equal 'todo kwd))
  8450. mm (if minus (list 'not mm) mm))
  8451. (push mm todomatcher))
  8452. (push (if (> (length todomatcher) 1)
  8453. (cons 'and todomatcher)
  8454. (car todomatcher))
  8455. orlist)
  8456. (setq todomatcher nil))
  8457. (setq todomatcher (if (> (length orlist) 1)
  8458. (cons 'or orlist) (car orlist))))
  8459. ;; Return the string and lisp forms of the matcher
  8460. (setq matcher (if todomatcher
  8461. (list 'and tagsmatcher todomatcher)
  8462. tagsmatcher))
  8463. (cons match0 matcher)))
  8464. (defun org-op-to-function (op &optional stringp)
  8465. (setq op
  8466. (cond
  8467. ((equal op "<" ) '(< string< ))
  8468. ((equal op ">" ) '(> org-string> ))
  8469. ((member op '("<=" "=<")) '(<= org-string<= ))
  8470. ((member op '(">=" "=>")) '(>= org-string>= ))
  8471. ((member op '("=" "==")) '(= string= ))
  8472. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8473. (nth (if stringp 1 0) op))
  8474. (defun org<> (a b) (not (= a b)))
  8475. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8476. (defun org-string>= (a b) (not (string< a b)))
  8477. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8478. (defun org-string<> (a b) (not (string= a b)))
  8479. (defun org-match-any-p (re list)
  8480. "Does re match any element of list?"
  8481. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8482. (delq nil list))
  8483. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8484. (defvar org-tags-overlay (org-make-overlay 1 1))
  8485. (org-detach-overlay org-tags-overlay)
  8486. (defun org-get-tags-at (&optional pos)
  8487. "Get a list of all headline tags applicable at POS.
  8488. POS defaults to point. If tags are inherited, the list contains
  8489. the targets in the same sequence as the headlines appear, i.e.
  8490. sthe tags of the current headline come last."
  8491. (interactive)
  8492. (let (tags ltags lastpos parent)
  8493. (save-excursion
  8494. (save-restriction
  8495. (widen)
  8496. (goto-char (or pos (point)))
  8497. (save-match-data
  8498. (condition-case nil
  8499. (progn
  8500. (org-back-to-heading t)
  8501. (while (not (equal lastpos (point)))
  8502. (setq lastpos (point))
  8503. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8504. (setq ltags (org-split-string
  8505. (org-match-string-no-properties 1) ":"))
  8506. (setq tags (append (org-remove-uniherited-tags ltags)
  8507. tags)))
  8508. (or org-use-tag-inheritance (error ""))
  8509. (org-up-heading-all 1)
  8510. (setq parent t)))
  8511. (error nil))))
  8512. tags)))
  8513. (defun org-toggle-tag (tag &optional onoff)
  8514. "Toggle the tag TAG for the current line.
  8515. If ONOFF is `on' or `off', don't toggle but set to this state."
  8516. (unless (org-on-heading-p t) (error "Not on headling"))
  8517. (let (res current)
  8518. (save-excursion
  8519. (beginning-of-line)
  8520. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8521. (point-at-eol) t)
  8522. (progn
  8523. (setq current (match-string 1))
  8524. (replace-match ""))
  8525. (setq current ""))
  8526. (setq current (nreverse (org-split-string current ":")))
  8527. (cond
  8528. ((eq onoff 'on)
  8529. (setq res t)
  8530. (or (member tag current) (push tag current)))
  8531. ((eq onoff 'off)
  8532. (or (not (member tag current)) (setq current (delete tag current))))
  8533. (t (if (member tag current)
  8534. (setq current (delete tag current))
  8535. (setq res t)
  8536. (push tag current))))
  8537. (end-of-line 1)
  8538. (if current
  8539. (progn
  8540. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8541. (org-set-tags nil t))
  8542. (delete-horizontal-space))
  8543. (run-hooks 'org-after-tags-change-hook))
  8544. res))
  8545. (defun org-align-tags-here (to-col)
  8546. ;; Assumes that this is a headline
  8547. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8548. (beginning-of-line 1)
  8549. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8550. (< pos (match-beginning 2)))
  8551. (progn
  8552. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8553. (goto-char (match-beginning 1))
  8554. (insert " ")
  8555. (delete-region (point) (1+ (match-beginning 2)))
  8556. (setq ncol (max (1+ (current-column))
  8557. (1+ col)
  8558. (if (> to-col 0)
  8559. to-col
  8560. (- (abs to-col) tags-l))))
  8561. (setq p (point))
  8562. (insert (make-string (- ncol (current-column)) ?\ ))
  8563. (setq ncol (current-column))
  8564. (tabify p (point-at-eol))
  8565. (org-move-to-column (min ncol col) t))
  8566. (goto-char pos))))
  8567. (defun org-set-tags (&optional arg just-align)
  8568. "Set the tags for the current headline.
  8569. With prefix ARG, realign all tags in headings in the current buffer."
  8570. (interactive "P")
  8571. (let* ((re (concat "^" outline-regexp))
  8572. (current (org-get-tags-string))
  8573. (col (current-column))
  8574. (org-setting-tags t)
  8575. table current-tags inherited-tags ; computed below when needed
  8576. tags p0 c0 c1 rpl)
  8577. (if arg
  8578. (save-excursion
  8579. (goto-char (point-min))
  8580. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8581. (while (re-search-forward re nil t)
  8582. (org-set-tags nil t)
  8583. (end-of-line 1)))
  8584. (message "All tags realigned to column %d" org-tags-column))
  8585. (if just-align
  8586. (setq tags current)
  8587. ;; Get a new set of tags from the user
  8588. (save-excursion
  8589. (setq table (or org-tag-alist (org-get-buffer-tags))
  8590. org-last-tags-completion-table table
  8591. current-tags (org-split-string current ":")
  8592. inherited-tags (nreverse
  8593. (nthcdr (length current-tags)
  8594. (nreverse (org-get-tags-at))))
  8595. tags
  8596. (if (or (eq t org-use-fast-tag-selection)
  8597. (and org-use-fast-tag-selection
  8598. (delq nil (mapcar 'cdr table))))
  8599. (org-fast-tag-selection
  8600. current-tags inherited-tags table
  8601. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8602. (let ((org-add-colon-after-tag-completion t))
  8603. (org-trim
  8604. (org-without-partial-completion
  8605. (completing-read "Tags: " 'org-tags-completion-function
  8606. nil nil current 'org-tags-history)))))))
  8607. (while (string-match "[-+&]+" tags)
  8608. ;; No boolean logic, just a list
  8609. (setq tags (replace-match ":" t t tags))))
  8610. (if (string-match "\\`[\t ]*\\'" tags)
  8611. (setq tags "")
  8612. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8613. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8614. ;; Insert new tags at the correct column
  8615. (beginning-of-line 1)
  8616. (cond
  8617. ((and (equal current "") (equal tags "")))
  8618. ((re-search-forward
  8619. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8620. (point-at-eol) t)
  8621. (if (equal tags "")
  8622. (setq rpl "")
  8623. (goto-char (match-beginning 0))
  8624. (setq c0 (current-column) p0 (point)
  8625. c1 (max (1+ c0) (if (> org-tags-column 0)
  8626. org-tags-column
  8627. (- (- org-tags-column) (length tags))))
  8628. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8629. (replace-match rpl t t)
  8630. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8631. tags)
  8632. (t (error "Tags alignment failed")))
  8633. (org-move-to-column col)
  8634. (unless just-align
  8635. (run-hooks 'org-after-tags-change-hook)))))
  8636. (defun org-change-tag-in-region (beg end tag off)
  8637. "Add or remove TAG for each entry in the region.
  8638. This works in the agenda, and also in an org-mode buffer."
  8639. (interactive
  8640. (list (region-beginning) (region-end)
  8641. (let ((org-last-tags-completion-table
  8642. (if (org-mode-p)
  8643. (org-get-buffer-tags)
  8644. (org-global-tags-completion-table))))
  8645. (completing-read
  8646. "Tag: " 'org-tags-completion-function nil nil nil
  8647. 'org-tags-history))
  8648. (progn
  8649. (message "[s]et or [r]emove? ")
  8650. (equal (read-char-exclusive) ?r))))
  8651. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8652. (let ((agendap (equal major-mode 'org-agenda-mode))
  8653. l1 l2 m buf pos newhead (cnt 0))
  8654. (goto-char end)
  8655. (setq l2 (1- (org-current-line)))
  8656. (goto-char beg)
  8657. (setq l1 (org-current-line))
  8658. (loop for l from l1 to l2 do
  8659. (goto-line l)
  8660. (setq m (get-text-property (point) 'org-hd-marker))
  8661. (when (or (and (org-mode-p) (org-on-heading-p))
  8662. (and agendap m))
  8663. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8664. pos (if agendap m (point)))
  8665. (with-current-buffer buf
  8666. (save-excursion
  8667. (save-restriction
  8668. (goto-char pos)
  8669. (setq cnt (1+ cnt))
  8670. (org-toggle-tag tag (if off 'off 'on))
  8671. (setq newhead (org-get-heading)))))
  8672. (and agendap (org-agenda-change-all-lines newhead m))))
  8673. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8674. (defun org-tags-completion-function (string predicate &optional flag)
  8675. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8676. (confirm (lambda (x) (stringp (car x)))))
  8677. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8678. (setq s1 (match-string 1 string)
  8679. s2 (match-string 2 string))
  8680. (setq s1 "" s2 string))
  8681. (cond
  8682. ((eq flag nil)
  8683. ;; try completion
  8684. (setq rtn (try-completion s2 ctable confirm))
  8685. (if (stringp rtn)
  8686. (setq rtn
  8687. (concat s1 s2 (substring rtn (length s2))
  8688. (if (and org-add-colon-after-tag-completion
  8689. (assoc rtn ctable))
  8690. ":" ""))))
  8691. rtn)
  8692. ((eq flag t)
  8693. ;; all-completions
  8694. (all-completions s2 ctable confirm)
  8695. )
  8696. ((eq flag 'lambda)
  8697. ;; exact match?
  8698. (assoc s2 ctable)))
  8699. ))
  8700. (defun org-fast-tag-insert (kwd tags face &optional end)
  8701. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8702. (insert (format "%-12s" (concat kwd ":"))
  8703. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8704. (or end "")))
  8705. (defun org-fast-tag-show-exit (flag)
  8706. (save-excursion
  8707. (goto-line 3)
  8708. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8709. (replace-match ""))
  8710. (when flag
  8711. (end-of-line 1)
  8712. (org-move-to-column (- (window-width) 19) t)
  8713. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8714. (defun org-set-current-tags-overlay (current prefix)
  8715. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8716. (if (featurep 'xemacs)
  8717. (org-overlay-display org-tags-overlay (concat prefix s)
  8718. 'secondary-selection)
  8719. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8720. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8721. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8722. "Fast tag selection with single keys.
  8723. CURRENT is the current list of tags in the headline, INHERITED is the
  8724. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8725. possibly with grouping information. TODO-TABLE is a similar table with
  8726. TODO keywords, should these have keys assigned to them.
  8727. If the keys are nil, a-z are automatically assigned.
  8728. Returns the new tags string, or nil to not change the current settings."
  8729. (let* ((fulltable (append table todo-table))
  8730. (maxlen (apply 'max (mapcar
  8731. (lambda (x)
  8732. (if (stringp (car x)) (string-width (car x)) 0))
  8733. fulltable)))
  8734. (buf (current-buffer))
  8735. (expert (eq org-fast-tag-selection-single-key 'expert))
  8736. (buffer-tags nil)
  8737. (fwidth (+ maxlen 3 1 3))
  8738. (ncol (/ (- (window-width) 4) fwidth))
  8739. (i-face 'org-done)
  8740. (c-face 'org-todo)
  8741. tg cnt e c char c1 c2 ntable tbl rtn
  8742. ov-start ov-end ov-prefix
  8743. (exit-after-next org-fast-tag-selection-single-key)
  8744. (done-keywords org-done-keywords)
  8745. groups ingroup)
  8746. (save-excursion
  8747. (beginning-of-line 1)
  8748. (if (looking-at
  8749. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8750. (setq ov-start (match-beginning 1)
  8751. ov-end (match-end 1)
  8752. ov-prefix "")
  8753. (setq ov-start (1- (point-at-eol))
  8754. ov-end (1+ ov-start))
  8755. (skip-chars-forward "^\n\r")
  8756. (setq ov-prefix
  8757. (concat
  8758. (buffer-substring (1- (point)) (point))
  8759. (if (> (current-column) org-tags-column)
  8760. " "
  8761. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8762. (org-move-overlay org-tags-overlay ov-start ov-end)
  8763. (save-window-excursion
  8764. (if expert
  8765. (set-buffer (get-buffer-create " *Org tags*"))
  8766. (delete-other-windows)
  8767. (split-window-vertically)
  8768. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8769. (erase-buffer)
  8770. (org-set-local 'org-done-keywords done-keywords)
  8771. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8772. (org-fast-tag-insert "Current" current c-face "\n\n")
  8773. (org-fast-tag-show-exit exit-after-next)
  8774. (org-set-current-tags-overlay current ov-prefix)
  8775. (setq tbl fulltable char ?a cnt 0)
  8776. (while (setq e (pop tbl))
  8777. (cond
  8778. ((equal e '(:startgroup))
  8779. (push '() groups) (setq ingroup t)
  8780. (when (not (= cnt 0))
  8781. (setq cnt 0)
  8782. (insert "\n"))
  8783. (insert "{ "))
  8784. ((equal e '(:endgroup))
  8785. (setq ingroup nil cnt 0)
  8786. (insert "}\n"))
  8787. (t
  8788. (setq tg (car e) c2 nil)
  8789. (if (cdr e)
  8790. (setq c (cdr e))
  8791. ;; automatically assign a character.
  8792. (setq c1 (string-to-char
  8793. (downcase (substring
  8794. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8795. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8796. (while (or (rassoc char ntable) (rassoc char table))
  8797. (setq char (1+ char)))
  8798. (setq c2 c1))
  8799. (setq c (or c2 char)))
  8800. (if ingroup (push tg (car groups)))
  8801. (setq tg (org-add-props tg nil 'face
  8802. (cond
  8803. ((not (assoc tg table))
  8804. (org-get-todo-face tg))
  8805. ((member tg current) c-face)
  8806. ((member tg inherited) i-face)
  8807. (t nil))))
  8808. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8809. (insert "[" c "] " tg (make-string
  8810. (- fwidth 4 (length tg)) ?\ ))
  8811. (push (cons tg c) ntable)
  8812. (when (= (setq cnt (1+ cnt)) ncol)
  8813. (insert "\n")
  8814. (if ingroup (insert " "))
  8815. (setq cnt 0)))))
  8816. (setq ntable (nreverse ntable))
  8817. (insert "\n")
  8818. (goto-char (point-min))
  8819. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8820. (fit-window-to-buffer))
  8821. (setq rtn
  8822. (catch 'exit
  8823. (while t
  8824. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8825. (if groups " [!] no groups" " [!]groups")
  8826. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8827. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8828. (cond
  8829. ((= c ?\r) (throw 'exit t))
  8830. ((= c ?!)
  8831. (setq groups (not groups))
  8832. (goto-char (point-min))
  8833. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8834. ((= c ?\C-c)
  8835. (if (not expert)
  8836. (org-fast-tag-show-exit
  8837. (setq exit-after-next (not exit-after-next)))
  8838. (setq expert nil)
  8839. (delete-other-windows)
  8840. (split-window-vertically)
  8841. (org-switch-to-buffer-other-window " *Org tags*")
  8842. (and (fboundp 'fit-window-to-buffer)
  8843. (fit-window-to-buffer))))
  8844. ((or (= c ?\C-g)
  8845. (and (= c ?q) (not (rassoc c ntable))))
  8846. (org-detach-overlay org-tags-overlay)
  8847. (setq quit-flag t))
  8848. ((= c ?\ )
  8849. (setq current nil)
  8850. (if exit-after-next (setq exit-after-next 'now)))
  8851. ((= c ?\t)
  8852. (condition-case nil
  8853. (setq tg (completing-read
  8854. "Tag: "
  8855. (or buffer-tags
  8856. (with-current-buffer buf
  8857. (org-get-buffer-tags)))))
  8858. (quit (setq tg "")))
  8859. (when (string-match "\\S-" tg)
  8860. (add-to-list 'buffer-tags (list tg))
  8861. (if (member tg current)
  8862. (setq current (delete tg current))
  8863. (push tg current)))
  8864. (if exit-after-next (setq exit-after-next 'now)))
  8865. ((setq e (rassoc c todo-table) tg (car e))
  8866. (with-current-buffer buf
  8867. (save-excursion (org-todo tg)))
  8868. (if exit-after-next (setq exit-after-next 'now)))
  8869. ((setq e (rassoc c ntable) tg (car e))
  8870. (if (member tg current)
  8871. (setq current (delete tg current))
  8872. (loop for g in groups do
  8873. (if (member tg g)
  8874. (mapc (lambda (x)
  8875. (setq current (delete x current)))
  8876. g)))
  8877. (push tg current))
  8878. (if exit-after-next (setq exit-after-next 'now))))
  8879. ;; Create a sorted list
  8880. (setq current
  8881. (sort current
  8882. (lambda (a b)
  8883. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8884. (if (eq exit-after-next 'now) (throw 'exit t))
  8885. (goto-char (point-min))
  8886. (beginning-of-line 2)
  8887. (delete-region (point) (point-at-eol))
  8888. (org-fast-tag-insert "Current" current c-face)
  8889. (org-set-current-tags-overlay current ov-prefix)
  8890. (while (re-search-forward
  8891. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8892. (setq tg (match-string 1))
  8893. (add-text-properties
  8894. (match-beginning 1) (match-end 1)
  8895. (list 'face
  8896. (cond
  8897. ((member tg current) c-face)
  8898. ((member tg inherited) i-face)
  8899. (t (get-text-property (match-beginning 1) 'face))))))
  8900. (goto-char (point-min)))))
  8901. (org-detach-overlay org-tags-overlay)
  8902. (if rtn
  8903. (mapconcat 'identity current ":")
  8904. nil))))
  8905. (defun org-get-tags-string ()
  8906. "Get the TAGS string in the current headline."
  8907. (unless (org-on-heading-p t)
  8908. (error "Not on a heading"))
  8909. (save-excursion
  8910. (beginning-of-line 1)
  8911. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8912. (org-match-string-no-properties 1)
  8913. "")))
  8914. (defun org-get-tags ()
  8915. "Get the list of tags specified in the current headline."
  8916. (org-split-string (org-get-tags-string) ":"))
  8917. (defun org-get-buffer-tags ()
  8918. "Get a table of all tags used in the buffer, for completion."
  8919. (let (tags)
  8920. (save-excursion
  8921. (goto-char (point-min))
  8922. (while (re-search-forward
  8923. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8924. (when (equal (char-after (point-at-bol 0)) ?*)
  8925. (mapc (lambda (x) (add-to-list 'tags x))
  8926. (org-split-string (org-match-string-no-properties 1) ":")))))
  8927. (mapcar 'list tags)))
  8928. ;;;; Properties
  8929. ;;; Setting and retrieving properties
  8930. (defconst org-special-properties
  8931. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8932. "TIMESTAMP" "TIMESTAMP_IA")
  8933. "The special properties valid in Org-mode.
  8934. These are properties that are not defined in the property drawer,
  8935. but in some other way.")
  8936. (defconst org-default-properties
  8937. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8938. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8939. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8940. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8941. "Some properties that are used by Org-mode for various purposes.
  8942. Being in this list makes sure that they are offered for completion.")
  8943. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8944. "Regular expression matching the first line of a property drawer.")
  8945. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8946. "Regular expression matching the first line of a property drawer.")
  8947. (defun org-property-action ()
  8948. "Do an action on properties."
  8949. (interactive)
  8950. (let (c)
  8951. (org-at-property-p)
  8952. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8953. (setq c (read-char-exclusive))
  8954. (cond
  8955. ((equal c ?s)
  8956. (call-interactively 'org-set-property))
  8957. ((equal c ?d)
  8958. (call-interactively 'org-delete-property))
  8959. ((equal c ?D)
  8960. (call-interactively 'org-delete-property-globally))
  8961. ((equal c ?c)
  8962. (call-interactively 'org-compute-property-at-point))
  8963. (t (error "No such property action %c" c)))))
  8964. (defun org-at-property-p ()
  8965. "Is the cursor in a property line?"
  8966. ;; FIXME: Does not check if we are actually in the drawer.
  8967. ;; FIXME: also returns true on any drawers.....
  8968. ;; This is used by C-c C-c for property action.
  8969. (save-excursion
  8970. (beginning-of-line 1)
  8971. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8972. (defun org-get-property-block (&optional beg end force)
  8973. "Return the (beg . end) range of the body of the property drawer.
  8974. BEG and END can be beginning and end of subtree, if not given
  8975. they will be found.
  8976. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8977. (catch 'exit
  8978. (save-excursion
  8979. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8980. (end (or end (progn (outline-next-heading) (point)))))
  8981. (goto-char beg)
  8982. (if (re-search-forward org-property-start-re end t)
  8983. (setq beg (1+ (match-end 0)))
  8984. (if force
  8985. (save-excursion
  8986. (org-insert-property-drawer)
  8987. (setq end (progn (outline-next-heading) (point))))
  8988. (throw 'exit nil))
  8989. (goto-char beg)
  8990. (if (re-search-forward org-property-start-re end t)
  8991. (setq beg (1+ (match-end 0)))))
  8992. (if (re-search-forward org-property-end-re end t)
  8993. (setq end (match-beginning 0))
  8994. (or force (throw 'exit nil))
  8995. (goto-char beg)
  8996. (setq end beg)
  8997. (org-indent-line-function)
  8998. (insert ":END:\n"))
  8999. (cons beg end)))))
  9000. (defun org-entry-properties (&optional pom which)
  9001. "Get all properties of the entry at point-or-marker POM.
  9002. This includes the TODO keyword, the tags, time strings for deadline,
  9003. scheduled, and clocking, and any additional properties defined in the
  9004. entry. The return value is an alist, keys may occur multiple times
  9005. if the property key was used several times.
  9006. POM may also be nil, in which case the current entry is used.
  9007. If WHICH is nil or `all', get all properties. If WHICH is
  9008. `special' or `standard', only get that subclass."
  9009. (setq which (or which 'all))
  9010. (org-with-point-at pom
  9011. (let ((clockstr (substring org-clock-string 0 -1))
  9012. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9013. beg end range props sum-props key value string clocksum)
  9014. (save-excursion
  9015. (when (condition-case nil (org-back-to-heading t) (error nil))
  9016. (setq beg (point))
  9017. (setq sum-props (get-text-property (point) 'org-summaries))
  9018. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9019. (outline-next-heading)
  9020. (setq end (point))
  9021. (when (memq which '(all special))
  9022. ;; Get the special properties, like TODO and tags
  9023. (goto-char beg)
  9024. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9025. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9026. (when (looking-at org-priority-regexp)
  9027. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9028. (when (and (setq value (org-get-tags-string))
  9029. (string-match "\\S-" value))
  9030. (push (cons "TAGS" value) props))
  9031. (when (setq value (org-get-tags-at))
  9032. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9033. props))
  9034. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9035. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9036. string (if (equal key clockstr)
  9037. (org-no-properties
  9038. (org-trim
  9039. (buffer-substring
  9040. (match-beginning 3) (goto-char (point-at-eol)))))
  9041. (substring (org-match-string-no-properties 3) 1 -1)))
  9042. (unless key
  9043. (if (= (char-after (match-beginning 3)) ?\[)
  9044. (setq key "TIMESTAMP_IA")
  9045. (setq key "TIMESTAMP")))
  9046. (when (or (equal key clockstr) (not (assoc key props)))
  9047. (push (cons key string) props)))
  9048. )
  9049. (when (memq which '(all standard))
  9050. ;; Get the standard properties, like :PORP: ...
  9051. (setq range (org-get-property-block beg end))
  9052. (when range
  9053. (goto-char (car range))
  9054. (while (re-search-forward
  9055. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9056. (cdr range) t)
  9057. (setq key (org-match-string-no-properties 1)
  9058. value (org-trim (or (org-match-string-no-properties 2) "")))
  9059. (unless (member key excluded)
  9060. (push (cons key (or value "")) props)))))
  9061. (if clocksum
  9062. (push (cons "CLOCKSUM"
  9063. (org-columns-number-to-string (/ (float clocksum) 60.)
  9064. 'add_times))
  9065. props))
  9066. (append sum-props (nreverse props)))))))
  9067. (defun org-entry-get (pom property &optional inherit)
  9068. "Get value of PROPERTY for entry at point-or-marker POM.
  9069. If INHERIT is non-nil and the entry does not have the property,
  9070. then also check higher levels of the hierarchy.
  9071. If INHERIT is the symbol `selective', use inheritance only if the setting
  9072. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9073. If the property is present but empty, the return value is the empty string.
  9074. If the property is not present at all, nil is returned."
  9075. (org-with-point-at pom
  9076. (if (and inherit (if (eq inherit 'selective)
  9077. (org-property-inherit-p property)
  9078. t))
  9079. (org-entry-get-with-inheritance property)
  9080. (if (member property org-special-properties)
  9081. ;; We need a special property. Use brute force, get all properties.
  9082. (cdr (assoc property (org-entry-properties nil 'special)))
  9083. (let ((range (org-get-property-block)))
  9084. (if (and range
  9085. (goto-char (car range))
  9086. (re-search-forward
  9087. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9088. (cdr range) t))
  9089. ;; Found the property, return it.
  9090. (if (match-end 1)
  9091. (org-match-string-no-properties 1)
  9092. "")))))))
  9093. (defun org-property-or-variable-value (var &optional inherit)
  9094. "Check if there is a property fixing the value of VAR.
  9095. If yes, return this value. If not, return the current value of the variable."
  9096. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9097. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9098. (read prop)
  9099. (symbol-value var))))
  9100. (defun org-entry-delete (pom property)
  9101. "Delete the property PROPERTY from entry at point-or-marker POM."
  9102. (org-with-point-at pom
  9103. (if (member property org-special-properties)
  9104. nil ; cannot delete these properties.
  9105. (let ((range (org-get-property-block)))
  9106. (if (and range
  9107. (goto-char (car range))
  9108. (re-search-forward
  9109. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9110. (cdr range) t))
  9111. (progn
  9112. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9113. t)
  9114. nil)))))
  9115. ;; Multi-values properties are properties that contain multiple values
  9116. ;; These values are assumed to be single words, separated by whitespace.
  9117. (defun org-entry-add-to-multivalued-property (pom property value)
  9118. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9119. (let* ((old (org-entry-get pom property))
  9120. (values (and old (org-split-string old "[ \t]"))))
  9121. (unless (member value values)
  9122. (setq values (cons value values))
  9123. (org-entry-put pom property
  9124. (mapconcat 'identity values " ")))))
  9125. (defun org-entry-remove-from-multivalued-property (pom property value)
  9126. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9127. (let* ((old (org-entry-get pom property))
  9128. (values (and old (org-split-string old "[ \t]"))))
  9129. (when (member value values)
  9130. (setq values (delete value values))
  9131. (org-entry-put pom property
  9132. (mapconcat 'identity values " ")))))
  9133. (defun org-entry-member-in-multivalued-property (pom property value)
  9134. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9135. (let* ((old (org-entry-get pom property))
  9136. (values (and old (org-split-string old "[ \t]"))))
  9137. (member value values)))
  9138. (defvar org-entry-property-inherited-from (make-marker))
  9139. (defun org-entry-get-with-inheritance (property)
  9140. "Get entry property, and search higher levels if not present."
  9141. (let (tmp)
  9142. (save-excursion
  9143. (save-restriction
  9144. (widen)
  9145. (catch 'ex
  9146. (while t
  9147. (when (setq tmp (org-entry-get nil property))
  9148. (org-back-to-heading t)
  9149. (move-marker org-entry-property-inherited-from (point))
  9150. (throw 'ex tmp))
  9151. (or (org-up-heading-safe) (throw 'ex nil)))))
  9152. (or tmp
  9153. (cdr (assoc property org-file-properties))
  9154. (cdr (assoc property org-global-properties))
  9155. (cdr (assoc property org-global-properties-fixed))))))
  9156. (defun org-entry-put (pom property value)
  9157. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9158. (org-with-point-at pom
  9159. (org-back-to-heading t)
  9160. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9161. range)
  9162. (cond
  9163. ((equal property "TODO")
  9164. (when (and (stringp value) (string-match "\\S-" value)
  9165. (not (member value org-todo-keywords-1)))
  9166. (error "\"%s\" is not a valid TODO state" value))
  9167. (if (or (not value)
  9168. (not (string-match "\\S-" value)))
  9169. (setq value 'none))
  9170. (org-todo value)
  9171. (org-set-tags nil 'align))
  9172. ((equal property "PRIORITY")
  9173. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9174. (string-to-char value) ?\ ))
  9175. (org-set-tags nil 'align))
  9176. ((equal property "SCHEDULED")
  9177. (if (re-search-forward org-scheduled-time-regexp end t)
  9178. (cond
  9179. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9180. ((eq value 'later) (org-timestamp-change 1 'day))
  9181. (t (call-interactively 'org-schedule)))
  9182. (call-interactively 'org-schedule)))
  9183. ((equal property "DEADLINE")
  9184. (if (re-search-forward org-deadline-time-regexp end t)
  9185. (cond
  9186. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9187. ((eq value 'later) (org-timestamp-change 1 'day))
  9188. (t (call-interactively 'org-deadline)))
  9189. (call-interactively 'org-deadline)))
  9190. ((member property org-special-properties)
  9191. (error "The %s property can not yet be set with `org-entry-put'"
  9192. property))
  9193. (t ; a non-special property
  9194. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9195. (setq range (org-get-property-block beg end 'force))
  9196. (goto-char (car range))
  9197. (if (re-search-forward
  9198. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9199. (progn
  9200. (delete-region (match-beginning 1) (match-end 1))
  9201. (goto-char (match-beginning 1)))
  9202. (goto-char (cdr range))
  9203. (insert "\n")
  9204. (backward-char 1)
  9205. (org-indent-line-function)
  9206. (insert ":" property ":"))
  9207. (and value (insert " " value))
  9208. (org-indent-line-function)))))))
  9209. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9210. "Get all property keys in the current buffer.
  9211. With INCLUDE-SPECIALS, also list the special properties that relect things
  9212. like tags and TODO state.
  9213. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9214. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9215. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9216. formats in the current buffer."
  9217. (let (rtn range cfmt cols s p)
  9218. (save-excursion
  9219. (save-restriction
  9220. (widen)
  9221. (goto-char (point-min))
  9222. (while (re-search-forward org-property-start-re nil t)
  9223. (setq range (org-get-property-block))
  9224. (goto-char (car range))
  9225. (while (re-search-forward
  9226. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9227. (cdr range) t)
  9228. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9229. (outline-next-heading))))
  9230. (when include-specials
  9231. (setq rtn (append org-special-properties rtn)))
  9232. (when include-defaults
  9233. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9234. (when include-columns
  9235. (save-excursion
  9236. (save-restriction
  9237. (widen)
  9238. (goto-char (point-min))
  9239. (while (re-search-forward
  9240. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9241. nil t)
  9242. (setq cfmt (match-string 2) s 0)
  9243. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9244. cfmt s)
  9245. (setq s (match-end 0)
  9246. p (match-string 1 cfmt))
  9247. (unless (or (equal p "ITEM")
  9248. (member p org-special-properties))
  9249. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9250. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9251. (defun org-property-values (key)
  9252. "Return a list of all values of property KEY."
  9253. (save-excursion
  9254. (save-restriction
  9255. (widen)
  9256. (goto-char (point-min))
  9257. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9258. values)
  9259. (while (re-search-forward re nil t)
  9260. (add-to-list 'values (org-trim (match-string 1))))
  9261. (delete "" values)))))
  9262. (defun org-insert-property-drawer ()
  9263. "Insert a property drawer into the current entry."
  9264. (interactive)
  9265. (org-back-to-heading t)
  9266. (looking-at outline-regexp)
  9267. (let ((indent (- (match-end 0)(match-beginning 0)))
  9268. (beg (point))
  9269. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9270. end hiddenp)
  9271. (outline-next-heading)
  9272. (setq end (point))
  9273. (goto-char beg)
  9274. (while (re-search-forward re end t))
  9275. (setq hiddenp (org-invisible-p))
  9276. (end-of-line 1)
  9277. (and (equal (char-after) ?\n) (forward-char 1))
  9278. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9279. (beginning-of-line 2))
  9280. (org-skip-over-state-notes)
  9281. (skip-chars-backward " \t\n\r")
  9282. (if (eq (char-before) ?*) (forward-char 1))
  9283. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9284. (beginning-of-line 0)
  9285. (org-indent-to-column indent)
  9286. (beginning-of-line 2)
  9287. (org-indent-to-column indent)
  9288. (beginning-of-line 0)
  9289. (if hiddenp
  9290. (save-excursion
  9291. (org-back-to-heading t)
  9292. (hide-entry))
  9293. (org-flag-drawer t))))
  9294. (defun org-set-property (property value)
  9295. "In the current entry, set PROPERTY to VALUE.
  9296. When called interactively, this will prompt for a property name, offering
  9297. completion on existing and default properties. And then it will prompt
  9298. for a value, offering competion either on allowed values (via an inherited
  9299. xxx_ALL property) or on existing values in other instances of this property
  9300. in the current file."
  9301. (interactive
  9302. (let* ((completion-ignore-case t)
  9303. (keys (org-buffer-property-keys nil t t))
  9304. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9305. (prop (if (member prop0 keys)
  9306. prop0
  9307. (or (cdr (assoc (downcase prop0)
  9308. (mapcar (lambda (x) (cons (downcase x) x))
  9309. keys)))
  9310. prop0)))
  9311. (cur (org-entry-get nil prop))
  9312. (allowed (org-property-get-allowed-values nil prop 'table))
  9313. (existing (mapcar 'list (org-property-values prop)))
  9314. (val (if allowed
  9315. (org-completing-read "Value: " allowed nil 'req-match)
  9316. (org-completing-read
  9317. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9318. (concat "[" cur "]") "")
  9319. ": ")
  9320. existing nil nil "" nil cur))))
  9321. (list prop (if (equal val "") cur val))))
  9322. (unless (equal (org-entry-get nil property) value)
  9323. (org-entry-put nil property value)))
  9324. (defun org-delete-property (property)
  9325. "In the current entry, delete PROPERTY."
  9326. (interactive
  9327. (let* ((completion-ignore-case t)
  9328. (prop (completing-read
  9329. "Property: " (org-entry-properties nil 'standard))))
  9330. (list prop)))
  9331. (message "Property %s %s" property
  9332. (if (org-entry-delete nil property)
  9333. "deleted"
  9334. "was not present in the entry")))
  9335. (defun org-delete-property-globally (property)
  9336. "Remove PROPERTY globally, from all entries."
  9337. (interactive
  9338. (let* ((completion-ignore-case t)
  9339. (prop (completing-read
  9340. "Globally remove property: "
  9341. (mapcar 'list (org-buffer-property-keys)))))
  9342. (list prop)))
  9343. (save-excursion
  9344. (save-restriction
  9345. (widen)
  9346. (goto-char (point-min))
  9347. (let ((cnt 0))
  9348. (while (re-search-forward
  9349. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9350. nil t)
  9351. (setq cnt (1+ cnt))
  9352. (replace-match ""))
  9353. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9354. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9355. (defun org-compute-property-at-point ()
  9356. "Compute the property at point.
  9357. This looks for an enclosing column format, extracts the operator and
  9358. then applies it to the proerty in the column format's scope."
  9359. (interactive)
  9360. (unless (org-at-property-p)
  9361. (error "Not at a property"))
  9362. (let ((prop (org-match-string-no-properties 2)))
  9363. (org-columns-get-format-and-top-level)
  9364. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9365. (error "No operator defined for property %s" prop))
  9366. (org-columns-compute prop)))
  9367. (defun org-property-get-allowed-values (pom property &optional table)
  9368. "Get allowed values for the property PROPERTY.
  9369. When TABLE is non-nil, return an alist that can directly be used for
  9370. completion."
  9371. (let (vals)
  9372. (cond
  9373. ((equal property "TODO")
  9374. (setq vals (org-with-point-at pom
  9375. (append org-todo-keywords-1 '("")))))
  9376. ((equal property "PRIORITY")
  9377. (let ((n org-lowest-priority))
  9378. (while (>= n org-highest-priority)
  9379. (push (char-to-string n) vals)
  9380. (setq n (1- n)))))
  9381. ((member property org-special-properties))
  9382. (t
  9383. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9384. (when (and vals (string-match "\\S-" vals))
  9385. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9386. (setq vals (mapcar (lambda (x)
  9387. (cond ((stringp x) x)
  9388. ((numberp x) (number-to-string x))
  9389. ((symbolp x) (symbol-name x))
  9390. (t "???")))
  9391. vals)))))
  9392. (if table (mapcar 'list vals) vals)))
  9393. (defun org-property-previous-allowed-value (&optional previous)
  9394. "Switch to the next allowed value for this property."
  9395. (interactive)
  9396. (org-property-next-allowed-value t))
  9397. (defun org-property-next-allowed-value (&optional previous)
  9398. "Switch to the next allowed value for this property."
  9399. (interactive)
  9400. (unless (org-at-property-p)
  9401. (error "Not at a property"))
  9402. (let* ((key (match-string 2))
  9403. (value (match-string 3))
  9404. (allowed (or (org-property-get-allowed-values (point) key)
  9405. (and (member value '("[ ]" "[-]" "[X]"))
  9406. '("[ ]" "[X]"))))
  9407. nval)
  9408. (unless allowed
  9409. (error "Allowed values for this property have not been defined"))
  9410. (if previous (setq allowed (reverse allowed)))
  9411. (if (member value allowed)
  9412. (setq nval (car (cdr (member value allowed)))))
  9413. (setq nval (or nval (car allowed)))
  9414. (if (equal nval value)
  9415. (error "Only one allowed value for this property"))
  9416. (org-at-property-p)
  9417. (replace-match (concat " :" key ": " nval) t t)
  9418. (org-indent-line-function)
  9419. (beginning-of-line 1)
  9420. (skip-chars-forward " \t")))
  9421. (defun org-find-entry-with-id (ident)
  9422. "Locate the entry that contains the ID property with exact value IDENT.
  9423. IDENT can be a string, a symbol or a number, this function will search for
  9424. the string representation of it.
  9425. Return the position where this entry starts, or nil if there is no such entry."
  9426. (let ((id (cond
  9427. ((stringp ident) ident)
  9428. ((symbol-name ident) (symbol-name ident))
  9429. ((numberp ident) (number-to-string ident))
  9430. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9431. (case-fold-search nil))
  9432. (save-excursion
  9433. (save-restriction
  9434. (widen)
  9435. (goto-char (point-min))
  9436. (when (re-search-forward
  9437. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9438. nil t)
  9439. (org-back-to-heading)
  9440. (point))))))
  9441. ;;;; Timestamps
  9442. (defvar org-last-changed-timestamp nil)
  9443. (defvar org-time-was-given) ; dynamically scoped parameter
  9444. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9445. (defvar org-ts-what) ; dynamically scoped parameter
  9446. (defun org-time-stamp (arg)
  9447. "Prompt for a date/time and insert a time stamp.
  9448. If the user specifies a time like HH:MM, or if this command is called
  9449. with a prefix argument, the time stamp will contain date and time.
  9450. Otherwise, only the date will be included. All parts of a date not
  9451. specified by the user will be filled in from the current date/time.
  9452. So if you press just return without typing anything, the time stamp
  9453. will represent the current date/time. If there is already a timestamp
  9454. at the cursor, it will be modified."
  9455. (interactive "P")
  9456. (let* ((ts nil)
  9457. (default-time
  9458. ;; Default time is either today, or, when entering a range,
  9459. ;; the range start.
  9460. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9461. (save-excursion
  9462. (re-search-backward
  9463. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9464. (- (point) 20) t)))
  9465. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9466. (current-time)))
  9467. (default-input (and ts (org-get-compact-tod ts)))
  9468. org-time-was-given org-end-time-was-given time)
  9469. (cond
  9470. ((and (org-at-timestamp-p)
  9471. (eq last-command 'org-time-stamp)
  9472. (eq this-command 'org-time-stamp))
  9473. (insert "--")
  9474. (setq time (let ((this-command this-command))
  9475. (org-read-date arg 'totime nil nil default-time default-input)))
  9476. (org-insert-time-stamp time (or org-time-was-given arg)))
  9477. ((org-at-timestamp-p)
  9478. (setq time (let ((this-command this-command))
  9479. (org-read-date arg 'totime nil nil default-time default-input)))
  9480. (when (org-at-timestamp-p) ; just to get the match data
  9481. (replace-match "")
  9482. (setq org-last-changed-timestamp
  9483. (org-insert-time-stamp
  9484. time (or org-time-was-given arg)
  9485. nil nil nil (list org-end-time-was-given))))
  9486. (message "Timestamp updated"))
  9487. (t
  9488. (setq time (let ((this-command this-command))
  9489. (org-read-date arg 'totime nil nil default-time default-input)))
  9490. (org-insert-time-stamp time (or org-time-was-given arg)
  9491. nil nil nil (list org-end-time-was-given))))))
  9492. ;; FIXME: can we use this for something else, like computing time differences?
  9493. (defun org-get-compact-tod (s)
  9494. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9495. (let* ((t1 (match-string 1 s))
  9496. (h1 (string-to-number (match-string 2 s)))
  9497. (m1 (string-to-number (match-string 3 s)))
  9498. (t2 (and (match-end 4) (match-string 5 s)))
  9499. (h2 (and t2 (string-to-number (match-string 6 s))))
  9500. (m2 (and t2 (string-to-number (match-string 7 s))))
  9501. dh dm)
  9502. (if (not t2)
  9503. t1
  9504. (setq dh (- h2 h1) dm (- m2 m1))
  9505. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9506. (concat t1 "+" (number-to-string dh)
  9507. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9508. (defun org-time-stamp-inactive (&optional arg)
  9509. "Insert an inactive time stamp.
  9510. An inactive time stamp is enclosed in square brackets instead of angle
  9511. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9512. does not link to the calendar and cannot be changed with the S-cursor keys.
  9513. So these are more for recording a certain time/date."
  9514. (interactive "P")
  9515. (let (org-time-was-given org-end-time-was-given time)
  9516. (setq time (org-read-date arg 'totime))
  9517. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9518. nil nil (list org-end-time-was-given))))
  9519. (defvar org-date-ovl (org-make-overlay 1 1))
  9520. (org-overlay-put org-date-ovl 'face 'org-warning)
  9521. (org-detach-overlay org-date-ovl)
  9522. (defvar org-ans1) ; dynamically scoped parameter
  9523. (defvar org-ans2) ; dynamically scoped parameter
  9524. (defvar org-plain-time-of-day-regexp) ; defined below
  9525. (defvar org-read-date-overlay nil)
  9526. (defvar org-dcst nil) ; dynamically scoped
  9527. (defun org-read-date (&optional with-time to-time from-string prompt
  9528. default-time default-input)
  9529. "Read a date, possibly a time, and make things smooth for the user.
  9530. The prompt will suggest to enter an ISO date, but you can also enter anything
  9531. which will at least partially be understood by `parse-time-string'.
  9532. Unrecognized parts of the date will default to the current day, month, year,
  9533. hour and minute. If this command is called to replace a timestamp at point,
  9534. of to enter the second timestamp of a range, the default time is taken from the
  9535. existing stamp. For example,
  9536. 3-2-5 --> 2003-02-05
  9537. feb 15 --> currentyear-02-15
  9538. sep 12 9 --> 2009-09-12
  9539. 12:45 --> today 12:45
  9540. 22 sept 0:34 --> currentyear-09-22 0:34
  9541. 12 --> currentyear-currentmonth-12
  9542. Fri --> nearest Friday (today or later)
  9543. etc.
  9544. Furthermore you can specify a relative date by giving, as the *first* thing
  9545. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9546. change in days weeks, months, years.
  9547. With a single plus or minus, the date is relative to today. With a double
  9548. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9549. +4d --> four days from today
  9550. +4 --> same as above
  9551. +2w --> two weeks from today
  9552. ++5 --> five days from default date
  9553. The function understands only English month and weekday abbreviations,
  9554. but this can be configured with the variables `parse-time-months' and
  9555. `parse-time-weekdays'.
  9556. While prompting, a calendar is popped up - you can also select the
  9557. date with the mouse (button 1). The calendar shows a period of three
  9558. months. To scroll it to other months, use the keys `>' and `<'.
  9559. If you don't like the calendar, turn it off with
  9560. \(setq org-read-date-popup-calendar nil)
  9561. With optional argument TO-TIME, the date will immediately be converted
  9562. to an internal time.
  9563. With an optional argument WITH-TIME, the prompt will suggest to also
  9564. insert a time. Note that when WITH-TIME is not set, you can still
  9565. enter a time, and this function will inform the calling routine about
  9566. this change. The calling routine may then choose to change the format
  9567. used to insert the time stamp into the buffer to include the time.
  9568. With optional argument FROM-STRING, read from this string instead from
  9569. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9570. the time/date that is used for everything that is not specified by the
  9571. user."
  9572. (require 'parse-time)
  9573. (let* ((org-time-stamp-rounding-minutes
  9574. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9575. (org-dcst org-display-custom-times)
  9576. (ct (org-current-time))
  9577. (def (or default-time ct))
  9578. (defdecode (decode-time def))
  9579. (dummy (progn
  9580. (when (< (nth 2 defdecode) org-extend-today-until)
  9581. (setcar (nthcdr 2 defdecode) -1)
  9582. (setcar (nthcdr 1 defdecode) 59)
  9583. (setq def (apply 'encode-time defdecode)
  9584. defdecode (decode-time def)))))
  9585. (calendar-move-hook nil)
  9586. (calendar-view-diary-initially-flag nil)
  9587. (view-diary-entries-initially nil)
  9588. (calendar-view-holidays-initially-flag nil)
  9589. (view-calendar-holidays-initially nil)
  9590. (timestr (format-time-string
  9591. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9592. (prompt (concat (if prompt (concat prompt " ") "")
  9593. (format "Date+time [%s]: " timestr)))
  9594. ans (org-ans0 "") org-ans1 org-ans2 final)
  9595. (cond
  9596. (from-string (setq ans from-string))
  9597. (org-read-date-popup-calendar
  9598. (save-excursion
  9599. (save-window-excursion
  9600. (calendar)
  9601. (calendar-forward-day (- (time-to-days def)
  9602. (calendar-absolute-from-gregorian
  9603. (calendar-current-date))))
  9604. (org-eval-in-calendar nil t)
  9605. (let* ((old-map (current-local-map))
  9606. (map (copy-keymap calendar-mode-map))
  9607. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9608. (org-defkey map (kbd "RET") 'org-calendar-select)
  9609. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9610. 'org-calendar-select-mouse)
  9611. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9612. 'org-calendar-select-mouse)
  9613. (org-defkey minibuffer-local-map [(meta shift left)]
  9614. (lambda () (interactive)
  9615. (org-eval-in-calendar '(calendar-backward-month 1))))
  9616. (org-defkey minibuffer-local-map [(meta shift right)]
  9617. (lambda () (interactive)
  9618. (org-eval-in-calendar '(calendar-forward-month 1))))
  9619. (org-defkey minibuffer-local-map [(meta shift up)]
  9620. (lambda () (interactive)
  9621. (org-eval-in-calendar '(calendar-backward-year 1))))
  9622. (org-defkey minibuffer-local-map [(meta shift down)]
  9623. (lambda () (interactive)
  9624. (org-eval-in-calendar '(calendar-forward-year 1))))
  9625. (org-defkey minibuffer-local-map [(shift up)]
  9626. (lambda () (interactive)
  9627. (org-eval-in-calendar '(calendar-backward-week 1))))
  9628. (org-defkey minibuffer-local-map [(shift down)]
  9629. (lambda () (interactive)
  9630. (org-eval-in-calendar '(calendar-forward-week 1))))
  9631. (org-defkey minibuffer-local-map [(shift left)]
  9632. (lambda () (interactive)
  9633. (org-eval-in-calendar '(calendar-backward-day 1))))
  9634. (org-defkey minibuffer-local-map [(shift right)]
  9635. (lambda () (interactive)
  9636. (org-eval-in-calendar '(calendar-forward-day 1))))
  9637. (org-defkey minibuffer-local-map ">"
  9638. (lambda () (interactive)
  9639. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9640. (org-defkey minibuffer-local-map "<"
  9641. (lambda () (interactive)
  9642. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9643. (unwind-protect
  9644. (progn
  9645. (use-local-map map)
  9646. (add-hook 'post-command-hook 'org-read-date-display)
  9647. (setq org-ans0 (read-string prompt default-input nil nil))
  9648. ;; org-ans0: from prompt
  9649. ;; org-ans1: from mouse click
  9650. ;; org-ans2: from calendar motion
  9651. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9652. (remove-hook 'post-command-hook 'org-read-date-display)
  9653. (use-local-map old-map)
  9654. (when org-read-date-overlay
  9655. (org-delete-overlay org-read-date-overlay)
  9656. (setq org-read-date-overlay nil)))))))
  9657. (t ; Naked prompt only
  9658. (unwind-protect
  9659. (setq ans (read-string prompt default-input nil timestr))
  9660. (when org-read-date-overlay
  9661. (org-delete-overlay org-read-date-overlay)
  9662. (setq org-read-date-overlay nil)))))
  9663. (setq final (org-read-date-analyze ans def defdecode))
  9664. (if to-time
  9665. (apply 'encode-time final)
  9666. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9667. (format "%04d-%02d-%02d %02d:%02d"
  9668. (nth 5 final) (nth 4 final) (nth 3 final)
  9669. (nth 2 final) (nth 1 final))
  9670. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9671. (defvar def)
  9672. (defvar defdecode)
  9673. (defvar with-time)
  9674. (defun org-read-date-display ()
  9675. "Display the currrent date prompt interpretation in the minibuffer."
  9676. (when org-read-date-display-live
  9677. (when org-read-date-overlay
  9678. (org-delete-overlay org-read-date-overlay))
  9679. (let ((p (point)))
  9680. (end-of-line 1)
  9681. (while (not (equal (buffer-substring
  9682. (max (point-min) (- (point) 4)) (point))
  9683. " "))
  9684. (insert " "))
  9685. (goto-char p))
  9686. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9687. " " (or org-ans1 org-ans2)))
  9688. (org-end-time-was-given nil)
  9689. (f (org-read-date-analyze ans def defdecode))
  9690. (fmts (if org-dcst
  9691. org-time-stamp-custom-formats
  9692. org-time-stamp-formats))
  9693. (fmt (if (or with-time
  9694. (and (boundp 'org-time-was-given) org-time-was-given))
  9695. (cdr fmts)
  9696. (car fmts)))
  9697. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9698. (when (and org-end-time-was-given
  9699. (string-match org-plain-time-of-day-regexp txt))
  9700. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9701. org-end-time-was-given
  9702. (substring txt (match-end 0)))))
  9703. (setq org-read-date-overlay
  9704. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9705. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9706. (defun org-read-date-analyze (ans def defdecode)
  9707. "Analyze the combined answer of the date prompt."
  9708. ;; FIXME: cleanup and comment
  9709. (let (delta deltan deltaw deltadef year month day
  9710. hour minute second wday pm h2 m2 tl wday1
  9711. iso-year iso-weekday iso-week iso-year iso-date)
  9712. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9713. (setq ans "+0"))
  9714. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9715. (setq ans (replace-match "" t t ans)
  9716. deltan (car delta)
  9717. deltaw (nth 1 delta)
  9718. deltadef (nth 2 delta)))
  9719. ;; Check if there is an iso week date in there
  9720. ;; If yes, sore the info and ostpone interpreting it until the rest
  9721. ;; of the parsing is done
  9722. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9723. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9724. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9725. iso-week (string-to-number (match-string 2 ans)))
  9726. (setq ans (replace-match "" t t ans)))
  9727. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9728. (when (string-match
  9729. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9730. (setq year (if (match-end 2)
  9731. (string-to-number (match-string 2 ans))
  9732. (string-to-number (format-time-string "%Y")))
  9733. month (string-to-number (match-string 3 ans))
  9734. day (string-to-number (match-string 4 ans)))
  9735. (if (< year 100) (setq year (+ 2000 year)))
  9736. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9737. t nil ans)))
  9738. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9739. ;; If there is a time with am/pm, and *no* time without it, we convert
  9740. ;; so that matching will be successful.
  9741. (loop for i from 1 to 2 do ; twice, for end time as well
  9742. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9743. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9744. (setq hour (string-to-number (match-string 1 ans))
  9745. minute (if (match-end 3)
  9746. (string-to-number (match-string 3 ans))
  9747. 0)
  9748. pm (equal ?p
  9749. (string-to-char (downcase (match-string 4 ans)))))
  9750. (if (and (= hour 12) (not pm))
  9751. (setq hour 0)
  9752. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9753. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9754. t t ans))))
  9755. ;; Check if a time range is given as a duration
  9756. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9757. (setq hour (string-to-number (match-string 1 ans))
  9758. h2 (+ hour (string-to-number (match-string 3 ans)))
  9759. minute (string-to-number (match-string 2 ans))
  9760. m2 (+ minute (if (match-end 5) (string-to-number
  9761. (match-string 5 ans))0)))
  9762. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9763. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9764. t t ans)))
  9765. ;; Check if there is a time range
  9766. (when (boundp 'org-end-time-was-given)
  9767. (setq org-time-was-given nil)
  9768. (when (and (string-match org-plain-time-of-day-regexp ans)
  9769. (match-end 8))
  9770. (setq org-end-time-was-given (match-string 8 ans))
  9771. (setq ans (concat (substring ans 0 (match-beginning 7))
  9772. (substring ans (match-end 7))))))
  9773. (setq tl (parse-time-string ans)
  9774. day (or (nth 3 tl) (nth 3 defdecode))
  9775. month (or (nth 4 tl)
  9776. (if (and org-read-date-prefer-future
  9777. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9778. (1+ (nth 4 defdecode))
  9779. (nth 4 defdecode)))
  9780. year (or (nth 5 tl)
  9781. (if (and org-read-date-prefer-future
  9782. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9783. (1+ (nth 5 defdecode))
  9784. (nth 5 defdecode)))
  9785. hour (or (nth 2 tl) (nth 2 defdecode))
  9786. minute (or (nth 1 tl) (nth 1 defdecode))
  9787. second (or (nth 0 tl) 0)
  9788. wday (nth 6 tl))
  9789. ;; Special date definitions below
  9790. (cond
  9791. (iso-week
  9792. ;; There was an iso week
  9793. (setq year (or iso-year year)
  9794. day (or iso-weekday wday 1)
  9795. wday nil ; to make sure that the trigger below does not match
  9796. iso-date (calendar-gregorian-from-absolute
  9797. (calendar-absolute-from-iso
  9798. (list iso-week day year))))
  9799. ; FIXME: Should we also push ISO weeks into the future?
  9800. ; (when (and org-read-date-prefer-future
  9801. ; (not iso-year)
  9802. ; (< (calendar-absolute-from-gregorian iso-date)
  9803. ; (time-to-days (current-time))))
  9804. ; (setq year (1+ year)
  9805. ; iso-date (calendar-gregorian-from-absolute
  9806. ; (calendar-absolute-from-iso
  9807. ; (list iso-week day year)))))
  9808. (setq month (car iso-date)
  9809. year (nth 2 iso-date)
  9810. day (nth 1 iso-date)))
  9811. (deltan
  9812. (unless deltadef
  9813. (let ((now (decode-time (current-time))))
  9814. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9815. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9816. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9817. ((equal deltaw "m") (setq month (+ month deltan)))
  9818. ((equal deltaw "y") (setq year (+ year deltan)))))
  9819. ((and wday (not (nth 3 tl)))
  9820. ;; Weekday was given, but no day, so pick that day in the week
  9821. ;; on or after the derived date.
  9822. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9823. (unless (equal wday wday1)
  9824. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9825. (if (and (boundp 'org-time-was-given)
  9826. (nth 2 tl))
  9827. (setq org-time-was-given t))
  9828. (if (< year 100) (setq year (+ 2000 year)))
  9829. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9830. (list second minute hour day month year)))
  9831. (defvar parse-time-weekdays)
  9832. (defun org-read-date-get-relative (s today default)
  9833. "Check string S for special relative date string.
  9834. TODAY and DEFAULT are internal times, for today and for a default.
  9835. Return shift list (N what def-flag)
  9836. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9837. N is the number of WHATs to shift.
  9838. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9839. the DEFAULT date rather than TODAY."
  9840. (when (and
  9841. (string-match
  9842. (concat
  9843. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9844. "\\([0-9]+\\)?"
  9845. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9846. "\\([ \t]\\|$\\)") s)
  9847. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9848. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9849. (string-to-char (substring (match-string 1 s) -1))
  9850. ?+))
  9851. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9852. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9853. (what (if (match-end 3) (match-string 3 s) "d"))
  9854. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9855. (date (if rel default today))
  9856. (wday (nth 6 (decode-time date)))
  9857. delta)
  9858. (if wday1
  9859. (progn
  9860. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9861. (if (= dir ?-) (setq delta (- delta 7)))
  9862. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9863. (list delta "d" rel))
  9864. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9865. (defun org-eval-in-calendar (form &optional keepdate)
  9866. "Eval FORM in the calendar window and return to current window.
  9867. Also, store the cursor date in variable org-ans2."
  9868. (let ((sw (selected-window)))
  9869. (select-window (get-buffer-window "*Calendar*"))
  9870. (eval form)
  9871. (when (and (not keepdate) (calendar-cursor-to-date))
  9872. (let* ((date (calendar-cursor-to-date))
  9873. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9874. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9875. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9876. (select-window sw)))
  9877. ; ;; Update the prompt to show new default date
  9878. ; (save-excursion
  9879. ; (goto-char (point-min))
  9880. ; (when (and org-ans2
  9881. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9882. ; (get-text-property (match-end 0) 'field))
  9883. ; (let ((inhibit-read-only t))
  9884. ; (replace-match (concat "[" org-ans2 "]") t t)
  9885. ; (add-text-properties (point-min) (1+ (match-end 0))
  9886. ; (text-properties-at (1+ (point-min)))))))))
  9887. (defun org-calendar-select ()
  9888. "Return to `org-read-date' with the date currently selected.
  9889. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9890. (interactive)
  9891. (when (calendar-cursor-to-date)
  9892. (let* ((date (calendar-cursor-to-date))
  9893. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9894. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9895. (if (active-minibuffer-window) (exit-minibuffer))))
  9896. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9897. "Insert a date stamp for the date given by the internal TIME.
  9898. WITH-HM means, use the stamp format that includes the time of the day.
  9899. INACTIVE means use square brackets instead of angular ones, so that the
  9900. stamp will not contribute to the agenda.
  9901. PRE and POST are optional strings to be inserted before and after the
  9902. stamp.
  9903. The command returns the inserted time stamp."
  9904. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9905. stamp)
  9906. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9907. (insert-before-markers (or pre ""))
  9908. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9909. (when (listp extra)
  9910. (setq extra (car extra))
  9911. (if (and (stringp extra)
  9912. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9913. (setq extra (format "-%02d:%02d"
  9914. (string-to-number (match-string 1 extra))
  9915. (string-to-number (match-string 2 extra))))
  9916. (setq extra nil)))
  9917. (when extra
  9918. (backward-char 1)
  9919. (insert-before-markers extra)
  9920. (forward-char 1))
  9921. (insert-before-markers (or post ""))
  9922. stamp))
  9923. (defun org-toggle-time-stamp-overlays ()
  9924. "Toggle the use of custom time stamp formats."
  9925. (interactive)
  9926. (setq org-display-custom-times (not org-display-custom-times))
  9927. (unless org-display-custom-times
  9928. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9929. (while (setq p (next-single-property-change p 'display))
  9930. (if (and (get-text-property p 'display)
  9931. (eq (get-text-property p 'face) 'org-date))
  9932. (remove-text-properties
  9933. p (setq p (next-single-property-change p 'display))
  9934. '(display t))))
  9935. (set-buffer-modified-p bmp)))
  9936. (if (featurep 'xemacs)
  9937. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9938. (org-restart-font-lock)
  9939. (setq org-table-may-need-update t)
  9940. (if org-display-custom-times
  9941. (message "Time stamps are overlayed with custom format")
  9942. (message "Time stamp overlays removed")))
  9943. (defun org-display-custom-time (beg end)
  9944. "Overlay modified time stamp format over timestamp between BEG and END."
  9945. (let* ((ts (buffer-substring beg end))
  9946. t1 w1 with-hm tf time str w2 (off 0))
  9947. (save-match-data
  9948. (setq t1 (org-parse-time-string ts t))
  9949. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9950. (setq off (- (match-end 0) (match-beginning 0)))))
  9951. (setq end (- end off))
  9952. (setq w1 (- end beg)
  9953. with-hm (and (nth 1 t1) (nth 2 t1))
  9954. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9955. time (org-fix-decoded-time t1)
  9956. str (org-add-props
  9957. (format-time-string
  9958. (substring tf 1 -1) (apply 'encode-time time))
  9959. nil 'mouse-face 'highlight)
  9960. w2 (length str))
  9961. (if (not (= w2 w1))
  9962. (add-text-properties (1+ beg) (+ 2 beg)
  9963. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9964. (if (featurep 'xemacs)
  9965. (progn
  9966. (put-text-property beg end 'invisible t)
  9967. (put-text-property beg end 'end-glyph (make-glyph str)))
  9968. (put-text-property beg end 'display str))))
  9969. (defun org-translate-time (string)
  9970. "Translate all timestamps in STRING to custom format.
  9971. But do this only if the variable `org-display-custom-times' is set."
  9972. (when org-display-custom-times
  9973. (save-match-data
  9974. (let* ((start 0)
  9975. (re org-ts-regexp-both)
  9976. t1 with-hm inactive tf time str beg end)
  9977. (while (setq start (string-match re string start))
  9978. (setq beg (match-beginning 0)
  9979. end (match-end 0)
  9980. t1 (save-match-data
  9981. (org-parse-time-string (substring string beg end) t))
  9982. with-hm (and (nth 1 t1) (nth 2 t1))
  9983. inactive (equal (substring string beg (1+ beg)) "[")
  9984. tf (funcall (if with-hm 'cdr 'car)
  9985. org-time-stamp-custom-formats)
  9986. time (org-fix-decoded-time t1)
  9987. str (format-time-string
  9988. (concat
  9989. (if inactive "[" "<") (substring tf 1 -1)
  9990. (if inactive "]" ">"))
  9991. (apply 'encode-time time))
  9992. string (replace-match str t t string)
  9993. start (+ start (length str)))))))
  9994. string)
  9995. (defun org-fix-decoded-time (time)
  9996. "Set 0 instead of nil for the first 6 elements of time.
  9997. Don't touch the rest."
  9998. (let ((n 0))
  9999. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10000. (defun org-days-to-time (timestamp-string)
  10001. "Difference between TIMESTAMP-STRING and now in days."
  10002. (- (time-to-days (org-time-string-to-time timestamp-string))
  10003. (time-to-days (current-time))))
  10004. (defun org-deadline-close (timestamp-string &optional ndays)
  10005. "Is the time in TIMESTAMP-STRING close to the current date?"
  10006. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10007. (and (< (org-days-to-time timestamp-string) ndays)
  10008. (not (org-entry-is-done-p))))
  10009. (defun org-get-wdays (ts)
  10010. "Get the deadline lead time appropriate for timestring TS."
  10011. (cond
  10012. ((<= org-deadline-warning-days 0)
  10013. ;; 0 or negative, enforce this value no matter what
  10014. (- org-deadline-warning-days))
  10015. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10016. ;; lead time is specified.
  10017. (floor (* (string-to-number (match-string 1 ts))
  10018. (cdr (assoc (match-string 2 ts)
  10019. '(("d" . 1) ("w" . 7)
  10020. ("m" . 30.4) ("y" . 365.25)))))))
  10021. ;; go for the default.
  10022. (t org-deadline-warning-days)))
  10023. (defun org-calendar-select-mouse (ev)
  10024. "Return to `org-read-date' with the date currently selected.
  10025. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10026. (interactive "e")
  10027. (mouse-set-point ev)
  10028. (when (calendar-cursor-to-date)
  10029. (let* ((date (calendar-cursor-to-date))
  10030. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10031. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10032. (if (active-minibuffer-window) (exit-minibuffer))))
  10033. (defun org-check-deadlines (ndays)
  10034. "Check if there are any deadlines due or past due.
  10035. A deadline is considered due if it happens within `org-deadline-warning-days'
  10036. days from today's date. If the deadline appears in an entry marked DONE,
  10037. it is not shown. The prefix arg NDAYS can be used to test that many
  10038. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10039. (interactive "P")
  10040. (let* ((org-warn-days
  10041. (cond
  10042. ((equal ndays '(4)) 100000)
  10043. (ndays (prefix-numeric-value ndays))
  10044. (t (abs org-deadline-warning-days))))
  10045. (case-fold-search nil)
  10046. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10047. (callback
  10048. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10049. (message "%d deadlines past-due or due within %d days"
  10050. (org-occur regexp nil callback)
  10051. org-warn-days)))
  10052. (defun org-check-before-date (date)
  10053. "Check if there are deadlines or scheduled entries before DATE."
  10054. (interactive (list (org-read-date)))
  10055. (let ((case-fold-search nil)
  10056. (regexp (concat "\\<\\(" org-deadline-string
  10057. "\\|" org-scheduled-string
  10058. "\\) *<\\([^>]+\\)>"))
  10059. (callback
  10060. (lambda () (time-less-p
  10061. (org-time-string-to-time (match-string 2))
  10062. (org-time-string-to-time date)))))
  10063. (message "%d entries before %s"
  10064. (org-occur regexp nil callback) date)))
  10065. (defun org-evaluate-time-range (&optional to-buffer)
  10066. "Evaluate a time range by computing the difference between start and end.
  10067. Normally the result is just printed in the echo area, but with prefix arg
  10068. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10069. If the time range is actually in a table, the result is inserted into the
  10070. next column.
  10071. For time difference computation, a year is assumed to be exactly 365
  10072. days in order to avoid rounding problems."
  10073. (interactive "P")
  10074. (or
  10075. (org-clock-update-time-maybe)
  10076. (save-excursion
  10077. (unless (org-at-date-range-p t)
  10078. (goto-char (point-at-bol))
  10079. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10080. (if (not (org-at-date-range-p t))
  10081. (error "Not at a time-stamp range, and none found in current line")))
  10082. (let* ((ts1 (match-string 1))
  10083. (ts2 (match-string 2))
  10084. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10085. (match-end (match-end 0))
  10086. (time1 (org-time-string-to-time ts1))
  10087. (time2 (org-time-string-to-time ts2))
  10088. (t1 (time-to-seconds time1))
  10089. (t2 (time-to-seconds time2))
  10090. (diff (abs (- t2 t1)))
  10091. (negative (< (- t2 t1) 0))
  10092. ;; (ys (floor (* 365 24 60 60)))
  10093. (ds (* 24 60 60))
  10094. (hs (* 60 60))
  10095. (fy "%dy %dd %02d:%02d")
  10096. (fy1 "%dy %dd")
  10097. (fd "%dd %02d:%02d")
  10098. (fd1 "%dd")
  10099. (fh "%02d:%02d")
  10100. y d h m align)
  10101. (if havetime
  10102. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10103. y 0
  10104. d (floor (/ diff ds)) diff (mod diff ds)
  10105. h (floor (/ diff hs)) diff (mod diff hs)
  10106. m (floor (/ diff 60)))
  10107. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10108. y 0
  10109. d (floor (+ (/ diff ds) 0.5))
  10110. h 0 m 0))
  10111. (if (not to-buffer)
  10112. (message "%s" (org-make-tdiff-string y d h m))
  10113. (if (org-at-table-p)
  10114. (progn
  10115. (goto-char match-end)
  10116. (setq align t)
  10117. (and (looking-at " *|") (goto-char (match-end 0))))
  10118. (goto-char match-end))
  10119. (if (looking-at
  10120. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10121. (replace-match ""))
  10122. (if negative (insert " -"))
  10123. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10124. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10125. (insert " " (format fh h m))))
  10126. (if align (org-table-align))
  10127. (message "Time difference inserted")))))
  10128. (defun org-make-tdiff-string (y d h m)
  10129. (let ((fmt "")
  10130. (l nil))
  10131. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10132. l (push y l)))
  10133. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10134. l (push d l)))
  10135. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10136. l (push h l)))
  10137. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10138. l (push m l)))
  10139. (apply 'format fmt (nreverse l))))
  10140. (defun org-time-string-to-time (s)
  10141. (apply 'encode-time (org-parse-time-string s)))
  10142. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10143. "Convert a time stamp to an absolute day number.
  10144. If there is a specifyer for a cyclic time stamp, get the closest date to
  10145. DAYNR.
  10146. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10147. (cond
  10148. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10149. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10150. daynr
  10151. (+ daynr 1000)))
  10152. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10153. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10154. (time-to-days (current-time))) (match-string 0 s)
  10155. prefer show-all))
  10156. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10157. (defun org-days-to-iso-week (days)
  10158. "Return the iso week number."
  10159. (require 'cal-iso)
  10160. (car (calendar-iso-from-absolute days)))
  10161. (defun org-small-year-to-year (year)
  10162. "Convert 2-digit years into 4-digit years.
  10163. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10164. The year 2000 cannot be abbreviated. Any year lager than 99
  10165. is retrned unchanged."
  10166. (if (< year 38)
  10167. (setq year (+ 2000 year))
  10168. (if (< year 100)
  10169. (setq year (+ 1900 year))))
  10170. year)
  10171. (defun org-time-from-absolute (d)
  10172. "Return the time corresponding to date D.
  10173. D may be an absolute day number, or a calendar-type list (month day year)."
  10174. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10175. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10176. (defun org-calendar-holiday ()
  10177. "List of holidays, for Diary display in Org-mode."
  10178. (require 'holidays)
  10179. (let ((hl (funcall
  10180. (if (fboundp 'calendar-check-holidays)
  10181. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10182. (if hl (mapconcat 'identity hl "; "))))
  10183. (defun org-diary-sexp-entry (sexp entry date)
  10184. "Process a SEXP diary ENTRY for DATE."
  10185. (require 'diary-lib)
  10186. (let ((result (if calendar-debug-sexp
  10187. (let ((stack-trace-on-error t))
  10188. (eval (car (read-from-string sexp))))
  10189. (condition-case nil
  10190. (eval (car (read-from-string sexp)))
  10191. (error
  10192. (beep)
  10193. (message "Bad sexp at line %d in %s: %s"
  10194. (org-current-line)
  10195. (buffer-file-name) sexp)
  10196. (sleep-for 2))))))
  10197. (cond ((stringp result) result)
  10198. ((and (consp result)
  10199. (stringp (cdr result))) (cdr result))
  10200. (result entry)
  10201. (t nil))))
  10202. (defun org-diary-to-ical-string (frombuf)
  10203. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10204. This uses the icalendar.el library."
  10205. (let* ((tmpdir (if (featurep 'xemacs)
  10206. (temp-directory)
  10207. temporary-file-directory))
  10208. (tmpfile (make-temp-name
  10209. (expand-file-name "orgics" tmpdir)))
  10210. buf rtn b e)
  10211. (save-excursion
  10212. (set-buffer frombuf)
  10213. (icalendar-export-region (point-min) (point-max) tmpfile)
  10214. (setq buf (find-buffer-visiting tmpfile))
  10215. (set-buffer buf)
  10216. (goto-char (point-min))
  10217. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10218. (setq b (match-beginning 0)))
  10219. (goto-char (point-max))
  10220. (if (re-search-backward "^END:VEVENT" nil t)
  10221. (setq e (match-end 0)))
  10222. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10223. (kill-buffer buf)
  10224. (kill-buffer frombuf)
  10225. (delete-file tmpfile)
  10226. rtn))
  10227. (defun org-closest-date (start current change prefer show-all)
  10228. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10229. When PREFER is `past' return a date that is either CURRENT or past.
  10230. When PREFER is `future', return a date that is either CURRENT or future.
  10231. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10232. ;; Make the proper lists from the dates
  10233. (catch 'exit
  10234. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10235. dn dw sday cday n1 n2
  10236. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10237. (setq start (org-date-to-gregorian start)
  10238. current (org-date-to-gregorian
  10239. (if show-all
  10240. current
  10241. (time-to-days (current-time))))
  10242. sday (calendar-absolute-from-gregorian start)
  10243. cday (calendar-absolute-from-gregorian current))
  10244. (if (<= cday sday) (throw 'exit sday))
  10245. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10246. (setq dn (string-to-number (match-string 1 change))
  10247. dw (cdr (assoc (match-string 2 change) a1)))
  10248. (error "Invalid change specifyer: %s" change))
  10249. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10250. (cond
  10251. ((eq dw 'day)
  10252. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10253. n2 (+ n1 dn)))
  10254. ((eq dw 'year)
  10255. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10256. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10257. (setq date1 (list m d y1)
  10258. n1 (calendar-absolute-from-gregorian date1)
  10259. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10260. n2 (calendar-absolute-from-gregorian date2)))
  10261. ((eq dw 'month)
  10262. ;; approx number of month between the tow dates
  10263. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10264. ;; How often does dn fit in there?
  10265. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10266. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10267. m (+ m nm)
  10268. ny (floor (/ m 12))
  10269. y (+ y ny)
  10270. m (- m (* ny 12)))
  10271. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10272. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10273. (setq m2 (+ m dn) y2 y)
  10274. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10275. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10276. (while (< n2 cday)
  10277. (setq n1 n2 m m2 y y2)
  10278. (setq m2 (+ m dn) y2 y)
  10279. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10280. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10281. (if show-all
  10282. (cond
  10283. ((eq prefer 'past) n1)
  10284. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10285. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10286. (cond
  10287. ((eq prefer 'past) n1)
  10288. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10289. (t (if (= cday n1) n1 n2)))))))
  10290. (defun org-date-to-gregorian (date)
  10291. "Turn any specification of DATE into a gregorian date for the calendar."
  10292. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10293. ((and (listp date) (= (length date) 3)) date)
  10294. ((stringp date)
  10295. (setq date (org-parse-time-string date))
  10296. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10297. ((listp date)
  10298. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10299. (defun org-parse-time-string (s &optional nodefault)
  10300. "Parse the standard Org-mode time string.
  10301. This should be a lot faster than the normal `parse-time-string'.
  10302. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10303. hour and minute fields will be nil if not given."
  10304. (if (string-match org-ts-regexp0 s)
  10305. (list 0
  10306. (if (or (match-beginning 8) (not nodefault))
  10307. (string-to-number (or (match-string 8 s) "0")))
  10308. (if (or (match-beginning 7) (not nodefault))
  10309. (string-to-number (or (match-string 7 s) "0")))
  10310. (string-to-number (match-string 4 s))
  10311. (string-to-number (match-string 3 s))
  10312. (string-to-number (match-string 2 s))
  10313. nil nil nil)
  10314. (make-list 9 0)))
  10315. (defun org-timestamp-up (&optional arg)
  10316. "Increase the date item at the cursor by one.
  10317. If the cursor is on the year, change the year. If it is on the month or
  10318. the day, change that.
  10319. With prefix ARG, change by that many units."
  10320. (interactive "p")
  10321. (org-timestamp-change (prefix-numeric-value arg)))
  10322. (defun org-timestamp-down (&optional arg)
  10323. "Decrease the date item at the cursor by one.
  10324. If the cursor is on the year, change the year. If it is on the month or
  10325. the day, change that.
  10326. With prefix ARG, change by that many units."
  10327. (interactive "p")
  10328. (org-timestamp-change (- (prefix-numeric-value arg))))
  10329. (defun org-timestamp-up-day (&optional arg)
  10330. "Increase the date in the time stamp by one day.
  10331. With prefix ARG, change that many days."
  10332. (interactive "p")
  10333. (if (and (not (org-at-timestamp-p t))
  10334. (org-on-heading-p))
  10335. (org-todo 'up)
  10336. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10337. (defun org-timestamp-down-day (&optional arg)
  10338. "Decrease the date in the time stamp by one day.
  10339. With prefix ARG, change that many days."
  10340. (interactive "p")
  10341. (if (and (not (org-at-timestamp-p t))
  10342. (org-on-heading-p))
  10343. (org-todo 'down)
  10344. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10345. (defun org-at-timestamp-p (&optional inactive-ok)
  10346. "Determine if the cursor is in or at a timestamp."
  10347. (interactive)
  10348. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10349. (pos (point))
  10350. (ans (or (looking-at tsr)
  10351. (save-excursion
  10352. (skip-chars-backward "^[<\n\r\t")
  10353. (if (> (point) (point-min)) (backward-char 1))
  10354. (and (looking-at tsr)
  10355. (> (- (match-end 0) pos) -1))))))
  10356. (and ans
  10357. (boundp 'org-ts-what)
  10358. (setq org-ts-what
  10359. (cond
  10360. ((= pos (match-beginning 0)) 'bracket)
  10361. ((= pos (1- (match-end 0))) 'bracket)
  10362. ((org-pos-in-match-range pos 2) 'year)
  10363. ((org-pos-in-match-range pos 3) 'month)
  10364. ((org-pos-in-match-range pos 7) 'hour)
  10365. ((org-pos-in-match-range pos 8) 'minute)
  10366. ((or (org-pos-in-match-range pos 4)
  10367. (org-pos-in-match-range pos 5)) 'day)
  10368. ((and (> pos (or (match-end 8) (match-end 5)))
  10369. (< pos (match-end 0)))
  10370. (- pos (or (match-end 8) (match-end 5))))
  10371. (t 'day))))
  10372. ans))
  10373. (defun org-toggle-timestamp-type ()
  10374. "Toggle the type (<active> or [inactive]) of a time stamp."
  10375. (interactive)
  10376. (when (org-at-timestamp-p t)
  10377. (save-excursion
  10378. (goto-char (match-beginning 0))
  10379. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10380. (goto-char (1- (match-end 0)))
  10381. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10382. (message "Timestamp is now %sactive"
  10383. (if (equal (char-before) ?>) "in" ""))))
  10384. (defun org-timestamp-change (n &optional what)
  10385. "Change the date in the time stamp at point.
  10386. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10387. `year', `minute', `second'. If WHAT is not given, the cursor position
  10388. in the timestamp determines what will be changed."
  10389. (let ((pos (point))
  10390. with-hm inactive
  10391. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10392. org-ts-what
  10393. extra rem
  10394. ts time time0)
  10395. (if (not (org-at-timestamp-p t))
  10396. (error "Not at a timestamp"))
  10397. (if (and (not what) (eq org-ts-what 'bracket))
  10398. (org-toggle-timestamp-type)
  10399. (if (and (not what) (not (eq org-ts-what 'day))
  10400. org-display-custom-times
  10401. (get-text-property (point) 'display)
  10402. (not (get-text-property (1- (point)) 'display)))
  10403. (setq org-ts-what 'day))
  10404. (setq org-ts-what (or what org-ts-what)
  10405. inactive (= (char-after (match-beginning 0)) ?\[)
  10406. ts (match-string 0))
  10407. (replace-match "")
  10408. (if (string-match
  10409. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10410. ts)
  10411. (setq extra (match-string 1 ts)))
  10412. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10413. (setq with-hm t))
  10414. (setq time0 (org-parse-time-string ts))
  10415. (when (and (eq org-ts-what 'minute)
  10416. (eq current-prefix-arg nil))
  10417. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10418. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10419. (setcar (cdr time0) (+ (nth 1 time0)
  10420. (if (> n 0) (- rem) (- dm rem))))))
  10421. (setq time
  10422. (encode-time (or (car time0) 0)
  10423. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10424. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10425. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10426. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10427. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10428. (nthcdr 6 time0)))
  10429. (when (integerp org-ts-what)
  10430. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10431. (if (eq what 'calendar)
  10432. (let ((cal-date (org-get-date-from-calendar)))
  10433. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10434. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10435. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10436. (setcar time0 (or (car time0) 0))
  10437. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10438. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10439. (setq time (apply 'encode-time time0))))
  10440. (setq org-last-changed-timestamp
  10441. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10442. (org-clock-update-time-maybe)
  10443. (goto-char pos)
  10444. ;; Try to recenter the calendar window, if any
  10445. (if (and org-calendar-follow-timestamp-change
  10446. (get-buffer-window "*Calendar*" t)
  10447. (memq org-ts-what '(day month year)))
  10448. (org-recenter-calendar (time-to-days time))))))
  10449. (defun org-modify-ts-extra (s pos n dm)
  10450. "Change the different parts of the lead-time and repeat fields in timestamp."
  10451. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10452. ng h m new rem)
  10453. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10454. (cond
  10455. ((or (org-pos-in-match-range pos 2)
  10456. (org-pos-in-match-range pos 3))
  10457. (setq m (string-to-number (match-string 3 s))
  10458. h (string-to-number (match-string 2 s)))
  10459. (if (org-pos-in-match-range pos 2)
  10460. (setq h (+ h n))
  10461. (setq n (* dm (org-no-warnings (signum n))))
  10462. (when (not (= 0 (setq rem (% m dm))))
  10463. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10464. (setq m (+ m n)))
  10465. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10466. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10467. (setq h (min 24 (max 0 h)))
  10468. (setq ng 1 new (format "-%02d:%02d" h m)))
  10469. ((org-pos-in-match-range pos 6)
  10470. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10471. ((org-pos-in-match-range pos 5)
  10472. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10473. ((org-pos-in-match-range pos 9)
  10474. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10475. ((org-pos-in-match-range pos 8)
  10476. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10477. (when ng
  10478. (setq s (concat
  10479. (substring s 0 (match-beginning ng))
  10480. new
  10481. (substring s (match-end ng))))))
  10482. s))
  10483. (defun org-recenter-calendar (date)
  10484. "If the calendar is visible, recenter it to DATE."
  10485. (let* ((win (selected-window))
  10486. (cwin (get-buffer-window "*Calendar*" t))
  10487. (calendar-move-hook nil))
  10488. (when cwin
  10489. (select-window cwin)
  10490. (calendar-goto-date (if (listp date) date
  10491. (calendar-gregorian-from-absolute date)))
  10492. (select-window win))))
  10493. (defun org-goto-calendar (&optional arg)
  10494. "Go to the Emacs calendar at the current date.
  10495. If there is a time stamp in the current line, go to that date.
  10496. A prefix ARG can be used to force the current date."
  10497. (interactive "P")
  10498. (let ((tsr org-ts-regexp) diff
  10499. (calendar-move-hook nil)
  10500. (calendar-view-holidays-initially-flag nil)
  10501. (view-calendar-holidays-initially nil)
  10502. (calendar-view-diary-initially-flag nil)
  10503. (view-diary-entries-initially nil))
  10504. (if (or (org-at-timestamp-p)
  10505. (save-excursion
  10506. (beginning-of-line 1)
  10507. (looking-at (concat ".*" tsr))))
  10508. (let ((d1 (time-to-days (current-time)))
  10509. (d2 (time-to-days
  10510. (org-time-string-to-time (match-string 1)))))
  10511. (setq diff (- d2 d1))))
  10512. (calendar)
  10513. (calendar-goto-today)
  10514. (if (and diff (not arg)) (calendar-forward-day diff))))
  10515. (defun org-get-date-from-calendar ()
  10516. "Return a list (month day year) of date at point in calendar."
  10517. (with-current-buffer "*Calendar*"
  10518. (save-match-data
  10519. (calendar-cursor-to-date))))
  10520. (defun org-date-from-calendar ()
  10521. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10522. If there is already a time stamp at the cursor position, update it."
  10523. (interactive)
  10524. (if (org-at-timestamp-p t)
  10525. (org-timestamp-change 0 'calendar)
  10526. (let ((cal-date (org-get-date-from-calendar)))
  10527. (org-insert-time-stamp
  10528. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10529. (defun org-minutes-to-hh:mm-string (m)
  10530. "Compute H:MM from a number of minutes."
  10531. (let ((h (/ m 60)))
  10532. (setq m (- m (* 60 h)))
  10533. (format org-time-clocksum-format h m)))
  10534. (defun org-hh:mm-string-to-minutes (s)
  10535. "Convert a string H:MM to a number of minutes."
  10536. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10537. (+ (* (string-to-number (match-string 1 s)) 60)
  10538. (string-to-number (match-string 2 s)))
  10539. 0))
  10540. ;;;; Agenda files
  10541. ;;;###autoload
  10542. (defun org-iswitchb (&optional arg)
  10543. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10544. With a prefix argument, restrict available to files.
  10545. With two prefix arguments, restrict available buffers to agenda files.
  10546. Due to some yet unresolved reason, global function
  10547. `iswitchb-mode' needs to be active for this function to work."
  10548. (interactive "P")
  10549. (require 'iswitchb)
  10550. (let ((enabled iswitchb-mode) blist)
  10551. (or enabled (iswitchb-mode 1))
  10552. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10553. ((equal arg '(16)) (org-buffer-list 'agenda))
  10554. (t (org-buffer-list))))
  10555. (unwind-protect
  10556. (let ((iswitchb-make-buflist-hook
  10557. (lambda ()
  10558. (setq iswitchb-temp-buflist
  10559. (mapcar 'buffer-name blist)))))
  10560. (switch-to-buffer
  10561. (iswitchb-read-buffer
  10562. "Switch-to: " nil t))
  10563. (or enabled (iswitchb-mode -1))))))
  10564. (defun org-buffer-list (&optional predicate tmp)
  10565. "Return a list of Org buffers.
  10566. PREDICATE can be either 'export, 'files or 'agenda.
  10567. 'export restrict the list to Export buffers.
  10568. 'files restrict the list to buffers visiting Org files.
  10569. 'agenda restrict the list to buffers visiting agenda files.
  10570. If TMP is non-nil, don't include temporary buffers."
  10571. (let (filter blist)
  10572. (setq filter
  10573. (cond ((eq predicate 'files) "\.org$")
  10574. ((eq predicate 'export) "\*Org .*Export")
  10575. (t "\*Org \\|\.org$")))
  10576. (setq blist
  10577. (mapcar
  10578. (lambda(b)
  10579. (let ((bname (buffer-name b))
  10580. (bfile (buffer-file-name b)))
  10581. (if (and (string-match filter bname)
  10582. (if (eq predicate 'agenda)
  10583. (member bfile
  10584. (mapcar (lambda(f) (file-truename f))
  10585. org-agenda-files)) t)
  10586. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10587. (buffer-list)))
  10588. (delete nil blist)))
  10589. (defun org-agenda-files (&optional unrestricted ext)
  10590. "Get the list of agenda files.
  10591. Optional UNRESTRICTED means return the full list even if a restriction
  10592. is currently in place.
  10593. When EXT is non-nil, try to add all files that are created by adding EXT
  10594. to the file nemes. Basically, this is a way to add the archive files
  10595. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10596. a string, \"_archive\" will be used."
  10597. (let ((files
  10598. (cond
  10599. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10600. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10601. ((listp org-agenda-files) org-agenda-files)
  10602. (t (error "Invalid value of `org-agenda-files'")))))
  10603. (setq files (apply 'append
  10604. (mapcar (lambda (f)
  10605. (if (file-directory-p f)
  10606. (directory-files
  10607. f t org-agenda-file-regexp)
  10608. (list f)))
  10609. files)))
  10610. (when org-agenda-skip-unavailable-files
  10611. (setq files (delq nil
  10612. (mapcar (function
  10613. (lambda (file)
  10614. (and (file-readable-p file) file)))
  10615. files))))
  10616. (when ext
  10617. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10618. ext "_archive"))
  10619. (setq files (apply 'append
  10620. (mapcar
  10621. (lambda (f)
  10622. (if (file-exists-p (concat f ext))
  10623. (list f (concat f ext))
  10624. (list f)))
  10625. files))))
  10626. files))
  10627. (defun org-edit-agenda-file-list ()
  10628. "Edit the list of agenda files.
  10629. Depending on setup, this either uses customize to edit the variable
  10630. `org-agenda-files', or it visits the file that is holding the list. In the
  10631. latter case, the buffer is set up in a way that saving it automatically kills
  10632. the buffer and restores the previous window configuration."
  10633. (interactive)
  10634. (if (stringp org-agenda-files)
  10635. (let ((cw (current-window-configuration)))
  10636. (find-file org-agenda-files)
  10637. (org-set-local 'org-window-configuration cw)
  10638. (org-add-hook 'after-save-hook
  10639. (lambda ()
  10640. (set-window-configuration
  10641. (prog1 org-window-configuration
  10642. (kill-buffer (current-buffer))))
  10643. (org-install-agenda-files-menu)
  10644. (message "New agenda file list installed"))
  10645. nil 'local)
  10646. (message "%s" (substitute-command-keys
  10647. "Edit list and finish with \\[save-buffer]")))
  10648. (customize-variable 'org-agenda-files)))
  10649. (defun org-store-new-agenda-file-list (list)
  10650. "Set new value for the agenda file list and save it correcly."
  10651. (if (stringp org-agenda-files)
  10652. (let ((f org-agenda-files) b)
  10653. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10654. (with-temp-file f
  10655. (insert (mapconcat 'identity list "\n") "\n")))
  10656. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10657. (setq org-agenda-files list)
  10658. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10659. (defun org-read-agenda-file-list ()
  10660. "Read the list of agenda files from a file."
  10661. (when (file-directory-p org-agenda-files)
  10662. (error "`org-agenda-files' cannot be a single directory"))
  10663. (when (stringp org-agenda-files)
  10664. (with-temp-buffer
  10665. (insert-file-contents org-agenda-files)
  10666. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10667. ;;;###autoload
  10668. (defun org-cycle-agenda-files ()
  10669. "Cycle through the files in `org-agenda-files'.
  10670. If the current buffer visits an agenda file, find the next one in the list.
  10671. If the current buffer does not, find the first agenda file."
  10672. (interactive)
  10673. (let* ((fs (org-agenda-files t))
  10674. (files (append fs (list (car fs))))
  10675. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10676. file)
  10677. (unless files (error "No agenda files"))
  10678. (catch 'exit
  10679. (while (setq file (pop files))
  10680. (if (equal (file-truename file) tcf)
  10681. (when (car files)
  10682. (find-file (car files))
  10683. (throw 'exit t))))
  10684. (find-file (car fs)))
  10685. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10686. (defun org-agenda-file-to-front (&optional to-end)
  10687. "Move/add the current file to the top of the agenda file list.
  10688. If the file is not present in the list, it is added to the front. If it is
  10689. present, it is moved there. With optional argument TO-END, add/move to the
  10690. end of the list."
  10691. (interactive "P")
  10692. (let ((org-agenda-skip-unavailable-files nil)
  10693. (file-alist (mapcar (lambda (x)
  10694. (cons (file-truename x) x))
  10695. (org-agenda-files t)))
  10696. (ctf (file-truename buffer-file-name))
  10697. x had)
  10698. (setq x (assoc ctf file-alist) had x)
  10699. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10700. (if to-end
  10701. (setq file-alist (append (delq x file-alist) (list x)))
  10702. (setq file-alist (cons x (delq x file-alist))))
  10703. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10704. (org-install-agenda-files-menu)
  10705. (message "File %s to %s of agenda file list"
  10706. (if had "moved" "added") (if to-end "end" "front"))))
  10707. (defun org-remove-file (&optional file)
  10708. "Remove current file from the list of files in variable `org-agenda-files'.
  10709. These are the files which are being checked for agenda entries.
  10710. Optional argument FILE means, use this file instead of the current."
  10711. (interactive)
  10712. (let* ((org-agenda-skip-unavailable-files nil)
  10713. (file (or file buffer-file-name))
  10714. (true-file (file-truename file))
  10715. (afile (abbreviate-file-name file))
  10716. (files (delq nil (mapcar
  10717. (lambda (x)
  10718. (if (equal true-file
  10719. (file-truename x))
  10720. nil x))
  10721. (org-agenda-files t)))))
  10722. (if (not (= (length files) (length (org-agenda-files t))))
  10723. (progn
  10724. (org-store-new-agenda-file-list files)
  10725. (org-install-agenda-files-menu)
  10726. (message "Removed file: %s" afile))
  10727. (message "File was not in list: %s (not removed)" afile))))
  10728. (defun org-file-menu-entry (file)
  10729. (vector file (list 'find-file file) t))
  10730. (defun org-check-agenda-file (file)
  10731. "Make sure FILE exists. If not, ask user what to do."
  10732. (when (not (file-exists-p file))
  10733. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10734. (abbreviate-file-name file))
  10735. (let ((r (downcase (read-char-exclusive))))
  10736. (cond
  10737. ((equal r ?r)
  10738. (org-remove-file file)
  10739. (throw 'nextfile t))
  10740. (t (error "Abort"))))))
  10741. (defun org-get-agenda-file-buffer (file)
  10742. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10743. it to the list of buffers which might be released later."
  10744. (let ((buf (org-find-base-buffer-visiting file)))
  10745. (if buf
  10746. buf ; just return it
  10747. ;; Make a new buffer and remember it
  10748. (setq buf (find-file-noselect file))
  10749. (if buf (push buf org-agenda-new-buffers))
  10750. buf)))
  10751. (defun org-release-buffers (blist)
  10752. "Release all buffers in list, asking the user for confirmation when needed.
  10753. When a buffer is unmodified, it is just killed. When modified, it is saved
  10754. \(if the user agrees) and then killed."
  10755. (let (buf file)
  10756. (while (setq buf (pop blist))
  10757. (setq file (buffer-file-name buf))
  10758. (when (and (buffer-modified-p buf)
  10759. file
  10760. (y-or-n-p (format "Save file %s? " file)))
  10761. (with-current-buffer buf (save-buffer)))
  10762. (kill-buffer buf))))
  10763. (defun org-prepare-agenda-buffers (files)
  10764. "Create buffers for all agenda files, protect archived trees and comments."
  10765. (interactive)
  10766. (let ((pa '(:org-archived t))
  10767. (pc '(:org-comment t))
  10768. (pall '(:org-archived t :org-comment t))
  10769. (inhibit-read-only t)
  10770. (rea (concat ":" org-archive-tag ":"))
  10771. bmp file re)
  10772. (save-excursion
  10773. (save-restriction
  10774. (while (setq file (pop files))
  10775. (if (bufferp file)
  10776. (set-buffer file)
  10777. (org-check-agenda-file file)
  10778. (set-buffer (org-get-agenda-file-buffer file)))
  10779. (widen)
  10780. (setq bmp (buffer-modified-p))
  10781. (org-refresh-category-properties)
  10782. (setq org-todo-keywords-for-agenda
  10783. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10784. (setq org-done-keywords-for-agenda
  10785. (append org-done-keywords-for-agenda org-done-keywords))
  10786. (save-excursion
  10787. (remove-text-properties (point-min) (point-max) pall)
  10788. (when org-agenda-skip-archived-trees
  10789. (goto-char (point-min))
  10790. (while (re-search-forward rea nil t)
  10791. (if (org-on-heading-p t)
  10792. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10793. (goto-char (point-min))
  10794. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10795. (while (re-search-forward re nil t)
  10796. (add-text-properties
  10797. (match-beginning 0) (org-end-of-subtree t) pc)))
  10798. (set-buffer-modified-p bmp))))))
  10799. ;;;; Embedded LaTeX
  10800. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10801. "Keymap for the minor `org-cdlatex-mode'.")
  10802. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10803. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10804. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10805. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10806. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10807. (defvar org-cdlatex-texmathp-advice-is-done nil
  10808. "Flag remembering if we have applied the advice to texmathp already.")
  10809. (define-minor-mode org-cdlatex-mode
  10810. "Toggle the minor `org-cdlatex-mode'.
  10811. This mode supports entering LaTeX environment and math in LaTeX fragments
  10812. in Org-mode.
  10813. \\{org-cdlatex-mode-map}"
  10814. nil " OCDL" nil
  10815. (when org-cdlatex-mode (require 'cdlatex))
  10816. (unless org-cdlatex-texmathp-advice-is-done
  10817. (setq org-cdlatex-texmathp-advice-is-done t)
  10818. (defadvice texmathp (around org-math-always-on activate)
  10819. "Always return t in org-mode buffers.
  10820. This is because we want to insert math symbols without dollars even outside
  10821. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10822. en embedded LaTeX fragement, let texmathp do its job.
  10823. \\[org-cdlatex-mode-map]"
  10824. (interactive)
  10825. (let (p)
  10826. (cond
  10827. ((not (org-mode-p)) ad-do-it)
  10828. ((eq this-command 'cdlatex-math-symbol)
  10829. (setq ad-return-value t
  10830. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10831. (t
  10832. (let ((p (org-inside-LaTeX-fragment-p)))
  10833. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10834. (setq ad-return-value t
  10835. texmathp-why '("Org-mode embedded math" . 0))
  10836. (if p ad-do-it)))))))))
  10837. (defun turn-on-org-cdlatex ()
  10838. "Unconditionally turn on `org-cdlatex-mode'."
  10839. (org-cdlatex-mode 1))
  10840. (defun org-inside-LaTeX-fragment-p ()
  10841. "Test if point is inside a LaTeX fragment.
  10842. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10843. sequence appearing also before point.
  10844. Even though the matchers for math are configurable, this function assumes
  10845. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10846. delimiters are skipped when they have been removed by customization.
  10847. The return value is nil, or a cons cell with the delimiter and
  10848. and the position of this delimiter.
  10849. This function does a reasonably good job, but can locally be fooled by
  10850. for example currency specifications. For example it will assume being in
  10851. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10852. fragments that are properly closed, but during editing, we have to live
  10853. with the uncertainty caused by missing closing delimiters. This function
  10854. looks only before point, not after."
  10855. (catch 'exit
  10856. (let ((pos (point))
  10857. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10858. (lim (progn
  10859. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10860. (point)))
  10861. dd-on str (start 0) m re)
  10862. (goto-char pos)
  10863. (when dodollar
  10864. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10865. re (nth 1 (assoc "$" org-latex-regexps)))
  10866. (while (string-match re str start)
  10867. (cond
  10868. ((= (match-end 0) (length str))
  10869. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10870. ((= (match-end 0) (- (length str) 5))
  10871. (throw 'exit nil))
  10872. (t (setq start (match-end 0))))))
  10873. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10874. (goto-char pos)
  10875. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10876. (and (match-beginning 2) (throw 'exit nil))
  10877. ;; count $$
  10878. (while (re-search-backward "\\$\\$" lim t)
  10879. (setq dd-on (not dd-on)))
  10880. (goto-char pos)
  10881. (if dd-on (cons "$$" m))))))
  10882. (defun org-try-cdlatex-tab ()
  10883. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10884. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10885. - inside a LaTeX fragment, or
  10886. - after the first word in a line, where an abbreviation expansion could
  10887. insert a LaTeX environment."
  10888. (when org-cdlatex-mode
  10889. (cond
  10890. ((save-excursion
  10891. (skip-chars-backward "a-zA-Z0-9*")
  10892. (skip-chars-backward " \t")
  10893. (bolp))
  10894. (cdlatex-tab) t)
  10895. ((org-inside-LaTeX-fragment-p)
  10896. (cdlatex-tab) t)
  10897. (t nil))))
  10898. (defun org-cdlatex-underscore-caret (&optional arg)
  10899. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10900. Revert to the normal definition outside of these fragments."
  10901. (interactive "P")
  10902. (if (org-inside-LaTeX-fragment-p)
  10903. (call-interactively 'cdlatex-sub-superscript)
  10904. (let (org-cdlatex-mode)
  10905. (call-interactively (key-binding (vector last-input-event))))))
  10906. (defun org-cdlatex-math-modify (&optional arg)
  10907. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10908. Revert to the normal definition outside of these fragments."
  10909. (interactive "P")
  10910. (if (org-inside-LaTeX-fragment-p)
  10911. (call-interactively 'cdlatex-math-modify)
  10912. (let (org-cdlatex-mode)
  10913. (call-interactively (key-binding (vector last-input-event))))))
  10914. (defvar org-latex-fragment-image-overlays nil
  10915. "List of overlays carrying the images of latex fragments.")
  10916. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10917. (defun org-remove-latex-fragment-image-overlays ()
  10918. "Remove all overlays with LaTeX fragment images in current buffer."
  10919. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10920. (setq org-latex-fragment-image-overlays nil))
  10921. (defun org-preview-latex-fragment (&optional subtree)
  10922. "Preview the LaTeX fragment at point, or all locally or globally.
  10923. If the cursor is in a LaTeX fragment, create the image and overlay
  10924. it over the source code. If there is no fragment at point, display
  10925. all fragments in the current text, from one headline to the next. With
  10926. prefix SUBTREE, display all fragments in the current subtree. With a
  10927. double prefix `C-u C-u', or when the cursor is before the first headline,
  10928. display all fragments in the buffer.
  10929. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10930. (interactive "P")
  10931. (org-remove-latex-fragment-image-overlays)
  10932. (save-excursion
  10933. (save-restriction
  10934. (let (beg end at msg)
  10935. (cond
  10936. ((or (equal subtree '(16))
  10937. (not (save-excursion
  10938. (re-search-backward (concat "^" outline-regexp) nil t))))
  10939. (setq beg (point-min) end (point-max)
  10940. msg "Creating images for buffer...%s"))
  10941. ((equal subtree '(4))
  10942. (org-back-to-heading)
  10943. (setq beg (point) end (org-end-of-subtree t)
  10944. msg "Creating images for subtree...%s"))
  10945. (t
  10946. (if (setq at (org-inside-LaTeX-fragment-p))
  10947. (goto-char (max (point-min) (- (cdr at) 2)))
  10948. (org-back-to-heading))
  10949. (setq beg (point) end (progn (outline-next-heading) (point))
  10950. msg (if at "Creating image...%s"
  10951. "Creating images for entry...%s"))))
  10952. (message msg "")
  10953. (narrow-to-region beg end)
  10954. (goto-char beg)
  10955. (org-format-latex
  10956. (concat "ltxpng/" (file-name-sans-extension
  10957. (file-name-nondirectory
  10958. buffer-file-name)))
  10959. default-directory 'overlays msg at 'forbuffer)
  10960. (message msg "done. Use `C-c C-c' to remove images.")))))
  10961. (defvar org-latex-regexps
  10962. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10963. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10964. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10965. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10966. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10967. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10968. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10969. "Regular expressions for matching embedded LaTeX.")
  10970. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10971. "Replace LaTeX fragments with links to an image, and produce images."
  10972. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10973. (let* ((prefixnodir (file-name-nondirectory prefix))
  10974. (absprefix (expand-file-name prefix dir))
  10975. (todir (file-name-directory absprefix))
  10976. (opt org-format-latex-options)
  10977. (matchers (plist-get opt :matchers))
  10978. (re-list org-latex-regexps)
  10979. (cnt 0) txt link beg end re e checkdir
  10980. m n block linkfile movefile ov)
  10981. ;; Check if there are old images files with this prefix, and remove them
  10982. (when (file-directory-p todir)
  10983. (mapc 'delete-file
  10984. (directory-files
  10985. todir 'full
  10986. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10987. ;; Check the different regular expressions
  10988. (while (setq e (pop re-list))
  10989. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10990. block (if (nth 3 e) "\n\n" ""))
  10991. (when (member m matchers)
  10992. (goto-char (point-min))
  10993. (while (re-search-forward re nil t)
  10994. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10995. (setq txt (match-string n)
  10996. beg (match-beginning n) end (match-end n)
  10997. cnt (1+ cnt)
  10998. linkfile (format "%s_%04d.png" prefix cnt)
  10999. movefile (format "%s_%04d.png" absprefix cnt)
  11000. link (concat block "[[file:" linkfile "]]" block))
  11001. (if msg (message msg cnt))
  11002. (goto-char beg)
  11003. (unless checkdir ; make sure the directory exists
  11004. (setq checkdir t)
  11005. (or (file-directory-p todir) (make-directory todir)))
  11006. (org-create-formula-image
  11007. txt movefile opt forbuffer)
  11008. (if overlays
  11009. (progn
  11010. (setq ov (org-make-overlay beg end))
  11011. (if (featurep 'xemacs)
  11012. (progn
  11013. (org-overlay-put ov 'invisible t)
  11014. (org-overlay-put
  11015. ov 'end-glyph
  11016. (make-glyph (vector 'png :file movefile))))
  11017. (org-overlay-put
  11018. ov 'display
  11019. (list 'image :type 'png :file movefile :ascent 'center)))
  11020. (push ov org-latex-fragment-image-overlays)
  11021. (goto-char end))
  11022. (delete-region beg end)
  11023. (insert link))))))))
  11024. ;; This function borrows from Ganesh Swami's latex2png.el
  11025. (defun org-create-formula-image (string tofile options buffer)
  11026. (let* ((tmpdir (if (featurep 'xemacs)
  11027. (temp-directory)
  11028. temporary-file-directory))
  11029. (texfilebase (make-temp-name
  11030. (expand-file-name "orgtex" tmpdir)))
  11031. (texfile (concat texfilebase ".tex"))
  11032. (dvifile (concat texfilebase ".dvi"))
  11033. (pngfile (concat texfilebase ".png"))
  11034. (fnh (if (featurep 'xemacs)
  11035. (font-height (get-face-font 'default))
  11036. (face-attribute 'default :height nil)))
  11037. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11038. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11039. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11040. "Black"))
  11041. (bg (or (plist-get options (if buffer :background :html-background))
  11042. "Transparent")))
  11043. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11044. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11045. (with-temp-file texfile
  11046. (insert org-format-latex-header
  11047. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11048. (let ((dir default-directory))
  11049. (condition-case nil
  11050. (progn
  11051. (cd tmpdir)
  11052. (call-process "latex" nil nil nil texfile))
  11053. (error nil))
  11054. (cd dir))
  11055. (if (not (file-exists-p dvifile))
  11056. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11057. (call-process "dvipng" nil nil nil
  11058. "-E" "-fg" fg "-bg" bg
  11059. "-D" dpi
  11060. ;;"-x" scale "-y" scale
  11061. "-T" "tight"
  11062. "-o" pngfile
  11063. dvifile)
  11064. (if (not (file-exists-p pngfile))
  11065. (progn (message "Failed to create png file from %s" texfile) nil)
  11066. ;; Use the requested file name and clean up
  11067. (copy-file pngfile tofile 'replace)
  11068. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11069. (delete-file (concat texfilebase e)))
  11070. pngfile))))
  11071. (defun org-dvipng-color (attr)
  11072. "Return an rgb color specification for dvipng."
  11073. (apply 'format "rgb %s %s %s"
  11074. (mapcar 'org-normalize-color
  11075. (color-values (face-attribute 'default attr nil)))))
  11076. (defun org-normalize-color (value)
  11077. "Return string to be used as color value for an RGB component."
  11078. (format "%g" (/ value 65535.0)))
  11079. ;;;; Key bindings
  11080. ;; Make `C-c C-x' a prefix key
  11081. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11082. ;; TAB key with modifiers
  11083. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11084. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11085. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11086. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11087. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11088. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11089. ;; The following line is necessary under Suse GNU/Linux
  11090. (unless (featurep 'xemacs)
  11091. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11092. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11093. (define-key org-mode-map [backtab] 'org-shifttab)
  11094. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11095. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11096. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11097. ;; Cursor keys with modifiers
  11098. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11099. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11100. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11101. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11102. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11103. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11104. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11105. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11106. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11107. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11108. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11109. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11110. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11111. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11112. ;;; Extra keys for tty access.
  11113. ;; We only set them when really needed because otherwise the
  11114. ;; menus don't show the simple keys
  11115. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11116. (not window-system))
  11117. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11118. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11119. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11120. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11121. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11122. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11123. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11124. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11125. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11126. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11127. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11128. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11129. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11130. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11131. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11132. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11133. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11134. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11135. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11136. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11137. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11138. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11139. ;; All the other keys
  11140. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11141. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11142. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  11143. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11144. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11145. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11146. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11147. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11148. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11149. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11150. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11151. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11152. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11153. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11154. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11155. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11156. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11157. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11158. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11159. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11160. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11161. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11162. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11163. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11164. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11165. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11166. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11167. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11168. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11169. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11170. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11171. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11172. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11173. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11174. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11175. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11176. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11177. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11178. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11179. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11180. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11181. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11182. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11183. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11184. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11185. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11186. (org-defkey org-mode-map "\C-m" 'org-return)
  11187. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11188. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11189. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11190. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11191. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11192. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11193. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11194. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11195. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11196. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11197. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11198. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11199. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11200. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11201. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11202. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11203. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11204. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11205. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11206. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11207. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11208. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11209. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11210. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11211. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11212. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11213. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11214. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11215. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11216. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11217. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11218. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11219. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11220. (when (featurep 'xemacs)
  11221. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11222. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11223. (defun org-self-insert-command (N)
  11224. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11225. If the cursor is in a table looking at whitespace, the whitespace is
  11226. overwritten, and the table is not marked as requiring realignment."
  11227. (interactive "p")
  11228. (if (and (org-table-p)
  11229. (progn
  11230. ;; check if we blank the field, and if that triggers align
  11231. (and (featurep 'org-table) org-table-auto-blank-field
  11232. (member last-command
  11233. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11234. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11235. ;; got extra space, this field does not determine column width
  11236. (let (org-table-may-need-update) (org-table-blank-field))
  11237. ;; no extra space, this field may determine column width
  11238. (org-table-blank-field)))
  11239. t)
  11240. (eq N 1)
  11241. (looking-at "[^|\n]* |"))
  11242. (let (org-table-may-need-update)
  11243. (goto-char (1- (match-end 0)))
  11244. (delete-backward-char 1)
  11245. (goto-char (match-beginning 0))
  11246. (self-insert-command N))
  11247. (setq org-table-may-need-update t)
  11248. (self-insert-command N)
  11249. (org-fix-tags-on-the-fly)))
  11250. (defun org-fix-tags-on-the-fly ()
  11251. (when (and (equal (char-after (point-at-bol)) ?*)
  11252. (org-on-heading-p))
  11253. (org-align-tags-here org-tags-column)))
  11254. (defun org-delete-backward-char (N)
  11255. "Like `delete-backward-char', insert whitespace at field end in tables.
  11256. When deleting backwards, in tables this function will insert whitespace in
  11257. front of the next \"|\" separator, to keep the table aligned. The table will
  11258. still be marked for re-alignment if the field did fill the entire column,
  11259. because, in this case the deletion might narrow the column."
  11260. (interactive "p")
  11261. (if (and (org-table-p)
  11262. (eq N 1)
  11263. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11264. (looking-at ".*?|"))
  11265. (let ((pos (point))
  11266. (noalign (looking-at "[^|\n\r]* |"))
  11267. (c org-table-may-need-update))
  11268. (backward-delete-char N)
  11269. (skip-chars-forward "^|")
  11270. (insert " ")
  11271. (goto-char (1- pos))
  11272. ;; noalign: if there were two spaces at the end, this field
  11273. ;; does not determine the width of the column.
  11274. (if noalign (setq org-table-may-need-update c)))
  11275. (backward-delete-char N)
  11276. (org-fix-tags-on-the-fly)))
  11277. (defun org-delete-char (N)
  11278. "Like `delete-char', but insert whitespace at field end in tables.
  11279. When deleting characters, in tables this function will insert whitespace in
  11280. front of the next \"|\" separator, to keep the table aligned. The table will
  11281. still be marked for re-alignment if the field did fill the entire column,
  11282. because, in this case the deletion might narrow the column."
  11283. (interactive "p")
  11284. (if (and (org-table-p)
  11285. (not (bolp))
  11286. (not (= (char-after) ?|))
  11287. (eq N 1))
  11288. (if (looking-at ".*?|")
  11289. (let ((pos (point))
  11290. (noalign (looking-at "[^|\n\r]* |"))
  11291. (c org-table-may-need-update))
  11292. (replace-match (concat
  11293. (substring (match-string 0) 1 -1)
  11294. " |"))
  11295. (goto-char pos)
  11296. ;; noalign: if there were two spaces at the end, this field
  11297. ;; does not determine the width of the column.
  11298. (if noalign (setq org-table-may-need-update c)))
  11299. (delete-char N))
  11300. (delete-char N)
  11301. (org-fix-tags-on-the-fly)))
  11302. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11303. (put 'org-self-insert-command 'delete-selection t)
  11304. (put 'orgtbl-self-insert-command 'delete-selection t)
  11305. (put 'org-delete-char 'delete-selection 'supersede)
  11306. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11307. ;; Make `flyspell-mode' delay after some commands
  11308. (put 'org-self-insert-command 'flyspell-delayed t)
  11309. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11310. (put 'org-delete-char 'flyspell-delayed t)
  11311. (put 'org-delete-backward-char 'flyspell-delayed t)
  11312. ;; Make pabbrev-mode expand after org-mode commands
  11313. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11314. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11315. ;; How to do this: Measure non-white length of current string
  11316. ;; If equal to column width, we should realign.
  11317. (defun org-remap (map &rest commands)
  11318. "In MAP, remap the functions given in COMMANDS.
  11319. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11320. (let (new old)
  11321. (while commands
  11322. (setq old (pop commands) new (pop commands))
  11323. (if (fboundp 'command-remapping)
  11324. (org-defkey map (vector 'remap old) new)
  11325. (substitute-key-definition old new map global-map)))))
  11326. (when (eq org-enable-table-editor 'optimized)
  11327. ;; If the user wants maximum table support, we need to hijack
  11328. ;; some standard editing functions
  11329. (org-remap org-mode-map
  11330. 'self-insert-command 'org-self-insert-command
  11331. 'delete-char 'org-delete-char
  11332. 'delete-backward-char 'org-delete-backward-char)
  11333. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11334. (defun org-shiftcursor-error ()
  11335. "Throw an error because Shift-Cursor command was applied in wrong context."
  11336. (error "This command is active in special context like tables, headlines or timestamps"))
  11337. (defun org-shifttab (&optional arg)
  11338. "Global visibility cycling or move to previous table field.
  11339. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11340. on context.
  11341. See the individual commands for more information."
  11342. (interactive "P")
  11343. (cond
  11344. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11345. ((integerp arg)
  11346. (message "Content view to level: %d" arg)
  11347. (org-content (prefix-numeric-value arg))
  11348. (setq org-cycle-global-status 'overview))
  11349. (t (call-interactively 'org-global-cycle))))
  11350. (defun org-shiftmetaleft ()
  11351. "Promote subtree or delete table column.
  11352. Calls `org-promote-subtree', `org-outdent-item',
  11353. or `org-table-delete-column', depending on context.
  11354. See the individual commands for more information."
  11355. (interactive)
  11356. (cond
  11357. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11358. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11359. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11360. (t (org-shiftcursor-error))))
  11361. (defun org-shiftmetaright ()
  11362. "Demote subtree or insert table column.
  11363. Calls `org-demote-subtree', `org-indent-item',
  11364. or `org-table-insert-column', depending on context.
  11365. See the individual commands for more information."
  11366. (interactive)
  11367. (cond
  11368. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11369. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11370. ((org-at-item-p) (call-interactively 'org-indent-item))
  11371. (t (org-shiftcursor-error))))
  11372. (defun org-shiftmetaup (&optional arg)
  11373. "Move subtree up or kill table row.
  11374. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11375. `org-move-item-up' depending on context. See the individual commands
  11376. for more information."
  11377. (interactive "P")
  11378. (cond
  11379. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11380. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11381. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11382. (t (org-shiftcursor-error))))
  11383. (defun org-shiftmetadown (&optional arg)
  11384. "Move subtree down or insert table row.
  11385. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11386. `org-move-item-down', depending on context. See the individual
  11387. commands for more information."
  11388. (interactive "P")
  11389. (cond
  11390. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11391. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11392. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11393. (t (org-shiftcursor-error))))
  11394. (defun org-metaleft (&optional arg)
  11395. "Promote heading or move table column to left.
  11396. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11397. With no specific context, calls the Emacs default `backward-word'.
  11398. See the individual commands for more information."
  11399. (interactive "P")
  11400. (cond
  11401. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11402. ((or (org-on-heading-p) (org-region-active-p))
  11403. (call-interactively 'org-do-promote))
  11404. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11405. (t (call-interactively 'backward-word))))
  11406. (defun org-metaright (&optional arg)
  11407. "Demote subtree or move table column to right.
  11408. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11409. With no specific context, calls the Emacs default `forward-word'.
  11410. See the individual commands for more information."
  11411. (interactive "P")
  11412. (cond
  11413. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11414. ((or (org-on-heading-p) (org-region-active-p))
  11415. (call-interactively 'org-do-demote))
  11416. ((org-at-item-p) (call-interactively 'org-indent-item))
  11417. (t (call-interactively 'forward-word))))
  11418. (defun org-metaup (&optional arg)
  11419. "Move subtree up or move table row up.
  11420. Calls `org-move-subtree-up' or `org-table-move-row' or
  11421. `org-move-item-up', depending on context. See the individual commands
  11422. for more information."
  11423. (interactive "P")
  11424. (cond
  11425. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11426. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11427. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11428. (t (transpose-lines 1) (beginning-of-line -1))))
  11429. (defun org-metadown (&optional arg)
  11430. "Move subtree down or move table row down.
  11431. Calls `org-move-subtree-down' or `org-table-move-row' or
  11432. `org-move-item-down', depending on context. See the individual
  11433. commands for more information."
  11434. (interactive "P")
  11435. (cond
  11436. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11437. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11438. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11439. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11440. (defun org-shiftup (&optional arg)
  11441. "Increase item in timestamp or increase priority of current headline.
  11442. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11443. depending on context. See the individual commands for more information."
  11444. (interactive "P")
  11445. (cond
  11446. ((org-at-timestamp-p t)
  11447. (call-interactively (if org-edit-timestamp-down-means-later
  11448. 'org-timestamp-down 'org-timestamp-up)))
  11449. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11450. ((org-at-item-p) (call-interactively 'org-previous-item))
  11451. ((org-clocktable-try-shift 'up arg))
  11452. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11453. (defun org-shiftdown (&optional arg)
  11454. "Decrease item in timestamp or decrease priority of current headline.
  11455. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11456. depending on context. See the individual commands for more information."
  11457. (interactive "P")
  11458. (cond
  11459. ((org-at-timestamp-p t)
  11460. (call-interactively (if org-edit-timestamp-down-means-later
  11461. 'org-timestamp-up 'org-timestamp-down)))
  11462. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11463. ((org-clocktable-try-shift 'down arg))
  11464. (t (call-interactively 'org-next-item))))
  11465. (defun org-shiftright (&optional arg)
  11466. "Next TODO keyword or timestamp one day later, depending on context."
  11467. (interactive "P")
  11468. (cond
  11469. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11470. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11471. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11472. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11473. ((org-clocktable-try-shift 'right arg))
  11474. (t (org-shiftcursor-error))))
  11475. (defun org-shiftleft (&optional arg)
  11476. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11477. (interactive "P")
  11478. (cond
  11479. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11480. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11481. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11482. ((org-at-property-p)
  11483. (call-interactively 'org-property-previous-allowed-value))
  11484. ((org-clocktable-try-shift 'left arg))
  11485. (t (org-shiftcursor-error))))
  11486. (defun org-shiftcontrolright ()
  11487. "Switch to next TODO set."
  11488. (interactive)
  11489. (cond
  11490. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11491. (t (org-shiftcursor-error))))
  11492. (defun org-shiftcontrolleft ()
  11493. "Switch to previous TODO set."
  11494. (interactive)
  11495. (cond
  11496. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11497. (t (org-shiftcursor-error))))
  11498. (defun org-ctrl-c-ret ()
  11499. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11500. (interactive)
  11501. (cond
  11502. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11503. (t (call-interactively 'org-insert-heading))))
  11504. (defun org-copy-special ()
  11505. "Copy region in table or copy current subtree.
  11506. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11507. See the individual commands for more information."
  11508. (interactive)
  11509. (call-interactively
  11510. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11511. (defun org-cut-special ()
  11512. "Cut region in table or cut current subtree.
  11513. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11514. See the individual commands for more information."
  11515. (interactive)
  11516. (call-interactively
  11517. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11518. (defun org-paste-special (arg)
  11519. "Paste rectangular region into table, or past subtree relative to level.
  11520. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11521. See the individual commands for more information."
  11522. (interactive "P")
  11523. (if (org-at-table-p)
  11524. (org-table-paste-rectangle)
  11525. (org-paste-subtree arg)))
  11526. (defun org-ctrl-c-ctrl-c (&optional arg)
  11527. "Set tags in headline, or update according to changed information at point.
  11528. This command does many different things, depending on context:
  11529. - If the cursor is in a headline, prompt for tags and insert them
  11530. into the current line, aligned to `org-tags-column'. When called
  11531. with prefix arg, realign all tags in the current buffer.
  11532. - If the cursor is in one of the special #+KEYWORD lines, this
  11533. triggers scanning the buffer for these lines and updating the
  11534. information.
  11535. - If the cursor is inside a table, realign the table. This command
  11536. works even if the automatic table editor has been turned off.
  11537. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11538. the entire table.
  11539. - If the cursor is a the beginning of a dynamic block, update it.
  11540. - If the cursor is inside a table created by the table.el package,
  11541. activate that table.
  11542. - If the current buffer is a remember buffer, close note and file it.
  11543. with a prefix argument, file it without further interaction to the default
  11544. location.
  11545. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11546. links in this buffer.
  11547. - If the cursor is on a numbered item in a plain list, renumber the
  11548. ordered list.
  11549. - If the cursor is on a checkbox, toggle it."
  11550. (interactive "P")
  11551. (let ((org-enable-table-editor t))
  11552. (cond
  11553. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11554. org-occur-highlights
  11555. org-latex-fragment-image-overlays)
  11556. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11557. (org-remove-occur-highlights)
  11558. (org-remove-latex-fragment-image-overlays)
  11559. (message "Temporary highlights/overlays removed from current buffer"))
  11560. ((and (local-variable-p 'org-finish-function (current-buffer))
  11561. (fboundp org-finish-function))
  11562. (funcall org-finish-function))
  11563. ((org-at-property-p)
  11564. (call-interactively 'org-property-action))
  11565. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11566. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11567. ((org-at-table.el-p)
  11568. (require 'table)
  11569. (beginning-of-line 1)
  11570. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11571. (call-interactively 'table-recognize-table))
  11572. ((org-at-table-p)
  11573. (org-table-maybe-eval-formula)
  11574. (if arg
  11575. (call-interactively 'org-table-recalculate)
  11576. (org-table-maybe-recalculate-line))
  11577. (call-interactively 'org-table-align))
  11578. ((org-at-item-checkbox-p)
  11579. (call-interactively 'org-toggle-checkbox))
  11580. ((org-at-item-p)
  11581. (call-interactively 'org-maybe-renumber-ordered-list))
  11582. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11583. ;; Dynamic block
  11584. (beginning-of-line 1)
  11585. (org-update-dblock))
  11586. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11587. (cond
  11588. ((equal (match-string 1) "TBLFM")
  11589. ;; Recalculate the table before this line
  11590. (save-excursion
  11591. (beginning-of-line 1)
  11592. (skip-chars-backward " \r\n\t")
  11593. (if (org-at-table-p)
  11594. (org-call-with-arg 'org-table-recalculate t))))
  11595. (t
  11596. ; (org-set-regexps-and-options)
  11597. ; (org-restart-font-lock)
  11598. (let ((org-inhibit-startup t)) (org-mode-restart))
  11599. (message "Local setup has been refreshed"))))
  11600. (t (error "C-c C-c can do nothing useful at this location.")))))
  11601. (defun org-mode-restart ()
  11602. "Restart Org-mode, to scan again for special lines.
  11603. Also updates the keyword regular expressions."
  11604. (interactive)
  11605. (org-mode)
  11606. (message "Org-mode restarted"))
  11607. (defun org-kill-note-or-show-branches ()
  11608. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11609. (interactive)
  11610. (if (not org-finish-function)
  11611. (call-interactively 'show-branches)
  11612. (let ((org-note-abort t))
  11613. (funcall org-finish-function))))
  11614. (defun org-return (&optional indent)
  11615. "Goto next table row or insert a newline.
  11616. Calls `org-table-next-row' or `newline', depending on context.
  11617. See the individual commands for more information."
  11618. (interactive)
  11619. (cond
  11620. ((bobp) (if indent (newline-and-indent) (newline)))
  11621. ((and (org-at-heading-p)
  11622. (looking-at
  11623. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11624. (org-show-entry)
  11625. (end-of-line 1)
  11626. (newline))
  11627. ((org-at-table-p)
  11628. (org-table-justify-field-maybe)
  11629. (call-interactively 'org-table-next-row))
  11630. (t (if indent (newline-and-indent) (newline)))))
  11631. (defun org-return-indent ()
  11632. "Goto next table row or insert a newline and indent.
  11633. Calls `org-table-next-row' or `newline-and-indent', depending on
  11634. context. See the individual commands for more information."
  11635. (interactive)
  11636. (org-return t))
  11637. (defun org-ctrl-c-star ()
  11638. "Compute table, or change heading status of lines.
  11639. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11640. depending on context. This will also turn a plain list item or a normal
  11641. line into a subheading."
  11642. (interactive)
  11643. (cond
  11644. ((org-at-table-p)
  11645. (call-interactively 'org-table-recalculate))
  11646. ((org-region-active-p)
  11647. ;; Convert all lines in region to list items
  11648. (call-interactively 'org-toggle-region-headings))
  11649. ((org-on-heading-p)
  11650. (org-toggle-region-headings (point-at-bol)
  11651. (min (1+ (point-at-eol)) (point-max))))
  11652. ((org-at-item-p)
  11653. ;; Convert to heading
  11654. (let ((level (save-match-data
  11655. (save-excursion
  11656. (condition-case nil
  11657. (progn
  11658. (org-back-to-heading t)
  11659. (funcall outline-level))
  11660. (error 0))))))
  11661. (replace-match
  11662. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11663. (t (org-toggle-region-headings (point-at-bol)
  11664. (min (1+ (point-at-eol)) (point-max))))))
  11665. (defun org-ctrl-c-minus ()
  11666. "Insert separator line in table or modify bullet status of line.
  11667. Also turns a plain line or a region of lines into list items.
  11668. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11669. `org-cycle-list-bullet', depending on context."
  11670. (interactive)
  11671. (cond
  11672. ((org-at-table-p)
  11673. (call-interactively 'org-table-insert-hline))
  11674. ((org-on-heading-p)
  11675. ;; Convert to item
  11676. (save-excursion
  11677. (beginning-of-line 1)
  11678. (if (looking-at "\\*+ ")
  11679. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11680. ((org-region-active-p)
  11681. ;; Convert all lines in region to list items
  11682. (call-interactively 'org-toggle-region-items))
  11683. ((org-in-item-p)
  11684. (call-interactively 'org-cycle-list-bullet))
  11685. (t (org-toggle-region-items (point-at-bol)
  11686. (min (1+ (point-at-eol)) (point-max))))))
  11687. (defun org-toggle-region-items (beg end)
  11688. "Convert all lines in region to list items.
  11689. If the first line is already an item, convert all list items in the region
  11690. to normal lines."
  11691. (interactive "r")
  11692. (let (l2 l)
  11693. (save-excursion
  11694. (goto-char end)
  11695. (setq l2 (org-current-line))
  11696. (goto-char beg)
  11697. (beginning-of-line 1)
  11698. (setq l (1- (org-current-line)))
  11699. (if (org-at-item-p)
  11700. ;; We already have items, de-itemize
  11701. (while (< (setq l (1+ l)) l2)
  11702. (when (org-at-item-p)
  11703. (goto-char (match-beginning 2))
  11704. (delete-region (match-beginning 2) (match-end 2))
  11705. (and (looking-at "[ \t]+") (replace-match "")))
  11706. (beginning-of-line 2))
  11707. (while (< (setq l (1+ l)) l2)
  11708. (unless (org-at-item-p)
  11709. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11710. (replace-match "\\1- \\2")))
  11711. (beginning-of-line 2))))))
  11712. (defun org-toggle-region-headings (beg end)
  11713. "Convert all lines in region to list items.
  11714. If the first line is already an item, convert all list items in the region
  11715. to normal lines."
  11716. (interactive "r")
  11717. (let (l2 l)
  11718. (save-excursion
  11719. (goto-char end)
  11720. (setq l2 (org-current-line))
  11721. (goto-char beg)
  11722. (beginning-of-line 1)
  11723. (setq l (1- (org-current-line)))
  11724. (if (org-on-heading-p)
  11725. ;; We already have headlines, de-star them
  11726. (while (< (setq l (1+ l)) l2)
  11727. (when (org-on-heading-p t)
  11728. (and (looking-at outline-regexp) (replace-match "")))
  11729. (beginning-of-line 2))
  11730. (let* ((stars (save-excursion
  11731. (re-search-backward org-complex-heading-regexp nil t)
  11732. (or (match-string 1) "*")))
  11733. (add-stars (if org-odd-levels-only "**" "*"))
  11734. (rpl (concat stars add-stars " \\2")))
  11735. (while (< (setq l (1+ l)) l2)
  11736. (unless (org-on-heading-p)
  11737. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11738. (replace-match rpl)))
  11739. (beginning-of-line 2)))))))
  11740. (defun org-meta-return (&optional arg)
  11741. "Insert a new heading or wrap a region in a table.
  11742. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11743. See the individual commands for more information."
  11744. (interactive "P")
  11745. (cond
  11746. ((org-at-table-p)
  11747. (call-interactively 'org-table-wrap-region))
  11748. (t (call-interactively 'org-insert-heading))))
  11749. ;;; Menu entries
  11750. ;; Define the Org-mode menus
  11751. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11752. '("Tbl"
  11753. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11754. ["Next Field" org-cycle (org-at-table-p)]
  11755. ["Previous Field" org-shifttab (org-at-table-p)]
  11756. ["Next Row" org-return (org-at-table-p)]
  11757. "--"
  11758. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11759. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11760. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11761. "--"
  11762. ("Column"
  11763. ["Move Column Left" org-metaleft (org-at-table-p)]
  11764. ["Move Column Right" org-metaright (org-at-table-p)]
  11765. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11766. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11767. ("Row"
  11768. ["Move Row Up" org-metaup (org-at-table-p)]
  11769. ["Move Row Down" org-metadown (org-at-table-p)]
  11770. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11771. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11772. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11773. "--"
  11774. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11775. ("Rectangle"
  11776. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11777. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11778. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11779. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11780. "--"
  11781. ("Calculate"
  11782. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11783. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11784. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11785. "--"
  11786. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11787. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11788. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11789. "--"
  11790. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11791. "--"
  11792. ["Sum Column/Rectangle" org-table-sum
  11793. (or (org-at-table-p) (org-region-active-p))]
  11794. ["Which Column?" org-table-current-column (org-at-table-p)])
  11795. ["Debug Formulas"
  11796. org-table-toggle-formula-debugger
  11797. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11798. ["Show Col/Row Numbers"
  11799. org-table-toggle-coordinate-overlays
  11800. :style toggle
  11801. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11802. "--"
  11803. ["Create" org-table-create (and (not (org-at-table-p))
  11804. org-enable-table-editor)]
  11805. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11806. ["Import from File" org-table-import (not (org-at-table-p))]
  11807. ["Export to File" org-table-export (org-at-table-p)]
  11808. "--"
  11809. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11810. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11811. '("Org"
  11812. ("Show/Hide"
  11813. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11814. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11815. ["Sparse Tree..." org-sparse-tree t]
  11816. ["Reveal Context" org-reveal t]
  11817. ["Show All" show-all t]
  11818. "--"
  11819. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11820. "--"
  11821. ["New Heading" org-insert-heading t]
  11822. ("Navigate Headings"
  11823. ["Up" outline-up-heading t]
  11824. ["Next" outline-next-visible-heading t]
  11825. ["Previous" outline-previous-visible-heading t]
  11826. ["Next Same Level" outline-forward-same-level t]
  11827. ["Previous Same Level" outline-backward-same-level t]
  11828. "--"
  11829. ["Jump" org-goto t])
  11830. ("Edit Structure"
  11831. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11832. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11833. "--"
  11834. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11835. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11836. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11837. "--"
  11838. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11839. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11840. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11841. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11842. "--"
  11843. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11844. "--"
  11845. ["Convert to odd levels" org-convert-to-odd-levels t]
  11846. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11847. ("Editing"
  11848. ["Emphasis..." org-emphasize t])
  11849. ("Archive"
  11850. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11851. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11852. ; :active t :keys "C-u C-c C-x C-a"]
  11853. ["Sparse trees open ARCHIVE trees"
  11854. (setq org-sparse-tree-open-archived-trees
  11855. (not org-sparse-tree-open-archived-trees))
  11856. :style toggle :selected org-sparse-tree-open-archived-trees]
  11857. ["Cycling opens ARCHIVE trees"
  11858. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11859. :style toggle :selected org-cycle-open-archived-trees]
  11860. ["Agenda includes ARCHIVE trees"
  11861. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11862. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11863. "--"
  11864. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11865. ; ["Check and Move Children" (org-archive-subtree '(4))
  11866. ; :active t :keys "C-u C-c C-x C-s"]
  11867. )
  11868. "--"
  11869. ("TODO Lists"
  11870. ["TODO/DONE/-" org-todo t]
  11871. ("Select keyword"
  11872. ["Next keyword" org-shiftright (org-on-heading-p)]
  11873. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11874. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11875. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11876. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11877. ["Show TODO Tree" org-show-todo-tree t]
  11878. ["Global TODO list" org-todo-list t]
  11879. "--"
  11880. ["Set Priority" org-priority t]
  11881. ["Priority Up" org-shiftup t]
  11882. ["Priority Down" org-shiftdown t])
  11883. ("TAGS and Properties"
  11884. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11885. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11886. "--"
  11887. ["Set property" 'org-set-property t]
  11888. ["Column view of properties" org-columns t]
  11889. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11890. ("Dates and Scheduling"
  11891. ["Timestamp" org-time-stamp t]
  11892. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11893. ("Change Date"
  11894. ["1 Day Later" org-shiftright t]
  11895. ["1 Day Earlier" org-shiftleft t]
  11896. ["1 ... Later" org-shiftup t]
  11897. ["1 ... Earlier" org-shiftdown t])
  11898. ["Compute Time Range" org-evaluate-time-range t]
  11899. ["Schedule Item" org-schedule t]
  11900. ["Deadline" org-deadline t]
  11901. "--"
  11902. ["Custom time format" org-toggle-time-stamp-overlays
  11903. :style radio :selected org-display-custom-times]
  11904. "--"
  11905. ["Goto Calendar" org-goto-calendar t]
  11906. ["Date from Calendar" org-date-from-calendar t])
  11907. ("Logging work"
  11908. ["Clock in" org-clock-in t]
  11909. ["Clock out" org-clock-out t]
  11910. ["Clock cancel" org-clock-cancel t]
  11911. ["Goto running clock" org-clock-goto t]
  11912. ["Display times" org-clock-display t]
  11913. ["Create clock table" org-clock-report t]
  11914. "--"
  11915. ["Record DONE time"
  11916. (progn (setq org-log-done (not org-log-done))
  11917. (message "Switching to %s will %s record a timestamp"
  11918. (car org-done-keywords)
  11919. (if org-log-done "automatically" "not")))
  11920. :style toggle :selected org-log-done])
  11921. "--"
  11922. ["Agenda Command..." org-agenda t]
  11923. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11924. ("File List for Agenda")
  11925. ("Special views current file"
  11926. ["TODO Tree" org-show-todo-tree t]
  11927. ["Check Deadlines" org-check-deadlines t]
  11928. ["Timeline" org-timeline t]
  11929. ["Tags Tree" org-tags-sparse-tree t])
  11930. "--"
  11931. ("Hyperlinks"
  11932. ["Store Link (Global)" org-store-link t]
  11933. ["Insert Link" org-insert-link t]
  11934. ["Follow Link" org-open-at-point t]
  11935. "--"
  11936. ["Next link" org-next-link t]
  11937. ["Previous link" org-previous-link t]
  11938. "--"
  11939. ["Descriptive Links"
  11940. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11941. :style radio
  11942. :selected (member '(org-link) buffer-invisibility-spec)]
  11943. ["Literal Links"
  11944. (progn
  11945. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11946. :style radio
  11947. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11948. "--"
  11949. ["Export/Publish..." org-export t]
  11950. ("LaTeX"
  11951. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11952. :selected org-cdlatex-mode]
  11953. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11954. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11955. ["Modify math symbol" org-cdlatex-math-modify
  11956. (org-inside-LaTeX-fragment-p)]
  11957. ["Export LaTeX fragments as images"
  11958. (if (featurep 'org-exp)
  11959. (setq org-export-with-LaTeX-fragments
  11960. (not org-export-with-LaTeX-fragments))
  11961. (require 'org-exp))
  11962. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11963. org-export-with-LaTeX-fragments)])
  11964. "--"
  11965. ("Documentation"
  11966. ["Show Version" org-version t]
  11967. ["Info Documentation" org-info t])
  11968. ("Customize"
  11969. ["Browse Org Group" org-customize t]
  11970. "--"
  11971. ["Expand This Menu" org-create-customize-menu
  11972. (fboundp 'customize-menu-create)])
  11973. "--"
  11974. ["Refresh setup" org-mode-restart t]
  11975. ))
  11976. (defun org-info (&optional node)
  11977. "Read documentation for Org-mode in the info system.
  11978. With optional NODE, go directly to that node."
  11979. (interactive)
  11980. (info (format "(org)%s" (or node ""))))
  11981. (defun org-install-agenda-files-menu ()
  11982. (let ((bl (buffer-list)))
  11983. (save-excursion
  11984. (while bl
  11985. (set-buffer (pop bl))
  11986. (if (org-mode-p) (setq bl nil)))
  11987. (when (org-mode-p)
  11988. (easy-menu-change
  11989. '("Org") "File List for Agenda"
  11990. (append
  11991. (list
  11992. ["Edit File List" (org-edit-agenda-file-list) t]
  11993. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11994. ["Remove Current File from List" org-remove-file t]
  11995. ["Cycle through agenda files" org-cycle-agenda-files t]
  11996. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11997. "--")
  11998. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11999. ;;;; Documentation
  12000. (defun org-require-autoloaded-modules ()
  12001. (interactive)
  12002. (mapc 'require
  12003. '(org-agenda org-archive org-clock org-colview
  12004. org-exp org-export-latex org-publish
  12005. org-remember org-table)))
  12006. (defun org-customize ()
  12007. "Call the customize function with org as argument."
  12008. (interactive)
  12009. (org-load-modules-maybe)
  12010. (org-require-autoloaded-modules)
  12011. (customize-browse 'org))
  12012. (defun org-create-customize-menu ()
  12013. "Create a full customization menu for Org-mode, insert it into the menu."
  12014. (interactive)
  12015. (org-load-modules-maybe)
  12016. (org-require-autoloaded-modules)
  12017. (if (fboundp 'customize-menu-create)
  12018. (progn
  12019. (easy-menu-change
  12020. '("Org") "Customize"
  12021. `(["Browse Org group" org-customize t]
  12022. "--"
  12023. ,(customize-menu-create 'org)
  12024. ["Set" Custom-set t]
  12025. ["Save" Custom-save t]
  12026. ["Reset to Current" Custom-reset-current t]
  12027. ["Reset to Saved" Custom-reset-saved t]
  12028. ["Reset to Standard Settings" Custom-reset-standard t]))
  12029. (message "\"Org\"-menu now contains full customization menu"))
  12030. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12031. ;;;; Miscellaneous stuff
  12032. ;;; Generally useful functions
  12033. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12034. "Display the given MESSAGE as a warning."
  12035. (if (fboundp 'display-warning)
  12036. (display-warning 'org message
  12037. (if (featurep 'xemacs)
  12038. 'warning
  12039. :warning))
  12040. (let ((buf (get-buffer-create "*Org warnings*")))
  12041. (with-current-buffer buf
  12042. (goto-char (point-max))
  12043. (insert "Warning (Org): " message)
  12044. (unless (bolp)
  12045. (newline)))
  12046. (display-buffer buf)
  12047. (sit-for 0))))
  12048. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12049. "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
  12050. (if (and marker (marker-buffer marker)
  12051. (buffer-live-p (marker-buffer marker)))
  12052. (progn
  12053. (switch-to-buffer (marker-buffer marker))
  12054. (if (or (> marker (point-max)) (< marker (point-min)))
  12055. (widen))
  12056. (goto-char marker))
  12057. (if bookmark
  12058. (bookmark-jump bookmark)
  12059. (error "Cannot find location"))))
  12060. (defun org-quote-csv-field (s)
  12061. "Quote field for inclusion in CSV material."
  12062. (if (string-match "[\",]" s)
  12063. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12064. s))
  12065. (defun org-plist-delete (plist property)
  12066. "Delete PROPERTY from PLIST.
  12067. This is in contrast to merely setting it to 0."
  12068. (let (p)
  12069. (while plist
  12070. (if (not (eq property (car plist)))
  12071. (setq p (plist-put p (car plist) (nth 1 plist))))
  12072. (setq plist (cddr plist)))
  12073. p))
  12074. (defun org-force-self-insert (N)
  12075. "Needed to enforce self-insert under remapping."
  12076. (interactive "p")
  12077. (self-insert-command N))
  12078. (defun org-string-width (s)
  12079. "Compute width of string, ignoring invisible characters.
  12080. This ignores character with invisibility property `org-link', and also
  12081. characters with property `org-cwidth', because these will become invisible
  12082. upon the next fontification round."
  12083. (let (b l)
  12084. (when (or (eq t buffer-invisibility-spec)
  12085. (assq 'org-link buffer-invisibility-spec))
  12086. (while (setq b (text-property-any 0 (length s)
  12087. 'invisible 'org-link s))
  12088. (setq s (concat (substring s 0 b)
  12089. (substring s (or (next-single-property-change
  12090. b 'invisible s) (length s)))))))
  12091. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12092. (setq s (concat (substring s 0 b)
  12093. (substring s (or (next-single-property-change
  12094. b 'org-cwidth s) (length s))))))
  12095. (setq l (string-width s) b -1)
  12096. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12097. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12098. l))
  12099. (defun org-base-buffer (buffer)
  12100. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12101. (if (not buffer)
  12102. buffer
  12103. (or (buffer-base-buffer buffer)
  12104. buffer)))
  12105. (defun org-trim (s)
  12106. "Remove whitespace at beginning and end of string."
  12107. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12108. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12109. s)
  12110. (defun org-wrap (string &optional width lines)
  12111. "Wrap string to either a number of lines, or a width in characters.
  12112. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12113. that costs. If there is a word longer than WIDTH, the text is actually
  12114. wrapped to the length of that word.
  12115. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12116. many lines, whatever width that takes.
  12117. The return value is a list of lines, without newlines at the end."
  12118. (let* ((words (org-split-string string "[ \t\n]+"))
  12119. (maxword (apply 'max (mapcar 'org-string-width words)))
  12120. w ll)
  12121. (cond (width
  12122. (org-do-wrap words (max maxword width)))
  12123. (lines
  12124. (setq w maxword)
  12125. (setq ll (org-do-wrap words maxword))
  12126. (if (<= (length ll) lines)
  12127. ll
  12128. (setq ll words)
  12129. (while (> (length ll) lines)
  12130. (setq w (1+ w))
  12131. (setq ll (org-do-wrap words w)))
  12132. ll))
  12133. (t (error "Cannot wrap this")))))
  12134. (defun org-do-wrap (words width)
  12135. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12136. (let (lines line)
  12137. (while words
  12138. (setq line (pop words))
  12139. (while (and words (< (+ (length line) (length (car words))) width))
  12140. (setq line (concat line " " (pop words))))
  12141. (setq lines (push line lines)))
  12142. (nreverse lines)))
  12143. (defun org-split-string (string &optional separators)
  12144. "Splits STRING into substrings at SEPARATORS.
  12145. No empty strings are returned if there are matches at the beginning
  12146. and end of string."
  12147. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12148. (start 0)
  12149. notfirst
  12150. (list nil))
  12151. (while (and (string-match rexp string
  12152. (if (and notfirst
  12153. (= start (match-beginning 0))
  12154. (< start (length string)))
  12155. (1+ start) start))
  12156. (< (match-beginning 0) (length string)))
  12157. (setq notfirst t)
  12158. (or (eq (match-beginning 0) 0)
  12159. (and (eq (match-beginning 0) (match-end 0))
  12160. (eq (match-beginning 0) start))
  12161. (setq list
  12162. (cons (substring string start (match-beginning 0))
  12163. list)))
  12164. (setq start (match-end 0)))
  12165. (or (eq start (length string))
  12166. (setq list
  12167. (cons (substring string start)
  12168. list)))
  12169. (nreverse list)))
  12170. (defun org-context ()
  12171. "Return a list of contexts of the current cursor position.
  12172. If several contexts apply, all are returned.
  12173. Each context entry is a list with a symbol naming the context, and
  12174. two positions indicating start and end of the context. Possible
  12175. contexts are:
  12176. :headline anywhere in a headline
  12177. :headline-stars on the leading stars in a headline
  12178. :todo-keyword on a TODO keyword (including DONE) in a headline
  12179. :tags on the TAGS in a headline
  12180. :priority on the priority cookie in a headline
  12181. :item on the first line of a plain list item
  12182. :item-bullet on the bullet/number of a plain list item
  12183. :checkbox on the checkbox in a plain list item
  12184. :table in an org-mode table
  12185. :table-special on a special filed in a table
  12186. :table-table in a table.el table
  12187. :link on a hyperlink
  12188. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12189. :target on a <<target>>
  12190. :radio-target on a <<<radio-target>>>
  12191. :latex-fragment on a LaTeX fragment
  12192. :latex-preview on a LaTeX fragment with overlayed preview image
  12193. This function expects the position to be visible because it uses font-lock
  12194. faces as a help to recognize the following contexts: :table-special, :link,
  12195. and :keyword."
  12196. (let* ((f (get-text-property (point) 'face))
  12197. (faces (if (listp f) f (list f)))
  12198. (p (point)) clist o)
  12199. ;; First the large context
  12200. (cond
  12201. ((org-on-heading-p t)
  12202. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12203. (when (progn
  12204. (beginning-of-line 1)
  12205. (looking-at org-todo-line-tags-regexp))
  12206. (push (org-point-in-group p 1 :headline-stars) clist)
  12207. (push (org-point-in-group p 2 :todo-keyword) clist)
  12208. (push (org-point-in-group p 4 :tags) clist))
  12209. (goto-char p)
  12210. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12211. (if (looking-at "\\[#[A-Z0-9]\\]")
  12212. (push (org-point-in-group p 0 :priority) clist)))
  12213. ((org-at-item-p)
  12214. (push (org-point-in-group p 2 :item-bullet) clist)
  12215. (push (list :item (point-at-bol)
  12216. (save-excursion (org-end-of-item) (point)))
  12217. clist)
  12218. (and (org-at-item-checkbox-p)
  12219. (push (org-point-in-group p 0 :checkbox) clist)))
  12220. ((org-at-table-p)
  12221. (push (list :table (org-table-begin) (org-table-end)) clist)
  12222. (if (memq 'org-formula faces)
  12223. (push (list :table-special
  12224. (previous-single-property-change p 'face)
  12225. (next-single-property-change p 'face)) clist)))
  12226. ((org-at-table-p 'any)
  12227. (push (list :table-table) clist)))
  12228. (goto-char p)
  12229. ;; Now the small context
  12230. (cond
  12231. ((org-at-timestamp-p)
  12232. (push (org-point-in-group p 0 :timestamp) clist))
  12233. ((memq 'org-link faces)
  12234. (push (list :link
  12235. (previous-single-property-change p 'face)
  12236. (next-single-property-change p 'face)) clist))
  12237. ((memq 'org-special-keyword faces)
  12238. (push (list :keyword
  12239. (previous-single-property-change p 'face)
  12240. (next-single-property-change p 'face)) clist))
  12241. ((org-on-target-p)
  12242. (push (org-point-in-group p 0 :target) clist)
  12243. (goto-char (1- (match-beginning 0)))
  12244. (if (looking-at org-radio-target-regexp)
  12245. (push (org-point-in-group p 0 :radio-target) clist))
  12246. (goto-char p))
  12247. ((setq o (car (delq nil
  12248. (mapcar
  12249. (lambda (x)
  12250. (if (memq x org-latex-fragment-image-overlays) x))
  12251. (org-overlays-at (point))))))
  12252. (push (list :latex-fragment
  12253. (org-overlay-start o) (org-overlay-end o)) clist)
  12254. (push (list :latex-preview
  12255. (org-overlay-start o) (org-overlay-end o)) clist))
  12256. ((org-inside-LaTeX-fragment-p)
  12257. ;; FIXME: positions wrong.
  12258. (push (list :latex-fragment (point) (point)) clist)))
  12259. (setq clist (nreverse (delq nil clist)))
  12260. clist))
  12261. ;; FIXME: Compare with at-regexp-p Do we need both?
  12262. (defun org-in-regexp (re &optional nlines visually)
  12263. "Check if point is inside a match of regexp.
  12264. Normally only the current line is checked, but you can include NLINES extra
  12265. lines both before and after point into the search.
  12266. If VISUALLY is set, require that the cursor is not after the match but
  12267. really on, so that the block visually is on the match."
  12268. (catch 'exit
  12269. (let ((pos (point))
  12270. (eol (point-at-eol (+ 1 (or nlines 0))))
  12271. (inc (if visually 1 0)))
  12272. (save-excursion
  12273. (beginning-of-line (- 1 (or nlines 0)))
  12274. (while (re-search-forward re eol t)
  12275. (if (and (<= (match-beginning 0) pos)
  12276. (>= (+ inc (match-end 0)) pos))
  12277. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12278. (defun org-at-regexp-p (regexp)
  12279. "Is point inside a match of REGEXP in the current line?"
  12280. (catch 'exit
  12281. (save-excursion
  12282. (let ((pos (point)) (end (point-at-eol)))
  12283. (beginning-of-line 1)
  12284. (while (re-search-forward regexp end t)
  12285. (if (and (<= (match-beginning 0) pos)
  12286. (>= (match-end 0) pos))
  12287. (throw 'exit t)))
  12288. nil))))
  12289. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12290. "Call `multi-occur' with buffers for all agenda files."
  12291. (interactive "sOrg-files matching: \np")
  12292. (let* ((files (org-agenda-files))
  12293. (tnames (mapcar 'file-truename files))
  12294. (extra org-agenda-text-search-extra-files)
  12295. f)
  12296. (when (eq (car extra) 'agenda-archives)
  12297. (setq extra (cdr extra))
  12298. (setq files (org-add-archive-files files)))
  12299. (while (setq f (pop extra))
  12300. (unless (member (file-truename f) tnames)
  12301. (add-to-list 'files f 'append)
  12302. (add-to-list 'tnames (file-truename f) 'append)))
  12303. (multi-occur
  12304. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12305. regexp)))
  12306. (if (boundp 'occur-mode-find-occurrence-hook)
  12307. ;; Emacs 23
  12308. (add-hook 'occur-mode-find-occurrence-hook
  12309. (lambda ()
  12310. (when (org-mode-p)
  12311. (org-reveal))))
  12312. ;; Emacs 22
  12313. (defadvice occur-mode-goto-occurrence
  12314. (after org-occur-reveal activate)
  12315. (and (org-mode-p) (org-reveal)))
  12316. (defadvice occur-mode-goto-occurrence-other-window
  12317. (after org-occur-reveal activate)
  12318. (and (org-mode-p) (org-reveal)))
  12319. (defadvice occur-mode-display-occurrence
  12320. (after org-occur-reveal activate)
  12321. (when (org-mode-p)
  12322. (let ((pos (occur-mode-find-occurrence)))
  12323. (with-current-buffer (marker-buffer pos)
  12324. (save-excursion
  12325. (goto-char pos)
  12326. (org-reveal)))))))
  12327. (defun org-uniquify (list)
  12328. "Remove duplicate elements from LIST."
  12329. (let (res)
  12330. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12331. res))
  12332. (defun org-delete-all (elts list)
  12333. "Remove all elements in ELTS from LIST."
  12334. (while elts
  12335. (setq list (delete (pop elts) list)))
  12336. list)
  12337. (defun org-back-over-empty-lines ()
  12338. "Move backwards over witespace, to the beginning of the first empty line.
  12339. Returns the number of empty lines passed."
  12340. (let ((pos (point)))
  12341. (skip-chars-backward " \t\n\r")
  12342. (beginning-of-line 2)
  12343. (goto-char (min (point) pos))
  12344. (count-lines (point) pos)))
  12345. (defun org-skip-whitespace ()
  12346. (skip-chars-forward " \t\n\r"))
  12347. (defun org-point-in-group (point group &optional context)
  12348. "Check if POINT is in match-group GROUP.
  12349. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12350. match. If the match group does ot exist or point is not inside it,
  12351. return nil."
  12352. (and (match-beginning group)
  12353. (>= point (match-beginning group))
  12354. (<= point (match-end group))
  12355. (if context
  12356. (list context (match-beginning group) (match-end group))
  12357. t)))
  12358. (defun org-switch-to-buffer-other-window (&rest args)
  12359. "Switch to buffer in a second window on the current frame.
  12360. In particular, do not allow pop-up frames."
  12361. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12362. special-display-function)
  12363. (apply 'switch-to-buffer-other-window args)))
  12364. (defun org-combine-plists (&rest plists)
  12365. "Create a single property list from all plists in PLISTS.
  12366. The process starts by copying the first list, and then setting properties
  12367. from the other lists. Settings in the last list are the most significant
  12368. ones and overrule settings in the other lists."
  12369. (let ((rtn (copy-sequence (pop plists)))
  12370. p v ls)
  12371. (while plists
  12372. (setq ls (pop plists))
  12373. (while ls
  12374. (setq p (pop ls) v (pop ls))
  12375. (setq rtn (plist-put rtn p v))))
  12376. rtn))
  12377. (defun org-move-line-down (arg)
  12378. "Move the current line down. With prefix argument, move it past ARG lines."
  12379. (interactive "p")
  12380. (let ((col (current-column))
  12381. beg end pos)
  12382. (beginning-of-line 1) (setq beg (point))
  12383. (beginning-of-line 2) (setq end (point))
  12384. (beginning-of-line (+ 1 arg))
  12385. (setq pos (move-marker (make-marker) (point)))
  12386. (insert (delete-and-extract-region beg end))
  12387. (goto-char pos)
  12388. (org-move-to-column col)))
  12389. (defun org-move-line-up (arg)
  12390. "Move the current line up. With prefix argument, move it past ARG lines."
  12391. (interactive "p")
  12392. (let ((col (current-column))
  12393. beg end pos)
  12394. (beginning-of-line 1) (setq beg (point))
  12395. (beginning-of-line 2) (setq end (point))
  12396. (beginning-of-line (- arg))
  12397. (setq pos (move-marker (make-marker) (point)))
  12398. (insert (delete-and-extract-region beg end))
  12399. (goto-char pos)
  12400. (org-move-to-column col)))
  12401. (defun org-replace-escapes (string table)
  12402. "Replace %-escapes in STRING with values in TABLE.
  12403. TABLE is an association list with keys like \"%a\" and string values.
  12404. The sequences in STRING may contain normal field width and padding information,
  12405. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12406. so values can contain further %-escapes if they are define later in TABLE."
  12407. (let ((case-fold-search nil)
  12408. e re rpl)
  12409. (while (setq e (pop table))
  12410. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12411. (while (string-match re string)
  12412. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12413. (cdr e)))
  12414. (setq string (replace-match rpl t t string))))
  12415. string))
  12416. (defun org-sublist (list start end)
  12417. "Return a section of LIST, from START to END.
  12418. Counting starts at 1."
  12419. (let (rtn (c start))
  12420. (setq list (nthcdr (1- start) list))
  12421. (while (and list (<= c end))
  12422. (push (pop list) rtn)
  12423. (setq c (1+ c)))
  12424. (nreverse rtn)))
  12425. (defun org-find-base-buffer-visiting (file)
  12426. "Like `find-buffer-visiting' but alway return the base buffer and
  12427. not an indirect buffer."
  12428. (let ((buf (find-buffer-visiting file)))
  12429. (if buf
  12430. (or (buffer-base-buffer buf) buf)
  12431. nil)))
  12432. (defun org-image-file-name-regexp ()
  12433. "Return regexp matching the file names of images."
  12434. (if (fboundp 'image-file-name-regexp)
  12435. (image-file-name-regexp)
  12436. (let ((image-file-name-extensions
  12437. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12438. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12439. (concat "\\."
  12440. (regexp-opt (nconc (mapcar 'upcase
  12441. image-file-name-extensions)
  12442. image-file-name-extensions)
  12443. t)
  12444. "\\'"))))
  12445. (defun org-file-image-p (file)
  12446. "Return non-nil if FILE is an image."
  12447. (save-match-data
  12448. (string-match (org-image-file-name-regexp) file)))
  12449. ;;; Paragraph filling stuff.
  12450. ;; We want this to be just right, so use the full arsenal.
  12451. (defun org-indent-line-function ()
  12452. "Indent line like previous, but further if previous was headline or item."
  12453. (interactive)
  12454. (let* ((pos (point))
  12455. (itemp (org-at-item-p))
  12456. column bpos bcol tpos tcol bullet btype bullet-type)
  12457. ;; Find the previous relevant line
  12458. (beginning-of-line 1)
  12459. (cond
  12460. ((looking-at "#") (setq column 0))
  12461. ((looking-at "\\*+ ") (setq column 0))
  12462. (t
  12463. (beginning-of-line 0)
  12464. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12465. (beginning-of-line 0))
  12466. (cond
  12467. ((looking-at "\\*+[ \t]+")
  12468. (if (not org-adapt-indentation)
  12469. (setq column 0)
  12470. (goto-char (match-end 0))
  12471. (setq column (current-column))))
  12472. ((org-in-item-p)
  12473. (org-beginning-of-item)
  12474. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12475. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12476. (setq bpos (match-beginning 1) tpos (match-end 0)
  12477. bcol (progn (goto-char bpos) (current-column))
  12478. tcol (progn (goto-char tpos) (current-column))
  12479. bullet (match-string 1)
  12480. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12481. (if (> tcol (+ bcol org-description-max-indent))
  12482. (setq tcol (+ bcol 5)))
  12483. (if (not itemp)
  12484. (setq column tcol)
  12485. (goto-char pos)
  12486. (beginning-of-line 1)
  12487. (if (looking-at "\\S-")
  12488. (progn
  12489. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12490. (setq bullet (match-string 1)
  12491. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12492. (setq column (if (equal btype bullet-type) bcol tcol)))
  12493. (setq column (org-get-indentation)))))
  12494. (t (setq column (org-get-indentation))))))
  12495. (goto-char pos)
  12496. (if (<= (current-column) (current-indentation))
  12497. (org-indent-line-to column)
  12498. (save-excursion (org-indent-line-to column)))
  12499. (setq column (current-column))
  12500. (beginning-of-line 1)
  12501. (if (looking-at
  12502. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12503. (replace-match (concat "\\1" (format org-property-format
  12504. (match-string 2) (match-string 3)))
  12505. t nil))
  12506. (org-move-to-column column)))
  12507. (defun org-set-autofill-regexps ()
  12508. (interactive)
  12509. ;; In the paragraph separator we include headlines, because filling
  12510. ;; text in a line directly attached to a headline would otherwise
  12511. ;; fill the headline as well.
  12512. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12513. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12514. ;; The paragraph starter includes hand-formatted lists.
  12515. (org-set-local 'paragraph-start
  12516. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12517. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12518. ;; But only if the user has not turned off tables or fixed-width regions
  12519. (org-set-local
  12520. 'auto-fill-inhibit-regexp
  12521. (concat "\\*+ \\|#\\+"
  12522. "\\|[ \t]*" org-keyword-time-regexp
  12523. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12524. (concat
  12525. "\\|[ \t]*["
  12526. (if org-enable-table-editor "|" "")
  12527. (if org-enable-fixed-width-editor ":" "")
  12528. "]"))))
  12529. ;; We use our own fill-paragraph function, to make sure that tables
  12530. ;; and fixed-width regions are not wrapped. That function will pass
  12531. ;; through to `fill-paragraph' when appropriate.
  12532. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12533. ; Adaptive filling: To get full control, first make sure that
  12534. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12535. (org-set-local 'adaptive-fill-regexp "\000")
  12536. (org-set-local 'adaptive-fill-function
  12537. 'org-adaptive-fill-function)
  12538. (org-set-local
  12539. 'align-mode-rules-list
  12540. '((org-in-buffer-settings
  12541. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12542. (modes . '(org-mode))))))
  12543. (defun org-fill-paragraph (&optional justify)
  12544. "Re-align a table, pass through to fill-paragraph if no table."
  12545. (let ((table-p (org-at-table-p))
  12546. (table.el-p (org-at-table.el-p)))
  12547. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12548. (save-excursion (goto-char (point-at-bol))
  12549. (looking-at outline-regexp)))
  12550. t) ; skip headlines
  12551. (table.el-p t) ; skip table.el tables
  12552. (table-p (org-table-align) t) ; align org-mode tables
  12553. (t nil)))) ; call paragraph-fill
  12554. ;; For reference, this is the default value of adaptive-fill-regexp
  12555. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12556. (defun org-adaptive-fill-function ()
  12557. "Return a fill prefix for org-mode files.
  12558. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12559. work correctly."
  12560. (cond ((looking-at "#[ \t]+")
  12561. (match-string 0))
  12562. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12563. (save-excursion
  12564. (if (> (match-end 1) (+ (match-beginning 1)
  12565. org-description-max-indent))
  12566. (goto-char (+ (match-beginning 1) 5))
  12567. (goto-char (match-end 0)))
  12568. (make-string (current-column) ?\ )))
  12569. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12570. (save-excursion
  12571. (goto-char (match-end 0))
  12572. (make-string (current-column) ?\ )))
  12573. (t nil)))
  12574. ;;; Other stuff.
  12575. (defun org-toggle-fixed-width-section (arg)
  12576. "Toggle the fixed-width export.
  12577. If there is no active region, the QUOTE keyword at the current headline is
  12578. inserted or removed. When present, it causes the text between this headline
  12579. and the next to be exported as fixed-width text, and unmodified.
  12580. If there is an active region, this command adds or removes a colon as the
  12581. first character of this line. If the first character of a line is a colon,
  12582. this line is also exported in fixed-width font."
  12583. (interactive "P")
  12584. (let* ((cc 0)
  12585. (regionp (org-region-active-p))
  12586. (beg (if regionp (region-beginning) (point)))
  12587. (end (if regionp (region-end)))
  12588. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12589. (case-fold-search nil)
  12590. (re "[ \t]*\\(:\\)")
  12591. off)
  12592. (if regionp
  12593. (save-excursion
  12594. (goto-char beg)
  12595. (setq cc (current-column))
  12596. (beginning-of-line 1)
  12597. (setq off (looking-at re))
  12598. (while (> nlines 0)
  12599. (setq nlines (1- nlines))
  12600. (beginning-of-line 1)
  12601. (cond
  12602. (arg
  12603. (org-move-to-column cc t)
  12604. (insert ":\n")
  12605. (forward-line -1))
  12606. ((and off (looking-at re))
  12607. (replace-match "" t t nil 1))
  12608. ((not off) (org-move-to-column cc t) (insert ":")))
  12609. (forward-line 1)))
  12610. (save-excursion
  12611. (org-back-to-heading)
  12612. (if (looking-at (concat outline-regexp
  12613. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12614. (replace-match "" t t nil 1)
  12615. (if (looking-at outline-regexp)
  12616. (progn
  12617. (goto-char (match-end 0))
  12618. (insert org-quote-string " "))))))))
  12619. ;;;; Functions extending outline functionality
  12620. (defun org-beginning-of-line (&optional arg)
  12621. "Go to the beginning of the current line. If that is invisible, continue
  12622. to a visible line beginning. This makes the function of C-a more intuitive.
  12623. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12624. first attempt, and only move to after the tags when the cursor is already
  12625. beyond the end of the headline."
  12626. (interactive "P")
  12627. (let ((pos (point)))
  12628. (beginning-of-line 1)
  12629. (if (bobp)
  12630. nil
  12631. (backward-char 1)
  12632. (if (org-invisible-p)
  12633. (while (and (not (bobp)) (org-invisible-p))
  12634. (backward-char 1)
  12635. (beginning-of-line 1))
  12636. (forward-char 1)))
  12637. (when org-special-ctrl-a/e
  12638. (cond
  12639. ((and (looking-at org-todo-line-regexp)
  12640. (= (char-after (match-end 1)) ?\ ))
  12641. (goto-char
  12642. (if (eq org-special-ctrl-a/e t)
  12643. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12644. ((= pos (point)) (match-beginning 3))
  12645. (t (point)))
  12646. (cond ((> pos (point)) (point))
  12647. ((not (eq last-command this-command)) (point))
  12648. (t (match-beginning 3))))))
  12649. ((org-at-item-p)
  12650. (goto-char
  12651. (if (eq org-special-ctrl-a/e t)
  12652. (cond ((> pos (match-end 4)) (match-end 4))
  12653. ((= pos (point)) (match-end 4))
  12654. (t (point)))
  12655. (cond ((> pos (point)) (point))
  12656. ((not (eq last-command this-command)) (point))
  12657. (t (match-end 4))))))))))
  12658. (defun org-end-of-line (&optional arg)
  12659. "Go to the end of the line.
  12660. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12661. first attempt, and only move to after the tags when the cursor is already
  12662. beyond the end of the headline."
  12663. (interactive "P")
  12664. (if (or (not org-special-ctrl-a/e)
  12665. (not (org-on-heading-p)))
  12666. (end-of-line arg)
  12667. (let ((pos (point)))
  12668. (beginning-of-line 1)
  12669. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12670. (if (eq org-special-ctrl-a/e t)
  12671. (if (or (< pos (match-beginning 1))
  12672. (= pos (match-end 0)))
  12673. (goto-char (match-beginning 1))
  12674. (goto-char (match-end 0)))
  12675. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12676. (goto-char (match-end 0))
  12677. (goto-char (match-beginning 1))))
  12678. (end-of-line arg)))))
  12679. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12680. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12681. (defun org-kill-line (&optional arg)
  12682. "Kill line, to tags or end of line."
  12683. (interactive "P")
  12684. (cond
  12685. ((or (not org-special-ctrl-k)
  12686. (bolp)
  12687. (not (org-on-heading-p)))
  12688. (call-interactively 'kill-line))
  12689. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12690. (kill-region (point) (match-beginning 1))
  12691. (org-set-tags nil t))
  12692. (t (kill-region (point) (point-at-eol)))))
  12693. (define-key org-mode-map "\C-k" 'org-kill-line)
  12694. (defun org-invisible-p ()
  12695. "Check if point is at a character currently not visible."
  12696. ;; Early versions of noutline don't have `outline-invisible-p'.
  12697. (if (fboundp 'outline-invisible-p)
  12698. (outline-invisible-p)
  12699. (get-char-property (point) 'invisible)))
  12700. (defun org-invisible-p2 ()
  12701. "Check if point is at a character currently not visible."
  12702. (save-excursion
  12703. (if (and (eolp) (not (bobp))) (backward-char 1))
  12704. ;; Early versions of noutline don't have `outline-invisible-p'.
  12705. (if (fboundp 'outline-invisible-p)
  12706. (outline-invisible-p)
  12707. (get-char-property (point) 'invisible))))
  12708. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12709. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12710. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12711. (defun org-at-heading-or-item-p ()
  12712. (or (org-on-heading-p) (org-at-item-p)))
  12713. (defun org-on-target-p ()
  12714. (or (org-in-regexp org-radio-target-regexp)
  12715. (org-in-regexp org-target-regexp)))
  12716. (defun org-up-heading-all (arg)
  12717. "Move to the heading line of which the present line is a subheading.
  12718. This function considers both visible and invisible heading lines.
  12719. With argument, move up ARG levels."
  12720. (if (fboundp 'outline-up-heading-all)
  12721. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12722. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12723. (defun org-up-heading-safe ()
  12724. "Move to the heading line of which the present line is a subheading.
  12725. This version will not throw an error. It will return the level of the
  12726. headline found, or nil if no higher level is found."
  12727. (let ((pos (point)) start-level level
  12728. (re (concat "^" outline-regexp)))
  12729. (catch 'exit
  12730. (outline-back-to-heading t)
  12731. (setq start-level (funcall outline-level))
  12732. (if (equal start-level 1) (throw 'exit nil))
  12733. (while (re-search-backward re nil t)
  12734. (setq level (funcall outline-level))
  12735. (if (< level start-level) (throw 'exit level)))
  12736. nil)))
  12737. (defun org-first-sibling-p ()
  12738. "Is this heading the first child of its parents?"
  12739. (interactive)
  12740. (let ((re (concat "^" outline-regexp))
  12741. level l)
  12742. (unless (org-at-heading-p t)
  12743. (error "Not at a heading"))
  12744. (setq level (funcall outline-level))
  12745. (save-excursion
  12746. (if (not (re-search-backward re nil t))
  12747. t
  12748. (setq l (funcall outline-level))
  12749. (< l level)))))
  12750. (defun org-goto-sibling (&optional previous)
  12751. "Goto the next sibling, even if it is invisible.
  12752. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12753. when a sibling was found. When none is found, return nil and don't
  12754. move point."
  12755. (let ((fun (if previous 're-search-backward 're-search-forward))
  12756. (pos (point))
  12757. (re (concat "^" outline-regexp))
  12758. level l)
  12759. (when (condition-case nil (org-back-to-heading t) (error nil))
  12760. (setq level (funcall outline-level))
  12761. (catch 'exit
  12762. (or previous (forward-char 1))
  12763. (while (funcall fun re nil t)
  12764. (setq l (funcall outline-level))
  12765. (when (< l level) (goto-char pos) (throw 'exit nil))
  12766. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12767. (goto-char pos)
  12768. nil))))
  12769. (defun org-show-siblings ()
  12770. "Show all siblings of the current headline."
  12771. (save-excursion
  12772. (while (org-goto-sibling) (org-flag-heading nil)))
  12773. (save-excursion
  12774. (while (org-goto-sibling 'previous)
  12775. (org-flag-heading nil))))
  12776. (defun org-show-hidden-entry ()
  12777. "Show an entry where even the heading is hidden."
  12778. (save-excursion
  12779. (org-show-entry)))
  12780. (defun org-flag-heading (flag &optional entry)
  12781. "Flag the current heading. FLAG non-nil means make invisible.
  12782. When ENTRY is non-nil, show the entire entry."
  12783. (save-excursion
  12784. (org-back-to-heading t)
  12785. ;; Check if we should show the entire entry
  12786. (if entry
  12787. (progn
  12788. (org-show-entry)
  12789. (save-excursion
  12790. (and (outline-next-heading)
  12791. (org-flag-heading nil))))
  12792. (outline-flag-region (max (point-min) (1- (point)))
  12793. (save-excursion (outline-end-of-heading) (point))
  12794. flag))))
  12795. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12796. ;; This is an exact copy of the original function, but it uses
  12797. ;; `org-back-to-heading', to make it work also in invisible
  12798. ;; trees. And is uses an invisible-OK argument.
  12799. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12800. (org-back-to-heading invisible-OK)
  12801. (let ((first t)
  12802. (level (funcall outline-level)))
  12803. (while (and (not (eobp))
  12804. (or first (> (funcall outline-level) level)))
  12805. (setq first nil)
  12806. (outline-next-heading))
  12807. (unless to-heading
  12808. (if (memq (preceding-char) '(?\n ?\^M))
  12809. (progn
  12810. ;; Go to end of line before heading
  12811. (forward-char -1)
  12812. (if (memq (preceding-char) '(?\n ?\^M))
  12813. ;; leave blank line before heading
  12814. (forward-char -1))))))
  12815. (point))
  12816. (defun org-show-subtree ()
  12817. "Show everything after this heading at deeper levels."
  12818. (outline-flag-region
  12819. (point)
  12820. (save-excursion
  12821. (outline-end-of-subtree) (outline-next-heading) (point))
  12822. nil))
  12823. (defun org-show-entry ()
  12824. "Show the body directly following this heading.
  12825. Show the heading too, if it is currently invisible."
  12826. (interactive)
  12827. (save-excursion
  12828. (condition-case nil
  12829. (progn
  12830. (org-back-to-heading t)
  12831. (outline-flag-region
  12832. (max (point-min) (1- (point)))
  12833. (save-excursion
  12834. (re-search-forward
  12835. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12836. (or (match-beginning 1) (point-max)))
  12837. nil))
  12838. (error nil))))
  12839. (defun org-make-options-regexp (kwds)
  12840. "Make a regular expression for keyword lines."
  12841. (concat
  12842. "^"
  12843. "#?[ \t]*\\+\\("
  12844. (mapconcat 'regexp-quote kwds "\\|")
  12845. "\\):[ \t]*"
  12846. "\\(.+\\)"))
  12847. ;; Make isearch reveal the necessary context
  12848. (defun org-isearch-end ()
  12849. "Reveal context after isearch exits."
  12850. (when isearch-success ; only if search was successful
  12851. (if (featurep 'xemacs)
  12852. ;; Under XEmacs, the hook is run in the correct place,
  12853. ;; we directly show the context.
  12854. (org-show-context 'isearch)
  12855. ;; In Emacs the hook runs *before* restoring the overlays.
  12856. ;; So we have to use a one-time post-command-hook to do this.
  12857. ;; (Emacs 22 has a special variable, see function `org-mode')
  12858. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12859. isearch-mode-end-hook-quit)
  12860. ;; Only when the isearch was not quitted.
  12861. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12862. 'append 'local)))))
  12863. (defun org-isearch-post-command ()
  12864. "Remove self from hook, and show context."
  12865. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12866. (org-show-context 'isearch))
  12867. ;;;; Integration with and fixes for other packages
  12868. ;;; Imenu support
  12869. (defvar org-imenu-markers nil
  12870. "All markers currently used by Imenu.")
  12871. (make-variable-buffer-local 'org-imenu-markers)
  12872. (defun org-imenu-new-marker (&optional pos)
  12873. "Return a new marker for use by Imenu, and remember the marker."
  12874. (let ((m (make-marker)))
  12875. (move-marker m (or pos (point)))
  12876. (push m org-imenu-markers)
  12877. m))
  12878. (defun org-imenu-get-tree ()
  12879. "Produce the index for Imenu."
  12880. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12881. (setq org-imenu-markers nil)
  12882. (let* ((n org-imenu-depth)
  12883. (re (concat "^" outline-regexp))
  12884. (subs (make-vector (1+ n) nil))
  12885. (last-level 0)
  12886. m tree level head)
  12887. (save-excursion
  12888. (save-restriction
  12889. (widen)
  12890. (goto-char (point-max))
  12891. (while (re-search-backward re nil t)
  12892. (setq level (org-reduced-level (funcall outline-level)))
  12893. (when (<= level n)
  12894. (looking-at org-complex-heading-regexp)
  12895. (setq head (org-match-string-no-properties 4)
  12896. m (org-imenu-new-marker))
  12897. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12898. (if (>= level last-level)
  12899. (push (cons head m) (aref subs level))
  12900. (push (cons head (aref subs (1+ level))) (aref subs level))
  12901. (loop for i from (1+ level) to n do (aset subs i nil)))
  12902. (setq last-level level)))))
  12903. (aref subs 1)))
  12904. (eval-after-load "imenu"
  12905. '(progn
  12906. (add-hook 'imenu-after-jump-hook
  12907. (lambda () (org-show-context 'org-goto)))))
  12908. ;; Speedbar support
  12909. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12910. "Overlay marking the agenda restriction line in speedbar.")
  12911. (org-overlay-put org-speedbar-restriction-lock-overlay
  12912. 'face 'org-agenda-restriction-lock)
  12913. (org-overlay-put org-speedbar-restriction-lock-overlay
  12914. 'help-echo "Agendas are currently limited to this item.")
  12915. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12916. (defun org-speedbar-set-agenda-restriction ()
  12917. "Restrict future agenda commands to the location at point in speedbar.
  12918. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12919. (interactive)
  12920. (require 'org-agenda)
  12921. (let (p m tp np dir txt w)
  12922. (cond
  12923. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12924. 'org-imenu t))
  12925. (setq m (get-text-property p 'org-imenu-marker))
  12926. (save-excursion
  12927. (save-restriction
  12928. (set-buffer (marker-buffer m))
  12929. (goto-char m)
  12930. (org-agenda-set-restriction-lock 'subtree))))
  12931. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12932. 'speedbar-function 'speedbar-find-file))
  12933. (setq tp (previous-single-property-change
  12934. (1+ p) 'speedbar-function)
  12935. np (next-single-property-change
  12936. tp 'speedbar-function)
  12937. dir (speedbar-line-directory)
  12938. txt (buffer-substring-no-properties (or tp (point-min))
  12939. (or np (point-max))))
  12940. (save-excursion
  12941. (save-restriction
  12942. (set-buffer (find-file-noselect
  12943. (let ((default-directory dir))
  12944. (expand-file-name txt))))
  12945. (unless (org-mode-p)
  12946. (error "Cannot restrict to non-Org-mode file"))
  12947. (org-agenda-set-restriction-lock 'file))))
  12948. (t (error "Don't know how to restrict Org-mode's agenda")))
  12949. (org-move-overlay org-speedbar-restriction-lock-overlay
  12950. (point-at-bol) (point-at-eol))
  12951. (setq current-prefix-arg nil)
  12952. (org-agenda-maybe-redo)))
  12953. (eval-after-load "speedbar"
  12954. '(progn
  12955. (speedbar-add-supported-extension ".org")
  12956. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12957. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12958. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12959. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12960. (add-hook 'speedbar-visiting-tag-hook
  12961. (lambda () (org-show-context 'org-goto)))))
  12962. ;;; Fixes and Hacks for problems with other packages
  12963. ;; Make flyspell not check words in links, to not mess up our keymap
  12964. (defun org-mode-flyspell-verify ()
  12965. "Don't let flyspell put overlays at active buttons."
  12966. (not (get-text-property (point) 'keymap)))
  12967. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12968. (eval-after-load "bookmark"
  12969. '(if (boundp 'bookmark-after-jump-hook)
  12970. ;; We can use the hook
  12971. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12972. ;; Hook not available, use advice
  12973. (defadvice bookmark-jump (after org-make-visible activate)
  12974. "Make the position visible."
  12975. (org-bookmark-jump-unhide))))
  12976. (defun org-bookmark-jump-unhide ()
  12977. "Unhide the current position, to show the bookmark location."
  12978. (and (org-mode-p)
  12979. (or (org-invisible-p)
  12980. (save-excursion (goto-char (max (point-min) (1- (point))))
  12981. (org-invisible-p)))
  12982. (org-show-context 'bookmark-jump)))
  12983. ;; Make session.el ignore our circular variable
  12984. (eval-after-load "session"
  12985. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12986. ;;;; Experimental code
  12987. (defun org-closed-in-range ()
  12988. "Sparse tree of items closed in a certain time range.
  12989. Still experimental, may disappear in the future."
  12990. (interactive)
  12991. ;; Get the time interval from the user.
  12992. (let* ((time1 (time-to-seconds
  12993. (org-read-date nil 'to-time nil "Starting date: ")))
  12994. (time2 (time-to-seconds
  12995. (org-read-date nil 'to-time nil "End date:")))
  12996. ;; callback function
  12997. (callback (lambda ()
  12998. (let ((time
  12999. (time-to-seconds
  13000. (apply 'encode-time
  13001. (org-parse-time-string
  13002. (match-string 1))))))
  13003. ;; check if time in interval
  13004. (and (>= time time1) (<= time time2))))))
  13005. ;; make tree, check each match with the callback
  13006. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13007. ;;;; Finish up
  13008. (provide 'org)
  13009. (run-hooks 'org-load-hook)
  13010. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13011. ;;; org.el ends here