org.el 541 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711
  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.06b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.06b"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-align-all-tables nil
  196. "Non-nil means, align all tables when visiting a file.
  197. This is useful when the column width in tables is forced with <N> cookies
  198. in table fields. Such tables will look correct only after the first re-align.
  199. This can also be configured on a per-file basis by adding one of
  200. the following lines anywhere in the buffer:
  201. #+STARTUP: align
  202. #+STARTUP: noalign"
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-insert-mode-line-in-empty-file nil
  206. "Non-nil means insert the first line setting Org-mode in empty files.
  207. When the function `org-mode' is called interactively in an empty file, this
  208. normally means that the file name does not automatically trigger Org-mode.
  209. To ensure that the file will always be in Org-mode in the future, a
  210. line enforcing Org-mode will be inserted into the buffer, if this option
  211. has been set."
  212. :group 'org-startup
  213. :type 'boolean)
  214. (defcustom org-replace-disputed-keys nil
  215. "Non-nil means use alternative key bindings for some keys.
  216. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  217. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  218. If you want to use Org-mode together with one of these other modes,
  219. or more generally if you would like to move some Org-mode commands to
  220. other keys, set this variable and configure the keys with the variable
  221. `org-disputed-keys'.
  222. This option is only relevant at load-time of Org-mode, and must be set
  223. *before* org.el is loaded. Changing it requires a restart of Emacs to
  224. become effective."
  225. :group 'org-startup
  226. :type 'boolean)
  227. (if (fboundp 'defvaralias)
  228. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  229. (defcustom org-disputed-keys
  230. '(([(shift up)] . [(meta p)])
  231. ([(shift down)] . [(meta n)])
  232. ([(shift left)] . [(meta -)])
  233. ([(shift right)] . [(meta +)])
  234. ([(control shift right)] . [(meta shift +)])
  235. ([(control shift left)] . [(meta shift -)]))
  236. "Keys for which Org-mode and other modes compete.
  237. This is an alist, cars are the default keys, second element specifies
  238. the alternative to use when `org-replace-disputed-keys' is t.
  239. Keys can be specified in any syntax supported by `define-key'.
  240. The value of this option takes effect only at Org-mode's startup,
  241. therefore you'll have to restart Emacs to apply it after changing."
  242. :group 'org-startup
  243. :type 'alist)
  244. (defun org-key (key)
  245. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  246. Or return the original if not disputed."
  247. (if org-replace-disputed-keys
  248. (let* ((nkey (key-description key))
  249. (x (org-find-if (lambda (x)
  250. (equal (key-description (car x)) nkey))
  251. org-disputed-keys)))
  252. (if x (cdr x) key))
  253. key))
  254. (defun org-find-if (predicate seq)
  255. (catch 'exit
  256. (while seq
  257. (if (funcall predicate (car seq))
  258. (throw 'exit (car seq))
  259. (pop seq)))))
  260. (defun org-defkey (keymap key def)
  261. "Define a key, possibly translated, as returned by `org-key'."
  262. (define-key keymap (org-key key) def))
  263. (defcustom org-ellipsis nil
  264. "The ellipsis to use in the Org-mode outline.
  265. When nil, just use the standard three dots. When a string, use that instead,
  266. When a face, use the standart 3 dots, but with the specified face.
  267. The change affects only Org-mode (which will then use its own display table).
  268. Changing this requires executing `M-x org-mode' in a buffer to become
  269. effective."
  270. :group 'org-startup
  271. :type '(choice (const :tag "Default" nil)
  272. (face :tag "Face" :value org-warning)
  273. (string :tag "String" :value "...#")))
  274. (defvar org-display-table nil
  275. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  276. (defgroup org-keywords nil
  277. "Keywords in Org-mode."
  278. :tag "Org Keywords"
  279. :group 'org)
  280. (defcustom org-deadline-string "DEADLINE:"
  281. "String to mark deadline entries.
  282. A deadline is this string, followed by a time stamp. Should be a word,
  283. terminated by a colon. You can insert a schedule keyword and
  284. a timestamp with \\[org-deadline].
  285. Changes become only effective after restarting Emacs."
  286. :group 'org-keywords
  287. :type 'string)
  288. (defcustom org-scheduled-string "SCHEDULED:"
  289. "String to mark scheduled TODO entries.
  290. A schedule is this string, followed by a time stamp. Should be a word,
  291. terminated by a colon. You can insert a schedule keyword and
  292. a timestamp with \\[org-schedule].
  293. Changes become only effective after restarting Emacs."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-closed-string "CLOSED:"
  297. "String used as the prefix for timestamps logging closing a TODO entry."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-clock-string "CLOCK:"
  301. "String used as prefix for timestamps clocking work hours on an item."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-comment-string "COMMENT"
  305. "Entries starting with this keyword will never be exported.
  306. An entry can be toggled between COMMENT and normal with
  307. \\[org-toggle-comment].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-quote-string "QUOTE"
  312. "Entries starting with this keyword will be exported in fixed-width font.
  313. Quoting applies only to the text in the entry following the headline, and does
  314. not extend beyond the next headline, even if that is lower level.
  315. An entry can be toggled between QUOTE and normal with
  316. \\[org-toggle-fixed-width-section]."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defconst org-repeat-re
  320. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  321. "Regular expression for specifying repeated events.
  322. After a match, group 1 contains the repeat expression.")
  323. (defgroup org-structure nil
  324. "Options concerning the general structure of Org-mode files."
  325. :tag "Org Structure"
  326. :group 'org)
  327. (defgroup org-reveal-location nil
  328. "Options about how to make context of a location visible."
  329. :tag "Org Reveal Location"
  330. :group 'org-structure)
  331. (defconst org-context-choice
  332. '(choice
  333. (const :tag "Always" t)
  334. (const :tag "Never" nil)
  335. (repeat :greedy t :tag "Individual contexts"
  336. (cons
  337. (choice :tag "Context"
  338. (const agenda)
  339. (const org-goto)
  340. (const occur-tree)
  341. (const tags-tree)
  342. (const link-search)
  343. (const mark-goto)
  344. (const bookmark-jump)
  345. (const isearch)
  346. (const default))
  347. (boolean))))
  348. "Contexts for the reveal options.")
  349. (defcustom org-show-hierarchy-above '((default . t))
  350. "Non-nil means, show full hierarchy when revealing a location.
  351. Org-mode often shows locations in an org-mode file which might have
  352. been invisible before. When this is set, the hierarchy of headings
  353. above the exposed location is shown.
  354. Turning this off for example for sparse trees makes them very compact.
  355. Instead of t, this can also be an alist specifying this option for different
  356. contexts. Valid contexts are
  357. agenda when exposing an entry from the agenda
  358. org-goto when using the command `org-goto' on key C-c C-j
  359. occur-tree when using the command `org-occur' on key C-c /
  360. tags-tree when constructing a sparse tree based on tags matches
  361. link-search when exposing search matches associated with a link
  362. mark-goto when exposing the jump goal of a mark
  363. bookmark-jump when exposing a bookmark location
  364. isearch when exiting from an incremental search
  365. default default for all contexts not set explicitly"
  366. :group 'org-reveal-location
  367. :type org-context-choice)
  368. (defcustom org-show-following-heading '((default . nil))
  369. "Non-nil means, show following heading when revealing a location.
  370. Org-mode often shows locations in an org-mode file which might have
  371. been invisible before. When this is set, the heading following the
  372. match is shown.
  373. Turning this off for example for sparse trees makes them very compact,
  374. but makes it harder to edit the location of the match. In such a case,
  375. use the command \\[org-reveal] to show more context.
  376. Instead of t, this can also be an alist specifying this option for different
  377. contexts. See `org-show-hierarchy-above' for valid contexts."
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-siblings '((default . nil) (isearch t))
  381. "Non-nil means, show all sibling heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the sibling of the current entry
  384. heading are all made visible. If `org-show-hierarchy-above' is t,
  385. the same happens on each level of the hierarchy above the current entry.
  386. By default this is on for the isearch context, off for all other contexts.
  387. Turning this off for example for sparse trees makes them very compact,
  388. but makes it harder to edit the location of the match. In such a case,
  389. use the command \\[org-reveal] to show more context.
  390. Instead of t, this can also be an alist specifying this option for different
  391. contexts. See `org-show-hierarchy-above' for valid contexts."
  392. :group 'org-reveal-location
  393. :type org-context-choice)
  394. (defcustom org-show-entry-below '((default . nil))
  395. "Non-nil means, show the entry below a headline when revealing a location.
  396. Org-mode often shows locations in an org-mode file which might have
  397. been invisible before. When this is set, the text below the headline that is
  398. exposed is also shown.
  399. By default this is off for all contexts.
  400. Instead of t, this can also be an alist specifying this option for different
  401. contexts. See `org-show-hierarchy-above' for valid contexts."
  402. :group 'org-reveal-location
  403. :type org-context-choice)
  404. (defcustom org-indirect-buffer-display 'other-window
  405. "How should indirect tree buffers be displayed?
  406. This applies to indirect buffers created with the commands
  407. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  408. Valid values are:
  409. current-window Display in the current window
  410. other-window Just display in another window.
  411. dedicated-frame Create one new frame, and re-use it each time.
  412. new-frame Make a new frame each time. Note that in this case
  413. previously-made indirect buffers are kept, and you need to
  414. kill these buffers yourself."
  415. :group 'org-structure
  416. :group 'org-agenda-windows
  417. :type '(choice
  418. (const :tag "In current window" current-window)
  419. (const :tag "In current frame, other window" other-window)
  420. (const :tag "Each time a new frame" new-frame)
  421. (const :tag "One dedicated frame" dedicated-frame)))
  422. (defgroup org-cycle nil
  423. "Options concerning visibility cycling in Org-mode."
  424. :tag "Org Cycle"
  425. :group 'org-structure)
  426. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  427. "Names of drawers. Drawers are not opened by cycling on the headline above.
  428. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  429. this:
  430. :DRAWERNAME:
  431. .....
  432. :END:
  433. The drawer \"PROPERTIES\" is special for capturing properties through
  434. the property API.
  435. Drawers can be defined on the per-file basis with a line like:
  436. #+DRAWERS: HIDDEN STATE PROPERTIES"
  437. :group 'org-structure
  438. :type '(repeat (string :tag "Drawer Name")))
  439. (defcustom org-cycle-global-at-bob nil
  440. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  441. This makes it possible to do global cycling without having to use S-TAB or
  442. C-u TAB. For this special case to work, the first line of the buffer
  443. must not be a headline - it may be empty ot some other text. When used in
  444. this way, `org-cycle-hook' is disables temporarily, to make sure the
  445. cursor stays at the beginning of the buffer.
  446. When this option is nil, don't do anything special at the beginning
  447. of the buffer."
  448. :group 'org-cycle
  449. :type 'boolean)
  450. (defcustom org-cycle-emulate-tab t
  451. "Where should `org-cycle' emulate TAB.
  452. nil Never
  453. white Only in completely white lines
  454. whitestart Only at the beginning of lines, before the first non-white char
  455. t Everywhere except in headlines
  456. exc-hl-bol Everywhere except at the start of a headline
  457. If TAB is used in a place where it does not emulate TAB, the current subtree
  458. visibility is cycled."
  459. :group 'org-cycle
  460. :type '(choice (const :tag "Never" nil)
  461. (const :tag "Only in completely white lines" white)
  462. (const :tag "Before first char in a line" whitestart)
  463. (const :tag "Everywhere except in headlines" t)
  464. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  465. ))
  466. (defcustom org-cycle-separator-lines 2
  467. "Number of empty lines needed to keep an empty line between collapsed trees.
  468. If you leave an empty line between the end of a subtree and the following
  469. headline, this empty line is hidden when the subtree is folded.
  470. Org-mode will leave (exactly) one empty line visible if the number of
  471. empty lines is equal or larger to the number given in this variable.
  472. So the default 2 means, at least 2 empty lines after the end of a subtree
  473. are needed to produce free space between a collapsed subtree and the
  474. following headline.
  475. Special case: when 0, never leave empty lines in collapsed view."
  476. :group 'org-cycle
  477. :type 'integer)
  478. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  479. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  480. org-cycle-hide-drawers
  481. org-cycle-show-empty-lines
  482. org-optimize-window-after-visibility-change)
  483. "Hook that is run after `org-cycle' has changed the buffer visibility.
  484. The function(s) in this hook must accept a single argument which indicates
  485. the new state that was set by the most recent `org-cycle' command. The
  486. argument is a symbol. After a global state change, it can have the values
  487. `overview', `content', or `all'. After a local state change, it can have
  488. the values `folded', `children', or `subtree'."
  489. :group 'org-cycle
  490. :type 'hook)
  491. (defgroup org-edit-structure nil
  492. "Options concerning structure editing in Org-mode."
  493. :tag "Org Edit Structure"
  494. :group 'org-structure)
  495. (defcustom org-odd-levels-only nil
  496. "Non-nil means, skip even levels and only use odd levels for the outline.
  497. This has the effect that two stars are being added/taken away in
  498. promotion/demotion commands. It also influences how levels are
  499. handled by the exporters.
  500. Changing it requires restart of `font-lock-mode' to become effective
  501. for fontification also in regions already fontified.
  502. You may also set this on a per-file basis by adding one of the following
  503. lines to the buffer:
  504. #+STARTUP: odd
  505. #+STARTUP: oddeven"
  506. :group 'org-edit-structure
  507. :group 'org-font-lock
  508. :type 'boolean)
  509. (defcustom org-adapt-indentation t
  510. "Non-nil means, adapt indentation when promoting and demoting.
  511. When this is set and the *entire* text in an entry is indented, the
  512. indentation is increased by one space in a demotion command, and
  513. decreased by one in a promotion command. If any line in the entry
  514. body starts at column 0, indentation is not changed at all."
  515. :group 'org-edit-structure
  516. :type 'boolean)
  517. (defcustom org-special-ctrl-a/e nil
  518. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  519. When t, `C-a' will bring back the cursor to the beginning of the
  520. headline text, i.e. after the stars and after a possible TODO keyword.
  521. In an item, this will be the position after the bullet.
  522. When the cursor is already at that position, another `C-a' will bring
  523. it to the beginning of the line.
  524. `C-e' will jump to the end of the headline, ignoring the presence of tags
  525. in the headline. A second `C-e' will then jump to the true end of the
  526. line, after any tags.
  527. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  528. and only a directly following, identical keypress will bring the cursor
  529. to the special positions."
  530. :group 'org-edit-structure
  531. :type '(choice
  532. (const :tag "off" nil)
  533. (const :tag "after bullet first" t)
  534. (const :tag "border first" reversed)))
  535. (if (fboundp 'defvaralias)
  536. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  537. (defcustom org-special-ctrl-k nil
  538. "Non-nil means `C-k' will behave specially in headlines.
  539. When nil, `C-k' will call the default `kill-line' command.
  540. When t, the following will happen while the cursor is in the headline:
  541. - When the cursor is at the beginning of a headline, kill the entire
  542. line and possible the folded subtree below the line.
  543. - When in the middle of the headline text, kill the headline up to the tags.
  544. - When after the headline text, kill the tags."
  545. :group 'org-edit-structure
  546. :type 'boolean)
  547. (defcustom org-M-RET-may-split-line '((default . t))
  548. "Non-nil means, M-RET will split the line at the cursor position.
  549. When nil, it will go to the end of the line before making a
  550. new line.
  551. You may also set this option in a different way for different
  552. contexts. Valid contexts are:
  553. headline when creating a new headline
  554. item when creating a new item
  555. table in a table field
  556. default the value to be used for all contexts not explicitly
  557. customized"
  558. :group 'org-structure
  559. :group 'org-table
  560. :type '(choice
  561. (const :tag "Always" t)
  562. (const :tag "Never" nil)
  563. (repeat :greedy t :tag "Individual contexts"
  564. (cons
  565. (choice :tag "Context"
  566. (const headline)
  567. (const item)
  568. (const table)
  569. (const default))
  570. (boolean)))))
  571. (defcustom org-blank-before-new-entry '((heading . nil)
  572. (plain-list-item . nil))
  573. "Should `org-insert-heading' leave a blank line before new heading/item?
  574. The value is an alist, with `heading' and `plain-list-item' as car,
  575. and a boolean flag as cdr."
  576. :group 'org-edit-structure
  577. :type '(list
  578. (cons (const heading) (boolean))
  579. (cons (const plain-list-item) (boolean))))
  580. (defcustom org-insert-heading-hook nil
  581. "Hook being run after inserting a new heading."
  582. :group 'org-edit-structure
  583. :type 'hook)
  584. (defcustom org-enable-fixed-width-editor t
  585. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  586. This currently only means, they are never auto-wrapped.
  587. When nil, such lines will be treated like ordinary lines.
  588. See also the QUOTE keyword."
  589. :group 'org-edit-structure
  590. :type 'boolean)
  591. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  592. "The mode that should be used to edit fixed-width regions.
  593. These are the regions where each line starts with a colon."
  594. :group 'org-edit-structure
  595. :type '(choice
  596. (const artist-mode)
  597. (const picture-mode)
  598. (const fundamental-mode)
  599. (function :tag "Other (specify)")))
  600. (defcustom org-goto-auto-isearch t
  601. "Non-nil means, typing characters in org-goto starts incremental search."
  602. :group 'org-edit-structure
  603. :type 'boolean)
  604. (defgroup org-sparse-trees nil
  605. "Options concerning sparse trees in Org-mode."
  606. :tag "Org Sparse Trees"
  607. :group 'org-structure)
  608. (defcustom org-highlight-sparse-tree-matches t
  609. "Non-nil means, highlight all matches that define a sparse tree.
  610. The highlights will automatically disappear the next time the buffer is
  611. changed by an edit command."
  612. :group 'org-sparse-trees
  613. :type 'boolean)
  614. (defcustom org-remove-highlights-with-change t
  615. "Non-nil means, any change to the buffer will remove temporary highlights.
  616. Such highlights are created by `org-occur' and `org-clock-display'.
  617. When nil, `C-c C-c needs to be used to get rid of the highlights.
  618. The highlights created by `org-preview-latex-fragment' always need
  619. `C-c C-c' to be removed."
  620. :group 'org-sparse-trees
  621. :group 'org-time
  622. :type 'boolean)
  623. (defcustom org-occur-hook '(org-first-headline-recenter)
  624. "Hook that is run after `org-occur' has constructed a sparse tree.
  625. This can be used to recenter the window to show as much of the structure
  626. as possible."
  627. :group 'org-sparse-trees
  628. :type 'hook)
  629. (defgroup org-plain-lists nil
  630. "Options concerning plain lists in Org-mode."
  631. :tag "Org Plain lists"
  632. :group 'org-structure)
  633. (defcustom org-cycle-include-plain-lists nil
  634. "Non-nil means, include plain lists into visibility cycling.
  635. This means that during cycling, plain list items will *temporarily* be
  636. interpreted as outline headlines with a level given by 1000+i where i is the
  637. indentation of the bullet. In all other operations, plain list items are
  638. not seen as headlines. For example, you cannot assign a TODO keyword to
  639. such an item."
  640. :group 'org-plain-lists
  641. :type 'boolean)
  642. (defcustom org-plain-list-ordered-item-terminator t
  643. "The character that makes a line with leading number an ordered list item.
  644. Valid values are ?. and ?\). To get both terminators, use t. While
  645. ?. may look nicer, it creates the danger that a line with leading
  646. number may be incorrectly interpreted as an item. ?\) therefore is
  647. the safe choice."
  648. :group 'org-plain-lists
  649. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  650. (const :tag "paren like in \"2)\"" ?\))
  651. (const :tab "both" t)))
  652. (defcustom org-empty-line-terminates-plain-lists nil
  653. "Non-nil means, an empty line ends all plain list levels.
  654. When nil, empty lines are part of the preceeding item."
  655. :group 'org-plain-lists
  656. :type 'boolean)
  657. (defcustom org-auto-renumber-ordered-lists t
  658. "Non-nil means, automatically renumber ordered plain lists.
  659. Renumbering happens when the sequence have been changed with
  660. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  661. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  662. :group 'org-plain-lists
  663. :type 'boolean)
  664. (defcustom org-provide-checkbox-statistics t
  665. "Non-nil means, update checkbox statistics after insert and toggle.
  666. When this is set, checkbox statistics is updated each time you either insert
  667. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  668. with \\[org-ctrl-c-ctrl-c\\]."
  669. :group 'org-plain-lists
  670. :type 'boolean)
  671. (defcustom org-description-max-indent 20
  672. "Maximum indentation for the second line of a description list.
  673. When the indentation would be larger than this, it will become
  674. 5 characters instead."
  675. :group 'org-plain-lists
  676. :type 'integer)
  677. (defgroup org-imenu-and-speedbar nil
  678. "Options concerning imenu and speedbar in Org-mode."
  679. :tag "Org Imenu and Speedbar"
  680. :group 'org-structure)
  681. (defcustom org-imenu-depth 2
  682. "The maximum level for Imenu access to Org-mode headlines.
  683. This also applied for speedbar access."
  684. :group 'org-imenu-and-speedbar
  685. :type 'number)
  686. (defgroup org-table nil
  687. "Options concerning tables in Org-mode."
  688. :tag "Org Table"
  689. :group 'org)
  690. (defcustom org-enable-table-editor 'optimized
  691. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  692. When nil, such lines will be treated like ordinary lines.
  693. When equal to the symbol `optimized', the table editor will be optimized to
  694. do the following:
  695. - Automatic overwrite mode in front of whitespace in table fields.
  696. This makes the structure of the table stay in tact as long as the edited
  697. field does not exceed the column width.
  698. - Minimize the number of realigns. Normally, the table is aligned each time
  699. TAB or RET are pressed to move to another field. With optimization this
  700. happens only if changes to a field might have changed the column width.
  701. Optimization requires replacing the functions `self-insert-command',
  702. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  703. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  704. very good at guessing when a re-align will be necessary, but you can always
  705. force one with \\[org-ctrl-c-ctrl-c].
  706. If you would like to use the optimized version in Org-mode, but the
  707. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  708. This variable can be used to turn on and off the table editor during a session,
  709. but in order to toggle optimization, a restart is required.
  710. See also the variable `org-table-auto-blank-field'."
  711. :group 'org-table
  712. :type '(choice
  713. (const :tag "off" nil)
  714. (const :tag "on" t)
  715. (const :tag "on, optimized" optimized)))
  716. (defcustom org-table-tab-recognizes-table.el t
  717. "Non-nil means, TAB will automatically notice a table.el table.
  718. When it sees such a table, it moves point into it and - if necessary -
  719. calls `table-recognize-table'."
  720. :group 'org-table-editing
  721. :type 'boolean)
  722. (defgroup org-link nil
  723. "Options concerning links in Org-mode."
  724. :tag "Org Link"
  725. :group 'org)
  726. (defvar org-link-abbrev-alist-local nil
  727. "Buffer-local version of `org-link-abbrev-alist', which see.
  728. The value of this is taken from the #+LINK lines.")
  729. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  730. (defcustom org-link-abbrev-alist nil
  731. "Alist of link abbreviations.
  732. The car of each element is a string, to be replaced at the start of a link.
  733. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  734. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  735. [[linkkey:tag][description]]
  736. If REPLACE is a string, the tag will simply be appended to create the link.
  737. If the string contains \"%s\", the tag will be inserted there.
  738. REPLACE may also be a function that will be called with the tag as the
  739. only argument to create the link, which should be returned as a string.
  740. See the manual for examples."
  741. :group 'org-link
  742. :type 'alist)
  743. (defcustom org-descriptive-links t
  744. "Non-nil means, hide link part and only show description of bracket links.
  745. Bracket links are like [[link][descritpion]]. This variable sets the initial
  746. state in new org-mode buffers. The setting can then be toggled on a
  747. per-buffer basis from the Org->Hyperlinks menu."
  748. :group 'org-link
  749. :type 'boolean)
  750. (defcustom org-link-file-path-type 'adaptive
  751. "How the path name in file links should be stored.
  752. Valid values are:
  753. relative Relative to the current directory, i.e. the directory of the file
  754. into which the link is being inserted.
  755. absolute Absolute path, if possible with ~ for home directory.
  756. noabbrev Absolute path, no abbreviation of home directory.
  757. adaptive Use relative path for files in the current directory and sub-
  758. directories of it. For other files, use an absolute path."
  759. :group 'org-link
  760. :type '(choice
  761. (const relative)
  762. (const absolute)
  763. (const noabbrev)
  764. (const adaptive)))
  765. (defcustom org-activate-links '(bracket angle plain radio tag date)
  766. "Types of links that should be activated in Org-mode files.
  767. This is a list of symbols, each leading to the activation of a certain link
  768. type. In principle, it does not hurt to turn on most link types - there may
  769. be a small gain when turning off unused link types. The types are:
  770. bracket The recommended [[link][description]] or [[link]] links with hiding.
  771. angular Links in angular brackes that may contain whitespace like
  772. <bbdb:Carsten Dominik>.
  773. plain Plain links in normal text, no whitespace, like http://google.com.
  774. radio Text that is matched by a radio target, see manual for details.
  775. tag Tag settings in a headline (link to tag search).
  776. date Time stamps (link to calendar).
  777. Changing this variable requires a restart of Emacs to become effective."
  778. :group 'org-link
  779. :type '(set (const :tag "Double bracket links (new style)" bracket)
  780. (const :tag "Angular bracket links (old style)" angular)
  781. (const :tag "Plain text links" plain)
  782. (const :tag "Radio target matches" radio)
  783. (const :tag "Tags" tag)
  784. (const :tag "Timestamps" date)))
  785. (defcustom org-make-link-description-function nil
  786. "Function to use to generate link descriptions from links. If
  787. nil the link location will be used. This function must take two
  788. parameters; the first is the link and the second the description
  789. org-insert-link has generated, and should return the description
  790. to use."
  791. :group 'org-link
  792. :type 'function)
  793. (defgroup org-link-store nil
  794. "Options concerning storing links in Org-mode."
  795. :tag "Org Store Link"
  796. :group 'org-link)
  797. (defcustom org-email-link-description-format "Email %c: %.30s"
  798. "Format of the description part of a link to an email or usenet message.
  799. The following %-excapes will be replaced by corresponding information:
  800. %F full \"From\" field
  801. %f name, taken from \"From\" field, address if no name
  802. %T full \"To\" field
  803. %t first name in \"To\" field, address if no name
  804. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  805. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  806. %s subject
  807. %m message-id.
  808. You may use normal field width specification between the % and the letter.
  809. This is for example useful to limit the length of the subject.
  810. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  811. :group 'org-link-store
  812. :type 'string)
  813. (defcustom org-from-is-user-regexp
  814. (let (r1 r2)
  815. (when (and user-mail-address (not (string= user-mail-address "")))
  816. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  817. (when (and user-full-name (not (string= user-full-name "")))
  818. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  819. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  820. "Regexp mached against the \"From:\" header of an email or usenet message.
  821. It should match if the message is from the user him/herself."
  822. :group 'org-link-store
  823. :type 'regexp)
  824. (defcustom org-context-in-file-links t
  825. "Non-nil means, file links from `org-store-link' contain context.
  826. A search string will be added to the file name with :: as separator and
  827. used to find the context when the link is activated by the command
  828. `org-open-at-point'.
  829. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  830. negates this setting for the duration of the command."
  831. :group 'org-link-store
  832. :type 'boolean)
  833. (defcustom org-keep-stored-link-after-insertion nil
  834. "Non-nil means, keep link in list for entire session.
  835. The command `org-store-link' adds a link pointing to the current
  836. location to an internal list. These links accumulate during a session.
  837. The command `org-insert-link' can be used to insert links into any
  838. Org-mode file (offering completion for all stored links). When this
  839. option is nil, every link which has been inserted once using \\[org-insert-link]
  840. will be removed from the list, to make completing the unused links
  841. more efficient."
  842. :group 'org-link-store
  843. :type 'boolean)
  844. (defgroup org-link-follow nil
  845. "Options concerning following links in Org-mode."
  846. :tag "Org Follow Link"
  847. :group 'org-link)
  848. (defcustom org-follow-link-hook nil
  849. "Hook that is run after a link has been followed."
  850. :group 'org-link-follow
  851. :type 'hook)
  852. (defcustom org-tab-follows-link nil
  853. "Non-nil means, on links TAB 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-return-follows-link nil
  858. "Non-nil means, on links RET will follow the link.
  859. Needs to be set before org.el is loaded."
  860. :group 'org-link-follow
  861. :type 'boolean)
  862. (defcustom org-mouse-1-follows-link
  863. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  864. "Non-nil means, mouse-1 on a link will follow the link.
  865. A longer mouse click will still set point. Does not work on XEmacs.
  866. Needs to be set before org.el is loaded."
  867. :group 'org-link-follow
  868. :type 'boolean)
  869. (defcustom org-mark-ring-length 4
  870. "Number of different positions to be recorded in the ring
  871. Changing this requires a restart of Emacs to work correctly."
  872. :group 'org-link-follow
  873. :type 'interger)
  874. (defcustom org-link-frame-setup
  875. '((vm . vm-visit-folder-other-frame)
  876. (gnus . gnus-other-frame)
  877. (file . find-file-other-window))
  878. "Setup the frame configuration for following links.
  879. When following a link with Emacs, it may often be useful to display
  880. this link in another window or frame. This variable can be used to
  881. set this up for the different types of links.
  882. For VM, use any of
  883. `vm-visit-folder'
  884. `vm-visit-folder-other-frame'
  885. For Gnus, use any of
  886. `gnus'
  887. `gnus-other-frame'
  888. For FILE, use any of
  889. `find-file'
  890. `find-file-other-window'
  891. `find-file-other-frame'
  892. For the calendar, use the variable `calendar-setup'.
  893. For BBDB, it is currently only possible to display the matches in
  894. another window."
  895. :group 'org-link-follow
  896. :type '(list
  897. (cons (const vm)
  898. (choice
  899. (const vm-visit-folder)
  900. (const vm-visit-folder-other-window)
  901. (const vm-visit-folder-other-frame)))
  902. (cons (const gnus)
  903. (choice
  904. (const gnus)
  905. (const gnus-other-frame)))
  906. (cons (const file)
  907. (choice
  908. (const find-file)
  909. (const find-file-other-window)
  910. (const find-file-other-frame)))))
  911. (defcustom org-display-internal-link-with-indirect-buffer nil
  912. "Non-nil means, use indirect buffer to display infile links.
  913. Activating internal links (from one location in a file to another location
  914. in the same file) normally just jumps to the location. When the link is
  915. activated with a C-u prefix (or with mouse-3), the link is displayed in
  916. another window. When this option is set, the other window actually displays
  917. an indirect buffer clone of the current buffer, to avoid any visibility
  918. changes to the current buffer."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-open-non-existing-files nil
  922. "Non-nil means, `org-open-file' will open non-existing files.
  923. When nil, an error will be generated."
  924. :group 'org-link-follow
  925. :type 'boolean)
  926. (defcustom org-open-directory-means-index-dot-org nil
  927. "Non-nil means, a link to a directory really means to index.org.
  928. When nil, following a directory link will run dired or open a finder/explorer
  929. window on that directory."
  930. :group 'org-link-follow
  931. :type 'boolean)
  932. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  933. "Function and arguments to call for following mailto links.
  934. This is a list with the first element being a lisp function, and the
  935. remaining elements being arguments to the function. In string arguments,
  936. %a will be replaced by the address, and %s will be replaced by the subject
  937. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  938. :group 'org-link-follow
  939. :type '(choice
  940. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  941. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  942. (const :tag "message-mail" (message-mail "%a" "%s"))
  943. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  944. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  945. "Non-nil means, ask for confirmation before executing shell links.
  946. Shell links can be dangerous: just think about a link
  947. [[shell:rm -rf ~/*][Google Search]]
  948. This link would show up in your Org-mode document as \"Google Search\",
  949. but really it would remove your entire home directory.
  950. Therefore we advise against setting this variable to nil.
  951. Just change it to `y-or-n-p' of you want to confirm with a
  952. single keystroke rather than having to type \"yes\"."
  953. :group 'org-link-follow
  954. :type '(choice
  955. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  956. (const :tag "with y-or-n (faster)" y-or-n-p)
  957. (const :tag "no confirmation (dangerous)" nil)))
  958. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  959. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  960. Elisp links can be dangerous: just think about a link
  961. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  962. This link would show up in your Org-mode document as \"Google Search\",
  963. but really it would remove your entire home directory.
  964. Therefore we advise against setting this variable to nil.
  965. Just change it to `y-or-n-p' of you want to confirm with a
  966. single keystroke rather than having to type \"yes\"."
  967. :group 'org-link-follow
  968. :type '(choice
  969. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  970. (const :tag "with y-or-n (faster)" y-or-n-p)
  971. (const :tag "no confirmation (dangerous)" nil)))
  972. (defconst org-file-apps-defaults-gnu
  973. '((remote . emacs)
  974. (t . mailcap))
  975. "Default file applications on a UNIX or GNU/Linux system.
  976. See `org-file-apps'.")
  977. (defconst org-file-apps-defaults-macosx
  978. '((remote . emacs)
  979. (t . "open %s")
  980. ("ps" . "gv %s")
  981. ("ps.gz" . "gv %s")
  982. ("eps" . "gv %s")
  983. ("eps.gz" . "gv %s")
  984. ("dvi" . "xdvi %s")
  985. ("fig" . "xfig %s"))
  986. "Default file applications on a MacOS X system.
  987. The system \"open\" is known as a default, but we use X11 applications
  988. for some files for which the OS does not have a good default.
  989. See `org-file-apps'.")
  990. (defconst org-file-apps-defaults-windowsnt
  991. (list
  992. '(remote . emacs)
  993. (cons t
  994. (list (if (featurep 'xemacs)
  995. 'mswindows-shell-execute
  996. 'w32-shell-execute)
  997. "open" 'file)))
  998. "Default file applications on a Windows NT system.
  999. The system \"open\" is used for most files.
  1000. See `org-file-apps'.")
  1001. (defcustom org-file-apps
  1002. '(
  1003. ("txt" . emacs)
  1004. ("tex" . emacs)
  1005. ("ltx" . emacs)
  1006. ("org" . emacs)
  1007. ("el" . emacs)
  1008. ("bib" . emacs)
  1009. )
  1010. "External applications for opening `file:path' items in a document.
  1011. Org-mode uses system defaults for different file types, but
  1012. you can use this variable to set the application for a given file
  1013. extension. The entries in this list are cons cells where the car identifies
  1014. files and the cdr the corresponding command. Possible values for the
  1015. file identifier are
  1016. \"ext\" A string identifying an extension
  1017. `directory' Matches a directory
  1018. `remote' Matches a remote file, accessible through tramp or efs.
  1019. Remote files most likely should be visited through Emacs
  1020. because external applications cannot handle such paths.
  1021. t Default for all remaining files
  1022. Possible values for the command are:
  1023. `emacs' The file will be visited by the current Emacs process.
  1024. `default' Use the default application for this file type.
  1025. string A command to be executed by a shell; %s will be replaced
  1026. by the path to the file.
  1027. sexp A Lisp form which will be evaluated. The file path will
  1028. be available in the Lisp variable `file'.
  1029. For more examples, see the system specific constants
  1030. `org-file-apps-defaults-macosx'
  1031. `org-file-apps-defaults-windowsnt'
  1032. `org-file-apps-defaults-gnu'."
  1033. :group 'org-link-follow
  1034. :type '(repeat
  1035. (cons (choice :value ""
  1036. (string :tag "Extension")
  1037. (const :tag "Default for unrecognized files" t)
  1038. (const :tag "Remote file" remote)
  1039. (const :tag "Links to a directory" directory))
  1040. (choice :value ""
  1041. (const :tag "Visit with Emacs" emacs)
  1042. (const :tag "Use system default" default)
  1043. (string :tag "Command")
  1044. (sexp :tag "Lisp form")))))
  1045. (defgroup org-refile nil
  1046. "Options concerning refiling entries in Org-mode."
  1047. :tag "Org Remember"
  1048. :group 'org)
  1049. (defcustom org-directory "~/org"
  1050. "Directory with org files.
  1051. This directory will be used as default to prompt for org files.
  1052. Used by the hooks for remember.el."
  1053. :group 'org-refile
  1054. :group 'org-remember
  1055. :type 'directory)
  1056. (defcustom org-default-notes-file "~/.notes"
  1057. "Default target for storing notes.
  1058. Used by the hooks for remember.el. This can be a string, or nil to mean
  1059. the value of `remember-data-file'.
  1060. You can set this on a per-template basis with the variable
  1061. `org-remember-templates'."
  1062. :group 'org-refile
  1063. :group 'org-remember
  1064. :type '(choice
  1065. (const :tag "Default from remember-data-file" nil)
  1066. file))
  1067. (defcustom org-goto-interface 'outline
  1068. "The default interface to be used for `org-goto'.
  1069. Allowed vaues are:
  1070. outline The interface shows an outline of the relevant file
  1071. and the correct heading is found by moving through
  1072. the outline or by searching with incremental search.
  1073. outline-path-completion Headlines in the current buffer are offered via
  1074. completion."
  1075. :group 'org-refile
  1076. :type '(choice
  1077. (const :tag "Outline" outline)
  1078. (const :tag "Outline-path-completion" outline-path-completion)))
  1079. (defcustom org-reverse-note-order nil
  1080. "Non-nil means, store new notes at the beginning of a file or entry.
  1081. When nil, new notes will be filed to the end of a file or entry.
  1082. This can also be a list with cons cells of regular expressions that
  1083. are matched against file names, and values."
  1084. :group 'org-remember
  1085. :type '(choice
  1086. (const :tag "Reverse always" t)
  1087. (const :tag "Reverse never" nil)
  1088. (repeat :tag "By file name regexp"
  1089. (cons regexp boolean))))
  1090. (defcustom org-refile-targets nil
  1091. "Targets for refiling entries with \\[org-refile].
  1092. This is list of cons cells. Each cell contains:
  1093. - a specification of the files to be considered, either a list of files,
  1094. or a symbol whose function or variable value will be used to retrieve
  1095. a file name or a list of file names. Nil means, refile to a different
  1096. heading in the current buffer.
  1097. - A specification of how to find candidate refile targets. This may be
  1098. any of
  1099. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1100. This tag has to be present in all target headlines, inheritance will
  1101. not be considered.
  1102. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1103. todo keyword.
  1104. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1105. headlines that are refiling targets.
  1106. - a cons cell (:level . N). Any headline of level N is considered a target.
  1107. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1108. When this variable is nil, all top-level headlines in the current buffer
  1109. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1110. :group 'org-remember
  1111. :type '(repeat
  1112. (cons
  1113. (choice :value org-agenda-files
  1114. (const :tag "All agenda files" org-agenda-files)
  1115. (const :tag "Current buffer" nil)
  1116. (function) (variable) (file))
  1117. (choice :tag "Identify target headline by"
  1118. (cons :tag "Specific tag" (const :tag) (string))
  1119. (cons :tag "TODO keyword" (const :todo) (string))
  1120. (cons :tag "Regular expression" (const :regexp) (regexp))
  1121. (cons :tag "Level number" (const :level) (integer))
  1122. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1123. (defcustom org-refile-use-outline-path nil
  1124. "Non-nil means, provide refile targets as paths.
  1125. So a level 3 headline will be available as level1/level2/level3.
  1126. When the value is `file', also include the file name (without directory)
  1127. into the path. When `full-file-path', include the full file path."
  1128. :group 'org-remember
  1129. :type '(choice
  1130. (const :tag "Not" nil)
  1131. (const :tag "Yes" t)
  1132. (const :tag "Start with file name" file)
  1133. (const :tag "Start with full file path" full-file-path)))
  1134. (defgroup org-todo nil
  1135. "Options concerning TODO items in Org-mode."
  1136. :tag "Org TODO"
  1137. :group 'org)
  1138. (defgroup org-progress nil
  1139. "Options concerning Progress logging in Org-mode."
  1140. :tag "Org Progress"
  1141. :group 'org-time)
  1142. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1143. "List of TODO entry keyword sequences and their interpretation.
  1144. \\<org-mode-map>This is a list of sequences.
  1145. Each sequence starts with a symbol, either `sequence' or `type',
  1146. indicating if the keywords should be interpreted as a sequence of
  1147. action steps, or as different types of TODO items. The first
  1148. keywords are states requiring action - these states will select a headline
  1149. for inclusion into the global TODO list Org-mode produces. If one of
  1150. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1151. signify that no further action is necessary. If \"|\" is not found,
  1152. the last keyword is treated as the only DONE state of the sequence.
  1153. The command \\[org-todo] cycles an entry through these states, and one
  1154. additional state where no keyword is present. For details about this
  1155. cycling, see the manual.
  1156. TODO keywords and interpretation can also be set on a per-file basis with
  1157. the special #+SEQ_TODO and #+TYP_TODO lines.
  1158. Each keyword can optionally specify a character for fast state selection
  1159. \(in combination with the variable `org-use-fast-todo-selection')
  1160. and specifiers for state change logging, using the same syntax
  1161. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1162. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1163. indicates to record a time stamp each time this state is selected.
  1164. Each keyword may also specify if a timestamp or a note should be
  1165. recorded when entering or leaving the state, by adding additional
  1166. characters in the parenthesis after the keyword. This looks like this:
  1167. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1168. record only the time of the state change. With X and Y being either
  1169. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1170. Y when leaving the state if and only if the *target* state does not
  1171. define X. You may omit any of the fast-selection key or X or /Y,
  1172. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1173. For backward compatibility, this variable may also be just a list
  1174. of keywords - in this case the interptetation (sequence or type) will be
  1175. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1176. :group 'org-todo
  1177. :group 'org-keywords
  1178. :type '(choice
  1179. (repeat :tag "Old syntax, just keywords"
  1180. (string :tag "Keyword"))
  1181. (repeat :tag "New syntax"
  1182. (cons
  1183. (choice
  1184. :tag "Interpretation"
  1185. (const :tag "Sequence (cycling hits every state)" sequence)
  1186. (const :tag "Type (cycling directly to DONE)" type))
  1187. (repeat
  1188. (string :tag "Keyword"))))))
  1189. (defvar org-todo-keywords-1 nil
  1190. "All TODO and DONE keywords active in a buffer.")
  1191. (make-variable-buffer-local 'org-todo-keywords-1)
  1192. (defvar org-todo-keywords-for-agenda nil)
  1193. (defvar org-done-keywords-for-agenda nil)
  1194. (defvar org-todo-keyword-alist-for-agenda nil)
  1195. (defvar org-tag-alist-for-agenda nil)
  1196. (defvar org-agenda-contributing-files nil)
  1197. (defvar org-not-done-keywords nil)
  1198. (make-variable-buffer-local 'org-not-done-keywords)
  1199. (defvar org-done-keywords nil)
  1200. (make-variable-buffer-local 'org-done-keywords)
  1201. (defvar org-todo-heads nil)
  1202. (make-variable-buffer-local 'org-todo-heads)
  1203. (defvar org-todo-sets nil)
  1204. (make-variable-buffer-local 'org-todo-sets)
  1205. (defvar org-todo-log-states nil)
  1206. (make-variable-buffer-local 'org-todo-log-states)
  1207. (defvar org-todo-kwd-alist nil)
  1208. (make-variable-buffer-local 'org-todo-kwd-alist)
  1209. (defvar org-todo-key-alist nil)
  1210. (make-variable-buffer-local 'org-todo-key-alist)
  1211. (defvar org-todo-key-trigger nil)
  1212. (make-variable-buffer-local 'org-todo-key-trigger)
  1213. (defcustom org-todo-interpretation 'sequence
  1214. "Controls how TODO keywords are interpreted.
  1215. This variable is in principle obsolete and is only used for
  1216. backward compatibility, if the interpretation of todo keywords is
  1217. not given already in `org-todo-keywords'. See that variable for
  1218. more information."
  1219. :group 'org-todo
  1220. :group 'org-keywords
  1221. :type '(choice (const sequence)
  1222. (const type)))
  1223. (defcustom org-use-fast-todo-selection 'prefix
  1224. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1225. This variable describes if and under what circumstances the cycling
  1226. mechanism for TODO keywords will be replaced by a single-key, direct
  1227. selection scheme.
  1228. When nil, fast selection is never used.
  1229. When the symbol `prefix', it will be used when `org-todo' is called with
  1230. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1231. in an agenda buffer.
  1232. When t, fast selection is used by default. In this case, the prefix
  1233. argument forces cycling instead.
  1234. In all cases, the special interface is only used if access keys have actually
  1235. been assigned by the user, i.e. if keywords in the configuration are followed
  1236. by a letter in parenthesis, like TODO(t)."
  1237. :group 'org-todo
  1238. :type '(choice
  1239. (const :tag "Never" nil)
  1240. (const :tag "By default" t)
  1241. (const :tag "Only with C-u C-c C-t" prefix)))
  1242. (defcustom org-provide-todo-statistics t
  1243. "Non-nil means, update todo statistics after insert and toggle.
  1244. When this is set, todo statistics is updated in the parent of the current
  1245. entry each time a todo state is changed."
  1246. :group 'org-todo
  1247. :type 'boolean)
  1248. (defcustom org-after-todo-state-change-hook nil
  1249. "Hook which is run after the state of a TODO item was changed.
  1250. The new state (a string with a TODO keyword, or nil) is available in the
  1251. Lisp variable `state'."
  1252. :group 'org-todo
  1253. :type 'hook)
  1254. (defcustom org-log-done nil
  1255. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1256. When equal to the list (done), also prompt for a closing note.
  1257. This can also be configured on a per-file basis by adding one of
  1258. the following lines anywhere in the buffer:
  1259. #+STARTUP: logdone
  1260. #+STARTUP: lognotedone
  1261. #+STARTUP: nologdone"
  1262. :group 'org-todo
  1263. :group 'org-progress
  1264. :type '(choice
  1265. (const :tag "No logging" nil)
  1266. (const :tag "Record CLOSED timestamp" time)
  1267. (const :tag "Record CLOSED timestamp with closing note." note)))
  1268. ;; Normalize old uses of org-log-done.
  1269. (cond
  1270. ((eq org-log-done t) (setq org-log-done 'time))
  1271. ((and (listp org-log-done) (memq 'done org-log-done))
  1272. (setq org-log-done 'note)))
  1273. (defcustom org-log-note-clock-out nil
  1274. "Non-nil means, record a note when clocking out of an item.
  1275. This can also be configured on a per-file basis by adding one of
  1276. the following lines anywhere in the buffer:
  1277. #+STARTUP: lognoteclock-out
  1278. #+STARTUP: nolognoteclock-out"
  1279. :group 'org-todo
  1280. :group 'org-progress
  1281. :type 'boolean)
  1282. (defcustom org-log-done-with-time t
  1283. "Non-nil means, the CLOSED time stamp will contain date and time.
  1284. When nil, only the date will be recorded."
  1285. :group 'org-progress
  1286. :type 'boolean)
  1287. (defcustom org-log-note-headings
  1288. '((done . "CLOSING NOTE %t")
  1289. (state . "State %-12s %t")
  1290. (note . "Note taken on %t")
  1291. (clock-out . ""))
  1292. "Headings for notes added to entries.
  1293. The value is an alist, with the car being a symbol indicating the note
  1294. context, and the cdr is the heading to be used. The heading may also be the
  1295. empty string.
  1296. %t in the heading will be replaced by a time stamp.
  1297. %s will be replaced by the new TODO state, in double quotes.
  1298. %u will be replaced by the user name.
  1299. %U will be replaced by the full user name."
  1300. :group 'org-todo
  1301. :group 'org-progress
  1302. :type '(list :greedy t
  1303. (cons (const :tag "Heading when closing an item" done) string)
  1304. (cons (const :tag
  1305. "Heading when changing todo state (todo sequence only)"
  1306. state) string)
  1307. (cons (const :tag "Heading when just taking a note" note) string)
  1308. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1309. (unless (assq 'note org-log-note-headings)
  1310. (push '(note . "%t") org-log-note-headings))
  1311. (defcustom org-log-states-order-reversed t
  1312. "Non-nil means, the latest state change note will be directly after heading.
  1313. When nil, the notes will be orderer according to time."
  1314. :group 'org-todo
  1315. :group 'org-progress
  1316. :type 'boolean)
  1317. (defcustom org-log-repeat 'time
  1318. "Non-nil means, record moving through the DONE state when triggering repeat.
  1319. An auto-repeating tasks is immediately switched back to TODO when marked
  1320. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1321. the TODO keyword definition, or recording a closing note by setting
  1322. `org-log-done', there will be no record of the task moving through DONE.
  1323. This variable forces taking a note anyway. Possible values are:
  1324. nil Don't force a record
  1325. time Record a time stamp
  1326. note Record a note
  1327. This option can also be set with on a per-file-basis with
  1328. #+STARTUP: logrepeat
  1329. #+STARTUP: lognoterepeat
  1330. #+STARTUP: nologrepeat
  1331. You can have local logging settings for a subtree by setting the LOGGING
  1332. property to one or more of these keywords."
  1333. :group 'org-todo
  1334. :group 'org-progress
  1335. :type '(choice
  1336. (const :tag "Don't force a record" nil)
  1337. (const :tag "Force recording the DONE state" time)
  1338. (const :tag "Force recording a note with the DONE state" note)))
  1339. (defgroup org-priorities nil
  1340. "Priorities in Org-mode."
  1341. :tag "Org Priorities"
  1342. :group 'org-todo)
  1343. (defcustom org-highest-priority ?A
  1344. "The highest priority of TODO items. A character like ?A, ?B etc.
  1345. Must have a smaller ASCII number than `org-lowest-priority'."
  1346. :group 'org-priorities
  1347. :type 'character)
  1348. (defcustom org-lowest-priority ?C
  1349. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1350. Must have a larger ASCII number than `org-highest-priority'."
  1351. :group 'org-priorities
  1352. :type 'character)
  1353. (defcustom org-default-priority ?B
  1354. "The default priority of TODO items.
  1355. This is the priority an item get if no explicit priority is given."
  1356. :group 'org-priorities
  1357. :type 'character)
  1358. (defcustom org-priority-start-cycle-with-default t
  1359. "Non-nil means, start with default priority when starting to cycle.
  1360. When this is nil, the first step in the cycle will be (depending on the
  1361. command used) one higher or lower that the default priority."
  1362. :group 'org-priorities
  1363. :type 'boolean)
  1364. (defgroup org-time nil
  1365. "Options concerning time stamps and deadlines in Org-mode."
  1366. :tag "Org Time"
  1367. :group 'org)
  1368. (defcustom org-insert-labeled-timestamps-at-point nil
  1369. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1370. When nil, these labeled time stamps are forces into the second line of an
  1371. entry, just after the headline. When scheduling from the global TODO list,
  1372. the time stamp will always be forced into the second line."
  1373. :group 'org-time
  1374. :type 'boolean)
  1375. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1376. "Formats for `format-time-string' which are used for time stamps.
  1377. It is not recommended to change this constant.")
  1378. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1379. "Number of minutes to round time stamps to.
  1380. These are two values, the first applies when first creating a time stamp.
  1381. The second applies when changing it with the commands `S-up' and `S-down'.
  1382. When changing the time stamp, this means that it will change in steps
  1383. of N minutes, as given by the second value.
  1384. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1385. numbers should be factors of 60, so for example 5, 10, 15.
  1386. When this is larger than 1, you can still force an exact time-stamp by using
  1387. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1388. and by using a prefix arg to `S-up/down' to specify the exact number
  1389. of minutes to shift."
  1390. :group 'org-time
  1391. :get '(lambda (var) ; Make sure all entries have 5 elements
  1392. (if (integerp (default-value var))
  1393. (list (default-value var) 5)
  1394. (default-value var)))
  1395. :type '(list
  1396. (integer :tag "when inserting times")
  1397. (integer :tag "when modifying times")))
  1398. ;; Normalize old customizations of this variable.
  1399. (when (integerp org-time-stamp-rounding-minutes)
  1400. (setq org-time-stamp-rounding-minutes
  1401. (list org-time-stamp-rounding-minutes
  1402. org-time-stamp-rounding-minutes)))
  1403. (defcustom org-display-custom-times nil
  1404. "Non-nil means, overlay custom formats over all time stamps.
  1405. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1406. To turn this on on a per-file basis, insert anywhere in the file:
  1407. #+STARTUP: customtime"
  1408. :group 'org-time
  1409. :set 'set-default
  1410. :type 'sexp)
  1411. (make-variable-buffer-local 'org-display-custom-times)
  1412. (defcustom org-time-stamp-custom-formats
  1413. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1414. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1415. These are overlayed over the default ISO format if the variable
  1416. `org-display-custom-times' is set. Time like %H:%M should be at the
  1417. end of the second format."
  1418. :group 'org-time
  1419. :type 'sexp)
  1420. (defun org-time-stamp-format (&optional long inactive)
  1421. "Get the right format for a time string."
  1422. (let ((f (if long (cdr org-time-stamp-formats)
  1423. (car org-time-stamp-formats))))
  1424. (if inactive
  1425. (concat "[" (substring f 1 -1) "]")
  1426. f)))
  1427. (defcustom org-time-clocksum-format "%d:%02d"
  1428. "The format string used when creating CLOCKSUM lines, or when
  1429. org-mode generates a time duration."
  1430. :group 'org-time
  1431. :type 'string)
  1432. (defcustom org-deadline-warning-days 14
  1433. "No. of days before expiration during which a deadline becomes active.
  1434. This variable governs the display in sparse trees and in the agenda.
  1435. When 0 or negative, it means use this number (the absolute value of it)
  1436. even if a deadline has a different individual lead time specified."
  1437. :group 'org-time
  1438. :group 'org-agenda-daily/weekly
  1439. :type 'number)
  1440. (defcustom org-read-date-prefer-future t
  1441. "Non-nil means, assume future for incomplete date input from user.
  1442. This affects the following situations:
  1443. 1. The user gives a day, but no month.
  1444. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1445. read this as the third of *next* month. However, if you enter \"17\",
  1446. it will be considered as *this* month.
  1447. 2. The user gives a month but not a year.
  1448. For example, if it is april and you enter \"feb 2\", this will be read
  1449. as feb 2, *next* year. \"May 5\", however, will be this year.
  1450. Currently this does not work for ISO week specifications.
  1451. When this option is nil, the current month and year will always be used
  1452. as defaults."
  1453. :group 'org-time
  1454. :type 'boolean)
  1455. (defcustom org-read-date-display-live t
  1456. "Non-nil means, display current interpretation of date prompt live.
  1457. This display will be in an overlay, in the minibuffer."
  1458. :group 'org-time
  1459. :type 'boolean)
  1460. (defcustom org-read-date-popup-calendar t
  1461. "Non-nil means, pop up a calendar when prompting for a date.
  1462. In the calendar, the date can be selected with mouse-1. However, the
  1463. minibuffer will also be active, and you can simply enter the date as well.
  1464. When nil, only the minibuffer will be available."
  1465. :group 'org-time
  1466. :type 'boolean)
  1467. (if (fboundp 'defvaralias)
  1468. (defvaralias 'org-popup-calendar-for-date-prompt
  1469. 'org-read-date-popup-calendar))
  1470. (defcustom org-extend-today-until 0
  1471. "The hour when your day really ends.
  1472. This has influence for the following applications:
  1473. - When switching the agenda to \"today\". It it is still earlier than
  1474. the time given here, the day recognized as TODAY is actually yesterday.
  1475. - When a date is read from the user and it is still before the time given
  1476. here, the current date and time will be assumed to be yesterday, 23:59.
  1477. FIXME:
  1478. IMPORTANT: This is still a very experimental feature, it may disappear
  1479. again or it may be extended to mean more things."
  1480. :group 'org-time
  1481. :type 'number)
  1482. (defcustom org-edit-timestamp-down-means-later nil
  1483. "Non-nil means, S-down will increase the time in a time stamp.
  1484. When nil, S-up will increase."
  1485. :group 'org-time
  1486. :type 'boolean)
  1487. (defcustom org-calendar-follow-timestamp-change t
  1488. "Non-nil means, make the calendar window follow timestamp changes.
  1489. When a timestamp is modified and the calendar window is visible, it will be
  1490. moved to the new date."
  1491. :group 'org-time
  1492. :type 'boolean)
  1493. (defgroup org-tags nil
  1494. "Options concerning tags in Org-mode."
  1495. :tag "Org Tags"
  1496. :group 'org)
  1497. (defcustom org-tag-alist nil
  1498. "List of tags allowed in Org-mode files.
  1499. When this list is nil, Org-mode will base TAG input on what is already in the
  1500. buffer.
  1501. The value of this variable is an alist, the car of each entry must be a
  1502. keyword as a string, the cdr may be a character that is used to select
  1503. that tag through the fast-tag-selection interface.
  1504. See the manual for details."
  1505. :group 'org-tags
  1506. :type '(repeat
  1507. (choice
  1508. (cons (string :tag "Tag name")
  1509. (character :tag "Access char"))
  1510. (const :tag "Start radio group" (:startgroup))
  1511. (const :tag "End radio group" (:endgroup)))))
  1512. (defvar org-file-tags nil
  1513. "List of tags that can be inherited by all entries in the file.
  1514. The tags will be inherited if the variable `org-use-tag-inheritance'
  1515. says they should be.
  1516. This variable is populated from #+TAG lines.")
  1517. (defcustom org-use-fast-tag-selection 'auto
  1518. "Non-nil means, use fast tag selection scheme.
  1519. This is a special interface to select and deselect tags with single keys.
  1520. When nil, fast selection is never used.
  1521. When the symbol `auto', fast selection is used if and only if selection
  1522. characters for tags have been configured, either through the variable
  1523. `org-tag-alist' or through a #+TAGS line in the buffer.
  1524. When t, fast selection is always used and selection keys are assigned
  1525. automatically if necessary."
  1526. :group 'org-tags
  1527. :type '(choice
  1528. (const :tag "Always" t)
  1529. (const :tag "Never" nil)
  1530. (const :tag "When selection characters are configured" 'auto)))
  1531. (defcustom org-fast-tag-selection-single-key nil
  1532. "Non-nil means, fast tag selection exits after first change.
  1533. When nil, you have to press RET to exit it.
  1534. During fast tag selection, you can toggle this flag with `C-c'.
  1535. This variable can also have the value `expert'. In this case, the window
  1536. displaying the tags menu is not even shown, until you press C-c again."
  1537. :group 'org-tags
  1538. :type '(choice
  1539. (const :tag "No" nil)
  1540. (const :tag "Yes" t)
  1541. (const :tag "Expert" expert)))
  1542. (defvar org-fast-tag-selection-include-todo nil
  1543. "Non-nil means, fast tags selection interface will also offer TODO states.
  1544. This is an undocumented feature, you should not rely on it.")
  1545. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1546. "The column to which tags should be indented in a headline.
  1547. If this number is positive, it specifies the column. If it is negative,
  1548. it means that the tags should be flushright to that column. For example,
  1549. -80 works well for a normal 80 character screen."
  1550. :group 'org-tags
  1551. :type 'integer)
  1552. (defcustom org-auto-align-tags t
  1553. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1554. These operations change the length of a headline and therefore shift
  1555. the tags around. With this options turned on, after each such operation
  1556. the tags are again aligned to `org-tags-column'."
  1557. :group 'org-tags
  1558. :type 'boolean)
  1559. (defcustom org-use-tag-inheritance t
  1560. "Non-nil means, tags in levels apply also for sublevels.
  1561. When nil, only the tags directly given in a specific line apply there.
  1562. If this option is t, a match early-on in a tree can lead to a large
  1563. number of matches in the subtree. If you only want to see the first
  1564. match in a tree during a search, check out the variable
  1565. `org-tags-match-list-sublevels'.
  1566. This may also be a list of tags that should be inherited, or a regexp that
  1567. matches tags that should be inherited."
  1568. :group 'org-tags
  1569. :type '(choice
  1570. (const :tag "Not" nil)
  1571. (const :tag "Always" t)
  1572. (repeat :tag "Specific tags" (string :tag "Tag"))
  1573. (regexp :tag "Tags matched by regexp")))
  1574. (defun org-tag-inherit-p (tag)
  1575. "Check if TAG is one that should be inherited."
  1576. (cond
  1577. ((eq org-use-tag-inheritance t) t)
  1578. ((not org-use-tag-inheritance) nil)
  1579. ((stringp org-use-tag-inheritance)
  1580. (string-match org-use-tag-inheritance tag))
  1581. ((listp org-use-tag-inheritance)
  1582. (member tag org-use-tag-inheritance))
  1583. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1584. (defcustom org-tags-match-list-sublevels t
  1585. "Non-nil means list also sublevels of headlines matching tag search.
  1586. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1587. the sublevels of a headline matching a tag search often also match
  1588. the same search. Listing all of them can create very long lists.
  1589. Setting this variable to nil causes subtrees of a match to be skipped.
  1590. This option is off by default, because inheritance in on. If you turn
  1591. inheritance off, you very likely want to turn this option on.
  1592. As a special case, if the tag search is restricted to TODO items, the
  1593. value of this variable is ignored and sublevels are always checked, to
  1594. make sure all corresponding TODO items find their way into the list."
  1595. :group 'org-tags
  1596. :type 'boolean)
  1597. (defvar org-tags-history nil
  1598. "History of minibuffer reads for tags.")
  1599. (defvar org-last-tags-completion-table nil
  1600. "The last used completion table for tags.")
  1601. (defvar org-after-tags-change-hook nil
  1602. "Hook that is run after the tags in a line have changed.")
  1603. (defgroup org-properties nil
  1604. "Options concerning properties in Org-mode."
  1605. :tag "Org Properties"
  1606. :group 'org)
  1607. (defcustom org-property-format "%-10s %s"
  1608. "How property key/value pairs should be formatted by `indent-line'.
  1609. When `indent-line' hits a property definition, it will format the line
  1610. according to this format, mainly to make sure that the values are
  1611. lined-up with respect to each other."
  1612. :group 'org-properties
  1613. :type 'string)
  1614. (defcustom org-use-property-inheritance nil
  1615. "Non-nil means, properties apply also for sublevels.
  1616. This setting is chiefly used during property searches. Turning it on can
  1617. cause significant overhead when doing a search, which is why it is not
  1618. on by default.
  1619. When nil, only the properties directly given in the current entry count.
  1620. When t, every property is inherited. The value may also be a list of
  1621. properties that should have inheritance, or a regular expression matching
  1622. properties that should be inherited.
  1623. However, note that some special properties use inheritance under special
  1624. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1625. and the properties ending in \"_ALL\" when they are used as descriptor
  1626. for valid values of a property.
  1627. Note for programmers:
  1628. When querying an entry with `org-entry-get', you can control if inheritance
  1629. should be used. By default, `org-entry-get' looks only at the local
  1630. properties. You can request inheritance by setting the inherit argument
  1631. to t (to force inheritance) or to `selective' (to respect the setting
  1632. in this variable)."
  1633. :group 'org-properties
  1634. :type '(choice
  1635. (const :tag "Not" nil)
  1636. (const :tag "Always" t)
  1637. (repeat :tag "Specific properties" (string :tag "Property"))
  1638. (regexp :tag "Properties matched by regexp")))
  1639. (defun org-property-inherit-p (property)
  1640. "Check if PROPERTY is one that should be inherited."
  1641. (cond
  1642. ((eq org-use-property-inheritance t) t)
  1643. ((not org-use-property-inheritance) nil)
  1644. ((stringp org-use-property-inheritance)
  1645. (string-match org-use-property-inheritance property))
  1646. ((listp org-use-property-inheritance)
  1647. (member property org-use-property-inheritance))
  1648. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1649. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1650. "The default column format, if no other format has been defined.
  1651. This variable can be set on the per-file basis by inserting a line
  1652. #+COLUMNS: %25ITEM ....."
  1653. :group 'org-properties
  1654. :type 'string)
  1655. (defcustom org-columns-ellipses ".."
  1656. "The ellipses to be used when a field in column view is truncated.
  1657. When this is the empty string, as many characters as possible are shown,
  1658. but then there will be no visual indication that the field has been truncated.
  1659. When this is a string of length N, the last N characters of a truncated
  1660. field are replaced by this string. If the column is narrower than the
  1661. ellipses string, only part of the ellipses string will be shown."
  1662. :group 'org-properties
  1663. :type 'string)
  1664. (defcustom org-columns-modify-value-for-display-function nil
  1665. "Function that modifies values for display in column view.
  1666. For example, it can be used to cut out a certain part from a time stamp.
  1667. The function must take 2 argments:
  1668. column-title The tite of the column (*not* the property name)
  1669. value The value that should be modified.
  1670. The function should return the value that should be displayed,
  1671. or nil if the normal value should be used."
  1672. :group 'org-properties
  1673. :type 'function)
  1674. (defcustom org-effort-property "Effort"
  1675. "The property that is being used to keep track of effort estimates.
  1676. Effort estimates given in this property need to have the format H:MM."
  1677. :group 'org-properties
  1678. :group 'org-progress
  1679. :type '(string :tag "Property"))
  1680. (defconst org-global-properties-fixed
  1681. '(("VISIBILITY_ALL" . "folded children content all"))
  1682. "List of property/value pairs that can be inherited by any entry.
  1683. These are fixed values, for the preset properties.")
  1684. (defcustom org-global-properties nil
  1685. "List of property/value pairs that can be inherited by any entry.
  1686. You can set buffer-local values for this by adding lines like
  1687. #+PROPERTY: NAME VALUE"
  1688. :group 'org-properties
  1689. :type '(repeat
  1690. (cons (string :tag "Property")
  1691. (string :tag "Value"))))
  1692. (defvar org-file-properties nil
  1693. "List of property/value pairs that can be inherited by any entry.
  1694. Valid for the current buffer.
  1695. This variable is populated from #+PROPERTY lines.")
  1696. (make-variable-buffer-local 'org-file-properties)
  1697. (defgroup org-agenda nil
  1698. "Options concerning agenda views in Org-mode."
  1699. :tag "Org Agenda"
  1700. :group 'org)
  1701. (defvar org-category nil
  1702. "Variable used by org files to set a category for agenda display.
  1703. Such files should use a file variable to set it, for example
  1704. # -*- mode: org; org-category: \"ELisp\"
  1705. or contain a special line
  1706. #+CATEGORY: ELisp
  1707. If the file does not specify a category, then file's base name
  1708. is used instead.")
  1709. (make-variable-buffer-local 'org-category)
  1710. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1711. (defcustom org-agenda-files nil
  1712. "The files to be used for agenda display.
  1713. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1714. \\[org-remove-file]. You can also use customize to edit the list.
  1715. If an entry is a directory, all files in that directory that are matched by
  1716. `org-agenda-file-regexp' will be part of the file list.
  1717. If the value of the variable is not a list but a single file name, then
  1718. the list of agenda files is actually stored and maintained in that file, one
  1719. agenda file per line."
  1720. :group 'org-agenda
  1721. :type '(choice
  1722. (repeat :tag "List of files and directories" file)
  1723. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1724. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1725. "Regular expression to match files for `org-agenda-files'.
  1726. If any element in the list in that variable contains a directory instead
  1727. of a normal file, all files in that directory that are matched by this
  1728. regular expression will be included."
  1729. :group 'org-agenda
  1730. :type 'regexp)
  1731. (defcustom org-agenda-text-search-extra-files nil
  1732. "List of extra files to be searched by text search commands.
  1733. These files will be search in addition to the agenda files by the
  1734. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1735. Note that these files will only be searched for text search commands,
  1736. not for the other agenda views like todo lists, tag searches or the weekly
  1737. agenda. This variable is intended to list notes and possibly archive files
  1738. that should also be searched by these two commands.
  1739. In fact, if the first element in the list is the symbol `agenda-archives',
  1740. than all archive files of all agenda files will be added to the search
  1741. scope."
  1742. :group 'org-agenda
  1743. :type '(set :greedy t
  1744. (const :tag "Agenda Archives" agenda-archives)
  1745. (repeat :inline t (file))))
  1746. (if (fboundp 'defvaralias)
  1747. (defvaralias 'org-agenda-multi-occur-extra-files
  1748. 'org-agenda-text-search-extra-files))
  1749. (defcustom org-agenda-skip-unavailable-files nil
  1750. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1751. A nil value means to remove them, after a query, from the list."
  1752. :group 'org-agenda
  1753. :type 'boolean)
  1754. (defcustom org-calendar-to-agenda-key [?c]
  1755. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1756. The command `org-calendar-goto-agenda' will be bound to this key. The
  1757. default is the character `c' because then `c' can be used to switch back and
  1758. forth between agenda and calendar."
  1759. :group 'org-agenda
  1760. :type 'sexp)
  1761. (defcustom org-calendar-agenda-action-key [?k]
  1762. "The key to be installed in `calendar-mode-map' for agenda-action.
  1763. The command `org-agenda-action' will be bound to this key. The
  1764. default is the character `k' because we use the same key in the agenda."
  1765. :group 'org-agenda
  1766. :type 'sexp)
  1767. (eval-after-load "calendar"
  1768. '(progn
  1769. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1770. 'org-calendar-goto-agenda)
  1771. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1772. 'org-agenda-action)))
  1773. (defgroup org-latex nil
  1774. "Options for embedding LaTeX code into Org-mode."
  1775. :tag "Org LaTeX"
  1776. :group 'org)
  1777. (defcustom org-format-latex-options
  1778. '(:foreground default :background default :scale 1.0
  1779. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1780. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1781. "Options for creating images from LaTeX fragments.
  1782. This is a property list with the following properties:
  1783. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1784. `default' means use the foreground of the default face.
  1785. :background the background color, or \"Transparent\".
  1786. `default' means use the background of the default face.
  1787. :scale a scaling factor for the size of the images.
  1788. :html-foreground, :html-background, :html-scale
  1789. the same numbers for HTML export.
  1790. :matchers a list indicating which matchers should be used to
  1791. find LaTeX fragments. Valid members of this list are:
  1792. \"begin\" find environments
  1793. \"$\" find math expressions surrounded by $...$
  1794. \"$$\" find math expressions surrounded by $$....$$
  1795. \"\\(\" find math expressions surrounded by \\(...\\)
  1796. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1797. :group 'org-latex
  1798. :type 'plist)
  1799. (defcustom org-format-latex-header "\\documentclass{article}
  1800. \\usepackage{fullpage} % do not remove
  1801. \\usepackage{amssymb}
  1802. \\usepackage[usenames]{color}
  1803. \\usepackage{amsmath}
  1804. \\usepackage{latexsym}
  1805. \\usepackage[mathscr]{eucal}
  1806. \\pagestyle{empty} % do not remove"
  1807. "The document header used for processing LaTeX fragments."
  1808. :group 'org-latex
  1809. :type 'string)
  1810. (defgroup org-font-lock nil
  1811. "Font-lock settings for highlighting in Org-mode."
  1812. :tag "Org Font Lock"
  1813. :group 'org)
  1814. (defcustom org-level-color-stars-only nil
  1815. "Non-nil means fontify only the stars in each headline.
  1816. When nil, the entire headline is fontified.
  1817. Changing it requires restart of `font-lock-mode' to become effective
  1818. also in regions already fontified."
  1819. :group 'org-font-lock
  1820. :type 'boolean)
  1821. (defcustom org-hide-leading-stars nil
  1822. "Non-nil means, hide the first N-1 stars in a headline.
  1823. This works by using the face `org-hide' for these stars. This
  1824. face is white for a light background, and black for a dark
  1825. background. You may have to customize the face `org-hide' to
  1826. make this work.
  1827. Changing it requires restart of `font-lock-mode' to become effective
  1828. also in regions already fontified.
  1829. You may also set this on a per-file basis by adding one of the following
  1830. lines to the buffer:
  1831. #+STARTUP: hidestars
  1832. #+STARTUP: showstars"
  1833. :group 'org-font-lock
  1834. :type 'boolean)
  1835. (defcustom org-fontify-done-headline nil
  1836. "Non-nil means, change the face of a headline if it is marked DONE.
  1837. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1838. When this is non-nil, the headline after the keyword is set to the
  1839. `org-headline-done' as an additional indication."
  1840. :group 'org-font-lock
  1841. :type 'boolean)
  1842. (defcustom org-fontify-emphasized-text t
  1843. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1844. Changing this variable requires a restart of Emacs to take effect."
  1845. :group 'org-font-lock
  1846. :type 'boolean)
  1847. (defcustom org-highlight-latex-fragments-and-specials nil
  1848. "Non-nil means, fontify what is treated specially by the exporters."
  1849. :group 'org-font-lock
  1850. :type 'boolean)
  1851. (defcustom org-hide-emphasis-markers nil
  1852. "Non-nil mean font-lock should hide the emphasis marker characters."
  1853. :group 'org-font-lock
  1854. :type 'boolean)
  1855. (defvar org-emph-re nil
  1856. "Regular expression for matching emphasis.")
  1857. (defvar org-verbatim-re nil
  1858. "Regular expression for matching verbatim text.")
  1859. (defvar org-emphasis-regexp-components) ; defined just below
  1860. (defvar org-emphasis-alist) ; defined just below
  1861. (defun org-set-emph-re (var val)
  1862. "Set variable and compute the emphasis regular expression."
  1863. (set var val)
  1864. (when (and (boundp 'org-emphasis-alist)
  1865. (boundp 'org-emphasis-regexp-components)
  1866. org-emphasis-alist org-emphasis-regexp-components)
  1867. (let* ((e org-emphasis-regexp-components)
  1868. (pre (car e))
  1869. (post (nth 1 e))
  1870. (border (nth 2 e))
  1871. (body (nth 3 e))
  1872. (nl (nth 4 e))
  1873. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1874. (body1 (concat body "*?"))
  1875. (markers (mapconcat 'car org-emphasis-alist ""))
  1876. (vmarkers (mapconcat
  1877. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1878. org-emphasis-alist "")))
  1879. ;; make sure special characters appear at the right position in the class
  1880. (if (string-match "\\^" markers)
  1881. (setq markers (concat (replace-match "" t t markers) "^")))
  1882. (if (string-match "-" markers)
  1883. (setq markers (concat (replace-match "" t t markers) "-")))
  1884. (if (string-match "\\^" vmarkers)
  1885. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1886. (if (string-match "-" vmarkers)
  1887. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1888. (if (> nl 0)
  1889. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1890. (int-to-string nl) "\\}")))
  1891. ;; Make the regexp
  1892. (setq org-emph-re
  1893. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1894. "\\("
  1895. "\\([" markers "]\\)"
  1896. "\\("
  1897. "[^" border "]\\|"
  1898. "[^" border (if (and nil stacked) markers) "]"
  1899. body1
  1900. "[^" border (if (and nil stacked) markers) "]"
  1901. "\\)"
  1902. "\\3\\)"
  1903. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1904. (setq org-verbatim-re
  1905. (concat "\\([" pre "]\\|^\\)"
  1906. "\\("
  1907. "\\([" vmarkers "]\\)"
  1908. "\\("
  1909. "[^" border "]\\|"
  1910. "[^" border "]"
  1911. body1
  1912. "[^" border "]"
  1913. "\\)"
  1914. "\\3\\)"
  1915. "\\([" post "]\\|$\\)")))))
  1916. (defcustom org-emphasis-regexp-components
  1917. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1918. "Components used to build the regular expression for emphasis.
  1919. This is a list with 6 entries. Terminology: In an emphasis string
  1920. like \" *strong word* \", we call the initial space PREMATCH, the final
  1921. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1922. and \"trong wor\" is the body. The different components in this variable
  1923. specify what is allowed/forbidden in each part:
  1924. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1925. post Chars allowed as postmatch. End of line will be allowed too.
  1926. border The chars *forbidden* as border characters.
  1927. body-regexp A regexp like \".\" to match a body character. Don't use
  1928. non-shy groups here, and don't allow newline here.
  1929. newline The maximum number of newlines allowed in an emphasis exp.
  1930. Use customize to modify this, or restart Emacs after changing it."
  1931. :group 'org-font-lock
  1932. :set 'org-set-emph-re
  1933. :type '(list
  1934. (sexp :tag "Allowed chars in pre ")
  1935. (sexp :tag "Allowed chars in post ")
  1936. (sexp :tag "Forbidden chars in border ")
  1937. (sexp :tag "Regexp for body ")
  1938. (integer :tag "number of newlines allowed")
  1939. (option (boolean :tag "Please ignore this button"))))
  1940. (defcustom org-emphasis-alist
  1941. `(("*" bold "<b>" "</b>")
  1942. ("/" italic "<i>" "</i>")
  1943. ("_" underline "<u>" "</u>")
  1944. ("=" org-code "<code>" "</code>" verbatim)
  1945. ("~" org-verbatim "" "" verbatim)
  1946. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1947. "<del>" "</del>")
  1948. )
  1949. "Special syntax for emphasized text.
  1950. Text starting and ending with a special character will be emphasized, for
  1951. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1952. characters, the face to be used by font-lock for highlighting in Org-mode
  1953. Emacs buffers, and the HTML tags to be used for this.
  1954. Use customize to modify this, or restart Emacs after changing it."
  1955. :group 'org-font-lock
  1956. :set 'org-set-emph-re
  1957. :type '(repeat
  1958. (list
  1959. (string :tag "Marker character")
  1960. (choice
  1961. (face :tag "Font-lock-face")
  1962. (plist :tag "Face property list"))
  1963. (string :tag "HTML start tag")
  1964. (string :tag "HTML end tag")
  1965. (option (const verbatim)))))
  1966. ;;; Miscellaneous options
  1967. (defgroup org-completion nil
  1968. "Completion in Org-mode."
  1969. :tag "Org Completion"
  1970. :group 'org)
  1971. (defcustom org-completion-fallback-command 'hippie-expand
  1972. "The expansion command called by \\[org-complete] in normal context.
  1973. Normal means, no org-mode-specific context."
  1974. :group 'org-completion
  1975. :type 'function)
  1976. ;;; Functions and variables from ther packages
  1977. ;; Declared here to avoid compiler warnings
  1978. ;; XEmacs only
  1979. (defvar outline-mode-menu-heading)
  1980. (defvar outline-mode-menu-show)
  1981. (defvar outline-mode-menu-hide)
  1982. (defvar zmacs-regions) ; XEmacs regions
  1983. ;; Emacs only
  1984. (defvar mark-active)
  1985. ;; Various packages
  1986. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1987. (declare-function calendar-forward-day "cal-move" (arg))
  1988. (declare-function calendar-goto-date "cal-move" (date))
  1989. (declare-function calendar-goto-today "cal-move" ())
  1990. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1991. (defvar calc-embedded-close-formula)
  1992. (defvar calc-embedded-open-formula)
  1993. (declare-function cdlatex-tab "ext:cdlatex" ())
  1994. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1995. (defvar font-lock-unfontify-region-function)
  1996. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1997. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1998. (defvar iswitchb-temp-buflist)
  1999. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2000. (declare-function org-agenda-skip "org-agenda" ())
  2001. (declare-function org-format-agenda-item "org-agenda"
  2002. (extra txt &optional category tags dotime noprefix remove-re))
  2003. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2004. (declare-function org-agenda-change-all-lines "org-agenda"
  2005. (newhead hdmarker &optional fixface))
  2006. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2007. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2008. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2009. (beg end))
  2010. (declare-function parse-time-string "parse-time" (string))
  2011. (declare-function remember "remember" (&optional initial))
  2012. (declare-function remember-buffer-desc "remember" ())
  2013. (declare-function remember-finalize "remember" ())
  2014. (defvar remember-save-after-remembering)
  2015. (defvar remember-data-file)
  2016. (defvar remember-register)
  2017. (defvar remember-buffer)
  2018. (defvar remember-handler-functions)
  2019. (defvar remember-annotation-functions)
  2020. (defvar texmathp-why)
  2021. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2022. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2023. (defvar w3m-current-url)
  2024. (defvar w3m-current-title)
  2025. (defvar org-latex-regexps)
  2026. ;;; Autoload and prepare some org modules
  2027. ;; Some table stuff that needs to be defined here, because it is used
  2028. ;; by the functions setting up org-mode or checking for table context.
  2029. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2030. "Detects an org-type or table-type table.")
  2031. (defconst org-table-line-regexp "^[ \t]*|"
  2032. "Detects an org-type table line.")
  2033. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2034. "Detects an org-type table line.")
  2035. (defconst org-table-hline-regexp "^[ \t]*|-"
  2036. "Detects an org-type table hline.")
  2037. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2038. "Detects a table-type table hline.")
  2039. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2040. "Searching from within a table (any type) this finds the first line
  2041. outside the table.")
  2042. ;; Autoload the functions in org-table.el that are needed by functions here.
  2043. (eval-and-compile
  2044. (org-autoload "org-table"
  2045. '(org-table-align org-table-begin org-table-blank-field
  2046. org-table-convert org-table-convert-region org-table-copy-down
  2047. org-table-copy-region org-table-create
  2048. org-table-create-or-convert-from-region
  2049. org-table-create-with-table.el org-table-current-dline
  2050. org-table-cut-region org-table-delete-column org-table-edit-field
  2051. org-table-edit-formulas org-table-end org-table-eval-formula
  2052. org-table-export org-table-field-info
  2053. org-table-get-stored-formulas org-table-goto-column
  2054. org-table-hline-and-move org-table-import org-table-insert-column
  2055. org-table-insert-hline org-table-insert-row org-table-iterate
  2056. org-table-justify-field-maybe org-table-kill-row
  2057. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2058. org-table-move-column org-table-move-column-left
  2059. org-table-move-column-right org-table-move-row
  2060. org-table-move-row-down org-table-move-row-up
  2061. org-table-next-field org-table-next-row org-table-paste-rectangle
  2062. org-table-previous-field org-table-recalculate
  2063. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2064. org-table-toggle-coordinate-overlays
  2065. org-table-toggle-formula-debugger org-table-wrap-region
  2066. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2067. (defun org-at-table-p (&optional table-type)
  2068. "Return t if the cursor is inside an org-type table.
  2069. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2070. (if org-enable-table-editor
  2071. (save-excursion
  2072. (beginning-of-line 1)
  2073. (looking-at (if table-type org-table-any-line-regexp
  2074. org-table-line-regexp)))
  2075. nil))
  2076. (defsubst org-table-p () (org-at-table-p))
  2077. (defun org-at-table.el-p ()
  2078. "Return t if and only if we are at a table.el table."
  2079. (and (org-at-table-p 'any)
  2080. (save-excursion
  2081. (goto-char (org-table-begin 'any))
  2082. (looking-at org-table1-hline-regexp))))
  2083. (defun org-table-recognize-table.el ()
  2084. "If there is a table.el table nearby, recognize it and move into it."
  2085. (if org-table-tab-recognizes-table.el
  2086. (if (org-at-table.el-p)
  2087. (progn
  2088. (beginning-of-line 1)
  2089. (if (looking-at org-table-dataline-regexp)
  2090. nil
  2091. (if (looking-at org-table1-hline-regexp)
  2092. (progn
  2093. (beginning-of-line 2)
  2094. (if (looking-at org-table-any-border-regexp)
  2095. (beginning-of-line -1)))))
  2096. (if (re-search-forward "|" (org-table-end t) t)
  2097. (progn
  2098. (require 'table)
  2099. (if (table--at-cell-p (point))
  2100. t
  2101. (message "recognizing table.el table...")
  2102. (table-recognize-table)
  2103. (message "recognizing table.el table...done")))
  2104. (error "This should not happen..."))
  2105. t)
  2106. nil)
  2107. nil))
  2108. (defun org-at-table-hline-p ()
  2109. "Return t if the cursor is inside a hline in a table."
  2110. (if org-enable-table-editor
  2111. (save-excursion
  2112. (beginning-of-line 1)
  2113. (looking-at org-table-hline-regexp))
  2114. nil))
  2115. (defvar org-table-clean-did-remove-column nil)
  2116. (defun org-table-map-tables (function)
  2117. "Apply FUNCTION to the start of all tables in the buffer."
  2118. (save-excursion
  2119. (save-restriction
  2120. (widen)
  2121. (goto-char (point-min))
  2122. (while (re-search-forward org-table-any-line-regexp nil t)
  2123. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2124. (beginning-of-line 1)
  2125. (if (looking-at org-table-line-regexp)
  2126. (save-excursion (funcall function)))
  2127. (re-search-forward org-table-any-border-regexp nil 1))))
  2128. (message "Mapping tables: done"))
  2129. ;; Declare and autoload functions from org-exp.el
  2130. (declare-function org-default-export-plist "org-exp")
  2131. (declare-function org-infile-export-plist "org-exp")
  2132. (declare-function org-get-current-options "org-exp")
  2133. (eval-and-compile
  2134. (org-autoload "org-exp"
  2135. '(org-export org-export-as-ascii org-export-visible
  2136. org-insert-export-options-template org-export-as-html-and-open
  2137. org-export-as-html-batch org-export-as-html-to-buffer
  2138. org-replace-region-by-html org-export-region-as-html
  2139. org-export-as-html org-export-icalendar-this-file
  2140. org-export-icalendar-all-agenda-files
  2141. org-table-clean-before-export
  2142. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2143. ;; Declare and autoload functions from org-exp.el
  2144. (eval-and-compile
  2145. (org-autoload "org-exp"
  2146. '(org-agenda org-agenda-list org-search-view
  2147. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2148. org-diary org-agenda-to-appt)))
  2149. ;; Autoload org-remember
  2150. (eval-and-compile
  2151. (org-autoload "org-remember"
  2152. '(org-remember-insinuate org-remember-annotation
  2153. org-remember-apply-template org-remember org-remember-handler)))
  2154. ;; Autoload org-clock.el
  2155. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2156. (beg end))
  2157. (declare-function org-update-mode-line "org-clock" ())
  2158. (defvar org-clock-start-time)
  2159. (defvar org-clock-marker (make-marker)
  2160. "Marker recording the last clock-in.")
  2161. (eval-and-compile
  2162. (org-autoload
  2163. "org-clock"
  2164. '(org-clock-in org-clock-out org-clock-cancel
  2165. org-clock-goto org-clock-sum org-clock-display
  2166. org-remove-clock-overlays org-clock-report
  2167. org-clocktable-shift org-dblock-write:clocktable
  2168. org-get-clocktable)))
  2169. (defun org-clock-update-time-maybe ()
  2170. "If this is a CLOCK line, update it and return t.
  2171. Otherwise, return nil."
  2172. (interactive)
  2173. (save-excursion
  2174. (beginning-of-line 1)
  2175. (skip-chars-forward " \t")
  2176. (when (looking-at org-clock-string)
  2177. (let ((re (concat "[ \t]*" org-clock-string
  2178. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2179. "\\([ \t]*=>.*\\)?\\)?"))
  2180. ts te h m s)
  2181. (cond
  2182. ((not (looking-at re))
  2183. nil)
  2184. ((not (match-end 2))
  2185. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2186. (> org-clock-marker (point))
  2187. (<= org-clock-marker (point-at-eol)))
  2188. ;; The clock is running here
  2189. (setq org-clock-start-time
  2190. (apply 'encode-time
  2191. (org-parse-time-string (match-string 1))))
  2192. (org-update-mode-line)))
  2193. (t
  2194. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2195. (end-of-line 1)
  2196. (setq ts (match-string 1)
  2197. te (match-string 3))
  2198. (setq s (- (time-to-seconds
  2199. (apply 'encode-time (org-parse-time-string te)))
  2200. (time-to-seconds
  2201. (apply 'encode-time (org-parse-time-string ts))))
  2202. h (floor (/ s 3600))
  2203. s (- s (* 3600 h))
  2204. m (floor (/ s 60))
  2205. s (- s (* 60 s)))
  2206. (insert " => " (format "%2d:%02d" h m))
  2207. t))))))
  2208. (defun org-check-running-clock ()
  2209. "Check if the current buffer contains the running clock.
  2210. If yes, offer to stop it and to save the buffer with the changes."
  2211. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2212. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2213. (buffer-name))))
  2214. (org-clock-out)
  2215. (when (y-or-n-p "Save changed buffer?")
  2216. (save-buffer))))
  2217. (defun org-clocktable-try-shift (dir n)
  2218. "Check if this line starts a clock table, if yes, shift the time block."
  2219. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2220. (org-clocktable-shift dir n)))
  2221. ;; Autoload archiving code
  2222. ;; The stuff that is needed for cycling and tags has to be defined here.
  2223. (defgroup org-archive nil
  2224. "Options concerning archiving in Org-mode."
  2225. :tag "Org Archive"
  2226. :group 'org-structure)
  2227. (defcustom org-archive-location "%s_archive::"
  2228. "The location where subtrees should be archived.
  2229. Otherwise, the value of this variable is a string, consisting of two
  2230. parts, separated by a double-colon.
  2231. The first part is a file name - when omitted, archiving happens in the same
  2232. file. %s will be replaced by the current file name (without directory part).
  2233. Archiving to a different file is useful to keep archived entries from
  2234. contributing to the Org-mode Agenda.
  2235. The part after the double colon is a headline. The archived entries will be
  2236. filed under that headline. When omitted, the subtrees are simply filed away
  2237. at the end of the file, as top-level entries.
  2238. Here are a few examples:
  2239. \"%s_archive::\"
  2240. If the current file is Projects.org, archive in file
  2241. Projects.org_archive, as top-level trees. This is the default.
  2242. \"::* Archived Tasks\"
  2243. Archive in the current file, under the top-level headline
  2244. \"* Archived Tasks\".
  2245. \"~/org/archive.org::\"
  2246. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2247. \"basement::** Finished Tasks\"
  2248. Archive in file ./basement (relative path), as level 3 trees
  2249. below the level 2 heading \"** Finished Tasks\".
  2250. You may set this option on a per-file basis by adding to the buffer a
  2251. line like
  2252. #+ARCHIVE: basement::** Finished Tasks
  2253. You may also define it locally for a subtree by setting an ARCHIVE property
  2254. in the entry. If such a property is found in an entry, or anywhere up
  2255. the hierarchy, it will be used."
  2256. :group 'org-archive
  2257. :type 'string)
  2258. (defcustom org-archive-tag "ARCHIVE"
  2259. "The tag that marks a subtree as archived.
  2260. An archived subtree does not open during visibility cycling, and does
  2261. not contribute to the agenda listings.
  2262. After changing this, font-lock must be restarted in the relevant buffers to
  2263. get the proper fontification."
  2264. :group 'org-archive
  2265. :group 'org-keywords
  2266. :type 'string)
  2267. (defcustom org-agenda-skip-archived-trees t
  2268. "Non-nil means, the agenda will skip any items located in archived trees.
  2269. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2270. variable is no longer recommended, you should leave it at the value t.
  2271. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2272. :group 'org-archive
  2273. :group 'org-agenda-skip
  2274. :type 'boolean)
  2275. (defcustom org-cycle-open-archived-trees nil
  2276. "Non-nil means, `org-cycle' will open archived trees.
  2277. An archived tree is a tree marked with the tag ARCHIVE.
  2278. When nil, archived trees will stay folded. You can still open them with
  2279. normal outline commands like `show-all', but not with the cycling commands."
  2280. :group 'org-archive
  2281. :group 'org-cycle
  2282. :type 'boolean)
  2283. (defcustom org-sparse-tree-open-archived-trees nil
  2284. "Non-nil means sparse tree construction shows matches in archived trees.
  2285. When nil, matches in these trees are highlighted, but the trees are kept in
  2286. collapsed state."
  2287. :group 'org-archive
  2288. :group 'org-sparse-trees
  2289. :type 'boolean)
  2290. (defun org-cycle-hide-archived-subtrees (state)
  2291. "Re-hide all archived subtrees after a visibility state change."
  2292. (when (and (not org-cycle-open-archived-trees)
  2293. (not (memq state '(overview folded))))
  2294. (save-excursion
  2295. (let* ((globalp (memq state '(contents all)))
  2296. (beg (if globalp (point-min) (point)))
  2297. (end (if globalp (point-max) (org-end-of-subtree t))))
  2298. (org-hide-archived-subtrees beg end)
  2299. (goto-char beg)
  2300. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2301. (message "%s" (substitute-command-keys
  2302. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2303. (defun org-force-cycle-archived ()
  2304. "Cycle subtree even if it is archived."
  2305. (interactive)
  2306. (setq this-command 'org-cycle)
  2307. (let ((org-cycle-open-archived-trees t))
  2308. (call-interactively 'org-cycle)))
  2309. (defun org-hide-archived-subtrees (beg end)
  2310. "Re-hide all archived subtrees after a visibility state change."
  2311. (save-excursion
  2312. (let* ((re (concat ":" org-archive-tag ":")))
  2313. (goto-char beg)
  2314. (while (re-search-forward re end t)
  2315. (and (org-on-heading-p) (hide-subtree))
  2316. (org-end-of-subtree t)))))
  2317. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2318. (eval-and-compile
  2319. (org-autoload "org-archive"
  2320. '(org-add-archive-files org-archive-subtree
  2321. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2322. ;; Autoload Column View Code
  2323. (declare-function org-columns-number-to-string "org-colview")
  2324. (declare-function org-columns-get-format-and-top-level "org-colview")
  2325. (declare-function org-columns-compute "org-colview")
  2326. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2327. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2328. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2329. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2330. ;; Autoload ID code
  2331. (org-autoload "org-id"
  2332. '(org-id-get-create org-id-new org-id-copy org-id-get
  2333. org-id-get-with-outline-path-completion
  2334. org-id-get-with-outline-drilling
  2335. org-id-goto org-id-find))
  2336. ;;; Variables for pre-computed regular expressions, all buffer local
  2337. (defvar org-drawer-regexp nil
  2338. "Matches first line of a hidden block.")
  2339. (make-variable-buffer-local 'org-drawer-regexp)
  2340. (defvar org-todo-regexp nil
  2341. "Matches any of the TODO state keywords.")
  2342. (make-variable-buffer-local 'org-todo-regexp)
  2343. (defvar org-not-done-regexp nil
  2344. "Matches any of the TODO state keywords except the last one.")
  2345. (make-variable-buffer-local 'org-not-done-regexp)
  2346. (defvar org-todo-line-regexp nil
  2347. "Matches a headline and puts TODO state into group 2 if present.")
  2348. (make-variable-buffer-local 'org-todo-line-regexp)
  2349. (defvar org-complex-heading-regexp nil
  2350. "Matches a headline and puts everything into groups:
  2351. group 1: the stars
  2352. group 2: The todo keyword, maybe
  2353. group 3: Priority cookie
  2354. group 4: True headline
  2355. group 5: Tags")
  2356. (make-variable-buffer-local 'org-complex-heading-regexp)
  2357. (defvar org-todo-line-tags-regexp nil
  2358. "Matches a headline and puts TODO state into group 2 if present.
  2359. Also put tags into group 4 if tags are present.")
  2360. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2361. (defvar org-nl-done-regexp nil
  2362. "Matches newline followed by a headline with the DONE keyword.")
  2363. (make-variable-buffer-local 'org-nl-done-regexp)
  2364. (defvar org-looking-at-done-regexp nil
  2365. "Matches the DONE keyword a point.")
  2366. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2367. (defvar org-ds-keyword-length 12
  2368. "Maximum length of the Deadline and SCHEDULED keywords.")
  2369. (make-variable-buffer-local 'org-ds-keyword-length)
  2370. (defvar org-deadline-regexp nil
  2371. "Matches the DEADLINE keyword.")
  2372. (make-variable-buffer-local 'org-deadline-regexp)
  2373. (defvar org-deadline-time-regexp nil
  2374. "Matches the DEADLINE keyword together with a time stamp.")
  2375. (make-variable-buffer-local 'org-deadline-time-regexp)
  2376. (defvar org-deadline-line-regexp nil
  2377. "Matches the DEADLINE keyword and the rest of the line.")
  2378. (make-variable-buffer-local 'org-deadline-line-regexp)
  2379. (defvar org-scheduled-regexp nil
  2380. "Matches the SCHEDULED keyword.")
  2381. (make-variable-buffer-local 'org-scheduled-regexp)
  2382. (defvar org-scheduled-time-regexp nil
  2383. "Matches the SCHEDULED keyword together with a time stamp.")
  2384. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2385. (defvar org-closed-time-regexp nil
  2386. "Matches the CLOSED keyword together with a time stamp.")
  2387. (make-variable-buffer-local 'org-closed-time-regexp)
  2388. (defvar org-keyword-time-regexp nil
  2389. "Matches any of the 4 keywords, together with the time stamp.")
  2390. (make-variable-buffer-local 'org-keyword-time-regexp)
  2391. (defvar org-keyword-time-not-clock-regexp nil
  2392. "Matches any of the 3 keywords, together with the time stamp.")
  2393. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2394. (defvar org-maybe-keyword-time-regexp nil
  2395. "Matches a timestamp, possibly preceeded by a keyword.")
  2396. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2397. (defvar org-planning-or-clock-line-re nil
  2398. "Matches a line with planning or clock info.")
  2399. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2400. (defconst org-plain-time-of-day-regexp
  2401. (concat
  2402. "\\(\\<[012]?[0-9]"
  2403. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2404. "\\(--?"
  2405. "\\(\\<[012]?[0-9]"
  2406. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2407. "\\)?")
  2408. "Regular expression to match a plain time or time range.
  2409. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2410. groups carry important information:
  2411. 0 the full match
  2412. 1 the first time, range or not
  2413. 8 the second time, if it is a range.")
  2414. (defconst org-plain-time-extension-regexp
  2415. (concat
  2416. "\\(\\<[012]?[0-9]"
  2417. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2418. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2419. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2420. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2421. groups carry important information:
  2422. 0 the full match
  2423. 7 hours of duration
  2424. 9 minutes of duration")
  2425. (defconst org-stamp-time-of-day-regexp
  2426. (concat
  2427. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2428. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2429. "\\(--?"
  2430. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2431. "Regular expression to match a timestamp time or time range.
  2432. After a match, the following groups carry important information:
  2433. 0 the full match
  2434. 1 date plus weekday, for backreferencing to make sure both times on same day
  2435. 2 the first time, range or not
  2436. 4 the second time, if it is a range.")
  2437. (defconst org-startup-options
  2438. '(("fold" org-startup-folded t)
  2439. ("overview" org-startup-folded t)
  2440. ("nofold" org-startup-folded nil)
  2441. ("showall" org-startup-folded nil)
  2442. ("content" org-startup-folded content)
  2443. ("hidestars" org-hide-leading-stars t)
  2444. ("showstars" org-hide-leading-stars nil)
  2445. ("odd" org-odd-levels-only t)
  2446. ("oddeven" org-odd-levels-only nil)
  2447. ("align" org-startup-align-all-tables t)
  2448. ("noalign" org-startup-align-all-tables nil)
  2449. ("customtime" org-display-custom-times t)
  2450. ("logdone" org-log-done time)
  2451. ("lognotedone" org-log-done note)
  2452. ("nologdone" org-log-done nil)
  2453. ("lognoteclock-out" org-log-note-clock-out t)
  2454. ("nolognoteclock-out" org-log-note-clock-out nil)
  2455. ("logrepeat" org-log-repeat state)
  2456. ("lognoterepeat" org-log-repeat note)
  2457. ("nologrepeat" org-log-repeat nil)
  2458. ("constcgs" constants-unit-system cgs)
  2459. ("constSI" constants-unit-system SI))
  2460. "Variable associated with STARTUP options for org-mode.
  2461. Each element is a list of three items: The startup options as written
  2462. in the #+STARTUP line, the corresponding variable, and the value to
  2463. set this variable to if the option is found. An optional forth element PUSH
  2464. means to push this value onto the list in the variable.")
  2465. (defun org-set-regexps-and-options ()
  2466. "Precompute regular expressions for current buffer."
  2467. (when (org-mode-p)
  2468. (org-set-local 'org-todo-kwd-alist nil)
  2469. (org-set-local 'org-todo-key-alist nil)
  2470. (org-set-local 'org-todo-key-trigger nil)
  2471. (org-set-local 'org-todo-keywords-1 nil)
  2472. (org-set-local 'org-done-keywords nil)
  2473. (org-set-local 'org-todo-heads nil)
  2474. (org-set-local 'org-todo-sets nil)
  2475. (org-set-local 'org-todo-log-states nil)
  2476. (org-set-local 'org-file-properties nil)
  2477. (org-set-local 'org-file-tags nil)
  2478. (let ((re (org-make-options-regexp
  2479. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2480. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2481. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2482. (splitre "[ \t]+")
  2483. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2484. tail sep kws1 prio props ftags drawers
  2485. ext-setup-or-nil setup-contents (start 0))
  2486. (save-excursion
  2487. (save-restriction
  2488. (widen)
  2489. (goto-char (point-min))
  2490. (while (or (and ext-setup-or-nil
  2491. (string-match re ext-setup-or-nil start)
  2492. (setq start (match-end 0)))
  2493. (and (setq ext-setup-or-nil nil start 0)
  2494. (re-search-forward re nil t)))
  2495. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2496. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2497. (cond
  2498. ((equal key "CATEGORY")
  2499. (if (string-match "[ \t]+$" value)
  2500. (setq value (replace-match "" t t value)))
  2501. (setq cat value))
  2502. ((member key '("SEQ_TODO" "TODO"))
  2503. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2504. ((equal key "TYP_TODO")
  2505. (push (cons 'type (org-split-string value splitre)) kwds))
  2506. ((equal key "TAGS")
  2507. (setq tags (append tags (org-split-string value splitre))))
  2508. ((equal key "COLUMNS")
  2509. (org-set-local 'org-columns-default-format value))
  2510. ((equal key "LINK")
  2511. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2512. (push (cons (match-string 1 value)
  2513. (org-trim (match-string 2 value)))
  2514. links)))
  2515. ((equal key "PRIORITIES")
  2516. (setq prio (org-split-string value " +")))
  2517. ((equal key "PROPERTY")
  2518. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2519. (push (cons (match-string 1 value) (match-string 2 value))
  2520. props)))
  2521. ((equal key "FILETAGS")
  2522. (when (string-match "\\S-" value)
  2523. (setq ftags
  2524. (append
  2525. ftags
  2526. (apply 'append
  2527. (mapcar (lambda (x) (org-split-string x ":"))
  2528. (org-split-string value)))))))
  2529. ((equal key "DRAWERS")
  2530. (setq drawers (org-split-string value splitre)))
  2531. ((equal key "CONSTANTS")
  2532. (setq const (append const (org-split-string value splitre))))
  2533. ((equal key "STARTUP")
  2534. (let ((opts (org-split-string value splitre))
  2535. l var val)
  2536. (while (setq l (pop opts))
  2537. (when (setq l (assoc l org-startup-options))
  2538. (setq var (nth 1 l) val (nth 2 l))
  2539. (if (not (nth 3 l))
  2540. (set (make-local-variable var) val)
  2541. (if (not (listp (symbol-value var)))
  2542. (set (make-local-variable var) nil))
  2543. (set (make-local-variable var) (symbol-value var))
  2544. (add-to-list var val))))))
  2545. ((equal key "ARCHIVE")
  2546. (string-match " *$" value)
  2547. (setq arch (replace-match "" t t value))
  2548. (remove-text-properties 0 (length arch)
  2549. '(face t fontified t) arch))
  2550. ((equal key "SETUPFILE")
  2551. (setq setup-contents (org-file-contents
  2552. (expand-file-name
  2553. (org-remove-double-quotes value))
  2554. 'noerror))
  2555. (if (not ext-setup-or-nil)
  2556. (setq ext-setup-or-nil setup-contents start 0)
  2557. (setq ext-setup-or-nil
  2558. (concat (substring ext-setup-or-nil 0 start)
  2559. "\n" setup-contents "\n"
  2560. (substring ext-setup-or-nil start)))))
  2561. ))))
  2562. (when cat
  2563. (org-set-local 'org-category (intern cat))
  2564. (push (cons "CATEGORY" cat) props))
  2565. (when prio
  2566. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2567. (setq prio (mapcar 'string-to-char prio))
  2568. (org-set-local 'org-highest-priority (nth 0 prio))
  2569. (org-set-local 'org-lowest-priority (nth 1 prio))
  2570. (org-set-local 'org-default-priority (nth 2 prio)))
  2571. (and props (org-set-local 'org-file-properties (nreverse props)))
  2572. (and ftags (org-set-local 'org-file-tags ftags))
  2573. (and drawers (org-set-local 'org-drawers drawers))
  2574. (and arch (org-set-local 'org-archive-location arch))
  2575. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2576. ;; Process the TODO keywords
  2577. (unless kwds
  2578. ;; Use the global values as if they had been given locally.
  2579. (setq kwds (default-value 'org-todo-keywords))
  2580. (if (stringp (car kwds))
  2581. (setq kwds (list (cons org-todo-interpretation
  2582. (default-value 'org-todo-keywords)))))
  2583. (setq kwds (reverse kwds)))
  2584. (setq kwds (nreverse kwds))
  2585. (let (inter kws kw)
  2586. (while (setq kws (pop kwds))
  2587. (setq inter (pop kws) sep (member "|" kws)
  2588. kws0 (delete "|" (copy-sequence kws))
  2589. kwsa nil
  2590. kws1 (mapcar
  2591. (lambda (x)
  2592. ;; 1 2
  2593. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2594. (progn
  2595. (setq kw (match-string 1 x)
  2596. key (and (match-end 2) (match-string 2 x))
  2597. log (org-extract-log-state-settings x))
  2598. (push (cons kw (and key (string-to-char key))) kwsa)
  2599. (and log (push log org-todo-log-states))
  2600. kw)
  2601. (error "Invalid TODO keyword %s" x)))
  2602. kws0)
  2603. kwsa (if kwsa (append '((:startgroup))
  2604. (nreverse kwsa)
  2605. '((:endgroup))))
  2606. hw (car kws1)
  2607. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2608. tail (list inter hw (car dws) (org-last dws)))
  2609. (add-to-list 'org-todo-heads hw 'append)
  2610. (push kws1 org-todo-sets)
  2611. (setq org-done-keywords (append org-done-keywords dws nil))
  2612. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2613. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2614. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2615. (setq org-todo-sets (nreverse org-todo-sets)
  2616. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2617. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2618. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2619. ;; Process the constants
  2620. (when const
  2621. (let (e cst)
  2622. (while (setq e (pop const))
  2623. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2624. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2625. (setq org-table-formula-constants-local cst)))
  2626. ;; Process the tags.
  2627. (when tags
  2628. (let (e tgs)
  2629. (while (setq e (pop tags))
  2630. (cond
  2631. ((equal e "{") (push '(:startgroup) tgs))
  2632. ((equal e "}") (push '(:endgroup) tgs))
  2633. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2634. (push (cons (match-string 1 e)
  2635. (string-to-char (match-string 2 e)))
  2636. tgs))
  2637. (t (push (list e) tgs))))
  2638. (org-set-local 'org-tag-alist nil)
  2639. (while (setq e (pop tgs))
  2640. (or (and (stringp (car e))
  2641. (assoc (car e) org-tag-alist))
  2642. (push e org-tag-alist)))))
  2643. ;; Compute the regular expressions and other local variables
  2644. (if (not org-done-keywords)
  2645. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2646. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2647. (length org-scheduled-string)
  2648. (length org-clock-string)
  2649. (length org-closed-string)))
  2650. org-drawer-regexp
  2651. (concat "^[ \t]*:\\("
  2652. (mapconcat 'regexp-quote org-drawers "\\|")
  2653. "\\):[ \t]*$")
  2654. org-not-done-keywords
  2655. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2656. org-todo-regexp
  2657. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2658. "\\|") "\\)\\>")
  2659. org-not-done-regexp
  2660. (concat "\\<\\("
  2661. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2662. "\\)\\>")
  2663. org-todo-line-regexp
  2664. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2665. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2666. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2667. org-complex-heading-regexp
  2668. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2669. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2670. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2671. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2672. org-nl-done-regexp
  2673. (concat "\n\\*+[ \t]+"
  2674. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2675. "\\)" "\\>")
  2676. org-todo-line-tags-regexp
  2677. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2678. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2679. (org-re
  2680. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2681. org-looking-at-done-regexp
  2682. (concat "^" "\\(?:"
  2683. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2684. "\\>")
  2685. org-deadline-regexp (concat "\\<" org-deadline-string)
  2686. org-deadline-time-regexp
  2687. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2688. org-deadline-line-regexp
  2689. (concat "\\<\\(" org-deadline-string "\\).*")
  2690. org-scheduled-regexp
  2691. (concat "\\<" org-scheduled-string)
  2692. org-scheduled-time-regexp
  2693. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2694. org-closed-time-regexp
  2695. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2696. org-keyword-time-regexp
  2697. (concat "\\<\\(" org-scheduled-string
  2698. "\\|" org-deadline-string
  2699. "\\|" org-closed-string
  2700. "\\|" org-clock-string "\\)"
  2701. " *[[<]\\([^]>]+\\)[]>]")
  2702. org-keyword-time-not-clock-regexp
  2703. (concat "\\<\\(" org-scheduled-string
  2704. "\\|" org-deadline-string
  2705. "\\|" org-closed-string
  2706. "\\)"
  2707. " *[[<]\\([^]>]+\\)[]>]")
  2708. org-maybe-keyword-time-regexp
  2709. (concat "\\(\\<\\(" org-scheduled-string
  2710. "\\|" org-deadline-string
  2711. "\\|" org-closed-string
  2712. "\\|" org-clock-string "\\)\\)?"
  2713. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2714. org-planning-or-clock-line-re
  2715. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2716. "\\|" org-deadline-string
  2717. "\\|" org-closed-string "\\|" org-clock-string
  2718. "\\)\\>\\)")
  2719. )
  2720. (org-compute-latex-and-specials-regexp)
  2721. (org-set-font-lock-defaults))))
  2722. (defun org-file-contents (file &optional noerror)
  2723. "Return the contents of FILE, as a string."
  2724. (if (or (not file)
  2725. (not (file-readable-p file)))
  2726. (if noerror
  2727. (progn
  2728. (message "Cannot read file %s" file)
  2729. (ding) (sit-for 2)
  2730. "")
  2731. (error "Cannot read file %s" file))
  2732. (with-temp-buffer
  2733. (insert-file-contents file)
  2734. (buffer-string))))
  2735. (defun org-extract-log-state-settings (x)
  2736. "Extract the log state setting from a TODO keyword string.
  2737. This will extract info from a string like \"WAIT(w@/!)\"."
  2738. (let (kw key log1 log2)
  2739. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2740. (setq kw (match-string 1 x)
  2741. key (and (match-end 2) (match-string 2 x))
  2742. log1 (and (match-end 3) (match-string 3 x))
  2743. log2 (and (match-end 4) (match-string 4 x)))
  2744. (and (or log1 log2)
  2745. (list kw
  2746. (and log1 (if (equal log1 "!") 'time 'note))
  2747. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2748. (defun org-remove-keyword-keys (list)
  2749. "Remove a pair of parenthesis at the end of each string in LIST."
  2750. (mapcar (lambda (x)
  2751. (if (string-match "(.*)$" x)
  2752. (substring x 0 (match-beginning 0))
  2753. x))
  2754. list))
  2755. ;; FIXME: this could be done much better, using second characters etc.
  2756. (defun org-assign-fast-keys (alist)
  2757. "Assign fast keys to a keyword-key alist.
  2758. Respect keys that are already there."
  2759. (let (new e k c c1 c2 (char ?a))
  2760. (while (setq e (pop alist))
  2761. (cond
  2762. ((equal e '(:startgroup)) (push e new))
  2763. ((equal e '(:endgroup)) (push e new))
  2764. (t
  2765. (setq k (car e) c2 nil)
  2766. (if (cdr e)
  2767. (setq c (cdr e))
  2768. ;; automatically assign a character.
  2769. (setq c1 (string-to-char
  2770. (downcase (substring
  2771. k (if (= (string-to-char k) ?@) 1 0)))))
  2772. (if (or (rassoc c1 new) (rassoc c1 alist))
  2773. (while (or (rassoc char new) (rassoc char alist))
  2774. (setq char (1+ char)))
  2775. (setq c2 c1))
  2776. (setq c (or c2 char)))
  2777. (push (cons k c) new))))
  2778. (nreverse new)))
  2779. ;;; Some variables used in various places
  2780. (defvar org-window-configuration nil
  2781. "Used in various places to store a window configuration.")
  2782. (defvar org-finish-function nil
  2783. "Function to be called when `C-c C-c' is used.
  2784. This is for getting out of special buffers like remember.")
  2785. ;; FIXME: Occasionally check by commenting these, to make sure
  2786. ;; no other functions uses these, forgetting to let-bind them.
  2787. (defvar entry)
  2788. (defvar state)
  2789. (defvar last-state)
  2790. (defvar date)
  2791. (defvar description)
  2792. ;; Defined somewhere in this file, but used before definition.
  2793. (defvar org-html-entities)
  2794. (defvar org-struct-menu)
  2795. (defvar org-org-menu)
  2796. (defvar org-tbl-menu)
  2797. (defvar org-agenda-keymap)
  2798. ;;;; Define the Org-mode
  2799. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2800. (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."))
  2801. ;; We use a before-change function to check if a table might need
  2802. ;; an update.
  2803. (defvar org-table-may-need-update t
  2804. "Indicates that a table might need an update.
  2805. This variable is set by `org-before-change-function'.
  2806. `org-table-align' sets it back to nil.")
  2807. (defun org-before-change-function (beg end)
  2808. "Every change indicates that a table might need an update."
  2809. (setq org-table-may-need-update t))
  2810. (defvar org-mode-map)
  2811. (defvar org-mode-hook nil)
  2812. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2813. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2814. (defvar org-table-buffer-is-an nil)
  2815. (defconst org-outline-regexp "\\*+ ")
  2816. ;;;###autoload
  2817. (define-derived-mode org-mode outline-mode "Org"
  2818. "Outline-based notes management and organizer, alias
  2819. \"Carsten's outline-mode for keeping track of everything.\"
  2820. Org-mode develops organizational tasks around a NOTES file which
  2821. contains information about projects as plain text. Org-mode is
  2822. implemented on top of outline-mode, which is ideal to keep the content
  2823. of large files well structured. It supports ToDo items, deadlines and
  2824. time stamps, which magically appear in the diary listing of the Emacs
  2825. calendar. Tables are easily created with a built-in table editor.
  2826. Plain text URL-like links connect to websites, emails (VM), Usenet
  2827. messages (Gnus), BBDB entries, and any files related to the project.
  2828. For printing and sharing of notes, an Org-mode file (or a part of it)
  2829. can be exported as a structured ASCII or HTML file.
  2830. The following commands are available:
  2831. \\{org-mode-map}"
  2832. ;; Get rid of Outline menus, they are not needed
  2833. ;; Need to do this here because define-derived-mode sets up
  2834. ;; the keymap so late. Still, it is a waste to call this each time
  2835. ;; we switch another buffer into org-mode.
  2836. (if (featurep 'xemacs)
  2837. (when (boundp 'outline-mode-menu-heading)
  2838. ;; Assume this is Greg's port, it used easymenu
  2839. (easy-menu-remove outline-mode-menu-heading)
  2840. (easy-menu-remove outline-mode-menu-show)
  2841. (easy-menu-remove outline-mode-menu-hide))
  2842. (define-key org-mode-map [menu-bar headings] 'undefined)
  2843. (define-key org-mode-map [menu-bar hide] 'undefined)
  2844. (define-key org-mode-map [menu-bar show] 'undefined))
  2845. (org-load-modules-maybe)
  2846. (easy-menu-add org-org-menu)
  2847. (easy-menu-add org-tbl-menu)
  2848. (org-install-agenda-files-menu)
  2849. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2850. (org-add-to-invisibility-spec '(org-cwidth))
  2851. (when (featurep 'xemacs)
  2852. (org-set-local 'line-move-ignore-invisible t))
  2853. (org-set-local 'outline-regexp org-outline-regexp)
  2854. (org-set-local 'outline-level 'org-outline-level)
  2855. (when (and org-ellipsis
  2856. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2857. (fboundp 'make-glyph-code))
  2858. (unless org-display-table
  2859. (setq org-display-table (make-display-table)))
  2860. (set-display-table-slot
  2861. org-display-table 4
  2862. (vconcat (mapcar
  2863. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2864. org-ellipsis)))
  2865. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2866. (setq buffer-display-table org-display-table))
  2867. (org-set-regexps-and-options)
  2868. ;; Calc embedded
  2869. (org-set-local 'calc-embedded-open-mode "# ")
  2870. (modify-syntax-entry ?# "<")
  2871. (modify-syntax-entry ?@ "w")
  2872. (if org-startup-truncated (setq truncate-lines t))
  2873. (org-set-local 'font-lock-unfontify-region-function
  2874. 'org-unfontify-region)
  2875. ;; Activate before-change-function
  2876. (org-set-local 'org-table-may-need-update t)
  2877. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2878. 'local)
  2879. ;; Check for running clock before killing a buffer
  2880. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2881. ;; Paragraphs and auto-filling
  2882. (org-set-autofill-regexps)
  2883. (setq indent-line-function 'org-indent-line-function)
  2884. (org-update-radio-target-regexp)
  2885. ;; Comment characters
  2886. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2887. (org-set-local 'comment-padding " ")
  2888. ;; Align options lines
  2889. (org-set-local
  2890. 'align-mode-rules-list
  2891. '((org-in-buffer-settings
  2892. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2893. (modes . '(org-mode)))))
  2894. ;; Imenu
  2895. (org-set-local 'imenu-create-index-function
  2896. 'org-imenu-get-tree)
  2897. ;; Make isearch reveal context
  2898. (if (or (featurep 'xemacs)
  2899. (not (boundp 'outline-isearch-open-invisible-function)))
  2900. ;; Emacs 21 and XEmacs make use of the hook
  2901. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2902. ;; Emacs 22 deals with this through a special variable
  2903. (org-set-local 'outline-isearch-open-invisible-function
  2904. (lambda (&rest ignore) (org-show-context 'isearch))))
  2905. ;; If empty file that did not turn on org-mode automatically, make it to.
  2906. (if (and org-insert-mode-line-in-empty-file
  2907. (interactive-p)
  2908. (= (point-min) (point-max)))
  2909. (insert "# -*- mode: org -*-\n\n"))
  2910. (unless org-inhibit-startup
  2911. (when org-startup-align-all-tables
  2912. (let ((bmp (buffer-modified-p)))
  2913. (org-table-map-tables 'org-table-align)
  2914. (set-buffer-modified-p bmp)))
  2915. (org-set-startup-visibility)))
  2916. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2917. (defun org-current-time ()
  2918. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2919. (if (> (car org-time-stamp-rounding-minutes) 1)
  2920. (let ((r (car org-time-stamp-rounding-minutes))
  2921. (time (decode-time)))
  2922. (apply 'encode-time
  2923. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2924. (nthcdr 2 time))))
  2925. (current-time)))
  2926. ;;;; Font-Lock stuff, including the activators
  2927. (defvar org-mouse-map (make-sparse-keymap))
  2928. (org-defkey org-mouse-map
  2929. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2930. (org-defkey org-mouse-map
  2931. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2932. (when org-mouse-1-follows-link
  2933. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2934. (when org-tab-follows-link
  2935. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2936. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2937. (when org-return-follows-link
  2938. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2939. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2940. (require 'font-lock)
  2941. (defconst org-non-link-chars "]\t\n\r<>")
  2942. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2943. "shell" "elisp"))
  2944. (defvar org-link-types-re nil
  2945. "Matches a link that has a url-like prefix like \"http:\"")
  2946. (defvar org-link-re-with-space nil
  2947. "Matches a link with spaces, optional angular brackets around it.")
  2948. (defvar org-link-re-with-space2 nil
  2949. "Matches a link with spaces, optional angular brackets around it.")
  2950. (defvar org-angle-link-re nil
  2951. "Matches link with angular brackets, spaces are allowed.")
  2952. (defvar org-plain-link-re nil
  2953. "Matches plain link, without spaces.")
  2954. (defvar org-bracket-link-regexp nil
  2955. "Matches a link in double brackets.")
  2956. (defvar org-bracket-link-analytic-regexp nil
  2957. "Regular expression used to analyze links.
  2958. Here is what the match groups contain after a match:
  2959. 1: http:
  2960. 2: http
  2961. 3: path
  2962. 4: [desc]
  2963. 5: desc")
  2964. (defvar org-any-link-re nil
  2965. "Regular expression matching any link.")
  2966. (defun org-make-link-regexps ()
  2967. "Update the link regular expressions.
  2968. This should be called after the variable `org-link-types' has changed."
  2969. (setq org-link-types-re
  2970. (concat
  2971. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2972. org-link-re-with-space
  2973. (concat
  2974. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2975. "\\([^" org-non-link-chars " ]"
  2976. "[^" org-non-link-chars "]*"
  2977. "[^" org-non-link-chars " ]\\)>?")
  2978. org-link-re-with-space2
  2979. (concat
  2980. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2981. "\\([^" org-non-link-chars " ]"
  2982. "[^]\t\n\r]*"
  2983. "[^" org-non-link-chars " ]\\)>?")
  2984. org-angle-link-re
  2985. (concat
  2986. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2987. "\\([^" org-non-link-chars " ]"
  2988. "[^" org-non-link-chars "]*"
  2989. "\\)>")
  2990. org-plain-link-re
  2991. (concat
  2992. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2993. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2994. org-bracket-link-regexp
  2995. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2996. org-bracket-link-analytic-regexp
  2997. (concat
  2998. "\\[\\["
  2999. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3000. "\\([^]]+\\)"
  3001. "\\]"
  3002. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3003. "\\]")
  3004. org-any-link-re
  3005. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3006. org-angle-link-re "\\)\\|\\("
  3007. org-plain-link-re "\\)")))
  3008. (org-make-link-regexps)
  3009. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3010. "Regular expression for fast time stamp matching.")
  3011. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3012. "Regular expression for fast time stamp matching.")
  3013. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3014. "Regular expression matching time strings for analysis.
  3015. This one does not require the space after the date, so it can be used
  3016. on a string that terminates immediately after the date.")
  3017. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3018. "Regular expression matching time strings for analysis.")
  3019. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3020. "Regular expression matching time stamps, with groups.")
  3021. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3022. "Regular expression matching time stamps (also [..]), with groups.")
  3023. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3024. "Regular expression matching a time stamp range.")
  3025. (defconst org-tr-regexp-both
  3026. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3027. "Regular expression matching a time stamp range.")
  3028. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3029. org-ts-regexp "\\)?")
  3030. "Regular expression matching a time stamp or time stamp range.")
  3031. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3032. org-ts-regexp-both "\\)?")
  3033. "Regular expression matching a time stamp or time stamp range.
  3034. The time stamps may be either active or inactive.")
  3035. (defvar org-emph-face nil)
  3036. (defun org-do-emphasis-faces (limit)
  3037. "Run through the buffer and add overlays to links."
  3038. (let (rtn)
  3039. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3040. (if (not (= (char-after (match-beginning 3))
  3041. (char-after (match-beginning 4))))
  3042. (progn
  3043. (setq rtn t)
  3044. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3045. 'face
  3046. (nth 1 (assoc (match-string 3)
  3047. org-emphasis-alist)))
  3048. (add-text-properties (match-beginning 2) (match-end 2)
  3049. '(font-lock-multiline t))
  3050. (when org-hide-emphasis-markers
  3051. (add-text-properties (match-end 4) (match-beginning 5)
  3052. '(invisible org-link))
  3053. (add-text-properties (match-beginning 3) (match-end 3)
  3054. '(invisible org-link)))))
  3055. (backward-char 1))
  3056. rtn))
  3057. (defun org-emphasize (&optional char)
  3058. "Insert or change an emphasis, i.e. a font like bold or italic.
  3059. If there is an active region, change that region to a new emphasis.
  3060. If there is no region, just insert the marker characters and position
  3061. the cursor between them.
  3062. CHAR should be either the marker character, or the first character of the
  3063. HTML tag associated with that emphasis. If CHAR is a space, the means
  3064. to remove the emphasis of the selected region.
  3065. If char is not given (for example in an interactive call) it
  3066. will be prompted for."
  3067. (interactive)
  3068. (let ((eal org-emphasis-alist) e det
  3069. (erc org-emphasis-regexp-components)
  3070. (prompt "")
  3071. (string "") beg end move tag c s)
  3072. (if (org-region-active-p)
  3073. (setq beg (region-beginning) end (region-end)
  3074. string (buffer-substring beg end))
  3075. (setq move t))
  3076. (while (setq e (pop eal))
  3077. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3078. c (aref tag 0))
  3079. (push (cons c (string-to-char (car e))) det)
  3080. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3081. (substring tag 1)))))
  3082. (unless char
  3083. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3084. (setq char (read-char-exclusive)))
  3085. (setq char (or (cdr (assoc char det)) char))
  3086. (if (equal char ?\ )
  3087. (setq s "" move nil)
  3088. (unless (assoc (char-to-string char) org-emphasis-alist)
  3089. (error "No such emphasis marker: \"%c\"" char))
  3090. (setq s (char-to-string char)))
  3091. (while (and (> (length string) 1)
  3092. (equal (substring string 0 1) (substring string -1))
  3093. (assoc (substring string 0 1) org-emphasis-alist))
  3094. (setq string (substring string 1 -1)))
  3095. (setq string (concat s string s))
  3096. (if beg (delete-region beg end))
  3097. (unless (or (bolp)
  3098. (string-match (concat "[" (nth 0 erc) "\n]")
  3099. (char-to-string (char-before (point)))))
  3100. (insert " "))
  3101. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3102. (char-to-string (char-after (point))))
  3103. (insert " ") (backward-char 1))
  3104. (insert string)
  3105. (and move (backward-char 1))))
  3106. (defconst org-nonsticky-props
  3107. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3108. (defun org-activate-plain-links (limit)
  3109. "Run through the buffer and add overlays to links."
  3110. (catch 'exit
  3111. (let (f)
  3112. (while (re-search-forward org-plain-link-re limit t)
  3113. (setq f (get-text-property (match-beginning 0) 'face))
  3114. (if (or (eq f 'org-tag)
  3115. (and (listp f) (memq 'org-tag f)))
  3116. nil
  3117. (add-text-properties (match-beginning 0) (match-end 0)
  3118. (list 'mouse-face 'highlight
  3119. 'rear-nonsticky org-nonsticky-props
  3120. 'keymap org-mouse-map
  3121. ))
  3122. (throw 'exit t))))))
  3123. (defun org-activate-code (limit)
  3124. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3125. (progn
  3126. (remove-text-properties (match-beginning 0) (match-end 0)
  3127. '(display t invisible t intangible t))
  3128. t)))
  3129. (defun org-activate-angle-links (limit)
  3130. "Run through the buffer and add overlays to links."
  3131. (if (re-search-forward org-angle-link-re limit t)
  3132. (progn
  3133. (add-text-properties (match-beginning 0) (match-end 0)
  3134. (list 'mouse-face 'highlight
  3135. 'rear-nonsticky org-nonsticky-props
  3136. 'keymap org-mouse-map
  3137. ))
  3138. t)))
  3139. (defun org-activate-bracket-links (limit)
  3140. "Run through the buffer and add overlays to bracketed links."
  3141. (if (re-search-forward org-bracket-link-regexp limit t)
  3142. (let* ((help (concat "LINK: "
  3143. (org-match-string-no-properties 1)))
  3144. ;; FIXME: above we should remove the escapes.
  3145. ;; but that requires another match, protecting match data,
  3146. ;; a lot of overhead for font-lock.
  3147. (ip (org-maybe-intangible
  3148. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3149. 'keymap org-mouse-map 'mouse-face 'highlight
  3150. 'font-lock-multiline t 'help-echo help)))
  3151. (vp (list 'rear-nonsticky org-nonsticky-props
  3152. 'keymap org-mouse-map 'mouse-face 'highlight
  3153. ' font-lock-multiline t 'help-echo help)))
  3154. ;; We need to remove the invisible property here. Table narrowing
  3155. ;; may have made some of this invisible.
  3156. (remove-text-properties (match-beginning 0) (match-end 0)
  3157. '(invisible nil))
  3158. (if (match-end 3)
  3159. (progn
  3160. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3161. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3162. (add-text-properties (match-end 3) (match-end 0) ip))
  3163. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3164. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3165. (add-text-properties (match-end 1) (match-end 0) ip))
  3166. t)))
  3167. (defun org-activate-dates (limit)
  3168. "Run through the buffer and add overlays to dates."
  3169. (if (re-search-forward org-tsr-regexp-both limit t)
  3170. (progn
  3171. (add-text-properties (match-beginning 0) (match-end 0)
  3172. (list 'mouse-face 'highlight
  3173. 'rear-nonsticky org-nonsticky-props
  3174. 'keymap org-mouse-map))
  3175. (when org-display-custom-times
  3176. (if (match-end 3)
  3177. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3178. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3179. t)))
  3180. (defvar org-target-link-regexp nil
  3181. "Regular expression matching radio targets in plain text.")
  3182. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3183. "Regular expression matching a link target.")
  3184. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3185. "Regular expression matching a radio target.")
  3186. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3187. "Regular expression matching any target.")
  3188. (defun org-activate-target-links (limit)
  3189. "Run through the buffer and add overlays to target matches."
  3190. (when org-target-link-regexp
  3191. (let ((case-fold-search t))
  3192. (if (re-search-forward org-target-link-regexp limit t)
  3193. (progn
  3194. (add-text-properties (match-beginning 0) (match-end 0)
  3195. (list 'mouse-face 'highlight
  3196. 'rear-nonsticky org-nonsticky-props
  3197. 'keymap org-mouse-map
  3198. 'help-echo "Radio target link"
  3199. 'org-linked-text t))
  3200. t)))))
  3201. (defun org-update-radio-target-regexp ()
  3202. "Find all radio targets in this file and update the regular expression."
  3203. (interactive)
  3204. (when (memq 'radio org-activate-links)
  3205. (setq org-target-link-regexp
  3206. (org-make-target-link-regexp (org-all-targets 'radio)))
  3207. (org-restart-font-lock)))
  3208. (defun org-hide-wide-columns (limit)
  3209. (let (s e)
  3210. (setq s (text-property-any (point) (or limit (point-max))
  3211. 'org-cwidth t))
  3212. (when s
  3213. (setq e (next-single-property-change s 'org-cwidth))
  3214. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3215. (goto-char e)
  3216. t)))
  3217. (defvar org-latex-and-specials-regexp nil
  3218. "Regular expression for highlighting export special stuff.")
  3219. (defvar org-match-substring-regexp)
  3220. (defvar org-match-substring-with-braces-regexp)
  3221. (defvar org-export-html-special-string-regexps)
  3222. (defun org-compute-latex-and-specials-regexp ()
  3223. "Compute regular expression for stuff treated specially by exporters."
  3224. (if (not org-highlight-latex-fragments-and-specials)
  3225. (org-set-local 'org-latex-and-specials-regexp nil)
  3226. (require 'org-exp)
  3227. (let*
  3228. ((matchers (plist-get org-format-latex-options :matchers))
  3229. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3230. org-latex-regexps)))
  3231. (options (org-combine-plists (org-default-export-plist)
  3232. (org-infile-export-plist)))
  3233. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3234. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3235. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3236. (org-export-html-expand (plist-get options :expand-quoted-html))
  3237. (org-export-with-special-strings (plist-get options :special-strings))
  3238. (re-sub
  3239. (cond
  3240. ((equal org-export-with-sub-superscripts '{})
  3241. (list org-match-substring-with-braces-regexp))
  3242. (org-export-with-sub-superscripts
  3243. (list org-match-substring-regexp))
  3244. (t nil)))
  3245. (re-latex
  3246. (if org-export-with-LaTeX-fragments
  3247. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3248. (re-macros
  3249. (if org-export-with-TeX-macros
  3250. (list (concat "\\\\"
  3251. (regexp-opt
  3252. (append (mapcar 'car org-html-entities)
  3253. (if (boundp 'org-latex-entities)
  3254. org-latex-entities nil))
  3255. 'words))) ; FIXME
  3256. ))
  3257. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3258. (re-special (if org-export-with-special-strings
  3259. (mapcar (lambda (x) (car x))
  3260. org-export-html-special-string-regexps)))
  3261. (re-rest
  3262. (delq nil
  3263. (list
  3264. (if org-export-html-expand "@<[^>\n]+>")
  3265. ))))
  3266. (org-set-local
  3267. 'org-latex-and-specials-regexp
  3268. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3269. re-rest) "\\|")))))
  3270. (defun org-do-latex-and-special-faces (limit)
  3271. "Run through the buffer and add overlays to links."
  3272. (when org-latex-and-specials-regexp
  3273. (let (rtn d)
  3274. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3275. limit t))
  3276. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3277. 'face))
  3278. '(org-code org-verbatim underline)))
  3279. (progn
  3280. (setq rtn t
  3281. d (cond ((member (char-after (1+ (match-beginning 0)))
  3282. '(?_ ?^)) 1)
  3283. (t 0)))
  3284. (font-lock-prepend-text-property
  3285. (+ d (match-beginning 0)) (match-end 0)
  3286. 'face 'org-latex-and-export-specials)
  3287. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3288. '(font-lock-multiline t)))))
  3289. rtn)))
  3290. (defun org-restart-font-lock ()
  3291. "Restart font-lock-mode, to force refontification."
  3292. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3293. (font-lock-mode -1)
  3294. (font-lock-mode 1)))
  3295. (defun org-all-targets (&optional radio)
  3296. "Return a list of all targets in this file.
  3297. With optional argument RADIO, only find radio targets."
  3298. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3299. rtn)
  3300. (save-excursion
  3301. (goto-char (point-min))
  3302. (while (re-search-forward re nil t)
  3303. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3304. rtn)))
  3305. (defun org-make-target-link-regexp (targets)
  3306. "Make regular expression matching all strings in TARGETS.
  3307. The regular expression finds the targets also if there is a line break
  3308. between words."
  3309. (and targets
  3310. (concat
  3311. "\\<\\("
  3312. (mapconcat
  3313. (lambda (x)
  3314. (while (string-match " +" x)
  3315. (setq x (replace-match "\\s-+" t t x)))
  3316. x)
  3317. targets
  3318. "\\|")
  3319. "\\)\\>")))
  3320. (defun org-activate-tags (limit)
  3321. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3322. (progn
  3323. (add-text-properties (match-beginning 1) (match-end 1)
  3324. (list 'mouse-face 'highlight
  3325. 'rear-nonsticky org-nonsticky-props
  3326. 'keymap org-mouse-map))
  3327. t)))
  3328. (defun org-outline-level ()
  3329. (save-excursion
  3330. (looking-at outline-regexp)
  3331. (if (match-beginning 1)
  3332. (+ (org-get-string-indentation (match-string 1)) 1000)
  3333. (1- (- (match-end 0) (match-beginning 0))))))
  3334. (defvar org-font-lock-keywords nil)
  3335. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3336. "Regular expression matching a property line.")
  3337. (defvar org-font-lock-hook nil
  3338. "Functions to be called for special font lock stuff.")
  3339. (defun org-font-lock-hook (limit)
  3340. (run-hook-with-args 'org-font-lock-hook limit))
  3341. (defun org-set-font-lock-defaults ()
  3342. (let* ((em org-fontify-emphasized-text)
  3343. (lk org-activate-links)
  3344. (org-font-lock-extra-keywords
  3345. (list
  3346. ;; Call the hook
  3347. '(org-font-lock-hook)
  3348. ;; Headlines
  3349. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3350. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3351. ;; Table lines
  3352. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3353. (1 'org-table t))
  3354. ;; Table internals
  3355. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3356. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3357. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3358. ;; Drawers
  3359. (list org-drawer-regexp '(0 'org-special-keyword t))
  3360. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3361. ;; Properties
  3362. (list org-property-re
  3363. '(1 'org-special-keyword t)
  3364. '(3 'org-property-value t))
  3365. (if org-format-transports-properties-p
  3366. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3367. ;; Links
  3368. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3369. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3370. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3371. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3372. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3373. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3374. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3375. '(org-hide-wide-columns (0 nil append))
  3376. ;; TODO lines
  3377. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3378. '(1 (org-get-todo-face 1) t))
  3379. ;; DONE
  3380. (if org-fontify-done-headline
  3381. (list (concat "^[*]+ +\\<\\("
  3382. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3383. "\\)\\(.*\\)")
  3384. '(2 'org-headline-done t))
  3385. nil)
  3386. ;; Priorities
  3387. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3388. ;; Special keywords
  3389. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3390. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3391. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3392. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3393. ;; Emphasis
  3394. (if em
  3395. (if (featurep 'xemacs)
  3396. '(org-do-emphasis-faces (0 nil append))
  3397. '(org-do-emphasis-faces)))
  3398. ;; Checkboxes
  3399. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3400. 2 'bold prepend)
  3401. (if org-provide-checkbox-statistics
  3402. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3403. (0 (org-get-checkbox-statistics-face) t)))
  3404. ;; Description list items
  3405. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3406. 2 'bold prepend)
  3407. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3408. '(1 'org-archived prepend))
  3409. ;; Specials
  3410. '(org-do-latex-and-special-faces)
  3411. ;; Code
  3412. '(org-activate-code (1 'org-code t))
  3413. ;; COMMENT
  3414. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3415. "\\|" org-quote-string "\\)\\>")
  3416. '(1 'org-special-keyword t))
  3417. '("^#.*" (0 'font-lock-comment-face t))
  3418. )))
  3419. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3420. ;; Now set the full font-lock-keywords
  3421. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3422. (org-set-local 'font-lock-defaults
  3423. '(org-font-lock-keywords t nil nil backward-paragraph))
  3424. (kill-local-variable 'font-lock-keywords) nil))
  3425. (defvar org-m nil)
  3426. (defvar org-l nil)
  3427. (defvar org-f nil)
  3428. (defun org-get-level-face (n)
  3429. "Get the right face for match N in font-lock matching of healdines."
  3430. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3431. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3432. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3433. (cond
  3434. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3435. ((eq n 2) org-f)
  3436. (t (if org-level-color-stars-only nil org-f))))
  3437. (defun org-get-todo-face (kwd)
  3438. "Get the right face for a TODO keyword KWD.
  3439. If KWD is a number, get the corresponding match group."
  3440. (if (numberp kwd) (setq kwd (match-string kwd)))
  3441. (or (cdr (assoc kwd org-todo-keyword-faces))
  3442. (and (member kwd org-done-keywords) 'org-done)
  3443. 'org-todo))
  3444. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3445. "Remove fontification and activation overlays from links."
  3446. (font-lock-default-unfontify-region beg end)
  3447. (let* ((buffer-undo-list t)
  3448. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3449. (inhibit-modification-hooks t)
  3450. deactivate-mark buffer-file-name buffer-file-truename)
  3451. (remove-text-properties beg end
  3452. '(mouse-face t keymap t org-linked-text t
  3453. invisible t intangible t))))
  3454. ;;;; Visibility cycling, including org-goto and indirect buffer
  3455. ;;; Cycling
  3456. (defvar org-cycle-global-status nil)
  3457. (make-variable-buffer-local 'org-cycle-global-status)
  3458. (defvar org-cycle-subtree-status nil)
  3459. (make-variable-buffer-local 'org-cycle-subtree-status)
  3460. ;;;###autoload
  3461. (defun org-cycle (&optional arg)
  3462. "Visibility cycling for Org-mode.
  3463. - When this function is called with a prefix argument, rotate the entire
  3464. buffer through 3 states (global cycling)
  3465. 1. OVERVIEW: Show only top-level headlines.
  3466. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3467. 3. SHOW ALL: Show everything.
  3468. When called with two C-c C-u prefixes, switch to the startup visibility,
  3469. determined by the variable `org-startup-folded', and by any VISIBILITY
  3470. properties in the buffer.
  3471. - When point is at the beginning of a headline, rotate the subtree started
  3472. by this line through 3 different states (local cycling)
  3473. 1. FOLDED: Only the main headline is shown.
  3474. 2. CHILDREN: The main headline and the direct children are shown.
  3475. From this state, you can move to one of the children
  3476. and zoom in further.
  3477. 3. SUBTREE: Show the entire subtree, including body text.
  3478. - When there is a numeric prefix, go up to a heading with level ARG, do
  3479. a `show-subtree' and return to the previous cursor position. If ARG
  3480. is negative, go up that many levels.
  3481. - When point is not at the beginning of a headline, execute the global
  3482. binding for TAB, which is re-indenting the line. See the option
  3483. `org-cycle-emulate-tab' for details.
  3484. - Special case: if point is at the beginning of the buffer and there is
  3485. no headline in line 1, this function will act as if called with prefix arg.
  3486. But only if also the variable `org-cycle-global-at-bob' is t."
  3487. (interactive "P")
  3488. (org-load-modules-maybe)
  3489. (let* ((outline-regexp
  3490. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3491. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3492. outline-regexp))
  3493. (bob-special (and org-cycle-global-at-bob (bobp)
  3494. (not (looking-at outline-regexp))))
  3495. (org-cycle-hook
  3496. (if bob-special
  3497. (delq 'org-optimize-window-after-visibility-change
  3498. (copy-sequence org-cycle-hook))
  3499. org-cycle-hook))
  3500. (pos (point)))
  3501. (if (or bob-special (equal arg '(4)))
  3502. ;; special case: use global cycling
  3503. (setq arg t))
  3504. (cond
  3505. ((equal arg '(16))
  3506. (org-set-startup-visibility)
  3507. (message "Startup visibility, plus VISIBILITY properties."))
  3508. ((org-at-table-p 'any)
  3509. ;; Enter the table or move to the next field in the table
  3510. (or (org-table-recognize-table.el)
  3511. (progn
  3512. (if arg (org-table-edit-field t)
  3513. (org-table-justify-field-maybe)
  3514. (call-interactively 'org-table-next-field)))))
  3515. ((eq arg t) ;; Global cycling
  3516. (cond
  3517. ((and (eq last-command this-command)
  3518. (eq org-cycle-global-status 'overview))
  3519. ;; We just created the overview - now do table of contents
  3520. ;; This can be slow in very large buffers, so indicate action
  3521. (message "CONTENTS...")
  3522. (org-content)
  3523. (message "CONTENTS...done")
  3524. (setq org-cycle-global-status 'contents)
  3525. (run-hook-with-args 'org-cycle-hook 'contents))
  3526. ((and (eq last-command this-command)
  3527. (eq org-cycle-global-status 'contents))
  3528. ;; We just showed the table of contents - now show everything
  3529. (show-all)
  3530. (message "SHOW ALL")
  3531. (setq org-cycle-global-status 'all)
  3532. (run-hook-with-args 'org-cycle-hook 'all))
  3533. (t
  3534. ;; Default action: go to overview
  3535. (org-overview)
  3536. (message "OVERVIEW")
  3537. (setq org-cycle-global-status 'overview)
  3538. (run-hook-with-args 'org-cycle-hook 'overview))))
  3539. ((and org-drawers org-drawer-regexp
  3540. (save-excursion
  3541. (beginning-of-line 1)
  3542. (looking-at org-drawer-regexp)))
  3543. ;; Toggle block visibility
  3544. (org-flag-drawer
  3545. (not (get-char-property (match-end 0) 'invisible))))
  3546. ((integerp arg)
  3547. ;; Show-subtree, ARG levels up from here.
  3548. (save-excursion
  3549. (org-back-to-heading)
  3550. (outline-up-heading (if (< arg 0) (- arg)
  3551. (- (funcall outline-level) arg)))
  3552. (org-show-subtree)))
  3553. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3554. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3555. ;; At a heading: rotate between three different views
  3556. (org-back-to-heading)
  3557. (let ((goal-column 0) eoh eol eos)
  3558. ;; First, some boundaries
  3559. (save-excursion
  3560. (org-back-to-heading)
  3561. (save-excursion
  3562. (beginning-of-line 2)
  3563. (while (and (not (eobp)) ;; this is like `next-line'
  3564. (get-char-property (1- (point)) 'invisible))
  3565. (beginning-of-line 2)) (setq eol (point)))
  3566. (outline-end-of-heading) (setq eoh (point))
  3567. (org-end-of-subtree t)
  3568. (unless (eobp)
  3569. (skip-chars-forward " \t\n")
  3570. (beginning-of-line 1) ; in case this is an item
  3571. )
  3572. (setq eos (1- (point))))
  3573. ;; Find out what to do next and set `this-command'
  3574. (cond
  3575. ((= eos eoh)
  3576. ;; Nothing is hidden behind this heading
  3577. (message "EMPTY ENTRY")
  3578. (setq org-cycle-subtree-status nil)
  3579. (save-excursion
  3580. (goto-char eos)
  3581. (outline-next-heading)
  3582. (if (org-invisible-p) (org-flag-heading nil))))
  3583. ((or (>= eol eos)
  3584. (not (string-match "\\S-" (buffer-substring eol eos))))
  3585. ;; Entire subtree is hidden in one line: open it
  3586. (org-show-entry)
  3587. (show-children)
  3588. (message "CHILDREN")
  3589. (save-excursion
  3590. (goto-char eos)
  3591. (outline-next-heading)
  3592. (if (org-invisible-p) (org-flag-heading nil)))
  3593. (setq org-cycle-subtree-status 'children)
  3594. (run-hook-with-args 'org-cycle-hook 'children))
  3595. ((and (eq last-command this-command)
  3596. (eq org-cycle-subtree-status 'children))
  3597. ;; We just showed the children, now show everything.
  3598. (org-show-subtree)
  3599. (message "SUBTREE")
  3600. (setq org-cycle-subtree-status 'subtree)
  3601. (run-hook-with-args 'org-cycle-hook 'subtree))
  3602. (t
  3603. ;; Default action: hide the subtree.
  3604. (hide-subtree)
  3605. (message "FOLDED")
  3606. (setq org-cycle-subtree-status 'folded)
  3607. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3608. ;; TAB emulation and template completion
  3609. (buffer-read-only (org-back-to-heading))
  3610. ((org-try-structure-completion))
  3611. ((org-try-cdlatex-tab))
  3612. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3613. (or (not (bolp))
  3614. (not (looking-at outline-regexp))))
  3615. (call-interactively (global-key-binding "\t")))
  3616. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3617. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3618. (or (and (eq org-cycle-emulate-tab 'white)
  3619. (= (match-end 0) (point-at-eol)))
  3620. (and (eq org-cycle-emulate-tab 'whitestart)
  3621. (>= (match-end 0) pos))))
  3622. t
  3623. (eq org-cycle-emulate-tab t))
  3624. (call-interactively (global-key-binding "\t")))
  3625. (t (save-excursion
  3626. (org-back-to-heading)
  3627. (org-cycle))))))
  3628. ;;;###autoload
  3629. (defun org-global-cycle (&optional arg)
  3630. "Cycle the global visibility. For details see `org-cycle'.
  3631. With C-u prefix arg, switch to startup visibility.
  3632. With a numeric prefix, show all headlines up to that level."
  3633. (interactive "P")
  3634. (let ((org-cycle-include-plain-lists
  3635. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3636. (cond
  3637. ((integerp arg)
  3638. (show-all)
  3639. (hide-sublevels arg)
  3640. (setq org-cycle-global-status 'contents))
  3641. ((equal arg '(4))
  3642. (org-set-startup-visibility)
  3643. (message "Startup visibility, plus VISIBILITY properties."))
  3644. (t
  3645. (org-cycle '(4))))))
  3646. (defun org-set-startup-visibility ()
  3647. "Set the visibility required by startup options and properties."
  3648. (cond
  3649. ((eq org-startup-folded t)
  3650. (org-cycle '(4)))
  3651. ((eq org-startup-folded 'content)
  3652. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3653. (org-cycle '(4)) (org-cycle '(4)))))
  3654. (org-set-visibility-according-to-property 'no-cleanup)
  3655. (org-cycle-hide-archived-subtrees 'all)
  3656. (org-cycle-hide-drawers 'all)
  3657. (org-cycle-show-empty-lines 'all))
  3658. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3659. "Switch subtree visibilities according to :VISIBILITY: property."
  3660. (interactive)
  3661. (let (state)
  3662. (save-excursion
  3663. (goto-char (point-min))
  3664. (while (re-search-forward
  3665. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3666. nil t)
  3667. (setq state (match-string 1))
  3668. (save-excursion
  3669. (org-back-to-heading t)
  3670. (hide-subtree)
  3671. (org-reveal)
  3672. (cond
  3673. ((equal state '("fold" "folded"))
  3674. (hide-subtree))
  3675. ((equal state "children")
  3676. (org-show-hidden-entry)
  3677. (show-children))
  3678. ((equal state "content")
  3679. (save-excursion
  3680. (save-restriction
  3681. (org-narrow-to-subtree)
  3682. (org-content))))
  3683. ((member state '("all" "showall"))
  3684. (show-subtree)))))
  3685. (unless no-cleanup
  3686. (org-cycle-hide-archived-subtrees 'all)
  3687. (org-cycle-hide-drawers 'all)
  3688. (org-cycle-show-empty-lines 'all)))))
  3689. (defun org-overview ()
  3690. "Switch to overview mode, shoing only top-level headlines.
  3691. Really, this shows all headlines with level equal or greater than the level
  3692. of the first headline in the buffer. This is important, because if the
  3693. first headline is not level one, then (hide-sublevels 1) gives confusing
  3694. results."
  3695. (interactive)
  3696. (let ((level (save-excursion
  3697. (goto-char (point-min))
  3698. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3699. (progn
  3700. (goto-char (match-beginning 0))
  3701. (funcall outline-level))))))
  3702. (and level (hide-sublevels level))))
  3703. (defun org-content (&optional arg)
  3704. "Show all headlines in the buffer, like a table of contents.
  3705. With numerical argument N, show content up to level N."
  3706. (interactive "P")
  3707. (save-excursion
  3708. ;; Visit all headings and show their offspring
  3709. (and (integerp arg) (org-overview))
  3710. (goto-char (point-max))
  3711. (catch 'exit
  3712. (while (and (progn (condition-case nil
  3713. (outline-previous-visible-heading 1)
  3714. (error (goto-char (point-min))))
  3715. t)
  3716. (looking-at outline-regexp))
  3717. (if (integerp arg)
  3718. (show-children (1- arg))
  3719. (show-branches))
  3720. (if (bobp) (throw 'exit nil))))))
  3721. (defun org-optimize-window-after-visibility-change (state)
  3722. "Adjust the window after a change in outline visibility.
  3723. This function is the default value of the hook `org-cycle-hook'."
  3724. (when (get-buffer-window (current-buffer))
  3725. (cond
  3726. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3727. ; ((eq state 'overview) (org-beginning-of-line))
  3728. ((eq state 'content) nil)
  3729. ((eq state 'all) nil)
  3730. ((eq state 'folded) nil)
  3731. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3732. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3733. (defun org-compact-display-after-subtree-move ()
  3734. (let (beg end)
  3735. (save-excursion
  3736. (if (org-up-heading-safe)
  3737. (progn
  3738. (hide-subtree)
  3739. (show-entry)
  3740. (show-children)
  3741. (org-cycle-show-empty-lines 'children)
  3742. (org-cycle-hide-drawers 'children))
  3743. (org-overview)))))
  3744. (defun org-cycle-show-empty-lines (state)
  3745. "Show empty lines above all visible headlines.
  3746. The region to be covered depends on STATE when called through
  3747. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3748. entire buffer covered. Note that an empty line is only shown if there
  3749. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3750. (when (> org-cycle-separator-lines 0)
  3751. (save-excursion
  3752. (let* ((n org-cycle-separator-lines)
  3753. (re (cond
  3754. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3755. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3756. (t (let ((ns (number-to-string (- n 2))))
  3757. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3758. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3759. beg end)
  3760. (cond
  3761. ((memq state '(overview contents t))
  3762. (setq beg (point-min) end (point-max)))
  3763. ((memq state '(children folded))
  3764. (setq beg (point) end (progn (org-end-of-subtree t t)
  3765. (beginning-of-line 2)
  3766. (point)))))
  3767. (when beg
  3768. (goto-char beg)
  3769. (while (re-search-forward re end t)
  3770. (if (not (get-char-property (match-end 1) 'invisible))
  3771. (outline-flag-region
  3772. (match-beginning 1) (match-end 1) nil)))))))
  3773. ;; Never hide empty lines at the end of the file.
  3774. (save-excursion
  3775. (goto-char (point-max))
  3776. (outline-previous-heading)
  3777. (outline-end-of-heading)
  3778. (if (and (looking-at "[ \t\n]+")
  3779. (= (match-end 0) (point-max)))
  3780. (outline-flag-region (point) (match-end 0) nil))))
  3781. (defun org-show-empty-lines-in-parent ()
  3782. "Move to the parent and re-show empty lines before visible headlines."
  3783. (save-excursion
  3784. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3785. (org-cycle-show-empty-lines context))))
  3786. (defun org-cycle-hide-drawers (state)
  3787. "Re-hide all drawers after a visibility state change."
  3788. (when (and (org-mode-p)
  3789. (not (memq state '(overview folded))))
  3790. (save-excursion
  3791. (let* ((globalp (memq state '(contents all)))
  3792. (beg (if globalp (point-min) (point)))
  3793. (end (if globalp (point-max) (org-end-of-subtree t))))
  3794. (goto-char beg)
  3795. (while (re-search-forward org-drawer-regexp end t)
  3796. (org-flag-drawer t))))))
  3797. (defun org-flag-drawer (flag)
  3798. (save-excursion
  3799. (beginning-of-line 1)
  3800. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3801. (let ((b (match-end 0))
  3802. (outline-regexp org-outline-regexp))
  3803. (if (re-search-forward
  3804. "^[ \t]*:END:"
  3805. (save-excursion (outline-next-heading) (point)) t)
  3806. (outline-flag-region b (point-at-eol) flag)
  3807. (error ":END: line missing"))))))
  3808. (defun org-subtree-end-visible-p ()
  3809. "Is the end of the current subtree visible?"
  3810. (pos-visible-in-window-p
  3811. (save-excursion (org-end-of-subtree t) (point))))
  3812. (defun org-first-headline-recenter (&optional N)
  3813. "Move cursor to the first headline and recenter the headline.
  3814. Optional argument N means, put the headline into the Nth line of the window."
  3815. (goto-char (point-min))
  3816. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3817. (beginning-of-line)
  3818. (recenter (prefix-numeric-value N))))
  3819. ;;; Org-goto
  3820. (defvar org-goto-window-configuration nil)
  3821. (defvar org-goto-marker nil)
  3822. (defvar org-goto-map
  3823. (let ((map (make-sparse-keymap)))
  3824. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3825. (while (setq cmd (pop cmds))
  3826. (substitute-key-definition cmd cmd map global-map)))
  3827. (suppress-keymap map)
  3828. (org-defkey map "\C-m" 'org-goto-ret)
  3829. (org-defkey map [(return)] 'org-goto-ret)
  3830. (org-defkey map [(left)] 'org-goto-left)
  3831. (org-defkey map [(right)] 'org-goto-right)
  3832. (org-defkey map [(control ?g)] 'org-goto-quit)
  3833. (org-defkey map "\C-i" 'org-cycle)
  3834. (org-defkey map [(tab)] 'org-cycle)
  3835. (org-defkey map [(down)] 'outline-next-visible-heading)
  3836. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3837. (if org-goto-auto-isearch
  3838. (if (fboundp 'define-key-after)
  3839. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3840. nil)
  3841. (org-defkey map "q" 'org-goto-quit)
  3842. (org-defkey map "n" 'outline-next-visible-heading)
  3843. (org-defkey map "p" 'outline-previous-visible-heading)
  3844. (org-defkey map "f" 'outline-forward-same-level)
  3845. (org-defkey map "b" 'outline-backward-same-level)
  3846. (org-defkey map "u" 'outline-up-heading))
  3847. (org-defkey map "/" 'org-occur)
  3848. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3849. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3850. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3851. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3852. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3853. map))
  3854. (defconst org-goto-help
  3855. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3856. RET=jump to location [Q]uit and return to previous location
  3857. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3858. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3859. ;; FIXME: Docstring doe not mention both interfaces
  3860. (defun org-goto (&optional alternative-interface)
  3861. "Look up a different location in the current file, keeping current visibility.
  3862. When you want look-up or go to a different location in a document, the
  3863. fastest way is often to fold the entire buffer and then dive into the tree.
  3864. This method has the disadvantage, that the previous location will be folded,
  3865. which may not be what you want.
  3866. This command works around this by showing a copy of the current buffer
  3867. in an indirect buffer, in overview mode. You can dive into the tree in
  3868. that copy, use org-occur and incremental search to find a location.
  3869. When pressing RET or `Q', the command returns to the original buffer in
  3870. which the visibility is still unchanged. After RET is will also jump to
  3871. the location selected in the indirect buffer and expose the
  3872. the headline hierarchy above."
  3873. (interactive "P")
  3874. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3875. (org-refile-use-outline-path t)
  3876. (interface
  3877. (if (not alternative-interface)
  3878. org-goto-interface
  3879. (if (eq org-goto-interface 'outline)
  3880. 'outline-path-completion
  3881. 'outline)))
  3882. (org-goto-start-pos (point))
  3883. (selected-point
  3884. (if (eq interface 'outline)
  3885. (car (org-get-location (current-buffer) org-goto-help))
  3886. (nth 3 (org-refile-get-location "Goto: ")))))
  3887. (if selected-point
  3888. (progn
  3889. (org-mark-ring-push org-goto-start-pos)
  3890. (goto-char selected-point)
  3891. (if (or (org-invisible-p) (org-invisible-p2))
  3892. (org-show-context 'org-goto)))
  3893. (message "Quit"))))
  3894. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3895. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3896. (defvar org-goto-local-auto-isearch-map) ; defined below
  3897. (defun org-get-location (buf help)
  3898. "Let the user select a location in the Org-mode buffer BUF.
  3899. This function uses a recursive edit. It returns the selected position
  3900. or nil."
  3901. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3902. (isearch-hide-immediately nil)
  3903. (isearch-search-fun-function
  3904. (lambda () 'org-goto-local-search-headings))
  3905. (org-goto-selected-point org-goto-exit-command))
  3906. (save-excursion
  3907. (save-window-excursion
  3908. (delete-other-windows)
  3909. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3910. (switch-to-buffer
  3911. (condition-case nil
  3912. (make-indirect-buffer (current-buffer) "*org-goto*")
  3913. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3914. (with-output-to-temp-buffer "*Help*"
  3915. (princ help))
  3916. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3917. (setq buffer-read-only nil)
  3918. (let ((org-startup-truncated t)
  3919. (org-startup-folded nil)
  3920. (org-startup-align-all-tables nil))
  3921. (org-mode)
  3922. (org-overview))
  3923. (setq buffer-read-only t)
  3924. (if (and (boundp 'org-goto-start-pos)
  3925. (integer-or-marker-p org-goto-start-pos))
  3926. (let ((org-show-hierarchy-above t)
  3927. (org-show-siblings t)
  3928. (org-show-following-heading t))
  3929. (goto-char org-goto-start-pos)
  3930. (and (org-invisible-p) (org-show-context)))
  3931. (goto-char (point-min)))
  3932. (org-beginning-of-line)
  3933. (message "Select location and press RET")
  3934. (use-local-map org-goto-map)
  3935. (recursive-edit)
  3936. ))
  3937. (kill-buffer "*org-goto*")
  3938. (cons org-goto-selected-point org-goto-exit-command)))
  3939. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3940. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3941. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3942. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3943. (defun org-goto-local-search-headings (string bound noerror)
  3944. "Search and make sure that any matches are in headlines."
  3945. (catch 'return
  3946. (while (if isearch-forward
  3947. (search-forward string bound noerror)
  3948. (search-backward string bound noerror))
  3949. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3950. (and (member :headline context)
  3951. (not (member :tags context))))
  3952. (throw 'return (point))))))
  3953. (defun org-goto-local-auto-isearch ()
  3954. "Start isearch."
  3955. (interactive)
  3956. (goto-char (point-min))
  3957. (let ((keys (this-command-keys)))
  3958. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3959. (isearch-mode t)
  3960. (isearch-process-search-char (string-to-char keys)))))
  3961. (defun org-goto-ret (&optional arg)
  3962. "Finish `org-goto' by going to the new location."
  3963. (interactive "P")
  3964. (setq org-goto-selected-point (point)
  3965. org-goto-exit-command 'return)
  3966. (throw 'exit nil))
  3967. (defun org-goto-left ()
  3968. "Finish `org-goto' by going to the new location."
  3969. (interactive)
  3970. (if (org-on-heading-p)
  3971. (progn
  3972. (beginning-of-line 1)
  3973. (setq org-goto-selected-point (point)
  3974. org-goto-exit-command 'left)
  3975. (throw 'exit nil))
  3976. (error "Not on a heading")))
  3977. (defun org-goto-right ()
  3978. "Finish `org-goto' by going to the new location."
  3979. (interactive)
  3980. (if (org-on-heading-p)
  3981. (progn
  3982. (setq org-goto-selected-point (point)
  3983. org-goto-exit-command 'right)
  3984. (throw 'exit nil))
  3985. (error "Not on a heading")))
  3986. (defun org-goto-quit ()
  3987. "Finish `org-goto' without cursor motion."
  3988. (interactive)
  3989. (setq org-goto-selected-point nil)
  3990. (setq org-goto-exit-command 'quit)
  3991. (throw 'exit nil))
  3992. ;;; Indirect buffer display of subtrees
  3993. (defvar org-indirect-dedicated-frame nil
  3994. "This is the frame being used for indirect tree display.")
  3995. (defvar org-last-indirect-buffer nil)
  3996. (defun org-tree-to-indirect-buffer (&optional arg)
  3997. "Create indirect buffer and narrow it to current subtree.
  3998. With numerical prefix ARG, go up to this level and then take that tree.
  3999. If ARG is negative, go up that many levels.
  4000. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4001. indirect buffer previously made with this command, to avoid proliferation of
  4002. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4003. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4004. is kept so that you can work with several indirect buffers at the same time.
  4005. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4006. requests that a new frame be made for the new buffer, so that the dedicated
  4007. frame is not changed."
  4008. (interactive "P")
  4009. (let ((cbuf (current-buffer))
  4010. (cwin (selected-window))
  4011. (pos (point))
  4012. beg end level heading ibuf)
  4013. (save-excursion
  4014. (org-back-to-heading t)
  4015. (when (numberp arg)
  4016. (setq level (org-outline-level))
  4017. (if (< arg 0) (setq arg (+ level arg)))
  4018. (while (> (setq level (org-outline-level)) arg)
  4019. (outline-up-heading 1 t)))
  4020. (setq beg (point)
  4021. heading (org-get-heading))
  4022. (org-end-of-subtree t) (setq end (point)))
  4023. (if (and (buffer-live-p org-last-indirect-buffer)
  4024. (not (eq org-indirect-buffer-display 'new-frame))
  4025. (not arg))
  4026. (kill-buffer org-last-indirect-buffer))
  4027. (setq ibuf (org-get-indirect-buffer cbuf)
  4028. org-last-indirect-buffer ibuf)
  4029. (cond
  4030. ((or (eq org-indirect-buffer-display 'new-frame)
  4031. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4032. (select-frame (make-frame))
  4033. (delete-other-windows)
  4034. (switch-to-buffer ibuf)
  4035. (org-set-frame-title heading))
  4036. ((eq org-indirect-buffer-display 'dedicated-frame)
  4037. (raise-frame
  4038. (select-frame (or (and org-indirect-dedicated-frame
  4039. (frame-live-p org-indirect-dedicated-frame)
  4040. org-indirect-dedicated-frame)
  4041. (setq org-indirect-dedicated-frame (make-frame)))))
  4042. (delete-other-windows)
  4043. (switch-to-buffer ibuf)
  4044. (org-set-frame-title (concat "Indirect: " heading)))
  4045. ((eq org-indirect-buffer-display 'current-window)
  4046. (switch-to-buffer ibuf))
  4047. ((eq org-indirect-buffer-display 'other-window)
  4048. (pop-to-buffer ibuf))
  4049. (t (error "Invalid value.")))
  4050. (if (featurep 'xemacs)
  4051. (save-excursion (org-mode) (turn-on-font-lock)))
  4052. (narrow-to-region beg end)
  4053. (show-all)
  4054. (goto-char pos)
  4055. (and (window-live-p cwin) (select-window cwin))))
  4056. (defun org-get-indirect-buffer (&optional buffer)
  4057. (setq buffer (or buffer (current-buffer)))
  4058. (let ((n 1) (base (buffer-name buffer)) bname)
  4059. (while (buffer-live-p
  4060. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4061. (setq n (1+ n)))
  4062. (condition-case nil
  4063. (make-indirect-buffer buffer bname 'clone)
  4064. (error (make-indirect-buffer buffer bname)))))
  4065. (defun org-set-frame-title (title)
  4066. "Set the title of the current frame to the string TITLE."
  4067. ;; FIXME: how to name a single frame in XEmacs???
  4068. (unless (featurep 'xemacs)
  4069. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4070. ;;;; Structure editing
  4071. ;;; Inserting headlines
  4072. (defun org-insert-heading (&optional force-heading)
  4073. "Insert a new heading or item with same depth at point.
  4074. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4075. If point is at the beginning of a headline, insert a sibling before the
  4076. current headline. If point is not at the beginning, do not split the line,
  4077. but create the new hedline after the current line."
  4078. (interactive "P")
  4079. (if (= (buffer-size) 0)
  4080. (insert "\n* ")
  4081. (when (or force-heading (not (org-insert-item)))
  4082. (let* ((head (save-excursion
  4083. (condition-case nil
  4084. (progn
  4085. (org-back-to-heading)
  4086. (match-string 0))
  4087. (error "*"))))
  4088. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4089. pos)
  4090. (cond
  4091. ((and (org-on-heading-p) (bolp)
  4092. (or (bobp)
  4093. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4094. ;; insert before the current line
  4095. (open-line (if blank 2 1)))
  4096. ((and (bolp)
  4097. (or (bobp)
  4098. (save-excursion
  4099. (backward-char 1) (not (org-invisible-p)))))
  4100. ;; insert right here
  4101. nil)
  4102. (t
  4103. ;; in the middle of the line
  4104. (org-show-entry)
  4105. (let ((split
  4106. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4107. tags pos)
  4108. (if (org-on-heading-p)
  4109. (progn
  4110. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4111. (setq tags (and (match-end 2) (match-string 2)))
  4112. (and (match-end 1)
  4113. (delete-region (match-beginning 1) (match-end 1)))
  4114. (setq pos (point-at-bol))
  4115. (or split (end-of-line 1))
  4116. (delete-horizontal-space)
  4117. (newline (if blank 2 1))
  4118. (when tags
  4119. (save-excursion
  4120. (goto-char pos)
  4121. (end-of-line 1)
  4122. (insert " " tags)
  4123. (org-set-tags nil 'align))))
  4124. (or split (end-of-line 1))
  4125. (newline (if blank 2 1))))))
  4126. (insert head) (just-one-space)
  4127. (setq pos (point))
  4128. (end-of-line 1)
  4129. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4130. (run-hooks 'org-insert-heading-hook)))))
  4131. (defun org-get-heading (&optional no-tags)
  4132. "Return the heading of the current entry, without the stars."
  4133. (save-excursion
  4134. (org-back-to-heading t)
  4135. (if (looking-at
  4136. (if no-tags
  4137. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4138. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4139. (match-string 1) "")))
  4140. (defun org-insert-heading-after-current ()
  4141. "Insert a new heading with same level as current, after current subtree."
  4142. (interactive)
  4143. (org-back-to-heading)
  4144. (org-insert-heading)
  4145. (org-move-subtree-down)
  4146. (end-of-line 1))
  4147. (defun org-insert-todo-heading (arg)
  4148. "Insert a new heading with the same level and TODO state as current heading.
  4149. If the heading has no TODO state, or if the state is DONE, use the first
  4150. state (TODO by default). Also with prefix arg, force first state."
  4151. (interactive "P")
  4152. (when (not (org-insert-item 'checkbox))
  4153. (org-insert-heading)
  4154. (save-excursion
  4155. (org-back-to-heading)
  4156. (outline-previous-heading)
  4157. (looking-at org-todo-line-regexp))
  4158. (if (or arg
  4159. (not (match-beginning 2))
  4160. (member (match-string 2) org-done-keywords))
  4161. (insert (car org-todo-keywords-1) " ")
  4162. (insert (match-string 2) " "))
  4163. (when org-provide-todo-statistics
  4164. (org-update-parent-todo-statistics))))
  4165. (defun org-insert-subheading (arg)
  4166. "Insert a new subheading and demote it.
  4167. Works for outline headings and for plain lists alike."
  4168. (interactive "P")
  4169. (org-insert-heading arg)
  4170. (cond
  4171. ((org-on-heading-p) (org-do-demote))
  4172. ((org-at-item-p) (org-indent-item 1))))
  4173. (defun org-insert-todo-subheading (arg)
  4174. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4175. Works for outline headings and for plain lists alike."
  4176. (interactive "P")
  4177. (org-insert-todo-heading arg)
  4178. (cond
  4179. ((org-on-heading-p) (org-do-demote))
  4180. ((org-at-item-p) (org-indent-item 1))))
  4181. ;;; Promotion and Demotion
  4182. (defun org-promote-subtree ()
  4183. "Promote the entire subtree.
  4184. See also `org-promote'."
  4185. (interactive)
  4186. (save-excursion
  4187. (org-map-tree 'org-promote))
  4188. (org-fix-position-after-promote))
  4189. (defun org-demote-subtree ()
  4190. "Demote the entire subtree. See `org-demote'.
  4191. See also `org-promote'."
  4192. (interactive)
  4193. (save-excursion
  4194. (org-map-tree 'org-demote))
  4195. (org-fix-position-after-promote))
  4196. (defun org-do-promote ()
  4197. "Promote the current heading higher up the tree.
  4198. If the region is active in `transient-mark-mode', promote all headings
  4199. in the region."
  4200. (interactive)
  4201. (save-excursion
  4202. (if (org-region-active-p)
  4203. (org-map-region 'org-promote (region-beginning) (region-end))
  4204. (org-promote)))
  4205. (org-fix-position-after-promote))
  4206. (defun org-do-demote ()
  4207. "Demote the current heading lower down the tree.
  4208. If the region is active in `transient-mark-mode', demote all headings
  4209. in the region."
  4210. (interactive)
  4211. (save-excursion
  4212. (if (org-region-active-p)
  4213. (org-map-region 'org-demote (region-beginning) (region-end))
  4214. (org-demote)))
  4215. (org-fix-position-after-promote))
  4216. (defun org-fix-position-after-promote ()
  4217. "Make sure that after pro/demotion cursor position is right."
  4218. (let ((pos (point)))
  4219. (when (save-excursion
  4220. (beginning-of-line 1)
  4221. (looking-at org-todo-line-regexp)
  4222. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4223. (cond ((eobp) (insert " "))
  4224. ((eolp) (insert " "))
  4225. ((equal (char-after) ?\ ) (forward-char 1))))))
  4226. (defun org-reduced-level (l)
  4227. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4228. (defun org-get-valid-level (level &optional change)
  4229. "Rectify a level change under the influence of `org-odd-levels-only'
  4230. LEVEL is a current level, CHANGE is by how much the level should be
  4231. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4232. even level numbers will become the next higher odd number."
  4233. (if org-odd-levels-only
  4234. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4235. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4236. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4237. (max 1 (+ level change))))
  4238. (if (boundp 'define-obsolete-function-alias)
  4239. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4240. (define-obsolete-function-alias 'org-get-legal-level
  4241. 'org-get-valid-level)
  4242. (define-obsolete-function-alias 'org-get-legal-level
  4243. 'org-get-valid-level "23.1")))
  4244. (defun org-promote ()
  4245. "Promote the current heading higher up the tree.
  4246. If the region is active in `transient-mark-mode', promote all headings
  4247. in the region."
  4248. (org-back-to-heading t)
  4249. (let* ((level (save-match-data (funcall outline-level)))
  4250. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4251. (diff (abs (- level (length up-head) -1))))
  4252. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4253. (replace-match up-head nil t)
  4254. ;; Fixup tag positioning
  4255. (and org-auto-align-tags (org-set-tags nil t))
  4256. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4257. (defun org-demote ()
  4258. "Demote the current heading lower down the tree.
  4259. If the region is active in `transient-mark-mode', demote all headings
  4260. in the region."
  4261. (org-back-to-heading t)
  4262. (let* ((level (save-match-data (funcall outline-level)))
  4263. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4264. (diff (abs (- level (length down-head) -1))))
  4265. (replace-match down-head nil t)
  4266. ;; Fixup tag positioning
  4267. (and org-auto-align-tags (org-set-tags nil t))
  4268. (if org-adapt-indentation (org-fixup-indentation diff))))
  4269. (defun org-map-tree (fun)
  4270. "Call FUN for every heading underneath the current one."
  4271. (org-back-to-heading)
  4272. (let ((level (funcall outline-level)))
  4273. (save-excursion
  4274. (funcall fun)
  4275. (while (and (progn
  4276. (outline-next-heading)
  4277. (> (funcall outline-level) level))
  4278. (not (eobp)))
  4279. (funcall fun)))))
  4280. (defun org-map-region (fun beg end)
  4281. "Call FUN for every heading between BEG and END."
  4282. (let ((org-ignore-region t))
  4283. (save-excursion
  4284. (setq end (copy-marker end))
  4285. (goto-char beg)
  4286. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4287. (< (point) end))
  4288. (funcall fun))
  4289. (while (and (progn
  4290. (outline-next-heading)
  4291. (< (point) end))
  4292. (not (eobp)))
  4293. (funcall fun)))))
  4294. (defun org-fixup-indentation (diff)
  4295. "Change the indentation in the current entry by DIFF
  4296. However, if any line in the current entry has no indentation, or if it
  4297. would end up with no indentation after the change, nothing at all is done."
  4298. (save-excursion
  4299. (let ((end (save-excursion (outline-next-heading)
  4300. (point-marker)))
  4301. (prohibit (if (> diff 0)
  4302. "^\\S-"
  4303. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4304. col)
  4305. (unless (save-excursion (end-of-line 1)
  4306. (re-search-forward prohibit end t))
  4307. (while (and (< (point) end)
  4308. (re-search-forward "^[ \t]+" end t))
  4309. (goto-char (match-end 0))
  4310. (setq col (current-column))
  4311. (if (< diff 0) (replace-match ""))
  4312. (indent-to (+ diff col))))
  4313. (move-marker end nil))))
  4314. (defun org-convert-to-odd-levels ()
  4315. "Convert an org-mode file with all levels allowed to one with odd levels.
  4316. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4317. level 5 etc."
  4318. (interactive)
  4319. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4320. (let ((org-odd-levels-only nil) n)
  4321. (save-excursion
  4322. (goto-char (point-min))
  4323. (while (re-search-forward "^\\*\\*+ " nil t)
  4324. (setq n (- (length (match-string 0)) 2))
  4325. (while (>= (setq n (1- n)) 0)
  4326. (org-demote))
  4327. (end-of-line 1))))))
  4328. (defun org-convert-to-oddeven-levels ()
  4329. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4330. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4331. section with an even level, conversion would destroy the structure of the file. An error
  4332. is signaled in this case."
  4333. (interactive)
  4334. (goto-char (point-min))
  4335. ;; First check if there are no even levels
  4336. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4337. (org-show-context t)
  4338. (error "Not all levels are odd in this file. Conversion not possible."))
  4339. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4340. (let ((org-odd-levels-only nil) n)
  4341. (save-excursion
  4342. (goto-char (point-min))
  4343. (while (re-search-forward "^\\*\\*+ " nil t)
  4344. (setq n (/ (1- (length (match-string 0))) 2))
  4345. (while (>= (setq n (1- n)) 0)
  4346. (org-promote))
  4347. (end-of-line 1))))))
  4348. (defun org-tr-level (n)
  4349. "Make N odd if required."
  4350. (if org-odd-levels-only (1+ (/ n 2)) n))
  4351. ;;; Vertical tree motion, cutting and pasting of subtrees
  4352. (defun org-move-subtree-up (&optional arg)
  4353. "Move the current subtree up past ARG headlines of the same level."
  4354. (interactive "p")
  4355. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4356. (defun org-move-subtree-down (&optional arg)
  4357. "Move the current subtree down past ARG headlines of the same level."
  4358. (interactive "p")
  4359. (setq arg (prefix-numeric-value arg))
  4360. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4361. 'outline-get-last-sibling))
  4362. (ins-point (make-marker))
  4363. (cnt (abs arg))
  4364. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4365. ;; Select the tree
  4366. (org-back-to-heading)
  4367. (setq beg0 (point))
  4368. (save-excursion
  4369. (setq ne-beg (org-back-over-empty-lines))
  4370. (setq beg (point)))
  4371. (save-match-data
  4372. (save-excursion (outline-end-of-heading)
  4373. (setq folded (org-invisible-p)))
  4374. (outline-end-of-subtree))
  4375. (outline-next-heading)
  4376. (setq ne-end (org-back-over-empty-lines))
  4377. (setq end (point))
  4378. (goto-char beg0)
  4379. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4380. ;; include less whitespace
  4381. (save-excursion
  4382. (goto-char beg)
  4383. (forward-line (- ne-beg ne-end))
  4384. (setq beg (point))))
  4385. ;; Find insertion point, with error handling
  4386. (while (> cnt 0)
  4387. (or (and (funcall movfunc) (looking-at outline-regexp))
  4388. (progn (goto-char beg0)
  4389. (error "Cannot move past superior level or buffer limit")))
  4390. (setq cnt (1- cnt)))
  4391. (if (> arg 0)
  4392. ;; Moving forward - still need to move over subtree
  4393. (progn (org-end-of-subtree t t)
  4394. (save-excursion
  4395. (org-back-over-empty-lines)
  4396. (or (bolp) (newline)))))
  4397. (setq ne-ins (org-back-over-empty-lines))
  4398. (move-marker ins-point (point))
  4399. (setq txt (buffer-substring beg end))
  4400. (org-save-markers-in-region beg end)
  4401. (delete-region beg end)
  4402. (outline-flag-region (1- beg) beg nil)
  4403. (outline-flag-region (1- (point)) (point) nil)
  4404. (let ((bbb (point)))
  4405. (insert-before-markers txt)
  4406. (org-reinstall-markers-in-region bbb)
  4407. (move-marker ins-point bbb))
  4408. (or (bolp) (insert "\n"))
  4409. (setq ins-end (point))
  4410. (goto-char ins-point)
  4411. (org-skip-whitespace)
  4412. (when (and (< arg 0)
  4413. (org-first-sibling-p)
  4414. (> ne-ins ne-beg))
  4415. ;; Move whitespace back to beginning
  4416. (save-excursion
  4417. (goto-char ins-end)
  4418. (let ((kill-whole-line t))
  4419. (kill-line (- ne-ins ne-beg)) (point)))
  4420. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4421. (move-marker ins-point nil)
  4422. (org-compact-display-after-subtree-move)
  4423. (org-show-empty-lines-in-parent)
  4424. (unless folded
  4425. (org-show-entry)
  4426. (show-children)
  4427. (org-cycle-hide-drawers 'children))))
  4428. (defvar org-subtree-clip ""
  4429. "Clipboard for cut and paste of subtrees.
  4430. This is actually only a copy of the kill, because we use the normal kill
  4431. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4432. (defvar org-subtree-clip-folded nil
  4433. "Was the last copied subtree folded?
  4434. This is used to fold the tree back after pasting.")
  4435. (defun org-cut-subtree (&optional n)
  4436. "Cut the current subtree into the clipboard.
  4437. With prefix arg N, cut this many sequential subtrees.
  4438. This is a short-hand for marking the subtree and then cutting it."
  4439. (interactive "p")
  4440. (org-copy-subtree n 'cut))
  4441. (defun org-copy-subtree (&optional n cut force-store-markers)
  4442. "Cut the current subtree into the clipboard.
  4443. With prefix arg N, cut this many sequential subtrees.
  4444. This is a short-hand for marking the subtree and then copying it.
  4445. If CUT is non-nil, actually cut the subtree.
  4446. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4447. of some markers in the region, even if CUT is non-nil. This is
  4448. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4449. (interactive "p")
  4450. (let (beg end folded (beg0 (point)))
  4451. (if (interactive-p)
  4452. (org-back-to-heading nil) ; take what looks like a subtree
  4453. (org-back-to-heading t)) ; take what is really there
  4454. (org-back-over-empty-lines)
  4455. (setq beg (point))
  4456. (skip-chars-forward " \t\r\n")
  4457. (save-match-data
  4458. (save-excursion (outline-end-of-heading)
  4459. (setq folded (org-invisible-p)))
  4460. (condition-case nil
  4461. (outline-forward-same-level (1- n))
  4462. (error nil))
  4463. (org-end-of-subtree t t))
  4464. (org-back-over-empty-lines)
  4465. (setq end (point))
  4466. (goto-char beg0)
  4467. (when (> end beg)
  4468. (setq org-subtree-clip-folded folded)
  4469. (when (or cut force-store-markers)
  4470. (org-save-markers-in-region beg end))
  4471. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4472. (setq org-subtree-clip (current-kill 0))
  4473. (message "%s: Subtree(s) with %d characters"
  4474. (if cut "Cut" "Copied")
  4475. (length org-subtree-clip)))))
  4476. (defun org-paste-subtree (&optional level tree)
  4477. "Paste the clipboard as a subtree, with modification of headline level.
  4478. The entire subtree is promoted or demoted in order to match a new headline
  4479. level. By default, the new level is derived from the visible headings
  4480. before and after the insertion point, and taken to be the inferior headline
  4481. level of the two. So if the previous visible heading is level 3 and the
  4482. next is level 4 (or vice versa), level 4 will be used for insertion.
  4483. This makes sure that the subtree remains an independent subtree and does
  4484. not swallow low level entries.
  4485. You can also force a different level, either by using a numeric prefix
  4486. argument, or by inserting the heading marker by hand. For example, if the
  4487. cursor is after \"*****\", then the tree will be shifted to level 5.
  4488. If you want to insert the tree as is, just use \\[yank].
  4489. If optional TREE is given, use this text instead of the kill ring."
  4490. (interactive "P")
  4491. (unless (org-kill-is-subtree-p tree)
  4492. (error "%s"
  4493. (substitute-command-keys
  4494. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4495. (let* ((visp (not (org-invisible-p)))
  4496. (txt (or tree (and kill-ring (current-kill 0))))
  4497. (^re (concat "^\\(" outline-regexp "\\)"))
  4498. (re (concat "\\(" outline-regexp "\\)"))
  4499. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4500. (old-level (if (string-match ^re txt)
  4501. (- (match-end 0) (match-beginning 0) 1)
  4502. -1))
  4503. (force-level (cond (level (prefix-numeric-value level))
  4504. ((string-match
  4505. ^re_ (buffer-substring (point-at-bol) (point)))
  4506. (- (match-end 1) (match-beginning 1)))
  4507. (t nil)))
  4508. (previous-level (save-excursion
  4509. (condition-case nil
  4510. (progn
  4511. (outline-previous-visible-heading 1)
  4512. (if (looking-at re)
  4513. (- (match-end 0) (match-beginning 0) 1)
  4514. 1))
  4515. (error 1))))
  4516. (next-level (save-excursion
  4517. (condition-case nil
  4518. (progn
  4519. (or (looking-at outline-regexp)
  4520. (outline-next-visible-heading 1))
  4521. (if (looking-at re)
  4522. (- (match-end 0) (match-beginning 0) 1)
  4523. 1))
  4524. (error 1))))
  4525. (new-level (or force-level (max previous-level next-level)))
  4526. (shift (if (or (= old-level -1)
  4527. (= new-level -1)
  4528. (= old-level new-level))
  4529. 0
  4530. (- new-level old-level)))
  4531. (delta (if (> shift 0) -1 1))
  4532. (func (if (> shift 0) 'org-demote 'org-promote))
  4533. (org-odd-levels-only nil)
  4534. beg end)
  4535. ;; Remove the forced level indicator
  4536. (if force-level
  4537. (delete-region (point-at-bol) (point)))
  4538. ;; Paste
  4539. (beginning-of-line 1)
  4540. (org-back-over-empty-lines)
  4541. (setq beg (point))
  4542. (insert-before-markers txt)
  4543. (unless (string-match "\n\\'" txt) (insert "\n"))
  4544. (org-reinstall-markers-in-region beg)
  4545. (setq end (point))
  4546. (goto-char beg)
  4547. (skip-chars-forward " \t\n\r")
  4548. (setq beg (point))
  4549. (if (and (org-invisible-p) visp)
  4550. (save-excursion (outline-show-heading)))
  4551. ;; Shift if necessary
  4552. (unless (= shift 0)
  4553. (save-restriction
  4554. (narrow-to-region beg end)
  4555. (while (not (= shift 0))
  4556. (org-map-region func (point-min) (point-max))
  4557. (setq shift (+ delta shift)))
  4558. (goto-char (point-min))))
  4559. (when (interactive-p)
  4560. (message "Clipboard pasted as level %d subtree" new-level))
  4561. (if (and kill-ring
  4562. (eq org-subtree-clip (current-kill 0))
  4563. org-subtree-clip-folded)
  4564. ;; The tree was folded before it was killed/copied
  4565. (hide-subtree))))
  4566. (defun org-kill-is-subtree-p (&optional txt)
  4567. "Check if the current kill is an outline subtree, or a set of trees.
  4568. Returns nil if kill does not start with a headline, or if the first
  4569. headline level is not the largest headline level in the tree.
  4570. So this will actually accept several entries of equal levels as well,
  4571. which is OK for `org-paste-subtree'.
  4572. If optional TXT is given, check this string instead of the current kill."
  4573. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4574. (start-level (and kill
  4575. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4576. org-outline-regexp "\\)")
  4577. kill)
  4578. (- (match-end 2) (match-beginning 2) 1)))
  4579. (re (concat "^" org-outline-regexp))
  4580. (start (1+ (match-beginning 2))))
  4581. (if (not start-level)
  4582. (progn
  4583. nil) ;; does not even start with a heading
  4584. (catch 'exit
  4585. (while (setq start (string-match re kill (1+ start)))
  4586. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4587. (throw 'exit nil)))
  4588. t))))
  4589. (defvar org-markers-to-move nil
  4590. "Markers that should be moved with a cut-and-paste operation.
  4591. Those markers are stored together with their positions relative to
  4592. the start of the region.")
  4593. (defun org-save-markers-in-region (beg end)
  4594. "Check markers in region.
  4595. If these markers are between BEG and END, record their position relative
  4596. to BEG, so that after moving the block of text, we can put the markers back
  4597. into place.
  4598. This function gets called just before an entry or tree gets cut from the
  4599. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4600. called immediately, to move the markers with the entries."
  4601. (setq org-markers-to-move nil)
  4602. (when (featurep 'org-clock)
  4603. (org-clock-save-markers-for-cut-and-paste beg end))
  4604. (when (featurep 'org-agenda)
  4605. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4606. (defun org-check-and-save-marker (marker beg end)
  4607. "Check if MARKER is between BEG and END.
  4608. If yes, remember the marker and the distance to BEG."
  4609. (when (and (marker-buffer marker)
  4610. (equal (marker-buffer marker) (current-buffer)))
  4611. (if (and (>= marker beg) (< marker end))
  4612. (push (cons marker (- marker beg)) org-markers-to-move))))
  4613. (defun org-reinstall-markers-in-region (beg)
  4614. "Move all remembered markers to their position relative to BEG."
  4615. (mapc (lambda (x)
  4616. (move-marker (car x) (+ beg (cdr x))))
  4617. org-markers-to-move)
  4618. (setq org-markers-to-move nil))
  4619. (defun org-narrow-to-subtree ()
  4620. "Narrow buffer to the current subtree."
  4621. (interactive)
  4622. (save-excursion
  4623. (save-match-data
  4624. (narrow-to-region
  4625. (progn (org-back-to-heading) (point))
  4626. (progn (org-end-of-subtree t) (point))))))
  4627. ;;; Outline Sorting
  4628. (defun org-sort (with-case)
  4629. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4630. Optional argument WITH-CASE means sort case-sensitively."
  4631. (interactive "P")
  4632. (if (org-at-table-p)
  4633. (org-call-with-arg 'org-table-sort-lines with-case)
  4634. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4635. (defun org-sort-remove-invisible (s)
  4636. (remove-text-properties 0 (length s) org-rm-props s)
  4637. (while (string-match org-bracket-link-regexp s)
  4638. (setq s (replace-match (if (match-end 2)
  4639. (match-string 3 s)
  4640. (match-string 1 s)) t t s)))
  4641. s)
  4642. (defvar org-priority-regexp) ; defined later in the file
  4643. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4644. "Sort entries on a certain level of an outline tree.
  4645. If there is an active region, the entries in the region are sorted.
  4646. Else, if the cursor is before the first entry, sort the top-level items.
  4647. Else, the children of the entry at point are sorted.
  4648. Sorting can be alphabetically, numerically, and by date/time as given by
  4649. the first time stamp in the entry. The command prompts for the sorting
  4650. type unless it has been given to the function through the SORTING-TYPE
  4651. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4652. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4653. called with point at the beginning of the record. It must return either
  4654. a string or a number that should serve as the sorting key for that record.
  4655. Comparing entries ignores case by default. However, with an optional argument
  4656. WITH-CASE, the sorting considers case as well."
  4657. (interactive "P")
  4658. (let ((case-func (if with-case 'identity 'downcase))
  4659. start beg end stars re re2
  4660. txt what tmp plain-list-p)
  4661. ;; Find beginning and end of region to sort
  4662. (cond
  4663. ((org-region-active-p)
  4664. ;; we will sort the region
  4665. (setq end (region-end)
  4666. what "region")
  4667. (goto-char (region-beginning))
  4668. (if (not (org-on-heading-p)) (outline-next-heading))
  4669. (setq start (point)))
  4670. ((org-at-item-p)
  4671. ;; we will sort this plain list
  4672. (org-beginning-of-item-list) (setq start (point))
  4673. (org-end-of-item-list) (setq end (point))
  4674. (goto-char start)
  4675. (setq plain-list-p t
  4676. what "plain list"))
  4677. ((or (org-on-heading-p)
  4678. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4679. ;; we will sort the children of the current headline
  4680. (org-back-to-heading)
  4681. (setq start (point)
  4682. end (progn (org-end-of-subtree t t)
  4683. (org-back-over-empty-lines)
  4684. (point))
  4685. what "children")
  4686. (goto-char start)
  4687. (show-subtree)
  4688. (outline-next-heading))
  4689. (t
  4690. ;; we will sort the top-level entries in this file
  4691. (goto-char (point-min))
  4692. (or (org-on-heading-p) (outline-next-heading))
  4693. (setq start (point) end (point-max) what "top-level")
  4694. (goto-char start)
  4695. (show-all)))
  4696. (setq beg (point))
  4697. (if (>= beg end) (error "Nothing to sort"))
  4698. (unless plain-list-p
  4699. (looking-at "\\(\\*+\\)")
  4700. (setq stars (match-string 1)
  4701. re (concat "^" (regexp-quote stars) " +")
  4702. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4703. txt (buffer-substring beg end))
  4704. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4705. (if (and (not (equal stars "*")) (string-match re2 txt))
  4706. (error "Region to sort contains a level above the first entry")))
  4707. (unless sorting-type
  4708. (message
  4709. (if plain-list-p
  4710. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4711. "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:")
  4712. what)
  4713. (setq sorting-type (read-char-exclusive))
  4714. (and (= (downcase sorting-type) ?f)
  4715. (setq getkey-func
  4716. (completing-read "Sort using function: "
  4717. obarray 'fboundp t nil nil))
  4718. (setq getkey-func (intern getkey-func)))
  4719. (and (= (downcase sorting-type) ?r)
  4720. (setq property
  4721. (completing-read "Property: "
  4722. (mapcar 'list (org-buffer-property-keys t))
  4723. nil t))))
  4724. (message "Sorting entries...")
  4725. (save-restriction
  4726. (narrow-to-region start end)
  4727. (let ((dcst (downcase sorting-type))
  4728. (now (current-time)))
  4729. (sort-subr
  4730. (/= dcst sorting-type)
  4731. ;; This function moves to the beginning character of the "record" to
  4732. ;; be sorted.
  4733. (if plain-list-p
  4734. (lambda nil
  4735. (if (org-at-item-p) t (goto-char (point-max))))
  4736. (lambda nil
  4737. (if (re-search-forward re nil t)
  4738. (goto-char (match-beginning 0))
  4739. (goto-char (point-max)))))
  4740. ;; This function moves to the last character of the "record" being
  4741. ;; sorted.
  4742. (if plain-list-p
  4743. 'org-end-of-item
  4744. (lambda nil
  4745. (save-match-data
  4746. (condition-case nil
  4747. (outline-forward-same-level 1)
  4748. (error
  4749. (goto-char (point-max)))))))
  4750. ;; This function returns the value that gets sorted against.
  4751. (if plain-list-p
  4752. (lambda nil
  4753. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4754. (cond
  4755. ((= dcst ?n)
  4756. (string-to-number (buffer-substring (match-end 0)
  4757. (point-at-eol))))
  4758. ((= dcst ?a)
  4759. (buffer-substring (match-end 0) (point-at-eol)))
  4760. ((= dcst ?t)
  4761. (if (re-search-forward org-ts-regexp
  4762. (point-at-eol) t)
  4763. (org-time-string-to-time (match-string 0))
  4764. now))
  4765. ((= dcst ?f)
  4766. (if getkey-func
  4767. (progn
  4768. (setq tmp (funcall getkey-func))
  4769. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4770. tmp)
  4771. (error "Invalid key function `%s'" getkey-func)))
  4772. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4773. (lambda nil
  4774. (cond
  4775. ((= dcst ?n)
  4776. (if (looking-at org-complex-heading-regexp)
  4777. (string-to-number (match-string 4))
  4778. nil))
  4779. ((= dcst ?a)
  4780. (if (looking-at org-complex-heading-regexp)
  4781. (funcall case-func (match-string 4))
  4782. nil))
  4783. ((= dcst ?t)
  4784. (if (re-search-forward org-ts-regexp
  4785. (save-excursion
  4786. (forward-line 2)
  4787. (point)) t)
  4788. (org-time-string-to-time (match-string 0))
  4789. now))
  4790. ((= dcst ?p)
  4791. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4792. (string-to-char (match-string 2))
  4793. org-default-priority))
  4794. ((= dcst ?r)
  4795. (or (org-entry-get nil property) ""))
  4796. ((= dcst ?o)
  4797. (if (looking-at org-complex-heading-regexp)
  4798. (- 9999 (length (member (match-string 2)
  4799. org-todo-keywords-1)))))
  4800. ((= dcst ?f)
  4801. (if getkey-func
  4802. (progn
  4803. (setq tmp (funcall getkey-func))
  4804. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4805. tmp)
  4806. (error "Invalid key function `%s'" getkey-func)))
  4807. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4808. nil
  4809. (cond
  4810. ((= dcst ?a) 'string<)
  4811. ((= dcst ?t) 'time-less-p)
  4812. (t nil)))))
  4813. (message "Sorting entries...done")))
  4814. (defun org-do-sort (table what &optional with-case sorting-type)
  4815. "Sort TABLE of WHAT according to SORTING-TYPE.
  4816. The user will be prompted for the SORTING-TYPE if the call to this
  4817. function does not specify it. WHAT is only for the prompt, to indicate
  4818. what is being sorted. The sorting key will be extracted from
  4819. the car of the elements of the table.
  4820. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4821. (unless sorting-type
  4822. (message
  4823. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4824. what)
  4825. (setq sorting-type (read-char-exclusive)))
  4826. (let ((dcst (downcase sorting-type))
  4827. extractfun comparefun)
  4828. ;; Define the appropriate functions
  4829. (cond
  4830. ((= dcst ?n)
  4831. (setq extractfun 'string-to-number
  4832. comparefun (if (= dcst sorting-type) '< '>)))
  4833. ((= dcst ?a)
  4834. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4835. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4836. comparefun (if (= dcst sorting-type)
  4837. 'string<
  4838. (lambda (a b) (and (not (string< a b))
  4839. (not (string= a b)))))))
  4840. ((= dcst ?t)
  4841. (setq extractfun
  4842. (lambda (x)
  4843. (if (string-match org-ts-regexp x)
  4844. (time-to-seconds
  4845. (org-time-string-to-time (match-string 0 x)))
  4846. 0))
  4847. comparefun (if (= dcst sorting-type) '< '>)))
  4848. (t (error "Invalid sorting type `%c'" sorting-type)))
  4849. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4850. table)
  4851. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4852. ;;; Editing source examples
  4853. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4854. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4855. (defvar org-edit-src-force-single-line nil)
  4856. (defvar org-edit-src-from-org-mode nil)
  4857. (define-minor-mode org-exit-edit-mode
  4858. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4859. (defun org-edit-src-code ()
  4860. "Edit the source code example at point.
  4861. An indirect buffer is created, and that buffer is then narrowed to the
  4862. example at point and switched to the correct language mode. When done,
  4863. exit by killing the buffer with \\[org-edit-src-exit]."
  4864. (interactive)
  4865. (let ((line (org-current-line))
  4866. (case-fold-search t)
  4867. (msg (substitute-command-keys
  4868. "Edit, then exit with C-c ' (C-c and single quote)"))
  4869. (info (org-edit-src-find-region-and-lang))
  4870. (org-mode-p (eq major-mode 'org-mode))
  4871. beg end lang lang-f single)
  4872. (if (not info)
  4873. nil
  4874. (setq beg (nth 0 info)
  4875. end (nth 1 info)
  4876. lang (nth 2 info)
  4877. single (nth 3 info)
  4878. lang-f (intern (concat lang "-mode")))
  4879. (unless (functionp lang-f)
  4880. (error "No such language mode: %s" lang-f))
  4881. (goto-line line)
  4882. (if (get-buffer "*Org Edit Src Example*")
  4883. (kill-buffer "*Org Edit Src Example*"))
  4884. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4885. "*Org Edit Src Example*"))
  4886. (narrow-to-region beg end)
  4887. (remove-text-properties beg end '(display nil invisible nil
  4888. intangible nil))
  4889. (let ((org-inhibit-startup t))
  4890. (funcall lang-f))
  4891. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4892. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4893. (when org-mode-p
  4894. (goto-char (point-min))
  4895. (while (re-search-forward "^," nil t)
  4896. (replace-match "")))
  4897. (goto-line line)
  4898. (org-exit-edit-mode)
  4899. (org-set-local 'header-line-format msg)
  4900. (message "%s" msg)
  4901. t)))
  4902. (defun org-edit-fixed-width-region ()
  4903. "Edit the fixed-width ascii drawing at point.
  4904. This must be a region where each line starts with ca colon followed by
  4905. a space character.
  4906. An indirect buffer is created, and that buffer is then narrowed to the
  4907. example at point and switched to artist-mode. When done,
  4908. exit by killing the buffer with \\[org-edit-src-exit]."
  4909. (interactive)
  4910. (let ((line (org-current-line))
  4911. (case-fold-search t)
  4912. (msg (substitute-command-keys
  4913. "Edit, then exit with C-c ' (C-c and single quote)"))
  4914. (org-mode-p (eq major-mode 'org-mode))
  4915. beg end lang lang-f)
  4916. (beginning-of-line 1)
  4917. (if (looking-at "[ \t]*[^:\n \t]")
  4918. nil
  4919. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4920. (setq beg (point) end (match-end 0))
  4921. (save-excursion
  4922. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4923. (setq beg (point-at-bol 2))
  4924. (setq beg (point))))
  4925. (save-excursion
  4926. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4927. (setq end (match-beginning 0))
  4928. (setq end (point))))
  4929. (goto-line line)
  4930. (if (get-buffer "*Org Edit Picture*")
  4931. (kill-buffer "*Org Edit Picture*"))
  4932. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4933. "*Org Edit Picture*"))
  4934. (narrow-to-region beg end)
  4935. (remove-text-properties beg end '(display nil invisible nil
  4936. intangible nil))
  4937. (when (fboundp 'font-lock-unfontify-region)
  4938. (font-lock-unfontify-region (point-min) (point-max)))
  4939. (cond
  4940. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4941. (fundamental-mode)
  4942. (artist-mode 1))
  4943. (t (funcall org-edit-fixed-width-region-mode)))
  4944. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4945. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4946. (set (make-local-variable 'org-edit-src-picture) t)
  4947. (goto-char (point-min))
  4948. (while (re-search-forward "^[ \t]*: " nil t)
  4949. (replace-match ""))
  4950. (goto-line line)
  4951. (org-exit-edit-mode)
  4952. (org-set-local 'header-line-format msg)
  4953. (message "%s" msg)
  4954. t))))
  4955. (defun org-edit-src-find-region-and-lang ()
  4956. "Find the region and language for a local edit.
  4957. Return a list with beginning and end of the region, a string representing
  4958. the language, a switch telling of the content should be in a single line."
  4959. (let ((re-list
  4960. '(
  4961. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4962. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4963. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4964. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4965. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4966. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4967. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4968. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4969. ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
  4970. ("^#\\+html:" "\n" "html" single-line)
  4971. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4972. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4973. ("^#\\+latex:" "\n" "latex" single-line)
  4974. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4975. ("^#\\+ascii:" "\n" "ascii" single-line)
  4976. ))
  4977. (pos (point))
  4978. re re1 re2 single beg end lang)
  4979. (catch 'exit
  4980. (while (setq entry (pop re-list))
  4981. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  4982. single (nth 3 entry))
  4983. (save-excursion
  4984. (if (or (looking-at re1)
  4985. (re-search-backward re1 nil t))
  4986. (progn
  4987. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  4988. (if (and (re-search-forward re2 nil t)
  4989. (>= (match-end 0) pos))
  4990. (throw 'exit (list beg (match-beginning 0) lang single))))
  4991. (if (or (looking-at re2)
  4992. (re-search-forward re2 nil t))
  4993. (progn
  4994. (setq end (match-beginning 0))
  4995. (if (and (re-search-backward re1 nil t)
  4996. (<= (match-beginning 0) pos))
  4997. (throw 'exit
  4998. (list (match-end 0) end
  4999. (org-edit-src-get-lang lang) single)))))))))))
  5000. (defun org-edit-src-get-lang (lang)
  5001. "Extract the src language."
  5002. (let ((m (match-string 0)))
  5003. (cond
  5004. ((stringp lang) lang)
  5005. ((integerp lang) (match-string lang))
  5006. ((and (eq lang lang)
  5007. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5008. (match-string 1 m))
  5009. ((and (eq lang lang)
  5010. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5011. (match-string 1 m))
  5012. (t "fundamental"))))
  5013. (defun org-edit-src-exit ()
  5014. "Exit special edit and protect problematic lines."
  5015. (interactive)
  5016. (unless (buffer-base-buffer (current-buffer))
  5017. (error "This is not an indirect buffer, something is wrong..."))
  5018. (unless (> (point-min) 1)
  5019. (error "This buffer is not narrowed, something is wrong..."))
  5020. (goto-char (point-min))
  5021. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5022. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5023. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5024. (goto-char (point-min))
  5025. (while (re-search-forward "\n" nil t)
  5026. (replace-match " "))
  5027. (goto-char (point-min))
  5028. (if (looking-at "\\s-*") (replace-match " "))
  5029. (if (re-search-forward "\\s-+\\'" nil t)
  5030. (replace-match "")))
  5031. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5032. (goto-char (point-min))
  5033. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5034. (replace-match ",\\1"))
  5035. (when font-lock-mode
  5036. (font-lock-unfontify-region (point-min) (point-max)))
  5037. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5038. (when (org-bound-and-true-p org-edit-src-picture)
  5039. (goto-char (point-min))
  5040. (while (re-search-forward "^" nil t)
  5041. (replace-match ": "))
  5042. (when font-lock-mode
  5043. (font-lock-unfontify-region (point-min) (point-max)))
  5044. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5045. (kill-buffer (current-buffer))
  5046. (and (org-mode-p) (org-restart-font-lock)))
  5047. ;;;; Plain list items, including checkboxes
  5048. ;;; Plain list items
  5049. (defun org-at-item-p ()
  5050. "Is point in a line starting a hand-formatted item?"
  5051. (let ((llt org-plain-list-ordered-item-terminator))
  5052. (save-excursion
  5053. (goto-char (point-at-bol))
  5054. (looking-at
  5055. (cond
  5056. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5057. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5058. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5059. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5060. (defun org-in-item-p ()
  5061. "It the cursor inside a plain list item.
  5062. Does not have to be the first line."
  5063. (save-excursion
  5064. (condition-case nil
  5065. (progn
  5066. (org-beginning-of-item)
  5067. (org-at-item-p)
  5068. t)
  5069. (error nil))))
  5070. (defun org-insert-item (&optional checkbox)
  5071. "Insert a new item at the current level.
  5072. Return t when things worked, nil when we are not in an item."
  5073. (when (save-excursion
  5074. (condition-case nil
  5075. (progn
  5076. (org-beginning-of-item)
  5077. (org-at-item-p)
  5078. (if (org-invisible-p) (error "Invisible item"))
  5079. t)
  5080. (error nil)))
  5081. (let* ((bul (match-string 0))
  5082. (descp (save-excursion (goto-char (match-beginning 0))
  5083. (beginning-of-line 1)
  5084. (save-match-data
  5085. (looking-at "[ \t]*.*? ::"))))
  5086. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5087. (match-end 0)))
  5088. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5089. pos)
  5090. (if descp (setq checkbox nil))
  5091. (cond
  5092. ((and (org-at-item-p) (<= (point) eow))
  5093. ;; before the bullet
  5094. (beginning-of-line 1)
  5095. (open-line (if blank 2 1)))
  5096. ((<= (point) eow)
  5097. (beginning-of-line 1))
  5098. (t
  5099. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  5100. (end-of-line 1)
  5101. (delete-horizontal-space))
  5102. (newline (if blank 2 1))))
  5103. (insert bul
  5104. (if checkbox "[ ]" "")
  5105. (if descp (concat (if checkbox " " "")
  5106. (read-string "Term: ") " :: ") ""))
  5107. (just-one-space)
  5108. (setq pos (point))
  5109. (end-of-line 1)
  5110. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5111. (org-maybe-renumber-ordered-list)
  5112. (and checkbox (org-update-checkbox-count-maybe))
  5113. t))
  5114. ;;; Checkboxes
  5115. (defun org-at-item-checkbox-p ()
  5116. "Is point at a line starting a plain-list item with a checklet?"
  5117. (and (org-at-item-p)
  5118. (save-excursion
  5119. (goto-char (match-end 0))
  5120. (skip-chars-forward " \t")
  5121. (looking-at "\\[[- X]\\]"))))
  5122. (defun org-toggle-checkbox (&optional arg)
  5123. "Toggle the checkbox in the current line."
  5124. (interactive "P")
  5125. (catch 'exit
  5126. (let (beg end status (firstnew 'unknown))
  5127. (cond
  5128. ((org-region-active-p)
  5129. (setq beg (region-beginning) end (region-end)))
  5130. ((org-on-heading-p)
  5131. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5132. ((org-at-item-checkbox-p)
  5133. (let ((pos (point)))
  5134. (replace-match
  5135. (cond (arg "[-]")
  5136. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5137. (t "[ ]"))
  5138. t t)
  5139. (goto-char pos))
  5140. (throw 'exit t))
  5141. (t (error "Not at a checkbox or heading, and no active region")))
  5142. (save-excursion
  5143. (goto-char beg)
  5144. (while (< (point) end)
  5145. (when (org-at-item-checkbox-p)
  5146. (setq status (equal (match-string 0) "[X]"))
  5147. (when (eq firstnew 'unknown)
  5148. (setq firstnew (not status)))
  5149. (replace-match
  5150. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5151. (beginning-of-line 2)))))
  5152. (org-update-checkbox-count-maybe))
  5153. (defun org-update-checkbox-count-maybe ()
  5154. "Update checkbox statistics unless turned off by user."
  5155. (when org-provide-checkbox-statistics
  5156. (org-update-checkbox-count)))
  5157. (defun org-update-checkbox-count (&optional all)
  5158. "Update the checkbox statistics in the current section.
  5159. This will find all statistic cookies like [57%] and [6/12] and update them
  5160. with the current numbers. With optional prefix argument ALL, do this for
  5161. the whole buffer."
  5162. (interactive "P")
  5163. (save-excursion
  5164. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5165. (beg (condition-case nil
  5166. (progn (outline-back-to-heading) (point))
  5167. (error (point-min))))
  5168. (end (move-marker (make-marker)
  5169. (progn (outline-next-heading) (point))))
  5170. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5171. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5172. (re-find (concat re "\\|" re-box))
  5173. beg-cookie end-cookie is-percent c-on c-off lim
  5174. eline curr-ind next-ind continue-from startsearch
  5175. (cstat 0)
  5176. )
  5177. (when all
  5178. (goto-char (point-min))
  5179. (outline-next-heading)
  5180. (setq beg (point) end (point-max)))
  5181. (goto-char end)
  5182. ;; find each statistic cookie
  5183. (while (re-search-backward re-find beg t)
  5184. (setq beg-cookie (match-beginning 1)
  5185. end-cookie (match-end 1)
  5186. cstat (+ cstat (if end-cookie 1 0))
  5187. startsearch (point-at-eol)
  5188. continue-from (point-at-bol)
  5189. is-percent (match-beginning 2)
  5190. lim (cond
  5191. ((org-on-heading-p) (outline-next-heading) (point))
  5192. ((org-at-item-p) (org-end-of-item) (point))
  5193. (t nil))
  5194. c-on 0
  5195. c-off 0)
  5196. (when lim
  5197. ;; find first checkbox for this cookie and gather
  5198. ;; statistics from all that are at this indentation level
  5199. (goto-char startsearch)
  5200. (if (re-search-forward re-box lim t)
  5201. (progn
  5202. (org-beginning-of-item)
  5203. (setq curr-ind (org-get-indentation))
  5204. (setq next-ind curr-ind)
  5205. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5206. (save-excursion (end-of-line) (setq eline (point)))
  5207. (if (re-search-forward re-box eline t)
  5208. (if (member (match-string 2) '("[ ]" "[-]"))
  5209. (setq c-off (1+ c-off))
  5210. (setq c-on (1+ c-on))
  5211. )
  5212. )
  5213. (org-end-of-item)
  5214. (setq next-ind (org-get-indentation))
  5215. )))
  5216. (goto-char continue-from)
  5217. ;; update cookie
  5218. (when end-cookie
  5219. (delete-region beg-cookie end-cookie)
  5220. (goto-char beg-cookie)
  5221. (insert
  5222. (if is-percent
  5223. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5224. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5225. ;; update items checkbox if it has one
  5226. (when (org-at-item-p)
  5227. (org-beginning-of-item)
  5228. (when (and (> (+ c-on c-off) 0)
  5229. (re-search-forward re-box (point-at-eol) t))
  5230. (setq beg-cookie (match-beginning 2)
  5231. end-cookie (match-end 2))
  5232. (delete-region beg-cookie end-cookie)
  5233. (goto-char beg-cookie)
  5234. (cond ((= c-off 0) (insert "[X]"))
  5235. ((= c-on 0) (insert "[ ]"))
  5236. (t (insert "[-]")))
  5237. )))
  5238. (goto-char continue-from))
  5239. (when (interactive-p)
  5240. (message "Checkbox satistics updated %s (%d places)"
  5241. (if all "in entire file" "in current outline entry") cstat)))))
  5242. (defun org-get-checkbox-statistics-face ()
  5243. "Select the face for checkbox statistics.
  5244. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5245. it will be `org-todo'."
  5246. (if (match-end 1)
  5247. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5248. (if (and (> (match-end 2) (match-beginning 2))
  5249. (equal (match-string 2) (match-string 3)))
  5250. 'org-done
  5251. 'org-todo)))
  5252. (defun org-get-indentation (&optional line)
  5253. "Get the indentation of the current line, interpreting tabs.
  5254. When LINE is given, assume it represents a line and compute its indentation."
  5255. (if line
  5256. (if (string-match "^ *" (org-remove-tabs line))
  5257. (match-end 0))
  5258. (save-excursion
  5259. (beginning-of-line 1)
  5260. (skip-chars-forward " \t")
  5261. (current-column))))
  5262. (defun org-remove-tabs (s &optional width)
  5263. "Replace tabulators in S with spaces.
  5264. Assumes that s is a single line, starting in column 0."
  5265. (setq width (or width tab-width))
  5266. (while (string-match "\t" s)
  5267. (setq s (replace-match
  5268. (make-string
  5269. (- (* width (/ (+ (match-beginning 0) width) width))
  5270. (match-beginning 0)) ?\ )
  5271. t t s)))
  5272. s)
  5273. (defun org-fix-indentation (line ind)
  5274. "Fix indentation in LINE.
  5275. IND is a cons cell with target and minimum indentation.
  5276. If the current indenation in LINE is smaller than the minimum,
  5277. leave it alone. If it is larger than ind, set it to the target."
  5278. (let* ((l (org-remove-tabs line))
  5279. (i (org-get-indentation l))
  5280. (i1 (car ind)) (i2 (cdr ind)))
  5281. (if (>= i i2) (setq l (substring line i2)))
  5282. (if (> i1 0)
  5283. (concat (make-string i1 ?\ ) l)
  5284. l)))
  5285. (defun org-beginning-of-item ()
  5286. "Go to the beginning of the current hand-formatted item.
  5287. If the cursor is not in an item, throw an error."
  5288. (interactive)
  5289. (let ((pos (point))
  5290. (limit (save-excursion
  5291. (condition-case nil
  5292. (progn
  5293. (org-back-to-heading)
  5294. (beginning-of-line 2) (point))
  5295. (error (point-min)))))
  5296. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5297. ind ind1)
  5298. (if (org-at-item-p)
  5299. (beginning-of-line 1)
  5300. (beginning-of-line 1)
  5301. (skip-chars-forward " \t")
  5302. (setq ind (current-column))
  5303. (if (catch 'exit
  5304. (while t
  5305. (beginning-of-line 0)
  5306. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5307. (if (looking-at "[ \t]*$")
  5308. (setq ind1 ind-empty)
  5309. (skip-chars-forward " \t")
  5310. (setq ind1 (current-column)))
  5311. (if (< ind1 ind)
  5312. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5313. nil
  5314. (goto-char pos)
  5315. (error "Not in an item")))))
  5316. (defun org-end-of-item ()
  5317. "Go to the end of the current hand-formatted item.
  5318. If the cursor is not in an item, throw an error."
  5319. (interactive)
  5320. (let* ((pos (point))
  5321. ind1
  5322. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5323. (limit (save-excursion (outline-next-heading) (point)))
  5324. (ind (save-excursion
  5325. (org-beginning-of-item)
  5326. (skip-chars-forward " \t")
  5327. (current-column)))
  5328. (end (catch 'exit
  5329. (while t
  5330. (beginning-of-line 2)
  5331. (if (eobp) (throw 'exit (point)))
  5332. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5333. (if (looking-at "[ \t]*$")
  5334. (setq ind1 ind-empty)
  5335. (skip-chars-forward " \t")
  5336. (setq ind1 (current-column)))
  5337. (if (<= ind1 ind)
  5338. (throw 'exit (point-at-bol)))))))
  5339. (if end
  5340. (goto-char end)
  5341. (goto-char pos)
  5342. (error "Not in an item"))))
  5343. (defun org-next-item ()
  5344. "Move to the beginning of the next item in the current plain list.
  5345. Error if not at a plain list, or if this is the last item in the list."
  5346. (interactive)
  5347. (let (ind ind1 (pos (point)))
  5348. (org-beginning-of-item)
  5349. (setq ind (org-get-indentation))
  5350. (org-end-of-item)
  5351. (setq ind1 (org-get-indentation))
  5352. (unless (and (org-at-item-p) (= ind ind1))
  5353. (goto-char pos)
  5354. (error "On last item"))))
  5355. (defun org-previous-item ()
  5356. "Move to the beginning of the previous item in the current plain list.
  5357. Error if not at a plain list, or if this is the first item in the list."
  5358. (interactive)
  5359. (let (beg ind ind1 (pos (point)))
  5360. (org-beginning-of-item)
  5361. (setq beg (point))
  5362. (setq ind (org-get-indentation))
  5363. (goto-char beg)
  5364. (catch 'exit
  5365. (while t
  5366. (beginning-of-line 0)
  5367. (if (looking-at "[ \t]*$")
  5368. nil
  5369. (if (<= (setq ind1 (org-get-indentation)) ind)
  5370. (throw 'exit t)))))
  5371. (condition-case nil
  5372. (if (or (not (org-at-item-p))
  5373. (< ind1 (1- ind)))
  5374. (error "")
  5375. (org-beginning-of-item))
  5376. (error (goto-char pos)
  5377. (error "On first item")))))
  5378. (defun org-first-list-item-p ()
  5379. "Is this heading the item in a plain list?"
  5380. (unless (org-at-item-p)
  5381. (error "Not at a plain list item"))
  5382. (org-beginning-of-item)
  5383. (= (point) (save-excursion (org-beginning-of-item-list))))
  5384. (defun org-move-item-down ()
  5385. "Move the plain list item at point down, i.e. swap with following item.
  5386. Subitems (items with larger indentation) are considered part of the item,
  5387. so this really moves item trees."
  5388. (interactive)
  5389. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5390. (org-beginning-of-item)
  5391. (setq beg0 (point))
  5392. (save-excursion
  5393. (setq ne-beg (org-back-over-empty-lines))
  5394. (setq beg (point)))
  5395. (goto-char beg0)
  5396. (setq ind (org-get-indentation))
  5397. (org-end-of-item)
  5398. (setq end0 (point))
  5399. (setq ind1 (org-get-indentation))
  5400. (setq ne-end (org-back-over-empty-lines))
  5401. (setq end (point))
  5402. (goto-char beg0)
  5403. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5404. ;; include less whitespace
  5405. (save-excursion
  5406. (goto-char beg)
  5407. (forward-line (- ne-beg ne-end))
  5408. (setq beg (point))))
  5409. (goto-char end0)
  5410. (if (and (org-at-item-p) (= ind ind1))
  5411. (progn
  5412. (org-end-of-item)
  5413. (org-back-over-empty-lines)
  5414. (setq txt (buffer-substring beg end))
  5415. (save-excursion
  5416. (delete-region beg end))
  5417. (setq pos (point))
  5418. (insert txt)
  5419. (goto-char pos) (org-skip-whitespace)
  5420. (org-maybe-renumber-ordered-list))
  5421. (goto-char pos)
  5422. (error "Cannot move this item further down"))))
  5423. (defun org-move-item-up (arg)
  5424. "Move the plain list item at point up, i.e. swap with previous item.
  5425. Subitems (items with larger indentation) are considered part of the item,
  5426. so this really moves item trees."
  5427. (interactive "p")
  5428. (let (beg beg0 end ind ind1 (pos (point)) txt
  5429. ne-beg ne-ins ins-end)
  5430. (org-beginning-of-item)
  5431. (setq beg0 (point))
  5432. (setq ind (org-get-indentation))
  5433. (save-excursion
  5434. (setq ne-beg (org-back-over-empty-lines))
  5435. (setq beg (point)))
  5436. (goto-char beg0)
  5437. (org-end-of-item)
  5438. (org-back-over-empty-lines)
  5439. (setq end (point))
  5440. (goto-char beg0)
  5441. (catch 'exit
  5442. (while t
  5443. (beginning-of-line 0)
  5444. (if (looking-at "[ \t]*$")
  5445. (if org-empty-line-terminates-plain-lists
  5446. (progn
  5447. (goto-char pos)
  5448. (error "Cannot move this item further up"))
  5449. nil)
  5450. (if (<= (setq ind1 (org-get-indentation)) ind)
  5451. (throw 'exit t)))))
  5452. (condition-case nil
  5453. (org-beginning-of-item)
  5454. (error (goto-char beg0)
  5455. (error "Cannot move this item further up")))
  5456. (setq ind1 (org-get-indentation))
  5457. (if (and (org-at-item-p) (= ind ind1))
  5458. (progn
  5459. (setq ne-ins (org-back-over-empty-lines))
  5460. (setq txt (buffer-substring beg end))
  5461. (save-excursion
  5462. (delete-region beg end))
  5463. (setq pos (point))
  5464. (insert txt)
  5465. (setq ins-end (point))
  5466. (goto-char pos) (org-skip-whitespace)
  5467. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5468. ;; Move whitespace back to beginning
  5469. (save-excursion
  5470. (goto-char ins-end)
  5471. (let ((kill-whole-line t))
  5472. (kill-line (- ne-ins ne-beg)) (point)))
  5473. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5474. (org-maybe-renumber-ordered-list))
  5475. (goto-char pos)
  5476. (error "Cannot move this item further up"))))
  5477. (defun org-maybe-renumber-ordered-list ()
  5478. "Renumber the ordered list at point if setup allows it.
  5479. This tests the user option `org-auto-renumber-ordered-lists' before
  5480. doing the renumbering."
  5481. (interactive)
  5482. (when (and org-auto-renumber-ordered-lists
  5483. (org-at-item-p))
  5484. (if (match-beginning 3)
  5485. (org-renumber-ordered-list 1)
  5486. (org-fix-bullet-type))))
  5487. (defun org-maybe-renumber-ordered-list-safe ()
  5488. (condition-case nil
  5489. (save-excursion
  5490. (org-maybe-renumber-ordered-list))
  5491. (error nil)))
  5492. (defun org-cycle-list-bullet (&optional which)
  5493. "Cycle through the different itemize/enumerate bullets.
  5494. This cycle the entire list level through the sequence:
  5495. `-' -> `+' -> `*' -> `1.' -> `1)'
  5496. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5497. 0 meand `-', 1 means `+' etc."
  5498. (interactive "P")
  5499. (org-preserve-lc
  5500. (org-beginning-of-item-list)
  5501. (org-at-item-p)
  5502. (beginning-of-line 1)
  5503. (let ((current (match-string 0))
  5504. (prevp (eq which 'previous))
  5505. new)
  5506. (setq new (cond
  5507. ((and (numberp which)
  5508. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5509. ((string-match "-" current) (if prevp "1)" "+"))
  5510. ((string-match "\\+" current)
  5511. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5512. ((string-match "\\*" current) (if prevp "+" "1."))
  5513. ((string-match "\\." current) (if prevp "*" "1)"))
  5514. ((string-match ")" current) (if prevp "1." "-"))
  5515. (t (error "This should not happen"))))
  5516. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5517. (org-fix-bullet-type)
  5518. (org-maybe-renumber-ordered-list))))
  5519. (defun org-get-string-indentation (s)
  5520. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5521. (let ((n -1) (i 0) (w tab-width) c)
  5522. (catch 'exit
  5523. (while (< (setq n (1+ n)) (length s))
  5524. (setq c (aref s n))
  5525. (cond ((= c ?\ ) (setq i (1+ i)))
  5526. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5527. (t (throw 'exit t)))))
  5528. i))
  5529. (defun org-renumber-ordered-list (arg)
  5530. "Renumber an ordered plain list.
  5531. Cursor needs to be in the first line of an item, the line that starts
  5532. with something like \"1.\" or \"2)\"."
  5533. (interactive "p")
  5534. (unless (and (org-at-item-p)
  5535. (match-beginning 3))
  5536. (error "This is not an ordered list"))
  5537. (let ((line (org-current-line))
  5538. (col (current-column))
  5539. (ind (org-get-string-indentation
  5540. (buffer-substring (point-at-bol) (match-beginning 3))))
  5541. ;; (term (substring (match-string 3) -1))
  5542. ind1 (n (1- arg))
  5543. fmt bob)
  5544. ;; find where this list begins
  5545. (org-beginning-of-item-list)
  5546. (setq bobp (bobp))
  5547. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5548. (setq fmt (concat "%d" (match-string 1)))
  5549. (beginning-of-line 0)
  5550. ;; walk forward and replace these numbers
  5551. (catch 'exit
  5552. (while t
  5553. (catch 'next
  5554. (if bobp (setq bobp nil) (beginning-of-line 2))
  5555. (if (eobp) (throw 'exit nil))
  5556. (if (looking-at "[ \t]*$") (throw 'next nil))
  5557. (skip-chars-forward " \t") (setq ind1 (current-column))
  5558. (if (> ind1 ind) (throw 'next t))
  5559. (if (< ind1 ind) (throw 'exit t))
  5560. (if (not (org-at-item-p)) (throw 'exit nil))
  5561. (delete-region (match-beginning 2) (match-end 2))
  5562. (goto-char (match-beginning 2))
  5563. (insert (format fmt (setq n (1+ n)))))))
  5564. (goto-line line)
  5565. (org-move-to-column col)))
  5566. (defun org-fix-bullet-type ()
  5567. "Make sure all items in this list have the same bullet as the firsst item."
  5568. (interactive)
  5569. (unless (org-at-item-p) (error "This is not a list"))
  5570. (let ((line (org-current-line))
  5571. (col (current-column))
  5572. (ind (current-indentation))
  5573. ind1 bullet)
  5574. ;; find where this list begins
  5575. (org-beginning-of-item-list)
  5576. (beginning-of-line 1)
  5577. ;; find out what the bullet type is
  5578. (looking-at "[ \t]*\\(\\S-+\\)")
  5579. (setq bullet (match-string 1))
  5580. ;; walk forward and replace these numbers
  5581. (beginning-of-line 0)
  5582. (catch 'exit
  5583. (while t
  5584. (catch 'next
  5585. (beginning-of-line 2)
  5586. (if (eobp) (throw 'exit nil))
  5587. (if (looking-at "[ \t]*$") (throw 'next nil))
  5588. (skip-chars-forward " \t") (setq ind1 (current-column))
  5589. (if (> ind1 ind) (throw 'next t))
  5590. (if (< ind1 ind) (throw 'exit t))
  5591. (if (not (org-at-item-p)) (throw 'exit nil))
  5592. (skip-chars-forward " \t")
  5593. (looking-at "\\S-+")
  5594. (replace-match bullet))))
  5595. (goto-line line)
  5596. (org-move-to-column col)
  5597. (if (string-match "[0-9]" bullet)
  5598. (org-renumber-ordered-list 1))))
  5599. (defun org-beginning-of-item-list ()
  5600. "Go to the beginning of the current item list.
  5601. I.e. to the first item in this list."
  5602. (interactive)
  5603. (org-beginning-of-item)
  5604. (let ((pos (point-at-bol))
  5605. (ind (org-get-indentation))
  5606. ind1)
  5607. ;; find where this list begins
  5608. (catch 'exit
  5609. (while t
  5610. (catch 'next
  5611. (beginning-of-line 0)
  5612. (if (looking-at "[ \t]*$")
  5613. (throw (if (bobp) 'exit 'next) t))
  5614. (skip-chars-forward " \t") (setq ind1 (current-column))
  5615. (if (or (< ind1 ind)
  5616. (and (= ind1 ind)
  5617. (not (org-at-item-p)))
  5618. (and (= (point-at-bol) (point-min))
  5619. (setq pos (point-min))))
  5620. (throw 'exit t)
  5621. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5622. (goto-char pos)))
  5623. (defun org-end-of-item-list ()
  5624. "Go to the end of the current item list.
  5625. I.e. to the text after the last item."
  5626. (interactive)
  5627. (org-beginning-of-item)
  5628. (let ((pos (point-at-bol))
  5629. (ind (org-get-indentation))
  5630. ind1)
  5631. ;; find where this list begins
  5632. (catch 'exit
  5633. (while t
  5634. (catch 'next
  5635. (beginning-of-line 2)
  5636. (if (looking-at "[ \t]*$")
  5637. (throw (if (eobp) 'exit 'next) t))
  5638. (skip-chars-forward " \t") (setq ind1 (current-column))
  5639. (if (or (< ind1 ind)
  5640. (and (= ind1 ind)
  5641. (not (org-at-item-p)))
  5642. (eobp))
  5643. (progn
  5644. (setq pos (point-at-bol))
  5645. (throw 'exit t))))))
  5646. (goto-char pos)))
  5647. (defvar org-last-indent-begin-marker (make-marker))
  5648. (defvar org-last-indent-end-marker (make-marker))
  5649. (defun org-outdent-item (arg)
  5650. "Outdent a local list item."
  5651. (interactive "p")
  5652. (org-indent-item (- arg)))
  5653. (defun org-indent-item (arg)
  5654. "Indent a local list item."
  5655. (interactive "p")
  5656. (unless (org-at-item-p)
  5657. (error "Not on an item"))
  5658. (save-excursion
  5659. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5660. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5661. (setq beg org-last-indent-begin-marker
  5662. end org-last-indent-end-marker)
  5663. (org-beginning-of-item)
  5664. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5665. (org-end-of-item)
  5666. (setq end (move-marker org-last-indent-end-marker (point))))
  5667. (goto-char beg)
  5668. (setq tmp (org-item-indent-positions)
  5669. ind (car tmp)
  5670. ind-down (nth 2 tmp)
  5671. ind-up (nth 1 tmp)
  5672. delta (if (> arg 0)
  5673. (if ind-down (- ind-down ind) 2)
  5674. (if ind-up (- ind-up ind) -2)))
  5675. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5676. (while (< (point) end)
  5677. (beginning-of-line 1)
  5678. (skip-chars-forward " \t") (setq ind1 (current-column))
  5679. (delete-region (point-at-bol) (point))
  5680. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5681. (beginning-of-line 2))))
  5682. (org-fix-bullet-type)
  5683. (org-maybe-renumber-ordered-list-safe)
  5684. (save-excursion
  5685. (beginning-of-line 0)
  5686. (condition-case nil (org-beginning-of-item) (error nil))
  5687. (org-maybe-renumber-ordered-list-safe)))
  5688. (defun org-item-indent-positions ()
  5689. "Return indentation for plain list items.
  5690. This returns a list with three values: The current indentation, the
  5691. parent indentation and the indentation a child should habe.
  5692. Assumes cursor in item line."
  5693. (let* ((bolpos (point-at-bol))
  5694. (ind (org-get-indentation))
  5695. ind-down ind-up pos)
  5696. (save-excursion
  5697. (org-beginning-of-item-list)
  5698. (skip-chars-backward "\n\r \t")
  5699. (when (org-in-item-p)
  5700. (org-beginning-of-item)
  5701. (setq ind-up (org-get-indentation))))
  5702. (setq pos (point))
  5703. (save-excursion
  5704. (cond
  5705. ((and (condition-case nil (progn (org-previous-item) t)
  5706. (error nil))
  5707. (or (forward-char 1) t)
  5708. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5709. (setq ind-down (org-get-indentation)))
  5710. ((and (goto-char pos)
  5711. (org-at-item-p))
  5712. (goto-char (match-end 0))
  5713. (skip-chars-forward " \t")
  5714. (setq ind-down (current-column)))))
  5715. (list ind ind-up ind-down)))
  5716. ;;; The orgstruct minor mode
  5717. ;; Define a minor mode which can be used in other modes in order to
  5718. ;; integrate the org-mode structure editing commands.
  5719. ;; This is really a hack, because the org-mode structure commands use
  5720. ;; keys which normally belong to the major mode. Here is how it
  5721. ;; works: The minor mode defines all the keys necessary to operate the
  5722. ;; structure commands, but wraps the commands into a function which
  5723. ;; tests if the cursor is currently at a headline or a plain list
  5724. ;; item. If that is the case, the structure command is used,
  5725. ;; temporarily setting many Org-mode variables like regular
  5726. ;; expressions for filling etc. However, when any of those keys is
  5727. ;; used at a different location, function uses `key-binding' to look
  5728. ;; up if the key has an associated command in another currently active
  5729. ;; keymap (minor modes, major mode, global), and executes that
  5730. ;; command. There might be problems if any of the keys is otherwise
  5731. ;; used as a prefix key.
  5732. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5733. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5734. ;; addresses this by checking explicitly for both bindings.
  5735. (defvar orgstruct-mode-map (make-sparse-keymap)
  5736. "Keymap for the minor `orgstruct-mode'.")
  5737. (defvar org-local-vars nil
  5738. "List of local variables, for use by `orgstruct-mode'")
  5739. ;;;###autoload
  5740. (define-minor-mode orgstruct-mode
  5741. "Toggle the minor more `orgstruct-mode'.
  5742. This mode is for using Org-mode structure commands in other modes.
  5743. The following key behave as if Org-mode was active, if the cursor
  5744. is on a headline, or on a plain list item (both in the definition
  5745. of Org-mode).
  5746. M-up Move entry/item up
  5747. M-down Move entry/item down
  5748. M-left Promote
  5749. M-right Demote
  5750. M-S-up Move entry/item up
  5751. M-S-down Move entry/item down
  5752. M-S-left Promote subtree
  5753. M-S-right Demote subtree
  5754. M-q Fill paragraph and items like in Org-mode
  5755. C-c ^ Sort entries
  5756. C-c - Cycle list bullet
  5757. TAB Cycle item visibility
  5758. M-RET Insert new heading/item
  5759. S-M-RET Insert new TODO heading / Chekbox item
  5760. C-c C-c Set tags / toggle checkbox"
  5761. nil " OrgStruct" nil
  5762. (org-load-modules-maybe)
  5763. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5764. ;;;###autoload
  5765. (defun turn-on-orgstruct ()
  5766. "Unconditionally turn on `orgstruct-mode'."
  5767. (orgstruct-mode 1))
  5768. ;;;###autoload
  5769. (defun turn-on-orgstruct++ ()
  5770. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5771. In addition to setting orgstruct-mode, this also exports all indentation and
  5772. autofilling variables from org-mode into the buffer. Note that turning
  5773. off orgstruct-mode will *not* remove these additional settings."
  5774. (orgstruct-mode 1)
  5775. (let (var val)
  5776. (mapc
  5777. (lambda (x)
  5778. (when (string-match
  5779. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5780. (symbol-name (car x)))
  5781. (setq var (car x) val (nth 1 x))
  5782. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5783. org-local-vars)))
  5784. (defun orgstruct-error ()
  5785. "Error when there is no default binding for a structure key."
  5786. (interactive)
  5787. (error "This key has no function outside structure elements"))
  5788. (defun orgstruct-setup ()
  5789. "Setup orgstruct keymaps."
  5790. (let ((nfunc 0)
  5791. (bindings
  5792. (list
  5793. '([(meta up)] org-metaup)
  5794. '([(meta down)] org-metadown)
  5795. '([(meta left)] org-metaleft)
  5796. '([(meta right)] org-metaright)
  5797. '([(meta shift up)] org-shiftmetaup)
  5798. '([(meta shift down)] org-shiftmetadown)
  5799. '([(meta shift left)] org-shiftmetaleft)
  5800. '([(meta shift right)] org-shiftmetaright)
  5801. '([(shift up)] org-shiftup)
  5802. '([(shift down)] org-shiftdown)
  5803. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5804. '("\M-q" fill-paragraph)
  5805. '("\C-c^" org-sort)
  5806. '("\C-c-" org-cycle-list-bullet)))
  5807. elt key fun cmd)
  5808. (while (setq elt (pop bindings))
  5809. (setq nfunc (1+ nfunc))
  5810. (setq key (org-key (car elt))
  5811. fun (nth 1 elt)
  5812. cmd (orgstruct-make-binding fun nfunc key))
  5813. (org-defkey orgstruct-mode-map key cmd))
  5814. ;; Special treatment needed for TAB and RET
  5815. (org-defkey orgstruct-mode-map [(tab)]
  5816. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5817. (org-defkey orgstruct-mode-map "\C-i"
  5818. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5819. (org-defkey orgstruct-mode-map "\M-\C-m"
  5820. (orgstruct-make-binding 'org-insert-heading 105
  5821. "\M-\C-m" [(meta return)]))
  5822. (org-defkey orgstruct-mode-map [(meta return)]
  5823. (orgstruct-make-binding 'org-insert-heading 106
  5824. [(meta return)] "\M-\C-m"))
  5825. (org-defkey orgstruct-mode-map [(shift meta return)]
  5826. (orgstruct-make-binding 'org-insert-todo-heading 107
  5827. [(meta return)] "\M-\C-m"))
  5828. (unless org-local-vars
  5829. (setq org-local-vars (org-get-local-variables)))
  5830. t))
  5831. (defun orgstruct-make-binding (fun n &rest keys)
  5832. "Create a function for binding in the structure minor mode.
  5833. FUN is the command to call inside a table. N is used to create a unique
  5834. command name. KEYS are keys that should be checked in for a command
  5835. to execute outside of tables."
  5836. (eval
  5837. (list 'defun
  5838. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5839. '(arg)
  5840. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5841. "Outside of structure, run the binding of `"
  5842. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5843. "'.")
  5844. '(interactive "p")
  5845. (list 'if
  5846. '(org-context-p 'headline 'item)
  5847. (list 'org-run-like-in-org-mode (list 'quote fun))
  5848. (list 'let '(orgstruct-mode)
  5849. (list 'call-interactively
  5850. (append '(or)
  5851. (mapcar (lambda (k)
  5852. (list 'key-binding k))
  5853. keys)
  5854. '('orgstruct-error))))))))
  5855. (defun org-context-p (&rest contexts)
  5856. "Check if local context is any of CONTEXTS.
  5857. Possible values in the list of contexts are `table', `headline', and `item'."
  5858. (let ((pos (point)))
  5859. (goto-char (point-at-bol))
  5860. (prog1 (or (and (memq 'table contexts)
  5861. (looking-at "[ \t]*|"))
  5862. (and (memq 'headline contexts)
  5863. ;;????????? (looking-at "\\*+"))
  5864. (looking-at outline-regexp))
  5865. (and (memq 'item contexts)
  5866. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5867. (goto-char pos))))
  5868. (defun org-get-local-variables ()
  5869. "Return a list of all local variables in an org-mode buffer."
  5870. (let (varlist)
  5871. (with-current-buffer (get-buffer-create "*Org tmp*")
  5872. (erase-buffer)
  5873. (org-mode)
  5874. (setq varlist (buffer-local-variables)))
  5875. (kill-buffer "*Org tmp*")
  5876. (delq nil
  5877. (mapcar
  5878. (lambda (x)
  5879. (setq x
  5880. (if (symbolp x)
  5881. (list x)
  5882. (list (car x) (list 'quote (cdr x)))))
  5883. (if (string-match
  5884. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5885. (symbol-name (car x)))
  5886. x nil))
  5887. varlist))))
  5888. ;;;###autoload
  5889. (defun org-run-like-in-org-mode (cmd)
  5890. (org-load-modules-maybe)
  5891. (unless org-local-vars
  5892. (setq org-local-vars (org-get-local-variables)))
  5893. (eval (list 'let org-local-vars
  5894. (list 'call-interactively (list 'quote cmd)))))
  5895. ;;;; Archiving
  5896. (defun org-get-category (&optional pos)
  5897. "Get the category applying to position POS."
  5898. (get-text-property (or pos (point)) 'org-category))
  5899. (defun org-refresh-category-properties ()
  5900. "Refresh category text properties in the buffer."
  5901. (let ((def-cat (cond
  5902. ((null org-category)
  5903. (if buffer-file-name
  5904. (file-name-sans-extension
  5905. (file-name-nondirectory buffer-file-name))
  5906. "???"))
  5907. ((symbolp org-category) (symbol-name org-category))
  5908. (t org-category)))
  5909. beg end cat pos optionp)
  5910. (org-unmodified
  5911. (save-excursion
  5912. (save-restriction
  5913. (widen)
  5914. (goto-char (point-min))
  5915. (put-text-property (point) (point-max) 'org-category def-cat)
  5916. (while (re-search-forward
  5917. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5918. (setq pos (match-end 0)
  5919. optionp (equal (char-after (match-beginning 0)) ?#)
  5920. cat (org-trim (match-string 2)))
  5921. (if optionp
  5922. (setq beg (point-at-bol) end (point-max))
  5923. (org-back-to-heading t)
  5924. (setq beg (point) end (org-end-of-subtree t t)))
  5925. (put-text-property beg end 'org-category cat)
  5926. (goto-char pos)))))))
  5927. ;;;; Link Stuff
  5928. ;;; Link abbreviations
  5929. (defun org-link-expand-abbrev (link)
  5930. "Apply replacements as defined in `org-link-abbrev-alist."
  5931. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5932. (let* ((key (match-string 1 link))
  5933. (as (or (assoc key org-link-abbrev-alist-local)
  5934. (assoc key org-link-abbrev-alist)))
  5935. (tag (and (match-end 2) (match-string 3 link)))
  5936. rpl)
  5937. (if (not as)
  5938. link
  5939. (setq rpl (cdr as))
  5940. (cond
  5941. ((symbolp rpl) (funcall rpl tag))
  5942. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5943. (t (concat rpl tag)))))
  5944. link))
  5945. ;;; Storing and inserting links
  5946. (defvar org-insert-link-history nil
  5947. "Minibuffer history for links inserted with `org-insert-link'.")
  5948. (defvar org-stored-links nil
  5949. "Contains the links stored with `org-store-link'.")
  5950. (defvar org-store-link-plist nil
  5951. "Plist with info about the most recently link created with `org-store-link'.")
  5952. (defvar org-link-protocols nil
  5953. "Link protocols added to Org-mode using `org-add-link-type'.")
  5954. (defvar org-store-link-functions nil
  5955. "List of functions that are called to create and store a link.
  5956. Each function will be called in turn until one returns a non-nil
  5957. value. Each function should check if it is responsible for creating
  5958. this link (for example by looking at the major mode).
  5959. If not, it must exit and return nil.
  5960. If yes, it should return a non-nil value after a calling
  5961. `org-store-link-props' with a list of properties and values.
  5962. Special properties are:
  5963. :type The link prefix. like \"http\". This must be given.
  5964. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5965. This is obligatory as well.
  5966. :description Optional default description for the second pair
  5967. of brackets in an Org-mode link. The user can still change
  5968. this when inserting this link into an Org-mode buffer.
  5969. In addition to these, any additional properties can be specified
  5970. and then used in remember templates.")
  5971. (defun org-add-link-type (type &optional follow export)
  5972. "Add TYPE to the list of `org-link-types'.
  5973. Re-compute all regular expressions depending on `org-link-types'
  5974. FOLLOW and EXPORT are two functions.
  5975. FOLLOW should take the link path as the single argument and do whatever
  5976. is necessary to follow the link, for example find a file or display
  5977. a mail message.
  5978. EXPORT should format the link path for export to one of the export formats.
  5979. It should be a function accepting three arguments:
  5980. path the path of the link, the text after the prefix (like \"http:\")
  5981. desc the description of the link, if any, nil if there was no descripton
  5982. format the export format, a symbol like `html' or `latex'.
  5983. The function may use the FORMAT information to return different values
  5984. depending on the format. The return value will be put literally into
  5985. the exported file.
  5986. Org-mode has a built-in default for exporting links. If you are happy with
  5987. this default, there is no need to define an export function for the link
  5988. type. For a simple example of an export function, see `org-bbdb.el'."
  5989. (add-to-list 'org-link-types type t)
  5990. (org-make-link-regexps)
  5991. (if (assoc type org-link-protocols)
  5992. (setcdr (assoc type org-link-protocols) (list follow export))
  5993. (push (list type follow export) org-link-protocols)))
  5994. ;;;###autoload
  5995. (defun org-store-link (arg)
  5996. "\\<org-mode-map>Store an org-link to the current location.
  5997. This link is added to `org-stored-links' and can later be inserted
  5998. into an org-buffer with \\[org-insert-link].
  5999. For some link types, a prefix arg is interpreted:
  6000. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6001. For file links, arg negates `org-context-in-file-links'."
  6002. (interactive "P")
  6003. (org-load-modules-maybe)
  6004. (setq org-store-link-plist nil) ; reset
  6005. (let (link cpltxt desc description search txt)
  6006. (cond
  6007. ((run-hook-with-args-until-success 'org-store-link-functions)
  6008. (setq link (plist-get org-store-link-plist :link)
  6009. desc (or (plist-get org-store-link-plist :description) link)))
  6010. ((eq major-mode 'calendar-mode)
  6011. (let ((cd (calendar-cursor-to-date)))
  6012. (setq link
  6013. (format-time-string
  6014. (car org-time-stamp-formats)
  6015. (apply 'encode-time
  6016. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6017. nil nil nil))))
  6018. (org-store-link-props :type "calendar" :date cd)))
  6019. ((eq major-mode 'w3-mode)
  6020. (setq cpltxt (url-view-url t)
  6021. link (org-make-link cpltxt))
  6022. (org-store-link-props :type "w3" :url (url-view-url t)))
  6023. ((eq major-mode 'w3m-mode)
  6024. (setq cpltxt (or w3m-current-title w3m-current-url)
  6025. link (org-make-link w3m-current-url))
  6026. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6027. ((setq search (run-hook-with-args-until-success
  6028. 'org-create-file-search-functions))
  6029. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6030. "::" search))
  6031. (setq cpltxt (or description link)))
  6032. ((eq major-mode 'image-mode)
  6033. (setq cpltxt (concat "file:"
  6034. (abbreviate-file-name buffer-file-name))
  6035. link (org-make-link cpltxt))
  6036. (org-store-link-props :type "image" :file buffer-file-name))
  6037. ((eq major-mode 'dired-mode)
  6038. ;; link to the file in the current line
  6039. (setq cpltxt (concat "file:"
  6040. (abbreviate-file-name
  6041. (expand-file-name
  6042. (dired-get-filename nil t))))
  6043. link (org-make-link cpltxt)))
  6044. ((and buffer-file-name (org-mode-p))
  6045. ;; Just link to current headline
  6046. (setq cpltxt (concat "file:"
  6047. (abbreviate-file-name buffer-file-name)))
  6048. ;; Add a context search string
  6049. (when (org-xor org-context-in-file-links arg)
  6050. ;; Check if we are on a target
  6051. (if (org-in-regexp "<<\\(.*?\\)>>")
  6052. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6053. (setq txt (cond
  6054. ((org-on-heading-p) nil)
  6055. ((org-region-active-p)
  6056. (buffer-substring (region-beginning) (region-end)))
  6057. (t nil)))
  6058. (when (or (null txt) (string-match "\\S-" txt))
  6059. (setq cpltxt
  6060. (concat cpltxt "::"
  6061. (condition-case nil
  6062. (org-make-org-heading-search-string txt)
  6063. (error "")))
  6064. desc "NONE"))))
  6065. (if (string-match "::\\'" cpltxt)
  6066. (setq cpltxt (substring cpltxt 0 -2)))
  6067. (setq link (org-make-link cpltxt)))
  6068. ((buffer-file-name (buffer-base-buffer))
  6069. ;; Just link to this file here.
  6070. (setq cpltxt (concat "file:"
  6071. (abbreviate-file-name
  6072. (buffer-file-name (buffer-base-buffer)))))
  6073. ;; Add a context string
  6074. (when (org-xor org-context-in-file-links arg)
  6075. (setq txt (if (org-region-active-p)
  6076. (buffer-substring (region-beginning) (region-end))
  6077. (buffer-substring (point-at-bol) (point-at-eol))))
  6078. ;; Only use search option if there is some text.
  6079. (when (string-match "\\S-" txt)
  6080. (setq cpltxt
  6081. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6082. desc "NONE")))
  6083. (setq link (org-make-link cpltxt)))
  6084. ((interactive-p)
  6085. (error "Cannot link to a buffer which is not visiting a file"))
  6086. (t (setq link nil)))
  6087. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6088. (setq link (or link cpltxt)
  6089. desc (or desc cpltxt))
  6090. (if (equal desc "NONE") (setq desc nil))
  6091. (if (and (interactive-p) link)
  6092. (progn
  6093. (setq org-stored-links
  6094. (cons (list link desc) org-stored-links))
  6095. (message "Stored: %s" (or desc link)))
  6096. (and link (org-make-link-string link desc)))))
  6097. (defun org-store-link-props (&rest plist)
  6098. "Store link properties, extract names and addresses."
  6099. (let (x adr)
  6100. (when (setq x (plist-get plist :from))
  6101. (setq adr (mail-extract-address-components x))
  6102. (plist-put plist :fromname (car adr))
  6103. (plist-put plist :fromaddress (nth 1 adr)))
  6104. (when (setq x (plist-get plist :to))
  6105. (setq adr (mail-extract-address-components x))
  6106. (plist-put plist :toname (car adr))
  6107. (plist-put plist :toaddress (nth 1 adr))))
  6108. (let ((from (plist-get plist :from))
  6109. (to (plist-get plist :to)))
  6110. (when (and from to org-from-is-user-regexp)
  6111. (plist-put plist :fromto
  6112. (if (string-match org-from-is-user-regexp from)
  6113. (concat "to %t")
  6114. (concat "from %f")))))
  6115. (setq org-store-link-plist plist))
  6116. (defun org-add-link-props (&rest plist)
  6117. "Add these properties to the link property list."
  6118. (let (key value)
  6119. (while plist
  6120. (setq key (pop plist) value (pop plist))
  6121. (setq org-store-link-plist
  6122. (plist-put org-store-link-plist key value)))))
  6123. (defun org-email-link-description (&optional fmt)
  6124. "Return the description part of an email link.
  6125. This takes information from `org-store-link-plist' and formats it
  6126. according to FMT (default from `org-email-link-description-format')."
  6127. (setq fmt (or fmt org-email-link-description-format))
  6128. (let* ((p org-store-link-plist)
  6129. (to (plist-get p :toaddress))
  6130. (from (plist-get p :fromaddress))
  6131. (table
  6132. (list
  6133. (cons "%c" (plist-get p :fromto))
  6134. (cons "%F" (plist-get p :from))
  6135. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6136. (cons "%T" (plist-get p :to))
  6137. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6138. (cons "%s" (plist-get p :subject))
  6139. (cons "%m" (plist-get p :message-id)))))
  6140. (when (string-match "%c" fmt)
  6141. ;; Check if the user wrote this message
  6142. (if (and org-from-is-user-regexp from to
  6143. (save-match-data (string-match org-from-is-user-regexp from)))
  6144. (setq fmt (replace-match "to %t" t t fmt))
  6145. (setq fmt (replace-match "from %f" t t fmt))))
  6146. (org-replace-escapes fmt table)))
  6147. (defun org-make-org-heading-search-string (&optional string heading)
  6148. "Make search string for STRING or current headline."
  6149. (interactive)
  6150. (let ((s (or string (org-get-heading))))
  6151. (unless (and string (not heading))
  6152. ;; We are using a headline, clean up garbage in there.
  6153. (if (string-match org-todo-regexp s)
  6154. (setq s (replace-match "" t t s)))
  6155. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6156. (setq s (replace-match "" t t s)))
  6157. (setq s (org-trim s))
  6158. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6159. org-comment-string "\\)") s)
  6160. (setq s (replace-match "" t t s)))
  6161. (while (string-match org-ts-regexp s)
  6162. (setq s (replace-match "" t t s))))
  6163. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6164. (setq s (replace-match " " t t s)))
  6165. (or string (setq s (concat "*" s))) ; Add * for headlines
  6166. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6167. (defun org-make-link (&rest strings)
  6168. "Concatenate STRINGS."
  6169. (apply 'concat strings))
  6170. (defun org-make-link-string (link &optional description)
  6171. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6172. (unless (string-match "\\S-" link)
  6173. (error "Empty link"))
  6174. (when (stringp description)
  6175. ;; Remove brackets from the description, they are fatal.
  6176. (while (string-match "\\[" description)
  6177. (setq description (replace-match "{" t t description)))
  6178. (while (string-match "\\]" description)
  6179. (setq description (replace-match "}" t t description))))
  6180. (when (equal (org-link-escape link) description)
  6181. ;; No description needed, it is identical
  6182. (setq description nil))
  6183. (when (and (not description)
  6184. (not (equal link (org-link-escape link))))
  6185. (setq description (org-extract-attributes link)))
  6186. (concat "[[" (org-link-escape link) "]"
  6187. (if description (concat "[" description "]") "")
  6188. "]"))
  6189. (defconst org-link-escape-chars
  6190. '((?\ . "%20")
  6191. (?\[ . "%5B")
  6192. (?\] . "%5D")
  6193. (?\340 . "%E0") ; `a
  6194. (?\342 . "%E2") ; ^a
  6195. (?\347 . "%E7") ; ,c
  6196. (?\350 . "%E8") ; `e
  6197. (?\351 . "%E9") ; 'e
  6198. (?\352 . "%EA") ; ^e
  6199. (?\356 . "%EE") ; ^i
  6200. (?\364 . "%F4") ; ^o
  6201. (?\371 . "%F9") ; `u
  6202. (?\373 . "%FB") ; ^u
  6203. (?\; . "%3B")
  6204. (?? . "%3F")
  6205. (?= . "%3D")
  6206. (?+ . "%2B")
  6207. )
  6208. "Association list of escapes for some characters problematic in links.
  6209. This is the list that is used for internal purposes.")
  6210. (defconst org-link-escape-chars-browser
  6211. '((?\ . "%20")) ; 32 for the SPC char
  6212. "Association list of escapes for some characters problematic in links.
  6213. This is the list that is used before handing over to the browser.")
  6214. (defun org-link-escape (text &optional table)
  6215. "Escape charaters in TEXT that are problematic for links."
  6216. (setq table (or table org-link-escape-chars))
  6217. (when text
  6218. (let ((re (mapconcat (lambda (x) (regexp-quote
  6219. (char-to-string (car x))))
  6220. table "\\|")))
  6221. (while (string-match re text)
  6222. (setq text
  6223. (replace-match
  6224. (cdr (assoc (string-to-char (match-string 0 text))
  6225. table))
  6226. t t text)))
  6227. text)))
  6228. (defun org-link-unescape (text &optional table)
  6229. "Reverse the action of `org-link-escape'."
  6230. (setq table (or table org-link-escape-chars))
  6231. (when text
  6232. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6233. table "\\|")))
  6234. (while (string-match re text)
  6235. (setq text
  6236. (replace-match
  6237. (char-to-string (car (rassoc (match-string 0 text) table)))
  6238. t t text)))
  6239. text)))
  6240. (defun org-xor (a b)
  6241. "Exclusive or."
  6242. (if a (not b) b))
  6243. (defun org-get-header (header)
  6244. "Find a header field in the current buffer."
  6245. (save-excursion
  6246. (goto-char (point-min))
  6247. (let ((case-fold-search t) s)
  6248. (cond
  6249. ((eq header 'from)
  6250. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6251. (setq s (match-string 1)))
  6252. (while (string-match "\"" s)
  6253. (setq s (replace-match "" t t s)))
  6254. (if (string-match "[<(].*" s)
  6255. (setq s (replace-match "" t t s))))
  6256. ((eq header 'message-id)
  6257. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6258. (setq s (match-string 1))))
  6259. ((eq header 'subject)
  6260. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6261. (setq s (match-string 1)))))
  6262. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6263. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6264. s)))
  6265. (defun org-fixup-message-id-for-http (s)
  6266. "Replace special characters in a message id, so it can be used in an http query."
  6267. (while (string-match "<" s)
  6268. (setq s (replace-match "%3C" t t s)))
  6269. (while (string-match ">" s)
  6270. (setq s (replace-match "%3E" t t s)))
  6271. (while (string-match "@" s)
  6272. (setq s (replace-match "%40" t t s)))
  6273. s)
  6274. ;;;###autoload
  6275. (defun org-insert-link-global ()
  6276. "Insert a link like Org-mode does.
  6277. This command can be called in any mode to insert a link in Org-mode syntax."
  6278. (interactive)
  6279. (org-load-modules-maybe)
  6280. (org-run-like-in-org-mode 'org-insert-link))
  6281. (defun org-insert-link (&optional complete-file link-location)
  6282. "Insert a link. At the prompt, enter the link.
  6283. Completion can be used to select a link previously stored with
  6284. `org-store-link'. When the empty string is entered (i.e. if you just
  6285. press RET at the prompt), the link defaults to the most recently
  6286. stored link. As SPC triggers completion in the minibuffer, you need to
  6287. use M-SPC or C-q SPC to force the insertion of a space character.
  6288. You will also be prompted for a description, and if one is given, it will
  6289. be displayed in the buffer instead of the link.
  6290. If there is already a link at point, this command will allow you to edit link
  6291. and description parts.
  6292. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6293. be selected using completion. The path to the file will be relative to the
  6294. current directory if the file is in the current directory or a subdirectory.
  6295. Otherwise, the link will be the absolute path as completed in the minibuffer
  6296. \(i.e. normally ~/path/to/file).
  6297. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6298. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6299. of `org-keep-stored-link-after-insertion'.
  6300. If `org-make-link-description-function' is non-nil, this function will be
  6301. called with the link target, and the result will be the default
  6302. link description.
  6303. If the LINK-LOCATION parameter is non-nil, this value will be
  6304. used as the link location instead of reading one interactively."
  6305. (interactive "P")
  6306. (let* ((wcf (current-window-configuration))
  6307. (region (if (org-region-active-p)
  6308. (buffer-substring (region-beginning) (region-end))))
  6309. (remove (and region (list (region-beginning) (region-end))))
  6310. (desc region)
  6311. tmphist ; byte-compile incorrectly complains about this
  6312. (link link-location)
  6313. entry file)
  6314. (cond
  6315. (link-location) ; specified by arg, just use it.
  6316. ((org-in-regexp org-bracket-link-regexp 1)
  6317. ;; We do have a link at point, and we are going to edit it.
  6318. (setq remove (list (match-beginning 0) (match-end 0)))
  6319. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6320. (setq link (read-string "Link: "
  6321. (org-link-unescape
  6322. (org-match-string-no-properties 1)))))
  6323. ((or (org-in-regexp org-angle-link-re)
  6324. (org-in-regexp org-plain-link-re))
  6325. ;; Convert to bracket link
  6326. (setq remove (list (match-beginning 0) (match-end 0))
  6327. link (read-string "Link: "
  6328. (org-remove-angle-brackets (match-string 0)))))
  6329. ((equal complete-file '(4))
  6330. ;; Completing read for file names.
  6331. (setq file (read-file-name "File: "))
  6332. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6333. (pwd1 (file-name-as-directory (abbreviate-file-name
  6334. (expand-file-name ".")))))
  6335. (cond
  6336. ((equal complete-file '(16))
  6337. (setq link (org-make-link
  6338. "file:"
  6339. (abbreviate-file-name (expand-file-name file)))))
  6340. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6341. (setq link (org-make-link "file:" (match-string 1 file))))
  6342. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6343. (expand-file-name file))
  6344. (setq link (org-make-link
  6345. "file:" (match-string 1 (expand-file-name file)))))
  6346. (t (setq link (org-make-link "file:" file))))))
  6347. (t
  6348. ;; Read link, with completion for stored links.
  6349. (with-output-to-temp-buffer "*Org Links*"
  6350. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6351. (when org-stored-links
  6352. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6353. (princ (mapconcat
  6354. (lambda (x)
  6355. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6356. (reverse org-stored-links) "\n"))))
  6357. (let ((cw (selected-window)))
  6358. (select-window (get-buffer-window "*Org Links*"))
  6359. (shrink-window-if-larger-than-buffer)
  6360. (setq truncate-lines t)
  6361. (select-window cw))
  6362. ;; Fake a link history, containing the stored links.
  6363. (setq tmphist (append (mapcar 'car org-stored-links)
  6364. org-insert-link-history))
  6365. (unwind-protect
  6366. (setq link (org-completing-read
  6367. "Link: "
  6368. (append
  6369. (mapcar (lambda (x) (list (concat (car x) ":")))
  6370. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6371. (mapcar (lambda (x) (list (concat x ":")))
  6372. org-link-types))
  6373. nil nil nil
  6374. 'tmphist
  6375. (or (car (car org-stored-links)))))
  6376. (set-window-configuration wcf)
  6377. (kill-buffer "*Org Links*"))
  6378. (setq entry (assoc link org-stored-links))
  6379. (or entry (push link org-insert-link-history))
  6380. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6381. (not org-keep-stored-link-after-insertion))
  6382. (setq org-stored-links (delq (assoc link org-stored-links)
  6383. org-stored-links)))
  6384. (setq desc (or desc (nth 1 entry)))))
  6385. (if (string-match org-plain-link-re link)
  6386. ;; URL-like link, normalize the use of angular brackets.
  6387. (setq link (org-make-link (org-remove-angle-brackets link))))
  6388. ;; Check if we are linking to the current file with a search option
  6389. ;; If yes, simplify the link by using only the search option.
  6390. (when (and buffer-file-name
  6391. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6392. (let* ((path (match-string 1 link))
  6393. (case-fold-search nil)
  6394. (search (match-string 2 link)))
  6395. (save-match-data
  6396. (if (equal (file-truename buffer-file-name) (file-truename path))
  6397. ;; We are linking to this same file, with a search option
  6398. (setq link search)))))
  6399. ;; Check if we can/should use a relative path. If yes, simplify the link
  6400. (when (string-match "\\<file:\\(.*\\)" link)
  6401. (let* ((path (match-string 1 link))
  6402. (origpath path)
  6403. (case-fold-search nil))
  6404. (cond
  6405. ((eq org-link-file-path-type 'absolute)
  6406. (setq path (abbreviate-file-name (expand-file-name path))))
  6407. ((eq org-link-file-path-type 'noabbrev)
  6408. (setq path (expand-file-name path)))
  6409. ((eq org-link-file-path-type 'relative)
  6410. (setq path (file-relative-name path)))
  6411. (t
  6412. (save-match-data
  6413. (if (string-match (concat "^" (regexp-quote
  6414. (file-name-as-directory
  6415. (expand-file-name "."))))
  6416. (expand-file-name path))
  6417. ;; We are linking a file with relative path name.
  6418. (setq path (substring (expand-file-name path)
  6419. (match-end 0)))))))
  6420. (setq link (concat "file:" path))
  6421. (if (equal desc origpath)
  6422. (setq desc path))))
  6423. (if org-make-link-description-function
  6424. (setq desc (funcall org-make-link-description-function link desc)))
  6425. (setq desc (read-string "Description: " desc))
  6426. (unless (string-match "\\S-" desc) (setq desc nil))
  6427. (if remove (apply 'delete-region remove))
  6428. (insert (org-make-link-string link desc))))
  6429. (defun org-completing-read (&rest args)
  6430. (let ((minibuffer-local-completion-map
  6431. (copy-keymap minibuffer-local-completion-map)))
  6432. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6433. (apply 'completing-read args)))
  6434. (defun org-extract-attributes (s)
  6435. "Extract the attributes cookie from a string and set as text property."
  6436. (let (a attr (start 0))
  6437. (save-match-data
  6438. (when (string-match "{{\\([^}]+\\)}}$" s)
  6439. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6440. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6441. (setq key (match-string 1 a) value (match-string 2 a)
  6442. start (match-end 0)
  6443. attr (plist-put attr (intern key) value))))
  6444. (org-add-props s nil 'org-attributes attr))
  6445. s))
  6446. (defun org-attributes-to-string (plist)
  6447. "Format a property list into an HTML attribute list."
  6448. (let ((s "") key value)
  6449. (while plist
  6450. (setq key (pop plist) value (pop plist))
  6451. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  6452. s))
  6453. ;;; Opening/following a link
  6454. (defvar org-link-search-failed nil)
  6455. (defun org-next-link ()
  6456. "Move forward to the next link.
  6457. If the link is in hidden text, expose it."
  6458. (interactive)
  6459. (when (and org-link-search-failed (eq this-command last-command))
  6460. (goto-char (point-min))
  6461. (message "Link search wrapped back to beginning of buffer"))
  6462. (setq org-link-search-failed nil)
  6463. (let* ((pos (point))
  6464. (ct (org-context))
  6465. (a (assoc :link ct)))
  6466. (if a (goto-char (nth 2 a)))
  6467. (if (re-search-forward org-any-link-re nil t)
  6468. (progn
  6469. (goto-char (match-beginning 0))
  6470. (if (org-invisible-p) (org-show-context)))
  6471. (goto-char pos)
  6472. (setq org-link-search-failed t)
  6473. (error "No further link found"))))
  6474. (defun org-previous-link ()
  6475. "Move backward to the previous link.
  6476. If the link is in hidden text, expose it."
  6477. (interactive)
  6478. (when (and org-link-search-failed (eq this-command last-command))
  6479. (goto-char (point-max))
  6480. (message "Link search wrapped back to end of buffer"))
  6481. (setq org-link-search-failed nil)
  6482. (let* ((pos (point))
  6483. (ct (org-context))
  6484. (a (assoc :link ct)))
  6485. (if a (goto-char (nth 1 a)))
  6486. (if (re-search-backward org-any-link-re nil t)
  6487. (progn
  6488. (goto-char (match-beginning 0))
  6489. (if (org-invisible-p) (org-show-context)))
  6490. (goto-char pos)
  6491. (setq org-link-search-failed t)
  6492. (error "No further link found"))))
  6493. (defun org-find-file-at-mouse (ev)
  6494. "Open file link or URL at mouse."
  6495. (interactive "e")
  6496. (mouse-set-point ev)
  6497. (org-open-at-point 'in-emacs))
  6498. (defun org-open-at-mouse (ev)
  6499. "Open file link or URL at mouse."
  6500. (interactive "e")
  6501. (mouse-set-point ev)
  6502. (org-open-at-point))
  6503. (defvar org-window-config-before-follow-link nil
  6504. "The window configuration before following a link.
  6505. This is saved in case the need arises to restore it.")
  6506. (defvar org-open-link-marker (make-marker)
  6507. "Marker pointing to the location where `org-open-at-point; was called.")
  6508. ;;;###autoload
  6509. (defun org-open-at-point-global ()
  6510. "Follow a link like Org-mode does.
  6511. This command can be called in any mode to follow a link that has
  6512. Org-mode syntax."
  6513. (interactive)
  6514. (org-run-like-in-org-mode 'org-open-at-point))
  6515. ;;;###autoload
  6516. (defun org-open-link-from-string (s &optional arg)
  6517. "Open a link in the string S, as if it was in Org-mode."
  6518. (interactive "sLink: \nP")
  6519. (with-temp-buffer
  6520. (let ((org-inhibit-startup t))
  6521. (org-mode)
  6522. (insert s)
  6523. (goto-char (point-min))
  6524. (org-open-at-point arg))))
  6525. (defun org-open-at-point (&optional in-emacs)
  6526. "Open link at or after point.
  6527. If there is no link at point, this function will search forward up to
  6528. the end of the current subtree.
  6529. Normally, files will be opened by an appropriate application. If the
  6530. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6531. (interactive "P")
  6532. (org-load-modules-maybe)
  6533. (move-marker org-open-link-marker (point))
  6534. (setq org-window-config-before-follow-link (current-window-configuration))
  6535. (org-remove-occur-highlights nil nil t)
  6536. (if (org-at-timestamp-p t)
  6537. (org-follow-timestamp-link)
  6538. (let (type path link line search (pos (point)))
  6539. (catch 'match
  6540. (save-excursion
  6541. (skip-chars-forward "^]\n\r")
  6542. (when (org-in-regexp org-bracket-link-regexp)
  6543. (setq link (org-extract-attributes
  6544. (org-link-unescape (org-match-string-no-properties 1))))
  6545. (while (string-match " *\n *" link)
  6546. (setq link (replace-match " " t t link)))
  6547. (setq link (org-link-expand-abbrev link))
  6548. (cond
  6549. ((or (file-name-absolute-p link)
  6550. (string-match "^\\.\\.?/" link))
  6551. (setq type "file" path link))
  6552. ((string-match org-link-re-with-space2 link)
  6553. (setq type (match-string 1 link) path (match-string 2 link)))
  6554. (t (setq type "thisfile" path link)))
  6555. (throw 'match t)))
  6556. (when (get-text-property (point) 'org-linked-text)
  6557. (setq type "thisfile"
  6558. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6559. (1+ (point)) (point))
  6560. path (buffer-substring
  6561. (previous-single-property-change pos 'org-linked-text)
  6562. (next-single-property-change pos 'org-linked-text)))
  6563. (throw 'match t))
  6564. (save-excursion
  6565. (when (or (org-in-regexp org-angle-link-re)
  6566. (org-in-regexp org-plain-link-re))
  6567. (setq type (match-string 1) path (match-string 2))
  6568. (throw 'match t)))
  6569. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6570. (setq type "tree-match"
  6571. path (match-string 1))
  6572. (throw 'match t))
  6573. (save-excursion
  6574. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6575. (setq type "tags"
  6576. path (match-string 1))
  6577. (while (string-match ":" path)
  6578. (setq path (replace-match "+" t t path)))
  6579. (throw 'match t))))
  6580. (unless path
  6581. (error "No link found"))
  6582. ;; Remove any trailing spaces in path
  6583. (if (string-match " +\\'" path)
  6584. (setq path (replace-match "" t t path)))
  6585. (cond
  6586. ((assoc type org-link-protocols)
  6587. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6588. ((equal type "mailto")
  6589. (let ((cmd (car org-link-mailto-program))
  6590. (args (cdr org-link-mailto-program)) args1
  6591. (address path) (subject "") a)
  6592. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6593. (setq address (match-string 1 path)
  6594. subject (org-link-escape (match-string 2 path))))
  6595. (while args
  6596. (cond
  6597. ((not (stringp (car args))) (push (pop args) args1))
  6598. (t (setq a (pop args))
  6599. (if (string-match "%a" a)
  6600. (setq a (replace-match address t t a)))
  6601. (if (string-match "%s" a)
  6602. (setq a (replace-match subject t t a)))
  6603. (push a args1))))
  6604. (apply cmd (nreverse args1))))
  6605. ((member type '("http" "https" "ftp" "news"))
  6606. (browse-url (concat type ":" (org-link-escape
  6607. path org-link-escape-chars-browser))))
  6608. ((member type '("message"))
  6609. (browse-url (concat type ":" path)))
  6610. ((string= type "tags")
  6611. (org-tags-view in-emacs path))
  6612. ((string= type "thisfile")
  6613. (if in-emacs
  6614. (switch-to-buffer-other-window
  6615. (org-get-buffer-for-internal-link (current-buffer)))
  6616. (org-mark-ring-push))
  6617. (let ((cmd `(org-link-search
  6618. ,path
  6619. ,(cond ((equal in-emacs '(4)) 'occur)
  6620. ((equal in-emacs '(16)) 'org-occur)
  6621. (t nil))
  6622. ,pos)))
  6623. (condition-case nil (eval cmd)
  6624. (error (progn (widen) (eval cmd))))))
  6625. ((string= type "tree-match")
  6626. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6627. ((string= type "file")
  6628. (if (string-match "::\\([0-9]+\\)\\'" path)
  6629. (setq line (string-to-number (match-string 1 path))
  6630. path (substring path 0 (match-beginning 0)))
  6631. (if (string-match "::\\(.+\\)\\'" path)
  6632. (setq search (match-string 1 path)
  6633. path (substring path 0 (match-beginning 0)))))
  6634. (if (string-match "[*?{]" (file-name-nondirectory path))
  6635. (dired path)
  6636. (org-open-file path in-emacs line search)))
  6637. ((string= type "news")
  6638. (require 'org-gnus)
  6639. (org-gnus-follow-link path))
  6640. ((string= type "shell")
  6641. (let ((cmd path))
  6642. (if (or (not org-confirm-shell-link-function)
  6643. (funcall org-confirm-shell-link-function
  6644. (format "Execute \"%s\" in shell? "
  6645. (org-add-props cmd nil
  6646. 'face 'org-warning))))
  6647. (progn
  6648. (message "Executing %s" cmd)
  6649. (shell-command cmd))
  6650. (error "Abort"))))
  6651. ((string= type "elisp")
  6652. (let ((cmd path))
  6653. (if (or (not org-confirm-elisp-link-function)
  6654. (funcall org-confirm-elisp-link-function
  6655. (format "Execute \"%s\" as elisp? "
  6656. (org-add-props cmd nil
  6657. 'face 'org-warning))))
  6658. (message "%s => %s" cmd (eval (read cmd)))
  6659. (error "Abort"))))
  6660. (t
  6661. (browse-url-at-point)))))
  6662. (move-marker org-open-link-marker nil)
  6663. (run-hook-with-args 'org-follow-link-hook))
  6664. ;;;; Time estimates
  6665. (defun org-get-effort (&optional pom)
  6666. "Get the effort estimate for the current entry."
  6667. (org-entry-get pom org-effort-property))
  6668. ;;; File search
  6669. (defvar org-create-file-search-functions nil
  6670. "List of functions to construct the right search string for a file link.
  6671. These functions are called in turn with point at the location to
  6672. which the link should point.
  6673. A function in the hook should first test if it would like to
  6674. handle this file type, for example by checking the major-mode or
  6675. the file extension. If it decides not to handle this file, it
  6676. should just return nil to give other functions a chance. If it
  6677. does handle the file, it must return the search string to be used
  6678. when following the link. The search string will be part of the
  6679. file link, given after a double colon, and `org-open-at-point'
  6680. will automatically search for it. If special measures must be
  6681. taken to make the search successful, another function should be
  6682. added to the companion hook `org-execute-file-search-functions',
  6683. which see.
  6684. A function in this hook may also use `setq' to set the variable
  6685. `description' to provide a suggestion for the descriptive text to
  6686. be used for this link when it gets inserted into an Org-mode
  6687. buffer with \\[org-insert-link].")
  6688. (defvar org-execute-file-search-functions nil
  6689. "List of functions to execute a file search triggered by a link.
  6690. Functions added to this hook must accept a single argument, the
  6691. search string that was part of the file link, the part after the
  6692. double colon. The function must first check if it would like to
  6693. handle this search, for example by checking the major-mode or the
  6694. file extension. If it decides not to handle this search, it
  6695. should just return nil to give other functions a chance. If it
  6696. does handle the search, it must return a non-nil value to keep
  6697. other functions from trying.
  6698. Each function can access the current prefix argument through the
  6699. variable `current-prefix-argument'. Note that a single prefix is
  6700. used to force opening a link in Emacs, so it may be good to only
  6701. use a numeric or double prefix to guide the search function.
  6702. In case this is needed, a function in this hook can also restore
  6703. the window configuration before `org-open-at-point' was called using:
  6704. (set-window-configuration org-window-config-before-follow-link)")
  6705. (defun org-link-search (s &optional type avoid-pos)
  6706. "Search for a link search option.
  6707. If S is surrounded by forward slashes, it is interpreted as a
  6708. regular expression. In org-mode files, this will create an `org-occur'
  6709. sparse tree. In ordinary files, `occur' will be used to list matches.
  6710. If the current buffer is in `dired-mode', grep will be used to search
  6711. in all files. If AVOID-POS is given, ignore matches near that position."
  6712. (let ((case-fold-search t)
  6713. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6714. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6715. (append '(("") (" ") ("\t") ("\n"))
  6716. org-emphasis-alist)
  6717. "\\|") "\\)"))
  6718. (pos (point))
  6719. (pre nil) (post nil)
  6720. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6721. (cond
  6722. ;; First check if there are any special
  6723. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6724. ;; Now try the builtin stuff
  6725. ((save-excursion
  6726. (goto-char (point-min))
  6727. (and
  6728. (re-search-forward
  6729. (concat "<<" (regexp-quote s0) ">>") nil t)
  6730. (setq type 'dedicated
  6731. pos (match-beginning 0))))
  6732. ;; There is an exact target for this
  6733. (goto-char pos))
  6734. ((string-match "^/\\(.*\\)/$" s)
  6735. ;; A regular expression
  6736. (cond
  6737. ((org-mode-p)
  6738. (org-occur (match-string 1 s)))
  6739. ;;((eq major-mode 'dired-mode)
  6740. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6741. (t (org-do-occur (match-string 1 s)))))
  6742. (t
  6743. ;; A normal search strings
  6744. (when (equal (string-to-char s) ?*)
  6745. ;; Anchor on headlines, post may include tags.
  6746. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6747. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6748. s (substring s 1)))
  6749. (remove-text-properties
  6750. 0 (length s)
  6751. '(face nil mouse-face nil keymap nil fontified nil) s)
  6752. ;; Make a series of regular expressions to find a match
  6753. (setq words (org-split-string s "[ \n\r\t]+")
  6754. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6755. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6756. "\\)" markers)
  6757. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6758. re2a (concat "[ \t\r\n]" re2a_)
  6759. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6760. re4 (concat "[^a-zA-Z_]" re4_)
  6761. re1 (concat pre re2 post)
  6762. re3 (concat pre (if pre re4_ re4) post)
  6763. re5 (concat pre ".*" re4)
  6764. re2 (concat pre re2)
  6765. re2a (concat pre (if pre re2a_ re2a))
  6766. re4 (concat pre (if pre re4_ re4))
  6767. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6768. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6769. re5 "\\)"
  6770. ))
  6771. (cond
  6772. ((eq type 'org-occur) (org-occur reall))
  6773. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6774. (t (goto-char (point-min))
  6775. (setq type 'fuzzy)
  6776. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6777. (org-search-not-self 1 re1 nil t)
  6778. (org-search-not-self 1 re2 nil t)
  6779. (org-search-not-self 1 re2a nil t)
  6780. (org-search-not-self 1 re3 nil t)
  6781. (org-search-not-self 1 re4 nil t)
  6782. (org-search-not-self 1 re5 nil t)
  6783. )
  6784. (goto-char (match-beginning 1))
  6785. (goto-char pos)
  6786. (error "No match")))))
  6787. (t
  6788. ;; Normal string-search
  6789. (goto-char (point-min))
  6790. (if (search-forward s nil t)
  6791. (goto-char (match-beginning 0))
  6792. (error "No match"))))
  6793. (and (org-mode-p) (org-show-context 'link-search))
  6794. type))
  6795. (defun org-search-not-self (group &rest args)
  6796. "Execute `re-search-forward', but only accept matches that do not
  6797. enclose the position of `org-open-link-marker'."
  6798. (let ((m org-open-link-marker))
  6799. (catch 'exit
  6800. (while (apply 're-search-forward args)
  6801. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6802. (goto-char (match-end group))
  6803. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6804. (> (match-beginning 0) (marker-position m))
  6805. (< (match-end 0) (marker-position m)))
  6806. (save-match-data
  6807. (or (not (org-in-regexp
  6808. org-bracket-link-analytic-regexp 1))
  6809. (not (match-end 4)) ; no description
  6810. (and (<= (match-beginning 4) (point))
  6811. (>= (match-end 4) (point))))))
  6812. (throw 'exit (point))))))))
  6813. (defun org-get-buffer-for-internal-link (buffer)
  6814. "Return a buffer to be used for displaying the link target of internal links."
  6815. (cond
  6816. ((not org-display-internal-link-with-indirect-buffer)
  6817. buffer)
  6818. ((string-match "(Clone)$" (buffer-name buffer))
  6819. (message "Buffer is already a clone, not making another one")
  6820. ;; we also do not modify visibility in this case
  6821. buffer)
  6822. (t ; make a new indirect buffer for displaying the link
  6823. (let* ((bn (buffer-name buffer))
  6824. (ibn (concat bn "(Clone)"))
  6825. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6826. (with-current-buffer ib (org-overview))
  6827. ib))))
  6828. (defun org-do-occur (regexp &optional cleanup)
  6829. "Call the Emacs command `occur'.
  6830. If CLEANUP is non-nil, remove the printout of the regular expression
  6831. in the *Occur* buffer. This is useful if the regex is long and not useful
  6832. to read."
  6833. (occur regexp)
  6834. (when cleanup
  6835. (let ((cwin (selected-window)) win beg end)
  6836. (when (setq win (get-buffer-window "*Occur*"))
  6837. (select-window win))
  6838. (goto-char (point-min))
  6839. (when (re-search-forward "match[a-z]+" nil t)
  6840. (setq beg (match-end 0))
  6841. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6842. (setq end (1- (match-beginning 0)))))
  6843. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6844. (goto-char (point-min))
  6845. (select-window cwin))))
  6846. ;;; The mark ring for links jumps
  6847. (defvar org-mark-ring nil
  6848. "Mark ring for positions before jumps in Org-mode.")
  6849. (defvar org-mark-ring-last-goto nil
  6850. "Last position in the mark ring used to go back.")
  6851. ;; Fill and close the ring
  6852. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6853. (loop for i from 1 to org-mark-ring-length do
  6854. (push (make-marker) org-mark-ring))
  6855. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6856. org-mark-ring)
  6857. (defun org-mark-ring-push (&optional pos buffer)
  6858. "Put the current position or POS into the mark ring and rotate it."
  6859. (interactive)
  6860. (setq pos (or pos (point)))
  6861. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6862. (move-marker (car org-mark-ring)
  6863. (or pos (point))
  6864. (or buffer (current-buffer)))
  6865. (message "%s"
  6866. (substitute-command-keys
  6867. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6868. (defun org-mark-ring-goto (&optional n)
  6869. "Jump to the previous position in the mark ring.
  6870. With prefix arg N, jump back that many stored positions. When
  6871. called several times in succession, walk through the entire ring.
  6872. Org-mode commands jumping to a different position in the current file,
  6873. or to another Org-mode file, automatically push the old position
  6874. onto the ring."
  6875. (interactive "p")
  6876. (let (p m)
  6877. (if (eq last-command this-command)
  6878. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6879. (setq p org-mark-ring))
  6880. (setq org-mark-ring-last-goto p)
  6881. (setq m (car p))
  6882. (switch-to-buffer (marker-buffer m))
  6883. (goto-char m)
  6884. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6885. (defun org-remove-angle-brackets (s)
  6886. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6887. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6888. s)
  6889. (defun org-add-angle-brackets (s)
  6890. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6891. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6892. s)
  6893. (defun org-remove-double-quotes (s)
  6894. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6895. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6896. s)
  6897. ;;; Following specific links
  6898. (defun org-follow-timestamp-link ()
  6899. (cond
  6900. ((org-at-date-range-p t)
  6901. (let ((org-agenda-start-on-weekday)
  6902. (t1 (match-string 1))
  6903. (t2 (match-string 2)))
  6904. (setq t1 (time-to-days (org-time-string-to-time t1))
  6905. t2 (time-to-days (org-time-string-to-time t2)))
  6906. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6907. ((org-at-timestamp-p t)
  6908. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6909. (substring (match-string 1) 0 10)))
  6910. 1))
  6911. (t (error "This should not happen"))))
  6912. ;;; Following file links
  6913. (defvar org-wait nil)
  6914. (defun org-open-file (path &optional in-emacs line search)
  6915. "Open the file at PATH.
  6916. First, this expands any special file name abbreviations. Then the
  6917. configuration variable `org-file-apps' is checked if it contains an
  6918. entry for this file type, and if yes, the corresponding command is launched.
  6919. If no application is found, Emacs simply visits the file.
  6920. With optional argument IN-EMACS, Emacs will visit the file.
  6921. Optional LINE specifies a line to go to, optional SEARCH a string to
  6922. search for. If LINE or SEARCH is given, the file will always be
  6923. opened in Emacs.
  6924. If the file does not exist, an error is thrown."
  6925. (setq in-emacs (or in-emacs line search))
  6926. (let* ((file (if (equal path "")
  6927. buffer-file-name
  6928. (substitute-in-file-name (expand-file-name path))))
  6929. (apps (append org-file-apps (org-default-apps)))
  6930. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6931. (dirp (if remp nil (file-directory-p file)))
  6932. (file (if (and dirp org-open-directory-means-index-dot-org)
  6933. (concat (file-name-as-directory file) "index.org")
  6934. file))
  6935. (dfile (downcase file))
  6936. (old-buffer (current-buffer))
  6937. (old-pos (point))
  6938. (old-mode major-mode)
  6939. ext cmd)
  6940. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6941. (setq ext (match-string 1 dfile))
  6942. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6943. (setq ext (match-string 1 dfile))))
  6944. (if in-emacs
  6945. (setq cmd 'emacs)
  6946. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6947. (and dirp (cdr (assoc 'directory apps)))
  6948. (cdr (assoc ext apps))
  6949. (cdr (assoc t apps)))))
  6950. (when (eq cmd 'mailcap)
  6951. (require 'mailcap)
  6952. (mailcap-parse-mailcaps)
  6953. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6954. (command (mailcap-mime-info mime-type)))
  6955. (if (stringp command)
  6956. (setq cmd command)
  6957. (setq cmd 'emacs))))
  6958. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6959. (not (file-exists-p file))
  6960. (not org-open-non-existing-files))
  6961. (error "No such file: %s" file))
  6962. (cond
  6963. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6964. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6965. (while (string-match "['\"]%s['\"]" cmd)
  6966. (setq cmd (replace-match "%s" t t cmd)))
  6967. (while (string-match "%s" cmd)
  6968. (setq cmd (replace-match
  6969. (save-match-data
  6970. (shell-quote-argument
  6971. (convert-standard-filename file)))
  6972. t t cmd)))
  6973. (save-window-excursion
  6974. (start-process-shell-command cmd nil cmd)
  6975. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6976. ))
  6977. ((or (stringp cmd)
  6978. (eq cmd 'emacs))
  6979. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6980. (widen)
  6981. (if line (goto-line line)
  6982. (if search (org-link-search search))))
  6983. ((consp cmd)
  6984. (let ((file (convert-standard-filename file)))
  6985. (eval cmd)))
  6986. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6987. (and (org-mode-p) (eq old-mode 'org-mode)
  6988. (or (not (equal old-buffer (current-buffer)))
  6989. (not (equal old-pos (point))))
  6990. (org-mark-ring-push old-pos old-buffer))))
  6991. (defun org-default-apps ()
  6992. "Return the default applications for this operating system."
  6993. (cond
  6994. ((eq system-type 'darwin)
  6995. org-file-apps-defaults-macosx)
  6996. ((eq system-type 'windows-nt)
  6997. org-file-apps-defaults-windowsnt)
  6998. (t org-file-apps-defaults-gnu)))
  6999. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7000. (defun org-file-remote-p (file)
  7001. "Test whether FILE specifies a location on a remote system.
  7002. Return non-nil if the location is indeed remote.
  7003. For example, the filename \"/user@host:/foo\" specifies a location
  7004. on the system \"/user@host:\"."
  7005. (cond ((fboundp 'file-remote-p)
  7006. (file-remote-p file))
  7007. ((fboundp 'tramp-handle-file-remote-p)
  7008. (tramp-handle-file-remote-p file))
  7009. ((and (boundp 'ange-ftp-name-format)
  7010. (string-match (car ange-ftp-name-format) file))
  7011. t)
  7012. (t nil)))
  7013. ;;;; Refiling
  7014. (defun org-get-org-file ()
  7015. "Read a filename, with default directory `org-directory'."
  7016. (let ((default (or org-default-notes-file remember-data-file)))
  7017. (read-file-name (format "File name [%s]: " default)
  7018. (file-name-as-directory org-directory)
  7019. default)))
  7020. (defun org-notes-order-reversed-p ()
  7021. "Check if the current file should receive notes in reversed order."
  7022. (cond
  7023. ((not org-reverse-note-order) nil)
  7024. ((eq t org-reverse-note-order) t)
  7025. ((not (listp org-reverse-note-order)) nil)
  7026. (t (catch 'exit
  7027. (let ((all org-reverse-note-order)
  7028. entry)
  7029. (while (setq entry (pop all))
  7030. (if (string-match (car entry) buffer-file-name)
  7031. (throw 'exit (cdr entry))))
  7032. nil)))))
  7033. (defvar org-refile-target-table nil
  7034. "The list of refile targets, created by `org-refile'.")
  7035. (defvar org-agenda-new-buffers nil
  7036. "Buffers created to visit agenda files.")
  7037. (defun org-get-refile-targets (&optional default-buffer)
  7038. "Produce a table with refile targets."
  7039. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7040. targets txt re files f desc descre)
  7041. (with-current-buffer (or default-buffer (current-buffer))
  7042. (while (setq entry (pop entries))
  7043. (setq files (car entry) desc (cdr entry))
  7044. (cond
  7045. ((null files) (setq files (list (current-buffer))))
  7046. ((eq files 'org-agenda-files)
  7047. (setq files (org-agenda-files 'unrestricted)))
  7048. ((and (symbolp files) (fboundp files))
  7049. (setq files (funcall files)))
  7050. ((and (symbolp files) (boundp files))
  7051. (setq files (symbol-value files))))
  7052. (if (stringp files) (setq files (list files)))
  7053. (cond
  7054. ((eq (car desc) :tag)
  7055. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7056. ((eq (car desc) :todo)
  7057. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7058. ((eq (car desc) :regexp)
  7059. (setq descre (cdr desc)))
  7060. ((eq (car desc) :level)
  7061. (setq descre (concat "^\\*\\{" (number-to-string
  7062. (if org-odd-levels-only
  7063. (1- (* 2 (cdr desc)))
  7064. (cdr desc)))
  7065. "\\}[ \t]")))
  7066. ((eq (car desc) :maxlevel)
  7067. (setq descre (concat "^\\*\\{1," (number-to-string
  7068. (if org-odd-levels-only
  7069. (1- (* 2 (cdr desc)))
  7070. (cdr desc)))
  7071. "\\}[ \t]")))
  7072. (t (error "Bad refiling target description %s" desc)))
  7073. (while (setq f (pop files))
  7074. (save-excursion
  7075. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7076. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7077. (save-excursion
  7078. (save-restriction
  7079. (widen)
  7080. (goto-char (point-min))
  7081. (while (re-search-forward descre nil t)
  7082. (goto-char (point-at-bol))
  7083. (when (looking-at org-complex-heading-regexp)
  7084. (setq txt (match-string 4)
  7085. re (concat "^" (regexp-quote
  7086. (buffer-substring (match-beginning 1)
  7087. (match-end 4)))))
  7088. (if (match-end 5) (setq re (concat re "[ \t]+"
  7089. (regexp-quote
  7090. (match-string 5)))))
  7091. (setq re (concat re "[ \t]*$"))
  7092. (when org-refile-use-outline-path
  7093. (setq txt (mapconcat 'identity
  7094. (append
  7095. (if (eq org-refile-use-outline-path 'file)
  7096. (list (file-name-nondirectory
  7097. (buffer-file-name (buffer-base-buffer))))
  7098. (if (eq org-refile-use-outline-path 'full-file-path)
  7099. (list (buffer-file-name (buffer-base-buffer)))))
  7100. (org-get-outline-path)
  7101. (list txt))
  7102. "/")))
  7103. (push (list txt f re (point)) targets))
  7104. (goto-char (point-at-eol))))))))
  7105. (nreverse targets))))
  7106. (defun org-get-outline-path ()
  7107. "Return the outline path to the current entry, as a list."
  7108. (let (rtn)
  7109. (save-excursion
  7110. (while (org-up-heading-safe)
  7111. (when (looking-at org-complex-heading-regexp)
  7112. (push (org-match-string-no-properties 4) rtn)))
  7113. rtn)))
  7114. (defvar org-refile-history nil
  7115. "History for refiling operations.")
  7116. (defun org-refile (&optional goto default-buffer)
  7117. "Move the entry at point to another heading.
  7118. The list of target headings is compiled using the information in
  7119. `org-refile-targets', which see. This list is created before each use
  7120. and will therefore always be up-to-date.
  7121. At the target location, the entry is filed as a subitem of the target heading.
  7122. Depending on `org-reverse-note-order', the new subitem will either be the
  7123. first of the last subitem.
  7124. With prefix arg GOTO, the command will only visit the target location,
  7125. not actually move anything.
  7126. With a double prefix `C-u C-u', go to the location where the last refiling
  7127. operation has put the subtree."
  7128. (interactive "P")
  7129. (let* ((cbuf (current-buffer))
  7130. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7131. pos it nbuf file re level reversed)
  7132. (if (equal goto '(16))
  7133. (org-refile-goto-last-stored)
  7134. (when (setq it (org-refile-get-location
  7135. (if goto "Goto: " "Refile to: ") default-buffer))
  7136. (setq file (nth 1 it)
  7137. re (nth 2 it)
  7138. pos (nth 3 it))
  7139. (setq nbuf (or (find-buffer-visiting file)
  7140. (find-file-noselect file)))
  7141. (if goto
  7142. (progn
  7143. (switch-to-buffer nbuf)
  7144. (goto-char pos)
  7145. (org-show-context 'org-goto))
  7146. (org-copy-subtree 1 nil t)
  7147. (save-excursion
  7148. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7149. (find-file-noselect file))))
  7150. (setq reversed (org-notes-order-reversed-p))
  7151. (save-excursion
  7152. (save-restriction
  7153. (widen)
  7154. (goto-char pos)
  7155. (looking-at outline-regexp)
  7156. (setq level (org-get-valid-level (funcall outline-level) 1))
  7157. (goto-char
  7158. (if reversed
  7159. (outline-next-heading)
  7160. (or (save-excursion (outline-get-next-sibling))
  7161. (org-end-of-subtree t t)
  7162. (point-max))))
  7163. (bookmark-set "org-refile-last-stored")
  7164. (org-paste-subtree level))))
  7165. (org-cut-subtree)
  7166. (setq org-markers-to-move nil)
  7167. (message "Entry refiled to \"%s\"" (car it)))))))
  7168. (defun org-refile-goto-last-stored ()
  7169. "Go to the location where the last refile was stored."
  7170. (interactive)
  7171. (bookmark-jump "org-refile-last-stored")
  7172. (message "This is the location of the last refile"))
  7173. (defun org-refile-get-location (&optional prompt default-buffer)
  7174. "Prompt the user for a refile location, using PROMPT."
  7175. (let ((org-refile-targets org-refile-targets)
  7176. (org-refile-use-outline-path org-refile-use-outline-path))
  7177. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7178. (unless org-refile-target-table
  7179. (error "No refile targets"))
  7180. (let* ((cbuf (current-buffer))
  7181. (cfunc (if org-refile-use-outline-path
  7182. 'org-olpath-completing-read
  7183. 'completing-read))
  7184. (extra (if org-refile-use-outline-path "/" ""))
  7185. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7186. (fname (and filename (file-truename filename)))
  7187. (tbl (mapcar
  7188. (lambda (x)
  7189. (if (not (equal fname (file-truename (nth 1 x))))
  7190. (cons (concat (car x) extra " ("
  7191. (file-name-nondirectory (nth 1 x)) ")")
  7192. (cdr x))
  7193. (cons (concat (car x) extra) (cdr x))))
  7194. org-refile-target-table))
  7195. (completion-ignore-case t))
  7196. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7197. tbl)))
  7198. (defun org-olpath-completing-read (prompt collection &rest args)
  7199. "Read an outline path like a file name."
  7200. (let ((thetable collection))
  7201. (apply
  7202. 'completing-read prompt
  7203. (lambda (string predicate &optional flag)
  7204. (let (rtn r s f (l (length string)))
  7205. (cond
  7206. ((eq flag nil)
  7207. ;; try completion
  7208. (try-completion string thetable))
  7209. ((eq flag t)
  7210. ;; all-completions
  7211. (setq rtn (all-completions string thetable predicate))
  7212. (mapcar
  7213. (lambda (x)
  7214. (setq r (substring x l))
  7215. (if (string-match " ([^)]*)$" x)
  7216. (setq f (match-string 0 x))
  7217. (setq f ""))
  7218. (if (string-match "/" r)
  7219. (concat string (substring r 0 (match-end 0)) f)
  7220. x))
  7221. rtn))
  7222. ((eq flag 'lambda)
  7223. ;; exact match?
  7224. (assoc string thetable)))
  7225. ))
  7226. args)))
  7227. ;;;; Dynamic blocks
  7228. (defun org-find-dblock (name)
  7229. "Find the first dynamic block with name NAME in the buffer.
  7230. If not found, stay at current position and return nil."
  7231. (let (pos)
  7232. (save-excursion
  7233. (goto-char (point-min))
  7234. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7235. nil t)
  7236. (match-beginning 0))))
  7237. (if pos (goto-char pos))
  7238. pos))
  7239. (defconst org-dblock-start-re
  7240. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7241. "Matches the startline of a dynamic block, with parameters.")
  7242. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7243. "Matches the end of a dyhamic block.")
  7244. (defun org-create-dblock (plist)
  7245. "Create a dynamic block section, with parameters taken from PLIST.
  7246. PLIST must containe a :name entry which is used as name of the block."
  7247. (unless (bolp) (newline))
  7248. (let ((name (plist-get plist :name)))
  7249. (insert "#+BEGIN: " name)
  7250. (while plist
  7251. (if (eq (car plist) :name)
  7252. (setq plist (cddr plist))
  7253. (insert " " (prin1-to-string (pop plist)))))
  7254. (insert "\n\n#+END:\n")
  7255. (beginning-of-line -2)))
  7256. (defun org-prepare-dblock ()
  7257. "Prepare dynamic block for refresh.
  7258. This empties the block, puts the cursor at the insert position and returns
  7259. the property list including an extra property :name with the block name."
  7260. (unless (looking-at org-dblock-start-re)
  7261. (error "Not at a dynamic block"))
  7262. (let* ((begdel (1+ (match-end 0)))
  7263. (name (org-no-properties (match-string 1)))
  7264. (params (append (list :name name)
  7265. (read (concat "(" (match-string 3) ")")))))
  7266. (unless (re-search-forward org-dblock-end-re nil t)
  7267. (error "Dynamic block not terminated"))
  7268. (setq params
  7269. (append params
  7270. (list :content (buffer-substring
  7271. begdel (match-beginning 0)))))
  7272. (delete-region begdel (match-beginning 0))
  7273. (goto-char begdel)
  7274. (open-line 1)
  7275. params))
  7276. (defun org-map-dblocks (&optional command)
  7277. "Apply COMMAND to all dynamic blocks in the current buffer.
  7278. If COMMAND is not given, use `org-update-dblock'."
  7279. (let ((cmd (or command 'org-update-dblock))
  7280. pos)
  7281. (save-excursion
  7282. (goto-char (point-min))
  7283. (while (re-search-forward org-dblock-start-re nil t)
  7284. (goto-char (setq pos (match-beginning 0)))
  7285. (condition-case nil
  7286. (funcall cmd)
  7287. (error (message "Error during update of dynamic block")))
  7288. (goto-char pos)
  7289. (unless (re-search-forward org-dblock-end-re nil t)
  7290. (error "Dynamic block not terminated"))))))
  7291. (defun org-dblock-update (&optional arg)
  7292. "User command for updating dynamic blocks.
  7293. Update the dynamic block at point. With prefix ARG, update all dynamic
  7294. blocks in the buffer."
  7295. (interactive "P")
  7296. (if arg
  7297. (org-update-all-dblocks)
  7298. (or (looking-at org-dblock-start-re)
  7299. (org-beginning-of-dblock))
  7300. (org-update-dblock)))
  7301. (defun org-update-dblock ()
  7302. "Update the dynamic block at point
  7303. This means to empty the block, parse for parameters and then call
  7304. the correct writing function."
  7305. (save-window-excursion
  7306. (let* ((pos (point))
  7307. (line (org-current-line))
  7308. (params (org-prepare-dblock))
  7309. (name (plist-get params :name))
  7310. (cmd (intern (concat "org-dblock-write:" name))))
  7311. (message "Updating dynamic block `%s' at line %d..." name line)
  7312. (funcall cmd params)
  7313. (message "Updating dynamic block `%s' at line %d...done" name line)
  7314. (goto-char pos))))
  7315. (defun org-beginning-of-dblock ()
  7316. "Find the beginning of the dynamic block at point.
  7317. Error if there is no scuh block at point."
  7318. (let ((pos (point))
  7319. beg)
  7320. (end-of-line 1)
  7321. (if (and (re-search-backward org-dblock-start-re nil t)
  7322. (setq beg (match-beginning 0))
  7323. (re-search-forward org-dblock-end-re nil t)
  7324. (> (match-end 0) pos))
  7325. (goto-char beg)
  7326. (goto-char pos)
  7327. (error "Not in a dynamic block"))))
  7328. (defun org-update-all-dblocks ()
  7329. "Update all dynamic blocks in the buffer.
  7330. This function can be used in a hook."
  7331. (when (org-mode-p)
  7332. (org-map-dblocks 'org-update-dblock)))
  7333. ;;;; Completion
  7334. (defconst org-additional-option-like-keywords
  7335. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7336. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7337. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7338. "BEGIN_QUOTE" "END_QUOTE"
  7339. "BEGIN_VERSE" "END_VERSE"
  7340. "BEGIN_SRC" "END_SRC"))
  7341. (defcustom org-structure-template-alist
  7342. '(
  7343. ("s" "#+begin_src ?\n\n#+end_src"
  7344. "<src lang=\"?\">\n\n</src>")
  7345. ("e" "#+begin_example\n?\n#+end_example"
  7346. "<example>\n?\n</example>")
  7347. ("q" "#+begin_quote\n?\n#+end_quote"
  7348. "<quote>\n?\n</quote>")
  7349. ("v" "#+begin_verse\n?\n#+end_verse"
  7350. "<verse>\n?\n/verse>")
  7351. ("l" "#+begin_latex\n?\n#+end_latex"
  7352. "<literal style=\"latex\">\n?\n</literal>")
  7353. ("L" "#+latex: "
  7354. "<literal style=\"latex\">?</literal>")
  7355. ("h" "#+begin_html\n?\n#+end_html"
  7356. "<literal style=\"html\">\n?\n</literal>")
  7357. ("H" "#+html: "
  7358. "<literal style=\"html\">?</literal>")
  7359. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7360. ("A" "#+ascii: ")
  7361. ("i" "#+include %file ?"
  7362. "<include file=%file markup=\"?\">")
  7363. )
  7364. "Structure completion elements.
  7365. This is a list of abbreviation keys and values. The value gets inserted
  7366. it you type @samp{.} followed by the key and then the completion key,
  7367. usually `M-TAB'. %file will be replaced by a file name after prompting
  7368. for the file uning completion.
  7369. There are two templates for each key, the first uses the original Org syntax,
  7370. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7371. the default when the /org-mtags.el/ module has been loaded. See also the
  7372. variable `org-mtags-prefere-muse-templates'.
  7373. This is an experimental feature, it is undecided if it is going to stay in."
  7374. :group 'org-completion
  7375. :type '(repeat
  7376. (string :tag "Key")
  7377. (string :tag "Template")
  7378. (string :tag "Muse Template")))
  7379. (defun org-try-structure-completion ()
  7380. "Try to complete a structure template before point.
  7381. This looks for strings like \"<e\" on an otherwise empty line and
  7382. expands them."
  7383. (let ((l (buffer-substring (point-at-bol) (point)))
  7384. a)
  7385. (when (and (looking-at "[ \t]*$")
  7386. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7387. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7388. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7389. (match-beginning 1)) a)
  7390. t)))
  7391. (defun org-complete-expand-structure-template (start cell)
  7392. "Expand a structure template."
  7393. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7394. (rpl (nth (if musep 2 1) cell)))
  7395. (delete-region start (point))
  7396. (when (string-match "\\`#\\+" rpl)
  7397. (cond
  7398. ((bolp))
  7399. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7400. (delete-region (point-at-bol) (point)))
  7401. (t (newline))))
  7402. (setq start (point))
  7403. (if (string-match "%file" rpl)
  7404. (setq rpl (replace-match
  7405. (concat
  7406. "\""
  7407. (save-match-data
  7408. (abbreviate-file-name (read-file-name "Include file: ")))
  7409. "\"")
  7410. t t rpl)))
  7411. (insert rpl)
  7412. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7413. (defun org-complete (&optional arg)
  7414. "Perform completion on word at point.
  7415. At the beginning of a headline, this completes TODO keywords as given in
  7416. `org-todo-keywords'.
  7417. If the current word is preceded by a backslash, completes the TeX symbols
  7418. that are supported for HTML support.
  7419. If the current word is preceded by \"#+\", completes special words for
  7420. setting file options.
  7421. In the line after \"#+STARTUP:, complete valid keywords.\"
  7422. At all other locations, this simply calls the value of
  7423. `org-completion-fallback-command'."
  7424. (interactive "P")
  7425. (org-without-partial-completion
  7426. (catch 'exit
  7427. (let* ((a nil)
  7428. (end (point))
  7429. (beg1 (save-excursion
  7430. (skip-chars-backward (org-re "[:alnum:]_@"))
  7431. (point)))
  7432. (beg (save-excursion
  7433. (skip-chars-backward "a-zA-Z0-9_:$")
  7434. (point)))
  7435. (confirm (lambda (x) (stringp (car x))))
  7436. (searchhead (equal (char-before beg) ?*))
  7437. (struct
  7438. (when (and (member (char-before beg1) '(?. ?<))
  7439. (setq a (assoc (buffer-substring beg1 (point))
  7440. org-structure-template-alist)))
  7441. (org-complete-expand-structure-template (1- beg1) a)
  7442. (throw 'exit t)))
  7443. (tag (and (equal (char-before beg1) ?:)
  7444. (equal (char-after (point-at-bol)) ?*)))
  7445. (prop (and (equal (char-before beg1) ?:)
  7446. (not (equal (char-after (point-at-bol)) ?*))))
  7447. (texp (equal (char-before beg) ?\\))
  7448. (link (equal (char-before beg) ?\[))
  7449. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7450. beg)
  7451. "#+"))
  7452. (startup (string-match "^#\\+STARTUP:.*"
  7453. (buffer-substring (point-at-bol) (point))))
  7454. (completion-ignore-case opt)
  7455. (type nil)
  7456. (tbl nil)
  7457. (table (cond
  7458. (opt
  7459. (setq type :opt)
  7460. (require 'org-exp)
  7461. (append
  7462. (mapcar
  7463. (lambda (x)
  7464. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7465. (cons (match-string 2 x) (match-string 1 x)))
  7466. (org-split-string (org-get-current-options) "\n"))
  7467. (mapcar 'list org-additional-option-like-keywords)))
  7468. (startup
  7469. (setq type :startup)
  7470. org-startup-options)
  7471. (link (append org-link-abbrev-alist-local
  7472. org-link-abbrev-alist))
  7473. (texp
  7474. (setq type :tex)
  7475. org-html-entities)
  7476. ((string-match "\\`\\*+[ \t]+\\'"
  7477. (buffer-substring (point-at-bol) beg))
  7478. (setq type :todo)
  7479. (mapcar 'list org-todo-keywords-1))
  7480. (searchhead
  7481. (setq type :searchhead)
  7482. (save-excursion
  7483. (goto-char (point-min))
  7484. (while (re-search-forward org-todo-line-regexp nil t)
  7485. (push (list
  7486. (org-make-org-heading-search-string
  7487. (match-string 3) t))
  7488. tbl)))
  7489. tbl)
  7490. (tag (setq type :tag beg beg1)
  7491. (or org-tag-alist (org-get-buffer-tags)))
  7492. (prop (setq type :prop beg beg1)
  7493. (mapcar 'list (org-buffer-property-keys nil t t)))
  7494. (t (progn
  7495. (call-interactively org-completion-fallback-command)
  7496. (throw 'exit nil)))))
  7497. (pattern (buffer-substring-no-properties beg end))
  7498. (completion (try-completion pattern table confirm)))
  7499. (cond ((eq completion t)
  7500. (if (not (assoc (upcase pattern) table))
  7501. (message "Already complete")
  7502. (if (and (equal type :opt)
  7503. (not (member (car (assoc (upcase pattern) table))
  7504. org-additional-option-like-keywords)))
  7505. (insert (substring (cdr (assoc (upcase pattern) table))
  7506. (length pattern)))
  7507. (if (memq type '(:tag :prop)) (insert ":")))))
  7508. ((null completion)
  7509. (message "Can't find completion for \"%s\"" pattern)
  7510. (ding))
  7511. ((not (string= pattern completion))
  7512. (delete-region beg end)
  7513. (if (string-match " +$" completion)
  7514. (setq completion (replace-match "" t t completion)))
  7515. (insert completion)
  7516. (if (get-buffer-window "*Completions*")
  7517. (delete-window (get-buffer-window "*Completions*")))
  7518. (if (assoc completion table)
  7519. (if (eq type :todo) (insert " ")
  7520. (if (memq type '(:tag :prop)) (insert ":"))))
  7521. (if (and (equal type :opt) (assoc completion table))
  7522. (message "%s" (substitute-command-keys
  7523. "Press \\[org-complete] again to insert example settings"))))
  7524. (t
  7525. (message "Making completion list...")
  7526. (let ((list (sort (all-completions pattern table confirm)
  7527. 'string<)))
  7528. (with-output-to-temp-buffer "*Completions*"
  7529. (condition-case nil
  7530. ;; Protection needed for XEmacs and emacs 21
  7531. (display-completion-list list pattern)
  7532. (error (display-completion-list list)))))
  7533. (message "Making completion list...%s" "done")))))))
  7534. ;;;; TODO, DEADLINE, Comments
  7535. (defun org-toggle-comment ()
  7536. "Change the COMMENT state of an entry."
  7537. (interactive)
  7538. (save-excursion
  7539. (org-back-to-heading)
  7540. (let (case-fold-search)
  7541. (if (looking-at (concat outline-regexp
  7542. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7543. (replace-match "" t t nil 1)
  7544. (if (looking-at outline-regexp)
  7545. (progn
  7546. (goto-char (match-end 0))
  7547. (insert org-comment-string " ")))))))
  7548. (defvar org-last-todo-state-is-todo nil
  7549. "This is non-nil when the last TODO state change led to a TODO state.
  7550. If the last change removed the TODO tag or switched to DONE, then
  7551. this is nil.")
  7552. (defvar org-setting-tags nil) ; dynamically skiped
  7553. (defun org-parse-local-options (string var)
  7554. "Parse STRING for startup setting relevant for variable VAR."
  7555. (let ((rtn (symbol-value var))
  7556. e opts)
  7557. (save-match-data
  7558. (if (or (not string) (not (string-match "\\S-" string)))
  7559. rtn
  7560. (setq opts (delq nil (mapcar (lambda (x)
  7561. (setq e (assoc x org-startup-options))
  7562. (if (eq (nth 1 e) var) e nil))
  7563. (org-split-string string "[ \t]+"))))
  7564. (if (not opts)
  7565. rtn
  7566. (setq rtn nil)
  7567. (while (setq e (pop opts))
  7568. (if (not (nth 3 e))
  7569. (setq rtn (nth 2 e))
  7570. (if (not (listp rtn)) (setq rtn nil))
  7571. (push (nth 2 e) rtn)))
  7572. rtn)))))
  7573. (defvar org-blocker-hook nil
  7574. "Hook for functions that are allowed to block a state change.
  7575. Each function gets as its single argument a property list, see
  7576. `org-trigger-hook' for more information about this list.
  7577. If any of the functions in this hook returns nil, the state change
  7578. is blocked.")
  7579. (defvar org-trigger-hook nil
  7580. "Hook for functions that are triggered by a state change.
  7581. Each function gets as its single argument a property list with at least
  7582. the following elements:
  7583. (:type type-of-change :position pos-at-entry-start
  7584. :from old-state :to new-state)
  7585. Depending on the type, more properties may be present.
  7586. This mechanism is currently implemented for:
  7587. TODO state changes
  7588. ------------------
  7589. :type todo-state-change
  7590. :from previous state (keyword as a string), or nil
  7591. :to new state (keyword as a string), or nil")
  7592. (defun org-todo (&optional arg)
  7593. "Change the TODO state of an item.
  7594. The state of an item is given by a keyword at the start of the heading,
  7595. like
  7596. *** TODO Write paper
  7597. *** DONE Call mom
  7598. The different keywords are specified in the variable `org-todo-keywords'.
  7599. By default the available states are \"TODO\" and \"DONE\".
  7600. So for this example: when the item starts with TODO, it is changed to DONE.
  7601. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7602. DONE are present, add TODO at the beginning of the heading.
  7603. With C-u prefix arg, use completion to determine the new state.
  7604. With numeric prefix arg, switch to that state.
  7605. For calling through lisp, arg is also interpreted in the following way:
  7606. 'none -> empty state
  7607. \"\"(empty string) -> switch to empty state
  7608. 'done -> switch to DONE
  7609. 'nextset -> switch to the next set of keywords
  7610. 'previousset -> switch to the previous set of keywords
  7611. \"WAITING\" -> switch to the specified keyword, but only if it
  7612. really is a member of `org-todo-keywords'."
  7613. (interactive "P")
  7614. (save-excursion
  7615. (catch 'exit
  7616. (org-back-to-heading)
  7617. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7618. (or (looking-at (concat " +" org-todo-regexp " *"))
  7619. (looking-at " *"))
  7620. (let* ((match-data (match-data))
  7621. (startpos (point-at-bol))
  7622. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7623. (org-log-done org-log-done)
  7624. (org-log-repeat org-log-repeat)
  7625. (org-todo-log-states org-todo-log-states)
  7626. (this (match-string 1))
  7627. (hl-pos (match-beginning 0))
  7628. (head (org-get-todo-sequence-head this))
  7629. (ass (assoc head org-todo-kwd-alist))
  7630. (interpret (nth 1 ass))
  7631. (done-word (nth 3 ass))
  7632. (final-done-word (nth 4 ass))
  7633. (last-state (or this ""))
  7634. (completion-ignore-case t)
  7635. (member (member this org-todo-keywords-1))
  7636. (tail (cdr member))
  7637. (state (cond
  7638. ((and org-todo-key-trigger
  7639. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7640. (and (not arg) org-use-fast-todo-selection
  7641. (not (eq org-use-fast-todo-selection 'prefix)))))
  7642. ;; Use fast selection
  7643. (org-fast-todo-selection))
  7644. ((and (equal arg '(4))
  7645. (or (not org-use-fast-todo-selection)
  7646. (not org-todo-key-trigger)))
  7647. ;; Read a state with completion
  7648. (completing-read "State: " (mapcar (lambda(x) (list x))
  7649. org-todo-keywords-1)
  7650. nil t))
  7651. ((eq arg 'right)
  7652. (if this
  7653. (if tail (car tail) nil)
  7654. (car org-todo-keywords-1)))
  7655. ((eq arg 'left)
  7656. (if (equal member org-todo-keywords-1)
  7657. nil
  7658. (if this
  7659. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7660. org-todo-keywords-1)
  7661. (org-last org-todo-keywords-1))))
  7662. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7663. (setq arg nil))) ; hack to fall back to cycling
  7664. (arg
  7665. ;; user or caller requests a specific state
  7666. (cond
  7667. ((equal arg "") nil)
  7668. ((eq arg 'none) nil)
  7669. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7670. ((eq arg 'nextset)
  7671. (or (car (cdr (member head org-todo-heads)))
  7672. (car org-todo-heads)))
  7673. ((eq arg 'previousset)
  7674. (let ((org-todo-heads (reverse org-todo-heads)))
  7675. (or (car (cdr (member head org-todo-heads)))
  7676. (car org-todo-heads))))
  7677. ((car (member arg org-todo-keywords-1)))
  7678. ((nth (1- (prefix-numeric-value arg))
  7679. org-todo-keywords-1))))
  7680. ((null member) (or head (car org-todo-keywords-1)))
  7681. ((equal this final-done-word) nil) ;; -> make empty
  7682. ((null tail) nil) ;; -> first entry
  7683. ((eq interpret 'sequence)
  7684. (car tail))
  7685. ((memq interpret '(type priority))
  7686. (if (eq this-command last-command)
  7687. (car tail)
  7688. (if (> (length tail) 0)
  7689. (or done-word (car org-done-keywords))
  7690. nil)))
  7691. (t nil)))
  7692. (next (if state (concat " " state " ") " "))
  7693. (change-plist (list :type 'todo-state-change :from this :to state
  7694. :position startpos))
  7695. dolog now-done-p)
  7696. (when org-blocker-hook
  7697. (unless (save-excursion
  7698. (save-match-data
  7699. (run-hook-with-args-until-failure
  7700. 'org-blocker-hook change-plist)))
  7701. (if (interactive-p)
  7702. (error "TODO state change from %s to %s blocked" this state)
  7703. ;; fail silently
  7704. (message "TODO state change from %s to %s blocked" this state)
  7705. (throw 'exit nil))))
  7706. (store-match-data match-data)
  7707. (replace-match next t t)
  7708. (unless (pos-visible-in-window-p hl-pos)
  7709. (message "TODO state changed to %s" (org-trim next)))
  7710. (unless head
  7711. (setq head (org-get-todo-sequence-head state)
  7712. ass (assoc head org-todo-kwd-alist)
  7713. interpret (nth 1 ass)
  7714. done-word (nth 3 ass)
  7715. final-done-word (nth 4 ass)))
  7716. (when (memq arg '(nextset previousset))
  7717. (message "Keyword-Set %d/%d: %s"
  7718. (- (length org-todo-sets) -1
  7719. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7720. (length org-todo-sets)
  7721. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7722. (setq org-last-todo-state-is-todo
  7723. (not (member state org-done-keywords)))
  7724. (setq now-done-p (and (member state org-done-keywords)
  7725. (not (member this org-done-keywords))))
  7726. (and logging (org-local-logging logging))
  7727. (when (and (or org-todo-log-states org-log-done)
  7728. (not (memq arg '(nextset previousset))))
  7729. ;; we need to look at recording a time and note
  7730. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7731. (nth 2 (assoc this org-todo-log-states))))
  7732. (when (and state
  7733. (member state org-not-done-keywords)
  7734. (not (member this org-not-done-keywords)))
  7735. ;; This is now a todo state and was not one before
  7736. ;; If there was a CLOSED time stamp, get rid of it.
  7737. (org-add-planning-info nil nil 'closed))
  7738. (when (and now-done-p org-log-done)
  7739. ;; It is now done, and it was not done before
  7740. (org-add-planning-info 'closed (org-current-time))
  7741. (if (and (not dolog) (eq 'note org-log-done))
  7742. (org-add-log-setup 'done state 'findpos 'note)))
  7743. (when (and state dolog)
  7744. ;; This is a non-nil state, and we need to log it
  7745. (org-add-log-setup 'state state 'findpos dolog)))
  7746. ;; Fixup tag positioning
  7747. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7748. (when org-provide-todo-statistics
  7749. (org-update-parent-todo-statistics))
  7750. (run-hooks 'org-after-todo-state-change-hook)
  7751. (if (and arg (not (member state org-done-keywords)))
  7752. (setq head (org-get-todo-sequence-head state)))
  7753. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7754. ;; Do we need to trigger a repeat?
  7755. (when now-done-p (org-auto-repeat-maybe state))
  7756. ;; Fixup cursor location if close to the keyword
  7757. (if (and (outline-on-heading-p)
  7758. (not (bolp))
  7759. (save-excursion (beginning-of-line 1)
  7760. (looking-at org-todo-line-regexp))
  7761. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7762. (progn
  7763. (goto-char (or (match-end 2) (match-end 1)))
  7764. (just-one-space)))
  7765. (when org-trigger-hook
  7766. (save-excursion
  7767. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7768. (defun org-update-parent-todo-statistics ()
  7769. "Update any statistics cookie in the parent of the current headline."
  7770. (interactive)
  7771. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7772. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7773. (catch 'exit
  7774. (save-excursion
  7775. (setq level (org-up-heading-safe))
  7776. (unless (and level
  7777. (re-search-forward box-re (point-at-eol) t))
  7778. (throw 'exit nil))
  7779. (setq is-percent (match-end 2))
  7780. (save-match-data
  7781. (unless (outline-next-heading) (throw 'exit nil))
  7782. (while (looking-at org-todo-line-regexp)
  7783. (setq kwd (match-string 2))
  7784. (and kwd (setq cnt-all (1+ cnt-all)))
  7785. (and (member kwd org-done-keywords)
  7786. (setq cnt-done (1+ cnt-done)))
  7787. (condition-case nil
  7788. (outline-forward-same-level 1)
  7789. (error (end-of-line 1)))))
  7790. (replace-match
  7791. (if is-percent
  7792. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7793. (format "[%d/%d]" cnt-done cnt-all)))
  7794. (run-hook-with-args 'org-after-todo-statistics-hook
  7795. cnt-done (- cnt-all cnt-done))))))
  7796. (defvar org-after-todo-statistics-hook nil
  7797. "Hook that is called after a TODO statistics cookie has been updated.
  7798. Each function is called with two arguments: the number of not-done entries
  7799. and the number of done entries.
  7800. For example, the following function, when added to this hook, will switch
  7801. an entry to DONE when all children are done, and back to TODO when new
  7802. entries are set to a TODO status. Note that this hook is only called
  7803. when there is a statistics cookie in the headline!
  7804. (defun org-summary-todo (n-done n-not-done)
  7805. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7806. (let (org-log-done org-log-states) ; turn off logging
  7807. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7808. ")
  7809. (defun org-local-logging (value)
  7810. "Get logging settings from a property VALUE."
  7811. (let* (words w a)
  7812. ;; directly set the variables, they are already local.
  7813. (setq org-log-done nil
  7814. org-log-repeat nil
  7815. org-todo-log-states nil)
  7816. (setq words (org-split-string value))
  7817. (while (setq w (pop words))
  7818. (cond
  7819. ((setq a (assoc w org-startup-options))
  7820. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7821. (set (nth 1 a) (nth 2 a))))
  7822. ((setq a (org-extract-log-state-settings w))
  7823. (and (member (car a) org-todo-keywords-1)
  7824. (push a org-todo-log-states)))))))
  7825. (defun org-get-todo-sequence-head (kwd)
  7826. "Return the head of the TODO sequence to which KWD belongs.
  7827. If KWD is not set, check if there is a text property remembering the
  7828. right sequence."
  7829. (let (p)
  7830. (cond
  7831. ((not kwd)
  7832. (or (get-text-property (point-at-bol) 'org-todo-head)
  7833. (progn
  7834. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7835. nil (point-at-eol)))
  7836. (get-text-property p 'org-todo-head))))
  7837. ((not (member kwd org-todo-keywords-1))
  7838. (car org-todo-keywords-1))
  7839. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7840. (defun org-fast-todo-selection ()
  7841. "Fast TODO keyword selection with single keys.
  7842. Returns the new TODO keyword, or nil if no state change should occur."
  7843. (let* ((fulltable org-todo-key-alist)
  7844. (done-keywords org-done-keywords) ;; needed for the faces.
  7845. (maxlen (apply 'max (mapcar
  7846. (lambda (x)
  7847. (if (stringp (car x)) (string-width (car x)) 0))
  7848. fulltable)))
  7849. (expert nil)
  7850. (fwidth (+ maxlen 3 1 3))
  7851. (ncol (/ (- (window-width) 4) fwidth))
  7852. tg cnt e c tbl
  7853. groups ingroup)
  7854. (save-window-excursion
  7855. (if expert
  7856. (set-buffer (get-buffer-create " *Org todo*"))
  7857. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7858. (erase-buffer)
  7859. (org-set-local 'org-done-keywords done-keywords)
  7860. (setq tbl fulltable cnt 0)
  7861. (while (setq e (pop tbl))
  7862. (cond
  7863. ((equal e '(:startgroup))
  7864. (push '() groups) (setq ingroup t)
  7865. (when (not (= cnt 0))
  7866. (setq cnt 0)
  7867. (insert "\n"))
  7868. (insert "{ "))
  7869. ((equal e '(:endgroup))
  7870. (setq ingroup nil cnt 0)
  7871. (insert "}\n"))
  7872. (t
  7873. (setq tg (car e) c (cdr e))
  7874. (if ingroup (push tg (car groups)))
  7875. (setq tg (org-add-props tg nil 'face
  7876. (org-get-todo-face tg)))
  7877. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7878. (insert "[" c "] " tg (make-string
  7879. (- fwidth 4 (length tg)) ?\ ))
  7880. (when (= (setq cnt (1+ cnt)) ncol)
  7881. (insert "\n")
  7882. (if ingroup (insert " "))
  7883. (setq cnt 0)))))
  7884. (insert "\n")
  7885. (goto-char (point-min))
  7886. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7887. (fit-window-to-buffer))
  7888. (message "[a-z..]:Set [SPC]:clear")
  7889. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7890. (cond
  7891. ((or (= c ?\C-g)
  7892. (and (= c ?q) (not (rassoc c fulltable))))
  7893. (setq quit-flag t))
  7894. ((= c ?\ ) nil)
  7895. ((setq e (rassoc c fulltable) tg (car e))
  7896. tg)
  7897. (t (setq quit-flag t))))))
  7898. (defun org-entry-is-todo-p ()
  7899. (member (org-get-todo-state) org-not-done-keywords))
  7900. (defun org-entry-is-done-p ()
  7901. (member (org-get-todo-state) org-done-keywords))
  7902. (defun org-get-todo-state ()
  7903. (save-excursion
  7904. (org-back-to-heading t)
  7905. (and (looking-at org-todo-line-regexp)
  7906. (match-end 2)
  7907. (match-string 2))))
  7908. (defun org-at-date-range-p (&optional inactive-ok)
  7909. "Is the cursor inside a date range?"
  7910. (interactive)
  7911. (save-excursion
  7912. (catch 'exit
  7913. (let ((pos (point)))
  7914. (skip-chars-backward "^[<\r\n")
  7915. (skip-chars-backward "<[")
  7916. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7917. (>= (match-end 0) pos)
  7918. (throw 'exit t))
  7919. (skip-chars-backward "^<[\r\n")
  7920. (skip-chars-backward "<[")
  7921. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7922. (>= (match-end 0) pos)
  7923. (throw 'exit t)))
  7924. nil)))
  7925. (defun org-get-repeat ()
  7926. "Check if there is a deadline/schedule with repeater in this entry."
  7927. (save-match-data
  7928. (save-excursion
  7929. (org-back-to-heading t)
  7930. (if (re-search-forward
  7931. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7932. (match-string 1)))))
  7933. (defvar org-last-changed-timestamp)
  7934. (defvar org-last-inserted-timestamp)
  7935. (defvar org-log-post-message)
  7936. (defvar org-log-note-purpose)
  7937. (defvar org-log-note-how)
  7938. (defun org-auto-repeat-maybe (done-word)
  7939. "Check if the current headline contains a repeated deadline/schedule.
  7940. If yes, set TODO state back to what it was and change the base date
  7941. of repeating deadline/scheduled time stamps to new date.
  7942. This function is run automatically after each state change to a DONE state."
  7943. ;; last-state is dynamically scoped into this function
  7944. (let* ((repeat (org-get-repeat))
  7945. (aa (assoc last-state org-todo-kwd-alist))
  7946. (interpret (nth 1 aa))
  7947. (head (nth 2 aa))
  7948. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7949. (msg "Entry repeats: ")
  7950. (org-log-done nil)
  7951. (org-todo-log-states nil)
  7952. (nshiftmax 10) (nshift 0)
  7953. re type n what ts mb0 time)
  7954. (when repeat
  7955. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7956. (org-todo (if (eq interpret 'type) last-state head))
  7957. (when org-log-repeat
  7958. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7959. (memq 'org-add-log-note post-command-hook))
  7960. ;; OK, we are already setup for some record
  7961. (if (eq org-log-repeat 'note)
  7962. ;; make sure we take a note, not only a time stamp
  7963. (setq org-log-note-how 'note))
  7964. ;; Set up for taking a record
  7965. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7966. 'findpos org-log-repeat)))
  7967. (org-back-to-heading t)
  7968. (org-add-planning-info nil nil 'closed)
  7969. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7970. org-deadline-time-regexp "\\)\\|\\("
  7971. org-ts-regexp "\\)"))
  7972. (while (re-search-forward
  7973. re (save-excursion (outline-next-heading) (point)) t)
  7974. (setq type (if (match-end 1) org-scheduled-string
  7975. (if (match-end 3) org-deadline-string "Plain:"))
  7976. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7977. mb0 (match-beginning 0))
  7978. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7979. (setq n (string-to-number (match-string 2 ts))
  7980. what (match-string 3 ts))
  7981. (if (equal what "w") (setq n (* n 7) what "d"))
  7982. ;; Preparation, see if we need to modify the start date for the change
  7983. (when (match-end 1)
  7984. (setq time (save-match-data (org-time-string-to-time ts)))
  7985. (cond
  7986. ((equal (match-string 1 ts) ".")
  7987. ;; Shift starting date to today
  7988. (org-timestamp-change
  7989. (- (time-to-days (current-time)) (time-to-days time))
  7990. 'day))
  7991. ((equal (match-string 1 ts) "+")
  7992. (while (or (= nshift 0)
  7993. (<= (time-to-days time) (time-to-days (current-time))))
  7994. (when (= (incf nshift) nshiftmax)
  7995. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7996. (error "Abort")))
  7997. (org-timestamp-change n (cdr (assoc what whata)))
  7998. (org-at-timestamp-p t)
  7999. (setq ts (match-string 1))
  8000. (setq time (save-match-data (org-time-string-to-time ts))))
  8001. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8002. ;; rematch, so that we have everything in place for the real shift
  8003. (org-at-timestamp-p t)
  8004. (setq ts (match-string 1))
  8005. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8006. (org-timestamp-change n (cdr (assoc what whata)))
  8007. (setq msg (concat msg type org-last-changed-timestamp " "))))
  8008. (setq org-log-post-message msg)
  8009. (message "%s" msg))))
  8010. (defun org-show-todo-tree (arg)
  8011. "Make a compact tree which shows all headlines marked with TODO.
  8012. The tree will show the lines where the regexp matches, and all higher
  8013. headlines above the match.
  8014. With a \\[universal-argument] prefix, also show the DONE entries.
  8015. With a numeric prefix N, construct a sparse tree for the Nth element
  8016. of `org-todo-keywords-1'."
  8017. (interactive "P")
  8018. (let ((case-fold-search nil)
  8019. (kwd-re
  8020. (cond ((null arg) org-not-done-regexp)
  8021. ((equal arg '(4))
  8022. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  8023. (mapcar 'list org-todo-keywords-1))))
  8024. (concat "\\("
  8025. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8026. "\\)\\>")))
  8027. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8028. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8029. org-todo-keywords-1)))
  8030. (t (error "Invalid prefix argument: %s" arg)))))
  8031. (message "%d TODO entries found"
  8032. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8033. (defun org-deadline (&optional remove time)
  8034. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8035. With argument REMOVE, remove any deadline from the item.
  8036. When TIME is set, it should be an internal time specification, and the
  8037. scheduling will use the corresponding date."
  8038. (interactive "P")
  8039. (if remove
  8040. (progn
  8041. (org-remove-timestamp-with-keyword org-deadline-string)
  8042. (message "Item no longer has a deadline."))
  8043. (if (org-get-repeat)
  8044. (error "Cannot change deadline on task with repeater, please do that by hand")
  8045. (org-add-planning-info 'deadline time 'closed)
  8046. (message "Deadline on %s" org-last-inserted-timestamp))))
  8047. (defun org-schedule (&optional remove time)
  8048. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8049. With argument REMOVE, remove any scheduling date from the item.
  8050. When TIME is set, it should be an internal time specification, and the
  8051. scheduling will use the corresponding date."
  8052. (interactive "P")
  8053. (if remove
  8054. (progn
  8055. (org-remove-timestamp-with-keyword org-scheduled-string)
  8056. (message "Item is no longer scheduled."))
  8057. (if (org-get-repeat)
  8058. (error "Cannot reschedule task with repeater, please do that by hand")
  8059. (org-add-planning-info 'scheduled time 'closed)
  8060. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8061. (defun org-remove-timestamp-with-keyword (keyword)
  8062. "Remove all time stamps with KEYWORD in the current entry."
  8063. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8064. beg)
  8065. (save-excursion
  8066. (org-back-to-heading t)
  8067. (setq beg (point))
  8068. (org-end-of-subtree t t)
  8069. (while (re-search-backward re beg t)
  8070. (replace-match "")
  8071. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8072. (equal (char-before) ?\ ))
  8073. (backward-delete-char 1)
  8074. (if (string-match "^[ \t]*$" (buffer-substring
  8075. (point-at-bol) (point-at-eol)))
  8076. (delete-region (point-at-bol)
  8077. (min (point-max) (1+ (point-at-eol))))))))))
  8078. (defun org-add-planning-info (what &optional time &rest remove)
  8079. "Insert new timestamp with keyword in the line directly after the headline.
  8080. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8081. If non is given, the user is prompted for a date.
  8082. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8083. be removed."
  8084. (interactive)
  8085. (let (org-time-was-given org-end-time-was-given ts
  8086. end default-time default-input)
  8087. (when (and (not time) (memq what '(scheduled deadline)))
  8088. ;; Try to get a default date/time from existing timestamp
  8089. (save-excursion
  8090. (org-back-to-heading t)
  8091. (setq end (save-excursion (outline-next-heading) (point)))
  8092. (when (re-search-forward (if (eq what 'scheduled)
  8093. org-scheduled-time-regexp
  8094. org-deadline-time-regexp)
  8095. end t)
  8096. (setq ts (match-string 1)
  8097. default-time
  8098. (apply 'encode-time (org-parse-time-string ts))
  8099. default-input (and ts (org-get-compact-tod ts))))))
  8100. (when what
  8101. ;; If necessary, get the time from the user
  8102. (setq time (or time (org-read-date nil 'to-time nil nil
  8103. default-time default-input))))
  8104. (when (and org-insert-labeled-timestamps-at-point
  8105. (member what '(scheduled deadline)))
  8106. (insert
  8107. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8108. (org-insert-time-stamp time org-time-was-given
  8109. nil nil nil (list org-end-time-was-given))
  8110. (setq what nil))
  8111. (save-excursion
  8112. (save-restriction
  8113. (let (col list elt ts buffer-invisibility-spec)
  8114. (org-back-to-heading t)
  8115. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8116. (goto-char (match-end 1))
  8117. (setq col (current-column))
  8118. (goto-char (match-end 0))
  8119. (if (eobp) (insert "\n") (forward-char 1))
  8120. (if (and (not (looking-at outline-regexp))
  8121. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8122. "[^\r\n]*"))
  8123. (not (equal (match-string 1) org-clock-string)))
  8124. (narrow-to-region (match-beginning 0) (match-end 0))
  8125. (insert-before-markers "\n")
  8126. (backward-char 1)
  8127. (narrow-to-region (point) (point))
  8128. (and org-adapt-indentation (org-indent-to-column col)))
  8129. ;; Check if we have to remove something.
  8130. (setq list (cons what remove))
  8131. (while list
  8132. (setq elt (pop list))
  8133. (goto-char (point-min))
  8134. (when (or (and (eq elt 'scheduled)
  8135. (re-search-forward org-scheduled-time-regexp nil t))
  8136. (and (eq elt 'deadline)
  8137. (re-search-forward org-deadline-time-regexp nil t))
  8138. (and (eq elt 'closed)
  8139. (re-search-forward org-closed-time-regexp nil t)))
  8140. (replace-match "")
  8141. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8142. (if (looking-at " +") (replace-match ""))))
  8143. (goto-char (point-max))
  8144. (when what
  8145. (insert
  8146. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8147. (cond ((eq what 'scheduled) org-scheduled-string)
  8148. ((eq what 'deadline) org-deadline-string)
  8149. ((eq what 'closed) org-closed-string))
  8150. " ")
  8151. (setq ts (org-insert-time-stamp
  8152. time
  8153. (or org-time-was-given
  8154. (and (eq what 'closed) org-log-done-with-time))
  8155. (eq what 'closed)
  8156. nil nil (list org-end-time-was-given)))
  8157. (end-of-line 1))
  8158. (goto-char (point-min))
  8159. (widen)
  8160. (if (and (looking-at "[ \t]+\n")
  8161. (equal (char-before) ?\n))
  8162. (delete-region (1- (point)) (point-at-eol)))
  8163. ts)))))
  8164. (defvar org-log-note-marker (make-marker))
  8165. (defvar org-log-note-purpose nil)
  8166. (defvar org-log-note-state nil)
  8167. (defvar org-log-note-how nil)
  8168. (defvar org-log-note-window-configuration nil)
  8169. (defvar org-log-note-return-to (make-marker))
  8170. (defvar org-log-post-message nil
  8171. "Message to be displayed after a log note has been stored.
  8172. The auto-repeater uses this.")
  8173. (defun org-add-note ()
  8174. "Add a note to the current entry.
  8175. This is done in the same way as adding a state change note."
  8176. (interactive)
  8177. (org-add-log-setup 'note nil t nil))
  8178. (defun org-add-log-setup (&optional purpose state findpos how)
  8179. "Set up the post command hook to take a note.
  8180. If this is about to TODO state change, the new state is expected in STATE.
  8181. When FINDPOS is non-nil, find the correct position for the note in
  8182. the current entry. If not, assume that it can be inserted at point."
  8183. (save-excursion
  8184. (when findpos
  8185. (org-back-to-heading t)
  8186. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8187. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8188. "[^\r\n]*\\)?"))
  8189. (goto-char (match-end 0))
  8190. (unless org-log-states-order-reversed
  8191. (and (= (char-after) ?\n) (forward-char 1))
  8192. (org-skip-over-state-notes)
  8193. (skip-chars-backward " \t\n\r")))
  8194. (move-marker org-log-note-marker (point))
  8195. (setq org-log-note-purpose purpose
  8196. org-log-note-state state
  8197. org-log-note-how how)
  8198. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8199. (defun org-skip-over-state-notes ()
  8200. "Skip past the list of State notes in an entry."
  8201. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8202. (while (looking-at "[ \t]*- State")
  8203. (condition-case nil
  8204. (org-next-item)
  8205. (error (org-end-of-item)))))
  8206. (defun org-add-log-note (&optional purpose)
  8207. "Pop up a window for taking a note, and add this note later at point."
  8208. (remove-hook 'post-command-hook 'org-add-log-note)
  8209. (setq org-log-note-window-configuration (current-window-configuration))
  8210. (delete-other-windows)
  8211. (move-marker org-log-note-return-to (point))
  8212. (switch-to-buffer (marker-buffer org-log-note-marker))
  8213. (goto-char org-log-note-marker)
  8214. (org-switch-to-buffer-other-window "*Org Note*")
  8215. (erase-buffer)
  8216. (if (memq org-log-note-how '(time state))
  8217. (org-store-log-note)
  8218. (let ((org-inhibit-startup t)) (org-mode))
  8219. (insert (format "# Insert note for %s.
  8220. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8221. (cond
  8222. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8223. ((eq org-log-note-purpose 'done) "closed todo item")
  8224. ((eq org-log-note-purpose 'state)
  8225. (format "state change to \"%s\"" org-log-note-state))
  8226. ((eq org-log-note-purpose 'note)
  8227. "this entry")
  8228. (t (error "This should not happen")))))
  8229. (org-set-local 'org-finish-function 'org-store-log-note)))
  8230. (defvar org-note-abort nil) ; dynamically scoped
  8231. (defun org-store-log-note ()
  8232. "Finish taking a log note, and insert it to where it belongs."
  8233. (let ((txt (buffer-string))
  8234. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8235. lines ind)
  8236. (kill-buffer (current-buffer))
  8237. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8238. (setq txt (replace-match "" t t txt)))
  8239. (if (string-match "\\s-+\\'" txt)
  8240. (setq txt (replace-match "" t t txt)))
  8241. (setq lines (org-split-string txt "\n"))
  8242. (when (and note (string-match "\\S-" note))
  8243. (setq note
  8244. (org-replace-escapes
  8245. note
  8246. (list (cons "%u" (user-login-name))
  8247. (cons "%U" user-full-name)
  8248. (cons "%t" (format-time-string
  8249. (org-time-stamp-format 'long 'inactive)
  8250. (current-time)))
  8251. (cons "%s" (if org-log-note-state
  8252. (concat "\"" org-log-note-state "\"")
  8253. "")))))
  8254. (if lines (setq note (concat note " \\\\")))
  8255. (push note lines))
  8256. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8257. (when lines
  8258. (save-excursion
  8259. (set-buffer (marker-buffer org-log-note-marker))
  8260. (save-excursion
  8261. (goto-char org-log-note-marker)
  8262. (move-marker org-log-note-marker nil)
  8263. (end-of-line 1)
  8264. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8265. (indent-relative nil)
  8266. (insert "- " (pop lines))
  8267. (org-indent-line-function)
  8268. (beginning-of-line 1)
  8269. (looking-at "[ \t]*")
  8270. (setq ind (concat (match-string 0) " "))
  8271. (end-of-line 1)
  8272. (while lines (insert "\n" ind (pop lines)))))))
  8273. (set-window-configuration org-log-note-window-configuration)
  8274. (with-current-buffer (marker-buffer org-log-note-return-to)
  8275. (goto-char org-log-note-return-to))
  8276. (move-marker org-log-note-return-to nil)
  8277. (and org-log-post-message (message "%s" org-log-post-message)))
  8278. (defun org-sparse-tree (&optional arg)
  8279. "Create a sparse tree, prompt for the details.
  8280. This command can create sparse trees. You first need to select the type
  8281. of match used to create the tree:
  8282. t Show entries with a specific TODO keyword.
  8283. T Show entries selected by a tags match.
  8284. p Enter a property name and its value (both with completion on existing
  8285. names/values) and show entries with that property.
  8286. r Show entries matching a regular expression
  8287. d Show deadlines due within `org-deadline-warning-days'."
  8288. (interactive "P")
  8289. (let (ans kwd value)
  8290. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8291. (setq ans (read-char-exclusive))
  8292. (cond
  8293. ((equal ans ?d)
  8294. (call-interactively 'org-check-deadlines))
  8295. ((equal ans ?b)
  8296. (call-interactively 'org-check-before-date))
  8297. ((equal ans ?t)
  8298. (org-show-todo-tree '(4)))
  8299. ((equal ans ?T)
  8300. (call-interactively 'org-tags-sparse-tree))
  8301. ((member ans '(?p ?P))
  8302. (setq kwd (completing-read "Property: "
  8303. (mapcar 'list (org-buffer-property-keys))))
  8304. (setq value (completing-read "Value: "
  8305. (mapcar 'list (org-property-values kwd))))
  8306. (unless (string-match "\\`{.*}\\'" value)
  8307. (setq value (concat "\"" value "\"")))
  8308. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8309. ((member ans '(?r ?R ?/))
  8310. (call-interactively 'org-occur))
  8311. (t (error "No such sparse tree command \"%c\"" ans)))))
  8312. (defvar org-occur-highlights nil
  8313. "List of overlays used for occur matches.")
  8314. (make-variable-buffer-local 'org-occur-highlights)
  8315. (defvar org-occur-parameters nil
  8316. "Parameters of the active org-occur calls.
  8317. This is a list, each call to org-occur pushes as cons cell,
  8318. containing the regular expression and the callback, onto the list.
  8319. The list can contain several entries if `org-occur' has been called
  8320. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8321. will only contain one set of parameters. When the highlights are
  8322. removed (for example with `C-c C-c', or with the next edit (depending
  8323. on `org-remove-highlights-with-change'), this variable is emptied
  8324. as well.")
  8325. (make-variable-buffer-local 'org-occur-parameters)
  8326. (defun org-occur (regexp &optional keep-previous callback)
  8327. "Make a compact tree which shows all matches of REGEXP.
  8328. The tree will show the lines where the regexp matches, and all higher
  8329. headlines above the match. It will also show the heading after the match,
  8330. to make sure editing the matching entry is easy.
  8331. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8332. call to `org-occur' will be kept, to allow stacking of calls to this
  8333. command.
  8334. If CALLBACK is non-nil, it is a function which is called to confirm
  8335. that the match should indeed be shown."
  8336. (interactive "sRegexp: \nP")
  8337. (unless keep-previous
  8338. (org-remove-occur-highlights nil nil t))
  8339. (push (cons regexp callback) org-occur-parameters)
  8340. (let ((cnt 0))
  8341. (save-excursion
  8342. (goto-char (point-min))
  8343. (if (or (not keep-previous) ; do not want to keep
  8344. (not org-occur-highlights)) ; no previous matches
  8345. ;; hide everything
  8346. (org-overview))
  8347. (while (re-search-forward regexp nil t)
  8348. (when (or (not callback)
  8349. (save-match-data (funcall callback)))
  8350. (setq cnt (1+ cnt))
  8351. (when org-highlight-sparse-tree-matches
  8352. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8353. (org-show-context 'occur-tree))))
  8354. (when org-remove-highlights-with-change
  8355. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8356. nil 'local))
  8357. (unless org-sparse-tree-open-archived-trees
  8358. (org-hide-archived-subtrees (point-min) (point-max)))
  8359. (run-hooks 'org-occur-hook)
  8360. (if (interactive-p)
  8361. (message "%d match(es) for regexp %s" cnt regexp))
  8362. cnt))
  8363. (defun org-show-context (&optional key)
  8364. "Make sure point and context and visible.
  8365. How much context is shown depends upon the variables
  8366. `org-show-hierarchy-above', `org-show-following-heading'. and
  8367. `org-show-siblings'."
  8368. (let ((heading-p (org-on-heading-p t))
  8369. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8370. (following-p (org-get-alist-option org-show-following-heading key))
  8371. (entry-p (org-get-alist-option org-show-entry-below key))
  8372. (siblings-p (org-get-alist-option org-show-siblings key)))
  8373. (catch 'exit
  8374. ;; Show heading or entry text
  8375. (if (and heading-p (not entry-p))
  8376. (org-flag-heading nil) ; only show the heading
  8377. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8378. (org-show-hidden-entry))) ; show entire entry
  8379. (when following-p
  8380. ;; Show next sibling, or heading below text
  8381. (save-excursion
  8382. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8383. (org-flag-heading nil))))
  8384. (when siblings-p (org-show-siblings))
  8385. (when hierarchy-p
  8386. ;; show all higher headings, possibly with siblings
  8387. (save-excursion
  8388. (while (and (condition-case nil
  8389. (progn (org-up-heading-all 1) t)
  8390. (error nil))
  8391. (not (bobp)))
  8392. (org-flag-heading nil)
  8393. (when siblings-p (org-show-siblings))))))))
  8394. (defun org-reveal (&optional siblings)
  8395. "Show current entry, hierarchy above it, and the following headline.
  8396. This can be used to show a consistent set of context around locations
  8397. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8398. not t for the search context.
  8399. With optional argument SIBLINGS, on each level of the hierarchy all
  8400. siblings are shown. This repairs the tree structure to what it would
  8401. look like when opened with hierarchical calls to `org-cycle'."
  8402. (interactive "P")
  8403. (let ((org-show-hierarchy-above t)
  8404. (org-show-following-heading t)
  8405. (org-show-siblings (if siblings t org-show-siblings)))
  8406. (org-show-context nil)))
  8407. (defun org-highlight-new-match (beg end)
  8408. "Highlight from BEG to END and mark the highlight is an occur headline."
  8409. (let ((ov (org-make-overlay beg end)))
  8410. (org-overlay-put ov 'face 'secondary-selection)
  8411. (push ov org-occur-highlights)))
  8412. (defun org-remove-occur-highlights (&optional beg end noremove)
  8413. "Remove the occur highlights from the buffer.
  8414. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8415. from the `before-change-functions' in the current buffer."
  8416. (interactive)
  8417. (unless org-inhibit-highlight-removal
  8418. (mapc 'org-delete-overlay org-occur-highlights)
  8419. (setq org-occur-highlights nil)
  8420. (setq org-occur-parameters nil)
  8421. (unless noremove
  8422. (remove-hook 'before-change-functions
  8423. 'org-remove-occur-highlights 'local))))
  8424. ;;;; Priorities
  8425. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8426. "Regular expression matching the priority indicator.")
  8427. (defvar org-remove-priority-next-time nil)
  8428. (defun org-priority-up ()
  8429. "Increase the priority of the current item."
  8430. (interactive)
  8431. (org-priority 'up))
  8432. (defun org-priority-down ()
  8433. "Decrease the priority of the current item."
  8434. (interactive)
  8435. (org-priority 'down))
  8436. (defun org-priority (&optional action)
  8437. "Change the priority of an item by ARG.
  8438. ACTION can be `set', `up', `down', or a character."
  8439. (interactive)
  8440. (setq action (or action 'set))
  8441. (let (current new news have remove)
  8442. (save-excursion
  8443. (org-back-to-heading)
  8444. (if (looking-at org-priority-regexp)
  8445. (setq current (string-to-char (match-string 2))
  8446. have t)
  8447. (setq current org-default-priority))
  8448. (cond
  8449. ((or (eq action 'set)
  8450. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8451. (if (not (eq action 'set))
  8452. (setq new action)
  8453. (message "Priority %c-%c, SPC to remove: "
  8454. org-highest-priority org-lowest-priority)
  8455. (setq new (read-char-exclusive)))
  8456. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8457. (= (upcase org-lowest-priority) org-lowest-priority))
  8458. (setq new (upcase new)))
  8459. (cond ((equal new ?\ ) (setq remove t))
  8460. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8461. (error "Priority must be between `%c' and `%c'"
  8462. org-highest-priority org-lowest-priority))))
  8463. ((eq action 'up)
  8464. (if (and (not have) (eq last-command this-command))
  8465. (setq new org-lowest-priority)
  8466. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8467. org-default-priority (1- current)))))
  8468. ((eq action 'down)
  8469. (if (and (not have) (eq last-command this-command))
  8470. (setq new org-highest-priority)
  8471. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8472. org-default-priority (1+ current)))))
  8473. (t (error "Invalid action")))
  8474. (if (or (< (upcase new) org-highest-priority)
  8475. (> (upcase new) org-lowest-priority))
  8476. (setq remove t))
  8477. (setq news (format "%c" new))
  8478. (if have
  8479. (if remove
  8480. (replace-match "" t t nil 1)
  8481. (replace-match news t t nil 2))
  8482. (if remove
  8483. (error "No priority cookie found in line")
  8484. (looking-at org-todo-line-regexp)
  8485. (if (match-end 2)
  8486. (progn
  8487. (goto-char (match-end 2))
  8488. (insert " [#" news "]"))
  8489. (goto-char (match-beginning 3))
  8490. (insert "[#" news "] ")))))
  8491. (org-preserve-lc (org-set-tags nil 'align))
  8492. (if remove
  8493. (message "Priority removed")
  8494. (message "Priority of current item set to %s" news))))
  8495. (defun org-get-priority (s)
  8496. "Find priority cookie and return priority."
  8497. (save-match-data
  8498. (if (not (string-match org-priority-regexp s))
  8499. (* 1000 (- org-lowest-priority org-default-priority))
  8500. (* 1000 (- org-lowest-priority
  8501. (string-to-char (match-string 2 s)))))))
  8502. ;;;; Tags
  8503. (defvar org-agenda-archives-mode)
  8504. (defun org-scan-tags (action matcher &optional todo-only)
  8505. "Scan headline tags with inheritance and produce output ACTION.
  8506. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8507. or `agenda' to produce an entry list for an agenda view. It can also be
  8508. a Lisp form or a function that should be called at each matched headline, in
  8509. this case the return value is a list of all return values from these calls.
  8510. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8511. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8512. only lines with a TODO keyword are included in the output."
  8513. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8514. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8515. (org-re
  8516. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8517. (props (list 'face 'default
  8518. 'done-face 'org-done
  8519. 'undone-face 'default
  8520. 'mouse-face 'highlight
  8521. 'org-not-done-regexp org-not-done-regexp
  8522. 'org-todo-regexp org-todo-regexp
  8523. 'keymap org-agenda-keymap
  8524. 'help-echo
  8525. (format "mouse-2 or RET jump to org file %s"
  8526. (abbreviate-file-name
  8527. (or (buffer-file-name (buffer-base-buffer))
  8528. (buffer-name (buffer-base-buffer)))))))
  8529. (case-fold-search nil)
  8530. lspos tags tags-list
  8531. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8532. (llast 0) rtn rtn1 level category i txt
  8533. todo marker entry priority)
  8534. (when (not (member action '(agenda sparse-tree)))
  8535. (setq action (list 'lambda nil action)))
  8536. (save-excursion
  8537. (goto-char (point-min))
  8538. (when (eq action 'sparse-tree)
  8539. (org-overview)
  8540. (org-remove-occur-highlights))
  8541. (while (re-search-forward re nil t)
  8542. (catch :skip
  8543. (setq todo (if (match-end 1) (match-string 2))
  8544. tags (if (match-end 4) (match-string 4)))
  8545. (goto-char (setq lspos (1+ (match-beginning 0))))
  8546. (setq level (org-reduced-level (funcall outline-level))
  8547. category (org-get-category))
  8548. (setq i llast llast level)
  8549. ;; remove tag lists from same and sublevels
  8550. (while (>= i level)
  8551. (when (setq entry (assoc i tags-alist))
  8552. (setq tags-alist (delete entry tags-alist)))
  8553. (setq i (1- i)))
  8554. ;; add the next tags
  8555. (when tags
  8556. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8557. tags-alist
  8558. (cons (cons level tags) tags-alist)))
  8559. ;; compile tags for current headline
  8560. (setq tags-list
  8561. (if org-use-tag-inheritance
  8562. (apply 'append (mapcar 'cdr tags-alist))
  8563. tags))
  8564. (when (and tags org-use-tag-inheritance
  8565. (not (eq t org-use-tag-inheritance)))
  8566. ;; selective inheritance, remove uninherited ones
  8567. (setcdr (car tags-alist)
  8568. (org-remove-uniherited-tags (cdar tags-alist))))
  8569. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8570. (eval matcher)
  8571. (or
  8572. (not (member org-archive-tag tags-list))
  8573. ;; we have an archive tag, should we use this anyway?
  8574. (or (not org-agenda-skip-archived-trees)
  8575. (and (eq action 'agenda) org-agenda-archives-mode))))
  8576. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8577. ;; select this headline
  8578. (cond
  8579. ((eq action 'sparse-tree)
  8580. (and org-highlight-sparse-tree-matches
  8581. (org-get-heading) (match-end 0)
  8582. (org-highlight-new-match
  8583. (match-beginning 0) (match-beginning 1)))
  8584. (org-show-context 'tags-tree))
  8585. ((eq action 'agenda)
  8586. (setq txt (org-format-agenda-item
  8587. ""
  8588. (concat
  8589. (if org-tags-match-list-sublevels
  8590. (make-string (1- level) ?.) "")
  8591. (org-get-heading))
  8592. category tags-list)
  8593. priority (org-get-priority txt))
  8594. (goto-char lspos)
  8595. (setq marker (org-agenda-new-marker))
  8596. (org-add-props txt props
  8597. 'org-marker marker 'org-hd-marker marker 'org-category category
  8598. 'priority priority 'type "tagsmatch")
  8599. (push txt rtn))
  8600. ((functionp action)
  8601. (save-excursion
  8602. (setq rtn1 (funcall action))
  8603. (push rtn1 rtn))
  8604. (goto-char (point-at-eol)))
  8605. (t (error "Invalid action")))
  8606. ;; if we are to skip sublevels, jump to end of subtree
  8607. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8608. (when (and (eq action 'sparse-tree)
  8609. (not org-sparse-tree-open-archived-trees))
  8610. (org-hide-archived-subtrees (point-min) (point-max)))
  8611. (nreverse rtn)))
  8612. (defun org-remove-uniherited-tags (tags)
  8613. "Remove all tags that are not inherited from the list TAGS."
  8614. (cond
  8615. ((eq org-use-tag-inheritance t) tags)
  8616. ((not org-use-tag-inheritance) nil)
  8617. ((stringp org-use-tag-inheritance)
  8618. (delq nil (mapcar
  8619. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8620. tags)))
  8621. ((listp org-use-tag-inheritance)
  8622. (org-delete-all org-use-tag-inheritance tags))))
  8623. (defvar todo-only) ;; dynamically scoped
  8624. (defun org-tags-sparse-tree (&optional todo-only match)
  8625. "Create a sparse tree according to tags string MATCH.
  8626. MATCH can contain positive and negative selection of tags, like
  8627. \"+WORK+URGENT-WITHBOSS\".
  8628. If optional argument TODO_ONLY is non-nil, only select lines that are
  8629. also TODO lines."
  8630. (interactive "P")
  8631. (org-prepare-agenda-buffers (list (current-buffer)))
  8632. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8633. (defvar org-cached-props nil)
  8634. (defun org-cached-entry-get (pom property)
  8635. (if (or (eq t org-use-property-inheritance)
  8636. (and (stringp org-use-property-inheritance)
  8637. (string-match org-use-property-inheritance property))
  8638. (and (listp org-use-property-inheritance)
  8639. (member property org-use-property-inheritance)))
  8640. ;; Caching is not possible, check it directly
  8641. (org-entry-get pom property 'inherit)
  8642. ;; Get all properties, so that we can do complicated checks easily
  8643. (cdr (assoc property (or org-cached-props
  8644. (setq org-cached-props
  8645. (org-entry-properties pom)))))))
  8646. (defun org-global-tags-completion-table (&optional files)
  8647. "Return the list of all tags in all agenda buffer/files."
  8648. (save-excursion
  8649. (org-uniquify
  8650. (delq nil
  8651. (apply 'append
  8652. (mapcar
  8653. (lambda (file)
  8654. (set-buffer (find-file-noselect file))
  8655. (append (org-get-buffer-tags)
  8656. (mapcar (lambda (x) (if (stringp (car-safe x))
  8657. (list (car-safe x)) nil))
  8658. org-tag-alist)))
  8659. (if (and files (car files))
  8660. files
  8661. (org-agenda-files))))))))
  8662. (defun org-make-tags-matcher (match)
  8663. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8664. ;; todo-only is scoped dynamically into this function, and the function
  8665. ;; may change it it the matcher asksk for it.
  8666. (unless match
  8667. ;; Get a new match request, with completion
  8668. (let ((org-last-tags-completion-table
  8669. (org-global-tags-completion-table)))
  8670. (setq match (completing-read
  8671. "Match: " 'org-tags-completion-function nil nil nil
  8672. 'org-tags-history))))
  8673. ;; Parse the string and create a lisp form
  8674. (let ((match0 match)
  8675. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8676. minus tag mm
  8677. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8678. orterms term orlist re-p str-p level-p level-op
  8679. prop-p pn pv po cat-p gv)
  8680. (if (string-match "/+" match)
  8681. ;; match contains also a todo-matching request
  8682. (progn
  8683. (setq tagsmatch (substring match 0 (match-beginning 0))
  8684. todomatch (substring match (match-end 0)))
  8685. (if (string-match "^!" todomatch)
  8686. (setq todo-only t todomatch (substring todomatch 1)))
  8687. (if (string-match "^\\s-*$" todomatch)
  8688. (setq todomatch nil)))
  8689. ;; only matching tags
  8690. (setq tagsmatch match todomatch nil))
  8691. ;; Make the tags matcher
  8692. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8693. (setq tagsmatcher t)
  8694. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8695. (while (setq term (pop orterms))
  8696. (while (and (equal (substring term -1) "\\") orterms)
  8697. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8698. (while (string-match re term)
  8699. (setq minus (and (match-end 1)
  8700. (equal (match-string 1 term) "-"))
  8701. tag (match-string 2 term)
  8702. re-p (equal (string-to-char tag) ?{)
  8703. level-p (match-end 4)
  8704. prop-p (match-end 5)
  8705. mm (cond
  8706. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8707. (level-p
  8708. (setq level-op (org-op-to-function (match-string 3 term)))
  8709. `(,level-op level ,(string-to-number
  8710. (match-string 4 term))))
  8711. (prop-p
  8712. (setq pn (match-string 5 term)
  8713. po (match-string 6 term)
  8714. pv (match-string 7 term)
  8715. cat-p (equal pn "CATEGORY")
  8716. re-p (equal (string-to-char pv) ?{)
  8717. str-p (equal (string-to-char pv) ?\")
  8718. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8719. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8720. (if time-p (setq pv (org-matcher-time pv)))
  8721. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8722. (if (equal pn "CATEGORY")
  8723. (setq gv '(get-text-property (point) 'org-category))
  8724. (setq gv `(org-cached-entry-get nil ,pn)))
  8725. (if re-p
  8726. (if (eq po 'org<>)
  8727. `(not (string-match ,pv (or ,gv "")))
  8728. `(string-match ,pv (or ,gv "")))
  8729. (if str-p
  8730. `(,po (or ,gv "") ,pv)
  8731. `(,po (string-to-number (or ,gv ""))
  8732. ,(string-to-number pv) ))))
  8733. (t `(member ,(downcase tag) tags-list)))
  8734. mm (if minus (list 'not mm) mm)
  8735. term (substring term (match-end 0)))
  8736. (push mm tagsmatcher))
  8737. (push (if (> (length tagsmatcher) 1)
  8738. (cons 'and tagsmatcher)
  8739. (car tagsmatcher))
  8740. orlist)
  8741. (setq tagsmatcher nil))
  8742. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8743. (setq tagsmatcher
  8744. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8745. ;; Make the todo matcher
  8746. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8747. (setq todomatcher t)
  8748. (setq orterms (org-split-string todomatch "|") orlist nil)
  8749. (while (setq term (pop orterms))
  8750. (while (string-match re term)
  8751. (setq minus (and (match-end 1)
  8752. (equal (match-string 1 term) "-"))
  8753. kwd (match-string 2 term)
  8754. re-p (equal (string-to-char kwd) ?{)
  8755. term (substring term (match-end 0))
  8756. mm (if re-p
  8757. `(string-match ,(substring kwd 1 -1) todo)
  8758. (list 'equal 'todo kwd))
  8759. mm (if minus (list 'not mm) mm))
  8760. (push mm todomatcher))
  8761. (push (if (> (length todomatcher) 1)
  8762. (cons 'and todomatcher)
  8763. (car todomatcher))
  8764. orlist)
  8765. (setq todomatcher nil))
  8766. (setq todomatcher (if (> (length orlist) 1)
  8767. (cons 'or orlist) (car orlist))))
  8768. ;; Return the string and lisp forms of the matcher
  8769. (setq matcher (if todomatcher
  8770. (list 'and tagsmatcher todomatcher)
  8771. tagsmatcher))
  8772. (cons match0 matcher)))
  8773. (defun org-op-to-function (op &optional stringp)
  8774. "Turn an operator into the appropriate function."
  8775. (setq op
  8776. (cond
  8777. ((equal op "<" ) '(< string< org-time<))
  8778. ((equal op ">" ) '(> org-string> org-time>))
  8779. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8780. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8781. ((member op '("=" "==")) '(= string= org-time=))
  8782. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8783. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8784. (defun org<> (a b) (not (= a b)))
  8785. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8786. (defun org-string>= (a b) (not (string< a b)))
  8787. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8788. (defun org-string<> (a b) (not (string= a b)))
  8789. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8790. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8791. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8792. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8793. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8794. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8795. (defun org-2ft (s)
  8796. "Convert S to a floating point time.
  8797. If S is already a number, just return it. If it is a string, parse
  8798. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8799. (cond
  8800. ((numberp s) s)
  8801. ((stringp s)
  8802. (condition-case nil
  8803. (float-time (apply 'encode-time (org-parse-time-string s)))
  8804. (error 0.)))
  8805. (t 0.)))
  8806. (defun org-matcher-time (s)
  8807. (cond
  8808. ((equal s "<now>") (float-time))
  8809. ((equal s "<today>")
  8810. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8811. (t (org-2ft s))))
  8812. (defun org-match-any-p (re list)
  8813. "Does re match any element of list?"
  8814. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8815. (delq nil list))
  8816. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8817. (defvar org-tags-overlay (org-make-overlay 1 1))
  8818. (org-detach-overlay org-tags-overlay)
  8819. (defun org-get-tags-at (&optional pos)
  8820. "Get a list of all headline tags applicable at POS.
  8821. POS defaults to point. If tags are inherited, the list contains
  8822. the targets in the same sequence as the headlines appear, i.e.
  8823. the tags of the current headline come last."
  8824. (interactive)
  8825. (let (tags ltags lastpos parent)
  8826. (save-excursion
  8827. (save-restriction
  8828. (widen)
  8829. (goto-char (or pos (point)))
  8830. (save-match-data
  8831. (condition-case nil
  8832. (progn
  8833. (org-back-to-heading t)
  8834. (while (not (equal lastpos (point)))
  8835. (setq lastpos (point))
  8836. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8837. (setq ltags (org-split-string
  8838. (org-match-string-no-properties 1) ":"))
  8839. (setq tags (append (org-remove-uniherited-tags ltags)
  8840. tags)))
  8841. (or org-use-tag-inheritance (error ""))
  8842. (org-up-heading-all 1)
  8843. (setq parent t)))
  8844. (error nil))))
  8845. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8846. (defun org-toggle-tag (tag &optional onoff)
  8847. "Toggle the tag TAG for the current line.
  8848. If ONOFF is `on' or `off', don't toggle but set to this state."
  8849. (unless (org-on-heading-p t) (error "Not on headling"))
  8850. (let (res current)
  8851. (save-excursion
  8852. (beginning-of-line)
  8853. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8854. (point-at-eol) t)
  8855. (progn
  8856. (setq current (match-string 1))
  8857. (replace-match ""))
  8858. (setq current ""))
  8859. (setq current (nreverse (org-split-string current ":")))
  8860. (cond
  8861. ((eq onoff 'on)
  8862. (setq res t)
  8863. (or (member tag current) (push tag current)))
  8864. ((eq onoff 'off)
  8865. (or (not (member tag current)) (setq current (delete tag current))))
  8866. (t (if (member tag current)
  8867. (setq current (delete tag current))
  8868. (setq res t)
  8869. (push tag current))))
  8870. (end-of-line 1)
  8871. (if current
  8872. (progn
  8873. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8874. (org-set-tags nil t))
  8875. (delete-horizontal-space))
  8876. (run-hooks 'org-after-tags-change-hook))
  8877. res))
  8878. (defun org-align-tags-here (to-col)
  8879. ;; Assumes that this is a headline
  8880. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8881. (beginning-of-line 1)
  8882. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8883. (< pos (match-beginning 2)))
  8884. (progn
  8885. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8886. (goto-char (match-beginning 1))
  8887. (insert " ")
  8888. (delete-region (point) (1+ (match-beginning 2)))
  8889. (setq ncol (max (1+ (current-column))
  8890. (1+ col)
  8891. (if (> to-col 0)
  8892. to-col
  8893. (- (abs to-col) tags-l))))
  8894. (setq p (point))
  8895. (insert (make-string (- ncol (current-column)) ?\ ))
  8896. (setq ncol (current-column))
  8897. (when indent-tabs-mode (tabify p (point-at-eol)))
  8898. (org-move-to-column (min ncol col) t))
  8899. (goto-char pos))))
  8900. (defun org-set-tags (&optional arg just-align)
  8901. "Set the tags for the current headline.
  8902. With prefix ARG, realign all tags in headings in the current buffer."
  8903. (interactive "P")
  8904. (let* ((re (concat "^" outline-regexp))
  8905. (current (org-get-tags-string))
  8906. (col (current-column))
  8907. (org-setting-tags t)
  8908. table current-tags inherited-tags ; computed below when needed
  8909. tags p0 c0 c1 rpl)
  8910. (if arg
  8911. (save-excursion
  8912. (goto-char (point-min))
  8913. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8914. (while (re-search-forward re nil t)
  8915. (org-set-tags nil t)
  8916. (end-of-line 1)))
  8917. (message "All tags realigned to column %d" org-tags-column))
  8918. (if just-align
  8919. (setq tags current)
  8920. ;; Get a new set of tags from the user
  8921. (save-excursion
  8922. (setq table (or org-tag-alist (org-get-buffer-tags))
  8923. org-last-tags-completion-table table
  8924. current-tags (org-split-string current ":")
  8925. inherited-tags (nreverse
  8926. (nthcdr (length current-tags)
  8927. (nreverse (org-get-tags-at))))
  8928. tags
  8929. (if (or (eq t org-use-fast-tag-selection)
  8930. (and org-use-fast-tag-selection
  8931. (delq nil (mapcar 'cdr table))))
  8932. (org-fast-tag-selection
  8933. current-tags inherited-tags table
  8934. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8935. (let ((org-add-colon-after-tag-completion t))
  8936. (org-trim
  8937. (org-without-partial-completion
  8938. (completing-read "Tags: " 'org-tags-completion-function
  8939. nil nil current 'org-tags-history)))))))
  8940. (while (string-match "[-+&]+" tags)
  8941. ;; No boolean logic, just a list
  8942. (setq tags (replace-match ":" t t tags))))
  8943. (if (string-match "\\`[\t ]*\\'" tags)
  8944. (setq tags "")
  8945. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8946. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8947. ;; Insert new tags at the correct column
  8948. (beginning-of-line 1)
  8949. (cond
  8950. ((and (equal current "") (equal tags "")))
  8951. ((re-search-forward
  8952. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8953. (point-at-eol) t)
  8954. (if (equal tags "")
  8955. (setq rpl "")
  8956. (goto-char (match-beginning 0))
  8957. (setq c0 (current-column) p0 (point)
  8958. c1 (max (1+ c0) (if (> org-tags-column 0)
  8959. org-tags-column
  8960. (- (- org-tags-column) (length tags))))
  8961. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8962. (replace-match rpl t t)
  8963. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8964. tags)
  8965. (t (error "Tags alignment failed")))
  8966. (org-move-to-column col)
  8967. (unless just-align
  8968. (run-hooks 'org-after-tags-change-hook)))))
  8969. (defun org-change-tag-in-region (beg end tag off)
  8970. "Add or remove TAG for each entry in the region.
  8971. This works in the agenda, and also in an org-mode buffer."
  8972. (interactive
  8973. (list (region-beginning) (region-end)
  8974. (let ((org-last-tags-completion-table
  8975. (if (org-mode-p)
  8976. (org-get-buffer-tags)
  8977. (org-global-tags-completion-table))))
  8978. (completing-read
  8979. "Tag: " 'org-tags-completion-function nil nil nil
  8980. 'org-tags-history))
  8981. (progn
  8982. (message "[s]et or [r]emove? ")
  8983. (equal (read-char-exclusive) ?r))))
  8984. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8985. (let ((agendap (equal major-mode 'org-agenda-mode))
  8986. l1 l2 m buf pos newhead (cnt 0))
  8987. (goto-char end)
  8988. (setq l2 (1- (org-current-line)))
  8989. (goto-char beg)
  8990. (setq l1 (org-current-line))
  8991. (loop for l from l1 to l2 do
  8992. (goto-line l)
  8993. (setq m (get-text-property (point) 'org-hd-marker))
  8994. (when (or (and (org-mode-p) (org-on-heading-p))
  8995. (and agendap m))
  8996. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8997. pos (if agendap m (point)))
  8998. (with-current-buffer buf
  8999. (save-excursion
  9000. (save-restriction
  9001. (goto-char pos)
  9002. (setq cnt (1+ cnt))
  9003. (org-toggle-tag tag (if off 'off 'on))
  9004. (setq newhead (org-get-heading)))))
  9005. (and agendap (org-agenda-change-all-lines newhead m))))
  9006. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9007. (defun org-tags-completion-function (string predicate &optional flag)
  9008. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9009. (confirm (lambda (x) (stringp (car x)))))
  9010. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9011. (setq s1 (match-string 1 string)
  9012. s2 (match-string 2 string))
  9013. (setq s1 "" s2 string))
  9014. (cond
  9015. ((eq flag nil)
  9016. ;; try completion
  9017. (setq rtn (try-completion s2 ctable confirm))
  9018. (if (stringp rtn)
  9019. (setq rtn
  9020. (concat s1 s2 (substring rtn (length s2))
  9021. (if (and org-add-colon-after-tag-completion
  9022. (assoc rtn ctable))
  9023. ":" ""))))
  9024. rtn)
  9025. ((eq flag t)
  9026. ;; all-completions
  9027. (all-completions s2 ctable confirm)
  9028. )
  9029. ((eq flag 'lambda)
  9030. ;; exact match?
  9031. (assoc s2 ctable)))
  9032. ))
  9033. (defun org-fast-tag-insert (kwd tags face &optional end)
  9034. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  9035. (insert (format "%-12s" (concat kwd ":"))
  9036. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9037. (or end "")))
  9038. (defun org-fast-tag-show-exit (flag)
  9039. (save-excursion
  9040. (goto-line 3)
  9041. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9042. (replace-match ""))
  9043. (when flag
  9044. (end-of-line 1)
  9045. (org-move-to-column (- (window-width) 19) t)
  9046. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9047. (defun org-set-current-tags-overlay (current prefix)
  9048. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9049. (if (featurep 'xemacs)
  9050. (org-overlay-display org-tags-overlay (concat prefix s)
  9051. 'secondary-selection)
  9052. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9053. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9054. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9055. "Fast tag selection with single keys.
  9056. CURRENT is the current list of tags in the headline, INHERITED is the
  9057. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9058. possibly with grouping information. TODO-TABLE is a similar table with
  9059. TODO keywords, should these have keys assigned to them.
  9060. If the keys are nil, a-z are automatically assigned.
  9061. Returns the new tags string, or nil to not change the current settings."
  9062. (let* ((fulltable (append table todo-table))
  9063. (maxlen (apply 'max (mapcar
  9064. (lambda (x)
  9065. (if (stringp (car x)) (string-width (car x)) 0))
  9066. fulltable)))
  9067. (buf (current-buffer))
  9068. (expert (eq org-fast-tag-selection-single-key 'expert))
  9069. (buffer-tags nil)
  9070. (fwidth (+ maxlen 3 1 3))
  9071. (ncol (/ (- (window-width) 4) fwidth))
  9072. (i-face 'org-done)
  9073. (c-face 'org-todo)
  9074. tg cnt e c char c1 c2 ntable tbl rtn
  9075. ov-start ov-end ov-prefix
  9076. (exit-after-next org-fast-tag-selection-single-key)
  9077. (done-keywords org-done-keywords)
  9078. groups ingroup)
  9079. (save-excursion
  9080. (beginning-of-line 1)
  9081. (if (looking-at
  9082. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9083. (setq ov-start (match-beginning 1)
  9084. ov-end (match-end 1)
  9085. ov-prefix "")
  9086. (setq ov-start (1- (point-at-eol))
  9087. ov-end (1+ ov-start))
  9088. (skip-chars-forward "^\n\r")
  9089. (setq ov-prefix
  9090. (concat
  9091. (buffer-substring (1- (point)) (point))
  9092. (if (> (current-column) org-tags-column)
  9093. " "
  9094. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9095. (org-move-overlay org-tags-overlay ov-start ov-end)
  9096. (save-window-excursion
  9097. (if expert
  9098. (set-buffer (get-buffer-create " *Org tags*"))
  9099. (delete-other-windows)
  9100. (split-window-vertically)
  9101. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9102. (erase-buffer)
  9103. (org-set-local 'org-done-keywords done-keywords)
  9104. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9105. (org-fast-tag-insert "Current" current c-face "\n\n")
  9106. (org-fast-tag-show-exit exit-after-next)
  9107. (org-set-current-tags-overlay current ov-prefix)
  9108. (setq tbl fulltable char ?a cnt 0)
  9109. (while (setq e (pop tbl))
  9110. (cond
  9111. ((equal e '(:startgroup))
  9112. (push '() groups) (setq ingroup t)
  9113. (when (not (= cnt 0))
  9114. (setq cnt 0)
  9115. (insert "\n"))
  9116. (insert "{ "))
  9117. ((equal e '(:endgroup))
  9118. (setq ingroup nil cnt 0)
  9119. (insert "}\n"))
  9120. (t
  9121. (setq tg (car e) c2 nil)
  9122. (if (cdr e)
  9123. (setq c (cdr e))
  9124. ;; automatically assign a character.
  9125. (setq c1 (string-to-char
  9126. (downcase (substring
  9127. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9128. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9129. (while (or (rassoc char ntable) (rassoc char table))
  9130. (setq char (1+ char)))
  9131. (setq c2 c1))
  9132. (setq c (or c2 char)))
  9133. (if ingroup (push tg (car groups)))
  9134. (setq tg (org-add-props tg nil 'face
  9135. (cond
  9136. ((not (assoc tg table))
  9137. (org-get-todo-face tg))
  9138. ((member tg current) c-face)
  9139. ((member tg inherited) i-face)
  9140. (t nil))))
  9141. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9142. (insert "[" c "] " tg (make-string
  9143. (- fwidth 4 (length tg)) ?\ ))
  9144. (push (cons tg c) ntable)
  9145. (when (= (setq cnt (1+ cnt)) ncol)
  9146. (insert "\n")
  9147. (if ingroup (insert " "))
  9148. (setq cnt 0)))))
  9149. (setq ntable (nreverse ntable))
  9150. (insert "\n")
  9151. (goto-char (point-min))
  9152. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  9153. (fit-window-to-buffer))
  9154. (setq rtn
  9155. (catch 'exit
  9156. (while t
  9157. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9158. (if groups " [!] no groups" " [!]groups")
  9159. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9160. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9161. (cond
  9162. ((= c ?\r) (throw 'exit t))
  9163. ((= c ?!)
  9164. (setq groups (not groups))
  9165. (goto-char (point-min))
  9166. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9167. ((= c ?\C-c)
  9168. (if (not expert)
  9169. (org-fast-tag-show-exit
  9170. (setq exit-after-next (not exit-after-next)))
  9171. (setq expert nil)
  9172. (delete-other-windows)
  9173. (split-window-vertically)
  9174. (org-switch-to-buffer-other-window " *Org tags*")
  9175. (and (fboundp 'fit-window-to-buffer)
  9176. (fit-window-to-buffer))))
  9177. ((or (= c ?\C-g)
  9178. (and (= c ?q) (not (rassoc c ntable))))
  9179. (org-detach-overlay org-tags-overlay)
  9180. (setq quit-flag t))
  9181. ((= c ?\ )
  9182. (setq current nil)
  9183. (if exit-after-next (setq exit-after-next 'now)))
  9184. ((= c ?\t)
  9185. (condition-case nil
  9186. (setq tg (completing-read
  9187. "Tag: "
  9188. (or buffer-tags
  9189. (with-current-buffer buf
  9190. (org-get-buffer-tags)))))
  9191. (quit (setq tg "")))
  9192. (when (string-match "\\S-" tg)
  9193. (add-to-list 'buffer-tags (list tg))
  9194. (if (member tg current)
  9195. (setq current (delete tg current))
  9196. (push tg current)))
  9197. (if exit-after-next (setq exit-after-next 'now)))
  9198. ((setq e (rassoc c todo-table) tg (car e))
  9199. (with-current-buffer buf
  9200. (save-excursion (org-todo tg)))
  9201. (if exit-after-next (setq exit-after-next 'now)))
  9202. ((setq e (rassoc c ntable) tg (car e))
  9203. (if (member tg current)
  9204. (setq current (delete tg current))
  9205. (loop for g in groups do
  9206. (if (member tg g)
  9207. (mapc (lambda (x)
  9208. (setq current (delete x current)))
  9209. g)))
  9210. (push tg current))
  9211. (if exit-after-next (setq exit-after-next 'now))))
  9212. ;; Create a sorted list
  9213. (setq current
  9214. (sort current
  9215. (lambda (a b)
  9216. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9217. (if (eq exit-after-next 'now) (throw 'exit t))
  9218. (goto-char (point-min))
  9219. (beginning-of-line 2)
  9220. (delete-region (point) (point-at-eol))
  9221. (org-fast-tag-insert "Current" current c-face)
  9222. (org-set-current-tags-overlay current ov-prefix)
  9223. (while (re-search-forward
  9224. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9225. (setq tg (match-string 1))
  9226. (add-text-properties
  9227. (match-beginning 1) (match-end 1)
  9228. (list 'face
  9229. (cond
  9230. ((member tg current) c-face)
  9231. ((member tg inherited) i-face)
  9232. (t (get-text-property (match-beginning 1) 'face))))))
  9233. (goto-char (point-min)))))
  9234. (org-detach-overlay org-tags-overlay)
  9235. (if rtn
  9236. (mapconcat 'identity current ":")
  9237. nil))))
  9238. (defun org-get-tags-string ()
  9239. "Get the TAGS string in the current headline."
  9240. (unless (org-on-heading-p t)
  9241. (error "Not on a heading"))
  9242. (save-excursion
  9243. (beginning-of-line 1)
  9244. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9245. (org-match-string-no-properties 1)
  9246. "")))
  9247. (defun org-get-tags ()
  9248. "Get the list of tags specified in the current headline."
  9249. (org-split-string (org-get-tags-string) ":"))
  9250. (defun org-get-buffer-tags ()
  9251. "Get a table of all tags used in the buffer, for completion."
  9252. (let (tags)
  9253. (save-excursion
  9254. (goto-char (point-min))
  9255. (while (re-search-forward
  9256. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9257. (when (equal (char-after (point-at-bol 0)) ?*)
  9258. (mapc (lambda (x) (add-to-list 'tags x))
  9259. (org-split-string (org-match-string-no-properties 1) ":")))))
  9260. (mapcar 'list tags)))
  9261. ;;;; The mapping API
  9262. ;;;###autoload
  9263. (defun org-map-entries (func &optional match scope &rest skip)
  9264. "Call FUNC at each headline selected by MATCH in SCOPE.
  9265. FUNC is a function or a lisp form. The function will be called without
  9266. arguments, with the cursor positioned at the beginning of the headline.
  9267. The return values of all calls to the function will be collected and
  9268. returned as a list.
  9269. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9270. Only headlines that are matched by this query will be considered during
  9271. the iteration. When MATCH is nil or t, all headlines will be
  9272. visited by the iteration.
  9273. SCOPE determines the scope of this command. It can be any of:
  9274. nil The current buffer, respecting the restriction if any
  9275. tree The subtree started with the entry at point
  9276. file The current buffer, without restriction
  9277. file-with-archives
  9278. The current buffer, and any archives associated with it
  9279. agenda All agenda files
  9280. agenda-with-archives
  9281. All agenda files with any archive files associated with them
  9282. \(file1 file2 ...)
  9283. If this is a list, all files in the list will be scanned
  9284. The remaining args are treated as settings for the skipping facilities of
  9285. the scanner. The following items can be given here:
  9286. archive skip trees with the archive tag.
  9287. comment skip trees with the COMMENT keyword
  9288. function or Emacs Lisp form:
  9289. will be used as value for `org-agenda-skip-function', so whenever
  9290. the the function returns t, FUNC will not be called for that
  9291. entry and search will continue from the point where the
  9292. function leaves it."
  9293. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9294. (org-agenda-skip-archived-trees (memq 'archive skip))
  9295. (org-agenda-skip-comment-trees (memq 'comment skip))
  9296. (org-agenda-skip-function
  9297. (car (org-delete-all '(comment archive) skip)))
  9298. (org-tags-match-list-sublevels t)
  9299. matcher pos file)
  9300. (cond
  9301. ((eq match t) (setq matcher t))
  9302. ((eq match nil) (setq matcher t))
  9303. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  9304. (when (eq scope 'tree)
  9305. (org-back-to-heading t)
  9306. (org-narrow-to-subtree)
  9307. (setq scope nil))
  9308. (if (not scope)
  9309. (progn
  9310. (org-prepare-agenda-buffers
  9311. (list (buffer-file-name (current-buffer))))
  9312. (org-scan-tags func matcher))
  9313. ;; Get the right scope
  9314. (setq pos (point))
  9315. (cond
  9316. ((and scope (listp scope) (symbolp (car scope)))
  9317. (setq scope (eval scope)))
  9318. ((eq scope 'agenda)
  9319. (setq scope (org-agenda-files t)))
  9320. ((eq scope 'agenda-with-archives)
  9321. (setq scope (org-agenda-files t))
  9322. (setq scope (org-add-archive-files scope)))
  9323. ((eq scope 'file)
  9324. (setq scope (list (buffer-file-name))))
  9325. ((eq scope 'file-with-archives)
  9326. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9327. (org-prepare-agenda-buffers scope)
  9328. (while (setq file (pop scope))
  9329. (with-current-buffer (org-find-base-buffer-visiting file)
  9330. (save-excursion
  9331. (save-restriction
  9332. (widen)
  9333. (goto-char (point-min))
  9334. (org-scan-tags func matcher))))))))
  9335. ;;;; Properties
  9336. ;;; Setting and retrieving properties
  9337. (defconst org-special-properties
  9338. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9339. "TIMESTAMP" "TIMESTAMP_IA")
  9340. "The special properties valid in Org-mode.
  9341. These are properties that are not defined in the property drawer,
  9342. but in some other way.")
  9343. (defconst org-default-properties
  9344. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9345. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9346. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9347. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9348. "Some properties that are used by Org-mode for various purposes.
  9349. Being in this list makes sure that they are offered for completion.")
  9350. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9351. "Regular expression matching the first line of a property drawer.")
  9352. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9353. "Regular expression matching the first line of a property drawer.")
  9354. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9355. "Regular expression matching the first line of a property drawer.")
  9356. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9357. "Regular expression matching the first line of a property drawer.")
  9358. (defconst org-property-drawer-re
  9359. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9360. org-property-end-re "\\)\n?")
  9361. "Matches an entire property drawer.")
  9362. (defconst org-clock-drawer-re
  9363. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9364. org-property-end-re "\\)\n?")
  9365. "Matches an entire clock drawer.")
  9366. (defun org-property-action ()
  9367. "Do an action on properties."
  9368. (interactive)
  9369. (let (c)
  9370. (org-at-property-p)
  9371. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9372. (setq c (read-char-exclusive))
  9373. (cond
  9374. ((equal c ?s)
  9375. (call-interactively 'org-set-property))
  9376. ((equal c ?d)
  9377. (call-interactively 'org-delete-property))
  9378. ((equal c ?D)
  9379. (call-interactively 'org-delete-property-globally))
  9380. ((equal c ?c)
  9381. (call-interactively 'org-compute-property-at-point))
  9382. (t (error "No such property action %c" c)))))
  9383. (defun org-at-property-p ()
  9384. "Is the cursor in a property line?"
  9385. ;; FIXME: Does not check if we are actually in the drawer.
  9386. ;; FIXME: also returns true on any drawers.....
  9387. ;; This is used by C-c C-c for property action.
  9388. (save-excursion
  9389. (beginning-of-line 1)
  9390. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9391. (defun org-get-property-block (&optional beg end force)
  9392. "Return the (beg . end) range of the body of the property drawer.
  9393. BEG and END can be beginning and end of subtree, if not given
  9394. they will be found.
  9395. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9396. (catch 'exit
  9397. (save-excursion
  9398. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9399. (end (or end (progn (outline-next-heading) (point)))))
  9400. (goto-char beg)
  9401. (if (re-search-forward org-property-start-re end t)
  9402. (setq beg (1+ (match-end 0)))
  9403. (if force
  9404. (save-excursion
  9405. (org-insert-property-drawer)
  9406. (setq end (progn (outline-next-heading) (point))))
  9407. (throw 'exit nil))
  9408. (goto-char beg)
  9409. (if (re-search-forward org-property-start-re end t)
  9410. (setq beg (1+ (match-end 0)))))
  9411. (if (re-search-forward org-property-end-re end t)
  9412. (setq end (match-beginning 0))
  9413. (or force (throw 'exit nil))
  9414. (goto-char beg)
  9415. (setq end beg)
  9416. (org-indent-line-function)
  9417. (insert ":END:\n"))
  9418. (cons beg end)))))
  9419. (defun org-entry-properties (&optional pom which)
  9420. "Get all properties of the entry at point-or-marker POM.
  9421. This includes the TODO keyword, the tags, time strings for deadline,
  9422. scheduled, and clocking, and any additional properties defined in the
  9423. entry. The return value is an alist, keys may occur multiple times
  9424. if the property key was used several times.
  9425. POM may also be nil, in which case the current entry is used.
  9426. If WHICH is nil or `all', get all properties. If WHICH is
  9427. `special' or `standard', only get that subclass."
  9428. (setq which (or which 'all))
  9429. (org-with-point-at pom
  9430. (let ((clockstr (substring org-clock-string 0 -1))
  9431. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9432. beg end range props sum-props key value string clocksum)
  9433. (save-excursion
  9434. (when (condition-case nil (org-back-to-heading t) (error nil))
  9435. (setq beg (point))
  9436. (setq sum-props (get-text-property (point) 'org-summaries))
  9437. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9438. (outline-next-heading)
  9439. (setq end (point))
  9440. (when (memq which '(all special))
  9441. ;; Get the special properties, like TODO and tags
  9442. (goto-char beg)
  9443. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9444. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9445. (when (looking-at org-priority-regexp)
  9446. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9447. (when (and (setq value (org-get-tags-string))
  9448. (string-match "\\S-" value))
  9449. (push (cons "TAGS" value) props))
  9450. (when (setq value (org-get-tags-at))
  9451. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9452. props))
  9453. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9454. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9455. string (if (equal key clockstr)
  9456. (org-no-properties
  9457. (org-trim
  9458. (buffer-substring
  9459. (match-beginning 3) (goto-char (point-at-eol)))))
  9460. (substring (org-match-string-no-properties 3) 1 -1)))
  9461. (unless key
  9462. (if (= (char-after (match-beginning 3)) ?\[)
  9463. (setq key "TIMESTAMP_IA")
  9464. (setq key "TIMESTAMP")))
  9465. (when (or (equal key clockstr) (not (assoc key props)))
  9466. (push (cons key string) props)))
  9467. )
  9468. (when (memq which '(all standard))
  9469. ;; Get the standard properties, like :PORP: ...
  9470. (setq range (org-get-property-block beg end))
  9471. (when range
  9472. (goto-char (car range))
  9473. (while (re-search-forward
  9474. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9475. (cdr range) t)
  9476. (setq key (org-match-string-no-properties 1)
  9477. value (org-trim (or (org-match-string-no-properties 2) "")))
  9478. (unless (member key excluded)
  9479. (push (cons key (or value "")) props)))))
  9480. (if clocksum
  9481. (push (cons "CLOCKSUM"
  9482. (org-columns-number-to-string (/ (float clocksum) 60.)
  9483. 'add_times))
  9484. props))
  9485. (append sum-props (nreverse props)))))))
  9486. (defun org-entry-get (pom property &optional inherit)
  9487. "Get value of PROPERTY for entry at point-or-marker POM.
  9488. If INHERIT is non-nil and the entry does not have the property,
  9489. then also check higher levels of the hierarchy.
  9490. If INHERIT is the symbol `selective', use inheritance only if the setting
  9491. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9492. If the property is present but empty, the return value is the empty string.
  9493. If the property is not present at all, nil is returned."
  9494. (org-with-point-at pom
  9495. (if (and inherit (if (eq inherit 'selective)
  9496. (org-property-inherit-p property)
  9497. t))
  9498. (org-entry-get-with-inheritance property)
  9499. (if (member property org-special-properties)
  9500. ;; We need a special property. Use brute force, get all properties.
  9501. (cdr (assoc property (org-entry-properties nil 'special)))
  9502. (let ((range (org-get-property-block)))
  9503. (if (and range
  9504. (goto-char (car range))
  9505. (re-search-forward
  9506. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9507. (cdr range) t))
  9508. ;; Found the property, return it.
  9509. (if (match-end 1)
  9510. (org-match-string-no-properties 1)
  9511. "")))))))
  9512. (defun org-property-or-variable-value (var &optional inherit)
  9513. "Check if there is a property fixing the value of VAR.
  9514. If yes, return this value. If not, return the current value of the variable."
  9515. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9516. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9517. (read prop)
  9518. (symbol-value var))))
  9519. (defun org-entry-delete (pom property)
  9520. "Delete the property PROPERTY from entry at point-or-marker POM."
  9521. (org-with-point-at pom
  9522. (if (member property org-special-properties)
  9523. nil ; cannot delete these properties.
  9524. (let ((range (org-get-property-block)))
  9525. (if (and range
  9526. (goto-char (car range))
  9527. (re-search-forward
  9528. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9529. (cdr range) t))
  9530. (progn
  9531. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9532. t)
  9533. nil)))))
  9534. ;; Multi-values properties are properties that contain multiple values
  9535. ;; These values are assumed to be single words, separated by whitespace.
  9536. (defun org-entry-add-to-multivalued-property (pom property value)
  9537. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9538. (let* ((old (org-entry-get pom property))
  9539. (values (and old (org-split-string old "[ \t]"))))
  9540. (unless (member value values)
  9541. (setq values (cons value values))
  9542. (org-entry-put pom property
  9543. (mapconcat 'identity values " ")))))
  9544. (defun org-entry-remove-from-multivalued-property (pom property value)
  9545. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9546. (let* ((old (org-entry-get pom property))
  9547. (values (and old (org-split-string old "[ \t]"))))
  9548. (when (member value values)
  9549. (setq values (delete value values))
  9550. (org-entry-put pom property
  9551. (mapconcat 'identity values " ")))))
  9552. (defun org-entry-member-in-multivalued-property (pom property value)
  9553. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9554. (let* ((old (org-entry-get pom property))
  9555. (values (and old (org-split-string old "[ \t]"))))
  9556. (member value values)))
  9557. (defvar org-entry-property-inherited-from (make-marker))
  9558. (defun org-entry-get-with-inheritance (property)
  9559. "Get entry property, and search higher levels if not present."
  9560. (let (tmp)
  9561. (save-excursion
  9562. (save-restriction
  9563. (widen)
  9564. (catch 'ex
  9565. (while t
  9566. (when (setq tmp (org-entry-get nil property))
  9567. (org-back-to-heading t)
  9568. (move-marker org-entry-property-inherited-from (point))
  9569. (throw 'ex tmp))
  9570. (or (org-up-heading-safe) (throw 'ex nil)))))
  9571. (or tmp
  9572. (cdr (assoc property org-file-properties))
  9573. (cdr (assoc property org-global-properties))
  9574. (cdr (assoc property org-global-properties-fixed))))))
  9575. (defun org-entry-put (pom property value)
  9576. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9577. (org-with-point-at pom
  9578. (org-back-to-heading t)
  9579. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9580. range)
  9581. (cond
  9582. ((equal property "TODO")
  9583. (when (and (stringp value) (string-match "\\S-" value)
  9584. (not (member value org-todo-keywords-1)))
  9585. (error "\"%s\" is not a valid TODO state" value))
  9586. (if (or (not value)
  9587. (not (string-match "\\S-" value)))
  9588. (setq value 'none))
  9589. (org-todo value)
  9590. (org-set-tags nil 'align))
  9591. ((equal property "PRIORITY")
  9592. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9593. (string-to-char value) ?\ ))
  9594. (org-set-tags nil 'align))
  9595. ((equal property "SCHEDULED")
  9596. (if (re-search-forward org-scheduled-time-regexp end t)
  9597. (cond
  9598. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9599. ((eq value 'later) (org-timestamp-change 1 'day))
  9600. (t (call-interactively 'org-schedule)))
  9601. (call-interactively 'org-schedule)))
  9602. ((equal property "DEADLINE")
  9603. (if (re-search-forward org-deadline-time-regexp end t)
  9604. (cond
  9605. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9606. ((eq value 'later) (org-timestamp-change 1 'day))
  9607. (t (call-interactively 'org-deadline)))
  9608. (call-interactively 'org-deadline)))
  9609. ((member property org-special-properties)
  9610. (error "The %s property can not yet be set with `org-entry-put'"
  9611. property))
  9612. (t ; a non-special property
  9613. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9614. (setq range (org-get-property-block beg end 'force))
  9615. (goto-char (car range))
  9616. (if (re-search-forward
  9617. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9618. (progn
  9619. (delete-region (match-beginning 1) (match-end 1))
  9620. (goto-char (match-beginning 1)))
  9621. (goto-char (cdr range))
  9622. (insert "\n")
  9623. (backward-char 1)
  9624. (org-indent-line-function)
  9625. (insert ":" property ":"))
  9626. (and value (insert " " value))
  9627. (org-indent-line-function)))))))
  9628. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9629. "Get all property keys in the current buffer.
  9630. With INCLUDE-SPECIALS, also list the special properties that relect things
  9631. like tags and TODO state.
  9632. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9633. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9634. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9635. formats in the current buffer."
  9636. (let (rtn range cfmt cols s p)
  9637. (save-excursion
  9638. (save-restriction
  9639. (widen)
  9640. (goto-char (point-min))
  9641. (while (re-search-forward org-property-start-re nil t)
  9642. (setq range (org-get-property-block))
  9643. (goto-char (car range))
  9644. (while (re-search-forward
  9645. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9646. (cdr range) t)
  9647. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9648. (outline-next-heading))))
  9649. (when include-specials
  9650. (setq rtn (append org-special-properties rtn)))
  9651. (when include-defaults
  9652. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9653. (when include-columns
  9654. (save-excursion
  9655. (save-restriction
  9656. (widen)
  9657. (goto-char (point-min))
  9658. (while (re-search-forward
  9659. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9660. nil t)
  9661. (setq cfmt (match-string 2) s 0)
  9662. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9663. cfmt s)
  9664. (setq s (match-end 0)
  9665. p (match-string 1 cfmt))
  9666. (unless (or (equal p "ITEM")
  9667. (member p org-special-properties))
  9668. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9669. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9670. (defun org-property-values (key)
  9671. "Return a list of all values of property KEY."
  9672. (save-excursion
  9673. (save-restriction
  9674. (widen)
  9675. (goto-char (point-min))
  9676. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9677. values)
  9678. (while (re-search-forward re nil t)
  9679. (add-to-list 'values (org-trim (match-string 1))))
  9680. (delete "" values)))))
  9681. (defun org-insert-property-drawer ()
  9682. "Insert a property drawer into the current entry."
  9683. (interactive)
  9684. (org-back-to-heading t)
  9685. (looking-at outline-regexp)
  9686. (let ((indent (- (match-end 0)(match-beginning 0)))
  9687. (beg (point))
  9688. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9689. end hiddenp)
  9690. (outline-next-heading)
  9691. (setq end (point))
  9692. (goto-char beg)
  9693. (while (re-search-forward re end t))
  9694. (setq hiddenp (org-invisible-p))
  9695. (end-of-line 1)
  9696. (and (equal (char-after) ?\n) (forward-char 1))
  9697. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9698. (beginning-of-line 2))
  9699. (org-skip-over-state-notes)
  9700. (skip-chars-backward " \t\n\r")
  9701. (if (eq (char-before) ?*) (forward-char 1))
  9702. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9703. (beginning-of-line 0)
  9704. (org-indent-to-column indent)
  9705. (beginning-of-line 2)
  9706. (org-indent-to-column indent)
  9707. (beginning-of-line 0)
  9708. (if hiddenp
  9709. (save-excursion
  9710. (org-back-to-heading t)
  9711. (hide-entry))
  9712. (org-flag-drawer t))))
  9713. (defun org-set-property (property value)
  9714. "In the current entry, set PROPERTY to VALUE.
  9715. When called interactively, this will prompt for a property name, offering
  9716. completion on existing and default properties. And then it will prompt
  9717. for a value, offering competion either on allowed values (via an inherited
  9718. xxx_ALL property) or on existing values in other instances of this property
  9719. in the current file."
  9720. (interactive
  9721. (let* ((completion-ignore-case t)
  9722. (keys (org-buffer-property-keys nil t t))
  9723. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9724. (prop (if (member prop0 keys)
  9725. prop0
  9726. (or (cdr (assoc (downcase prop0)
  9727. (mapcar (lambda (x) (cons (downcase x) x))
  9728. keys)))
  9729. prop0)))
  9730. (cur (org-entry-get nil prop))
  9731. (allowed (org-property-get-allowed-values nil prop 'table))
  9732. (existing (mapcar 'list (org-property-values prop)))
  9733. (val (if allowed
  9734. (org-completing-read "Value: " allowed nil 'req-match)
  9735. (org-completing-read
  9736. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9737. (concat "[" cur "]") "")
  9738. ": ")
  9739. existing nil nil "" nil cur))))
  9740. (list prop (if (equal val "") cur val))))
  9741. (unless (equal (org-entry-get nil property) value)
  9742. (org-entry-put nil property value)))
  9743. (defun org-delete-property (property)
  9744. "In the current entry, delete PROPERTY."
  9745. (interactive
  9746. (let* ((completion-ignore-case t)
  9747. (prop (completing-read
  9748. "Property: " (org-entry-properties nil 'standard))))
  9749. (list prop)))
  9750. (message "Property %s %s" property
  9751. (if (org-entry-delete nil property)
  9752. "deleted"
  9753. "was not present in the entry")))
  9754. (defun org-delete-property-globally (property)
  9755. "Remove PROPERTY globally, from all entries."
  9756. (interactive
  9757. (let* ((completion-ignore-case t)
  9758. (prop (completing-read
  9759. "Globally remove property: "
  9760. (mapcar 'list (org-buffer-property-keys)))))
  9761. (list prop)))
  9762. (save-excursion
  9763. (save-restriction
  9764. (widen)
  9765. (goto-char (point-min))
  9766. (let ((cnt 0))
  9767. (while (re-search-forward
  9768. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9769. nil t)
  9770. (setq cnt (1+ cnt))
  9771. (replace-match ""))
  9772. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9773. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9774. (defun org-compute-property-at-point ()
  9775. "Compute the property at point.
  9776. This looks for an enclosing column format, extracts the operator and
  9777. then applies it to the proerty in the column format's scope."
  9778. (interactive)
  9779. (unless (org-at-property-p)
  9780. (error "Not at a property"))
  9781. (let ((prop (org-match-string-no-properties 2)))
  9782. (org-columns-get-format-and-top-level)
  9783. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9784. (error "No operator defined for property %s" prop))
  9785. (org-columns-compute prop)))
  9786. (defun org-property-get-allowed-values (pom property &optional table)
  9787. "Get allowed values for the property PROPERTY.
  9788. When TABLE is non-nil, return an alist that can directly be used for
  9789. completion."
  9790. (let (vals)
  9791. (cond
  9792. ((equal property "TODO")
  9793. (setq vals (org-with-point-at pom
  9794. (append org-todo-keywords-1 '("")))))
  9795. ((equal property "PRIORITY")
  9796. (let ((n org-lowest-priority))
  9797. (while (>= n org-highest-priority)
  9798. (push (char-to-string n) vals)
  9799. (setq n (1- n)))))
  9800. ((member property org-special-properties))
  9801. (t
  9802. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9803. (when (and vals (string-match "\\S-" vals))
  9804. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9805. (setq vals (mapcar (lambda (x)
  9806. (cond ((stringp x) x)
  9807. ((numberp x) (number-to-string x))
  9808. ((symbolp x) (symbol-name x))
  9809. (t "???")))
  9810. vals)))))
  9811. (if table (mapcar 'list vals) vals)))
  9812. (defun org-property-previous-allowed-value (&optional previous)
  9813. "Switch to the next allowed value for this property."
  9814. (interactive)
  9815. (org-property-next-allowed-value t))
  9816. (defun org-property-next-allowed-value (&optional previous)
  9817. "Switch to the next allowed value for this property."
  9818. (interactive)
  9819. (unless (org-at-property-p)
  9820. (error "Not at a property"))
  9821. (let* ((key (match-string 2))
  9822. (value (match-string 3))
  9823. (allowed (or (org-property-get-allowed-values (point) key)
  9824. (and (member value '("[ ]" "[-]" "[X]"))
  9825. '("[ ]" "[X]"))))
  9826. nval)
  9827. (unless allowed
  9828. (error "Allowed values for this property have not been defined"))
  9829. (if previous (setq allowed (reverse allowed)))
  9830. (if (member value allowed)
  9831. (setq nval (car (cdr (member value allowed)))))
  9832. (setq nval (or nval (car allowed)))
  9833. (if (equal nval value)
  9834. (error "Only one allowed value for this property"))
  9835. (org-at-property-p)
  9836. (replace-match (concat " :" key ": " nval) t t)
  9837. (org-indent-line-function)
  9838. (beginning-of-line 1)
  9839. (skip-chars-forward " \t")))
  9840. (defun org-find-entry-with-id (ident)
  9841. "Locate the entry that contains the ID property with exact value IDENT.
  9842. IDENT can be a string, a symbol or a number, this function will search for
  9843. the string representation of it.
  9844. Return the position where this entry starts, or nil if there is no such entry."
  9845. (let ((id (cond
  9846. ((stringp ident) ident)
  9847. ((symbol-name ident) (symbol-name ident))
  9848. ((numberp ident) (number-to-string ident))
  9849. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9850. (case-fold-search nil))
  9851. (save-excursion
  9852. (save-restriction
  9853. (widen)
  9854. (goto-char (point-min))
  9855. (when (re-search-forward
  9856. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9857. nil t)
  9858. (org-back-to-heading)
  9859. (point))))))
  9860. ;;;; Timestamps
  9861. (defvar org-last-changed-timestamp nil)
  9862. (defvar org-last-inserted-timestamp nil
  9863. "The last time stamp inserted with `org-insert-time-stamp'.")
  9864. (defvar org-time-was-given) ; dynamically scoped parameter
  9865. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9866. (defvar org-ts-what) ; dynamically scoped parameter
  9867. (defun org-time-stamp (arg)
  9868. "Prompt for a date/time and insert a time stamp.
  9869. If the user specifies a time like HH:MM, or if this command is called
  9870. with a prefix argument, the time stamp will contain date and time.
  9871. Otherwise, only the date will be included. All parts of a date not
  9872. specified by the user will be filled in from the current date/time.
  9873. So if you press just return without typing anything, the time stamp
  9874. will represent the current date/time. If there is already a timestamp
  9875. at the cursor, it will be modified."
  9876. (interactive "P")
  9877. (let* ((ts nil)
  9878. (default-time
  9879. ;; Default time is either today, or, when entering a range,
  9880. ;; the range start.
  9881. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9882. (save-excursion
  9883. (re-search-backward
  9884. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9885. (- (point) 20) t)))
  9886. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9887. (current-time)))
  9888. (default-input (and ts (org-get-compact-tod ts)))
  9889. org-time-was-given org-end-time-was-given time)
  9890. (cond
  9891. ((and (org-at-timestamp-p)
  9892. (eq last-command 'org-time-stamp)
  9893. (eq this-command 'org-time-stamp))
  9894. (insert "--")
  9895. (setq time (let ((this-command this-command))
  9896. (org-read-date arg 'totime nil nil default-time default-input)))
  9897. (org-insert-time-stamp time (or org-time-was-given arg)))
  9898. ((org-at-timestamp-p)
  9899. (setq time (let ((this-command this-command))
  9900. (org-read-date arg 'totime nil nil default-time default-input)))
  9901. (when (org-at-timestamp-p) ; just to get the match data
  9902. (replace-match "")
  9903. (setq org-last-changed-timestamp
  9904. (org-insert-time-stamp
  9905. time (or org-time-was-given arg)
  9906. nil nil nil (list org-end-time-was-given))))
  9907. (message "Timestamp updated"))
  9908. (t
  9909. (setq time (let ((this-command this-command))
  9910. (org-read-date arg 'totime nil nil default-time default-input)))
  9911. (org-insert-time-stamp time (or org-time-was-given arg)
  9912. nil nil nil (list org-end-time-was-given))))))
  9913. ;; FIXME: can we use this for something else, like computing time differences?
  9914. (defun org-get-compact-tod (s)
  9915. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9916. (let* ((t1 (match-string 1 s))
  9917. (h1 (string-to-number (match-string 2 s)))
  9918. (m1 (string-to-number (match-string 3 s)))
  9919. (t2 (and (match-end 4) (match-string 5 s)))
  9920. (h2 (and t2 (string-to-number (match-string 6 s))))
  9921. (m2 (and t2 (string-to-number (match-string 7 s))))
  9922. dh dm)
  9923. (if (not t2)
  9924. t1
  9925. (setq dh (- h2 h1) dm (- m2 m1))
  9926. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9927. (concat t1 "+" (number-to-string dh)
  9928. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9929. (defun org-time-stamp-inactive (&optional arg)
  9930. "Insert an inactive time stamp.
  9931. An inactive time stamp is enclosed in square brackets instead of angle
  9932. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9933. does not link to the calendar and cannot be changed with the S-cursor keys.
  9934. So these are more for recording a certain time/date."
  9935. (interactive "P")
  9936. (let (org-time-was-given org-end-time-was-given time)
  9937. (setq time (org-read-date arg 'totime))
  9938. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9939. nil nil (list org-end-time-was-given))))
  9940. (defvar org-date-ovl (org-make-overlay 1 1))
  9941. (org-overlay-put org-date-ovl 'face 'org-warning)
  9942. (org-detach-overlay org-date-ovl)
  9943. (defvar org-ans1) ; dynamically scoped parameter
  9944. (defvar org-ans2) ; dynamically scoped parameter
  9945. (defvar org-plain-time-of-day-regexp) ; defined below
  9946. (defvar org-overriding-default-time nil) ; dynamically scoped
  9947. (defvar org-read-date-overlay nil)
  9948. (defvar org-dcst nil) ; dynamically scoped
  9949. (defun org-read-date (&optional with-time to-time from-string prompt
  9950. default-time default-input)
  9951. "Read a date, possibly a time, and make things smooth for the user.
  9952. The prompt will suggest to enter an ISO date, but you can also enter anything
  9953. which will at least partially be understood by `parse-time-string'.
  9954. Unrecognized parts of the date will default to the current day, month, year,
  9955. hour and minute. If this command is called to replace a timestamp at point,
  9956. of to enter the second timestamp of a range, the default time is taken from the
  9957. existing stamp. For example,
  9958. 3-2-5 --> 2003-02-05
  9959. feb 15 --> currentyear-02-15
  9960. sep 12 9 --> 2009-09-12
  9961. 12:45 --> today 12:45
  9962. 22 sept 0:34 --> currentyear-09-22 0:34
  9963. 12 --> currentyear-currentmonth-12
  9964. Fri --> nearest Friday (today or later)
  9965. etc.
  9966. Furthermore you can specify a relative date by giving, as the *first* thing
  9967. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9968. change in days weeks, months, years.
  9969. With a single plus or minus, the date is relative to today. With a double
  9970. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9971. +4d --> four days from today
  9972. +4 --> same as above
  9973. +2w --> two weeks from today
  9974. ++5 --> five days from default date
  9975. The function understands only English month and weekday abbreviations,
  9976. but this can be configured with the variables `parse-time-months' and
  9977. `parse-time-weekdays'.
  9978. While prompting, a calendar is popped up - you can also select the
  9979. date with the mouse (button 1). The calendar shows a period of three
  9980. months. To scroll it to other months, use the keys `>' and `<'.
  9981. If you don't like the calendar, turn it off with
  9982. \(setq org-read-date-popup-calendar nil)
  9983. With optional argument TO-TIME, the date will immediately be converted
  9984. to an internal time.
  9985. With an optional argument WITH-TIME, the prompt will suggest to also
  9986. insert a time. Note that when WITH-TIME is not set, you can still
  9987. enter a time, and this function will inform the calling routine about
  9988. this change. The calling routine may then choose to change the format
  9989. used to insert the time stamp into the buffer to include the time.
  9990. With optional argument FROM-STRING, read from this string instead from
  9991. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9992. the time/date that is used for everything that is not specified by the
  9993. user."
  9994. (require 'parse-time)
  9995. (let* ((org-time-stamp-rounding-minutes
  9996. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9997. (org-dcst org-display-custom-times)
  9998. (ct (org-current-time))
  9999. (def (or org-overriding-default-time default-time ct))
  10000. (defdecode (decode-time def))
  10001. (dummy (progn
  10002. (when (< (nth 2 defdecode) org-extend-today-until)
  10003. (setcar (nthcdr 2 defdecode) -1)
  10004. (setcar (nthcdr 1 defdecode) 59)
  10005. (setq def (apply 'encode-time defdecode)
  10006. defdecode (decode-time def)))))
  10007. (calendar-move-hook nil)
  10008. (calendar-view-diary-initially-flag nil)
  10009. (view-diary-entries-initially nil)
  10010. (calendar-view-holidays-initially-flag nil)
  10011. (view-calendar-holidays-initially nil)
  10012. (timestr (format-time-string
  10013. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10014. (prompt (concat (if prompt (concat prompt " ") "")
  10015. (format "Date+time [%s]: " timestr)))
  10016. ans (org-ans0 "") org-ans1 org-ans2 final)
  10017. (cond
  10018. (from-string (setq ans from-string))
  10019. (org-read-date-popup-calendar
  10020. (save-excursion
  10021. (save-window-excursion
  10022. (calendar)
  10023. (calendar-forward-day (- (time-to-days def)
  10024. (calendar-absolute-from-gregorian
  10025. (calendar-current-date))))
  10026. (org-eval-in-calendar nil t)
  10027. (let* ((old-map (current-local-map))
  10028. (map (copy-keymap calendar-mode-map))
  10029. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10030. (org-defkey map (kbd "RET") 'org-calendar-select)
  10031. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10032. 'org-calendar-select-mouse)
  10033. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10034. 'org-calendar-select-mouse)
  10035. (org-defkey minibuffer-local-map [(meta shift left)]
  10036. (lambda () (interactive)
  10037. (org-eval-in-calendar '(calendar-backward-month 1))))
  10038. (org-defkey minibuffer-local-map [(meta shift right)]
  10039. (lambda () (interactive)
  10040. (org-eval-in-calendar '(calendar-forward-month 1))))
  10041. (org-defkey minibuffer-local-map [(meta shift up)]
  10042. (lambda () (interactive)
  10043. (org-eval-in-calendar '(calendar-backward-year 1))))
  10044. (org-defkey minibuffer-local-map [(meta shift down)]
  10045. (lambda () (interactive)
  10046. (org-eval-in-calendar '(calendar-forward-year 1))))
  10047. (org-defkey minibuffer-local-map [(shift up)]
  10048. (lambda () (interactive)
  10049. (org-eval-in-calendar '(calendar-backward-week 1))))
  10050. (org-defkey minibuffer-local-map [(shift down)]
  10051. (lambda () (interactive)
  10052. (org-eval-in-calendar '(calendar-forward-week 1))))
  10053. (org-defkey minibuffer-local-map [(shift left)]
  10054. (lambda () (interactive)
  10055. (org-eval-in-calendar '(calendar-backward-day 1))))
  10056. (org-defkey minibuffer-local-map [(shift right)]
  10057. (lambda () (interactive)
  10058. (org-eval-in-calendar '(calendar-forward-day 1))))
  10059. (org-defkey minibuffer-local-map ">"
  10060. (lambda () (interactive)
  10061. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10062. (org-defkey minibuffer-local-map "<"
  10063. (lambda () (interactive)
  10064. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10065. (unwind-protect
  10066. (progn
  10067. (use-local-map map)
  10068. (add-hook 'post-command-hook 'org-read-date-display)
  10069. (setq org-ans0 (read-string prompt default-input nil nil))
  10070. ;; org-ans0: from prompt
  10071. ;; org-ans1: from mouse click
  10072. ;; org-ans2: from calendar motion
  10073. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10074. (remove-hook 'post-command-hook 'org-read-date-display)
  10075. (use-local-map old-map)
  10076. (when org-read-date-overlay
  10077. (org-delete-overlay org-read-date-overlay)
  10078. (setq org-read-date-overlay nil)))))))
  10079. (t ; Naked prompt only
  10080. (unwind-protect
  10081. (setq ans (read-string prompt default-input nil timestr))
  10082. (when org-read-date-overlay
  10083. (org-delete-overlay org-read-date-overlay)
  10084. (setq org-read-date-overlay nil)))))
  10085. (setq final (org-read-date-analyze ans def defdecode))
  10086. (if to-time
  10087. (apply 'encode-time final)
  10088. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10089. (format "%04d-%02d-%02d %02d:%02d"
  10090. (nth 5 final) (nth 4 final) (nth 3 final)
  10091. (nth 2 final) (nth 1 final))
  10092. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10093. (defvar def)
  10094. (defvar defdecode)
  10095. (defvar with-time)
  10096. (defun org-read-date-display ()
  10097. "Display the currrent date prompt interpretation in the minibuffer."
  10098. (when org-read-date-display-live
  10099. (when org-read-date-overlay
  10100. (org-delete-overlay org-read-date-overlay))
  10101. (let ((p (point)))
  10102. (end-of-line 1)
  10103. (while (not (equal (buffer-substring
  10104. (max (point-min) (- (point) 4)) (point))
  10105. " "))
  10106. (insert " "))
  10107. (goto-char p))
  10108. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10109. " " (or org-ans1 org-ans2)))
  10110. (org-end-time-was-given nil)
  10111. (f (org-read-date-analyze ans def defdecode))
  10112. (fmts (if org-dcst
  10113. org-time-stamp-custom-formats
  10114. org-time-stamp-formats))
  10115. (fmt (if (or with-time
  10116. (and (boundp 'org-time-was-given) org-time-was-given))
  10117. (cdr fmts)
  10118. (car fmts)))
  10119. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10120. (when (and org-end-time-was-given
  10121. (string-match org-plain-time-of-day-regexp txt))
  10122. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10123. org-end-time-was-given
  10124. (substring txt (match-end 0)))))
  10125. (setq org-read-date-overlay
  10126. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10127. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10128. (defun org-read-date-analyze (ans def defdecode)
  10129. "Analyze the combined answer of the date prompt."
  10130. ;; FIXME: cleanup and comment
  10131. (let (delta deltan deltaw deltadef year month day
  10132. hour minute second wday pm h2 m2 tl wday1
  10133. iso-year iso-weekday iso-week iso-year iso-date)
  10134. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10135. (setq ans "+0"))
  10136. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10137. (setq ans (replace-match "" t t ans)
  10138. deltan (car delta)
  10139. deltaw (nth 1 delta)
  10140. deltadef (nth 2 delta)))
  10141. ;; Check if there is an iso week date in there
  10142. ;; If yes, sore the info and ostpone interpreting it until the rest
  10143. ;; of the parsing is done
  10144. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10145. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10146. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10147. iso-week (string-to-number (match-string 2 ans)))
  10148. (setq ans (replace-match "" t t ans)))
  10149. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10150. (when (string-match
  10151. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10152. (setq year (if (match-end 2)
  10153. (string-to-number (match-string 2 ans))
  10154. (string-to-number (format-time-string "%Y")))
  10155. month (string-to-number (match-string 3 ans))
  10156. day (string-to-number (match-string 4 ans)))
  10157. (if (< year 100) (setq year (+ 2000 year)))
  10158. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10159. t nil ans)))
  10160. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10161. ;; If there is a time with am/pm, and *no* time without it, we convert
  10162. ;; so that matching will be successful.
  10163. (loop for i from 1 to 2 do ; twice, for end time as well
  10164. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10165. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10166. (setq hour (string-to-number (match-string 1 ans))
  10167. minute (if (match-end 3)
  10168. (string-to-number (match-string 3 ans))
  10169. 0)
  10170. pm (equal ?p
  10171. (string-to-char (downcase (match-string 4 ans)))))
  10172. (if (and (= hour 12) (not pm))
  10173. (setq hour 0)
  10174. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10175. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10176. t t ans))))
  10177. ;; Check if a time range is given as a duration
  10178. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10179. (setq hour (string-to-number (match-string 1 ans))
  10180. h2 (+ hour (string-to-number (match-string 3 ans)))
  10181. minute (string-to-number (match-string 2 ans))
  10182. m2 (+ minute (if (match-end 5) (string-to-number
  10183. (match-string 5 ans))0)))
  10184. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10185. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10186. t t ans)))
  10187. ;; Check if there is a time range
  10188. (when (boundp 'org-end-time-was-given)
  10189. (setq org-time-was-given nil)
  10190. (when (and (string-match org-plain-time-of-day-regexp ans)
  10191. (match-end 8))
  10192. (setq org-end-time-was-given (match-string 8 ans))
  10193. (setq ans (concat (substring ans 0 (match-beginning 7))
  10194. (substring ans (match-end 7))))))
  10195. (setq tl (parse-time-string ans)
  10196. day (or (nth 3 tl) (nth 3 defdecode))
  10197. month (or (nth 4 tl)
  10198. (if (and org-read-date-prefer-future
  10199. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10200. (1+ (nth 4 defdecode))
  10201. (nth 4 defdecode)))
  10202. year (or (nth 5 tl)
  10203. (if (and org-read-date-prefer-future
  10204. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10205. (1+ (nth 5 defdecode))
  10206. (nth 5 defdecode)))
  10207. hour (or (nth 2 tl) (nth 2 defdecode))
  10208. minute (or (nth 1 tl) (nth 1 defdecode))
  10209. second (or (nth 0 tl) 0)
  10210. wday (nth 6 tl))
  10211. ;; Special date definitions below
  10212. (cond
  10213. (iso-week
  10214. ;; There was an iso week
  10215. (setq year (or iso-year year)
  10216. day (or iso-weekday wday 1)
  10217. wday nil ; to make sure that the trigger below does not match
  10218. iso-date (calendar-gregorian-from-absolute
  10219. (calendar-absolute-from-iso
  10220. (list iso-week day year))))
  10221. ; FIXME: Should we also push ISO weeks into the future?
  10222. ; (when (and org-read-date-prefer-future
  10223. ; (not iso-year)
  10224. ; (< (calendar-absolute-from-gregorian iso-date)
  10225. ; (time-to-days (current-time))))
  10226. ; (setq year (1+ year)
  10227. ; iso-date (calendar-gregorian-from-absolute
  10228. ; (calendar-absolute-from-iso
  10229. ; (list iso-week day year)))))
  10230. (setq month (car iso-date)
  10231. year (nth 2 iso-date)
  10232. day (nth 1 iso-date)))
  10233. (deltan
  10234. (unless deltadef
  10235. (let ((now (decode-time (current-time))))
  10236. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10237. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10238. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10239. ((equal deltaw "m") (setq month (+ month deltan)))
  10240. ((equal deltaw "y") (setq year (+ year deltan)))))
  10241. ((and wday (not (nth 3 tl)))
  10242. ;; Weekday was given, but no day, so pick that day in the week
  10243. ;; on or after the derived date.
  10244. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10245. (unless (equal wday wday1)
  10246. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10247. (if (and (boundp 'org-time-was-given)
  10248. (nth 2 tl))
  10249. (setq org-time-was-given t))
  10250. (if (< year 100) (setq year (+ 2000 year)))
  10251. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10252. (list second minute hour day month year)))
  10253. (defvar parse-time-weekdays)
  10254. (defun org-read-date-get-relative (s today default)
  10255. "Check string S for special relative date string.
  10256. TODAY and DEFAULT are internal times, for today and for a default.
  10257. Return shift list (N what def-flag)
  10258. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10259. N is the number of WHATs to shift.
  10260. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10261. the DEFAULT date rather than TODAY."
  10262. (when (and
  10263. (string-match
  10264. (concat
  10265. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10266. "\\([0-9]+\\)?"
  10267. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10268. "\\([ \t]\\|$\\)") s)
  10269. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10270. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10271. (string-to-char (substring (match-string 1 s) -1))
  10272. ?+))
  10273. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10274. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10275. (what (if (match-end 3) (match-string 3 s) "d"))
  10276. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10277. (date (if rel default today))
  10278. (wday (nth 6 (decode-time date)))
  10279. delta)
  10280. (if wday1
  10281. (progn
  10282. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10283. (if (= dir ?-) (setq delta (- delta 7)))
  10284. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10285. (list delta "d" rel))
  10286. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10287. (defun org-eval-in-calendar (form &optional keepdate)
  10288. "Eval FORM in the calendar window and return to current window.
  10289. Also, store the cursor date in variable org-ans2."
  10290. (let ((sw (selected-window)))
  10291. (select-window (get-buffer-window "*Calendar*"))
  10292. (eval form)
  10293. (when (and (not keepdate) (calendar-cursor-to-date))
  10294. (let* ((date (calendar-cursor-to-date))
  10295. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10296. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10297. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10298. (select-window sw)))
  10299. ; ;; Update the prompt to show new default date
  10300. ; (save-excursion
  10301. ; (goto-char (point-min))
  10302. ; (when (and org-ans2
  10303. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10304. ; (get-text-property (match-end 0) 'field))
  10305. ; (let ((inhibit-read-only t))
  10306. ; (replace-match (concat "[" org-ans2 "]") t t)
  10307. ; (add-text-properties (point-min) (1+ (match-end 0))
  10308. ; (text-properties-at (1+ (point-min)))))))))
  10309. (defun org-calendar-select ()
  10310. "Return to `org-read-date' with the date currently selected.
  10311. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10312. (interactive)
  10313. (when (calendar-cursor-to-date)
  10314. (let* ((date (calendar-cursor-to-date))
  10315. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10316. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10317. (if (active-minibuffer-window) (exit-minibuffer))))
  10318. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10319. "Insert a date stamp for the date given by the internal TIME.
  10320. WITH-HM means, use the stamp format that includes the time of the day.
  10321. INACTIVE means use square brackets instead of angular ones, so that the
  10322. stamp will not contribute to the agenda.
  10323. PRE and POST are optional strings to be inserted before and after the
  10324. stamp.
  10325. The command returns the inserted time stamp."
  10326. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10327. stamp)
  10328. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10329. (insert-before-markers (or pre ""))
  10330. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10331. (when (listp extra)
  10332. (setq extra (car extra))
  10333. (if (and (stringp extra)
  10334. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10335. (setq extra (format "-%02d:%02d"
  10336. (string-to-number (match-string 1 extra))
  10337. (string-to-number (match-string 2 extra))))
  10338. (setq extra nil)))
  10339. (when extra
  10340. (backward-char 1)
  10341. (insert-before-markers extra)
  10342. (forward-char 1))
  10343. (insert-before-markers (or post ""))
  10344. (setq org-last-inserted-timestamp stamp)))
  10345. (defun org-toggle-time-stamp-overlays ()
  10346. "Toggle the use of custom time stamp formats."
  10347. (interactive)
  10348. (setq org-display-custom-times (not org-display-custom-times))
  10349. (unless org-display-custom-times
  10350. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10351. (while (setq p (next-single-property-change p 'display))
  10352. (if (and (get-text-property p 'display)
  10353. (eq (get-text-property p 'face) 'org-date))
  10354. (remove-text-properties
  10355. p (setq p (next-single-property-change p 'display))
  10356. '(display t))))
  10357. (set-buffer-modified-p bmp)))
  10358. (if (featurep 'xemacs)
  10359. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10360. (org-restart-font-lock)
  10361. (setq org-table-may-need-update t)
  10362. (if org-display-custom-times
  10363. (message "Time stamps are overlayed with custom format")
  10364. (message "Time stamp overlays removed")))
  10365. (defun org-display-custom-time (beg end)
  10366. "Overlay modified time stamp format over timestamp between BEG and END."
  10367. (let* ((ts (buffer-substring beg end))
  10368. t1 w1 with-hm tf time str w2 (off 0))
  10369. (save-match-data
  10370. (setq t1 (org-parse-time-string ts t))
  10371. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10372. (setq off (- (match-end 0) (match-beginning 0)))))
  10373. (setq end (- end off))
  10374. (setq w1 (- end beg)
  10375. with-hm (and (nth 1 t1) (nth 2 t1))
  10376. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10377. time (org-fix-decoded-time t1)
  10378. str (org-add-props
  10379. (format-time-string
  10380. (substring tf 1 -1) (apply 'encode-time time))
  10381. nil 'mouse-face 'highlight)
  10382. w2 (length str))
  10383. (if (not (= w2 w1))
  10384. (add-text-properties (1+ beg) (+ 2 beg)
  10385. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10386. (if (featurep 'xemacs)
  10387. (progn
  10388. (put-text-property beg end 'invisible t)
  10389. (put-text-property beg end 'end-glyph (make-glyph str)))
  10390. (put-text-property beg end 'display str))))
  10391. (defun org-translate-time (string)
  10392. "Translate all timestamps in STRING to custom format.
  10393. But do this only if the variable `org-display-custom-times' is set."
  10394. (when org-display-custom-times
  10395. (save-match-data
  10396. (let* ((start 0)
  10397. (re org-ts-regexp-both)
  10398. t1 with-hm inactive tf time str beg end)
  10399. (while (setq start (string-match re string start))
  10400. (setq beg (match-beginning 0)
  10401. end (match-end 0)
  10402. t1 (save-match-data
  10403. (org-parse-time-string (substring string beg end) t))
  10404. with-hm (and (nth 1 t1) (nth 2 t1))
  10405. inactive (equal (substring string beg (1+ beg)) "[")
  10406. tf (funcall (if with-hm 'cdr 'car)
  10407. org-time-stamp-custom-formats)
  10408. time (org-fix-decoded-time t1)
  10409. str (format-time-string
  10410. (concat
  10411. (if inactive "[" "<") (substring tf 1 -1)
  10412. (if inactive "]" ">"))
  10413. (apply 'encode-time time))
  10414. string (replace-match str t t string)
  10415. start (+ start (length str)))))))
  10416. string)
  10417. (defun org-fix-decoded-time (time)
  10418. "Set 0 instead of nil for the first 6 elements of time.
  10419. Don't touch the rest."
  10420. (let ((n 0))
  10421. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10422. (defun org-days-to-time (timestamp-string)
  10423. "Difference between TIMESTAMP-STRING and now in days."
  10424. (- (time-to-days (org-time-string-to-time timestamp-string))
  10425. (time-to-days (current-time))))
  10426. (defun org-deadline-close (timestamp-string &optional ndays)
  10427. "Is the time in TIMESTAMP-STRING close to the current date?"
  10428. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10429. (and (< (org-days-to-time timestamp-string) ndays)
  10430. (not (org-entry-is-done-p))))
  10431. (defun org-get-wdays (ts)
  10432. "Get the deadline lead time appropriate for timestring TS."
  10433. (cond
  10434. ((<= org-deadline-warning-days 0)
  10435. ;; 0 or negative, enforce this value no matter what
  10436. (- org-deadline-warning-days))
  10437. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10438. ;; lead time is specified.
  10439. (floor (* (string-to-number (match-string 1 ts))
  10440. (cdr (assoc (match-string 2 ts)
  10441. '(("d" . 1) ("w" . 7)
  10442. ("m" . 30.4) ("y" . 365.25)))))))
  10443. ;; go for the default.
  10444. (t org-deadline-warning-days)))
  10445. (defun org-calendar-select-mouse (ev)
  10446. "Return to `org-read-date' with the date currently selected.
  10447. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10448. (interactive "e")
  10449. (mouse-set-point ev)
  10450. (when (calendar-cursor-to-date)
  10451. (let* ((date (calendar-cursor-to-date))
  10452. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10453. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10454. (if (active-minibuffer-window) (exit-minibuffer))))
  10455. (defun org-check-deadlines (ndays)
  10456. "Check if there are any deadlines due or past due.
  10457. A deadline is considered due if it happens within `org-deadline-warning-days'
  10458. days from today's date. If the deadline appears in an entry marked DONE,
  10459. it is not shown. The prefix arg NDAYS can be used to test that many
  10460. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10461. (interactive "P")
  10462. (let* ((org-warn-days
  10463. (cond
  10464. ((equal ndays '(4)) 100000)
  10465. (ndays (prefix-numeric-value ndays))
  10466. (t (abs org-deadline-warning-days))))
  10467. (case-fold-search nil)
  10468. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10469. (callback
  10470. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10471. (message "%d deadlines past-due or due within %d days"
  10472. (org-occur regexp nil callback)
  10473. org-warn-days)))
  10474. (defun org-check-before-date (date)
  10475. "Check if there are deadlines or scheduled entries before DATE."
  10476. (interactive (list (org-read-date)))
  10477. (let ((case-fold-search nil)
  10478. (regexp (concat "\\<\\(" org-deadline-string
  10479. "\\|" org-scheduled-string
  10480. "\\) *<\\([^>]+\\)>"))
  10481. (callback
  10482. (lambda () (time-less-p
  10483. (org-time-string-to-time (match-string 2))
  10484. (org-time-string-to-time date)))))
  10485. (message "%d entries before %s"
  10486. (org-occur regexp nil callback) date)))
  10487. (defun org-evaluate-time-range (&optional to-buffer)
  10488. "Evaluate a time range by computing the difference between start and end.
  10489. Normally the result is just printed in the echo area, but with prefix arg
  10490. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10491. If the time range is actually in a table, the result is inserted into the
  10492. next column.
  10493. For time difference computation, a year is assumed to be exactly 365
  10494. days in order to avoid rounding problems."
  10495. (interactive "P")
  10496. (or
  10497. (org-clock-update-time-maybe)
  10498. (save-excursion
  10499. (unless (org-at-date-range-p t)
  10500. (goto-char (point-at-bol))
  10501. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10502. (if (not (org-at-date-range-p t))
  10503. (error "Not at a time-stamp range, and none found in current line")))
  10504. (let* ((ts1 (match-string 1))
  10505. (ts2 (match-string 2))
  10506. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10507. (match-end (match-end 0))
  10508. (time1 (org-time-string-to-time ts1))
  10509. (time2 (org-time-string-to-time ts2))
  10510. (t1 (time-to-seconds time1))
  10511. (t2 (time-to-seconds time2))
  10512. (diff (abs (- t2 t1)))
  10513. (negative (< (- t2 t1) 0))
  10514. ;; (ys (floor (* 365 24 60 60)))
  10515. (ds (* 24 60 60))
  10516. (hs (* 60 60))
  10517. (fy "%dy %dd %02d:%02d")
  10518. (fy1 "%dy %dd")
  10519. (fd "%dd %02d:%02d")
  10520. (fd1 "%dd")
  10521. (fh "%02d:%02d")
  10522. y d h m align)
  10523. (if havetime
  10524. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10525. y 0
  10526. d (floor (/ diff ds)) diff (mod diff ds)
  10527. h (floor (/ diff hs)) diff (mod diff hs)
  10528. m (floor (/ diff 60)))
  10529. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10530. y 0
  10531. d (floor (+ (/ diff ds) 0.5))
  10532. h 0 m 0))
  10533. (if (not to-buffer)
  10534. (message "%s" (org-make-tdiff-string y d h m))
  10535. (if (org-at-table-p)
  10536. (progn
  10537. (goto-char match-end)
  10538. (setq align t)
  10539. (and (looking-at " *|") (goto-char (match-end 0))))
  10540. (goto-char match-end))
  10541. (if (looking-at
  10542. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10543. (replace-match ""))
  10544. (if negative (insert " -"))
  10545. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10546. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10547. (insert " " (format fh h m))))
  10548. (if align (org-table-align))
  10549. (message "Time difference inserted")))))
  10550. (defun org-make-tdiff-string (y d h m)
  10551. (let ((fmt "")
  10552. (l nil))
  10553. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10554. l (push y l)))
  10555. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10556. l (push d l)))
  10557. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10558. l (push h l)))
  10559. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10560. l (push m l)))
  10561. (apply 'format fmt (nreverse l))))
  10562. (defun org-time-string-to-time (s)
  10563. (apply 'encode-time (org-parse-time-string s)))
  10564. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10565. "Convert a time stamp to an absolute day number.
  10566. If there is a specifyer for a cyclic time stamp, get the closest date to
  10567. DAYNR.
  10568. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10569. (cond
  10570. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10571. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10572. daynr
  10573. (+ daynr 1000)))
  10574. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10575. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10576. (time-to-days (current-time))) (match-string 0 s)
  10577. prefer show-all))
  10578. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10579. (defun org-days-to-iso-week (days)
  10580. "Return the iso week number."
  10581. (require 'cal-iso)
  10582. (car (calendar-iso-from-absolute days)))
  10583. (defun org-small-year-to-year (year)
  10584. "Convert 2-digit years into 4-digit years.
  10585. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10586. The year 2000 cannot be abbreviated. Any year lager than 99
  10587. is retrned unchanged."
  10588. (if (< year 38)
  10589. (setq year (+ 2000 year))
  10590. (if (< year 100)
  10591. (setq year (+ 1900 year))))
  10592. year)
  10593. (defun org-time-from-absolute (d)
  10594. "Return the time corresponding to date D.
  10595. D may be an absolute day number, or a calendar-type list (month day year)."
  10596. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10597. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10598. (defun org-calendar-holiday ()
  10599. "List of holidays, for Diary display in Org-mode."
  10600. (require 'holidays)
  10601. (let ((hl (funcall
  10602. (if (fboundp 'calendar-check-holidays)
  10603. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10604. (if hl (mapconcat 'identity hl "; "))))
  10605. (defun org-diary-sexp-entry (sexp entry date)
  10606. "Process a SEXP diary ENTRY for DATE."
  10607. (require 'diary-lib)
  10608. (let ((result (if calendar-debug-sexp
  10609. (let ((stack-trace-on-error t))
  10610. (eval (car (read-from-string sexp))))
  10611. (condition-case nil
  10612. (eval (car (read-from-string sexp)))
  10613. (error
  10614. (beep)
  10615. (message "Bad sexp at line %d in %s: %s"
  10616. (org-current-line)
  10617. (buffer-file-name) sexp)
  10618. (sleep-for 2))))))
  10619. (cond ((stringp result) result)
  10620. ((and (consp result)
  10621. (stringp (cdr result))) (cdr result))
  10622. (result entry)
  10623. (t nil))))
  10624. (defun org-diary-to-ical-string (frombuf)
  10625. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10626. This uses the icalendar.el library."
  10627. (let* ((tmpdir (if (featurep 'xemacs)
  10628. (temp-directory)
  10629. temporary-file-directory))
  10630. (tmpfile (make-temp-name
  10631. (expand-file-name "orgics" tmpdir)))
  10632. buf rtn b e)
  10633. (save-excursion
  10634. (set-buffer frombuf)
  10635. (icalendar-export-region (point-min) (point-max) tmpfile)
  10636. (setq buf (find-buffer-visiting tmpfile))
  10637. (set-buffer buf)
  10638. (goto-char (point-min))
  10639. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10640. (setq b (match-beginning 0)))
  10641. (goto-char (point-max))
  10642. (if (re-search-backward "^END:VEVENT" nil t)
  10643. (setq e (match-end 0)))
  10644. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10645. (kill-buffer buf)
  10646. (delete-file tmpfile)
  10647. rtn))
  10648. (defun org-closest-date (start current change prefer show-all)
  10649. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10650. When PREFER is `past' return a date that is either CURRENT or past.
  10651. When PREFER is `future', return a date that is either CURRENT or future.
  10652. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10653. ;; Make the proper lists from the dates
  10654. (catch 'exit
  10655. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10656. dn dw sday cday n1 n2
  10657. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10658. (setq start (org-date-to-gregorian start)
  10659. current (org-date-to-gregorian
  10660. (if show-all
  10661. current
  10662. (time-to-days (current-time))))
  10663. sday (calendar-absolute-from-gregorian start)
  10664. cday (calendar-absolute-from-gregorian current))
  10665. (if (<= cday sday) (throw 'exit sday))
  10666. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10667. (setq dn (string-to-number (match-string 1 change))
  10668. dw (cdr (assoc (match-string 2 change) a1)))
  10669. (error "Invalid change specifyer: %s" change))
  10670. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10671. (cond
  10672. ((eq dw 'day)
  10673. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10674. n2 (+ n1 dn)))
  10675. ((eq dw 'year)
  10676. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10677. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10678. (setq date1 (list m d y1)
  10679. n1 (calendar-absolute-from-gregorian date1)
  10680. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10681. n2 (calendar-absolute-from-gregorian date2)))
  10682. ((eq dw 'month)
  10683. ;; approx number of month between the two dates
  10684. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10685. ;; How often does dn fit in there?
  10686. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10687. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10688. m (+ m nm)
  10689. ny (floor (/ m 12))
  10690. y (+ y ny)
  10691. m (- m (* ny 12)))
  10692. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10693. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10694. (setq m2 (+ m dn) y2 y)
  10695. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10696. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10697. (while (<= n2 cday)
  10698. (setq n1 n2 m m2 y y2)
  10699. (setq m2 (+ m dn) y2 y)
  10700. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10701. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10702. (if show-all
  10703. (cond
  10704. ((eq prefer 'past) n1)
  10705. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10706. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10707. (cond
  10708. ((eq prefer 'past) n1)
  10709. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10710. (t (if (= cday n1) n1 n2)))))))
  10711. (defun org-date-to-gregorian (date)
  10712. "Turn any specification of DATE into a gregorian date for the calendar."
  10713. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10714. ((and (listp date) (= (length date) 3)) date)
  10715. ((stringp date)
  10716. (setq date (org-parse-time-string date))
  10717. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10718. ((listp date)
  10719. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10720. (defun org-parse-time-string (s &optional nodefault)
  10721. "Parse the standard Org-mode time string.
  10722. This should be a lot faster than the normal `parse-time-string'.
  10723. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10724. hour and minute fields will be nil if not given."
  10725. (if (string-match org-ts-regexp0 s)
  10726. (list 0
  10727. (if (or (match-beginning 8) (not nodefault))
  10728. (string-to-number (or (match-string 8 s) "0")))
  10729. (if (or (match-beginning 7) (not nodefault))
  10730. (string-to-number (or (match-string 7 s) "0")))
  10731. (string-to-number (match-string 4 s))
  10732. (string-to-number (match-string 3 s))
  10733. (string-to-number (match-string 2 s))
  10734. nil nil nil)
  10735. (make-list 9 0)))
  10736. (defun org-timestamp-up (&optional arg)
  10737. "Increase the date item at the cursor by one.
  10738. If the cursor is on the year, change the year. If it is on the month or
  10739. the day, change that.
  10740. With prefix ARG, change by that many units."
  10741. (interactive "p")
  10742. (org-timestamp-change (prefix-numeric-value arg)))
  10743. (defun org-timestamp-down (&optional arg)
  10744. "Decrease the date item at the cursor by one.
  10745. If the cursor is on the year, change the year. If it is on the month or
  10746. the day, change that.
  10747. With prefix ARG, change by that many units."
  10748. (interactive "p")
  10749. (org-timestamp-change (- (prefix-numeric-value arg))))
  10750. (defun org-timestamp-up-day (&optional arg)
  10751. "Increase the date in the time stamp by one day.
  10752. With prefix ARG, change that many days."
  10753. (interactive "p")
  10754. (if (and (not (org-at-timestamp-p t))
  10755. (org-on-heading-p))
  10756. (org-todo 'up)
  10757. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10758. (defun org-timestamp-down-day (&optional arg)
  10759. "Decrease the date in the time stamp by one day.
  10760. With prefix ARG, change that many days."
  10761. (interactive "p")
  10762. (if (and (not (org-at-timestamp-p t))
  10763. (org-on-heading-p))
  10764. (org-todo 'down)
  10765. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10766. (defun org-at-timestamp-p (&optional inactive-ok)
  10767. "Determine if the cursor is in or at a timestamp."
  10768. (interactive)
  10769. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10770. (pos (point))
  10771. (ans (or (looking-at tsr)
  10772. (save-excursion
  10773. (skip-chars-backward "^[<\n\r\t")
  10774. (if (> (point) (point-min)) (backward-char 1))
  10775. (and (looking-at tsr)
  10776. (> (- (match-end 0) pos) -1))))))
  10777. (and ans
  10778. (boundp 'org-ts-what)
  10779. (setq org-ts-what
  10780. (cond
  10781. ((= pos (match-beginning 0)) 'bracket)
  10782. ((= pos (1- (match-end 0))) 'bracket)
  10783. ((org-pos-in-match-range pos 2) 'year)
  10784. ((org-pos-in-match-range pos 3) 'month)
  10785. ((org-pos-in-match-range pos 7) 'hour)
  10786. ((org-pos-in-match-range pos 8) 'minute)
  10787. ((or (org-pos-in-match-range pos 4)
  10788. (org-pos-in-match-range pos 5)) 'day)
  10789. ((and (> pos (or (match-end 8) (match-end 5)))
  10790. (< pos (match-end 0)))
  10791. (- pos (or (match-end 8) (match-end 5))))
  10792. (t 'day))))
  10793. ans))
  10794. (defun org-toggle-timestamp-type ()
  10795. "Toggle the type (<active> or [inactive]) of a time stamp."
  10796. (interactive)
  10797. (when (org-at-timestamp-p t)
  10798. (save-excursion
  10799. (goto-char (match-beginning 0))
  10800. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10801. (goto-char (1- (match-end 0)))
  10802. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10803. (message "Timestamp is now %sactive"
  10804. (if (equal (char-before) ?>) "in" ""))))
  10805. (defun org-timestamp-change (n &optional what)
  10806. "Change the date in the time stamp at point.
  10807. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10808. `year', `minute', `second'. If WHAT is not given, the cursor position
  10809. in the timestamp determines what will be changed."
  10810. (let ((pos (point))
  10811. with-hm inactive
  10812. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10813. org-ts-what
  10814. extra rem
  10815. ts time time0)
  10816. (if (not (org-at-timestamp-p t))
  10817. (error "Not at a timestamp"))
  10818. (if (and (not what) (eq org-ts-what 'bracket))
  10819. (org-toggle-timestamp-type)
  10820. (if (and (not what) (not (eq org-ts-what 'day))
  10821. org-display-custom-times
  10822. (get-text-property (point) 'display)
  10823. (not (get-text-property (1- (point)) 'display)))
  10824. (setq org-ts-what 'day))
  10825. (setq org-ts-what (or what org-ts-what)
  10826. inactive (= (char-after (match-beginning 0)) ?\[)
  10827. ts (match-string 0))
  10828. (replace-match "")
  10829. (if (string-match
  10830. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10831. ts)
  10832. (setq extra (match-string 1 ts)))
  10833. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10834. (setq with-hm t))
  10835. (setq time0 (org-parse-time-string ts))
  10836. (when (and (eq org-ts-what 'minute)
  10837. (eq current-prefix-arg nil))
  10838. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10839. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10840. (setcar (cdr time0) (+ (nth 1 time0)
  10841. (if (> n 0) (- rem) (- dm rem))))))
  10842. (setq time
  10843. (encode-time (or (car time0) 0)
  10844. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10845. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10846. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10847. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10848. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10849. (nthcdr 6 time0)))
  10850. (when (integerp org-ts-what)
  10851. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10852. (if (eq what 'calendar)
  10853. (let ((cal-date (org-get-date-from-calendar)))
  10854. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10855. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10856. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10857. (setcar time0 (or (car time0) 0))
  10858. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10859. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10860. (setq time (apply 'encode-time time0))))
  10861. (setq org-last-changed-timestamp
  10862. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10863. (org-clock-update-time-maybe)
  10864. (goto-char pos)
  10865. ;; Try to recenter the calendar window, if any
  10866. (if (and org-calendar-follow-timestamp-change
  10867. (get-buffer-window "*Calendar*" t)
  10868. (memq org-ts-what '(day month year)))
  10869. (org-recenter-calendar (time-to-days time))))))
  10870. (defun org-modify-ts-extra (s pos n dm)
  10871. "Change the different parts of the lead-time and repeat fields in timestamp."
  10872. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10873. ng h m new rem)
  10874. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10875. (cond
  10876. ((or (org-pos-in-match-range pos 2)
  10877. (org-pos-in-match-range pos 3))
  10878. (setq m (string-to-number (match-string 3 s))
  10879. h (string-to-number (match-string 2 s)))
  10880. (if (org-pos-in-match-range pos 2)
  10881. (setq h (+ h n))
  10882. (setq n (* dm (org-no-warnings (signum n))))
  10883. (when (not (= 0 (setq rem (% m dm))))
  10884. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10885. (setq m (+ m n)))
  10886. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10887. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10888. (setq h (min 24 (max 0 h)))
  10889. (setq ng 1 new (format "-%02d:%02d" h m)))
  10890. ((org-pos-in-match-range pos 6)
  10891. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10892. ((org-pos-in-match-range pos 5)
  10893. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10894. ((org-pos-in-match-range pos 9)
  10895. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10896. ((org-pos-in-match-range pos 8)
  10897. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10898. (when ng
  10899. (setq s (concat
  10900. (substring s 0 (match-beginning ng))
  10901. new
  10902. (substring s (match-end ng))))))
  10903. s))
  10904. (defun org-recenter-calendar (date)
  10905. "If the calendar is visible, recenter it to DATE."
  10906. (let* ((win (selected-window))
  10907. (cwin (get-buffer-window "*Calendar*" t))
  10908. (calendar-move-hook nil))
  10909. (when cwin
  10910. (select-window cwin)
  10911. (calendar-goto-date (if (listp date) date
  10912. (calendar-gregorian-from-absolute date)))
  10913. (select-window win))))
  10914. (defun org-goto-calendar (&optional arg)
  10915. "Go to the Emacs calendar at the current date.
  10916. If there is a time stamp in the current line, go to that date.
  10917. A prefix ARG can be used to force the current date."
  10918. (interactive "P")
  10919. (let ((tsr org-ts-regexp) diff
  10920. (calendar-move-hook nil)
  10921. (calendar-view-holidays-initially-flag nil)
  10922. (view-calendar-holidays-initially nil)
  10923. (calendar-view-diary-initially-flag nil)
  10924. (view-diary-entries-initially nil))
  10925. (if (or (org-at-timestamp-p)
  10926. (save-excursion
  10927. (beginning-of-line 1)
  10928. (looking-at (concat ".*" tsr))))
  10929. (let ((d1 (time-to-days (current-time)))
  10930. (d2 (time-to-days
  10931. (org-time-string-to-time (match-string 1)))))
  10932. (setq diff (- d2 d1))))
  10933. (calendar)
  10934. (calendar-goto-today)
  10935. (if (and diff (not arg)) (calendar-forward-day diff))))
  10936. (defun org-get-date-from-calendar ()
  10937. "Return a list (month day year) of date at point in calendar."
  10938. (with-current-buffer "*Calendar*"
  10939. (save-match-data
  10940. (calendar-cursor-to-date))))
  10941. (defun org-date-from-calendar ()
  10942. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10943. If there is already a time stamp at the cursor position, update it."
  10944. (interactive)
  10945. (if (org-at-timestamp-p t)
  10946. (org-timestamp-change 0 'calendar)
  10947. (let ((cal-date (org-get-date-from-calendar)))
  10948. (org-insert-time-stamp
  10949. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10950. (defun org-minutes-to-hh:mm-string (m)
  10951. "Compute H:MM from a number of minutes."
  10952. (let ((h (/ m 60)))
  10953. (setq m (- m (* 60 h)))
  10954. (format org-time-clocksum-format h m)))
  10955. (defun org-hh:mm-string-to-minutes (s)
  10956. "Convert a string H:MM to a number of minutes."
  10957. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10958. (+ (* (string-to-number (match-string 1 s)) 60)
  10959. (string-to-number (match-string 2 s)))
  10960. 0))
  10961. ;;;; Agenda files
  10962. ;;;###autoload
  10963. (defun org-iswitchb (&optional arg)
  10964. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10965. With a prefix argument, restrict available to files.
  10966. With two prefix arguments, restrict available buffers to agenda files.
  10967. Due to some yet unresolved reason, the global function
  10968. `iswitchb-mode' needs to be active for this function to work."
  10969. (interactive "P")
  10970. (require 'iswitchb)
  10971. (let ((enabled iswitchb-mode) blist)
  10972. (or enabled (iswitchb-mode 1))
  10973. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10974. ((equal arg '(16)) (org-buffer-list 'agenda))
  10975. (t (org-buffer-list))))
  10976. (unwind-protect
  10977. (let ((iswitchb-make-buflist-hook
  10978. (lambda ()
  10979. (setq iswitchb-temp-buflist
  10980. (mapcar 'buffer-name blist)))))
  10981. (switch-to-buffer
  10982. (iswitchb-read-buffer
  10983. "Switch-to: " nil t))
  10984. (or enabled (iswitchb-mode -1))))))
  10985. (defun org-buffer-list (&optional predicate exclude-tmp)
  10986. "Return a list of Org buffers.
  10987. PREDICATE can be `export', `files' or `agenda'.
  10988. export restrict the list to Export buffers.
  10989. files restrict the list to buffers visiting Org files.
  10990. agenda restrict the list to buffers visiting agenda files.
  10991. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10992. (let* ((bfn nil)
  10993. (agenda-files (and (eq predicate 'agenda)
  10994. (mapcar 'file-truename (org-agenda-files t))))
  10995. (filter
  10996. (cond
  10997. ((eq predicate 'files)
  10998. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10999. ((eq predicate 'export)
  11000. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11001. ((eq predicate 'agenda)
  11002. (lambda (b)
  11003. (with-current-buffer b
  11004. (and (eq major-mode 'org-mode)
  11005. (setq bfn (buffer-file-name b))
  11006. (member (file-truename bfn) agenda-files)))))
  11007. (t (lambda (b) (with-current-buffer b
  11008. (or (eq major-mode 'org-mode)
  11009. (string-match "\*Org .*Export"
  11010. (buffer-name b)))))))))
  11011. (delq nil
  11012. (mapcar
  11013. (lambda(b)
  11014. (if (and (funcall filter b)
  11015. (or (not exclude-tmp)
  11016. (not (string-match "tmp" (buffer-name b)))))
  11017. b
  11018. nil))
  11019. (buffer-list)))))
  11020. (defun org-agenda-files (&optional unrestricted archives)
  11021. "Get the list of agenda files.
  11022. Optional UNRESTRICTED means return the full list even if a restriction
  11023. is currently in place.
  11024. When ARCHIVES is t, include all archive files hat are really being
  11025. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11026. `org-agenda-archives-mode' is t."
  11027. (let ((files
  11028. (cond
  11029. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11030. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11031. ((listp org-agenda-files) org-agenda-files)
  11032. (t (error "Invalid value of `org-agenda-files'")))))
  11033. (setq files (apply 'append
  11034. (mapcar (lambda (f)
  11035. (if (file-directory-p f)
  11036. (directory-files
  11037. f t org-agenda-file-regexp)
  11038. (list f)))
  11039. files)))
  11040. (when org-agenda-skip-unavailable-files
  11041. (setq files (delq nil
  11042. (mapcar (function
  11043. (lambda (file)
  11044. (and (file-readable-p file) file)))
  11045. files))))
  11046. (when (or (eq archives t)
  11047. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11048. (setq files (org-add-archive-files files)))
  11049. files))
  11050. (defun org-edit-agenda-file-list ()
  11051. "Edit the list of agenda files.
  11052. Depending on setup, this either uses customize to edit the variable
  11053. `org-agenda-files', or it visits the file that is holding the list. In the
  11054. latter case, the buffer is set up in a way that saving it automatically kills
  11055. the buffer and restores the previous window configuration."
  11056. (interactive)
  11057. (if (stringp org-agenda-files)
  11058. (let ((cw (current-window-configuration)))
  11059. (find-file org-agenda-files)
  11060. (org-set-local 'org-window-configuration cw)
  11061. (org-add-hook 'after-save-hook
  11062. (lambda ()
  11063. (set-window-configuration
  11064. (prog1 org-window-configuration
  11065. (kill-buffer (current-buffer))))
  11066. (org-install-agenda-files-menu)
  11067. (message "New agenda file list installed"))
  11068. nil 'local)
  11069. (message "%s" (substitute-command-keys
  11070. "Edit list and finish with \\[save-buffer]")))
  11071. (customize-variable 'org-agenda-files)))
  11072. (defun org-store-new-agenda-file-list (list)
  11073. "Set new value for the agenda file list and save it correcly."
  11074. (if (stringp org-agenda-files)
  11075. (let ((f org-agenda-files) b)
  11076. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11077. (with-temp-file f
  11078. (insert (mapconcat 'identity list "\n") "\n")))
  11079. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11080. (setq org-agenda-files list)
  11081. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11082. (defun org-read-agenda-file-list ()
  11083. "Read the list of agenda files from a file."
  11084. (when (file-directory-p org-agenda-files)
  11085. (error "`org-agenda-files' cannot be a single directory"))
  11086. (when (stringp org-agenda-files)
  11087. (with-temp-buffer
  11088. (insert-file-contents org-agenda-files)
  11089. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11090. ;;;###autoload
  11091. (defun org-cycle-agenda-files ()
  11092. "Cycle through the files in `org-agenda-files'.
  11093. If the current buffer visits an agenda file, find the next one in the list.
  11094. If the current buffer does not, find the first agenda file."
  11095. (interactive)
  11096. (let* ((fs (org-agenda-files t))
  11097. (files (append fs (list (car fs))))
  11098. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11099. file)
  11100. (unless files (error "No agenda files"))
  11101. (catch 'exit
  11102. (while (setq file (pop files))
  11103. (if (equal (file-truename file) tcf)
  11104. (when (car files)
  11105. (find-file (car files))
  11106. (throw 'exit t))))
  11107. (find-file (car fs)))
  11108. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11109. (defun org-agenda-file-to-front (&optional to-end)
  11110. "Move/add the current file to the top of the agenda file list.
  11111. If the file is not present in the list, it is added to the front. If it is
  11112. present, it is moved there. With optional argument TO-END, add/move to the
  11113. end of the list."
  11114. (interactive "P")
  11115. (let ((org-agenda-skip-unavailable-files nil)
  11116. (file-alist (mapcar (lambda (x)
  11117. (cons (file-truename x) x))
  11118. (org-agenda-files t)))
  11119. (ctf (file-truename buffer-file-name))
  11120. x had)
  11121. (setq x (assoc ctf file-alist) had x)
  11122. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11123. (if to-end
  11124. (setq file-alist (append (delq x file-alist) (list x)))
  11125. (setq file-alist (cons x (delq x file-alist))))
  11126. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11127. (org-install-agenda-files-menu)
  11128. (message "File %s to %s of agenda file list"
  11129. (if had "moved" "added") (if to-end "end" "front"))))
  11130. (defun org-remove-file (&optional file)
  11131. "Remove current file from the list of files in variable `org-agenda-files'.
  11132. These are the files which are being checked for agenda entries.
  11133. Optional argument FILE means, use this file instead of the current."
  11134. (interactive)
  11135. (let* ((org-agenda-skip-unavailable-files nil)
  11136. (file (or file buffer-file-name))
  11137. (true-file (file-truename file))
  11138. (afile (abbreviate-file-name file))
  11139. (files (delq nil (mapcar
  11140. (lambda (x)
  11141. (if (equal true-file
  11142. (file-truename x))
  11143. nil x))
  11144. (org-agenda-files t)))))
  11145. (if (not (= (length files) (length (org-agenda-files t))))
  11146. (progn
  11147. (org-store-new-agenda-file-list files)
  11148. (org-install-agenda-files-menu)
  11149. (message "Removed file: %s" afile))
  11150. (message "File was not in list: %s (not removed)" afile))))
  11151. (defun org-file-menu-entry (file)
  11152. (vector file (list 'find-file file) t))
  11153. (defun org-check-agenda-file (file)
  11154. "Make sure FILE exists. If not, ask user what to do."
  11155. (when (not (file-exists-p file))
  11156. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11157. (abbreviate-file-name file))
  11158. (let ((r (downcase (read-char-exclusive))))
  11159. (cond
  11160. ((equal r ?r)
  11161. (org-remove-file file)
  11162. (throw 'nextfile t))
  11163. (t (error "Abort"))))))
  11164. (defun org-get-agenda-file-buffer (file)
  11165. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11166. it to the list of buffers which might be released later."
  11167. (let ((buf (org-find-base-buffer-visiting file)))
  11168. (if buf
  11169. buf ; just return it
  11170. ;; Make a new buffer and remember it
  11171. (setq buf (find-file-noselect file))
  11172. (if buf (push buf org-agenda-new-buffers))
  11173. buf)))
  11174. (defun org-release-buffers (blist)
  11175. "Release all buffers in list, asking the user for confirmation when needed.
  11176. When a buffer is unmodified, it is just killed. When modified, it is saved
  11177. \(if the user agrees) and then killed."
  11178. (let (buf file)
  11179. (while (setq buf (pop blist))
  11180. (setq file (buffer-file-name buf))
  11181. (when (and (buffer-modified-p buf)
  11182. file
  11183. (y-or-n-p (format "Save file %s? " file)))
  11184. (with-current-buffer buf (save-buffer)))
  11185. (kill-buffer buf))))
  11186. (defun org-prepare-agenda-buffers (files)
  11187. "Create buffers for all agenda files, protect archived trees and comments."
  11188. (interactive)
  11189. (let ((pa '(:org-archived t))
  11190. (pc '(:org-comment t))
  11191. (pall '(:org-archived t :org-comment t))
  11192. (inhibit-read-only t)
  11193. (rea (concat ":" org-archive-tag ":"))
  11194. bmp file re)
  11195. (save-excursion
  11196. (save-restriction
  11197. (while (setq file (pop files))
  11198. (if (bufferp file)
  11199. (set-buffer file)
  11200. (org-check-agenda-file file)
  11201. (set-buffer (org-get-agenda-file-buffer file)))
  11202. (widen)
  11203. (setq bmp (buffer-modified-p))
  11204. (org-refresh-category-properties)
  11205. (setq org-todo-keywords-for-agenda
  11206. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11207. (setq org-done-keywords-for-agenda
  11208. (append org-done-keywords-for-agenda org-done-keywords))
  11209. (setq org-todo-keyword-alist-for-agenda
  11210. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11211. (setq org-tag-alist-for-agenda
  11212. (append org-tag-alist-for-agenda org-tag-alist))
  11213. (save-excursion
  11214. (remove-text-properties (point-min) (point-max) pall)
  11215. (when org-agenda-skip-archived-trees
  11216. (goto-char (point-min))
  11217. (while (re-search-forward rea nil t)
  11218. (if (org-on-heading-p t)
  11219. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11220. (goto-char (point-min))
  11221. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11222. (while (re-search-forward re nil t)
  11223. (add-text-properties
  11224. (match-beginning 0) (org-end-of-subtree t) pc)))
  11225. (set-buffer-modified-p bmp))))
  11226. (setq debug-on-error t)
  11227. (setq org-todo-keyword-alist-for-agenda
  11228. (org-uniquify org-todo-keyword-alist-for-agenda)
  11229. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11230. ;;;; Embedded LaTeX
  11231. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11232. "Keymap for the minor `org-cdlatex-mode'.")
  11233. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11234. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11235. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11236. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11237. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11238. (defvar org-cdlatex-texmathp-advice-is-done nil
  11239. "Flag remembering if we have applied the advice to texmathp already.")
  11240. (define-minor-mode org-cdlatex-mode
  11241. "Toggle the minor `org-cdlatex-mode'.
  11242. This mode supports entering LaTeX environment and math in LaTeX fragments
  11243. in Org-mode.
  11244. \\{org-cdlatex-mode-map}"
  11245. nil " OCDL" nil
  11246. (when org-cdlatex-mode (require 'cdlatex))
  11247. (unless org-cdlatex-texmathp-advice-is-done
  11248. (setq org-cdlatex-texmathp-advice-is-done t)
  11249. (defadvice texmathp (around org-math-always-on activate)
  11250. "Always return t in org-mode buffers.
  11251. This is because we want to insert math symbols without dollars even outside
  11252. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11253. en embedded LaTeX fragement, let texmathp do its job.
  11254. \\[org-cdlatex-mode-map]"
  11255. (interactive)
  11256. (let (p)
  11257. (cond
  11258. ((not (org-mode-p)) ad-do-it)
  11259. ((eq this-command 'cdlatex-math-symbol)
  11260. (setq ad-return-value t
  11261. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11262. (t
  11263. (let ((p (org-inside-LaTeX-fragment-p)))
  11264. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11265. (setq ad-return-value t
  11266. texmathp-why '("Org-mode embedded math" . 0))
  11267. (if p ad-do-it)))))))))
  11268. (defun turn-on-org-cdlatex ()
  11269. "Unconditionally turn on `org-cdlatex-mode'."
  11270. (org-cdlatex-mode 1))
  11271. (defun org-inside-LaTeX-fragment-p ()
  11272. "Test if point is inside a LaTeX fragment.
  11273. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11274. sequence appearing also before point.
  11275. Even though the matchers for math are configurable, this function assumes
  11276. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11277. delimiters are skipped when they have been removed by customization.
  11278. The return value is nil, or a cons cell with the delimiter and
  11279. and the position of this delimiter.
  11280. This function does a reasonably good job, but can locally be fooled by
  11281. for example currency specifications. For example it will assume being in
  11282. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11283. fragments that are properly closed, but during editing, we have to live
  11284. with the uncertainty caused by missing closing delimiters. This function
  11285. looks only before point, not after."
  11286. (catch 'exit
  11287. (let ((pos (point))
  11288. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11289. (lim (progn
  11290. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11291. (point)))
  11292. dd-on str (start 0) m re)
  11293. (goto-char pos)
  11294. (when dodollar
  11295. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11296. re (nth 1 (assoc "$" org-latex-regexps)))
  11297. (while (string-match re str start)
  11298. (cond
  11299. ((= (match-end 0) (length str))
  11300. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11301. ((= (match-end 0) (- (length str) 5))
  11302. (throw 'exit nil))
  11303. (t (setq start (match-end 0))))))
  11304. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11305. (goto-char pos)
  11306. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11307. (and (match-beginning 2) (throw 'exit nil))
  11308. ;; count $$
  11309. (while (re-search-backward "\\$\\$" lim t)
  11310. (setq dd-on (not dd-on)))
  11311. (goto-char pos)
  11312. (if dd-on (cons "$$" m))))))
  11313. (defun org-try-cdlatex-tab ()
  11314. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11315. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11316. - inside a LaTeX fragment, or
  11317. - after the first word in a line, where an abbreviation expansion could
  11318. insert a LaTeX environment."
  11319. (when org-cdlatex-mode
  11320. (cond
  11321. ((save-excursion
  11322. (skip-chars-backward "a-zA-Z0-9*")
  11323. (skip-chars-backward " \t")
  11324. (bolp))
  11325. (cdlatex-tab) t)
  11326. ((org-inside-LaTeX-fragment-p)
  11327. (cdlatex-tab) t)
  11328. (t nil))))
  11329. (defun org-cdlatex-underscore-caret (&optional arg)
  11330. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11331. Revert to the normal definition outside of these fragments."
  11332. (interactive "P")
  11333. (if (org-inside-LaTeX-fragment-p)
  11334. (call-interactively 'cdlatex-sub-superscript)
  11335. (let (org-cdlatex-mode)
  11336. (call-interactively (key-binding (vector last-input-event))))))
  11337. (defun org-cdlatex-math-modify (&optional arg)
  11338. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11339. Revert to the normal definition outside of these fragments."
  11340. (interactive "P")
  11341. (if (org-inside-LaTeX-fragment-p)
  11342. (call-interactively 'cdlatex-math-modify)
  11343. (let (org-cdlatex-mode)
  11344. (call-interactively (key-binding (vector last-input-event))))))
  11345. (defvar org-latex-fragment-image-overlays nil
  11346. "List of overlays carrying the images of latex fragments.")
  11347. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11348. (defun org-remove-latex-fragment-image-overlays ()
  11349. "Remove all overlays with LaTeX fragment images in current buffer."
  11350. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11351. (setq org-latex-fragment-image-overlays nil))
  11352. (defun org-preview-latex-fragment (&optional subtree)
  11353. "Preview the LaTeX fragment at point, or all locally or globally.
  11354. If the cursor is in a LaTeX fragment, create the image and overlay
  11355. it over the source code. If there is no fragment at point, display
  11356. all fragments in the current text, from one headline to the next. With
  11357. prefix SUBTREE, display all fragments in the current subtree. With a
  11358. double prefix `C-u C-u', or when the cursor is before the first headline,
  11359. display all fragments in the buffer.
  11360. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11361. (interactive "P")
  11362. (org-remove-latex-fragment-image-overlays)
  11363. (save-excursion
  11364. (save-restriction
  11365. (let (beg end at msg)
  11366. (cond
  11367. ((or (equal subtree '(16))
  11368. (not (save-excursion
  11369. (re-search-backward (concat "^" outline-regexp) nil t))))
  11370. (setq beg (point-min) end (point-max)
  11371. msg "Creating images for buffer...%s"))
  11372. ((equal subtree '(4))
  11373. (org-back-to-heading)
  11374. (setq beg (point) end (org-end-of-subtree t)
  11375. msg "Creating images for subtree...%s"))
  11376. (t
  11377. (if (setq at (org-inside-LaTeX-fragment-p))
  11378. (goto-char (max (point-min) (- (cdr at) 2)))
  11379. (org-back-to-heading))
  11380. (setq beg (point) end (progn (outline-next-heading) (point))
  11381. msg (if at "Creating image...%s"
  11382. "Creating images for entry...%s"))))
  11383. (message msg "")
  11384. (narrow-to-region beg end)
  11385. (goto-char beg)
  11386. (org-format-latex
  11387. (concat "ltxpng/" (file-name-sans-extension
  11388. (file-name-nondirectory
  11389. buffer-file-name)))
  11390. default-directory 'overlays msg at 'forbuffer)
  11391. (message msg "done. Use `C-c C-c' to remove images.")))))
  11392. (defvar org-latex-regexps
  11393. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11394. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11395. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11396. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11397. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11398. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11399. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11400. "Regular expressions for matching embedded LaTeX.")
  11401. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11402. "Replace LaTeX fragments with links to an image, and produce images."
  11403. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11404. (let* ((prefixnodir (file-name-nondirectory prefix))
  11405. (absprefix (expand-file-name prefix dir))
  11406. (todir (file-name-directory absprefix))
  11407. (opt org-format-latex-options)
  11408. (matchers (plist-get opt :matchers))
  11409. (re-list org-latex-regexps)
  11410. (cnt 0) txt link beg end re e checkdir
  11411. m n block linkfile movefile ov)
  11412. ;; Check if there are old images files with this prefix, and remove them
  11413. (when (file-directory-p todir)
  11414. (mapc 'delete-file
  11415. (directory-files
  11416. todir 'full
  11417. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11418. ;; Check the different regular expressions
  11419. (while (setq e (pop re-list))
  11420. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11421. block (if (nth 3 e) "\n\n" ""))
  11422. (when (member m matchers)
  11423. (goto-char (point-min))
  11424. (while (re-search-forward re nil t)
  11425. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11426. (setq txt (match-string n)
  11427. beg (match-beginning n) end (match-end n)
  11428. cnt (1+ cnt)
  11429. linkfile (format "%s_%04d.png" prefix cnt)
  11430. movefile (format "%s_%04d.png" absprefix cnt)
  11431. link (concat block "[[file:" linkfile "]]" block))
  11432. (if msg (message msg cnt))
  11433. (goto-char beg)
  11434. (unless checkdir ; make sure the directory exists
  11435. (setq checkdir t)
  11436. (or (file-directory-p todir) (make-directory todir)))
  11437. (org-create-formula-image
  11438. txt movefile opt forbuffer)
  11439. (if overlays
  11440. (progn
  11441. (setq ov (org-make-overlay beg end))
  11442. (if (featurep 'xemacs)
  11443. (progn
  11444. (org-overlay-put ov 'invisible t)
  11445. (org-overlay-put
  11446. ov 'end-glyph
  11447. (make-glyph (vector 'png :file movefile))))
  11448. (org-overlay-put
  11449. ov 'display
  11450. (list 'image :type 'png :file movefile :ascent 'center)))
  11451. (push ov org-latex-fragment-image-overlays)
  11452. (goto-char end))
  11453. (delete-region beg end)
  11454. (insert link))))))))
  11455. ;; This function borrows from Ganesh Swami's latex2png.el
  11456. (defun org-create-formula-image (string tofile options buffer)
  11457. (let* ((tmpdir (if (featurep 'xemacs)
  11458. (temp-directory)
  11459. temporary-file-directory))
  11460. (texfilebase (make-temp-name
  11461. (expand-file-name "orgtex" tmpdir)))
  11462. (texfile (concat texfilebase ".tex"))
  11463. (dvifile (concat texfilebase ".dvi"))
  11464. (pngfile (concat texfilebase ".png"))
  11465. (fnh (if (featurep 'xemacs)
  11466. (font-height (get-face-font 'default))
  11467. (face-attribute 'default :height nil)))
  11468. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11469. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11470. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11471. "Black"))
  11472. (bg (or (plist-get options (if buffer :background :html-background))
  11473. "Transparent")))
  11474. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11475. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11476. (with-temp-file texfile
  11477. (insert org-format-latex-header
  11478. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11479. (let ((dir default-directory))
  11480. (condition-case nil
  11481. (progn
  11482. (cd tmpdir)
  11483. (call-process "latex" nil nil nil texfile))
  11484. (error nil))
  11485. (cd dir))
  11486. (if (not (file-exists-p dvifile))
  11487. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11488. (condition-case nil
  11489. (call-process "dvipng" nil nil nil
  11490. "-E" "-fg" fg "-bg" bg
  11491. "-D" dpi
  11492. ;;"-x" scale "-y" scale
  11493. "-T" "tight"
  11494. "-o" pngfile
  11495. dvifile)
  11496. (error nil))
  11497. (if (not (file-exists-p pngfile))
  11498. (progn (message "Failed to create png file from %s" texfile) nil)
  11499. ;; Use the requested file name and clean up
  11500. (copy-file pngfile tofile 'replace)
  11501. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11502. (delete-file (concat texfilebase e)))
  11503. pngfile))))
  11504. (defun org-dvipng-color (attr)
  11505. "Return an rgb color specification for dvipng."
  11506. (apply 'format "rgb %s %s %s"
  11507. (mapcar 'org-normalize-color
  11508. (color-values (face-attribute 'default attr nil)))))
  11509. (defun org-normalize-color (value)
  11510. "Return string to be used as color value for an RGB component."
  11511. (format "%g" (/ value 65535.0)))
  11512. ;;;; Key bindings
  11513. ;; Make `C-c C-x' a prefix key
  11514. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11515. ;; TAB key with modifiers
  11516. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11517. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11518. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11519. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11520. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11521. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11522. ;; The following line is necessary under Suse GNU/Linux
  11523. (unless (featurep 'xemacs)
  11524. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11525. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11526. (define-key org-mode-map [backtab] 'org-shifttab)
  11527. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11528. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11529. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11530. ;; Cursor keys with modifiers
  11531. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11532. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11533. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11534. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11535. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11536. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11537. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11538. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11539. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11540. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11541. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11542. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11543. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11544. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11545. ;;; Extra keys for tty access.
  11546. ;; We only set them when really needed because otherwise the
  11547. ;; menus don't show the simple keys
  11548. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11549. (not window-system))
  11550. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11551. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11552. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11553. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11554. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11555. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11556. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11557. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11558. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11559. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11560. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11561. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11562. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11563. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11564. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11565. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11566. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11567. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11568. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11569. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11570. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11571. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11572. ;; All the other keys
  11573. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11574. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11575. (if (boundp 'narrow-map)
  11576. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11577. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11578. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11579. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11580. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11581. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11582. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11583. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11584. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11585. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11586. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11587. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11588. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11589. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11590. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11591. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11592. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11593. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11594. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11595. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11596. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11597. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11598. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11599. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11600. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11601. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11602. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11603. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11604. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11605. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11606. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11607. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11608. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11609. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11610. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11611. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11612. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11613. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11614. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11615. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11616. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11617. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11618. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11619. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11620. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11621. (org-defkey org-mode-map "\C-m" 'org-return)
  11622. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11623. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11624. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11625. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11626. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11627. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11628. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11629. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11630. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11631. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11632. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11633. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11634. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11635. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11636. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11637. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11638. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11639. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11640. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11641. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11642. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11643. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11644. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11645. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11646. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11647. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11648. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11649. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11650. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11651. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11652. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11653. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11654. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11655. (when (featurep 'xemacs)
  11656. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11657. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11658. (defun org-self-insert-command (N)
  11659. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11660. If the cursor is in a table looking at whitespace, the whitespace is
  11661. overwritten, and the table is not marked as requiring realignment."
  11662. (interactive "p")
  11663. (if (and (org-table-p)
  11664. (progn
  11665. ;; check if we blank the field, and if that triggers align
  11666. (and (featurep 'org-table) org-table-auto-blank-field
  11667. (member last-command
  11668. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11669. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11670. ;; got extra space, this field does not determine column width
  11671. (let (org-table-may-need-update) (org-table-blank-field))
  11672. ;; no extra space, this field may determine column width
  11673. (org-table-blank-field)))
  11674. t)
  11675. (eq N 1)
  11676. (looking-at "[^|\n]* |"))
  11677. (let (org-table-may-need-update)
  11678. (goto-char (1- (match-end 0)))
  11679. (delete-backward-char 1)
  11680. (goto-char (match-beginning 0))
  11681. (self-insert-command N))
  11682. (setq org-table-may-need-update t)
  11683. (self-insert-command N)
  11684. (org-fix-tags-on-the-fly)))
  11685. (defun org-fix-tags-on-the-fly ()
  11686. (when (and (equal (char-after (point-at-bol)) ?*)
  11687. (org-on-heading-p))
  11688. (org-align-tags-here org-tags-column)))
  11689. (defun org-delete-backward-char (N)
  11690. "Like `delete-backward-char', insert whitespace at field end in tables.
  11691. When deleting backwards, in tables this function will insert whitespace in
  11692. front of the next \"|\" separator, to keep the table aligned. The table will
  11693. still be marked for re-alignment if the field did fill the entire column,
  11694. because, in this case the deletion might narrow the column."
  11695. (interactive "p")
  11696. (if (and (org-table-p)
  11697. (eq N 1)
  11698. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11699. (looking-at ".*?|"))
  11700. (let ((pos (point))
  11701. (noalign (looking-at "[^|\n\r]* |"))
  11702. (c org-table-may-need-update))
  11703. (backward-delete-char N)
  11704. (skip-chars-forward "^|")
  11705. (insert " ")
  11706. (goto-char (1- pos))
  11707. ;; noalign: if there were two spaces at the end, this field
  11708. ;; does not determine the width of the column.
  11709. (if noalign (setq org-table-may-need-update c)))
  11710. (backward-delete-char N)
  11711. (org-fix-tags-on-the-fly)))
  11712. (defun org-delete-char (N)
  11713. "Like `delete-char', but insert whitespace at field end in tables.
  11714. When deleting characters, in tables this function will insert whitespace in
  11715. front of the next \"|\" separator, to keep the table aligned. The table will
  11716. still be marked for re-alignment if the field did fill the entire column,
  11717. because, in this case the deletion might narrow the column."
  11718. (interactive "p")
  11719. (if (and (org-table-p)
  11720. (not (bolp))
  11721. (not (= (char-after) ?|))
  11722. (eq N 1))
  11723. (if (looking-at ".*?|")
  11724. (let ((pos (point))
  11725. (noalign (looking-at "[^|\n\r]* |"))
  11726. (c org-table-may-need-update))
  11727. (replace-match (concat
  11728. (substring (match-string 0) 1 -1)
  11729. " |"))
  11730. (goto-char pos)
  11731. ;; noalign: if there were two spaces at the end, this field
  11732. ;; does not determine the width of the column.
  11733. (if noalign (setq org-table-may-need-update c)))
  11734. (delete-char N))
  11735. (delete-char N)
  11736. (org-fix-tags-on-the-fly)))
  11737. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11738. (put 'org-self-insert-command 'delete-selection t)
  11739. (put 'orgtbl-self-insert-command 'delete-selection t)
  11740. (put 'org-delete-char 'delete-selection 'supersede)
  11741. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11742. ;; Make `flyspell-mode' delay after some commands
  11743. (put 'org-self-insert-command 'flyspell-delayed t)
  11744. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11745. (put 'org-delete-char 'flyspell-delayed t)
  11746. (put 'org-delete-backward-char 'flyspell-delayed t)
  11747. ;; Make pabbrev-mode expand after org-mode commands
  11748. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11749. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11750. ;; How to do this: Measure non-white length of current string
  11751. ;; If equal to column width, we should realign.
  11752. (defun org-remap (map &rest commands)
  11753. "In MAP, remap the functions given in COMMANDS.
  11754. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11755. (let (new old)
  11756. (while commands
  11757. (setq old (pop commands) new (pop commands))
  11758. (if (fboundp 'command-remapping)
  11759. (org-defkey map (vector 'remap old) new)
  11760. (substitute-key-definition old new map global-map)))))
  11761. (when (eq org-enable-table-editor 'optimized)
  11762. ;; If the user wants maximum table support, we need to hijack
  11763. ;; some standard editing functions
  11764. (org-remap org-mode-map
  11765. 'self-insert-command 'org-self-insert-command
  11766. 'delete-char 'org-delete-char
  11767. 'delete-backward-char 'org-delete-backward-char)
  11768. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11769. (defun org-shiftcursor-error ()
  11770. "Throw an error because Shift-Cursor command was applied in wrong context."
  11771. (error "This command is active in special context like tables, headlines or timestamps"))
  11772. (defun org-shifttab (&optional arg)
  11773. "Global visibility cycling or move to previous table field.
  11774. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11775. on context.
  11776. See the individual commands for more information."
  11777. (interactive "P")
  11778. (cond
  11779. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11780. ((integerp arg)
  11781. (message "Content view to level: %d" arg)
  11782. (org-content (prefix-numeric-value arg))
  11783. (setq org-cycle-global-status 'overview))
  11784. (t (call-interactively 'org-global-cycle))))
  11785. (defun org-shiftmetaleft ()
  11786. "Promote subtree or delete table column.
  11787. Calls `org-promote-subtree', `org-outdent-item',
  11788. or `org-table-delete-column', depending on context.
  11789. See the individual commands for more information."
  11790. (interactive)
  11791. (cond
  11792. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11793. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11794. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11795. (t (org-shiftcursor-error))))
  11796. (defun org-shiftmetaright ()
  11797. "Demote subtree or insert table column.
  11798. Calls `org-demote-subtree', `org-indent-item',
  11799. or `org-table-insert-column', depending on context.
  11800. See the individual commands for more information."
  11801. (interactive)
  11802. (cond
  11803. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11804. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11805. ((org-at-item-p) (call-interactively 'org-indent-item))
  11806. (t (org-shiftcursor-error))))
  11807. (defun org-shiftmetaup (&optional arg)
  11808. "Move subtree up or kill table row.
  11809. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11810. `org-move-item-up' depending on context. See the individual commands
  11811. for more information."
  11812. (interactive "P")
  11813. (cond
  11814. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11815. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11816. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11817. (t (org-shiftcursor-error))))
  11818. (defun org-shiftmetadown (&optional arg)
  11819. "Move subtree down or insert table row.
  11820. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11821. `org-move-item-down', depending on context. See the individual
  11822. commands for more information."
  11823. (interactive "P")
  11824. (cond
  11825. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11826. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11827. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11828. (t (org-shiftcursor-error))))
  11829. (defun org-metaleft (&optional arg)
  11830. "Promote heading or move table column to left.
  11831. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11832. With no specific context, calls the Emacs default `backward-word'.
  11833. See the individual commands for more information."
  11834. (interactive "P")
  11835. (cond
  11836. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11837. ((or (org-on-heading-p) (org-region-active-p))
  11838. (call-interactively 'org-do-promote))
  11839. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11840. (t (call-interactively 'backward-word))))
  11841. (defun org-metaright (&optional arg)
  11842. "Demote subtree or move table column to right.
  11843. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11844. With no specific context, calls the Emacs default `forward-word'.
  11845. See the individual commands for more information."
  11846. (interactive "P")
  11847. (cond
  11848. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11849. ((or (org-on-heading-p) (org-region-active-p))
  11850. (call-interactively 'org-do-demote))
  11851. ((org-at-item-p) (call-interactively 'org-indent-item))
  11852. (t (call-interactively 'forward-word))))
  11853. (defun org-metaup (&optional arg)
  11854. "Move subtree up or move table row up.
  11855. Calls `org-move-subtree-up' or `org-table-move-row' or
  11856. `org-move-item-up', depending on context. See the individual commands
  11857. for more information."
  11858. (interactive "P")
  11859. (cond
  11860. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11861. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11862. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11863. (t (transpose-lines 1) (beginning-of-line -1))))
  11864. (defun org-metadown (&optional arg)
  11865. "Move subtree down or move table row down.
  11866. Calls `org-move-subtree-down' or `org-table-move-row' or
  11867. `org-move-item-down', depending on context. See the individual
  11868. commands for more information."
  11869. (interactive "P")
  11870. (cond
  11871. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11872. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11873. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11874. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11875. (defun org-shiftup (&optional arg)
  11876. "Increase item in timestamp or increase priority of current headline.
  11877. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11878. depending on context. See the individual commands for more information."
  11879. (interactive "P")
  11880. (cond
  11881. ((org-at-timestamp-p t)
  11882. (call-interactively (if org-edit-timestamp-down-means-later
  11883. 'org-timestamp-down 'org-timestamp-up)))
  11884. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11885. ((org-at-item-p) (call-interactively 'org-previous-item))
  11886. ((org-clocktable-try-shift 'up arg))
  11887. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11888. (defun org-shiftdown (&optional arg)
  11889. "Decrease item in timestamp or decrease priority of current headline.
  11890. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11891. depending on context. See the individual commands for more information."
  11892. (interactive "P")
  11893. (cond
  11894. ((org-at-timestamp-p t)
  11895. (call-interactively (if org-edit-timestamp-down-means-later
  11896. 'org-timestamp-up 'org-timestamp-down)))
  11897. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11898. ((org-clocktable-try-shift 'down arg))
  11899. (t (call-interactively 'org-next-item))))
  11900. (defun org-shiftright (&optional arg)
  11901. "Next TODO keyword or timestamp one day later, depending on context."
  11902. (interactive "P")
  11903. (cond
  11904. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11905. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11906. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11907. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11908. ((org-clocktable-try-shift 'right arg))
  11909. (t (org-shiftcursor-error))))
  11910. (defun org-shiftleft (&optional arg)
  11911. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11912. (interactive "P")
  11913. (cond
  11914. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11915. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11916. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11917. ((org-at-property-p)
  11918. (call-interactively 'org-property-previous-allowed-value))
  11919. ((org-clocktable-try-shift 'left arg))
  11920. (t (org-shiftcursor-error))))
  11921. (defun org-shiftcontrolright ()
  11922. "Switch to next TODO set."
  11923. (interactive)
  11924. (cond
  11925. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11926. (t (org-shiftcursor-error))))
  11927. (defun org-shiftcontrolleft ()
  11928. "Switch to previous TODO set."
  11929. (interactive)
  11930. (cond
  11931. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11932. (t (org-shiftcursor-error))))
  11933. (defun org-ctrl-c-ret ()
  11934. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11935. (interactive)
  11936. (cond
  11937. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11938. (t (call-interactively 'org-insert-heading))))
  11939. (defun org-copy-special ()
  11940. "Copy region in table or copy current subtree.
  11941. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11942. See the individual commands for more information."
  11943. (interactive)
  11944. (call-interactively
  11945. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11946. (defun org-cut-special ()
  11947. "Cut region in table or cut current subtree.
  11948. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11949. See the individual commands for more information."
  11950. (interactive)
  11951. (call-interactively
  11952. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11953. (defun org-paste-special (arg)
  11954. "Paste rectangular region into table, or past subtree relative to level.
  11955. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11956. See the individual commands for more information."
  11957. (interactive "P")
  11958. (if (org-at-table-p)
  11959. (org-table-paste-rectangle)
  11960. (org-paste-subtree arg)))
  11961. (defun org-edit-special ()
  11962. "Call a special editor for the stuff at point.
  11963. When at a table, call the formula editor with `org-table-edit-formulas'.
  11964. When at the first line of an src example, call `org-edit-src-code'.
  11965. When in an #+include line, visit the include file. Otherwise call
  11966. `ffap' to visit the file at point."
  11967. (interactive)
  11968. (cond
  11969. ((org-at-table-p)
  11970. (call-interactively 'org-table-edit-formulas))
  11971. ((save-excursion
  11972. (beginning-of-line 1)
  11973. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11974. (find-file (org-trim (match-string 1))))
  11975. ((org-edit-src-code))
  11976. ((org-edit-fixed-width-region))
  11977. (t (call-interactively 'ffap))))
  11978. (defun org-ctrl-c-ctrl-c (&optional arg)
  11979. "Set tags in headline, or update according to changed information at point.
  11980. This command does many different things, depending on context:
  11981. - If the cursor is in a headline, prompt for tags and insert them
  11982. into the current line, aligned to `org-tags-column'. When called
  11983. with prefix arg, realign all tags in the current buffer.
  11984. - If the cursor is in one of the special #+KEYWORD lines, this
  11985. triggers scanning the buffer for these lines and updating the
  11986. information.
  11987. - If the cursor is inside a table, realign the table. This command
  11988. works even if the automatic table editor has been turned off.
  11989. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11990. the entire table.
  11991. - If the cursor is a the beginning of a dynamic block, update it.
  11992. - If the cursor is inside a table created by the table.el package,
  11993. activate that table.
  11994. - If the current buffer is a remember buffer, close note and file it.
  11995. with a prefix argument, file it without further interaction to the default
  11996. location.
  11997. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11998. links in this buffer.
  11999. - If the cursor is on a numbered item in a plain list, renumber the
  12000. ordered list.
  12001. - If the cursor is on a checkbox, toggle it."
  12002. (interactive "P")
  12003. (let ((org-enable-table-editor t))
  12004. (cond
  12005. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12006. org-occur-highlights
  12007. org-latex-fragment-image-overlays)
  12008. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12009. (org-remove-occur-highlights)
  12010. (org-remove-latex-fragment-image-overlays)
  12011. (message "Temporary highlights/overlays removed from current buffer"))
  12012. ((and (local-variable-p 'org-finish-function (current-buffer))
  12013. (fboundp org-finish-function))
  12014. (funcall org-finish-function))
  12015. ((org-at-property-p)
  12016. (call-interactively 'org-property-action))
  12017. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12018. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12019. ((org-at-table.el-p)
  12020. (require 'table)
  12021. (beginning-of-line 1)
  12022. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12023. (call-interactively 'table-recognize-table))
  12024. ((org-at-table-p)
  12025. (org-table-maybe-eval-formula)
  12026. (if arg
  12027. (call-interactively 'org-table-recalculate)
  12028. (org-table-maybe-recalculate-line))
  12029. (call-interactively 'org-table-align))
  12030. ((org-at-item-checkbox-p)
  12031. (call-interactively 'org-toggle-checkbox))
  12032. ((org-at-item-p)
  12033. (call-interactively 'org-maybe-renumber-ordered-list))
  12034. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12035. ;; Dynamic block
  12036. (beginning-of-line 1)
  12037. (org-update-dblock))
  12038. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12039. (cond
  12040. ((equal (match-string 1) "TBLFM")
  12041. ;; Recalculate the table before this line
  12042. (save-excursion
  12043. (beginning-of-line 1)
  12044. (skip-chars-backward " \r\n\t")
  12045. (if (org-at-table-p)
  12046. (org-call-with-arg 'org-table-recalculate t))))
  12047. (t
  12048. ; (org-set-regexps-and-options)
  12049. ; (org-restart-font-lock)
  12050. (let ((org-inhibit-startup t)) (org-mode-restart))
  12051. (message "Local setup has been refreshed"))))
  12052. (t (error "C-c C-c can do nothing useful at this location.")))))
  12053. (defun org-mode-restart ()
  12054. "Restart Org-mode, to scan again for special lines.
  12055. Also updates the keyword regular expressions."
  12056. (interactive)
  12057. (org-mode)
  12058. (message "Org-mode restarted"))
  12059. (defun org-kill-note-or-show-branches ()
  12060. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12061. (interactive)
  12062. (if (not org-finish-function)
  12063. (call-interactively 'show-branches)
  12064. (let ((org-note-abort t))
  12065. (funcall org-finish-function))))
  12066. (defun org-return (&optional indent)
  12067. "Goto next table row or insert a newline.
  12068. Calls `org-table-next-row' or `newline', depending on context.
  12069. See the individual commands for more information."
  12070. (interactive)
  12071. (cond
  12072. ((bobp) (if indent (newline-and-indent) (newline)))
  12073. ((and (org-at-heading-p)
  12074. (looking-at
  12075. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12076. (org-show-entry)
  12077. (end-of-line 1)
  12078. (newline))
  12079. ((org-at-table-p)
  12080. (org-table-justify-field-maybe)
  12081. (call-interactively 'org-table-next-row))
  12082. (t (if indent (newline-and-indent) (newline)))))
  12083. (defun org-return-indent ()
  12084. "Goto next table row or insert a newline and indent.
  12085. Calls `org-table-next-row' or `newline-and-indent', depending on
  12086. context. See the individual commands for more information."
  12087. (interactive)
  12088. (org-return t))
  12089. (defun org-ctrl-c-star ()
  12090. "Compute table, or change heading status of lines.
  12091. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12092. depending on context. This will also turn a plain list item or a normal
  12093. line into a subheading."
  12094. (interactive)
  12095. (cond
  12096. ((org-at-table-p)
  12097. (call-interactively 'org-table-recalculate))
  12098. ((org-region-active-p)
  12099. ;; Convert all lines in region to list items
  12100. (call-interactively 'org-toggle-region-headings))
  12101. ((org-on-heading-p)
  12102. (org-toggle-region-headings (point-at-bol)
  12103. (min (1+ (point-at-eol)) (point-max))))
  12104. ((org-at-item-p)
  12105. ;; Convert to heading
  12106. (let ((level (save-match-data
  12107. (save-excursion
  12108. (condition-case nil
  12109. (progn
  12110. (org-back-to-heading t)
  12111. (funcall outline-level))
  12112. (error 0))))))
  12113. (replace-match
  12114. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12115. (t (org-toggle-region-headings (point-at-bol)
  12116. (min (1+ (point-at-eol)) (point-max))))))
  12117. (defun org-ctrl-c-minus ()
  12118. "Insert separator line in table or modify bullet status of line.
  12119. Also turns a plain line or a region of lines into list items.
  12120. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12121. `org-cycle-list-bullet', depending on context."
  12122. (interactive)
  12123. (cond
  12124. ((org-at-table-p)
  12125. (call-interactively 'org-table-insert-hline))
  12126. ((org-on-heading-p)
  12127. ;; Convert to item
  12128. (save-excursion
  12129. (beginning-of-line 1)
  12130. (if (looking-at "\\*+ ")
  12131. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12132. ((org-region-active-p)
  12133. ;; Convert all lines in region to list items
  12134. (call-interactively 'org-toggle-region-items))
  12135. ((org-in-item-p)
  12136. (call-interactively 'org-cycle-list-bullet))
  12137. (t (org-toggle-region-items (point-at-bol)
  12138. (min (1+ (point-at-eol)) (point-max))))))
  12139. (defun org-toggle-region-items (beg end)
  12140. "Convert all lines in region to list items.
  12141. If the first line is already an item, convert all list items in the region
  12142. to normal lines."
  12143. (interactive "r")
  12144. (let (l2 l)
  12145. (save-excursion
  12146. (goto-char end)
  12147. (setq l2 (org-current-line))
  12148. (goto-char beg)
  12149. (beginning-of-line 1)
  12150. (setq l (1- (org-current-line)))
  12151. (if (org-at-item-p)
  12152. ;; We already have items, de-itemize
  12153. (while (< (setq l (1+ l)) l2)
  12154. (when (org-at-item-p)
  12155. (goto-char (match-beginning 2))
  12156. (delete-region (match-beginning 2) (match-end 2))
  12157. (and (looking-at "[ \t]+") (replace-match "")))
  12158. (beginning-of-line 2))
  12159. (while (< (setq l (1+ l)) l2)
  12160. (unless (org-at-item-p)
  12161. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12162. (replace-match "\\1- \\2")))
  12163. (beginning-of-line 2))))))
  12164. (defun org-toggle-region-headings (beg end)
  12165. "Convert all lines in region to list items.
  12166. If the first line is already an item, convert all list items in the region
  12167. to normal lines."
  12168. (interactive "r")
  12169. (let (l2 l)
  12170. (save-excursion
  12171. (goto-char end)
  12172. (setq l2 (org-current-line))
  12173. (goto-char beg)
  12174. (beginning-of-line 1)
  12175. (setq l (1- (org-current-line)))
  12176. (if (org-on-heading-p)
  12177. ;; We already have headlines, de-star them
  12178. (while (< (setq l (1+ l)) l2)
  12179. (when (org-on-heading-p t)
  12180. (and (looking-at outline-regexp) (replace-match "")))
  12181. (beginning-of-line 2))
  12182. (let* ((stars (save-excursion
  12183. (re-search-backward org-complex-heading-regexp nil t)
  12184. (or (match-string 1) "*")))
  12185. (add-stars (if org-odd-levels-only "**" "*"))
  12186. (rpl (concat stars add-stars " \\2")))
  12187. (while (< (setq l (1+ l)) l2)
  12188. (unless (org-on-heading-p)
  12189. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12190. (replace-match rpl)))
  12191. (beginning-of-line 2)))))))
  12192. (defun org-meta-return (&optional arg)
  12193. "Insert a new heading or wrap a region in a table.
  12194. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12195. See the individual commands for more information."
  12196. (interactive "P")
  12197. (cond
  12198. ((org-at-table-p)
  12199. (call-interactively 'org-table-wrap-region))
  12200. (t (call-interactively 'org-insert-heading))))
  12201. ;;; Menu entries
  12202. ;; Define the Org-mode menus
  12203. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12204. '("Tbl"
  12205. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12206. ["Next Field" org-cycle (org-at-table-p)]
  12207. ["Previous Field" org-shifttab (org-at-table-p)]
  12208. ["Next Row" org-return (org-at-table-p)]
  12209. "--"
  12210. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12211. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12212. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12213. "--"
  12214. ("Column"
  12215. ["Move Column Left" org-metaleft (org-at-table-p)]
  12216. ["Move Column Right" org-metaright (org-at-table-p)]
  12217. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12218. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12219. ("Row"
  12220. ["Move Row Up" org-metaup (org-at-table-p)]
  12221. ["Move Row Down" org-metadown (org-at-table-p)]
  12222. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12223. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12224. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12225. "--"
  12226. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12227. ("Rectangle"
  12228. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12229. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12230. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12231. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12232. "--"
  12233. ("Calculate"
  12234. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12235. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12236. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12237. "--"
  12238. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12239. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12240. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12241. "--"
  12242. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12243. "--"
  12244. ["Sum Column/Rectangle" org-table-sum
  12245. (or (org-at-table-p) (org-region-active-p))]
  12246. ["Which Column?" org-table-current-column (org-at-table-p)])
  12247. ["Debug Formulas"
  12248. org-table-toggle-formula-debugger
  12249. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12250. ["Show Col/Row Numbers"
  12251. org-table-toggle-coordinate-overlays
  12252. :style toggle
  12253. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12254. "--"
  12255. ["Create" org-table-create (and (not (org-at-table-p))
  12256. org-enable-table-editor)]
  12257. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12258. ["Import from File" org-table-import (not (org-at-table-p))]
  12259. ["Export to File" org-table-export (org-at-table-p)]
  12260. "--"
  12261. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12262. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12263. '("Org"
  12264. ("Show/Hide"
  12265. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12266. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12267. ["Sparse Tree..." org-sparse-tree t]
  12268. ["Reveal Context" org-reveal t]
  12269. ["Show All" show-all t]
  12270. "--"
  12271. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12272. "--"
  12273. ["New Heading" org-insert-heading t]
  12274. ("Navigate Headings"
  12275. ["Up" outline-up-heading t]
  12276. ["Next" outline-next-visible-heading t]
  12277. ["Previous" outline-previous-visible-heading t]
  12278. ["Next Same Level" outline-forward-same-level t]
  12279. ["Previous Same Level" outline-backward-same-level t]
  12280. "--"
  12281. ["Jump" org-goto t])
  12282. ("Edit Structure"
  12283. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12284. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12285. "--"
  12286. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12287. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12288. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12289. "--"
  12290. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12291. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12292. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12293. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12294. "--"
  12295. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12296. "--"
  12297. ["Convert to odd levels" org-convert-to-odd-levels t]
  12298. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12299. ("Editing"
  12300. ["Emphasis..." org-emphasize t]
  12301. ["Edit Source Example" org-edit-special t])
  12302. ("Archive"
  12303. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12304. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12305. ; :active t :keys "C-u C-c C-x C-a"]
  12306. ["Sparse trees open ARCHIVE trees"
  12307. (setq org-sparse-tree-open-archived-trees
  12308. (not org-sparse-tree-open-archived-trees))
  12309. :style toggle :selected org-sparse-tree-open-archived-trees]
  12310. ["Cycling opens ARCHIVE trees"
  12311. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12312. :style toggle :selected org-cycle-open-archived-trees]
  12313. "--"
  12314. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12315. ; ["Check and Move Children" (org-archive-subtree '(4))
  12316. ; :active t :keys "C-u C-c C-x C-s"]
  12317. )
  12318. "--"
  12319. ("TODO Lists"
  12320. ["TODO/DONE/-" org-todo t]
  12321. ("Select keyword"
  12322. ["Next keyword" org-shiftright (org-on-heading-p)]
  12323. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12324. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12325. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12326. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12327. ["Show TODO Tree" org-show-todo-tree t]
  12328. ["Global TODO list" org-todo-list t]
  12329. "--"
  12330. ["Set Priority" org-priority t]
  12331. ["Priority Up" org-shiftup t]
  12332. ["Priority Down" org-shiftdown t])
  12333. ("TAGS and Properties"
  12334. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12335. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12336. "--"
  12337. ["Set property" 'org-set-property t]
  12338. ["Column view of properties" org-columns t]
  12339. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12340. ("Dates and Scheduling"
  12341. ["Timestamp" org-time-stamp t]
  12342. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12343. ("Change Date"
  12344. ["1 Day Later" org-shiftright t]
  12345. ["1 Day Earlier" org-shiftleft t]
  12346. ["1 ... Later" org-shiftup t]
  12347. ["1 ... Earlier" org-shiftdown t])
  12348. ["Compute Time Range" org-evaluate-time-range t]
  12349. ["Schedule Item" org-schedule t]
  12350. ["Deadline" org-deadline t]
  12351. "--"
  12352. ["Custom time format" org-toggle-time-stamp-overlays
  12353. :style radio :selected org-display-custom-times]
  12354. "--"
  12355. ["Goto Calendar" org-goto-calendar t]
  12356. ["Date from Calendar" org-date-from-calendar t])
  12357. ("Logging work"
  12358. ["Clock in" org-clock-in t]
  12359. ["Clock out" org-clock-out t]
  12360. ["Clock cancel" org-clock-cancel t]
  12361. ["Goto running clock" org-clock-goto t]
  12362. ["Display times" org-clock-display t]
  12363. ["Create clock table" org-clock-report t]
  12364. "--"
  12365. ["Record DONE time"
  12366. (progn (setq org-log-done (not org-log-done))
  12367. (message "Switching to %s will %s record a timestamp"
  12368. (car org-done-keywords)
  12369. (if org-log-done "automatically" "not")))
  12370. :style toggle :selected org-log-done])
  12371. "--"
  12372. ["Agenda Command..." org-agenda t]
  12373. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12374. ("File List for Agenda")
  12375. ("Special views current file"
  12376. ["TODO Tree" org-show-todo-tree t]
  12377. ["Check Deadlines" org-check-deadlines t]
  12378. ["Timeline" org-timeline t]
  12379. ["Tags Tree" org-tags-sparse-tree t])
  12380. "--"
  12381. ("Hyperlinks"
  12382. ["Store Link (Global)" org-store-link t]
  12383. ["Insert Link" org-insert-link t]
  12384. ["Follow Link" org-open-at-point t]
  12385. "--"
  12386. ["Next link" org-next-link t]
  12387. ["Previous link" org-previous-link t]
  12388. "--"
  12389. ["Descriptive Links"
  12390. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12391. :style radio
  12392. :selected (member '(org-link) buffer-invisibility-spec)]
  12393. ["Literal Links"
  12394. (progn
  12395. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12396. :style radio
  12397. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12398. "--"
  12399. ["Export/Publish..." org-export t]
  12400. ("LaTeX"
  12401. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12402. :selected org-cdlatex-mode]
  12403. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12404. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12405. ["Modify math symbol" org-cdlatex-math-modify
  12406. (org-inside-LaTeX-fragment-p)]
  12407. ["Export LaTeX fragments as images"
  12408. (if (featurep 'org-exp)
  12409. (setq org-export-with-LaTeX-fragments
  12410. (not org-export-with-LaTeX-fragments))
  12411. (require 'org-exp))
  12412. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12413. org-export-with-LaTeX-fragments)])
  12414. "--"
  12415. ("Documentation"
  12416. ["Show Version" org-version t]
  12417. ["Info Documentation" org-info t])
  12418. ("Customize"
  12419. ["Browse Org Group" org-customize t]
  12420. "--"
  12421. ["Expand This Menu" org-create-customize-menu
  12422. (fboundp 'customize-menu-create)])
  12423. "--"
  12424. ["Refresh setup" org-mode-restart t]
  12425. ))
  12426. (defun org-info (&optional node)
  12427. "Read documentation for Org-mode in the info system.
  12428. With optional NODE, go directly to that node."
  12429. (interactive)
  12430. (info (format "(org)%s" (or node ""))))
  12431. (defun org-install-agenda-files-menu ()
  12432. (let ((bl (buffer-list)))
  12433. (save-excursion
  12434. (while bl
  12435. (set-buffer (pop bl))
  12436. (if (org-mode-p) (setq bl nil)))
  12437. (when (org-mode-p)
  12438. (easy-menu-change
  12439. '("Org") "File List for Agenda"
  12440. (append
  12441. (list
  12442. ["Edit File List" (org-edit-agenda-file-list) t]
  12443. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12444. ["Remove Current File from List" org-remove-file t]
  12445. ["Cycle through agenda files" org-cycle-agenda-files t]
  12446. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12447. "--")
  12448. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12449. ;;;; Documentation
  12450. ;;;###autoload
  12451. (defun org-require-autoloaded-modules ()
  12452. (interactive)
  12453. (mapc 'require
  12454. '(org-agenda org-archive org-clock org-colview
  12455. org-exp org-id org-export-latex org-publish
  12456. org-remember org-table)))
  12457. ;;;###autoload
  12458. (defun org-customize ()
  12459. "Call the customize function with org as argument."
  12460. (interactive)
  12461. (org-load-modules-maybe)
  12462. (org-require-autoloaded-modules)
  12463. (customize-browse 'org))
  12464. (defun org-create-customize-menu ()
  12465. "Create a full customization menu for Org-mode, insert it into the menu."
  12466. (interactive)
  12467. (org-load-modules-maybe)
  12468. (org-require-autoloaded-modules)
  12469. (if (fboundp 'customize-menu-create)
  12470. (progn
  12471. (easy-menu-change
  12472. '("Org") "Customize"
  12473. `(["Browse Org group" org-customize t]
  12474. "--"
  12475. ,(customize-menu-create 'org)
  12476. ["Set" Custom-set t]
  12477. ["Save" Custom-save t]
  12478. ["Reset to Current" Custom-reset-current t]
  12479. ["Reset to Saved" Custom-reset-saved t]
  12480. ["Reset to Standard Settings" Custom-reset-standard t]))
  12481. (message "\"Org\"-menu now contains full customization menu"))
  12482. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12483. ;;;; Miscellaneous stuff
  12484. ;;; Generally useful functions
  12485. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12486. "Display the given MESSAGE as a warning."
  12487. (if (fboundp 'display-warning)
  12488. (display-warning 'org message
  12489. (if (featurep 'xemacs)
  12490. 'warning
  12491. :warning))
  12492. (let ((buf (get-buffer-create "*Org warnings*")))
  12493. (with-current-buffer buf
  12494. (goto-char (point-max))
  12495. (insert "Warning (Org): " message)
  12496. (unless (bolp)
  12497. (newline)))
  12498. (display-buffer buf)
  12499. (sit-for 0))))
  12500. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12501. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12502. (if (and marker (marker-buffer marker)
  12503. (buffer-live-p (marker-buffer marker)))
  12504. (progn
  12505. (switch-to-buffer (marker-buffer marker))
  12506. (if (or (> marker (point-max)) (< marker (point-min)))
  12507. (widen))
  12508. (goto-char marker))
  12509. (if bookmark
  12510. (bookmark-jump bookmark)
  12511. (error "Cannot find location"))))
  12512. (defun org-quote-csv-field (s)
  12513. "Quote field for inclusion in CSV material."
  12514. (if (string-match "[\",]" s)
  12515. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12516. s))
  12517. (defun org-plist-delete (plist property)
  12518. "Delete PROPERTY from PLIST.
  12519. This is in contrast to merely setting it to 0."
  12520. (let (p)
  12521. (while plist
  12522. (if (not (eq property (car plist)))
  12523. (setq p (plist-put p (car plist) (nth 1 plist))))
  12524. (setq plist (cddr plist)))
  12525. p))
  12526. (defun org-force-self-insert (N)
  12527. "Needed to enforce self-insert under remapping."
  12528. (interactive "p")
  12529. (self-insert-command N))
  12530. (defun org-string-width (s)
  12531. "Compute width of string, ignoring invisible characters.
  12532. This ignores character with invisibility property `org-link', and also
  12533. characters with property `org-cwidth', because these will become invisible
  12534. upon the next fontification round."
  12535. (let (b l)
  12536. (when (or (eq t buffer-invisibility-spec)
  12537. (assq 'org-link buffer-invisibility-spec))
  12538. (while (setq b (text-property-any 0 (length s)
  12539. 'invisible 'org-link s))
  12540. (setq s (concat (substring s 0 b)
  12541. (substring s (or (next-single-property-change
  12542. b 'invisible s) (length s)))))))
  12543. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12544. (setq s (concat (substring s 0 b)
  12545. (substring s (or (next-single-property-change
  12546. b 'org-cwidth s) (length s))))))
  12547. (setq l (string-width s) b -1)
  12548. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12549. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12550. l))
  12551. (defun org-base-buffer (buffer)
  12552. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12553. (if (not buffer)
  12554. buffer
  12555. (or (buffer-base-buffer buffer)
  12556. buffer)))
  12557. (defun org-trim (s)
  12558. "Remove whitespace at beginning and end of string."
  12559. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12560. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12561. s)
  12562. (defun org-wrap (string &optional width lines)
  12563. "Wrap string to either a number of lines, or a width in characters.
  12564. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12565. that costs. If there is a word longer than WIDTH, the text is actually
  12566. wrapped to the length of that word.
  12567. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12568. many lines, whatever width that takes.
  12569. The return value is a list of lines, without newlines at the end."
  12570. (let* ((words (org-split-string string "[ \t\n]+"))
  12571. (maxword (apply 'max (mapcar 'org-string-width words)))
  12572. w ll)
  12573. (cond (width
  12574. (org-do-wrap words (max maxword width)))
  12575. (lines
  12576. (setq w maxword)
  12577. (setq ll (org-do-wrap words maxword))
  12578. (if (<= (length ll) lines)
  12579. ll
  12580. (setq ll words)
  12581. (while (> (length ll) lines)
  12582. (setq w (1+ w))
  12583. (setq ll (org-do-wrap words w)))
  12584. ll))
  12585. (t (error "Cannot wrap this")))))
  12586. (defun org-do-wrap (words width)
  12587. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12588. (let (lines line)
  12589. (while words
  12590. (setq line (pop words))
  12591. (while (and words (< (+ (length line) (length (car words))) width))
  12592. (setq line (concat line " " (pop words))))
  12593. (setq lines (push line lines)))
  12594. (nreverse lines)))
  12595. (defun org-split-string (string &optional separators)
  12596. "Splits STRING into substrings at SEPARATORS.
  12597. No empty strings are returned if there are matches at the beginning
  12598. and end of string."
  12599. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12600. (start 0)
  12601. notfirst
  12602. (list nil))
  12603. (while (and (string-match rexp string
  12604. (if (and notfirst
  12605. (= start (match-beginning 0))
  12606. (< start (length string)))
  12607. (1+ start) start))
  12608. (< (match-beginning 0) (length string)))
  12609. (setq notfirst t)
  12610. (or (eq (match-beginning 0) 0)
  12611. (and (eq (match-beginning 0) (match-end 0))
  12612. (eq (match-beginning 0) start))
  12613. (setq list
  12614. (cons (substring string start (match-beginning 0))
  12615. list)))
  12616. (setq start (match-end 0)))
  12617. (or (eq start (length string))
  12618. (setq list
  12619. (cons (substring string start)
  12620. list)))
  12621. (nreverse list)))
  12622. (defun org-context ()
  12623. "Return a list of contexts of the current cursor position.
  12624. If several contexts apply, all are returned.
  12625. Each context entry is a list with a symbol naming the context, and
  12626. two positions indicating start and end of the context. Possible
  12627. contexts are:
  12628. :headline anywhere in a headline
  12629. :headline-stars on the leading stars in a headline
  12630. :todo-keyword on a TODO keyword (including DONE) in a headline
  12631. :tags on the TAGS in a headline
  12632. :priority on the priority cookie in a headline
  12633. :item on the first line of a plain list item
  12634. :item-bullet on the bullet/number of a plain list item
  12635. :checkbox on the checkbox in a plain list item
  12636. :table in an org-mode table
  12637. :table-special on a special filed in a table
  12638. :table-table in a table.el table
  12639. :link on a hyperlink
  12640. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12641. :target on a <<target>>
  12642. :radio-target on a <<<radio-target>>>
  12643. :latex-fragment on a LaTeX fragment
  12644. :latex-preview on a LaTeX fragment with overlayed preview image
  12645. This function expects the position to be visible because it uses font-lock
  12646. faces as a help to recognize the following contexts: :table-special, :link,
  12647. and :keyword."
  12648. (let* ((f (get-text-property (point) 'face))
  12649. (faces (if (listp f) f (list f)))
  12650. (p (point)) clist o)
  12651. ;; First the large context
  12652. (cond
  12653. ((org-on-heading-p t)
  12654. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12655. (when (progn
  12656. (beginning-of-line 1)
  12657. (looking-at org-todo-line-tags-regexp))
  12658. (push (org-point-in-group p 1 :headline-stars) clist)
  12659. (push (org-point-in-group p 2 :todo-keyword) clist)
  12660. (push (org-point-in-group p 4 :tags) clist))
  12661. (goto-char p)
  12662. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12663. (if (looking-at "\\[#[A-Z0-9]\\]")
  12664. (push (org-point-in-group p 0 :priority) clist)))
  12665. ((org-at-item-p)
  12666. (push (org-point-in-group p 2 :item-bullet) clist)
  12667. (push (list :item (point-at-bol)
  12668. (save-excursion (org-end-of-item) (point)))
  12669. clist)
  12670. (and (org-at-item-checkbox-p)
  12671. (push (org-point-in-group p 0 :checkbox) clist)))
  12672. ((org-at-table-p)
  12673. (push (list :table (org-table-begin) (org-table-end)) clist)
  12674. (if (memq 'org-formula faces)
  12675. (push (list :table-special
  12676. (previous-single-property-change p 'face)
  12677. (next-single-property-change p 'face)) clist)))
  12678. ((org-at-table-p 'any)
  12679. (push (list :table-table) clist)))
  12680. (goto-char p)
  12681. ;; Now the small context
  12682. (cond
  12683. ((org-at-timestamp-p)
  12684. (push (org-point-in-group p 0 :timestamp) clist))
  12685. ((memq 'org-link faces)
  12686. (push (list :link
  12687. (previous-single-property-change p 'face)
  12688. (next-single-property-change p 'face)) clist))
  12689. ((memq 'org-special-keyword faces)
  12690. (push (list :keyword
  12691. (previous-single-property-change p 'face)
  12692. (next-single-property-change p 'face)) clist))
  12693. ((org-on-target-p)
  12694. (push (org-point-in-group p 0 :target) clist)
  12695. (goto-char (1- (match-beginning 0)))
  12696. (if (looking-at org-radio-target-regexp)
  12697. (push (org-point-in-group p 0 :radio-target) clist))
  12698. (goto-char p))
  12699. ((setq o (car (delq nil
  12700. (mapcar
  12701. (lambda (x)
  12702. (if (memq x org-latex-fragment-image-overlays) x))
  12703. (org-overlays-at (point))))))
  12704. (push (list :latex-fragment
  12705. (org-overlay-start o) (org-overlay-end o)) clist)
  12706. (push (list :latex-preview
  12707. (org-overlay-start o) (org-overlay-end o)) clist))
  12708. ((org-inside-LaTeX-fragment-p)
  12709. ;; FIXME: positions wrong.
  12710. (push (list :latex-fragment (point) (point)) clist)))
  12711. (setq clist (nreverse (delq nil clist)))
  12712. clist))
  12713. ;; FIXME: Compare with at-regexp-p Do we need both?
  12714. (defun org-in-regexp (re &optional nlines visually)
  12715. "Check if point is inside a match of regexp.
  12716. Normally only the current line is checked, but you can include NLINES extra
  12717. lines both before and after point into the search.
  12718. If VISUALLY is set, require that the cursor is not after the match but
  12719. really on, so that the block visually is on the match."
  12720. (catch 'exit
  12721. (let ((pos (point))
  12722. (eol (point-at-eol (+ 1 (or nlines 0))))
  12723. (inc (if visually 1 0)))
  12724. (save-excursion
  12725. (beginning-of-line (- 1 (or nlines 0)))
  12726. (while (re-search-forward re eol t)
  12727. (if (and (<= (match-beginning 0) pos)
  12728. (>= (+ inc (match-end 0)) pos))
  12729. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12730. (defun org-at-regexp-p (regexp)
  12731. "Is point inside a match of REGEXP in the current line?"
  12732. (catch 'exit
  12733. (save-excursion
  12734. (let ((pos (point)) (end (point-at-eol)))
  12735. (beginning-of-line 1)
  12736. (while (re-search-forward regexp end t)
  12737. (if (and (<= (match-beginning 0) pos)
  12738. (>= (match-end 0) pos))
  12739. (throw 'exit t)))
  12740. nil))))
  12741. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12742. "Call `multi-occur' with buffers for all agenda files."
  12743. (interactive "sOrg-files matching: \np")
  12744. (let* ((files (org-agenda-files))
  12745. (tnames (mapcar 'file-truename files))
  12746. (extra org-agenda-text-search-extra-files)
  12747. f)
  12748. (when (eq (car extra) 'agenda-archives)
  12749. (setq extra (cdr extra))
  12750. (setq files (org-add-archive-files files)))
  12751. (while (setq f (pop extra))
  12752. (unless (member (file-truename f) tnames)
  12753. (add-to-list 'files f 'append)
  12754. (add-to-list 'tnames (file-truename f) 'append)))
  12755. (multi-occur
  12756. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12757. regexp)))
  12758. (if (boundp 'occur-mode-find-occurrence-hook)
  12759. ;; Emacs 23
  12760. (add-hook 'occur-mode-find-occurrence-hook
  12761. (lambda ()
  12762. (when (org-mode-p)
  12763. (org-reveal))))
  12764. ;; Emacs 22
  12765. (defadvice occur-mode-goto-occurrence
  12766. (after org-occur-reveal activate)
  12767. (and (org-mode-p) (org-reveal)))
  12768. (defadvice occur-mode-goto-occurrence-other-window
  12769. (after org-occur-reveal activate)
  12770. (and (org-mode-p) (org-reveal)))
  12771. (defadvice occur-mode-display-occurrence
  12772. (after org-occur-reveal activate)
  12773. (when (org-mode-p)
  12774. (let ((pos (occur-mode-find-occurrence)))
  12775. (with-current-buffer (marker-buffer pos)
  12776. (save-excursion
  12777. (goto-char pos)
  12778. (org-reveal)))))))
  12779. (defun org-uniquify (list)
  12780. "Remove duplicate elements from LIST."
  12781. (let (res)
  12782. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12783. res))
  12784. (defun org-delete-all (elts list)
  12785. "Remove all elements in ELTS from LIST."
  12786. (while elts
  12787. (setq list (delete (pop elts) list)))
  12788. list)
  12789. (defun org-back-over-empty-lines ()
  12790. "Move backwards over witespace, to the beginning of the first empty line.
  12791. Returns the number of empty lines passed."
  12792. (let ((pos (point)))
  12793. (skip-chars-backward " \t\n\r")
  12794. (beginning-of-line 2)
  12795. (goto-char (min (point) pos))
  12796. (count-lines (point) pos)))
  12797. (defun org-skip-whitespace ()
  12798. (skip-chars-forward " \t\n\r"))
  12799. (defun org-point-in-group (point group &optional context)
  12800. "Check if POINT is in match-group GROUP.
  12801. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12802. match. If the match group does ot exist or point is not inside it,
  12803. return nil."
  12804. (and (match-beginning group)
  12805. (>= point (match-beginning group))
  12806. (<= point (match-end group))
  12807. (if context
  12808. (list context (match-beginning group) (match-end group))
  12809. t)))
  12810. (defun org-switch-to-buffer-other-window (&rest args)
  12811. "Switch to buffer in a second window on the current frame.
  12812. In particular, do not allow pop-up frames."
  12813. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12814. special-display-function)
  12815. (apply 'switch-to-buffer-other-window args)))
  12816. (defun org-combine-plists (&rest plists)
  12817. "Create a single property list from all plists in PLISTS.
  12818. The process starts by copying the first list, and then setting properties
  12819. from the other lists. Settings in the last list are the most significant
  12820. ones and overrule settings in the other lists."
  12821. (let ((rtn (copy-sequence (pop plists)))
  12822. p v ls)
  12823. (while plists
  12824. (setq ls (pop plists))
  12825. (while ls
  12826. (setq p (pop ls) v (pop ls))
  12827. (setq rtn (plist-put rtn p v))))
  12828. rtn))
  12829. (defun org-move-line-down (arg)
  12830. "Move the current line down. With prefix argument, move it past ARG lines."
  12831. (interactive "p")
  12832. (let ((col (current-column))
  12833. beg end pos)
  12834. (beginning-of-line 1) (setq beg (point))
  12835. (beginning-of-line 2) (setq end (point))
  12836. (beginning-of-line (+ 1 arg))
  12837. (setq pos (move-marker (make-marker) (point)))
  12838. (insert (delete-and-extract-region beg end))
  12839. (goto-char pos)
  12840. (org-move-to-column col)))
  12841. (defun org-move-line-up (arg)
  12842. "Move the current line up. With prefix argument, move it past ARG lines."
  12843. (interactive "p")
  12844. (let ((col (current-column))
  12845. beg end pos)
  12846. (beginning-of-line 1) (setq beg (point))
  12847. (beginning-of-line 2) (setq end (point))
  12848. (beginning-of-line (- arg))
  12849. (setq pos (move-marker (make-marker) (point)))
  12850. (insert (delete-and-extract-region beg end))
  12851. (goto-char pos)
  12852. (org-move-to-column col)))
  12853. (defun org-replace-escapes (string table)
  12854. "Replace %-escapes in STRING with values in TABLE.
  12855. TABLE is an association list with keys like \"%a\" and string values.
  12856. The sequences in STRING may contain normal field width and padding information,
  12857. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12858. so values can contain further %-escapes if they are define later in TABLE."
  12859. (let ((case-fold-search nil)
  12860. e re rpl)
  12861. (while (setq e (pop table))
  12862. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12863. (while (string-match re string)
  12864. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12865. (cdr e)))
  12866. (setq string (replace-match rpl t t string))))
  12867. string))
  12868. (defun org-sublist (list start end)
  12869. "Return a section of LIST, from START to END.
  12870. Counting starts at 1."
  12871. (let (rtn (c start))
  12872. (setq list (nthcdr (1- start) list))
  12873. (while (and list (<= c end))
  12874. (push (pop list) rtn)
  12875. (setq c (1+ c)))
  12876. (nreverse rtn)))
  12877. (defun org-find-base-buffer-visiting (file)
  12878. "Like `find-buffer-visiting' but alway return the base buffer and
  12879. not an indirect buffer."
  12880. (let ((buf (find-buffer-visiting file)))
  12881. (if buf
  12882. (or (buffer-base-buffer buf) buf)
  12883. nil)))
  12884. (defun org-image-file-name-regexp ()
  12885. "Return regexp matching the file names of images."
  12886. (if (fboundp 'image-file-name-regexp)
  12887. (image-file-name-regexp)
  12888. (let ((image-file-name-extensions
  12889. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12890. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12891. (concat "\\."
  12892. (regexp-opt (nconc (mapcar 'upcase
  12893. image-file-name-extensions)
  12894. image-file-name-extensions)
  12895. t)
  12896. "\\'"))))
  12897. (defun org-file-image-p (file)
  12898. "Return non-nil if FILE is an image."
  12899. (save-match-data
  12900. (string-match (org-image-file-name-regexp) file)))
  12901. (defun org-get-cursor-date ()
  12902. "Return the date at cursor in as a time.
  12903. This works in the calendar and in the agenda, anywhere else it just
  12904. returns the current time."
  12905. (let (date day defd)
  12906. (cond
  12907. ((eq major-mode 'calendar-mode)
  12908. (setq date (calendar-cursor-to-date)
  12909. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12910. ((eq major-mode 'org-agenda-mode)
  12911. (setq day (get-text-property (point) 'day))
  12912. (if day
  12913. (setq date (calendar-gregorian-from-absolute day)
  12914. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12915. (nth 2 date))))))
  12916. (or defd (current-time))))
  12917. (defvar org-agenda-action-marker (make-marker)
  12918. "Marker pointing to the entry for the next agenda action.")
  12919. (defun org-mark-entry-for-agenda-action ()
  12920. "Mark the current entry as target of an agenda action.
  12921. Agenda actions are actions executed from the agenda with the key `k',
  12922. which make use of the date at the cursor."
  12923. (interactive)
  12924. (move-marker org-agenda-action-marker
  12925. (save-excursion (org-back-to-heading t) (point))
  12926. (current-buffer))
  12927. (message
  12928. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12929. ;;; Paragraph filling stuff.
  12930. ;; We want this to be just right, so use the full arsenal.
  12931. (defun org-indent-line-function ()
  12932. "Indent line like previous, but further if previous was headline or item."
  12933. (interactive)
  12934. (let* ((pos (point))
  12935. (itemp (org-at-item-p))
  12936. column bpos bcol tpos tcol bullet btype bullet-type)
  12937. ;; Find the previous relevant line
  12938. (beginning-of-line 1)
  12939. (cond
  12940. ((looking-at "#") (setq column 0))
  12941. ((looking-at "\\*+ ") (setq column 0))
  12942. (t
  12943. (beginning-of-line 0)
  12944. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12945. (beginning-of-line 0))
  12946. (cond
  12947. ((looking-at "\\*+[ \t]+")
  12948. (if (not org-adapt-indentation)
  12949. (setq column 0)
  12950. (goto-char (match-end 0))
  12951. (setq column (current-column))))
  12952. ((org-in-item-p)
  12953. (org-beginning-of-item)
  12954. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12955. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12956. (setq bpos (match-beginning 1) tpos (match-end 0)
  12957. bcol (progn (goto-char bpos) (current-column))
  12958. tcol (progn (goto-char tpos) (current-column))
  12959. bullet (match-string 1)
  12960. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12961. (if (> tcol (+ bcol org-description-max-indent))
  12962. (setq tcol (+ bcol 5)))
  12963. (if (not itemp)
  12964. (setq column tcol)
  12965. (goto-char pos)
  12966. (beginning-of-line 1)
  12967. (if (looking-at "\\S-")
  12968. (progn
  12969. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12970. (setq bullet (match-string 1)
  12971. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12972. (setq column (if (equal btype bullet-type) bcol tcol)))
  12973. (setq column (org-get-indentation)))))
  12974. (t (setq column (org-get-indentation))))))
  12975. (goto-char pos)
  12976. (if (<= (current-column) (current-indentation))
  12977. (org-indent-line-to column)
  12978. (save-excursion (org-indent-line-to column)))
  12979. (setq column (current-column))
  12980. (beginning-of-line 1)
  12981. (if (looking-at
  12982. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12983. (replace-match (concat "\\1" (format org-property-format
  12984. (match-string 2) (match-string 3)))
  12985. t nil))
  12986. (org-move-to-column column)))
  12987. (defun org-set-autofill-regexps ()
  12988. (interactive)
  12989. ;; In the paragraph separator we include headlines, because filling
  12990. ;; text in a line directly attached to a headline would otherwise
  12991. ;; fill the headline as well.
  12992. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12993. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12994. ;; The paragraph starter includes hand-formatted lists.
  12995. (org-set-local 'paragraph-start
  12996. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12997. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12998. ;; But only if the user has not turned off tables or fixed-width regions
  12999. (org-set-local
  13000. 'auto-fill-inhibit-regexp
  13001. (concat "\\*+ \\|#\\+"
  13002. "\\|[ \t]*" org-keyword-time-regexp
  13003. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13004. (concat
  13005. "\\|[ \t]*["
  13006. (if org-enable-table-editor "|" "")
  13007. (if org-enable-fixed-width-editor ":" "")
  13008. "]"))))
  13009. ;; We use our own fill-paragraph function, to make sure that tables
  13010. ;; and fixed-width regions are not wrapped. That function will pass
  13011. ;; through to `fill-paragraph' when appropriate.
  13012. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13013. ; Adaptive filling: To get full control, first make sure that
  13014. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13015. (org-set-local 'adaptive-fill-regexp "\000")
  13016. (org-set-local 'adaptive-fill-function
  13017. 'org-adaptive-fill-function)
  13018. (org-set-local
  13019. 'align-mode-rules-list
  13020. '((org-in-buffer-settings
  13021. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13022. (modes . '(org-mode))))))
  13023. (defun org-fill-paragraph (&optional justify)
  13024. "Re-align a table, pass through to fill-paragraph if no table."
  13025. (let ((table-p (org-at-table-p))
  13026. (table.el-p (org-at-table.el-p)))
  13027. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13028. (save-excursion (goto-char (point-at-bol))
  13029. (looking-at outline-regexp)))
  13030. t) ; skip headlines
  13031. (table.el-p t) ; skip table.el tables
  13032. (table-p (org-table-align) t) ; align org-mode tables
  13033. (t nil)))) ; call paragraph-fill
  13034. ;; For reference, this is the default value of adaptive-fill-regexp
  13035. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13036. (defun org-adaptive-fill-function ()
  13037. "Return a fill prefix for org-mode files.
  13038. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13039. work correctly."
  13040. (cond ((looking-at "#[ \t]+")
  13041. (match-string 0))
  13042. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13043. (save-excursion
  13044. (if (> (match-end 1) (+ (match-beginning 1)
  13045. org-description-max-indent))
  13046. (goto-char (+ (match-beginning 1) 5))
  13047. (goto-char (match-end 0)))
  13048. (make-string (current-column) ?\ )))
  13049. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13050. (save-excursion
  13051. (goto-char (match-end 0))
  13052. (make-string (current-column) ?\ )))
  13053. (t nil)))
  13054. ;;; Other stuff.
  13055. (defun org-toggle-fixed-width-section (arg)
  13056. "Toggle the fixed-width export.
  13057. If there is no active region, the QUOTE keyword at the current headline is
  13058. inserted or removed. When present, it causes the text between this headline
  13059. and the next to be exported as fixed-width text, and unmodified.
  13060. If there is an active region, this command adds or removes a colon as the
  13061. first character of this line. If the first character of a line is a colon,
  13062. this line is also exported in fixed-width font."
  13063. (interactive "P")
  13064. (let* ((cc 0)
  13065. (regionp (org-region-active-p))
  13066. (beg (if regionp (region-beginning) (point)))
  13067. (end (if regionp (region-end)))
  13068. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13069. (case-fold-search nil)
  13070. (re "[ \t]*\\(:\\)")
  13071. off)
  13072. (if regionp
  13073. (save-excursion
  13074. (goto-char beg)
  13075. (setq cc (current-column))
  13076. (beginning-of-line 1)
  13077. (setq off (looking-at re))
  13078. (while (> nlines 0)
  13079. (setq nlines (1- nlines))
  13080. (beginning-of-line 1)
  13081. (cond
  13082. (arg
  13083. (org-move-to-column cc t)
  13084. (insert ":\n")
  13085. (forward-line -1))
  13086. ((and off (looking-at re))
  13087. (replace-match "" t t nil 1))
  13088. ((not off) (org-move-to-column cc t) (insert ":")))
  13089. (forward-line 1)))
  13090. (save-excursion
  13091. (org-back-to-heading)
  13092. (if (looking-at (concat outline-regexp
  13093. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13094. (replace-match "" t t nil 1)
  13095. (if (looking-at outline-regexp)
  13096. (progn
  13097. (goto-char (match-end 0))
  13098. (insert org-quote-string " "))))))))
  13099. ;;;; Functions extending outline functionality
  13100. (defun org-beginning-of-line (&optional arg)
  13101. "Go to the beginning of the current line. If that is invisible, continue
  13102. to a visible line beginning. This makes the function of C-a more intuitive.
  13103. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13104. first attempt, and only move to after the tags when the cursor is already
  13105. beyond the end of the headline."
  13106. (interactive "P")
  13107. (let ((pos (point)) refpos)
  13108. (beginning-of-line 1)
  13109. (if (bobp)
  13110. nil
  13111. (backward-char 1)
  13112. (if (org-invisible-p)
  13113. (while (and (not (bobp)) (org-invisible-p))
  13114. (backward-char 1)
  13115. (beginning-of-line 1))
  13116. (forward-char 1)))
  13117. (when org-special-ctrl-a/e
  13118. (cond
  13119. ((and (looking-at org-complex-heading-regexp)
  13120. (= (char-after (match-end 1)) ?\ ))
  13121. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13122. (point-at-eol)))
  13123. (goto-char
  13124. (if (eq org-special-ctrl-a/e t)
  13125. (cond ((> pos refpos) refpos)
  13126. ((= pos (point)) refpos)
  13127. (t (point)))
  13128. (cond ((> pos (point)) (point))
  13129. ((not (eq last-command this-command)) (point))
  13130. (t refpos)))))
  13131. ((org-at-item-p)
  13132. (goto-char
  13133. (if (eq org-special-ctrl-a/e t)
  13134. (cond ((> pos (match-end 4)) (match-end 4))
  13135. ((= pos (point)) (match-end 4))
  13136. (t (point)))
  13137. (cond ((> pos (point)) (point))
  13138. ((not (eq last-command this-command)) (point))
  13139. (t (match-end 4))))))))
  13140. (org-no-warnings
  13141. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13142. (defun org-end-of-line (&optional arg)
  13143. "Go to the end of the line.
  13144. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13145. first attempt, and only move to after the tags when the cursor is already
  13146. beyond the end of the headline."
  13147. (interactive "P")
  13148. (if (or (not org-special-ctrl-a/e)
  13149. (not (org-on-heading-p)))
  13150. (end-of-line arg)
  13151. (let ((pos (point)))
  13152. (beginning-of-line 1)
  13153. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13154. (if (eq org-special-ctrl-a/e t)
  13155. (if (or (< pos (match-beginning 1))
  13156. (= pos (match-end 0)))
  13157. (goto-char (match-beginning 1))
  13158. (goto-char (match-end 0)))
  13159. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13160. (goto-char (match-end 0))
  13161. (goto-char (match-beginning 1))))
  13162. (end-of-line arg))))
  13163. (org-no-warnings
  13164. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13165. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13166. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13167. (defun org-kill-line (&optional arg)
  13168. "Kill line, to tags or end of line."
  13169. (interactive "P")
  13170. (cond
  13171. ((or (not org-special-ctrl-k)
  13172. (bolp)
  13173. (not (org-on-heading-p)))
  13174. (call-interactively 'kill-line))
  13175. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13176. (kill-region (point) (match-beginning 1))
  13177. (org-set-tags nil t))
  13178. (t (kill-region (point) (point-at-eol)))))
  13179. (define-key org-mode-map "\C-k" 'org-kill-line)
  13180. (defun org-invisible-p ()
  13181. "Check if point is at a character currently not visible."
  13182. ;; Early versions of noutline don't have `outline-invisible-p'.
  13183. (if (fboundp 'outline-invisible-p)
  13184. (outline-invisible-p)
  13185. (get-char-property (point) 'invisible)))
  13186. (defun org-invisible-p2 ()
  13187. "Check if point is at a character currently not visible."
  13188. (save-excursion
  13189. (if (and (eolp) (not (bobp))) (backward-char 1))
  13190. ;; Early versions of noutline don't have `outline-invisible-p'.
  13191. (if (fboundp 'outline-invisible-p)
  13192. (outline-invisible-p)
  13193. (get-char-property (point) 'invisible))))
  13194. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13195. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13196. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13197. (defun org-at-heading-or-item-p ()
  13198. (or (org-on-heading-p) (org-at-item-p)))
  13199. (defun org-on-target-p ()
  13200. (or (org-in-regexp org-radio-target-regexp)
  13201. (org-in-regexp org-target-regexp)))
  13202. (defun org-up-heading-all (arg)
  13203. "Move to the heading line of which the present line is a subheading.
  13204. This function considers both visible and invisible heading lines.
  13205. With argument, move up ARG levels."
  13206. (if (fboundp 'outline-up-heading-all)
  13207. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13208. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13209. (defun org-up-heading-safe ()
  13210. "Move to the heading line of which the present line is a subheading.
  13211. This version will not throw an error. It will return the level of the
  13212. headline found, or nil if no higher level is found."
  13213. (let ((pos (point)) start-level level
  13214. (re (concat "^" outline-regexp)))
  13215. (catch 'exit
  13216. (outline-back-to-heading t)
  13217. (setq start-level (funcall outline-level))
  13218. (if (equal start-level 1) (throw 'exit nil))
  13219. (while (re-search-backward re nil t)
  13220. (setq level (funcall outline-level))
  13221. (if (< level start-level) (throw 'exit level)))
  13222. nil)))
  13223. (defun org-first-sibling-p ()
  13224. "Is this heading the first child of its parents?"
  13225. (interactive)
  13226. (let ((re (concat "^" outline-regexp))
  13227. level l)
  13228. (unless (org-at-heading-p t)
  13229. (error "Not at a heading"))
  13230. (setq level (funcall outline-level))
  13231. (save-excursion
  13232. (if (not (re-search-backward re nil t))
  13233. t
  13234. (setq l (funcall outline-level))
  13235. (< l level)))))
  13236. (defun org-goto-sibling (&optional previous)
  13237. "Goto the next sibling, even if it is invisible.
  13238. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13239. when a sibling was found. When none is found, return nil and don't
  13240. move point."
  13241. (let ((fun (if previous 're-search-backward 're-search-forward))
  13242. (pos (point))
  13243. (re (concat "^" outline-regexp))
  13244. level l)
  13245. (when (condition-case nil (org-back-to-heading t) (error nil))
  13246. (setq level (funcall outline-level))
  13247. (catch 'exit
  13248. (or previous (forward-char 1))
  13249. (while (funcall fun re nil t)
  13250. (setq l (funcall outline-level))
  13251. (when (< l level) (goto-char pos) (throw 'exit nil))
  13252. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13253. (goto-char pos)
  13254. nil))))
  13255. (defun org-show-siblings ()
  13256. "Show all siblings of the current headline."
  13257. (save-excursion
  13258. (while (org-goto-sibling) (org-flag-heading nil)))
  13259. (save-excursion
  13260. (while (org-goto-sibling 'previous)
  13261. (org-flag-heading nil))))
  13262. (defun org-show-hidden-entry ()
  13263. "Show an entry where even the heading is hidden."
  13264. (save-excursion
  13265. (org-show-entry)))
  13266. (defun org-flag-heading (flag &optional entry)
  13267. "Flag the current heading. FLAG non-nil means make invisible.
  13268. When ENTRY is non-nil, show the entire entry."
  13269. (save-excursion
  13270. (org-back-to-heading t)
  13271. ;; Check if we should show the entire entry
  13272. (if entry
  13273. (progn
  13274. (org-show-entry)
  13275. (save-excursion
  13276. (and (outline-next-heading)
  13277. (org-flag-heading nil))))
  13278. (outline-flag-region (max (point-min) (1- (point)))
  13279. (save-excursion (outline-end-of-heading) (point))
  13280. flag))))
  13281. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13282. ;; This is an exact copy of the original function, but it uses
  13283. ;; `org-back-to-heading', to make it work also in invisible
  13284. ;; trees. And is uses an invisible-OK argument.
  13285. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13286. (org-back-to-heading invisible-OK)
  13287. (let ((first t)
  13288. (level (funcall outline-level)))
  13289. (while (and (not (eobp))
  13290. (or first (> (funcall outline-level) level)))
  13291. (setq first nil)
  13292. (outline-next-heading))
  13293. (unless to-heading
  13294. (if (memq (preceding-char) '(?\n ?\^M))
  13295. (progn
  13296. ;; Go to end of line before heading
  13297. (forward-char -1)
  13298. (if (memq (preceding-char) '(?\n ?\^M))
  13299. ;; leave blank line before heading
  13300. (forward-char -1))))))
  13301. (point))
  13302. (defun org-show-subtree ()
  13303. "Show everything after this heading at deeper levels."
  13304. (outline-flag-region
  13305. (point)
  13306. (save-excursion
  13307. (outline-end-of-subtree) (outline-next-heading) (point))
  13308. nil))
  13309. (defun org-show-entry ()
  13310. "Show the body directly following this heading.
  13311. Show the heading too, if it is currently invisible."
  13312. (interactive)
  13313. (save-excursion
  13314. (condition-case nil
  13315. (progn
  13316. (org-back-to-heading t)
  13317. (outline-flag-region
  13318. (max (point-min) (1- (point)))
  13319. (save-excursion
  13320. (re-search-forward
  13321. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13322. (or (match-beginning 1) (point-max)))
  13323. nil))
  13324. (error nil))))
  13325. (defun org-make-options-regexp (kwds)
  13326. "Make a regular expression for keyword lines."
  13327. (concat
  13328. "^"
  13329. "#?[ \t]*\\+\\("
  13330. (mapconcat 'regexp-quote kwds "\\|")
  13331. "\\):[ \t]*"
  13332. "\\(.+\\)"))
  13333. ;; Make isearch reveal the necessary context
  13334. (defun org-isearch-end ()
  13335. "Reveal context after isearch exits."
  13336. (when isearch-success ; only if search was successful
  13337. (if (featurep 'xemacs)
  13338. ;; Under XEmacs, the hook is run in the correct place,
  13339. ;; we directly show the context.
  13340. (org-show-context 'isearch)
  13341. ;; In Emacs the hook runs *before* restoring the overlays.
  13342. ;; So we have to use a one-time post-command-hook to do this.
  13343. ;; (Emacs 22 has a special variable, see function `org-mode')
  13344. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13345. isearch-mode-end-hook-quit)
  13346. ;; Only when the isearch was not quitted.
  13347. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13348. 'append 'local)))))
  13349. (defun org-isearch-post-command ()
  13350. "Remove self from hook, and show context."
  13351. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13352. (org-show-context 'isearch))
  13353. ;;;; Integration with and fixes for other packages
  13354. ;;; Imenu support
  13355. (defvar org-imenu-markers nil
  13356. "All markers currently used by Imenu.")
  13357. (make-variable-buffer-local 'org-imenu-markers)
  13358. (defun org-imenu-new-marker (&optional pos)
  13359. "Return a new marker for use by Imenu, and remember the marker."
  13360. (let ((m (make-marker)))
  13361. (move-marker m (or pos (point)))
  13362. (push m org-imenu-markers)
  13363. m))
  13364. (defun org-imenu-get-tree ()
  13365. "Produce the index for Imenu."
  13366. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13367. (setq org-imenu-markers nil)
  13368. (let* ((n org-imenu-depth)
  13369. (re (concat "^" outline-regexp))
  13370. (subs (make-vector (1+ n) nil))
  13371. (last-level 0)
  13372. m tree level head)
  13373. (save-excursion
  13374. (save-restriction
  13375. (widen)
  13376. (goto-char (point-max))
  13377. (while (re-search-backward re nil t)
  13378. (setq level (org-reduced-level (funcall outline-level)))
  13379. (when (<= level n)
  13380. (looking-at org-complex-heading-regexp)
  13381. (setq head (org-match-string-no-properties 4)
  13382. m (org-imenu-new-marker))
  13383. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13384. (if (>= level last-level)
  13385. (push (cons head m) (aref subs level))
  13386. (push (cons head (aref subs (1+ level))) (aref subs level))
  13387. (loop for i from (1+ level) to n do (aset subs i nil)))
  13388. (setq last-level level)))))
  13389. (aref subs 1)))
  13390. (eval-after-load "imenu"
  13391. '(progn
  13392. (add-hook 'imenu-after-jump-hook
  13393. (lambda ()
  13394. (if (eq major-mode 'org-mode)
  13395. (org-show-context 'org-goto))))))
  13396. ;; Speedbar support
  13397. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13398. "Overlay marking the agenda restriction line in speedbar.")
  13399. (org-overlay-put org-speedbar-restriction-lock-overlay
  13400. 'face 'org-agenda-restriction-lock)
  13401. (org-overlay-put org-speedbar-restriction-lock-overlay
  13402. 'help-echo "Agendas are currently limited to this item.")
  13403. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13404. (defun org-speedbar-set-agenda-restriction ()
  13405. "Restrict future agenda commands to the location at point in speedbar.
  13406. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13407. (interactive)
  13408. (require 'org-agenda)
  13409. (let (p m tp np dir txt w)
  13410. (cond
  13411. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13412. 'org-imenu t))
  13413. (setq m (get-text-property p 'org-imenu-marker))
  13414. (save-excursion
  13415. (save-restriction
  13416. (set-buffer (marker-buffer m))
  13417. (goto-char m)
  13418. (org-agenda-set-restriction-lock 'subtree))))
  13419. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13420. 'speedbar-function 'speedbar-find-file))
  13421. (setq tp (previous-single-property-change
  13422. (1+ p) 'speedbar-function)
  13423. np (next-single-property-change
  13424. tp 'speedbar-function)
  13425. dir (speedbar-line-directory)
  13426. txt (buffer-substring-no-properties (or tp (point-min))
  13427. (or np (point-max))))
  13428. (save-excursion
  13429. (save-restriction
  13430. (set-buffer (find-file-noselect
  13431. (let ((default-directory dir))
  13432. (expand-file-name txt))))
  13433. (unless (org-mode-p)
  13434. (error "Cannot restrict to non-Org-mode file"))
  13435. (org-agenda-set-restriction-lock 'file))))
  13436. (t (error "Don't know how to restrict Org-mode's agenda")))
  13437. (org-move-overlay org-speedbar-restriction-lock-overlay
  13438. (point-at-bol) (point-at-eol))
  13439. (setq current-prefix-arg nil)
  13440. (org-agenda-maybe-redo)))
  13441. (eval-after-load "speedbar"
  13442. '(progn
  13443. (speedbar-add-supported-extension ".org")
  13444. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13445. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13446. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13447. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13448. (add-hook 'speedbar-visiting-tag-hook
  13449. (lambda () (org-show-context 'org-goto)))))
  13450. ;;; Fixes and Hacks for problems with other packages
  13451. ;; Make flyspell not check words in links, to not mess up our keymap
  13452. (defun org-mode-flyspell-verify ()
  13453. "Don't let flyspell put overlays at active buttons."
  13454. (not (get-text-property (point) 'keymap)))
  13455. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13456. (eval-after-load "bookmark"
  13457. '(if (boundp 'bookmark-after-jump-hook)
  13458. ;; We can use the hook
  13459. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13460. ;; Hook not available, use advice
  13461. (defadvice bookmark-jump (after org-make-visible activate)
  13462. "Make the position visible."
  13463. (org-bookmark-jump-unhide))))
  13464. (defun org-bookmark-jump-unhide ()
  13465. "Unhide the current position, to show the bookmark location."
  13466. (and (org-mode-p)
  13467. (or (org-invisible-p)
  13468. (save-excursion (goto-char (max (point-min) (1- (point))))
  13469. (org-invisible-p)))
  13470. (org-show-context 'bookmark-jump)))
  13471. ;; Make session.el ignore our circular variable
  13472. (eval-after-load "session"
  13473. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13474. ;;;; Experimental code
  13475. (defun org-closed-in-range ()
  13476. "Sparse tree of items closed in a certain time range.
  13477. Still experimental, may disappear in the future."
  13478. (interactive)
  13479. ;; Get the time interval from the user.
  13480. (let* ((time1 (time-to-seconds
  13481. (org-read-date nil 'to-time nil "Starting date: ")))
  13482. (time2 (time-to-seconds
  13483. (org-read-date nil 'to-time nil "End date:")))
  13484. ;; callback function
  13485. (callback (lambda ()
  13486. (let ((time
  13487. (time-to-seconds
  13488. (apply 'encode-time
  13489. (org-parse-time-string
  13490. (match-string 1))))))
  13491. ;; check if time in interval
  13492. (and (>= time time1) (<= time time2))))))
  13493. ;; make tree, check each match with the callback
  13494. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13495. ;;;; Finish up
  13496. (provide 'org)
  13497. (run-hooks 'org-load-hook)
  13498. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13499. ;;; org.el ends here