org.el 525 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192
  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.08
  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. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.08"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (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)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  162. (const :tag "C id: Global id's for identifying entries" org-id)
  163. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  164. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  165. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  166. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  167. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  168. (const :tag "C registry: A registry for Org links" org-registry)
  169. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  170. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  171. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  172. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  173. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  174. (defgroup org-startup nil
  175. "Options concerning startup of Org-mode."
  176. :tag "Org Startup"
  177. :group 'org)
  178. (defcustom org-startup-folded t
  179. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  180. This can also be configured on a per-file basis by adding one of
  181. the following lines anywhere in the buffer:
  182. #+STARTUP: fold
  183. #+STARTUP: nofold
  184. #+STARTUP: content"
  185. :group 'org-startup
  186. :type '(choice
  187. (const :tag "nofold: show all" nil)
  188. (const :tag "fold: overview" t)
  189. (const :tag "content: all headlines" content)))
  190. (defcustom org-startup-truncated t
  191. "Non-nil means, entering Org-mode will set `truncate-lines'.
  192. This is useful since some lines containing links can be very long and
  193. uninteresting. Also tables look terrible when wrapped."
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-startup-align-all-tables nil
  197. "Non-nil means, align all tables when visiting a file.
  198. This is useful when the column width in tables is forced with <N> cookies
  199. in table fields. Such tables will look correct only after the first re-align.
  200. This can also be configured on a per-file basis by adding one of
  201. the following lines anywhere in the buffer:
  202. #+STARTUP: align
  203. #+STARTUP: noalign"
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-insert-mode-line-in-empty-file nil
  207. "Non-nil means insert the first line setting Org-mode in empty files.
  208. When the function `org-mode' is called interactively in an empty file, this
  209. normally means that the file name does not automatically trigger Org-mode.
  210. To ensure that the file will always be in Org-mode in the future, a
  211. line enforcing Org-mode will be inserted into the buffer, if this option
  212. has been set."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-replace-disputed-keys nil
  216. "Non-nil means use alternative key bindings for some keys.
  217. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  218. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  219. If you want to use Org-mode together with one of these other modes,
  220. or more generally if you would like to move some Org-mode commands to
  221. other keys, set this variable and configure the keys with the variable
  222. `org-disputed-keys'.
  223. This option is only relevant at load-time of Org-mode, and must be set
  224. *before* org.el is loaded. Changing it requires a restart of Emacs to
  225. become effective."
  226. :group 'org-startup
  227. :type 'boolean)
  228. (defcustom org-use-extra-keys nil
  229. "Non-nil means use extra key sequence definitions for certain
  230. commands. This happens automatically if you run XEmacs or if
  231. window-system is nil. This variable lets you do the same
  232. manually. You must set it before loading org.
  233. Example: on Carbon Emacs 22 running graphically, with an external
  234. keyboard on a Powerbook, the default way of setting M-left might
  235. not work for either Alt or ESC. Setting this variable will make
  236. it work for ESC."
  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. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  491. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  492. org-cycle-hide-drawers
  493. org-cycle-show-empty-lines
  494. org-optimize-window-after-visibility-change)
  495. "Hook that is run after `org-cycle' has changed the buffer visibility.
  496. The function(s) in this hook must accept a single argument which indicates
  497. the new state that was set by the most recent `org-cycle' command. The
  498. argument is a symbol. After a global state change, it can have the values
  499. `overview', `content', or `all'. After a local state change, it can have
  500. the values `folded', `children', or `subtree'."
  501. :group 'org-cycle
  502. :type 'hook)
  503. (defgroup org-edit-structure nil
  504. "Options concerning structure editing in Org-mode."
  505. :tag "Org Edit Structure"
  506. :group 'org-structure)
  507. (defcustom org-odd-levels-only nil
  508. "Non-nil means, skip even levels and only use odd levels for the outline.
  509. This has the effect that two stars are being added/taken away in
  510. promotion/demotion commands. It also influences how levels are
  511. handled by the exporters.
  512. Changing it requires restart of `font-lock-mode' to become effective
  513. for fontification also in regions already fontified.
  514. You may also set this on a per-file basis by adding one of the following
  515. lines to the buffer:
  516. #+STARTUP: odd
  517. #+STARTUP: oddeven"
  518. :group 'org-edit-structure
  519. :group 'org-font-lock
  520. :type 'boolean)
  521. (defcustom org-adapt-indentation t
  522. "Non-nil means, adapt indentation when promoting and demoting.
  523. When this is set and the *entire* text in an entry is indented, the
  524. indentation is increased by one space in a demotion command, and
  525. decreased by one in a promotion command. If any line in the entry
  526. body starts at column 0, indentation is not changed at all."
  527. :group 'org-edit-structure
  528. :type 'boolean)
  529. (defcustom org-special-ctrl-a/e nil
  530. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  531. When t, `C-a' will bring back the cursor to the beginning of the
  532. headline text, i.e. after the stars and after a possible TODO keyword.
  533. In an item, this will be the position after the bullet.
  534. When the cursor is already at that position, another `C-a' will bring
  535. it to the beginning of the line.
  536. `C-e' will jump to the end of the headline, ignoring the presence of tags
  537. in the headline. A second `C-e' will then jump to the true end of the
  538. line, after any tags.
  539. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  540. and only a directly following, identical keypress will bring the cursor
  541. to the special positions."
  542. :group 'org-edit-structure
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "after bullet first" t)
  546. (const :tag "border first" reversed)))
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  549. (defcustom org-special-ctrl-k nil
  550. "Non-nil means `C-k' will behave specially in headlines.
  551. When nil, `C-k' will call the default `kill-line' command.
  552. When t, the following will happen while the cursor is in the headline:
  553. - When the cursor is at the beginning of a headline, kill the entire
  554. line and possible the folded subtree below the line.
  555. - When in the middle of the headline text, kill the headline up to the tags.
  556. - When after the headline text, kill the tags."
  557. :group 'org-edit-structure
  558. :type 'boolean)
  559. (defcustom org-yank-folded-subtrees t
  560. "Non-nil means, when yanking subtrees, fold them.
  561. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  562. it starts with a heading and all other headings in it are either children
  563. or siblings, then fold all the subtrees."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-M-RET-may-split-line '((default . t))
  567. "Non-nil means, M-RET will split the line at the cursor position.
  568. When nil, it will go to the end of the line before making a
  569. new line.
  570. You may also set this option in a different way for different
  571. contexts. Valid contexts are:
  572. headline when creating a new headline
  573. item when creating a new item
  574. table in a table field
  575. default the value to be used for all contexts not explicitly
  576. customized"
  577. :group 'org-structure
  578. :group 'org-table
  579. :type '(choice
  580. (const :tag "Always" t)
  581. (const :tag "Never" nil)
  582. (repeat :greedy t :tag "Individual contexts"
  583. (cons
  584. (choice :tag "Context"
  585. (const headline)
  586. (const item)
  587. (const table)
  588. (const default))
  589. (boolean)))))
  590. (defcustom org-insert-heading-respect-content nil
  591. "Non-nil means, insert new headings after the current subtree.
  592. When nil, the new heading is created directly after the current line.
  593. The commands \\[org-insert-heading-respect-content] and
  594. \\[org-insert-todo-heading-respect-content] turn this variable on
  595. for the duration of the command."
  596. :group 'org-structure
  597. :type 'boolean)
  598. (defcustom org-blank-before-new-entry '((heading . nil)
  599. (plain-list-item . nil))
  600. "Should `org-insert-heading' leave a blank line before new heading/item?
  601. The value is an alist, with `heading' and `plain-list-item' as car,
  602. and a boolean flag as cdr."
  603. :group 'org-edit-structure
  604. :type '(list
  605. (cons (const heading) (boolean))
  606. (cons (const plain-list-item) (boolean))))
  607. (defcustom org-insert-heading-hook nil
  608. "Hook being run after inserting a new heading."
  609. :group 'org-edit-structure
  610. :type 'hook)
  611. (defcustom org-enable-fixed-width-editor t
  612. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  613. This currently only means, they are never auto-wrapped.
  614. When nil, such lines will be treated like ordinary lines.
  615. See also the QUOTE keyword."
  616. :group 'org-edit-structure
  617. :type 'boolean)
  618. (defcustom org-edit-src-region-extra nil
  619. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  620. For examples see the function `org-edit-src-find-region-and-lang'.
  621. The regular expression identifying the begin marker should end with a newline,
  622. and the regexp marking the end line should start with a newline, to make sure
  623. there are kept outside the narrowed region."
  624. :group 'org-edit-structure
  625. :type '(repeat
  626. (list
  627. (regexp :tag "begin regexp")
  628. (regexp :tag "end regexp")
  629. (choice :tag "language"
  630. (string :tag "specify")
  631. (integer :tag "from match group")
  632. (const :tag "from `lang' element")
  633. (const :tag "from `style' element")))))
  634. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  635. "The mode that should be used to edit fixed-width regions.
  636. These are the regions where each line starts with a colon."
  637. :group 'org-edit-structure
  638. :type '(choice
  639. (const artist-mode)
  640. (const picture-mode)
  641. (const fundamental-mode)
  642. (function :tag "Other (specify)")))
  643. (defcustom org-goto-auto-isearch t
  644. "Non-nil means, typing characters in org-goto starts incremental search."
  645. :group 'org-edit-structure
  646. :type 'boolean)
  647. (defgroup org-sparse-trees nil
  648. "Options concerning sparse trees in Org-mode."
  649. :tag "Org Sparse Trees"
  650. :group 'org-structure)
  651. (defcustom org-highlight-sparse-tree-matches t
  652. "Non-nil means, highlight all matches that define a sparse tree.
  653. The highlights will automatically disappear the next time the buffer is
  654. changed by an edit command."
  655. :group 'org-sparse-trees
  656. :type 'boolean)
  657. (defcustom org-remove-highlights-with-change t
  658. "Non-nil means, any change to the buffer will remove temporary highlights.
  659. Such highlights are created by `org-occur' and `org-clock-display'.
  660. When nil, `C-c C-c needs to be used to get rid of the highlights.
  661. The highlights created by `org-preview-latex-fragment' always need
  662. `C-c C-c' to be removed."
  663. :group 'org-sparse-trees
  664. :group 'org-time
  665. :type 'boolean)
  666. (defcustom org-occur-hook '(org-first-headline-recenter)
  667. "Hook that is run after `org-occur' has constructed a sparse tree.
  668. This can be used to recenter the window to show as much of the structure
  669. as possible."
  670. :group 'org-sparse-trees
  671. :type 'hook)
  672. (defgroup org-imenu-and-speedbar nil
  673. "Options concerning imenu and speedbar in Org-mode."
  674. :tag "Org Imenu and Speedbar"
  675. :group 'org-structure)
  676. (defcustom org-imenu-depth 2
  677. "The maximum level for Imenu access to Org-mode headlines.
  678. This also applied for speedbar access."
  679. :group 'org-imenu-and-speedbar
  680. :type 'number)
  681. (defgroup org-table nil
  682. "Options concerning tables in Org-mode."
  683. :tag "Org Table"
  684. :group 'org)
  685. (defcustom org-enable-table-editor 'optimized
  686. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  687. When nil, such lines will be treated like ordinary lines.
  688. When equal to the symbol `optimized', the table editor will be optimized to
  689. do the following:
  690. - Automatic overwrite mode in front of whitespace in table fields.
  691. This makes the structure of the table stay in tact as long as the edited
  692. field does not exceed the column width.
  693. - Minimize the number of realigns. Normally, the table is aligned each time
  694. TAB or RET are pressed to move to another field. With optimization this
  695. happens only if changes to a field might have changed the column width.
  696. Optimization requires replacing the functions `self-insert-command',
  697. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  698. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  699. very good at guessing when a re-align will be necessary, but you can always
  700. force one with \\[org-ctrl-c-ctrl-c].
  701. If you would like to use the optimized version in Org-mode, but the
  702. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  703. This variable can be used to turn on and off the table editor during a session,
  704. but in order to toggle optimization, a restart is required.
  705. See also the variable `org-table-auto-blank-field'."
  706. :group 'org-table
  707. :type '(choice
  708. (const :tag "off" nil)
  709. (const :tag "on" t)
  710. (const :tag "on, optimized" optimized)))
  711. (defcustom org-table-tab-recognizes-table.el t
  712. "Non-nil means, TAB will automatically notice a table.el table.
  713. When it sees such a table, it moves point into it and - if necessary -
  714. calls `table-recognize-table'."
  715. :group 'org-table-editing
  716. :type 'boolean)
  717. (defgroup org-link nil
  718. "Options concerning links in Org-mode."
  719. :tag "Org Link"
  720. :group 'org)
  721. (defvar org-link-abbrev-alist-local nil
  722. "Buffer-local version of `org-link-abbrev-alist', which see.
  723. The value of this is taken from the #+LINK lines.")
  724. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  725. (defcustom org-link-abbrev-alist nil
  726. "Alist of link abbreviations.
  727. The car of each element is a string, to be replaced at the start of a link.
  728. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  729. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  730. [[linkkey:tag][description]]
  731. If REPLACE is a string, the tag will simply be appended to create the link.
  732. If the string contains \"%s\", the tag will be inserted there.
  733. REPLACE may also be a function that will be called with the tag as the
  734. only argument to create the link, which should be returned as a string.
  735. See the manual for examples."
  736. :group 'org-link
  737. :type 'alist)
  738. (defcustom org-descriptive-links t
  739. "Non-nil means, hide link part and only show description of bracket links.
  740. Bracket links are like [[link][descritpion]]. This variable sets the initial
  741. state in new org-mode buffers. The setting can then be toggled on a
  742. per-buffer basis from the Org->Hyperlinks menu."
  743. :group 'org-link
  744. :type 'boolean)
  745. (defcustom org-link-file-path-type 'adaptive
  746. "How the path name in file links should be stored.
  747. Valid values are:
  748. relative Relative to the current directory, i.e. the directory of the file
  749. into which the link is being inserted.
  750. absolute Absolute path, if possible with ~ for home directory.
  751. noabbrev Absolute path, no abbreviation of home directory.
  752. adaptive Use relative path for files in the current directory and sub-
  753. directories of it. For other files, use an absolute path."
  754. :group 'org-link
  755. :type '(choice
  756. (const relative)
  757. (const absolute)
  758. (const noabbrev)
  759. (const adaptive)))
  760. (defcustom org-activate-links '(bracket angle plain radio tag date)
  761. "Types of links that should be activated in Org-mode files.
  762. This is a list of symbols, each leading to the activation of a certain link
  763. type. In principle, it does not hurt to turn on most link types - there may
  764. be a small gain when turning off unused link types. The types are:
  765. bracket The recommended [[link][description]] or [[link]] links with hiding.
  766. angular Links in angular brackes that may contain whitespace like
  767. <bbdb:Carsten Dominik>.
  768. plain Plain links in normal text, no whitespace, like http://google.com.
  769. radio Text that is matched by a radio target, see manual for details.
  770. tag Tag settings in a headline (link to tag search).
  771. date Time stamps (link to calendar).
  772. Changing this variable requires a restart of Emacs to become effective."
  773. :group 'org-link
  774. :type '(set (const :tag "Double bracket links (new style)" bracket)
  775. (const :tag "Angular bracket links (old style)" angular)
  776. (const :tag "Plain text links" plain)
  777. (const :tag "Radio target matches" radio)
  778. (const :tag "Tags" tag)
  779. (const :tag "Timestamps" date)))
  780. (defcustom org-make-link-description-function nil
  781. "Function to use to generate link descriptions from links. If
  782. nil the link location will be used. This function must take two
  783. parameters; the first is the link and the second the description
  784. org-insert-link has generated, and should return the description
  785. to use."
  786. :group 'org-link
  787. :type 'function)
  788. (defgroup org-link-store nil
  789. "Options concerning storing links in Org-mode."
  790. :tag "Org Store Link"
  791. :group 'org-link)
  792. (defcustom org-email-link-description-format "Email %c: %.30s"
  793. "Format of the description part of a link to an email or usenet message.
  794. The following %-excapes will be replaced by corresponding information:
  795. %F full \"From\" field
  796. %f name, taken from \"From\" field, address if no name
  797. %T full \"To\" field
  798. %t first name in \"To\" field, address if no name
  799. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  800. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  801. %s subject
  802. %m message-id.
  803. You may use normal field width specification between the % and the letter.
  804. This is for example useful to limit the length of the subject.
  805. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  806. :group 'org-link-store
  807. :type 'string)
  808. (defcustom org-from-is-user-regexp
  809. (let (r1 r2)
  810. (when (and user-mail-address (not (string= user-mail-address "")))
  811. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  812. (when (and user-full-name (not (string= user-full-name "")))
  813. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  814. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  815. "Regexp mached against the \"From:\" header of an email or usenet message.
  816. It should match if the message is from the user him/herself."
  817. :group 'org-link-store
  818. :type 'regexp)
  819. (defcustom org-context-in-file-links t
  820. "Non-nil means, file links from `org-store-link' contain context.
  821. A search string will be added to the file name with :: as separator and
  822. used to find the context when the link is activated by the command
  823. `org-open-at-point'.
  824. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  825. negates this setting for the duration of the command."
  826. :group 'org-link-store
  827. :type 'boolean)
  828. (defcustom org-keep-stored-link-after-insertion nil
  829. "Non-nil means, keep link in list for entire session.
  830. The command `org-store-link' adds a link pointing to the current
  831. location to an internal list. These links accumulate during a session.
  832. The command `org-insert-link' can be used to insert links into any
  833. Org-mode file (offering completion for all stored links). When this
  834. option is nil, every link which has been inserted once using \\[org-insert-link]
  835. will be removed from the list, to make completing the unused links
  836. more efficient."
  837. :group 'org-link-store
  838. :type 'boolean)
  839. (defgroup org-link-follow nil
  840. "Options concerning following links in Org-mode."
  841. :tag "Org Follow Link"
  842. :group 'org-link)
  843. (defcustom org-follow-link-hook nil
  844. "Hook that is run after a link has been followed."
  845. :group 'org-link-follow
  846. :type 'hook)
  847. (defcustom org-tab-follows-link nil
  848. "Non-nil means, on links TAB will follow the link.
  849. Needs to be set before org.el is loaded."
  850. :group 'org-link-follow
  851. :type 'boolean)
  852. (defcustom org-return-follows-link nil
  853. "Non-nil means, on links RET will follow the link.
  854. Needs to be set before org.el is loaded."
  855. :group 'org-link-follow
  856. :type 'boolean)
  857. (defcustom org-mouse-1-follows-link
  858. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  859. "Non-nil means, mouse-1 on a link will follow the link.
  860. A longer mouse click will still set point. Does not work on XEmacs.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mark-ring-length 4
  865. "Number of different positions to be recorded in the ring
  866. Changing this requires a restart of Emacs to work correctly."
  867. :group 'org-link-follow
  868. :type 'interger)
  869. (defcustom org-link-frame-setup
  870. '((vm . vm-visit-folder-other-frame)
  871. (gnus . gnus-other-frame)
  872. (file . find-file-other-window))
  873. "Setup the frame configuration for following links.
  874. When following a link with Emacs, it may often be useful to display
  875. this link in another window or frame. This variable can be used to
  876. set this up for the different types of links.
  877. For VM, use any of
  878. `vm-visit-folder'
  879. `vm-visit-folder-other-frame'
  880. For Gnus, use any of
  881. `gnus'
  882. `gnus-other-frame'
  883. For FILE, use any of
  884. `find-file'
  885. `find-file-other-window'
  886. `find-file-other-frame'
  887. For the calendar, use the variable `calendar-setup'.
  888. For BBDB, it is currently only possible to display the matches in
  889. another window."
  890. :group 'org-link-follow
  891. :type '(list
  892. (cons (const vm)
  893. (choice
  894. (const vm-visit-folder)
  895. (const vm-visit-folder-other-window)
  896. (const vm-visit-folder-other-frame)))
  897. (cons (const gnus)
  898. (choice
  899. (const gnus)
  900. (const gnus-other-frame)))
  901. (cons (const file)
  902. (choice
  903. (const find-file)
  904. (const find-file-other-window)
  905. (const find-file-other-frame)))))
  906. (defcustom org-display-internal-link-with-indirect-buffer nil
  907. "Non-nil means, use indirect buffer to display infile links.
  908. Activating internal links (from one location in a file to another location
  909. in the same file) normally just jumps to the location. When the link is
  910. activated with a C-u prefix (or with mouse-3), the link is displayed in
  911. another window. When this option is set, the other window actually displays
  912. an indirect buffer clone of the current buffer, to avoid any visibility
  913. changes to the current buffer."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-open-non-existing-files nil
  917. "Non-nil means, `org-open-file' will open non-existing files.
  918. When nil, an error will be generated."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-open-directory-means-index-dot-org nil
  922. "Non-nil means, a link to a directory really means to index.org.
  923. When nil, following a directory link will run dired or open a finder/explorer
  924. window on that directory."
  925. :group 'org-link-follow
  926. :type 'boolean)
  927. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  928. "Function and arguments to call for following mailto links.
  929. This is a list with the first element being a lisp function, and the
  930. remaining elements being arguments to the function. In string arguments,
  931. %a will be replaced by the address, and %s will be replaced by the subject
  932. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  933. :group 'org-link-follow
  934. :type '(choice
  935. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  936. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  937. (const :tag "message-mail" (message-mail "%a" "%s"))
  938. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  939. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  940. "Non-nil means, ask for confirmation before executing shell links.
  941. Shell links can be dangerous: just think about a link
  942. [[shell:rm -rf ~/*][Google Search]]
  943. This link would show up in your Org-mode document as \"Google Search\",
  944. but really it would remove your entire home directory.
  945. Therefore we advise against setting this variable to nil.
  946. Just change it to `y-or-n-p' of you want to confirm with a
  947. single keystroke rather than having to type \"yes\"."
  948. :group 'org-link-follow
  949. :type '(choice
  950. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  951. (const :tag "with y-or-n (faster)" y-or-n-p)
  952. (const :tag "no confirmation (dangerous)" nil)))
  953. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  954. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  955. Elisp links can be dangerous: just think about a link
  956. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  957. This link would show up in your Org-mode document as \"Google Search\",
  958. but really it would remove your entire home directory.
  959. Therefore we advise against setting this variable to nil.
  960. Just change it to `y-or-n-p' of you want to confirm with a
  961. single keystroke rather than having to type \"yes\"."
  962. :group 'org-link-follow
  963. :type '(choice
  964. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  965. (const :tag "with y-or-n (faster)" y-or-n-p)
  966. (const :tag "no confirmation (dangerous)" nil)))
  967. (defconst org-file-apps-defaults-gnu
  968. '((remote . emacs)
  969. (t . mailcap))
  970. "Default file applications on a UNIX or GNU/Linux system.
  971. See `org-file-apps'.")
  972. (defconst org-file-apps-defaults-macosx
  973. '((remote . emacs)
  974. (t . "open %s")
  975. ("ps.gz" . "gv %s")
  976. ("eps.gz" . "gv %s")
  977. ("dvi" . "xdvi %s")
  978. ("fig" . "xfig %s"))
  979. "Default file applications on a MacOS X system.
  980. The system \"open\" is known as a default, but we use X11 applications
  981. for some files for which the OS does not have a good default.
  982. See `org-file-apps'.")
  983. (defconst org-file-apps-defaults-windowsnt
  984. (list
  985. '(remote . emacs)
  986. (cons t
  987. (list (if (featurep 'xemacs)
  988. 'mswindows-shell-execute
  989. 'w32-shell-execute)
  990. "open" 'file)))
  991. "Default file applications on a Windows NT system.
  992. The system \"open\" is used for most files.
  993. See `org-file-apps'.")
  994. (defcustom org-file-apps
  995. '(
  996. ("txt" . emacs)
  997. ("tex" . emacs)
  998. ("ltx" . emacs)
  999. ("org" . emacs)
  1000. ("el" . emacs)
  1001. ("bib" . emacs)
  1002. )
  1003. "External applications for opening `file:path' items in a document.
  1004. Org-mode uses system defaults for different file types, but
  1005. you can use this variable to set the application for a given file
  1006. extension. The entries in this list are cons cells where the car identifies
  1007. files and the cdr the corresponding command. Possible values for the
  1008. file identifier are
  1009. \"ext\" A string identifying an extension
  1010. `directory' Matches a directory
  1011. `remote' Matches a remote file, accessible through tramp or efs.
  1012. Remote files most likely should be visited through Emacs
  1013. because external applications cannot handle such paths.
  1014. t Default for all remaining files
  1015. Possible values for the command are:
  1016. `emacs' The file will be visited by the current Emacs process.
  1017. `default' Use the default application for this file type.
  1018. string A command to be executed by a shell; %s will be replaced
  1019. by the path to the file.
  1020. sexp A Lisp form which will be evaluated. The file path will
  1021. be available in the Lisp variable `file'.
  1022. For more examples, see the system specific constants
  1023. `org-file-apps-defaults-macosx'
  1024. `org-file-apps-defaults-windowsnt'
  1025. `org-file-apps-defaults-gnu'."
  1026. :group 'org-link-follow
  1027. :type '(repeat
  1028. (cons (choice :value ""
  1029. (string :tag "Extension")
  1030. (const :tag "Default for unrecognized files" t)
  1031. (const :tag "Remote file" remote)
  1032. (const :tag "Links to a directory" directory))
  1033. (choice :value ""
  1034. (const :tag "Visit with Emacs" emacs)
  1035. (const :tag "Use system default" default)
  1036. (string :tag "Command")
  1037. (sexp :tag "Lisp form")))))
  1038. (defgroup org-refile nil
  1039. "Options concerning refiling entries in Org-mode."
  1040. :tag "Org Remember"
  1041. :group 'org)
  1042. (defcustom org-directory "~/org"
  1043. "Directory with org files.
  1044. This directory will be used as default to prompt for org files.
  1045. Used by the hooks for remember.el."
  1046. :group 'org-refile
  1047. :group 'org-remember
  1048. :type 'directory)
  1049. (defcustom org-default-notes-file "~/.notes"
  1050. "Default target for storing notes.
  1051. Used by the hooks for remember.el. This can be a string, or nil to mean
  1052. the value of `remember-data-file'.
  1053. You can set this on a per-template basis with the variable
  1054. `org-remember-templates'."
  1055. :group 'org-refile
  1056. :group 'org-remember
  1057. :type '(choice
  1058. (const :tag "Default from remember-data-file" nil)
  1059. file))
  1060. (defcustom org-goto-interface 'outline
  1061. "The default interface to be used for `org-goto'.
  1062. Allowed vaues are:
  1063. outline The interface shows an outline of the relevant file
  1064. and the correct heading is found by moving through
  1065. the outline or by searching with incremental search.
  1066. outline-path-completion Headlines in the current buffer are offered via
  1067. completion."
  1068. :group 'org-refile
  1069. :type '(choice
  1070. (const :tag "Outline" outline)
  1071. (const :tag "Outline-path-completion" outline-path-completion)))
  1072. (defcustom org-reverse-note-order nil
  1073. "Non-nil means, store new notes at the beginning of a file or entry.
  1074. When nil, new notes will be filed to the end of a file or entry.
  1075. This can also be a list with cons cells of regular expressions that
  1076. are matched against file names, and values."
  1077. :group 'org-remember
  1078. :type '(choice
  1079. (const :tag "Reverse always" t)
  1080. (const :tag "Reverse never" nil)
  1081. (repeat :tag "By file name regexp"
  1082. (cons regexp boolean))))
  1083. (defcustom org-refile-targets nil
  1084. "Targets for refiling entries with \\[org-refile].
  1085. This is list of cons cells. Each cell contains:
  1086. - a specification of the files to be considered, either a list of files,
  1087. or a symbol whose function or variable value will be used to retrieve
  1088. a file name or a list of file names. Nil means, refile to a different
  1089. heading in the current buffer.
  1090. - A specification of how to find candidate refile targets. This may be
  1091. any of
  1092. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1093. This tag has to be present in all target headlines, inheritance will
  1094. not be considered.
  1095. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1096. todo keyword.
  1097. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1098. headlines that are refiling targets.
  1099. - a cons cell (:level . N). Any headline of level N is considered a target.
  1100. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1101. When this variable is nil, all top-level headlines in the current buffer
  1102. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1103. :group 'org-remember
  1104. :type '(repeat
  1105. (cons
  1106. (choice :value org-agenda-files
  1107. (const :tag "All agenda files" org-agenda-files)
  1108. (const :tag "Current buffer" nil)
  1109. (function) (variable) (file))
  1110. (choice :tag "Identify target headline by"
  1111. (cons :tag "Specific tag" (const :tag) (string))
  1112. (cons :tag "TODO keyword" (const :todo) (string))
  1113. (cons :tag "Regular expression" (const :regexp) (regexp))
  1114. (cons :tag "Level number" (const :level) (integer))
  1115. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1116. (defcustom org-refile-use-outline-path nil
  1117. "Non-nil means, provide refile targets as paths.
  1118. So a level 3 headline will be available as level1/level2/level3.
  1119. When the value is `file', also include the file name (without directory)
  1120. into the path. When `full-file-path', include the full file path."
  1121. :group 'org-remember
  1122. :type '(choice
  1123. (const :tag "Not" nil)
  1124. (const :tag "Yes" t)
  1125. (const :tag "Start with file name" file)
  1126. (const :tag "Start with full file path" full-file-path)))
  1127. (defgroup org-todo nil
  1128. "Options concerning TODO items in Org-mode."
  1129. :tag "Org TODO"
  1130. :group 'org)
  1131. (defgroup org-progress nil
  1132. "Options concerning Progress logging in Org-mode."
  1133. :tag "Org Progress"
  1134. :group 'org-time)
  1135. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1136. "List of TODO entry keyword sequences and their interpretation.
  1137. \\<org-mode-map>This is a list of sequences.
  1138. Each sequence starts with a symbol, either `sequence' or `type',
  1139. indicating if the keywords should be interpreted as a sequence of
  1140. action steps, or as different types of TODO items. The first
  1141. keywords are states requiring action - these states will select a headline
  1142. for inclusion into the global TODO list Org-mode produces. If one of
  1143. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1144. signify that no further action is necessary. If \"|\" is not found,
  1145. the last keyword is treated as the only DONE state of the sequence.
  1146. The command \\[org-todo] cycles an entry through these states, and one
  1147. additional state where no keyword is present. For details about this
  1148. cycling, see the manual.
  1149. TODO keywords and interpretation can also be set on a per-file basis with
  1150. the special #+SEQ_TODO and #+TYP_TODO lines.
  1151. Each keyword can optionally specify a character for fast state selection
  1152. \(in combination with the variable `org-use-fast-todo-selection')
  1153. and specifiers for state change logging, using the same syntax
  1154. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1155. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1156. indicates to record a time stamp each time this state is selected.
  1157. Each keyword may also specify if a timestamp or a note should be
  1158. recorded when entering or leaving the state, by adding additional
  1159. characters in the parenthesis after the keyword. This looks like this:
  1160. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1161. record only the time of the state change. With X and Y being either
  1162. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1163. Y when leaving the state if and only if the *target* state does not
  1164. define X. You may omit any of the fast-selection key or X or /Y,
  1165. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1166. For backward compatibility, this variable may also be just a list
  1167. of keywords - in this case the interptetation (sequence or type) will be
  1168. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1169. :group 'org-todo
  1170. :group 'org-keywords
  1171. :type '(choice
  1172. (repeat :tag "Old syntax, just keywords"
  1173. (string :tag "Keyword"))
  1174. (repeat :tag "New syntax"
  1175. (cons
  1176. (choice
  1177. :tag "Interpretation"
  1178. (const :tag "Sequence (cycling hits every state)" sequence)
  1179. (const :tag "Type (cycling directly to DONE)" type))
  1180. (repeat
  1181. (string :tag "Keyword"))))))
  1182. (defvar org-todo-keywords-1 nil
  1183. "All TODO and DONE keywords active in a buffer.")
  1184. (make-variable-buffer-local 'org-todo-keywords-1)
  1185. (defvar org-todo-keywords-for-agenda nil)
  1186. (defvar org-done-keywords-for-agenda nil)
  1187. (defvar org-todo-keyword-alist-for-agenda nil)
  1188. (defvar org-tag-alist-for-agenda nil)
  1189. (defvar org-agenda-contributing-files nil)
  1190. (defvar org-not-done-keywords nil)
  1191. (make-variable-buffer-local 'org-not-done-keywords)
  1192. (defvar org-done-keywords nil)
  1193. (make-variable-buffer-local 'org-done-keywords)
  1194. (defvar org-todo-heads nil)
  1195. (make-variable-buffer-local 'org-todo-heads)
  1196. (defvar org-todo-sets nil)
  1197. (make-variable-buffer-local 'org-todo-sets)
  1198. (defvar org-todo-log-states nil)
  1199. (make-variable-buffer-local 'org-todo-log-states)
  1200. (defvar org-todo-kwd-alist nil)
  1201. (make-variable-buffer-local 'org-todo-kwd-alist)
  1202. (defvar org-todo-key-alist nil)
  1203. (make-variable-buffer-local 'org-todo-key-alist)
  1204. (defvar org-todo-key-trigger nil)
  1205. (make-variable-buffer-local 'org-todo-key-trigger)
  1206. (defcustom org-todo-interpretation 'sequence
  1207. "Controls how TODO keywords are interpreted.
  1208. This variable is in principle obsolete and is only used for
  1209. backward compatibility, if the interpretation of todo keywords is
  1210. not given already in `org-todo-keywords'. See that variable for
  1211. more information."
  1212. :group 'org-todo
  1213. :group 'org-keywords
  1214. :type '(choice (const sequence)
  1215. (const type)))
  1216. (defcustom org-use-fast-todo-selection 'prefix
  1217. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1218. This variable describes if and under what circumstances the cycling
  1219. mechanism for TODO keywords will be replaced by a single-key, direct
  1220. selection scheme.
  1221. When nil, fast selection is never used.
  1222. When the symbol `prefix', it will be used when `org-todo' is called with
  1223. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1224. in an agenda buffer.
  1225. When t, fast selection is used by default. In this case, the prefix
  1226. argument forces cycling instead.
  1227. In all cases, the special interface is only used if access keys have actually
  1228. been assigned by the user, i.e. if keywords in the configuration are followed
  1229. by a letter in parenthesis, like TODO(t)."
  1230. :group 'org-todo
  1231. :type '(choice
  1232. (const :tag "Never" nil)
  1233. (const :tag "By default" t)
  1234. (const :tag "Only with C-u C-c C-t" prefix)))
  1235. (defcustom org-provide-todo-statistics t
  1236. "Non-nil means, update todo statistics after insert and toggle.
  1237. When this is set, todo statistics is updated in the parent of the current
  1238. entry each time a todo state is changed."
  1239. :group 'org-todo
  1240. :type 'boolean)
  1241. (defcustom org-after-todo-state-change-hook nil
  1242. "Hook which is run after the state of a TODO item was changed.
  1243. The new state (a string with a TODO keyword, or nil) is available in the
  1244. Lisp variable `state'."
  1245. :group 'org-todo
  1246. :type 'hook)
  1247. (defcustom org-log-done nil
  1248. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1249. When equal to the list (done), also prompt for a closing note.
  1250. This can also be configured on a per-file basis by adding one of
  1251. the following lines anywhere in the buffer:
  1252. #+STARTUP: logdone
  1253. #+STARTUP: lognotedone
  1254. #+STARTUP: nologdone"
  1255. :group 'org-todo
  1256. :group 'org-progress
  1257. :type '(choice
  1258. (const :tag "No logging" nil)
  1259. (const :tag "Record CLOSED timestamp" time)
  1260. (const :tag "Record CLOSED timestamp with closing note." note)))
  1261. ;; Normalize old uses of org-log-done.
  1262. (cond
  1263. ((eq org-log-done t) (setq org-log-done 'time))
  1264. ((and (listp org-log-done) (memq 'done org-log-done))
  1265. (setq org-log-done 'note)))
  1266. (defcustom org-log-note-clock-out nil
  1267. "Non-nil means, record a note when clocking out of an item.
  1268. This can also be configured on a per-file basis by adding one of
  1269. the following lines anywhere in the buffer:
  1270. #+STARTUP: lognoteclock-out
  1271. #+STARTUP: nolognoteclock-out"
  1272. :group 'org-todo
  1273. :group 'org-progress
  1274. :type 'boolean)
  1275. (defcustom org-log-done-with-time t
  1276. "Non-nil means, the CLOSED time stamp will contain date and time.
  1277. When nil, only the date will be recorded."
  1278. :group 'org-progress
  1279. :type 'boolean)
  1280. (defcustom org-log-note-headings
  1281. '((done . "CLOSING NOTE %t")
  1282. (state . "State %-12s %t")
  1283. (note . "Note taken on %t")
  1284. (clock-out . ""))
  1285. "Headings for notes added to entries.
  1286. The value is an alist, with the car being a symbol indicating the note
  1287. context, and the cdr is the heading to be used. The heading may also be the
  1288. empty string.
  1289. %t in the heading will be replaced by a time stamp.
  1290. %s will be replaced by the new TODO state, in double quotes.
  1291. %u will be replaced by the user name.
  1292. %U will be replaced by the full user name."
  1293. :group 'org-todo
  1294. :group 'org-progress
  1295. :type '(list :greedy t
  1296. (cons (const :tag "Heading when closing an item" done) string)
  1297. (cons (const :tag
  1298. "Heading when changing todo state (todo sequence only)"
  1299. state) string)
  1300. (cons (const :tag "Heading when just taking a note" note) string)
  1301. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1302. (unless (assq 'note org-log-note-headings)
  1303. (push '(note . "%t") org-log-note-headings))
  1304. (defcustom org-log-states-order-reversed t
  1305. "Non-nil means, the latest state change note will be directly after heading.
  1306. When nil, the notes will be orderer according to time."
  1307. :group 'org-todo
  1308. :group 'org-progress
  1309. :type 'boolean)
  1310. (defcustom org-log-repeat 'time
  1311. "Non-nil means, record moving through the DONE state when triggering repeat.
  1312. An auto-repeating tasks is immediately switched back to TODO when marked
  1313. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1314. the TODO keyword definition, or recording a closing note by setting
  1315. `org-log-done', there will be no record of the task moving through DONE.
  1316. This variable forces taking a note anyway. Possible values are:
  1317. nil Don't force a record
  1318. time Record a time stamp
  1319. note Record a note
  1320. This option can also be set with on a per-file-basis with
  1321. #+STARTUP: logrepeat
  1322. #+STARTUP: lognoterepeat
  1323. #+STARTUP: nologrepeat
  1324. You can have local logging settings for a subtree by setting the LOGGING
  1325. property to one or more of these keywords."
  1326. :group 'org-todo
  1327. :group 'org-progress
  1328. :type '(choice
  1329. (const :tag "Don't force a record" nil)
  1330. (const :tag "Force recording the DONE state" time)
  1331. (const :tag "Force recording a note with the DONE state" note)))
  1332. (defgroup org-priorities nil
  1333. "Priorities in Org-mode."
  1334. :tag "Org Priorities"
  1335. :group 'org-todo)
  1336. (defcustom org-highest-priority ?A
  1337. "The highest priority of TODO items. A character like ?A, ?B etc.
  1338. Must have a smaller ASCII number than `org-lowest-priority'."
  1339. :group 'org-priorities
  1340. :type 'character)
  1341. (defcustom org-lowest-priority ?C
  1342. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1343. Must have a larger ASCII number than `org-highest-priority'."
  1344. :group 'org-priorities
  1345. :type 'character)
  1346. (defcustom org-default-priority ?B
  1347. "The default priority of TODO items.
  1348. This is the priority an item get if no explicit priority is given."
  1349. :group 'org-priorities
  1350. :type 'character)
  1351. (defcustom org-priority-start-cycle-with-default t
  1352. "Non-nil means, start with default priority when starting to cycle.
  1353. When this is nil, the first step in the cycle will be (depending on the
  1354. command used) one higher or lower that the default priority."
  1355. :group 'org-priorities
  1356. :type 'boolean)
  1357. (defgroup org-time nil
  1358. "Options concerning time stamps and deadlines in Org-mode."
  1359. :tag "Org Time"
  1360. :group 'org)
  1361. (defcustom org-insert-labeled-timestamps-at-point nil
  1362. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1363. When nil, these labeled time stamps are forces into the second line of an
  1364. entry, just after the headline. When scheduling from the global TODO list,
  1365. the time stamp will always be forced into the second line."
  1366. :group 'org-time
  1367. :type 'boolean)
  1368. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1369. "Formats for `format-time-string' which are used for time stamps.
  1370. It is not recommended to change this constant.")
  1371. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1372. "Number of minutes to round time stamps to.
  1373. These are two values, the first applies when first creating a time stamp.
  1374. The second applies when changing it with the commands `S-up' and `S-down'.
  1375. When changing the time stamp, this means that it will change in steps
  1376. of N minutes, as given by the second value.
  1377. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1378. numbers should be factors of 60, so for example 5, 10, 15.
  1379. When this is larger than 1, you can still force an exact time-stamp by using
  1380. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1381. and by using a prefix arg to `S-up/down' to specify the exact number
  1382. of minutes to shift."
  1383. :group 'org-time
  1384. :get '(lambda (var) ; Make sure all entries have 5 elements
  1385. (if (integerp (default-value var))
  1386. (list (default-value var) 5)
  1387. (default-value var)))
  1388. :type '(list
  1389. (integer :tag "when inserting times")
  1390. (integer :tag "when modifying times")))
  1391. ;; Normalize old customizations of this variable.
  1392. (when (integerp org-time-stamp-rounding-minutes)
  1393. (setq org-time-stamp-rounding-minutes
  1394. (list org-time-stamp-rounding-minutes
  1395. org-time-stamp-rounding-minutes)))
  1396. (defcustom org-display-custom-times nil
  1397. "Non-nil means, overlay custom formats over all time stamps.
  1398. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1399. To turn this on on a per-file basis, insert anywhere in the file:
  1400. #+STARTUP: customtime"
  1401. :group 'org-time
  1402. :set 'set-default
  1403. :type 'sexp)
  1404. (make-variable-buffer-local 'org-display-custom-times)
  1405. (defcustom org-time-stamp-custom-formats
  1406. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1407. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1408. These are overlayed over the default ISO format if the variable
  1409. `org-display-custom-times' is set. Time like %H:%M should be at the
  1410. end of the second format."
  1411. :group 'org-time
  1412. :type 'sexp)
  1413. (defun org-time-stamp-format (&optional long inactive)
  1414. "Get the right format for a time string."
  1415. (let ((f (if long (cdr org-time-stamp-formats)
  1416. (car org-time-stamp-formats))))
  1417. (if inactive
  1418. (concat "[" (substring f 1 -1) "]")
  1419. f)))
  1420. (defcustom org-time-clocksum-format "%d:%02d"
  1421. "The format string used when creating CLOCKSUM lines, or when
  1422. org-mode generates a time duration."
  1423. :group 'org-time
  1424. :type 'string)
  1425. (defcustom org-deadline-warning-days 14
  1426. "No. of days before expiration during which a deadline becomes active.
  1427. This variable governs the display in sparse trees and in the agenda.
  1428. When 0 or negative, it means use this number (the absolute value of it)
  1429. even if a deadline has a different individual lead time specified."
  1430. :group 'org-time
  1431. :group 'org-agenda-daily/weekly
  1432. :type 'number)
  1433. (defcustom org-read-date-prefer-future t
  1434. "Non-nil means, assume future for incomplete date input from user.
  1435. This affects the following situations:
  1436. 1. The user gives a day, but no month.
  1437. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1438. read this as the third of *next* month. However, if you enter \"17\",
  1439. it will be considered as *this* month.
  1440. 2. The user gives a month but not a year.
  1441. For example, if it is april and you enter \"feb 2\", this will be read
  1442. as feb 2, *next* year. \"May 5\", however, will be this year.
  1443. Currently this does not work for ISO week specifications.
  1444. When this option is nil, the current month and year will always be used
  1445. as defaults."
  1446. :group 'org-time
  1447. :type 'boolean)
  1448. (defcustom org-read-date-display-live t
  1449. "Non-nil means, display current interpretation of date prompt live.
  1450. This display will be in an overlay, in the minibuffer."
  1451. :group 'org-time
  1452. :type 'boolean)
  1453. (defcustom org-read-date-popup-calendar t
  1454. "Non-nil means, pop up a calendar when prompting for a date.
  1455. In the calendar, the date can be selected with mouse-1. However, the
  1456. minibuffer will also be active, and you can simply enter the date as well.
  1457. When nil, only the minibuffer will be available."
  1458. :group 'org-time
  1459. :type 'boolean)
  1460. (if (fboundp 'defvaralias)
  1461. (defvaralias 'org-popup-calendar-for-date-prompt
  1462. 'org-read-date-popup-calendar))
  1463. (defcustom org-extend-today-until 0
  1464. "The hour when your day really ends. Must be an integer.
  1465. This has influence for the following applications:
  1466. - When switching the agenda to \"today\". It it is still earlier than
  1467. the time given here, the day recognized as TODAY is actually yesterday.
  1468. - When a date is read from the user and it is still before the time given
  1469. here, the current date and time will be assumed to be yesterday, 23:59.
  1470. Also, timestamps inserted in remember templates follow this rule.
  1471. IMPORTANT: This is a feature whose implementation is and likely will
  1472. remain incomplete. Really, it is only here because past midnight seems to
  1473. ne the favorite working time of John Wiegley :-)"
  1474. :group 'org-time
  1475. :type 'number)
  1476. (defcustom org-edit-timestamp-down-means-later nil
  1477. "Non-nil means, S-down will increase the time in a time stamp.
  1478. When nil, S-up will increase."
  1479. :group 'org-time
  1480. :type 'boolean)
  1481. (defcustom org-calendar-follow-timestamp-change t
  1482. "Non-nil means, make the calendar window follow timestamp changes.
  1483. When a timestamp is modified and the calendar window is visible, it will be
  1484. moved to the new date."
  1485. :group 'org-time
  1486. :type 'boolean)
  1487. (defgroup org-tags nil
  1488. "Options concerning tags in Org-mode."
  1489. :tag "Org Tags"
  1490. :group 'org)
  1491. (defcustom org-tag-alist nil
  1492. "List of tags allowed in Org-mode files.
  1493. When this list is nil, Org-mode will base TAG input on what is already in the
  1494. buffer.
  1495. The value of this variable is an alist, the car of each entry must be a
  1496. keyword as a string, the cdr may be a character that is used to select
  1497. that tag through the fast-tag-selection interface.
  1498. See the manual for details."
  1499. :group 'org-tags
  1500. :type '(repeat
  1501. (choice
  1502. (cons (string :tag "Tag name")
  1503. (character :tag "Access char"))
  1504. (const :tag "Start radio group" (:startgroup))
  1505. (const :tag "End radio group" (:endgroup)))))
  1506. (defvar org-file-tags nil
  1507. "List of tags that can be inherited by all entries in the file.
  1508. The tags will be inherited if the variable `org-use-tag-inheritance'
  1509. says they should be.
  1510. This variable is populated from #+TAG lines.")
  1511. (defcustom org-use-fast-tag-selection 'auto
  1512. "Non-nil means, use fast tag selection scheme.
  1513. This is a special interface to select and deselect tags with single keys.
  1514. When nil, fast selection is never used.
  1515. When the symbol `auto', fast selection is used if and only if selection
  1516. characters for tags have been configured, either through the variable
  1517. `org-tag-alist' or through a #+TAGS line in the buffer.
  1518. When t, fast selection is always used and selection keys are assigned
  1519. automatically if necessary."
  1520. :group 'org-tags
  1521. :type '(choice
  1522. (const :tag "Always" t)
  1523. (const :tag "Never" nil)
  1524. (const :tag "When selection characters are configured" 'auto)))
  1525. (defcustom org-fast-tag-selection-single-key nil
  1526. "Non-nil means, fast tag selection exits after first change.
  1527. When nil, you have to press RET to exit it.
  1528. During fast tag selection, you can toggle this flag with `C-c'.
  1529. This variable can also have the value `expert'. In this case, the window
  1530. displaying the tags menu is not even shown, until you press C-c again."
  1531. :group 'org-tags
  1532. :type '(choice
  1533. (const :tag "No" nil)
  1534. (const :tag "Yes" t)
  1535. (const :tag "Expert" expert)))
  1536. (defvar org-fast-tag-selection-include-todo nil
  1537. "Non-nil means, fast tags selection interface will also offer TODO states.
  1538. This is an undocumented feature, you should not rely on it.")
  1539. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1540. "The column to which tags should be indented in a headline.
  1541. If this number is positive, it specifies the column. If it is negative,
  1542. it means that the tags should be flushright to that column. For example,
  1543. -80 works well for a normal 80 character screen."
  1544. :group 'org-tags
  1545. :type 'integer)
  1546. (defcustom org-auto-align-tags t
  1547. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1548. These operations change the length of a headline and therefore shift
  1549. the tags around. With this options turned on, after each such operation
  1550. the tags are again aligned to `org-tags-column'."
  1551. :group 'org-tags
  1552. :type 'boolean)
  1553. (defcustom org-use-tag-inheritance t
  1554. "Non-nil means, tags in levels apply also for sublevels.
  1555. When nil, only the tags directly given in a specific line apply there.
  1556. If this option is t, a match early-on in a tree can lead to a large
  1557. number of matches in the subtree. If you only want to see the first
  1558. match in a tree during a search, check out the variable
  1559. `org-tags-match-list-sublevels'.
  1560. This may also be a list of tags that should be inherited, or a regexp that
  1561. matches tags that should be inherited."
  1562. :group 'org-tags
  1563. :type '(choice
  1564. (const :tag "Not" nil)
  1565. (const :tag "Always" t)
  1566. (repeat :tag "Specific tags" (string :tag "Tag"))
  1567. (regexp :tag "Tags matched by regexp")))
  1568. (defun org-tag-inherit-p (tag)
  1569. "Check if TAG is one that should be inherited."
  1570. (cond
  1571. ((eq org-use-tag-inheritance t) t)
  1572. ((not org-use-tag-inheritance) nil)
  1573. ((stringp org-use-tag-inheritance)
  1574. (string-match org-use-tag-inheritance tag))
  1575. ((listp org-use-tag-inheritance)
  1576. (member tag org-use-tag-inheritance))
  1577. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1578. (defcustom org-tags-match-list-sublevels t
  1579. "Non-nil means list also sublevels of headlines matching tag search.
  1580. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1581. the sublevels of a headline matching a tag search often also match
  1582. the same search. Listing all of them can create very long lists.
  1583. Setting this variable to nil causes subtrees of a match to be skipped.
  1584. This option is off by default, because inheritance in on. If you turn
  1585. inheritance off, you very likely want to turn this option on.
  1586. As a special case, if the tag search is restricted to TODO items, the
  1587. value of this variable is ignored and sublevels are always checked, to
  1588. make sure all corresponding TODO items find their way into the list."
  1589. :group 'org-tags
  1590. :type 'boolean)
  1591. (defvar org-tags-history nil
  1592. "History of minibuffer reads for tags.")
  1593. (defvar org-last-tags-completion-table nil
  1594. "The last used completion table for tags.")
  1595. (defvar org-after-tags-change-hook nil
  1596. "Hook that is run after the tags in a line have changed.")
  1597. (defgroup org-properties nil
  1598. "Options concerning properties in Org-mode."
  1599. :tag "Org Properties"
  1600. :group 'org)
  1601. (defcustom org-property-format "%-10s %s"
  1602. "How property key/value pairs should be formatted by `indent-line'.
  1603. When `indent-line' hits a property definition, it will format the line
  1604. according to this format, mainly to make sure that the values are
  1605. lined-up with respect to each other."
  1606. :group 'org-properties
  1607. :type 'string)
  1608. (defcustom org-use-property-inheritance nil
  1609. "Non-nil means, properties apply also for sublevels.
  1610. This setting is chiefly used during property searches. Turning it on can
  1611. cause significant overhead when doing a search, which is why it is not
  1612. on by default.
  1613. When nil, only the properties directly given in the current entry count.
  1614. When t, every property is inherited. The value may also be a list of
  1615. properties that should have inheritance, or a regular expression matching
  1616. properties that should be inherited.
  1617. However, note that some special properties use inheritance under special
  1618. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1619. and the properties ending in \"_ALL\" when they are used as descriptor
  1620. for valid values of a property.
  1621. Note for programmers:
  1622. When querying an entry with `org-entry-get', you can control if inheritance
  1623. should be used. By default, `org-entry-get' looks only at the local
  1624. properties. You can request inheritance by setting the inherit argument
  1625. to t (to force inheritance) or to `selective' (to respect the setting
  1626. in this variable)."
  1627. :group 'org-properties
  1628. :type '(choice
  1629. (const :tag "Not" nil)
  1630. (const :tag "Always" t)
  1631. (repeat :tag "Specific properties" (string :tag "Property"))
  1632. (regexp :tag "Properties matched by regexp")))
  1633. (defun org-property-inherit-p (property)
  1634. "Check if PROPERTY is one that should be inherited."
  1635. (cond
  1636. ((eq org-use-property-inheritance t) t)
  1637. ((not org-use-property-inheritance) nil)
  1638. ((stringp org-use-property-inheritance)
  1639. (string-match org-use-property-inheritance property))
  1640. ((listp org-use-property-inheritance)
  1641. (member property org-use-property-inheritance))
  1642. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1643. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1644. "The default column format, if no other format has been defined.
  1645. This variable can be set on the per-file basis by inserting a line
  1646. #+COLUMNS: %25ITEM ....."
  1647. :group 'org-properties
  1648. :type 'string)
  1649. (defcustom org-columns-ellipses ".."
  1650. "The ellipses to be used when a field in column view is truncated.
  1651. When this is the empty string, as many characters as possible are shown,
  1652. but then there will be no visual indication that the field has been truncated.
  1653. When this is a string of length N, the last N characters of a truncated
  1654. field are replaced by this string. If the column is narrower than the
  1655. ellipses string, only part of the ellipses string will be shown."
  1656. :group 'org-properties
  1657. :type 'string)
  1658. (defcustom org-columns-modify-value-for-display-function nil
  1659. "Function that modifies values for display in column view.
  1660. For example, it can be used to cut out a certain part from a time stamp.
  1661. The function must take 2 argments:
  1662. column-title The tite of the column (*not* the property name)
  1663. value The value that should be modified.
  1664. The function should return the value that should be displayed,
  1665. or nil if the normal value should be used."
  1666. :group 'org-properties
  1667. :type 'function)
  1668. (defcustom org-effort-property "Effort"
  1669. "The property that is being used to keep track of effort estimates.
  1670. Effort estimates given in this property need to have the format H:MM."
  1671. :group 'org-properties
  1672. :group 'org-progress
  1673. :type '(string :tag "Property"))
  1674. (defconst org-global-properties-fixed
  1675. '(("VISIBILITY_ALL" . "folded children content all"))
  1676. "List of property/value pairs that can be inherited by any entry.
  1677. These are fixed values, for the preset properties.")
  1678. (defcustom org-global-properties nil
  1679. "List of property/value pairs that can be inherited by any entry.
  1680. You can set buffer-local values for this by adding lines like
  1681. #+PROPERTY: NAME VALUE"
  1682. :group 'org-properties
  1683. :type '(repeat
  1684. (cons (string :tag "Property")
  1685. (string :tag "Value"))))
  1686. (defvar org-file-properties nil
  1687. "List of property/value pairs that can be inherited by any entry.
  1688. Valid for the current buffer.
  1689. This variable is populated from #+PROPERTY lines.")
  1690. (make-variable-buffer-local 'org-file-properties)
  1691. (defgroup org-agenda nil
  1692. "Options concerning agenda views in Org-mode."
  1693. :tag "Org Agenda"
  1694. :group 'org)
  1695. (defvar org-category nil
  1696. "Variable used by org files to set a category for agenda display.
  1697. Such files should use a file variable to set it, for example
  1698. # -*- mode: org; org-category: \"ELisp\"
  1699. or contain a special line
  1700. #+CATEGORY: ELisp
  1701. If the file does not specify a category, then file's base name
  1702. is used instead.")
  1703. (make-variable-buffer-local 'org-category)
  1704. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1705. (defcustom org-agenda-files nil
  1706. "The files to be used for agenda display.
  1707. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1708. \\[org-remove-file]. You can also use customize to edit the list.
  1709. If an entry is a directory, all files in that directory that are matched by
  1710. `org-agenda-file-regexp' will be part of the file list.
  1711. If the value of the variable is not a list but a single file name, then
  1712. the list of agenda files is actually stored and maintained in that file, one
  1713. agenda file per line."
  1714. :group 'org-agenda
  1715. :type '(choice
  1716. (repeat :tag "List of files and directories" file)
  1717. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1718. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1719. "Regular expression to match files for `org-agenda-files'.
  1720. If any element in the list in that variable contains a directory instead
  1721. of a normal file, all files in that directory that are matched by this
  1722. regular expression will be included."
  1723. :group 'org-agenda
  1724. :type 'regexp)
  1725. (defcustom org-agenda-text-search-extra-files nil
  1726. "List of extra files to be searched by text search commands.
  1727. These files will be search in addition to the agenda files by the
  1728. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1729. Note that these files will only be searched for text search commands,
  1730. not for the other agenda views like todo lists, tag searches or the weekly
  1731. agenda. This variable is intended to list notes and possibly archive files
  1732. that should also be searched by these two commands.
  1733. In fact, if the first element in the list is the symbol `agenda-archives',
  1734. than all archive files of all agenda files will be added to the search
  1735. scope."
  1736. :group 'org-agenda
  1737. :type '(set :greedy t
  1738. (const :tag "Agenda Archives" agenda-archives)
  1739. (repeat :inline t (file))))
  1740. (if (fboundp 'defvaralias)
  1741. (defvaralias 'org-agenda-multi-occur-extra-files
  1742. 'org-agenda-text-search-extra-files))
  1743. (defcustom org-agenda-skip-unavailable-files nil
  1744. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1745. A nil value means to remove them, after a query, from the list."
  1746. :group 'org-agenda
  1747. :type 'boolean)
  1748. (defcustom org-calendar-to-agenda-key [?c]
  1749. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1750. The command `org-calendar-goto-agenda' will be bound to this key. The
  1751. default is the character `c' because then `c' can be used to switch back and
  1752. forth between agenda and calendar."
  1753. :group 'org-agenda
  1754. :type 'sexp)
  1755. (defcustom org-calendar-agenda-action-key [?k]
  1756. "The key to be installed in `calendar-mode-map' for agenda-action.
  1757. The command `org-agenda-action' will be bound to this key. The
  1758. default is the character `k' because we use the same key in the agenda."
  1759. :group 'org-agenda
  1760. :type 'sexp)
  1761. (eval-after-load "calendar"
  1762. '(progn
  1763. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1764. 'org-calendar-goto-agenda)
  1765. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1766. 'org-agenda-action)))
  1767. (defgroup org-latex nil
  1768. "Options for embedding LaTeX code into Org-mode."
  1769. :tag "Org LaTeX"
  1770. :group 'org)
  1771. (defcustom org-format-latex-options
  1772. '(:foreground default :background default :scale 1.0
  1773. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1774. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1775. "Options for creating images from LaTeX fragments.
  1776. This is a property list with the following properties:
  1777. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1778. `default' means use the foreground of the default face.
  1779. :background the background color, or \"Transparent\".
  1780. `default' means use the background of the default face.
  1781. :scale a scaling factor for the size of the images.
  1782. :html-foreground, :html-background, :html-scale
  1783. the same numbers for HTML export.
  1784. :matchers a list indicating which matchers should be used to
  1785. find LaTeX fragments. Valid members of this list are:
  1786. \"begin\" find environments
  1787. \"$\" find math expressions surrounded by $...$
  1788. \"$$\" find math expressions surrounded by $$....$$
  1789. \"\\(\" find math expressions surrounded by \\(...\\)
  1790. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1791. :group 'org-latex
  1792. :type 'plist)
  1793. (defcustom org-format-latex-header "\\documentclass{article}
  1794. \\usepackage{fullpage} % do not remove
  1795. \\usepackage{amssymb}
  1796. \\usepackage[usenames]{color}
  1797. \\usepackage{amsmath}
  1798. \\usepackage{latexsym}
  1799. \\usepackage[mathscr]{eucal}
  1800. \\pagestyle{empty} % do not remove"
  1801. "The document header used for processing LaTeX fragments."
  1802. :group 'org-latex
  1803. :type 'string)
  1804. (defgroup org-font-lock nil
  1805. "Font-lock settings for highlighting in Org-mode."
  1806. :tag "Org Font Lock"
  1807. :group 'org)
  1808. (defcustom org-level-color-stars-only nil
  1809. "Non-nil means fontify only the stars in each headline.
  1810. When nil, the entire headline is fontified.
  1811. Changing it requires restart of `font-lock-mode' to become effective
  1812. also in regions already fontified."
  1813. :group 'org-font-lock
  1814. :type 'boolean)
  1815. (defcustom org-hide-leading-stars nil
  1816. "Non-nil means, hide the first N-1 stars in a headline.
  1817. This works by using the face `org-hide' for these stars. This
  1818. face is white for a light background, and black for a dark
  1819. background. You may have to customize the face `org-hide' to
  1820. make this work.
  1821. Changing it requires restart of `font-lock-mode' to become effective
  1822. also in regions already fontified.
  1823. You may also set this on a per-file basis by adding one of the following
  1824. lines to the buffer:
  1825. #+STARTUP: hidestars
  1826. #+STARTUP: showstars"
  1827. :group 'org-font-lock
  1828. :type 'boolean)
  1829. (defcustom org-fontify-done-headline nil
  1830. "Non-nil means, change the face of a headline if it is marked DONE.
  1831. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1832. When this is non-nil, the headline after the keyword is set to the
  1833. `org-headline-done' as an additional indication."
  1834. :group 'org-font-lock
  1835. :type 'boolean)
  1836. (defcustom org-fontify-emphasized-text t
  1837. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1838. Changing this variable requires a restart of Emacs to take effect."
  1839. :group 'org-font-lock
  1840. :type 'boolean)
  1841. (defcustom org-highlight-latex-fragments-and-specials nil
  1842. "Non-nil means, fontify what is treated specially by the exporters."
  1843. :group 'org-font-lock
  1844. :type 'boolean)
  1845. (defcustom org-hide-emphasis-markers nil
  1846. "Non-nil mean font-lock should hide the emphasis marker characters."
  1847. :group 'org-font-lock
  1848. :type 'boolean)
  1849. (defvar org-emph-re nil
  1850. "Regular expression for matching emphasis.")
  1851. (defvar org-verbatim-re nil
  1852. "Regular expression for matching verbatim text.")
  1853. (defvar org-emphasis-regexp-components) ; defined just below
  1854. (defvar org-emphasis-alist) ; defined just below
  1855. (defun org-set-emph-re (var val)
  1856. "Set variable and compute the emphasis regular expression."
  1857. (set var val)
  1858. (when (and (boundp 'org-emphasis-alist)
  1859. (boundp 'org-emphasis-regexp-components)
  1860. org-emphasis-alist org-emphasis-regexp-components)
  1861. (let* ((e org-emphasis-regexp-components)
  1862. (pre (car e))
  1863. (post (nth 1 e))
  1864. (border (nth 2 e))
  1865. (body (nth 3 e))
  1866. (nl (nth 4 e))
  1867. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1868. (body1 (concat body "*?"))
  1869. (markers (mapconcat 'car org-emphasis-alist ""))
  1870. (vmarkers (mapconcat
  1871. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1872. org-emphasis-alist "")))
  1873. ;; make sure special characters appear at the right position in the class
  1874. (if (string-match "\\^" markers)
  1875. (setq markers (concat (replace-match "" t t markers) "^")))
  1876. (if (string-match "-" markers)
  1877. (setq markers (concat (replace-match "" t t markers) "-")))
  1878. (if (string-match "\\^" vmarkers)
  1879. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1880. (if (string-match "-" vmarkers)
  1881. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1882. (if (> nl 0)
  1883. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1884. (int-to-string nl) "\\}")))
  1885. ;; Make the regexp
  1886. (setq org-emph-re
  1887. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1888. "\\("
  1889. "\\([" markers "]\\)"
  1890. "\\("
  1891. "[^" border "]\\|"
  1892. "[^" border (if (and nil stacked) markers) "]"
  1893. body1
  1894. "[^" border (if (and nil stacked) markers) "]"
  1895. "\\)"
  1896. "\\3\\)"
  1897. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1898. (setq org-verbatim-re
  1899. (concat "\\([" pre "]\\|^\\)"
  1900. "\\("
  1901. "\\([" vmarkers "]\\)"
  1902. "\\("
  1903. "[^" border "]\\|"
  1904. "[^" border "]"
  1905. body1
  1906. "[^" border "]"
  1907. "\\)"
  1908. "\\3\\)"
  1909. "\\([" post "]\\|$\\)")))))
  1910. (defcustom org-emphasis-regexp-components
  1911. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1912. "Components used to build the regular expression for emphasis.
  1913. This is a list with 6 entries. Terminology: In an emphasis string
  1914. like \" *strong word* \", we call the initial space PREMATCH, the final
  1915. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1916. and \"trong wor\" is the body. The different components in this variable
  1917. specify what is allowed/forbidden in each part:
  1918. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1919. post Chars allowed as postmatch. End of line will be allowed too.
  1920. border The chars *forbidden* as border characters.
  1921. body-regexp A regexp like \".\" to match a body character. Don't use
  1922. non-shy groups here, and don't allow newline here.
  1923. newline The maximum number of newlines allowed in an emphasis exp.
  1924. Use customize to modify this, or restart Emacs after changing it."
  1925. :group 'org-font-lock
  1926. :set 'org-set-emph-re
  1927. :type '(list
  1928. (sexp :tag "Allowed chars in pre ")
  1929. (sexp :tag "Allowed chars in post ")
  1930. (sexp :tag "Forbidden chars in border ")
  1931. (sexp :tag "Regexp for body ")
  1932. (integer :tag "number of newlines allowed")
  1933. (option (boolean :tag "Please ignore this button"))))
  1934. (defcustom org-emphasis-alist
  1935. `(("*" bold "<b>" "</b>")
  1936. ("/" italic "<i>" "</i>")
  1937. ("_" underline "<u>" "</u>")
  1938. ("=" org-code "<code>" "</code>" verbatim)
  1939. ("~" org-verbatim "" "" verbatim)
  1940. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1941. "<del>" "</del>")
  1942. )
  1943. "Special syntax for emphasized text.
  1944. Text starting and ending with a special character will be emphasized, for
  1945. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1946. characters, the face to be used by font-lock for highlighting in Org-mode
  1947. Emacs buffers, and the HTML tags to be used for this.
  1948. Use customize to modify this, or restart Emacs after changing it."
  1949. :group 'org-font-lock
  1950. :set 'org-set-emph-re
  1951. :type '(repeat
  1952. (list
  1953. (string :tag "Marker character")
  1954. (choice
  1955. (face :tag "Font-lock-face")
  1956. (plist :tag "Face property list"))
  1957. (string :tag "HTML start tag")
  1958. (string :tag "HTML end tag")
  1959. (option (const verbatim)))))
  1960. ;;; Miscellaneous options
  1961. (defgroup org-completion nil
  1962. "Completion in Org-mode."
  1963. :tag "Org Completion"
  1964. :group 'org)
  1965. (defcustom org-completion-fallback-command 'hippie-expand
  1966. "The expansion command called by \\[org-complete] in normal context.
  1967. Normal means, no org-mode-specific context."
  1968. :group 'org-completion
  1969. :type 'function)
  1970. ;;; Functions and variables from ther packages
  1971. ;; Declared here to avoid compiler warnings
  1972. ;; XEmacs only
  1973. (defvar outline-mode-menu-heading)
  1974. (defvar outline-mode-menu-show)
  1975. (defvar outline-mode-menu-hide)
  1976. (defvar zmacs-regions) ; XEmacs regions
  1977. ;; Emacs only
  1978. (defvar mark-active)
  1979. ;; Various packages
  1980. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1981. (declare-function calendar-forward-day "cal-move" (arg))
  1982. (declare-function calendar-goto-date "cal-move" (date))
  1983. (declare-function calendar-goto-today "cal-move" ())
  1984. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1985. (defvar calc-embedded-close-formula)
  1986. (defvar calc-embedded-open-formula)
  1987. (declare-function cdlatex-tab "ext:cdlatex" ())
  1988. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1989. (defvar font-lock-unfontify-region-function)
  1990. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1991. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1992. (defvar iswitchb-temp-buflist)
  1993. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1994. (declare-function org-agenda-skip "org-agenda" ())
  1995. (declare-function org-format-agenda-item "org-agenda"
  1996. (extra txt &optional category tags dotime noprefix remove-re))
  1997. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1998. (declare-function org-agenda-change-all-lines "org-agenda"
  1999. (newhead hdmarker &optional fixface))
  2000. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2001. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2002. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2003. (beg end))
  2004. (declare-function parse-time-string "parse-time" (string))
  2005. (declare-function remember "remember" (&optional initial))
  2006. (declare-function remember-buffer-desc "remember" ())
  2007. (declare-function remember-finalize "remember" ())
  2008. (defvar remember-save-after-remembering)
  2009. (defvar remember-data-file)
  2010. (defvar remember-register)
  2011. (defvar remember-buffer)
  2012. (defvar remember-handler-functions)
  2013. (defvar remember-annotation-functions)
  2014. (defvar texmathp-why)
  2015. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2016. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2017. (defvar w3m-current-url)
  2018. (defvar w3m-current-title)
  2019. (defvar org-latex-regexps)
  2020. ;;; Autoload and prepare some org modules
  2021. ;; Some table stuff that needs to be defined here, because it is used
  2022. ;; by the functions setting up org-mode or checking for table context.
  2023. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2024. "Detects an org-type or table-type table.")
  2025. (defconst org-table-line-regexp "^[ \t]*|"
  2026. "Detects an org-type table line.")
  2027. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2028. "Detects an org-type table line.")
  2029. (defconst org-table-hline-regexp "^[ \t]*|-"
  2030. "Detects an org-type table hline.")
  2031. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2032. "Detects a table-type table hline.")
  2033. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2034. "Searching from within a table (any type) this finds the first line
  2035. outside the table.")
  2036. ;; Autoload the functions in org-table.el that are needed by functions here.
  2037. (eval-and-compile
  2038. (org-autoload "org-table"
  2039. '(org-table-align org-table-begin org-table-blank-field
  2040. org-table-convert org-table-convert-region org-table-copy-down
  2041. org-table-copy-region org-table-create
  2042. org-table-create-or-convert-from-region
  2043. org-table-create-with-table.el org-table-current-dline
  2044. org-table-cut-region org-table-delete-column org-table-edit-field
  2045. org-table-edit-formulas org-table-end org-table-eval-formula
  2046. org-table-export org-table-field-info
  2047. org-table-get-stored-formulas org-table-goto-column
  2048. org-table-hline-and-move org-table-import org-table-insert-column
  2049. org-table-insert-hline org-table-insert-row org-table-iterate
  2050. org-table-justify-field-maybe org-table-kill-row
  2051. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2052. org-table-move-column org-table-move-column-left
  2053. org-table-move-column-right org-table-move-row
  2054. org-table-move-row-down org-table-move-row-up
  2055. org-table-next-field org-table-next-row org-table-paste-rectangle
  2056. org-table-previous-field org-table-recalculate
  2057. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2058. org-table-toggle-coordinate-overlays
  2059. org-table-toggle-formula-debugger org-table-wrap-region
  2060. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2061. (defun org-at-table-p (&optional table-type)
  2062. "Return t if the cursor is inside an org-type table.
  2063. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2064. (if org-enable-table-editor
  2065. (save-excursion
  2066. (beginning-of-line 1)
  2067. (looking-at (if table-type org-table-any-line-regexp
  2068. org-table-line-regexp)))
  2069. nil))
  2070. (defsubst org-table-p () (org-at-table-p))
  2071. (defun org-at-table.el-p ()
  2072. "Return t if and only if we are at a table.el table."
  2073. (and (org-at-table-p 'any)
  2074. (save-excursion
  2075. (goto-char (org-table-begin 'any))
  2076. (looking-at org-table1-hline-regexp))))
  2077. (defun org-table-recognize-table.el ()
  2078. "If there is a table.el table nearby, recognize it and move into it."
  2079. (if org-table-tab-recognizes-table.el
  2080. (if (org-at-table.el-p)
  2081. (progn
  2082. (beginning-of-line 1)
  2083. (if (looking-at org-table-dataline-regexp)
  2084. nil
  2085. (if (looking-at org-table1-hline-regexp)
  2086. (progn
  2087. (beginning-of-line 2)
  2088. (if (looking-at org-table-any-border-regexp)
  2089. (beginning-of-line -1)))))
  2090. (if (re-search-forward "|" (org-table-end t) t)
  2091. (progn
  2092. (require 'table)
  2093. (if (table--at-cell-p (point))
  2094. t
  2095. (message "recognizing table.el table...")
  2096. (table-recognize-table)
  2097. (message "recognizing table.el table...done")))
  2098. (error "This should not happen..."))
  2099. t)
  2100. nil)
  2101. nil))
  2102. (defun org-at-table-hline-p ()
  2103. "Return t if the cursor is inside a hline in a table."
  2104. (if org-enable-table-editor
  2105. (save-excursion
  2106. (beginning-of-line 1)
  2107. (looking-at org-table-hline-regexp))
  2108. nil))
  2109. (defvar org-table-clean-did-remove-column nil)
  2110. (defun org-table-map-tables (function)
  2111. "Apply FUNCTION to the start of all tables in the buffer."
  2112. (save-excursion
  2113. (save-restriction
  2114. (widen)
  2115. (goto-char (point-min))
  2116. (while (re-search-forward org-table-any-line-regexp nil t)
  2117. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2118. (beginning-of-line 1)
  2119. (if (looking-at org-table-line-regexp)
  2120. (save-excursion (funcall function)))
  2121. (re-search-forward org-table-any-border-regexp nil 1))))
  2122. (message "Mapping tables: done"))
  2123. ;; Declare and autoload functions from org-exp.el
  2124. (declare-function org-default-export-plist "org-exp")
  2125. (declare-function org-infile-export-plist "org-exp")
  2126. (declare-function org-get-current-options "org-exp")
  2127. (eval-and-compile
  2128. (org-autoload "org-exp"
  2129. '(org-export org-export-as-ascii org-export-visible
  2130. org-insert-export-options-template org-export-as-html-and-open
  2131. org-export-as-html-batch org-export-as-html-to-buffer
  2132. org-replace-region-by-html org-export-region-as-html
  2133. org-export-as-html org-export-icalendar-this-file
  2134. org-export-icalendar-all-agenda-files
  2135. org-table-clean-before-export
  2136. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2137. ;; Declare and autoload functions from org-agenda.el
  2138. (eval-and-compile
  2139. (org-autoload "org-agenda"
  2140. '(org-agenda org-agenda-list org-search-view
  2141. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2142. org-diary org-agenda-to-appt)))
  2143. ;; Autoload org-remember
  2144. (eval-and-compile
  2145. (org-autoload "org-remember"
  2146. '(org-remember-insinuate org-remember-annotation
  2147. org-remember-apply-template org-remember org-remember-handler)))
  2148. ;; Autoload org-clock.el
  2149. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2150. (beg end))
  2151. (declare-function org-update-mode-line "org-clock" ())
  2152. (defvar org-clock-start-time)
  2153. (defvar org-clock-marker (make-marker)
  2154. "Marker recording the last clock-in.")
  2155. (eval-and-compile
  2156. (org-autoload
  2157. "org-clock"
  2158. '(org-clock-in org-clock-out org-clock-cancel
  2159. org-clock-goto org-clock-sum org-clock-display
  2160. org-remove-clock-overlays org-clock-report
  2161. org-clocktable-shift org-dblock-write:clocktable
  2162. org-get-clocktable)))
  2163. (defun org-clock-update-time-maybe ()
  2164. "If this is a CLOCK line, update it and return t.
  2165. Otherwise, return nil."
  2166. (interactive)
  2167. (save-excursion
  2168. (beginning-of-line 1)
  2169. (skip-chars-forward " \t")
  2170. (when (looking-at org-clock-string)
  2171. (let ((re (concat "[ \t]*" org-clock-string
  2172. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2173. "\\([ \t]*=>.*\\)?\\)?"))
  2174. ts te h m s)
  2175. (cond
  2176. ((not (looking-at re))
  2177. nil)
  2178. ((not (match-end 2))
  2179. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2180. (> org-clock-marker (point))
  2181. (<= org-clock-marker (point-at-eol)))
  2182. ;; The clock is running here
  2183. (setq org-clock-start-time
  2184. (apply 'encode-time
  2185. (org-parse-time-string (match-string 1))))
  2186. (org-update-mode-line)))
  2187. (t
  2188. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2189. (end-of-line 1)
  2190. (setq ts (match-string 1)
  2191. te (match-string 3))
  2192. (setq s (- (time-to-seconds
  2193. (apply 'encode-time (org-parse-time-string te)))
  2194. (time-to-seconds
  2195. (apply 'encode-time (org-parse-time-string ts))))
  2196. h (floor (/ s 3600))
  2197. s (- s (* 3600 h))
  2198. m (floor (/ s 60))
  2199. s (- s (* 60 s)))
  2200. (insert " => " (format "%2d:%02d" h m))
  2201. t))))))
  2202. (defun org-check-running-clock ()
  2203. "Check if the current buffer contains the running clock.
  2204. If yes, offer to stop it and to save the buffer with the changes."
  2205. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2206. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2207. (buffer-name))))
  2208. (org-clock-out)
  2209. (when (y-or-n-p "Save changed buffer?")
  2210. (save-buffer))))
  2211. (defun org-clocktable-try-shift (dir n)
  2212. "Check if this line starts a clock table, if yes, shift the time block."
  2213. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2214. (org-clocktable-shift dir n)))
  2215. ;; Autoload archiving code
  2216. ;; The stuff that is needed for cycling and tags has to be defined here.
  2217. (defgroup org-archive nil
  2218. "Options concerning archiving in Org-mode."
  2219. :tag "Org Archive"
  2220. :group 'org-structure)
  2221. (defcustom org-archive-location "%s_archive::"
  2222. "The location where subtrees should be archived.
  2223. Otherwise, the value of this variable is a string, consisting of two
  2224. parts, separated by a double-colon.
  2225. The first part is a file name - when omitted, archiving happens in the same
  2226. file. %s will be replaced by the current file name (without directory part).
  2227. Archiving to a different file is useful to keep archived entries from
  2228. contributing to the Org-mode Agenda.
  2229. The part after the double colon is a headline. The archived entries will be
  2230. filed under that headline. When omitted, the subtrees are simply filed away
  2231. at the end of the file, as top-level entries.
  2232. Here are a few examples:
  2233. \"%s_archive::\"
  2234. If the current file is Projects.org, archive in file
  2235. Projects.org_archive, as top-level trees. This is the default.
  2236. \"::* Archived Tasks\"
  2237. Archive in the current file, under the top-level headline
  2238. \"* Archived Tasks\".
  2239. \"~/org/archive.org::\"
  2240. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2241. \"basement::** Finished Tasks\"
  2242. Archive in file ./basement (relative path), as level 3 trees
  2243. below the level 2 heading \"** Finished Tasks\".
  2244. You may set this option on a per-file basis by adding to the buffer a
  2245. line like
  2246. #+ARCHIVE: basement::** Finished Tasks
  2247. You may also define it locally for a subtree by setting an ARCHIVE property
  2248. in the entry. If such a property is found in an entry, or anywhere up
  2249. the hierarchy, it will be used."
  2250. :group 'org-archive
  2251. :type 'string)
  2252. (defcustom org-archive-tag "ARCHIVE"
  2253. "The tag that marks a subtree as archived.
  2254. An archived subtree does not open during visibility cycling, and does
  2255. not contribute to the agenda listings.
  2256. After changing this, font-lock must be restarted in the relevant buffers to
  2257. get the proper fontification."
  2258. :group 'org-archive
  2259. :group 'org-keywords
  2260. :type 'string)
  2261. (defcustom org-agenda-skip-archived-trees t
  2262. "Non-nil means, the agenda will skip any items located in archived trees.
  2263. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2264. variable is no longer recommended, you should leave it at the value t.
  2265. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2266. :group 'org-archive
  2267. :group 'org-agenda-skip
  2268. :type 'boolean)
  2269. (defcustom org-cycle-open-archived-trees nil
  2270. "Non-nil means, `org-cycle' will open archived trees.
  2271. An archived tree is a tree marked with the tag ARCHIVE.
  2272. When nil, archived trees will stay folded. You can still open them with
  2273. normal outline commands like `show-all', but not with the cycling commands."
  2274. :group 'org-archive
  2275. :group 'org-cycle
  2276. :type 'boolean)
  2277. (defcustom org-sparse-tree-open-archived-trees nil
  2278. "Non-nil means sparse tree construction shows matches in archived trees.
  2279. When nil, matches in these trees are highlighted, but the trees are kept in
  2280. collapsed state."
  2281. :group 'org-archive
  2282. :group 'org-sparse-trees
  2283. :type 'boolean)
  2284. (defun org-cycle-hide-archived-subtrees (state)
  2285. "Re-hide all archived subtrees after a visibility state change."
  2286. (when (and (not org-cycle-open-archived-trees)
  2287. (not (memq state '(overview folded))))
  2288. (save-excursion
  2289. (let* ((globalp (memq state '(contents all)))
  2290. (beg (if globalp (point-min) (point)))
  2291. (end (if globalp (point-max) (org-end-of-subtree t))))
  2292. (org-hide-archived-subtrees beg end)
  2293. (goto-char beg)
  2294. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2295. (message "%s" (substitute-command-keys
  2296. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2297. (defun org-force-cycle-archived ()
  2298. "Cycle subtree even if it is archived."
  2299. (interactive)
  2300. (setq this-command 'org-cycle)
  2301. (let ((org-cycle-open-archived-trees t))
  2302. (call-interactively 'org-cycle)))
  2303. (defun org-hide-archived-subtrees (beg end)
  2304. "Re-hide all archived subtrees after a visibility state change."
  2305. (save-excursion
  2306. (let* ((re (concat ":" org-archive-tag ":")))
  2307. (goto-char beg)
  2308. (while (re-search-forward re end t)
  2309. (and (org-on-heading-p) (hide-subtree))
  2310. (org-end-of-subtree t)))))
  2311. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2312. (eval-and-compile
  2313. (org-autoload "org-archive"
  2314. '(org-add-archive-files org-archive-subtree
  2315. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2316. ;; Autoload Column View Code
  2317. (declare-function org-columns-number-to-string "org-colview")
  2318. (declare-function org-columns-get-format-and-top-level "org-colview")
  2319. (declare-function org-columns-compute "org-colview")
  2320. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2321. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2322. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2323. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2324. ;; Autoload ID code
  2325. (org-autoload "org-id"
  2326. '(org-id-get-create org-id-new org-id-copy org-id-get
  2327. org-id-get-with-outline-path-completion
  2328. org-id-get-with-outline-drilling
  2329. org-id-goto org-id-find))
  2330. ;;; Variables for pre-computed regular expressions, all buffer local
  2331. (defvar org-drawer-regexp nil
  2332. "Matches first line of a hidden block.")
  2333. (make-variable-buffer-local 'org-drawer-regexp)
  2334. (defvar org-todo-regexp nil
  2335. "Matches any of the TODO state keywords.")
  2336. (make-variable-buffer-local 'org-todo-regexp)
  2337. (defvar org-not-done-regexp nil
  2338. "Matches any of the TODO state keywords except the last one.")
  2339. (make-variable-buffer-local 'org-not-done-regexp)
  2340. (defvar org-todo-line-regexp nil
  2341. "Matches a headline and puts TODO state into group 2 if present.")
  2342. (make-variable-buffer-local 'org-todo-line-regexp)
  2343. (defvar org-complex-heading-regexp nil
  2344. "Matches a headline and puts everything into groups:
  2345. group 1: the stars
  2346. group 2: The todo keyword, maybe
  2347. group 3: Priority cookie
  2348. group 4: True headline
  2349. group 5: Tags")
  2350. (make-variable-buffer-local 'org-complex-heading-regexp)
  2351. (defvar org-todo-line-tags-regexp nil
  2352. "Matches a headline and puts TODO state into group 2 if present.
  2353. Also put tags into group 4 if tags are present.")
  2354. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2355. (defvar org-nl-done-regexp nil
  2356. "Matches newline followed by a headline with the DONE keyword.")
  2357. (make-variable-buffer-local 'org-nl-done-regexp)
  2358. (defvar org-looking-at-done-regexp nil
  2359. "Matches the DONE keyword a point.")
  2360. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2361. (defvar org-ds-keyword-length 12
  2362. "Maximum length of the Deadline and SCHEDULED keywords.")
  2363. (make-variable-buffer-local 'org-ds-keyword-length)
  2364. (defvar org-deadline-regexp nil
  2365. "Matches the DEADLINE keyword.")
  2366. (make-variable-buffer-local 'org-deadline-regexp)
  2367. (defvar org-deadline-time-regexp nil
  2368. "Matches the DEADLINE keyword together with a time stamp.")
  2369. (make-variable-buffer-local 'org-deadline-time-regexp)
  2370. (defvar org-deadline-line-regexp nil
  2371. "Matches the DEADLINE keyword and the rest of the line.")
  2372. (make-variable-buffer-local 'org-deadline-line-regexp)
  2373. (defvar org-scheduled-regexp nil
  2374. "Matches the SCHEDULED keyword.")
  2375. (make-variable-buffer-local 'org-scheduled-regexp)
  2376. (defvar org-scheduled-time-regexp nil
  2377. "Matches the SCHEDULED keyword together with a time stamp.")
  2378. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2379. (defvar org-closed-time-regexp nil
  2380. "Matches the CLOSED keyword together with a time stamp.")
  2381. (make-variable-buffer-local 'org-closed-time-regexp)
  2382. (defvar org-keyword-time-regexp nil
  2383. "Matches any of the 4 keywords, together with the time stamp.")
  2384. (make-variable-buffer-local 'org-keyword-time-regexp)
  2385. (defvar org-keyword-time-not-clock-regexp nil
  2386. "Matches any of the 3 keywords, together with the time stamp.")
  2387. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2388. (defvar org-maybe-keyword-time-regexp nil
  2389. "Matches a timestamp, possibly preceeded by a keyword.")
  2390. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2391. (defvar org-planning-or-clock-line-re nil
  2392. "Matches a line with planning or clock info.")
  2393. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2394. (defconst org-plain-time-of-day-regexp
  2395. (concat
  2396. "\\(\\<[012]?[0-9]"
  2397. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2398. "\\(--?"
  2399. "\\(\\<[012]?[0-9]"
  2400. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2401. "\\)?")
  2402. "Regular expression to match a plain time or time range.
  2403. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2404. groups carry important information:
  2405. 0 the full match
  2406. 1 the first time, range or not
  2407. 8 the second time, if it is a range.")
  2408. (defconst org-plain-time-extension-regexp
  2409. (concat
  2410. "\\(\\<[012]?[0-9]"
  2411. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2412. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2413. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2414. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2415. groups carry important information:
  2416. 0 the full match
  2417. 7 hours of duration
  2418. 9 minutes of duration")
  2419. (defconst org-stamp-time-of-day-regexp
  2420. (concat
  2421. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2422. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2423. "\\(--?"
  2424. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2425. "Regular expression to match a timestamp time or time range.
  2426. After a match, the following groups carry important information:
  2427. 0 the full match
  2428. 1 date plus weekday, for backreferencing to make sure both times on same day
  2429. 2 the first time, range or not
  2430. 4 the second time, if it is a range.")
  2431. (defconst org-startup-options
  2432. '(("fold" org-startup-folded t)
  2433. ("overview" org-startup-folded t)
  2434. ("nofold" org-startup-folded nil)
  2435. ("showall" org-startup-folded nil)
  2436. ("content" org-startup-folded content)
  2437. ("hidestars" org-hide-leading-stars t)
  2438. ("showstars" org-hide-leading-stars nil)
  2439. ("odd" org-odd-levels-only t)
  2440. ("oddeven" org-odd-levels-only nil)
  2441. ("align" org-startup-align-all-tables t)
  2442. ("noalign" org-startup-align-all-tables nil)
  2443. ("customtime" org-display-custom-times t)
  2444. ("logdone" org-log-done time)
  2445. ("lognotedone" org-log-done note)
  2446. ("nologdone" org-log-done nil)
  2447. ("lognoteclock-out" org-log-note-clock-out t)
  2448. ("nolognoteclock-out" org-log-note-clock-out nil)
  2449. ("logrepeat" org-log-repeat state)
  2450. ("lognoterepeat" org-log-repeat note)
  2451. ("nologrepeat" org-log-repeat nil)
  2452. ("constcgs" constants-unit-system cgs)
  2453. ("constSI" constants-unit-system SI))
  2454. "Variable associated with STARTUP options for org-mode.
  2455. Each element is a list of three items: The startup options as written
  2456. in the #+STARTUP line, the corresponding variable, and the value to
  2457. set this variable to if the option is found. An optional forth element PUSH
  2458. means to push this value onto the list in the variable.")
  2459. (defun org-set-regexps-and-options ()
  2460. "Precompute regular expressions for current buffer."
  2461. (when (org-mode-p)
  2462. (org-set-local 'org-todo-kwd-alist nil)
  2463. (org-set-local 'org-todo-key-alist nil)
  2464. (org-set-local 'org-todo-key-trigger nil)
  2465. (org-set-local 'org-todo-keywords-1 nil)
  2466. (org-set-local 'org-done-keywords nil)
  2467. (org-set-local 'org-todo-heads nil)
  2468. (org-set-local 'org-todo-sets nil)
  2469. (org-set-local 'org-todo-log-states nil)
  2470. (org-set-local 'org-file-properties nil)
  2471. (org-set-local 'org-file-tags nil)
  2472. (let ((re (org-make-options-regexp
  2473. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2474. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2475. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2476. (splitre "[ \t]+")
  2477. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2478. tail sep kws1 prio props ftags drawers
  2479. ext-setup-or-nil setup-contents (start 0))
  2480. (save-excursion
  2481. (save-restriction
  2482. (widen)
  2483. (goto-char (point-min))
  2484. (while (or (and ext-setup-or-nil
  2485. (string-match re ext-setup-or-nil start)
  2486. (setq start (match-end 0)))
  2487. (and (setq ext-setup-or-nil nil start 0)
  2488. (re-search-forward re nil t)))
  2489. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2490. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2491. (cond
  2492. ((equal key "CATEGORY")
  2493. (if (string-match "[ \t]+$" value)
  2494. (setq value (replace-match "" t t value)))
  2495. (setq cat value))
  2496. ((member key '("SEQ_TODO" "TODO"))
  2497. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2498. ((equal key "TYP_TODO")
  2499. (push (cons 'type (org-split-string value splitre)) kwds))
  2500. ((equal key "TAGS")
  2501. (setq tags (append tags (org-split-string value splitre))))
  2502. ((equal key "COLUMNS")
  2503. (org-set-local 'org-columns-default-format value))
  2504. ((equal key "LINK")
  2505. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2506. (push (cons (match-string 1 value)
  2507. (org-trim (match-string 2 value)))
  2508. links)))
  2509. ((equal key "PRIORITIES")
  2510. (setq prio (org-split-string value " +")))
  2511. ((equal key "PROPERTY")
  2512. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2513. (push (cons (match-string 1 value) (match-string 2 value))
  2514. props)))
  2515. ((equal key "FILETAGS")
  2516. (when (string-match "\\S-" value)
  2517. (setq ftags
  2518. (append
  2519. ftags
  2520. (apply 'append
  2521. (mapcar (lambda (x) (org-split-string x ":"))
  2522. (org-split-string value)))))))
  2523. ((equal key "DRAWERS")
  2524. (setq drawers (org-split-string value splitre)))
  2525. ((equal key "CONSTANTS")
  2526. (setq const (append const (org-split-string value splitre))))
  2527. ((equal key "STARTUP")
  2528. (let ((opts (org-split-string value splitre))
  2529. l var val)
  2530. (while (setq l (pop opts))
  2531. (when (setq l (assoc l org-startup-options))
  2532. (setq var (nth 1 l) val (nth 2 l))
  2533. (if (not (nth 3 l))
  2534. (set (make-local-variable var) val)
  2535. (if (not (listp (symbol-value var)))
  2536. (set (make-local-variable var) nil))
  2537. (set (make-local-variable var) (symbol-value var))
  2538. (add-to-list var val))))))
  2539. ((equal key "ARCHIVE")
  2540. (string-match " *$" value)
  2541. (setq arch (replace-match "" t t value))
  2542. (remove-text-properties 0 (length arch)
  2543. '(face t fontified t) arch))
  2544. ((equal key "SETUPFILE")
  2545. (setq setup-contents (org-file-contents
  2546. (expand-file-name
  2547. (org-remove-double-quotes value))
  2548. 'noerror))
  2549. (if (not ext-setup-or-nil)
  2550. (setq ext-setup-or-nil setup-contents start 0)
  2551. (setq ext-setup-or-nil
  2552. (concat (substring ext-setup-or-nil 0 start)
  2553. "\n" setup-contents "\n"
  2554. (substring ext-setup-or-nil start)))))
  2555. ))))
  2556. (when cat
  2557. (org-set-local 'org-category (intern cat))
  2558. (push (cons "CATEGORY" cat) props))
  2559. (when prio
  2560. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2561. (setq prio (mapcar 'string-to-char prio))
  2562. (org-set-local 'org-highest-priority (nth 0 prio))
  2563. (org-set-local 'org-lowest-priority (nth 1 prio))
  2564. (org-set-local 'org-default-priority (nth 2 prio)))
  2565. (and props (org-set-local 'org-file-properties (nreverse props)))
  2566. (and ftags (org-set-local 'org-file-tags ftags))
  2567. (and drawers (org-set-local 'org-drawers drawers))
  2568. (and arch (org-set-local 'org-archive-location arch))
  2569. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2570. ;; Process the TODO keywords
  2571. (unless kwds
  2572. ;; Use the global values as if they had been given locally.
  2573. (setq kwds (default-value 'org-todo-keywords))
  2574. (if (stringp (car kwds))
  2575. (setq kwds (list (cons org-todo-interpretation
  2576. (default-value 'org-todo-keywords)))))
  2577. (setq kwds (reverse kwds)))
  2578. (setq kwds (nreverse kwds))
  2579. (let (inter kws kw)
  2580. (while (setq kws (pop kwds))
  2581. (setq inter (pop kws) sep (member "|" kws)
  2582. kws0 (delete "|" (copy-sequence kws))
  2583. kwsa nil
  2584. kws1 (mapcar
  2585. (lambda (x)
  2586. ;; 1 2
  2587. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2588. (progn
  2589. (setq kw (match-string 1 x)
  2590. key (and (match-end 2) (match-string 2 x))
  2591. log (org-extract-log-state-settings x))
  2592. (push (cons kw (and key (string-to-char key))) kwsa)
  2593. (and log (push log org-todo-log-states))
  2594. kw)
  2595. (error "Invalid TODO keyword %s" x)))
  2596. kws0)
  2597. kwsa (if kwsa (append '((:startgroup))
  2598. (nreverse kwsa)
  2599. '((:endgroup))))
  2600. hw (car kws1)
  2601. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2602. tail (list inter hw (car dws) (org-last dws)))
  2603. (add-to-list 'org-todo-heads hw 'append)
  2604. (push kws1 org-todo-sets)
  2605. (setq org-done-keywords (append org-done-keywords dws nil))
  2606. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2607. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2608. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2609. (setq org-todo-sets (nreverse org-todo-sets)
  2610. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2611. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2612. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2613. ;; Process the constants
  2614. (when const
  2615. (let (e cst)
  2616. (while (setq e (pop const))
  2617. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2618. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2619. (setq org-table-formula-constants-local cst)))
  2620. ;; Process the tags.
  2621. (when tags
  2622. (let (e tgs)
  2623. (while (setq e (pop tags))
  2624. (cond
  2625. ((equal e "{") (push '(:startgroup) tgs))
  2626. ((equal e "}") (push '(:endgroup) tgs))
  2627. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2628. (push (cons (match-string 1 e)
  2629. (string-to-char (match-string 2 e)))
  2630. tgs))
  2631. (t (push (list e) tgs))))
  2632. (org-set-local 'org-tag-alist nil)
  2633. (while (setq e (pop tgs))
  2634. (or (and (stringp (car e))
  2635. (assoc (car e) org-tag-alist))
  2636. (push e org-tag-alist)))))
  2637. ;; Compute the regular expressions and other local variables
  2638. (if (not org-done-keywords)
  2639. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2640. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2641. (length org-scheduled-string)
  2642. (length org-clock-string)
  2643. (length org-closed-string)))
  2644. org-drawer-regexp
  2645. (concat "^[ \t]*:\\("
  2646. (mapconcat 'regexp-quote org-drawers "\\|")
  2647. "\\):[ \t]*$")
  2648. org-not-done-keywords
  2649. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2650. org-todo-regexp
  2651. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2652. "\\|") "\\)\\>")
  2653. org-not-done-regexp
  2654. (concat "\\<\\("
  2655. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2656. "\\)\\>")
  2657. org-todo-line-regexp
  2658. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2659. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2660. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2661. org-complex-heading-regexp
  2662. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2663. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2664. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2665. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2666. org-nl-done-regexp
  2667. (concat "\n\\*+[ \t]+"
  2668. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2669. "\\)" "\\>")
  2670. org-todo-line-tags-regexp
  2671. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2672. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2673. (org-re
  2674. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2675. org-looking-at-done-regexp
  2676. (concat "^" "\\(?:"
  2677. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2678. "\\>")
  2679. org-deadline-regexp (concat "\\<" org-deadline-string)
  2680. org-deadline-time-regexp
  2681. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2682. org-deadline-line-regexp
  2683. (concat "\\<\\(" org-deadline-string "\\).*")
  2684. org-scheduled-regexp
  2685. (concat "\\<" org-scheduled-string)
  2686. org-scheduled-time-regexp
  2687. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2688. org-closed-time-regexp
  2689. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2690. org-keyword-time-regexp
  2691. (concat "\\<\\(" org-scheduled-string
  2692. "\\|" org-deadline-string
  2693. "\\|" org-closed-string
  2694. "\\|" org-clock-string "\\)"
  2695. " *[[<]\\([^]>]+\\)[]>]")
  2696. org-keyword-time-not-clock-regexp
  2697. (concat "\\<\\(" org-scheduled-string
  2698. "\\|" org-deadline-string
  2699. "\\|" org-closed-string
  2700. "\\)"
  2701. " *[[<]\\([^]>]+\\)[]>]")
  2702. org-maybe-keyword-time-regexp
  2703. (concat "\\(\\<\\(" org-scheduled-string
  2704. "\\|" org-deadline-string
  2705. "\\|" org-closed-string
  2706. "\\|" org-clock-string "\\)\\)?"
  2707. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2708. org-planning-or-clock-line-re
  2709. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2710. "\\|" org-deadline-string
  2711. "\\|" org-closed-string "\\|" org-clock-string
  2712. "\\)\\>\\)")
  2713. )
  2714. (org-compute-latex-and-specials-regexp)
  2715. (org-set-font-lock-defaults))))
  2716. (defun org-file-contents (file &optional noerror)
  2717. "Return the contents of FILE, as a string."
  2718. (if (or (not file)
  2719. (not (file-readable-p file)))
  2720. (if noerror
  2721. (progn
  2722. (message "Cannot read file %s" file)
  2723. (ding) (sit-for 2)
  2724. "")
  2725. (error "Cannot read file %s" file))
  2726. (with-temp-buffer
  2727. (insert-file-contents file)
  2728. (buffer-string))))
  2729. (defun org-extract-log-state-settings (x)
  2730. "Extract the log state setting from a TODO keyword string.
  2731. This will extract info from a string like \"WAIT(w@/!)\"."
  2732. (let (kw key log1 log2)
  2733. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2734. (setq kw (match-string 1 x)
  2735. key (and (match-end 2) (match-string 2 x))
  2736. log1 (and (match-end 3) (match-string 3 x))
  2737. log2 (and (match-end 4) (match-string 4 x)))
  2738. (and (or log1 log2)
  2739. (list kw
  2740. (and log1 (if (equal log1 "!") 'time 'note))
  2741. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2742. (defun org-remove-keyword-keys (list)
  2743. "Remove a pair of parenthesis at the end of each string in LIST."
  2744. (mapcar (lambda (x)
  2745. (if (string-match "(.*)$" x)
  2746. (substring x 0 (match-beginning 0))
  2747. x))
  2748. list))
  2749. ;; FIXME: this could be done much better, using second characters etc.
  2750. (defun org-assign-fast-keys (alist)
  2751. "Assign fast keys to a keyword-key alist.
  2752. Respect keys that are already there."
  2753. (let (new e k c c1 c2 (char ?a))
  2754. (while (setq e (pop alist))
  2755. (cond
  2756. ((equal e '(:startgroup)) (push e new))
  2757. ((equal e '(:endgroup)) (push e new))
  2758. (t
  2759. (setq k (car e) c2 nil)
  2760. (if (cdr e)
  2761. (setq c (cdr e))
  2762. ;; automatically assign a character.
  2763. (setq c1 (string-to-char
  2764. (downcase (substring
  2765. k (if (= (string-to-char k) ?@) 1 0)))))
  2766. (if (or (rassoc c1 new) (rassoc c1 alist))
  2767. (while (or (rassoc char new) (rassoc char alist))
  2768. (setq char (1+ char)))
  2769. (setq c2 c1))
  2770. (setq c (or c2 char)))
  2771. (push (cons k c) new))))
  2772. (nreverse new)))
  2773. ;;; Some variables used in various places
  2774. (defvar org-window-configuration nil
  2775. "Used in various places to store a window configuration.")
  2776. (defvar org-finish-function nil
  2777. "Function to be called when `C-c C-c' is used.
  2778. This is for getting out of special buffers like remember.")
  2779. ;; FIXME: Occasionally check by commenting these, to make sure
  2780. ;; no other functions uses these, forgetting to let-bind them.
  2781. (defvar entry)
  2782. (defvar state)
  2783. (defvar last-state)
  2784. (defvar date)
  2785. (defvar description)
  2786. ;; Defined somewhere in this file, but used before definition.
  2787. (defvar org-html-entities)
  2788. (defvar org-struct-menu)
  2789. (defvar org-org-menu)
  2790. (defvar org-tbl-menu)
  2791. (defvar org-agenda-keymap)
  2792. ;;;; Define the Org-mode
  2793. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2794. (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."))
  2795. ;; We use a before-change function to check if a table might need
  2796. ;; an update.
  2797. (defvar org-table-may-need-update t
  2798. "Indicates that a table might need an update.
  2799. This variable is set by `org-before-change-function'.
  2800. `org-table-align' sets it back to nil.")
  2801. (defun org-before-change-function (beg end)
  2802. "Every change indicates that a table might need an update."
  2803. (setq org-table-may-need-update t))
  2804. (defvar org-mode-map)
  2805. (defvar org-mode-hook nil)
  2806. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2807. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2808. (defvar org-table-buffer-is-an nil)
  2809. (defconst org-outline-regexp "\\*+ ")
  2810. ;;;###autoload
  2811. (define-derived-mode org-mode outline-mode "Org"
  2812. "Outline-based notes management and organizer, alias
  2813. \"Carsten's outline-mode for keeping track of everything.\"
  2814. Org-mode develops organizational tasks around a NOTES file which
  2815. contains information about projects as plain text. Org-mode is
  2816. implemented on top of outline-mode, which is ideal to keep the content
  2817. of large files well structured. It supports ToDo items, deadlines and
  2818. time stamps, which magically appear in the diary listing of the Emacs
  2819. calendar. Tables are easily created with a built-in table editor.
  2820. Plain text URL-like links connect to websites, emails (VM), Usenet
  2821. messages (Gnus), BBDB entries, and any files related to the project.
  2822. For printing and sharing of notes, an Org-mode file (or a part of it)
  2823. can be exported as a structured ASCII or HTML file.
  2824. The following commands are available:
  2825. \\{org-mode-map}"
  2826. ;; Get rid of Outline menus, they are not needed
  2827. ;; Need to do this here because define-derived-mode sets up
  2828. ;; the keymap so late. Still, it is a waste to call this each time
  2829. ;; we switch another buffer into org-mode.
  2830. (if (featurep 'xemacs)
  2831. (when (boundp 'outline-mode-menu-heading)
  2832. ;; Assume this is Greg's port, it used easymenu
  2833. (easy-menu-remove outline-mode-menu-heading)
  2834. (easy-menu-remove outline-mode-menu-show)
  2835. (easy-menu-remove outline-mode-menu-hide))
  2836. (define-key org-mode-map [menu-bar headings] 'undefined)
  2837. (define-key org-mode-map [menu-bar hide] 'undefined)
  2838. (define-key org-mode-map [menu-bar show] 'undefined))
  2839. (org-load-modules-maybe)
  2840. (easy-menu-add org-org-menu)
  2841. (easy-menu-add org-tbl-menu)
  2842. (org-install-agenda-files-menu)
  2843. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2844. (org-add-to-invisibility-spec '(org-cwidth))
  2845. (when (featurep 'xemacs)
  2846. (org-set-local 'line-move-ignore-invisible t))
  2847. (org-set-local 'outline-regexp org-outline-regexp)
  2848. (org-set-local 'outline-level 'org-outline-level)
  2849. (when (and org-ellipsis
  2850. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2851. (fboundp 'make-glyph-code))
  2852. (unless org-display-table
  2853. (setq org-display-table (make-display-table)))
  2854. (set-display-table-slot
  2855. org-display-table 4
  2856. (vconcat (mapcar
  2857. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2858. org-ellipsis)))
  2859. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2860. (setq buffer-display-table org-display-table))
  2861. (org-set-regexps-and-options)
  2862. ;; Calc embedded
  2863. (org-set-local 'calc-embedded-open-mode "# ")
  2864. (modify-syntax-entry ?# "<")
  2865. (modify-syntax-entry ?@ "w")
  2866. (if org-startup-truncated (setq truncate-lines t))
  2867. (org-set-local 'font-lock-unfontify-region-function
  2868. 'org-unfontify-region)
  2869. ;; Activate before-change-function
  2870. (org-set-local 'org-table-may-need-update t)
  2871. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2872. 'local)
  2873. ;; Check for running clock before killing a buffer
  2874. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2875. ;; Paragraphs and auto-filling
  2876. (org-set-autofill-regexps)
  2877. (setq indent-line-function 'org-indent-line-function)
  2878. (org-update-radio-target-regexp)
  2879. ;; Comment characters
  2880. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2881. (org-set-local 'comment-padding " ")
  2882. ;; Align options lines
  2883. (org-set-local
  2884. 'align-mode-rules-list
  2885. '((org-in-buffer-settings
  2886. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2887. (modes . '(org-mode)))))
  2888. ;; Imenu
  2889. (org-set-local 'imenu-create-index-function
  2890. 'org-imenu-get-tree)
  2891. ;; Make isearch reveal context
  2892. (if (or (featurep 'xemacs)
  2893. (not (boundp 'outline-isearch-open-invisible-function)))
  2894. ;; Emacs 21 and XEmacs make use of the hook
  2895. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2896. ;; Emacs 22 deals with this through a special variable
  2897. (org-set-local 'outline-isearch-open-invisible-function
  2898. (lambda (&rest ignore) (org-show-context 'isearch))))
  2899. ;; If empty file that did not turn on org-mode automatically, make it to.
  2900. (if (and org-insert-mode-line-in-empty-file
  2901. (interactive-p)
  2902. (= (point-min) (point-max)))
  2903. (insert "# -*- mode: org -*-\n\n"))
  2904. (unless org-inhibit-startup
  2905. (when org-startup-align-all-tables
  2906. (let ((bmp (buffer-modified-p)))
  2907. (org-table-map-tables 'org-table-align)
  2908. (set-buffer-modified-p bmp)))
  2909. (org-set-startup-visibility)))
  2910. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2911. (defun org-current-time ()
  2912. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2913. (if (> (car org-time-stamp-rounding-minutes) 1)
  2914. (let ((r (car org-time-stamp-rounding-minutes))
  2915. (time (decode-time)))
  2916. (apply 'encode-time
  2917. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2918. (nthcdr 2 time))))
  2919. (current-time)))
  2920. ;;;; Font-Lock stuff, including the activators
  2921. (defvar org-mouse-map (make-sparse-keymap))
  2922. (org-defkey org-mouse-map
  2923. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2924. (org-defkey org-mouse-map
  2925. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2926. (when org-mouse-1-follows-link
  2927. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2928. (when org-tab-follows-link
  2929. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2930. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2931. (when org-return-follows-link
  2932. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2933. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2934. (require 'font-lock)
  2935. (defconst org-non-link-chars "]\t\n\r<>")
  2936. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2937. "shell" "elisp"))
  2938. (defvar org-link-types-re nil
  2939. "Matches a link that has a url-like prefix like \"http:\"")
  2940. (defvar org-link-re-with-space nil
  2941. "Matches a link with spaces, optional angular brackets around it.")
  2942. (defvar org-link-re-with-space2 nil
  2943. "Matches a link with spaces, optional angular brackets around it.")
  2944. (defvar org-angle-link-re nil
  2945. "Matches link with angular brackets, spaces are allowed.")
  2946. (defvar org-plain-link-re nil
  2947. "Matches plain link, without spaces.")
  2948. (defvar org-bracket-link-regexp nil
  2949. "Matches a link in double brackets.")
  2950. (defvar org-bracket-link-analytic-regexp nil
  2951. "Regular expression used to analyze links.
  2952. Here is what the match groups contain after a match:
  2953. 1: http:
  2954. 2: http
  2955. 3: path
  2956. 4: [desc]
  2957. 5: desc")
  2958. (defvar org-any-link-re nil
  2959. "Regular expression matching any link.")
  2960. (defun org-make-link-regexps ()
  2961. "Update the link regular expressions.
  2962. This should be called after the variable `org-link-types' has changed."
  2963. (setq org-link-types-re
  2964. (concat
  2965. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2966. org-link-re-with-space
  2967. (concat
  2968. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2969. "\\([^" org-non-link-chars " ]"
  2970. "[^" org-non-link-chars "]*"
  2971. "[^" org-non-link-chars " ]\\)>?")
  2972. org-link-re-with-space2
  2973. (concat
  2974. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2975. "\\([^" org-non-link-chars " ]"
  2976. "[^]\t\n\r]*"
  2977. "[^" org-non-link-chars " ]\\)>?")
  2978. org-angle-link-re
  2979. (concat
  2980. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2981. "\\([^" org-non-link-chars " ]"
  2982. "[^" org-non-link-chars "]*"
  2983. "\\)>")
  2984. org-plain-link-re
  2985. (concat
  2986. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2987. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2988. org-bracket-link-regexp
  2989. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2990. org-bracket-link-analytic-regexp
  2991. (concat
  2992. "\\[\\["
  2993. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2994. "\\([^]]+\\)"
  2995. "\\]"
  2996. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2997. "\\]")
  2998. org-any-link-re
  2999. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3000. org-angle-link-re "\\)\\|\\("
  3001. org-plain-link-re "\\)")))
  3002. (org-make-link-regexps)
  3003. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3004. "Regular expression for fast time stamp matching.")
  3005. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3006. "Regular expression for fast time stamp matching.")
  3007. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3008. "Regular expression matching time strings for analysis.
  3009. This one does not require the space after the date, so it can be used
  3010. on a string that terminates immediately after the date.")
  3011. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3012. "Regular expression matching time strings for analysis.")
  3013. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3014. "Regular expression matching time stamps, with groups.")
  3015. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3016. "Regular expression matching time stamps (also [..]), with groups.")
  3017. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3018. "Regular expression matching a time stamp range.")
  3019. (defconst org-tr-regexp-both
  3020. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3021. "Regular expression matching a time stamp range.")
  3022. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3023. org-ts-regexp "\\)?")
  3024. "Regular expression matching a time stamp or time stamp range.")
  3025. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3026. org-ts-regexp-both "\\)?")
  3027. "Regular expression matching a time stamp or time stamp range.
  3028. The time stamps may be either active or inactive.")
  3029. (defvar org-emph-face nil)
  3030. (defun org-do-emphasis-faces (limit)
  3031. "Run through the buffer and add overlays to links."
  3032. (let (rtn)
  3033. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3034. (if (not (= (char-after (match-beginning 3))
  3035. (char-after (match-beginning 4))))
  3036. (progn
  3037. (setq rtn t)
  3038. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3039. 'face
  3040. (nth 1 (assoc (match-string 3)
  3041. org-emphasis-alist)))
  3042. (add-text-properties (match-beginning 2) (match-end 2)
  3043. '(font-lock-multiline t))
  3044. (when org-hide-emphasis-markers
  3045. (add-text-properties (match-end 4) (match-beginning 5)
  3046. '(invisible org-link))
  3047. (add-text-properties (match-beginning 3) (match-end 3)
  3048. '(invisible org-link)))))
  3049. (backward-char 1))
  3050. rtn))
  3051. (defun org-emphasize (&optional char)
  3052. "Insert or change an emphasis, i.e. a font like bold or italic.
  3053. If there is an active region, change that region to a new emphasis.
  3054. If there is no region, just insert the marker characters and position
  3055. the cursor between them.
  3056. CHAR should be either the marker character, or the first character of the
  3057. HTML tag associated with that emphasis. If CHAR is a space, the means
  3058. to remove the emphasis of the selected region.
  3059. If char is not given (for example in an interactive call) it
  3060. will be prompted for."
  3061. (interactive)
  3062. (let ((eal org-emphasis-alist) e det
  3063. (erc org-emphasis-regexp-components)
  3064. (prompt "")
  3065. (string "") beg end move tag c s)
  3066. (if (org-region-active-p)
  3067. (setq beg (region-beginning) end (region-end)
  3068. string (buffer-substring beg end))
  3069. (setq move t))
  3070. (while (setq e (pop eal))
  3071. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3072. c (aref tag 0))
  3073. (push (cons c (string-to-char (car e))) det)
  3074. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3075. (substring tag 1)))))
  3076. (unless char
  3077. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3078. (setq char (read-char-exclusive)))
  3079. (setq char (or (cdr (assoc char det)) char))
  3080. (if (equal char ?\ )
  3081. (setq s "" move nil)
  3082. (unless (assoc (char-to-string char) org-emphasis-alist)
  3083. (error "No such emphasis marker: \"%c\"" char))
  3084. (setq s (char-to-string char)))
  3085. (while (and (> (length string) 1)
  3086. (equal (substring string 0 1) (substring string -1))
  3087. (assoc (substring string 0 1) org-emphasis-alist))
  3088. (setq string (substring string 1 -1)))
  3089. (setq string (concat s string s))
  3090. (if beg (delete-region beg end))
  3091. (unless (or (bolp)
  3092. (string-match (concat "[" (nth 0 erc) "\n]")
  3093. (char-to-string (char-before (point)))))
  3094. (insert " "))
  3095. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3096. (char-to-string (char-after (point))))
  3097. (insert " ") (backward-char 1))
  3098. (insert string)
  3099. (and move (backward-char 1))))
  3100. (defconst org-nonsticky-props
  3101. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3102. (defun org-activate-plain-links (limit)
  3103. "Run through the buffer and add overlays to links."
  3104. (catch 'exit
  3105. (let (f)
  3106. (while (re-search-forward org-plain-link-re limit t)
  3107. (setq f (get-text-property (match-beginning 0) 'face))
  3108. (if (or (eq f 'org-tag)
  3109. (and (listp f) (memq 'org-tag f)))
  3110. nil
  3111. (add-text-properties (match-beginning 0) (match-end 0)
  3112. (list 'mouse-face 'highlight
  3113. 'rear-nonsticky org-nonsticky-props
  3114. 'keymap org-mouse-map
  3115. ))
  3116. (throw 'exit t))))))
  3117. (defun org-activate-code (limit)
  3118. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3119. (progn
  3120. (remove-text-properties (match-beginning 0) (match-end 0)
  3121. '(display t invisible t intangible t))
  3122. t)))
  3123. (defun org-activate-angle-links (limit)
  3124. "Run through the buffer and add overlays to links."
  3125. (if (re-search-forward org-angle-link-re limit t)
  3126. (progn
  3127. (add-text-properties (match-beginning 0) (match-end 0)
  3128. (list 'mouse-face 'highlight
  3129. 'rear-nonsticky org-nonsticky-props
  3130. 'keymap org-mouse-map
  3131. ))
  3132. t)))
  3133. (defun org-activate-bracket-links (limit)
  3134. "Run through the buffer and add overlays to bracketed links."
  3135. (if (re-search-forward org-bracket-link-regexp limit t)
  3136. (let* ((help (concat "LINK: "
  3137. (org-match-string-no-properties 1)))
  3138. ;; FIXME: above we should remove the escapes.
  3139. ;; but that requires another match, protecting match data,
  3140. ;; a lot of overhead for font-lock.
  3141. (ip (org-maybe-intangible
  3142. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3143. 'keymap org-mouse-map 'mouse-face 'highlight
  3144. 'font-lock-multiline t 'help-echo help)))
  3145. (vp (list 'rear-nonsticky org-nonsticky-props
  3146. 'keymap org-mouse-map 'mouse-face 'highlight
  3147. ' font-lock-multiline t 'help-echo help)))
  3148. ;; We need to remove the invisible property here. Table narrowing
  3149. ;; may have made some of this invisible.
  3150. (remove-text-properties (match-beginning 0) (match-end 0)
  3151. '(invisible nil))
  3152. (if (match-end 3)
  3153. (progn
  3154. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3155. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3156. (add-text-properties (match-end 3) (match-end 0) ip))
  3157. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3158. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3159. (add-text-properties (match-end 1) (match-end 0) ip))
  3160. t)))
  3161. (defun org-activate-dates (limit)
  3162. "Run through the buffer and add overlays to dates."
  3163. (if (re-search-forward org-tsr-regexp-both limit t)
  3164. (progn
  3165. (add-text-properties (match-beginning 0) (match-end 0)
  3166. (list 'mouse-face 'highlight
  3167. 'rear-nonsticky org-nonsticky-props
  3168. 'keymap org-mouse-map))
  3169. (when org-display-custom-times
  3170. (if (match-end 3)
  3171. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3172. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3173. t)))
  3174. (defvar org-target-link-regexp nil
  3175. "Regular expression matching radio targets in plain text.")
  3176. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3177. "Regular expression matching a link target.")
  3178. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3179. "Regular expression matching a radio target.")
  3180. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3181. "Regular expression matching any target.")
  3182. (defun org-activate-target-links (limit)
  3183. "Run through the buffer and add overlays to target matches."
  3184. (when org-target-link-regexp
  3185. (let ((case-fold-search t))
  3186. (if (re-search-forward org-target-link-regexp limit t)
  3187. (progn
  3188. (add-text-properties (match-beginning 0) (match-end 0)
  3189. (list 'mouse-face 'highlight
  3190. 'rear-nonsticky org-nonsticky-props
  3191. 'keymap org-mouse-map
  3192. 'help-echo "Radio target link"
  3193. 'org-linked-text t))
  3194. t)))))
  3195. (defun org-update-radio-target-regexp ()
  3196. "Find all radio targets in this file and update the regular expression."
  3197. (interactive)
  3198. (when (memq 'radio org-activate-links)
  3199. (setq org-target-link-regexp
  3200. (org-make-target-link-regexp (org-all-targets 'radio)))
  3201. (org-restart-font-lock)))
  3202. (defun org-hide-wide-columns (limit)
  3203. (let (s e)
  3204. (setq s (text-property-any (point) (or limit (point-max))
  3205. 'org-cwidth t))
  3206. (when s
  3207. (setq e (next-single-property-change s 'org-cwidth))
  3208. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3209. (goto-char e)
  3210. t)))
  3211. (defvar org-latex-and-specials-regexp nil
  3212. "Regular expression for highlighting export special stuff.")
  3213. (defvar org-match-substring-regexp)
  3214. (defvar org-match-substring-with-braces-regexp)
  3215. (defvar org-export-html-special-string-regexps)
  3216. (defun org-compute-latex-and-specials-regexp ()
  3217. "Compute regular expression for stuff treated specially by exporters."
  3218. (if (not org-highlight-latex-fragments-and-specials)
  3219. (org-set-local 'org-latex-and-specials-regexp nil)
  3220. (require 'org-exp)
  3221. (let*
  3222. ((matchers (plist-get org-format-latex-options :matchers))
  3223. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3224. org-latex-regexps)))
  3225. (options (org-combine-plists (org-default-export-plist)
  3226. (org-infile-export-plist)))
  3227. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3228. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3229. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3230. (org-export-html-expand (plist-get options :expand-quoted-html))
  3231. (org-export-with-special-strings (plist-get options :special-strings))
  3232. (re-sub
  3233. (cond
  3234. ((equal org-export-with-sub-superscripts '{})
  3235. (list org-match-substring-with-braces-regexp))
  3236. (org-export-with-sub-superscripts
  3237. (list org-match-substring-regexp))
  3238. (t nil)))
  3239. (re-latex
  3240. (if org-export-with-LaTeX-fragments
  3241. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3242. (re-macros
  3243. (if org-export-with-TeX-macros
  3244. (list (concat "\\\\"
  3245. (regexp-opt
  3246. (append (mapcar 'car org-html-entities)
  3247. (if (boundp 'org-latex-entities)
  3248. org-latex-entities nil))
  3249. 'words))) ; FIXME
  3250. ))
  3251. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3252. (re-special (if org-export-with-special-strings
  3253. (mapcar (lambda (x) (car x))
  3254. org-export-html-special-string-regexps)))
  3255. (re-rest
  3256. (delq nil
  3257. (list
  3258. (if org-export-html-expand "@<[^>\n]+>")
  3259. ))))
  3260. (org-set-local
  3261. 'org-latex-and-specials-regexp
  3262. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3263. re-rest) "\\|")))))
  3264. (defun org-do-latex-and-special-faces (limit)
  3265. "Run through the buffer and add overlays to links."
  3266. (when org-latex-and-specials-regexp
  3267. (let (rtn d)
  3268. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3269. limit t))
  3270. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3271. 'face))
  3272. '(org-code org-verbatim underline)))
  3273. (progn
  3274. (setq rtn t
  3275. d (cond ((member (char-after (1+ (match-beginning 0)))
  3276. '(?_ ?^)) 1)
  3277. (t 0)))
  3278. (font-lock-prepend-text-property
  3279. (+ d (match-beginning 0)) (match-end 0)
  3280. 'face 'org-latex-and-export-specials)
  3281. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3282. '(font-lock-multiline t)))))
  3283. rtn)))
  3284. (defun org-restart-font-lock ()
  3285. "Restart font-lock-mode, to force refontification."
  3286. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3287. (font-lock-mode -1)
  3288. (font-lock-mode 1)))
  3289. (defun org-all-targets (&optional radio)
  3290. "Return a list of all targets in this file.
  3291. With optional argument RADIO, only find radio targets."
  3292. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3293. rtn)
  3294. (save-excursion
  3295. (goto-char (point-min))
  3296. (while (re-search-forward re nil t)
  3297. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3298. rtn)))
  3299. (defun org-make-target-link-regexp (targets)
  3300. "Make regular expression matching all strings in TARGETS.
  3301. The regular expression finds the targets also if there is a line break
  3302. between words."
  3303. (and targets
  3304. (concat
  3305. "\\<\\("
  3306. (mapconcat
  3307. (lambda (x)
  3308. (while (string-match " +" x)
  3309. (setq x (replace-match "\\s-+" t t x)))
  3310. x)
  3311. targets
  3312. "\\|")
  3313. "\\)\\>")))
  3314. (defun org-activate-tags (limit)
  3315. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3316. (progn
  3317. (add-text-properties (match-beginning 1) (match-end 1)
  3318. (list 'mouse-face 'highlight
  3319. 'rear-nonsticky org-nonsticky-props
  3320. 'keymap org-mouse-map))
  3321. t)))
  3322. (defun org-outline-level ()
  3323. (save-excursion
  3324. (looking-at outline-regexp)
  3325. (if (match-beginning 1)
  3326. (+ (org-get-string-indentation (match-string 1)) 1000)
  3327. (1- (- (match-end 0) (match-beginning 0))))))
  3328. (defvar org-font-lock-keywords nil)
  3329. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3330. "Regular expression matching a property line.")
  3331. (defvar org-font-lock-hook nil
  3332. "Functions to be called for special font lock stuff.")
  3333. (defun org-font-lock-hook (limit)
  3334. (run-hook-with-args 'org-font-lock-hook limit))
  3335. (defun org-set-font-lock-defaults ()
  3336. (let* ((em org-fontify-emphasized-text)
  3337. (lk org-activate-links)
  3338. (org-font-lock-extra-keywords
  3339. (list
  3340. ;; Call the hook
  3341. '(org-font-lock-hook)
  3342. ;; Headlines
  3343. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3344. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3345. ;; Table lines
  3346. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3347. (1 'org-table t))
  3348. ;; Table internals
  3349. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3350. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3351. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3352. ;; Drawers
  3353. (list org-drawer-regexp '(0 'org-special-keyword t))
  3354. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3355. ;; Properties
  3356. (list org-property-re
  3357. '(1 'org-special-keyword t)
  3358. '(3 'org-property-value t))
  3359. (if org-format-transports-properties-p
  3360. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3361. ;; Links
  3362. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3363. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3364. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3365. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3366. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3367. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3368. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3369. '(org-hide-wide-columns (0 nil append))
  3370. ;; TODO lines
  3371. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3372. '(1 (org-get-todo-face 1) t))
  3373. ;; DONE
  3374. (if org-fontify-done-headline
  3375. (list (concat "^[*]+ +\\<\\("
  3376. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3377. "\\)\\(.*\\)")
  3378. '(2 'org-headline-done t))
  3379. nil)
  3380. ;; Priorities
  3381. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3382. ;; Special keywords
  3383. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3384. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3385. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3386. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3387. ;; Emphasis
  3388. (if em
  3389. (if (featurep 'xemacs)
  3390. '(org-do-emphasis-faces (0 nil append))
  3391. '(org-do-emphasis-faces)))
  3392. ;; Checkboxes
  3393. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3394. 2 'bold prepend)
  3395. (if org-provide-checkbox-statistics
  3396. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3397. (0 (org-get-checkbox-statistics-face) t)))
  3398. ;; Description list items
  3399. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3400. 2 'bold prepend)
  3401. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3402. '(1 'org-archived prepend))
  3403. ;; Specials
  3404. '(org-do-latex-and-special-faces)
  3405. ;; Code
  3406. '(org-activate-code (1 'org-code t))
  3407. ;; COMMENT
  3408. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3409. "\\|" org-quote-string "\\)\\>")
  3410. '(1 'org-special-keyword t))
  3411. '("^#.*" (0 'font-lock-comment-face t))
  3412. )))
  3413. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3414. ;; Now set the full font-lock-keywords
  3415. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3416. (org-set-local 'font-lock-defaults
  3417. '(org-font-lock-keywords t nil nil backward-paragraph))
  3418. (kill-local-variable 'font-lock-keywords) nil))
  3419. (defvar org-m nil)
  3420. (defvar org-l nil)
  3421. (defvar org-f nil)
  3422. (defun org-get-level-face (n)
  3423. "Get the right face for match N in font-lock matching of healdines."
  3424. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3425. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3426. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3427. (cond
  3428. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3429. ((eq n 2) org-f)
  3430. (t (if org-level-color-stars-only nil org-f))))
  3431. (defun org-get-todo-face (kwd)
  3432. "Get the right face for a TODO keyword KWD.
  3433. If KWD is a number, get the corresponding match group."
  3434. (if (numberp kwd) (setq kwd (match-string kwd)))
  3435. (or (cdr (assoc kwd org-todo-keyword-faces))
  3436. (and (member kwd org-done-keywords) 'org-done)
  3437. 'org-todo))
  3438. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3439. "Remove fontification and activation overlays from links."
  3440. (font-lock-default-unfontify-region beg end)
  3441. (let* ((buffer-undo-list t)
  3442. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3443. (inhibit-modification-hooks t)
  3444. deactivate-mark buffer-file-name buffer-file-truename)
  3445. (remove-text-properties beg end
  3446. '(mouse-face t keymap t org-linked-text t
  3447. invisible t intangible t))))
  3448. ;;;; Visibility cycling, including org-goto and indirect buffer
  3449. ;;; Cycling
  3450. (defvar org-cycle-global-status nil)
  3451. (make-variable-buffer-local 'org-cycle-global-status)
  3452. (defvar org-cycle-subtree-status nil)
  3453. (make-variable-buffer-local 'org-cycle-subtree-status)
  3454. ;;;###autoload
  3455. (defun org-cycle (&optional arg)
  3456. "Visibility cycling for Org-mode.
  3457. - When this function is called with a prefix argument, rotate the entire
  3458. buffer through 3 states (global cycling)
  3459. 1. OVERVIEW: Show only top-level headlines.
  3460. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3461. 3. SHOW ALL: Show everything.
  3462. When called with two C-u C-u prefixes, switch to the startup visibility,
  3463. determined by the variable `org-startup-folded', and by any VISIBILITY
  3464. properties in the buffer.
  3465. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3466. including drawers.
  3467. - When point is at the beginning of a headline, rotate the subtree started
  3468. by this line through 3 different states (local cycling)
  3469. 1. FOLDED: Only the main headline is shown.
  3470. 2. CHILDREN: The main headline and the direct children are shown.
  3471. From this state, you can move to one of the children
  3472. and zoom in further.
  3473. 3. SUBTREE: Show the entire subtree, including body text.
  3474. - When there is a numeric prefix, go up to a heading with level ARG, do
  3475. a `show-subtree' and return to the previous cursor position. If ARG
  3476. is negative, go up that many levels.
  3477. - When point is not at the beginning of a headline, execute the global
  3478. binding for TAB, which is re-indenting the line. See the option
  3479. `org-cycle-emulate-tab' for details.
  3480. - Special case: if point is at the beginning of the buffer and there is
  3481. no headline in line 1, this function will act as if called with prefix arg.
  3482. But only if also the variable `org-cycle-global-at-bob' is t."
  3483. (interactive "P")
  3484. (org-load-modules-maybe)
  3485. (let* ((outline-regexp
  3486. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3487. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3488. outline-regexp))
  3489. (bob-special (and org-cycle-global-at-bob (bobp)
  3490. (not (looking-at outline-regexp))))
  3491. (org-cycle-hook
  3492. (if bob-special
  3493. (delq 'org-optimize-window-after-visibility-change
  3494. (copy-sequence org-cycle-hook))
  3495. org-cycle-hook))
  3496. (pos (point)))
  3497. (if (or bob-special (equal arg '(4)))
  3498. ;; special case: use global cycling
  3499. (setq arg t))
  3500. (cond
  3501. ((equal arg '(16))
  3502. (org-set-startup-visibility)
  3503. (message "Startup visibility, plus VISIBILITY properties"))
  3504. ((equal arg '(64))
  3505. (show-all)
  3506. (message "Entire buffer visible, including drawers"))
  3507. ((org-at-table-p 'any)
  3508. ;; Enter the table or move to the next field in the table
  3509. (or (org-table-recognize-table.el)
  3510. (progn
  3511. (if arg (org-table-edit-field t)
  3512. (org-table-justify-field-maybe)
  3513. (call-interactively 'org-table-next-field)))))
  3514. ((eq arg t) ;; Global cycling
  3515. (cond
  3516. ((and (eq last-command this-command)
  3517. (eq org-cycle-global-status 'overview))
  3518. ;; We just created the overview - now do table of contents
  3519. ;; This can be slow in very large buffers, so indicate action
  3520. (message "CONTENTS...")
  3521. (org-content)
  3522. (message "CONTENTS...done")
  3523. (setq org-cycle-global-status 'contents)
  3524. (run-hook-with-args 'org-cycle-hook 'contents))
  3525. ((and (eq last-command this-command)
  3526. (eq org-cycle-global-status 'contents))
  3527. ;; We just showed the table of contents - now show everything
  3528. (show-all)
  3529. (message "SHOW ALL")
  3530. (setq org-cycle-global-status 'all)
  3531. (run-hook-with-args 'org-cycle-hook 'all))
  3532. (t
  3533. ;; Default action: go to overview
  3534. (org-overview)
  3535. (message "OVERVIEW")
  3536. (setq org-cycle-global-status 'overview)
  3537. (run-hook-with-args 'org-cycle-hook 'overview))))
  3538. ((and org-drawers org-drawer-regexp
  3539. (save-excursion
  3540. (beginning-of-line 1)
  3541. (looking-at org-drawer-regexp)))
  3542. ;; Toggle block visibility
  3543. (org-flag-drawer
  3544. (not (get-char-property (match-end 0) 'invisible))))
  3545. ((integerp arg)
  3546. ;; Show-subtree, ARG levels up from here.
  3547. (save-excursion
  3548. (org-back-to-heading)
  3549. (outline-up-heading (if (< arg 0) (- arg)
  3550. (- (funcall outline-level) arg)))
  3551. (org-show-subtree)))
  3552. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3553. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3554. ;; At a heading: rotate between three different views
  3555. (org-back-to-heading)
  3556. (let ((goal-column 0) eoh eol eos)
  3557. ;; First, some boundaries
  3558. (save-excursion
  3559. (org-back-to-heading)
  3560. (save-excursion
  3561. (beginning-of-line 2)
  3562. (while (and (not (eobp)) ;; this is like `next-line'
  3563. (get-char-property (1- (point)) 'invisible))
  3564. (beginning-of-line 2)) (setq eol (point)))
  3565. (outline-end-of-heading) (setq eoh (point))
  3566. (org-end-of-subtree t)
  3567. (unless (eobp)
  3568. (skip-chars-forward " \t\n")
  3569. (beginning-of-line 1) ; in case this is an item
  3570. )
  3571. (setq eos (1- (point))))
  3572. ;; Find out what to do next and set `this-command'
  3573. (cond
  3574. ((= eos eoh)
  3575. ;; Nothing is hidden behind this heading
  3576. (message "EMPTY ENTRY")
  3577. (setq org-cycle-subtree-status nil)
  3578. (save-excursion
  3579. (goto-char eos)
  3580. (outline-next-heading)
  3581. (if (org-invisible-p) (org-flag-heading nil))))
  3582. ((or (>= eol eos)
  3583. (not (string-match "\\S-" (buffer-substring eol eos))))
  3584. ;; Entire subtree is hidden in one line: open it
  3585. (org-show-entry)
  3586. (show-children)
  3587. (message "CHILDREN")
  3588. (save-excursion
  3589. (goto-char eos)
  3590. (outline-next-heading)
  3591. (if (org-invisible-p) (org-flag-heading nil)))
  3592. (setq org-cycle-subtree-status 'children)
  3593. (run-hook-with-args 'org-cycle-hook 'children))
  3594. ((and (eq last-command this-command)
  3595. (eq org-cycle-subtree-status 'children))
  3596. ;; We just showed the children, now show everything.
  3597. (org-show-subtree)
  3598. (message "SUBTREE")
  3599. (setq org-cycle-subtree-status 'subtree)
  3600. (run-hook-with-args 'org-cycle-hook 'subtree))
  3601. (t
  3602. ;; Default action: hide the subtree.
  3603. (hide-subtree)
  3604. (message "FOLDED")
  3605. (setq org-cycle-subtree-status 'folded)
  3606. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3607. ;; TAB emulation and template completion
  3608. (buffer-read-only (org-back-to-heading))
  3609. ((org-try-structure-completion))
  3610. ((org-try-cdlatex-tab))
  3611. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3612. (or (not (bolp))
  3613. (not (looking-at outline-regexp))))
  3614. (call-interactively (global-key-binding "\t")))
  3615. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3616. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3617. (or (and (eq org-cycle-emulate-tab 'white)
  3618. (= (match-end 0) (point-at-eol)))
  3619. (and (eq org-cycle-emulate-tab 'whitestart)
  3620. (>= (match-end 0) pos))))
  3621. t
  3622. (eq org-cycle-emulate-tab t))
  3623. (call-interactively (global-key-binding "\t")))
  3624. (t (save-excursion
  3625. (org-back-to-heading)
  3626. (org-cycle))))))
  3627. ;;;###autoload
  3628. (defun org-global-cycle (&optional arg)
  3629. "Cycle the global visibility. For details see `org-cycle'.
  3630. With C-u prefix arg, switch to startup visibility.
  3631. With a numeric prefix, show all headlines up to that level."
  3632. (interactive "P")
  3633. (let ((org-cycle-include-plain-lists
  3634. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3635. (cond
  3636. ((integerp arg)
  3637. (show-all)
  3638. (hide-sublevels arg)
  3639. (setq org-cycle-global-status 'contents))
  3640. ((equal arg '(4))
  3641. (org-set-startup-visibility)
  3642. (message "Startup visibility, plus VISIBILITY properties."))
  3643. (t
  3644. (org-cycle '(4))))))
  3645. (defun org-set-startup-visibility ()
  3646. "Set the visibility required by startup options and properties."
  3647. (cond
  3648. ((eq org-startup-folded t)
  3649. (org-cycle '(4)))
  3650. ((eq org-startup-folded 'content)
  3651. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3652. (org-cycle '(4)) (org-cycle '(4)))))
  3653. (org-set-visibility-according-to-property 'no-cleanup)
  3654. (org-cycle-hide-archived-subtrees 'all)
  3655. (org-cycle-hide-drawers 'all)
  3656. (org-cycle-show-empty-lines 'all))
  3657. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3658. "Switch subtree visibilities according to :VISIBILITY: property."
  3659. (interactive)
  3660. (let (state)
  3661. (save-excursion
  3662. (goto-char (point-min))
  3663. (while (re-search-forward
  3664. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3665. nil t)
  3666. (setq state (match-string 1))
  3667. (save-excursion
  3668. (org-back-to-heading t)
  3669. (hide-subtree)
  3670. (org-reveal)
  3671. (cond
  3672. ((equal state '("fold" "folded"))
  3673. (hide-subtree))
  3674. ((equal state "children")
  3675. (org-show-hidden-entry)
  3676. (show-children))
  3677. ((equal state "content")
  3678. (save-excursion
  3679. (save-restriction
  3680. (org-narrow-to-subtree)
  3681. (org-content))))
  3682. ((member state '("all" "showall"))
  3683. (show-subtree)))))
  3684. (unless no-cleanup
  3685. (org-cycle-hide-archived-subtrees 'all)
  3686. (org-cycle-hide-drawers 'all)
  3687. (org-cycle-show-empty-lines 'all)))))
  3688. (defun org-overview ()
  3689. "Switch to overview mode, shoing only top-level headlines.
  3690. Really, this shows all headlines with level equal or greater than the level
  3691. of the first headline in the buffer. This is important, because if the
  3692. first headline is not level one, then (hide-sublevels 1) gives confusing
  3693. results."
  3694. (interactive)
  3695. (let ((level (save-excursion
  3696. (goto-char (point-min))
  3697. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3698. (progn
  3699. (goto-char (match-beginning 0))
  3700. (funcall outline-level))))))
  3701. (and level (hide-sublevels level))))
  3702. (defun org-content (&optional arg)
  3703. "Show all headlines in the buffer, like a table of contents.
  3704. With numerical argument N, show content up to level N."
  3705. (interactive "P")
  3706. (save-excursion
  3707. ;; Visit all headings and show their offspring
  3708. (and (integerp arg) (org-overview))
  3709. (goto-char (point-max))
  3710. (catch 'exit
  3711. (while (and (progn (condition-case nil
  3712. (outline-previous-visible-heading 1)
  3713. (error (goto-char (point-min))))
  3714. t)
  3715. (looking-at outline-regexp))
  3716. (if (integerp arg)
  3717. (show-children (1- arg))
  3718. (show-branches))
  3719. (if (bobp) (throw 'exit nil))))))
  3720. (defun org-optimize-window-after-visibility-change (state)
  3721. "Adjust the window after a change in outline visibility.
  3722. This function is the default value of the hook `org-cycle-hook'."
  3723. (when (get-buffer-window (current-buffer))
  3724. (cond
  3725. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3726. ; ((eq state 'overview) (org-beginning-of-line))
  3727. ((eq state 'content) nil)
  3728. ((eq state 'all) nil)
  3729. ((eq state 'folded) nil)
  3730. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3731. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3732. (defun org-compact-display-after-subtree-move ()
  3733. (let (beg end)
  3734. (save-excursion
  3735. (if (org-up-heading-safe)
  3736. (progn
  3737. (hide-subtree)
  3738. (show-entry)
  3739. (show-children)
  3740. (org-cycle-show-empty-lines 'children)
  3741. (org-cycle-hide-drawers 'children))
  3742. (org-overview)))))
  3743. (defun org-cycle-show-empty-lines (state)
  3744. "Show empty lines above all visible headlines.
  3745. The region to be covered depends on STATE when called through
  3746. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3747. entire buffer covered. Note that an empty line is only shown if there
  3748. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3749. (when (> org-cycle-separator-lines 0)
  3750. (save-excursion
  3751. (let* ((n org-cycle-separator-lines)
  3752. (re (cond
  3753. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3754. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3755. (t (let ((ns (number-to-string (- n 2))))
  3756. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3757. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3758. beg end)
  3759. (cond
  3760. ((memq state '(overview contents t))
  3761. (setq beg (point-min) end (point-max)))
  3762. ((memq state '(children folded))
  3763. (setq beg (point) end (progn (org-end-of-subtree t t)
  3764. (beginning-of-line 2)
  3765. (point)))))
  3766. (when beg
  3767. (goto-char beg)
  3768. (while (re-search-forward re end t)
  3769. (if (not (get-char-property (match-end 1) 'invisible))
  3770. (outline-flag-region
  3771. (match-beginning 1) (match-end 1) nil)))))))
  3772. ;; Never hide empty lines at the end of the file.
  3773. (save-excursion
  3774. (goto-char (point-max))
  3775. (outline-previous-heading)
  3776. (outline-end-of-heading)
  3777. (if (and (looking-at "[ \t\n]+")
  3778. (= (match-end 0) (point-max)))
  3779. (outline-flag-region (point) (match-end 0) nil))))
  3780. (defun org-show-empty-lines-in-parent ()
  3781. "Move to the parent and re-show empty lines before visible headlines."
  3782. (save-excursion
  3783. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3784. (org-cycle-show-empty-lines context))))
  3785. (defun org-cycle-hide-drawers (state)
  3786. "Re-hide all drawers after a visibility state change."
  3787. (when (and (org-mode-p)
  3788. (not (memq state '(overview folded))))
  3789. (save-excursion
  3790. (let* ((globalp (memq state '(contents all)))
  3791. (beg (if globalp (point-min) (point)))
  3792. (end (if globalp (point-max) (org-end-of-subtree t))))
  3793. (goto-char beg)
  3794. (while (re-search-forward org-drawer-regexp end t)
  3795. (org-flag-drawer t))))))
  3796. (defun org-flag-drawer (flag)
  3797. (save-excursion
  3798. (beginning-of-line 1)
  3799. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3800. (let ((b (match-end 0))
  3801. (outline-regexp org-outline-regexp))
  3802. (if (re-search-forward
  3803. "^[ \t]*:END:"
  3804. (save-excursion (outline-next-heading) (point)) t)
  3805. (outline-flag-region b (point-at-eol) flag)
  3806. (error ":END: line missing"))))))
  3807. (defun org-subtree-end-visible-p ()
  3808. "Is the end of the current subtree visible?"
  3809. (pos-visible-in-window-p
  3810. (save-excursion (org-end-of-subtree t) (point))))
  3811. (defun org-first-headline-recenter (&optional N)
  3812. "Move cursor to the first headline and recenter the headline.
  3813. Optional argument N means, put the headline into the Nth line of the window."
  3814. (goto-char (point-min))
  3815. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3816. (beginning-of-line)
  3817. (recenter (prefix-numeric-value N))))
  3818. ;;; Org-goto
  3819. (defvar org-goto-window-configuration nil)
  3820. (defvar org-goto-marker nil)
  3821. (defvar org-goto-map
  3822. (let ((map (make-sparse-keymap)))
  3823. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3824. (while (setq cmd (pop cmds))
  3825. (substitute-key-definition cmd cmd map global-map)))
  3826. (suppress-keymap map)
  3827. (org-defkey map "\C-m" 'org-goto-ret)
  3828. (org-defkey map [(return)] 'org-goto-ret)
  3829. (org-defkey map [(left)] 'org-goto-left)
  3830. (org-defkey map [(right)] 'org-goto-right)
  3831. (org-defkey map [(control ?g)] 'org-goto-quit)
  3832. (org-defkey map "\C-i" 'org-cycle)
  3833. (org-defkey map [(tab)] 'org-cycle)
  3834. (org-defkey map [(down)] 'outline-next-visible-heading)
  3835. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3836. (if org-goto-auto-isearch
  3837. (if (fboundp 'define-key-after)
  3838. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3839. nil)
  3840. (org-defkey map "q" 'org-goto-quit)
  3841. (org-defkey map "n" 'outline-next-visible-heading)
  3842. (org-defkey map "p" 'outline-previous-visible-heading)
  3843. (org-defkey map "f" 'outline-forward-same-level)
  3844. (org-defkey map "b" 'outline-backward-same-level)
  3845. (org-defkey map "u" 'outline-up-heading))
  3846. (org-defkey map "/" 'org-occur)
  3847. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3848. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3849. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3850. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3851. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3852. map))
  3853. (defconst org-goto-help
  3854. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3855. RET=jump to location [Q]uit and return to previous location
  3856. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3857. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3858. ;; FIXME: Docstring doe not mention both interfaces
  3859. (defun org-goto (&optional alternative-interface)
  3860. "Look up a different location in the current file, keeping current visibility.
  3861. When you want look-up or go to a different location in a document, the
  3862. fastest way is often to fold the entire buffer and then dive into the tree.
  3863. This method has the disadvantage, that the previous location will be folded,
  3864. which may not be what you want.
  3865. This command works around this by showing a copy of the current buffer
  3866. in an indirect buffer, in overview mode. You can dive into the tree in
  3867. that copy, use org-occur and incremental search to find a location.
  3868. When pressing RET or `Q', the command returns to the original buffer in
  3869. which the visibility is still unchanged. After RET is will also jump to
  3870. the location selected in the indirect buffer and expose the
  3871. the headline hierarchy above."
  3872. (interactive "P")
  3873. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3874. (org-refile-use-outline-path t)
  3875. (interface
  3876. (if (not alternative-interface)
  3877. org-goto-interface
  3878. (if (eq org-goto-interface 'outline)
  3879. 'outline-path-completion
  3880. 'outline)))
  3881. (org-goto-start-pos (point))
  3882. (selected-point
  3883. (if (eq interface 'outline)
  3884. (car (org-get-location (current-buffer) org-goto-help))
  3885. (nth 3 (org-refile-get-location "Goto: ")))))
  3886. (if selected-point
  3887. (progn
  3888. (org-mark-ring-push org-goto-start-pos)
  3889. (goto-char selected-point)
  3890. (if (or (org-invisible-p) (org-invisible-p2))
  3891. (org-show-context 'org-goto)))
  3892. (message "Quit"))))
  3893. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3894. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3895. (defvar org-goto-local-auto-isearch-map) ; defined below
  3896. (defun org-get-location (buf help)
  3897. "Let the user select a location in the Org-mode buffer BUF.
  3898. This function uses a recursive edit. It returns the selected position
  3899. or nil."
  3900. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3901. (isearch-hide-immediately nil)
  3902. (isearch-search-fun-function
  3903. (lambda () 'org-goto-local-search-headings))
  3904. (org-goto-selected-point org-goto-exit-command))
  3905. (save-excursion
  3906. (save-window-excursion
  3907. (delete-other-windows)
  3908. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3909. (switch-to-buffer
  3910. (condition-case nil
  3911. (make-indirect-buffer (current-buffer) "*org-goto*")
  3912. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3913. (with-output-to-temp-buffer "*Help*"
  3914. (princ help))
  3915. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3916. (setq buffer-read-only nil)
  3917. (let ((org-startup-truncated t)
  3918. (org-startup-folded nil)
  3919. (org-startup-align-all-tables nil))
  3920. (org-mode)
  3921. (org-overview))
  3922. (setq buffer-read-only t)
  3923. (if (and (boundp 'org-goto-start-pos)
  3924. (integer-or-marker-p org-goto-start-pos))
  3925. (let ((org-show-hierarchy-above t)
  3926. (org-show-siblings t)
  3927. (org-show-following-heading t))
  3928. (goto-char org-goto-start-pos)
  3929. (and (org-invisible-p) (org-show-context)))
  3930. (goto-char (point-min)))
  3931. (org-beginning-of-line)
  3932. (message "Select location and press RET")
  3933. (use-local-map org-goto-map)
  3934. (recursive-edit)
  3935. ))
  3936. (kill-buffer "*org-goto*")
  3937. (cons org-goto-selected-point org-goto-exit-command)))
  3938. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3939. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3940. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3941. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3942. (defun org-goto-local-search-headings (string bound noerror)
  3943. "Search and make sure that any matches are in headlines."
  3944. (catch 'return
  3945. (while (if isearch-forward
  3946. (search-forward string bound noerror)
  3947. (search-backward string bound noerror))
  3948. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3949. (and (member :headline context)
  3950. (not (member :tags context))))
  3951. (throw 'return (point))))))
  3952. (defun org-goto-local-auto-isearch ()
  3953. "Start isearch."
  3954. (interactive)
  3955. (goto-char (point-min))
  3956. (let ((keys (this-command-keys)))
  3957. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3958. (isearch-mode t)
  3959. (isearch-process-search-char (string-to-char keys)))))
  3960. (defun org-goto-ret (&optional arg)
  3961. "Finish `org-goto' by going to the new location."
  3962. (interactive "P")
  3963. (setq org-goto-selected-point (point)
  3964. org-goto-exit-command 'return)
  3965. (throw 'exit nil))
  3966. (defun org-goto-left ()
  3967. "Finish `org-goto' by going to the new location."
  3968. (interactive)
  3969. (if (org-on-heading-p)
  3970. (progn
  3971. (beginning-of-line 1)
  3972. (setq org-goto-selected-point (point)
  3973. org-goto-exit-command 'left)
  3974. (throw 'exit nil))
  3975. (error "Not on a heading")))
  3976. (defun org-goto-right ()
  3977. "Finish `org-goto' by going to the new location."
  3978. (interactive)
  3979. (if (org-on-heading-p)
  3980. (progn
  3981. (setq org-goto-selected-point (point)
  3982. org-goto-exit-command 'right)
  3983. (throw 'exit nil))
  3984. (error "Not on a heading")))
  3985. (defun org-goto-quit ()
  3986. "Finish `org-goto' without cursor motion."
  3987. (interactive)
  3988. (setq org-goto-selected-point nil)
  3989. (setq org-goto-exit-command 'quit)
  3990. (throw 'exit nil))
  3991. ;;; Indirect buffer display of subtrees
  3992. (defvar org-indirect-dedicated-frame nil
  3993. "This is the frame being used for indirect tree display.")
  3994. (defvar org-last-indirect-buffer nil)
  3995. (defun org-tree-to-indirect-buffer (&optional arg)
  3996. "Create indirect buffer and narrow it to current subtree.
  3997. With numerical prefix ARG, go up to this level and then take that tree.
  3998. If ARG is negative, go up that many levels.
  3999. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4000. indirect buffer previously made with this command, to avoid proliferation of
  4001. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4002. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4003. is kept so that you can work with several indirect buffers at the same time.
  4004. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4005. requests that a new frame be made for the new buffer, so that the dedicated
  4006. frame is not changed."
  4007. (interactive "P")
  4008. (let ((cbuf (current-buffer))
  4009. (cwin (selected-window))
  4010. (pos (point))
  4011. beg end level heading ibuf)
  4012. (save-excursion
  4013. (org-back-to-heading t)
  4014. (when (numberp arg)
  4015. (setq level (org-outline-level))
  4016. (if (< arg 0) (setq arg (+ level arg)))
  4017. (while (> (setq level (org-outline-level)) arg)
  4018. (outline-up-heading 1 t)))
  4019. (setq beg (point)
  4020. heading (org-get-heading))
  4021. (org-end-of-subtree t) (setq end (point)))
  4022. (if (and (buffer-live-p org-last-indirect-buffer)
  4023. (not (eq org-indirect-buffer-display 'new-frame))
  4024. (not arg))
  4025. (kill-buffer org-last-indirect-buffer))
  4026. (setq ibuf (org-get-indirect-buffer cbuf)
  4027. org-last-indirect-buffer ibuf)
  4028. (cond
  4029. ((or (eq org-indirect-buffer-display 'new-frame)
  4030. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4031. (select-frame (make-frame))
  4032. (delete-other-windows)
  4033. (switch-to-buffer ibuf)
  4034. (org-set-frame-title heading))
  4035. ((eq org-indirect-buffer-display 'dedicated-frame)
  4036. (raise-frame
  4037. (select-frame (or (and org-indirect-dedicated-frame
  4038. (frame-live-p org-indirect-dedicated-frame)
  4039. org-indirect-dedicated-frame)
  4040. (setq org-indirect-dedicated-frame (make-frame)))))
  4041. (delete-other-windows)
  4042. (switch-to-buffer ibuf)
  4043. (org-set-frame-title (concat "Indirect: " heading)))
  4044. ((eq org-indirect-buffer-display 'current-window)
  4045. (switch-to-buffer ibuf))
  4046. ((eq org-indirect-buffer-display 'other-window)
  4047. (pop-to-buffer ibuf))
  4048. (t (error "Invalid value.")))
  4049. (if (featurep 'xemacs)
  4050. (save-excursion (org-mode) (turn-on-font-lock)))
  4051. (narrow-to-region beg end)
  4052. (show-all)
  4053. (goto-char pos)
  4054. (and (window-live-p cwin) (select-window cwin))))
  4055. (defun org-get-indirect-buffer (&optional buffer)
  4056. (setq buffer (or buffer (current-buffer)))
  4057. (let ((n 1) (base (buffer-name buffer)) bname)
  4058. (while (buffer-live-p
  4059. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4060. (setq n (1+ n)))
  4061. (condition-case nil
  4062. (make-indirect-buffer buffer bname 'clone)
  4063. (error (make-indirect-buffer buffer bname)))))
  4064. (defun org-set-frame-title (title)
  4065. "Set the title of the current frame to the string TITLE."
  4066. ;; FIXME: how to name a single frame in XEmacs???
  4067. (unless (featurep 'xemacs)
  4068. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4069. ;;;; Structure editing
  4070. ;;; Inserting headlines
  4071. (defun org-insert-heading (&optional force-heading)
  4072. "Insert a new heading or item with same depth at point.
  4073. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4074. If point is at the beginning of a headline, insert a sibling before the
  4075. current headline. If point is not at the beginning, do not split the line,
  4076. but create the new hedline after the current line."
  4077. (interactive "P")
  4078. (if (= (buffer-size) 0)
  4079. (insert "\n* ")
  4080. (when (or force-heading (not (org-insert-item)))
  4081. (let* ((head (save-excursion
  4082. (condition-case nil
  4083. (progn
  4084. (org-back-to-heading)
  4085. (match-string 0))
  4086. (error "*"))))
  4087. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4088. pos)
  4089. (cond
  4090. ((and (org-on-heading-p) (bolp)
  4091. (or (bobp)
  4092. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4093. ;; insert before the current line
  4094. (open-line (if blank 2 1)))
  4095. ((and (bolp)
  4096. (or (bobp)
  4097. (save-excursion
  4098. (backward-char 1) (not (org-invisible-p)))))
  4099. ;; insert right here
  4100. nil)
  4101. (t
  4102. ;; in the middle of the line
  4103. (org-show-entry)
  4104. (let ((split
  4105. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4106. tags pos)
  4107. (cond
  4108. (org-insert-heading-respect-content
  4109. (org-end-of-subtree nil t)
  4110. (open-line 1))
  4111. ((org-on-heading-p)
  4112. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4113. (setq tags (and (match-end 2) (match-string 2)))
  4114. (and (match-end 1)
  4115. (delete-region (match-beginning 1) (match-end 1)))
  4116. (setq pos (point-at-bol))
  4117. (or split (end-of-line 1))
  4118. (delete-horizontal-space)
  4119. (newline (if blank 2 1))
  4120. (when tags
  4121. (save-excursion
  4122. (goto-char pos)
  4123. (end-of-line 1)
  4124. (insert " " tags)
  4125. (org-set-tags nil 'align))))
  4126. (t
  4127. (or split (end-of-line 1))
  4128. (newline (if blank 2 1)))))))
  4129. (insert head) (just-one-space)
  4130. (setq pos (point))
  4131. (end-of-line 1)
  4132. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4133. (run-hooks 'org-insert-heading-hook)))))
  4134. (defun org-get-heading (&optional no-tags)
  4135. "Return the heading of the current entry, without the stars."
  4136. (save-excursion
  4137. (org-back-to-heading t)
  4138. (if (looking-at
  4139. (if no-tags
  4140. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4141. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4142. (match-string 1) "")))
  4143. (defun org-insert-heading-after-current ()
  4144. "Insert a new heading with same level as current, after current subtree."
  4145. (interactive)
  4146. (org-back-to-heading)
  4147. (org-insert-heading)
  4148. (org-move-subtree-down)
  4149. (end-of-line 1))
  4150. (defun org-insert-heading-respect-content ()
  4151. (interactive)
  4152. (let ((org-insert-heading-respect-content t))
  4153. (call-interactively 'org-insert-heading)))
  4154. (defun org-insert-todo-heading-respect-content ()
  4155. (interactive)
  4156. (let ((org-insert-heading-respect-content t))
  4157. (call-interactively 'org-insert-todo-todo-heading)))
  4158. (defun org-insert-todo-heading (arg)
  4159. "Insert a new heading with the same level and TODO state as current heading.
  4160. If the heading has no TODO state, or if the state is DONE, use the first
  4161. state (TODO by default). Also with prefix arg, force first state."
  4162. (interactive "P")
  4163. (when (not (org-insert-item 'checkbox))
  4164. (org-insert-heading)
  4165. (save-excursion
  4166. (org-back-to-heading)
  4167. (outline-previous-heading)
  4168. (looking-at org-todo-line-regexp))
  4169. (if (or arg
  4170. (not (match-beginning 2))
  4171. (member (match-string 2) org-done-keywords))
  4172. (insert (car org-todo-keywords-1) " ")
  4173. (insert (match-string 2) " "))
  4174. (when org-provide-todo-statistics
  4175. (org-update-parent-todo-statistics))))
  4176. (defun org-insert-subheading (arg)
  4177. "Insert a new subheading and demote it.
  4178. Works for outline headings and for plain lists alike."
  4179. (interactive "P")
  4180. (org-insert-heading arg)
  4181. (cond
  4182. ((org-on-heading-p) (org-do-demote))
  4183. ((org-at-item-p) (org-indent-item 1))))
  4184. (defun org-insert-todo-subheading (arg)
  4185. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4186. Works for outline headings and for plain lists alike."
  4187. (interactive "P")
  4188. (org-insert-todo-heading arg)
  4189. (cond
  4190. ((org-on-heading-p) (org-do-demote))
  4191. ((org-at-item-p) (org-indent-item 1))))
  4192. ;;; Promotion and Demotion
  4193. (defun org-promote-subtree ()
  4194. "Promote the entire subtree.
  4195. See also `org-promote'."
  4196. (interactive)
  4197. (save-excursion
  4198. (org-map-tree 'org-promote))
  4199. (org-fix-position-after-promote))
  4200. (defun org-demote-subtree ()
  4201. "Demote the entire subtree. See `org-demote'.
  4202. See also `org-promote'."
  4203. (interactive)
  4204. (save-excursion
  4205. (org-map-tree 'org-demote))
  4206. (org-fix-position-after-promote))
  4207. (defun org-do-promote ()
  4208. "Promote the current heading higher up the tree.
  4209. If the region is active in `transient-mark-mode', promote all headings
  4210. in the region."
  4211. (interactive)
  4212. (save-excursion
  4213. (if (org-region-active-p)
  4214. (org-map-region 'org-promote (region-beginning) (region-end))
  4215. (org-promote)))
  4216. (org-fix-position-after-promote))
  4217. (defun org-do-demote ()
  4218. "Demote the current heading lower down the tree.
  4219. If the region is active in `transient-mark-mode', demote all headings
  4220. in the region."
  4221. (interactive)
  4222. (save-excursion
  4223. (if (org-region-active-p)
  4224. (org-map-region 'org-demote (region-beginning) (region-end))
  4225. (org-demote)))
  4226. (org-fix-position-after-promote))
  4227. (defun org-fix-position-after-promote ()
  4228. "Make sure that after pro/demotion cursor position is right."
  4229. (let ((pos (point)))
  4230. (when (save-excursion
  4231. (beginning-of-line 1)
  4232. (looking-at org-todo-line-regexp)
  4233. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4234. (cond ((eobp) (insert " "))
  4235. ((eolp) (insert " "))
  4236. ((equal (char-after) ?\ ) (forward-char 1))))))
  4237. (defun org-reduced-level (l)
  4238. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4239. (defun org-get-valid-level (level &optional change)
  4240. "Rectify a level change under the influence of `org-odd-levels-only'
  4241. LEVEL is a current level, CHANGE is by how much the level should be
  4242. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4243. even level numbers will become the next higher odd number."
  4244. (if org-odd-levels-only
  4245. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4246. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4247. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4248. (max 1 (+ level change))))
  4249. (if (boundp 'define-obsolete-function-alias)
  4250. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4251. (define-obsolete-function-alias 'org-get-legal-level
  4252. 'org-get-valid-level)
  4253. (define-obsolete-function-alias 'org-get-legal-level
  4254. 'org-get-valid-level "23.1")))
  4255. (defun org-promote ()
  4256. "Promote the current heading higher up the tree.
  4257. If the region is active in `transient-mark-mode', promote all headings
  4258. in the region."
  4259. (org-back-to-heading t)
  4260. (let* ((level (save-match-data (funcall outline-level)))
  4261. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4262. (diff (abs (- level (length up-head) -1))))
  4263. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4264. (replace-match up-head nil t)
  4265. ;; Fixup tag positioning
  4266. (and org-auto-align-tags (org-set-tags nil t))
  4267. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4268. (defun org-demote ()
  4269. "Demote the current heading lower down the tree.
  4270. If the region is active in `transient-mark-mode', demote all headings
  4271. in the region."
  4272. (org-back-to-heading t)
  4273. (let* ((level (save-match-data (funcall outline-level)))
  4274. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4275. (diff (abs (- level (length down-head) -1))))
  4276. (replace-match down-head nil t)
  4277. ;; Fixup tag positioning
  4278. (and org-auto-align-tags (org-set-tags nil t))
  4279. (if org-adapt-indentation (org-fixup-indentation diff))))
  4280. (defun org-map-tree (fun)
  4281. "Call FUN for every heading underneath the current one."
  4282. (org-back-to-heading)
  4283. (let ((level (funcall outline-level)))
  4284. (save-excursion
  4285. (funcall fun)
  4286. (while (and (progn
  4287. (outline-next-heading)
  4288. (> (funcall outline-level) level))
  4289. (not (eobp)))
  4290. (funcall fun)))))
  4291. (defun org-map-region (fun beg end)
  4292. "Call FUN for every heading between BEG and END."
  4293. (let ((org-ignore-region t))
  4294. (save-excursion
  4295. (setq end (copy-marker end))
  4296. (goto-char beg)
  4297. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4298. (< (point) end))
  4299. (funcall fun))
  4300. (while (and (progn
  4301. (outline-next-heading)
  4302. (< (point) end))
  4303. (not (eobp)))
  4304. (funcall fun)))))
  4305. (defun org-fixup-indentation (diff)
  4306. "Change the indentation in the current entry by DIFF
  4307. However, if any line in the current entry has no indentation, or if it
  4308. would end up with no indentation after the change, nothing at all is done."
  4309. (save-excursion
  4310. (let ((end (save-excursion (outline-next-heading)
  4311. (point-marker)))
  4312. (prohibit (if (> diff 0)
  4313. "^\\S-"
  4314. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4315. col)
  4316. (unless (save-excursion (end-of-line 1)
  4317. (re-search-forward prohibit end t))
  4318. (while (and (< (point) end)
  4319. (re-search-forward "^[ \t]+" end t))
  4320. (goto-char (match-end 0))
  4321. (setq col (current-column))
  4322. (if (< diff 0) (replace-match ""))
  4323. (indent-to (+ diff col))))
  4324. (move-marker end nil))))
  4325. (defun org-convert-to-odd-levels ()
  4326. "Convert an org-mode file with all levels allowed to one with odd levels.
  4327. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4328. level 5 etc."
  4329. (interactive)
  4330. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4331. (let ((org-odd-levels-only nil) n)
  4332. (save-excursion
  4333. (goto-char (point-min))
  4334. (while (re-search-forward "^\\*\\*+ " nil t)
  4335. (setq n (- (length (match-string 0)) 2))
  4336. (while (>= (setq n (1- n)) 0)
  4337. (org-demote))
  4338. (end-of-line 1))))))
  4339. (defun org-convert-to-oddeven-levels ()
  4340. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4341. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4342. section with an even level, conversion would destroy the structure of the file. An error
  4343. is signaled in this case."
  4344. (interactive)
  4345. (goto-char (point-min))
  4346. ;; First check if there are no even levels
  4347. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4348. (org-show-context t)
  4349. (error "Not all levels are odd in this file. Conversion not possible."))
  4350. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4351. (let ((org-odd-levels-only nil) n)
  4352. (save-excursion
  4353. (goto-char (point-min))
  4354. (while (re-search-forward "^\\*\\*+ " nil t)
  4355. (setq n (/ (1- (length (match-string 0))) 2))
  4356. (while (>= (setq n (1- n)) 0)
  4357. (org-promote))
  4358. (end-of-line 1))))))
  4359. (defun org-tr-level (n)
  4360. "Make N odd if required."
  4361. (if org-odd-levels-only (1+ (/ n 2)) n))
  4362. ;;; Vertical tree motion, cutting and pasting of subtrees
  4363. (defun org-move-subtree-up (&optional arg)
  4364. "Move the current subtree up past ARG headlines of the same level."
  4365. (interactive "p")
  4366. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4367. (defun org-move-subtree-down (&optional arg)
  4368. "Move the current subtree down past ARG headlines of the same level."
  4369. (interactive "p")
  4370. (setq arg (prefix-numeric-value arg))
  4371. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4372. 'outline-get-last-sibling))
  4373. (ins-point (make-marker))
  4374. (cnt (abs arg))
  4375. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4376. ;; Select the tree
  4377. (org-back-to-heading)
  4378. (setq beg0 (point))
  4379. (save-excursion
  4380. (setq ne-beg (org-back-over-empty-lines))
  4381. (setq beg (point)))
  4382. (save-match-data
  4383. (save-excursion (outline-end-of-heading)
  4384. (setq folded (org-invisible-p)))
  4385. (outline-end-of-subtree))
  4386. (outline-next-heading)
  4387. (setq ne-end (org-back-over-empty-lines))
  4388. (setq end (point))
  4389. (goto-char beg0)
  4390. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4391. ;; include less whitespace
  4392. (save-excursion
  4393. (goto-char beg)
  4394. (forward-line (- ne-beg ne-end))
  4395. (setq beg (point))))
  4396. ;; Find insertion point, with error handling
  4397. (while (> cnt 0)
  4398. (or (and (funcall movfunc) (looking-at outline-regexp))
  4399. (progn (goto-char beg0)
  4400. (error "Cannot move past superior level or buffer limit")))
  4401. (setq cnt (1- cnt)))
  4402. (if (> arg 0)
  4403. ;; Moving forward - still need to move over subtree
  4404. (progn (org-end-of-subtree t t)
  4405. (save-excursion
  4406. (org-back-over-empty-lines)
  4407. (or (bolp) (newline)))))
  4408. (setq ne-ins (org-back-over-empty-lines))
  4409. (move-marker ins-point (point))
  4410. (setq txt (buffer-substring beg end))
  4411. (org-save-markers-in-region beg end)
  4412. (delete-region beg end)
  4413. (outline-flag-region (1- beg) beg nil)
  4414. (outline-flag-region (1- (point)) (point) nil)
  4415. (let ((bbb (point)))
  4416. (insert-before-markers txt)
  4417. (org-reinstall-markers-in-region bbb)
  4418. (move-marker ins-point bbb))
  4419. (or (bolp) (insert "\n"))
  4420. (setq ins-end (point))
  4421. (goto-char ins-point)
  4422. (org-skip-whitespace)
  4423. (when (and (< arg 0)
  4424. (org-first-sibling-p)
  4425. (> ne-ins ne-beg))
  4426. ;; Move whitespace back to beginning
  4427. (save-excursion
  4428. (goto-char ins-end)
  4429. (let ((kill-whole-line t))
  4430. (kill-line (- ne-ins ne-beg)) (point)))
  4431. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4432. (move-marker ins-point nil)
  4433. (org-compact-display-after-subtree-move)
  4434. (org-show-empty-lines-in-parent)
  4435. (unless folded
  4436. (org-show-entry)
  4437. (show-children)
  4438. (org-cycle-hide-drawers 'children))))
  4439. (defvar org-subtree-clip ""
  4440. "Clipboard for cut and paste of subtrees.
  4441. This is actually only a copy of the kill, because we use the normal kill
  4442. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4443. (defvar org-subtree-clip-folded nil
  4444. "Was the last copied subtree folded?
  4445. This is used to fold the tree back after pasting.")
  4446. (defun org-cut-subtree (&optional n)
  4447. "Cut the current subtree into the clipboard.
  4448. With prefix arg N, cut this many sequential subtrees.
  4449. This is a short-hand for marking the subtree and then cutting it."
  4450. (interactive "p")
  4451. (org-copy-subtree n 'cut))
  4452. (defun org-copy-subtree (&optional n cut force-store-markers)
  4453. "Cut the current subtree into the clipboard.
  4454. With prefix arg N, cut this many sequential subtrees.
  4455. This is a short-hand for marking the subtree and then copying it.
  4456. If CUT is non-nil, actually cut the subtree.
  4457. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4458. of some markers in the region, even if CUT is non-nil. This is
  4459. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4460. (interactive "p")
  4461. (let (beg end folded (beg0 (point)))
  4462. (if (interactive-p)
  4463. (org-back-to-heading nil) ; take what looks like a subtree
  4464. (org-back-to-heading t)) ; take what is really there
  4465. (org-back-over-empty-lines)
  4466. (setq beg (point))
  4467. (skip-chars-forward " \t\r\n")
  4468. (save-match-data
  4469. (save-excursion (outline-end-of-heading)
  4470. (setq folded (org-invisible-p)))
  4471. (condition-case nil
  4472. (outline-forward-same-level (1- n))
  4473. (error nil))
  4474. (org-end-of-subtree t t))
  4475. (org-back-over-empty-lines)
  4476. (setq end (point))
  4477. (goto-char beg0)
  4478. (when (> end beg)
  4479. (setq org-subtree-clip-folded folded)
  4480. (when (or cut force-store-markers)
  4481. (org-save-markers-in-region beg end))
  4482. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4483. (setq org-subtree-clip (current-kill 0))
  4484. (message "%s: Subtree(s) with %d characters"
  4485. (if cut "Cut" "Copied")
  4486. (length org-subtree-clip)))))
  4487. (defun org-paste-subtree (&optional level tree)
  4488. "Paste the clipboard as a subtree, with modification of headline level.
  4489. The entire subtree is promoted or demoted in order to match a new headline
  4490. level. By default, the new level is derived from the visible headings
  4491. before and after the insertion point, and taken to be the inferior headline
  4492. level of the two. So if the previous visible heading is level 3 and the
  4493. next is level 4 (or vice versa), level 4 will be used for insertion.
  4494. This makes sure that the subtree remains an independent subtree and does
  4495. not swallow low level entries.
  4496. You can also force a different level, either by using a numeric prefix
  4497. argument, or by inserting the heading marker by hand. For example, if the
  4498. cursor is after \"*****\", then the tree will be shifted to level 5.
  4499. If you want to insert the tree as is, just use \\[yank].
  4500. If optional TREE is given, use this text instead of the kill ring."
  4501. (interactive "P")
  4502. (unless (org-kill-is-subtree-p tree)
  4503. (error "%s"
  4504. (substitute-command-keys
  4505. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4506. (let* ((visp (not (org-invisible-p)))
  4507. (txt (or tree (and kill-ring (current-kill 0))))
  4508. (^re (concat "^\\(" outline-regexp "\\)"))
  4509. (re (concat "\\(" outline-regexp "\\)"))
  4510. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4511. (old-level (if (string-match ^re txt)
  4512. (- (match-end 0) (match-beginning 0) 1)
  4513. -1))
  4514. (force-level (cond (level (prefix-numeric-value level))
  4515. ((string-match
  4516. ^re_ (buffer-substring (point-at-bol) (point)))
  4517. (- (match-end 1) (match-beginning 1)))
  4518. (t nil)))
  4519. (previous-level (save-excursion
  4520. (condition-case nil
  4521. (progn
  4522. (outline-previous-visible-heading 1)
  4523. (if (looking-at re)
  4524. (- (match-end 0) (match-beginning 0) 1)
  4525. 1))
  4526. (error 1))))
  4527. (next-level (save-excursion
  4528. (condition-case nil
  4529. (progn
  4530. (or (looking-at outline-regexp)
  4531. (outline-next-visible-heading 1))
  4532. (if (looking-at re)
  4533. (- (match-end 0) (match-beginning 0) 1)
  4534. 1))
  4535. (error 1))))
  4536. (new-level (or force-level (max previous-level next-level)))
  4537. (shift (if (or (= old-level -1)
  4538. (= new-level -1)
  4539. (= old-level new-level))
  4540. 0
  4541. (- new-level old-level)))
  4542. (delta (if (> shift 0) -1 1))
  4543. (func (if (> shift 0) 'org-demote 'org-promote))
  4544. (org-odd-levels-only nil)
  4545. beg end)
  4546. ;; Remove the forced level indicator
  4547. (if force-level
  4548. (delete-region (point-at-bol) (point)))
  4549. ;; Paste
  4550. (beginning-of-line 1)
  4551. (org-back-over-empty-lines)
  4552. (setq beg (point))
  4553. (insert-before-markers txt)
  4554. (unless (string-match "\n\\'" txt) (insert "\n"))
  4555. (org-reinstall-markers-in-region beg)
  4556. (setq end (point))
  4557. (goto-char beg)
  4558. (skip-chars-forward " \t\n\r")
  4559. (setq beg (point))
  4560. (if (and (org-invisible-p) visp)
  4561. (save-excursion (outline-show-heading)))
  4562. ;; Shift if necessary
  4563. (unless (= shift 0)
  4564. (save-restriction
  4565. (narrow-to-region beg end)
  4566. (while (not (= shift 0))
  4567. (org-map-region func (point-min) (point-max))
  4568. (setq shift (+ delta shift)))
  4569. (goto-char (point-min))))
  4570. (when (interactive-p)
  4571. (message "Clipboard pasted as level %d subtree" new-level))
  4572. (if (and kill-ring
  4573. (eq org-subtree-clip (current-kill 0))
  4574. org-subtree-clip-folded)
  4575. ;; The tree was folded before it was killed/copied
  4576. (hide-subtree))))
  4577. (defun org-kill-is-subtree-p (&optional txt)
  4578. "Check if the current kill is an outline subtree, or a set of trees.
  4579. Returns nil if kill does not start with a headline, or if the first
  4580. headline level is not the largest headline level in the tree.
  4581. So this will actually accept several entries of equal levels as well,
  4582. which is OK for `org-paste-subtree'.
  4583. If optional TXT is given, check this string instead of the current kill."
  4584. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4585. (start-level (and kill
  4586. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4587. org-outline-regexp "\\)")
  4588. kill)
  4589. (- (match-end 2) (match-beginning 2) 1)))
  4590. (re (concat "^" org-outline-regexp))
  4591. (start (1+ (or (match-beginning 2) -1))))
  4592. (if (not start-level)
  4593. (progn
  4594. nil) ;; does not even start with a heading
  4595. (catch 'exit
  4596. (while (setq start (string-match re kill (1+ start)))
  4597. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4598. (throw 'exit nil)))
  4599. t))))
  4600. (defvar org-markers-to-move nil
  4601. "Markers that should be moved with a cut-and-paste operation.
  4602. Those markers are stored together with their positions relative to
  4603. the start of the region.")
  4604. (defun org-save-markers-in-region (beg end)
  4605. "Check markers in region.
  4606. If these markers are between BEG and END, record their position relative
  4607. to BEG, so that after moving the block of text, we can put the markers back
  4608. into place.
  4609. This function gets called just before an entry or tree gets cut from the
  4610. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4611. called immediately, to move the markers with the entries."
  4612. (setq org-markers-to-move nil)
  4613. (when (featurep 'org-clock)
  4614. (org-clock-save-markers-for-cut-and-paste beg end))
  4615. (when (featurep 'org-agenda)
  4616. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4617. (defun org-check-and-save-marker (marker beg end)
  4618. "Check if MARKER is between BEG and END.
  4619. If yes, remember the marker and the distance to BEG."
  4620. (when (and (marker-buffer marker)
  4621. (equal (marker-buffer marker) (current-buffer)))
  4622. (if (and (>= marker beg) (< marker end))
  4623. (push (cons marker (- marker beg)) org-markers-to-move))))
  4624. (defun org-reinstall-markers-in-region (beg)
  4625. "Move all remembered markers to their position relative to BEG."
  4626. (mapc (lambda (x)
  4627. (move-marker (car x) (+ beg (cdr x))))
  4628. org-markers-to-move)
  4629. (setq org-markers-to-move nil))
  4630. (defun org-narrow-to-subtree ()
  4631. "Narrow buffer to the current subtree."
  4632. (interactive)
  4633. (save-excursion
  4634. (save-match-data
  4635. (narrow-to-region
  4636. (progn (org-back-to-heading) (point))
  4637. (progn (org-end-of-subtree t) (point))))))
  4638. ;;; Outline Sorting
  4639. (defun org-sort (with-case)
  4640. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4641. Optional argument WITH-CASE means sort case-sensitively."
  4642. (interactive "P")
  4643. (if (org-at-table-p)
  4644. (org-call-with-arg 'org-table-sort-lines with-case)
  4645. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4646. (defun org-sort-remove-invisible (s)
  4647. (remove-text-properties 0 (length s) org-rm-props s)
  4648. (while (string-match org-bracket-link-regexp s)
  4649. (setq s (replace-match (if (match-end 2)
  4650. (match-string 3 s)
  4651. (match-string 1 s)) t t s)))
  4652. s)
  4653. (defvar org-priority-regexp) ; defined later in the file
  4654. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4655. "Sort entries on a certain level of an outline tree.
  4656. If there is an active region, the entries in the region are sorted.
  4657. Else, if the cursor is before the first entry, sort the top-level items.
  4658. Else, the children of the entry at point are sorted.
  4659. Sorting can be alphabetically, numerically, and by date/time as given by
  4660. the first time stamp in the entry. The command prompts for the sorting
  4661. type unless it has been given to the function through the SORTING-TYPE
  4662. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4663. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4664. called with point at the beginning of the record. It must return either
  4665. a string or a number that should serve as the sorting key for that record.
  4666. Comparing entries ignores case by default. However, with an optional argument
  4667. WITH-CASE, the sorting considers case as well."
  4668. (interactive "P")
  4669. (let ((case-func (if with-case 'identity 'downcase))
  4670. start beg end stars re re2
  4671. txt what tmp plain-list-p)
  4672. ;; Find beginning and end of region to sort
  4673. (cond
  4674. ((org-region-active-p)
  4675. ;; we will sort the region
  4676. (setq end (region-end)
  4677. what "region")
  4678. (goto-char (region-beginning))
  4679. (if (not (org-on-heading-p)) (outline-next-heading))
  4680. (setq start (point)))
  4681. ((org-at-item-p)
  4682. ;; we will sort this plain list
  4683. (org-beginning-of-item-list) (setq start (point))
  4684. (org-end-of-item-list) (setq end (point))
  4685. (goto-char start)
  4686. (setq plain-list-p t
  4687. what "plain list"))
  4688. ((or (org-on-heading-p)
  4689. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4690. ;; we will sort the children of the current headline
  4691. (org-back-to-heading)
  4692. (setq start (point)
  4693. end (progn (org-end-of-subtree t t)
  4694. (org-back-over-empty-lines)
  4695. (point))
  4696. what "children")
  4697. (goto-char start)
  4698. (show-subtree)
  4699. (outline-next-heading))
  4700. (t
  4701. ;; we will sort the top-level entries in this file
  4702. (goto-char (point-min))
  4703. (or (org-on-heading-p) (outline-next-heading))
  4704. (setq start (point) end (point-max) what "top-level")
  4705. (goto-char start)
  4706. (show-all)))
  4707. (setq beg (point))
  4708. (if (>= beg end) (error "Nothing to sort"))
  4709. (unless plain-list-p
  4710. (looking-at "\\(\\*+\\)")
  4711. (setq stars (match-string 1)
  4712. re (concat "^" (regexp-quote stars) " +")
  4713. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4714. txt (buffer-substring beg end))
  4715. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4716. (if (and (not (equal stars "*")) (string-match re2 txt))
  4717. (error "Region to sort contains a level above the first entry")))
  4718. (unless sorting-type
  4719. (message
  4720. (if plain-list-p
  4721. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4722. "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:")
  4723. what)
  4724. (setq sorting-type (read-char-exclusive))
  4725. (and (= (downcase sorting-type) ?f)
  4726. (setq getkey-func
  4727. (completing-read "Sort using function: "
  4728. obarray 'fboundp t nil nil))
  4729. (setq getkey-func (intern getkey-func)))
  4730. (and (= (downcase sorting-type) ?r)
  4731. (setq property
  4732. (completing-read "Property: "
  4733. (mapcar 'list (org-buffer-property-keys t))
  4734. nil t))))
  4735. (message "Sorting entries...")
  4736. (save-restriction
  4737. (narrow-to-region start end)
  4738. (let ((dcst (downcase sorting-type))
  4739. (now (current-time)))
  4740. (sort-subr
  4741. (/= dcst sorting-type)
  4742. ;; This function moves to the beginning character of the "record" to
  4743. ;; be sorted.
  4744. (if plain-list-p
  4745. (lambda nil
  4746. (if (org-at-item-p) t (goto-char (point-max))))
  4747. (lambda nil
  4748. (if (re-search-forward re nil t)
  4749. (goto-char (match-beginning 0))
  4750. (goto-char (point-max)))))
  4751. ;; This function moves to the last character of the "record" being
  4752. ;; sorted.
  4753. (if plain-list-p
  4754. 'org-end-of-item
  4755. (lambda nil
  4756. (save-match-data
  4757. (condition-case nil
  4758. (outline-forward-same-level 1)
  4759. (error
  4760. (goto-char (point-max)))))))
  4761. ;; This function returns the value that gets sorted against.
  4762. (if plain-list-p
  4763. (lambda nil
  4764. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4765. (cond
  4766. ((= dcst ?n)
  4767. (string-to-number (buffer-substring (match-end 0)
  4768. (point-at-eol))))
  4769. ((= dcst ?a)
  4770. (buffer-substring (match-end 0) (point-at-eol)))
  4771. ((= dcst ?t)
  4772. (if (re-search-forward org-ts-regexp
  4773. (point-at-eol) t)
  4774. (org-time-string-to-time (match-string 0))
  4775. now))
  4776. ((= dcst ?f)
  4777. (if getkey-func
  4778. (progn
  4779. (setq tmp (funcall getkey-func))
  4780. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4781. tmp)
  4782. (error "Invalid key function `%s'" getkey-func)))
  4783. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4784. (lambda nil
  4785. (cond
  4786. ((= dcst ?n)
  4787. (if (looking-at org-complex-heading-regexp)
  4788. (string-to-number (match-string 4))
  4789. nil))
  4790. ((= dcst ?a)
  4791. (if (looking-at org-complex-heading-regexp)
  4792. (funcall case-func (match-string 4))
  4793. nil))
  4794. ((= dcst ?t)
  4795. (if (re-search-forward org-ts-regexp
  4796. (save-excursion
  4797. (forward-line 2)
  4798. (point)) t)
  4799. (org-time-string-to-time (match-string 0))
  4800. now))
  4801. ((= dcst ?p)
  4802. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4803. (string-to-char (match-string 2))
  4804. org-default-priority))
  4805. ((= dcst ?r)
  4806. (or (org-entry-get nil property) ""))
  4807. ((= dcst ?o)
  4808. (if (looking-at org-complex-heading-regexp)
  4809. (- 9999 (length (member (match-string 2)
  4810. org-todo-keywords-1)))))
  4811. ((= dcst ?f)
  4812. (if getkey-func
  4813. (progn
  4814. (setq tmp (funcall getkey-func))
  4815. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4816. tmp)
  4817. (error "Invalid key function `%s'" getkey-func)))
  4818. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4819. nil
  4820. (cond
  4821. ((= dcst ?a) 'string<)
  4822. ((= dcst ?t) 'time-less-p)
  4823. (t nil)))))
  4824. (message "Sorting entries...done")))
  4825. (defun org-do-sort (table what &optional with-case sorting-type)
  4826. "Sort TABLE of WHAT according to SORTING-TYPE.
  4827. The user will be prompted for the SORTING-TYPE if the call to this
  4828. function does not specify it. WHAT is only for the prompt, to indicate
  4829. what is being sorted. The sorting key will be extracted from
  4830. the car of the elements of the table.
  4831. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4832. (unless sorting-type
  4833. (message
  4834. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4835. what)
  4836. (setq sorting-type (read-char-exclusive)))
  4837. (let ((dcst (downcase sorting-type))
  4838. extractfun comparefun)
  4839. ;; Define the appropriate functions
  4840. (cond
  4841. ((= dcst ?n)
  4842. (setq extractfun 'string-to-number
  4843. comparefun (if (= dcst sorting-type) '< '>)))
  4844. ((= dcst ?a)
  4845. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4846. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4847. comparefun (if (= dcst sorting-type)
  4848. 'string<
  4849. (lambda (a b) (and (not (string< a b))
  4850. (not (string= a b)))))))
  4851. ((= dcst ?t)
  4852. (setq extractfun
  4853. (lambda (x)
  4854. (if (string-match org-ts-regexp x)
  4855. (time-to-seconds
  4856. (org-time-string-to-time (match-string 0 x)))
  4857. 0))
  4858. comparefun (if (= dcst sorting-type) '< '>)))
  4859. (t (error "Invalid sorting type `%c'" sorting-type)))
  4860. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4861. table)
  4862. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4863. ;;; Editing source examples
  4864. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4865. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4866. (defvar org-edit-src-force-single-line nil)
  4867. (defvar org-edit-src-from-org-mode nil)
  4868. (defvar org-edit-src-picture nil)
  4869. (define-minor-mode org-exit-edit-mode
  4870. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4871. (defun org-edit-src-code ()
  4872. "Edit the source code example at point.
  4873. An indirect buffer is created, and that buffer is then narrowed to the
  4874. example at point and switched to the correct language mode. When done,
  4875. exit by killing the buffer with \\[org-edit-src-exit]."
  4876. (interactive)
  4877. (let ((line (org-current-line))
  4878. (case-fold-search t)
  4879. (msg (substitute-command-keys
  4880. "Edit, then exit with C-c ' (C-c and single quote)"))
  4881. (info (org-edit-src-find-region-and-lang))
  4882. (org-mode-p (eq major-mode 'org-mode))
  4883. beg end lang lang-f single)
  4884. (if (not info)
  4885. nil
  4886. (setq beg (nth 0 info)
  4887. end (nth 1 info)
  4888. lang (nth 2 info)
  4889. single (nth 3 info)
  4890. lang-f (intern (concat lang "-mode")))
  4891. (unless (functionp lang-f)
  4892. (error "No such language mode: %s" lang-f))
  4893. (goto-line line)
  4894. (if (get-buffer "*Org Edit Src Example*")
  4895. (kill-buffer "*Org Edit Src Example*"))
  4896. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4897. "*Org Edit Src Example*"))
  4898. (narrow-to-region beg end)
  4899. (remove-text-properties beg end '(display nil invisible nil
  4900. intangible nil))
  4901. (let ((org-inhibit-startup t))
  4902. (funcall lang-f))
  4903. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4904. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4905. (when org-mode-p
  4906. (goto-char (point-min))
  4907. (while (re-search-forward "^," nil t)
  4908. (replace-match "")))
  4909. (goto-line line)
  4910. (org-exit-edit-mode)
  4911. (org-set-local 'header-line-format msg)
  4912. (message "%s" msg)
  4913. t)))
  4914. (defun org-edit-fixed-width-region ()
  4915. "Edit the fixed-width ascii drawing at point.
  4916. This must be a region where each line starts with ca colon followed by
  4917. a space character.
  4918. An indirect buffer is created, and that buffer is then narrowed to the
  4919. example at point and switched to artist-mode. When done,
  4920. exit by killing the buffer with \\[org-edit-src-exit]."
  4921. (interactive)
  4922. (let ((line (org-current-line))
  4923. (case-fold-search t)
  4924. (msg (substitute-command-keys
  4925. "Edit, then exit with C-c ' (C-c and single quote)"))
  4926. (org-mode-p (eq major-mode 'org-mode))
  4927. beg end lang lang-f)
  4928. (beginning-of-line 1)
  4929. (if (looking-at "[ \t]*[^:\n \t]")
  4930. nil
  4931. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4932. (setq beg (point) end (match-end 0))
  4933. (save-excursion
  4934. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4935. (setq beg (point-at-bol 2))
  4936. (setq beg (point))))
  4937. (save-excursion
  4938. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4939. (setq end (match-beginning 0))
  4940. (setq end (point))))
  4941. (goto-line line)
  4942. (if (get-buffer "*Org Edit Picture*")
  4943. (kill-buffer "*Org Edit Picture*"))
  4944. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4945. "*Org Edit Picture*"))
  4946. (narrow-to-region beg end)
  4947. (remove-text-properties beg end '(display nil invisible nil
  4948. intangible nil))
  4949. (when (fboundp 'font-lock-unfontify-region)
  4950. (font-lock-unfontify-region (point-min) (point-max)))
  4951. (cond
  4952. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4953. (fundamental-mode)
  4954. (artist-mode 1))
  4955. (t (funcall org-edit-fixed-width-region-mode)))
  4956. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4957. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4958. (set (make-local-variable 'org-edit-src-picture) t)
  4959. (goto-char (point-min))
  4960. (while (re-search-forward "^[ \t]*: " nil t)
  4961. (replace-match ""))
  4962. (goto-line line)
  4963. (org-exit-edit-mode)
  4964. (org-set-local 'header-line-format msg)
  4965. (message "%s" msg)
  4966. t))))
  4967. (defun org-edit-src-find-region-and-lang ()
  4968. "Find the region and language for a local edit.
  4969. Return a list with beginning and end of the region, a string representing
  4970. the language, a switch telling of the content should be in a single line."
  4971. (let ((re-list
  4972. (append
  4973. org-edit-src-region-extra
  4974. '(
  4975. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4976. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4977. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4978. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4979. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4980. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4981. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4982. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4983. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  4984. ("^#\\+html:" "\n" "html" single-line)
  4985. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4986. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4987. ("^#\\+latex:" "\n" "latex" single-line)
  4988. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4989. ("^#\\+ascii:" "\n" "ascii" single-line)
  4990. )))
  4991. (pos (point))
  4992. re re1 re2 single beg end lang)
  4993. (catch 'exit
  4994. (while (setq entry (pop re-list))
  4995. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  4996. single (nth 3 entry))
  4997. (save-excursion
  4998. (if (or (looking-at re1)
  4999. (re-search-backward re1 nil t))
  5000. (progn
  5001. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5002. (if (and (re-search-forward re2 nil t)
  5003. (>= (match-end 0) pos))
  5004. (throw 'exit (list beg (match-beginning 0) lang single))))
  5005. (if (or (looking-at re2)
  5006. (re-search-forward re2 nil t))
  5007. (progn
  5008. (setq end (match-beginning 0))
  5009. (if (and (re-search-backward re1 nil t)
  5010. (<= (match-beginning 0) pos))
  5011. (throw 'exit
  5012. (list (match-end 0) end
  5013. (org-edit-src-get-lang lang) single)))))))))))
  5014. (defun org-edit-src-get-lang (lang)
  5015. "Extract the src language."
  5016. (let ((m (match-string 0)))
  5017. (cond
  5018. ((stringp lang) lang)
  5019. ((integerp lang) (match-string lang))
  5020. ((and (eq lang 'lang)
  5021. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5022. (match-string 1 m))
  5023. ((and (eq lang 'style)
  5024. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5025. (match-string 1 m))
  5026. (t "fundamental"))))
  5027. (defun org-edit-src-exit ()
  5028. "Exit special edit and protect problematic lines."
  5029. (interactive)
  5030. (unless (buffer-base-buffer (current-buffer))
  5031. (error "This is not an indirect buffer, something is wrong..."))
  5032. (unless (> (point-min) 1)
  5033. (error "This buffer is not narrowed, something is wrong..."))
  5034. (goto-char (point-min))
  5035. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5036. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5037. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5038. (goto-char (point-min))
  5039. (while (re-search-forward "\n" nil t)
  5040. (replace-match " "))
  5041. (goto-char (point-min))
  5042. (if (looking-at "\\s-*") (replace-match " "))
  5043. (if (re-search-forward "\\s-+\\'" nil t)
  5044. (replace-match "")))
  5045. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5046. (goto-char (point-min))
  5047. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5048. (replace-match ",\\1"))
  5049. (when font-lock-mode
  5050. (font-lock-unfontify-region (point-min) (point-max)))
  5051. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5052. (when (org-bound-and-true-p org-edit-src-picture)
  5053. (goto-char (point-min))
  5054. (while (re-search-forward "^" nil t)
  5055. (replace-match ": "))
  5056. (when font-lock-mode
  5057. (font-lock-unfontify-region (point-min) (point-max)))
  5058. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5059. (kill-buffer (current-buffer))
  5060. (and (org-mode-p) (org-restart-font-lock)))
  5061. ;;; The orgstruct minor mode
  5062. ;; Define a minor mode which can be used in other modes in order to
  5063. ;; integrate the org-mode structure editing commands.
  5064. ;; This is really a hack, because the org-mode structure commands use
  5065. ;; keys which normally belong to the major mode. Here is how it
  5066. ;; works: The minor mode defines all the keys necessary to operate the
  5067. ;; structure commands, but wraps the commands into a function which
  5068. ;; tests if the cursor is currently at a headline or a plain list
  5069. ;; item. If that is the case, the structure command is used,
  5070. ;; temporarily setting many Org-mode variables like regular
  5071. ;; expressions for filling etc. However, when any of those keys is
  5072. ;; used at a different location, function uses `key-binding' to look
  5073. ;; up if the key has an associated command in another currently active
  5074. ;; keymap (minor modes, major mode, global), and executes that
  5075. ;; command. There might be problems if any of the keys is otherwise
  5076. ;; used as a prefix key.
  5077. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5078. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5079. ;; addresses this by checking explicitly for both bindings.
  5080. (defvar orgstruct-mode-map (make-sparse-keymap)
  5081. "Keymap for the minor `orgstruct-mode'.")
  5082. (defvar org-local-vars nil
  5083. "List of local variables, for use by `orgstruct-mode'")
  5084. ;;;###autoload
  5085. (define-minor-mode orgstruct-mode
  5086. "Toggle the minor more `orgstruct-mode'.
  5087. This mode is for using Org-mode structure commands in other modes.
  5088. The following key behave as if Org-mode was active, if the cursor
  5089. is on a headline, or on a plain list item (both in the definition
  5090. of Org-mode).
  5091. M-up Move entry/item up
  5092. M-down Move entry/item down
  5093. M-left Promote
  5094. M-right Demote
  5095. M-S-up Move entry/item up
  5096. M-S-down Move entry/item down
  5097. M-S-left Promote subtree
  5098. M-S-right Demote subtree
  5099. M-q Fill paragraph and items like in Org-mode
  5100. C-c ^ Sort entries
  5101. C-c - Cycle list bullet
  5102. TAB Cycle item visibility
  5103. M-RET Insert new heading/item
  5104. S-M-RET Insert new TODO heading / Chekbox item
  5105. C-c C-c Set tags / toggle checkbox"
  5106. nil " OrgStruct" nil
  5107. (org-load-modules-maybe)
  5108. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5109. ;;;###autoload
  5110. (defun turn-on-orgstruct ()
  5111. "Unconditionally turn on `orgstruct-mode'."
  5112. (orgstruct-mode 1))
  5113. ;;;###autoload
  5114. (defun turn-on-orgstruct++ ()
  5115. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5116. In addition to setting orgstruct-mode, this also exports all indentation and
  5117. autofilling variables from org-mode into the buffer. Note that turning
  5118. off orgstruct-mode will *not* remove these additional settings."
  5119. (orgstruct-mode 1)
  5120. (let (var val)
  5121. (mapc
  5122. (lambda (x)
  5123. (when (string-match
  5124. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5125. (symbol-name (car x)))
  5126. (setq var (car x) val (nth 1 x))
  5127. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5128. org-local-vars)))
  5129. (defun orgstruct-error ()
  5130. "Error when there is no default binding for a structure key."
  5131. (interactive)
  5132. (error "This key has no function outside structure elements"))
  5133. (defun orgstruct-setup ()
  5134. "Setup orgstruct keymaps."
  5135. (let ((nfunc 0)
  5136. (bindings
  5137. (list
  5138. '([(meta up)] org-metaup)
  5139. '([(meta down)] org-metadown)
  5140. '([(meta left)] org-metaleft)
  5141. '([(meta right)] org-metaright)
  5142. '([(meta shift up)] org-shiftmetaup)
  5143. '([(meta shift down)] org-shiftmetadown)
  5144. '([(meta shift left)] org-shiftmetaleft)
  5145. '([(meta shift right)] org-shiftmetaright)
  5146. '([(shift up)] org-shiftup)
  5147. '([(shift down)] org-shiftdown)
  5148. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5149. '("\M-q" fill-paragraph)
  5150. '("\C-c^" org-sort)
  5151. '("\C-c-" org-cycle-list-bullet)))
  5152. elt key fun cmd)
  5153. (while (setq elt (pop bindings))
  5154. (setq nfunc (1+ nfunc))
  5155. (setq key (org-key (car elt))
  5156. fun (nth 1 elt)
  5157. cmd (orgstruct-make-binding fun nfunc key))
  5158. (org-defkey orgstruct-mode-map key cmd))
  5159. ;; Special treatment needed for TAB and RET
  5160. (org-defkey orgstruct-mode-map [(tab)]
  5161. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5162. (org-defkey orgstruct-mode-map "\C-i"
  5163. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5164. (org-defkey orgstruct-mode-map "\M-\C-m"
  5165. (orgstruct-make-binding 'org-insert-heading 105
  5166. "\M-\C-m" [(meta return)]))
  5167. (org-defkey orgstruct-mode-map [(meta return)]
  5168. (orgstruct-make-binding 'org-insert-heading 106
  5169. [(meta return)] "\M-\C-m"))
  5170. (org-defkey orgstruct-mode-map [(shift meta return)]
  5171. (orgstruct-make-binding 'org-insert-todo-heading 107
  5172. [(meta return)] "\M-\C-m"))
  5173. (unless org-local-vars
  5174. (setq org-local-vars (org-get-local-variables)))
  5175. t))
  5176. (defun orgstruct-make-binding (fun n &rest keys)
  5177. "Create a function for binding in the structure minor mode.
  5178. FUN is the command to call inside a table. N is used to create a unique
  5179. command name. KEYS are keys that should be checked in for a command
  5180. to execute outside of tables."
  5181. (eval
  5182. (list 'defun
  5183. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5184. '(arg)
  5185. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5186. "Outside of structure, run the binding of `"
  5187. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5188. "'.")
  5189. '(interactive "p")
  5190. (list 'if
  5191. '(org-context-p 'headline 'item)
  5192. (list 'org-run-like-in-org-mode (list 'quote fun))
  5193. (list 'let '(orgstruct-mode)
  5194. (list 'call-interactively
  5195. (append '(or)
  5196. (mapcar (lambda (k)
  5197. (list 'key-binding k))
  5198. keys)
  5199. '('orgstruct-error))))))))
  5200. (defun org-context-p (&rest contexts)
  5201. "Check if local context is any of CONTEXTS.
  5202. Possible values in the list of contexts are `table', `headline', and `item'."
  5203. (let ((pos (point)))
  5204. (goto-char (point-at-bol))
  5205. (prog1 (or (and (memq 'table contexts)
  5206. (looking-at "[ \t]*|"))
  5207. (and (memq 'headline contexts)
  5208. ;;????????? (looking-at "\\*+"))
  5209. (looking-at outline-regexp))
  5210. (and (memq 'item contexts)
  5211. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5212. (goto-char pos))))
  5213. (defun org-get-local-variables ()
  5214. "Return a list of all local variables in an org-mode buffer."
  5215. (let (varlist)
  5216. (with-current-buffer (get-buffer-create "*Org tmp*")
  5217. (erase-buffer)
  5218. (org-mode)
  5219. (setq varlist (buffer-local-variables)))
  5220. (kill-buffer "*Org tmp*")
  5221. (delq nil
  5222. (mapcar
  5223. (lambda (x)
  5224. (setq x
  5225. (if (symbolp x)
  5226. (list x)
  5227. (list (car x) (list 'quote (cdr x)))))
  5228. (if (string-match
  5229. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5230. (symbol-name (car x)))
  5231. x nil))
  5232. varlist))))
  5233. ;;;###autoload
  5234. (defun org-run-like-in-org-mode (cmd)
  5235. (org-load-modules-maybe)
  5236. (unless org-local-vars
  5237. (setq org-local-vars (org-get-local-variables)))
  5238. (eval (list 'let org-local-vars
  5239. (list 'call-interactively (list 'quote cmd)))))
  5240. ;;;; Archiving
  5241. (defun org-get-category (&optional pos)
  5242. "Get the category applying to position POS."
  5243. (get-text-property (or pos (point)) 'org-category))
  5244. (defun org-refresh-category-properties ()
  5245. "Refresh category text properties in the buffer."
  5246. (let ((def-cat (cond
  5247. ((null org-category)
  5248. (if buffer-file-name
  5249. (file-name-sans-extension
  5250. (file-name-nondirectory buffer-file-name))
  5251. "???"))
  5252. ((symbolp org-category) (symbol-name org-category))
  5253. (t org-category)))
  5254. beg end cat pos optionp)
  5255. (org-unmodified
  5256. (save-excursion
  5257. (save-restriction
  5258. (widen)
  5259. (goto-char (point-min))
  5260. (put-text-property (point) (point-max) 'org-category def-cat)
  5261. (while (re-search-forward
  5262. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5263. (setq pos (match-end 0)
  5264. optionp (equal (char-after (match-beginning 0)) ?#)
  5265. cat (org-trim (match-string 2)))
  5266. (if optionp
  5267. (setq beg (point-at-bol) end (point-max))
  5268. (org-back-to-heading t)
  5269. (setq beg (point) end (org-end-of-subtree t t)))
  5270. (put-text-property beg end 'org-category cat)
  5271. (goto-char pos)))))))
  5272. ;;;; Link Stuff
  5273. ;;; Link abbreviations
  5274. (defun org-link-expand-abbrev (link)
  5275. "Apply replacements as defined in `org-link-abbrev-alist."
  5276. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5277. (let* ((key (match-string 1 link))
  5278. (as (or (assoc key org-link-abbrev-alist-local)
  5279. (assoc key org-link-abbrev-alist)))
  5280. (tag (and (match-end 2) (match-string 3 link)))
  5281. rpl)
  5282. (if (not as)
  5283. link
  5284. (setq rpl (cdr as))
  5285. (cond
  5286. ((symbolp rpl) (funcall rpl tag))
  5287. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5288. (t (concat rpl tag)))))
  5289. link))
  5290. ;;; Storing and inserting links
  5291. (defvar org-insert-link-history nil
  5292. "Minibuffer history for links inserted with `org-insert-link'.")
  5293. (defvar org-stored-links nil
  5294. "Contains the links stored with `org-store-link'.")
  5295. (defvar org-store-link-plist nil
  5296. "Plist with info about the most recently link created with `org-store-link'.")
  5297. (defvar org-link-protocols nil
  5298. "Link protocols added to Org-mode using `org-add-link-type'.")
  5299. (defvar org-store-link-functions nil
  5300. "List of functions that are called to create and store a link.
  5301. Each function will be called in turn until one returns a non-nil
  5302. value. Each function should check if it is responsible for creating
  5303. this link (for example by looking at the major mode).
  5304. If not, it must exit and return nil.
  5305. If yes, it should return a non-nil value after a calling
  5306. `org-store-link-props' with a list of properties and values.
  5307. Special properties are:
  5308. :type The link prefix. like \"http\". This must be given.
  5309. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5310. This is obligatory as well.
  5311. :description Optional default description for the second pair
  5312. of brackets in an Org-mode link. The user can still change
  5313. this when inserting this link into an Org-mode buffer.
  5314. In addition to these, any additional properties can be specified
  5315. and then used in remember templates.")
  5316. (defun org-add-link-type (type &optional follow export)
  5317. "Add TYPE to the list of `org-link-types'.
  5318. Re-compute all regular expressions depending on `org-link-types'
  5319. FOLLOW and EXPORT are two functions.
  5320. FOLLOW should take the link path as the single argument and do whatever
  5321. is necessary to follow the link, for example find a file or display
  5322. a mail message.
  5323. EXPORT should format the link path for export to one of the export formats.
  5324. It should be a function accepting three arguments:
  5325. path the path of the link, the text after the prefix (like \"http:\")
  5326. desc the description of the link, if any, nil if there was no descripton
  5327. format the export format, a symbol like `html' or `latex'.
  5328. The function may use the FORMAT information to return different values
  5329. depending on the format. The return value will be put literally into
  5330. the exported file.
  5331. Org-mode has a built-in default for exporting links. If you are happy with
  5332. this default, there is no need to define an export function for the link
  5333. type. For a simple example of an export function, see `org-bbdb.el'."
  5334. (add-to-list 'org-link-types type t)
  5335. (org-make-link-regexps)
  5336. (if (assoc type org-link-protocols)
  5337. (setcdr (assoc type org-link-protocols) (list follow export))
  5338. (push (list type follow export) org-link-protocols)))
  5339. ;;;###autoload
  5340. (defun org-store-link (arg)
  5341. "\\<org-mode-map>Store an org-link to the current location.
  5342. This link is added to `org-stored-links' and can later be inserted
  5343. into an org-buffer with \\[org-insert-link].
  5344. For some link types, a prefix arg is interpreted:
  5345. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5346. For file links, arg negates `org-context-in-file-links'."
  5347. (interactive "P")
  5348. (org-load-modules-maybe)
  5349. (setq org-store-link-plist nil) ; reset
  5350. (let (link cpltxt desc description search txt)
  5351. (cond
  5352. ((run-hook-with-args-until-success 'org-store-link-functions)
  5353. (setq link (plist-get org-store-link-plist :link)
  5354. desc (or (plist-get org-store-link-plist :description) link)))
  5355. ((eq major-mode 'calendar-mode)
  5356. (let ((cd (calendar-cursor-to-date)))
  5357. (setq link
  5358. (format-time-string
  5359. (car org-time-stamp-formats)
  5360. (apply 'encode-time
  5361. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5362. nil nil nil))))
  5363. (org-store-link-props :type "calendar" :date cd)))
  5364. ((eq major-mode 'w3-mode)
  5365. (setq cpltxt (url-view-url t)
  5366. link (org-make-link cpltxt))
  5367. (org-store-link-props :type "w3" :url (url-view-url t)))
  5368. ((eq major-mode 'w3m-mode)
  5369. (setq cpltxt (or w3m-current-title w3m-current-url)
  5370. link (org-make-link w3m-current-url))
  5371. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5372. ((setq search (run-hook-with-args-until-success
  5373. 'org-create-file-search-functions))
  5374. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5375. "::" search))
  5376. (setq cpltxt (or description link)))
  5377. ((eq major-mode 'image-mode)
  5378. (setq cpltxt (concat "file:"
  5379. (abbreviate-file-name buffer-file-name))
  5380. link (org-make-link cpltxt))
  5381. (org-store-link-props :type "image" :file buffer-file-name))
  5382. ((eq major-mode 'dired-mode)
  5383. ;; link to the file in the current line
  5384. (setq cpltxt (concat "file:"
  5385. (abbreviate-file-name
  5386. (expand-file-name
  5387. (dired-get-filename nil t))))
  5388. link (org-make-link cpltxt)))
  5389. ((and buffer-file-name (org-mode-p))
  5390. ;; Just link to current headline
  5391. (setq cpltxt (concat "file:"
  5392. (abbreviate-file-name buffer-file-name)))
  5393. ;; Add a context search string
  5394. (when (org-xor org-context-in-file-links arg)
  5395. ;; Check if we are on a target
  5396. (if (org-in-regexp "<<\\(.*?\\)>>")
  5397. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5398. (setq txt (cond
  5399. ((org-on-heading-p) nil)
  5400. ((org-region-active-p)
  5401. (buffer-substring (region-beginning) (region-end)))
  5402. (t nil)))
  5403. (when (or (null txt) (string-match "\\S-" txt))
  5404. (setq cpltxt
  5405. (concat cpltxt "::"
  5406. (condition-case nil
  5407. (org-make-org-heading-search-string txt)
  5408. (error "")))
  5409. desc "NONE"))))
  5410. (if (string-match "::\\'" cpltxt)
  5411. (setq cpltxt (substring cpltxt 0 -2)))
  5412. (setq link (org-make-link cpltxt)))
  5413. ((buffer-file-name (buffer-base-buffer))
  5414. ;; Just link to this file here.
  5415. (setq cpltxt (concat "file:"
  5416. (abbreviate-file-name
  5417. (buffer-file-name (buffer-base-buffer)))))
  5418. ;; Add a context string
  5419. (when (org-xor org-context-in-file-links arg)
  5420. (setq txt (if (org-region-active-p)
  5421. (buffer-substring (region-beginning) (region-end))
  5422. (buffer-substring (point-at-bol) (point-at-eol))))
  5423. ;; Only use search option if there is some text.
  5424. (when (string-match "\\S-" txt)
  5425. (setq cpltxt
  5426. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5427. desc "NONE")))
  5428. (setq link (org-make-link cpltxt)))
  5429. ((interactive-p)
  5430. (error "Cannot link to a buffer which is not visiting a file"))
  5431. (t (setq link nil)))
  5432. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5433. (setq link (or link cpltxt)
  5434. desc (or desc cpltxt))
  5435. (if (equal desc "NONE") (setq desc nil))
  5436. (if (and (interactive-p) link)
  5437. (progn
  5438. (setq org-stored-links
  5439. (cons (list link desc) org-stored-links))
  5440. (message "Stored: %s" (or desc link)))
  5441. (and link (org-make-link-string link desc)))))
  5442. (defun org-store-link-props (&rest plist)
  5443. "Store link properties, extract names and addresses."
  5444. (let (x adr)
  5445. (when (setq x (plist-get plist :from))
  5446. (setq adr (mail-extract-address-components x))
  5447. (plist-put plist :fromname (car adr))
  5448. (plist-put plist :fromaddress (nth 1 adr)))
  5449. (when (setq x (plist-get plist :to))
  5450. (setq adr (mail-extract-address-components x))
  5451. (plist-put plist :toname (car adr))
  5452. (plist-put plist :toaddress (nth 1 adr))))
  5453. (let ((from (plist-get plist :from))
  5454. (to (plist-get plist :to)))
  5455. (when (and from to org-from-is-user-regexp)
  5456. (plist-put plist :fromto
  5457. (if (string-match org-from-is-user-regexp from)
  5458. (concat "to %t")
  5459. (concat "from %f")))))
  5460. (setq org-store-link-plist plist))
  5461. (defun org-add-link-props (&rest plist)
  5462. "Add these properties to the link property list."
  5463. (let (key value)
  5464. (while plist
  5465. (setq key (pop plist) value (pop plist))
  5466. (setq org-store-link-plist
  5467. (plist-put org-store-link-plist key value)))))
  5468. (defun org-email-link-description (&optional fmt)
  5469. "Return the description part of an email link.
  5470. This takes information from `org-store-link-plist' and formats it
  5471. according to FMT (default from `org-email-link-description-format')."
  5472. (setq fmt (or fmt org-email-link-description-format))
  5473. (let* ((p org-store-link-plist)
  5474. (to (plist-get p :toaddress))
  5475. (from (plist-get p :fromaddress))
  5476. (table
  5477. (list
  5478. (cons "%c" (plist-get p :fromto))
  5479. (cons "%F" (plist-get p :from))
  5480. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5481. (cons "%T" (plist-get p :to))
  5482. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5483. (cons "%s" (plist-get p :subject))
  5484. (cons "%m" (plist-get p :message-id)))))
  5485. (when (string-match "%c" fmt)
  5486. ;; Check if the user wrote this message
  5487. (if (and org-from-is-user-regexp from to
  5488. (save-match-data (string-match org-from-is-user-regexp from)))
  5489. (setq fmt (replace-match "to %t" t t fmt))
  5490. (setq fmt (replace-match "from %f" t t fmt))))
  5491. (org-replace-escapes fmt table)))
  5492. (defun org-make-org-heading-search-string (&optional string heading)
  5493. "Make search string for STRING or current headline."
  5494. (interactive)
  5495. (let ((s (or string (org-get-heading))))
  5496. (unless (and string (not heading))
  5497. ;; We are using a headline, clean up garbage in there.
  5498. (if (string-match org-todo-regexp s)
  5499. (setq s (replace-match "" t t s)))
  5500. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5501. (setq s (replace-match "" t t s)))
  5502. (setq s (org-trim s))
  5503. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5504. org-comment-string "\\)") s)
  5505. (setq s (replace-match "" t t s)))
  5506. (while (string-match org-ts-regexp s)
  5507. (setq s (replace-match "" t t s))))
  5508. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5509. (setq s (replace-match " " t t s)))
  5510. (or string (setq s (concat "*" s))) ; Add * for headlines
  5511. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5512. (defun org-make-link (&rest strings)
  5513. "Concatenate STRINGS."
  5514. (apply 'concat strings))
  5515. (defun org-make-link-string (link &optional description)
  5516. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5517. (unless (string-match "\\S-" link)
  5518. (error "Empty link"))
  5519. (when (stringp description)
  5520. ;; Remove brackets from the description, they are fatal.
  5521. (while (string-match "\\[" description)
  5522. (setq description (replace-match "{" t t description)))
  5523. (while (string-match "\\]" description)
  5524. (setq description (replace-match "}" t t description))))
  5525. (when (equal (org-link-escape link) description)
  5526. ;; No description needed, it is identical
  5527. (setq description nil))
  5528. (when (and (not description)
  5529. (not (equal link (org-link-escape link))))
  5530. (setq description (org-extract-attributes link)))
  5531. (concat "[[" (org-link-escape link) "]"
  5532. (if description (concat "[" description "]") "")
  5533. "]"))
  5534. (defconst org-link-escape-chars
  5535. '((?\ . "%20")
  5536. (?\[ . "%5B")
  5537. (?\] . "%5D")
  5538. (?\340 . "%E0") ; `a
  5539. (?\342 . "%E2") ; ^a
  5540. (?\347 . "%E7") ; ,c
  5541. (?\350 . "%E8") ; `e
  5542. (?\351 . "%E9") ; 'e
  5543. (?\352 . "%EA") ; ^e
  5544. (?\356 . "%EE") ; ^i
  5545. (?\364 . "%F4") ; ^o
  5546. (?\371 . "%F9") ; `u
  5547. (?\373 . "%FB") ; ^u
  5548. (?\; . "%3B")
  5549. (?? . "%3F")
  5550. (?= . "%3D")
  5551. (?+ . "%2B")
  5552. )
  5553. "Association list of escapes for some characters problematic in links.
  5554. This is the list that is used for internal purposes.")
  5555. (defconst org-link-escape-chars-browser
  5556. '((?\ . "%20")) ; 32 for the SPC char
  5557. "Association list of escapes for some characters problematic in links.
  5558. This is the list that is used before handing over to the browser.")
  5559. (defun org-link-escape (text &optional table)
  5560. "Escape charaters in TEXT that are problematic for links."
  5561. (setq table (or table org-link-escape-chars))
  5562. (when text
  5563. (let ((re (mapconcat (lambda (x) (regexp-quote
  5564. (char-to-string (car x))))
  5565. table "\\|")))
  5566. (while (string-match re text)
  5567. (setq text
  5568. (replace-match
  5569. (cdr (assoc (string-to-char (match-string 0 text))
  5570. table))
  5571. t t text)))
  5572. text)))
  5573. (defun org-link-unescape (text &optional table)
  5574. "Reverse the action of `org-link-escape'."
  5575. (setq table (or table org-link-escape-chars))
  5576. (when text
  5577. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5578. table "\\|")))
  5579. (while (string-match re text)
  5580. (setq text
  5581. (replace-match
  5582. (char-to-string (car (rassoc (match-string 0 text) table)))
  5583. t t text)))
  5584. text)))
  5585. (defun org-xor (a b)
  5586. "Exclusive or."
  5587. (if a (not b) b))
  5588. (defun org-get-header (header)
  5589. "Find a header field in the current buffer."
  5590. (save-excursion
  5591. (goto-char (point-min))
  5592. (let ((case-fold-search t) s)
  5593. (cond
  5594. ((eq header 'from)
  5595. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5596. (setq s (match-string 1)))
  5597. (while (string-match "\"" s)
  5598. (setq s (replace-match "" t t s)))
  5599. (if (string-match "[<(].*" s)
  5600. (setq s (replace-match "" t t s))))
  5601. ((eq header 'message-id)
  5602. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5603. (setq s (match-string 1))))
  5604. ((eq header 'subject)
  5605. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5606. (setq s (match-string 1)))))
  5607. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5608. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5609. s)))
  5610. (defun org-fixup-message-id-for-http (s)
  5611. "Replace special characters in a message id, so it can be used in an http query."
  5612. (while (string-match "<" s)
  5613. (setq s (replace-match "%3C" t t s)))
  5614. (while (string-match ">" s)
  5615. (setq s (replace-match "%3E" t t s)))
  5616. (while (string-match "@" s)
  5617. (setq s (replace-match "%40" t t s)))
  5618. s)
  5619. ;;;###autoload
  5620. (defun org-insert-link-global ()
  5621. "Insert a link like Org-mode does.
  5622. This command can be called in any mode to insert a link in Org-mode syntax."
  5623. (interactive)
  5624. (org-load-modules-maybe)
  5625. (org-run-like-in-org-mode 'org-insert-link))
  5626. (defun org-insert-link (&optional complete-file link-location)
  5627. "Insert a link. At the prompt, enter the link.
  5628. Completion can be used to select a link previously stored with
  5629. `org-store-link'. When the empty string is entered (i.e. if you just
  5630. press RET at the prompt), the link defaults to the most recently
  5631. stored link. As SPC triggers completion in the minibuffer, you need to
  5632. use M-SPC or C-q SPC to force the insertion of a space character.
  5633. You will also be prompted for a description, and if one is given, it will
  5634. be displayed in the buffer instead of the link.
  5635. If there is already a link at point, this command will allow you to edit link
  5636. and description parts.
  5637. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5638. be selected using completion. The path to the file will be relative to the
  5639. current directory if the file is in the current directory or a subdirectory.
  5640. Otherwise, the link will be the absolute path as completed in the minibuffer
  5641. \(i.e. normally ~/path/to/file).
  5642. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5643. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5644. of `org-keep-stored-link-after-insertion'.
  5645. If `org-make-link-description-function' is non-nil, this function will be
  5646. called with the link target, and the result will be the default
  5647. link description.
  5648. If the LINK-LOCATION parameter is non-nil, this value will be
  5649. used as the link location instead of reading one interactively."
  5650. (interactive "P")
  5651. (let* ((wcf (current-window-configuration))
  5652. (region (if (org-region-active-p)
  5653. (buffer-substring (region-beginning) (region-end))))
  5654. (remove (and region (list (region-beginning) (region-end))))
  5655. (desc region)
  5656. tmphist ; byte-compile incorrectly complains about this
  5657. (link link-location)
  5658. entry file)
  5659. (cond
  5660. (link-location) ; specified by arg, just use it.
  5661. ((org-in-regexp org-bracket-link-regexp 1)
  5662. ;; We do have a link at point, and we are going to edit it.
  5663. (setq remove (list (match-beginning 0) (match-end 0)))
  5664. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5665. (setq link (read-string "Link: "
  5666. (org-link-unescape
  5667. (org-match-string-no-properties 1)))))
  5668. ((or (org-in-regexp org-angle-link-re)
  5669. (org-in-regexp org-plain-link-re))
  5670. ;; Convert to bracket link
  5671. (setq remove (list (match-beginning 0) (match-end 0))
  5672. link (read-string "Link: "
  5673. (org-remove-angle-brackets (match-string 0)))))
  5674. ((equal complete-file '(4))
  5675. ;; Completing read for file names.
  5676. (setq file (read-file-name "File: "))
  5677. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5678. (pwd1 (file-name-as-directory (abbreviate-file-name
  5679. (expand-file-name ".")))))
  5680. (cond
  5681. ((equal complete-file '(16))
  5682. (setq link (org-make-link
  5683. "file:"
  5684. (abbreviate-file-name (expand-file-name file)))))
  5685. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5686. (setq link (org-make-link "file:" (match-string 1 file))))
  5687. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5688. (expand-file-name file))
  5689. (setq link (org-make-link
  5690. "file:" (match-string 1 (expand-file-name file)))))
  5691. (t (setq link (org-make-link "file:" file))))))
  5692. (t
  5693. ;; Read link, with completion for stored links.
  5694. (with-output-to-temp-buffer "*Org Links*"
  5695. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5696. (when org-stored-links
  5697. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5698. (princ (mapconcat
  5699. (lambda (x)
  5700. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5701. (reverse org-stored-links) "\n"))))
  5702. (let ((cw (selected-window)))
  5703. (select-window (get-buffer-window "*Org Links*"))
  5704. (shrink-window-if-larger-than-buffer)
  5705. (setq truncate-lines t)
  5706. (select-window cw))
  5707. ;; Fake a link history, containing the stored links.
  5708. (setq tmphist (append (mapcar 'car org-stored-links)
  5709. org-insert-link-history))
  5710. (unwind-protect
  5711. (setq link (org-completing-read
  5712. "Link: "
  5713. (append
  5714. (mapcar (lambda (x) (list (concat (car x) ":")))
  5715. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5716. (mapcar (lambda (x) (list (concat x ":")))
  5717. org-link-types))
  5718. nil nil nil
  5719. 'tmphist
  5720. (or (car (car org-stored-links)))))
  5721. (set-window-configuration wcf)
  5722. (kill-buffer "*Org Links*"))
  5723. (setq entry (assoc link org-stored-links))
  5724. (or entry (push link org-insert-link-history))
  5725. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5726. (not org-keep-stored-link-after-insertion))
  5727. (setq org-stored-links (delq (assoc link org-stored-links)
  5728. org-stored-links)))
  5729. (setq desc (or desc (nth 1 entry)))))
  5730. (if (string-match org-plain-link-re link)
  5731. ;; URL-like link, normalize the use of angular brackets.
  5732. (setq link (org-make-link (org-remove-angle-brackets link))))
  5733. ;; Check if we are linking to the current file with a search option
  5734. ;; If yes, simplify the link by using only the search option.
  5735. (when (and buffer-file-name
  5736. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5737. (let* ((path (match-string 1 link))
  5738. (case-fold-search nil)
  5739. (search (match-string 2 link)))
  5740. (save-match-data
  5741. (if (equal (file-truename buffer-file-name) (file-truename path))
  5742. ;; We are linking to this same file, with a search option
  5743. (setq link search)))))
  5744. ;; Check if we can/should use a relative path. If yes, simplify the link
  5745. (when (string-match "\\<file:\\(.*\\)" link)
  5746. (let* ((path (match-string 1 link))
  5747. (origpath path)
  5748. (case-fold-search nil))
  5749. (cond
  5750. ((eq org-link-file-path-type 'absolute)
  5751. (setq path (abbreviate-file-name (expand-file-name path))))
  5752. ((eq org-link-file-path-type 'noabbrev)
  5753. (setq path (expand-file-name path)))
  5754. ((eq org-link-file-path-type 'relative)
  5755. (setq path (file-relative-name path)))
  5756. (t
  5757. (save-match-data
  5758. (if (string-match (concat "^" (regexp-quote
  5759. (file-name-as-directory
  5760. (expand-file-name "."))))
  5761. (expand-file-name path))
  5762. ;; We are linking a file with relative path name.
  5763. (setq path (substring (expand-file-name path)
  5764. (match-end 0)))))))
  5765. (setq link (concat "file:" path))
  5766. (if (equal desc origpath)
  5767. (setq desc path))))
  5768. (if org-make-link-description-function
  5769. (setq desc (funcall org-make-link-description-function link desc)))
  5770. (setq desc (read-string "Description: " desc))
  5771. (unless (string-match "\\S-" desc) (setq desc nil))
  5772. (if remove (apply 'delete-region remove))
  5773. (insert (org-make-link-string link desc))))
  5774. (defun org-completing-read (&rest args)
  5775. (let ((minibuffer-local-completion-map
  5776. (copy-keymap minibuffer-local-completion-map)))
  5777. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5778. (apply 'completing-read args)))
  5779. (defun org-extract-attributes (s)
  5780. "Extract the attributes cookie from a string and set as text property."
  5781. (let (a attr (start 0) key value)
  5782. (save-match-data
  5783. (when (string-match "{{\\([^}]+\\)}}$" s)
  5784. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5785. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5786. (setq key (match-string 1 a) value (match-string 2 a)
  5787. start (match-end 0)
  5788. attr (plist-put attr (intern key) value))))
  5789. (org-add-props s nil 'org-attributes attr))
  5790. s))
  5791. (defun org-attributes-to-string (plist)
  5792. "Format a property list into an HTML attribute list."
  5793. (let ((s "") key value)
  5794. (while plist
  5795. (setq key (pop plist) value (pop plist))
  5796. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5797. s))
  5798. ;;; Opening/following a link
  5799. (defvar org-link-search-failed nil)
  5800. (defun org-next-link ()
  5801. "Move forward to the next link.
  5802. If the link is in hidden text, expose it."
  5803. (interactive)
  5804. (when (and org-link-search-failed (eq this-command last-command))
  5805. (goto-char (point-min))
  5806. (message "Link search wrapped back to beginning of buffer"))
  5807. (setq org-link-search-failed nil)
  5808. (let* ((pos (point))
  5809. (ct (org-context))
  5810. (a (assoc :link ct)))
  5811. (if a (goto-char (nth 2 a)))
  5812. (if (re-search-forward org-any-link-re nil t)
  5813. (progn
  5814. (goto-char (match-beginning 0))
  5815. (if (org-invisible-p) (org-show-context)))
  5816. (goto-char pos)
  5817. (setq org-link-search-failed t)
  5818. (error "No further link found"))))
  5819. (defun org-previous-link ()
  5820. "Move backward to the previous link.
  5821. If the link is in hidden text, expose it."
  5822. (interactive)
  5823. (when (and org-link-search-failed (eq this-command last-command))
  5824. (goto-char (point-max))
  5825. (message "Link search wrapped back to end of buffer"))
  5826. (setq org-link-search-failed nil)
  5827. (let* ((pos (point))
  5828. (ct (org-context))
  5829. (a (assoc :link ct)))
  5830. (if a (goto-char (nth 1 a)))
  5831. (if (re-search-backward org-any-link-re nil t)
  5832. (progn
  5833. (goto-char (match-beginning 0))
  5834. (if (org-invisible-p) (org-show-context)))
  5835. (goto-char pos)
  5836. (setq org-link-search-failed t)
  5837. (error "No further link found"))))
  5838. (defun org-find-file-at-mouse (ev)
  5839. "Open file link or URL at mouse."
  5840. (interactive "e")
  5841. (mouse-set-point ev)
  5842. (org-open-at-point 'in-emacs))
  5843. (defun org-open-at-mouse (ev)
  5844. "Open file link or URL at mouse."
  5845. (interactive "e")
  5846. (mouse-set-point ev)
  5847. (org-open-at-point))
  5848. (defvar org-window-config-before-follow-link nil
  5849. "The window configuration before following a link.
  5850. This is saved in case the need arises to restore it.")
  5851. (defvar org-open-link-marker (make-marker)
  5852. "Marker pointing to the location where `org-open-at-point; was called.")
  5853. ;;;###autoload
  5854. (defun org-open-at-point-global ()
  5855. "Follow a link like Org-mode does.
  5856. This command can be called in any mode to follow a link that has
  5857. Org-mode syntax."
  5858. (interactive)
  5859. (org-run-like-in-org-mode 'org-open-at-point))
  5860. ;;;###autoload
  5861. (defun org-open-link-from-string (s &optional arg)
  5862. "Open a link in the string S, as if it was in Org-mode."
  5863. (interactive "sLink: \nP")
  5864. (with-temp-buffer
  5865. (let ((org-inhibit-startup t))
  5866. (org-mode)
  5867. (insert s)
  5868. (goto-char (point-min))
  5869. (org-open-at-point arg))))
  5870. (defun org-open-at-point (&optional in-emacs)
  5871. "Open link at or after point.
  5872. If there is no link at point, this function will search forward up to
  5873. the end of the current subtree.
  5874. Normally, files will be opened by an appropriate application. If the
  5875. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  5876. (interactive "P")
  5877. (org-load-modules-maybe)
  5878. (move-marker org-open-link-marker (point))
  5879. (setq org-window-config-before-follow-link (current-window-configuration))
  5880. (org-remove-occur-highlights nil nil t)
  5881. (if (org-at-timestamp-p t)
  5882. (org-follow-timestamp-link)
  5883. (let (type path link line search (pos (point)))
  5884. (catch 'match
  5885. (save-excursion
  5886. (skip-chars-forward "^]\n\r")
  5887. (when (org-in-regexp org-bracket-link-regexp)
  5888. (setq link (org-extract-attributes
  5889. (org-link-unescape (org-match-string-no-properties 1))))
  5890. (while (string-match " *\n *" link)
  5891. (setq link (replace-match " " t t link)))
  5892. (setq link (org-link-expand-abbrev link))
  5893. (cond
  5894. ((or (file-name-absolute-p link)
  5895. (string-match "^\\.\\.?/" link))
  5896. (setq type "file" path link))
  5897. ((string-match org-link-re-with-space2 link)
  5898. (setq type (match-string 1 link) path (match-string 2 link)))
  5899. (t (setq type "thisfile" path link)))
  5900. (throw 'match t)))
  5901. (when (get-text-property (point) 'org-linked-text)
  5902. (setq type "thisfile"
  5903. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  5904. (1+ (point)) (point))
  5905. path (buffer-substring
  5906. (previous-single-property-change pos 'org-linked-text)
  5907. (next-single-property-change pos 'org-linked-text)))
  5908. (throw 'match t))
  5909. (save-excursion
  5910. (when (or (org-in-regexp org-angle-link-re)
  5911. (org-in-regexp org-plain-link-re))
  5912. (setq type (match-string 1) path (match-string 2))
  5913. (throw 'match t)))
  5914. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  5915. (setq type "tree-match"
  5916. path (match-string 1))
  5917. (throw 'match t))
  5918. (save-excursion
  5919. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  5920. (setq type "tags"
  5921. path (match-string 1))
  5922. (while (string-match ":" path)
  5923. (setq path (replace-match "+" t t path)))
  5924. (throw 'match t))))
  5925. (unless path
  5926. (error "No link found"))
  5927. ;; Remove any trailing spaces in path
  5928. (if (string-match " +\\'" path)
  5929. (setq path (replace-match "" t t path)))
  5930. (cond
  5931. ((assoc type org-link-protocols)
  5932. (funcall (nth 1 (assoc type org-link-protocols)) path))
  5933. ((equal type "mailto")
  5934. (let ((cmd (car org-link-mailto-program))
  5935. (args (cdr org-link-mailto-program)) args1
  5936. (address path) (subject "") a)
  5937. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  5938. (setq address (match-string 1 path)
  5939. subject (org-link-escape (match-string 2 path))))
  5940. (while args
  5941. (cond
  5942. ((not (stringp (car args))) (push (pop args) args1))
  5943. (t (setq a (pop args))
  5944. (if (string-match "%a" a)
  5945. (setq a (replace-match address t t a)))
  5946. (if (string-match "%s" a)
  5947. (setq a (replace-match subject t t a)))
  5948. (push a args1))))
  5949. (apply cmd (nreverse args1))))
  5950. ((member type '("http" "https" "ftp" "news"))
  5951. (browse-url (concat type ":" (org-link-escape
  5952. path org-link-escape-chars-browser))))
  5953. ((member type '("message"))
  5954. (browse-url (concat type ":" path)))
  5955. ((string= type "tags")
  5956. (org-tags-view in-emacs path))
  5957. ((string= type "thisfile")
  5958. (if in-emacs
  5959. (switch-to-buffer-other-window
  5960. (org-get-buffer-for-internal-link (current-buffer)))
  5961. (org-mark-ring-push))
  5962. (let ((cmd `(org-link-search
  5963. ,path
  5964. ,(cond ((equal in-emacs '(4)) 'occur)
  5965. ((equal in-emacs '(16)) 'org-occur)
  5966. (t nil))
  5967. ,pos)))
  5968. (condition-case nil (eval cmd)
  5969. (error (progn (widen) (eval cmd))))))
  5970. ((string= type "tree-match")
  5971. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  5972. ((string= type "file")
  5973. (if (string-match "::\\([0-9]+\\)\\'" path)
  5974. (setq line (string-to-number (match-string 1 path))
  5975. path (substring path 0 (match-beginning 0)))
  5976. (if (string-match "::\\(.+\\)\\'" path)
  5977. (setq search (match-string 1 path)
  5978. path (substring path 0 (match-beginning 0)))))
  5979. (if (string-match "[*?{]" (file-name-nondirectory path))
  5980. (dired path)
  5981. (org-open-file path in-emacs line search)))
  5982. ((string= type "news")
  5983. (require 'org-gnus)
  5984. (org-gnus-follow-link path))
  5985. ((string= type "shell")
  5986. (let ((cmd path))
  5987. (if (or (not org-confirm-shell-link-function)
  5988. (funcall org-confirm-shell-link-function
  5989. (format "Execute \"%s\" in shell? "
  5990. (org-add-props cmd nil
  5991. 'face 'org-warning))))
  5992. (progn
  5993. (message "Executing %s" cmd)
  5994. (shell-command cmd))
  5995. (error "Abort"))))
  5996. ((string= type "elisp")
  5997. (let ((cmd path))
  5998. (if (or (not org-confirm-elisp-link-function)
  5999. (funcall org-confirm-elisp-link-function
  6000. (format "Execute \"%s\" as elisp? "
  6001. (org-add-props cmd nil
  6002. 'face 'org-warning))))
  6003. (message "%s => %s" cmd (eval (read cmd)))
  6004. (error "Abort"))))
  6005. (t
  6006. (browse-url-at-point)))))
  6007. (move-marker org-open-link-marker nil)
  6008. (run-hook-with-args 'org-follow-link-hook))
  6009. ;;;; Time estimates
  6010. (defun org-get-effort (&optional pom)
  6011. "Get the effort estimate for the current entry."
  6012. (org-entry-get pom org-effort-property))
  6013. ;;; File search
  6014. (defvar org-create-file-search-functions nil
  6015. "List of functions to construct the right search string for a file link.
  6016. These functions are called in turn with point at the location to
  6017. which the link should point.
  6018. A function in the hook should first test if it would like to
  6019. handle this file type, for example by checking the major-mode or
  6020. the file extension. If it decides not to handle this file, it
  6021. should just return nil to give other functions a chance. If it
  6022. does handle the file, it must return the search string to be used
  6023. when following the link. The search string will be part of the
  6024. file link, given after a double colon, and `org-open-at-point'
  6025. will automatically search for it. If special measures must be
  6026. taken to make the search successful, another function should be
  6027. added to the companion hook `org-execute-file-search-functions',
  6028. which see.
  6029. A function in this hook may also use `setq' to set the variable
  6030. `description' to provide a suggestion for the descriptive text to
  6031. be used for this link when it gets inserted into an Org-mode
  6032. buffer with \\[org-insert-link].")
  6033. (defvar org-execute-file-search-functions nil
  6034. "List of functions to execute a file search triggered by a link.
  6035. Functions added to this hook must accept a single argument, the
  6036. search string that was part of the file link, the part after the
  6037. double colon. The function must first check if it would like to
  6038. handle this search, for example by checking the major-mode or the
  6039. file extension. If it decides not to handle this search, it
  6040. should just return nil to give other functions a chance. If it
  6041. does handle the search, it must return a non-nil value to keep
  6042. other functions from trying.
  6043. Each function can access the current prefix argument through the
  6044. variable `current-prefix-argument'. Note that a single prefix is
  6045. used to force opening a link in Emacs, so it may be good to only
  6046. use a numeric or double prefix to guide the search function.
  6047. In case this is needed, a function in this hook can also restore
  6048. the window configuration before `org-open-at-point' was called using:
  6049. (set-window-configuration org-window-config-before-follow-link)")
  6050. (defun org-link-search (s &optional type avoid-pos)
  6051. "Search for a link search option.
  6052. If S is surrounded by forward slashes, it is interpreted as a
  6053. regular expression. In org-mode files, this will create an `org-occur'
  6054. sparse tree. In ordinary files, `occur' will be used to list matches.
  6055. If the current buffer is in `dired-mode', grep will be used to search
  6056. in all files. If AVOID-POS is given, ignore matches near that position."
  6057. (let ((case-fold-search t)
  6058. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6059. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6060. (append '(("") (" ") ("\t") ("\n"))
  6061. org-emphasis-alist)
  6062. "\\|") "\\)"))
  6063. (pos (point))
  6064. (pre nil) (post nil)
  6065. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6066. (cond
  6067. ;; First check if there are any special
  6068. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6069. ;; Now try the builtin stuff
  6070. ((save-excursion
  6071. (goto-char (point-min))
  6072. (and
  6073. (re-search-forward
  6074. (concat "<<" (regexp-quote s0) ">>") nil t)
  6075. (setq type 'dedicated
  6076. pos (match-beginning 0))))
  6077. ;; There is an exact target for this
  6078. (goto-char pos))
  6079. ((string-match "^/\\(.*\\)/$" s)
  6080. ;; A regular expression
  6081. (cond
  6082. ((org-mode-p)
  6083. (org-occur (match-string 1 s)))
  6084. ;;((eq major-mode 'dired-mode)
  6085. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6086. (t (org-do-occur (match-string 1 s)))))
  6087. (t
  6088. ;; A normal search strings
  6089. (when (equal (string-to-char s) ?*)
  6090. ;; Anchor on headlines, post may include tags.
  6091. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6092. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6093. s (substring s 1)))
  6094. (remove-text-properties
  6095. 0 (length s)
  6096. '(face nil mouse-face nil keymap nil fontified nil) s)
  6097. ;; Make a series of regular expressions to find a match
  6098. (setq words (org-split-string s "[ \n\r\t]+")
  6099. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6100. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6101. "\\)" markers)
  6102. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6103. re2a (concat "[ \t\r\n]" re2a_)
  6104. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6105. re4 (concat "[^a-zA-Z_]" re4_)
  6106. re1 (concat pre re2 post)
  6107. re3 (concat pre (if pre re4_ re4) post)
  6108. re5 (concat pre ".*" re4)
  6109. re2 (concat pre re2)
  6110. re2a (concat pre (if pre re2a_ re2a))
  6111. re4 (concat pre (if pre re4_ re4))
  6112. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6113. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6114. re5 "\\)"
  6115. ))
  6116. (cond
  6117. ((eq type 'org-occur) (org-occur reall))
  6118. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6119. (t (goto-char (point-min))
  6120. (setq type 'fuzzy)
  6121. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6122. (org-search-not-self 1 re1 nil t)
  6123. (org-search-not-self 1 re2 nil t)
  6124. (org-search-not-self 1 re2a nil t)
  6125. (org-search-not-self 1 re3 nil t)
  6126. (org-search-not-self 1 re4 nil t)
  6127. (org-search-not-self 1 re5 nil t)
  6128. )
  6129. (goto-char (match-beginning 1))
  6130. (goto-char pos)
  6131. (error "No match")))))
  6132. (t
  6133. ;; Normal string-search
  6134. (goto-char (point-min))
  6135. (if (search-forward s nil t)
  6136. (goto-char (match-beginning 0))
  6137. (error "No match"))))
  6138. (and (org-mode-p) (org-show-context 'link-search))
  6139. type))
  6140. (defun org-search-not-self (group &rest args)
  6141. "Execute `re-search-forward', but only accept matches that do not
  6142. enclose the position of `org-open-link-marker'."
  6143. (let ((m org-open-link-marker))
  6144. (catch 'exit
  6145. (while (apply 're-search-forward args)
  6146. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6147. (goto-char (match-end group))
  6148. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6149. (> (match-beginning 0) (marker-position m))
  6150. (< (match-end 0) (marker-position m)))
  6151. (save-match-data
  6152. (or (not (org-in-regexp
  6153. org-bracket-link-analytic-regexp 1))
  6154. (not (match-end 4)) ; no description
  6155. (and (<= (match-beginning 4) (point))
  6156. (>= (match-end 4) (point))))))
  6157. (throw 'exit (point))))))))
  6158. (defun org-get-buffer-for-internal-link (buffer)
  6159. "Return a buffer to be used for displaying the link target of internal links."
  6160. (cond
  6161. ((not org-display-internal-link-with-indirect-buffer)
  6162. buffer)
  6163. ((string-match "(Clone)$" (buffer-name buffer))
  6164. (message "Buffer is already a clone, not making another one")
  6165. ;; we also do not modify visibility in this case
  6166. buffer)
  6167. (t ; make a new indirect buffer for displaying the link
  6168. (let* ((bn (buffer-name buffer))
  6169. (ibn (concat bn "(Clone)"))
  6170. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6171. (with-current-buffer ib (org-overview))
  6172. ib))))
  6173. (defun org-do-occur (regexp &optional cleanup)
  6174. "Call the Emacs command `occur'.
  6175. If CLEANUP is non-nil, remove the printout of the regular expression
  6176. in the *Occur* buffer. This is useful if the regex is long and not useful
  6177. to read."
  6178. (occur regexp)
  6179. (when cleanup
  6180. (let ((cwin (selected-window)) win beg end)
  6181. (when (setq win (get-buffer-window "*Occur*"))
  6182. (select-window win))
  6183. (goto-char (point-min))
  6184. (when (re-search-forward "match[a-z]+" nil t)
  6185. (setq beg (match-end 0))
  6186. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6187. (setq end (1- (match-beginning 0)))))
  6188. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6189. (goto-char (point-min))
  6190. (select-window cwin))))
  6191. ;;; The mark ring for links jumps
  6192. (defvar org-mark-ring nil
  6193. "Mark ring for positions before jumps in Org-mode.")
  6194. (defvar org-mark-ring-last-goto nil
  6195. "Last position in the mark ring used to go back.")
  6196. ;; Fill and close the ring
  6197. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6198. (loop for i from 1 to org-mark-ring-length do
  6199. (push (make-marker) org-mark-ring))
  6200. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6201. org-mark-ring)
  6202. (defun org-mark-ring-push (&optional pos buffer)
  6203. "Put the current position or POS into the mark ring and rotate it."
  6204. (interactive)
  6205. (setq pos (or pos (point)))
  6206. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6207. (move-marker (car org-mark-ring)
  6208. (or pos (point))
  6209. (or buffer (current-buffer)))
  6210. (message "%s"
  6211. (substitute-command-keys
  6212. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6213. (defun org-mark-ring-goto (&optional n)
  6214. "Jump to the previous position in the mark ring.
  6215. With prefix arg N, jump back that many stored positions. When
  6216. called several times in succession, walk through the entire ring.
  6217. Org-mode commands jumping to a different position in the current file,
  6218. or to another Org-mode file, automatically push the old position
  6219. onto the ring."
  6220. (interactive "p")
  6221. (let (p m)
  6222. (if (eq last-command this-command)
  6223. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6224. (setq p org-mark-ring))
  6225. (setq org-mark-ring-last-goto p)
  6226. (setq m (car p))
  6227. (switch-to-buffer (marker-buffer m))
  6228. (goto-char m)
  6229. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6230. (defun org-remove-angle-brackets (s)
  6231. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6232. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6233. s)
  6234. (defun org-add-angle-brackets (s)
  6235. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6236. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6237. s)
  6238. (defun org-remove-double-quotes (s)
  6239. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6240. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6241. s)
  6242. ;;; Following specific links
  6243. (defun org-follow-timestamp-link ()
  6244. (cond
  6245. ((org-at-date-range-p t)
  6246. (let ((org-agenda-start-on-weekday)
  6247. (t1 (match-string 1))
  6248. (t2 (match-string 2)))
  6249. (setq t1 (time-to-days (org-time-string-to-time t1))
  6250. t2 (time-to-days (org-time-string-to-time t2)))
  6251. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6252. ((org-at-timestamp-p t)
  6253. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6254. (substring (match-string 1) 0 10)))
  6255. 1))
  6256. (t (error "This should not happen"))))
  6257. ;;; Following file links
  6258. (defvar org-wait nil)
  6259. (defun org-open-file (path &optional in-emacs line search)
  6260. "Open the file at PATH.
  6261. First, this expands any special file name abbreviations. Then the
  6262. configuration variable `org-file-apps' is checked if it contains an
  6263. entry for this file type, and if yes, the corresponding command is launched.
  6264. If no application is found, Emacs simply visits the file.
  6265. With optional argument IN-EMACS, Emacs will visit the file.
  6266. Optional LINE specifies a line to go to, optional SEARCH a string to
  6267. search for. If LINE or SEARCH is given, the file will always be
  6268. opened in Emacs.
  6269. If the file does not exist, an error is thrown."
  6270. (setq in-emacs (or in-emacs line search))
  6271. (let* ((file (if (equal path "")
  6272. buffer-file-name
  6273. (substitute-in-file-name (expand-file-name path))))
  6274. (apps (append org-file-apps (org-default-apps)))
  6275. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6276. (dirp (if remp nil (file-directory-p file)))
  6277. (file (if (and dirp org-open-directory-means-index-dot-org)
  6278. (concat (file-name-as-directory file) "index.org")
  6279. file))
  6280. (dfile (downcase file))
  6281. (old-buffer (current-buffer))
  6282. (old-pos (point))
  6283. (old-mode major-mode)
  6284. ext cmd)
  6285. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6286. (setq ext (match-string 1 dfile))
  6287. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6288. (setq ext (match-string 1 dfile))))
  6289. (if in-emacs
  6290. (setq cmd 'emacs)
  6291. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6292. (and dirp (cdr (assoc 'directory apps)))
  6293. (cdr (assoc ext apps))
  6294. (cdr (assoc t apps)))))
  6295. (when (eq cmd 'default)
  6296. (setq cmd (cdr (assoc t apps))))
  6297. (when (eq cmd 'mailcap)
  6298. (require 'mailcap)
  6299. (mailcap-parse-mailcaps)
  6300. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6301. (command (mailcap-mime-info mime-type)))
  6302. (if (stringp command)
  6303. (setq cmd command)
  6304. (setq cmd 'emacs))))
  6305. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6306. (not (file-exists-p file))
  6307. (not org-open-non-existing-files))
  6308. (error "No such file: %s" file))
  6309. (cond
  6310. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6311. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6312. (while (string-match "['\"]%s['\"]" cmd)
  6313. (setq cmd (replace-match "%s" t t cmd)))
  6314. (while (string-match "%s" cmd)
  6315. (setq cmd (replace-match
  6316. (save-match-data
  6317. (shell-quote-argument
  6318. (convert-standard-filename file)))
  6319. t t cmd)))
  6320. (save-window-excursion
  6321. (start-process-shell-command cmd nil cmd)
  6322. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6323. ))
  6324. ((or (stringp cmd)
  6325. (eq cmd 'emacs))
  6326. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6327. (widen)
  6328. (if line (goto-line line)
  6329. (if search (org-link-search search))))
  6330. ((consp cmd)
  6331. (let ((file (convert-standard-filename file)))
  6332. (eval cmd)))
  6333. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6334. (and (org-mode-p) (eq old-mode 'org-mode)
  6335. (or (not (equal old-buffer (current-buffer)))
  6336. (not (equal old-pos (point))))
  6337. (org-mark-ring-push old-pos old-buffer))))
  6338. (defun org-default-apps ()
  6339. "Return the default applications for this operating system."
  6340. (cond
  6341. ((eq system-type 'darwin)
  6342. org-file-apps-defaults-macosx)
  6343. ((eq system-type 'windows-nt)
  6344. org-file-apps-defaults-windowsnt)
  6345. (t org-file-apps-defaults-gnu)))
  6346. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6347. (defun org-file-remote-p (file)
  6348. "Test whether FILE specifies a location on a remote system.
  6349. Return non-nil if the location is indeed remote.
  6350. For example, the filename \"/user@host:/foo\" specifies a location
  6351. on the system \"/user@host:\"."
  6352. (cond ((fboundp 'file-remote-p)
  6353. (file-remote-p file))
  6354. ((fboundp 'tramp-handle-file-remote-p)
  6355. (tramp-handle-file-remote-p file))
  6356. ((and (boundp 'ange-ftp-name-format)
  6357. (string-match (car ange-ftp-name-format) file))
  6358. t)
  6359. (t nil)))
  6360. ;;;; Refiling
  6361. (defun org-get-org-file ()
  6362. "Read a filename, with default directory `org-directory'."
  6363. (let ((default (or org-default-notes-file remember-data-file)))
  6364. (read-file-name (format "File name [%s]: " default)
  6365. (file-name-as-directory org-directory)
  6366. default)))
  6367. (defun org-notes-order-reversed-p ()
  6368. "Check if the current file should receive notes in reversed order."
  6369. (cond
  6370. ((not org-reverse-note-order) nil)
  6371. ((eq t org-reverse-note-order) t)
  6372. ((not (listp org-reverse-note-order)) nil)
  6373. (t (catch 'exit
  6374. (let ((all org-reverse-note-order)
  6375. entry)
  6376. (while (setq entry (pop all))
  6377. (if (string-match (car entry) buffer-file-name)
  6378. (throw 'exit (cdr entry))))
  6379. nil)))))
  6380. (defvar org-refile-target-table nil
  6381. "The list of refile targets, created by `org-refile'.")
  6382. (defvar org-agenda-new-buffers nil
  6383. "Buffers created to visit agenda files.")
  6384. (defun org-get-refile-targets (&optional default-buffer)
  6385. "Produce a table with refile targets."
  6386. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6387. targets txt re files f desc descre)
  6388. (with-current-buffer (or default-buffer (current-buffer))
  6389. (while (setq entry (pop entries))
  6390. (setq files (car entry) desc (cdr entry))
  6391. (cond
  6392. ((null files) (setq files (list (current-buffer))))
  6393. ((eq files 'org-agenda-files)
  6394. (setq files (org-agenda-files 'unrestricted)))
  6395. ((and (symbolp files) (fboundp files))
  6396. (setq files (funcall files)))
  6397. ((and (symbolp files) (boundp files))
  6398. (setq files (symbol-value files))))
  6399. (if (stringp files) (setq files (list files)))
  6400. (cond
  6401. ((eq (car desc) :tag)
  6402. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6403. ((eq (car desc) :todo)
  6404. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6405. ((eq (car desc) :regexp)
  6406. (setq descre (cdr desc)))
  6407. ((eq (car desc) :level)
  6408. (setq descre (concat "^\\*\\{" (number-to-string
  6409. (if org-odd-levels-only
  6410. (1- (* 2 (cdr desc)))
  6411. (cdr desc)))
  6412. "\\}[ \t]")))
  6413. ((eq (car desc) :maxlevel)
  6414. (setq descre (concat "^\\*\\{1," (number-to-string
  6415. (if org-odd-levels-only
  6416. (1- (* 2 (cdr desc)))
  6417. (cdr desc)))
  6418. "\\}[ \t]")))
  6419. (t (error "Bad refiling target description %s" desc)))
  6420. (while (setq f (pop files))
  6421. (save-excursion
  6422. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6423. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6424. (save-excursion
  6425. (save-restriction
  6426. (widen)
  6427. (goto-char (point-min))
  6428. (while (re-search-forward descre nil t)
  6429. (goto-char (point-at-bol))
  6430. (when (looking-at org-complex-heading-regexp)
  6431. (setq txt (org-link-display-format (match-string 4))
  6432. re (concat "^" (regexp-quote
  6433. (buffer-substring (match-beginning 1)
  6434. (match-end 4)))))
  6435. (if (match-end 5) (setq re (concat re "[ \t]+"
  6436. (regexp-quote
  6437. (match-string 5)))))
  6438. (setq re (concat re "[ \t]*$"))
  6439. (when org-refile-use-outline-path
  6440. (setq txt (mapconcat 'org-protect-slash
  6441. (append
  6442. (if (eq org-refile-use-outline-path 'file)
  6443. (list (file-name-nondirectory
  6444. (buffer-file-name (buffer-base-buffer))))
  6445. (if (eq org-refile-use-outline-path 'full-file-path)
  6446. (list (buffer-file-name (buffer-base-buffer)))))
  6447. (org-get-outline-path)
  6448. (list txt))
  6449. "/")))
  6450. (push (list txt f re (point)) targets))
  6451. (goto-char (point-at-eol))))))))
  6452. (nreverse targets))))
  6453. (defun org-protect-slash (s)
  6454. (while (string-match "/" s)
  6455. (setq s (replace-match "\\" t t s)))
  6456. s)
  6457. (defun org-get-outline-path ()
  6458. "Return the outline path to the current entry, as a list."
  6459. (let (rtn)
  6460. (save-excursion
  6461. (while (org-up-heading-safe)
  6462. (when (looking-at org-complex-heading-regexp)
  6463. (push (org-match-string-no-properties 4) rtn)))
  6464. rtn)))
  6465. (defvar org-refile-history nil
  6466. "History for refiling operations.")
  6467. (defun org-refile (&optional goto default-buffer)
  6468. "Move the entry at point to another heading.
  6469. The list of target headings is compiled using the information in
  6470. `org-refile-targets', which see. This list is created before each use
  6471. and will therefore always be up-to-date.
  6472. At the target location, the entry is filed as a subitem of the target heading.
  6473. Depending on `org-reverse-note-order', the new subitem will either be the
  6474. first of the last subitem.
  6475. With prefix arg GOTO, the command will only visit the target location,
  6476. not actually move anything.
  6477. With a double prefix `C-u C-u', go to the location where the last refiling
  6478. operation has put the subtree."
  6479. (interactive "P")
  6480. (let* ((cbuf (current-buffer))
  6481. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6482. pos it nbuf file re level reversed)
  6483. (if (equal goto '(16))
  6484. (org-refile-goto-last-stored)
  6485. (when (setq it (org-refile-get-location
  6486. (if goto "Goto: " "Refile to: ") default-buffer))
  6487. (setq file (nth 1 it)
  6488. re (nth 2 it)
  6489. pos (nth 3 it))
  6490. (setq nbuf (or (find-buffer-visiting file)
  6491. (find-file-noselect file)))
  6492. (if goto
  6493. (progn
  6494. (switch-to-buffer nbuf)
  6495. (goto-char pos)
  6496. (org-show-context 'org-goto))
  6497. (org-copy-subtree 1 nil t)
  6498. (save-excursion
  6499. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6500. (find-file-noselect file))))
  6501. (setq reversed (org-notes-order-reversed-p))
  6502. (save-excursion
  6503. (save-restriction
  6504. (widen)
  6505. (goto-char pos)
  6506. (looking-at outline-regexp)
  6507. (setq level (org-get-valid-level (funcall outline-level) 1))
  6508. (goto-char
  6509. (if reversed
  6510. (or (outline-next-heading) (point-max))
  6511. (or (save-excursion (outline-get-next-sibling))
  6512. (org-end-of-subtree t t)
  6513. (point-max))))
  6514. (if (not (bolp)) (newline))
  6515. (bookmark-set "org-refile-last-stored")
  6516. (org-paste-subtree level))))
  6517. (org-cut-subtree)
  6518. (setq org-markers-to-move nil)
  6519. (message "Entry refiled to \"%s\"" (car it)))))))
  6520. (defun org-refile-goto-last-stored ()
  6521. "Go to the location where the last refile was stored."
  6522. (interactive)
  6523. (bookmark-jump "org-refile-last-stored")
  6524. (message "This is the location of the last refile"))
  6525. (defun org-refile-get-location (&optional prompt default-buffer)
  6526. "Prompt the user for a refile location, using PROMPT."
  6527. (let ((org-refile-targets org-refile-targets)
  6528. (org-refile-use-outline-path org-refile-use-outline-path))
  6529. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6530. (unless org-refile-target-table
  6531. (error "No refile targets"))
  6532. (let* ((cbuf (current-buffer))
  6533. (cfunc (if org-refile-use-outline-path
  6534. 'org-olpath-completing-read
  6535. 'completing-read))
  6536. (extra (if org-refile-use-outline-path "/" ""))
  6537. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6538. (fname (and filename (file-truename filename)))
  6539. (tbl (mapcar
  6540. (lambda (x)
  6541. (if (not (equal fname (file-truename (nth 1 x))))
  6542. (cons (concat (car x) extra " ("
  6543. (file-name-nondirectory (nth 1 x)) ")")
  6544. (cdr x))
  6545. (cons (concat (car x) extra) (cdr x))))
  6546. org-refile-target-table))
  6547. (completion-ignore-case t))
  6548. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6549. tbl)))
  6550. (defun org-olpath-completing-read (prompt collection &rest args)
  6551. "Read an outline path like a file name."
  6552. (let ((thetable collection))
  6553. (apply
  6554. 'completing-read prompt
  6555. (lambda (string predicate &optional flag)
  6556. (let (rtn r s f (l (length string)))
  6557. (cond
  6558. ((eq flag nil)
  6559. ;; try completion
  6560. (try-completion string thetable))
  6561. ((eq flag t)
  6562. ;; all-completions
  6563. (setq rtn (all-completions string thetable predicate))
  6564. (mapcar
  6565. (lambda (x)
  6566. (setq r (substring x l))
  6567. (if (string-match " ([^)]*)$" x)
  6568. (setq f (match-string 0 x))
  6569. (setq f ""))
  6570. (if (string-match "/" r)
  6571. (concat string (substring r 0 (match-end 0)) f)
  6572. x))
  6573. rtn))
  6574. ((eq flag 'lambda)
  6575. ;; exact match?
  6576. (assoc string thetable)))
  6577. ))
  6578. args)))
  6579. ;;;; Dynamic blocks
  6580. (defun org-find-dblock (name)
  6581. "Find the first dynamic block with name NAME in the buffer.
  6582. If not found, stay at current position and return nil."
  6583. (let (pos)
  6584. (save-excursion
  6585. (goto-char (point-min))
  6586. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6587. nil t)
  6588. (match-beginning 0))))
  6589. (if pos (goto-char pos))
  6590. pos))
  6591. (defconst org-dblock-start-re
  6592. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6593. "Matches the startline of a dynamic block, with parameters.")
  6594. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6595. "Matches the end of a dyhamic block.")
  6596. (defun org-create-dblock (plist)
  6597. "Create a dynamic block section, with parameters taken from PLIST.
  6598. PLIST must containe a :name entry which is used as name of the block."
  6599. (unless (bolp) (newline))
  6600. (let ((name (plist-get plist :name)))
  6601. (insert "#+BEGIN: " name)
  6602. (while plist
  6603. (if (eq (car plist) :name)
  6604. (setq plist (cddr plist))
  6605. (insert " " (prin1-to-string (pop plist)))))
  6606. (insert "\n\n#+END:\n")
  6607. (beginning-of-line -2)))
  6608. (defun org-prepare-dblock ()
  6609. "Prepare dynamic block for refresh.
  6610. This empties the block, puts the cursor at the insert position and returns
  6611. the property list including an extra property :name with the block name."
  6612. (unless (looking-at org-dblock-start-re)
  6613. (error "Not at a dynamic block"))
  6614. (let* ((begdel (1+ (match-end 0)))
  6615. (name (org-no-properties (match-string 1)))
  6616. (params (append (list :name name)
  6617. (read (concat "(" (match-string 3) ")")))))
  6618. (unless (re-search-forward org-dblock-end-re nil t)
  6619. (error "Dynamic block not terminated"))
  6620. (setq params
  6621. (append params
  6622. (list :content (buffer-substring
  6623. begdel (match-beginning 0)))))
  6624. (delete-region begdel (match-beginning 0))
  6625. (goto-char begdel)
  6626. (open-line 1)
  6627. params))
  6628. (defun org-map-dblocks (&optional command)
  6629. "Apply COMMAND to all dynamic blocks in the current buffer.
  6630. If COMMAND is not given, use `org-update-dblock'."
  6631. (let ((cmd (or command 'org-update-dblock))
  6632. pos)
  6633. (save-excursion
  6634. (goto-char (point-min))
  6635. (while (re-search-forward org-dblock-start-re nil t)
  6636. (goto-char (setq pos (match-beginning 0)))
  6637. (condition-case nil
  6638. (funcall cmd)
  6639. (error (message "Error during update of dynamic block")))
  6640. (goto-char pos)
  6641. (unless (re-search-forward org-dblock-end-re nil t)
  6642. (error "Dynamic block not terminated"))))))
  6643. (defun org-dblock-update (&optional arg)
  6644. "User command for updating dynamic blocks.
  6645. Update the dynamic block at point. With prefix ARG, update all dynamic
  6646. blocks in the buffer."
  6647. (interactive "P")
  6648. (if arg
  6649. (org-update-all-dblocks)
  6650. (or (looking-at org-dblock-start-re)
  6651. (org-beginning-of-dblock))
  6652. (org-update-dblock)))
  6653. (defun org-update-dblock ()
  6654. "Update the dynamic block at point
  6655. This means to empty the block, parse for parameters and then call
  6656. the correct writing function."
  6657. (save-window-excursion
  6658. (let* ((pos (point))
  6659. (line (org-current-line))
  6660. (params (org-prepare-dblock))
  6661. (name (plist-get params :name))
  6662. (cmd (intern (concat "org-dblock-write:" name))))
  6663. (message "Updating dynamic block `%s' at line %d..." name line)
  6664. (funcall cmd params)
  6665. (message "Updating dynamic block `%s' at line %d...done" name line)
  6666. (goto-char pos))))
  6667. (defun org-beginning-of-dblock ()
  6668. "Find the beginning of the dynamic block at point.
  6669. Error if there is no scuh block at point."
  6670. (let ((pos (point))
  6671. beg)
  6672. (end-of-line 1)
  6673. (if (and (re-search-backward org-dblock-start-re nil t)
  6674. (setq beg (match-beginning 0))
  6675. (re-search-forward org-dblock-end-re nil t)
  6676. (> (match-end 0) pos))
  6677. (goto-char beg)
  6678. (goto-char pos)
  6679. (error "Not in a dynamic block"))))
  6680. (defun org-update-all-dblocks ()
  6681. "Update all dynamic blocks in the buffer.
  6682. This function can be used in a hook."
  6683. (when (org-mode-p)
  6684. (org-map-dblocks 'org-update-dblock)))
  6685. ;;;; Completion
  6686. (defconst org-additional-option-like-keywords
  6687. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6688. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6689. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6690. "BEGIN_QUOTE" "END_QUOTE"
  6691. "BEGIN_VERSE" "END_VERSE"
  6692. "BEGIN_SRC" "END_SRC"))
  6693. (defcustom org-structure-template-alist
  6694. '(
  6695. ("s" "#+begin_src ?\n\n#+end_src"
  6696. "<src lang=\"?\">\n\n</src>")
  6697. ("e" "#+begin_example\n?\n#+end_example"
  6698. "<example>\n?\n</example>")
  6699. ("q" "#+begin_quote\n?\n#+end_quote"
  6700. "<quote>\n?\n</quote>")
  6701. ("v" "#+begin_verse\n?\n#+end_verse"
  6702. "<verse>\n?\n/verse>")
  6703. ("l" "#+begin_latex\n?\n#+end_latex"
  6704. "<literal style=\"latex\">\n?\n</literal>")
  6705. ("L" "#+latex: "
  6706. "<literal style=\"latex\">?</literal>")
  6707. ("h" "#+begin_html\n?\n#+end_html"
  6708. "<literal style=\"html\">\n?\n</literal>")
  6709. ("H" "#+html: "
  6710. "<literal style=\"html\">?</literal>")
  6711. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6712. ("A" "#+ascii: ")
  6713. ("i" "#+include %file ?"
  6714. "<include file=%file markup=\"?\">")
  6715. )
  6716. "Structure completion elements.
  6717. This is a list of abbreviation keys and values. The value gets inserted
  6718. it you type @samp{.} followed by the key and then the completion key,
  6719. usually `M-TAB'. %file will be replaced by a file name after prompting
  6720. for the file uning completion.
  6721. There are two templates for each key, the first uses the original Org syntax,
  6722. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6723. the default when the /org-mtags.el/ module has been loaded. See also the
  6724. variable `org-mtags-prefere-muse-templates'.
  6725. This is an experimental feature, it is undecided if it is going to stay in."
  6726. :group 'org-completion
  6727. :type '(repeat
  6728. (string :tag "Key")
  6729. (string :tag "Template")
  6730. (string :tag "Muse Template")))
  6731. (defun org-try-structure-completion ()
  6732. "Try to complete a structure template before point.
  6733. This looks for strings like \"<e\" on an otherwise empty line and
  6734. expands them."
  6735. (let ((l (buffer-substring (point-at-bol) (point)))
  6736. a)
  6737. (when (and (looking-at "[ \t]*$")
  6738. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6739. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6740. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6741. (match-beginning 1)) a)
  6742. t)))
  6743. (defun org-complete-expand-structure-template (start cell)
  6744. "Expand a structure template."
  6745. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6746. (rpl (nth (if musep 2 1) cell)))
  6747. (delete-region start (point))
  6748. (when (string-match "\\`#\\+" rpl)
  6749. (cond
  6750. ((bolp))
  6751. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6752. (delete-region (point-at-bol) (point)))
  6753. (t (newline))))
  6754. (setq start (point))
  6755. (if (string-match "%file" rpl)
  6756. (setq rpl (replace-match
  6757. (concat
  6758. "\""
  6759. (save-match-data
  6760. (abbreviate-file-name (read-file-name "Include file: ")))
  6761. "\"")
  6762. t t rpl)))
  6763. (insert rpl)
  6764. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6765. (defun org-complete (&optional arg)
  6766. "Perform completion on word at point.
  6767. At the beginning of a headline, this completes TODO keywords as given in
  6768. `org-todo-keywords'.
  6769. If the current word is preceded by a backslash, completes the TeX symbols
  6770. that are supported for HTML support.
  6771. If the current word is preceded by \"#+\", completes special words for
  6772. setting file options.
  6773. In the line after \"#+STARTUP:, complete valid keywords.\"
  6774. At all other locations, this simply calls the value of
  6775. `org-completion-fallback-command'."
  6776. (interactive "P")
  6777. (org-without-partial-completion
  6778. (catch 'exit
  6779. (let* ((a nil)
  6780. (end (point))
  6781. (beg1 (save-excursion
  6782. (skip-chars-backward (org-re "[:alnum:]_@"))
  6783. (point)))
  6784. (beg (save-excursion
  6785. (skip-chars-backward "a-zA-Z0-9_:$")
  6786. (point)))
  6787. (confirm (lambda (x) (stringp (car x))))
  6788. (searchhead (equal (char-before beg) ?*))
  6789. (struct
  6790. (when (and (member (char-before beg1) '(?. ?<))
  6791. (setq a (assoc (buffer-substring beg1 (point))
  6792. org-structure-template-alist)))
  6793. (org-complete-expand-structure-template (1- beg1) a)
  6794. (throw 'exit t)))
  6795. (tag (and (equal (char-before beg1) ?:)
  6796. (equal (char-after (point-at-bol)) ?*)))
  6797. (prop (and (equal (char-before beg1) ?:)
  6798. (not (equal (char-after (point-at-bol)) ?*))))
  6799. (texp (equal (char-before beg) ?\\))
  6800. (link (equal (char-before beg) ?\[))
  6801. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6802. beg)
  6803. "#+"))
  6804. (startup (string-match "^#\\+STARTUP:.*"
  6805. (buffer-substring (point-at-bol) (point))))
  6806. (completion-ignore-case opt)
  6807. (type nil)
  6808. (tbl nil)
  6809. (table (cond
  6810. (opt
  6811. (setq type :opt)
  6812. (require 'org-exp)
  6813. (append
  6814. (mapcar
  6815. (lambda (x)
  6816. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6817. (cons (match-string 2 x) (match-string 1 x)))
  6818. (org-split-string (org-get-current-options) "\n"))
  6819. (mapcar 'list org-additional-option-like-keywords)))
  6820. (startup
  6821. (setq type :startup)
  6822. org-startup-options)
  6823. (link (append org-link-abbrev-alist-local
  6824. org-link-abbrev-alist))
  6825. (texp
  6826. (setq type :tex)
  6827. org-html-entities)
  6828. ((string-match "\\`\\*+[ \t]+\\'"
  6829. (buffer-substring (point-at-bol) beg))
  6830. (setq type :todo)
  6831. (mapcar 'list org-todo-keywords-1))
  6832. (searchhead
  6833. (setq type :searchhead)
  6834. (save-excursion
  6835. (goto-char (point-min))
  6836. (while (re-search-forward org-todo-line-regexp nil t)
  6837. (push (list
  6838. (org-make-org-heading-search-string
  6839. (match-string 3) t))
  6840. tbl)))
  6841. tbl)
  6842. (tag (setq type :tag beg beg1)
  6843. (or org-tag-alist (org-get-buffer-tags)))
  6844. (prop (setq type :prop beg beg1)
  6845. (mapcar 'list (org-buffer-property-keys nil t t)))
  6846. (t (progn
  6847. (call-interactively org-completion-fallback-command)
  6848. (throw 'exit nil)))))
  6849. (pattern (buffer-substring-no-properties beg end))
  6850. (completion (try-completion pattern table confirm)))
  6851. (cond ((eq completion t)
  6852. (if (not (assoc (upcase pattern) table))
  6853. (message "Already complete")
  6854. (if (and (equal type :opt)
  6855. (not (member (car (assoc (upcase pattern) table))
  6856. org-additional-option-like-keywords)))
  6857. (insert (substring (cdr (assoc (upcase pattern) table))
  6858. (length pattern)))
  6859. (if (memq type '(:tag :prop)) (insert ":")))))
  6860. ((null completion)
  6861. (message "Can't find completion for \"%s\"" pattern)
  6862. (ding))
  6863. ((not (string= pattern completion))
  6864. (delete-region beg end)
  6865. (if (string-match " +$" completion)
  6866. (setq completion (replace-match "" t t completion)))
  6867. (insert completion)
  6868. (if (get-buffer-window "*Completions*")
  6869. (delete-window (get-buffer-window "*Completions*")))
  6870. (if (assoc completion table)
  6871. (if (eq type :todo) (insert " ")
  6872. (if (memq type '(:tag :prop)) (insert ":"))))
  6873. (if (and (equal type :opt) (assoc completion table))
  6874. (message "%s" (substitute-command-keys
  6875. "Press \\[org-complete] again to insert example settings"))))
  6876. (t
  6877. (message "Making completion list...")
  6878. (let ((list (sort (all-completions pattern table confirm)
  6879. 'string<)))
  6880. (with-output-to-temp-buffer "*Completions*"
  6881. (condition-case nil
  6882. ;; Protection needed for XEmacs and emacs 21
  6883. (display-completion-list list pattern)
  6884. (error (display-completion-list list)))))
  6885. (message "Making completion list...%s" "done")))))))
  6886. ;;;; TODO, DEADLINE, Comments
  6887. (defun org-toggle-comment ()
  6888. "Change the COMMENT state of an entry."
  6889. (interactive)
  6890. (save-excursion
  6891. (org-back-to-heading)
  6892. (let (case-fold-search)
  6893. (if (looking-at (concat outline-regexp
  6894. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6895. (replace-match "" t t nil 1)
  6896. (if (looking-at outline-regexp)
  6897. (progn
  6898. (goto-char (match-end 0))
  6899. (insert org-comment-string " ")))))))
  6900. (defvar org-last-todo-state-is-todo nil
  6901. "This is non-nil when the last TODO state change led to a TODO state.
  6902. If the last change removed the TODO tag or switched to DONE, then
  6903. this is nil.")
  6904. (defvar org-setting-tags nil) ; dynamically skiped
  6905. (defun org-parse-local-options (string var)
  6906. "Parse STRING for startup setting relevant for variable VAR."
  6907. (let ((rtn (symbol-value var))
  6908. e opts)
  6909. (save-match-data
  6910. (if (or (not string) (not (string-match "\\S-" string)))
  6911. rtn
  6912. (setq opts (delq nil (mapcar (lambda (x)
  6913. (setq e (assoc x org-startup-options))
  6914. (if (eq (nth 1 e) var) e nil))
  6915. (org-split-string string "[ \t]+"))))
  6916. (if (not opts)
  6917. rtn
  6918. (setq rtn nil)
  6919. (while (setq e (pop opts))
  6920. (if (not (nth 3 e))
  6921. (setq rtn (nth 2 e))
  6922. (if (not (listp rtn)) (setq rtn nil))
  6923. (push (nth 2 e) rtn)))
  6924. rtn)))))
  6925. (defvar org-blocker-hook nil
  6926. "Hook for functions that are allowed to block a state change.
  6927. Each function gets as its single argument a property list, see
  6928. `org-trigger-hook' for more information about this list.
  6929. If any of the functions in this hook returns nil, the state change
  6930. is blocked.")
  6931. (defvar org-trigger-hook nil
  6932. "Hook for functions that are triggered by a state change.
  6933. Each function gets as its single argument a property list with at least
  6934. the following elements:
  6935. (:type type-of-change :position pos-at-entry-start
  6936. :from old-state :to new-state)
  6937. Depending on the type, more properties may be present.
  6938. This mechanism is currently implemented for:
  6939. TODO state changes
  6940. ------------------
  6941. :type todo-state-change
  6942. :from previous state (keyword as a string), or nil
  6943. :to new state (keyword as a string), or nil")
  6944. (defun org-todo (&optional arg)
  6945. "Change the TODO state of an item.
  6946. The state of an item is given by a keyword at the start of the heading,
  6947. like
  6948. *** TODO Write paper
  6949. *** DONE Call mom
  6950. The different keywords are specified in the variable `org-todo-keywords'.
  6951. By default the available states are \"TODO\" and \"DONE\".
  6952. So for this example: when the item starts with TODO, it is changed to DONE.
  6953. When it starts with DONE, the DONE is removed. And when neither TODO nor
  6954. DONE are present, add TODO at the beginning of the heading.
  6955. With C-u prefix arg, use completion to determine the new state.
  6956. With numeric prefix arg, switch to that state.
  6957. For calling through lisp, arg is also interpreted in the following way:
  6958. 'none -> empty state
  6959. \"\"(empty string) -> switch to empty state
  6960. 'done -> switch to DONE
  6961. 'nextset -> switch to the next set of keywords
  6962. 'previousset -> switch to the previous set of keywords
  6963. \"WAITING\" -> switch to the specified keyword, but only if it
  6964. really is a member of `org-todo-keywords'."
  6965. (interactive "P")
  6966. (save-excursion
  6967. (catch 'exit
  6968. (org-back-to-heading)
  6969. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  6970. (or (looking-at (concat " +" org-todo-regexp " *"))
  6971. (looking-at " *"))
  6972. (let* ((match-data (match-data))
  6973. (startpos (point-at-bol))
  6974. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  6975. (org-log-done org-log-done)
  6976. (org-log-repeat org-log-repeat)
  6977. (org-todo-log-states org-todo-log-states)
  6978. (this (match-string 1))
  6979. (hl-pos (match-beginning 0))
  6980. (head (org-get-todo-sequence-head this))
  6981. (ass (assoc head org-todo-kwd-alist))
  6982. (interpret (nth 1 ass))
  6983. (done-word (nth 3 ass))
  6984. (final-done-word (nth 4 ass))
  6985. (last-state (or this ""))
  6986. (completion-ignore-case t)
  6987. (member (member this org-todo-keywords-1))
  6988. (tail (cdr member))
  6989. (state (cond
  6990. ((and org-todo-key-trigger
  6991. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  6992. (and (not arg) org-use-fast-todo-selection
  6993. (not (eq org-use-fast-todo-selection 'prefix)))))
  6994. ;; Use fast selection
  6995. (org-fast-todo-selection))
  6996. ((and (equal arg '(4))
  6997. (or (not org-use-fast-todo-selection)
  6998. (not org-todo-key-trigger)))
  6999. ;; Read a state with completion
  7000. (completing-read "State: " (mapcar (lambda(x) (list x))
  7001. org-todo-keywords-1)
  7002. nil t))
  7003. ((eq arg 'right)
  7004. (if this
  7005. (if tail (car tail) nil)
  7006. (car org-todo-keywords-1)))
  7007. ((eq arg 'left)
  7008. (if (equal member org-todo-keywords-1)
  7009. nil
  7010. (if this
  7011. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7012. org-todo-keywords-1)
  7013. (org-last org-todo-keywords-1))))
  7014. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7015. (setq arg nil))) ; hack to fall back to cycling
  7016. (arg
  7017. ;; user or caller requests a specific state
  7018. (cond
  7019. ((equal arg "") nil)
  7020. ((eq arg 'none) nil)
  7021. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7022. ((eq arg 'nextset)
  7023. (or (car (cdr (member head org-todo-heads)))
  7024. (car org-todo-heads)))
  7025. ((eq arg 'previousset)
  7026. (let ((org-todo-heads (reverse org-todo-heads)))
  7027. (or (car (cdr (member head org-todo-heads)))
  7028. (car org-todo-heads))))
  7029. ((car (member arg org-todo-keywords-1)))
  7030. ((nth (1- (prefix-numeric-value arg))
  7031. org-todo-keywords-1))))
  7032. ((null member) (or head (car org-todo-keywords-1)))
  7033. ((equal this final-done-word) nil) ;; -> make empty
  7034. ((null tail) nil) ;; -> first entry
  7035. ((eq interpret 'sequence)
  7036. (car tail))
  7037. ((memq interpret '(type priority))
  7038. (if (eq this-command last-command)
  7039. (car tail)
  7040. (if (> (length tail) 0)
  7041. (or done-word (car org-done-keywords))
  7042. nil)))
  7043. (t nil)))
  7044. (next (if state (concat " " state " ") " "))
  7045. (change-plist (list :type 'todo-state-change :from this :to state
  7046. :position startpos))
  7047. dolog now-done-p)
  7048. (when org-blocker-hook
  7049. (unless (save-excursion
  7050. (save-match-data
  7051. (run-hook-with-args-until-failure
  7052. 'org-blocker-hook change-plist)))
  7053. (if (interactive-p)
  7054. (error "TODO state change from %s to %s blocked" this state)
  7055. ;; fail silently
  7056. (message "TODO state change from %s to %s blocked" this state)
  7057. (throw 'exit nil))))
  7058. (store-match-data match-data)
  7059. (replace-match next t t)
  7060. (unless (pos-visible-in-window-p hl-pos)
  7061. (message "TODO state changed to %s" (org-trim next)))
  7062. (unless head
  7063. (setq head (org-get-todo-sequence-head state)
  7064. ass (assoc head org-todo-kwd-alist)
  7065. interpret (nth 1 ass)
  7066. done-word (nth 3 ass)
  7067. final-done-word (nth 4 ass)))
  7068. (when (memq arg '(nextset previousset))
  7069. (message "Keyword-Set %d/%d: %s"
  7070. (- (length org-todo-sets) -1
  7071. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7072. (length org-todo-sets)
  7073. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7074. (setq org-last-todo-state-is-todo
  7075. (not (member state org-done-keywords)))
  7076. (setq now-done-p (and (member state org-done-keywords)
  7077. (not (member this org-done-keywords))))
  7078. (and logging (org-local-logging logging))
  7079. (when (and (or org-todo-log-states org-log-done)
  7080. (not (memq arg '(nextset previousset))))
  7081. ;; we need to look at recording a time and note
  7082. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7083. (nth 2 (assoc this org-todo-log-states))))
  7084. (when (and state
  7085. (member state org-not-done-keywords)
  7086. (not (member this org-not-done-keywords)))
  7087. ;; This is now a todo state and was not one before
  7088. ;; If there was a CLOSED time stamp, get rid of it.
  7089. (org-add-planning-info nil nil 'closed))
  7090. (when (and now-done-p org-log-done)
  7091. ;; It is now done, and it was not done before
  7092. (org-add-planning-info 'closed (org-current-time))
  7093. (if (and (not dolog) (eq 'note org-log-done))
  7094. (org-add-log-setup 'done state 'findpos 'note)))
  7095. (when (and state dolog)
  7096. ;; This is a non-nil state, and we need to log it
  7097. (org-add-log-setup 'state state 'findpos dolog)))
  7098. ;; Fixup tag positioning
  7099. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7100. (when org-provide-todo-statistics
  7101. (org-update-parent-todo-statistics))
  7102. (run-hooks 'org-after-todo-state-change-hook)
  7103. (if (and arg (not (member state org-done-keywords)))
  7104. (setq head (org-get-todo-sequence-head state)))
  7105. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7106. ;; Do we need to trigger a repeat?
  7107. (when now-done-p (org-auto-repeat-maybe state))
  7108. ;; Fixup cursor location if close to the keyword
  7109. (if (and (outline-on-heading-p)
  7110. (not (bolp))
  7111. (save-excursion (beginning-of-line 1)
  7112. (looking-at org-todo-line-regexp))
  7113. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7114. (progn
  7115. (goto-char (or (match-end 2) (match-end 1)))
  7116. (just-one-space)))
  7117. (when org-trigger-hook
  7118. (save-excursion
  7119. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7120. (defun org-update-parent-todo-statistics ()
  7121. "Update any statistics cookie in the parent of the current headline."
  7122. (interactive)
  7123. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7124. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7125. (catch 'exit
  7126. (save-excursion
  7127. (setq level (org-up-heading-safe))
  7128. (unless (and level
  7129. (re-search-forward box-re (point-at-eol) t))
  7130. (throw 'exit nil))
  7131. (setq is-percent (match-end 2))
  7132. (save-match-data
  7133. (unless (outline-next-heading) (throw 'exit nil))
  7134. (while (looking-at org-todo-line-regexp)
  7135. (setq kwd (match-string 2))
  7136. (and kwd (setq cnt-all (1+ cnt-all)))
  7137. (and (member kwd org-done-keywords)
  7138. (setq cnt-done (1+ cnt-done)))
  7139. (condition-case nil
  7140. (org-forward-same-level 1)
  7141. (error (end-of-line 1)))))
  7142. (replace-match
  7143. (if is-percent
  7144. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7145. (format "[%d/%d]" cnt-done cnt-all)))
  7146. (run-hook-with-args 'org-after-todo-statistics-hook
  7147. cnt-done (- cnt-all cnt-done))))))
  7148. (defvar org-after-todo-statistics-hook nil
  7149. "Hook that is called after a TODO statistics cookie has been updated.
  7150. Each function is called with two arguments: the number of not-done entries
  7151. and the number of done entries.
  7152. For example, the following function, when added to this hook, will switch
  7153. an entry to DONE when all children are done, and back to TODO when new
  7154. entries are set to a TODO status. Note that this hook is only called
  7155. when there is a statistics cookie in the headline!
  7156. (defun org-summary-todo (n-done n-not-done)
  7157. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7158. (let (org-log-done org-log-states) ; turn off logging
  7159. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7160. ")
  7161. (defun org-local-logging (value)
  7162. "Get logging settings from a property VALUE."
  7163. (let* (words w a)
  7164. ;; directly set the variables, they are already local.
  7165. (setq org-log-done nil
  7166. org-log-repeat nil
  7167. org-todo-log-states nil)
  7168. (setq words (org-split-string value))
  7169. (while (setq w (pop words))
  7170. (cond
  7171. ((setq a (assoc w org-startup-options))
  7172. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7173. (set (nth 1 a) (nth 2 a))))
  7174. ((setq a (org-extract-log-state-settings w))
  7175. (and (member (car a) org-todo-keywords-1)
  7176. (push a org-todo-log-states)))))))
  7177. (defun org-get-todo-sequence-head (kwd)
  7178. "Return the head of the TODO sequence to which KWD belongs.
  7179. If KWD is not set, check if there is a text property remembering the
  7180. right sequence."
  7181. (let (p)
  7182. (cond
  7183. ((not kwd)
  7184. (or (get-text-property (point-at-bol) 'org-todo-head)
  7185. (progn
  7186. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7187. nil (point-at-eol)))
  7188. (get-text-property p 'org-todo-head))))
  7189. ((not (member kwd org-todo-keywords-1))
  7190. (car org-todo-keywords-1))
  7191. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7192. (defun org-fast-todo-selection ()
  7193. "Fast TODO keyword selection with single keys.
  7194. Returns the new TODO keyword, or nil if no state change should occur."
  7195. (let* ((fulltable org-todo-key-alist)
  7196. (done-keywords org-done-keywords) ;; needed for the faces.
  7197. (maxlen (apply 'max (mapcar
  7198. (lambda (x)
  7199. (if (stringp (car x)) (string-width (car x)) 0))
  7200. fulltable)))
  7201. (expert nil)
  7202. (fwidth (+ maxlen 3 1 3))
  7203. (ncol (/ (- (window-width) 4) fwidth))
  7204. tg cnt e c tbl
  7205. groups ingroup)
  7206. (save-window-excursion
  7207. (if expert
  7208. (set-buffer (get-buffer-create " *Org todo*"))
  7209. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7210. (erase-buffer)
  7211. (org-set-local 'org-done-keywords done-keywords)
  7212. (setq tbl fulltable cnt 0)
  7213. (while (setq e (pop tbl))
  7214. (cond
  7215. ((equal e '(:startgroup))
  7216. (push '() groups) (setq ingroup t)
  7217. (when (not (= cnt 0))
  7218. (setq cnt 0)
  7219. (insert "\n"))
  7220. (insert "{ "))
  7221. ((equal e '(:endgroup))
  7222. (setq ingroup nil cnt 0)
  7223. (insert "}\n"))
  7224. (t
  7225. (setq tg (car e) c (cdr e))
  7226. (if ingroup (push tg (car groups)))
  7227. (setq tg (org-add-props tg nil 'face
  7228. (org-get-todo-face tg)))
  7229. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7230. (insert "[" c "] " tg (make-string
  7231. (- fwidth 4 (length tg)) ?\ ))
  7232. (when (= (setq cnt (1+ cnt)) ncol)
  7233. (insert "\n")
  7234. (if ingroup (insert " "))
  7235. (setq cnt 0)))))
  7236. (insert "\n")
  7237. (goto-char (point-min))
  7238. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7239. (fit-window-to-buffer))
  7240. (message "[a-z..]:Set [SPC]:clear")
  7241. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7242. (cond
  7243. ((or (= c ?\C-g)
  7244. (and (= c ?q) (not (rassoc c fulltable))))
  7245. (setq quit-flag t))
  7246. ((= c ?\ ) nil)
  7247. ((setq e (rassoc c fulltable) tg (car e))
  7248. tg)
  7249. (t (setq quit-flag t))))))
  7250. (defun org-entry-is-todo-p ()
  7251. (member (org-get-todo-state) org-not-done-keywords))
  7252. (defun org-entry-is-done-p ()
  7253. (member (org-get-todo-state) org-done-keywords))
  7254. (defun org-get-todo-state ()
  7255. (save-excursion
  7256. (org-back-to-heading t)
  7257. (and (looking-at org-todo-line-regexp)
  7258. (match-end 2)
  7259. (match-string 2))))
  7260. (defun org-at-date-range-p (&optional inactive-ok)
  7261. "Is the cursor inside a date range?"
  7262. (interactive)
  7263. (save-excursion
  7264. (catch 'exit
  7265. (let ((pos (point)))
  7266. (skip-chars-backward "^[<\r\n")
  7267. (skip-chars-backward "<[")
  7268. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7269. (>= (match-end 0) pos)
  7270. (throw 'exit t))
  7271. (skip-chars-backward "^<[\r\n")
  7272. (skip-chars-backward "<[")
  7273. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7274. (>= (match-end 0) pos)
  7275. (throw 'exit t)))
  7276. nil)))
  7277. (defun org-get-repeat ()
  7278. "Check if there is a deadline/schedule with repeater in this entry."
  7279. (save-match-data
  7280. (save-excursion
  7281. (org-back-to-heading t)
  7282. (if (re-search-forward
  7283. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7284. (match-string 1)))))
  7285. (defvar org-last-changed-timestamp)
  7286. (defvar org-last-inserted-timestamp)
  7287. (defvar org-log-post-message)
  7288. (defvar org-log-note-purpose)
  7289. (defvar org-log-note-how)
  7290. (defvar org-log-note-extra)
  7291. (defun org-auto-repeat-maybe (done-word)
  7292. "Check if the current headline contains a repeated deadline/schedule.
  7293. If yes, set TODO state back to what it was and change the base date
  7294. of repeating deadline/scheduled time stamps to new date.
  7295. This function is run automatically after each state change to a DONE state."
  7296. ;; last-state is dynamically scoped into this function
  7297. (let* ((repeat (org-get-repeat))
  7298. (aa (assoc last-state org-todo-kwd-alist))
  7299. (interpret (nth 1 aa))
  7300. (head (nth 2 aa))
  7301. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7302. (msg "Entry repeats: ")
  7303. (org-log-done nil)
  7304. (org-todo-log-states nil)
  7305. (nshiftmax 10) (nshift 0)
  7306. re type n what ts mb0 time)
  7307. (when repeat
  7308. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7309. (org-todo (if (eq interpret 'type) last-state head))
  7310. (when org-log-repeat
  7311. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7312. (memq 'org-add-log-note post-command-hook))
  7313. ;; OK, we are already setup for some record
  7314. (if (eq org-log-repeat 'note)
  7315. ;; make sure we take a note, not only a time stamp
  7316. (setq org-log-note-how 'note))
  7317. ;; Set up for taking a record
  7318. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7319. 'findpos org-log-repeat)))
  7320. (org-back-to-heading t)
  7321. (org-add-planning-info nil nil 'closed)
  7322. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7323. org-deadline-time-regexp "\\)\\|\\("
  7324. org-ts-regexp "\\)"))
  7325. (while (re-search-forward
  7326. re (save-excursion (outline-next-heading) (point)) t)
  7327. (setq type (if (match-end 1) org-scheduled-string
  7328. (if (match-end 3) org-deadline-string "Plain:"))
  7329. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7330. mb0 (match-beginning 0))
  7331. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7332. (setq n (string-to-number (match-string 2 ts))
  7333. what (match-string 3 ts))
  7334. (if (equal what "w") (setq n (* n 7) what "d"))
  7335. ;; Preparation, see if we need to modify the start date for the change
  7336. (when (match-end 1)
  7337. (setq time (save-match-data (org-time-string-to-time ts)))
  7338. (cond
  7339. ((equal (match-string 1 ts) ".")
  7340. ;; Shift starting date to today
  7341. (org-timestamp-change
  7342. (- (time-to-days (current-time)) (time-to-days time))
  7343. 'day))
  7344. ((equal (match-string 1 ts) "+")
  7345. (while (or (= nshift 0)
  7346. (<= (time-to-days time) (time-to-days (current-time))))
  7347. (when (= (incf nshift) nshiftmax)
  7348. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7349. (error "Abort")))
  7350. (org-timestamp-change n (cdr (assoc what whata)))
  7351. (org-at-timestamp-p t)
  7352. (setq ts (match-string 1))
  7353. (setq time (save-match-data (org-time-string-to-time ts))))
  7354. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7355. ;; rematch, so that we have everything in place for the real shift
  7356. (org-at-timestamp-p t)
  7357. (setq ts (match-string 1))
  7358. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7359. (org-timestamp-change n (cdr (assoc what whata)))
  7360. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7361. (setq org-log-post-message msg)
  7362. (message "%s" msg))))
  7363. (defun org-show-todo-tree (arg)
  7364. "Make a compact tree which shows all headlines marked with TODO.
  7365. The tree will show the lines where the regexp matches, and all higher
  7366. headlines above the match.
  7367. With a \\[universal-argument] prefix, also show the DONE entries.
  7368. With a numeric prefix N, construct a sparse tree for the Nth element
  7369. of `org-todo-keywords-1'."
  7370. (interactive "P")
  7371. (let ((case-fold-search nil)
  7372. (kwd-re
  7373. (cond ((null arg) org-not-done-regexp)
  7374. ((equal arg '(4))
  7375. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7376. (mapcar 'list org-todo-keywords-1))))
  7377. (concat "\\("
  7378. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7379. "\\)\\>")))
  7380. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7381. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7382. org-todo-keywords-1)))
  7383. (t (error "Invalid prefix argument: %s" arg)))))
  7384. (message "%d TODO entries found"
  7385. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7386. (defun org-deadline (&optional remove time)
  7387. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7388. With argument REMOVE, remove any deadline from the item.
  7389. When TIME is set, it should be an internal time specification, and the
  7390. scheduling will use the corresponding date."
  7391. (interactive "P")
  7392. (if remove
  7393. (progn
  7394. (org-remove-timestamp-with-keyword org-deadline-string)
  7395. (message "Item no longer has a deadline."))
  7396. (if (org-get-repeat)
  7397. (error "Cannot change deadline on task with repeater, please do that by hand")
  7398. (org-add-planning-info 'deadline time 'closed)
  7399. (message "Deadline on %s" org-last-inserted-timestamp))))
  7400. (defun org-schedule (&optional remove time)
  7401. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7402. With argument REMOVE, remove any scheduling date from the item.
  7403. When TIME is set, it should be an internal time specification, and the
  7404. scheduling will use the corresponding date."
  7405. (interactive "P")
  7406. (if remove
  7407. (progn
  7408. (org-remove-timestamp-with-keyword org-scheduled-string)
  7409. (message "Item is no longer scheduled."))
  7410. (if (org-get-repeat)
  7411. (error "Cannot reschedule task with repeater, please do that by hand")
  7412. (org-add-planning-info 'scheduled time 'closed)
  7413. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7414. (defun org-remove-timestamp-with-keyword (keyword)
  7415. "Remove all time stamps with KEYWORD in the current entry."
  7416. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7417. beg)
  7418. (save-excursion
  7419. (org-back-to-heading t)
  7420. (setq beg (point))
  7421. (org-end-of-subtree t t)
  7422. (while (re-search-backward re beg t)
  7423. (replace-match "")
  7424. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7425. (equal (char-before) ?\ ))
  7426. (backward-delete-char 1)
  7427. (if (string-match "^[ \t]*$" (buffer-substring
  7428. (point-at-bol) (point-at-eol)))
  7429. (delete-region (point-at-bol)
  7430. (min (point-max) (1+ (point-at-eol))))))))))
  7431. (defun org-add-planning-info (what &optional time &rest remove)
  7432. "Insert new timestamp with keyword in the line directly after the headline.
  7433. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7434. If non is given, the user is prompted for a date.
  7435. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7436. be removed."
  7437. (interactive)
  7438. (let (org-time-was-given org-end-time-was-given ts
  7439. end default-time default-input)
  7440. (when (and (not time) (memq what '(scheduled deadline)))
  7441. ;; Try to get a default date/time from existing timestamp
  7442. (save-excursion
  7443. (org-back-to-heading t)
  7444. (setq end (save-excursion (outline-next-heading) (point)))
  7445. (when (re-search-forward (if (eq what 'scheduled)
  7446. org-scheduled-time-regexp
  7447. org-deadline-time-regexp)
  7448. end t)
  7449. (setq ts (match-string 1)
  7450. default-time
  7451. (apply 'encode-time (org-parse-time-string ts))
  7452. default-input (and ts (org-get-compact-tod ts))))))
  7453. (when what
  7454. ;; If necessary, get the time from the user
  7455. (setq time (or time (org-read-date nil 'to-time nil nil
  7456. default-time default-input))))
  7457. (when (and org-insert-labeled-timestamps-at-point
  7458. (member what '(scheduled deadline)))
  7459. (insert
  7460. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7461. (org-insert-time-stamp time org-time-was-given
  7462. nil nil nil (list org-end-time-was-given))
  7463. (setq what nil))
  7464. (save-excursion
  7465. (save-restriction
  7466. (let (col list elt ts buffer-invisibility-spec)
  7467. (org-back-to-heading t)
  7468. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7469. (goto-char (match-end 1))
  7470. (setq col (current-column))
  7471. (goto-char (match-end 0))
  7472. (if (eobp) (insert "\n") (forward-char 1))
  7473. (if (and (not (looking-at outline-regexp))
  7474. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7475. "[^\r\n]*"))
  7476. (not (equal (match-string 1) org-clock-string)))
  7477. (narrow-to-region (match-beginning 0) (match-end 0))
  7478. (insert-before-markers "\n")
  7479. (backward-char 1)
  7480. (narrow-to-region (point) (point))
  7481. (and org-adapt-indentation (org-indent-to-column col)))
  7482. ;; Check if we have to remove something.
  7483. (setq list (cons what remove))
  7484. (while list
  7485. (setq elt (pop list))
  7486. (goto-char (point-min))
  7487. (when (or (and (eq elt 'scheduled)
  7488. (re-search-forward org-scheduled-time-regexp nil t))
  7489. (and (eq elt 'deadline)
  7490. (re-search-forward org-deadline-time-regexp nil t))
  7491. (and (eq elt 'closed)
  7492. (re-search-forward org-closed-time-regexp nil t)))
  7493. (replace-match "")
  7494. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7495. (if (looking-at " +") (replace-match ""))))
  7496. (goto-char (point-max))
  7497. (when what
  7498. (insert
  7499. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7500. (cond ((eq what 'scheduled) org-scheduled-string)
  7501. ((eq what 'deadline) org-deadline-string)
  7502. ((eq what 'closed) org-closed-string))
  7503. " ")
  7504. (setq ts (org-insert-time-stamp
  7505. time
  7506. (or org-time-was-given
  7507. (and (eq what 'closed) org-log-done-with-time))
  7508. (eq what 'closed)
  7509. nil nil (list org-end-time-was-given)))
  7510. (end-of-line 1))
  7511. (goto-char (point-min))
  7512. (widen)
  7513. (if (and (looking-at "[ \t]+\n")
  7514. (equal (char-before) ?\n))
  7515. (delete-region (1- (point)) (point-at-eol)))
  7516. ts)))))
  7517. (defvar org-log-note-marker (make-marker))
  7518. (defvar org-log-note-purpose nil)
  7519. (defvar org-log-note-state nil)
  7520. (defvar org-log-note-how nil)
  7521. (defvar org-log-note-extra nil)
  7522. (defvar org-log-note-window-configuration nil)
  7523. (defvar org-log-note-return-to (make-marker))
  7524. (defvar org-log-post-message nil
  7525. "Message to be displayed after a log note has been stored.
  7526. The auto-repeater uses this.")
  7527. (defun org-add-note ()
  7528. "Add a note to the current entry.
  7529. This is done in the same way as adding a state change note."
  7530. (interactive)
  7531. (org-add-log-setup 'note nil 'findpos nil))
  7532. (defvar org-property-end-re)
  7533. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7534. "Set up the post command hook to take a note.
  7535. If this is about to TODO state change, the new state is expected in STATE.
  7536. When FINDPOS is non-nil, find the correct position for the note in
  7537. the current entry. If not, assume that it can be inserted at point.
  7538. HOW is an indicator what kind of note should be created.
  7539. EXTRA is additional text that will be inserted into the notes buffer."
  7540. (save-restriction
  7541. (save-excursion
  7542. (when findpos
  7543. (org-back-to-heading t)
  7544. (narrow-to-region (point) (save-excursion
  7545. (outline-next-heading) (point)))
  7546. (while (re-search-forward
  7547. (concat "\\(" org-drawer-regexp "\\|" org-property-end-re "\\)")
  7548. (point-max) t) (forward-line))
  7549. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7550. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7551. "[^\r\n]*\\)?"))
  7552. (goto-char (match-end 0))
  7553. (unless org-log-states-order-reversed
  7554. (and (= (char-after) ?\n) (forward-char 1))
  7555. (org-skip-over-state-notes)
  7556. (skip-chars-backward " \t\n\r")))
  7557. (move-marker org-log-note-marker (point))
  7558. (setq org-log-note-purpose purpose
  7559. org-log-note-state state
  7560. org-log-note-how how
  7561. org-log-note-extra extra)
  7562. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7563. (defun org-skip-over-state-notes ()
  7564. "Skip past the list of State notes in an entry."
  7565. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7566. (while (looking-at "[ \t]*- State")
  7567. (condition-case nil
  7568. (org-next-item)
  7569. (error (org-end-of-item)))))
  7570. (defun org-add-log-note (&optional purpose)
  7571. "Pop up a window for taking a note, and add this note later at point."
  7572. (remove-hook 'post-command-hook 'org-add-log-note)
  7573. (setq org-log-note-window-configuration (current-window-configuration))
  7574. (delete-other-windows)
  7575. (move-marker org-log-note-return-to (point))
  7576. (switch-to-buffer (marker-buffer org-log-note-marker))
  7577. (goto-char org-log-note-marker)
  7578. (org-switch-to-buffer-other-window "*Org Note*")
  7579. (erase-buffer)
  7580. (if (memq org-log-note-how '(time state))
  7581. (org-store-log-note)
  7582. (let ((org-inhibit-startup t)) (org-mode))
  7583. (insert (format "# Insert note for %s.
  7584. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7585. (cond
  7586. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7587. ((eq org-log-note-purpose 'done) "closed todo item")
  7588. ((eq org-log-note-purpose 'state)
  7589. (format "state change to \"%s\"" org-log-note-state))
  7590. ((eq org-log-note-purpose 'note)
  7591. "this entry")
  7592. (t (error "This should not happen")))))
  7593. (if org-log-note-extra (insert org-log-note-extra))
  7594. (org-set-local 'org-finish-function 'org-store-log-note)))
  7595. (defvar org-note-abort nil) ; dynamically scoped
  7596. (defun org-store-log-note ()
  7597. "Finish taking a log note, and insert it to where it belongs."
  7598. (let ((txt (buffer-string))
  7599. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7600. lines ind)
  7601. (kill-buffer (current-buffer))
  7602. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7603. (setq txt (replace-match "" t t txt)))
  7604. (if (string-match "\\s-+\\'" txt)
  7605. (setq txt (replace-match "" t t txt)))
  7606. (setq lines (org-split-string txt "\n"))
  7607. (when (and note (string-match "\\S-" note))
  7608. (setq note
  7609. (org-replace-escapes
  7610. note
  7611. (list (cons "%u" (user-login-name))
  7612. (cons "%U" user-full-name)
  7613. (cons "%t" (format-time-string
  7614. (org-time-stamp-format 'long 'inactive)
  7615. (current-time)))
  7616. (cons "%s" (if org-log-note-state
  7617. (concat "\"" org-log-note-state "\"")
  7618. "")))))
  7619. (if lines (setq note (concat note " \\\\")))
  7620. (push note lines))
  7621. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7622. (when lines
  7623. (save-excursion
  7624. (set-buffer (marker-buffer org-log-note-marker))
  7625. (save-excursion
  7626. (goto-char org-log-note-marker)
  7627. (move-marker org-log-note-marker nil)
  7628. (end-of-line 1)
  7629. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7630. (indent-relative nil)
  7631. (insert "- " (pop lines))
  7632. (org-indent-line-function)
  7633. (beginning-of-line 1)
  7634. (looking-at "[ \t]*")
  7635. (setq ind (concat (match-string 0) " "))
  7636. (end-of-line 1)
  7637. (while lines (insert "\n" ind (pop lines)))))))
  7638. (set-window-configuration org-log-note-window-configuration)
  7639. (with-current-buffer (marker-buffer org-log-note-return-to)
  7640. (goto-char org-log-note-return-to))
  7641. (move-marker org-log-note-return-to nil)
  7642. (and org-log-post-message (message "%s" org-log-post-message)))
  7643. (defun org-sparse-tree (&optional arg)
  7644. "Create a sparse tree, prompt for the details.
  7645. This command can create sparse trees. You first need to select the type
  7646. of match used to create the tree:
  7647. t Show entries with a specific TODO keyword.
  7648. T Show entries selected by a tags match.
  7649. p Enter a property name and its value (both with completion on existing
  7650. names/values) and show entries with that property.
  7651. r Show entries matching a regular expression
  7652. d Show deadlines due within `org-deadline-warning-days'."
  7653. (interactive "P")
  7654. (let (ans kwd value)
  7655. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7656. (setq ans (read-char-exclusive))
  7657. (cond
  7658. ((equal ans ?d)
  7659. (call-interactively 'org-check-deadlines))
  7660. ((equal ans ?b)
  7661. (call-interactively 'org-check-before-date))
  7662. ((equal ans ?t)
  7663. (org-show-todo-tree '(4)))
  7664. ((equal ans ?T)
  7665. (call-interactively 'org-tags-sparse-tree))
  7666. ((member ans '(?p ?P))
  7667. (setq kwd (completing-read "Property: "
  7668. (mapcar 'list (org-buffer-property-keys))))
  7669. (setq value (completing-read "Value: "
  7670. (mapcar 'list (org-property-values kwd))))
  7671. (unless (string-match "\\`{.*}\\'" value)
  7672. (setq value (concat "\"" value "\"")))
  7673. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7674. ((member ans '(?r ?R ?/))
  7675. (call-interactively 'org-occur))
  7676. (t (error "No such sparse tree command \"%c\"" ans)))))
  7677. (defvar org-occur-highlights nil
  7678. "List of overlays used for occur matches.")
  7679. (make-variable-buffer-local 'org-occur-highlights)
  7680. (defvar org-occur-parameters nil
  7681. "Parameters of the active org-occur calls.
  7682. This is a list, each call to org-occur pushes as cons cell,
  7683. containing the regular expression and the callback, onto the list.
  7684. The list can contain several entries if `org-occur' has been called
  7685. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7686. will only contain one set of parameters. When the highlights are
  7687. removed (for example with `C-c C-c', or with the next edit (depending
  7688. on `org-remove-highlights-with-change'), this variable is emptied
  7689. as well.")
  7690. (make-variable-buffer-local 'org-occur-parameters)
  7691. (defun org-occur (regexp &optional keep-previous callback)
  7692. "Make a compact tree which shows all matches of REGEXP.
  7693. The tree will show the lines where the regexp matches, and all higher
  7694. headlines above the match. It will also show the heading after the match,
  7695. to make sure editing the matching entry is easy.
  7696. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7697. call to `org-occur' will be kept, to allow stacking of calls to this
  7698. command.
  7699. If CALLBACK is non-nil, it is a function which is called to confirm
  7700. that the match should indeed be shown."
  7701. (interactive "sRegexp: \nP")
  7702. (unless keep-previous
  7703. (org-remove-occur-highlights nil nil t))
  7704. (push (cons regexp callback) org-occur-parameters)
  7705. (let ((cnt 0))
  7706. (save-excursion
  7707. (goto-char (point-min))
  7708. (if (or (not keep-previous) ; do not want to keep
  7709. (not org-occur-highlights)) ; no previous matches
  7710. ;; hide everything
  7711. (org-overview))
  7712. (while (re-search-forward regexp nil t)
  7713. (when (or (not callback)
  7714. (save-match-data (funcall callback)))
  7715. (setq cnt (1+ cnt))
  7716. (when org-highlight-sparse-tree-matches
  7717. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7718. (org-show-context 'occur-tree))))
  7719. (when org-remove-highlights-with-change
  7720. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7721. nil 'local))
  7722. (unless org-sparse-tree-open-archived-trees
  7723. (org-hide-archived-subtrees (point-min) (point-max)))
  7724. (run-hooks 'org-occur-hook)
  7725. (if (interactive-p)
  7726. (message "%d match(es) for regexp %s" cnt regexp))
  7727. cnt))
  7728. (defun org-show-context (&optional key)
  7729. "Make sure point and context and visible.
  7730. How much context is shown depends upon the variables
  7731. `org-show-hierarchy-above', `org-show-following-heading'. and
  7732. `org-show-siblings'."
  7733. (let ((heading-p (org-on-heading-p t))
  7734. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7735. (following-p (org-get-alist-option org-show-following-heading key))
  7736. (entry-p (org-get-alist-option org-show-entry-below key))
  7737. (siblings-p (org-get-alist-option org-show-siblings key)))
  7738. (catch 'exit
  7739. ;; Show heading or entry text
  7740. (if (and heading-p (not entry-p))
  7741. (org-flag-heading nil) ; only show the heading
  7742. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7743. (org-show-hidden-entry))) ; show entire entry
  7744. (when following-p
  7745. ;; Show next sibling, or heading below text
  7746. (save-excursion
  7747. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7748. (org-flag-heading nil))))
  7749. (when siblings-p (org-show-siblings))
  7750. (when hierarchy-p
  7751. ;; show all higher headings, possibly with siblings
  7752. (save-excursion
  7753. (while (and (condition-case nil
  7754. (progn (org-up-heading-all 1) t)
  7755. (error nil))
  7756. (not (bobp)))
  7757. (org-flag-heading nil)
  7758. (when siblings-p (org-show-siblings))))))))
  7759. (defun org-reveal (&optional siblings)
  7760. "Show current entry, hierarchy above it, and the following headline.
  7761. This can be used to show a consistent set of context around locations
  7762. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7763. not t for the search context.
  7764. With optional argument SIBLINGS, on each level of the hierarchy all
  7765. siblings are shown. This repairs the tree structure to what it would
  7766. look like when opened with hierarchical calls to `org-cycle'."
  7767. (interactive "P")
  7768. (let ((org-show-hierarchy-above t)
  7769. (org-show-following-heading t)
  7770. (org-show-siblings (if siblings t org-show-siblings)))
  7771. (org-show-context nil)))
  7772. (defun org-highlight-new-match (beg end)
  7773. "Highlight from BEG to END and mark the highlight is an occur headline."
  7774. (let ((ov (org-make-overlay beg end)))
  7775. (org-overlay-put ov 'face 'secondary-selection)
  7776. (push ov org-occur-highlights)))
  7777. (defun org-remove-occur-highlights (&optional beg end noremove)
  7778. "Remove the occur highlights from the buffer.
  7779. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7780. from the `before-change-functions' in the current buffer."
  7781. (interactive)
  7782. (unless org-inhibit-highlight-removal
  7783. (mapc 'org-delete-overlay org-occur-highlights)
  7784. (setq org-occur-highlights nil)
  7785. (setq org-occur-parameters nil)
  7786. (unless noremove
  7787. (remove-hook 'before-change-functions
  7788. 'org-remove-occur-highlights 'local))))
  7789. ;;;; Priorities
  7790. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7791. "Regular expression matching the priority indicator.")
  7792. (defvar org-remove-priority-next-time nil)
  7793. (defun org-priority-up ()
  7794. "Increase the priority of the current item."
  7795. (interactive)
  7796. (org-priority 'up))
  7797. (defun org-priority-down ()
  7798. "Decrease the priority of the current item."
  7799. (interactive)
  7800. (org-priority 'down))
  7801. (defun org-priority (&optional action)
  7802. "Change the priority of an item by ARG.
  7803. ACTION can be `set', `up', `down', or a character."
  7804. (interactive)
  7805. (setq action (or action 'set))
  7806. (let (current new news have remove)
  7807. (save-excursion
  7808. (org-back-to-heading)
  7809. (if (looking-at org-priority-regexp)
  7810. (setq current (string-to-char (match-string 2))
  7811. have t)
  7812. (setq current org-default-priority))
  7813. (cond
  7814. ((or (eq action 'set)
  7815. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7816. (if (not (eq action 'set))
  7817. (setq new action)
  7818. (message "Priority %c-%c, SPC to remove: "
  7819. org-highest-priority org-lowest-priority)
  7820. (setq new (read-char-exclusive)))
  7821. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7822. (= (upcase org-lowest-priority) org-lowest-priority))
  7823. (setq new (upcase new)))
  7824. (cond ((equal new ?\ ) (setq remove t))
  7825. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7826. (error "Priority must be between `%c' and `%c'"
  7827. org-highest-priority org-lowest-priority))))
  7828. ((eq action 'up)
  7829. (if (and (not have) (eq last-command this-command))
  7830. (setq new org-lowest-priority)
  7831. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7832. org-default-priority (1- current)))))
  7833. ((eq action 'down)
  7834. (if (and (not have) (eq last-command this-command))
  7835. (setq new org-highest-priority)
  7836. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7837. org-default-priority (1+ current)))))
  7838. (t (error "Invalid action")))
  7839. (if (or (< (upcase new) org-highest-priority)
  7840. (> (upcase new) org-lowest-priority))
  7841. (setq remove t))
  7842. (setq news (format "%c" new))
  7843. (if have
  7844. (if remove
  7845. (replace-match "" t t nil 1)
  7846. (replace-match news t t nil 2))
  7847. (if remove
  7848. (error "No priority cookie found in line")
  7849. (looking-at org-todo-line-regexp)
  7850. (if (match-end 2)
  7851. (progn
  7852. (goto-char (match-end 2))
  7853. (insert " [#" news "]"))
  7854. (goto-char (match-beginning 3))
  7855. (insert "[#" news "] ")))))
  7856. (org-preserve-lc (org-set-tags nil 'align))
  7857. (if remove
  7858. (message "Priority removed")
  7859. (message "Priority of current item set to %s" news))))
  7860. (defun org-get-priority (s)
  7861. "Find priority cookie and return priority."
  7862. (save-match-data
  7863. (if (not (string-match org-priority-regexp s))
  7864. (* 1000 (- org-lowest-priority org-default-priority))
  7865. (* 1000 (- org-lowest-priority
  7866. (string-to-char (match-string 2 s)))))))
  7867. ;;;; Tags
  7868. (defvar org-agenda-archives-mode)
  7869. (defun org-scan-tags (action matcher &optional todo-only)
  7870. "Scan headline tags with inheritance and produce output ACTION.
  7871. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  7872. or `agenda' to produce an entry list for an agenda view. It can also be
  7873. a Lisp form or a function that should be called at each matched headline, in
  7874. this case the return value is a list of all return values from these calls.
  7875. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  7876. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  7877. only lines with a TODO keyword are included in the output."
  7878. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7879. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7880. (org-re
  7881. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7882. (props (list 'face 'default
  7883. 'done-face 'org-done
  7884. 'undone-face 'default
  7885. 'mouse-face 'highlight
  7886. 'org-not-done-regexp org-not-done-regexp
  7887. 'org-todo-regexp org-todo-regexp
  7888. 'keymap org-agenda-keymap
  7889. 'help-echo
  7890. (format "mouse-2 or RET jump to org file %s"
  7891. (abbreviate-file-name
  7892. (or (buffer-file-name (buffer-base-buffer))
  7893. (buffer-name (buffer-base-buffer)))))))
  7894. (case-fold-search nil)
  7895. lspos tags tags-list
  7896. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  7897. (llast 0) rtn rtn1 level category i txt
  7898. todo marker entry priority)
  7899. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  7900. (setq action (list 'lambda nil action)))
  7901. (save-excursion
  7902. (goto-char (point-min))
  7903. (when (eq action 'sparse-tree)
  7904. (org-overview)
  7905. (org-remove-occur-highlights))
  7906. (while (re-search-forward re nil t)
  7907. (catch :skip
  7908. (setq todo (if (match-end 1) (match-string 2))
  7909. tags (if (match-end 4) (match-string 4)))
  7910. (goto-char (setq lspos (1+ (match-beginning 0))))
  7911. (setq level (org-reduced-level (funcall outline-level))
  7912. category (org-get-category))
  7913. (setq i llast llast level)
  7914. ;; remove tag lists from same and sublevels
  7915. (while (>= i level)
  7916. (when (setq entry (assoc i tags-alist))
  7917. (setq tags-alist (delete entry tags-alist)))
  7918. (setq i (1- i)))
  7919. ;; add the next tags
  7920. (when tags
  7921. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7922. tags-alist
  7923. (cons (cons level tags) tags-alist)))
  7924. ;; compile tags for current headline
  7925. (setq tags-list
  7926. (if org-use-tag-inheritance
  7927. (apply 'append (mapcar 'cdr tags-alist))
  7928. tags))
  7929. (when (and tags org-use-tag-inheritance
  7930. (not (eq t org-use-tag-inheritance)))
  7931. ;; selective inheritance, remove uninherited ones
  7932. (setcdr (car tags-alist)
  7933. (org-remove-uniherited-tags (cdar tags-alist))))
  7934. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7935. (let ((case-fold-search t)) (eval matcher))
  7936. (or
  7937. (not (member org-archive-tag tags-list))
  7938. ;; we have an archive tag, should we use this anyway?
  7939. (or (not org-agenda-skip-archived-trees)
  7940. (and (eq action 'agenda) org-agenda-archives-mode))))
  7941. (unless (eq action 'sparse-tree) (org-agenda-skip))
  7942. ;; select this headline
  7943. (cond
  7944. ((eq action 'sparse-tree)
  7945. (and org-highlight-sparse-tree-matches
  7946. (org-get-heading) (match-end 0)
  7947. (org-highlight-new-match
  7948. (match-beginning 0) (match-beginning 1)))
  7949. (org-show-context 'tags-tree))
  7950. ((eq action 'agenda)
  7951. (setq txt (org-format-agenda-item
  7952. ""
  7953. (concat
  7954. (if org-tags-match-list-sublevels
  7955. (make-string (1- level) ?.) "")
  7956. (org-get-heading))
  7957. category tags-list)
  7958. priority (org-get-priority txt))
  7959. (goto-char lspos)
  7960. (setq marker (org-agenda-new-marker))
  7961. (org-add-props txt props
  7962. 'org-marker marker 'org-hd-marker marker 'org-category category
  7963. 'priority priority 'type "tagsmatch")
  7964. (push txt rtn))
  7965. ((functionp action)
  7966. (save-excursion
  7967. (setq rtn1 (funcall action))
  7968. (push rtn1 rtn))
  7969. (goto-char (point-at-eol)))
  7970. (t (error "Invalid action")))
  7971. ;; if we are to skip sublevels, jump to end of subtree
  7972. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7973. (when (and (eq action 'sparse-tree)
  7974. (not org-sparse-tree-open-archived-trees))
  7975. (org-hide-archived-subtrees (point-min) (point-max)))
  7976. (nreverse rtn)))
  7977. (defun org-remove-uniherited-tags (tags)
  7978. "Remove all tags that are not inherited from the list TAGS."
  7979. (cond
  7980. ((eq org-use-tag-inheritance t) tags)
  7981. ((not org-use-tag-inheritance) nil)
  7982. ((stringp org-use-tag-inheritance)
  7983. (delq nil (mapcar
  7984. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7985. tags)))
  7986. ((listp org-use-tag-inheritance)
  7987. (org-delete-all org-use-tag-inheritance tags))))
  7988. (defvar todo-only) ;; dynamically scoped
  7989. (defun org-tags-sparse-tree (&optional todo-only match)
  7990. "Create a sparse tree according to tags string MATCH.
  7991. MATCH can contain positive and negative selection of tags, like
  7992. \"+WORK+URGENT-WITHBOSS\".
  7993. If optional argument TODO_ONLY is non-nil, only select lines that are
  7994. also TODO lines."
  7995. (interactive "P")
  7996. (org-prepare-agenda-buffers (list (current-buffer)))
  7997. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7998. (defvar org-cached-props nil)
  7999. (defun org-cached-entry-get (pom property)
  8000. (if (or (eq t org-use-property-inheritance)
  8001. (and (stringp org-use-property-inheritance)
  8002. (string-match org-use-property-inheritance property))
  8003. (and (listp org-use-property-inheritance)
  8004. (member property org-use-property-inheritance)))
  8005. ;; Caching is not possible, check it directly
  8006. (org-entry-get pom property 'inherit)
  8007. ;; Get all properties, so that we can do complicated checks easily
  8008. (cdr (assoc property (or org-cached-props
  8009. (setq org-cached-props
  8010. (org-entry-properties pom)))))))
  8011. (defun org-global-tags-completion-table (&optional files)
  8012. "Return the list of all tags in all agenda buffer/files."
  8013. (save-excursion
  8014. (org-uniquify
  8015. (delq nil
  8016. (apply 'append
  8017. (mapcar
  8018. (lambda (file)
  8019. (set-buffer (find-file-noselect file))
  8020. (append (org-get-buffer-tags)
  8021. (mapcar (lambda (x) (if (stringp (car-safe x))
  8022. (list (car-safe x)) nil))
  8023. org-tag-alist)))
  8024. (if (and files (car files))
  8025. files
  8026. (org-agenda-files))))))))
  8027. (defun org-make-tags-matcher (match)
  8028. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8029. ;; todo-only is scoped dynamically into this function, and the function
  8030. ;; may change it it the matcher asksk for it.
  8031. (unless match
  8032. ;; Get a new match request, with completion
  8033. (let ((org-last-tags-completion-table
  8034. (org-global-tags-completion-table)))
  8035. (setq match (completing-read
  8036. "Match: " 'org-tags-completion-function nil nil nil
  8037. 'org-tags-history))))
  8038. ;; Parse the string and create a lisp form
  8039. (let ((match0 match)
  8040. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8041. minus tag mm
  8042. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8043. orterms term orlist re-p str-p level-p level-op time-p
  8044. prop-p pn pv po cat-p gv)
  8045. (if (string-match "/+" match)
  8046. ;; match contains also a todo-matching request
  8047. (progn
  8048. (setq tagsmatch (substring match 0 (match-beginning 0))
  8049. todomatch (substring match (match-end 0)))
  8050. (if (string-match "^!" todomatch)
  8051. (setq todo-only t todomatch (substring todomatch 1)))
  8052. (if (string-match "^\\s-*$" todomatch)
  8053. (setq todomatch nil)))
  8054. ;; only matching tags
  8055. (setq tagsmatch match todomatch nil))
  8056. ;; Make the tags matcher
  8057. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8058. (setq tagsmatcher t)
  8059. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8060. (while (setq term (pop orterms))
  8061. (while (and (equal (substring term -1) "\\") orterms)
  8062. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8063. (while (string-match re term)
  8064. (setq minus (and (match-end 1)
  8065. (equal (match-string 1 term) "-"))
  8066. tag (match-string 2 term)
  8067. re-p (equal (string-to-char tag) ?{)
  8068. level-p (match-end 4)
  8069. prop-p (match-end 5)
  8070. mm (cond
  8071. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8072. (level-p
  8073. (setq level-op (org-op-to-function (match-string 3 term)))
  8074. `(,level-op level ,(string-to-number
  8075. (match-string 4 term))))
  8076. (prop-p
  8077. (setq pn (match-string 5 term)
  8078. po (match-string 6 term)
  8079. pv (match-string 7 term)
  8080. cat-p (equal pn "CATEGORY")
  8081. re-p (equal (string-to-char pv) ?{)
  8082. str-p (equal (string-to-char pv) ?\")
  8083. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8084. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8085. (if time-p (setq pv (org-matcher-time pv)))
  8086. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8087. (if (equal pn "CATEGORY")
  8088. (setq gv '(get-text-property (point) 'org-category))
  8089. (setq gv `(org-cached-entry-get nil ,pn)))
  8090. (if re-p
  8091. (if (eq po 'org<>)
  8092. `(not (string-match ,pv (or ,gv "")))
  8093. `(string-match ,pv (or ,gv "")))
  8094. (if str-p
  8095. `(,po (or ,gv "") ,pv)
  8096. `(,po (string-to-number (or ,gv ""))
  8097. ,(string-to-number pv) ))))
  8098. (t `(member ,(downcase tag) tags-list)))
  8099. mm (if minus (list 'not mm) mm)
  8100. term (substring term (match-end 0)))
  8101. (push mm tagsmatcher))
  8102. (push (if (> (length tagsmatcher) 1)
  8103. (cons 'and tagsmatcher)
  8104. (car tagsmatcher))
  8105. orlist)
  8106. (setq tagsmatcher nil))
  8107. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8108. (setq tagsmatcher
  8109. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8110. ;; Make the todo matcher
  8111. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8112. (setq todomatcher t)
  8113. (setq orterms (org-split-string todomatch "|") orlist nil)
  8114. (while (setq term (pop orterms))
  8115. (while (string-match re term)
  8116. (setq minus (and (match-end 1)
  8117. (equal (match-string 1 term) "-"))
  8118. kwd (match-string 2 term)
  8119. re-p (equal (string-to-char kwd) ?{)
  8120. term (substring term (match-end 0))
  8121. mm (if re-p
  8122. `(string-match ,(substring kwd 1 -1) todo)
  8123. (list 'equal 'todo kwd))
  8124. mm (if minus (list 'not mm) mm))
  8125. (push mm todomatcher))
  8126. (push (if (> (length todomatcher) 1)
  8127. (cons 'and todomatcher)
  8128. (car todomatcher))
  8129. orlist)
  8130. (setq todomatcher nil))
  8131. (setq todomatcher (if (> (length orlist) 1)
  8132. (cons 'or orlist) (car orlist))))
  8133. ;; Return the string and lisp forms of the matcher
  8134. (setq matcher (if todomatcher
  8135. (list 'and tagsmatcher todomatcher)
  8136. tagsmatcher))
  8137. (cons match0 matcher)))
  8138. (defun org-op-to-function (op &optional stringp)
  8139. "Turn an operator into the appropriate function."
  8140. (setq op
  8141. (cond
  8142. ((equal op "<" ) '(< string< org-time<))
  8143. ((equal op ">" ) '(> org-string> org-time>))
  8144. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8145. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8146. ((member op '("=" "==")) '(= string= org-time=))
  8147. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8148. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8149. (defun org<> (a b) (not (= a b)))
  8150. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8151. (defun org-string>= (a b) (not (string< a b)))
  8152. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8153. (defun org-string<> (a b) (not (string= a b)))
  8154. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8155. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8156. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8157. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8158. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8159. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8160. (defun org-2ft (s)
  8161. "Convert S to a floating point time.
  8162. If S is already a number, just return it. If it is a string, parse
  8163. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8164. (cond
  8165. ((numberp s) s)
  8166. ((stringp s)
  8167. (condition-case nil
  8168. (float-time (apply 'encode-time (org-parse-time-string s)))
  8169. (error 0.)))
  8170. (t 0.)))
  8171. (defun org-matcher-time (s)
  8172. (cond
  8173. ((equal s "<now>") (float-time))
  8174. ((equal s "<today>")
  8175. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8176. (t (org-2ft s))))
  8177. (defun org-match-any-p (re list)
  8178. "Does re match any element of list?"
  8179. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8180. (delq nil list))
  8181. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8182. (defvar org-tags-overlay (org-make-overlay 1 1))
  8183. (org-detach-overlay org-tags-overlay)
  8184. (defun org-get-local-tags-at (&optional pos)
  8185. "Get a list of tags defined in the current headline."
  8186. (org-get-tags-at pos 'local))
  8187. (defun org-get-local-tags ()
  8188. "Get a list of tags defined in the current headline."
  8189. (org-get-tags-at nil 'local))
  8190. (defun org-get-tags-at (&optional pos local)
  8191. "Get a list of all headline tags applicable at POS.
  8192. POS defaults to point. If tags are inherited, the list contains
  8193. the targets in the same sequence as the headlines appear, i.e.
  8194. the tags of the current headline come last.
  8195. When LOCAL is non-nil, only return tags from the current headline,
  8196. ignore inherited ones."
  8197. (interactive)
  8198. (let (tags ltags lastpos parent)
  8199. (save-excursion
  8200. (save-restriction
  8201. (widen)
  8202. (goto-char (or pos (point)))
  8203. (save-match-data
  8204. (catch 'done
  8205. (condition-case nil
  8206. (progn
  8207. (org-back-to-heading t)
  8208. (while (not (equal lastpos (point)))
  8209. (setq lastpos (point))
  8210. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8211. (setq ltags (org-split-string
  8212. (org-match-string-no-properties 1) ":"))
  8213. (setq tags (append
  8214. (if parent
  8215. (org-remove-uniherited-tags ltags)
  8216. ltags)
  8217. tags)))
  8218. (or org-use-tag-inheritance (throw 'done t))
  8219. (if local (throw 'done t))
  8220. (org-up-heading-all 1)
  8221. (setq parent t)))
  8222. (error nil)))))
  8223. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8224. (defun org-toggle-tag (tag &optional onoff)
  8225. "Toggle the tag TAG for the current line.
  8226. If ONOFF is `on' or `off', don't toggle but set to this state."
  8227. (unless (org-on-heading-p t) (error "Not on headling"))
  8228. (let (res current)
  8229. (save-excursion
  8230. (beginning-of-line)
  8231. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8232. (point-at-eol) t)
  8233. (progn
  8234. (setq current (match-string 1))
  8235. (replace-match ""))
  8236. (setq current ""))
  8237. (setq current (nreverse (org-split-string current ":")))
  8238. (cond
  8239. ((eq onoff 'on)
  8240. (setq res t)
  8241. (or (member tag current) (push tag current)))
  8242. ((eq onoff 'off)
  8243. (or (not (member tag current)) (setq current (delete tag current))))
  8244. (t (if (member tag current)
  8245. (setq current (delete tag current))
  8246. (setq res t)
  8247. (push tag current))))
  8248. (end-of-line 1)
  8249. (if current
  8250. (progn
  8251. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8252. (org-set-tags nil t))
  8253. (delete-horizontal-space))
  8254. (run-hooks 'org-after-tags-change-hook))
  8255. res))
  8256. (defun org-align-tags-here (to-col)
  8257. ;; Assumes that this is a headline
  8258. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8259. (beginning-of-line 1)
  8260. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8261. (< pos (match-beginning 2)))
  8262. (progn
  8263. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8264. (goto-char (match-beginning 1))
  8265. (insert " ")
  8266. (delete-region (point) (1+ (match-beginning 2)))
  8267. (setq ncol (max (1+ (current-column))
  8268. (1+ col)
  8269. (if (> to-col 0)
  8270. to-col
  8271. (- (abs to-col) tags-l))))
  8272. (setq p (point))
  8273. (insert (make-string (- ncol (current-column)) ?\ ))
  8274. (setq ncol (current-column))
  8275. (when indent-tabs-mode (tabify p (point-at-eol)))
  8276. (org-move-to-column (min ncol col) t))
  8277. (goto-char pos))))
  8278. (defun org-set-tags (&optional arg just-align)
  8279. "Set the tags for the current headline.
  8280. With prefix ARG, realign all tags in headings in the current buffer."
  8281. (interactive "P")
  8282. (let* ((re (concat "^" outline-regexp))
  8283. (current (org-get-tags-string))
  8284. (col (current-column))
  8285. (org-setting-tags t)
  8286. table current-tags inherited-tags ; computed below when needed
  8287. tags p0 c0 c1 rpl)
  8288. (if arg
  8289. (save-excursion
  8290. (goto-char (point-min))
  8291. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8292. (while (re-search-forward re nil t)
  8293. (org-set-tags nil t)
  8294. (end-of-line 1)))
  8295. (message "All tags realigned to column %d" org-tags-column))
  8296. (if just-align
  8297. (setq tags current)
  8298. ;; Get a new set of tags from the user
  8299. (save-excursion
  8300. (setq table (or org-tag-alist (org-get-buffer-tags))
  8301. org-last-tags-completion-table table
  8302. current-tags (org-split-string current ":")
  8303. inherited-tags (nreverse
  8304. (nthcdr (length current-tags)
  8305. (nreverse (org-get-tags-at))))
  8306. tags
  8307. (if (or (eq t org-use-fast-tag-selection)
  8308. (and org-use-fast-tag-selection
  8309. (delq nil (mapcar 'cdr table))))
  8310. (org-fast-tag-selection
  8311. current-tags inherited-tags table
  8312. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8313. (let ((org-add-colon-after-tag-completion t))
  8314. (org-trim
  8315. (org-without-partial-completion
  8316. (completing-read "Tags: " 'org-tags-completion-function
  8317. nil nil current 'org-tags-history)))))))
  8318. (while (string-match "[-+&]+" tags)
  8319. ;; No boolean logic, just a list
  8320. (setq tags (replace-match ":" t t tags))))
  8321. (if (string-match "\\`[\t ]*\\'" tags)
  8322. (setq tags "")
  8323. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8324. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8325. ;; Insert new tags at the correct column
  8326. (beginning-of-line 1)
  8327. (cond
  8328. ((and (equal current "") (equal tags "")))
  8329. ((re-search-forward
  8330. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8331. (point-at-eol) t)
  8332. (if (equal tags "")
  8333. (setq rpl "")
  8334. (goto-char (match-beginning 0))
  8335. (setq c0 (current-column) p0 (point)
  8336. c1 (max (1+ c0) (if (> org-tags-column 0)
  8337. org-tags-column
  8338. (- (- org-tags-column) (length tags))))
  8339. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8340. (replace-match rpl t t)
  8341. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8342. tags)
  8343. (t (error "Tags alignment failed")))
  8344. (org-move-to-column col)
  8345. (unless just-align
  8346. (run-hooks 'org-after-tags-change-hook)))))
  8347. (defun org-change-tag-in-region (beg end tag off)
  8348. "Add or remove TAG for each entry in the region.
  8349. This works in the agenda, and also in an org-mode buffer."
  8350. (interactive
  8351. (list (region-beginning) (region-end)
  8352. (let ((org-last-tags-completion-table
  8353. (if (org-mode-p)
  8354. (org-get-buffer-tags)
  8355. (org-global-tags-completion-table))))
  8356. (completing-read
  8357. "Tag: " 'org-tags-completion-function nil nil nil
  8358. 'org-tags-history))
  8359. (progn
  8360. (message "[s]et or [r]emove? ")
  8361. (equal (read-char-exclusive) ?r))))
  8362. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8363. (let ((agendap (equal major-mode 'org-agenda-mode))
  8364. l1 l2 m buf pos newhead (cnt 0))
  8365. (goto-char end)
  8366. (setq l2 (1- (org-current-line)))
  8367. (goto-char beg)
  8368. (setq l1 (org-current-line))
  8369. (loop for l from l1 to l2 do
  8370. (goto-line l)
  8371. (setq m (get-text-property (point) 'org-hd-marker))
  8372. (when (or (and (org-mode-p) (org-on-heading-p))
  8373. (and agendap m))
  8374. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8375. pos (if agendap m (point)))
  8376. (with-current-buffer buf
  8377. (save-excursion
  8378. (save-restriction
  8379. (goto-char pos)
  8380. (setq cnt (1+ cnt))
  8381. (org-toggle-tag tag (if off 'off 'on))
  8382. (setq newhead (org-get-heading)))))
  8383. (and agendap (org-agenda-change-all-lines newhead m))))
  8384. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8385. (defun org-tags-completion-function (string predicate &optional flag)
  8386. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8387. (confirm (lambda (x) (stringp (car x)))))
  8388. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8389. (setq s1 (match-string 1 string)
  8390. s2 (match-string 2 string))
  8391. (setq s1 "" s2 string))
  8392. (cond
  8393. ((eq flag nil)
  8394. ;; try completion
  8395. (setq rtn (try-completion s2 ctable confirm))
  8396. (if (stringp rtn)
  8397. (setq rtn
  8398. (concat s1 s2 (substring rtn (length s2))
  8399. (if (and org-add-colon-after-tag-completion
  8400. (assoc rtn ctable))
  8401. ":" ""))))
  8402. rtn)
  8403. ((eq flag t)
  8404. ;; all-completions
  8405. (all-completions s2 ctable confirm)
  8406. )
  8407. ((eq flag 'lambda)
  8408. ;; exact match?
  8409. (assoc s2 ctable)))
  8410. ))
  8411. (defun org-fast-tag-insert (kwd tags face &optional end)
  8412. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8413. (insert (format "%-12s" (concat kwd ":"))
  8414. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8415. (or end "")))
  8416. (defun org-fast-tag-show-exit (flag)
  8417. (save-excursion
  8418. (goto-line 3)
  8419. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8420. (replace-match ""))
  8421. (when flag
  8422. (end-of-line 1)
  8423. (org-move-to-column (- (window-width) 19) t)
  8424. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8425. (defun org-set-current-tags-overlay (current prefix)
  8426. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8427. (if (featurep 'xemacs)
  8428. (org-overlay-display org-tags-overlay (concat prefix s)
  8429. 'secondary-selection)
  8430. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8431. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8432. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8433. "Fast tag selection with single keys.
  8434. CURRENT is the current list of tags in the headline, INHERITED is the
  8435. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8436. possibly with grouping information. TODO-TABLE is a similar table with
  8437. TODO keywords, should these have keys assigned to them.
  8438. If the keys are nil, a-z are automatically assigned.
  8439. Returns the new tags string, or nil to not change the current settings."
  8440. (let* ((fulltable (append table todo-table))
  8441. (maxlen (apply 'max (mapcar
  8442. (lambda (x)
  8443. (if (stringp (car x)) (string-width (car x)) 0))
  8444. fulltable)))
  8445. (buf (current-buffer))
  8446. (expert (eq org-fast-tag-selection-single-key 'expert))
  8447. (buffer-tags nil)
  8448. (fwidth (+ maxlen 3 1 3))
  8449. (ncol (/ (- (window-width) 4) fwidth))
  8450. (i-face 'org-done)
  8451. (c-face 'org-todo)
  8452. tg cnt e c char c1 c2 ntable tbl rtn
  8453. ov-start ov-end ov-prefix
  8454. (exit-after-next org-fast-tag-selection-single-key)
  8455. (done-keywords org-done-keywords)
  8456. groups ingroup)
  8457. (save-excursion
  8458. (beginning-of-line 1)
  8459. (if (looking-at
  8460. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8461. (setq ov-start (match-beginning 1)
  8462. ov-end (match-end 1)
  8463. ov-prefix "")
  8464. (setq ov-start (1- (point-at-eol))
  8465. ov-end (1+ ov-start))
  8466. (skip-chars-forward "^\n\r")
  8467. (setq ov-prefix
  8468. (concat
  8469. (buffer-substring (1- (point)) (point))
  8470. (if (> (current-column) org-tags-column)
  8471. " "
  8472. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8473. (org-move-overlay org-tags-overlay ov-start ov-end)
  8474. (save-window-excursion
  8475. (if expert
  8476. (set-buffer (get-buffer-create " *Org tags*"))
  8477. (delete-other-windows)
  8478. (split-window-vertically)
  8479. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8480. (erase-buffer)
  8481. (org-set-local 'org-done-keywords done-keywords)
  8482. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8483. (org-fast-tag-insert "Current" current c-face "\n\n")
  8484. (org-fast-tag-show-exit exit-after-next)
  8485. (org-set-current-tags-overlay current ov-prefix)
  8486. (setq tbl fulltable char ?a cnt 0)
  8487. (while (setq e (pop tbl))
  8488. (cond
  8489. ((equal e '(:startgroup))
  8490. (push '() groups) (setq ingroup t)
  8491. (when (not (= cnt 0))
  8492. (setq cnt 0)
  8493. (insert "\n"))
  8494. (insert "{ "))
  8495. ((equal e '(:endgroup))
  8496. (setq ingroup nil cnt 0)
  8497. (insert "}\n"))
  8498. (t
  8499. (setq tg (car e) c2 nil)
  8500. (if (cdr e)
  8501. (setq c (cdr e))
  8502. ;; automatically assign a character.
  8503. (setq c1 (string-to-char
  8504. (downcase (substring
  8505. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8506. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8507. (while (or (rassoc char ntable) (rassoc char table))
  8508. (setq char (1+ char)))
  8509. (setq c2 c1))
  8510. (setq c (or c2 char)))
  8511. (if ingroup (push tg (car groups)))
  8512. (setq tg (org-add-props tg nil 'face
  8513. (cond
  8514. ((not (assoc tg table))
  8515. (org-get-todo-face tg))
  8516. ((member tg current) c-face)
  8517. ((member tg inherited) i-face)
  8518. (t nil))))
  8519. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8520. (insert "[" c "] " tg (make-string
  8521. (- fwidth 4 (length tg)) ?\ ))
  8522. (push (cons tg c) ntable)
  8523. (when (= (setq cnt (1+ cnt)) ncol)
  8524. (insert "\n")
  8525. (if ingroup (insert " "))
  8526. (setq cnt 0)))))
  8527. (setq ntable (nreverse ntable))
  8528. (insert "\n")
  8529. (goto-char (point-min))
  8530. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8531. (fit-window-to-buffer))
  8532. (setq rtn
  8533. (catch 'exit
  8534. (while t
  8535. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8536. (if groups " [!] no groups" " [!]groups")
  8537. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8538. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8539. (cond
  8540. ((= c ?\r) (throw 'exit t))
  8541. ((= c ?!)
  8542. (setq groups (not groups))
  8543. (goto-char (point-min))
  8544. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8545. ((= c ?\C-c)
  8546. (if (not expert)
  8547. (org-fast-tag-show-exit
  8548. (setq exit-after-next (not exit-after-next)))
  8549. (setq expert nil)
  8550. (delete-other-windows)
  8551. (split-window-vertically)
  8552. (org-switch-to-buffer-other-window " *Org tags*")
  8553. (and (fboundp 'fit-window-to-buffer)
  8554. (fit-window-to-buffer))))
  8555. ((or (= c ?\C-g)
  8556. (and (= c ?q) (not (rassoc c ntable))))
  8557. (org-detach-overlay org-tags-overlay)
  8558. (setq quit-flag t))
  8559. ((= c ?\ )
  8560. (setq current nil)
  8561. (if exit-after-next (setq exit-after-next 'now)))
  8562. ((= c ?\t)
  8563. (condition-case nil
  8564. (setq tg (completing-read
  8565. "Tag: "
  8566. (or buffer-tags
  8567. (with-current-buffer buf
  8568. (org-get-buffer-tags)))))
  8569. (quit (setq tg "")))
  8570. (when (string-match "\\S-" tg)
  8571. (add-to-list 'buffer-tags (list tg))
  8572. (if (member tg current)
  8573. (setq current (delete tg current))
  8574. (push tg current)))
  8575. (if exit-after-next (setq exit-after-next 'now)))
  8576. ((setq e (rassoc c todo-table) tg (car e))
  8577. (with-current-buffer buf
  8578. (save-excursion (org-todo tg)))
  8579. (if exit-after-next (setq exit-after-next 'now)))
  8580. ((setq e (rassoc c ntable) tg (car e))
  8581. (if (member tg current)
  8582. (setq current (delete tg current))
  8583. (loop for g in groups do
  8584. (if (member tg g)
  8585. (mapc (lambda (x)
  8586. (setq current (delete x current)))
  8587. g)))
  8588. (push tg current))
  8589. (if exit-after-next (setq exit-after-next 'now))))
  8590. ;; Create a sorted list
  8591. (setq current
  8592. (sort current
  8593. (lambda (a b)
  8594. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8595. (if (eq exit-after-next 'now) (throw 'exit t))
  8596. (goto-char (point-min))
  8597. (beginning-of-line 2)
  8598. (delete-region (point) (point-at-eol))
  8599. (org-fast-tag-insert "Current" current c-face)
  8600. (org-set-current-tags-overlay current ov-prefix)
  8601. (while (re-search-forward
  8602. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8603. (setq tg (match-string 1))
  8604. (add-text-properties
  8605. (match-beginning 1) (match-end 1)
  8606. (list 'face
  8607. (cond
  8608. ((member tg current) c-face)
  8609. ((member tg inherited) i-face)
  8610. (t (get-text-property (match-beginning 1) 'face))))))
  8611. (goto-char (point-min)))))
  8612. (org-detach-overlay org-tags-overlay)
  8613. (if rtn
  8614. (mapconcat 'identity current ":")
  8615. nil))))
  8616. (defun org-get-tags-string ()
  8617. "Get the TAGS string in the current headline."
  8618. (unless (org-on-heading-p t)
  8619. (error "Not on a heading"))
  8620. (save-excursion
  8621. (beginning-of-line 1)
  8622. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8623. (org-match-string-no-properties 1)
  8624. "")))
  8625. (defun org-get-tags ()
  8626. "Get the list of tags specified in the current headline."
  8627. (org-split-string (org-get-tags-string) ":"))
  8628. (defun org-get-buffer-tags ()
  8629. "Get a table of all tags used in the buffer, for completion."
  8630. (let (tags)
  8631. (save-excursion
  8632. (goto-char (point-min))
  8633. (while (re-search-forward
  8634. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8635. (when (equal (char-after (point-at-bol 0)) ?*)
  8636. (mapc (lambda (x) (add-to-list 'tags x))
  8637. (org-split-string (org-match-string-no-properties 1) ":")))))
  8638. (mapcar 'list tags)))
  8639. ;;;; The mapping API
  8640. ;;;###autoload
  8641. (defun org-map-entries (func &optional match scope &rest skip)
  8642. "Call FUNC at each headline selected by MATCH in SCOPE.
  8643. FUNC is a function or a lisp form. The function will be called without
  8644. arguments, with the cursor positioned at the beginning of the headline.
  8645. The return values of all calls to the function will be collected and
  8646. returned as a list.
  8647. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8648. Only headlines that are matched by this query will be considered during
  8649. the iteration. When MATCH is nil or t, all headlines will be
  8650. visited by the iteration.
  8651. SCOPE determines the scope of this command. It can be any of:
  8652. nil The current buffer, respecting the restriction if any
  8653. tree The subtree started with the entry at point
  8654. file The current buffer, without restriction
  8655. file-with-archives
  8656. The current buffer, and any archives associated with it
  8657. agenda All agenda files
  8658. agenda-with-archives
  8659. All agenda files with any archive files associated with them
  8660. \(file1 file2 ...)
  8661. If this is a list, all files in the list will be scanned
  8662. The remaining args are treated as settings for the skipping facilities of
  8663. the scanner. The following items can be given here:
  8664. archive skip trees with the archive tag.
  8665. comment skip trees with the COMMENT keyword
  8666. function or Emacs Lisp form:
  8667. will be used as value for `org-agenda-skip-function', so whenever
  8668. the the function returns t, FUNC will not be called for that
  8669. entry and search will continue from the point where the
  8670. function leaves it."
  8671. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8672. (org-agenda-skip-archived-trees (memq 'archive skip))
  8673. (org-agenda-skip-comment-trees (memq 'comment skip))
  8674. (org-agenda-skip-function
  8675. (car (org-delete-all '(comment archive) skip)))
  8676. (org-tags-match-list-sublevels t)
  8677. matcher pos file
  8678. org-todo-keywords-for-agenda
  8679. org-done-keywords-for-agenda
  8680. org-todo-keyword-alist-for-agenda
  8681. org-tag-alist-for-agenda)
  8682. (cond
  8683. ((eq match t) (setq matcher t))
  8684. ((eq match nil) (setq matcher t))
  8685. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8686. (when (eq scope 'tree)
  8687. (org-back-to-heading t)
  8688. (org-narrow-to-subtree)
  8689. (setq scope nil))
  8690. (if (not scope)
  8691. (progn
  8692. (org-prepare-agenda-buffers
  8693. (list (buffer-file-name (current-buffer))))
  8694. (org-scan-tags func matcher))
  8695. ;; Get the right scope
  8696. (setq pos (point))
  8697. (cond
  8698. ((and scope (listp scope) (symbolp (car scope)))
  8699. (setq scope (eval scope)))
  8700. ((eq scope 'agenda)
  8701. (setq scope (org-agenda-files t)))
  8702. ((eq scope 'agenda-with-archives)
  8703. (setq scope (org-agenda-files t))
  8704. (setq scope (org-add-archive-files scope)))
  8705. ((eq scope 'file)
  8706. (setq scope (list (buffer-file-name))))
  8707. ((eq scope 'file-with-archives)
  8708. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8709. (org-prepare-agenda-buffers scope)
  8710. (while (setq file (pop scope))
  8711. (with-current-buffer (org-find-base-buffer-visiting file)
  8712. (save-excursion
  8713. (save-restriction
  8714. (widen)
  8715. (goto-char (point-min))
  8716. (org-scan-tags func matcher))))))))
  8717. ;;;; Properties
  8718. ;;; Setting and retrieving properties
  8719. (defconst org-special-properties
  8720. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8721. "TIMESTAMP" "TIMESTAMP_IA")
  8722. "The special properties valid in Org-mode.
  8723. These are properties that are not defined in the property drawer,
  8724. but in some other way.")
  8725. (defconst org-default-properties
  8726. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8727. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8728. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8729. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8730. "Some properties that are used by Org-mode for various purposes.
  8731. Being in this list makes sure that they are offered for completion.")
  8732. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8733. "Regular expression matching the first line of a property drawer.")
  8734. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8735. "Regular expression matching the first line of a property drawer.")
  8736. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8737. "Regular expression matching the first line of a property drawer.")
  8738. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8739. "Regular expression matching the first line of a property drawer.")
  8740. (defconst org-property-drawer-re
  8741. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8742. org-property-end-re "\\)\n?")
  8743. "Matches an entire property drawer.")
  8744. (defconst org-clock-drawer-re
  8745. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8746. org-property-end-re "\\)\n?")
  8747. "Matches an entire clock drawer.")
  8748. (defun org-property-action ()
  8749. "Do an action on properties."
  8750. (interactive)
  8751. (let (c)
  8752. (org-at-property-p)
  8753. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8754. (setq c (read-char-exclusive))
  8755. (cond
  8756. ((equal c ?s)
  8757. (call-interactively 'org-set-property))
  8758. ((equal c ?d)
  8759. (call-interactively 'org-delete-property))
  8760. ((equal c ?D)
  8761. (call-interactively 'org-delete-property-globally))
  8762. ((equal c ?c)
  8763. (call-interactively 'org-compute-property-at-point))
  8764. (t (error "No such property action %c" c)))))
  8765. (defun org-at-property-p ()
  8766. "Is the cursor in a property line?"
  8767. ;; FIXME: Does not check if we are actually in the drawer.
  8768. ;; FIXME: also returns true on any drawers.....
  8769. ;; This is used by C-c C-c for property action.
  8770. (save-excursion
  8771. (beginning-of-line 1)
  8772. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8773. (defun org-get-property-block (&optional beg end force)
  8774. "Return the (beg . end) range of the body of the property drawer.
  8775. BEG and END can be beginning and end of subtree, if not given
  8776. they will be found.
  8777. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8778. (catch 'exit
  8779. (save-excursion
  8780. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8781. (end (or end (progn (outline-next-heading) (point)))))
  8782. (goto-char beg)
  8783. (if (re-search-forward org-property-start-re end t)
  8784. (setq beg (1+ (match-end 0)))
  8785. (if force
  8786. (save-excursion
  8787. (org-insert-property-drawer)
  8788. (setq end (progn (outline-next-heading) (point))))
  8789. (throw 'exit nil))
  8790. (goto-char beg)
  8791. (if (re-search-forward org-property-start-re end t)
  8792. (setq beg (1+ (match-end 0)))))
  8793. (if (re-search-forward org-property-end-re end t)
  8794. (setq end (match-beginning 0))
  8795. (or force (throw 'exit nil))
  8796. (goto-char beg)
  8797. (setq end beg)
  8798. (org-indent-line-function)
  8799. (insert ":END:\n"))
  8800. (cons beg end)))))
  8801. (defun org-entry-properties (&optional pom which)
  8802. "Get all properties of the entry at point-or-marker POM.
  8803. This includes the TODO keyword, the tags, time strings for deadline,
  8804. scheduled, and clocking, and any additional properties defined in the
  8805. entry. The return value is an alist, keys may occur multiple times
  8806. if the property key was used several times.
  8807. POM may also be nil, in which case the current entry is used.
  8808. If WHICH is nil or `all', get all properties. If WHICH is
  8809. `special' or `standard', only get that subclass."
  8810. (setq which (or which 'all))
  8811. (org-with-point-at pom
  8812. (let ((clockstr (substring org-clock-string 0 -1))
  8813. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8814. beg end range props sum-props key value string clocksum)
  8815. (save-excursion
  8816. (when (condition-case nil (org-back-to-heading t) (error nil))
  8817. (setq beg (point))
  8818. (setq sum-props (get-text-property (point) 'org-summaries))
  8819. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8820. (outline-next-heading)
  8821. (setq end (point))
  8822. (when (memq which '(all special))
  8823. ;; Get the special properties, like TODO and tags
  8824. (goto-char beg)
  8825. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8826. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8827. (when (looking-at org-priority-regexp)
  8828. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8829. (when (and (setq value (org-get-tags-string))
  8830. (string-match "\\S-" value))
  8831. (push (cons "TAGS" value) props))
  8832. (when (setq value (org-get-tags-at))
  8833. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8834. props))
  8835. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8836. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8837. string (if (equal key clockstr)
  8838. (org-no-properties
  8839. (org-trim
  8840. (buffer-substring
  8841. (match-beginning 3) (goto-char (point-at-eol)))))
  8842. (substring (org-match-string-no-properties 3) 1 -1)))
  8843. (unless key
  8844. (if (= (char-after (match-beginning 3)) ?\[)
  8845. (setq key "TIMESTAMP_IA")
  8846. (setq key "TIMESTAMP")))
  8847. (when (or (equal key clockstr) (not (assoc key props)))
  8848. (push (cons key string) props)))
  8849. )
  8850. (when (memq which '(all standard))
  8851. ;; Get the standard properties, like :PORP: ...
  8852. (setq range (org-get-property-block beg end))
  8853. (when range
  8854. (goto-char (car range))
  8855. (while (re-search-forward
  8856. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8857. (cdr range) t)
  8858. (setq key (org-match-string-no-properties 1)
  8859. value (org-trim (or (org-match-string-no-properties 2) "")))
  8860. (unless (member key excluded)
  8861. (push (cons key (or value "")) props)))))
  8862. (if clocksum
  8863. (push (cons "CLOCKSUM"
  8864. (org-columns-number-to-string (/ (float clocksum) 60.)
  8865. 'add_times))
  8866. props))
  8867. (append sum-props (nreverse props)))))))
  8868. (defun org-entry-get (pom property &optional inherit)
  8869. "Get value of PROPERTY for entry at point-or-marker POM.
  8870. If INHERIT is non-nil and the entry does not have the property,
  8871. then also check higher levels of the hierarchy.
  8872. If INHERIT is the symbol `selective', use inheritance only if the setting
  8873. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8874. If the property is present but empty, the return value is the empty string.
  8875. If the property is not present at all, nil is returned."
  8876. (org-with-point-at pom
  8877. (if (and inherit (if (eq inherit 'selective)
  8878. (org-property-inherit-p property)
  8879. t))
  8880. (org-entry-get-with-inheritance property)
  8881. (if (member property org-special-properties)
  8882. ;; We need a special property. Use brute force, get all properties.
  8883. (cdr (assoc property (org-entry-properties nil 'special)))
  8884. (let ((range (org-get-property-block)))
  8885. (if (and range
  8886. (goto-char (car range))
  8887. (re-search-forward
  8888. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8889. (cdr range) t))
  8890. ;; Found the property, return it.
  8891. (if (match-end 1)
  8892. (org-match-string-no-properties 1)
  8893. "")))))))
  8894. (defun org-property-or-variable-value (var &optional inherit)
  8895. "Check if there is a property fixing the value of VAR.
  8896. If yes, return this value. If not, return the current value of the variable."
  8897. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8898. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8899. (read prop)
  8900. (symbol-value var))))
  8901. (defun org-entry-delete (pom property)
  8902. "Delete the property PROPERTY from entry at point-or-marker POM."
  8903. (org-with-point-at pom
  8904. (if (member property org-special-properties)
  8905. nil ; cannot delete these properties.
  8906. (let ((range (org-get-property-block)))
  8907. (if (and range
  8908. (goto-char (car range))
  8909. (re-search-forward
  8910. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8911. (cdr range) t))
  8912. (progn
  8913. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8914. t)
  8915. nil)))))
  8916. ;; Multi-values properties are properties that contain multiple values
  8917. ;; These values are assumed to be single words, separated by whitespace.
  8918. (defun org-entry-add-to-multivalued-property (pom property value)
  8919. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8920. (let* ((old (org-entry-get pom property))
  8921. (values (and old (org-split-string old "[ \t]"))))
  8922. (setq value (org-entry-protect-space value))
  8923. (unless (member value values)
  8924. (setq values (cons value values))
  8925. (org-entry-put pom property
  8926. (mapconcat 'identity values " ")))))
  8927. (defun org-entry-remove-from-multivalued-property (pom property value)
  8928. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8929. (let* ((old (org-entry-get pom property))
  8930. (values (and old (org-split-string old "[ \t]"))))
  8931. (setq value (org-entry-protect-space value))
  8932. (when (member value values)
  8933. (setq values (delete value values))
  8934. (org-entry-put pom property
  8935. (mapconcat 'identity values " ")))))
  8936. (defun org-entry-member-in-multivalued-property (pom property value)
  8937. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8938. (let* ((old (org-entry-get pom property))
  8939. (values (and old (org-split-string old "[ \t]"))))
  8940. (setq value (org-entry-protect-space value))
  8941. (member value values)))
  8942. (defun org-entry-get-multivalued-property (pom property)
  8943. "Return a list of values in a multivalued property."
  8944. (let* ((value (org-entry-get pom property))
  8945. (values (and value (org-split-string value "[ \t]"))))
  8946. (mapcar 'org-entry-restore-space values)))
  8947. (defun org-entry-put-multivalued-property (pom property &rest values)
  8948. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  8949. VALUES should be a list of strings. Spaces will be protected."
  8950. (org-entry-put pom property
  8951. (mapconcat 'org-entry-protect-space values " "))
  8952. (let* ((value (org-entry-get pom property))
  8953. (values (and value (org-split-string value "[ \t]"))))
  8954. (mapcar 'org-entry-restore-space values)))
  8955. (defun org-entry-protect-space (s)
  8956. "Protect spaces and newline in string S."
  8957. (while (string-match " " s)
  8958. (setq s (replace-match "%20" t t s)))
  8959. (while (string-match "\n" s)
  8960. (setq s (replace-match "%0A" t t s)))
  8961. s)
  8962. (defun org-entry-restore-space (s)
  8963. "Restore spaces and newline in string S."
  8964. (while (string-match "%20" s)
  8965. (setq s (replace-match " " t t s)))
  8966. (while (string-match "%0A" s)
  8967. (setq s (replace-match "\n" t t s)))
  8968. s)
  8969. (defvar org-entry-property-inherited-from (make-marker)
  8970. "Marker pointing to the entry from where a proerty was inherited.
  8971. Each call to `org-entry-get-with-inheritance' will set this marker to the
  8972. location of the entry where the inheriance search matched. If there was
  8973. no match, the marker will point nowhere.
  8974. Note that also `org-entry-get' calls this function, if the INHERIT flag
  8975. is set.")
  8976. (defun org-entry-get-with-inheritance (property)
  8977. "Get entry property, and search higher levels if not present."
  8978. (move-marker org-entry-property-inherited-from nil)
  8979. (let (tmp)
  8980. (save-excursion
  8981. (save-restriction
  8982. (widen)
  8983. (catch 'ex
  8984. (while t
  8985. (when (setq tmp (org-entry-get nil property))
  8986. (org-back-to-heading t)
  8987. (move-marker org-entry-property-inherited-from (point))
  8988. (throw 'ex tmp))
  8989. (or (org-up-heading-safe) (throw 'ex nil)))))
  8990. (or tmp
  8991. (cdr (assoc property org-file-properties))
  8992. (cdr (assoc property org-global-properties))
  8993. (cdr (assoc property org-global-properties-fixed))))))
  8994. (defun org-entry-put (pom property value)
  8995. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8996. (org-with-point-at pom
  8997. (org-back-to-heading t)
  8998. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8999. range)
  9000. (cond
  9001. ((equal property "TODO")
  9002. (when (and (stringp value) (string-match "\\S-" value)
  9003. (not (member value org-todo-keywords-1)))
  9004. (error "\"%s\" is not a valid TODO state" value))
  9005. (if (or (not value)
  9006. (not (string-match "\\S-" value)))
  9007. (setq value 'none))
  9008. (org-todo value)
  9009. (org-set-tags nil 'align))
  9010. ((equal property "PRIORITY")
  9011. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9012. (string-to-char value) ?\ ))
  9013. (org-set-tags nil 'align))
  9014. ((equal property "SCHEDULED")
  9015. (if (re-search-forward org-scheduled-time-regexp end t)
  9016. (cond
  9017. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9018. ((eq value 'later) (org-timestamp-change 1 'day))
  9019. (t (call-interactively 'org-schedule)))
  9020. (call-interactively 'org-schedule)))
  9021. ((equal property "DEADLINE")
  9022. (if (re-search-forward org-deadline-time-regexp end t)
  9023. (cond
  9024. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9025. ((eq value 'later) (org-timestamp-change 1 'day))
  9026. (t (call-interactively 'org-deadline)))
  9027. (call-interactively 'org-deadline)))
  9028. ((member property org-special-properties)
  9029. (error "The %s property can not yet be set with `org-entry-put'"
  9030. property))
  9031. (t ; a non-special property
  9032. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9033. (setq range (org-get-property-block beg end 'force))
  9034. (goto-char (car range))
  9035. (if (re-search-forward
  9036. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9037. (progn
  9038. (delete-region (match-beginning 1) (match-end 1))
  9039. (goto-char (match-beginning 1)))
  9040. (goto-char (cdr range))
  9041. (insert "\n")
  9042. (backward-char 1)
  9043. (org-indent-line-function)
  9044. (insert ":" property ":"))
  9045. (and value (insert " " value))
  9046. (org-indent-line-function)))))))
  9047. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9048. "Get all property keys in the current buffer.
  9049. With INCLUDE-SPECIALS, also list the special properties that relect things
  9050. like tags and TODO state.
  9051. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9052. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9053. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9054. formats in the current buffer."
  9055. (let (rtn range cfmt cols s p)
  9056. (save-excursion
  9057. (save-restriction
  9058. (widen)
  9059. (goto-char (point-min))
  9060. (while (re-search-forward org-property-start-re nil t)
  9061. (setq range (org-get-property-block))
  9062. (goto-char (car range))
  9063. (while (re-search-forward
  9064. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9065. (cdr range) t)
  9066. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9067. (outline-next-heading))))
  9068. (when include-specials
  9069. (setq rtn (append org-special-properties rtn)))
  9070. (when include-defaults
  9071. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9072. (when include-columns
  9073. (save-excursion
  9074. (save-restriction
  9075. (widen)
  9076. (goto-char (point-min))
  9077. (while (re-search-forward
  9078. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9079. nil t)
  9080. (setq cfmt (match-string 2) s 0)
  9081. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9082. cfmt s)
  9083. (setq s (match-end 0)
  9084. p (match-string 1 cfmt))
  9085. (unless (or (equal p "ITEM")
  9086. (member p org-special-properties))
  9087. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9088. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9089. (defun org-property-values (key)
  9090. "Return a list of all values of property KEY."
  9091. (save-excursion
  9092. (save-restriction
  9093. (widen)
  9094. (goto-char (point-min))
  9095. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9096. values)
  9097. (while (re-search-forward re nil t)
  9098. (add-to-list 'values (org-trim (match-string 1))))
  9099. (delete "" values)))))
  9100. (defun org-insert-property-drawer ()
  9101. "Insert a property drawer into the current entry."
  9102. (interactive)
  9103. (org-back-to-heading t)
  9104. (looking-at outline-regexp)
  9105. (let ((indent (- (match-end 0)(match-beginning 0)))
  9106. (beg (point))
  9107. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9108. end hiddenp)
  9109. (outline-next-heading)
  9110. (setq end (point))
  9111. (goto-char beg)
  9112. (while (re-search-forward re end t))
  9113. (setq hiddenp (org-invisible-p))
  9114. (end-of-line 1)
  9115. (and (equal (char-after) ?\n) (forward-char 1))
  9116. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9117. (beginning-of-line 2))
  9118. (org-skip-over-state-notes)
  9119. (skip-chars-backward " \t\n\r")
  9120. (if (eq (char-before) ?*) (forward-char 1))
  9121. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9122. (beginning-of-line 0)
  9123. (org-indent-to-column indent)
  9124. (beginning-of-line 2)
  9125. (org-indent-to-column indent)
  9126. (beginning-of-line 0)
  9127. (if hiddenp
  9128. (save-excursion
  9129. (org-back-to-heading t)
  9130. (hide-entry))
  9131. (org-flag-drawer t))))
  9132. (defun org-set-property (property value)
  9133. "In the current entry, set PROPERTY to VALUE.
  9134. When called interactively, this will prompt for a property name, offering
  9135. completion on existing and default properties. And then it will prompt
  9136. for a value, offering competion either on allowed values (via an inherited
  9137. xxx_ALL property) or on existing values in other instances of this property
  9138. in the current file."
  9139. (interactive
  9140. (let* ((completion-ignore-case t)
  9141. (keys (org-buffer-property-keys nil t t))
  9142. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9143. (prop (if (member prop0 keys)
  9144. prop0
  9145. (or (cdr (assoc (downcase prop0)
  9146. (mapcar (lambda (x) (cons (downcase x) x))
  9147. keys)))
  9148. prop0)))
  9149. (cur (org-entry-get nil prop))
  9150. (allowed (org-property-get-allowed-values nil prop 'table))
  9151. (existing (mapcar 'list (org-property-values prop)))
  9152. (val (if allowed
  9153. (org-completing-read "Value: " allowed nil 'req-match)
  9154. (org-completing-read
  9155. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9156. (concat "[" cur "]") "")
  9157. ": ")
  9158. existing nil nil "" nil cur))))
  9159. (list prop (if (equal val "") cur val))))
  9160. (unless (equal (org-entry-get nil property) value)
  9161. (org-entry-put nil property value)))
  9162. (defun org-delete-property (property)
  9163. "In the current entry, delete PROPERTY."
  9164. (interactive
  9165. (let* ((completion-ignore-case t)
  9166. (prop (completing-read
  9167. "Property: " (org-entry-properties nil 'standard))))
  9168. (list prop)))
  9169. (message "Property %s %s" property
  9170. (if (org-entry-delete nil property)
  9171. "deleted"
  9172. "was not present in the entry")))
  9173. (defun org-delete-property-globally (property)
  9174. "Remove PROPERTY globally, from all entries."
  9175. (interactive
  9176. (let* ((completion-ignore-case t)
  9177. (prop (completing-read
  9178. "Globally remove property: "
  9179. (mapcar 'list (org-buffer-property-keys)))))
  9180. (list prop)))
  9181. (save-excursion
  9182. (save-restriction
  9183. (widen)
  9184. (goto-char (point-min))
  9185. (let ((cnt 0))
  9186. (while (re-search-forward
  9187. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9188. nil t)
  9189. (setq cnt (1+ cnt))
  9190. (replace-match ""))
  9191. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9192. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9193. (defun org-compute-property-at-point ()
  9194. "Compute the property at point.
  9195. This looks for an enclosing column format, extracts the operator and
  9196. then applies it to the proerty in the column format's scope."
  9197. (interactive)
  9198. (unless (org-at-property-p)
  9199. (error "Not at a property"))
  9200. (let ((prop (org-match-string-no-properties 2)))
  9201. (org-columns-get-format-and-top-level)
  9202. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9203. (error "No operator defined for property %s" prop))
  9204. (org-columns-compute prop)))
  9205. (defun org-property-get-allowed-values (pom property &optional table)
  9206. "Get allowed values for the property PROPERTY.
  9207. When TABLE is non-nil, return an alist that can directly be used for
  9208. completion."
  9209. (let (vals)
  9210. (cond
  9211. ((equal property "TODO")
  9212. (setq vals (org-with-point-at pom
  9213. (append org-todo-keywords-1 '("")))))
  9214. ((equal property "PRIORITY")
  9215. (let ((n org-lowest-priority))
  9216. (while (>= n org-highest-priority)
  9217. (push (char-to-string n) vals)
  9218. (setq n (1- n)))))
  9219. ((member property org-special-properties))
  9220. (t
  9221. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9222. (when (and vals (string-match "\\S-" vals))
  9223. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9224. (setq vals (mapcar (lambda (x)
  9225. (cond ((stringp x) x)
  9226. ((numberp x) (number-to-string x))
  9227. ((symbolp x) (symbol-name x))
  9228. (t "???")))
  9229. vals)))))
  9230. (if table (mapcar 'list vals) vals)))
  9231. (defun org-property-previous-allowed-value (&optional previous)
  9232. "Switch to the next allowed value for this property."
  9233. (interactive)
  9234. (org-property-next-allowed-value t))
  9235. (defun org-property-next-allowed-value (&optional previous)
  9236. "Switch to the next allowed value for this property."
  9237. (interactive)
  9238. (unless (org-at-property-p)
  9239. (error "Not at a property"))
  9240. (let* ((key (match-string 2))
  9241. (value (match-string 3))
  9242. (allowed (or (org-property-get-allowed-values (point) key)
  9243. (and (member value '("[ ]" "[-]" "[X]"))
  9244. '("[ ]" "[X]"))))
  9245. nval)
  9246. (unless allowed
  9247. (error "Allowed values for this property have not been defined"))
  9248. (if previous (setq allowed (reverse allowed)))
  9249. (if (member value allowed)
  9250. (setq nval (car (cdr (member value allowed)))))
  9251. (setq nval (or nval (car allowed)))
  9252. (if (equal nval value)
  9253. (error "Only one allowed value for this property"))
  9254. (org-at-property-p)
  9255. (replace-match (concat " :" key ": " nval) t t)
  9256. (org-indent-line-function)
  9257. (beginning-of-line 1)
  9258. (skip-chars-forward " \t")))
  9259. (defun org-find-entry-with-id (ident)
  9260. "Locate the entry that contains the ID property with exact value IDENT.
  9261. IDENT can be a string, a symbol or a number, this function will search for
  9262. the string representation of it.
  9263. Return the position where this entry starts, or nil if there is no such entry."
  9264. (let ((id (cond
  9265. ((stringp ident) ident)
  9266. ((symbol-name ident) (symbol-name ident))
  9267. ((numberp ident) (number-to-string ident))
  9268. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9269. (case-fold-search nil))
  9270. (save-excursion
  9271. (save-restriction
  9272. (widen)
  9273. (goto-char (point-min))
  9274. (when (re-search-forward
  9275. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9276. nil t)
  9277. (org-back-to-heading)
  9278. (point))))))
  9279. ;;;; Timestamps
  9280. (defvar org-last-changed-timestamp nil)
  9281. (defvar org-last-inserted-timestamp nil
  9282. "The last time stamp inserted with `org-insert-time-stamp'.")
  9283. (defvar org-time-was-given) ; dynamically scoped parameter
  9284. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9285. (defvar org-ts-what) ; dynamically scoped parameter
  9286. (defun org-time-stamp (arg &optional inactive)
  9287. "Prompt for a date/time and insert a time stamp.
  9288. If the user specifies a time like HH:MM, or if this command is called
  9289. with a prefix argument, the time stamp will contain date and time.
  9290. Otherwise, only the date will be included. All parts of a date not
  9291. specified by the user will be filled in from the current date/time.
  9292. So if you press just return without typing anything, the time stamp
  9293. will represent the current date/time. If there is already a timestamp
  9294. at the cursor, it will be modified."
  9295. (interactive "P")
  9296. (let* ((ts nil)
  9297. (default-time
  9298. ;; Default time is either today, or, when entering a range,
  9299. ;; the range start.
  9300. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9301. (save-excursion
  9302. (re-search-backward
  9303. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9304. (- (point) 20) t)))
  9305. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9306. (current-time)))
  9307. (default-input (and ts (org-get-compact-tod ts)))
  9308. org-time-was-given org-end-time-was-given time)
  9309. (cond
  9310. ((and (org-at-timestamp-p t)
  9311. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9312. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9313. (insert "--")
  9314. (setq time (let ((this-command this-command))
  9315. (org-read-date arg 'totime nil nil
  9316. default-time default-input)))
  9317. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9318. ((org-at-timestamp-p t)
  9319. (setq time (let ((this-command this-command))
  9320. (org-read-date arg 'totime nil nil default-time default-input)))
  9321. (when (org-at-timestamp-p t) ; just to get the match data
  9322. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9323. (replace-match "")
  9324. (setq org-last-changed-timestamp
  9325. (org-insert-time-stamp
  9326. time (or org-time-was-given arg)
  9327. inactive nil nil (list org-end-time-was-given))))
  9328. (message "Timestamp updated"))
  9329. (t
  9330. (setq time (let ((this-command this-command))
  9331. (org-read-date arg 'totime nil nil default-time default-input)))
  9332. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9333. nil nil (list org-end-time-was-given))))))
  9334. ;; FIXME: can we use this for something else, like computing time differences?
  9335. (defun org-get-compact-tod (s)
  9336. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9337. (let* ((t1 (match-string 1 s))
  9338. (h1 (string-to-number (match-string 2 s)))
  9339. (m1 (string-to-number (match-string 3 s)))
  9340. (t2 (and (match-end 4) (match-string 5 s)))
  9341. (h2 (and t2 (string-to-number (match-string 6 s))))
  9342. (m2 (and t2 (string-to-number (match-string 7 s))))
  9343. dh dm)
  9344. (if (not t2)
  9345. t1
  9346. (setq dh (- h2 h1) dm (- m2 m1))
  9347. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9348. (concat t1 "+" (number-to-string dh)
  9349. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9350. (defun org-time-stamp-inactive (&optional arg)
  9351. "Insert an inactive time stamp.
  9352. An inactive time stamp is enclosed in square brackets instead of angle
  9353. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9354. does not link to the calendar and cannot be changed with the S-cursor keys.
  9355. So these are more for recording a certain time/date."
  9356. (interactive "P")
  9357. (org-time-stamp arg 'inactive))
  9358. (defvar org-date-ovl (org-make-overlay 1 1))
  9359. (org-overlay-put org-date-ovl 'face 'org-warning)
  9360. (org-detach-overlay org-date-ovl)
  9361. (defvar org-ans1) ; dynamically scoped parameter
  9362. (defvar org-ans2) ; dynamically scoped parameter
  9363. (defvar org-plain-time-of-day-regexp) ; defined below
  9364. (defvar org-overriding-default-time nil) ; dynamically scoped
  9365. (defvar org-read-date-overlay nil)
  9366. (defvar org-dcst nil) ; dynamically scoped
  9367. (defun org-read-date (&optional with-time to-time from-string prompt
  9368. default-time default-input)
  9369. "Read a date, possibly a time, and make things smooth for the user.
  9370. The prompt will suggest to enter an ISO date, but you can also enter anything
  9371. which will at least partially be understood by `parse-time-string'.
  9372. Unrecognized parts of the date will default to the current day, month, year,
  9373. hour and minute. If this command is called to replace a timestamp at point,
  9374. of to enter the second timestamp of a range, the default time is taken from the
  9375. existing stamp. For example,
  9376. 3-2-5 --> 2003-02-05
  9377. feb 15 --> currentyear-02-15
  9378. sep 12 9 --> 2009-09-12
  9379. 12:45 --> today 12:45
  9380. 22 sept 0:34 --> currentyear-09-22 0:34
  9381. 12 --> currentyear-currentmonth-12
  9382. Fri --> nearest Friday (today or later)
  9383. etc.
  9384. Furthermore you can specify a relative date by giving, as the *first* thing
  9385. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9386. change in days weeks, months, years.
  9387. With a single plus or minus, the date is relative to today. With a double
  9388. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9389. +4d --> four days from today
  9390. +4 --> same as above
  9391. +2w --> two weeks from today
  9392. ++5 --> five days from default date
  9393. The function understands only English month and weekday abbreviations,
  9394. but this can be configured with the variables `parse-time-months' and
  9395. `parse-time-weekdays'.
  9396. While prompting, a calendar is popped up - you can also select the
  9397. date with the mouse (button 1). The calendar shows a period of three
  9398. months. To scroll it to other months, use the keys `>' and `<'.
  9399. If you don't like the calendar, turn it off with
  9400. \(setq org-read-date-popup-calendar nil)
  9401. With optional argument TO-TIME, the date will immediately be converted
  9402. to an internal time.
  9403. With an optional argument WITH-TIME, the prompt will suggest to also
  9404. insert a time. Note that when WITH-TIME is not set, you can still
  9405. enter a time, and this function will inform the calling routine about
  9406. this change. The calling routine may then choose to change the format
  9407. used to insert the time stamp into the buffer to include the time.
  9408. With optional argument FROM-STRING, read from this string instead from
  9409. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9410. the time/date that is used for everything that is not specified by the
  9411. user."
  9412. (require 'parse-time)
  9413. (let* ((org-time-stamp-rounding-minutes
  9414. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9415. (org-dcst org-display-custom-times)
  9416. (ct (org-current-time))
  9417. (def (or org-overriding-default-time default-time ct))
  9418. (defdecode (decode-time def))
  9419. (dummy (progn
  9420. (when (< (nth 2 defdecode) org-extend-today-until)
  9421. (setcar (nthcdr 2 defdecode) -1)
  9422. (setcar (nthcdr 1 defdecode) 59)
  9423. (setq def (apply 'encode-time defdecode)
  9424. defdecode (decode-time def)))))
  9425. (calendar-move-hook nil)
  9426. (calendar-view-diary-initially-flag nil)
  9427. (view-diary-entries-initially nil)
  9428. (calendar-view-holidays-initially-flag nil)
  9429. (view-calendar-holidays-initially nil)
  9430. (timestr (format-time-string
  9431. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9432. (prompt (concat (if prompt (concat prompt " ") "")
  9433. (format "Date+time [%s]: " timestr)))
  9434. ans (org-ans0 "") org-ans1 org-ans2 final)
  9435. (cond
  9436. (from-string (setq ans from-string))
  9437. (org-read-date-popup-calendar
  9438. (save-excursion
  9439. (save-window-excursion
  9440. (calendar)
  9441. (calendar-forward-day (- (time-to-days def)
  9442. (calendar-absolute-from-gregorian
  9443. (calendar-current-date))))
  9444. (org-eval-in-calendar nil t)
  9445. (let* ((old-map (current-local-map))
  9446. (map (copy-keymap calendar-mode-map))
  9447. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9448. (org-defkey map (kbd "RET") 'org-calendar-select)
  9449. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9450. 'org-calendar-select-mouse)
  9451. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9452. 'org-calendar-select-mouse)
  9453. (org-defkey minibuffer-local-map [(meta shift left)]
  9454. (lambda () (interactive)
  9455. (org-eval-in-calendar '(calendar-backward-month 1))))
  9456. (org-defkey minibuffer-local-map [(meta shift right)]
  9457. (lambda () (interactive)
  9458. (org-eval-in-calendar '(calendar-forward-month 1))))
  9459. (org-defkey minibuffer-local-map [(meta shift up)]
  9460. (lambda () (interactive)
  9461. (org-eval-in-calendar '(calendar-backward-year 1))))
  9462. (org-defkey minibuffer-local-map [(meta shift down)]
  9463. (lambda () (interactive)
  9464. (org-eval-in-calendar '(calendar-forward-year 1))))
  9465. (org-defkey minibuffer-local-map [(shift up)]
  9466. (lambda () (interactive)
  9467. (org-eval-in-calendar '(calendar-backward-week 1))))
  9468. (org-defkey minibuffer-local-map [(shift down)]
  9469. (lambda () (interactive)
  9470. (org-eval-in-calendar '(calendar-forward-week 1))))
  9471. (org-defkey minibuffer-local-map [(shift left)]
  9472. (lambda () (interactive)
  9473. (org-eval-in-calendar '(calendar-backward-day 1))))
  9474. (org-defkey minibuffer-local-map [(shift right)]
  9475. (lambda () (interactive)
  9476. (org-eval-in-calendar '(calendar-forward-day 1))))
  9477. (org-defkey minibuffer-local-map ">"
  9478. (lambda () (interactive)
  9479. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9480. (org-defkey minibuffer-local-map "<"
  9481. (lambda () (interactive)
  9482. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9483. (unwind-protect
  9484. (progn
  9485. (use-local-map map)
  9486. (add-hook 'post-command-hook 'org-read-date-display)
  9487. (setq org-ans0 (read-string prompt default-input nil nil))
  9488. ;; org-ans0: from prompt
  9489. ;; org-ans1: from mouse click
  9490. ;; org-ans2: from calendar motion
  9491. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9492. (remove-hook 'post-command-hook 'org-read-date-display)
  9493. (use-local-map old-map)
  9494. (when org-read-date-overlay
  9495. (org-delete-overlay org-read-date-overlay)
  9496. (setq org-read-date-overlay nil)))))))
  9497. (t ; Naked prompt only
  9498. (unwind-protect
  9499. (setq ans (read-string prompt default-input nil timestr))
  9500. (when org-read-date-overlay
  9501. (org-delete-overlay org-read-date-overlay)
  9502. (setq org-read-date-overlay nil)))))
  9503. (setq final (org-read-date-analyze ans def defdecode))
  9504. (if to-time
  9505. (apply 'encode-time final)
  9506. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9507. (format "%04d-%02d-%02d %02d:%02d"
  9508. (nth 5 final) (nth 4 final) (nth 3 final)
  9509. (nth 2 final) (nth 1 final))
  9510. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9511. (defvar def)
  9512. (defvar defdecode)
  9513. (defvar with-time)
  9514. (defun org-read-date-display ()
  9515. "Display the currrent date prompt interpretation in the minibuffer."
  9516. (when org-read-date-display-live
  9517. (when org-read-date-overlay
  9518. (org-delete-overlay org-read-date-overlay))
  9519. (let ((p (point)))
  9520. (end-of-line 1)
  9521. (while (not (equal (buffer-substring
  9522. (max (point-min) (- (point) 4)) (point))
  9523. " "))
  9524. (insert " "))
  9525. (goto-char p))
  9526. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9527. " " (or org-ans1 org-ans2)))
  9528. (org-end-time-was-given nil)
  9529. (f (org-read-date-analyze ans def defdecode))
  9530. (fmts (if org-dcst
  9531. org-time-stamp-custom-formats
  9532. org-time-stamp-formats))
  9533. (fmt (if (or with-time
  9534. (and (boundp 'org-time-was-given) org-time-was-given))
  9535. (cdr fmts)
  9536. (car fmts)))
  9537. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9538. (when (and org-end-time-was-given
  9539. (string-match org-plain-time-of-day-regexp txt))
  9540. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9541. org-end-time-was-given
  9542. (substring txt (match-end 0)))))
  9543. (setq org-read-date-overlay
  9544. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9545. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9546. (defun org-read-date-analyze (ans def defdecode)
  9547. "Analyze the combined answer of the date prompt."
  9548. ;; FIXME: cleanup and comment
  9549. (let (delta deltan deltaw deltadef year month day
  9550. hour minute second wday pm h2 m2 tl wday1
  9551. iso-year iso-weekday iso-week iso-year iso-date)
  9552. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9553. (setq ans "+0"))
  9554. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9555. (setq ans (replace-match "" t t ans)
  9556. deltan (car delta)
  9557. deltaw (nth 1 delta)
  9558. deltadef (nth 2 delta)))
  9559. ;; Check if there is an iso week date in there
  9560. ;; If yes, sore the info and ostpone interpreting it until the rest
  9561. ;; of the parsing is done
  9562. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9563. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9564. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9565. iso-week (string-to-number (match-string 2 ans)))
  9566. (setq ans (replace-match "" t t ans)))
  9567. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9568. (when (string-match
  9569. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9570. (setq year (if (match-end 2)
  9571. (string-to-number (match-string 2 ans))
  9572. (string-to-number (format-time-string "%Y")))
  9573. month (string-to-number (match-string 3 ans))
  9574. day (string-to-number (match-string 4 ans)))
  9575. (if (< year 100) (setq year (+ 2000 year)))
  9576. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9577. t nil ans)))
  9578. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9579. ;; If there is a time with am/pm, and *no* time without it, we convert
  9580. ;; so that matching will be successful.
  9581. (loop for i from 1 to 2 do ; twice, for end time as well
  9582. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9583. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9584. (setq hour (string-to-number (match-string 1 ans))
  9585. minute (if (match-end 3)
  9586. (string-to-number (match-string 3 ans))
  9587. 0)
  9588. pm (equal ?p
  9589. (string-to-char (downcase (match-string 4 ans)))))
  9590. (if (and (= hour 12) (not pm))
  9591. (setq hour 0)
  9592. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9593. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9594. t t ans))))
  9595. ;; Check if a time range is given as a duration
  9596. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9597. (setq hour (string-to-number (match-string 1 ans))
  9598. h2 (+ hour (string-to-number (match-string 3 ans)))
  9599. minute (string-to-number (match-string 2 ans))
  9600. m2 (+ minute (if (match-end 5) (string-to-number
  9601. (match-string 5 ans))0)))
  9602. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9603. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9604. t t ans)))
  9605. ;; Check if there is a time range
  9606. (when (boundp 'org-end-time-was-given)
  9607. (setq org-time-was-given nil)
  9608. (when (and (string-match org-plain-time-of-day-regexp ans)
  9609. (match-end 8))
  9610. (setq org-end-time-was-given (match-string 8 ans))
  9611. (setq ans (concat (substring ans 0 (match-beginning 7))
  9612. (substring ans (match-end 7))))))
  9613. (setq tl (parse-time-string ans)
  9614. day (or (nth 3 tl) (nth 3 defdecode))
  9615. month (or (nth 4 tl)
  9616. (if (and org-read-date-prefer-future
  9617. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9618. (1+ (nth 4 defdecode))
  9619. (nth 4 defdecode)))
  9620. year (or (nth 5 tl)
  9621. (if (and org-read-date-prefer-future
  9622. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9623. (1+ (nth 5 defdecode))
  9624. (nth 5 defdecode)))
  9625. hour (or (nth 2 tl) (nth 2 defdecode))
  9626. minute (or (nth 1 tl) (nth 1 defdecode))
  9627. second (or (nth 0 tl) 0)
  9628. wday (nth 6 tl))
  9629. ;; Special date definitions below
  9630. (cond
  9631. (iso-week
  9632. ;; There was an iso week
  9633. (setq year (or iso-year year)
  9634. day (or iso-weekday wday 1)
  9635. wday nil ; to make sure that the trigger below does not match
  9636. iso-date (calendar-gregorian-from-absolute
  9637. (calendar-absolute-from-iso
  9638. (list iso-week day year))))
  9639. ; FIXME: Should we also push ISO weeks into the future?
  9640. ; (when (and org-read-date-prefer-future
  9641. ; (not iso-year)
  9642. ; (< (calendar-absolute-from-gregorian iso-date)
  9643. ; (time-to-days (current-time))))
  9644. ; (setq year (1+ year)
  9645. ; iso-date (calendar-gregorian-from-absolute
  9646. ; (calendar-absolute-from-iso
  9647. ; (list iso-week day year)))))
  9648. (setq month (car iso-date)
  9649. year (nth 2 iso-date)
  9650. day (nth 1 iso-date)))
  9651. (deltan
  9652. (unless deltadef
  9653. (let ((now (decode-time (current-time))))
  9654. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9655. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9656. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9657. ((equal deltaw "m") (setq month (+ month deltan)))
  9658. ((equal deltaw "y") (setq year (+ year deltan)))))
  9659. ((and wday (not (nth 3 tl)))
  9660. ;; Weekday was given, but no day, so pick that day in the week
  9661. ;; on or after the derived date.
  9662. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9663. (unless (equal wday wday1)
  9664. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9665. (if (and (boundp 'org-time-was-given)
  9666. (nth 2 tl))
  9667. (setq org-time-was-given t))
  9668. (if (< year 100) (setq year (+ 2000 year)))
  9669. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9670. (list second minute hour day month year)))
  9671. (defvar parse-time-weekdays)
  9672. (defun org-read-date-get-relative (s today default)
  9673. "Check string S for special relative date string.
  9674. TODAY and DEFAULT are internal times, for today and for a default.
  9675. Return shift list (N what def-flag)
  9676. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9677. N is the number of WHATs to shift.
  9678. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9679. the DEFAULT date rather than TODAY."
  9680. (when (and
  9681. (string-match
  9682. (concat
  9683. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9684. "\\([0-9]+\\)?"
  9685. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9686. "\\([ \t]\\|$\\)") s)
  9687. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9688. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9689. (string-to-char (substring (match-string 1 s) -1))
  9690. ?+))
  9691. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9692. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9693. (what (if (match-end 3) (match-string 3 s) "d"))
  9694. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9695. (date (if rel default today))
  9696. (wday (nth 6 (decode-time date)))
  9697. delta)
  9698. (if wday1
  9699. (progn
  9700. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9701. (if (= dir ?-) (setq delta (- delta 7)))
  9702. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9703. (list delta "d" rel))
  9704. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9705. (defun org-eval-in-calendar (form &optional keepdate)
  9706. "Eval FORM in the calendar window and return to current window.
  9707. Also, store the cursor date in variable org-ans2."
  9708. (let ((sw (selected-window)))
  9709. (select-window (get-buffer-window "*Calendar*"))
  9710. (eval form)
  9711. (when (and (not keepdate) (calendar-cursor-to-date))
  9712. (let* ((date (calendar-cursor-to-date))
  9713. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9714. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9715. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9716. (select-window sw)))
  9717. (defun org-calendar-select ()
  9718. "Return to `org-read-date' with the date currently selected.
  9719. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9720. (interactive)
  9721. (when (calendar-cursor-to-date)
  9722. (let* ((date (calendar-cursor-to-date))
  9723. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9724. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9725. (if (active-minibuffer-window) (exit-minibuffer))))
  9726. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9727. "Insert a date stamp for the date given by the internal TIME.
  9728. WITH-HM means, use the stamp format that includes the time of the day.
  9729. INACTIVE means use square brackets instead of angular ones, so that the
  9730. stamp will not contribute to the agenda.
  9731. PRE and POST are optional strings to be inserted before and after the
  9732. stamp.
  9733. The command returns the inserted time stamp."
  9734. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9735. stamp)
  9736. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9737. (insert-before-markers (or pre ""))
  9738. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9739. (when (listp extra)
  9740. (setq extra (car extra))
  9741. (if (and (stringp extra)
  9742. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9743. (setq extra (format "-%02d:%02d"
  9744. (string-to-number (match-string 1 extra))
  9745. (string-to-number (match-string 2 extra))))
  9746. (setq extra nil)))
  9747. (when extra
  9748. (backward-char 1)
  9749. (insert-before-markers extra)
  9750. (forward-char 1))
  9751. (insert-before-markers (or post ""))
  9752. (setq org-last-inserted-timestamp stamp)))
  9753. (defun org-toggle-time-stamp-overlays ()
  9754. "Toggle the use of custom time stamp formats."
  9755. (interactive)
  9756. (setq org-display-custom-times (not org-display-custom-times))
  9757. (unless org-display-custom-times
  9758. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9759. (while (setq p (next-single-property-change p 'display))
  9760. (if (and (get-text-property p 'display)
  9761. (eq (get-text-property p 'face) 'org-date))
  9762. (remove-text-properties
  9763. p (setq p (next-single-property-change p 'display))
  9764. '(display t))))
  9765. (set-buffer-modified-p bmp)))
  9766. (if (featurep 'xemacs)
  9767. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9768. (org-restart-font-lock)
  9769. (setq org-table-may-need-update t)
  9770. (if org-display-custom-times
  9771. (message "Time stamps are overlayed with custom format")
  9772. (message "Time stamp overlays removed")))
  9773. (defun org-display-custom-time (beg end)
  9774. "Overlay modified time stamp format over timestamp between BEG and END."
  9775. (let* ((ts (buffer-substring beg end))
  9776. t1 w1 with-hm tf time str w2 (off 0))
  9777. (save-match-data
  9778. (setq t1 (org-parse-time-string ts t))
  9779. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9780. (setq off (- (match-end 0) (match-beginning 0)))))
  9781. (setq end (- end off))
  9782. (setq w1 (- end beg)
  9783. with-hm (and (nth 1 t1) (nth 2 t1))
  9784. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9785. time (org-fix-decoded-time t1)
  9786. str (org-add-props
  9787. (format-time-string
  9788. (substring tf 1 -1) (apply 'encode-time time))
  9789. nil 'mouse-face 'highlight)
  9790. w2 (length str))
  9791. (if (not (= w2 w1))
  9792. (add-text-properties (1+ beg) (+ 2 beg)
  9793. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9794. (if (featurep 'xemacs)
  9795. (progn
  9796. (put-text-property beg end 'invisible t)
  9797. (put-text-property beg end 'end-glyph (make-glyph str)))
  9798. (put-text-property beg end 'display str))))
  9799. (defun org-translate-time (string)
  9800. "Translate all timestamps in STRING to custom format.
  9801. But do this only if the variable `org-display-custom-times' is set."
  9802. (when org-display-custom-times
  9803. (save-match-data
  9804. (let* ((start 0)
  9805. (re org-ts-regexp-both)
  9806. t1 with-hm inactive tf time str beg end)
  9807. (while (setq start (string-match re string start))
  9808. (setq beg (match-beginning 0)
  9809. end (match-end 0)
  9810. t1 (save-match-data
  9811. (org-parse-time-string (substring string beg end) t))
  9812. with-hm (and (nth 1 t1) (nth 2 t1))
  9813. inactive (equal (substring string beg (1+ beg)) "[")
  9814. tf (funcall (if with-hm 'cdr 'car)
  9815. org-time-stamp-custom-formats)
  9816. time (org-fix-decoded-time t1)
  9817. str (format-time-string
  9818. (concat
  9819. (if inactive "[" "<") (substring tf 1 -1)
  9820. (if inactive "]" ">"))
  9821. (apply 'encode-time time))
  9822. string (replace-match str t t string)
  9823. start (+ start (length str)))))))
  9824. string)
  9825. (defun org-fix-decoded-time (time)
  9826. "Set 0 instead of nil for the first 6 elements of time.
  9827. Don't touch the rest."
  9828. (let ((n 0))
  9829. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9830. (defun org-days-to-time (timestamp-string)
  9831. "Difference between TIMESTAMP-STRING and now in days."
  9832. (- (time-to-days (org-time-string-to-time timestamp-string))
  9833. (time-to-days (current-time))))
  9834. (defun org-deadline-close (timestamp-string &optional ndays)
  9835. "Is the time in TIMESTAMP-STRING close to the current date?"
  9836. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9837. (and (< (org-days-to-time timestamp-string) ndays)
  9838. (not (org-entry-is-done-p))))
  9839. (defun org-get-wdays (ts)
  9840. "Get the deadline lead time appropriate for timestring TS."
  9841. (cond
  9842. ((<= org-deadline-warning-days 0)
  9843. ;; 0 or negative, enforce this value no matter what
  9844. (- org-deadline-warning-days))
  9845. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9846. ;; lead time is specified.
  9847. (floor (* (string-to-number (match-string 1 ts))
  9848. (cdr (assoc (match-string 2 ts)
  9849. '(("d" . 1) ("w" . 7)
  9850. ("m" . 30.4) ("y" . 365.25)))))))
  9851. ;; go for the default.
  9852. (t org-deadline-warning-days)))
  9853. (defun org-calendar-select-mouse (ev)
  9854. "Return to `org-read-date' with the date currently selected.
  9855. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9856. (interactive "e")
  9857. (mouse-set-point ev)
  9858. (when (calendar-cursor-to-date)
  9859. (let* ((date (calendar-cursor-to-date))
  9860. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9861. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9862. (if (active-minibuffer-window) (exit-minibuffer))))
  9863. (defun org-check-deadlines (ndays)
  9864. "Check if there are any deadlines due or past due.
  9865. A deadline is considered due if it happens within `org-deadline-warning-days'
  9866. days from today's date. If the deadline appears in an entry marked DONE,
  9867. it is not shown. The prefix arg NDAYS can be used to test that many
  9868. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9869. (interactive "P")
  9870. (let* ((org-warn-days
  9871. (cond
  9872. ((equal ndays '(4)) 100000)
  9873. (ndays (prefix-numeric-value ndays))
  9874. (t (abs org-deadline-warning-days))))
  9875. (case-fold-search nil)
  9876. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9877. (callback
  9878. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9879. (message "%d deadlines past-due or due within %d days"
  9880. (org-occur regexp nil callback)
  9881. org-warn-days)))
  9882. (defun org-check-before-date (date)
  9883. "Check if there are deadlines or scheduled entries before DATE."
  9884. (interactive (list (org-read-date)))
  9885. (let ((case-fold-search nil)
  9886. (regexp (concat "\\<\\(" org-deadline-string
  9887. "\\|" org-scheduled-string
  9888. "\\) *<\\([^>]+\\)>"))
  9889. (callback
  9890. (lambda () (time-less-p
  9891. (org-time-string-to-time (match-string 2))
  9892. (org-time-string-to-time date)))))
  9893. (message "%d entries before %s"
  9894. (org-occur regexp nil callback) date)))
  9895. (defun org-evaluate-time-range (&optional to-buffer)
  9896. "Evaluate a time range by computing the difference between start and end.
  9897. Normally the result is just printed in the echo area, but with prefix arg
  9898. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9899. If the time range is actually in a table, the result is inserted into the
  9900. next column.
  9901. For time difference computation, a year is assumed to be exactly 365
  9902. days in order to avoid rounding problems."
  9903. (interactive "P")
  9904. (or
  9905. (org-clock-update-time-maybe)
  9906. (save-excursion
  9907. (unless (org-at-date-range-p t)
  9908. (goto-char (point-at-bol))
  9909. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9910. (if (not (org-at-date-range-p t))
  9911. (error "Not at a time-stamp range, and none found in current line")))
  9912. (let* ((ts1 (match-string 1))
  9913. (ts2 (match-string 2))
  9914. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9915. (match-end (match-end 0))
  9916. (time1 (org-time-string-to-time ts1))
  9917. (time2 (org-time-string-to-time ts2))
  9918. (t1 (time-to-seconds time1))
  9919. (t2 (time-to-seconds time2))
  9920. (diff (abs (- t2 t1)))
  9921. (negative (< (- t2 t1) 0))
  9922. ;; (ys (floor (* 365 24 60 60)))
  9923. (ds (* 24 60 60))
  9924. (hs (* 60 60))
  9925. (fy "%dy %dd %02d:%02d")
  9926. (fy1 "%dy %dd")
  9927. (fd "%dd %02d:%02d")
  9928. (fd1 "%dd")
  9929. (fh "%02d:%02d")
  9930. y d h m align)
  9931. (if havetime
  9932. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9933. y 0
  9934. d (floor (/ diff ds)) diff (mod diff ds)
  9935. h (floor (/ diff hs)) diff (mod diff hs)
  9936. m (floor (/ diff 60)))
  9937. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9938. y 0
  9939. d (floor (+ (/ diff ds) 0.5))
  9940. h 0 m 0))
  9941. (if (not to-buffer)
  9942. (message "%s" (org-make-tdiff-string y d h m))
  9943. (if (org-at-table-p)
  9944. (progn
  9945. (goto-char match-end)
  9946. (setq align t)
  9947. (and (looking-at " *|") (goto-char (match-end 0))))
  9948. (goto-char match-end))
  9949. (if (looking-at
  9950. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9951. (replace-match ""))
  9952. (if negative (insert " -"))
  9953. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9954. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9955. (insert " " (format fh h m))))
  9956. (if align (org-table-align))
  9957. (message "Time difference inserted")))))
  9958. (defun org-make-tdiff-string (y d h m)
  9959. (let ((fmt "")
  9960. (l nil))
  9961. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9962. l (push y l)))
  9963. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9964. l (push d l)))
  9965. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9966. l (push h l)))
  9967. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9968. l (push m l)))
  9969. (apply 'format fmt (nreverse l))))
  9970. (defun org-time-string-to-time (s)
  9971. (apply 'encode-time (org-parse-time-string s)))
  9972. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9973. "Convert a time stamp to an absolute day number.
  9974. If there is a specifyer for a cyclic time stamp, get the closest date to
  9975. DAYNR.
  9976. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9977. (cond
  9978. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9979. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9980. daynr
  9981. (+ daynr 1000)))
  9982. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9983. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9984. (time-to-days (current-time))) (match-string 0 s)
  9985. prefer show-all))
  9986. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9987. (defun org-days-to-iso-week (days)
  9988. "Return the iso week number."
  9989. (require 'cal-iso)
  9990. (car (calendar-iso-from-absolute days)))
  9991. (defun org-small-year-to-year (year)
  9992. "Convert 2-digit years into 4-digit years.
  9993. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9994. The year 2000 cannot be abbreviated. Any year lager than 99
  9995. is retrned unchanged."
  9996. (if (< year 38)
  9997. (setq year (+ 2000 year))
  9998. (if (< year 100)
  9999. (setq year (+ 1900 year))))
  10000. year)
  10001. (defun org-time-from-absolute (d)
  10002. "Return the time corresponding to date D.
  10003. D may be an absolute day number, or a calendar-type list (month day year)."
  10004. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10005. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10006. (defun org-calendar-holiday ()
  10007. "List of holidays, for Diary display in Org-mode."
  10008. (require 'holidays)
  10009. (let ((hl (funcall
  10010. (if (fboundp 'calendar-check-holidays)
  10011. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10012. (if hl (mapconcat 'identity hl "; "))))
  10013. (defun org-diary-sexp-entry (sexp entry date)
  10014. "Process a SEXP diary ENTRY for DATE."
  10015. (require 'diary-lib)
  10016. (let ((result (if calendar-debug-sexp
  10017. (let ((stack-trace-on-error t))
  10018. (eval (car (read-from-string sexp))))
  10019. (condition-case nil
  10020. (eval (car (read-from-string sexp)))
  10021. (error
  10022. (beep)
  10023. (message "Bad sexp at line %d in %s: %s"
  10024. (org-current-line)
  10025. (buffer-file-name) sexp)
  10026. (sleep-for 2))))))
  10027. (cond ((stringp result) result)
  10028. ((and (consp result)
  10029. (stringp (cdr result))) (cdr result))
  10030. (result entry)
  10031. (t nil))))
  10032. (defun org-diary-to-ical-string (frombuf)
  10033. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10034. This uses the icalendar.el library."
  10035. (let* ((tmpdir (if (featurep 'xemacs)
  10036. (temp-directory)
  10037. temporary-file-directory))
  10038. (tmpfile (make-temp-name
  10039. (expand-file-name "orgics" tmpdir)))
  10040. buf rtn b e)
  10041. (save-excursion
  10042. (set-buffer frombuf)
  10043. (icalendar-export-region (point-min) (point-max) tmpfile)
  10044. (setq buf (find-buffer-visiting tmpfile))
  10045. (set-buffer buf)
  10046. (goto-char (point-min))
  10047. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10048. (setq b (match-beginning 0)))
  10049. (goto-char (point-max))
  10050. (if (re-search-backward "^END:VEVENT" nil t)
  10051. (setq e (match-end 0)))
  10052. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10053. (kill-buffer buf)
  10054. (delete-file tmpfile)
  10055. rtn))
  10056. (defun org-closest-date (start current change prefer show-all)
  10057. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10058. When PREFER is `past' return a date that is either CURRENT or past.
  10059. When PREFER is `future', return a date that is either CURRENT or future.
  10060. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10061. ;; Make the proper lists from the dates
  10062. (catch 'exit
  10063. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10064. dn dw sday cday n1 n2
  10065. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10066. (setq start (org-date-to-gregorian start)
  10067. current (org-date-to-gregorian
  10068. (if show-all
  10069. current
  10070. (time-to-days (current-time))))
  10071. sday (calendar-absolute-from-gregorian start)
  10072. cday (calendar-absolute-from-gregorian current))
  10073. (if (<= cday sday) (throw 'exit sday))
  10074. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10075. (setq dn (string-to-number (match-string 1 change))
  10076. dw (cdr (assoc (match-string 2 change) a1)))
  10077. (error "Invalid change specifyer: %s" change))
  10078. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10079. (cond
  10080. ((eq dw 'day)
  10081. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10082. n2 (+ n1 dn)))
  10083. ((eq dw 'year)
  10084. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10085. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10086. (setq date1 (list m d y1)
  10087. n1 (calendar-absolute-from-gregorian date1)
  10088. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10089. n2 (calendar-absolute-from-gregorian date2)))
  10090. ((eq dw 'month)
  10091. ;; approx number of month between the two dates
  10092. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10093. ;; How often does dn fit in there?
  10094. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10095. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10096. m (+ m nm)
  10097. ny (floor (/ m 12))
  10098. y (+ y ny)
  10099. m (- m (* ny 12)))
  10100. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10101. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10102. (setq m2 (+ m dn) y2 y)
  10103. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10104. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10105. (while (<= n2 cday)
  10106. (setq n1 n2 m m2 y y2)
  10107. (setq m2 (+ m dn) y2 y)
  10108. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10109. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10110. (if show-all
  10111. (cond
  10112. ((eq prefer 'past) n1)
  10113. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10114. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10115. (cond
  10116. ((eq prefer 'past) n1)
  10117. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10118. (t (if (= cday n1) n1 n2)))))))
  10119. (defun org-date-to-gregorian (date)
  10120. "Turn any specification of DATE into a gregorian date for the calendar."
  10121. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10122. ((and (listp date) (= (length date) 3)) date)
  10123. ((stringp date)
  10124. (setq date (org-parse-time-string date))
  10125. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10126. ((listp date)
  10127. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10128. (defun org-parse-time-string (s &optional nodefault)
  10129. "Parse the standard Org-mode time string.
  10130. This should be a lot faster than the normal `parse-time-string'.
  10131. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10132. hour and minute fields will be nil if not given."
  10133. (if (string-match org-ts-regexp0 s)
  10134. (list 0
  10135. (if (or (match-beginning 8) (not nodefault))
  10136. (string-to-number (or (match-string 8 s) "0")))
  10137. (if (or (match-beginning 7) (not nodefault))
  10138. (string-to-number (or (match-string 7 s) "0")))
  10139. (string-to-number (match-string 4 s))
  10140. (string-to-number (match-string 3 s))
  10141. (string-to-number (match-string 2 s))
  10142. nil nil nil)
  10143. (make-list 9 0)))
  10144. (defun org-timestamp-up (&optional arg)
  10145. "Increase the date item at the cursor by one.
  10146. If the cursor is on the year, change the year. If it is on the month or
  10147. the day, change that.
  10148. With prefix ARG, change by that many units."
  10149. (interactive "p")
  10150. (org-timestamp-change (prefix-numeric-value arg)))
  10151. (defun org-timestamp-down (&optional arg)
  10152. "Decrease the date item at the cursor by one.
  10153. If the cursor is on the year, change the year. If it is on the month or
  10154. the day, change that.
  10155. With prefix ARG, change by that many units."
  10156. (interactive "p")
  10157. (org-timestamp-change (- (prefix-numeric-value arg))))
  10158. (defun org-timestamp-up-day (&optional arg)
  10159. "Increase the date in the time stamp by one day.
  10160. With prefix ARG, change that many days."
  10161. (interactive "p")
  10162. (if (and (not (org-at-timestamp-p t))
  10163. (org-on-heading-p))
  10164. (org-todo 'up)
  10165. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10166. (defun org-timestamp-down-day (&optional arg)
  10167. "Decrease the date in the time stamp by one day.
  10168. With prefix ARG, change that many days."
  10169. (interactive "p")
  10170. (if (and (not (org-at-timestamp-p t))
  10171. (org-on-heading-p))
  10172. (org-todo 'down)
  10173. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10174. (defun org-at-timestamp-p (&optional inactive-ok)
  10175. "Determine if the cursor is in or at a timestamp."
  10176. (interactive)
  10177. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10178. (pos (point))
  10179. (ans (or (looking-at tsr)
  10180. (save-excursion
  10181. (skip-chars-backward "^[<\n\r\t")
  10182. (if (> (point) (point-min)) (backward-char 1))
  10183. (and (looking-at tsr)
  10184. (> (- (match-end 0) pos) -1))))))
  10185. (and ans
  10186. (boundp 'org-ts-what)
  10187. (setq org-ts-what
  10188. (cond
  10189. ((= pos (match-beginning 0)) 'bracket)
  10190. ((= pos (1- (match-end 0))) 'bracket)
  10191. ((org-pos-in-match-range pos 2) 'year)
  10192. ((org-pos-in-match-range pos 3) 'month)
  10193. ((org-pos-in-match-range pos 7) 'hour)
  10194. ((org-pos-in-match-range pos 8) 'minute)
  10195. ((or (org-pos-in-match-range pos 4)
  10196. (org-pos-in-match-range pos 5)) 'day)
  10197. ((and (> pos (or (match-end 8) (match-end 5)))
  10198. (< pos (match-end 0)))
  10199. (- pos (or (match-end 8) (match-end 5))))
  10200. (t 'day))))
  10201. ans))
  10202. (defun org-toggle-timestamp-type ()
  10203. "Toggle the type (<active> or [inactive]) of a time stamp."
  10204. (interactive)
  10205. (when (org-at-timestamp-p t)
  10206. (save-excursion
  10207. (goto-char (match-beginning 0))
  10208. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10209. (goto-char (1- (match-end 0)))
  10210. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10211. (message "Timestamp is now %sactive"
  10212. (if (equal (char-before) ?>) "in" ""))))
  10213. (defun org-timestamp-change (n &optional what)
  10214. "Change the date in the time stamp at point.
  10215. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10216. `year', `minute', `second'. If WHAT is not given, the cursor position
  10217. in the timestamp determines what will be changed."
  10218. (let ((pos (point))
  10219. with-hm inactive
  10220. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10221. org-ts-what
  10222. extra rem
  10223. ts time time0)
  10224. (if (not (org-at-timestamp-p t))
  10225. (error "Not at a timestamp"))
  10226. (if (and (not what) (eq org-ts-what 'bracket))
  10227. (org-toggle-timestamp-type)
  10228. (if (and (not what) (not (eq org-ts-what 'day))
  10229. org-display-custom-times
  10230. (get-text-property (point) 'display)
  10231. (not (get-text-property (1- (point)) 'display)))
  10232. (setq org-ts-what 'day))
  10233. (setq org-ts-what (or what org-ts-what)
  10234. inactive (= (char-after (match-beginning 0)) ?\[)
  10235. ts (match-string 0))
  10236. (replace-match "")
  10237. (if (string-match
  10238. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10239. ts)
  10240. (setq extra (match-string 1 ts)))
  10241. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10242. (setq with-hm t))
  10243. (setq time0 (org-parse-time-string ts))
  10244. (when (and (eq org-ts-what 'minute)
  10245. (eq current-prefix-arg nil))
  10246. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10247. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10248. (setcar (cdr time0) (+ (nth 1 time0)
  10249. (if (> n 0) (- rem) (- dm rem))))))
  10250. (setq time
  10251. (encode-time (or (car time0) 0)
  10252. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10253. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10254. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10255. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10256. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10257. (nthcdr 6 time0)))
  10258. (when (integerp org-ts-what)
  10259. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10260. (if (eq what 'calendar)
  10261. (let ((cal-date (org-get-date-from-calendar)))
  10262. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10263. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10264. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10265. (setcar time0 (or (car time0) 0))
  10266. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10267. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10268. (setq time (apply 'encode-time time0))))
  10269. (setq org-last-changed-timestamp
  10270. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10271. (org-clock-update-time-maybe)
  10272. (goto-char pos)
  10273. ;; Try to recenter the calendar window, if any
  10274. (if (and org-calendar-follow-timestamp-change
  10275. (get-buffer-window "*Calendar*" t)
  10276. (memq org-ts-what '(day month year)))
  10277. (org-recenter-calendar (time-to-days time))))))
  10278. (defun org-modify-ts-extra (s pos n dm)
  10279. "Change the different parts of the lead-time and repeat fields in timestamp."
  10280. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10281. ng h m new rem)
  10282. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10283. (cond
  10284. ((or (org-pos-in-match-range pos 2)
  10285. (org-pos-in-match-range pos 3))
  10286. (setq m (string-to-number (match-string 3 s))
  10287. h (string-to-number (match-string 2 s)))
  10288. (if (org-pos-in-match-range pos 2)
  10289. (setq h (+ h n))
  10290. (setq n (* dm (org-no-warnings (signum n))))
  10291. (when (not (= 0 (setq rem (% m dm))))
  10292. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10293. (setq m (+ m n)))
  10294. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10295. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10296. (setq h (min 24 (max 0 h)))
  10297. (setq ng 1 new (format "-%02d:%02d" h m)))
  10298. ((org-pos-in-match-range pos 6)
  10299. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10300. ((org-pos-in-match-range pos 5)
  10301. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10302. ((org-pos-in-match-range pos 9)
  10303. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10304. ((org-pos-in-match-range pos 8)
  10305. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10306. (when ng
  10307. (setq s (concat
  10308. (substring s 0 (match-beginning ng))
  10309. new
  10310. (substring s (match-end ng))))))
  10311. s))
  10312. (defun org-recenter-calendar (date)
  10313. "If the calendar is visible, recenter it to DATE."
  10314. (let* ((win (selected-window))
  10315. (cwin (get-buffer-window "*Calendar*" t))
  10316. (calendar-move-hook nil))
  10317. (when cwin
  10318. (select-window cwin)
  10319. (calendar-goto-date (if (listp date) date
  10320. (calendar-gregorian-from-absolute date)))
  10321. (select-window win))))
  10322. (defun org-goto-calendar (&optional arg)
  10323. "Go to the Emacs calendar at the current date.
  10324. If there is a time stamp in the current line, go to that date.
  10325. A prefix ARG can be used to force the current date."
  10326. (interactive "P")
  10327. (let ((tsr org-ts-regexp) diff
  10328. (calendar-move-hook nil)
  10329. (calendar-view-holidays-initially-flag nil)
  10330. (view-calendar-holidays-initially nil)
  10331. (calendar-view-diary-initially-flag nil)
  10332. (view-diary-entries-initially nil))
  10333. (if (or (org-at-timestamp-p)
  10334. (save-excursion
  10335. (beginning-of-line 1)
  10336. (looking-at (concat ".*" tsr))))
  10337. (let ((d1 (time-to-days (current-time)))
  10338. (d2 (time-to-days
  10339. (org-time-string-to-time (match-string 1)))))
  10340. (setq diff (- d2 d1))))
  10341. (calendar)
  10342. (calendar-goto-today)
  10343. (if (and diff (not arg)) (calendar-forward-day diff))))
  10344. (defun org-get-date-from-calendar ()
  10345. "Return a list (month day year) of date at point in calendar."
  10346. (with-current-buffer "*Calendar*"
  10347. (save-match-data
  10348. (calendar-cursor-to-date))))
  10349. (defun org-date-from-calendar ()
  10350. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10351. If there is already a time stamp at the cursor position, update it."
  10352. (interactive)
  10353. (if (org-at-timestamp-p t)
  10354. (org-timestamp-change 0 'calendar)
  10355. (let ((cal-date (org-get-date-from-calendar)))
  10356. (org-insert-time-stamp
  10357. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10358. (defun org-minutes-to-hh:mm-string (m)
  10359. "Compute H:MM from a number of minutes."
  10360. (let ((h (/ m 60)))
  10361. (setq m (- m (* 60 h)))
  10362. (format org-time-clocksum-format h m)))
  10363. (defun org-hh:mm-string-to-minutes (s)
  10364. "Convert a string H:MM to a number of minutes."
  10365. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10366. (+ (* (string-to-number (match-string 1 s)) 60)
  10367. (string-to-number (match-string 2 s)))
  10368. 0))
  10369. ;;;; Agenda files
  10370. ;;;###autoload
  10371. (defun org-iswitchb (&optional arg)
  10372. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10373. With a prefix argument, restrict available to files.
  10374. With two prefix arguments, restrict available buffers to agenda files.
  10375. Due to some yet unresolved reason, the global function
  10376. `iswitchb-mode' needs to be active for this function to work."
  10377. (interactive "P")
  10378. (require 'iswitchb)
  10379. (let ((enabled iswitchb-mode) blist)
  10380. (or enabled (iswitchb-mode 1))
  10381. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10382. ((equal arg '(16)) (org-buffer-list 'agenda))
  10383. (t (org-buffer-list))))
  10384. (unwind-protect
  10385. (let ((iswitchb-make-buflist-hook
  10386. (lambda ()
  10387. (setq iswitchb-temp-buflist
  10388. (mapcar 'buffer-name blist)))))
  10389. (switch-to-buffer
  10390. (iswitchb-read-buffer
  10391. "Switch-to: " nil t))
  10392. (or enabled (iswitchb-mode -1))))))
  10393. (defun org-buffer-list (&optional predicate exclude-tmp)
  10394. "Return a list of Org buffers.
  10395. PREDICATE can be `export', `files' or `agenda'.
  10396. export restrict the list to Export buffers.
  10397. files restrict the list to buffers visiting Org files.
  10398. agenda restrict the list to buffers visiting agenda files.
  10399. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10400. (let* ((bfn nil)
  10401. (agenda-files (and (eq predicate 'agenda)
  10402. (mapcar 'file-truename (org-agenda-files t))))
  10403. (filter
  10404. (cond
  10405. ((eq predicate 'files)
  10406. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10407. ((eq predicate 'export)
  10408. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10409. ((eq predicate 'agenda)
  10410. (lambda (b)
  10411. (with-current-buffer b
  10412. (and (eq major-mode 'org-mode)
  10413. (setq bfn (buffer-file-name b))
  10414. (member (file-truename bfn) agenda-files)))))
  10415. (t (lambda (b) (with-current-buffer b
  10416. (or (eq major-mode 'org-mode)
  10417. (string-match "\*Org .*Export"
  10418. (buffer-name b)))))))))
  10419. (delq nil
  10420. (mapcar
  10421. (lambda(b)
  10422. (if (and (funcall filter b)
  10423. (or (not exclude-tmp)
  10424. (not (string-match "tmp" (buffer-name b)))))
  10425. b
  10426. nil))
  10427. (buffer-list)))))
  10428. (defun org-agenda-files (&optional unrestricted archives)
  10429. "Get the list of agenda files.
  10430. Optional UNRESTRICTED means return the full list even if a restriction
  10431. is currently in place.
  10432. When ARCHIVES is t, include all archive files hat are really being
  10433. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10434. `org-agenda-archives-mode' is t."
  10435. (let ((files
  10436. (cond
  10437. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10438. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10439. ((listp org-agenda-files) org-agenda-files)
  10440. (t (error "Invalid value of `org-agenda-files'")))))
  10441. (setq files (apply 'append
  10442. (mapcar (lambda (f)
  10443. (if (file-directory-p f)
  10444. (directory-files
  10445. f t org-agenda-file-regexp)
  10446. (list f)))
  10447. files)))
  10448. (when org-agenda-skip-unavailable-files
  10449. (setq files (delq nil
  10450. (mapcar (function
  10451. (lambda (file)
  10452. (and (file-readable-p file) file)))
  10453. files))))
  10454. (when (or (eq archives t)
  10455. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10456. (setq files (org-add-archive-files files)))
  10457. files))
  10458. (defun org-edit-agenda-file-list ()
  10459. "Edit the list of agenda files.
  10460. Depending on setup, this either uses customize to edit the variable
  10461. `org-agenda-files', or it visits the file that is holding the list. In the
  10462. latter case, the buffer is set up in a way that saving it automatically kills
  10463. the buffer and restores the previous window configuration."
  10464. (interactive)
  10465. (if (stringp org-agenda-files)
  10466. (let ((cw (current-window-configuration)))
  10467. (find-file org-agenda-files)
  10468. (org-set-local 'org-window-configuration cw)
  10469. (org-add-hook 'after-save-hook
  10470. (lambda ()
  10471. (set-window-configuration
  10472. (prog1 org-window-configuration
  10473. (kill-buffer (current-buffer))))
  10474. (org-install-agenda-files-menu)
  10475. (message "New agenda file list installed"))
  10476. nil 'local)
  10477. (message "%s" (substitute-command-keys
  10478. "Edit list and finish with \\[save-buffer]")))
  10479. (customize-variable 'org-agenda-files)))
  10480. (defun org-store-new-agenda-file-list (list)
  10481. "Set new value for the agenda file list and save it correcly."
  10482. (if (stringp org-agenda-files)
  10483. (let ((f org-agenda-files) b)
  10484. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10485. (with-temp-file f
  10486. (insert (mapconcat 'identity list "\n") "\n")))
  10487. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10488. (setq org-agenda-files list)
  10489. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10490. (defun org-read-agenda-file-list ()
  10491. "Read the list of agenda files from a file."
  10492. (when (file-directory-p org-agenda-files)
  10493. (error "`org-agenda-files' cannot be a single directory"))
  10494. (when (stringp org-agenda-files)
  10495. (with-temp-buffer
  10496. (insert-file-contents org-agenda-files)
  10497. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10498. ;;;###autoload
  10499. (defun org-cycle-agenda-files ()
  10500. "Cycle through the files in `org-agenda-files'.
  10501. If the current buffer visits an agenda file, find the next one in the list.
  10502. If the current buffer does not, find the first agenda file."
  10503. (interactive)
  10504. (let* ((fs (org-agenda-files t))
  10505. (files (append fs (list (car fs))))
  10506. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10507. file)
  10508. (unless files (error "No agenda files"))
  10509. (catch 'exit
  10510. (while (setq file (pop files))
  10511. (if (equal (file-truename file) tcf)
  10512. (when (car files)
  10513. (find-file (car files))
  10514. (throw 'exit t))))
  10515. (find-file (car fs)))
  10516. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10517. (defun org-agenda-file-to-front (&optional to-end)
  10518. "Move/add the current file to the top of the agenda file list.
  10519. If the file is not present in the list, it is added to the front. If it is
  10520. present, it is moved there. With optional argument TO-END, add/move to the
  10521. end of the list."
  10522. (interactive "P")
  10523. (let ((org-agenda-skip-unavailable-files nil)
  10524. (file-alist (mapcar (lambda (x)
  10525. (cons (file-truename x) x))
  10526. (org-agenda-files t)))
  10527. (ctf (file-truename buffer-file-name))
  10528. x had)
  10529. (setq x (assoc ctf file-alist) had x)
  10530. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10531. (if to-end
  10532. (setq file-alist (append (delq x file-alist) (list x)))
  10533. (setq file-alist (cons x (delq x file-alist))))
  10534. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10535. (org-install-agenda-files-menu)
  10536. (message "File %s to %s of agenda file list"
  10537. (if had "moved" "added") (if to-end "end" "front"))))
  10538. (defun org-remove-file (&optional file)
  10539. "Remove current file from the list of files in variable `org-agenda-files'.
  10540. These are the files which are being checked for agenda entries.
  10541. Optional argument FILE means, use this file instead of the current."
  10542. (interactive)
  10543. (let* ((org-agenda-skip-unavailable-files nil)
  10544. (file (or file buffer-file-name))
  10545. (true-file (file-truename file))
  10546. (afile (abbreviate-file-name file))
  10547. (files (delq nil (mapcar
  10548. (lambda (x)
  10549. (if (equal true-file
  10550. (file-truename x))
  10551. nil x))
  10552. (org-agenda-files t)))))
  10553. (if (not (= (length files) (length (org-agenda-files t))))
  10554. (progn
  10555. (org-store-new-agenda-file-list files)
  10556. (org-install-agenda-files-menu)
  10557. (message "Removed file: %s" afile))
  10558. (message "File was not in list: %s (not removed)" afile))))
  10559. (defun org-file-menu-entry (file)
  10560. (vector file (list 'find-file file) t))
  10561. (defun org-check-agenda-file (file)
  10562. "Make sure FILE exists. If not, ask user what to do."
  10563. (when (not (file-exists-p file))
  10564. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10565. (abbreviate-file-name file))
  10566. (let ((r (downcase (read-char-exclusive))))
  10567. (cond
  10568. ((equal r ?r)
  10569. (org-remove-file file)
  10570. (throw 'nextfile t))
  10571. (t (error "Abort"))))))
  10572. (defun org-get-agenda-file-buffer (file)
  10573. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10574. it to the list of buffers which might be released later."
  10575. (let ((buf (org-find-base-buffer-visiting file)))
  10576. (if buf
  10577. buf ; just return it
  10578. ;; Make a new buffer and remember it
  10579. (setq buf (find-file-noselect file))
  10580. (if buf (push buf org-agenda-new-buffers))
  10581. buf)))
  10582. (defun org-release-buffers (blist)
  10583. "Release all buffers in list, asking the user for confirmation when needed.
  10584. When a buffer is unmodified, it is just killed. When modified, it is saved
  10585. \(if the user agrees) and then killed."
  10586. (let (buf file)
  10587. (while (setq buf (pop blist))
  10588. (setq file (buffer-file-name buf))
  10589. (when (and (buffer-modified-p buf)
  10590. file
  10591. (y-or-n-p (format "Save file %s? " file)))
  10592. (with-current-buffer buf (save-buffer)))
  10593. (kill-buffer buf))))
  10594. (defun org-prepare-agenda-buffers (files)
  10595. "Create buffers for all agenda files, protect archived trees and comments."
  10596. (interactive)
  10597. (let ((pa '(:org-archived t))
  10598. (pc '(:org-comment t))
  10599. (pall '(:org-archived t :org-comment t))
  10600. (inhibit-read-only t)
  10601. (rea (concat ":" org-archive-tag ":"))
  10602. bmp file re)
  10603. (save-excursion
  10604. (save-restriction
  10605. (while (setq file (pop files))
  10606. (if (bufferp file)
  10607. (set-buffer file)
  10608. (org-check-agenda-file file)
  10609. (set-buffer (org-get-agenda-file-buffer file)))
  10610. (widen)
  10611. (setq bmp (buffer-modified-p))
  10612. (org-refresh-category-properties)
  10613. (setq org-todo-keywords-for-agenda
  10614. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10615. (setq org-done-keywords-for-agenda
  10616. (append org-done-keywords-for-agenda org-done-keywords))
  10617. (setq org-todo-keyword-alist-for-agenda
  10618. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10619. (setq org-tag-alist-for-agenda
  10620. (append org-tag-alist-for-agenda org-tag-alist))
  10621. (save-excursion
  10622. (remove-text-properties (point-min) (point-max) pall)
  10623. (when org-agenda-skip-archived-trees
  10624. (goto-char (point-min))
  10625. (while (re-search-forward rea nil t)
  10626. (if (org-on-heading-p t)
  10627. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10628. (goto-char (point-min))
  10629. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10630. (while (re-search-forward re nil t)
  10631. (add-text-properties
  10632. (match-beginning 0) (org-end-of-subtree t) pc)))
  10633. (set-buffer-modified-p bmp))))
  10634. (setq org-todo-keyword-alist-for-agenda
  10635. (org-uniquify org-todo-keyword-alist-for-agenda)
  10636. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10637. ;;;; Embedded LaTeX
  10638. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10639. "Keymap for the minor `org-cdlatex-mode'.")
  10640. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10641. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10642. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10643. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10644. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10645. (defvar org-cdlatex-texmathp-advice-is-done nil
  10646. "Flag remembering if we have applied the advice to texmathp already.")
  10647. (define-minor-mode org-cdlatex-mode
  10648. "Toggle the minor `org-cdlatex-mode'.
  10649. This mode supports entering LaTeX environment and math in LaTeX fragments
  10650. in Org-mode.
  10651. \\{org-cdlatex-mode-map}"
  10652. nil " OCDL" nil
  10653. (when org-cdlatex-mode (require 'cdlatex))
  10654. (unless org-cdlatex-texmathp-advice-is-done
  10655. (setq org-cdlatex-texmathp-advice-is-done t)
  10656. (defadvice texmathp (around org-math-always-on activate)
  10657. "Always return t in org-mode buffers.
  10658. This is because we want to insert math symbols without dollars even outside
  10659. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10660. en embedded LaTeX fragement, let texmathp do its job.
  10661. \\[org-cdlatex-mode-map]"
  10662. (interactive)
  10663. (let (p)
  10664. (cond
  10665. ((not (org-mode-p)) ad-do-it)
  10666. ((eq this-command 'cdlatex-math-symbol)
  10667. (setq ad-return-value t
  10668. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10669. (t
  10670. (let ((p (org-inside-LaTeX-fragment-p)))
  10671. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10672. (setq ad-return-value t
  10673. texmathp-why '("Org-mode embedded math" . 0))
  10674. (if p ad-do-it)))))))))
  10675. (defun turn-on-org-cdlatex ()
  10676. "Unconditionally turn on `org-cdlatex-mode'."
  10677. (org-cdlatex-mode 1))
  10678. (defun org-inside-LaTeX-fragment-p ()
  10679. "Test if point is inside a LaTeX fragment.
  10680. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10681. sequence appearing also before point.
  10682. Even though the matchers for math are configurable, this function assumes
  10683. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10684. delimiters are skipped when they have been removed by customization.
  10685. The return value is nil, or a cons cell with the delimiter and
  10686. and the position of this delimiter.
  10687. This function does a reasonably good job, but can locally be fooled by
  10688. for example currency specifications. For example it will assume being in
  10689. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10690. fragments that are properly closed, but during editing, we have to live
  10691. with the uncertainty caused by missing closing delimiters. This function
  10692. looks only before point, not after."
  10693. (catch 'exit
  10694. (let ((pos (point))
  10695. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10696. (lim (progn
  10697. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10698. (point)))
  10699. dd-on str (start 0) m re)
  10700. (goto-char pos)
  10701. (when dodollar
  10702. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10703. re (nth 1 (assoc "$" org-latex-regexps)))
  10704. (while (string-match re str start)
  10705. (cond
  10706. ((= (match-end 0) (length str))
  10707. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10708. ((= (match-end 0) (- (length str) 5))
  10709. (throw 'exit nil))
  10710. (t (setq start (match-end 0))))))
  10711. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10712. (goto-char pos)
  10713. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10714. (and (match-beginning 2) (throw 'exit nil))
  10715. ;; count $$
  10716. (while (re-search-backward "\\$\\$" lim t)
  10717. (setq dd-on (not dd-on)))
  10718. (goto-char pos)
  10719. (if dd-on (cons "$$" m))))))
  10720. (defun org-try-cdlatex-tab ()
  10721. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10722. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10723. - inside a LaTeX fragment, or
  10724. - after the first word in a line, where an abbreviation expansion could
  10725. insert a LaTeX environment."
  10726. (when org-cdlatex-mode
  10727. (cond
  10728. ((save-excursion
  10729. (skip-chars-backward "a-zA-Z0-9*")
  10730. (skip-chars-backward " \t")
  10731. (bolp))
  10732. (cdlatex-tab) t)
  10733. ((org-inside-LaTeX-fragment-p)
  10734. (cdlatex-tab) t)
  10735. (t nil))))
  10736. (defun org-cdlatex-underscore-caret (&optional arg)
  10737. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10738. Revert to the normal definition outside of these fragments."
  10739. (interactive "P")
  10740. (if (org-inside-LaTeX-fragment-p)
  10741. (call-interactively 'cdlatex-sub-superscript)
  10742. (let (org-cdlatex-mode)
  10743. (call-interactively (key-binding (vector last-input-event))))))
  10744. (defun org-cdlatex-math-modify (&optional arg)
  10745. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10746. Revert to the normal definition outside of these fragments."
  10747. (interactive "P")
  10748. (if (org-inside-LaTeX-fragment-p)
  10749. (call-interactively 'cdlatex-math-modify)
  10750. (let (org-cdlatex-mode)
  10751. (call-interactively (key-binding (vector last-input-event))))))
  10752. (defvar org-latex-fragment-image-overlays nil
  10753. "List of overlays carrying the images of latex fragments.")
  10754. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10755. (defun org-remove-latex-fragment-image-overlays ()
  10756. "Remove all overlays with LaTeX fragment images in current buffer."
  10757. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10758. (setq org-latex-fragment-image-overlays nil))
  10759. (defun org-preview-latex-fragment (&optional subtree)
  10760. "Preview the LaTeX fragment at point, or all locally or globally.
  10761. If the cursor is in a LaTeX fragment, create the image and overlay
  10762. it over the source code. If there is no fragment at point, display
  10763. all fragments in the current text, from one headline to the next. With
  10764. prefix SUBTREE, display all fragments in the current subtree. With a
  10765. double prefix `C-u C-u', or when the cursor is before the first headline,
  10766. display all fragments in the buffer.
  10767. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10768. (interactive "P")
  10769. (org-remove-latex-fragment-image-overlays)
  10770. (save-excursion
  10771. (save-restriction
  10772. (let (beg end at msg)
  10773. (cond
  10774. ((or (equal subtree '(16))
  10775. (not (save-excursion
  10776. (re-search-backward (concat "^" outline-regexp) nil t))))
  10777. (setq beg (point-min) end (point-max)
  10778. msg "Creating images for buffer...%s"))
  10779. ((equal subtree '(4))
  10780. (org-back-to-heading)
  10781. (setq beg (point) end (org-end-of-subtree t)
  10782. msg "Creating images for subtree...%s"))
  10783. (t
  10784. (if (setq at (org-inside-LaTeX-fragment-p))
  10785. (goto-char (max (point-min) (- (cdr at) 2)))
  10786. (org-back-to-heading))
  10787. (setq beg (point) end (progn (outline-next-heading) (point))
  10788. msg (if at "Creating image...%s"
  10789. "Creating images for entry...%s"))))
  10790. (message msg "")
  10791. (narrow-to-region beg end)
  10792. (goto-char beg)
  10793. (org-format-latex
  10794. (concat "ltxpng/" (file-name-sans-extension
  10795. (file-name-nondirectory
  10796. buffer-file-name)))
  10797. default-directory 'overlays msg at 'forbuffer)
  10798. (message msg "done. Use `C-c C-c' to remove images.")))))
  10799. (defvar org-latex-regexps
  10800. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10801. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10802. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10803. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10804. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10805. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10806. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10807. "Regular expressions for matching embedded LaTeX.")
  10808. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10809. "Replace LaTeX fragments with links to an image, and produce images."
  10810. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10811. (let* ((prefixnodir (file-name-nondirectory prefix))
  10812. (absprefix (expand-file-name prefix dir))
  10813. (todir (file-name-directory absprefix))
  10814. (opt org-format-latex-options)
  10815. (matchers (plist-get opt :matchers))
  10816. (re-list org-latex-regexps)
  10817. (cnt 0) txt link beg end re e checkdir
  10818. m n block linkfile movefile ov)
  10819. ;; Check if there are old images files with this prefix, and remove them
  10820. (when (file-directory-p todir)
  10821. (mapc 'delete-file
  10822. (directory-files
  10823. todir 'full
  10824. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10825. ;; Check the different regular expressions
  10826. (while (setq e (pop re-list))
  10827. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10828. block (if (nth 3 e) "\n\n" ""))
  10829. (when (member m matchers)
  10830. (goto-char (point-min))
  10831. (while (re-search-forward re nil t)
  10832. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10833. (setq txt (match-string n)
  10834. beg (match-beginning n) end (match-end n)
  10835. cnt (1+ cnt)
  10836. linkfile (format "%s_%04d.png" prefix cnt)
  10837. movefile (format "%s_%04d.png" absprefix cnt)
  10838. link (concat block "[[file:" linkfile "]]" block))
  10839. (if msg (message msg cnt))
  10840. (goto-char beg)
  10841. (unless checkdir ; make sure the directory exists
  10842. (setq checkdir t)
  10843. (or (file-directory-p todir) (make-directory todir)))
  10844. (org-create-formula-image
  10845. txt movefile opt forbuffer)
  10846. (if overlays
  10847. (progn
  10848. (setq ov (org-make-overlay beg end))
  10849. (if (featurep 'xemacs)
  10850. (progn
  10851. (org-overlay-put ov 'invisible t)
  10852. (org-overlay-put
  10853. ov 'end-glyph
  10854. (make-glyph (vector 'png :file movefile))))
  10855. (org-overlay-put
  10856. ov 'display
  10857. (list 'image :type 'png :file movefile :ascent 'center)))
  10858. (push ov org-latex-fragment-image-overlays)
  10859. (goto-char end))
  10860. (delete-region beg end)
  10861. (insert link))))))))
  10862. ;; This function borrows from Ganesh Swami's latex2png.el
  10863. (defun org-create-formula-image (string tofile options buffer)
  10864. (let* ((tmpdir (if (featurep 'xemacs)
  10865. (temp-directory)
  10866. temporary-file-directory))
  10867. (texfilebase (make-temp-name
  10868. (expand-file-name "orgtex" tmpdir)))
  10869. (texfile (concat texfilebase ".tex"))
  10870. (dvifile (concat texfilebase ".dvi"))
  10871. (pngfile (concat texfilebase ".png"))
  10872. (fnh (if (featurep 'xemacs)
  10873. (font-height (get-face-font 'default))
  10874. (face-attribute 'default :height nil)))
  10875. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10876. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10877. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10878. "Black"))
  10879. (bg (or (plist-get options (if buffer :background :html-background))
  10880. "Transparent")))
  10881. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10882. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10883. (with-temp-file texfile
  10884. (insert org-format-latex-header
  10885. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10886. (let ((dir default-directory))
  10887. (condition-case nil
  10888. (progn
  10889. (cd tmpdir)
  10890. (call-process "latex" nil nil nil texfile))
  10891. (error nil))
  10892. (cd dir))
  10893. (if (not (file-exists-p dvifile))
  10894. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10895. (condition-case nil
  10896. (call-process "dvipng" nil nil nil
  10897. "-E" "-fg" fg "-bg" bg
  10898. "-D" dpi
  10899. ;;"-x" scale "-y" scale
  10900. "-T" "tight"
  10901. "-o" pngfile
  10902. dvifile)
  10903. (error nil))
  10904. (if (not (file-exists-p pngfile))
  10905. (progn (message "Failed to create png file from %s" texfile) nil)
  10906. ;; Use the requested file name and clean up
  10907. (copy-file pngfile tofile 'replace)
  10908. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10909. (delete-file (concat texfilebase e)))
  10910. pngfile))))
  10911. (defun org-dvipng-color (attr)
  10912. "Return an rgb color specification for dvipng."
  10913. (apply 'format "rgb %s %s %s"
  10914. (mapcar 'org-normalize-color
  10915. (color-values (face-attribute 'default attr nil)))))
  10916. (defun org-normalize-color (value)
  10917. "Return string to be used as color value for an RGB component."
  10918. (format "%g" (/ value 65535.0)))
  10919. ;;;; Key bindings
  10920. ;; Make `C-c C-x' a prefix key
  10921. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10922. ;; TAB key with modifiers
  10923. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10924. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10925. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10926. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10927. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10928. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10929. ;; The following line is necessary under Suse GNU/Linux
  10930. (unless (featurep 'xemacs)
  10931. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10932. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10933. (define-key org-mode-map [backtab] 'org-shifttab)
  10934. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10935. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10936. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10937. ;; Cursor keys with modifiers
  10938. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10939. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10940. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10941. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10942. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10943. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10944. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10945. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10946. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10947. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10948. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10949. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10950. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10951. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10952. ;;; Extra keys for tty access.
  10953. ;; We only set them when really needed because otherwise the
  10954. ;; menus don't show the simple keys
  10955. (when (or org-use-extra-keys
  10956. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10957. (not window-system))
  10958. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10959. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10960. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10961. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10962. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10963. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10964. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10965. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10966. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10967. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10968. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10969. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10970. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10971. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10972. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10973. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10974. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10975. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10976. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10977. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10978. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10979. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10980. ;; All the other keys
  10981. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10982. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10983. (if (boundp 'narrow-map)
  10984. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  10985. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  10986. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10987. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10988. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10989. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10990. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10991. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10992. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10993. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10994. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10995. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10996. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10997. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10998. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10999. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11000. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11001. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11002. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11003. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11004. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11005. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11006. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11007. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11008. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11009. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11010. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11011. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11012. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11013. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11014. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11015. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11016. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11017. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11018. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11019. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11020. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11021. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11022. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11023. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11024. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11025. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11026. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11027. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11028. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11029. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11030. (org-defkey org-mode-map "\C-m" 'org-return)
  11031. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11032. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11033. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11034. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11035. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11036. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11037. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11038. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11039. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11040. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11041. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11042. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11043. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11044. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11045. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11046. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11047. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11048. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11049. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11050. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11051. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11052. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11053. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11054. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11055. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11056. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11057. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11058. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11059. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11060. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11061. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11062. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11063. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11064. (when (featurep 'xemacs)
  11065. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11066. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11067. (defun org-self-insert-command (N)
  11068. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11069. If the cursor is in a table looking at whitespace, the whitespace is
  11070. overwritten, and the table is not marked as requiring realignment."
  11071. (interactive "p")
  11072. (if (and (org-table-p)
  11073. (progn
  11074. ;; check if we blank the field, and if that triggers align
  11075. (and (featurep 'org-table) org-table-auto-blank-field
  11076. (member last-command
  11077. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11078. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11079. ;; got extra space, this field does not determine column width
  11080. (let (org-table-may-need-update) (org-table-blank-field))
  11081. ;; no extra space, this field may determine column width
  11082. (org-table-blank-field)))
  11083. t)
  11084. (eq N 1)
  11085. (looking-at "[^|\n]* |"))
  11086. (let (org-table-may-need-update)
  11087. (goto-char (1- (match-end 0)))
  11088. (delete-backward-char 1)
  11089. (goto-char (match-beginning 0))
  11090. (self-insert-command N))
  11091. (setq org-table-may-need-update t)
  11092. (self-insert-command N)
  11093. (org-fix-tags-on-the-fly)))
  11094. (defun org-fix-tags-on-the-fly ()
  11095. (when (and (equal (char-after (point-at-bol)) ?*)
  11096. (org-on-heading-p))
  11097. (org-align-tags-here org-tags-column)))
  11098. (defun org-delete-backward-char (N)
  11099. "Like `delete-backward-char', insert whitespace at field end in tables.
  11100. When deleting backwards, in tables this function will insert whitespace in
  11101. front of the next \"|\" separator, to keep the table aligned. The table will
  11102. still be marked for re-alignment if the field did fill the entire column,
  11103. because, in this case the deletion might narrow the column."
  11104. (interactive "p")
  11105. (if (and (org-table-p)
  11106. (eq N 1)
  11107. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11108. (looking-at ".*?|"))
  11109. (let ((pos (point))
  11110. (noalign (looking-at "[^|\n\r]* |"))
  11111. (c org-table-may-need-update))
  11112. (backward-delete-char N)
  11113. (skip-chars-forward "^|")
  11114. (insert " ")
  11115. (goto-char (1- pos))
  11116. ;; noalign: if there were two spaces at the end, this field
  11117. ;; does not determine the width of the column.
  11118. (if noalign (setq org-table-may-need-update c)))
  11119. (backward-delete-char N)
  11120. (org-fix-tags-on-the-fly)))
  11121. (defun org-delete-char (N)
  11122. "Like `delete-char', but insert whitespace at field end in tables.
  11123. When deleting characters, in tables this function will insert whitespace in
  11124. front of the next \"|\" separator, to keep the table aligned. The table will
  11125. still be marked for re-alignment if the field did fill the entire column,
  11126. because, in this case the deletion might narrow the column."
  11127. (interactive "p")
  11128. (if (and (org-table-p)
  11129. (not (bolp))
  11130. (not (= (char-after) ?|))
  11131. (eq N 1))
  11132. (if (looking-at ".*?|")
  11133. (let ((pos (point))
  11134. (noalign (looking-at "[^|\n\r]* |"))
  11135. (c org-table-may-need-update))
  11136. (replace-match (concat
  11137. (substring (match-string 0) 1 -1)
  11138. " |"))
  11139. (goto-char pos)
  11140. ;; noalign: if there were two spaces at the end, this field
  11141. ;; does not determine the width of the column.
  11142. (if noalign (setq org-table-may-need-update c)))
  11143. (delete-char N))
  11144. (delete-char N)
  11145. (org-fix-tags-on-the-fly)))
  11146. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11147. (put 'org-self-insert-command 'delete-selection t)
  11148. (put 'orgtbl-self-insert-command 'delete-selection t)
  11149. (put 'org-delete-char 'delete-selection 'supersede)
  11150. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11151. ;; Make `flyspell-mode' delay after some commands
  11152. (put 'org-self-insert-command 'flyspell-delayed t)
  11153. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11154. (put 'org-delete-char 'flyspell-delayed t)
  11155. (put 'org-delete-backward-char 'flyspell-delayed t)
  11156. ;; Make pabbrev-mode expand after org-mode commands
  11157. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11158. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11159. ;; How to do this: Measure non-white length of current string
  11160. ;; If equal to column width, we should realign.
  11161. (defun org-remap (map &rest commands)
  11162. "In MAP, remap the functions given in COMMANDS.
  11163. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11164. (let (new old)
  11165. (while commands
  11166. (setq old (pop commands) new (pop commands))
  11167. (if (fboundp 'command-remapping)
  11168. (org-defkey map (vector 'remap old) new)
  11169. (substitute-key-definition old new map global-map)))))
  11170. (when (eq org-enable-table-editor 'optimized)
  11171. ;; If the user wants maximum table support, we need to hijack
  11172. ;; some standard editing functions
  11173. (org-remap org-mode-map
  11174. 'self-insert-command 'org-self-insert-command
  11175. 'delete-char 'org-delete-char
  11176. 'delete-backward-char 'org-delete-backward-char)
  11177. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11178. (defun org-shiftcursor-error ()
  11179. "Throw an error because Shift-Cursor command was applied in wrong context."
  11180. (error "This command is active in special context like tables, headlines or timestamps"))
  11181. (defun org-shifttab (&optional arg)
  11182. "Global visibility cycling or move to previous table field.
  11183. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11184. on context.
  11185. See the individual commands for more information."
  11186. (interactive "P")
  11187. (cond
  11188. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11189. ((integerp arg)
  11190. (message "Content view to level: %d" arg)
  11191. (org-content (prefix-numeric-value arg))
  11192. (setq org-cycle-global-status 'overview))
  11193. (t (call-interactively 'org-global-cycle))))
  11194. (defun org-shiftmetaleft ()
  11195. "Promote subtree or delete table column.
  11196. Calls `org-promote-subtree', `org-outdent-item',
  11197. or `org-table-delete-column', depending on context.
  11198. See the individual commands for more information."
  11199. (interactive)
  11200. (cond
  11201. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11202. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11203. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11204. (t (org-shiftcursor-error))))
  11205. (defun org-shiftmetaright ()
  11206. "Demote subtree or insert table column.
  11207. Calls `org-demote-subtree', `org-indent-item',
  11208. or `org-table-insert-column', depending on context.
  11209. See the individual commands for more information."
  11210. (interactive)
  11211. (cond
  11212. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11213. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11214. ((org-at-item-p) (call-interactively 'org-indent-item))
  11215. (t (org-shiftcursor-error))))
  11216. (defun org-shiftmetaup (&optional arg)
  11217. "Move subtree up or kill table row.
  11218. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11219. `org-move-item-up' depending on context. See the individual commands
  11220. for more information."
  11221. (interactive "P")
  11222. (cond
  11223. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11224. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11225. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11226. (t (org-shiftcursor-error))))
  11227. (defun org-shiftmetadown (&optional arg)
  11228. "Move subtree down or insert table row.
  11229. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11230. `org-move-item-down', depending on context. See the individual
  11231. commands for more information."
  11232. (interactive "P")
  11233. (cond
  11234. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11235. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11236. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11237. (t (org-shiftcursor-error))))
  11238. (defun org-metaleft (&optional arg)
  11239. "Promote heading or move table column to left.
  11240. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11241. With no specific context, calls the Emacs default `backward-word'.
  11242. See the individual commands for more information."
  11243. (interactive "P")
  11244. (cond
  11245. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11246. ((or (org-on-heading-p) (org-region-active-p))
  11247. (call-interactively 'org-do-promote))
  11248. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11249. (t (call-interactively 'backward-word))))
  11250. (defun org-metaright (&optional arg)
  11251. "Demote subtree or move table column to right.
  11252. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11253. With no specific context, calls the Emacs default `forward-word'.
  11254. See the individual commands for more information."
  11255. (interactive "P")
  11256. (cond
  11257. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11258. ((or (org-on-heading-p) (org-region-active-p))
  11259. (call-interactively 'org-do-demote))
  11260. ((org-at-item-p) (call-interactively 'org-indent-item))
  11261. (t (call-interactively 'forward-word))))
  11262. (defun org-metaup (&optional arg)
  11263. "Move subtree up or move table row up.
  11264. Calls `org-move-subtree-up' or `org-table-move-row' or
  11265. `org-move-item-up', depending on context. See the individual commands
  11266. for more information."
  11267. (interactive "P")
  11268. (cond
  11269. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11270. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11271. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11272. (t (transpose-lines 1) (beginning-of-line -1))))
  11273. (defun org-metadown (&optional arg)
  11274. "Move subtree down or move table row down.
  11275. Calls `org-move-subtree-down' or `org-table-move-row' or
  11276. `org-move-item-down', depending on context. See the individual
  11277. commands for more information."
  11278. (interactive "P")
  11279. (cond
  11280. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11281. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11282. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11283. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11284. (defun org-shiftup (&optional arg)
  11285. "Increase item in timestamp or increase priority of current headline.
  11286. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11287. depending on context. See the individual commands for more information."
  11288. (interactive "P")
  11289. (cond
  11290. ((org-at-timestamp-p t)
  11291. (call-interactively (if org-edit-timestamp-down-means-later
  11292. 'org-timestamp-down 'org-timestamp-up)))
  11293. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11294. ((org-at-item-p) (call-interactively 'org-previous-item))
  11295. ((org-clocktable-try-shift 'up arg))
  11296. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11297. (defun org-shiftdown (&optional arg)
  11298. "Decrease item in timestamp or decrease priority of current headline.
  11299. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11300. depending on context. See the individual commands for more information."
  11301. (interactive "P")
  11302. (cond
  11303. ((org-at-timestamp-p t)
  11304. (call-interactively (if org-edit-timestamp-down-means-later
  11305. 'org-timestamp-up 'org-timestamp-down)))
  11306. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11307. ((org-clocktable-try-shift 'down arg))
  11308. (t (call-interactively 'org-next-item))))
  11309. (defun org-shiftright (&optional arg)
  11310. "Next TODO keyword or timestamp one day later, depending on context."
  11311. (interactive "P")
  11312. (cond
  11313. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11314. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11315. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11316. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11317. ((org-clocktable-try-shift 'right arg))
  11318. (t (org-shiftcursor-error))))
  11319. (defun org-shiftleft (&optional arg)
  11320. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11321. (interactive "P")
  11322. (cond
  11323. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11324. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11325. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11326. ((org-at-property-p)
  11327. (call-interactively 'org-property-previous-allowed-value))
  11328. ((org-clocktable-try-shift 'left arg))
  11329. (t (org-shiftcursor-error))))
  11330. (defun org-shiftcontrolright ()
  11331. "Switch to next TODO set."
  11332. (interactive)
  11333. (cond
  11334. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11335. (t (org-shiftcursor-error))))
  11336. (defun org-shiftcontrolleft ()
  11337. "Switch to previous TODO set."
  11338. (interactive)
  11339. (cond
  11340. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11341. (t (org-shiftcursor-error))))
  11342. (defun org-ctrl-c-ret ()
  11343. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11344. (interactive)
  11345. (cond
  11346. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11347. (t (call-interactively 'org-insert-heading))))
  11348. (defun org-copy-special ()
  11349. "Copy region in table or copy current subtree.
  11350. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11351. See the individual commands for more information."
  11352. (interactive)
  11353. (call-interactively
  11354. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11355. (defun org-cut-special ()
  11356. "Cut region in table or cut current subtree.
  11357. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11358. See the individual commands for more information."
  11359. (interactive)
  11360. (call-interactively
  11361. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11362. (defun org-paste-special (arg)
  11363. "Paste rectangular region into table, or past subtree relative to level.
  11364. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11365. See the individual commands for more information."
  11366. (interactive "P")
  11367. (if (org-at-table-p)
  11368. (org-table-paste-rectangle)
  11369. (org-paste-subtree arg)))
  11370. (defun org-edit-special ()
  11371. "Call a special editor for the stuff at point.
  11372. When at a table, call the formula editor with `org-table-edit-formulas'.
  11373. When at the first line of an src example, call `org-edit-src-code'.
  11374. When in an #+include line, visit the include file. Otherwise call
  11375. `ffap' to visit the file at point."
  11376. (interactive)
  11377. (cond
  11378. ((org-at-table-p)
  11379. (call-interactively 'org-table-edit-formulas))
  11380. ((save-excursion
  11381. (beginning-of-line 1)
  11382. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11383. (find-file (org-trim (match-string 1))))
  11384. ((org-edit-src-code))
  11385. ((org-edit-fixed-width-region))
  11386. (t (call-interactively 'ffap))))
  11387. (defun org-ctrl-c-ctrl-c (&optional arg)
  11388. "Set tags in headline, or update according to changed information at point.
  11389. This command does many different things, depending on context:
  11390. - If the cursor is in a headline, prompt for tags and insert them
  11391. into the current line, aligned to `org-tags-column'. When called
  11392. with prefix arg, realign all tags in the current buffer.
  11393. - If the cursor is in one of the special #+KEYWORD lines, this
  11394. triggers scanning the buffer for these lines and updating the
  11395. information.
  11396. - If the cursor is inside a table, realign the table. This command
  11397. works even if the automatic table editor has been turned off.
  11398. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11399. the entire table.
  11400. - If the cursor is a the beginning of a dynamic block, update it.
  11401. - If the cursor is inside a table created by the table.el package,
  11402. activate that table.
  11403. - If the current buffer is a remember buffer, close note and file it.
  11404. with a prefix argument, file it without further interaction to the default
  11405. location.
  11406. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11407. links in this buffer.
  11408. - If the cursor is on a numbered item in a plain list, renumber the
  11409. ordered list.
  11410. - If the cursor is on a checkbox, toggle it."
  11411. (interactive "P")
  11412. (let ((org-enable-table-editor t))
  11413. (cond
  11414. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11415. org-occur-highlights
  11416. org-latex-fragment-image-overlays)
  11417. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11418. (org-remove-occur-highlights)
  11419. (org-remove-latex-fragment-image-overlays)
  11420. (message "Temporary highlights/overlays removed from current buffer"))
  11421. ((and (local-variable-p 'org-finish-function (current-buffer))
  11422. (fboundp org-finish-function))
  11423. (funcall org-finish-function))
  11424. ((org-at-property-p)
  11425. (call-interactively 'org-property-action))
  11426. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11427. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11428. ((org-at-table.el-p)
  11429. (require 'table)
  11430. (beginning-of-line 1)
  11431. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11432. (call-interactively 'table-recognize-table))
  11433. ((org-at-table-p)
  11434. (org-table-maybe-eval-formula)
  11435. (if arg
  11436. (call-interactively 'org-table-recalculate)
  11437. (org-table-maybe-recalculate-line))
  11438. (call-interactively 'org-table-align))
  11439. ((org-at-item-checkbox-p)
  11440. (call-interactively 'org-toggle-checkbox))
  11441. ((org-at-item-p)
  11442. (call-interactively 'org-maybe-renumber-ordered-list))
  11443. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11444. ;; Dynamic block
  11445. (beginning-of-line 1)
  11446. (save-excursion (org-update-dblock)))
  11447. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11448. (cond
  11449. ((equal (match-string 1) "TBLFM")
  11450. ;; Recalculate the table before this line
  11451. (save-excursion
  11452. (beginning-of-line 1)
  11453. (skip-chars-backward " \r\n\t")
  11454. (if (org-at-table-p)
  11455. (org-call-with-arg 'org-table-recalculate t))))
  11456. (t
  11457. ; (org-set-regexps-and-options)
  11458. ; (org-restart-font-lock)
  11459. (let ((org-inhibit-startup t)) (org-mode-restart))
  11460. (message "Local setup has been refreshed"))))
  11461. (t (error "C-c C-c can do nothing useful at this location.")))))
  11462. (defun org-mode-restart ()
  11463. "Restart Org-mode, to scan again for special lines.
  11464. Also updates the keyword regular expressions."
  11465. (interactive)
  11466. (org-mode)
  11467. (message "Org-mode restarted"))
  11468. (defun org-kill-note-or-show-branches ()
  11469. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11470. (interactive)
  11471. (if (not org-finish-function)
  11472. (call-interactively 'show-branches)
  11473. (let ((org-note-abort t))
  11474. (funcall org-finish-function))))
  11475. (defun org-return (&optional indent)
  11476. "Goto next table row or insert a newline.
  11477. Calls `org-table-next-row' or `newline', depending on context.
  11478. See the individual commands for more information."
  11479. (interactive)
  11480. (cond
  11481. ((bobp) (if indent (newline-and-indent) (newline)))
  11482. ((and (org-at-heading-p)
  11483. (looking-at
  11484. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11485. (org-show-entry)
  11486. (end-of-line 1)
  11487. (newline))
  11488. ((org-at-table-p)
  11489. (org-table-justify-field-maybe)
  11490. (call-interactively 'org-table-next-row))
  11491. (t (if indent (newline-and-indent) (newline)))))
  11492. (defun org-return-indent ()
  11493. "Goto next table row or insert a newline and indent.
  11494. Calls `org-table-next-row' or `newline-and-indent', depending on
  11495. context. See the individual commands for more information."
  11496. (interactive)
  11497. (org-return t))
  11498. (defun org-ctrl-c-star ()
  11499. "Compute table, or change heading status of lines.
  11500. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11501. depending on context. This will also turn a plain list item or a normal
  11502. line into a subheading."
  11503. (interactive)
  11504. (cond
  11505. ((org-at-table-p)
  11506. (call-interactively 'org-table-recalculate))
  11507. ((org-region-active-p)
  11508. ;; Convert all lines in region to list items
  11509. (call-interactively 'org-toggle-region-headings))
  11510. ((org-on-heading-p)
  11511. (org-toggle-region-headings (point-at-bol)
  11512. (min (1+ (point-at-eol)) (point-max))))
  11513. ((org-at-item-p)
  11514. ;; Convert to heading
  11515. (let ((level (save-match-data
  11516. (save-excursion
  11517. (condition-case nil
  11518. (progn
  11519. (org-back-to-heading t)
  11520. (funcall outline-level))
  11521. (error 0))))))
  11522. (replace-match
  11523. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11524. (t (org-toggle-region-headings (point-at-bol)
  11525. (min (1+ (point-at-eol)) (point-max))))))
  11526. (defun org-ctrl-c-minus ()
  11527. "Insert separator line in table or modify bullet status of line.
  11528. Also turns a plain line or a region of lines into list items.
  11529. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11530. `org-cycle-list-bullet', depending on context."
  11531. (interactive)
  11532. (cond
  11533. ((org-at-table-p)
  11534. (call-interactively 'org-table-insert-hline))
  11535. ((org-on-heading-p)
  11536. ;; Convert to item
  11537. (save-excursion
  11538. (beginning-of-line 1)
  11539. (if (looking-at "\\*+ ")
  11540. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11541. ((org-region-active-p)
  11542. ;; Convert all lines in region to list items
  11543. (call-interactively 'org-toggle-region-items))
  11544. ((org-in-item-p)
  11545. (call-interactively 'org-cycle-list-bullet))
  11546. (t (org-toggle-region-items (point-at-bol)
  11547. (min (1+ (point-at-eol)) (point-max))))))
  11548. (defun org-toggle-region-items (beg end)
  11549. "Convert all lines in region to list items.
  11550. If the first line is already an item, convert all list items in the region
  11551. to normal lines."
  11552. (interactive "r")
  11553. (let (l2 l)
  11554. (save-excursion
  11555. (goto-char end)
  11556. (setq l2 (org-current-line))
  11557. (goto-char beg)
  11558. (beginning-of-line 1)
  11559. (setq l (1- (org-current-line)))
  11560. (if (org-at-item-p)
  11561. ;; We already have items, de-itemize
  11562. (while (< (setq l (1+ l)) l2)
  11563. (when (org-at-item-p)
  11564. (goto-char (match-beginning 2))
  11565. (delete-region (match-beginning 2) (match-end 2))
  11566. (and (looking-at "[ \t]+") (replace-match "")))
  11567. (beginning-of-line 2))
  11568. (while (< (setq l (1+ l)) l2)
  11569. (unless (org-at-item-p)
  11570. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11571. (replace-match "\\1- \\2")))
  11572. (beginning-of-line 2))))))
  11573. (defun org-toggle-region-headings (beg end)
  11574. "Convert all lines in region to list items.
  11575. If the first line is already an item, convert all list items in the region
  11576. to normal lines."
  11577. (interactive "r")
  11578. (let (l2 l)
  11579. (save-excursion
  11580. (goto-char end)
  11581. (setq l2 (org-current-line))
  11582. (goto-char beg)
  11583. (beginning-of-line 1)
  11584. (setq l (1- (org-current-line)))
  11585. (if (org-on-heading-p)
  11586. ;; We already have headlines, de-star them
  11587. (while (< (setq l (1+ l)) l2)
  11588. (when (org-on-heading-p t)
  11589. (and (looking-at outline-regexp) (replace-match "")))
  11590. (beginning-of-line 2))
  11591. (let* ((stars (save-excursion
  11592. (re-search-backward org-complex-heading-regexp nil t)
  11593. (or (match-string 1) "*")))
  11594. (add-stars (if org-odd-levels-only "**" "*"))
  11595. (rpl (concat stars add-stars " \\2")))
  11596. (while (< (setq l (1+ l)) l2)
  11597. (unless (org-on-heading-p)
  11598. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11599. (replace-match rpl)))
  11600. (beginning-of-line 2)))))))
  11601. (defun org-meta-return (&optional arg)
  11602. "Insert a new heading or wrap a region in a table.
  11603. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11604. See the individual commands for more information."
  11605. (interactive "P")
  11606. (cond
  11607. ((org-at-table-p)
  11608. (call-interactively 'org-table-wrap-region))
  11609. (t (call-interactively 'org-insert-heading))))
  11610. ;;; Menu entries
  11611. ;; Define the Org-mode menus
  11612. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11613. '("Tbl"
  11614. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11615. ["Next Field" org-cycle (org-at-table-p)]
  11616. ["Previous Field" org-shifttab (org-at-table-p)]
  11617. ["Next Row" org-return (org-at-table-p)]
  11618. "--"
  11619. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11620. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11621. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11622. "--"
  11623. ("Column"
  11624. ["Move Column Left" org-metaleft (org-at-table-p)]
  11625. ["Move Column Right" org-metaright (org-at-table-p)]
  11626. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11627. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11628. ("Row"
  11629. ["Move Row Up" org-metaup (org-at-table-p)]
  11630. ["Move Row Down" org-metadown (org-at-table-p)]
  11631. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11632. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11633. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11634. "--"
  11635. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11636. ("Rectangle"
  11637. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11638. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11639. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11640. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11641. "--"
  11642. ("Calculate"
  11643. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11644. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11645. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11646. "--"
  11647. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11648. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11649. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11650. "--"
  11651. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11652. "--"
  11653. ["Sum Column/Rectangle" org-table-sum
  11654. (or (org-at-table-p) (org-region-active-p))]
  11655. ["Which Column?" org-table-current-column (org-at-table-p)])
  11656. ["Debug Formulas"
  11657. org-table-toggle-formula-debugger
  11658. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11659. ["Show Col/Row Numbers"
  11660. org-table-toggle-coordinate-overlays
  11661. :style toggle
  11662. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11663. "--"
  11664. ["Create" org-table-create (and (not (org-at-table-p))
  11665. org-enable-table-editor)]
  11666. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11667. ["Import from File" org-table-import (not (org-at-table-p))]
  11668. ["Export to File" org-table-export (org-at-table-p)]
  11669. "--"
  11670. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11671. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11672. '("Org"
  11673. ("Show/Hide"
  11674. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11675. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11676. ["Sparse Tree..." org-sparse-tree t]
  11677. ["Reveal Context" org-reveal t]
  11678. ["Show All" show-all t]
  11679. "--"
  11680. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11681. "--"
  11682. ["New Heading" org-insert-heading t]
  11683. ("Navigate Headings"
  11684. ["Up" outline-up-heading t]
  11685. ["Next" outline-next-visible-heading t]
  11686. ["Previous" outline-previous-visible-heading t]
  11687. ["Next Same Level" outline-forward-same-level t]
  11688. ["Previous Same Level" outline-backward-same-level t]
  11689. "--"
  11690. ["Jump" org-goto t])
  11691. ("Edit Structure"
  11692. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11693. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11694. "--"
  11695. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11696. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11697. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11698. "--"
  11699. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11700. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11701. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11702. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11703. "--"
  11704. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11705. "--"
  11706. ["Convert to odd levels" org-convert-to-odd-levels t]
  11707. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11708. ("Editing"
  11709. ["Emphasis..." org-emphasize t]
  11710. ["Edit Source Example" org-edit-special t])
  11711. ("Archive"
  11712. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11713. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11714. ; :active t :keys "C-u C-c C-x C-a"]
  11715. ["Sparse trees open ARCHIVE trees"
  11716. (setq org-sparse-tree-open-archived-trees
  11717. (not org-sparse-tree-open-archived-trees))
  11718. :style toggle :selected org-sparse-tree-open-archived-trees]
  11719. ["Cycling opens ARCHIVE trees"
  11720. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11721. :style toggle :selected org-cycle-open-archived-trees]
  11722. "--"
  11723. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11724. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11725. ; ["Check and Move Children" (org-archive-subtree '(4))
  11726. ; :active t :keys "C-u C-c C-x C-s"]
  11727. )
  11728. "--"
  11729. ("TODO Lists"
  11730. ["TODO/DONE/-" org-todo t]
  11731. ("Select keyword"
  11732. ["Next keyword" org-shiftright (org-on-heading-p)]
  11733. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11734. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11735. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11736. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11737. ["Show TODO Tree" org-show-todo-tree t]
  11738. ["Global TODO list" org-todo-list t]
  11739. "--"
  11740. ["Set Priority" org-priority t]
  11741. ["Priority Up" org-shiftup t]
  11742. ["Priority Down" org-shiftdown t])
  11743. ("TAGS and Properties"
  11744. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11745. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11746. "--"
  11747. ["Set property" 'org-set-property t]
  11748. ["Column view of properties" org-columns t]
  11749. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11750. ("Dates and Scheduling"
  11751. ["Timestamp" org-time-stamp t]
  11752. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11753. ("Change Date"
  11754. ["1 Day Later" org-shiftright t]
  11755. ["1 Day Earlier" org-shiftleft t]
  11756. ["1 ... Later" org-shiftup t]
  11757. ["1 ... Earlier" org-shiftdown t])
  11758. ["Compute Time Range" org-evaluate-time-range t]
  11759. ["Schedule Item" org-schedule t]
  11760. ["Deadline" org-deadline t]
  11761. "--"
  11762. ["Custom time format" org-toggle-time-stamp-overlays
  11763. :style radio :selected org-display-custom-times]
  11764. "--"
  11765. ["Goto Calendar" org-goto-calendar t]
  11766. ["Date from Calendar" org-date-from-calendar t])
  11767. ("Logging work"
  11768. ["Clock in" org-clock-in t]
  11769. ["Clock out" org-clock-out t]
  11770. ["Clock cancel" org-clock-cancel t]
  11771. ["Goto running clock" org-clock-goto t]
  11772. ["Display times" org-clock-display t]
  11773. ["Create clock table" org-clock-report t]
  11774. "--"
  11775. ["Record DONE time"
  11776. (progn (setq org-log-done (not org-log-done))
  11777. (message "Switching to %s will %s record a timestamp"
  11778. (car org-done-keywords)
  11779. (if org-log-done "automatically" "not")))
  11780. :style toggle :selected org-log-done])
  11781. "--"
  11782. ["Agenda Command..." org-agenda t]
  11783. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11784. ("File List for Agenda")
  11785. ("Special views current file"
  11786. ["TODO Tree" org-show-todo-tree t]
  11787. ["Check Deadlines" org-check-deadlines t]
  11788. ["Timeline" org-timeline t]
  11789. ["Tags Tree" org-tags-sparse-tree t])
  11790. "--"
  11791. ("Hyperlinks"
  11792. ["Store Link (Global)" org-store-link t]
  11793. ["Insert Link" org-insert-link t]
  11794. ["Follow Link" org-open-at-point t]
  11795. "--"
  11796. ["Next link" org-next-link t]
  11797. ["Previous link" org-previous-link t]
  11798. "--"
  11799. ["Descriptive Links"
  11800. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11801. :style radio
  11802. :selected (member '(org-link) buffer-invisibility-spec)]
  11803. ["Literal Links"
  11804. (progn
  11805. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11806. :style radio
  11807. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11808. "--"
  11809. ["Export/Publish..." org-export t]
  11810. ("LaTeX"
  11811. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11812. :selected org-cdlatex-mode]
  11813. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11814. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11815. ["Modify math symbol" org-cdlatex-math-modify
  11816. (org-inside-LaTeX-fragment-p)]
  11817. ["Export LaTeX fragments as images"
  11818. (if (featurep 'org-exp)
  11819. (setq org-export-with-LaTeX-fragments
  11820. (not org-export-with-LaTeX-fragments))
  11821. (require 'org-exp))
  11822. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11823. org-export-with-LaTeX-fragments)])
  11824. "--"
  11825. ("Documentation"
  11826. ["Show Version" org-version t]
  11827. ["Info Documentation" org-info t])
  11828. ("Customize"
  11829. ["Browse Org Group" org-customize t]
  11830. "--"
  11831. ["Expand This Menu" org-create-customize-menu
  11832. (fboundp 'customize-menu-create)])
  11833. "--"
  11834. ["Refresh setup" org-mode-restart t]
  11835. ))
  11836. (defun org-info (&optional node)
  11837. "Read documentation for Org-mode in the info system.
  11838. With optional NODE, go directly to that node."
  11839. (interactive)
  11840. (info (format "(org)%s" (or node ""))))
  11841. (defun org-install-agenda-files-menu ()
  11842. (let ((bl (buffer-list)))
  11843. (save-excursion
  11844. (while bl
  11845. (set-buffer (pop bl))
  11846. (if (org-mode-p) (setq bl nil)))
  11847. (when (org-mode-p)
  11848. (easy-menu-change
  11849. '("Org") "File List for Agenda"
  11850. (append
  11851. (list
  11852. ["Edit File List" (org-edit-agenda-file-list) t]
  11853. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11854. ["Remove Current File from List" org-remove-file t]
  11855. ["Cycle through agenda files" org-cycle-agenda-files t]
  11856. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11857. "--")
  11858. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11859. ;;;; Documentation
  11860. ;;;###autoload
  11861. (defun org-require-autoloaded-modules ()
  11862. (interactive)
  11863. (mapc 'require
  11864. '(org-agenda org-archive org-clock org-colview
  11865. org-exp org-id org-export-latex org-publish
  11866. org-remember org-table)))
  11867. ;;;###autoload
  11868. (defun org-customize ()
  11869. "Call the customize function with org as argument."
  11870. (interactive)
  11871. (org-load-modules-maybe)
  11872. (org-require-autoloaded-modules)
  11873. (customize-browse 'org))
  11874. (defun org-create-customize-menu ()
  11875. "Create a full customization menu for Org-mode, insert it into the menu."
  11876. (interactive)
  11877. (org-load-modules-maybe)
  11878. (org-require-autoloaded-modules)
  11879. (if (fboundp 'customize-menu-create)
  11880. (progn
  11881. (easy-menu-change
  11882. '("Org") "Customize"
  11883. `(["Browse Org group" org-customize t]
  11884. "--"
  11885. ,(customize-menu-create 'org)
  11886. ["Set" Custom-set t]
  11887. ["Save" Custom-save t]
  11888. ["Reset to Current" Custom-reset-current t]
  11889. ["Reset to Saved" Custom-reset-saved t]
  11890. ["Reset to Standard Settings" Custom-reset-standard t]))
  11891. (message "\"Org\"-menu now contains full customization menu"))
  11892. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11893. ;;;; Miscellaneous stuff
  11894. ;;; Generally useful functions
  11895. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11896. "Display the given MESSAGE as a warning."
  11897. (if (fboundp 'display-warning)
  11898. (display-warning 'org message
  11899. (if (featurep 'xemacs)
  11900. 'warning
  11901. :warning))
  11902. (let ((buf (get-buffer-create "*Org warnings*")))
  11903. (with-current-buffer buf
  11904. (goto-char (point-max))
  11905. (insert "Warning (Org): " message)
  11906. (unless (bolp)
  11907. (newline)))
  11908. (display-buffer buf)
  11909. (sit-for 0))))
  11910. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  11911. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  11912. (if (and marker (marker-buffer marker)
  11913. (buffer-live-p (marker-buffer marker)))
  11914. (progn
  11915. (switch-to-buffer (marker-buffer marker))
  11916. (if (or (> marker (point-max)) (< marker (point-min)))
  11917. (widen))
  11918. (goto-char marker))
  11919. (if bookmark
  11920. (bookmark-jump bookmark)
  11921. (error "Cannot find location"))))
  11922. (defun org-quote-csv-field (s)
  11923. "Quote field for inclusion in CSV material."
  11924. (if (string-match "[\",]" s)
  11925. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  11926. s))
  11927. (defun org-plist-delete (plist property)
  11928. "Delete PROPERTY from PLIST.
  11929. This is in contrast to merely setting it to 0."
  11930. (let (p)
  11931. (while plist
  11932. (if (not (eq property (car plist)))
  11933. (setq p (plist-put p (car plist) (nth 1 plist))))
  11934. (setq plist (cddr plist)))
  11935. p))
  11936. (defun org-force-self-insert (N)
  11937. "Needed to enforce self-insert under remapping."
  11938. (interactive "p")
  11939. (self-insert-command N))
  11940. (defun org-string-width (s)
  11941. "Compute width of string, ignoring invisible characters.
  11942. This ignores character with invisibility property `org-link', and also
  11943. characters with property `org-cwidth', because these will become invisible
  11944. upon the next fontification round."
  11945. (let (b l)
  11946. (when (or (eq t buffer-invisibility-spec)
  11947. (assq 'org-link buffer-invisibility-spec))
  11948. (while (setq b (text-property-any 0 (length s)
  11949. 'invisible 'org-link s))
  11950. (setq s (concat (substring s 0 b)
  11951. (substring s (or (next-single-property-change
  11952. b 'invisible s) (length s)))))))
  11953. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11954. (setq s (concat (substring s 0 b)
  11955. (substring s (or (next-single-property-change
  11956. b 'org-cwidth s) (length s))))))
  11957. (setq l (string-width s) b -1)
  11958. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11959. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11960. l))
  11961. (defun org-get-indentation (&optional line)
  11962. "Get the indentation of the current line, interpreting tabs.
  11963. When LINE is given, assume it represents a line and compute its indentation."
  11964. (if line
  11965. (if (string-match "^ *" (org-remove-tabs line))
  11966. (match-end 0))
  11967. (save-excursion
  11968. (beginning-of-line 1)
  11969. (skip-chars-forward " \t")
  11970. (current-column))))
  11971. (defun org-remove-tabs (s &optional width)
  11972. "Replace tabulators in S with spaces.
  11973. Assumes that s is a single line, starting in column 0."
  11974. (setq width (or width tab-width))
  11975. (while (string-match "\t" s)
  11976. (setq s (replace-match
  11977. (make-string
  11978. (- (* width (/ (+ (match-beginning 0) width) width))
  11979. (match-beginning 0)) ?\ )
  11980. t t s)))
  11981. s)
  11982. (defun org-fix-indentation (line ind)
  11983. "Fix indentation in LINE.
  11984. IND is a cons cell with target and minimum indentation.
  11985. If the current indenation in LINE is smaller than the minimum,
  11986. leave it alone. If it is larger than ind, set it to the target."
  11987. (let* ((l (org-remove-tabs line))
  11988. (i (org-get-indentation l))
  11989. (i1 (car ind)) (i2 (cdr ind)))
  11990. (if (>= i i2) (setq l (substring line i2)))
  11991. (if (> i1 0)
  11992. (concat (make-string i1 ?\ ) l)
  11993. l)))
  11994. (defun org-base-buffer (buffer)
  11995. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11996. (if (not buffer)
  11997. buffer
  11998. (or (buffer-base-buffer buffer)
  11999. buffer)))
  12000. (defun org-trim (s)
  12001. "Remove whitespace at beginning and end of string."
  12002. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12003. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12004. s)
  12005. (defun org-wrap (string &optional width lines)
  12006. "Wrap string to either a number of lines, or a width in characters.
  12007. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12008. that costs. If there is a word longer than WIDTH, the text is actually
  12009. wrapped to the length of that word.
  12010. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12011. many lines, whatever width that takes.
  12012. The return value is a list of lines, without newlines at the end."
  12013. (let* ((words (org-split-string string "[ \t\n]+"))
  12014. (maxword (apply 'max (mapcar 'org-string-width words)))
  12015. w ll)
  12016. (cond (width
  12017. (org-do-wrap words (max maxword width)))
  12018. (lines
  12019. (setq w maxword)
  12020. (setq ll (org-do-wrap words maxword))
  12021. (if (<= (length ll) lines)
  12022. ll
  12023. (setq ll words)
  12024. (while (> (length ll) lines)
  12025. (setq w (1+ w))
  12026. (setq ll (org-do-wrap words w)))
  12027. ll))
  12028. (t (error "Cannot wrap this")))))
  12029. (defun org-do-wrap (words width)
  12030. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12031. (let (lines line)
  12032. (while words
  12033. (setq line (pop words))
  12034. (while (and words (< (+ (length line) (length (car words))) width))
  12035. (setq line (concat line " " (pop words))))
  12036. (setq lines (push line lines)))
  12037. (nreverse lines)))
  12038. (defun org-split-string (string &optional separators)
  12039. "Splits STRING into substrings at SEPARATORS.
  12040. No empty strings are returned if there are matches at the beginning
  12041. and end of string."
  12042. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12043. (start 0)
  12044. notfirst
  12045. (list nil))
  12046. (while (and (string-match rexp string
  12047. (if (and notfirst
  12048. (= start (match-beginning 0))
  12049. (< start (length string)))
  12050. (1+ start) start))
  12051. (< (match-beginning 0) (length string)))
  12052. (setq notfirst t)
  12053. (or (eq (match-beginning 0) 0)
  12054. (and (eq (match-beginning 0) (match-end 0))
  12055. (eq (match-beginning 0) start))
  12056. (setq list
  12057. (cons (substring string start (match-beginning 0))
  12058. list)))
  12059. (setq start (match-end 0)))
  12060. (or (eq start (length string))
  12061. (setq list
  12062. (cons (substring string start)
  12063. list)))
  12064. (nreverse list)))
  12065. (defun org-context ()
  12066. "Return a list of contexts of the current cursor position.
  12067. If several contexts apply, all are returned.
  12068. Each context entry is a list with a symbol naming the context, and
  12069. two positions indicating start and end of the context. Possible
  12070. contexts are:
  12071. :headline anywhere in a headline
  12072. :headline-stars on the leading stars in a headline
  12073. :todo-keyword on a TODO keyword (including DONE) in a headline
  12074. :tags on the TAGS in a headline
  12075. :priority on the priority cookie in a headline
  12076. :item on the first line of a plain list item
  12077. :item-bullet on the bullet/number of a plain list item
  12078. :checkbox on the checkbox in a plain list item
  12079. :table in an org-mode table
  12080. :table-special on a special filed in a table
  12081. :table-table in a table.el table
  12082. :link on a hyperlink
  12083. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12084. :target on a <<target>>
  12085. :radio-target on a <<<radio-target>>>
  12086. :latex-fragment on a LaTeX fragment
  12087. :latex-preview on a LaTeX fragment with overlayed preview image
  12088. This function expects the position to be visible because it uses font-lock
  12089. faces as a help to recognize the following contexts: :table-special, :link,
  12090. and :keyword."
  12091. (let* ((f (get-text-property (point) 'face))
  12092. (faces (if (listp f) f (list f)))
  12093. (p (point)) clist o)
  12094. ;; First the large context
  12095. (cond
  12096. ((org-on-heading-p t)
  12097. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12098. (when (progn
  12099. (beginning-of-line 1)
  12100. (looking-at org-todo-line-tags-regexp))
  12101. (push (org-point-in-group p 1 :headline-stars) clist)
  12102. (push (org-point-in-group p 2 :todo-keyword) clist)
  12103. (push (org-point-in-group p 4 :tags) clist))
  12104. (goto-char p)
  12105. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12106. (if (looking-at "\\[#[A-Z0-9]\\]")
  12107. (push (org-point-in-group p 0 :priority) clist)))
  12108. ((org-at-item-p)
  12109. (push (org-point-in-group p 2 :item-bullet) clist)
  12110. (push (list :item (point-at-bol)
  12111. (save-excursion (org-end-of-item) (point)))
  12112. clist)
  12113. (and (org-at-item-checkbox-p)
  12114. (push (org-point-in-group p 0 :checkbox) clist)))
  12115. ((org-at-table-p)
  12116. (push (list :table (org-table-begin) (org-table-end)) clist)
  12117. (if (memq 'org-formula faces)
  12118. (push (list :table-special
  12119. (previous-single-property-change p 'face)
  12120. (next-single-property-change p 'face)) clist)))
  12121. ((org-at-table-p 'any)
  12122. (push (list :table-table) clist)))
  12123. (goto-char p)
  12124. ;; Now the small context
  12125. (cond
  12126. ((org-at-timestamp-p)
  12127. (push (org-point-in-group p 0 :timestamp) clist))
  12128. ((memq 'org-link faces)
  12129. (push (list :link
  12130. (previous-single-property-change p 'face)
  12131. (next-single-property-change p 'face)) clist))
  12132. ((memq 'org-special-keyword faces)
  12133. (push (list :keyword
  12134. (previous-single-property-change p 'face)
  12135. (next-single-property-change p 'face)) clist))
  12136. ((org-on-target-p)
  12137. (push (org-point-in-group p 0 :target) clist)
  12138. (goto-char (1- (match-beginning 0)))
  12139. (if (looking-at org-radio-target-regexp)
  12140. (push (org-point-in-group p 0 :radio-target) clist))
  12141. (goto-char p))
  12142. ((setq o (car (delq nil
  12143. (mapcar
  12144. (lambda (x)
  12145. (if (memq x org-latex-fragment-image-overlays) x))
  12146. (org-overlays-at (point))))))
  12147. (push (list :latex-fragment
  12148. (org-overlay-start o) (org-overlay-end o)) clist)
  12149. (push (list :latex-preview
  12150. (org-overlay-start o) (org-overlay-end o)) clist))
  12151. ((org-inside-LaTeX-fragment-p)
  12152. ;; FIXME: positions wrong.
  12153. (push (list :latex-fragment (point) (point)) clist)))
  12154. (setq clist (nreverse (delq nil clist)))
  12155. clist))
  12156. ;; FIXME: Compare with at-regexp-p Do we need both?
  12157. (defun org-in-regexp (re &optional nlines visually)
  12158. "Check if point is inside a match of regexp.
  12159. Normally only the current line is checked, but you can include NLINES extra
  12160. lines both before and after point into the search.
  12161. If VISUALLY is set, require that the cursor is not after the match but
  12162. really on, so that the block visually is on the match."
  12163. (catch 'exit
  12164. (let ((pos (point))
  12165. (eol (point-at-eol (+ 1 (or nlines 0))))
  12166. (inc (if visually 1 0)))
  12167. (save-excursion
  12168. (beginning-of-line (- 1 (or nlines 0)))
  12169. (while (re-search-forward re eol t)
  12170. (if (and (<= (match-beginning 0) pos)
  12171. (>= (+ inc (match-end 0)) pos))
  12172. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12173. (defun org-at-regexp-p (regexp)
  12174. "Is point inside a match of REGEXP in the current line?"
  12175. (catch 'exit
  12176. (save-excursion
  12177. (let ((pos (point)) (end (point-at-eol)))
  12178. (beginning-of-line 1)
  12179. (while (re-search-forward regexp end t)
  12180. (if (and (<= (match-beginning 0) pos)
  12181. (>= (match-end 0) pos))
  12182. (throw 'exit t)))
  12183. nil))))
  12184. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12185. "Call `multi-occur' with buffers for all agenda files."
  12186. (interactive "sOrg-files matching: \np")
  12187. (let* ((files (org-agenda-files))
  12188. (tnames (mapcar 'file-truename files))
  12189. (extra org-agenda-text-search-extra-files)
  12190. f)
  12191. (when (eq (car extra) 'agenda-archives)
  12192. (setq extra (cdr extra))
  12193. (setq files (org-add-archive-files files)))
  12194. (while (setq f (pop extra))
  12195. (unless (member (file-truename f) tnames)
  12196. (add-to-list 'files f 'append)
  12197. (add-to-list 'tnames (file-truename f) 'append)))
  12198. (multi-occur
  12199. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12200. regexp)))
  12201. (if (boundp 'occur-mode-find-occurrence-hook)
  12202. ;; Emacs 23
  12203. (add-hook 'occur-mode-find-occurrence-hook
  12204. (lambda ()
  12205. (when (org-mode-p)
  12206. (org-reveal))))
  12207. ;; Emacs 22
  12208. (defadvice occur-mode-goto-occurrence
  12209. (after org-occur-reveal activate)
  12210. (and (org-mode-p) (org-reveal)))
  12211. (defadvice occur-mode-goto-occurrence-other-window
  12212. (after org-occur-reveal activate)
  12213. (and (org-mode-p) (org-reveal)))
  12214. (defadvice occur-mode-display-occurrence
  12215. (after org-occur-reveal activate)
  12216. (when (org-mode-p)
  12217. (let ((pos (occur-mode-find-occurrence)))
  12218. (with-current-buffer (marker-buffer pos)
  12219. (save-excursion
  12220. (goto-char pos)
  12221. (org-reveal)))))))
  12222. (defun org-uniquify (list)
  12223. "Remove duplicate elements from LIST."
  12224. (let (res)
  12225. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12226. res))
  12227. (defun org-delete-all (elts list)
  12228. "Remove all elements in ELTS from LIST."
  12229. (while elts
  12230. (setq list (delete (pop elts) list)))
  12231. list)
  12232. (defun org-back-over-empty-lines ()
  12233. "Move backwards over witespace, to the beginning of the first empty line.
  12234. Returns the number of empty lines passed."
  12235. (let ((pos (point)))
  12236. (skip-chars-backward " \t\n\r")
  12237. (beginning-of-line 2)
  12238. (goto-char (min (point) pos))
  12239. (count-lines (point) pos)))
  12240. (defun org-skip-whitespace ()
  12241. (skip-chars-forward " \t\n\r"))
  12242. (defun org-point-in-group (point group &optional context)
  12243. "Check if POINT is in match-group GROUP.
  12244. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12245. match. If the match group does ot exist or point is not inside it,
  12246. return nil."
  12247. (and (match-beginning group)
  12248. (>= point (match-beginning group))
  12249. (<= point (match-end group))
  12250. (if context
  12251. (list context (match-beginning group) (match-end group))
  12252. t)))
  12253. (defun org-switch-to-buffer-other-window (&rest args)
  12254. "Switch to buffer in a second window on the current frame.
  12255. In particular, do not allow pop-up frames."
  12256. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12257. special-display-function)
  12258. (apply 'switch-to-buffer-other-window args)))
  12259. (defun org-combine-plists (&rest plists)
  12260. "Create a single property list from all plists in PLISTS.
  12261. The process starts by copying the first list, and then setting properties
  12262. from the other lists. Settings in the last list are the most significant
  12263. ones and overrule settings in the other lists."
  12264. (let ((rtn (copy-sequence (pop plists)))
  12265. p v ls)
  12266. (while plists
  12267. (setq ls (pop plists))
  12268. (while ls
  12269. (setq p (pop ls) v (pop ls))
  12270. (setq rtn (plist-put rtn p v))))
  12271. rtn))
  12272. (defun org-move-line-down (arg)
  12273. "Move the current line down. With prefix argument, move it past ARG lines."
  12274. (interactive "p")
  12275. (let ((col (current-column))
  12276. beg end pos)
  12277. (beginning-of-line 1) (setq beg (point))
  12278. (beginning-of-line 2) (setq end (point))
  12279. (beginning-of-line (+ 1 arg))
  12280. (setq pos (move-marker (make-marker) (point)))
  12281. (insert (delete-and-extract-region beg end))
  12282. (goto-char pos)
  12283. (org-move-to-column col)))
  12284. (defun org-move-line-up (arg)
  12285. "Move the current line up. With prefix argument, move it past ARG lines."
  12286. (interactive "p")
  12287. (let ((col (current-column))
  12288. beg end pos)
  12289. (beginning-of-line 1) (setq beg (point))
  12290. (beginning-of-line 2) (setq end (point))
  12291. (beginning-of-line (- arg))
  12292. (setq pos (move-marker (make-marker) (point)))
  12293. (insert (delete-and-extract-region beg end))
  12294. (goto-char pos)
  12295. (org-move-to-column col)))
  12296. (defun org-replace-escapes (string table)
  12297. "Replace %-escapes in STRING with values in TABLE.
  12298. TABLE is an association list with keys like \"%a\" and string values.
  12299. The sequences in STRING may contain normal field width and padding information,
  12300. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12301. so values can contain further %-escapes if they are define later in TABLE."
  12302. (let ((case-fold-search nil)
  12303. e re rpl)
  12304. (while (setq e (pop table))
  12305. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12306. (while (string-match re string)
  12307. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12308. (cdr e)))
  12309. (setq string (replace-match rpl t t string))))
  12310. string))
  12311. (defun org-sublist (list start end)
  12312. "Return a section of LIST, from START to END.
  12313. Counting starts at 1."
  12314. (let (rtn (c start))
  12315. (setq list (nthcdr (1- start) list))
  12316. (while (and list (<= c end))
  12317. (push (pop list) rtn)
  12318. (setq c (1+ c)))
  12319. (nreverse rtn)))
  12320. (defun org-find-base-buffer-visiting (file)
  12321. "Like `find-buffer-visiting' but alway return the base buffer and
  12322. not an indirect buffer."
  12323. (let ((buf (find-buffer-visiting file)))
  12324. (if buf
  12325. (or (buffer-base-buffer buf) buf)
  12326. nil)))
  12327. (defun org-image-file-name-regexp ()
  12328. "Return regexp matching the file names of images."
  12329. (if (fboundp 'image-file-name-regexp)
  12330. (image-file-name-regexp)
  12331. (let ((image-file-name-extensions
  12332. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12333. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12334. (concat "\\."
  12335. (regexp-opt (nconc (mapcar 'upcase
  12336. image-file-name-extensions)
  12337. image-file-name-extensions)
  12338. t)
  12339. "\\'"))))
  12340. (defun org-file-image-p (file)
  12341. "Return non-nil if FILE is an image."
  12342. (save-match-data
  12343. (string-match (org-image-file-name-regexp) file)))
  12344. (defun org-get-cursor-date ()
  12345. "Return the date at cursor in as a time.
  12346. This works in the calendar and in the agenda, anywhere else it just
  12347. returns the current time."
  12348. (let (date day defd)
  12349. (cond
  12350. ((eq major-mode 'calendar-mode)
  12351. (setq date (calendar-cursor-to-date)
  12352. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12353. ((eq major-mode 'org-agenda-mode)
  12354. (setq day (get-text-property (point) 'day))
  12355. (if day
  12356. (setq date (calendar-gregorian-from-absolute day)
  12357. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12358. (nth 2 date))))))
  12359. (or defd (current-time))))
  12360. (defvar org-agenda-action-marker (make-marker)
  12361. "Marker pointing to the entry for the next agenda action.")
  12362. (defun org-mark-entry-for-agenda-action ()
  12363. "Mark the current entry as target of an agenda action.
  12364. Agenda actions are actions executed from the agenda with the key `k',
  12365. which make use of the date at the cursor."
  12366. (interactive)
  12367. (move-marker org-agenda-action-marker
  12368. (save-excursion (org-back-to-heading t) (point))
  12369. (current-buffer))
  12370. (message
  12371. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12372. ;;; Paragraph filling stuff.
  12373. ;; We want this to be just right, so use the full arsenal.
  12374. (defun org-indent-line-function ()
  12375. "Indent line like previous, but further if previous was headline or item."
  12376. (interactive)
  12377. (let* ((pos (point))
  12378. (itemp (org-at-item-p))
  12379. column bpos bcol tpos tcol bullet btype bullet-type)
  12380. ;; Find the previous relevant line
  12381. (beginning-of-line 1)
  12382. (cond
  12383. ((looking-at "#") (setq column 0))
  12384. ((looking-at "\\*+ ") (setq column 0))
  12385. (t
  12386. (beginning-of-line 0)
  12387. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12388. (beginning-of-line 0))
  12389. (cond
  12390. ((looking-at "\\*+[ \t]+")
  12391. (if (not org-adapt-indentation)
  12392. (setq column 0)
  12393. (goto-char (match-end 0))
  12394. (setq column (current-column))))
  12395. ((org-in-item-p)
  12396. (org-beginning-of-item)
  12397. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12398. (setq bpos (match-beginning 1) tpos (match-end 0)
  12399. bcol (progn (goto-char bpos) (current-column))
  12400. tcol (progn (goto-char tpos) (current-column))
  12401. bullet (match-string 1)
  12402. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12403. (if (> tcol (+ bcol org-description-max-indent))
  12404. (setq tcol (+ bcol 5)))
  12405. (if (not itemp)
  12406. (setq column tcol)
  12407. (goto-char pos)
  12408. (beginning-of-line 1)
  12409. (if (looking-at "\\S-")
  12410. (progn
  12411. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12412. (setq bullet (match-string 1)
  12413. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12414. (setq column (if (equal btype bullet-type) bcol tcol)))
  12415. (setq column (org-get-indentation)))))
  12416. (t (setq column (org-get-indentation))))))
  12417. (goto-char pos)
  12418. (if (<= (current-column) (current-indentation))
  12419. (org-indent-line-to column)
  12420. (save-excursion (org-indent-line-to column)))
  12421. (setq column (current-column))
  12422. (beginning-of-line 1)
  12423. (if (looking-at
  12424. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12425. (replace-match (concat "\\1" (format org-property-format
  12426. (match-string 2) (match-string 3)))
  12427. t nil))
  12428. (org-move-to-column column)))
  12429. (defun org-set-autofill-regexps ()
  12430. (interactive)
  12431. ;; In the paragraph separator we include headlines, because filling
  12432. ;; text in a line directly attached to a headline would otherwise
  12433. ;; fill the headline as well.
  12434. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12435. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12436. ;; The paragraph starter includes hand-formatted lists.
  12437. (org-set-local 'paragraph-start
  12438. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12439. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12440. ;; But only if the user has not turned off tables or fixed-width regions
  12441. (org-set-local
  12442. 'auto-fill-inhibit-regexp
  12443. (concat "\\*+ \\|#\\+"
  12444. "\\|[ \t]*" org-keyword-time-regexp
  12445. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12446. (concat
  12447. "\\|[ \t]*["
  12448. (if org-enable-table-editor "|" "")
  12449. (if org-enable-fixed-width-editor ":" "")
  12450. "]"))))
  12451. ;; We use our own fill-paragraph function, to make sure that tables
  12452. ;; and fixed-width regions are not wrapped. That function will pass
  12453. ;; through to `fill-paragraph' when appropriate.
  12454. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12455. ; Adaptive filling: To get full control, first make sure that
  12456. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12457. (org-set-local 'adaptive-fill-regexp "\000")
  12458. (org-set-local 'adaptive-fill-function
  12459. 'org-adaptive-fill-function)
  12460. (org-set-local
  12461. 'align-mode-rules-list
  12462. '((org-in-buffer-settings
  12463. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12464. (modes . '(org-mode))))))
  12465. (defun org-fill-paragraph (&optional justify)
  12466. "Re-align a table, pass through to fill-paragraph if no table."
  12467. (let ((table-p (org-at-table-p))
  12468. (table.el-p (org-at-table.el-p)))
  12469. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12470. (save-excursion (goto-char (point-at-bol))
  12471. (looking-at outline-regexp)))
  12472. t) ; skip headlines
  12473. (table.el-p t) ; skip table.el tables
  12474. (table-p (org-table-align) t) ; align org-mode tables
  12475. (t nil)))) ; call paragraph-fill
  12476. ;; For reference, this is the default value of adaptive-fill-regexp
  12477. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12478. (defun org-adaptive-fill-function ()
  12479. "Return a fill prefix for org-mode files.
  12480. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12481. work correctly."
  12482. (cond ((looking-at "#[ \t]+")
  12483. (match-string 0))
  12484. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12485. (save-excursion
  12486. (if (> (match-end 1) (+ (match-beginning 1)
  12487. org-description-max-indent))
  12488. (goto-char (+ (match-beginning 1) 5))
  12489. (goto-char (match-end 0)))
  12490. (make-string (current-column) ?\ )))
  12491. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12492. (save-excursion
  12493. (goto-char (match-end 0))
  12494. (make-string (current-column) ?\ )))
  12495. (t nil)))
  12496. ;;; Other stuff.
  12497. (defun org-toggle-fixed-width-section (arg)
  12498. "Toggle the fixed-width export.
  12499. If there is no active region, the QUOTE keyword at the current headline is
  12500. inserted or removed. When present, it causes the text between this headline
  12501. and the next to be exported as fixed-width text, and unmodified.
  12502. If there is an active region, this command adds or removes a colon as the
  12503. first character of this line. If the first character of a line is a colon,
  12504. this line is also exported in fixed-width font."
  12505. (interactive "P")
  12506. (let* ((cc 0)
  12507. (regionp (org-region-active-p))
  12508. (beg (if regionp (region-beginning) (point)))
  12509. (end (if regionp (region-end)))
  12510. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12511. (case-fold-search nil)
  12512. (re "[ \t]*\\(:\\)")
  12513. off)
  12514. (if regionp
  12515. (save-excursion
  12516. (goto-char beg)
  12517. (setq cc (current-column))
  12518. (beginning-of-line 1)
  12519. (setq off (looking-at re))
  12520. (while (> nlines 0)
  12521. (setq nlines (1- nlines))
  12522. (beginning-of-line 1)
  12523. (cond
  12524. (arg
  12525. (org-move-to-column cc t)
  12526. (insert ":\n")
  12527. (forward-line -1))
  12528. ((and off (looking-at re))
  12529. (replace-match "" t t nil 1))
  12530. ((not off) (org-move-to-column cc t) (insert ":")))
  12531. (forward-line 1)))
  12532. (save-excursion
  12533. (org-back-to-heading)
  12534. (if (looking-at (concat outline-regexp
  12535. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12536. (replace-match "" t t nil 1)
  12537. (if (looking-at outline-regexp)
  12538. (progn
  12539. (goto-char (match-end 0))
  12540. (insert org-quote-string " "))))))))
  12541. ;;;; Functions extending outline functionality
  12542. (defun org-beginning-of-line (&optional arg)
  12543. "Go to the beginning of the current line. If that is invisible, continue
  12544. to a visible line beginning. This makes the function of C-a more intuitive.
  12545. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12546. first attempt, and only move to after the tags when the cursor is already
  12547. beyond the end of the headline."
  12548. (interactive "P")
  12549. (let ((pos (point)) refpos)
  12550. (beginning-of-line 1)
  12551. (if (bobp)
  12552. nil
  12553. (backward-char 1)
  12554. (if (org-invisible-p)
  12555. (while (and (not (bobp)) (org-invisible-p))
  12556. (backward-char 1)
  12557. (beginning-of-line 1))
  12558. (forward-char 1)))
  12559. (when org-special-ctrl-a/e
  12560. (cond
  12561. ((and (looking-at org-complex-heading-regexp)
  12562. (= (char-after (match-end 1)) ?\ ))
  12563. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12564. (point-at-eol)))
  12565. (goto-char
  12566. (if (eq org-special-ctrl-a/e t)
  12567. (cond ((> pos refpos) refpos)
  12568. ((= pos (point)) refpos)
  12569. (t (point)))
  12570. (cond ((> pos (point)) (point))
  12571. ((not (eq last-command this-command)) (point))
  12572. (t refpos)))))
  12573. ((org-at-item-p)
  12574. (goto-char
  12575. (if (eq org-special-ctrl-a/e t)
  12576. (cond ((> pos (match-end 4)) (match-end 4))
  12577. ((= pos (point)) (match-end 4))
  12578. (t (point)))
  12579. (cond ((> pos (point)) (point))
  12580. ((not (eq last-command this-command)) (point))
  12581. (t (match-end 4))))))))
  12582. (org-no-warnings
  12583. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12584. (defun org-end-of-line (&optional arg)
  12585. "Go to the end of the line.
  12586. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12587. first attempt, and only move to after the tags when the cursor is already
  12588. beyond the end of the headline."
  12589. (interactive "P")
  12590. (if (or (not org-special-ctrl-a/e)
  12591. (not (org-on-heading-p)))
  12592. (end-of-line arg)
  12593. (let ((pos (point)))
  12594. (beginning-of-line 1)
  12595. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12596. (if (eq org-special-ctrl-a/e t)
  12597. (if (or (< pos (match-beginning 1))
  12598. (= pos (match-end 0)))
  12599. (goto-char (match-beginning 1))
  12600. (goto-char (match-end 0)))
  12601. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12602. (goto-char (match-end 0))
  12603. (goto-char (match-beginning 1))))
  12604. (end-of-line arg))))
  12605. (org-no-warnings
  12606. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12607. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12608. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12609. (defun org-kill-line (&optional arg)
  12610. "Kill line, to tags or end of line."
  12611. (interactive "P")
  12612. (cond
  12613. ((or (not org-special-ctrl-k)
  12614. (bolp)
  12615. (not (org-on-heading-p)))
  12616. (call-interactively 'kill-line))
  12617. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12618. (kill-region (point) (match-beginning 1))
  12619. (org-set-tags nil t))
  12620. (t (kill-region (point) (point-at-eol)))))
  12621. (define-key org-mode-map "\C-k" 'org-kill-line)
  12622. (defun org-yank ()
  12623. "Yank, and if the yanked text is a single subtree, fold it.
  12624. In fact, if the yanked text is a sequence of subtrees, fold all of them."
  12625. (interactive)
  12626. (if org-yank-folded-subtrees
  12627. (let ((beg (point)) end)
  12628. (call-interactively 'yank)
  12629. (setq end (point))
  12630. (goto-char beg)
  12631. (when (and (bolp)
  12632. (org-kill-is-subtree-p))
  12633. (or (looking-at outline-regexp)
  12634. (re-search-forward (concat "^" outline-regexp) end t))
  12635. (while (and (< (point) end) (looking-at outline-regexp))
  12636. (hide-subtree)
  12637. (org-cycle-show-empty-lines 'folded)
  12638. (condition-case nil
  12639. (outline-forward-same-level 1)
  12640. (error (goto-char end)))))
  12641. (goto-char end)
  12642. (skip-chars-forward " \t\n\r"))
  12643. (call-interactively 'yank)))
  12644. (define-key org-mode-map "\C-y" 'org-yank)
  12645. (defun org-invisible-p ()
  12646. "Check if point is at a character currently not visible."
  12647. ;; Early versions of noutline don't have `outline-invisible-p'.
  12648. (if (fboundp 'outline-invisible-p)
  12649. (outline-invisible-p)
  12650. (get-char-property (point) 'invisible)))
  12651. (defun org-invisible-p2 ()
  12652. "Check if point is at a character currently not visible."
  12653. (save-excursion
  12654. (if (and (eolp) (not (bobp))) (backward-char 1))
  12655. ;; Early versions of noutline don't have `outline-invisible-p'.
  12656. (if (fboundp 'outline-invisible-p)
  12657. (outline-invisible-p)
  12658. (get-char-property (point) 'invisible))))
  12659. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12660. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12661. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12662. (defun org-at-heading-or-item-p ()
  12663. (or (org-on-heading-p) (org-at-item-p)))
  12664. (defun org-on-target-p ()
  12665. (or (org-in-regexp org-radio-target-regexp)
  12666. (org-in-regexp org-target-regexp)))
  12667. (defun org-up-heading-all (arg)
  12668. "Move to the heading line of which the present line is a subheading.
  12669. This function considers both visible and invisible heading lines.
  12670. With argument, move up ARG levels."
  12671. (if (fboundp 'outline-up-heading-all)
  12672. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12673. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12674. (defun org-up-heading-safe ()
  12675. "Move to the heading line of which the present line is a subheading.
  12676. This version will not throw an error. It will return the level of the
  12677. headline found, or nil if no higher level is found."
  12678. (let ((pos (point)) start-level level
  12679. (re (concat "^" outline-regexp)))
  12680. (catch 'exit
  12681. (outline-back-to-heading t)
  12682. (setq start-level (funcall outline-level))
  12683. (if (equal start-level 1) (throw 'exit nil))
  12684. (while (re-search-backward re nil t)
  12685. (setq level (funcall outline-level))
  12686. (if (< level start-level) (throw 'exit level)))
  12687. nil)))
  12688. (defun org-first-sibling-p ()
  12689. "Is this heading the first child of its parents?"
  12690. (interactive)
  12691. (let ((re (concat "^" outline-regexp))
  12692. level l)
  12693. (unless (org-at-heading-p t)
  12694. (error "Not at a heading"))
  12695. (setq level (funcall outline-level))
  12696. (save-excursion
  12697. (if (not (re-search-backward re nil t))
  12698. t
  12699. (setq l (funcall outline-level))
  12700. (< l level)))))
  12701. (defun org-goto-sibling (&optional previous)
  12702. "Goto the next sibling, even if it is invisible.
  12703. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12704. when a sibling was found. When none is found, return nil and don't
  12705. move point."
  12706. (let ((fun (if previous 're-search-backward 're-search-forward))
  12707. (pos (point))
  12708. (re (concat "^" outline-regexp))
  12709. level l)
  12710. (when (condition-case nil (org-back-to-heading t) (error nil))
  12711. (setq level (funcall outline-level))
  12712. (catch 'exit
  12713. (or previous (forward-char 1))
  12714. (while (funcall fun re nil t)
  12715. (setq l (funcall outline-level))
  12716. (when (< l level) (goto-char pos) (throw 'exit nil))
  12717. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12718. (goto-char pos)
  12719. nil))))
  12720. (defun org-show-siblings ()
  12721. "Show all siblings of the current headline."
  12722. (save-excursion
  12723. (while (org-goto-sibling) (org-flag-heading nil)))
  12724. (save-excursion
  12725. (while (org-goto-sibling 'previous)
  12726. (org-flag-heading nil))))
  12727. (defun org-show-hidden-entry ()
  12728. "Show an entry where even the heading is hidden."
  12729. (save-excursion
  12730. (org-show-entry)))
  12731. (defun org-flag-heading (flag &optional entry)
  12732. "Flag the current heading. FLAG non-nil means make invisible.
  12733. When ENTRY is non-nil, show the entire entry."
  12734. (save-excursion
  12735. (org-back-to-heading t)
  12736. ;; Check if we should show the entire entry
  12737. (if entry
  12738. (progn
  12739. (org-show-entry)
  12740. (save-excursion
  12741. (and (outline-next-heading)
  12742. (org-flag-heading nil))))
  12743. (outline-flag-region (max (point-min) (1- (point)))
  12744. (save-excursion (outline-end-of-heading) (point))
  12745. flag))))
  12746. (defun org-forward-same-level (arg)
  12747. "Move forward to the ARG'th subheading at same level as this one.
  12748. Stop at the first and last subheadings of a superior heading.
  12749. This is like outline-forward-same-level, but invisible headings are ok."
  12750. (interactive "p")
  12751. (outline-back-to-heading t)
  12752. (while (> arg 0)
  12753. (let ((point-to-move-to (save-excursion
  12754. (org-get-next-sibling))))
  12755. (if point-to-move-to
  12756. (progn
  12757. (goto-char point-to-move-to)
  12758. (setq arg (1- arg)))
  12759. (progn
  12760. (setq arg 0)
  12761. (error "No following same-level heading"))))))
  12762. (defun org-get-next-sibling ()
  12763. "Move to next heading of the same level, and return point.
  12764. If there is no such heading, return nil.
  12765. This is like outline-next-sibling, but invisible headings are ok."
  12766. (let ((level (funcall outline-level)))
  12767. (outline-next-heading)
  12768. (while (and (not (eobp)) (> (funcall outline-level) level))
  12769. (outline-next-heading))
  12770. (if (or (eobp) (< (funcall outline-level) level))
  12771. nil
  12772. (point))))
  12773. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12774. ;; This is an exact copy of the original function, but it uses
  12775. ;; `org-back-to-heading', to make it work also in invisible
  12776. ;; trees. And is uses an invisible-OK argument.
  12777. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12778. (org-back-to-heading invisible-OK)
  12779. (let ((first t)
  12780. (level (funcall outline-level)))
  12781. (while (and (not (eobp))
  12782. (or first (> (funcall outline-level) level)))
  12783. (setq first nil)
  12784. (outline-next-heading))
  12785. (unless to-heading
  12786. (if (memq (preceding-char) '(?\n ?\^M))
  12787. (progn
  12788. ;; Go to end of line before heading
  12789. (forward-char -1)
  12790. (if (memq (preceding-char) '(?\n ?\^M))
  12791. ;; leave blank line before heading
  12792. (forward-char -1))))))
  12793. (point))
  12794. (defun org-show-subtree ()
  12795. "Show everything after this heading at deeper levels."
  12796. (outline-flag-region
  12797. (point)
  12798. (save-excursion
  12799. (outline-end-of-subtree) (outline-next-heading) (point))
  12800. nil))
  12801. (defun org-show-entry ()
  12802. "Show the body directly following this heading.
  12803. Show the heading too, if it is currently invisible."
  12804. (interactive)
  12805. (save-excursion
  12806. (condition-case nil
  12807. (progn
  12808. (org-back-to-heading t)
  12809. (outline-flag-region
  12810. (max (point-min) (1- (point)))
  12811. (save-excursion
  12812. (re-search-forward
  12813. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12814. (or (match-beginning 1) (point-max)))
  12815. nil))
  12816. (error nil))))
  12817. (defun org-make-options-regexp (kwds)
  12818. "Make a regular expression for keyword lines."
  12819. (concat
  12820. "^"
  12821. "#?[ \t]*\\+\\("
  12822. (mapconcat 'regexp-quote kwds "\\|")
  12823. "\\):[ \t]*"
  12824. "\\(.+\\)"))
  12825. ;; Make isearch reveal the necessary context
  12826. (defun org-isearch-end ()
  12827. "Reveal context after isearch exits."
  12828. (when isearch-success ; only if search was successful
  12829. (if (featurep 'xemacs)
  12830. ;; Under XEmacs, the hook is run in the correct place,
  12831. ;; we directly show the context.
  12832. (org-show-context 'isearch)
  12833. ;; In Emacs the hook runs *before* restoring the overlays.
  12834. ;; So we have to use a one-time post-command-hook to do this.
  12835. ;; (Emacs 22 has a special variable, see function `org-mode')
  12836. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12837. isearch-mode-end-hook-quit)
  12838. ;; Only when the isearch was not quitted.
  12839. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12840. 'append 'local)))))
  12841. (defun org-isearch-post-command ()
  12842. "Remove self from hook, and show context."
  12843. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12844. (org-show-context 'isearch))
  12845. ;;;; Integration with and fixes for other packages
  12846. ;;; Imenu support
  12847. (defvar org-imenu-markers nil
  12848. "All markers currently used by Imenu.")
  12849. (make-variable-buffer-local 'org-imenu-markers)
  12850. (defun org-imenu-new-marker (&optional pos)
  12851. "Return a new marker for use by Imenu, and remember the marker."
  12852. (let ((m (make-marker)))
  12853. (move-marker m (or pos (point)))
  12854. (push m org-imenu-markers)
  12855. m))
  12856. (defun org-imenu-get-tree ()
  12857. "Produce the index for Imenu."
  12858. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12859. (setq org-imenu-markers nil)
  12860. (let* ((n org-imenu-depth)
  12861. (re (concat "^" outline-regexp))
  12862. (subs (make-vector (1+ n) nil))
  12863. (last-level 0)
  12864. m tree level head)
  12865. (save-excursion
  12866. (save-restriction
  12867. (widen)
  12868. (goto-char (point-max))
  12869. (while (re-search-backward re nil t)
  12870. (setq level (org-reduced-level (funcall outline-level)))
  12871. (when (<= level n)
  12872. (looking-at org-complex-heading-regexp)
  12873. (setq head (org-link-display-format
  12874. (org-match-string-no-properties 4))
  12875. m (org-imenu-new-marker))
  12876. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12877. (if (>= level last-level)
  12878. (push (cons head m) (aref subs level))
  12879. (push (cons head (aref subs (1+ level))) (aref subs level))
  12880. (loop for i from (1+ level) to n do (aset subs i nil)))
  12881. (setq last-level level)))))
  12882. (aref subs 1)))
  12883. (eval-after-load "imenu"
  12884. '(progn
  12885. (add-hook 'imenu-after-jump-hook
  12886. (lambda ()
  12887. (if (eq major-mode 'org-mode)
  12888. (org-show-context 'org-goto))))))
  12889. (defun org-link-display-format (link)
  12890. "Replace a link with either the description, or the link target
  12891. if no description is present"
  12892. (save-match-data
  12893. (if (string-match org-bracket-link-analytic-regexp link)
  12894. (replace-match (or (match-string 5 link)
  12895. (concat (match-string 1 link)
  12896. (match-string 3 link)))
  12897. nil nil link)
  12898. link)))
  12899. ;; Speedbar support
  12900. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12901. "Overlay marking the agenda restriction line in speedbar.")
  12902. (org-overlay-put org-speedbar-restriction-lock-overlay
  12903. 'face 'org-agenda-restriction-lock)
  12904. (org-overlay-put org-speedbar-restriction-lock-overlay
  12905. 'help-echo "Agendas are currently limited to this item.")
  12906. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12907. (defun org-speedbar-set-agenda-restriction ()
  12908. "Restrict future agenda commands to the location at point in speedbar.
  12909. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12910. (interactive)
  12911. (require 'org-agenda)
  12912. (let (p m tp np dir txt w)
  12913. (cond
  12914. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12915. 'org-imenu t))
  12916. (setq m (get-text-property p 'org-imenu-marker))
  12917. (save-excursion
  12918. (save-restriction
  12919. (set-buffer (marker-buffer m))
  12920. (goto-char m)
  12921. (org-agenda-set-restriction-lock 'subtree))))
  12922. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12923. 'speedbar-function 'speedbar-find-file))
  12924. (setq tp (previous-single-property-change
  12925. (1+ p) 'speedbar-function)
  12926. np (next-single-property-change
  12927. tp 'speedbar-function)
  12928. dir (speedbar-line-directory)
  12929. txt (buffer-substring-no-properties (or tp (point-min))
  12930. (or np (point-max))))
  12931. (save-excursion
  12932. (save-restriction
  12933. (set-buffer (find-file-noselect
  12934. (let ((default-directory dir))
  12935. (expand-file-name txt))))
  12936. (unless (org-mode-p)
  12937. (error "Cannot restrict to non-Org-mode file"))
  12938. (org-agenda-set-restriction-lock 'file))))
  12939. (t (error "Don't know how to restrict Org-mode's agenda")))
  12940. (org-move-overlay org-speedbar-restriction-lock-overlay
  12941. (point-at-bol) (point-at-eol))
  12942. (setq current-prefix-arg nil)
  12943. (org-agenda-maybe-redo)))
  12944. (eval-after-load "speedbar"
  12945. '(progn
  12946. (speedbar-add-supported-extension ".org")
  12947. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12948. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12949. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12950. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12951. (add-hook 'speedbar-visiting-tag-hook
  12952. (lambda () (org-show-context 'org-goto)))))
  12953. ;;; Fixes and Hacks for problems with other packages
  12954. ;; Make flyspell not check words in links, to not mess up our keymap
  12955. (defun org-mode-flyspell-verify ()
  12956. "Don't let flyspell put overlays at active buttons."
  12957. (not (get-text-property (point) 'keymap)))
  12958. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12959. (eval-after-load "bookmark"
  12960. '(if (boundp 'bookmark-after-jump-hook)
  12961. ;; We can use the hook
  12962. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12963. ;; Hook not available, use advice
  12964. (defadvice bookmark-jump (after org-make-visible activate)
  12965. "Make the position visible."
  12966. (org-bookmark-jump-unhide))))
  12967. (defun org-bookmark-jump-unhide ()
  12968. "Unhide the current position, to show the bookmark location."
  12969. (and (org-mode-p)
  12970. (or (org-invisible-p)
  12971. (save-excursion (goto-char (max (point-min) (1- (point))))
  12972. (org-invisible-p)))
  12973. (org-show-context 'bookmark-jump)))
  12974. ;; Make session.el ignore our circular variable
  12975. (eval-after-load "session"
  12976. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12977. ;;;; Experimental code
  12978. (defun org-closed-in-range ()
  12979. "Sparse tree of items closed in a certain time range.
  12980. Still experimental, may disappear in the future."
  12981. (interactive)
  12982. ;; Get the time interval from the user.
  12983. (let* ((time1 (time-to-seconds
  12984. (org-read-date nil 'to-time nil "Starting date: ")))
  12985. (time2 (time-to-seconds
  12986. (org-read-date nil 'to-time nil "End date:")))
  12987. ;; callback function
  12988. (callback (lambda ()
  12989. (let ((time
  12990. (time-to-seconds
  12991. (apply 'encode-time
  12992. (org-parse-time-string
  12993. (match-string 1))))))
  12994. ;; check if time in interval
  12995. (and (>= time time1) (<= time time2))))))
  12996. ;; make tree, check each match with the callback
  12997. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12998. ;;;; Finish up
  12999. (provide 'org)
  13000. (run-hooks 'org-load-hook)
  13001. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13002. ;;; org.el ends here