org.el 542 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753
  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. (defcustom org-use-extra-keys nil
  228. "Non-nil means use extra key sequence definitions for certain
  229. commands. This happens automatically if you run XEmacs or if
  230. window-system is nil. This variable lets you do the same
  231. manually. You must set it before loading org.
  232. Example: on Carbon Emacs 22 running graphically, with an external
  233. keyboard on a Powerbook, the default way of setting M-left might
  234. not work for either Alt or ESC. Setting this variable will make
  235. it work for ESC."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (if (fboundp 'defvaralias)
  239. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  240. (defcustom org-disputed-keys
  241. '(([(shift up)] . [(meta p)])
  242. ([(shift down)] . [(meta n)])
  243. ([(shift left)] . [(meta -)])
  244. ([(shift right)] . [(meta +)])
  245. ([(control shift right)] . [(meta shift +)])
  246. ([(control shift left)] . [(meta shift -)]))
  247. "Keys for which Org-mode and other modes compete.
  248. This is an alist, cars are the default keys, second element specifies
  249. the alternative to use when `org-replace-disputed-keys' is t.
  250. Keys can be specified in any syntax supported by `define-key'.
  251. The value of this option takes effect only at Org-mode's startup,
  252. therefore you'll have to restart Emacs to apply it after changing."
  253. :group 'org-startup
  254. :type 'alist)
  255. (defun org-key (key)
  256. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  257. Or return the original if not disputed."
  258. (if org-replace-disputed-keys
  259. (let* ((nkey (key-description key))
  260. (x (org-find-if (lambda (x)
  261. (equal (key-description (car x)) nkey))
  262. org-disputed-keys)))
  263. (if x (cdr x) key))
  264. key))
  265. (defun org-find-if (predicate seq)
  266. (catch 'exit
  267. (while seq
  268. (if (funcall predicate (car seq))
  269. (throw 'exit (car seq))
  270. (pop seq)))))
  271. (defun org-defkey (keymap key def)
  272. "Define a key, possibly translated, as returned by `org-key'."
  273. (define-key keymap (org-key key) def))
  274. (defcustom org-ellipsis nil
  275. "The ellipsis to use in the Org-mode outline.
  276. When nil, just use the standard three dots. When a string, use that instead,
  277. When a face, use the standart 3 dots, but with the specified face.
  278. The change affects only Org-mode (which will then use its own display table).
  279. Changing this requires executing `M-x org-mode' in a buffer to become
  280. effective."
  281. :group 'org-startup
  282. :type '(choice (const :tag "Default" nil)
  283. (face :tag "Face" :value org-warning)
  284. (string :tag "String" :value "...#")))
  285. (defvar org-display-table nil
  286. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  287. (defgroup org-keywords nil
  288. "Keywords in Org-mode."
  289. :tag "Org Keywords"
  290. :group 'org)
  291. (defcustom org-deadline-string "DEADLINE:"
  292. "String to mark deadline entries.
  293. A deadline is this string, followed by a time stamp. Should be a word,
  294. terminated by a colon. You can insert a schedule keyword and
  295. a timestamp with \\[org-deadline].
  296. Changes become only effective after restarting Emacs."
  297. :group 'org-keywords
  298. :type 'string)
  299. (defcustom org-scheduled-string "SCHEDULED:"
  300. "String to mark scheduled TODO entries.
  301. A schedule is this string, followed by a time stamp. Should be a word,
  302. terminated by a colon. You can insert a schedule keyword and
  303. a timestamp with \\[org-schedule].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-closed-string "CLOSED:"
  308. "String used as the prefix for timestamps logging closing a TODO entry."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-clock-string "CLOCK:"
  312. "String used as prefix for timestamps clocking work hours on an item."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-comment-string "COMMENT"
  316. "Entries starting with this keyword will never be exported.
  317. An entry can be toggled between COMMENT and normal with
  318. \\[org-toggle-comment].
  319. Changes become only effective after restarting Emacs."
  320. :group 'org-keywords
  321. :type 'string)
  322. (defcustom org-quote-string "QUOTE"
  323. "Entries starting with this keyword will be exported in fixed-width font.
  324. Quoting applies only to the text in the entry following the headline, and does
  325. not extend beyond the next headline, even if that is lower level.
  326. An entry can be toggled between QUOTE and normal with
  327. \\[org-toggle-fixed-width-section]."
  328. :group 'org-keywords
  329. :type 'string)
  330. (defconst org-repeat-re
  331. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  332. "Regular expression for specifying repeated events.
  333. After a match, group 1 contains the repeat expression.")
  334. (defgroup org-structure nil
  335. "Options concerning the general structure of Org-mode files."
  336. :tag "Org Structure"
  337. :group 'org)
  338. (defgroup org-reveal-location nil
  339. "Options about how to make context of a location visible."
  340. :tag "Org Reveal Location"
  341. :group 'org-structure)
  342. (defconst org-context-choice
  343. '(choice
  344. (const :tag "Always" t)
  345. (const :tag "Never" nil)
  346. (repeat :greedy t :tag "Individual contexts"
  347. (cons
  348. (choice :tag "Context"
  349. (const agenda)
  350. (const org-goto)
  351. (const occur-tree)
  352. (const tags-tree)
  353. (const link-search)
  354. (const mark-goto)
  355. (const bookmark-jump)
  356. (const isearch)
  357. (const default))
  358. (boolean))))
  359. "Contexts for the reveal options.")
  360. (defcustom org-show-hierarchy-above '((default . t))
  361. "Non-nil means, show full hierarchy when revealing a location.
  362. Org-mode often shows locations in an org-mode file which might have
  363. been invisible before. When this is set, the hierarchy of headings
  364. above the exposed location is shown.
  365. Turning this off for example for sparse trees makes them very compact.
  366. Instead of t, this can also be an alist specifying this option for different
  367. contexts. Valid contexts are
  368. agenda when exposing an entry from the agenda
  369. org-goto when using the command `org-goto' on key C-c C-j
  370. occur-tree when using the command `org-occur' on key C-c /
  371. tags-tree when constructing a sparse tree based on tags matches
  372. link-search when exposing search matches associated with a link
  373. mark-goto when exposing the jump goal of a mark
  374. bookmark-jump when exposing a bookmark location
  375. isearch when exiting from an incremental search
  376. default default for all contexts not set explicitly"
  377. :group 'org-reveal-location
  378. :type org-context-choice)
  379. (defcustom org-show-following-heading '((default . nil))
  380. "Non-nil means, show following heading when revealing a location.
  381. Org-mode often shows locations in an org-mode file which might have
  382. been invisible before. When this is set, the heading following the
  383. match is shown.
  384. Turning this off for example for sparse trees makes them very compact,
  385. but makes it harder to edit the location of the match. In such a case,
  386. use the command \\[org-reveal] to show more context.
  387. Instead of t, this can also be an alist specifying this option for different
  388. contexts. See `org-show-hierarchy-above' for valid contexts."
  389. :group 'org-reveal-location
  390. :type org-context-choice)
  391. (defcustom org-show-siblings '((default . nil) (isearch t))
  392. "Non-nil means, show all sibling heading when revealing a location.
  393. Org-mode often shows locations in an org-mode file which might have
  394. been invisible before. When this is set, the sibling of the current entry
  395. heading are all made visible. If `org-show-hierarchy-above' is t,
  396. the same happens on each level of the hierarchy above the current entry.
  397. By default this is on for the isearch context, off for all other contexts.
  398. Turning this off for example for sparse trees makes them very compact,
  399. but makes it harder to edit the location of the match. In such a case,
  400. use the command \\[org-reveal] to show more context.
  401. Instead of t, this can also be an alist specifying this option for different
  402. contexts. See `org-show-hierarchy-above' for valid contexts."
  403. :group 'org-reveal-location
  404. :type org-context-choice)
  405. (defcustom org-show-entry-below '((default . nil))
  406. "Non-nil means, show the entry below a headline when revealing a location.
  407. Org-mode often shows locations in an org-mode file which might have
  408. been invisible before. When this is set, the text below the headline that is
  409. exposed is also shown.
  410. By default this is off for all contexts.
  411. Instead of t, this can also be an alist specifying this option for different
  412. contexts. See `org-show-hierarchy-above' for valid contexts."
  413. :group 'org-reveal-location
  414. :type org-context-choice)
  415. (defcustom org-indirect-buffer-display 'other-window
  416. "How should indirect tree buffers be displayed?
  417. This applies to indirect buffers created with the commands
  418. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  419. Valid values are:
  420. current-window Display in the current window
  421. other-window Just display in another window.
  422. dedicated-frame Create one new frame, and re-use it each time.
  423. new-frame Make a new frame each time. Note that in this case
  424. previously-made indirect buffers are kept, and you need to
  425. kill these buffers yourself."
  426. :group 'org-structure
  427. :group 'org-agenda-windows
  428. :type '(choice
  429. (const :tag "In current window" current-window)
  430. (const :tag "In current frame, other window" other-window)
  431. (const :tag "Each time a new frame" new-frame)
  432. (const :tag "One dedicated frame" dedicated-frame)))
  433. (defgroup org-cycle nil
  434. "Options concerning visibility cycling in Org-mode."
  435. :tag "Org Cycle"
  436. :group 'org-structure)
  437. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  438. "Names of drawers. Drawers are not opened by cycling on the headline above.
  439. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  440. this:
  441. :DRAWERNAME:
  442. .....
  443. :END:
  444. The drawer \"PROPERTIES\" is special for capturing properties through
  445. the property API.
  446. Drawers can be defined on the per-file basis with a line like:
  447. #+DRAWERS: HIDDEN STATE PROPERTIES"
  448. :group 'org-structure
  449. :type '(repeat (string :tag "Drawer Name")))
  450. (defcustom org-cycle-global-at-bob nil
  451. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  452. This makes it possible to do global cycling without having to use S-TAB or
  453. C-u TAB. For this special case to work, the first line of the buffer
  454. must not be a headline - it may be empty ot some other text. When used in
  455. this way, `org-cycle-hook' is disables temporarily, to make sure the
  456. cursor stays at the beginning of the buffer.
  457. When this option is nil, don't do anything special at the beginning
  458. of the buffer."
  459. :group 'org-cycle
  460. :type 'boolean)
  461. (defcustom org-cycle-emulate-tab t
  462. "Where should `org-cycle' emulate TAB.
  463. nil Never
  464. white Only in completely white lines
  465. whitestart Only at the beginning of lines, before the first non-white char
  466. t Everywhere except in headlines
  467. exc-hl-bol Everywhere except at the start of a headline
  468. If TAB is used in a place where it does not emulate TAB, the current subtree
  469. visibility is cycled."
  470. :group 'org-cycle
  471. :type '(choice (const :tag "Never" nil)
  472. (const :tag "Only in completely white lines" white)
  473. (const :tag "Before first char in a line" whitestart)
  474. (const :tag "Everywhere except in headlines" t)
  475. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  476. ))
  477. (defcustom org-cycle-separator-lines 2
  478. "Number of empty lines needed to keep an empty line between collapsed trees.
  479. If you leave an empty line between the end of a subtree and the following
  480. headline, this empty line is hidden when the subtree is folded.
  481. Org-mode will leave (exactly) one empty line visible if the number of
  482. empty lines is equal or larger to the number given in this variable.
  483. So the default 2 means, at least 2 empty lines after the end of a subtree
  484. are needed to produce free space between a collapsed subtree and the
  485. following headline.
  486. Special case: when 0, never leave empty lines in collapsed view."
  487. :group 'org-cycle
  488. :type 'integer)
  489. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  490. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  491. org-cycle-hide-drawers
  492. org-cycle-show-empty-lines
  493. org-optimize-window-after-visibility-change)
  494. "Hook that is run after `org-cycle' has changed the buffer visibility.
  495. The function(s) in this hook must accept a single argument which indicates
  496. the new state that was set by the most recent `org-cycle' command. The
  497. argument is a symbol. After a global state change, it can have the values
  498. `overview', `content', or `all'. After a local state change, it can have
  499. the values `folded', `children', or `subtree'."
  500. :group 'org-cycle
  501. :type 'hook)
  502. (defgroup org-edit-structure nil
  503. "Options concerning structure editing in Org-mode."
  504. :tag "Org Edit Structure"
  505. :group 'org-structure)
  506. (defcustom org-odd-levels-only nil
  507. "Non-nil means, skip even levels and only use odd levels for the outline.
  508. This has the effect that two stars are being added/taken away in
  509. promotion/demotion commands. It also influences how levels are
  510. handled by the exporters.
  511. Changing it requires restart of `font-lock-mode' to become effective
  512. for fontification also in regions already fontified.
  513. You may also set this on a per-file basis by adding one of the following
  514. lines to the buffer:
  515. #+STARTUP: odd
  516. #+STARTUP: oddeven"
  517. :group 'org-edit-structure
  518. :group 'org-font-lock
  519. :type 'boolean)
  520. (defcustom org-adapt-indentation t
  521. "Non-nil means, adapt indentation when promoting and demoting.
  522. When this is set and the *entire* text in an entry is indented, the
  523. indentation is increased by one space in a demotion command, and
  524. decreased by one in a promotion command. If any line in the entry
  525. body starts at column 0, indentation is not changed at all."
  526. :group 'org-edit-structure
  527. :type 'boolean)
  528. (defcustom org-special-ctrl-a/e nil
  529. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  530. When t, `C-a' will bring back the cursor to the beginning of the
  531. headline text, i.e. after the stars and after a possible TODO keyword.
  532. In an item, this will be the position after the bullet.
  533. When the cursor is already at that position, another `C-a' will bring
  534. it to the beginning of the line.
  535. `C-e' will jump to the end of the headline, ignoring the presence of tags
  536. in the headline. A second `C-e' will then jump to the true end of the
  537. line, after any tags.
  538. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  539. and only a directly following, identical keypress will bring the cursor
  540. to the special positions."
  541. :group 'org-edit-structure
  542. :type '(choice
  543. (const :tag "off" nil)
  544. (const :tag "after bullet first" t)
  545. (const :tag "border first" reversed)))
  546. (if (fboundp 'defvaralias)
  547. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  548. (defcustom org-special-ctrl-k nil
  549. "Non-nil means `C-k' will behave specially in headlines.
  550. When nil, `C-k' will call the default `kill-line' command.
  551. When t, the following will happen while the cursor is in the headline:
  552. - When the cursor is at the beginning of a headline, kill the entire
  553. line and possible the folded subtree below the line.
  554. - When in the middle of the headline text, kill the headline up to the tags.
  555. - When after the headline text, kill the tags."
  556. :group 'org-edit-structure
  557. :type 'boolean)
  558. (defcustom org-M-RET-may-split-line '((default . t))
  559. "Non-nil means, M-RET will split the line at the cursor position.
  560. When nil, it will go to the end of the line before making a
  561. new line.
  562. You may also set this option in a different way for different
  563. contexts. Valid contexts are:
  564. headline when creating a new headline
  565. item when creating a new item
  566. table in a table field
  567. default the value to be used for all contexts not explicitly
  568. customized"
  569. :group 'org-structure
  570. :group 'org-table
  571. :type '(choice
  572. (const :tag "Always" t)
  573. (const :tag "Never" nil)
  574. (repeat :greedy t :tag "Individual contexts"
  575. (cons
  576. (choice :tag "Context"
  577. (const headline)
  578. (const item)
  579. (const table)
  580. (const default))
  581. (boolean)))))
  582. (defcustom org-blank-before-new-entry '((heading . nil)
  583. (plain-list-item . nil))
  584. "Should `org-insert-heading' leave a blank line before new heading/item?
  585. The value is an alist, with `heading' and `plain-list-item' as car,
  586. and a boolean flag as cdr."
  587. :group 'org-edit-structure
  588. :type '(list
  589. (cons (const heading) (boolean))
  590. (cons (const plain-list-item) (boolean))))
  591. (defcustom org-insert-heading-hook nil
  592. "Hook being run after inserting a new heading."
  593. :group 'org-edit-structure
  594. :type 'hook)
  595. (defcustom org-enable-fixed-width-editor t
  596. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  597. This currently only means, they are never auto-wrapped.
  598. When nil, such lines will be treated like ordinary lines.
  599. See also the QUOTE keyword."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  603. "The mode that should be used to edit fixed-width regions.
  604. These are the regions where each line starts with a colon."
  605. :group 'org-edit-structure
  606. :type '(choice
  607. (const artist-mode)
  608. (const picture-mode)
  609. (const fundamental-mode)
  610. (function :tag "Other (specify)")))
  611. (defcustom org-goto-auto-isearch t
  612. "Non-nil means, typing characters in org-goto starts incremental search."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defgroup org-sparse-trees nil
  616. "Options concerning sparse trees in Org-mode."
  617. :tag "Org Sparse Trees"
  618. :group 'org-structure)
  619. (defcustom org-highlight-sparse-tree-matches t
  620. "Non-nil means, highlight all matches that define a sparse tree.
  621. The highlights will automatically disappear the next time the buffer is
  622. changed by an edit command."
  623. :group 'org-sparse-trees
  624. :type 'boolean)
  625. (defcustom org-remove-highlights-with-change t
  626. "Non-nil means, any change to the buffer will remove temporary highlights.
  627. Such highlights are created by `org-occur' and `org-clock-display'.
  628. When nil, `C-c C-c needs to be used to get rid of the highlights.
  629. The highlights created by `org-preview-latex-fragment' always need
  630. `C-c C-c' to be removed."
  631. :group 'org-sparse-trees
  632. :group 'org-time
  633. :type 'boolean)
  634. (defcustom org-occur-hook '(org-first-headline-recenter)
  635. "Hook that is run after `org-occur' has constructed a sparse tree.
  636. This can be used to recenter the window to show as much of the structure
  637. as possible."
  638. :group 'org-sparse-trees
  639. :type 'hook)
  640. (defgroup org-plain-lists nil
  641. "Options concerning plain lists in Org-mode."
  642. :tag "Org Plain lists"
  643. :group 'org-structure)
  644. (defcustom org-cycle-include-plain-lists nil
  645. "Non-nil means, include plain lists into visibility cycling.
  646. This means that during cycling, plain list items will *temporarily* be
  647. interpreted as outline headlines with a level given by 1000+i where i is the
  648. indentation of the bullet. In all other operations, plain list items are
  649. not seen as headlines. For example, you cannot assign a TODO keyword to
  650. such an item."
  651. :group 'org-plain-lists
  652. :type 'boolean)
  653. (defcustom org-plain-list-ordered-item-terminator t
  654. "The character that makes a line with leading number an ordered list item.
  655. Valid values are ?. and ?\). To get both terminators, use t. While
  656. ?. may look nicer, it creates the danger that a line with leading
  657. number may be incorrectly interpreted as an item. ?\) therefore is
  658. the safe choice."
  659. :group 'org-plain-lists
  660. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  661. (const :tag "paren like in \"2)\"" ?\))
  662. (const :tab "both" t)))
  663. (defcustom org-empty-line-terminates-plain-lists nil
  664. "Non-nil means, an empty line ends all plain list levels.
  665. When nil, empty lines are part of the preceeding item."
  666. :group 'org-plain-lists
  667. :type 'boolean)
  668. (defcustom org-auto-renumber-ordered-lists t
  669. "Non-nil means, automatically renumber ordered plain lists.
  670. Renumbering happens when the sequence have been changed with
  671. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  672. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  673. :group 'org-plain-lists
  674. :type 'boolean)
  675. (defcustom org-provide-checkbox-statistics t
  676. "Non-nil means, update checkbox statistics after insert and toggle.
  677. When this is set, checkbox statistics is updated each time you either insert
  678. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  679. with \\[org-ctrl-c-ctrl-c\\]."
  680. :group 'org-plain-lists
  681. :type 'boolean)
  682. (defcustom org-description-max-indent 20
  683. "Maximum indentation for the second line of a description list.
  684. When the indentation would be larger than this, it will become
  685. 5 characters instead."
  686. :group 'org-plain-lists
  687. :type 'integer)
  688. (defgroup org-imenu-and-speedbar nil
  689. "Options concerning imenu and speedbar in Org-mode."
  690. :tag "Org Imenu and Speedbar"
  691. :group 'org-structure)
  692. (defcustom org-imenu-depth 2
  693. "The maximum level for Imenu access to Org-mode headlines.
  694. This also applied for speedbar access."
  695. :group 'org-imenu-and-speedbar
  696. :type 'number)
  697. (defgroup org-table nil
  698. "Options concerning tables in Org-mode."
  699. :tag "Org Table"
  700. :group 'org)
  701. (defcustom org-enable-table-editor 'optimized
  702. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  703. When nil, such lines will be treated like ordinary lines.
  704. When equal to the symbol `optimized', the table editor will be optimized to
  705. do the following:
  706. - Automatic overwrite mode in front of whitespace in table fields.
  707. This makes the structure of the table stay in tact as long as the edited
  708. field does not exceed the column width.
  709. - Minimize the number of realigns. Normally, the table is aligned each time
  710. TAB or RET are pressed to move to another field. With optimization this
  711. happens only if changes to a field might have changed the column width.
  712. Optimization requires replacing the functions `self-insert-command',
  713. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  714. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  715. very good at guessing when a re-align will be necessary, but you can always
  716. force one with \\[org-ctrl-c-ctrl-c].
  717. If you would like to use the optimized version in Org-mode, but the
  718. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  719. This variable can be used to turn on and off the table editor during a session,
  720. but in order to toggle optimization, a restart is required.
  721. See also the variable `org-table-auto-blank-field'."
  722. :group 'org-table
  723. :type '(choice
  724. (const :tag "off" nil)
  725. (const :tag "on" t)
  726. (const :tag "on, optimized" optimized)))
  727. (defcustom org-table-tab-recognizes-table.el t
  728. "Non-nil means, TAB will automatically notice a table.el table.
  729. When it sees such a table, it moves point into it and - if necessary -
  730. calls `table-recognize-table'."
  731. :group 'org-table-editing
  732. :type 'boolean)
  733. (defgroup org-link nil
  734. "Options concerning links in Org-mode."
  735. :tag "Org Link"
  736. :group 'org)
  737. (defvar org-link-abbrev-alist-local nil
  738. "Buffer-local version of `org-link-abbrev-alist', which see.
  739. The value of this is taken from the #+LINK lines.")
  740. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  741. (defcustom org-link-abbrev-alist nil
  742. "Alist of link abbreviations.
  743. The car of each element is a string, to be replaced at the start of a link.
  744. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  745. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  746. [[linkkey:tag][description]]
  747. If REPLACE is a string, the tag will simply be appended to create the link.
  748. If the string contains \"%s\", the tag will be inserted there.
  749. REPLACE may also be a function that will be called with the tag as the
  750. only argument to create the link, which should be returned as a string.
  751. See the manual for examples."
  752. :group 'org-link
  753. :type 'alist)
  754. (defcustom org-descriptive-links t
  755. "Non-nil means, hide link part and only show description of bracket links.
  756. Bracket links are like [[link][descritpion]]. This variable sets the initial
  757. state in new org-mode buffers. The setting can then be toggled on a
  758. per-buffer basis from the Org->Hyperlinks menu."
  759. :group 'org-link
  760. :type 'boolean)
  761. (defcustom org-link-file-path-type 'adaptive
  762. "How the path name in file links should be stored.
  763. Valid values are:
  764. relative Relative to the current directory, i.e. the directory of the file
  765. into which the link is being inserted.
  766. absolute Absolute path, if possible with ~ for home directory.
  767. noabbrev Absolute path, no abbreviation of home directory.
  768. adaptive Use relative path for files in the current directory and sub-
  769. directories of it. For other files, use an absolute path."
  770. :group 'org-link
  771. :type '(choice
  772. (const relative)
  773. (const absolute)
  774. (const noabbrev)
  775. (const adaptive)))
  776. (defcustom org-activate-links '(bracket angle plain radio tag date)
  777. "Types of links that should be activated in Org-mode files.
  778. This is a list of symbols, each leading to the activation of a certain link
  779. type. In principle, it does not hurt to turn on most link types - there may
  780. be a small gain when turning off unused link types. The types are:
  781. bracket The recommended [[link][description]] or [[link]] links with hiding.
  782. angular Links in angular brackes that may contain whitespace like
  783. <bbdb:Carsten Dominik>.
  784. plain Plain links in normal text, no whitespace, like http://google.com.
  785. radio Text that is matched by a radio target, see manual for details.
  786. tag Tag settings in a headline (link to tag search).
  787. date Time stamps (link to calendar).
  788. Changing this variable requires a restart of Emacs to become effective."
  789. :group 'org-link
  790. :type '(set (const :tag "Double bracket links (new style)" bracket)
  791. (const :tag "Angular bracket links (old style)" angular)
  792. (const :tag "Plain text links" plain)
  793. (const :tag "Radio target matches" radio)
  794. (const :tag "Tags" tag)
  795. (const :tag "Timestamps" date)))
  796. (defcustom org-make-link-description-function nil
  797. "Function to use to generate link descriptions from links. If
  798. nil the link location will be used. This function must take two
  799. parameters; the first is the link and the second the description
  800. org-insert-link has generated, and should return the description
  801. to use."
  802. :group 'org-link
  803. :type 'function)
  804. (defgroup org-link-store nil
  805. "Options concerning storing links in Org-mode."
  806. :tag "Org Store Link"
  807. :group 'org-link)
  808. (defcustom org-email-link-description-format "Email %c: %.30s"
  809. "Format of the description part of a link to an email or usenet message.
  810. The following %-excapes will be replaced by corresponding information:
  811. %F full \"From\" field
  812. %f name, taken from \"From\" field, address if no name
  813. %T full \"To\" field
  814. %t first name in \"To\" field, address if no name
  815. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  816. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  817. %s subject
  818. %m message-id.
  819. You may use normal field width specification between the % and the letter.
  820. This is for example useful to limit the length of the subject.
  821. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  822. :group 'org-link-store
  823. :type 'string)
  824. (defcustom org-from-is-user-regexp
  825. (let (r1 r2)
  826. (when (and user-mail-address (not (string= user-mail-address "")))
  827. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  828. (when (and user-full-name (not (string= user-full-name "")))
  829. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  830. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  831. "Regexp mached against the \"From:\" header of an email or usenet message.
  832. It should match if the message is from the user him/herself."
  833. :group 'org-link-store
  834. :type 'regexp)
  835. (defcustom org-context-in-file-links t
  836. "Non-nil means, file links from `org-store-link' contain context.
  837. A search string will be added to the file name with :: as separator and
  838. used to find the context when the link is activated by the command
  839. `org-open-at-point'.
  840. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  841. negates this setting for the duration of the command."
  842. :group 'org-link-store
  843. :type 'boolean)
  844. (defcustom org-keep-stored-link-after-insertion nil
  845. "Non-nil means, keep link in list for entire session.
  846. The command `org-store-link' adds a link pointing to the current
  847. location to an internal list. These links accumulate during a session.
  848. The command `org-insert-link' can be used to insert links into any
  849. Org-mode file (offering completion for all stored links). When this
  850. option is nil, every link which has been inserted once using \\[org-insert-link]
  851. will be removed from the list, to make completing the unused links
  852. more efficient."
  853. :group 'org-link-store
  854. :type 'boolean)
  855. (defgroup org-link-follow nil
  856. "Options concerning following links in Org-mode."
  857. :tag "Org Follow Link"
  858. :group 'org-link)
  859. (defcustom org-follow-link-hook nil
  860. "Hook that is run after a link has been followed."
  861. :group 'org-link-follow
  862. :type 'hook)
  863. (defcustom org-tab-follows-link nil
  864. "Non-nil means, on links TAB will follow the link.
  865. Needs to be set before org.el is loaded."
  866. :group 'org-link-follow
  867. :type 'boolean)
  868. (defcustom org-return-follows-link nil
  869. "Non-nil means, on links RET will follow the link.
  870. Needs to be set before org.el is loaded."
  871. :group 'org-link-follow
  872. :type 'boolean)
  873. (defcustom org-mouse-1-follows-link
  874. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  875. "Non-nil means, mouse-1 on a link will follow the link.
  876. A longer mouse click will still set point. Does not work on XEmacs.
  877. Needs to be set before org.el is loaded."
  878. :group 'org-link-follow
  879. :type 'boolean)
  880. (defcustom org-mark-ring-length 4
  881. "Number of different positions to be recorded in the ring
  882. Changing this requires a restart of Emacs to work correctly."
  883. :group 'org-link-follow
  884. :type 'interger)
  885. (defcustom org-link-frame-setup
  886. '((vm . vm-visit-folder-other-frame)
  887. (gnus . gnus-other-frame)
  888. (file . find-file-other-window))
  889. "Setup the frame configuration for following links.
  890. When following a link with Emacs, it may often be useful to display
  891. this link in another window or frame. This variable can be used to
  892. set this up for the different types of links.
  893. For VM, use any of
  894. `vm-visit-folder'
  895. `vm-visit-folder-other-frame'
  896. For Gnus, use any of
  897. `gnus'
  898. `gnus-other-frame'
  899. For FILE, use any of
  900. `find-file'
  901. `find-file-other-window'
  902. `find-file-other-frame'
  903. For the calendar, use the variable `calendar-setup'.
  904. For BBDB, it is currently only possible to display the matches in
  905. another window."
  906. :group 'org-link-follow
  907. :type '(list
  908. (cons (const vm)
  909. (choice
  910. (const vm-visit-folder)
  911. (const vm-visit-folder-other-window)
  912. (const vm-visit-folder-other-frame)))
  913. (cons (const gnus)
  914. (choice
  915. (const gnus)
  916. (const gnus-other-frame)))
  917. (cons (const file)
  918. (choice
  919. (const find-file)
  920. (const find-file-other-window)
  921. (const find-file-other-frame)))))
  922. (defcustom org-display-internal-link-with-indirect-buffer nil
  923. "Non-nil means, use indirect buffer to display infile links.
  924. Activating internal links (from one location in a file to another location
  925. in the same file) normally just jumps to the location. When the link is
  926. activated with a C-u prefix (or with mouse-3), the link is displayed in
  927. another window. When this option is set, the other window actually displays
  928. an indirect buffer clone of the current buffer, to avoid any visibility
  929. changes to the current buffer."
  930. :group 'org-link-follow
  931. :type 'boolean)
  932. (defcustom org-open-non-existing-files nil
  933. "Non-nil means, `org-open-file' will open non-existing files.
  934. When nil, an error will be generated."
  935. :group 'org-link-follow
  936. :type 'boolean)
  937. (defcustom org-open-directory-means-index-dot-org nil
  938. "Non-nil means, a link to a directory really means to index.org.
  939. When nil, following a directory link will run dired or open a finder/explorer
  940. window on that directory."
  941. :group 'org-link-follow
  942. :type 'boolean)
  943. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  944. "Function and arguments to call for following mailto links.
  945. This is a list with the first element being a lisp function, and the
  946. remaining elements being arguments to the function. In string arguments,
  947. %a will be replaced by the address, and %s will be replaced by the subject
  948. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  952. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  953. (const :tag "message-mail" (message-mail "%a" "%s"))
  954. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  955. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  956. "Non-nil means, ask for confirmation before executing shell links.
  957. Shell links can be dangerous: just think about a link
  958. [[shell:rm -rf ~/*][Google Search]]
  959. This link would show up in your Org-mode document as \"Google Search\",
  960. but really it would remove your entire home directory.
  961. Therefore we advise against setting this variable to nil.
  962. Just change it to `y-or-n-p' of you want to confirm with a
  963. single keystroke rather than having to type \"yes\"."
  964. :group 'org-link-follow
  965. :type '(choice
  966. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  967. (const :tag "with y-or-n (faster)" y-or-n-p)
  968. (const :tag "no confirmation (dangerous)" nil)))
  969. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  970. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  971. Elisp links can be dangerous: just think about a link
  972. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  973. This link would show up in your Org-mode document as \"Google Search\",
  974. but really it would remove your entire home directory.
  975. Therefore we advise against setting this variable to nil.
  976. Just change it to `y-or-n-p' of you want to confirm with a
  977. single keystroke rather than having to type \"yes\"."
  978. :group 'org-link-follow
  979. :type '(choice
  980. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  981. (const :tag "with y-or-n (faster)" y-or-n-p)
  982. (const :tag "no confirmation (dangerous)" nil)))
  983. (defconst org-file-apps-defaults-gnu
  984. '((remote . emacs)
  985. (t . mailcap))
  986. "Default file applications on a UNIX or GNU/Linux system.
  987. See `org-file-apps'.")
  988. (defconst org-file-apps-defaults-macosx
  989. '((remote . emacs)
  990. (t . "open %s")
  991. ("ps" . "gv %s")
  992. ("ps.gz" . "gv %s")
  993. ("eps" . "gv %s")
  994. ("eps.gz" . "gv %s")
  995. ("dvi" . "xdvi %s")
  996. ("fig" . "xfig %s"))
  997. "Default file applications on a MacOS X system.
  998. The system \"open\" is known as a default, but we use X11 applications
  999. for some files for which the OS does not have a good default.
  1000. See `org-file-apps'.")
  1001. (defconst org-file-apps-defaults-windowsnt
  1002. (list
  1003. '(remote . emacs)
  1004. (cons t
  1005. (list (if (featurep 'xemacs)
  1006. 'mswindows-shell-execute
  1007. 'w32-shell-execute)
  1008. "open" 'file)))
  1009. "Default file applications on a Windows NT system.
  1010. The system \"open\" is used for most files.
  1011. See `org-file-apps'.")
  1012. (defcustom org-file-apps
  1013. '(
  1014. ("txt" . emacs)
  1015. ("tex" . emacs)
  1016. ("ltx" . emacs)
  1017. ("org" . emacs)
  1018. ("el" . emacs)
  1019. ("bib" . emacs)
  1020. )
  1021. "External applications for opening `file:path' items in a document.
  1022. Org-mode uses system defaults for different file types, but
  1023. you can use this variable to set the application for a given file
  1024. extension. The entries in this list are cons cells where the car identifies
  1025. files and the cdr the corresponding command. Possible values for the
  1026. file identifier are
  1027. \"ext\" A string identifying an extension
  1028. `directory' Matches a directory
  1029. `remote' Matches a remote file, accessible through tramp or efs.
  1030. Remote files most likely should be visited through Emacs
  1031. because external applications cannot handle such paths.
  1032. t Default for all remaining files
  1033. Possible values for the command are:
  1034. `emacs' The file will be visited by the current Emacs process.
  1035. `default' Use the default application for this file type.
  1036. string A command to be executed by a shell; %s will be replaced
  1037. by the path to the file.
  1038. sexp A Lisp form which will be evaluated. The file path will
  1039. be available in the Lisp variable `file'.
  1040. For more examples, see the system specific constants
  1041. `org-file-apps-defaults-macosx'
  1042. `org-file-apps-defaults-windowsnt'
  1043. `org-file-apps-defaults-gnu'."
  1044. :group 'org-link-follow
  1045. :type '(repeat
  1046. (cons (choice :value ""
  1047. (string :tag "Extension")
  1048. (const :tag "Default for unrecognized files" t)
  1049. (const :tag "Remote file" remote)
  1050. (const :tag "Links to a directory" directory))
  1051. (choice :value ""
  1052. (const :tag "Visit with Emacs" emacs)
  1053. (const :tag "Use system default" default)
  1054. (string :tag "Command")
  1055. (sexp :tag "Lisp form")))))
  1056. (defgroup org-refile nil
  1057. "Options concerning refiling entries in Org-mode."
  1058. :tag "Org Remember"
  1059. :group 'org)
  1060. (defcustom org-directory "~/org"
  1061. "Directory with org files.
  1062. This directory will be used as default to prompt for org files.
  1063. Used by the hooks for remember.el."
  1064. :group 'org-refile
  1065. :group 'org-remember
  1066. :type 'directory)
  1067. (defcustom org-default-notes-file "~/.notes"
  1068. "Default target for storing notes.
  1069. Used by the hooks for remember.el. This can be a string, or nil to mean
  1070. the value of `remember-data-file'.
  1071. You can set this on a per-template basis with the variable
  1072. `org-remember-templates'."
  1073. :group 'org-refile
  1074. :group 'org-remember
  1075. :type '(choice
  1076. (const :tag "Default from remember-data-file" nil)
  1077. file))
  1078. (defcustom org-goto-interface 'outline
  1079. "The default interface to be used for `org-goto'.
  1080. Allowed vaues are:
  1081. outline The interface shows an outline of the relevant file
  1082. and the correct heading is found by moving through
  1083. the outline or by searching with incremental search.
  1084. outline-path-completion Headlines in the current buffer are offered via
  1085. completion."
  1086. :group 'org-refile
  1087. :type '(choice
  1088. (const :tag "Outline" outline)
  1089. (const :tag "Outline-path-completion" outline-path-completion)))
  1090. (defcustom org-reverse-note-order nil
  1091. "Non-nil means, store new notes at the beginning of a file or entry.
  1092. When nil, new notes will be filed to the end of a file or entry.
  1093. This can also be a list with cons cells of regular expressions that
  1094. are matched against file names, and values."
  1095. :group 'org-remember
  1096. :type '(choice
  1097. (const :tag "Reverse always" t)
  1098. (const :tag "Reverse never" nil)
  1099. (repeat :tag "By file name regexp"
  1100. (cons regexp boolean))))
  1101. (defcustom org-refile-targets nil
  1102. "Targets for refiling entries with \\[org-refile].
  1103. This is list of cons cells. Each cell contains:
  1104. - a specification of the files to be considered, either a list of files,
  1105. or a symbol whose function or variable value will be used to retrieve
  1106. a file name or a list of file names. Nil means, refile to a different
  1107. heading in the current buffer.
  1108. - A specification of how to find candidate refile targets. This may be
  1109. any of
  1110. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1111. This tag has to be present in all target headlines, inheritance will
  1112. not be considered.
  1113. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1114. todo keyword.
  1115. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1116. headlines that are refiling targets.
  1117. - a cons cell (:level . N). Any headline of level N is considered a target.
  1118. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1119. When this variable is nil, all top-level headlines in the current buffer
  1120. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1121. :group 'org-remember
  1122. :type '(repeat
  1123. (cons
  1124. (choice :value org-agenda-files
  1125. (const :tag "All agenda files" org-agenda-files)
  1126. (const :tag "Current buffer" nil)
  1127. (function) (variable) (file))
  1128. (choice :tag "Identify target headline by"
  1129. (cons :tag "Specific tag" (const :tag) (string))
  1130. (cons :tag "TODO keyword" (const :todo) (string))
  1131. (cons :tag "Regular expression" (const :regexp) (regexp))
  1132. (cons :tag "Level number" (const :level) (integer))
  1133. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1134. (defcustom org-refile-use-outline-path nil
  1135. "Non-nil means, provide refile targets as paths.
  1136. So a level 3 headline will be available as level1/level2/level3.
  1137. When the value is `file', also include the file name (without directory)
  1138. into the path. When `full-file-path', include the full file path."
  1139. :group 'org-remember
  1140. :type '(choice
  1141. (const :tag "Not" nil)
  1142. (const :tag "Yes" t)
  1143. (const :tag "Start with file name" file)
  1144. (const :tag "Start with full file path" full-file-path)))
  1145. (defgroup org-todo nil
  1146. "Options concerning TODO items in Org-mode."
  1147. :tag "Org TODO"
  1148. :group 'org)
  1149. (defgroup org-progress nil
  1150. "Options concerning Progress logging in Org-mode."
  1151. :tag "Org Progress"
  1152. :group 'org-time)
  1153. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1154. "List of TODO entry keyword sequences and their interpretation.
  1155. \\<org-mode-map>This is a list of sequences.
  1156. Each sequence starts with a symbol, either `sequence' or `type',
  1157. indicating if the keywords should be interpreted as a sequence of
  1158. action steps, or as different types of TODO items. The first
  1159. keywords are states requiring action - these states will select a headline
  1160. for inclusion into the global TODO list Org-mode produces. If one of
  1161. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1162. signify that no further action is necessary. If \"|\" is not found,
  1163. the last keyword is treated as the only DONE state of the sequence.
  1164. The command \\[org-todo] cycles an entry through these states, and one
  1165. additional state where no keyword is present. For details about this
  1166. cycling, see the manual.
  1167. TODO keywords and interpretation can also be set on a per-file basis with
  1168. the special #+SEQ_TODO and #+TYP_TODO lines.
  1169. Each keyword can optionally specify a character for fast state selection
  1170. \(in combination with the variable `org-use-fast-todo-selection')
  1171. and specifiers for state change logging, using the same syntax
  1172. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1173. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1174. indicates to record a time stamp each time this state is selected.
  1175. Each keyword may also specify if a timestamp or a note should be
  1176. recorded when entering or leaving the state, by adding additional
  1177. characters in the parenthesis after the keyword. This looks like this:
  1178. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1179. record only the time of the state change. With X and Y being either
  1180. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1181. Y when leaving the state if and only if the *target* state does not
  1182. define X. You may omit any of the fast-selection key or X or /Y,
  1183. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1184. For backward compatibility, this variable may also be just a list
  1185. of keywords - in this case the interptetation (sequence or type) will be
  1186. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1187. :group 'org-todo
  1188. :group 'org-keywords
  1189. :type '(choice
  1190. (repeat :tag "Old syntax, just keywords"
  1191. (string :tag "Keyword"))
  1192. (repeat :tag "New syntax"
  1193. (cons
  1194. (choice
  1195. :tag "Interpretation"
  1196. (const :tag "Sequence (cycling hits every state)" sequence)
  1197. (const :tag "Type (cycling directly to DONE)" type))
  1198. (repeat
  1199. (string :tag "Keyword"))))))
  1200. (defvar org-todo-keywords-1 nil
  1201. "All TODO and DONE keywords active in a buffer.")
  1202. (make-variable-buffer-local 'org-todo-keywords-1)
  1203. (defvar org-todo-keywords-for-agenda nil)
  1204. (defvar org-done-keywords-for-agenda nil)
  1205. (defvar org-todo-keyword-alist-for-agenda nil)
  1206. (defvar org-tag-alist-for-agenda nil)
  1207. (defvar org-agenda-contributing-files nil)
  1208. (defvar org-not-done-keywords nil)
  1209. (make-variable-buffer-local 'org-not-done-keywords)
  1210. (defvar org-done-keywords nil)
  1211. (make-variable-buffer-local 'org-done-keywords)
  1212. (defvar org-todo-heads nil)
  1213. (make-variable-buffer-local 'org-todo-heads)
  1214. (defvar org-todo-sets nil)
  1215. (make-variable-buffer-local 'org-todo-sets)
  1216. (defvar org-todo-log-states nil)
  1217. (make-variable-buffer-local 'org-todo-log-states)
  1218. (defvar org-todo-kwd-alist nil)
  1219. (make-variable-buffer-local 'org-todo-kwd-alist)
  1220. (defvar org-todo-key-alist nil)
  1221. (make-variable-buffer-local 'org-todo-key-alist)
  1222. (defvar org-todo-key-trigger nil)
  1223. (make-variable-buffer-local 'org-todo-key-trigger)
  1224. (defcustom org-todo-interpretation 'sequence
  1225. "Controls how TODO keywords are interpreted.
  1226. This variable is in principle obsolete and is only used for
  1227. backward compatibility, if the interpretation of todo keywords is
  1228. not given already in `org-todo-keywords'. See that variable for
  1229. more information."
  1230. :group 'org-todo
  1231. :group 'org-keywords
  1232. :type '(choice (const sequence)
  1233. (const type)))
  1234. (defcustom org-use-fast-todo-selection 'prefix
  1235. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1236. This variable describes if and under what circumstances the cycling
  1237. mechanism for TODO keywords will be replaced by a single-key, direct
  1238. selection scheme.
  1239. When nil, fast selection is never used.
  1240. When the symbol `prefix', it will be used when `org-todo' is called with
  1241. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1242. in an agenda buffer.
  1243. When t, fast selection is used by default. In this case, the prefix
  1244. argument forces cycling instead.
  1245. In all cases, the special interface is only used if access keys have actually
  1246. been assigned by the user, i.e. if keywords in the configuration are followed
  1247. by a letter in parenthesis, like TODO(t)."
  1248. :group 'org-todo
  1249. :type '(choice
  1250. (const :tag "Never" nil)
  1251. (const :tag "By default" t)
  1252. (const :tag "Only with C-u C-c C-t" prefix)))
  1253. (defcustom org-provide-todo-statistics t
  1254. "Non-nil means, update todo statistics after insert and toggle.
  1255. When this is set, todo statistics is updated in the parent of the current
  1256. entry each time a todo state is changed."
  1257. :group 'org-todo
  1258. :type 'boolean)
  1259. (defcustom org-after-todo-state-change-hook nil
  1260. "Hook which is run after the state of a TODO item was changed.
  1261. The new state (a string with a TODO keyword, or nil) is available in the
  1262. Lisp variable `state'."
  1263. :group 'org-todo
  1264. :type 'hook)
  1265. (defcustom org-log-done nil
  1266. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1267. When equal to the list (done), also prompt for a closing note.
  1268. This can also be configured on a per-file basis by adding one of
  1269. the following lines anywhere in the buffer:
  1270. #+STARTUP: logdone
  1271. #+STARTUP: lognotedone
  1272. #+STARTUP: nologdone"
  1273. :group 'org-todo
  1274. :group 'org-progress
  1275. :type '(choice
  1276. (const :tag "No logging" nil)
  1277. (const :tag "Record CLOSED timestamp" time)
  1278. (const :tag "Record CLOSED timestamp with closing note." note)))
  1279. ;; Normalize old uses of org-log-done.
  1280. (cond
  1281. ((eq org-log-done t) (setq org-log-done 'time))
  1282. ((and (listp org-log-done) (memq 'done org-log-done))
  1283. (setq org-log-done 'note)))
  1284. (defcustom org-log-note-clock-out nil
  1285. "Non-nil means, record a note when clocking out of an item.
  1286. This can also be configured on a per-file basis by adding one of
  1287. the following lines anywhere in the buffer:
  1288. #+STARTUP: lognoteclock-out
  1289. #+STARTUP: nolognoteclock-out"
  1290. :group 'org-todo
  1291. :group 'org-progress
  1292. :type 'boolean)
  1293. (defcustom org-log-done-with-time t
  1294. "Non-nil means, the CLOSED time stamp will contain date and time.
  1295. When nil, only the date will be recorded."
  1296. :group 'org-progress
  1297. :type 'boolean)
  1298. (defcustom org-log-note-headings
  1299. '((done . "CLOSING NOTE %t")
  1300. (state . "State %-12s %t")
  1301. (note . "Note taken on %t")
  1302. (clock-out . ""))
  1303. "Headings for notes added to entries.
  1304. The value is an alist, with the car being a symbol indicating the note
  1305. context, and the cdr is the heading to be used. The heading may also be the
  1306. empty string.
  1307. %t in the heading will be replaced by a time stamp.
  1308. %s will be replaced by the new TODO state, in double quotes.
  1309. %u will be replaced by the user name.
  1310. %U will be replaced by the full user name."
  1311. :group 'org-todo
  1312. :group 'org-progress
  1313. :type '(list :greedy t
  1314. (cons (const :tag "Heading when closing an item" done) string)
  1315. (cons (const :tag
  1316. "Heading when changing todo state (todo sequence only)"
  1317. state) string)
  1318. (cons (const :tag "Heading when just taking a note" note) string)
  1319. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1320. (unless (assq 'note org-log-note-headings)
  1321. (push '(note . "%t") org-log-note-headings))
  1322. (defcustom org-log-states-order-reversed t
  1323. "Non-nil means, the latest state change note will be directly after heading.
  1324. When nil, the notes will be orderer according to time."
  1325. :group 'org-todo
  1326. :group 'org-progress
  1327. :type 'boolean)
  1328. (defcustom org-log-repeat 'time
  1329. "Non-nil means, record moving through the DONE state when triggering repeat.
  1330. An auto-repeating tasks is immediately switched back to TODO when marked
  1331. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1332. the TODO keyword definition, or recording a closing note by setting
  1333. `org-log-done', there will be no record of the task moving through DONE.
  1334. This variable forces taking a note anyway. Possible values are:
  1335. nil Don't force a record
  1336. time Record a time stamp
  1337. note Record a note
  1338. This option can also be set with on a per-file-basis with
  1339. #+STARTUP: logrepeat
  1340. #+STARTUP: lognoterepeat
  1341. #+STARTUP: nologrepeat
  1342. You can have local logging settings for a subtree by setting the LOGGING
  1343. property to one or more of these keywords."
  1344. :group 'org-todo
  1345. :group 'org-progress
  1346. :type '(choice
  1347. (const :tag "Don't force a record" nil)
  1348. (const :tag "Force recording the DONE state" time)
  1349. (const :tag "Force recording a note with the DONE state" note)))
  1350. (defgroup org-priorities nil
  1351. "Priorities in Org-mode."
  1352. :tag "Org Priorities"
  1353. :group 'org-todo)
  1354. (defcustom org-highest-priority ?A
  1355. "The highest priority of TODO items. A character like ?A, ?B etc.
  1356. Must have a smaller ASCII number than `org-lowest-priority'."
  1357. :group 'org-priorities
  1358. :type 'character)
  1359. (defcustom org-lowest-priority ?C
  1360. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1361. Must have a larger ASCII number than `org-highest-priority'."
  1362. :group 'org-priorities
  1363. :type 'character)
  1364. (defcustom org-default-priority ?B
  1365. "The default priority of TODO items.
  1366. This is the priority an item get if no explicit priority is given."
  1367. :group 'org-priorities
  1368. :type 'character)
  1369. (defcustom org-priority-start-cycle-with-default t
  1370. "Non-nil means, start with default priority when starting to cycle.
  1371. When this is nil, the first step in the cycle will be (depending on the
  1372. command used) one higher or lower that the default priority."
  1373. :group 'org-priorities
  1374. :type 'boolean)
  1375. (defgroup org-time nil
  1376. "Options concerning time stamps and deadlines in Org-mode."
  1377. :tag "Org Time"
  1378. :group 'org)
  1379. (defcustom org-insert-labeled-timestamps-at-point nil
  1380. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1381. When nil, these labeled time stamps are forces into the second line of an
  1382. entry, just after the headline. When scheduling from the global TODO list,
  1383. the time stamp will always be forced into the second line."
  1384. :group 'org-time
  1385. :type 'boolean)
  1386. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1387. "Formats for `format-time-string' which are used for time stamps.
  1388. It is not recommended to change this constant.")
  1389. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1390. "Number of minutes to round time stamps to.
  1391. These are two values, the first applies when first creating a time stamp.
  1392. The second applies when changing it with the commands `S-up' and `S-down'.
  1393. When changing the time stamp, this means that it will change in steps
  1394. of N minutes, as given by the second value.
  1395. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1396. numbers should be factors of 60, so for example 5, 10, 15.
  1397. When this is larger than 1, you can still force an exact time-stamp by using
  1398. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1399. and by using a prefix arg to `S-up/down' to specify the exact number
  1400. of minutes to shift."
  1401. :group 'org-time
  1402. :get '(lambda (var) ; Make sure all entries have 5 elements
  1403. (if (integerp (default-value var))
  1404. (list (default-value var) 5)
  1405. (default-value var)))
  1406. :type '(list
  1407. (integer :tag "when inserting times")
  1408. (integer :tag "when modifying times")))
  1409. ;; Normalize old customizations of this variable.
  1410. (when (integerp org-time-stamp-rounding-minutes)
  1411. (setq org-time-stamp-rounding-minutes
  1412. (list org-time-stamp-rounding-minutes
  1413. org-time-stamp-rounding-minutes)))
  1414. (defcustom org-display-custom-times nil
  1415. "Non-nil means, overlay custom formats over all time stamps.
  1416. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1417. To turn this on on a per-file basis, insert anywhere in the file:
  1418. #+STARTUP: customtime"
  1419. :group 'org-time
  1420. :set 'set-default
  1421. :type 'sexp)
  1422. (make-variable-buffer-local 'org-display-custom-times)
  1423. (defcustom org-time-stamp-custom-formats
  1424. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1425. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1426. These are overlayed over the default ISO format if the variable
  1427. `org-display-custom-times' is set. Time like %H:%M should be at the
  1428. end of the second format."
  1429. :group 'org-time
  1430. :type 'sexp)
  1431. (defun org-time-stamp-format (&optional long inactive)
  1432. "Get the right format for a time string."
  1433. (let ((f (if long (cdr org-time-stamp-formats)
  1434. (car org-time-stamp-formats))))
  1435. (if inactive
  1436. (concat "[" (substring f 1 -1) "]")
  1437. f)))
  1438. (defcustom org-time-clocksum-format "%d:%02d"
  1439. "The format string used when creating CLOCKSUM lines, or when
  1440. org-mode generates a time duration."
  1441. :group 'org-time
  1442. :type 'string)
  1443. (defcustom org-deadline-warning-days 14
  1444. "No. of days before expiration during which a deadline becomes active.
  1445. This variable governs the display in sparse trees and in the agenda.
  1446. When 0 or negative, it means use this number (the absolute value of it)
  1447. even if a deadline has a different individual lead time specified."
  1448. :group 'org-time
  1449. :group 'org-agenda-daily/weekly
  1450. :type 'number)
  1451. (defcustom org-read-date-prefer-future t
  1452. "Non-nil means, assume future for incomplete date input from user.
  1453. This affects the following situations:
  1454. 1. The user gives a day, but no month.
  1455. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1456. read this as the third of *next* month. However, if you enter \"17\",
  1457. it will be considered as *this* month.
  1458. 2. The user gives a month but not a year.
  1459. For example, if it is april and you enter \"feb 2\", this will be read
  1460. as feb 2, *next* year. \"May 5\", however, will be this year.
  1461. Currently this does not work for ISO week specifications.
  1462. When this option is nil, the current month and year will always be used
  1463. as defaults."
  1464. :group 'org-time
  1465. :type 'boolean)
  1466. (defcustom org-read-date-display-live t
  1467. "Non-nil means, display current interpretation of date prompt live.
  1468. This display will be in an overlay, in the minibuffer."
  1469. :group 'org-time
  1470. :type 'boolean)
  1471. (defcustom org-read-date-popup-calendar t
  1472. "Non-nil means, pop up a calendar when prompting for a date.
  1473. In the calendar, the date can be selected with mouse-1. However, the
  1474. minibuffer will also be active, and you can simply enter the date as well.
  1475. When nil, only the minibuffer will be available."
  1476. :group 'org-time
  1477. :type 'boolean)
  1478. (if (fboundp 'defvaralias)
  1479. (defvaralias 'org-popup-calendar-for-date-prompt
  1480. 'org-read-date-popup-calendar))
  1481. (defcustom org-extend-today-until 0
  1482. "The hour when your day really ends. Must be an integer.
  1483. This has influence for the following applications:
  1484. - When switching the agenda to \"today\". It it is still earlier than
  1485. the time given here, the day recognized as TODAY is actually yesterday.
  1486. - When a date is read from the user and it is still before the time given
  1487. here, the current date and time will be assumed to be yesterday, 23:59.
  1488. Also, timestamps inserted in remember templates follow this rule.
  1489. IMPORTANT: This is a feature whose implementation is and likely will
  1490. remain incomplete. Really, it is only here because past midnight seems to
  1491. ne the favorite working time of John Wiegley :-)"
  1492. :group 'org-time
  1493. :type 'number)
  1494. (defcustom org-edit-timestamp-down-means-later nil
  1495. "Non-nil means, S-down will increase the time in a time stamp.
  1496. When nil, S-up will increase."
  1497. :group 'org-time
  1498. :type 'boolean)
  1499. (defcustom org-calendar-follow-timestamp-change t
  1500. "Non-nil means, make the calendar window follow timestamp changes.
  1501. When a timestamp is modified and the calendar window is visible, it will be
  1502. moved to the new date."
  1503. :group 'org-time
  1504. :type 'boolean)
  1505. (defgroup org-tags nil
  1506. "Options concerning tags in Org-mode."
  1507. :tag "Org Tags"
  1508. :group 'org)
  1509. (defcustom org-tag-alist nil
  1510. "List of tags allowed in Org-mode files.
  1511. When this list is nil, Org-mode will base TAG input on what is already in the
  1512. buffer.
  1513. The value of this variable is an alist, the car of each entry must be a
  1514. keyword as a string, the cdr may be a character that is used to select
  1515. that tag through the fast-tag-selection interface.
  1516. See the manual for details."
  1517. :group 'org-tags
  1518. :type '(repeat
  1519. (choice
  1520. (cons (string :tag "Tag name")
  1521. (character :tag "Access char"))
  1522. (const :tag "Start radio group" (:startgroup))
  1523. (const :tag "End radio group" (:endgroup)))))
  1524. (defvar org-file-tags nil
  1525. "List of tags that can be inherited by all entries in the file.
  1526. The tags will be inherited if the variable `org-use-tag-inheritance'
  1527. says they should be.
  1528. This variable is populated from #+TAG lines.")
  1529. (defcustom org-use-fast-tag-selection 'auto
  1530. "Non-nil means, use fast tag selection scheme.
  1531. This is a special interface to select and deselect tags with single keys.
  1532. When nil, fast selection is never used.
  1533. When the symbol `auto', fast selection is used if and only if selection
  1534. characters for tags have been configured, either through the variable
  1535. `org-tag-alist' or through a #+TAGS line in the buffer.
  1536. When t, fast selection is always used and selection keys are assigned
  1537. automatically if necessary."
  1538. :group 'org-tags
  1539. :type '(choice
  1540. (const :tag "Always" t)
  1541. (const :tag "Never" nil)
  1542. (const :tag "When selection characters are configured" 'auto)))
  1543. (defcustom org-fast-tag-selection-single-key nil
  1544. "Non-nil means, fast tag selection exits after first change.
  1545. When nil, you have to press RET to exit it.
  1546. During fast tag selection, you can toggle this flag with `C-c'.
  1547. This variable can also have the value `expert'. In this case, the window
  1548. displaying the tags menu is not even shown, until you press C-c again."
  1549. :group 'org-tags
  1550. :type '(choice
  1551. (const :tag "No" nil)
  1552. (const :tag "Yes" t)
  1553. (const :tag "Expert" expert)))
  1554. (defvar org-fast-tag-selection-include-todo nil
  1555. "Non-nil means, fast tags selection interface will also offer TODO states.
  1556. This is an undocumented feature, you should not rely on it.")
  1557. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1558. "The column to which tags should be indented in a headline.
  1559. If this number is positive, it specifies the column. If it is negative,
  1560. it means that the tags should be flushright to that column. For example,
  1561. -80 works well for a normal 80 character screen."
  1562. :group 'org-tags
  1563. :type 'integer)
  1564. (defcustom org-auto-align-tags t
  1565. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1566. These operations change the length of a headline and therefore shift
  1567. the tags around. With this options turned on, after each such operation
  1568. the tags are again aligned to `org-tags-column'."
  1569. :group 'org-tags
  1570. :type 'boolean)
  1571. (defcustom org-use-tag-inheritance t
  1572. "Non-nil means, tags in levels apply also for sublevels.
  1573. When nil, only the tags directly given in a specific line apply there.
  1574. If this option is t, a match early-on in a tree can lead to a large
  1575. number of matches in the subtree. If you only want to see the first
  1576. match in a tree during a search, check out the variable
  1577. `org-tags-match-list-sublevels'.
  1578. This may also be a list of tags that should be inherited, or a regexp that
  1579. matches tags that should be inherited."
  1580. :group 'org-tags
  1581. :type '(choice
  1582. (const :tag "Not" nil)
  1583. (const :tag "Always" t)
  1584. (repeat :tag "Specific tags" (string :tag "Tag"))
  1585. (regexp :tag "Tags matched by regexp")))
  1586. (defun org-tag-inherit-p (tag)
  1587. "Check if TAG is one that should be inherited."
  1588. (cond
  1589. ((eq org-use-tag-inheritance t) t)
  1590. ((not org-use-tag-inheritance) nil)
  1591. ((stringp org-use-tag-inheritance)
  1592. (string-match org-use-tag-inheritance tag))
  1593. ((listp org-use-tag-inheritance)
  1594. (member tag org-use-tag-inheritance))
  1595. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1596. (defcustom org-tags-match-list-sublevels t
  1597. "Non-nil means list also sublevels of headlines matching tag search.
  1598. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1599. the sublevels of a headline matching a tag search often also match
  1600. the same search. Listing all of them can create very long lists.
  1601. Setting this variable to nil causes subtrees of a match to be skipped.
  1602. This option is off by default, because inheritance in on. If you turn
  1603. inheritance off, you very likely want to turn this option on.
  1604. As a special case, if the tag search is restricted to TODO items, the
  1605. value of this variable is ignored and sublevels are always checked, to
  1606. make sure all corresponding TODO items find their way into the list."
  1607. :group 'org-tags
  1608. :type 'boolean)
  1609. (defvar org-tags-history nil
  1610. "History of minibuffer reads for tags.")
  1611. (defvar org-last-tags-completion-table nil
  1612. "The last used completion table for tags.")
  1613. (defvar org-after-tags-change-hook nil
  1614. "Hook that is run after the tags in a line have changed.")
  1615. (defgroup org-properties nil
  1616. "Options concerning properties in Org-mode."
  1617. :tag "Org Properties"
  1618. :group 'org)
  1619. (defcustom org-property-format "%-10s %s"
  1620. "How property key/value pairs should be formatted by `indent-line'.
  1621. When `indent-line' hits a property definition, it will format the line
  1622. according to this format, mainly to make sure that the values are
  1623. lined-up with respect to each other."
  1624. :group 'org-properties
  1625. :type 'string)
  1626. (defcustom org-use-property-inheritance nil
  1627. "Non-nil means, properties apply also for sublevels.
  1628. This setting is chiefly used during property searches. Turning it on can
  1629. cause significant overhead when doing a search, which is why it is not
  1630. on by default.
  1631. When nil, only the properties directly given in the current entry count.
  1632. When t, every property is inherited. The value may also be a list of
  1633. properties that should have inheritance, or a regular expression matching
  1634. properties that should be inherited.
  1635. However, note that some special properties use inheritance under special
  1636. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1637. and the properties ending in \"_ALL\" when they are used as descriptor
  1638. for valid values of a property.
  1639. Note for programmers:
  1640. When querying an entry with `org-entry-get', you can control if inheritance
  1641. should be used. By default, `org-entry-get' looks only at the local
  1642. properties. You can request inheritance by setting the inherit argument
  1643. to t (to force inheritance) or to `selective' (to respect the setting
  1644. in this variable)."
  1645. :group 'org-properties
  1646. :type '(choice
  1647. (const :tag "Not" nil)
  1648. (const :tag "Always" t)
  1649. (repeat :tag "Specific properties" (string :tag "Property"))
  1650. (regexp :tag "Properties matched by regexp")))
  1651. (defun org-property-inherit-p (property)
  1652. "Check if PROPERTY is one that should be inherited."
  1653. (cond
  1654. ((eq org-use-property-inheritance t) t)
  1655. ((not org-use-property-inheritance) nil)
  1656. ((stringp org-use-property-inheritance)
  1657. (string-match org-use-property-inheritance property))
  1658. ((listp org-use-property-inheritance)
  1659. (member property org-use-property-inheritance))
  1660. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1661. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1662. "The default column format, if no other format has been defined.
  1663. This variable can be set on the per-file basis by inserting a line
  1664. #+COLUMNS: %25ITEM ....."
  1665. :group 'org-properties
  1666. :type 'string)
  1667. (defcustom org-columns-ellipses ".."
  1668. "The ellipses to be used when a field in column view is truncated.
  1669. When this is the empty string, as many characters as possible are shown,
  1670. but then there will be no visual indication that the field has been truncated.
  1671. When this is a string of length N, the last N characters of a truncated
  1672. field are replaced by this string. If the column is narrower than the
  1673. ellipses string, only part of the ellipses string will be shown."
  1674. :group 'org-properties
  1675. :type 'string)
  1676. (defcustom org-columns-modify-value-for-display-function nil
  1677. "Function that modifies values for display in column view.
  1678. For example, it can be used to cut out a certain part from a time stamp.
  1679. The function must take 2 argments:
  1680. column-title The tite of the column (*not* the property name)
  1681. value The value that should be modified.
  1682. The function should return the value that should be displayed,
  1683. or nil if the normal value should be used."
  1684. :group 'org-properties
  1685. :type 'function)
  1686. (defcustom org-effort-property "Effort"
  1687. "The property that is being used to keep track of effort estimates.
  1688. Effort estimates given in this property need to have the format H:MM."
  1689. :group 'org-properties
  1690. :group 'org-progress
  1691. :type '(string :tag "Property"))
  1692. (defconst org-global-properties-fixed
  1693. '(("VISIBILITY_ALL" . "folded children content all"))
  1694. "List of property/value pairs that can be inherited by any entry.
  1695. These are fixed values, for the preset properties.")
  1696. (defcustom org-global-properties nil
  1697. "List of property/value pairs that can be inherited by any entry.
  1698. You can set buffer-local values for this by adding lines like
  1699. #+PROPERTY: NAME VALUE"
  1700. :group 'org-properties
  1701. :type '(repeat
  1702. (cons (string :tag "Property")
  1703. (string :tag "Value"))))
  1704. (defvar org-file-properties nil
  1705. "List of property/value pairs that can be inherited by any entry.
  1706. Valid for the current buffer.
  1707. This variable is populated from #+PROPERTY lines.")
  1708. (make-variable-buffer-local 'org-file-properties)
  1709. (defgroup org-agenda nil
  1710. "Options concerning agenda views in Org-mode."
  1711. :tag "Org Agenda"
  1712. :group 'org)
  1713. (defvar org-category nil
  1714. "Variable used by org files to set a category for agenda display.
  1715. Such files should use a file variable to set it, for example
  1716. # -*- mode: org; org-category: \"ELisp\"
  1717. or contain a special line
  1718. #+CATEGORY: ELisp
  1719. If the file does not specify a category, then file's base name
  1720. is used instead.")
  1721. (make-variable-buffer-local 'org-category)
  1722. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1723. (defcustom org-agenda-files nil
  1724. "The files to be used for agenda display.
  1725. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1726. \\[org-remove-file]. You can also use customize to edit the list.
  1727. If an entry is a directory, all files in that directory that are matched by
  1728. `org-agenda-file-regexp' will be part of the file list.
  1729. If the value of the variable is not a list but a single file name, then
  1730. the list of agenda files is actually stored and maintained in that file, one
  1731. agenda file per line."
  1732. :group 'org-agenda
  1733. :type '(choice
  1734. (repeat :tag "List of files and directories" file)
  1735. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1736. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1737. "Regular expression to match files for `org-agenda-files'.
  1738. If any element in the list in that variable contains a directory instead
  1739. of a normal file, all files in that directory that are matched by this
  1740. regular expression will be included."
  1741. :group 'org-agenda
  1742. :type 'regexp)
  1743. (defcustom org-agenda-text-search-extra-files nil
  1744. "List of extra files to be searched by text search commands.
  1745. These files will be search in addition to the agenda files by the
  1746. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1747. Note that these files will only be searched for text search commands,
  1748. not for the other agenda views like todo lists, tag searches or the weekly
  1749. agenda. This variable is intended to list notes and possibly archive files
  1750. that should also be searched by these two commands.
  1751. In fact, if the first element in the list is the symbol `agenda-archives',
  1752. than all archive files of all agenda files will be added to the search
  1753. scope."
  1754. :group 'org-agenda
  1755. :type '(set :greedy t
  1756. (const :tag "Agenda Archives" agenda-archives)
  1757. (repeat :inline t (file))))
  1758. (if (fboundp 'defvaralias)
  1759. (defvaralias 'org-agenda-multi-occur-extra-files
  1760. 'org-agenda-text-search-extra-files))
  1761. (defcustom org-agenda-skip-unavailable-files nil
  1762. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1763. A nil value means to remove them, after a query, from the list."
  1764. :group 'org-agenda
  1765. :type 'boolean)
  1766. (defcustom org-calendar-to-agenda-key [?c]
  1767. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1768. The command `org-calendar-goto-agenda' will be bound to this key. The
  1769. default is the character `c' because then `c' can be used to switch back and
  1770. forth between agenda and calendar."
  1771. :group 'org-agenda
  1772. :type 'sexp)
  1773. (defcustom org-calendar-agenda-action-key [?k]
  1774. "The key to be installed in `calendar-mode-map' for agenda-action.
  1775. The command `org-agenda-action' will be bound to this key. The
  1776. default is the character `k' because we use the same key in the agenda."
  1777. :group 'org-agenda
  1778. :type 'sexp)
  1779. (eval-after-load "calendar"
  1780. '(progn
  1781. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1782. 'org-calendar-goto-agenda)
  1783. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1784. 'org-agenda-action)))
  1785. (defgroup org-latex nil
  1786. "Options for embedding LaTeX code into Org-mode."
  1787. :tag "Org LaTeX"
  1788. :group 'org)
  1789. (defcustom org-format-latex-options
  1790. '(:foreground default :background default :scale 1.0
  1791. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1792. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1793. "Options for creating images from LaTeX fragments.
  1794. This is a property list with the following properties:
  1795. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1796. `default' means use the foreground of the default face.
  1797. :background the background color, or \"Transparent\".
  1798. `default' means use the background of the default face.
  1799. :scale a scaling factor for the size of the images.
  1800. :html-foreground, :html-background, :html-scale
  1801. the same numbers for HTML export.
  1802. :matchers a list indicating which matchers should be used to
  1803. find LaTeX fragments. Valid members of this list are:
  1804. \"begin\" find environments
  1805. \"$\" find math expressions surrounded by $...$
  1806. \"$$\" find math expressions surrounded by $$....$$
  1807. \"\\(\" find math expressions surrounded by \\(...\\)
  1808. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1809. :group 'org-latex
  1810. :type 'plist)
  1811. (defcustom org-format-latex-header "\\documentclass{article}
  1812. \\usepackage{fullpage} % do not remove
  1813. \\usepackage{amssymb}
  1814. \\usepackage[usenames]{color}
  1815. \\usepackage{amsmath}
  1816. \\usepackage{latexsym}
  1817. \\usepackage[mathscr]{eucal}
  1818. \\pagestyle{empty} % do not remove"
  1819. "The document header used for processing LaTeX fragments."
  1820. :group 'org-latex
  1821. :type 'string)
  1822. (defgroup org-font-lock nil
  1823. "Font-lock settings for highlighting in Org-mode."
  1824. :tag "Org Font Lock"
  1825. :group 'org)
  1826. (defcustom org-level-color-stars-only nil
  1827. "Non-nil means fontify only the stars in each headline.
  1828. When nil, the entire headline is fontified.
  1829. Changing it requires restart of `font-lock-mode' to become effective
  1830. also in regions already fontified."
  1831. :group 'org-font-lock
  1832. :type 'boolean)
  1833. (defcustom org-hide-leading-stars nil
  1834. "Non-nil means, hide the first N-1 stars in a headline.
  1835. This works by using the face `org-hide' for these stars. This
  1836. face is white for a light background, and black for a dark
  1837. background. You may have to customize the face `org-hide' to
  1838. make this work.
  1839. Changing it requires restart of `font-lock-mode' to become effective
  1840. also in regions already fontified.
  1841. You may also set this on a per-file basis by adding one of the following
  1842. lines to the buffer:
  1843. #+STARTUP: hidestars
  1844. #+STARTUP: showstars"
  1845. :group 'org-font-lock
  1846. :type 'boolean)
  1847. (defcustom org-fontify-done-headline nil
  1848. "Non-nil means, change the face of a headline if it is marked DONE.
  1849. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1850. When this is non-nil, the headline after the keyword is set to the
  1851. `org-headline-done' as an additional indication."
  1852. :group 'org-font-lock
  1853. :type 'boolean)
  1854. (defcustom org-fontify-emphasized-text t
  1855. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1856. Changing this variable requires a restart of Emacs to take effect."
  1857. :group 'org-font-lock
  1858. :type 'boolean)
  1859. (defcustom org-highlight-latex-fragments-and-specials nil
  1860. "Non-nil means, fontify what is treated specially by the exporters."
  1861. :group 'org-font-lock
  1862. :type 'boolean)
  1863. (defcustom org-hide-emphasis-markers nil
  1864. "Non-nil mean font-lock should hide the emphasis marker characters."
  1865. :group 'org-font-lock
  1866. :type 'boolean)
  1867. (defvar org-emph-re nil
  1868. "Regular expression for matching emphasis.")
  1869. (defvar org-verbatim-re nil
  1870. "Regular expression for matching verbatim text.")
  1871. (defvar org-emphasis-regexp-components) ; defined just below
  1872. (defvar org-emphasis-alist) ; defined just below
  1873. (defun org-set-emph-re (var val)
  1874. "Set variable and compute the emphasis regular expression."
  1875. (set var val)
  1876. (when (and (boundp 'org-emphasis-alist)
  1877. (boundp 'org-emphasis-regexp-components)
  1878. org-emphasis-alist org-emphasis-regexp-components)
  1879. (let* ((e org-emphasis-regexp-components)
  1880. (pre (car e))
  1881. (post (nth 1 e))
  1882. (border (nth 2 e))
  1883. (body (nth 3 e))
  1884. (nl (nth 4 e))
  1885. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1886. (body1 (concat body "*?"))
  1887. (markers (mapconcat 'car org-emphasis-alist ""))
  1888. (vmarkers (mapconcat
  1889. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1890. org-emphasis-alist "")))
  1891. ;; make sure special characters appear at the right position in the class
  1892. (if (string-match "\\^" markers)
  1893. (setq markers (concat (replace-match "" t t markers) "^")))
  1894. (if (string-match "-" markers)
  1895. (setq markers (concat (replace-match "" t t markers) "-")))
  1896. (if (string-match "\\^" vmarkers)
  1897. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1898. (if (string-match "-" vmarkers)
  1899. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1900. (if (> nl 0)
  1901. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1902. (int-to-string nl) "\\}")))
  1903. ;; Make the regexp
  1904. (setq org-emph-re
  1905. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1906. "\\("
  1907. "\\([" markers "]\\)"
  1908. "\\("
  1909. "[^" border "]\\|"
  1910. "[^" border (if (and nil stacked) markers) "]"
  1911. body1
  1912. "[^" border (if (and nil stacked) markers) "]"
  1913. "\\)"
  1914. "\\3\\)"
  1915. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1916. (setq org-verbatim-re
  1917. (concat "\\([" pre "]\\|^\\)"
  1918. "\\("
  1919. "\\([" vmarkers "]\\)"
  1920. "\\("
  1921. "[^" border "]\\|"
  1922. "[^" border "]"
  1923. body1
  1924. "[^" border "]"
  1925. "\\)"
  1926. "\\3\\)"
  1927. "\\([" post "]\\|$\\)")))))
  1928. (defcustom org-emphasis-regexp-components
  1929. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1930. "Components used to build the regular expression for emphasis.
  1931. This is a list with 6 entries. Terminology: In an emphasis string
  1932. like \" *strong word* \", we call the initial space PREMATCH, the final
  1933. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1934. and \"trong wor\" is the body. The different components in this variable
  1935. specify what is allowed/forbidden in each part:
  1936. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1937. post Chars allowed as postmatch. End of line will be allowed too.
  1938. border The chars *forbidden* as border characters.
  1939. body-regexp A regexp like \".\" to match a body character. Don't use
  1940. non-shy groups here, and don't allow newline here.
  1941. newline The maximum number of newlines allowed in an emphasis exp.
  1942. Use customize to modify this, or restart Emacs after changing it."
  1943. :group 'org-font-lock
  1944. :set 'org-set-emph-re
  1945. :type '(list
  1946. (sexp :tag "Allowed chars in pre ")
  1947. (sexp :tag "Allowed chars in post ")
  1948. (sexp :tag "Forbidden chars in border ")
  1949. (sexp :tag "Regexp for body ")
  1950. (integer :tag "number of newlines allowed")
  1951. (option (boolean :tag "Please ignore this button"))))
  1952. (defcustom org-emphasis-alist
  1953. `(("*" bold "<b>" "</b>")
  1954. ("/" italic "<i>" "</i>")
  1955. ("_" underline "<u>" "</u>")
  1956. ("=" org-code "<code>" "</code>" verbatim)
  1957. ("~" org-verbatim "" "" verbatim)
  1958. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1959. "<del>" "</del>")
  1960. )
  1961. "Special syntax for emphasized text.
  1962. Text starting and ending with a special character will be emphasized, for
  1963. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1964. characters, the face to be used by font-lock for highlighting in Org-mode
  1965. Emacs buffers, and the HTML tags to be used for this.
  1966. Use customize to modify this, or restart Emacs after changing it."
  1967. :group 'org-font-lock
  1968. :set 'org-set-emph-re
  1969. :type '(repeat
  1970. (list
  1971. (string :tag "Marker character")
  1972. (choice
  1973. (face :tag "Font-lock-face")
  1974. (plist :tag "Face property list"))
  1975. (string :tag "HTML start tag")
  1976. (string :tag "HTML end tag")
  1977. (option (const verbatim)))))
  1978. ;;; Miscellaneous options
  1979. (defgroup org-completion nil
  1980. "Completion in Org-mode."
  1981. :tag "Org Completion"
  1982. :group 'org)
  1983. (defcustom org-completion-fallback-command 'hippie-expand
  1984. "The expansion command called by \\[org-complete] in normal context.
  1985. Normal means, no org-mode-specific context."
  1986. :group 'org-completion
  1987. :type 'function)
  1988. ;;; Functions and variables from ther packages
  1989. ;; Declared here to avoid compiler warnings
  1990. ;; XEmacs only
  1991. (defvar outline-mode-menu-heading)
  1992. (defvar outline-mode-menu-show)
  1993. (defvar outline-mode-menu-hide)
  1994. (defvar zmacs-regions) ; XEmacs regions
  1995. ;; Emacs only
  1996. (defvar mark-active)
  1997. ;; Various packages
  1998. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1999. (declare-function calendar-forward-day "cal-move" (arg))
  2000. (declare-function calendar-goto-date "cal-move" (date))
  2001. (declare-function calendar-goto-today "cal-move" ())
  2002. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2003. (defvar calc-embedded-close-formula)
  2004. (defvar calc-embedded-open-formula)
  2005. (declare-function cdlatex-tab "ext:cdlatex" ())
  2006. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2007. (defvar font-lock-unfontify-region-function)
  2008. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2009. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2010. (defvar iswitchb-temp-buflist)
  2011. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2012. (declare-function org-agenda-skip "org-agenda" ())
  2013. (declare-function org-format-agenda-item "org-agenda"
  2014. (extra txt &optional category tags dotime noprefix remove-re))
  2015. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2016. (declare-function org-agenda-change-all-lines "org-agenda"
  2017. (newhead hdmarker &optional fixface))
  2018. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2019. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2020. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2021. (beg end))
  2022. (declare-function parse-time-string "parse-time" (string))
  2023. (declare-function remember "remember" (&optional initial))
  2024. (declare-function remember-buffer-desc "remember" ())
  2025. (declare-function remember-finalize "remember" ())
  2026. (defvar remember-save-after-remembering)
  2027. (defvar remember-data-file)
  2028. (defvar remember-register)
  2029. (defvar remember-buffer)
  2030. (defvar remember-handler-functions)
  2031. (defvar remember-annotation-functions)
  2032. (defvar texmathp-why)
  2033. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2034. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2035. (defvar w3m-current-url)
  2036. (defvar w3m-current-title)
  2037. (defvar org-latex-regexps)
  2038. ;;; Autoload and prepare some org modules
  2039. ;; Some table stuff that needs to be defined here, because it is used
  2040. ;; by the functions setting up org-mode or checking for table context.
  2041. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2042. "Detects an org-type or table-type table.")
  2043. (defconst org-table-line-regexp "^[ \t]*|"
  2044. "Detects an org-type table line.")
  2045. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2046. "Detects an org-type table line.")
  2047. (defconst org-table-hline-regexp "^[ \t]*|-"
  2048. "Detects an org-type table hline.")
  2049. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2050. "Detects a table-type table hline.")
  2051. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2052. "Searching from within a table (any type) this finds the first line
  2053. outside the table.")
  2054. ;; Autoload the functions in org-table.el that are needed by functions here.
  2055. (eval-and-compile
  2056. (org-autoload "org-table"
  2057. '(org-table-align org-table-begin org-table-blank-field
  2058. org-table-convert org-table-convert-region org-table-copy-down
  2059. org-table-copy-region org-table-create
  2060. org-table-create-or-convert-from-region
  2061. org-table-create-with-table.el org-table-current-dline
  2062. org-table-cut-region org-table-delete-column org-table-edit-field
  2063. org-table-edit-formulas org-table-end org-table-eval-formula
  2064. org-table-export org-table-field-info
  2065. org-table-get-stored-formulas org-table-goto-column
  2066. org-table-hline-and-move org-table-import org-table-insert-column
  2067. org-table-insert-hline org-table-insert-row org-table-iterate
  2068. org-table-justify-field-maybe org-table-kill-row
  2069. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2070. org-table-move-column org-table-move-column-left
  2071. org-table-move-column-right org-table-move-row
  2072. org-table-move-row-down org-table-move-row-up
  2073. org-table-next-field org-table-next-row org-table-paste-rectangle
  2074. org-table-previous-field org-table-recalculate
  2075. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2076. org-table-toggle-coordinate-overlays
  2077. org-table-toggle-formula-debugger org-table-wrap-region
  2078. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2079. (defun org-at-table-p (&optional table-type)
  2080. "Return t if the cursor is inside an org-type table.
  2081. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2082. (if org-enable-table-editor
  2083. (save-excursion
  2084. (beginning-of-line 1)
  2085. (looking-at (if table-type org-table-any-line-regexp
  2086. org-table-line-regexp)))
  2087. nil))
  2088. (defsubst org-table-p () (org-at-table-p))
  2089. (defun org-at-table.el-p ()
  2090. "Return t if and only if we are at a table.el table."
  2091. (and (org-at-table-p 'any)
  2092. (save-excursion
  2093. (goto-char (org-table-begin 'any))
  2094. (looking-at org-table1-hline-regexp))))
  2095. (defun org-table-recognize-table.el ()
  2096. "If there is a table.el table nearby, recognize it and move into it."
  2097. (if org-table-tab-recognizes-table.el
  2098. (if (org-at-table.el-p)
  2099. (progn
  2100. (beginning-of-line 1)
  2101. (if (looking-at org-table-dataline-regexp)
  2102. nil
  2103. (if (looking-at org-table1-hline-regexp)
  2104. (progn
  2105. (beginning-of-line 2)
  2106. (if (looking-at org-table-any-border-regexp)
  2107. (beginning-of-line -1)))))
  2108. (if (re-search-forward "|" (org-table-end t) t)
  2109. (progn
  2110. (require 'table)
  2111. (if (table--at-cell-p (point))
  2112. t
  2113. (message "recognizing table.el table...")
  2114. (table-recognize-table)
  2115. (message "recognizing table.el table...done")))
  2116. (error "This should not happen..."))
  2117. t)
  2118. nil)
  2119. nil))
  2120. (defun org-at-table-hline-p ()
  2121. "Return t if the cursor is inside a hline in a table."
  2122. (if org-enable-table-editor
  2123. (save-excursion
  2124. (beginning-of-line 1)
  2125. (looking-at org-table-hline-regexp))
  2126. nil))
  2127. (defvar org-table-clean-did-remove-column nil)
  2128. (defun org-table-map-tables (function)
  2129. "Apply FUNCTION to the start of all tables in the buffer."
  2130. (save-excursion
  2131. (save-restriction
  2132. (widen)
  2133. (goto-char (point-min))
  2134. (while (re-search-forward org-table-any-line-regexp nil t)
  2135. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2136. (beginning-of-line 1)
  2137. (if (looking-at org-table-line-regexp)
  2138. (save-excursion (funcall function)))
  2139. (re-search-forward org-table-any-border-regexp nil 1))))
  2140. (message "Mapping tables: done"))
  2141. ;; Declare and autoload functions from org-exp.el
  2142. (declare-function org-default-export-plist "org-exp")
  2143. (declare-function org-infile-export-plist "org-exp")
  2144. (declare-function org-get-current-options "org-exp")
  2145. (eval-and-compile
  2146. (org-autoload "org-exp"
  2147. '(org-export org-export-as-ascii org-export-visible
  2148. org-insert-export-options-template org-export-as-html-and-open
  2149. org-export-as-html-batch org-export-as-html-to-buffer
  2150. org-replace-region-by-html org-export-region-as-html
  2151. org-export-as-html org-export-icalendar-this-file
  2152. org-export-icalendar-all-agenda-files
  2153. org-table-clean-before-export
  2154. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2155. ;; Declare and autoload functions from org-exp.el
  2156. (eval-and-compile
  2157. (org-autoload "org-exp"
  2158. '(org-agenda org-agenda-list org-search-view
  2159. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2160. org-diary org-agenda-to-appt)))
  2161. ;; Autoload org-remember
  2162. (eval-and-compile
  2163. (org-autoload "org-remember"
  2164. '(org-remember-insinuate org-remember-annotation
  2165. org-remember-apply-template org-remember org-remember-handler)))
  2166. ;; Autoload org-clock.el
  2167. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2168. (beg end))
  2169. (declare-function org-update-mode-line "org-clock" ())
  2170. (defvar org-clock-start-time)
  2171. (defvar org-clock-marker (make-marker)
  2172. "Marker recording the last clock-in.")
  2173. (eval-and-compile
  2174. (org-autoload
  2175. "org-clock"
  2176. '(org-clock-in org-clock-out org-clock-cancel
  2177. org-clock-goto org-clock-sum org-clock-display
  2178. org-remove-clock-overlays org-clock-report
  2179. org-clocktable-shift org-dblock-write:clocktable
  2180. org-get-clocktable)))
  2181. (defun org-clock-update-time-maybe ()
  2182. "If this is a CLOCK line, update it and return t.
  2183. Otherwise, return nil."
  2184. (interactive)
  2185. (save-excursion
  2186. (beginning-of-line 1)
  2187. (skip-chars-forward " \t")
  2188. (when (looking-at org-clock-string)
  2189. (let ((re (concat "[ \t]*" org-clock-string
  2190. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2191. "\\([ \t]*=>.*\\)?\\)?"))
  2192. ts te h m s)
  2193. (cond
  2194. ((not (looking-at re))
  2195. nil)
  2196. ((not (match-end 2))
  2197. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2198. (> org-clock-marker (point))
  2199. (<= org-clock-marker (point-at-eol)))
  2200. ;; The clock is running here
  2201. (setq org-clock-start-time
  2202. (apply 'encode-time
  2203. (org-parse-time-string (match-string 1))))
  2204. (org-update-mode-line)))
  2205. (t
  2206. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2207. (end-of-line 1)
  2208. (setq ts (match-string 1)
  2209. te (match-string 3))
  2210. (setq s (- (time-to-seconds
  2211. (apply 'encode-time (org-parse-time-string te)))
  2212. (time-to-seconds
  2213. (apply 'encode-time (org-parse-time-string ts))))
  2214. h (floor (/ s 3600))
  2215. s (- s (* 3600 h))
  2216. m (floor (/ s 60))
  2217. s (- s (* 60 s)))
  2218. (insert " => " (format "%2d:%02d" h m))
  2219. t))))))
  2220. (defun org-check-running-clock ()
  2221. "Check if the current buffer contains the running clock.
  2222. If yes, offer to stop it and to save the buffer with the changes."
  2223. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2224. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2225. (buffer-name))))
  2226. (org-clock-out)
  2227. (when (y-or-n-p "Save changed buffer?")
  2228. (save-buffer))))
  2229. (defun org-clocktable-try-shift (dir n)
  2230. "Check if this line starts a clock table, if yes, shift the time block."
  2231. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2232. (org-clocktable-shift dir n)))
  2233. ;; Autoload archiving code
  2234. ;; The stuff that is needed for cycling and tags has to be defined here.
  2235. (defgroup org-archive nil
  2236. "Options concerning archiving in Org-mode."
  2237. :tag "Org Archive"
  2238. :group 'org-structure)
  2239. (defcustom org-archive-location "%s_archive::"
  2240. "The location where subtrees should be archived.
  2241. Otherwise, the value of this variable is a string, consisting of two
  2242. parts, separated by a double-colon.
  2243. The first part is a file name - when omitted, archiving happens in the same
  2244. file. %s will be replaced by the current file name (without directory part).
  2245. Archiving to a different file is useful to keep archived entries from
  2246. contributing to the Org-mode Agenda.
  2247. The part after the double colon is a headline. The archived entries will be
  2248. filed under that headline. When omitted, the subtrees are simply filed away
  2249. at the end of the file, as top-level entries.
  2250. Here are a few examples:
  2251. \"%s_archive::\"
  2252. If the current file is Projects.org, archive in file
  2253. Projects.org_archive, as top-level trees. This is the default.
  2254. \"::* Archived Tasks\"
  2255. Archive in the current file, under the top-level headline
  2256. \"* Archived Tasks\".
  2257. \"~/org/archive.org::\"
  2258. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2259. \"basement::** Finished Tasks\"
  2260. Archive in file ./basement (relative path), as level 3 trees
  2261. below the level 2 heading \"** Finished Tasks\".
  2262. You may set this option on a per-file basis by adding to the buffer a
  2263. line like
  2264. #+ARCHIVE: basement::** Finished Tasks
  2265. You may also define it locally for a subtree by setting an ARCHIVE property
  2266. in the entry. If such a property is found in an entry, or anywhere up
  2267. the hierarchy, it will be used."
  2268. :group 'org-archive
  2269. :type 'string)
  2270. (defcustom org-archive-tag "ARCHIVE"
  2271. "The tag that marks a subtree as archived.
  2272. An archived subtree does not open during visibility cycling, and does
  2273. not contribute to the agenda listings.
  2274. After changing this, font-lock must be restarted in the relevant buffers to
  2275. get the proper fontification."
  2276. :group 'org-archive
  2277. :group 'org-keywords
  2278. :type 'string)
  2279. (defcustom org-agenda-skip-archived-trees t
  2280. "Non-nil means, the agenda will skip any items located in archived trees.
  2281. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2282. variable is no longer recommended, you should leave it at the value t.
  2283. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2284. :group 'org-archive
  2285. :group 'org-agenda-skip
  2286. :type 'boolean)
  2287. (defcustom org-cycle-open-archived-trees nil
  2288. "Non-nil means, `org-cycle' will open archived trees.
  2289. An archived tree is a tree marked with the tag ARCHIVE.
  2290. When nil, archived trees will stay folded. You can still open them with
  2291. normal outline commands like `show-all', but not with the cycling commands."
  2292. :group 'org-archive
  2293. :group 'org-cycle
  2294. :type 'boolean)
  2295. (defcustom org-sparse-tree-open-archived-trees nil
  2296. "Non-nil means sparse tree construction shows matches in archived trees.
  2297. When nil, matches in these trees are highlighted, but the trees are kept in
  2298. collapsed state."
  2299. :group 'org-archive
  2300. :group 'org-sparse-trees
  2301. :type 'boolean)
  2302. (defun org-cycle-hide-archived-subtrees (state)
  2303. "Re-hide all archived subtrees after a visibility state change."
  2304. (when (and (not org-cycle-open-archived-trees)
  2305. (not (memq state '(overview folded))))
  2306. (save-excursion
  2307. (let* ((globalp (memq state '(contents all)))
  2308. (beg (if globalp (point-min) (point)))
  2309. (end (if globalp (point-max) (org-end-of-subtree t))))
  2310. (org-hide-archived-subtrees beg end)
  2311. (goto-char beg)
  2312. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2313. (message "%s" (substitute-command-keys
  2314. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2315. (defun org-force-cycle-archived ()
  2316. "Cycle subtree even if it is archived."
  2317. (interactive)
  2318. (setq this-command 'org-cycle)
  2319. (let ((org-cycle-open-archived-trees t))
  2320. (call-interactively 'org-cycle)))
  2321. (defun org-hide-archived-subtrees (beg end)
  2322. "Re-hide all archived subtrees after a visibility state change."
  2323. (save-excursion
  2324. (let* ((re (concat ":" org-archive-tag ":")))
  2325. (goto-char beg)
  2326. (while (re-search-forward re end t)
  2327. (and (org-on-heading-p) (hide-subtree))
  2328. (org-end-of-subtree t)))))
  2329. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2330. (eval-and-compile
  2331. (org-autoload "org-archive"
  2332. '(org-add-archive-files org-archive-subtree
  2333. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2334. ;; Autoload Column View Code
  2335. (declare-function org-columns-number-to-string "org-colview")
  2336. (declare-function org-columns-get-format-and-top-level "org-colview")
  2337. (declare-function org-columns-compute "org-colview")
  2338. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2339. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2340. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2341. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2342. ;; Autoload ID code
  2343. (org-autoload "org-id"
  2344. '(org-id-get-create org-id-new org-id-copy org-id-get
  2345. org-id-get-with-outline-path-completion
  2346. org-id-get-with-outline-drilling
  2347. org-id-goto org-id-find))
  2348. ;;; Variables for pre-computed regular expressions, all buffer local
  2349. (defvar org-drawer-regexp nil
  2350. "Matches first line of a hidden block.")
  2351. (make-variable-buffer-local 'org-drawer-regexp)
  2352. (defvar org-todo-regexp nil
  2353. "Matches any of the TODO state keywords.")
  2354. (make-variable-buffer-local 'org-todo-regexp)
  2355. (defvar org-not-done-regexp nil
  2356. "Matches any of the TODO state keywords except the last one.")
  2357. (make-variable-buffer-local 'org-not-done-regexp)
  2358. (defvar org-todo-line-regexp nil
  2359. "Matches a headline and puts TODO state into group 2 if present.")
  2360. (make-variable-buffer-local 'org-todo-line-regexp)
  2361. (defvar org-complex-heading-regexp nil
  2362. "Matches a headline and puts everything into groups:
  2363. group 1: the stars
  2364. group 2: The todo keyword, maybe
  2365. group 3: Priority cookie
  2366. group 4: True headline
  2367. group 5: Tags")
  2368. (make-variable-buffer-local 'org-complex-heading-regexp)
  2369. (defvar org-todo-line-tags-regexp nil
  2370. "Matches a headline and puts TODO state into group 2 if present.
  2371. Also put tags into group 4 if tags are present.")
  2372. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2373. (defvar org-nl-done-regexp nil
  2374. "Matches newline followed by a headline with the DONE keyword.")
  2375. (make-variable-buffer-local 'org-nl-done-regexp)
  2376. (defvar org-looking-at-done-regexp nil
  2377. "Matches the DONE keyword a point.")
  2378. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2379. (defvar org-ds-keyword-length 12
  2380. "Maximum length of the Deadline and SCHEDULED keywords.")
  2381. (make-variable-buffer-local 'org-ds-keyword-length)
  2382. (defvar org-deadline-regexp nil
  2383. "Matches the DEADLINE keyword.")
  2384. (make-variable-buffer-local 'org-deadline-regexp)
  2385. (defvar org-deadline-time-regexp nil
  2386. "Matches the DEADLINE keyword together with a time stamp.")
  2387. (make-variable-buffer-local 'org-deadline-time-regexp)
  2388. (defvar org-deadline-line-regexp nil
  2389. "Matches the DEADLINE keyword and the rest of the line.")
  2390. (make-variable-buffer-local 'org-deadline-line-regexp)
  2391. (defvar org-scheduled-regexp nil
  2392. "Matches the SCHEDULED keyword.")
  2393. (make-variable-buffer-local 'org-scheduled-regexp)
  2394. (defvar org-scheduled-time-regexp nil
  2395. "Matches the SCHEDULED keyword together with a time stamp.")
  2396. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2397. (defvar org-closed-time-regexp nil
  2398. "Matches the CLOSED keyword together with a time stamp.")
  2399. (make-variable-buffer-local 'org-closed-time-regexp)
  2400. (defvar org-keyword-time-regexp nil
  2401. "Matches any of the 4 keywords, together with the time stamp.")
  2402. (make-variable-buffer-local 'org-keyword-time-regexp)
  2403. (defvar org-keyword-time-not-clock-regexp nil
  2404. "Matches any of the 3 keywords, together with the time stamp.")
  2405. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2406. (defvar org-maybe-keyword-time-regexp nil
  2407. "Matches a timestamp, possibly preceeded by a keyword.")
  2408. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2409. (defvar org-planning-or-clock-line-re nil
  2410. "Matches a line with planning or clock info.")
  2411. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2412. (defconst org-plain-time-of-day-regexp
  2413. (concat
  2414. "\\(\\<[012]?[0-9]"
  2415. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2416. "\\(--?"
  2417. "\\(\\<[012]?[0-9]"
  2418. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2419. "\\)?")
  2420. "Regular expression to match a plain time or time range.
  2421. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2422. groups carry important information:
  2423. 0 the full match
  2424. 1 the first time, range or not
  2425. 8 the second time, if it is a range.")
  2426. (defconst org-plain-time-extension-regexp
  2427. (concat
  2428. "\\(\\<[012]?[0-9]"
  2429. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2430. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2431. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2432. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2433. groups carry important information:
  2434. 0 the full match
  2435. 7 hours of duration
  2436. 9 minutes of duration")
  2437. (defconst org-stamp-time-of-day-regexp
  2438. (concat
  2439. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2440. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2441. "\\(--?"
  2442. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2443. "Regular expression to match a timestamp time or time range.
  2444. After a match, the following groups carry important information:
  2445. 0 the full match
  2446. 1 date plus weekday, for backreferencing to make sure both times on same day
  2447. 2 the first time, range or not
  2448. 4 the second time, if it is a range.")
  2449. (defconst org-startup-options
  2450. '(("fold" org-startup-folded t)
  2451. ("overview" org-startup-folded t)
  2452. ("nofold" org-startup-folded nil)
  2453. ("showall" org-startup-folded nil)
  2454. ("content" org-startup-folded content)
  2455. ("hidestars" org-hide-leading-stars t)
  2456. ("showstars" org-hide-leading-stars nil)
  2457. ("odd" org-odd-levels-only t)
  2458. ("oddeven" org-odd-levels-only nil)
  2459. ("align" org-startup-align-all-tables t)
  2460. ("noalign" org-startup-align-all-tables nil)
  2461. ("customtime" org-display-custom-times t)
  2462. ("logdone" org-log-done time)
  2463. ("lognotedone" org-log-done note)
  2464. ("nologdone" org-log-done nil)
  2465. ("lognoteclock-out" org-log-note-clock-out t)
  2466. ("nolognoteclock-out" org-log-note-clock-out nil)
  2467. ("logrepeat" org-log-repeat state)
  2468. ("lognoterepeat" org-log-repeat note)
  2469. ("nologrepeat" org-log-repeat nil)
  2470. ("constcgs" constants-unit-system cgs)
  2471. ("constSI" constants-unit-system SI))
  2472. "Variable associated with STARTUP options for org-mode.
  2473. Each element is a list of three items: The startup options as written
  2474. in the #+STARTUP line, the corresponding variable, and the value to
  2475. set this variable to if the option is found. An optional forth element PUSH
  2476. means to push this value onto the list in the variable.")
  2477. (defun org-set-regexps-and-options ()
  2478. "Precompute regular expressions for current buffer."
  2479. (when (org-mode-p)
  2480. (org-set-local 'org-todo-kwd-alist nil)
  2481. (org-set-local 'org-todo-key-alist nil)
  2482. (org-set-local 'org-todo-key-trigger nil)
  2483. (org-set-local 'org-todo-keywords-1 nil)
  2484. (org-set-local 'org-done-keywords nil)
  2485. (org-set-local 'org-todo-heads nil)
  2486. (org-set-local 'org-todo-sets nil)
  2487. (org-set-local 'org-todo-log-states nil)
  2488. (org-set-local 'org-file-properties nil)
  2489. (org-set-local 'org-file-tags nil)
  2490. (let ((re (org-make-options-regexp
  2491. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2492. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2493. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2494. (splitre "[ \t]+")
  2495. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2496. tail sep kws1 prio props ftags drawers
  2497. ext-setup-or-nil setup-contents (start 0))
  2498. (save-excursion
  2499. (save-restriction
  2500. (widen)
  2501. (goto-char (point-min))
  2502. (while (or (and ext-setup-or-nil
  2503. (string-match re ext-setup-or-nil start)
  2504. (setq start (match-end 0)))
  2505. (and (setq ext-setup-or-nil nil start 0)
  2506. (re-search-forward re nil t)))
  2507. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2508. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2509. (cond
  2510. ((equal key "CATEGORY")
  2511. (if (string-match "[ \t]+$" value)
  2512. (setq value (replace-match "" t t value)))
  2513. (setq cat value))
  2514. ((member key '("SEQ_TODO" "TODO"))
  2515. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2516. ((equal key "TYP_TODO")
  2517. (push (cons 'type (org-split-string value splitre)) kwds))
  2518. ((equal key "TAGS")
  2519. (setq tags (append tags (org-split-string value splitre))))
  2520. ((equal key "COLUMNS")
  2521. (org-set-local 'org-columns-default-format value))
  2522. ((equal key "LINK")
  2523. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2524. (push (cons (match-string 1 value)
  2525. (org-trim (match-string 2 value)))
  2526. links)))
  2527. ((equal key "PRIORITIES")
  2528. (setq prio (org-split-string value " +")))
  2529. ((equal key "PROPERTY")
  2530. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2531. (push (cons (match-string 1 value) (match-string 2 value))
  2532. props)))
  2533. ((equal key "FILETAGS")
  2534. (when (string-match "\\S-" value)
  2535. (setq ftags
  2536. (append
  2537. ftags
  2538. (apply 'append
  2539. (mapcar (lambda (x) (org-split-string x ":"))
  2540. (org-split-string value)))))))
  2541. ((equal key "DRAWERS")
  2542. (setq drawers (org-split-string value splitre)))
  2543. ((equal key "CONSTANTS")
  2544. (setq const (append const (org-split-string value splitre))))
  2545. ((equal key "STARTUP")
  2546. (let ((opts (org-split-string value splitre))
  2547. l var val)
  2548. (while (setq l (pop opts))
  2549. (when (setq l (assoc l org-startup-options))
  2550. (setq var (nth 1 l) val (nth 2 l))
  2551. (if (not (nth 3 l))
  2552. (set (make-local-variable var) val)
  2553. (if (not (listp (symbol-value var)))
  2554. (set (make-local-variable var) nil))
  2555. (set (make-local-variable var) (symbol-value var))
  2556. (add-to-list var val))))))
  2557. ((equal key "ARCHIVE")
  2558. (string-match " *$" value)
  2559. (setq arch (replace-match "" t t value))
  2560. (remove-text-properties 0 (length arch)
  2561. '(face t fontified t) arch))
  2562. ((equal key "SETUPFILE")
  2563. (setq setup-contents (org-file-contents
  2564. (expand-file-name
  2565. (org-remove-double-quotes value))
  2566. 'noerror))
  2567. (if (not ext-setup-or-nil)
  2568. (setq ext-setup-or-nil setup-contents start 0)
  2569. (setq ext-setup-or-nil
  2570. (concat (substring ext-setup-or-nil 0 start)
  2571. "\n" setup-contents "\n"
  2572. (substring ext-setup-or-nil start)))))
  2573. ))))
  2574. (when cat
  2575. (org-set-local 'org-category (intern cat))
  2576. (push (cons "CATEGORY" cat) props))
  2577. (when prio
  2578. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2579. (setq prio (mapcar 'string-to-char prio))
  2580. (org-set-local 'org-highest-priority (nth 0 prio))
  2581. (org-set-local 'org-lowest-priority (nth 1 prio))
  2582. (org-set-local 'org-default-priority (nth 2 prio)))
  2583. (and props (org-set-local 'org-file-properties (nreverse props)))
  2584. (and ftags (org-set-local 'org-file-tags ftags))
  2585. (and drawers (org-set-local 'org-drawers drawers))
  2586. (and arch (org-set-local 'org-archive-location arch))
  2587. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2588. ;; Process the TODO keywords
  2589. (unless kwds
  2590. ;; Use the global values as if they had been given locally.
  2591. (setq kwds (default-value 'org-todo-keywords))
  2592. (if (stringp (car kwds))
  2593. (setq kwds (list (cons org-todo-interpretation
  2594. (default-value 'org-todo-keywords)))))
  2595. (setq kwds (reverse kwds)))
  2596. (setq kwds (nreverse kwds))
  2597. (let (inter kws kw)
  2598. (while (setq kws (pop kwds))
  2599. (setq inter (pop kws) sep (member "|" kws)
  2600. kws0 (delete "|" (copy-sequence kws))
  2601. kwsa nil
  2602. kws1 (mapcar
  2603. (lambda (x)
  2604. ;; 1 2
  2605. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2606. (progn
  2607. (setq kw (match-string 1 x)
  2608. key (and (match-end 2) (match-string 2 x))
  2609. log (org-extract-log-state-settings x))
  2610. (push (cons kw (and key (string-to-char key))) kwsa)
  2611. (and log (push log org-todo-log-states))
  2612. kw)
  2613. (error "Invalid TODO keyword %s" x)))
  2614. kws0)
  2615. kwsa (if kwsa (append '((:startgroup))
  2616. (nreverse kwsa)
  2617. '((:endgroup))))
  2618. hw (car kws1)
  2619. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2620. tail (list inter hw (car dws) (org-last dws)))
  2621. (add-to-list 'org-todo-heads hw 'append)
  2622. (push kws1 org-todo-sets)
  2623. (setq org-done-keywords (append org-done-keywords dws nil))
  2624. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2625. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2626. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2627. (setq org-todo-sets (nreverse org-todo-sets)
  2628. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2629. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2630. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2631. ;; Process the constants
  2632. (when const
  2633. (let (e cst)
  2634. (while (setq e (pop const))
  2635. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2636. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2637. (setq org-table-formula-constants-local cst)))
  2638. ;; Process the tags.
  2639. (when tags
  2640. (let (e tgs)
  2641. (while (setq e (pop tags))
  2642. (cond
  2643. ((equal e "{") (push '(:startgroup) tgs))
  2644. ((equal e "}") (push '(:endgroup) tgs))
  2645. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2646. (push (cons (match-string 1 e)
  2647. (string-to-char (match-string 2 e)))
  2648. tgs))
  2649. (t (push (list e) tgs))))
  2650. (org-set-local 'org-tag-alist nil)
  2651. (while (setq e (pop tgs))
  2652. (or (and (stringp (car e))
  2653. (assoc (car e) org-tag-alist))
  2654. (push e org-tag-alist)))))
  2655. ;; Compute the regular expressions and other local variables
  2656. (if (not org-done-keywords)
  2657. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2658. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2659. (length org-scheduled-string)
  2660. (length org-clock-string)
  2661. (length org-closed-string)))
  2662. org-drawer-regexp
  2663. (concat "^[ \t]*:\\("
  2664. (mapconcat 'regexp-quote org-drawers "\\|")
  2665. "\\):[ \t]*$")
  2666. org-not-done-keywords
  2667. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2668. org-todo-regexp
  2669. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2670. "\\|") "\\)\\>")
  2671. org-not-done-regexp
  2672. (concat "\\<\\("
  2673. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2674. "\\)\\>")
  2675. org-todo-line-regexp
  2676. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2677. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2678. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2679. org-complex-heading-regexp
  2680. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2681. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2682. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2683. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2684. org-nl-done-regexp
  2685. (concat "\n\\*+[ \t]+"
  2686. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2687. "\\)" "\\>")
  2688. org-todo-line-tags-regexp
  2689. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2690. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2691. (org-re
  2692. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2693. org-looking-at-done-regexp
  2694. (concat "^" "\\(?:"
  2695. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2696. "\\>")
  2697. org-deadline-regexp (concat "\\<" org-deadline-string)
  2698. org-deadline-time-regexp
  2699. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2700. org-deadline-line-regexp
  2701. (concat "\\<\\(" org-deadline-string "\\).*")
  2702. org-scheduled-regexp
  2703. (concat "\\<" org-scheduled-string)
  2704. org-scheduled-time-regexp
  2705. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2706. org-closed-time-regexp
  2707. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2708. org-keyword-time-regexp
  2709. (concat "\\<\\(" org-scheduled-string
  2710. "\\|" org-deadline-string
  2711. "\\|" org-closed-string
  2712. "\\|" org-clock-string "\\)"
  2713. " *[[<]\\([^]>]+\\)[]>]")
  2714. org-keyword-time-not-clock-regexp
  2715. (concat "\\<\\(" org-scheduled-string
  2716. "\\|" org-deadline-string
  2717. "\\|" org-closed-string
  2718. "\\)"
  2719. " *[[<]\\([^]>]+\\)[]>]")
  2720. org-maybe-keyword-time-regexp
  2721. (concat "\\(\\<\\(" org-scheduled-string
  2722. "\\|" org-deadline-string
  2723. "\\|" org-closed-string
  2724. "\\|" org-clock-string "\\)\\)?"
  2725. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2726. org-planning-or-clock-line-re
  2727. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2728. "\\|" org-deadline-string
  2729. "\\|" org-closed-string "\\|" org-clock-string
  2730. "\\)\\>\\)")
  2731. )
  2732. (org-compute-latex-and-specials-regexp)
  2733. (org-set-font-lock-defaults))))
  2734. (defun org-file-contents (file &optional noerror)
  2735. "Return the contents of FILE, as a string."
  2736. (if (or (not file)
  2737. (not (file-readable-p file)))
  2738. (if noerror
  2739. (progn
  2740. (message "Cannot read file %s" file)
  2741. (ding) (sit-for 2)
  2742. "")
  2743. (error "Cannot read file %s" file))
  2744. (with-temp-buffer
  2745. (insert-file-contents file)
  2746. (buffer-string))))
  2747. (defun org-extract-log-state-settings (x)
  2748. "Extract the log state setting from a TODO keyword string.
  2749. This will extract info from a string like \"WAIT(w@/!)\"."
  2750. (let (kw key log1 log2)
  2751. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2752. (setq kw (match-string 1 x)
  2753. key (and (match-end 2) (match-string 2 x))
  2754. log1 (and (match-end 3) (match-string 3 x))
  2755. log2 (and (match-end 4) (match-string 4 x)))
  2756. (and (or log1 log2)
  2757. (list kw
  2758. (and log1 (if (equal log1 "!") 'time 'note))
  2759. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2760. (defun org-remove-keyword-keys (list)
  2761. "Remove a pair of parenthesis at the end of each string in LIST."
  2762. (mapcar (lambda (x)
  2763. (if (string-match "(.*)$" x)
  2764. (substring x 0 (match-beginning 0))
  2765. x))
  2766. list))
  2767. ;; FIXME: this could be done much better, using second characters etc.
  2768. (defun org-assign-fast-keys (alist)
  2769. "Assign fast keys to a keyword-key alist.
  2770. Respect keys that are already there."
  2771. (let (new e k c c1 c2 (char ?a))
  2772. (while (setq e (pop alist))
  2773. (cond
  2774. ((equal e '(:startgroup)) (push e new))
  2775. ((equal e '(:endgroup)) (push e new))
  2776. (t
  2777. (setq k (car e) c2 nil)
  2778. (if (cdr e)
  2779. (setq c (cdr e))
  2780. ;; automatically assign a character.
  2781. (setq c1 (string-to-char
  2782. (downcase (substring
  2783. k (if (= (string-to-char k) ?@) 1 0)))))
  2784. (if (or (rassoc c1 new) (rassoc c1 alist))
  2785. (while (or (rassoc char new) (rassoc char alist))
  2786. (setq char (1+ char)))
  2787. (setq c2 c1))
  2788. (setq c (or c2 char)))
  2789. (push (cons k c) new))))
  2790. (nreverse new)))
  2791. ;;; Some variables used in various places
  2792. (defvar org-window-configuration nil
  2793. "Used in various places to store a window configuration.")
  2794. (defvar org-finish-function nil
  2795. "Function to be called when `C-c C-c' is used.
  2796. This is for getting out of special buffers like remember.")
  2797. ;; FIXME: Occasionally check by commenting these, to make sure
  2798. ;; no other functions uses these, forgetting to let-bind them.
  2799. (defvar entry)
  2800. (defvar state)
  2801. (defvar last-state)
  2802. (defvar date)
  2803. (defvar description)
  2804. ;; Defined somewhere in this file, but used before definition.
  2805. (defvar org-html-entities)
  2806. (defvar org-struct-menu)
  2807. (defvar org-org-menu)
  2808. (defvar org-tbl-menu)
  2809. (defvar org-agenda-keymap)
  2810. ;;;; Define the Org-mode
  2811. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2812. (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."))
  2813. ;; We use a before-change function to check if a table might need
  2814. ;; an update.
  2815. (defvar org-table-may-need-update t
  2816. "Indicates that a table might need an update.
  2817. This variable is set by `org-before-change-function'.
  2818. `org-table-align' sets it back to nil.")
  2819. (defun org-before-change-function (beg end)
  2820. "Every change indicates that a table might need an update."
  2821. (setq org-table-may-need-update t))
  2822. (defvar org-mode-map)
  2823. (defvar org-mode-hook nil)
  2824. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2825. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2826. (defvar org-table-buffer-is-an nil)
  2827. (defconst org-outline-regexp "\\*+ ")
  2828. ;;;###autoload
  2829. (define-derived-mode org-mode outline-mode "Org"
  2830. "Outline-based notes management and organizer, alias
  2831. \"Carsten's outline-mode for keeping track of everything.\"
  2832. Org-mode develops organizational tasks around a NOTES file which
  2833. contains information about projects as plain text. Org-mode is
  2834. implemented on top of outline-mode, which is ideal to keep the content
  2835. of large files well structured. It supports ToDo items, deadlines and
  2836. time stamps, which magically appear in the diary listing of the Emacs
  2837. calendar. Tables are easily created with a built-in table editor.
  2838. Plain text URL-like links connect to websites, emails (VM), Usenet
  2839. messages (Gnus), BBDB entries, and any files related to the project.
  2840. For printing and sharing of notes, an Org-mode file (or a part of it)
  2841. can be exported as a structured ASCII or HTML file.
  2842. The following commands are available:
  2843. \\{org-mode-map}"
  2844. ;; Get rid of Outline menus, they are not needed
  2845. ;; Need to do this here because define-derived-mode sets up
  2846. ;; the keymap so late. Still, it is a waste to call this each time
  2847. ;; we switch another buffer into org-mode.
  2848. (if (featurep 'xemacs)
  2849. (when (boundp 'outline-mode-menu-heading)
  2850. ;; Assume this is Greg's port, it used easymenu
  2851. (easy-menu-remove outline-mode-menu-heading)
  2852. (easy-menu-remove outline-mode-menu-show)
  2853. (easy-menu-remove outline-mode-menu-hide))
  2854. (define-key org-mode-map [menu-bar headings] 'undefined)
  2855. (define-key org-mode-map [menu-bar hide] 'undefined)
  2856. (define-key org-mode-map [menu-bar show] 'undefined))
  2857. (org-load-modules-maybe)
  2858. (easy-menu-add org-org-menu)
  2859. (easy-menu-add org-tbl-menu)
  2860. (org-install-agenda-files-menu)
  2861. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2862. (org-add-to-invisibility-spec '(org-cwidth))
  2863. (when (featurep 'xemacs)
  2864. (org-set-local 'line-move-ignore-invisible t))
  2865. (org-set-local 'outline-regexp org-outline-regexp)
  2866. (org-set-local 'outline-level 'org-outline-level)
  2867. (when (and org-ellipsis
  2868. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2869. (fboundp 'make-glyph-code))
  2870. (unless org-display-table
  2871. (setq org-display-table (make-display-table)))
  2872. (set-display-table-slot
  2873. org-display-table 4
  2874. (vconcat (mapcar
  2875. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2876. org-ellipsis)))
  2877. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2878. (setq buffer-display-table org-display-table))
  2879. (org-set-regexps-and-options)
  2880. ;; Calc embedded
  2881. (org-set-local 'calc-embedded-open-mode "# ")
  2882. (modify-syntax-entry ?# "<")
  2883. (modify-syntax-entry ?@ "w")
  2884. (if org-startup-truncated (setq truncate-lines t))
  2885. (org-set-local 'font-lock-unfontify-region-function
  2886. 'org-unfontify-region)
  2887. ;; Activate before-change-function
  2888. (org-set-local 'org-table-may-need-update t)
  2889. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2890. 'local)
  2891. ;; Check for running clock before killing a buffer
  2892. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2893. ;; Paragraphs and auto-filling
  2894. (org-set-autofill-regexps)
  2895. (setq indent-line-function 'org-indent-line-function)
  2896. (org-update-radio-target-regexp)
  2897. ;; Comment characters
  2898. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2899. (org-set-local 'comment-padding " ")
  2900. ;; Align options lines
  2901. (org-set-local
  2902. 'align-mode-rules-list
  2903. '((org-in-buffer-settings
  2904. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2905. (modes . '(org-mode)))))
  2906. ;; Imenu
  2907. (org-set-local 'imenu-create-index-function
  2908. 'org-imenu-get-tree)
  2909. ;; Make isearch reveal context
  2910. (if (or (featurep 'xemacs)
  2911. (not (boundp 'outline-isearch-open-invisible-function)))
  2912. ;; Emacs 21 and XEmacs make use of the hook
  2913. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2914. ;; Emacs 22 deals with this through a special variable
  2915. (org-set-local 'outline-isearch-open-invisible-function
  2916. (lambda (&rest ignore) (org-show-context 'isearch))))
  2917. ;; If empty file that did not turn on org-mode automatically, make it to.
  2918. (if (and org-insert-mode-line-in-empty-file
  2919. (interactive-p)
  2920. (= (point-min) (point-max)))
  2921. (insert "# -*- mode: org -*-\n\n"))
  2922. (unless org-inhibit-startup
  2923. (when org-startup-align-all-tables
  2924. (let ((bmp (buffer-modified-p)))
  2925. (org-table-map-tables 'org-table-align)
  2926. (set-buffer-modified-p bmp)))
  2927. (org-set-startup-visibility)))
  2928. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2929. (defun org-current-time ()
  2930. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2931. (if (> (car org-time-stamp-rounding-minutes) 1)
  2932. (let ((r (car org-time-stamp-rounding-minutes))
  2933. (time (decode-time)))
  2934. (apply 'encode-time
  2935. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2936. (nthcdr 2 time))))
  2937. (current-time)))
  2938. ;;;; Font-Lock stuff, including the activators
  2939. (defvar org-mouse-map (make-sparse-keymap))
  2940. (org-defkey org-mouse-map
  2941. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2942. (org-defkey org-mouse-map
  2943. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2944. (when org-mouse-1-follows-link
  2945. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2946. (when org-tab-follows-link
  2947. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2948. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2949. (when org-return-follows-link
  2950. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2951. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2952. (require 'font-lock)
  2953. (defconst org-non-link-chars "]\t\n\r<>")
  2954. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2955. "shell" "elisp"))
  2956. (defvar org-link-types-re nil
  2957. "Matches a link that has a url-like prefix like \"http:\"")
  2958. (defvar org-link-re-with-space nil
  2959. "Matches a link with spaces, optional angular brackets around it.")
  2960. (defvar org-link-re-with-space2 nil
  2961. "Matches a link with spaces, optional angular brackets around it.")
  2962. (defvar org-angle-link-re nil
  2963. "Matches link with angular brackets, spaces are allowed.")
  2964. (defvar org-plain-link-re nil
  2965. "Matches plain link, without spaces.")
  2966. (defvar org-bracket-link-regexp nil
  2967. "Matches a link in double brackets.")
  2968. (defvar org-bracket-link-analytic-regexp nil
  2969. "Regular expression used to analyze links.
  2970. Here is what the match groups contain after a match:
  2971. 1: http:
  2972. 2: http
  2973. 3: path
  2974. 4: [desc]
  2975. 5: desc")
  2976. (defvar org-any-link-re nil
  2977. "Regular expression matching any link.")
  2978. (defun org-make-link-regexps ()
  2979. "Update the link regular expressions.
  2980. This should be called after the variable `org-link-types' has changed."
  2981. (setq org-link-types-re
  2982. (concat
  2983. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2984. org-link-re-with-space
  2985. (concat
  2986. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2987. "\\([^" org-non-link-chars " ]"
  2988. "[^" org-non-link-chars "]*"
  2989. "[^" org-non-link-chars " ]\\)>?")
  2990. org-link-re-with-space2
  2991. (concat
  2992. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2993. "\\([^" org-non-link-chars " ]"
  2994. "[^]\t\n\r]*"
  2995. "[^" org-non-link-chars " ]\\)>?")
  2996. org-angle-link-re
  2997. (concat
  2998. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2999. "\\([^" org-non-link-chars " ]"
  3000. "[^" org-non-link-chars "]*"
  3001. "\\)>")
  3002. org-plain-link-re
  3003. (concat
  3004. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3005. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3006. org-bracket-link-regexp
  3007. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3008. org-bracket-link-analytic-regexp
  3009. (concat
  3010. "\\[\\["
  3011. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3012. "\\([^]]+\\)"
  3013. "\\]"
  3014. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3015. "\\]")
  3016. org-any-link-re
  3017. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3018. org-angle-link-re "\\)\\|\\("
  3019. org-plain-link-re "\\)")))
  3020. (org-make-link-regexps)
  3021. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3022. "Regular expression for fast time stamp matching.")
  3023. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3024. "Regular expression for fast time stamp matching.")
  3025. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3026. "Regular expression matching time strings for analysis.
  3027. This one does not require the space after the date, so it can be used
  3028. on a string that terminates immediately after the date.")
  3029. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3030. "Regular expression matching time strings for analysis.")
  3031. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3032. "Regular expression matching time stamps, with groups.")
  3033. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3034. "Regular expression matching time stamps (also [..]), with groups.")
  3035. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3036. "Regular expression matching a time stamp range.")
  3037. (defconst org-tr-regexp-both
  3038. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3039. "Regular expression matching a time stamp range.")
  3040. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3041. org-ts-regexp "\\)?")
  3042. "Regular expression matching a time stamp or time stamp range.")
  3043. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3044. org-ts-regexp-both "\\)?")
  3045. "Regular expression matching a time stamp or time stamp range.
  3046. The time stamps may be either active or inactive.")
  3047. (defvar org-emph-face nil)
  3048. (defun org-do-emphasis-faces (limit)
  3049. "Run through the buffer and add overlays to links."
  3050. (let (rtn)
  3051. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3052. (if (not (= (char-after (match-beginning 3))
  3053. (char-after (match-beginning 4))))
  3054. (progn
  3055. (setq rtn t)
  3056. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3057. 'face
  3058. (nth 1 (assoc (match-string 3)
  3059. org-emphasis-alist)))
  3060. (add-text-properties (match-beginning 2) (match-end 2)
  3061. '(font-lock-multiline t))
  3062. (when org-hide-emphasis-markers
  3063. (add-text-properties (match-end 4) (match-beginning 5)
  3064. '(invisible org-link))
  3065. (add-text-properties (match-beginning 3) (match-end 3)
  3066. '(invisible org-link)))))
  3067. (backward-char 1))
  3068. rtn))
  3069. (defun org-emphasize (&optional char)
  3070. "Insert or change an emphasis, i.e. a font like bold or italic.
  3071. If there is an active region, change that region to a new emphasis.
  3072. If there is no region, just insert the marker characters and position
  3073. the cursor between them.
  3074. CHAR should be either the marker character, or the first character of the
  3075. HTML tag associated with that emphasis. If CHAR is a space, the means
  3076. to remove the emphasis of the selected region.
  3077. If char is not given (for example in an interactive call) it
  3078. will be prompted for."
  3079. (interactive)
  3080. (let ((eal org-emphasis-alist) e det
  3081. (erc org-emphasis-regexp-components)
  3082. (prompt "")
  3083. (string "") beg end move tag c s)
  3084. (if (org-region-active-p)
  3085. (setq beg (region-beginning) end (region-end)
  3086. string (buffer-substring beg end))
  3087. (setq move t))
  3088. (while (setq e (pop eal))
  3089. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3090. c (aref tag 0))
  3091. (push (cons c (string-to-char (car e))) det)
  3092. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3093. (substring tag 1)))))
  3094. (unless char
  3095. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3096. (setq char (read-char-exclusive)))
  3097. (setq char (or (cdr (assoc char det)) char))
  3098. (if (equal char ?\ )
  3099. (setq s "" move nil)
  3100. (unless (assoc (char-to-string char) org-emphasis-alist)
  3101. (error "No such emphasis marker: \"%c\"" char))
  3102. (setq s (char-to-string char)))
  3103. (while (and (> (length string) 1)
  3104. (equal (substring string 0 1) (substring string -1))
  3105. (assoc (substring string 0 1) org-emphasis-alist))
  3106. (setq string (substring string 1 -1)))
  3107. (setq string (concat s string s))
  3108. (if beg (delete-region beg end))
  3109. (unless (or (bolp)
  3110. (string-match (concat "[" (nth 0 erc) "\n]")
  3111. (char-to-string (char-before (point)))))
  3112. (insert " "))
  3113. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3114. (char-to-string (char-after (point))))
  3115. (insert " ") (backward-char 1))
  3116. (insert string)
  3117. (and move (backward-char 1))))
  3118. (defconst org-nonsticky-props
  3119. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3120. (defun org-activate-plain-links (limit)
  3121. "Run through the buffer and add overlays to links."
  3122. (catch 'exit
  3123. (let (f)
  3124. (while (re-search-forward org-plain-link-re limit t)
  3125. (setq f (get-text-property (match-beginning 0) 'face))
  3126. (if (or (eq f 'org-tag)
  3127. (and (listp f) (memq 'org-tag f)))
  3128. nil
  3129. (add-text-properties (match-beginning 0) (match-end 0)
  3130. (list 'mouse-face 'highlight
  3131. 'rear-nonsticky org-nonsticky-props
  3132. 'keymap org-mouse-map
  3133. ))
  3134. (throw 'exit t))))))
  3135. (defun org-activate-code (limit)
  3136. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3137. (progn
  3138. (remove-text-properties (match-beginning 0) (match-end 0)
  3139. '(display t invisible t intangible t))
  3140. t)))
  3141. (defun org-activate-angle-links (limit)
  3142. "Run through the buffer and add overlays to links."
  3143. (if (re-search-forward org-angle-link-re limit t)
  3144. (progn
  3145. (add-text-properties (match-beginning 0) (match-end 0)
  3146. (list 'mouse-face 'highlight
  3147. 'rear-nonsticky org-nonsticky-props
  3148. 'keymap org-mouse-map
  3149. ))
  3150. t)))
  3151. (defun org-activate-bracket-links (limit)
  3152. "Run through the buffer and add overlays to bracketed links."
  3153. (if (re-search-forward org-bracket-link-regexp limit t)
  3154. (let* ((help (concat "LINK: "
  3155. (org-match-string-no-properties 1)))
  3156. ;; FIXME: above we should remove the escapes.
  3157. ;; but that requires another match, protecting match data,
  3158. ;; a lot of overhead for font-lock.
  3159. (ip (org-maybe-intangible
  3160. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3161. 'keymap org-mouse-map 'mouse-face 'highlight
  3162. 'font-lock-multiline t 'help-echo help)))
  3163. (vp (list 'rear-nonsticky org-nonsticky-props
  3164. 'keymap org-mouse-map 'mouse-face 'highlight
  3165. ' font-lock-multiline t 'help-echo help)))
  3166. ;; We need to remove the invisible property here. Table narrowing
  3167. ;; may have made some of this invisible.
  3168. (remove-text-properties (match-beginning 0) (match-end 0)
  3169. '(invisible nil))
  3170. (if (match-end 3)
  3171. (progn
  3172. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3173. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3174. (add-text-properties (match-end 3) (match-end 0) ip))
  3175. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3176. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3177. (add-text-properties (match-end 1) (match-end 0) ip))
  3178. t)))
  3179. (defun org-activate-dates (limit)
  3180. "Run through the buffer and add overlays to dates."
  3181. (if (re-search-forward org-tsr-regexp-both limit t)
  3182. (progn
  3183. (add-text-properties (match-beginning 0) (match-end 0)
  3184. (list 'mouse-face 'highlight
  3185. 'rear-nonsticky org-nonsticky-props
  3186. 'keymap org-mouse-map))
  3187. (when org-display-custom-times
  3188. (if (match-end 3)
  3189. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3190. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3191. t)))
  3192. (defvar org-target-link-regexp nil
  3193. "Regular expression matching radio targets in plain text.")
  3194. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3195. "Regular expression matching a link target.")
  3196. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3197. "Regular expression matching a radio target.")
  3198. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3199. "Regular expression matching any target.")
  3200. (defun org-activate-target-links (limit)
  3201. "Run through the buffer and add overlays to target matches."
  3202. (when org-target-link-regexp
  3203. (let ((case-fold-search t))
  3204. (if (re-search-forward org-target-link-regexp limit t)
  3205. (progn
  3206. (add-text-properties (match-beginning 0) (match-end 0)
  3207. (list 'mouse-face 'highlight
  3208. 'rear-nonsticky org-nonsticky-props
  3209. 'keymap org-mouse-map
  3210. 'help-echo "Radio target link"
  3211. 'org-linked-text t))
  3212. t)))))
  3213. (defun org-update-radio-target-regexp ()
  3214. "Find all radio targets in this file and update the regular expression."
  3215. (interactive)
  3216. (when (memq 'radio org-activate-links)
  3217. (setq org-target-link-regexp
  3218. (org-make-target-link-regexp (org-all-targets 'radio)))
  3219. (org-restart-font-lock)))
  3220. (defun org-hide-wide-columns (limit)
  3221. (let (s e)
  3222. (setq s (text-property-any (point) (or limit (point-max))
  3223. 'org-cwidth t))
  3224. (when s
  3225. (setq e (next-single-property-change s 'org-cwidth))
  3226. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3227. (goto-char e)
  3228. t)))
  3229. (defvar org-latex-and-specials-regexp nil
  3230. "Regular expression for highlighting export special stuff.")
  3231. (defvar org-match-substring-regexp)
  3232. (defvar org-match-substring-with-braces-regexp)
  3233. (defvar org-export-html-special-string-regexps)
  3234. (defun org-compute-latex-and-specials-regexp ()
  3235. "Compute regular expression for stuff treated specially by exporters."
  3236. (if (not org-highlight-latex-fragments-and-specials)
  3237. (org-set-local 'org-latex-and-specials-regexp nil)
  3238. (require 'org-exp)
  3239. (let*
  3240. ((matchers (plist-get org-format-latex-options :matchers))
  3241. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3242. org-latex-regexps)))
  3243. (options (org-combine-plists (org-default-export-plist)
  3244. (org-infile-export-plist)))
  3245. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3246. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3247. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3248. (org-export-html-expand (plist-get options :expand-quoted-html))
  3249. (org-export-with-special-strings (plist-get options :special-strings))
  3250. (re-sub
  3251. (cond
  3252. ((equal org-export-with-sub-superscripts '{})
  3253. (list org-match-substring-with-braces-regexp))
  3254. (org-export-with-sub-superscripts
  3255. (list org-match-substring-regexp))
  3256. (t nil)))
  3257. (re-latex
  3258. (if org-export-with-LaTeX-fragments
  3259. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3260. (re-macros
  3261. (if org-export-with-TeX-macros
  3262. (list (concat "\\\\"
  3263. (regexp-opt
  3264. (append (mapcar 'car org-html-entities)
  3265. (if (boundp 'org-latex-entities)
  3266. org-latex-entities nil))
  3267. 'words))) ; FIXME
  3268. ))
  3269. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3270. (re-special (if org-export-with-special-strings
  3271. (mapcar (lambda (x) (car x))
  3272. org-export-html-special-string-regexps)))
  3273. (re-rest
  3274. (delq nil
  3275. (list
  3276. (if org-export-html-expand "@<[^>\n]+>")
  3277. ))))
  3278. (org-set-local
  3279. 'org-latex-and-specials-regexp
  3280. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3281. re-rest) "\\|")))))
  3282. (defun org-do-latex-and-special-faces (limit)
  3283. "Run through the buffer and add overlays to links."
  3284. (when org-latex-and-specials-regexp
  3285. (let (rtn d)
  3286. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3287. limit t))
  3288. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3289. 'face))
  3290. '(org-code org-verbatim underline)))
  3291. (progn
  3292. (setq rtn t
  3293. d (cond ((member (char-after (1+ (match-beginning 0)))
  3294. '(?_ ?^)) 1)
  3295. (t 0)))
  3296. (font-lock-prepend-text-property
  3297. (+ d (match-beginning 0)) (match-end 0)
  3298. 'face 'org-latex-and-export-specials)
  3299. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3300. '(font-lock-multiline t)))))
  3301. rtn)))
  3302. (defun org-restart-font-lock ()
  3303. "Restart font-lock-mode, to force refontification."
  3304. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3305. (font-lock-mode -1)
  3306. (font-lock-mode 1)))
  3307. (defun org-all-targets (&optional radio)
  3308. "Return a list of all targets in this file.
  3309. With optional argument RADIO, only find radio targets."
  3310. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3311. rtn)
  3312. (save-excursion
  3313. (goto-char (point-min))
  3314. (while (re-search-forward re nil t)
  3315. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3316. rtn)))
  3317. (defun org-make-target-link-regexp (targets)
  3318. "Make regular expression matching all strings in TARGETS.
  3319. The regular expression finds the targets also if there is a line break
  3320. between words."
  3321. (and targets
  3322. (concat
  3323. "\\<\\("
  3324. (mapconcat
  3325. (lambda (x)
  3326. (while (string-match " +" x)
  3327. (setq x (replace-match "\\s-+" t t x)))
  3328. x)
  3329. targets
  3330. "\\|")
  3331. "\\)\\>")))
  3332. (defun org-activate-tags (limit)
  3333. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3334. (progn
  3335. (add-text-properties (match-beginning 1) (match-end 1)
  3336. (list 'mouse-face 'highlight
  3337. 'rear-nonsticky org-nonsticky-props
  3338. 'keymap org-mouse-map))
  3339. t)))
  3340. (defun org-outline-level ()
  3341. (save-excursion
  3342. (looking-at outline-regexp)
  3343. (if (match-beginning 1)
  3344. (+ (org-get-string-indentation (match-string 1)) 1000)
  3345. (1- (- (match-end 0) (match-beginning 0))))))
  3346. (defvar org-font-lock-keywords nil)
  3347. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3348. "Regular expression matching a property line.")
  3349. (defvar org-font-lock-hook nil
  3350. "Functions to be called for special font lock stuff.")
  3351. (defun org-font-lock-hook (limit)
  3352. (run-hook-with-args 'org-font-lock-hook limit))
  3353. (defun org-set-font-lock-defaults ()
  3354. (let* ((em org-fontify-emphasized-text)
  3355. (lk org-activate-links)
  3356. (org-font-lock-extra-keywords
  3357. (list
  3358. ;; Call the hook
  3359. '(org-font-lock-hook)
  3360. ;; Headlines
  3361. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3362. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3363. ;; Table lines
  3364. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3365. (1 'org-table t))
  3366. ;; Table internals
  3367. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3368. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3369. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3370. ;; Drawers
  3371. (list org-drawer-regexp '(0 'org-special-keyword t))
  3372. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3373. ;; Properties
  3374. (list org-property-re
  3375. '(1 'org-special-keyword t)
  3376. '(3 'org-property-value t))
  3377. (if org-format-transports-properties-p
  3378. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3379. ;; Links
  3380. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3381. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3382. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3383. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3384. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3385. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3386. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3387. '(org-hide-wide-columns (0 nil append))
  3388. ;; TODO lines
  3389. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3390. '(1 (org-get-todo-face 1) t))
  3391. ;; DONE
  3392. (if org-fontify-done-headline
  3393. (list (concat "^[*]+ +\\<\\("
  3394. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3395. "\\)\\(.*\\)")
  3396. '(2 'org-headline-done t))
  3397. nil)
  3398. ;; Priorities
  3399. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3400. ;; Special keywords
  3401. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3402. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3403. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3404. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3405. ;; Emphasis
  3406. (if em
  3407. (if (featurep 'xemacs)
  3408. '(org-do-emphasis-faces (0 nil append))
  3409. '(org-do-emphasis-faces)))
  3410. ;; Checkboxes
  3411. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3412. 2 'bold prepend)
  3413. (if org-provide-checkbox-statistics
  3414. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3415. (0 (org-get-checkbox-statistics-face) t)))
  3416. ;; Description list items
  3417. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3418. 2 'bold prepend)
  3419. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3420. '(1 'org-archived prepend))
  3421. ;; Specials
  3422. '(org-do-latex-and-special-faces)
  3423. ;; Code
  3424. '(org-activate-code (1 'org-code t))
  3425. ;; COMMENT
  3426. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3427. "\\|" org-quote-string "\\)\\>")
  3428. '(1 'org-special-keyword t))
  3429. '("^#.*" (0 'font-lock-comment-face t))
  3430. )))
  3431. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3432. ;; Now set the full font-lock-keywords
  3433. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3434. (org-set-local 'font-lock-defaults
  3435. '(org-font-lock-keywords t nil nil backward-paragraph))
  3436. (kill-local-variable 'font-lock-keywords) nil))
  3437. (defvar org-m nil)
  3438. (defvar org-l nil)
  3439. (defvar org-f nil)
  3440. (defun org-get-level-face (n)
  3441. "Get the right face for match N in font-lock matching of healdines."
  3442. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3443. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3444. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3445. (cond
  3446. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3447. ((eq n 2) org-f)
  3448. (t (if org-level-color-stars-only nil org-f))))
  3449. (defun org-get-todo-face (kwd)
  3450. "Get the right face for a TODO keyword KWD.
  3451. If KWD is a number, get the corresponding match group."
  3452. (if (numberp kwd) (setq kwd (match-string kwd)))
  3453. (or (cdr (assoc kwd org-todo-keyword-faces))
  3454. (and (member kwd org-done-keywords) 'org-done)
  3455. 'org-todo))
  3456. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3457. "Remove fontification and activation overlays from links."
  3458. (font-lock-default-unfontify-region beg end)
  3459. (let* ((buffer-undo-list t)
  3460. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3461. (inhibit-modification-hooks t)
  3462. deactivate-mark buffer-file-name buffer-file-truename)
  3463. (remove-text-properties beg end
  3464. '(mouse-face t keymap t org-linked-text t
  3465. invisible t intangible t))))
  3466. ;;;; Visibility cycling, including org-goto and indirect buffer
  3467. ;;; Cycling
  3468. (defvar org-cycle-global-status nil)
  3469. (make-variable-buffer-local 'org-cycle-global-status)
  3470. (defvar org-cycle-subtree-status nil)
  3471. (make-variable-buffer-local 'org-cycle-subtree-status)
  3472. ;;;###autoload
  3473. (defun org-cycle (&optional arg)
  3474. "Visibility cycling for Org-mode.
  3475. - When this function is called with a prefix argument, rotate the entire
  3476. buffer through 3 states (global cycling)
  3477. 1. OVERVIEW: Show only top-level headlines.
  3478. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3479. 3. SHOW ALL: Show everything.
  3480. When called with two C-c C-u prefixes, switch to the startup visibility,
  3481. determined by the variable `org-startup-folded', and by any VISIBILITY
  3482. properties in the buffer.
  3483. - When point is at the beginning of a headline, rotate the subtree started
  3484. by this line through 3 different states (local cycling)
  3485. 1. FOLDED: Only the main headline is shown.
  3486. 2. CHILDREN: The main headline and the direct children are shown.
  3487. From this state, you can move to one of the children
  3488. and zoom in further.
  3489. 3. SUBTREE: Show the entire subtree, including body text.
  3490. - When there is a numeric prefix, go up to a heading with level ARG, do
  3491. a `show-subtree' and return to the previous cursor position. If ARG
  3492. is negative, go up that many levels.
  3493. - When point is not at the beginning of a headline, execute the global
  3494. binding for TAB, which is re-indenting the line. See the option
  3495. `org-cycle-emulate-tab' for details.
  3496. - Special case: if point is at the beginning of the buffer and there is
  3497. no headline in line 1, this function will act as if called with prefix arg.
  3498. But only if also the variable `org-cycle-global-at-bob' is t."
  3499. (interactive "P")
  3500. (org-load-modules-maybe)
  3501. (let* ((outline-regexp
  3502. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3503. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3504. outline-regexp))
  3505. (bob-special (and org-cycle-global-at-bob (bobp)
  3506. (not (looking-at outline-regexp))))
  3507. (org-cycle-hook
  3508. (if bob-special
  3509. (delq 'org-optimize-window-after-visibility-change
  3510. (copy-sequence org-cycle-hook))
  3511. org-cycle-hook))
  3512. (pos (point)))
  3513. (if (or bob-special (equal arg '(4)))
  3514. ;; special case: use global cycling
  3515. (setq arg t))
  3516. (cond
  3517. ((equal arg '(16))
  3518. (org-set-startup-visibility)
  3519. (message "Startup visibility, plus VISIBILITY properties."))
  3520. ((org-at-table-p 'any)
  3521. ;; Enter the table or move to the next field in the table
  3522. (or (org-table-recognize-table.el)
  3523. (progn
  3524. (if arg (org-table-edit-field t)
  3525. (org-table-justify-field-maybe)
  3526. (call-interactively 'org-table-next-field)))))
  3527. ((eq arg t) ;; Global cycling
  3528. (cond
  3529. ((and (eq last-command this-command)
  3530. (eq org-cycle-global-status 'overview))
  3531. ;; We just created the overview - now do table of contents
  3532. ;; This can be slow in very large buffers, so indicate action
  3533. (message "CONTENTS...")
  3534. (org-content)
  3535. (message "CONTENTS...done")
  3536. (setq org-cycle-global-status 'contents)
  3537. (run-hook-with-args 'org-cycle-hook 'contents))
  3538. ((and (eq last-command this-command)
  3539. (eq org-cycle-global-status 'contents))
  3540. ;; We just showed the table of contents - now show everything
  3541. (show-all)
  3542. (message "SHOW ALL")
  3543. (setq org-cycle-global-status 'all)
  3544. (run-hook-with-args 'org-cycle-hook 'all))
  3545. (t
  3546. ;; Default action: go to overview
  3547. (org-overview)
  3548. (message "OVERVIEW")
  3549. (setq org-cycle-global-status 'overview)
  3550. (run-hook-with-args 'org-cycle-hook 'overview))))
  3551. ((and org-drawers org-drawer-regexp
  3552. (save-excursion
  3553. (beginning-of-line 1)
  3554. (looking-at org-drawer-regexp)))
  3555. ;; Toggle block visibility
  3556. (org-flag-drawer
  3557. (not (get-char-property (match-end 0) 'invisible))))
  3558. ((integerp arg)
  3559. ;; Show-subtree, ARG levels up from here.
  3560. (save-excursion
  3561. (org-back-to-heading)
  3562. (outline-up-heading (if (< arg 0) (- arg)
  3563. (- (funcall outline-level) arg)))
  3564. (org-show-subtree)))
  3565. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3566. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3567. ;; At a heading: rotate between three different views
  3568. (org-back-to-heading)
  3569. (let ((goal-column 0) eoh eol eos)
  3570. ;; First, some boundaries
  3571. (save-excursion
  3572. (org-back-to-heading)
  3573. (save-excursion
  3574. (beginning-of-line 2)
  3575. (while (and (not (eobp)) ;; this is like `next-line'
  3576. (get-char-property (1- (point)) 'invisible))
  3577. (beginning-of-line 2)) (setq eol (point)))
  3578. (outline-end-of-heading) (setq eoh (point))
  3579. (org-end-of-subtree t)
  3580. (unless (eobp)
  3581. (skip-chars-forward " \t\n")
  3582. (beginning-of-line 1) ; in case this is an item
  3583. )
  3584. (setq eos (1- (point))))
  3585. ;; Find out what to do next and set `this-command'
  3586. (cond
  3587. ((= eos eoh)
  3588. ;; Nothing is hidden behind this heading
  3589. (message "EMPTY ENTRY")
  3590. (setq org-cycle-subtree-status nil)
  3591. (save-excursion
  3592. (goto-char eos)
  3593. (outline-next-heading)
  3594. (if (org-invisible-p) (org-flag-heading nil))))
  3595. ((or (>= eol eos)
  3596. (not (string-match "\\S-" (buffer-substring eol eos))))
  3597. ;; Entire subtree is hidden in one line: open it
  3598. (org-show-entry)
  3599. (show-children)
  3600. (message "CHILDREN")
  3601. (save-excursion
  3602. (goto-char eos)
  3603. (outline-next-heading)
  3604. (if (org-invisible-p) (org-flag-heading nil)))
  3605. (setq org-cycle-subtree-status 'children)
  3606. (run-hook-with-args 'org-cycle-hook 'children))
  3607. ((and (eq last-command this-command)
  3608. (eq org-cycle-subtree-status 'children))
  3609. ;; We just showed the children, now show everything.
  3610. (org-show-subtree)
  3611. (message "SUBTREE")
  3612. (setq org-cycle-subtree-status 'subtree)
  3613. (run-hook-with-args 'org-cycle-hook 'subtree))
  3614. (t
  3615. ;; Default action: hide the subtree.
  3616. (hide-subtree)
  3617. (message "FOLDED")
  3618. (setq org-cycle-subtree-status 'folded)
  3619. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3620. ;; TAB emulation and template completion
  3621. (buffer-read-only (org-back-to-heading))
  3622. ((org-try-structure-completion))
  3623. ((org-try-cdlatex-tab))
  3624. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3625. (or (not (bolp))
  3626. (not (looking-at outline-regexp))))
  3627. (call-interactively (global-key-binding "\t")))
  3628. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3629. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3630. (or (and (eq org-cycle-emulate-tab 'white)
  3631. (= (match-end 0) (point-at-eol)))
  3632. (and (eq org-cycle-emulate-tab 'whitestart)
  3633. (>= (match-end 0) pos))))
  3634. t
  3635. (eq org-cycle-emulate-tab t))
  3636. (call-interactively (global-key-binding "\t")))
  3637. (t (save-excursion
  3638. (org-back-to-heading)
  3639. (org-cycle))))))
  3640. ;;;###autoload
  3641. (defun org-global-cycle (&optional arg)
  3642. "Cycle the global visibility. For details see `org-cycle'.
  3643. With C-u prefix arg, switch to startup visibility.
  3644. With a numeric prefix, show all headlines up to that level."
  3645. (interactive "P")
  3646. (let ((org-cycle-include-plain-lists
  3647. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3648. (cond
  3649. ((integerp arg)
  3650. (show-all)
  3651. (hide-sublevels arg)
  3652. (setq org-cycle-global-status 'contents))
  3653. ((equal arg '(4))
  3654. (org-set-startup-visibility)
  3655. (message "Startup visibility, plus VISIBILITY properties."))
  3656. (t
  3657. (org-cycle '(4))))))
  3658. (defun org-set-startup-visibility ()
  3659. "Set the visibility required by startup options and properties."
  3660. (cond
  3661. ((eq org-startup-folded t)
  3662. (org-cycle '(4)))
  3663. ((eq org-startup-folded 'content)
  3664. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3665. (org-cycle '(4)) (org-cycle '(4)))))
  3666. (org-set-visibility-according-to-property 'no-cleanup)
  3667. (org-cycle-hide-archived-subtrees 'all)
  3668. (org-cycle-hide-drawers 'all)
  3669. (org-cycle-show-empty-lines 'all))
  3670. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3671. "Switch subtree visibilities according to :VISIBILITY: property."
  3672. (interactive)
  3673. (let (state)
  3674. (save-excursion
  3675. (goto-char (point-min))
  3676. (while (re-search-forward
  3677. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3678. nil t)
  3679. (setq state (match-string 1))
  3680. (save-excursion
  3681. (org-back-to-heading t)
  3682. (hide-subtree)
  3683. (org-reveal)
  3684. (cond
  3685. ((equal state '("fold" "folded"))
  3686. (hide-subtree))
  3687. ((equal state "children")
  3688. (org-show-hidden-entry)
  3689. (show-children))
  3690. ((equal state "content")
  3691. (save-excursion
  3692. (save-restriction
  3693. (org-narrow-to-subtree)
  3694. (org-content))))
  3695. ((member state '("all" "showall"))
  3696. (show-subtree)))))
  3697. (unless no-cleanup
  3698. (org-cycle-hide-archived-subtrees 'all)
  3699. (org-cycle-hide-drawers 'all)
  3700. (org-cycle-show-empty-lines 'all)))))
  3701. (defun org-overview ()
  3702. "Switch to overview mode, shoing only top-level headlines.
  3703. Really, this shows all headlines with level equal or greater than the level
  3704. of the first headline in the buffer. This is important, because if the
  3705. first headline is not level one, then (hide-sublevels 1) gives confusing
  3706. results."
  3707. (interactive)
  3708. (let ((level (save-excursion
  3709. (goto-char (point-min))
  3710. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3711. (progn
  3712. (goto-char (match-beginning 0))
  3713. (funcall outline-level))))))
  3714. (and level (hide-sublevels level))))
  3715. (defun org-content (&optional arg)
  3716. "Show all headlines in the buffer, like a table of contents.
  3717. With numerical argument N, show content up to level N."
  3718. (interactive "P")
  3719. (save-excursion
  3720. ;; Visit all headings and show their offspring
  3721. (and (integerp arg) (org-overview))
  3722. (goto-char (point-max))
  3723. (catch 'exit
  3724. (while (and (progn (condition-case nil
  3725. (outline-previous-visible-heading 1)
  3726. (error (goto-char (point-min))))
  3727. t)
  3728. (looking-at outline-regexp))
  3729. (if (integerp arg)
  3730. (show-children (1- arg))
  3731. (show-branches))
  3732. (if (bobp) (throw 'exit nil))))))
  3733. (defun org-optimize-window-after-visibility-change (state)
  3734. "Adjust the window after a change in outline visibility.
  3735. This function is the default value of the hook `org-cycle-hook'."
  3736. (when (get-buffer-window (current-buffer))
  3737. (cond
  3738. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3739. ; ((eq state 'overview) (org-beginning-of-line))
  3740. ((eq state 'content) nil)
  3741. ((eq state 'all) nil)
  3742. ((eq state 'folded) nil)
  3743. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3744. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3745. (defun org-compact-display-after-subtree-move ()
  3746. (let (beg end)
  3747. (save-excursion
  3748. (if (org-up-heading-safe)
  3749. (progn
  3750. (hide-subtree)
  3751. (show-entry)
  3752. (show-children)
  3753. (org-cycle-show-empty-lines 'children)
  3754. (org-cycle-hide-drawers 'children))
  3755. (org-overview)))))
  3756. (defun org-cycle-show-empty-lines (state)
  3757. "Show empty lines above all visible headlines.
  3758. The region to be covered depends on STATE when called through
  3759. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3760. entire buffer covered. Note that an empty line is only shown if there
  3761. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3762. (when (> org-cycle-separator-lines 0)
  3763. (save-excursion
  3764. (let* ((n org-cycle-separator-lines)
  3765. (re (cond
  3766. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3767. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3768. (t (let ((ns (number-to-string (- n 2))))
  3769. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3770. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3771. beg end)
  3772. (cond
  3773. ((memq state '(overview contents t))
  3774. (setq beg (point-min) end (point-max)))
  3775. ((memq state '(children folded))
  3776. (setq beg (point) end (progn (org-end-of-subtree t t)
  3777. (beginning-of-line 2)
  3778. (point)))))
  3779. (when beg
  3780. (goto-char beg)
  3781. (while (re-search-forward re end t)
  3782. (if (not (get-char-property (match-end 1) 'invisible))
  3783. (outline-flag-region
  3784. (match-beginning 1) (match-end 1) nil)))))))
  3785. ;; Never hide empty lines at the end of the file.
  3786. (save-excursion
  3787. (goto-char (point-max))
  3788. (outline-previous-heading)
  3789. (outline-end-of-heading)
  3790. (if (and (looking-at "[ \t\n]+")
  3791. (= (match-end 0) (point-max)))
  3792. (outline-flag-region (point) (match-end 0) nil))))
  3793. (defun org-show-empty-lines-in-parent ()
  3794. "Move to the parent and re-show empty lines before visible headlines."
  3795. (save-excursion
  3796. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3797. (org-cycle-show-empty-lines context))))
  3798. (defun org-cycle-hide-drawers (state)
  3799. "Re-hide all drawers after a visibility state change."
  3800. (when (and (org-mode-p)
  3801. (not (memq state '(overview folded))))
  3802. (save-excursion
  3803. (let* ((globalp (memq state '(contents all)))
  3804. (beg (if globalp (point-min) (point)))
  3805. (end (if globalp (point-max) (org-end-of-subtree t))))
  3806. (goto-char beg)
  3807. (while (re-search-forward org-drawer-regexp end t)
  3808. (org-flag-drawer t))))))
  3809. (defun org-flag-drawer (flag)
  3810. (save-excursion
  3811. (beginning-of-line 1)
  3812. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3813. (let ((b (match-end 0))
  3814. (outline-regexp org-outline-regexp))
  3815. (if (re-search-forward
  3816. "^[ \t]*:END:"
  3817. (save-excursion (outline-next-heading) (point)) t)
  3818. (outline-flag-region b (point-at-eol) flag)
  3819. (error ":END: line missing"))))))
  3820. (defun org-subtree-end-visible-p ()
  3821. "Is the end of the current subtree visible?"
  3822. (pos-visible-in-window-p
  3823. (save-excursion (org-end-of-subtree t) (point))))
  3824. (defun org-first-headline-recenter (&optional N)
  3825. "Move cursor to the first headline and recenter the headline.
  3826. Optional argument N means, put the headline into the Nth line of the window."
  3827. (goto-char (point-min))
  3828. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3829. (beginning-of-line)
  3830. (recenter (prefix-numeric-value N))))
  3831. ;;; Org-goto
  3832. (defvar org-goto-window-configuration nil)
  3833. (defvar org-goto-marker nil)
  3834. (defvar org-goto-map
  3835. (let ((map (make-sparse-keymap)))
  3836. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3837. (while (setq cmd (pop cmds))
  3838. (substitute-key-definition cmd cmd map global-map)))
  3839. (suppress-keymap map)
  3840. (org-defkey map "\C-m" 'org-goto-ret)
  3841. (org-defkey map [(return)] 'org-goto-ret)
  3842. (org-defkey map [(left)] 'org-goto-left)
  3843. (org-defkey map [(right)] 'org-goto-right)
  3844. (org-defkey map [(control ?g)] 'org-goto-quit)
  3845. (org-defkey map "\C-i" 'org-cycle)
  3846. (org-defkey map [(tab)] 'org-cycle)
  3847. (org-defkey map [(down)] 'outline-next-visible-heading)
  3848. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3849. (if org-goto-auto-isearch
  3850. (if (fboundp 'define-key-after)
  3851. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3852. nil)
  3853. (org-defkey map "q" 'org-goto-quit)
  3854. (org-defkey map "n" 'outline-next-visible-heading)
  3855. (org-defkey map "p" 'outline-previous-visible-heading)
  3856. (org-defkey map "f" 'outline-forward-same-level)
  3857. (org-defkey map "b" 'outline-backward-same-level)
  3858. (org-defkey map "u" 'outline-up-heading))
  3859. (org-defkey map "/" 'org-occur)
  3860. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3861. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3862. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3863. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3864. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3865. map))
  3866. (defconst org-goto-help
  3867. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3868. RET=jump to location [Q]uit and return to previous location
  3869. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3870. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3871. ;; FIXME: Docstring doe not mention both interfaces
  3872. (defun org-goto (&optional alternative-interface)
  3873. "Look up a different location in the current file, keeping current visibility.
  3874. When you want look-up or go to a different location in a document, the
  3875. fastest way is often to fold the entire buffer and then dive into the tree.
  3876. This method has the disadvantage, that the previous location will be folded,
  3877. which may not be what you want.
  3878. This command works around this by showing a copy of the current buffer
  3879. in an indirect buffer, in overview mode. You can dive into the tree in
  3880. that copy, use org-occur and incremental search to find a location.
  3881. When pressing RET or `Q', the command returns to the original buffer in
  3882. which the visibility is still unchanged. After RET is will also jump to
  3883. the location selected in the indirect buffer and expose the
  3884. the headline hierarchy above."
  3885. (interactive "P")
  3886. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3887. (org-refile-use-outline-path t)
  3888. (interface
  3889. (if (not alternative-interface)
  3890. org-goto-interface
  3891. (if (eq org-goto-interface 'outline)
  3892. 'outline-path-completion
  3893. 'outline)))
  3894. (org-goto-start-pos (point))
  3895. (selected-point
  3896. (if (eq interface 'outline)
  3897. (car (org-get-location (current-buffer) org-goto-help))
  3898. (nth 3 (org-refile-get-location "Goto: ")))))
  3899. (if selected-point
  3900. (progn
  3901. (org-mark-ring-push org-goto-start-pos)
  3902. (goto-char selected-point)
  3903. (if (or (org-invisible-p) (org-invisible-p2))
  3904. (org-show-context 'org-goto)))
  3905. (message "Quit"))))
  3906. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3907. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3908. (defvar org-goto-local-auto-isearch-map) ; defined below
  3909. (defun org-get-location (buf help)
  3910. "Let the user select a location in the Org-mode buffer BUF.
  3911. This function uses a recursive edit. It returns the selected position
  3912. or nil."
  3913. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3914. (isearch-hide-immediately nil)
  3915. (isearch-search-fun-function
  3916. (lambda () 'org-goto-local-search-headings))
  3917. (org-goto-selected-point org-goto-exit-command))
  3918. (save-excursion
  3919. (save-window-excursion
  3920. (delete-other-windows)
  3921. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3922. (switch-to-buffer
  3923. (condition-case nil
  3924. (make-indirect-buffer (current-buffer) "*org-goto*")
  3925. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3926. (with-output-to-temp-buffer "*Help*"
  3927. (princ help))
  3928. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3929. (setq buffer-read-only nil)
  3930. (let ((org-startup-truncated t)
  3931. (org-startup-folded nil)
  3932. (org-startup-align-all-tables nil))
  3933. (org-mode)
  3934. (org-overview))
  3935. (setq buffer-read-only t)
  3936. (if (and (boundp 'org-goto-start-pos)
  3937. (integer-or-marker-p org-goto-start-pos))
  3938. (let ((org-show-hierarchy-above t)
  3939. (org-show-siblings t)
  3940. (org-show-following-heading t))
  3941. (goto-char org-goto-start-pos)
  3942. (and (org-invisible-p) (org-show-context)))
  3943. (goto-char (point-min)))
  3944. (org-beginning-of-line)
  3945. (message "Select location and press RET")
  3946. (use-local-map org-goto-map)
  3947. (recursive-edit)
  3948. ))
  3949. (kill-buffer "*org-goto*")
  3950. (cons org-goto-selected-point org-goto-exit-command)))
  3951. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3952. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3953. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3954. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3955. (defun org-goto-local-search-headings (string bound noerror)
  3956. "Search and make sure that any matches are in headlines."
  3957. (catch 'return
  3958. (while (if isearch-forward
  3959. (search-forward string bound noerror)
  3960. (search-backward string bound noerror))
  3961. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3962. (and (member :headline context)
  3963. (not (member :tags context))))
  3964. (throw 'return (point))))))
  3965. (defun org-goto-local-auto-isearch ()
  3966. "Start isearch."
  3967. (interactive)
  3968. (goto-char (point-min))
  3969. (let ((keys (this-command-keys)))
  3970. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3971. (isearch-mode t)
  3972. (isearch-process-search-char (string-to-char keys)))))
  3973. (defun org-goto-ret (&optional arg)
  3974. "Finish `org-goto' by going to the new location."
  3975. (interactive "P")
  3976. (setq org-goto-selected-point (point)
  3977. org-goto-exit-command 'return)
  3978. (throw 'exit nil))
  3979. (defun org-goto-left ()
  3980. "Finish `org-goto' by going to the new location."
  3981. (interactive)
  3982. (if (org-on-heading-p)
  3983. (progn
  3984. (beginning-of-line 1)
  3985. (setq org-goto-selected-point (point)
  3986. org-goto-exit-command 'left)
  3987. (throw 'exit nil))
  3988. (error "Not on a heading")))
  3989. (defun org-goto-right ()
  3990. "Finish `org-goto' by going to the new location."
  3991. (interactive)
  3992. (if (org-on-heading-p)
  3993. (progn
  3994. (setq org-goto-selected-point (point)
  3995. org-goto-exit-command 'right)
  3996. (throw 'exit nil))
  3997. (error "Not on a heading")))
  3998. (defun org-goto-quit ()
  3999. "Finish `org-goto' without cursor motion."
  4000. (interactive)
  4001. (setq org-goto-selected-point nil)
  4002. (setq org-goto-exit-command 'quit)
  4003. (throw 'exit nil))
  4004. ;;; Indirect buffer display of subtrees
  4005. (defvar org-indirect-dedicated-frame nil
  4006. "This is the frame being used for indirect tree display.")
  4007. (defvar org-last-indirect-buffer nil)
  4008. (defun org-tree-to-indirect-buffer (&optional arg)
  4009. "Create indirect buffer and narrow it to current subtree.
  4010. With numerical prefix ARG, go up to this level and then take that tree.
  4011. If ARG is negative, go up that many levels.
  4012. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4013. indirect buffer previously made with this command, to avoid proliferation of
  4014. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4015. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4016. is kept so that you can work with several indirect buffers at the same time.
  4017. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4018. requests that a new frame be made for the new buffer, so that the dedicated
  4019. frame is not changed."
  4020. (interactive "P")
  4021. (let ((cbuf (current-buffer))
  4022. (cwin (selected-window))
  4023. (pos (point))
  4024. beg end level heading ibuf)
  4025. (save-excursion
  4026. (org-back-to-heading t)
  4027. (when (numberp arg)
  4028. (setq level (org-outline-level))
  4029. (if (< arg 0) (setq arg (+ level arg)))
  4030. (while (> (setq level (org-outline-level)) arg)
  4031. (outline-up-heading 1 t)))
  4032. (setq beg (point)
  4033. heading (org-get-heading))
  4034. (org-end-of-subtree t) (setq end (point)))
  4035. (if (and (buffer-live-p org-last-indirect-buffer)
  4036. (not (eq org-indirect-buffer-display 'new-frame))
  4037. (not arg))
  4038. (kill-buffer org-last-indirect-buffer))
  4039. (setq ibuf (org-get-indirect-buffer cbuf)
  4040. org-last-indirect-buffer ibuf)
  4041. (cond
  4042. ((or (eq org-indirect-buffer-display 'new-frame)
  4043. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4044. (select-frame (make-frame))
  4045. (delete-other-windows)
  4046. (switch-to-buffer ibuf)
  4047. (org-set-frame-title heading))
  4048. ((eq org-indirect-buffer-display 'dedicated-frame)
  4049. (raise-frame
  4050. (select-frame (or (and org-indirect-dedicated-frame
  4051. (frame-live-p org-indirect-dedicated-frame)
  4052. org-indirect-dedicated-frame)
  4053. (setq org-indirect-dedicated-frame (make-frame)))))
  4054. (delete-other-windows)
  4055. (switch-to-buffer ibuf)
  4056. (org-set-frame-title (concat "Indirect: " heading)))
  4057. ((eq org-indirect-buffer-display 'current-window)
  4058. (switch-to-buffer ibuf))
  4059. ((eq org-indirect-buffer-display 'other-window)
  4060. (pop-to-buffer ibuf))
  4061. (t (error "Invalid value.")))
  4062. (if (featurep 'xemacs)
  4063. (save-excursion (org-mode) (turn-on-font-lock)))
  4064. (narrow-to-region beg end)
  4065. (show-all)
  4066. (goto-char pos)
  4067. (and (window-live-p cwin) (select-window cwin))))
  4068. (defun org-get-indirect-buffer (&optional buffer)
  4069. (setq buffer (or buffer (current-buffer)))
  4070. (let ((n 1) (base (buffer-name buffer)) bname)
  4071. (while (buffer-live-p
  4072. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4073. (setq n (1+ n)))
  4074. (condition-case nil
  4075. (make-indirect-buffer buffer bname 'clone)
  4076. (error (make-indirect-buffer buffer bname)))))
  4077. (defun org-set-frame-title (title)
  4078. "Set the title of the current frame to the string TITLE."
  4079. ;; FIXME: how to name a single frame in XEmacs???
  4080. (unless (featurep 'xemacs)
  4081. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4082. ;;;; Structure editing
  4083. ;;; Inserting headlines
  4084. (defun org-insert-heading (&optional force-heading)
  4085. "Insert a new heading or item with same depth at point.
  4086. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4087. If point is at the beginning of a headline, insert a sibling before the
  4088. current headline. If point is not at the beginning, do not split the line,
  4089. but create the new hedline after the current line."
  4090. (interactive "P")
  4091. (if (= (buffer-size) 0)
  4092. (insert "\n* ")
  4093. (when (or force-heading (not (org-insert-item)))
  4094. (let* ((head (save-excursion
  4095. (condition-case nil
  4096. (progn
  4097. (org-back-to-heading)
  4098. (match-string 0))
  4099. (error "*"))))
  4100. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4101. pos)
  4102. (cond
  4103. ((and (org-on-heading-p) (bolp)
  4104. (or (bobp)
  4105. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4106. ;; insert before the current line
  4107. (open-line (if blank 2 1)))
  4108. ((and (bolp)
  4109. (or (bobp)
  4110. (save-excursion
  4111. (backward-char 1) (not (org-invisible-p)))))
  4112. ;; insert right here
  4113. nil)
  4114. (t
  4115. ;; in the middle of the line
  4116. (org-show-entry)
  4117. (let ((split
  4118. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4119. tags pos)
  4120. (if (org-on-heading-p)
  4121. (progn
  4122. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4123. (setq tags (and (match-end 2) (match-string 2)))
  4124. (and (match-end 1)
  4125. (delete-region (match-beginning 1) (match-end 1)))
  4126. (setq pos (point-at-bol))
  4127. (or split (end-of-line 1))
  4128. (delete-horizontal-space)
  4129. (newline (if blank 2 1))
  4130. (when tags
  4131. (save-excursion
  4132. (goto-char pos)
  4133. (end-of-line 1)
  4134. (insert " " tags)
  4135. (org-set-tags nil 'align))))
  4136. (or split (end-of-line 1))
  4137. (newline (if blank 2 1))))))
  4138. (insert head) (just-one-space)
  4139. (setq pos (point))
  4140. (end-of-line 1)
  4141. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4142. (run-hooks 'org-insert-heading-hook)))))
  4143. (defun org-get-heading (&optional no-tags)
  4144. "Return the heading of the current entry, without the stars."
  4145. (save-excursion
  4146. (org-back-to-heading t)
  4147. (if (looking-at
  4148. (if no-tags
  4149. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4150. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4151. (match-string 1) "")))
  4152. (defun org-insert-heading-after-current ()
  4153. "Insert a new heading with same level as current, after current subtree."
  4154. (interactive)
  4155. (org-back-to-heading)
  4156. (org-insert-heading)
  4157. (org-move-subtree-down)
  4158. (end-of-line 1))
  4159. (defun org-insert-todo-heading (arg)
  4160. "Insert a new heading with the same level and TODO state as current heading.
  4161. If the heading has no TODO state, or if the state is DONE, use the first
  4162. state (TODO by default). Also with prefix arg, force first state."
  4163. (interactive "P")
  4164. (when (not (org-insert-item 'checkbox))
  4165. (org-insert-heading)
  4166. (save-excursion
  4167. (org-back-to-heading)
  4168. (outline-previous-heading)
  4169. (looking-at org-todo-line-regexp))
  4170. (if (or arg
  4171. (not (match-beginning 2))
  4172. (member (match-string 2) org-done-keywords))
  4173. (insert (car org-todo-keywords-1) " ")
  4174. (insert (match-string 2) " "))
  4175. (when org-provide-todo-statistics
  4176. (org-update-parent-todo-statistics))))
  4177. (defun org-insert-subheading (arg)
  4178. "Insert a new subheading and demote it.
  4179. Works for outline headings and for plain lists alike."
  4180. (interactive "P")
  4181. (org-insert-heading arg)
  4182. (cond
  4183. ((org-on-heading-p) (org-do-demote))
  4184. ((org-at-item-p) (org-indent-item 1))))
  4185. (defun org-insert-todo-subheading (arg)
  4186. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4187. Works for outline headings and for plain lists alike."
  4188. (interactive "P")
  4189. (org-insert-todo-heading arg)
  4190. (cond
  4191. ((org-on-heading-p) (org-do-demote))
  4192. ((org-at-item-p) (org-indent-item 1))))
  4193. ;;; Promotion and Demotion
  4194. (defun org-promote-subtree ()
  4195. "Promote the entire subtree.
  4196. See also `org-promote'."
  4197. (interactive)
  4198. (save-excursion
  4199. (org-map-tree 'org-promote))
  4200. (org-fix-position-after-promote))
  4201. (defun org-demote-subtree ()
  4202. "Demote the entire subtree. See `org-demote'.
  4203. See also `org-promote'."
  4204. (interactive)
  4205. (save-excursion
  4206. (org-map-tree 'org-demote))
  4207. (org-fix-position-after-promote))
  4208. (defun org-do-promote ()
  4209. "Promote the current heading higher up the tree.
  4210. If the region is active in `transient-mark-mode', promote all headings
  4211. in the region."
  4212. (interactive)
  4213. (save-excursion
  4214. (if (org-region-active-p)
  4215. (org-map-region 'org-promote (region-beginning) (region-end))
  4216. (org-promote)))
  4217. (org-fix-position-after-promote))
  4218. (defun org-do-demote ()
  4219. "Demote the current heading lower down the tree.
  4220. If the region is active in `transient-mark-mode', demote all headings
  4221. in the region."
  4222. (interactive)
  4223. (save-excursion
  4224. (if (org-region-active-p)
  4225. (org-map-region 'org-demote (region-beginning) (region-end))
  4226. (org-demote)))
  4227. (org-fix-position-after-promote))
  4228. (defun org-fix-position-after-promote ()
  4229. "Make sure that after pro/demotion cursor position is right."
  4230. (let ((pos (point)))
  4231. (when (save-excursion
  4232. (beginning-of-line 1)
  4233. (looking-at org-todo-line-regexp)
  4234. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4235. (cond ((eobp) (insert " "))
  4236. ((eolp) (insert " "))
  4237. ((equal (char-after) ?\ ) (forward-char 1))))))
  4238. (defun org-reduced-level (l)
  4239. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4240. (defun org-get-valid-level (level &optional change)
  4241. "Rectify a level change under the influence of `org-odd-levels-only'
  4242. LEVEL is a current level, CHANGE is by how much the level should be
  4243. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4244. even level numbers will become the next higher odd number."
  4245. (if org-odd-levels-only
  4246. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4247. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4248. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4249. (max 1 (+ level change))))
  4250. (if (boundp 'define-obsolete-function-alias)
  4251. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4252. (define-obsolete-function-alias 'org-get-legal-level
  4253. 'org-get-valid-level)
  4254. (define-obsolete-function-alias 'org-get-legal-level
  4255. 'org-get-valid-level "23.1")))
  4256. (defun org-promote ()
  4257. "Promote the current heading higher up the tree.
  4258. If the region is active in `transient-mark-mode', promote all headings
  4259. in the region."
  4260. (org-back-to-heading t)
  4261. (let* ((level (save-match-data (funcall outline-level)))
  4262. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4263. (diff (abs (- level (length up-head) -1))))
  4264. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4265. (replace-match up-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-demote ()
  4270. "Demote the current heading lower down the tree.
  4271. If the region is active in `transient-mark-mode', demote all headings
  4272. in the region."
  4273. (org-back-to-heading t)
  4274. (let* ((level (save-match-data (funcall outline-level)))
  4275. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4276. (diff (abs (- level (length down-head) -1))))
  4277. (replace-match down-head nil t)
  4278. ;; Fixup tag positioning
  4279. (and org-auto-align-tags (org-set-tags nil t))
  4280. (if org-adapt-indentation (org-fixup-indentation diff))))
  4281. (defun org-map-tree (fun)
  4282. "Call FUN for every heading underneath the current one."
  4283. (org-back-to-heading)
  4284. (let ((level (funcall outline-level)))
  4285. (save-excursion
  4286. (funcall fun)
  4287. (while (and (progn
  4288. (outline-next-heading)
  4289. (> (funcall outline-level) level))
  4290. (not (eobp)))
  4291. (funcall fun)))))
  4292. (defun org-map-region (fun beg end)
  4293. "Call FUN for every heading between BEG and END."
  4294. (let ((org-ignore-region t))
  4295. (save-excursion
  4296. (setq end (copy-marker end))
  4297. (goto-char beg)
  4298. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4299. (< (point) end))
  4300. (funcall fun))
  4301. (while (and (progn
  4302. (outline-next-heading)
  4303. (< (point) end))
  4304. (not (eobp)))
  4305. (funcall fun)))))
  4306. (defun org-fixup-indentation (diff)
  4307. "Change the indentation in the current entry by DIFF
  4308. However, if any line in the current entry has no indentation, or if it
  4309. would end up with no indentation after the change, nothing at all is done."
  4310. (save-excursion
  4311. (let ((end (save-excursion (outline-next-heading)
  4312. (point-marker)))
  4313. (prohibit (if (> diff 0)
  4314. "^\\S-"
  4315. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4316. col)
  4317. (unless (save-excursion (end-of-line 1)
  4318. (re-search-forward prohibit end t))
  4319. (while (and (< (point) end)
  4320. (re-search-forward "^[ \t]+" end t))
  4321. (goto-char (match-end 0))
  4322. (setq col (current-column))
  4323. (if (< diff 0) (replace-match ""))
  4324. (indent-to (+ diff col))))
  4325. (move-marker end nil))))
  4326. (defun org-convert-to-odd-levels ()
  4327. "Convert an org-mode file with all levels allowed to one with odd levels.
  4328. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4329. level 5 etc."
  4330. (interactive)
  4331. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4332. (let ((org-odd-levels-only nil) n)
  4333. (save-excursion
  4334. (goto-char (point-min))
  4335. (while (re-search-forward "^\\*\\*+ " nil t)
  4336. (setq n (- (length (match-string 0)) 2))
  4337. (while (>= (setq n (1- n)) 0)
  4338. (org-demote))
  4339. (end-of-line 1))))))
  4340. (defun org-convert-to-oddeven-levels ()
  4341. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4342. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4343. section with an even level, conversion would destroy the structure of the file. An error
  4344. is signaled in this case."
  4345. (interactive)
  4346. (goto-char (point-min))
  4347. ;; First check if there are no even levels
  4348. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4349. (org-show-context t)
  4350. (error "Not all levels are odd in this file. Conversion not possible."))
  4351. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4352. (let ((org-odd-levels-only nil) n)
  4353. (save-excursion
  4354. (goto-char (point-min))
  4355. (while (re-search-forward "^\\*\\*+ " nil t)
  4356. (setq n (/ (1- (length (match-string 0))) 2))
  4357. (while (>= (setq n (1- n)) 0)
  4358. (org-promote))
  4359. (end-of-line 1))))))
  4360. (defun org-tr-level (n)
  4361. "Make N odd if required."
  4362. (if org-odd-levels-only (1+ (/ n 2)) n))
  4363. ;;; Vertical tree motion, cutting and pasting of subtrees
  4364. (defun org-move-subtree-up (&optional arg)
  4365. "Move the current subtree up past ARG headlines of the same level."
  4366. (interactive "p")
  4367. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4368. (defun org-move-subtree-down (&optional arg)
  4369. "Move the current subtree down past ARG headlines of the same level."
  4370. (interactive "p")
  4371. (setq arg (prefix-numeric-value arg))
  4372. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4373. 'outline-get-last-sibling))
  4374. (ins-point (make-marker))
  4375. (cnt (abs arg))
  4376. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4377. ;; Select the tree
  4378. (org-back-to-heading)
  4379. (setq beg0 (point))
  4380. (save-excursion
  4381. (setq ne-beg (org-back-over-empty-lines))
  4382. (setq beg (point)))
  4383. (save-match-data
  4384. (save-excursion (outline-end-of-heading)
  4385. (setq folded (org-invisible-p)))
  4386. (outline-end-of-subtree))
  4387. (outline-next-heading)
  4388. (setq ne-end (org-back-over-empty-lines))
  4389. (setq end (point))
  4390. (goto-char beg0)
  4391. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4392. ;; include less whitespace
  4393. (save-excursion
  4394. (goto-char beg)
  4395. (forward-line (- ne-beg ne-end))
  4396. (setq beg (point))))
  4397. ;; Find insertion point, with error handling
  4398. (while (> cnt 0)
  4399. (or (and (funcall movfunc) (looking-at outline-regexp))
  4400. (progn (goto-char beg0)
  4401. (error "Cannot move past superior level or buffer limit")))
  4402. (setq cnt (1- cnt)))
  4403. (if (> arg 0)
  4404. ;; Moving forward - still need to move over subtree
  4405. (progn (org-end-of-subtree t t)
  4406. (save-excursion
  4407. (org-back-over-empty-lines)
  4408. (or (bolp) (newline)))))
  4409. (setq ne-ins (org-back-over-empty-lines))
  4410. (move-marker ins-point (point))
  4411. (setq txt (buffer-substring beg end))
  4412. (org-save-markers-in-region beg end)
  4413. (delete-region beg end)
  4414. (outline-flag-region (1- beg) beg nil)
  4415. (outline-flag-region (1- (point)) (point) nil)
  4416. (let ((bbb (point)))
  4417. (insert-before-markers txt)
  4418. (org-reinstall-markers-in-region bbb)
  4419. (move-marker ins-point bbb))
  4420. (or (bolp) (insert "\n"))
  4421. (setq ins-end (point))
  4422. (goto-char ins-point)
  4423. (org-skip-whitespace)
  4424. (when (and (< arg 0)
  4425. (org-first-sibling-p)
  4426. (> ne-ins ne-beg))
  4427. ;; Move whitespace back to beginning
  4428. (save-excursion
  4429. (goto-char ins-end)
  4430. (let ((kill-whole-line t))
  4431. (kill-line (- ne-ins ne-beg)) (point)))
  4432. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4433. (move-marker ins-point nil)
  4434. (org-compact-display-after-subtree-move)
  4435. (org-show-empty-lines-in-parent)
  4436. (unless folded
  4437. (org-show-entry)
  4438. (show-children)
  4439. (org-cycle-hide-drawers 'children))))
  4440. (defvar org-subtree-clip ""
  4441. "Clipboard for cut and paste of subtrees.
  4442. This is actually only a copy of the kill, because we use the normal kill
  4443. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4444. (defvar org-subtree-clip-folded nil
  4445. "Was the last copied subtree folded?
  4446. This is used to fold the tree back after pasting.")
  4447. (defun org-cut-subtree (&optional n)
  4448. "Cut the current subtree into the clipboard.
  4449. With prefix arg N, cut this many sequential subtrees.
  4450. This is a short-hand for marking the subtree and then cutting it."
  4451. (interactive "p")
  4452. (org-copy-subtree n 'cut))
  4453. (defun org-copy-subtree (&optional n cut force-store-markers)
  4454. "Cut the current subtree into the clipboard.
  4455. With prefix arg N, cut this many sequential subtrees.
  4456. This is a short-hand for marking the subtree and then copying it.
  4457. If CUT is non-nil, actually cut the subtree.
  4458. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4459. of some markers in the region, even if CUT is non-nil. This is
  4460. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4461. (interactive "p")
  4462. (let (beg end folded (beg0 (point)))
  4463. (if (interactive-p)
  4464. (org-back-to-heading nil) ; take what looks like a subtree
  4465. (org-back-to-heading t)) ; take what is really there
  4466. (org-back-over-empty-lines)
  4467. (setq beg (point))
  4468. (skip-chars-forward " \t\r\n")
  4469. (save-match-data
  4470. (save-excursion (outline-end-of-heading)
  4471. (setq folded (org-invisible-p)))
  4472. (condition-case nil
  4473. (outline-forward-same-level (1- n))
  4474. (error nil))
  4475. (org-end-of-subtree t t))
  4476. (org-back-over-empty-lines)
  4477. (setq end (point))
  4478. (goto-char beg0)
  4479. (when (> end beg)
  4480. (setq org-subtree-clip-folded folded)
  4481. (when (or cut force-store-markers)
  4482. (org-save-markers-in-region beg end))
  4483. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4484. (setq org-subtree-clip (current-kill 0))
  4485. (message "%s: Subtree(s) with %d characters"
  4486. (if cut "Cut" "Copied")
  4487. (length org-subtree-clip)))))
  4488. (defun org-paste-subtree (&optional level tree)
  4489. "Paste the clipboard as a subtree, with modification of headline level.
  4490. The entire subtree is promoted or demoted in order to match a new headline
  4491. level. By default, the new level is derived from the visible headings
  4492. before and after the insertion point, and taken to be the inferior headline
  4493. level of the two. So if the previous visible heading is level 3 and the
  4494. next is level 4 (or vice versa), level 4 will be used for insertion.
  4495. This makes sure that the subtree remains an independent subtree and does
  4496. not swallow low level entries.
  4497. You can also force a different level, either by using a numeric prefix
  4498. argument, or by inserting the heading marker by hand. For example, if the
  4499. cursor is after \"*****\", then the tree will be shifted to level 5.
  4500. If you want to insert the tree as is, just use \\[yank].
  4501. If optional TREE is given, use this text instead of the kill ring."
  4502. (interactive "P")
  4503. (unless (org-kill-is-subtree-p tree)
  4504. (error "%s"
  4505. (substitute-command-keys
  4506. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4507. (let* ((visp (not (org-invisible-p)))
  4508. (txt (or tree (and kill-ring (current-kill 0))))
  4509. (^re (concat "^\\(" outline-regexp "\\)"))
  4510. (re (concat "\\(" outline-regexp "\\)"))
  4511. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4512. (old-level (if (string-match ^re txt)
  4513. (- (match-end 0) (match-beginning 0) 1)
  4514. -1))
  4515. (force-level (cond (level (prefix-numeric-value level))
  4516. ((string-match
  4517. ^re_ (buffer-substring (point-at-bol) (point)))
  4518. (- (match-end 1) (match-beginning 1)))
  4519. (t nil)))
  4520. (previous-level (save-excursion
  4521. (condition-case nil
  4522. (progn
  4523. (outline-previous-visible-heading 1)
  4524. (if (looking-at re)
  4525. (- (match-end 0) (match-beginning 0) 1)
  4526. 1))
  4527. (error 1))))
  4528. (next-level (save-excursion
  4529. (condition-case nil
  4530. (progn
  4531. (or (looking-at outline-regexp)
  4532. (outline-next-visible-heading 1))
  4533. (if (looking-at re)
  4534. (- (match-end 0) (match-beginning 0) 1)
  4535. 1))
  4536. (error 1))))
  4537. (new-level (or force-level (max previous-level next-level)))
  4538. (shift (if (or (= old-level -1)
  4539. (= new-level -1)
  4540. (= old-level new-level))
  4541. 0
  4542. (- new-level old-level)))
  4543. (delta (if (> shift 0) -1 1))
  4544. (func (if (> shift 0) 'org-demote 'org-promote))
  4545. (org-odd-levels-only nil)
  4546. beg end)
  4547. ;; Remove the forced level indicator
  4548. (if force-level
  4549. (delete-region (point-at-bol) (point)))
  4550. ;; Paste
  4551. (beginning-of-line 1)
  4552. (org-back-over-empty-lines)
  4553. (setq beg (point))
  4554. (insert-before-markers txt)
  4555. (unless (string-match "\n\\'" txt) (insert "\n"))
  4556. (org-reinstall-markers-in-region beg)
  4557. (setq end (point))
  4558. (goto-char beg)
  4559. (skip-chars-forward " \t\n\r")
  4560. (setq beg (point))
  4561. (if (and (org-invisible-p) visp)
  4562. (save-excursion (outline-show-heading)))
  4563. ;; Shift if necessary
  4564. (unless (= shift 0)
  4565. (save-restriction
  4566. (narrow-to-region beg end)
  4567. (while (not (= shift 0))
  4568. (org-map-region func (point-min) (point-max))
  4569. (setq shift (+ delta shift)))
  4570. (goto-char (point-min))))
  4571. (when (interactive-p)
  4572. (message "Clipboard pasted as level %d subtree" new-level))
  4573. (if (and kill-ring
  4574. (eq org-subtree-clip (current-kill 0))
  4575. org-subtree-clip-folded)
  4576. ;; The tree was folded before it was killed/copied
  4577. (hide-subtree))))
  4578. (defun org-kill-is-subtree-p (&optional txt)
  4579. "Check if the current kill is an outline subtree, or a set of trees.
  4580. Returns nil if kill does not start with a headline, or if the first
  4581. headline level is not the largest headline level in the tree.
  4582. So this will actually accept several entries of equal levels as well,
  4583. which is OK for `org-paste-subtree'.
  4584. If optional TXT is given, check this string instead of the current kill."
  4585. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4586. (start-level (and kill
  4587. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4588. org-outline-regexp "\\)")
  4589. kill)
  4590. (- (match-end 2) (match-beginning 2) 1)))
  4591. (re (concat "^" org-outline-regexp))
  4592. (start (1+ (match-beginning 2))))
  4593. (if (not start-level)
  4594. (progn
  4595. nil) ;; does not even start with a heading
  4596. (catch 'exit
  4597. (while (setq start (string-match re kill (1+ start)))
  4598. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4599. (throw 'exit nil)))
  4600. t))))
  4601. (defvar org-markers-to-move nil
  4602. "Markers that should be moved with a cut-and-paste operation.
  4603. Those markers are stored together with their positions relative to
  4604. the start of the region.")
  4605. (defun org-save-markers-in-region (beg end)
  4606. "Check markers in region.
  4607. If these markers are between BEG and END, record their position relative
  4608. to BEG, so that after moving the block of text, we can put the markers back
  4609. into place.
  4610. This function gets called just before an entry or tree gets cut from the
  4611. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4612. called immediately, to move the markers with the entries."
  4613. (setq org-markers-to-move nil)
  4614. (when (featurep 'org-clock)
  4615. (org-clock-save-markers-for-cut-and-paste beg end))
  4616. (when (featurep 'org-agenda)
  4617. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4618. (defun org-check-and-save-marker (marker beg end)
  4619. "Check if MARKER is between BEG and END.
  4620. If yes, remember the marker and the distance to BEG."
  4621. (when (and (marker-buffer marker)
  4622. (equal (marker-buffer marker) (current-buffer)))
  4623. (if (and (>= marker beg) (< marker end))
  4624. (push (cons marker (- marker beg)) org-markers-to-move))))
  4625. (defun org-reinstall-markers-in-region (beg)
  4626. "Move all remembered markers to their position relative to BEG."
  4627. (mapc (lambda (x)
  4628. (move-marker (car x) (+ beg (cdr x))))
  4629. org-markers-to-move)
  4630. (setq org-markers-to-move nil))
  4631. (defun org-narrow-to-subtree ()
  4632. "Narrow buffer to the current subtree."
  4633. (interactive)
  4634. (save-excursion
  4635. (save-match-data
  4636. (narrow-to-region
  4637. (progn (org-back-to-heading) (point))
  4638. (progn (org-end-of-subtree t) (point))))))
  4639. ;;; Outline Sorting
  4640. (defun org-sort (with-case)
  4641. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4642. Optional argument WITH-CASE means sort case-sensitively."
  4643. (interactive "P")
  4644. (if (org-at-table-p)
  4645. (org-call-with-arg 'org-table-sort-lines with-case)
  4646. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4647. (defun org-sort-remove-invisible (s)
  4648. (remove-text-properties 0 (length s) org-rm-props s)
  4649. (while (string-match org-bracket-link-regexp s)
  4650. (setq s (replace-match (if (match-end 2)
  4651. (match-string 3 s)
  4652. (match-string 1 s)) t t s)))
  4653. s)
  4654. (defvar org-priority-regexp) ; defined later in the file
  4655. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4656. "Sort entries on a certain level of an outline tree.
  4657. If there is an active region, the entries in the region are sorted.
  4658. Else, if the cursor is before the first entry, sort the top-level items.
  4659. Else, the children of the entry at point are sorted.
  4660. Sorting can be alphabetically, numerically, and by date/time as given by
  4661. the first time stamp in the entry. The command prompts for the sorting
  4662. type unless it has been given to the function through the SORTING-TYPE
  4663. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4664. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4665. called with point at the beginning of the record. It must return either
  4666. a string or a number that should serve as the sorting key for that record.
  4667. Comparing entries ignores case by default. However, with an optional argument
  4668. WITH-CASE, the sorting considers case as well."
  4669. (interactive "P")
  4670. (let ((case-func (if with-case 'identity 'downcase))
  4671. start beg end stars re re2
  4672. txt what tmp plain-list-p)
  4673. ;; Find beginning and end of region to sort
  4674. (cond
  4675. ((org-region-active-p)
  4676. ;; we will sort the region
  4677. (setq end (region-end)
  4678. what "region")
  4679. (goto-char (region-beginning))
  4680. (if (not (org-on-heading-p)) (outline-next-heading))
  4681. (setq start (point)))
  4682. ((org-at-item-p)
  4683. ;; we will sort this plain list
  4684. (org-beginning-of-item-list) (setq start (point))
  4685. (org-end-of-item-list) (setq end (point))
  4686. (goto-char start)
  4687. (setq plain-list-p t
  4688. what "plain list"))
  4689. ((or (org-on-heading-p)
  4690. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4691. ;; we will sort the children of the current headline
  4692. (org-back-to-heading)
  4693. (setq start (point)
  4694. end (progn (org-end-of-subtree t t)
  4695. (org-back-over-empty-lines)
  4696. (point))
  4697. what "children")
  4698. (goto-char start)
  4699. (show-subtree)
  4700. (outline-next-heading))
  4701. (t
  4702. ;; we will sort the top-level entries in this file
  4703. (goto-char (point-min))
  4704. (or (org-on-heading-p) (outline-next-heading))
  4705. (setq start (point) end (point-max) what "top-level")
  4706. (goto-char start)
  4707. (show-all)))
  4708. (setq beg (point))
  4709. (if (>= beg end) (error "Nothing to sort"))
  4710. (unless plain-list-p
  4711. (looking-at "\\(\\*+\\)")
  4712. (setq stars (match-string 1)
  4713. re (concat "^" (regexp-quote stars) " +")
  4714. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4715. txt (buffer-substring beg end))
  4716. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4717. (if (and (not (equal stars "*")) (string-match re2 txt))
  4718. (error "Region to sort contains a level above the first entry")))
  4719. (unless sorting-type
  4720. (message
  4721. (if plain-list-p
  4722. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4723. "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:")
  4724. what)
  4725. (setq sorting-type (read-char-exclusive))
  4726. (and (= (downcase sorting-type) ?f)
  4727. (setq getkey-func
  4728. (completing-read "Sort using function: "
  4729. obarray 'fboundp t nil nil))
  4730. (setq getkey-func (intern getkey-func)))
  4731. (and (= (downcase sorting-type) ?r)
  4732. (setq property
  4733. (completing-read "Property: "
  4734. (mapcar 'list (org-buffer-property-keys t))
  4735. nil t))))
  4736. (message "Sorting entries...")
  4737. (save-restriction
  4738. (narrow-to-region start end)
  4739. (let ((dcst (downcase sorting-type))
  4740. (now (current-time)))
  4741. (sort-subr
  4742. (/= dcst sorting-type)
  4743. ;; This function moves to the beginning character of the "record" to
  4744. ;; be sorted.
  4745. (if plain-list-p
  4746. (lambda nil
  4747. (if (org-at-item-p) t (goto-char (point-max))))
  4748. (lambda nil
  4749. (if (re-search-forward re nil t)
  4750. (goto-char (match-beginning 0))
  4751. (goto-char (point-max)))))
  4752. ;; This function moves to the last character of the "record" being
  4753. ;; sorted.
  4754. (if plain-list-p
  4755. 'org-end-of-item
  4756. (lambda nil
  4757. (save-match-data
  4758. (condition-case nil
  4759. (outline-forward-same-level 1)
  4760. (error
  4761. (goto-char (point-max)))))))
  4762. ;; This function returns the value that gets sorted against.
  4763. (if plain-list-p
  4764. (lambda nil
  4765. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4766. (cond
  4767. ((= dcst ?n)
  4768. (string-to-number (buffer-substring (match-end 0)
  4769. (point-at-eol))))
  4770. ((= dcst ?a)
  4771. (buffer-substring (match-end 0) (point-at-eol)))
  4772. ((= dcst ?t)
  4773. (if (re-search-forward org-ts-regexp
  4774. (point-at-eol) t)
  4775. (org-time-string-to-time (match-string 0))
  4776. now))
  4777. ((= dcst ?f)
  4778. (if getkey-func
  4779. (progn
  4780. (setq tmp (funcall getkey-func))
  4781. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4782. tmp)
  4783. (error "Invalid key function `%s'" getkey-func)))
  4784. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4785. (lambda nil
  4786. (cond
  4787. ((= dcst ?n)
  4788. (if (looking-at org-complex-heading-regexp)
  4789. (string-to-number (match-string 4))
  4790. nil))
  4791. ((= dcst ?a)
  4792. (if (looking-at org-complex-heading-regexp)
  4793. (funcall case-func (match-string 4))
  4794. nil))
  4795. ((= dcst ?t)
  4796. (if (re-search-forward org-ts-regexp
  4797. (save-excursion
  4798. (forward-line 2)
  4799. (point)) t)
  4800. (org-time-string-to-time (match-string 0))
  4801. now))
  4802. ((= dcst ?p)
  4803. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4804. (string-to-char (match-string 2))
  4805. org-default-priority))
  4806. ((= dcst ?r)
  4807. (or (org-entry-get nil property) ""))
  4808. ((= dcst ?o)
  4809. (if (looking-at org-complex-heading-regexp)
  4810. (- 9999 (length (member (match-string 2)
  4811. org-todo-keywords-1)))))
  4812. ((= dcst ?f)
  4813. (if getkey-func
  4814. (progn
  4815. (setq tmp (funcall getkey-func))
  4816. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4817. tmp)
  4818. (error "Invalid key function `%s'" getkey-func)))
  4819. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4820. nil
  4821. (cond
  4822. ((= dcst ?a) 'string<)
  4823. ((= dcst ?t) 'time-less-p)
  4824. (t nil)))))
  4825. (message "Sorting entries...done")))
  4826. (defun org-do-sort (table what &optional with-case sorting-type)
  4827. "Sort TABLE of WHAT according to SORTING-TYPE.
  4828. The user will be prompted for the SORTING-TYPE if the call to this
  4829. function does not specify it. WHAT is only for the prompt, to indicate
  4830. what is being sorted. The sorting key will be extracted from
  4831. the car of the elements of the table.
  4832. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4833. (unless sorting-type
  4834. (message
  4835. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4836. what)
  4837. (setq sorting-type (read-char-exclusive)))
  4838. (let ((dcst (downcase sorting-type))
  4839. extractfun comparefun)
  4840. ;; Define the appropriate functions
  4841. (cond
  4842. ((= dcst ?n)
  4843. (setq extractfun 'string-to-number
  4844. comparefun (if (= dcst sorting-type) '< '>)))
  4845. ((= dcst ?a)
  4846. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4847. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4848. comparefun (if (= dcst sorting-type)
  4849. 'string<
  4850. (lambda (a b) (and (not (string< a b))
  4851. (not (string= a b)))))))
  4852. ((= dcst ?t)
  4853. (setq extractfun
  4854. (lambda (x)
  4855. (if (string-match org-ts-regexp x)
  4856. (time-to-seconds
  4857. (org-time-string-to-time (match-string 0 x)))
  4858. 0))
  4859. comparefun (if (= dcst sorting-type) '< '>)))
  4860. (t (error "Invalid sorting type `%c'" sorting-type)))
  4861. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4862. table)
  4863. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4864. ;;; Editing source examples
  4865. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4866. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4867. (defvar org-edit-src-force-single-line nil)
  4868. (defvar org-edit-src-from-org-mode nil)
  4869. (defvar org-edit-src-picture nil)
  4870. (define-minor-mode org-exit-edit-mode
  4871. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4872. (defun org-edit-src-code ()
  4873. "Edit the source code example at point.
  4874. An indirect buffer is created, and that buffer is then narrowed to the
  4875. example at point and switched to the correct language mode. When done,
  4876. exit by killing the buffer with \\[org-edit-src-exit]."
  4877. (interactive)
  4878. (let ((line (org-current-line))
  4879. (case-fold-search t)
  4880. (msg (substitute-command-keys
  4881. "Edit, then exit with C-c ' (C-c and single quote)"))
  4882. (info (org-edit-src-find-region-and-lang))
  4883. (org-mode-p (eq major-mode 'org-mode))
  4884. beg end lang lang-f single)
  4885. (if (not info)
  4886. nil
  4887. (setq beg (nth 0 info)
  4888. end (nth 1 info)
  4889. lang (nth 2 info)
  4890. single (nth 3 info)
  4891. lang-f (intern (concat lang "-mode")))
  4892. (unless (functionp lang-f)
  4893. (error "No such language mode: %s" lang-f))
  4894. (goto-line line)
  4895. (if (get-buffer "*Org Edit Src Example*")
  4896. (kill-buffer "*Org Edit Src Example*"))
  4897. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4898. "*Org Edit Src Example*"))
  4899. (narrow-to-region beg end)
  4900. (remove-text-properties beg end '(display nil invisible nil
  4901. intangible nil))
  4902. (let ((org-inhibit-startup t))
  4903. (funcall lang-f))
  4904. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4905. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4906. (when org-mode-p
  4907. (goto-char (point-min))
  4908. (while (re-search-forward "^," nil t)
  4909. (replace-match "")))
  4910. (goto-line line)
  4911. (org-exit-edit-mode)
  4912. (org-set-local 'header-line-format msg)
  4913. (message "%s" msg)
  4914. t)))
  4915. (defun org-edit-fixed-width-region ()
  4916. "Edit the fixed-width ascii drawing at point.
  4917. This must be a region where each line starts with ca colon followed by
  4918. a space character.
  4919. An indirect buffer is created, and that buffer is then narrowed to the
  4920. example at point and switched to artist-mode. When done,
  4921. exit by killing the buffer with \\[org-edit-src-exit]."
  4922. (interactive)
  4923. (let ((line (org-current-line))
  4924. (case-fold-search t)
  4925. (msg (substitute-command-keys
  4926. "Edit, then exit with C-c ' (C-c and single quote)"))
  4927. (org-mode-p (eq major-mode 'org-mode))
  4928. beg end lang lang-f)
  4929. (beginning-of-line 1)
  4930. (if (looking-at "[ \t]*[^:\n \t]")
  4931. nil
  4932. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4933. (setq beg (point) end (match-end 0))
  4934. (save-excursion
  4935. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4936. (setq beg (point-at-bol 2))
  4937. (setq beg (point))))
  4938. (save-excursion
  4939. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4940. (setq end (match-beginning 0))
  4941. (setq end (point))))
  4942. (goto-line line)
  4943. (if (get-buffer "*Org Edit Picture*")
  4944. (kill-buffer "*Org Edit Picture*"))
  4945. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4946. "*Org Edit Picture*"))
  4947. (narrow-to-region beg end)
  4948. (remove-text-properties beg end '(display nil invisible nil
  4949. intangible nil))
  4950. (when (fboundp 'font-lock-unfontify-region)
  4951. (font-lock-unfontify-region (point-min) (point-max)))
  4952. (cond
  4953. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4954. (fundamental-mode)
  4955. (artist-mode 1))
  4956. (t (funcall org-edit-fixed-width-region-mode)))
  4957. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4958. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4959. (set (make-local-variable 'org-edit-src-picture) t)
  4960. (goto-char (point-min))
  4961. (while (re-search-forward "^[ \t]*: " nil t)
  4962. (replace-match ""))
  4963. (goto-line line)
  4964. (org-exit-edit-mode)
  4965. (org-set-local 'header-line-format msg)
  4966. (message "%s" msg)
  4967. t))))
  4968. (defun org-edit-src-find-region-and-lang ()
  4969. "Find the region and language for a local edit.
  4970. Return a list with beginning and end of the region, a string representing
  4971. the language, a switch telling of the content should be in a single line."
  4972. (let ((re-list
  4973. '(
  4974. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4975. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4976. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4977. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4978. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4979. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4980. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4981. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4982. ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
  4983. ("^#\\+html:" "\n" "html" single-line)
  4984. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4985. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4986. ("^#\\+latex:" "\n" "latex" single-line)
  4987. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4988. ("^#\\+ascii:" "\n" "ascii" single-line)
  4989. ))
  4990. (pos (point))
  4991. re re1 re2 single beg end lang)
  4992. (catch 'exit
  4993. (while (setq entry (pop re-list))
  4994. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  4995. single (nth 3 entry))
  4996. (save-excursion
  4997. (if (or (looking-at re1)
  4998. (re-search-backward re1 nil t))
  4999. (progn
  5000. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5001. (if (and (re-search-forward re2 nil t)
  5002. (>= (match-end 0) pos))
  5003. (throw 'exit (list beg (match-beginning 0) lang single))))
  5004. (if (or (looking-at re2)
  5005. (re-search-forward re2 nil t))
  5006. (progn
  5007. (setq end (match-beginning 0))
  5008. (if (and (re-search-backward re1 nil t)
  5009. (<= (match-beginning 0) pos))
  5010. (throw 'exit
  5011. (list (match-end 0) end
  5012. (org-edit-src-get-lang lang) single)))))))))))
  5013. (defun org-edit-src-get-lang (lang)
  5014. "Extract the src language."
  5015. (let ((m (match-string 0)))
  5016. (cond
  5017. ((stringp lang) lang)
  5018. ((integerp lang) (match-string lang))
  5019. ((and (eq lang lang)
  5020. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5021. (match-string 1 m))
  5022. ((and (eq lang lang)
  5023. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5024. (match-string 1 m))
  5025. (t "fundamental"))))
  5026. (defun org-edit-src-exit ()
  5027. "Exit special edit and protect problematic lines."
  5028. (interactive)
  5029. (unless (buffer-base-buffer (current-buffer))
  5030. (error "This is not an indirect buffer, something is wrong..."))
  5031. (unless (> (point-min) 1)
  5032. (error "This buffer is not narrowed, something is wrong..."))
  5033. (goto-char (point-min))
  5034. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5035. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5036. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5037. (goto-char (point-min))
  5038. (while (re-search-forward "\n" nil t)
  5039. (replace-match " "))
  5040. (goto-char (point-min))
  5041. (if (looking-at "\\s-*") (replace-match " "))
  5042. (if (re-search-forward "\\s-+\\'" nil t)
  5043. (replace-match "")))
  5044. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5045. (goto-char (point-min))
  5046. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5047. (replace-match ",\\1"))
  5048. (when font-lock-mode
  5049. (font-lock-unfontify-region (point-min) (point-max)))
  5050. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5051. (when (org-bound-and-true-p org-edit-src-picture)
  5052. (goto-char (point-min))
  5053. (while (re-search-forward "^" nil t)
  5054. (replace-match ": "))
  5055. (when font-lock-mode
  5056. (font-lock-unfontify-region (point-min) (point-max)))
  5057. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5058. (kill-buffer (current-buffer))
  5059. (and (org-mode-p) (org-restart-font-lock)))
  5060. ;;;; Plain list items, including checkboxes
  5061. ;;; Plain list items
  5062. (defun org-at-item-p ()
  5063. "Is point in a line starting a hand-formatted item?"
  5064. (let ((llt org-plain-list-ordered-item-terminator))
  5065. (save-excursion
  5066. (goto-char (point-at-bol))
  5067. (looking-at
  5068. (cond
  5069. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5070. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5071. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5072. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5073. (defun org-in-item-p ()
  5074. "It the cursor inside a plain list item.
  5075. Does not have to be the first line."
  5076. (save-excursion
  5077. (condition-case nil
  5078. (progn
  5079. (org-beginning-of-item)
  5080. (org-at-item-p)
  5081. t)
  5082. (error nil))))
  5083. (defun org-insert-item (&optional checkbox)
  5084. "Insert a new item at the current level.
  5085. Return t when things worked, nil when we are not in an item."
  5086. (when (save-excursion
  5087. (condition-case nil
  5088. (progn
  5089. (org-beginning-of-item)
  5090. (org-at-item-p)
  5091. (if (org-invisible-p) (error "Invisible item"))
  5092. t)
  5093. (error nil)))
  5094. (let* ((bul (match-string 0))
  5095. (descp (save-excursion (goto-char (match-beginning 0))
  5096. (beginning-of-line 1)
  5097. (save-match-data
  5098. (looking-at "[ \t]*.*? ::"))))
  5099. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5100. (match-end 0)))
  5101. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5102. pos)
  5103. (if descp (setq checkbox nil))
  5104. (cond
  5105. ((and (org-at-item-p) (<= (point) eow))
  5106. ;; before the bullet
  5107. (beginning-of-line 1)
  5108. (open-line (if blank 2 1)))
  5109. ((<= (point) eow)
  5110. (beginning-of-line 1))
  5111. (t
  5112. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  5113. (end-of-line 1)
  5114. (delete-horizontal-space))
  5115. (newline (if blank 2 1))))
  5116. (insert bul
  5117. (if checkbox "[ ]" "")
  5118. (if descp (concat (if checkbox " " "")
  5119. (read-string "Term: ") " :: ") ""))
  5120. (just-one-space)
  5121. (setq pos (point))
  5122. (end-of-line 1)
  5123. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5124. (org-maybe-renumber-ordered-list)
  5125. (and checkbox (org-update-checkbox-count-maybe))
  5126. t))
  5127. ;;; Checkboxes
  5128. (defun org-at-item-checkbox-p ()
  5129. "Is point at a line starting a plain-list item with a checklet?"
  5130. (and (org-at-item-p)
  5131. (save-excursion
  5132. (goto-char (match-end 0))
  5133. (skip-chars-forward " \t")
  5134. (looking-at "\\[[- X]\\]"))))
  5135. (defun org-toggle-checkbox (&optional arg)
  5136. "Toggle the checkbox in the current line."
  5137. (interactive "P")
  5138. (catch 'exit
  5139. (let (beg end status (firstnew 'unknown))
  5140. (cond
  5141. ((org-region-active-p)
  5142. (setq beg (region-beginning) end (region-end)))
  5143. ((org-on-heading-p)
  5144. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5145. ((org-at-item-checkbox-p)
  5146. (let ((pos (point)))
  5147. (replace-match
  5148. (cond (arg "[-]")
  5149. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5150. (t "[ ]"))
  5151. t t)
  5152. (goto-char pos))
  5153. (throw 'exit t))
  5154. (t (error "Not at a checkbox or heading, and no active region")))
  5155. (save-excursion
  5156. (goto-char beg)
  5157. (while (< (point) end)
  5158. (when (org-at-item-checkbox-p)
  5159. (setq status (equal (match-string 0) "[X]"))
  5160. (when (eq firstnew 'unknown)
  5161. (setq firstnew (not status)))
  5162. (replace-match
  5163. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5164. (beginning-of-line 2)))))
  5165. (org-update-checkbox-count-maybe))
  5166. (defun org-update-checkbox-count-maybe ()
  5167. "Update checkbox statistics unless turned off by user."
  5168. (when org-provide-checkbox-statistics
  5169. (org-update-checkbox-count)))
  5170. (defun org-update-checkbox-count (&optional all)
  5171. "Update the checkbox statistics in the current section.
  5172. This will find all statistic cookies like [57%] and [6/12] and update them
  5173. with the current numbers. With optional prefix argument ALL, do this for
  5174. the whole buffer."
  5175. (interactive "P")
  5176. (save-excursion
  5177. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5178. (beg (condition-case nil
  5179. (progn (outline-back-to-heading) (point))
  5180. (error (point-min))))
  5181. (end (move-marker (make-marker)
  5182. (progn (outline-next-heading) (point))))
  5183. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5184. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5185. (re-find (concat re "\\|" re-box))
  5186. beg-cookie end-cookie is-percent c-on c-off lim
  5187. eline curr-ind next-ind continue-from startsearch
  5188. (cstat 0)
  5189. )
  5190. (when all
  5191. (goto-char (point-min))
  5192. (outline-next-heading)
  5193. (setq beg (point) end (point-max)))
  5194. (goto-char end)
  5195. ;; find each statistic cookie
  5196. (while (re-search-backward re-find beg t)
  5197. (setq beg-cookie (match-beginning 1)
  5198. end-cookie (match-end 1)
  5199. cstat (+ cstat (if end-cookie 1 0))
  5200. startsearch (point-at-eol)
  5201. continue-from (point-at-bol)
  5202. is-percent (match-beginning 2)
  5203. lim (cond
  5204. ((org-on-heading-p) (outline-next-heading) (point))
  5205. ((org-at-item-p) (org-end-of-item) (point))
  5206. (t nil))
  5207. c-on 0
  5208. c-off 0)
  5209. (when lim
  5210. ;; find first checkbox for this cookie and gather
  5211. ;; statistics from all that are at this indentation level
  5212. (goto-char startsearch)
  5213. (if (re-search-forward re-box lim t)
  5214. (progn
  5215. (org-beginning-of-item)
  5216. (setq curr-ind (org-get-indentation))
  5217. (setq next-ind curr-ind)
  5218. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5219. (save-excursion (end-of-line) (setq eline (point)))
  5220. (if (re-search-forward re-box eline t)
  5221. (if (member (match-string 2) '("[ ]" "[-]"))
  5222. (setq c-off (1+ c-off))
  5223. (setq c-on (1+ c-on))
  5224. )
  5225. )
  5226. (org-end-of-item)
  5227. (setq next-ind (org-get-indentation))
  5228. )))
  5229. (goto-char continue-from)
  5230. ;; update cookie
  5231. (when end-cookie
  5232. (delete-region beg-cookie end-cookie)
  5233. (goto-char beg-cookie)
  5234. (insert
  5235. (if is-percent
  5236. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5237. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5238. ;; update items checkbox if it has one
  5239. (when (org-at-item-p)
  5240. (org-beginning-of-item)
  5241. (when (and (> (+ c-on c-off) 0)
  5242. (re-search-forward re-box (point-at-eol) t))
  5243. (setq beg-cookie (match-beginning 2)
  5244. end-cookie (match-end 2))
  5245. (delete-region beg-cookie end-cookie)
  5246. (goto-char beg-cookie)
  5247. (cond ((= c-off 0) (insert "[X]"))
  5248. ((= c-on 0) (insert "[ ]"))
  5249. (t (insert "[-]")))
  5250. )))
  5251. (goto-char continue-from))
  5252. (when (interactive-p)
  5253. (message "Checkbox satistics updated %s (%d places)"
  5254. (if all "in entire file" "in current outline entry") cstat)))))
  5255. (defun org-get-checkbox-statistics-face ()
  5256. "Select the face for checkbox statistics.
  5257. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5258. it will be `org-todo'."
  5259. (if (match-end 1)
  5260. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5261. (if (and (> (match-end 2) (match-beginning 2))
  5262. (equal (match-string 2) (match-string 3)))
  5263. 'org-done
  5264. 'org-todo)))
  5265. (defun org-get-indentation (&optional line)
  5266. "Get the indentation of the current line, interpreting tabs.
  5267. When LINE is given, assume it represents a line and compute its indentation."
  5268. (if line
  5269. (if (string-match "^ *" (org-remove-tabs line))
  5270. (match-end 0))
  5271. (save-excursion
  5272. (beginning-of-line 1)
  5273. (skip-chars-forward " \t")
  5274. (current-column))))
  5275. (defun org-remove-tabs (s &optional width)
  5276. "Replace tabulators in S with spaces.
  5277. Assumes that s is a single line, starting in column 0."
  5278. (setq width (or width tab-width))
  5279. (while (string-match "\t" s)
  5280. (setq s (replace-match
  5281. (make-string
  5282. (- (* width (/ (+ (match-beginning 0) width) width))
  5283. (match-beginning 0)) ?\ )
  5284. t t s)))
  5285. s)
  5286. (defun org-fix-indentation (line ind)
  5287. "Fix indentation in LINE.
  5288. IND is a cons cell with target and minimum indentation.
  5289. If the current indenation in LINE is smaller than the minimum,
  5290. leave it alone. If it is larger than ind, set it to the target."
  5291. (let* ((l (org-remove-tabs line))
  5292. (i (org-get-indentation l))
  5293. (i1 (car ind)) (i2 (cdr ind)))
  5294. (if (>= i i2) (setq l (substring line i2)))
  5295. (if (> i1 0)
  5296. (concat (make-string i1 ?\ ) l)
  5297. l)))
  5298. (defun org-beginning-of-item ()
  5299. "Go to the beginning of the current hand-formatted item.
  5300. If the cursor is not in an item, throw an error."
  5301. (interactive)
  5302. (let ((pos (point))
  5303. (limit (save-excursion
  5304. (condition-case nil
  5305. (progn
  5306. (org-back-to-heading)
  5307. (beginning-of-line 2) (point))
  5308. (error (point-min)))))
  5309. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5310. ind ind1)
  5311. (if (org-at-item-p)
  5312. (beginning-of-line 1)
  5313. (beginning-of-line 1)
  5314. (skip-chars-forward " \t")
  5315. (setq ind (current-column))
  5316. (if (catch 'exit
  5317. (while t
  5318. (beginning-of-line 0)
  5319. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5320. (if (looking-at "[ \t]*$")
  5321. (setq ind1 ind-empty)
  5322. (skip-chars-forward " \t")
  5323. (setq ind1 (current-column)))
  5324. (if (< ind1 ind)
  5325. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5326. nil
  5327. (goto-char pos)
  5328. (error "Not in an item")))))
  5329. (defun org-end-of-item ()
  5330. "Go to the end of the current hand-formatted item.
  5331. If the cursor is not in an item, throw an error."
  5332. (interactive)
  5333. (let* ((pos (point))
  5334. ind1
  5335. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5336. (limit (save-excursion (outline-next-heading) (point)))
  5337. (ind (save-excursion
  5338. (org-beginning-of-item)
  5339. (skip-chars-forward " \t")
  5340. (current-column)))
  5341. (end (catch 'exit
  5342. (while t
  5343. (beginning-of-line 2)
  5344. (if (eobp) (throw 'exit (point)))
  5345. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5346. (if (looking-at "[ \t]*$")
  5347. (setq ind1 ind-empty)
  5348. (skip-chars-forward " \t")
  5349. (setq ind1 (current-column)))
  5350. (if (<= ind1 ind)
  5351. (throw 'exit (point-at-bol)))))))
  5352. (if end
  5353. (goto-char end)
  5354. (goto-char pos)
  5355. (error "Not in an item"))))
  5356. (defun org-next-item ()
  5357. "Move to the beginning of the next item in the current plain list.
  5358. Error if not at a plain list, or if this is the last item in the list."
  5359. (interactive)
  5360. (let (ind ind1 (pos (point)))
  5361. (org-beginning-of-item)
  5362. (setq ind (org-get-indentation))
  5363. (org-end-of-item)
  5364. (setq ind1 (org-get-indentation))
  5365. (unless (and (org-at-item-p) (= ind ind1))
  5366. (goto-char pos)
  5367. (error "On last item"))))
  5368. (defun org-previous-item ()
  5369. "Move to the beginning of the previous item in the current plain list.
  5370. Error if not at a plain list, or if this is the first item in the list."
  5371. (interactive)
  5372. (let (beg ind ind1 (pos (point)))
  5373. (org-beginning-of-item)
  5374. (setq beg (point))
  5375. (setq ind (org-get-indentation))
  5376. (goto-char beg)
  5377. (catch 'exit
  5378. (while t
  5379. (beginning-of-line 0)
  5380. (if (looking-at "[ \t]*$")
  5381. nil
  5382. (if (<= (setq ind1 (org-get-indentation)) ind)
  5383. (throw 'exit t)))))
  5384. (condition-case nil
  5385. (if (or (not (org-at-item-p))
  5386. (< ind1 (1- ind)))
  5387. (error "")
  5388. (org-beginning-of-item))
  5389. (error (goto-char pos)
  5390. (error "On first item")))))
  5391. (defun org-first-list-item-p ()
  5392. "Is this heading the item in a plain list?"
  5393. (unless (org-at-item-p)
  5394. (error "Not at a plain list item"))
  5395. (org-beginning-of-item)
  5396. (= (point) (save-excursion (org-beginning-of-item-list))))
  5397. (defun org-move-item-down ()
  5398. "Move the plain list item at point down, i.e. swap with following item.
  5399. Subitems (items with larger indentation) are considered part of the item,
  5400. so this really moves item trees."
  5401. (interactive)
  5402. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5403. (org-beginning-of-item)
  5404. (setq beg0 (point))
  5405. (save-excursion
  5406. (setq ne-beg (org-back-over-empty-lines))
  5407. (setq beg (point)))
  5408. (goto-char beg0)
  5409. (setq ind (org-get-indentation))
  5410. (org-end-of-item)
  5411. (setq end0 (point))
  5412. (setq ind1 (org-get-indentation))
  5413. (setq ne-end (org-back-over-empty-lines))
  5414. (setq end (point))
  5415. (goto-char beg0)
  5416. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5417. ;; include less whitespace
  5418. (save-excursion
  5419. (goto-char beg)
  5420. (forward-line (- ne-beg ne-end))
  5421. (setq beg (point))))
  5422. (goto-char end0)
  5423. (if (and (org-at-item-p) (= ind ind1))
  5424. (progn
  5425. (org-end-of-item)
  5426. (org-back-over-empty-lines)
  5427. (setq txt (buffer-substring beg end))
  5428. (save-excursion
  5429. (delete-region beg end))
  5430. (setq pos (point))
  5431. (insert txt)
  5432. (goto-char pos) (org-skip-whitespace)
  5433. (org-maybe-renumber-ordered-list))
  5434. (goto-char pos)
  5435. (error "Cannot move this item further down"))))
  5436. (defun org-move-item-up (arg)
  5437. "Move the plain list item at point up, i.e. swap with previous item.
  5438. Subitems (items with larger indentation) are considered part of the item,
  5439. so this really moves item trees."
  5440. (interactive "p")
  5441. (let (beg beg0 end ind ind1 (pos (point)) txt
  5442. ne-beg ne-ins ins-end)
  5443. (org-beginning-of-item)
  5444. (setq beg0 (point))
  5445. (setq ind (org-get-indentation))
  5446. (save-excursion
  5447. (setq ne-beg (org-back-over-empty-lines))
  5448. (setq beg (point)))
  5449. (goto-char beg0)
  5450. (org-end-of-item)
  5451. (org-back-over-empty-lines)
  5452. (setq end (point))
  5453. (goto-char beg0)
  5454. (catch 'exit
  5455. (while t
  5456. (beginning-of-line 0)
  5457. (if (looking-at "[ \t]*$")
  5458. (if org-empty-line-terminates-plain-lists
  5459. (progn
  5460. (goto-char pos)
  5461. (error "Cannot move this item further up"))
  5462. nil)
  5463. (if (<= (setq ind1 (org-get-indentation)) ind)
  5464. (throw 'exit t)))))
  5465. (condition-case nil
  5466. (org-beginning-of-item)
  5467. (error (goto-char beg0)
  5468. (error "Cannot move this item further up")))
  5469. (setq ind1 (org-get-indentation))
  5470. (if (and (org-at-item-p) (= ind ind1))
  5471. (progn
  5472. (setq ne-ins (org-back-over-empty-lines))
  5473. (setq txt (buffer-substring beg end))
  5474. (save-excursion
  5475. (delete-region beg end))
  5476. (setq pos (point))
  5477. (insert txt)
  5478. (setq ins-end (point))
  5479. (goto-char pos) (org-skip-whitespace)
  5480. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5481. ;; Move whitespace back to beginning
  5482. (save-excursion
  5483. (goto-char ins-end)
  5484. (let ((kill-whole-line t))
  5485. (kill-line (- ne-ins ne-beg)) (point)))
  5486. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5487. (org-maybe-renumber-ordered-list))
  5488. (goto-char pos)
  5489. (error "Cannot move this item further up"))))
  5490. (defun org-maybe-renumber-ordered-list ()
  5491. "Renumber the ordered list at point if setup allows it.
  5492. This tests the user option `org-auto-renumber-ordered-lists' before
  5493. doing the renumbering."
  5494. (interactive)
  5495. (when (and org-auto-renumber-ordered-lists
  5496. (org-at-item-p))
  5497. (if (match-beginning 3)
  5498. (org-renumber-ordered-list 1)
  5499. (org-fix-bullet-type))))
  5500. (defun org-maybe-renumber-ordered-list-safe ()
  5501. (condition-case nil
  5502. (save-excursion
  5503. (org-maybe-renumber-ordered-list))
  5504. (error nil)))
  5505. (defun org-cycle-list-bullet (&optional which)
  5506. "Cycle through the different itemize/enumerate bullets.
  5507. This cycle the entire list level through the sequence:
  5508. `-' -> `+' -> `*' -> `1.' -> `1)'
  5509. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5510. 0 meand `-', 1 means `+' etc."
  5511. (interactive "P")
  5512. (org-preserve-lc
  5513. (org-beginning-of-item-list)
  5514. (org-at-item-p)
  5515. (beginning-of-line 1)
  5516. (let ((current (match-string 0))
  5517. (prevp (eq which 'previous))
  5518. new)
  5519. (setq new (cond
  5520. ((and (numberp which)
  5521. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5522. ((string-match "-" current) (if prevp "1)" "+"))
  5523. ((string-match "\\+" current)
  5524. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5525. ((string-match "\\*" current) (if prevp "+" "1."))
  5526. ((string-match "\\." current) (if prevp "*" "1)"))
  5527. ((string-match ")" current) (if prevp "1." "-"))
  5528. (t (error "This should not happen"))))
  5529. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5530. (org-fix-bullet-type)
  5531. (org-maybe-renumber-ordered-list))))
  5532. (defun org-get-string-indentation (s)
  5533. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5534. (let ((n -1) (i 0) (w tab-width) c)
  5535. (catch 'exit
  5536. (while (< (setq n (1+ n)) (length s))
  5537. (setq c (aref s n))
  5538. (cond ((= c ?\ ) (setq i (1+ i)))
  5539. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5540. (t (throw 'exit t)))))
  5541. i))
  5542. (defun org-renumber-ordered-list (arg)
  5543. "Renumber an ordered plain list.
  5544. Cursor needs to be in the first line of an item, the line that starts
  5545. with something like \"1.\" or \"2)\"."
  5546. (interactive "p")
  5547. (unless (and (org-at-item-p)
  5548. (match-beginning 3))
  5549. (error "This is not an ordered list"))
  5550. (let ((line (org-current-line))
  5551. (col (current-column))
  5552. (ind (org-get-string-indentation
  5553. (buffer-substring (point-at-bol) (match-beginning 3))))
  5554. ;; (term (substring (match-string 3) -1))
  5555. ind1 (n (1- arg))
  5556. fmt bobp)
  5557. ;; find where this list begins
  5558. (org-beginning-of-item-list)
  5559. (setq bobp (bobp))
  5560. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5561. (setq fmt (concat "%d" (match-string 1)))
  5562. (beginning-of-line 0)
  5563. ;; walk forward and replace these numbers
  5564. (catch 'exit
  5565. (while t
  5566. (catch 'next
  5567. (if bobp (setq bobp nil) (beginning-of-line 2))
  5568. (if (eobp) (throw 'exit nil))
  5569. (if (looking-at "[ \t]*$") (throw 'next nil))
  5570. (skip-chars-forward " \t") (setq ind1 (current-column))
  5571. (if (> ind1 ind) (throw 'next t))
  5572. (if (< ind1 ind) (throw 'exit t))
  5573. (if (not (org-at-item-p)) (throw 'exit nil))
  5574. (delete-region (match-beginning 2) (match-end 2))
  5575. (goto-char (match-beginning 2))
  5576. (insert (format fmt (setq n (1+ n)))))))
  5577. (goto-line line)
  5578. (org-move-to-column col)))
  5579. (defun org-fix-bullet-type ()
  5580. "Make sure all items in this list have the same bullet as the firsst item."
  5581. (interactive)
  5582. (unless (org-at-item-p) (error "This is not a list"))
  5583. (let ((line (org-current-line))
  5584. (col (current-column))
  5585. (ind (current-indentation))
  5586. ind1 bullet)
  5587. ;; find where this list begins
  5588. (org-beginning-of-item-list)
  5589. (beginning-of-line 1)
  5590. ;; find out what the bullet type is
  5591. (looking-at "[ \t]*\\(\\S-+\\)")
  5592. (setq bullet (match-string 1))
  5593. ;; walk forward and replace these numbers
  5594. (beginning-of-line 0)
  5595. (catch 'exit
  5596. (while t
  5597. (catch 'next
  5598. (beginning-of-line 2)
  5599. (if (eobp) (throw 'exit nil))
  5600. (if (looking-at "[ \t]*$") (throw 'next nil))
  5601. (skip-chars-forward " \t") (setq ind1 (current-column))
  5602. (if (> ind1 ind) (throw 'next t))
  5603. (if (< ind1 ind) (throw 'exit t))
  5604. (if (not (org-at-item-p)) (throw 'exit nil))
  5605. (skip-chars-forward " \t")
  5606. (looking-at "\\S-+")
  5607. (replace-match bullet))))
  5608. (goto-line line)
  5609. (org-move-to-column col)
  5610. (if (string-match "[0-9]" bullet)
  5611. (org-renumber-ordered-list 1))))
  5612. (defun org-beginning-of-item-list ()
  5613. "Go to the beginning of the current item list.
  5614. I.e. to the first item in this list."
  5615. (interactive)
  5616. (org-beginning-of-item)
  5617. (let ((pos (point-at-bol))
  5618. (ind (org-get-indentation))
  5619. ind1)
  5620. ;; find where this list begins
  5621. (catch 'exit
  5622. (while t
  5623. (catch 'next
  5624. (beginning-of-line 0)
  5625. (if (looking-at "[ \t]*$")
  5626. (throw (if (bobp) 'exit 'next) t))
  5627. (skip-chars-forward " \t") (setq ind1 (current-column))
  5628. (if (or (< ind1 ind)
  5629. (and (= ind1 ind)
  5630. (not (org-at-item-p)))
  5631. (and (= (point-at-bol) (point-min))
  5632. (setq pos (point-min))))
  5633. (throw 'exit t)
  5634. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5635. (goto-char pos)))
  5636. (defun org-end-of-item-list ()
  5637. "Go to the end of the current item list.
  5638. I.e. to the text after the last item."
  5639. (interactive)
  5640. (org-beginning-of-item)
  5641. (let ((pos (point-at-bol))
  5642. (ind (org-get-indentation))
  5643. ind1)
  5644. ;; find where this list begins
  5645. (catch 'exit
  5646. (while t
  5647. (catch 'next
  5648. (beginning-of-line 2)
  5649. (if (looking-at "[ \t]*$")
  5650. (throw (if (eobp) 'exit 'next) t))
  5651. (skip-chars-forward " \t") (setq ind1 (current-column))
  5652. (if (or (< ind1 ind)
  5653. (and (= ind1 ind)
  5654. (not (org-at-item-p)))
  5655. (eobp))
  5656. (progn
  5657. (setq pos (point-at-bol))
  5658. (throw 'exit t))))))
  5659. (goto-char pos)))
  5660. (defvar org-last-indent-begin-marker (make-marker))
  5661. (defvar org-last-indent-end-marker (make-marker))
  5662. (defun org-outdent-item (arg)
  5663. "Outdent a local list item."
  5664. (interactive "p")
  5665. (org-indent-item (- arg)))
  5666. (defun org-indent-item (arg)
  5667. "Indent a local list item."
  5668. (interactive "p")
  5669. (unless (org-at-item-p)
  5670. (error "Not on an item"))
  5671. (save-excursion
  5672. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5673. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5674. (setq beg org-last-indent-begin-marker
  5675. end org-last-indent-end-marker)
  5676. (org-beginning-of-item)
  5677. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5678. (org-end-of-item)
  5679. (setq end (move-marker org-last-indent-end-marker (point))))
  5680. (goto-char beg)
  5681. (setq tmp (org-item-indent-positions)
  5682. ind (car tmp)
  5683. ind-down (nth 2 tmp)
  5684. ind-up (nth 1 tmp)
  5685. delta (if (> arg 0)
  5686. (if ind-down (- ind-down ind) 2)
  5687. (if ind-up (- ind-up ind) -2)))
  5688. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5689. (while (< (point) end)
  5690. (beginning-of-line 1)
  5691. (skip-chars-forward " \t") (setq ind1 (current-column))
  5692. (delete-region (point-at-bol) (point))
  5693. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5694. (beginning-of-line 2))))
  5695. (org-fix-bullet-type)
  5696. (org-maybe-renumber-ordered-list-safe)
  5697. (save-excursion
  5698. (beginning-of-line 0)
  5699. (condition-case nil (org-beginning-of-item) (error nil))
  5700. (org-maybe-renumber-ordered-list-safe)))
  5701. (defun org-item-indent-positions ()
  5702. "Return indentation for plain list items.
  5703. This returns a list with three values: The current indentation, the
  5704. parent indentation and the indentation a child should habe.
  5705. Assumes cursor in item line."
  5706. (let* ((bolpos (point-at-bol))
  5707. (ind (org-get-indentation))
  5708. ind-down ind-up pos)
  5709. (save-excursion
  5710. (org-beginning-of-item-list)
  5711. (skip-chars-backward "\n\r \t")
  5712. (when (org-in-item-p)
  5713. (org-beginning-of-item)
  5714. (setq ind-up (org-get-indentation))))
  5715. (setq pos (point))
  5716. (save-excursion
  5717. (cond
  5718. ((and (condition-case nil (progn (org-previous-item) t)
  5719. (error nil))
  5720. (or (forward-char 1) t)
  5721. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5722. (setq ind-down (org-get-indentation)))
  5723. ((and (goto-char pos)
  5724. (org-at-item-p))
  5725. (goto-char (match-end 0))
  5726. (skip-chars-forward " \t")
  5727. (setq ind-down (current-column)))))
  5728. (list ind ind-up ind-down)))
  5729. ;;; The orgstruct minor mode
  5730. ;; Define a minor mode which can be used in other modes in order to
  5731. ;; integrate the org-mode structure editing commands.
  5732. ;; This is really a hack, because the org-mode structure commands use
  5733. ;; keys which normally belong to the major mode. Here is how it
  5734. ;; works: The minor mode defines all the keys necessary to operate the
  5735. ;; structure commands, but wraps the commands into a function which
  5736. ;; tests if the cursor is currently at a headline or a plain list
  5737. ;; item. If that is the case, the structure command is used,
  5738. ;; temporarily setting many Org-mode variables like regular
  5739. ;; expressions for filling etc. However, when any of those keys is
  5740. ;; used at a different location, function uses `key-binding' to look
  5741. ;; up if the key has an associated command in another currently active
  5742. ;; keymap (minor modes, major mode, global), and executes that
  5743. ;; command. There might be problems if any of the keys is otherwise
  5744. ;; used as a prefix key.
  5745. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5746. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5747. ;; addresses this by checking explicitly for both bindings.
  5748. (defvar orgstruct-mode-map (make-sparse-keymap)
  5749. "Keymap for the minor `orgstruct-mode'.")
  5750. (defvar org-local-vars nil
  5751. "List of local variables, for use by `orgstruct-mode'")
  5752. ;;;###autoload
  5753. (define-minor-mode orgstruct-mode
  5754. "Toggle the minor more `orgstruct-mode'.
  5755. This mode is for using Org-mode structure commands in other modes.
  5756. The following key behave as if Org-mode was active, if the cursor
  5757. is on a headline, or on a plain list item (both in the definition
  5758. of Org-mode).
  5759. M-up Move entry/item up
  5760. M-down Move entry/item down
  5761. M-left Promote
  5762. M-right Demote
  5763. M-S-up Move entry/item up
  5764. M-S-down Move entry/item down
  5765. M-S-left Promote subtree
  5766. M-S-right Demote subtree
  5767. M-q Fill paragraph and items like in Org-mode
  5768. C-c ^ Sort entries
  5769. C-c - Cycle list bullet
  5770. TAB Cycle item visibility
  5771. M-RET Insert new heading/item
  5772. S-M-RET Insert new TODO heading / Chekbox item
  5773. C-c C-c Set tags / toggle checkbox"
  5774. nil " OrgStruct" nil
  5775. (org-load-modules-maybe)
  5776. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5777. ;;;###autoload
  5778. (defun turn-on-orgstruct ()
  5779. "Unconditionally turn on `orgstruct-mode'."
  5780. (orgstruct-mode 1))
  5781. ;;;###autoload
  5782. (defun turn-on-orgstruct++ ()
  5783. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5784. In addition to setting orgstruct-mode, this also exports all indentation and
  5785. autofilling variables from org-mode into the buffer. Note that turning
  5786. off orgstruct-mode will *not* remove these additional settings."
  5787. (orgstruct-mode 1)
  5788. (let (var val)
  5789. (mapc
  5790. (lambda (x)
  5791. (when (string-match
  5792. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5793. (symbol-name (car x)))
  5794. (setq var (car x) val (nth 1 x))
  5795. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5796. org-local-vars)))
  5797. (defun orgstruct-error ()
  5798. "Error when there is no default binding for a structure key."
  5799. (interactive)
  5800. (error "This key has no function outside structure elements"))
  5801. (defun orgstruct-setup ()
  5802. "Setup orgstruct keymaps."
  5803. (let ((nfunc 0)
  5804. (bindings
  5805. (list
  5806. '([(meta up)] org-metaup)
  5807. '([(meta down)] org-metadown)
  5808. '([(meta left)] org-metaleft)
  5809. '([(meta right)] org-metaright)
  5810. '([(meta shift up)] org-shiftmetaup)
  5811. '([(meta shift down)] org-shiftmetadown)
  5812. '([(meta shift left)] org-shiftmetaleft)
  5813. '([(meta shift right)] org-shiftmetaright)
  5814. '([(shift up)] org-shiftup)
  5815. '([(shift down)] org-shiftdown)
  5816. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5817. '("\M-q" fill-paragraph)
  5818. '("\C-c^" org-sort)
  5819. '("\C-c-" org-cycle-list-bullet)))
  5820. elt key fun cmd)
  5821. (while (setq elt (pop bindings))
  5822. (setq nfunc (1+ nfunc))
  5823. (setq key (org-key (car elt))
  5824. fun (nth 1 elt)
  5825. cmd (orgstruct-make-binding fun nfunc key))
  5826. (org-defkey orgstruct-mode-map key cmd))
  5827. ;; Special treatment needed for TAB and RET
  5828. (org-defkey orgstruct-mode-map [(tab)]
  5829. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5830. (org-defkey orgstruct-mode-map "\C-i"
  5831. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5832. (org-defkey orgstruct-mode-map "\M-\C-m"
  5833. (orgstruct-make-binding 'org-insert-heading 105
  5834. "\M-\C-m" [(meta return)]))
  5835. (org-defkey orgstruct-mode-map [(meta return)]
  5836. (orgstruct-make-binding 'org-insert-heading 106
  5837. [(meta return)] "\M-\C-m"))
  5838. (org-defkey orgstruct-mode-map [(shift meta return)]
  5839. (orgstruct-make-binding 'org-insert-todo-heading 107
  5840. [(meta return)] "\M-\C-m"))
  5841. (unless org-local-vars
  5842. (setq org-local-vars (org-get-local-variables)))
  5843. t))
  5844. (defun orgstruct-make-binding (fun n &rest keys)
  5845. "Create a function for binding in the structure minor mode.
  5846. FUN is the command to call inside a table. N is used to create a unique
  5847. command name. KEYS are keys that should be checked in for a command
  5848. to execute outside of tables."
  5849. (eval
  5850. (list 'defun
  5851. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5852. '(arg)
  5853. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5854. "Outside of structure, run the binding of `"
  5855. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5856. "'.")
  5857. '(interactive "p")
  5858. (list 'if
  5859. '(org-context-p 'headline 'item)
  5860. (list 'org-run-like-in-org-mode (list 'quote fun))
  5861. (list 'let '(orgstruct-mode)
  5862. (list 'call-interactively
  5863. (append '(or)
  5864. (mapcar (lambda (k)
  5865. (list 'key-binding k))
  5866. keys)
  5867. '('orgstruct-error))))))))
  5868. (defun org-context-p (&rest contexts)
  5869. "Check if local context is any of CONTEXTS.
  5870. Possible values in the list of contexts are `table', `headline', and `item'."
  5871. (let ((pos (point)))
  5872. (goto-char (point-at-bol))
  5873. (prog1 (or (and (memq 'table contexts)
  5874. (looking-at "[ \t]*|"))
  5875. (and (memq 'headline contexts)
  5876. ;;????????? (looking-at "\\*+"))
  5877. (looking-at outline-regexp))
  5878. (and (memq 'item contexts)
  5879. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5880. (goto-char pos))))
  5881. (defun org-get-local-variables ()
  5882. "Return a list of all local variables in an org-mode buffer."
  5883. (let (varlist)
  5884. (with-current-buffer (get-buffer-create "*Org tmp*")
  5885. (erase-buffer)
  5886. (org-mode)
  5887. (setq varlist (buffer-local-variables)))
  5888. (kill-buffer "*Org tmp*")
  5889. (delq nil
  5890. (mapcar
  5891. (lambda (x)
  5892. (setq x
  5893. (if (symbolp x)
  5894. (list x)
  5895. (list (car x) (list 'quote (cdr x)))))
  5896. (if (string-match
  5897. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5898. (symbol-name (car x)))
  5899. x nil))
  5900. varlist))))
  5901. ;;;###autoload
  5902. (defun org-run-like-in-org-mode (cmd)
  5903. (org-load-modules-maybe)
  5904. (unless org-local-vars
  5905. (setq org-local-vars (org-get-local-variables)))
  5906. (eval (list 'let org-local-vars
  5907. (list 'call-interactively (list 'quote cmd)))))
  5908. ;;;; Archiving
  5909. (defun org-get-category (&optional pos)
  5910. "Get the category applying to position POS."
  5911. (get-text-property (or pos (point)) 'org-category))
  5912. (defun org-refresh-category-properties ()
  5913. "Refresh category text properties in the buffer."
  5914. (let ((def-cat (cond
  5915. ((null org-category)
  5916. (if buffer-file-name
  5917. (file-name-sans-extension
  5918. (file-name-nondirectory buffer-file-name))
  5919. "???"))
  5920. ((symbolp org-category) (symbol-name org-category))
  5921. (t org-category)))
  5922. beg end cat pos optionp)
  5923. (org-unmodified
  5924. (save-excursion
  5925. (save-restriction
  5926. (widen)
  5927. (goto-char (point-min))
  5928. (put-text-property (point) (point-max) 'org-category def-cat)
  5929. (while (re-search-forward
  5930. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5931. (setq pos (match-end 0)
  5932. optionp (equal (char-after (match-beginning 0)) ?#)
  5933. cat (org-trim (match-string 2)))
  5934. (if optionp
  5935. (setq beg (point-at-bol) end (point-max))
  5936. (org-back-to-heading t)
  5937. (setq beg (point) end (org-end-of-subtree t t)))
  5938. (put-text-property beg end 'org-category cat)
  5939. (goto-char pos)))))))
  5940. ;;;; Link Stuff
  5941. ;;; Link abbreviations
  5942. (defun org-link-expand-abbrev (link)
  5943. "Apply replacements as defined in `org-link-abbrev-alist."
  5944. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5945. (let* ((key (match-string 1 link))
  5946. (as (or (assoc key org-link-abbrev-alist-local)
  5947. (assoc key org-link-abbrev-alist)))
  5948. (tag (and (match-end 2) (match-string 3 link)))
  5949. rpl)
  5950. (if (not as)
  5951. link
  5952. (setq rpl (cdr as))
  5953. (cond
  5954. ((symbolp rpl) (funcall rpl tag))
  5955. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5956. (t (concat rpl tag)))))
  5957. link))
  5958. ;;; Storing and inserting links
  5959. (defvar org-insert-link-history nil
  5960. "Minibuffer history for links inserted with `org-insert-link'.")
  5961. (defvar org-stored-links nil
  5962. "Contains the links stored with `org-store-link'.")
  5963. (defvar org-store-link-plist nil
  5964. "Plist with info about the most recently link created with `org-store-link'.")
  5965. (defvar org-link-protocols nil
  5966. "Link protocols added to Org-mode using `org-add-link-type'.")
  5967. (defvar org-store-link-functions nil
  5968. "List of functions that are called to create and store a link.
  5969. Each function will be called in turn until one returns a non-nil
  5970. value. Each function should check if it is responsible for creating
  5971. this link (for example by looking at the major mode).
  5972. If not, it must exit and return nil.
  5973. If yes, it should return a non-nil value after a calling
  5974. `org-store-link-props' with a list of properties and values.
  5975. Special properties are:
  5976. :type The link prefix. like \"http\". This must be given.
  5977. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5978. This is obligatory as well.
  5979. :description Optional default description for the second pair
  5980. of brackets in an Org-mode link. The user can still change
  5981. this when inserting this link into an Org-mode buffer.
  5982. In addition to these, any additional properties can be specified
  5983. and then used in remember templates.")
  5984. (defun org-add-link-type (type &optional follow export)
  5985. "Add TYPE to the list of `org-link-types'.
  5986. Re-compute all regular expressions depending on `org-link-types'
  5987. FOLLOW and EXPORT are two functions.
  5988. FOLLOW should take the link path as the single argument and do whatever
  5989. is necessary to follow the link, for example find a file or display
  5990. a mail message.
  5991. EXPORT should format the link path for export to one of the export formats.
  5992. It should be a function accepting three arguments:
  5993. path the path of the link, the text after the prefix (like \"http:\")
  5994. desc the description of the link, if any, nil if there was no descripton
  5995. format the export format, a symbol like `html' or `latex'.
  5996. The function may use the FORMAT information to return different values
  5997. depending on the format. The return value will be put literally into
  5998. the exported file.
  5999. Org-mode has a built-in default for exporting links. If you are happy with
  6000. this default, there is no need to define an export function for the link
  6001. type. For a simple example of an export function, see `org-bbdb.el'."
  6002. (add-to-list 'org-link-types type t)
  6003. (org-make-link-regexps)
  6004. (if (assoc type org-link-protocols)
  6005. (setcdr (assoc type org-link-protocols) (list follow export))
  6006. (push (list type follow export) org-link-protocols)))
  6007. ;;;###autoload
  6008. (defun org-store-link (arg)
  6009. "\\<org-mode-map>Store an org-link to the current location.
  6010. This link is added to `org-stored-links' and can later be inserted
  6011. into an org-buffer with \\[org-insert-link].
  6012. For some link types, a prefix arg is interpreted:
  6013. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6014. For file links, arg negates `org-context-in-file-links'."
  6015. (interactive "P")
  6016. (org-load-modules-maybe)
  6017. (setq org-store-link-plist nil) ; reset
  6018. (let (link cpltxt desc description search txt)
  6019. (cond
  6020. ((run-hook-with-args-until-success 'org-store-link-functions)
  6021. (setq link (plist-get org-store-link-plist :link)
  6022. desc (or (plist-get org-store-link-plist :description) link)))
  6023. ((eq major-mode 'calendar-mode)
  6024. (let ((cd (calendar-cursor-to-date)))
  6025. (setq link
  6026. (format-time-string
  6027. (car org-time-stamp-formats)
  6028. (apply 'encode-time
  6029. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6030. nil nil nil))))
  6031. (org-store-link-props :type "calendar" :date cd)))
  6032. ((eq major-mode 'w3-mode)
  6033. (setq cpltxt (url-view-url t)
  6034. link (org-make-link cpltxt))
  6035. (org-store-link-props :type "w3" :url (url-view-url t)))
  6036. ((eq major-mode 'w3m-mode)
  6037. (setq cpltxt (or w3m-current-title w3m-current-url)
  6038. link (org-make-link w3m-current-url))
  6039. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6040. ((setq search (run-hook-with-args-until-success
  6041. 'org-create-file-search-functions))
  6042. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6043. "::" search))
  6044. (setq cpltxt (or description link)))
  6045. ((eq major-mode 'image-mode)
  6046. (setq cpltxt (concat "file:"
  6047. (abbreviate-file-name buffer-file-name))
  6048. link (org-make-link cpltxt))
  6049. (org-store-link-props :type "image" :file buffer-file-name))
  6050. ((eq major-mode 'dired-mode)
  6051. ;; link to the file in the current line
  6052. (setq cpltxt (concat "file:"
  6053. (abbreviate-file-name
  6054. (expand-file-name
  6055. (dired-get-filename nil t))))
  6056. link (org-make-link cpltxt)))
  6057. ((and buffer-file-name (org-mode-p))
  6058. ;; Just link to current headline
  6059. (setq cpltxt (concat "file:"
  6060. (abbreviate-file-name buffer-file-name)))
  6061. ;; Add a context search string
  6062. (when (org-xor org-context-in-file-links arg)
  6063. ;; Check if we are on a target
  6064. (if (org-in-regexp "<<\\(.*?\\)>>")
  6065. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6066. (setq txt (cond
  6067. ((org-on-heading-p) nil)
  6068. ((org-region-active-p)
  6069. (buffer-substring (region-beginning) (region-end)))
  6070. (t nil)))
  6071. (when (or (null txt) (string-match "\\S-" txt))
  6072. (setq cpltxt
  6073. (concat cpltxt "::"
  6074. (condition-case nil
  6075. (org-make-org-heading-search-string txt)
  6076. (error "")))
  6077. desc "NONE"))))
  6078. (if (string-match "::\\'" cpltxt)
  6079. (setq cpltxt (substring cpltxt 0 -2)))
  6080. (setq link (org-make-link cpltxt)))
  6081. ((buffer-file-name (buffer-base-buffer))
  6082. ;; Just link to this file here.
  6083. (setq cpltxt (concat "file:"
  6084. (abbreviate-file-name
  6085. (buffer-file-name (buffer-base-buffer)))))
  6086. ;; Add a context string
  6087. (when (org-xor org-context-in-file-links arg)
  6088. (setq txt (if (org-region-active-p)
  6089. (buffer-substring (region-beginning) (region-end))
  6090. (buffer-substring (point-at-bol) (point-at-eol))))
  6091. ;; Only use search option if there is some text.
  6092. (when (string-match "\\S-" txt)
  6093. (setq cpltxt
  6094. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6095. desc "NONE")))
  6096. (setq link (org-make-link cpltxt)))
  6097. ((interactive-p)
  6098. (error "Cannot link to a buffer which is not visiting a file"))
  6099. (t (setq link nil)))
  6100. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6101. (setq link (or link cpltxt)
  6102. desc (or desc cpltxt))
  6103. (if (equal desc "NONE") (setq desc nil))
  6104. (if (and (interactive-p) link)
  6105. (progn
  6106. (setq org-stored-links
  6107. (cons (list link desc) org-stored-links))
  6108. (message "Stored: %s" (or desc link)))
  6109. (and link (org-make-link-string link desc)))))
  6110. (defun org-store-link-props (&rest plist)
  6111. "Store link properties, extract names and addresses."
  6112. (let (x adr)
  6113. (when (setq x (plist-get plist :from))
  6114. (setq adr (mail-extract-address-components x))
  6115. (plist-put plist :fromname (car adr))
  6116. (plist-put plist :fromaddress (nth 1 adr)))
  6117. (when (setq x (plist-get plist :to))
  6118. (setq adr (mail-extract-address-components x))
  6119. (plist-put plist :toname (car adr))
  6120. (plist-put plist :toaddress (nth 1 adr))))
  6121. (let ((from (plist-get plist :from))
  6122. (to (plist-get plist :to)))
  6123. (when (and from to org-from-is-user-regexp)
  6124. (plist-put plist :fromto
  6125. (if (string-match org-from-is-user-regexp from)
  6126. (concat "to %t")
  6127. (concat "from %f")))))
  6128. (setq org-store-link-plist plist))
  6129. (defun org-add-link-props (&rest plist)
  6130. "Add these properties to the link property list."
  6131. (let (key value)
  6132. (while plist
  6133. (setq key (pop plist) value (pop plist))
  6134. (setq org-store-link-plist
  6135. (plist-put org-store-link-plist key value)))))
  6136. (defun org-email-link-description (&optional fmt)
  6137. "Return the description part of an email link.
  6138. This takes information from `org-store-link-plist' and formats it
  6139. according to FMT (default from `org-email-link-description-format')."
  6140. (setq fmt (or fmt org-email-link-description-format))
  6141. (let* ((p org-store-link-plist)
  6142. (to (plist-get p :toaddress))
  6143. (from (plist-get p :fromaddress))
  6144. (table
  6145. (list
  6146. (cons "%c" (plist-get p :fromto))
  6147. (cons "%F" (plist-get p :from))
  6148. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6149. (cons "%T" (plist-get p :to))
  6150. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6151. (cons "%s" (plist-get p :subject))
  6152. (cons "%m" (plist-get p :message-id)))))
  6153. (when (string-match "%c" fmt)
  6154. ;; Check if the user wrote this message
  6155. (if (and org-from-is-user-regexp from to
  6156. (save-match-data (string-match org-from-is-user-regexp from)))
  6157. (setq fmt (replace-match "to %t" t t fmt))
  6158. (setq fmt (replace-match "from %f" t t fmt))))
  6159. (org-replace-escapes fmt table)))
  6160. (defun org-make-org-heading-search-string (&optional string heading)
  6161. "Make search string for STRING or current headline."
  6162. (interactive)
  6163. (let ((s (or string (org-get-heading))))
  6164. (unless (and string (not heading))
  6165. ;; We are using a headline, clean up garbage in there.
  6166. (if (string-match org-todo-regexp s)
  6167. (setq s (replace-match "" t t s)))
  6168. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6169. (setq s (replace-match "" t t s)))
  6170. (setq s (org-trim s))
  6171. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6172. org-comment-string "\\)") s)
  6173. (setq s (replace-match "" t t s)))
  6174. (while (string-match org-ts-regexp s)
  6175. (setq s (replace-match "" t t s))))
  6176. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6177. (setq s (replace-match " " t t s)))
  6178. (or string (setq s (concat "*" s))) ; Add * for headlines
  6179. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6180. (defun org-make-link (&rest strings)
  6181. "Concatenate STRINGS."
  6182. (apply 'concat strings))
  6183. (defun org-make-link-string (link &optional description)
  6184. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6185. (unless (string-match "\\S-" link)
  6186. (error "Empty link"))
  6187. (when (stringp description)
  6188. ;; Remove brackets from the description, they are fatal.
  6189. (while (string-match "\\[" description)
  6190. (setq description (replace-match "{" t t description)))
  6191. (while (string-match "\\]" description)
  6192. (setq description (replace-match "}" t t description))))
  6193. (when (equal (org-link-escape link) description)
  6194. ;; No description needed, it is identical
  6195. (setq description nil))
  6196. (when (and (not description)
  6197. (not (equal link (org-link-escape link))))
  6198. (setq description (org-extract-attributes link)))
  6199. (concat "[[" (org-link-escape link) "]"
  6200. (if description (concat "[" description "]") "")
  6201. "]"))
  6202. (defconst org-link-escape-chars
  6203. '((?\ . "%20")
  6204. (?\[ . "%5B")
  6205. (?\] . "%5D")
  6206. (?\340 . "%E0") ; `a
  6207. (?\342 . "%E2") ; ^a
  6208. (?\347 . "%E7") ; ,c
  6209. (?\350 . "%E8") ; `e
  6210. (?\351 . "%E9") ; 'e
  6211. (?\352 . "%EA") ; ^e
  6212. (?\356 . "%EE") ; ^i
  6213. (?\364 . "%F4") ; ^o
  6214. (?\371 . "%F9") ; `u
  6215. (?\373 . "%FB") ; ^u
  6216. (?\; . "%3B")
  6217. (?? . "%3F")
  6218. (?= . "%3D")
  6219. (?+ . "%2B")
  6220. )
  6221. "Association list of escapes for some characters problematic in links.
  6222. This is the list that is used for internal purposes.")
  6223. (defconst org-link-escape-chars-browser
  6224. '((?\ . "%20")) ; 32 for the SPC char
  6225. "Association list of escapes for some characters problematic in links.
  6226. This is the list that is used before handing over to the browser.")
  6227. (defun org-link-escape (text &optional table)
  6228. "Escape charaters in TEXT that are problematic for links."
  6229. (setq table (or table org-link-escape-chars))
  6230. (when text
  6231. (let ((re (mapconcat (lambda (x) (regexp-quote
  6232. (char-to-string (car x))))
  6233. table "\\|")))
  6234. (while (string-match re text)
  6235. (setq text
  6236. (replace-match
  6237. (cdr (assoc (string-to-char (match-string 0 text))
  6238. table))
  6239. t t text)))
  6240. text)))
  6241. (defun org-link-unescape (text &optional table)
  6242. "Reverse the action of `org-link-escape'."
  6243. (setq table (or table org-link-escape-chars))
  6244. (when text
  6245. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6246. table "\\|")))
  6247. (while (string-match re text)
  6248. (setq text
  6249. (replace-match
  6250. (char-to-string (car (rassoc (match-string 0 text) table)))
  6251. t t text)))
  6252. text)))
  6253. (defun org-xor (a b)
  6254. "Exclusive or."
  6255. (if a (not b) b))
  6256. (defun org-get-header (header)
  6257. "Find a header field in the current buffer."
  6258. (save-excursion
  6259. (goto-char (point-min))
  6260. (let ((case-fold-search t) s)
  6261. (cond
  6262. ((eq header 'from)
  6263. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6264. (setq s (match-string 1)))
  6265. (while (string-match "\"" s)
  6266. (setq s (replace-match "" t t s)))
  6267. (if (string-match "[<(].*" s)
  6268. (setq s (replace-match "" t t s))))
  6269. ((eq header 'message-id)
  6270. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6271. (setq s (match-string 1))))
  6272. ((eq header 'subject)
  6273. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6274. (setq s (match-string 1)))))
  6275. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6276. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6277. s)))
  6278. (defun org-fixup-message-id-for-http (s)
  6279. "Replace special characters in a message id, so it can be used in an http query."
  6280. (while (string-match "<" s)
  6281. (setq s (replace-match "%3C" t t s)))
  6282. (while (string-match ">" s)
  6283. (setq s (replace-match "%3E" t t s)))
  6284. (while (string-match "@" s)
  6285. (setq s (replace-match "%40" t t s)))
  6286. s)
  6287. ;;;###autoload
  6288. (defun org-insert-link-global ()
  6289. "Insert a link like Org-mode does.
  6290. This command can be called in any mode to insert a link in Org-mode syntax."
  6291. (interactive)
  6292. (org-load-modules-maybe)
  6293. (org-run-like-in-org-mode 'org-insert-link))
  6294. (defun org-insert-link (&optional complete-file link-location)
  6295. "Insert a link. At the prompt, enter the link.
  6296. Completion can be used to select a link previously stored with
  6297. `org-store-link'. When the empty string is entered (i.e. if you just
  6298. press RET at the prompt), the link defaults to the most recently
  6299. stored link. As SPC triggers completion in the minibuffer, you need to
  6300. use M-SPC or C-q SPC to force the insertion of a space character.
  6301. You will also be prompted for a description, and if one is given, it will
  6302. be displayed in the buffer instead of the link.
  6303. If there is already a link at point, this command will allow you to edit link
  6304. and description parts.
  6305. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6306. be selected using completion. The path to the file will be relative to the
  6307. current directory if the file is in the current directory or a subdirectory.
  6308. Otherwise, the link will be the absolute path as completed in the minibuffer
  6309. \(i.e. normally ~/path/to/file).
  6310. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6311. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6312. of `org-keep-stored-link-after-insertion'.
  6313. If `org-make-link-description-function' is non-nil, this function will be
  6314. called with the link target, and the result will be the default
  6315. link description.
  6316. If the LINK-LOCATION parameter is non-nil, this value will be
  6317. used as the link location instead of reading one interactively."
  6318. (interactive "P")
  6319. (let* ((wcf (current-window-configuration))
  6320. (region (if (org-region-active-p)
  6321. (buffer-substring (region-beginning) (region-end))))
  6322. (remove (and region (list (region-beginning) (region-end))))
  6323. (desc region)
  6324. tmphist ; byte-compile incorrectly complains about this
  6325. (link link-location)
  6326. entry file)
  6327. (cond
  6328. (link-location) ; specified by arg, just use it.
  6329. ((org-in-regexp org-bracket-link-regexp 1)
  6330. ;; We do have a link at point, and we are going to edit it.
  6331. (setq remove (list (match-beginning 0) (match-end 0)))
  6332. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6333. (setq link (read-string "Link: "
  6334. (org-link-unescape
  6335. (org-match-string-no-properties 1)))))
  6336. ((or (org-in-regexp org-angle-link-re)
  6337. (org-in-regexp org-plain-link-re))
  6338. ;; Convert to bracket link
  6339. (setq remove (list (match-beginning 0) (match-end 0))
  6340. link (read-string "Link: "
  6341. (org-remove-angle-brackets (match-string 0)))))
  6342. ((equal complete-file '(4))
  6343. ;; Completing read for file names.
  6344. (setq file (read-file-name "File: "))
  6345. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6346. (pwd1 (file-name-as-directory (abbreviate-file-name
  6347. (expand-file-name ".")))))
  6348. (cond
  6349. ((equal complete-file '(16))
  6350. (setq link (org-make-link
  6351. "file:"
  6352. (abbreviate-file-name (expand-file-name file)))))
  6353. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6354. (setq link (org-make-link "file:" (match-string 1 file))))
  6355. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6356. (expand-file-name file))
  6357. (setq link (org-make-link
  6358. "file:" (match-string 1 (expand-file-name file)))))
  6359. (t (setq link (org-make-link "file:" file))))))
  6360. (t
  6361. ;; Read link, with completion for stored links.
  6362. (with-output-to-temp-buffer "*Org Links*"
  6363. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6364. (when org-stored-links
  6365. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6366. (princ (mapconcat
  6367. (lambda (x)
  6368. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6369. (reverse org-stored-links) "\n"))))
  6370. (let ((cw (selected-window)))
  6371. (select-window (get-buffer-window "*Org Links*"))
  6372. (shrink-window-if-larger-than-buffer)
  6373. (setq truncate-lines t)
  6374. (select-window cw))
  6375. ;; Fake a link history, containing the stored links.
  6376. (setq tmphist (append (mapcar 'car org-stored-links)
  6377. org-insert-link-history))
  6378. (unwind-protect
  6379. (setq link (org-completing-read
  6380. "Link: "
  6381. (append
  6382. (mapcar (lambda (x) (list (concat (car x) ":")))
  6383. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6384. (mapcar (lambda (x) (list (concat x ":")))
  6385. org-link-types))
  6386. nil nil nil
  6387. 'tmphist
  6388. (or (car (car org-stored-links)))))
  6389. (set-window-configuration wcf)
  6390. (kill-buffer "*Org Links*"))
  6391. (setq entry (assoc link org-stored-links))
  6392. (or entry (push link org-insert-link-history))
  6393. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6394. (not org-keep-stored-link-after-insertion))
  6395. (setq org-stored-links (delq (assoc link org-stored-links)
  6396. org-stored-links)))
  6397. (setq desc (or desc (nth 1 entry)))))
  6398. (if (string-match org-plain-link-re link)
  6399. ;; URL-like link, normalize the use of angular brackets.
  6400. (setq link (org-make-link (org-remove-angle-brackets link))))
  6401. ;; Check if we are linking to the current file with a search option
  6402. ;; If yes, simplify the link by using only the search option.
  6403. (when (and buffer-file-name
  6404. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6405. (let* ((path (match-string 1 link))
  6406. (case-fold-search nil)
  6407. (search (match-string 2 link)))
  6408. (save-match-data
  6409. (if (equal (file-truename buffer-file-name) (file-truename path))
  6410. ;; We are linking to this same file, with a search option
  6411. (setq link search)))))
  6412. ;; Check if we can/should use a relative path. If yes, simplify the link
  6413. (when (string-match "\\<file:\\(.*\\)" link)
  6414. (let* ((path (match-string 1 link))
  6415. (origpath path)
  6416. (case-fold-search nil))
  6417. (cond
  6418. ((eq org-link-file-path-type 'absolute)
  6419. (setq path (abbreviate-file-name (expand-file-name path))))
  6420. ((eq org-link-file-path-type 'noabbrev)
  6421. (setq path (expand-file-name path)))
  6422. ((eq org-link-file-path-type 'relative)
  6423. (setq path (file-relative-name path)))
  6424. (t
  6425. (save-match-data
  6426. (if (string-match (concat "^" (regexp-quote
  6427. (file-name-as-directory
  6428. (expand-file-name "."))))
  6429. (expand-file-name path))
  6430. ;; We are linking a file with relative path name.
  6431. (setq path (substring (expand-file-name path)
  6432. (match-end 0)))))))
  6433. (setq link (concat "file:" path))
  6434. (if (equal desc origpath)
  6435. (setq desc path))))
  6436. (if org-make-link-description-function
  6437. (setq desc (funcall org-make-link-description-function link desc)))
  6438. (setq desc (read-string "Description: " desc))
  6439. (unless (string-match "\\S-" desc) (setq desc nil))
  6440. (if remove (apply 'delete-region remove))
  6441. (insert (org-make-link-string link desc))))
  6442. (defun org-completing-read (&rest args)
  6443. (let ((minibuffer-local-completion-map
  6444. (copy-keymap minibuffer-local-completion-map)))
  6445. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6446. (apply 'completing-read args)))
  6447. (defun org-extract-attributes (s)
  6448. "Extract the attributes cookie from a string and set as text property."
  6449. (let (a attr (start 0) key value)
  6450. (save-match-data
  6451. (when (string-match "{{\\([^}]+\\)}}$" s)
  6452. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6453. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6454. (setq key (match-string 1 a) value (match-string 2 a)
  6455. start (match-end 0)
  6456. attr (plist-put attr (intern key) value))))
  6457. (org-add-props s nil 'org-attributes attr))
  6458. s))
  6459. (defun org-attributes-to-string (plist)
  6460. "Format a property list into an HTML attribute list."
  6461. (let ((s "") key value)
  6462. (while plist
  6463. (setq key (pop plist) value (pop plist))
  6464. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  6465. s))
  6466. ;;; Opening/following a link
  6467. (defvar org-link-search-failed nil)
  6468. (defun org-next-link ()
  6469. "Move forward to the next link.
  6470. If the link is in hidden text, expose it."
  6471. (interactive)
  6472. (when (and org-link-search-failed (eq this-command last-command))
  6473. (goto-char (point-min))
  6474. (message "Link search wrapped back to beginning of buffer"))
  6475. (setq org-link-search-failed nil)
  6476. (let* ((pos (point))
  6477. (ct (org-context))
  6478. (a (assoc :link ct)))
  6479. (if a (goto-char (nth 2 a)))
  6480. (if (re-search-forward org-any-link-re nil t)
  6481. (progn
  6482. (goto-char (match-beginning 0))
  6483. (if (org-invisible-p) (org-show-context)))
  6484. (goto-char pos)
  6485. (setq org-link-search-failed t)
  6486. (error "No further link found"))))
  6487. (defun org-previous-link ()
  6488. "Move backward to the previous link.
  6489. If the link is in hidden text, expose it."
  6490. (interactive)
  6491. (when (and org-link-search-failed (eq this-command last-command))
  6492. (goto-char (point-max))
  6493. (message "Link search wrapped back to end of buffer"))
  6494. (setq org-link-search-failed nil)
  6495. (let* ((pos (point))
  6496. (ct (org-context))
  6497. (a (assoc :link ct)))
  6498. (if a (goto-char (nth 1 a)))
  6499. (if (re-search-backward org-any-link-re nil t)
  6500. (progn
  6501. (goto-char (match-beginning 0))
  6502. (if (org-invisible-p) (org-show-context)))
  6503. (goto-char pos)
  6504. (setq org-link-search-failed t)
  6505. (error "No further link found"))))
  6506. (defun org-find-file-at-mouse (ev)
  6507. "Open file link or URL at mouse."
  6508. (interactive "e")
  6509. (mouse-set-point ev)
  6510. (org-open-at-point 'in-emacs))
  6511. (defun org-open-at-mouse (ev)
  6512. "Open file link or URL at mouse."
  6513. (interactive "e")
  6514. (mouse-set-point ev)
  6515. (org-open-at-point))
  6516. (defvar org-window-config-before-follow-link nil
  6517. "The window configuration before following a link.
  6518. This is saved in case the need arises to restore it.")
  6519. (defvar org-open-link-marker (make-marker)
  6520. "Marker pointing to the location where `org-open-at-point; was called.")
  6521. ;;;###autoload
  6522. (defun org-open-at-point-global ()
  6523. "Follow a link like Org-mode does.
  6524. This command can be called in any mode to follow a link that has
  6525. Org-mode syntax."
  6526. (interactive)
  6527. (org-run-like-in-org-mode 'org-open-at-point))
  6528. ;;;###autoload
  6529. (defun org-open-link-from-string (s &optional arg)
  6530. "Open a link in the string S, as if it was in Org-mode."
  6531. (interactive "sLink: \nP")
  6532. (with-temp-buffer
  6533. (let ((org-inhibit-startup t))
  6534. (org-mode)
  6535. (insert s)
  6536. (goto-char (point-min))
  6537. (org-open-at-point arg))))
  6538. (defun org-open-at-point (&optional in-emacs)
  6539. "Open link at or after point.
  6540. If there is no link at point, this function will search forward up to
  6541. the end of the current subtree.
  6542. Normally, files will be opened by an appropriate application. If the
  6543. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6544. (interactive "P")
  6545. (org-load-modules-maybe)
  6546. (move-marker org-open-link-marker (point))
  6547. (setq org-window-config-before-follow-link (current-window-configuration))
  6548. (org-remove-occur-highlights nil nil t)
  6549. (if (org-at-timestamp-p t)
  6550. (org-follow-timestamp-link)
  6551. (let (type path link line search (pos (point)))
  6552. (catch 'match
  6553. (save-excursion
  6554. (skip-chars-forward "^]\n\r")
  6555. (when (org-in-regexp org-bracket-link-regexp)
  6556. (setq link (org-extract-attributes
  6557. (org-link-unescape (org-match-string-no-properties 1))))
  6558. (while (string-match " *\n *" link)
  6559. (setq link (replace-match " " t t link)))
  6560. (setq link (org-link-expand-abbrev link))
  6561. (cond
  6562. ((or (file-name-absolute-p link)
  6563. (string-match "^\\.\\.?/" link))
  6564. (setq type "file" path link))
  6565. ((string-match org-link-re-with-space2 link)
  6566. (setq type (match-string 1 link) path (match-string 2 link)))
  6567. (t (setq type "thisfile" path link)))
  6568. (throw 'match t)))
  6569. (when (get-text-property (point) 'org-linked-text)
  6570. (setq type "thisfile"
  6571. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6572. (1+ (point)) (point))
  6573. path (buffer-substring
  6574. (previous-single-property-change pos 'org-linked-text)
  6575. (next-single-property-change pos 'org-linked-text)))
  6576. (throw 'match t))
  6577. (save-excursion
  6578. (when (or (org-in-regexp org-angle-link-re)
  6579. (org-in-regexp org-plain-link-re))
  6580. (setq type (match-string 1) path (match-string 2))
  6581. (throw 'match t)))
  6582. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6583. (setq type "tree-match"
  6584. path (match-string 1))
  6585. (throw 'match t))
  6586. (save-excursion
  6587. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6588. (setq type "tags"
  6589. path (match-string 1))
  6590. (while (string-match ":" path)
  6591. (setq path (replace-match "+" t t path)))
  6592. (throw 'match t))))
  6593. (unless path
  6594. (error "No link found"))
  6595. ;; Remove any trailing spaces in path
  6596. (if (string-match " +\\'" path)
  6597. (setq path (replace-match "" t t path)))
  6598. (cond
  6599. ((assoc type org-link-protocols)
  6600. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6601. ((equal type "mailto")
  6602. (let ((cmd (car org-link-mailto-program))
  6603. (args (cdr org-link-mailto-program)) args1
  6604. (address path) (subject "") a)
  6605. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6606. (setq address (match-string 1 path)
  6607. subject (org-link-escape (match-string 2 path))))
  6608. (while args
  6609. (cond
  6610. ((not (stringp (car args))) (push (pop args) args1))
  6611. (t (setq a (pop args))
  6612. (if (string-match "%a" a)
  6613. (setq a (replace-match address t t a)))
  6614. (if (string-match "%s" a)
  6615. (setq a (replace-match subject t t a)))
  6616. (push a args1))))
  6617. (apply cmd (nreverse args1))))
  6618. ((member type '("http" "https" "ftp" "news"))
  6619. (browse-url (concat type ":" (org-link-escape
  6620. path org-link-escape-chars-browser))))
  6621. ((member type '("message"))
  6622. (browse-url (concat type ":" path)))
  6623. ((string= type "tags")
  6624. (org-tags-view in-emacs path))
  6625. ((string= type "thisfile")
  6626. (if in-emacs
  6627. (switch-to-buffer-other-window
  6628. (org-get-buffer-for-internal-link (current-buffer)))
  6629. (org-mark-ring-push))
  6630. (let ((cmd `(org-link-search
  6631. ,path
  6632. ,(cond ((equal in-emacs '(4)) 'occur)
  6633. ((equal in-emacs '(16)) 'org-occur)
  6634. (t nil))
  6635. ,pos)))
  6636. (condition-case nil (eval cmd)
  6637. (error (progn (widen) (eval cmd))))))
  6638. ((string= type "tree-match")
  6639. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6640. ((string= type "file")
  6641. (if (string-match "::\\([0-9]+\\)\\'" path)
  6642. (setq line (string-to-number (match-string 1 path))
  6643. path (substring path 0 (match-beginning 0)))
  6644. (if (string-match "::\\(.+\\)\\'" path)
  6645. (setq search (match-string 1 path)
  6646. path (substring path 0 (match-beginning 0)))))
  6647. (if (string-match "[*?{]" (file-name-nondirectory path))
  6648. (dired path)
  6649. (org-open-file path in-emacs line search)))
  6650. ((string= type "news")
  6651. (require 'org-gnus)
  6652. (org-gnus-follow-link path))
  6653. ((string= type "shell")
  6654. (let ((cmd path))
  6655. (if (or (not org-confirm-shell-link-function)
  6656. (funcall org-confirm-shell-link-function
  6657. (format "Execute \"%s\" in shell? "
  6658. (org-add-props cmd nil
  6659. 'face 'org-warning))))
  6660. (progn
  6661. (message "Executing %s" cmd)
  6662. (shell-command cmd))
  6663. (error "Abort"))))
  6664. ((string= type "elisp")
  6665. (let ((cmd path))
  6666. (if (or (not org-confirm-elisp-link-function)
  6667. (funcall org-confirm-elisp-link-function
  6668. (format "Execute \"%s\" as elisp? "
  6669. (org-add-props cmd nil
  6670. 'face 'org-warning))))
  6671. (message "%s => %s" cmd (eval (read cmd)))
  6672. (error "Abort"))))
  6673. (t
  6674. (browse-url-at-point)))))
  6675. (move-marker org-open-link-marker nil)
  6676. (run-hook-with-args 'org-follow-link-hook))
  6677. ;;;; Time estimates
  6678. (defun org-get-effort (&optional pom)
  6679. "Get the effort estimate for the current entry."
  6680. (org-entry-get pom org-effort-property))
  6681. ;;; File search
  6682. (defvar org-create-file-search-functions nil
  6683. "List of functions to construct the right search string for a file link.
  6684. These functions are called in turn with point at the location to
  6685. which the link should point.
  6686. A function in the hook should first test if it would like to
  6687. handle this file type, for example by checking the major-mode or
  6688. the file extension. If it decides not to handle this file, it
  6689. should just return nil to give other functions a chance. If it
  6690. does handle the file, it must return the search string to be used
  6691. when following the link. The search string will be part of the
  6692. file link, given after a double colon, and `org-open-at-point'
  6693. will automatically search for it. If special measures must be
  6694. taken to make the search successful, another function should be
  6695. added to the companion hook `org-execute-file-search-functions',
  6696. which see.
  6697. A function in this hook may also use `setq' to set the variable
  6698. `description' to provide a suggestion for the descriptive text to
  6699. be used for this link when it gets inserted into an Org-mode
  6700. buffer with \\[org-insert-link].")
  6701. (defvar org-execute-file-search-functions nil
  6702. "List of functions to execute a file search triggered by a link.
  6703. Functions added to this hook must accept a single argument, the
  6704. search string that was part of the file link, the part after the
  6705. double colon. The function must first check if it would like to
  6706. handle this search, for example by checking the major-mode or the
  6707. file extension. If it decides not to handle this search, it
  6708. should just return nil to give other functions a chance. If it
  6709. does handle the search, it must return a non-nil value to keep
  6710. other functions from trying.
  6711. Each function can access the current prefix argument through the
  6712. variable `current-prefix-argument'. Note that a single prefix is
  6713. used to force opening a link in Emacs, so it may be good to only
  6714. use a numeric or double prefix to guide the search function.
  6715. In case this is needed, a function in this hook can also restore
  6716. the window configuration before `org-open-at-point' was called using:
  6717. (set-window-configuration org-window-config-before-follow-link)")
  6718. (defun org-link-search (s &optional type avoid-pos)
  6719. "Search for a link search option.
  6720. If S is surrounded by forward slashes, it is interpreted as a
  6721. regular expression. In org-mode files, this will create an `org-occur'
  6722. sparse tree. In ordinary files, `occur' will be used to list matches.
  6723. If the current buffer is in `dired-mode', grep will be used to search
  6724. in all files. If AVOID-POS is given, ignore matches near that position."
  6725. (let ((case-fold-search t)
  6726. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6727. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6728. (append '(("") (" ") ("\t") ("\n"))
  6729. org-emphasis-alist)
  6730. "\\|") "\\)"))
  6731. (pos (point))
  6732. (pre nil) (post nil)
  6733. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6734. (cond
  6735. ;; First check if there are any special
  6736. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6737. ;; Now try the builtin stuff
  6738. ((save-excursion
  6739. (goto-char (point-min))
  6740. (and
  6741. (re-search-forward
  6742. (concat "<<" (regexp-quote s0) ">>") nil t)
  6743. (setq type 'dedicated
  6744. pos (match-beginning 0))))
  6745. ;; There is an exact target for this
  6746. (goto-char pos))
  6747. ((string-match "^/\\(.*\\)/$" s)
  6748. ;; A regular expression
  6749. (cond
  6750. ((org-mode-p)
  6751. (org-occur (match-string 1 s)))
  6752. ;;((eq major-mode 'dired-mode)
  6753. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6754. (t (org-do-occur (match-string 1 s)))))
  6755. (t
  6756. ;; A normal search strings
  6757. (when (equal (string-to-char s) ?*)
  6758. ;; Anchor on headlines, post may include tags.
  6759. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6760. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6761. s (substring s 1)))
  6762. (remove-text-properties
  6763. 0 (length s)
  6764. '(face nil mouse-face nil keymap nil fontified nil) s)
  6765. ;; Make a series of regular expressions to find a match
  6766. (setq words (org-split-string s "[ \n\r\t]+")
  6767. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6768. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6769. "\\)" markers)
  6770. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6771. re2a (concat "[ \t\r\n]" re2a_)
  6772. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6773. re4 (concat "[^a-zA-Z_]" re4_)
  6774. re1 (concat pre re2 post)
  6775. re3 (concat pre (if pre re4_ re4) post)
  6776. re5 (concat pre ".*" re4)
  6777. re2 (concat pre re2)
  6778. re2a (concat pre (if pre re2a_ re2a))
  6779. re4 (concat pre (if pre re4_ re4))
  6780. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6781. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6782. re5 "\\)"
  6783. ))
  6784. (cond
  6785. ((eq type 'org-occur) (org-occur reall))
  6786. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6787. (t (goto-char (point-min))
  6788. (setq type 'fuzzy)
  6789. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6790. (org-search-not-self 1 re1 nil t)
  6791. (org-search-not-self 1 re2 nil t)
  6792. (org-search-not-self 1 re2a nil t)
  6793. (org-search-not-self 1 re3 nil t)
  6794. (org-search-not-self 1 re4 nil t)
  6795. (org-search-not-self 1 re5 nil t)
  6796. )
  6797. (goto-char (match-beginning 1))
  6798. (goto-char pos)
  6799. (error "No match")))))
  6800. (t
  6801. ;; Normal string-search
  6802. (goto-char (point-min))
  6803. (if (search-forward s nil t)
  6804. (goto-char (match-beginning 0))
  6805. (error "No match"))))
  6806. (and (org-mode-p) (org-show-context 'link-search))
  6807. type))
  6808. (defun org-search-not-self (group &rest args)
  6809. "Execute `re-search-forward', but only accept matches that do not
  6810. enclose the position of `org-open-link-marker'."
  6811. (let ((m org-open-link-marker))
  6812. (catch 'exit
  6813. (while (apply 're-search-forward args)
  6814. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6815. (goto-char (match-end group))
  6816. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6817. (> (match-beginning 0) (marker-position m))
  6818. (< (match-end 0) (marker-position m)))
  6819. (save-match-data
  6820. (or (not (org-in-regexp
  6821. org-bracket-link-analytic-regexp 1))
  6822. (not (match-end 4)) ; no description
  6823. (and (<= (match-beginning 4) (point))
  6824. (>= (match-end 4) (point))))))
  6825. (throw 'exit (point))))))))
  6826. (defun org-get-buffer-for-internal-link (buffer)
  6827. "Return a buffer to be used for displaying the link target of internal links."
  6828. (cond
  6829. ((not org-display-internal-link-with-indirect-buffer)
  6830. buffer)
  6831. ((string-match "(Clone)$" (buffer-name buffer))
  6832. (message "Buffer is already a clone, not making another one")
  6833. ;; we also do not modify visibility in this case
  6834. buffer)
  6835. (t ; make a new indirect buffer for displaying the link
  6836. (let* ((bn (buffer-name buffer))
  6837. (ibn (concat bn "(Clone)"))
  6838. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6839. (with-current-buffer ib (org-overview))
  6840. ib))))
  6841. (defun org-do-occur (regexp &optional cleanup)
  6842. "Call the Emacs command `occur'.
  6843. If CLEANUP is non-nil, remove the printout of the regular expression
  6844. in the *Occur* buffer. This is useful if the regex is long and not useful
  6845. to read."
  6846. (occur regexp)
  6847. (when cleanup
  6848. (let ((cwin (selected-window)) win beg end)
  6849. (when (setq win (get-buffer-window "*Occur*"))
  6850. (select-window win))
  6851. (goto-char (point-min))
  6852. (when (re-search-forward "match[a-z]+" nil t)
  6853. (setq beg (match-end 0))
  6854. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6855. (setq end (1- (match-beginning 0)))))
  6856. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6857. (goto-char (point-min))
  6858. (select-window cwin))))
  6859. ;;; The mark ring for links jumps
  6860. (defvar org-mark-ring nil
  6861. "Mark ring for positions before jumps in Org-mode.")
  6862. (defvar org-mark-ring-last-goto nil
  6863. "Last position in the mark ring used to go back.")
  6864. ;; Fill and close the ring
  6865. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6866. (loop for i from 1 to org-mark-ring-length do
  6867. (push (make-marker) org-mark-ring))
  6868. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6869. org-mark-ring)
  6870. (defun org-mark-ring-push (&optional pos buffer)
  6871. "Put the current position or POS into the mark ring and rotate it."
  6872. (interactive)
  6873. (setq pos (or pos (point)))
  6874. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6875. (move-marker (car org-mark-ring)
  6876. (or pos (point))
  6877. (or buffer (current-buffer)))
  6878. (message "%s"
  6879. (substitute-command-keys
  6880. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6881. (defun org-mark-ring-goto (&optional n)
  6882. "Jump to the previous position in the mark ring.
  6883. With prefix arg N, jump back that many stored positions. When
  6884. called several times in succession, walk through the entire ring.
  6885. Org-mode commands jumping to a different position in the current file,
  6886. or to another Org-mode file, automatically push the old position
  6887. onto the ring."
  6888. (interactive "p")
  6889. (let (p m)
  6890. (if (eq last-command this-command)
  6891. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6892. (setq p org-mark-ring))
  6893. (setq org-mark-ring-last-goto p)
  6894. (setq m (car p))
  6895. (switch-to-buffer (marker-buffer m))
  6896. (goto-char m)
  6897. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6898. (defun org-remove-angle-brackets (s)
  6899. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6900. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6901. s)
  6902. (defun org-add-angle-brackets (s)
  6903. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6904. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6905. s)
  6906. (defun org-remove-double-quotes (s)
  6907. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6908. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6909. s)
  6910. ;;; Following specific links
  6911. (defun org-follow-timestamp-link ()
  6912. (cond
  6913. ((org-at-date-range-p t)
  6914. (let ((org-agenda-start-on-weekday)
  6915. (t1 (match-string 1))
  6916. (t2 (match-string 2)))
  6917. (setq t1 (time-to-days (org-time-string-to-time t1))
  6918. t2 (time-to-days (org-time-string-to-time t2)))
  6919. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6920. ((org-at-timestamp-p t)
  6921. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6922. (substring (match-string 1) 0 10)))
  6923. 1))
  6924. (t (error "This should not happen"))))
  6925. ;;; Following file links
  6926. (defvar org-wait nil)
  6927. (defun org-open-file (path &optional in-emacs line search)
  6928. "Open the file at PATH.
  6929. First, this expands any special file name abbreviations. Then the
  6930. configuration variable `org-file-apps' is checked if it contains an
  6931. entry for this file type, and if yes, the corresponding command is launched.
  6932. If no application is found, Emacs simply visits the file.
  6933. With optional argument IN-EMACS, Emacs will visit the file.
  6934. Optional LINE specifies a line to go to, optional SEARCH a string to
  6935. search for. If LINE or SEARCH is given, the file will always be
  6936. opened in Emacs.
  6937. If the file does not exist, an error is thrown."
  6938. (setq in-emacs (or in-emacs line search))
  6939. (let* ((file (if (equal path "")
  6940. buffer-file-name
  6941. (substitute-in-file-name (expand-file-name path))))
  6942. (apps (append org-file-apps (org-default-apps)))
  6943. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6944. (dirp (if remp nil (file-directory-p file)))
  6945. (file (if (and dirp org-open-directory-means-index-dot-org)
  6946. (concat (file-name-as-directory file) "index.org")
  6947. file))
  6948. (dfile (downcase file))
  6949. (old-buffer (current-buffer))
  6950. (old-pos (point))
  6951. (old-mode major-mode)
  6952. ext cmd)
  6953. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6954. (setq ext (match-string 1 dfile))
  6955. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6956. (setq ext (match-string 1 dfile))))
  6957. (if in-emacs
  6958. (setq cmd 'emacs)
  6959. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6960. (and dirp (cdr (assoc 'directory apps)))
  6961. (cdr (assoc ext apps))
  6962. (cdr (assoc t apps)))))
  6963. (when (eq cmd 'mailcap)
  6964. (require 'mailcap)
  6965. (mailcap-parse-mailcaps)
  6966. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6967. (command (mailcap-mime-info mime-type)))
  6968. (if (stringp command)
  6969. (setq cmd command)
  6970. (setq cmd 'emacs))))
  6971. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6972. (not (file-exists-p file))
  6973. (not org-open-non-existing-files))
  6974. (error "No such file: %s" file))
  6975. (cond
  6976. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6977. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6978. (while (string-match "['\"]%s['\"]" cmd)
  6979. (setq cmd (replace-match "%s" t t cmd)))
  6980. (while (string-match "%s" cmd)
  6981. (setq cmd (replace-match
  6982. (save-match-data
  6983. (shell-quote-argument
  6984. (convert-standard-filename file)))
  6985. t t cmd)))
  6986. (save-window-excursion
  6987. (start-process-shell-command cmd nil cmd)
  6988. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6989. ))
  6990. ((or (stringp cmd)
  6991. (eq cmd 'emacs))
  6992. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6993. (widen)
  6994. (if line (goto-line line)
  6995. (if search (org-link-search search))))
  6996. ((consp cmd)
  6997. (let ((file (convert-standard-filename file)))
  6998. (eval cmd)))
  6999. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7000. (and (org-mode-p) (eq old-mode 'org-mode)
  7001. (or (not (equal old-buffer (current-buffer)))
  7002. (not (equal old-pos (point))))
  7003. (org-mark-ring-push old-pos old-buffer))))
  7004. (defun org-default-apps ()
  7005. "Return the default applications for this operating system."
  7006. (cond
  7007. ((eq system-type 'darwin)
  7008. org-file-apps-defaults-macosx)
  7009. ((eq system-type 'windows-nt)
  7010. org-file-apps-defaults-windowsnt)
  7011. (t org-file-apps-defaults-gnu)))
  7012. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7013. (defun org-file-remote-p (file)
  7014. "Test whether FILE specifies a location on a remote system.
  7015. Return non-nil if the location is indeed remote.
  7016. For example, the filename \"/user@host:/foo\" specifies a location
  7017. on the system \"/user@host:\"."
  7018. (cond ((fboundp 'file-remote-p)
  7019. (file-remote-p file))
  7020. ((fboundp 'tramp-handle-file-remote-p)
  7021. (tramp-handle-file-remote-p file))
  7022. ((and (boundp 'ange-ftp-name-format)
  7023. (string-match (car ange-ftp-name-format) file))
  7024. t)
  7025. (t nil)))
  7026. ;;;; Refiling
  7027. (defun org-get-org-file ()
  7028. "Read a filename, with default directory `org-directory'."
  7029. (let ((default (or org-default-notes-file remember-data-file)))
  7030. (read-file-name (format "File name [%s]: " default)
  7031. (file-name-as-directory org-directory)
  7032. default)))
  7033. (defun org-notes-order-reversed-p ()
  7034. "Check if the current file should receive notes in reversed order."
  7035. (cond
  7036. ((not org-reverse-note-order) nil)
  7037. ((eq t org-reverse-note-order) t)
  7038. ((not (listp org-reverse-note-order)) nil)
  7039. (t (catch 'exit
  7040. (let ((all org-reverse-note-order)
  7041. entry)
  7042. (while (setq entry (pop all))
  7043. (if (string-match (car entry) buffer-file-name)
  7044. (throw 'exit (cdr entry))))
  7045. nil)))))
  7046. (defvar org-refile-target-table nil
  7047. "The list of refile targets, created by `org-refile'.")
  7048. (defvar org-agenda-new-buffers nil
  7049. "Buffers created to visit agenda files.")
  7050. (defun org-get-refile-targets (&optional default-buffer)
  7051. "Produce a table with refile targets."
  7052. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7053. targets txt re files f desc descre)
  7054. (with-current-buffer (or default-buffer (current-buffer))
  7055. (while (setq entry (pop entries))
  7056. (setq files (car entry) desc (cdr entry))
  7057. (cond
  7058. ((null files) (setq files (list (current-buffer))))
  7059. ((eq files 'org-agenda-files)
  7060. (setq files (org-agenda-files 'unrestricted)))
  7061. ((and (symbolp files) (fboundp files))
  7062. (setq files (funcall files)))
  7063. ((and (symbolp files) (boundp files))
  7064. (setq files (symbol-value files))))
  7065. (if (stringp files) (setq files (list files)))
  7066. (cond
  7067. ((eq (car desc) :tag)
  7068. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7069. ((eq (car desc) :todo)
  7070. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7071. ((eq (car desc) :regexp)
  7072. (setq descre (cdr desc)))
  7073. ((eq (car desc) :level)
  7074. (setq descre (concat "^\\*\\{" (number-to-string
  7075. (if org-odd-levels-only
  7076. (1- (* 2 (cdr desc)))
  7077. (cdr desc)))
  7078. "\\}[ \t]")))
  7079. ((eq (car desc) :maxlevel)
  7080. (setq descre (concat "^\\*\\{1," (number-to-string
  7081. (if org-odd-levels-only
  7082. (1- (* 2 (cdr desc)))
  7083. (cdr desc)))
  7084. "\\}[ \t]")))
  7085. (t (error "Bad refiling target description %s" desc)))
  7086. (while (setq f (pop files))
  7087. (save-excursion
  7088. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7089. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7090. (save-excursion
  7091. (save-restriction
  7092. (widen)
  7093. (goto-char (point-min))
  7094. (while (re-search-forward descre nil t)
  7095. (goto-char (point-at-bol))
  7096. (when (looking-at org-complex-heading-regexp)
  7097. (setq txt (match-string 4)
  7098. re (concat "^" (regexp-quote
  7099. (buffer-substring (match-beginning 1)
  7100. (match-end 4)))))
  7101. (if (match-end 5) (setq re (concat re "[ \t]+"
  7102. (regexp-quote
  7103. (match-string 5)))))
  7104. (setq re (concat re "[ \t]*$"))
  7105. (when org-refile-use-outline-path
  7106. (setq txt (mapconcat 'identity
  7107. (append
  7108. (if (eq org-refile-use-outline-path 'file)
  7109. (list (file-name-nondirectory
  7110. (buffer-file-name (buffer-base-buffer))))
  7111. (if (eq org-refile-use-outline-path 'full-file-path)
  7112. (list (buffer-file-name (buffer-base-buffer)))))
  7113. (org-get-outline-path)
  7114. (list txt))
  7115. "/")))
  7116. (push (list txt f re (point)) targets))
  7117. (goto-char (point-at-eol))))))))
  7118. (nreverse targets))))
  7119. (defun org-get-outline-path ()
  7120. "Return the outline path to the current entry, as a list."
  7121. (let (rtn)
  7122. (save-excursion
  7123. (while (org-up-heading-safe)
  7124. (when (looking-at org-complex-heading-regexp)
  7125. (push (org-match-string-no-properties 4) rtn)))
  7126. rtn)))
  7127. (defvar org-refile-history nil
  7128. "History for refiling operations.")
  7129. (defun org-refile (&optional goto default-buffer)
  7130. "Move the entry at point to another heading.
  7131. The list of target headings is compiled using the information in
  7132. `org-refile-targets', which see. This list is created before each use
  7133. and will therefore always be up-to-date.
  7134. At the target location, the entry is filed as a subitem of the target heading.
  7135. Depending on `org-reverse-note-order', the new subitem will either be the
  7136. first of the last subitem.
  7137. With prefix arg GOTO, the command will only visit the target location,
  7138. not actually move anything.
  7139. With a double prefix `C-u C-u', go to the location where the last refiling
  7140. operation has put the subtree."
  7141. (interactive "P")
  7142. (let* ((cbuf (current-buffer))
  7143. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7144. pos it nbuf file re level reversed)
  7145. (if (equal goto '(16))
  7146. (org-refile-goto-last-stored)
  7147. (when (setq it (org-refile-get-location
  7148. (if goto "Goto: " "Refile to: ") default-buffer))
  7149. (setq file (nth 1 it)
  7150. re (nth 2 it)
  7151. pos (nth 3 it))
  7152. (setq nbuf (or (find-buffer-visiting file)
  7153. (find-file-noselect file)))
  7154. (if goto
  7155. (progn
  7156. (switch-to-buffer nbuf)
  7157. (goto-char pos)
  7158. (org-show-context 'org-goto))
  7159. (org-copy-subtree 1 nil t)
  7160. (save-excursion
  7161. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7162. (find-file-noselect file))))
  7163. (setq reversed (org-notes-order-reversed-p))
  7164. (save-excursion
  7165. (save-restriction
  7166. (widen)
  7167. (goto-char pos)
  7168. (looking-at outline-regexp)
  7169. (setq level (org-get-valid-level (funcall outline-level) 1))
  7170. (goto-char
  7171. (if reversed
  7172. (outline-next-heading)
  7173. (or (save-excursion (outline-get-next-sibling))
  7174. (org-end-of-subtree t t)
  7175. (point-max))))
  7176. (bookmark-set "org-refile-last-stored")
  7177. (org-paste-subtree level))))
  7178. (org-cut-subtree)
  7179. (setq org-markers-to-move nil)
  7180. (message "Entry refiled to \"%s\"" (car it)))))))
  7181. (defun org-refile-goto-last-stored ()
  7182. "Go to the location where the last refile was stored."
  7183. (interactive)
  7184. (bookmark-jump "org-refile-last-stored")
  7185. (message "This is the location of the last refile"))
  7186. (defun org-refile-get-location (&optional prompt default-buffer)
  7187. "Prompt the user for a refile location, using PROMPT."
  7188. (let ((org-refile-targets org-refile-targets)
  7189. (org-refile-use-outline-path org-refile-use-outline-path))
  7190. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7191. (unless org-refile-target-table
  7192. (error "No refile targets"))
  7193. (let* ((cbuf (current-buffer))
  7194. (cfunc (if org-refile-use-outline-path
  7195. 'org-olpath-completing-read
  7196. 'completing-read))
  7197. (extra (if org-refile-use-outline-path "/" ""))
  7198. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7199. (fname (and filename (file-truename filename)))
  7200. (tbl (mapcar
  7201. (lambda (x)
  7202. (if (not (equal fname (file-truename (nth 1 x))))
  7203. (cons (concat (car x) extra " ("
  7204. (file-name-nondirectory (nth 1 x)) ")")
  7205. (cdr x))
  7206. (cons (concat (car x) extra) (cdr x))))
  7207. org-refile-target-table))
  7208. (completion-ignore-case t))
  7209. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7210. tbl)))
  7211. (defun org-olpath-completing-read (prompt collection &rest args)
  7212. "Read an outline path like a file name."
  7213. (let ((thetable collection))
  7214. (apply
  7215. 'completing-read prompt
  7216. (lambda (string predicate &optional flag)
  7217. (let (rtn r s f (l (length string)))
  7218. (cond
  7219. ((eq flag nil)
  7220. ;; try completion
  7221. (try-completion string thetable))
  7222. ((eq flag t)
  7223. ;; all-completions
  7224. (setq rtn (all-completions string thetable predicate))
  7225. (mapcar
  7226. (lambda (x)
  7227. (setq r (substring x l))
  7228. (if (string-match " ([^)]*)$" x)
  7229. (setq f (match-string 0 x))
  7230. (setq f ""))
  7231. (if (string-match "/" r)
  7232. (concat string (substring r 0 (match-end 0)) f)
  7233. x))
  7234. rtn))
  7235. ((eq flag 'lambda)
  7236. ;; exact match?
  7237. (assoc string thetable)))
  7238. ))
  7239. args)))
  7240. ;;;; Dynamic blocks
  7241. (defun org-find-dblock (name)
  7242. "Find the first dynamic block with name NAME in the buffer.
  7243. If not found, stay at current position and return nil."
  7244. (let (pos)
  7245. (save-excursion
  7246. (goto-char (point-min))
  7247. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7248. nil t)
  7249. (match-beginning 0))))
  7250. (if pos (goto-char pos))
  7251. pos))
  7252. (defconst org-dblock-start-re
  7253. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7254. "Matches the startline of a dynamic block, with parameters.")
  7255. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7256. "Matches the end of a dyhamic block.")
  7257. (defun org-create-dblock (plist)
  7258. "Create a dynamic block section, with parameters taken from PLIST.
  7259. PLIST must containe a :name entry which is used as name of the block."
  7260. (unless (bolp) (newline))
  7261. (let ((name (plist-get plist :name)))
  7262. (insert "#+BEGIN: " name)
  7263. (while plist
  7264. (if (eq (car plist) :name)
  7265. (setq plist (cddr plist))
  7266. (insert " " (prin1-to-string (pop plist)))))
  7267. (insert "\n\n#+END:\n")
  7268. (beginning-of-line -2)))
  7269. (defun org-prepare-dblock ()
  7270. "Prepare dynamic block for refresh.
  7271. This empties the block, puts the cursor at the insert position and returns
  7272. the property list including an extra property :name with the block name."
  7273. (unless (looking-at org-dblock-start-re)
  7274. (error "Not at a dynamic block"))
  7275. (let* ((begdel (1+ (match-end 0)))
  7276. (name (org-no-properties (match-string 1)))
  7277. (params (append (list :name name)
  7278. (read (concat "(" (match-string 3) ")")))))
  7279. (unless (re-search-forward org-dblock-end-re nil t)
  7280. (error "Dynamic block not terminated"))
  7281. (setq params
  7282. (append params
  7283. (list :content (buffer-substring
  7284. begdel (match-beginning 0)))))
  7285. (delete-region begdel (match-beginning 0))
  7286. (goto-char begdel)
  7287. (open-line 1)
  7288. params))
  7289. (defun org-map-dblocks (&optional command)
  7290. "Apply COMMAND to all dynamic blocks in the current buffer.
  7291. If COMMAND is not given, use `org-update-dblock'."
  7292. (let ((cmd (or command 'org-update-dblock))
  7293. pos)
  7294. (save-excursion
  7295. (goto-char (point-min))
  7296. (while (re-search-forward org-dblock-start-re nil t)
  7297. (goto-char (setq pos (match-beginning 0)))
  7298. (condition-case nil
  7299. (funcall cmd)
  7300. (error (message "Error during update of dynamic block")))
  7301. (goto-char pos)
  7302. (unless (re-search-forward org-dblock-end-re nil t)
  7303. (error "Dynamic block not terminated"))))))
  7304. (defun org-dblock-update (&optional arg)
  7305. "User command for updating dynamic blocks.
  7306. Update the dynamic block at point. With prefix ARG, update all dynamic
  7307. blocks in the buffer."
  7308. (interactive "P")
  7309. (if arg
  7310. (org-update-all-dblocks)
  7311. (or (looking-at org-dblock-start-re)
  7312. (org-beginning-of-dblock))
  7313. (org-update-dblock)))
  7314. (defun org-update-dblock ()
  7315. "Update the dynamic block at point
  7316. This means to empty the block, parse for parameters and then call
  7317. the correct writing function."
  7318. (save-window-excursion
  7319. (let* ((pos (point))
  7320. (line (org-current-line))
  7321. (params (org-prepare-dblock))
  7322. (name (plist-get params :name))
  7323. (cmd (intern (concat "org-dblock-write:" name))))
  7324. (message "Updating dynamic block `%s' at line %d..." name line)
  7325. (funcall cmd params)
  7326. (message "Updating dynamic block `%s' at line %d...done" name line)
  7327. (goto-char pos))))
  7328. (defun org-beginning-of-dblock ()
  7329. "Find the beginning of the dynamic block at point.
  7330. Error if there is no scuh block at point."
  7331. (let ((pos (point))
  7332. beg)
  7333. (end-of-line 1)
  7334. (if (and (re-search-backward org-dblock-start-re nil t)
  7335. (setq beg (match-beginning 0))
  7336. (re-search-forward org-dblock-end-re nil t)
  7337. (> (match-end 0) pos))
  7338. (goto-char beg)
  7339. (goto-char pos)
  7340. (error "Not in a dynamic block"))))
  7341. (defun org-update-all-dblocks ()
  7342. "Update all dynamic blocks in the buffer.
  7343. This function can be used in a hook."
  7344. (when (org-mode-p)
  7345. (org-map-dblocks 'org-update-dblock)))
  7346. ;;;; Completion
  7347. (defconst org-additional-option-like-keywords
  7348. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7349. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7350. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7351. "BEGIN_QUOTE" "END_QUOTE"
  7352. "BEGIN_VERSE" "END_VERSE"
  7353. "BEGIN_SRC" "END_SRC"))
  7354. (defcustom org-structure-template-alist
  7355. '(
  7356. ("s" "#+begin_src ?\n\n#+end_src"
  7357. "<src lang=\"?\">\n\n</src>")
  7358. ("e" "#+begin_example\n?\n#+end_example"
  7359. "<example>\n?\n</example>")
  7360. ("q" "#+begin_quote\n?\n#+end_quote"
  7361. "<quote>\n?\n</quote>")
  7362. ("v" "#+begin_verse\n?\n#+end_verse"
  7363. "<verse>\n?\n/verse>")
  7364. ("l" "#+begin_latex\n?\n#+end_latex"
  7365. "<literal style=\"latex\">\n?\n</literal>")
  7366. ("L" "#+latex: "
  7367. "<literal style=\"latex\">?</literal>")
  7368. ("h" "#+begin_html\n?\n#+end_html"
  7369. "<literal style=\"html\">\n?\n</literal>")
  7370. ("H" "#+html: "
  7371. "<literal style=\"html\">?</literal>")
  7372. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7373. ("A" "#+ascii: ")
  7374. ("i" "#+include %file ?"
  7375. "<include file=%file markup=\"?\">")
  7376. )
  7377. "Structure completion elements.
  7378. This is a list of abbreviation keys and values. The value gets inserted
  7379. it you type @samp{.} followed by the key and then the completion key,
  7380. usually `M-TAB'. %file will be replaced by a file name after prompting
  7381. for the file uning completion.
  7382. There are two templates for each key, the first uses the original Org syntax,
  7383. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7384. the default when the /org-mtags.el/ module has been loaded. See also the
  7385. variable `org-mtags-prefere-muse-templates'.
  7386. This is an experimental feature, it is undecided if it is going to stay in."
  7387. :group 'org-completion
  7388. :type '(repeat
  7389. (string :tag "Key")
  7390. (string :tag "Template")
  7391. (string :tag "Muse Template")))
  7392. (defun org-try-structure-completion ()
  7393. "Try to complete a structure template before point.
  7394. This looks for strings like \"<e\" on an otherwise empty line and
  7395. expands them."
  7396. (let ((l (buffer-substring (point-at-bol) (point)))
  7397. a)
  7398. (when (and (looking-at "[ \t]*$")
  7399. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7400. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7401. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7402. (match-beginning 1)) a)
  7403. t)))
  7404. (defun org-complete-expand-structure-template (start cell)
  7405. "Expand a structure template."
  7406. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7407. (rpl (nth (if musep 2 1) cell)))
  7408. (delete-region start (point))
  7409. (when (string-match "\\`#\\+" rpl)
  7410. (cond
  7411. ((bolp))
  7412. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7413. (delete-region (point-at-bol) (point)))
  7414. (t (newline))))
  7415. (setq start (point))
  7416. (if (string-match "%file" rpl)
  7417. (setq rpl (replace-match
  7418. (concat
  7419. "\""
  7420. (save-match-data
  7421. (abbreviate-file-name (read-file-name "Include file: ")))
  7422. "\"")
  7423. t t rpl)))
  7424. (insert rpl)
  7425. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7426. (defun org-complete (&optional arg)
  7427. "Perform completion on word at point.
  7428. At the beginning of a headline, this completes TODO keywords as given in
  7429. `org-todo-keywords'.
  7430. If the current word is preceded by a backslash, completes the TeX symbols
  7431. that are supported for HTML support.
  7432. If the current word is preceded by \"#+\", completes special words for
  7433. setting file options.
  7434. In the line after \"#+STARTUP:, complete valid keywords.\"
  7435. At all other locations, this simply calls the value of
  7436. `org-completion-fallback-command'."
  7437. (interactive "P")
  7438. (org-without-partial-completion
  7439. (catch 'exit
  7440. (let* ((a nil)
  7441. (end (point))
  7442. (beg1 (save-excursion
  7443. (skip-chars-backward (org-re "[:alnum:]_@"))
  7444. (point)))
  7445. (beg (save-excursion
  7446. (skip-chars-backward "a-zA-Z0-9_:$")
  7447. (point)))
  7448. (confirm (lambda (x) (stringp (car x))))
  7449. (searchhead (equal (char-before beg) ?*))
  7450. (struct
  7451. (when (and (member (char-before beg1) '(?. ?<))
  7452. (setq a (assoc (buffer-substring beg1 (point))
  7453. org-structure-template-alist)))
  7454. (org-complete-expand-structure-template (1- beg1) a)
  7455. (throw 'exit t)))
  7456. (tag (and (equal (char-before beg1) ?:)
  7457. (equal (char-after (point-at-bol)) ?*)))
  7458. (prop (and (equal (char-before beg1) ?:)
  7459. (not (equal (char-after (point-at-bol)) ?*))))
  7460. (texp (equal (char-before beg) ?\\))
  7461. (link (equal (char-before beg) ?\[))
  7462. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7463. beg)
  7464. "#+"))
  7465. (startup (string-match "^#\\+STARTUP:.*"
  7466. (buffer-substring (point-at-bol) (point))))
  7467. (completion-ignore-case opt)
  7468. (type nil)
  7469. (tbl nil)
  7470. (table (cond
  7471. (opt
  7472. (setq type :opt)
  7473. (require 'org-exp)
  7474. (append
  7475. (mapcar
  7476. (lambda (x)
  7477. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7478. (cons (match-string 2 x) (match-string 1 x)))
  7479. (org-split-string (org-get-current-options) "\n"))
  7480. (mapcar 'list org-additional-option-like-keywords)))
  7481. (startup
  7482. (setq type :startup)
  7483. org-startup-options)
  7484. (link (append org-link-abbrev-alist-local
  7485. org-link-abbrev-alist))
  7486. (texp
  7487. (setq type :tex)
  7488. org-html-entities)
  7489. ((string-match "\\`\\*+[ \t]+\\'"
  7490. (buffer-substring (point-at-bol) beg))
  7491. (setq type :todo)
  7492. (mapcar 'list org-todo-keywords-1))
  7493. (searchhead
  7494. (setq type :searchhead)
  7495. (save-excursion
  7496. (goto-char (point-min))
  7497. (while (re-search-forward org-todo-line-regexp nil t)
  7498. (push (list
  7499. (org-make-org-heading-search-string
  7500. (match-string 3) t))
  7501. tbl)))
  7502. tbl)
  7503. (tag (setq type :tag beg beg1)
  7504. (or org-tag-alist (org-get-buffer-tags)))
  7505. (prop (setq type :prop beg beg1)
  7506. (mapcar 'list (org-buffer-property-keys nil t t)))
  7507. (t (progn
  7508. (call-interactively org-completion-fallback-command)
  7509. (throw 'exit nil)))))
  7510. (pattern (buffer-substring-no-properties beg end))
  7511. (completion (try-completion pattern table confirm)))
  7512. (cond ((eq completion t)
  7513. (if (not (assoc (upcase pattern) table))
  7514. (message "Already complete")
  7515. (if (and (equal type :opt)
  7516. (not (member (car (assoc (upcase pattern) table))
  7517. org-additional-option-like-keywords)))
  7518. (insert (substring (cdr (assoc (upcase pattern) table))
  7519. (length pattern)))
  7520. (if (memq type '(:tag :prop)) (insert ":")))))
  7521. ((null completion)
  7522. (message "Can't find completion for \"%s\"" pattern)
  7523. (ding))
  7524. ((not (string= pattern completion))
  7525. (delete-region beg end)
  7526. (if (string-match " +$" completion)
  7527. (setq completion (replace-match "" t t completion)))
  7528. (insert completion)
  7529. (if (get-buffer-window "*Completions*")
  7530. (delete-window (get-buffer-window "*Completions*")))
  7531. (if (assoc completion table)
  7532. (if (eq type :todo) (insert " ")
  7533. (if (memq type '(:tag :prop)) (insert ":"))))
  7534. (if (and (equal type :opt) (assoc completion table))
  7535. (message "%s" (substitute-command-keys
  7536. "Press \\[org-complete] again to insert example settings"))))
  7537. (t
  7538. (message "Making completion list...")
  7539. (let ((list (sort (all-completions pattern table confirm)
  7540. 'string<)))
  7541. (with-output-to-temp-buffer "*Completions*"
  7542. (condition-case nil
  7543. ;; Protection needed for XEmacs and emacs 21
  7544. (display-completion-list list pattern)
  7545. (error (display-completion-list list)))))
  7546. (message "Making completion list...%s" "done")))))))
  7547. ;;;; TODO, DEADLINE, Comments
  7548. (defun org-toggle-comment ()
  7549. "Change the COMMENT state of an entry."
  7550. (interactive)
  7551. (save-excursion
  7552. (org-back-to-heading)
  7553. (let (case-fold-search)
  7554. (if (looking-at (concat outline-regexp
  7555. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7556. (replace-match "" t t nil 1)
  7557. (if (looking-at outline-regexp)
  7558. (progn
  7559. (goto-char (match-end 0))
  7560. (insert org-comment-string " ")))))))
  7561. (defvar org-last-todo-state-is-todo nil
  7562. "This is non-nil when the last TODO state change led to a TODO state.
  7563. If the last change removed the TODO tag or switched to DONE, then
  7564. this is nil.")
  7565. (defvar org-setting-tags nil) ; dynamically skiped
  7566. (defun org-parse-local-options (string var)
  7567. "Parse STRING for startup setting relevant for variable VAR."
  7568. (let ((rtn (symbol-value var))
  7569. e opts)
  7570. (save-match-data
  7571. (if (or (not string) (not (string-match "\\S-" string)))
  7572. rtn
  7573. (setq opts (delq nil (mapcar (lambda (x)
  7574. (setq e (assoc x org-startup-options))
  7575. (if (eq (nth 1 e) var) e nil))
  7576. (org-split-string string "[ \t]+"))))
  7577. (if (not opts)
  7578. rtn
  7579. (setq rtn nil)
  7580. (while (setq e (pop opts))
  7581. (if (not (nth 3 e))
  7582. (setq rtn (nth 2 e))
  7583. (if (not (listp rtn)) (setq rtn nil))
  7584. (push (nth 2 e) rtn)))
  7585. rtn)))))
  7586. (defvar org-blocker-hook nil
  7587. "Hook for functions that are allowed to block a state change.
  7588. Each function gets as its single argument a property list, see
  7589. `org-trigger-hook' for more information about this list.
  7590. If any of the functions in this hook returns nil, the state change
  7591. is blocked.")
  7592. (defvar org-trigger-hook nil
  7593. "Hook for functions that are triggered by a state change.
  7594. Each function gets as its single argument a property list with at least
  7595. the following elements:
  7596. (:type type-of-change :position pos-at-entry-start
  7597. :from old-state :to new-state)
  7598. Depending on the type, more properties may be present.
  7599. This mechanism is currently implemented for:
  7600. TODO state changes
  7601. ------------------
  7602. :type todo-state-change
  7603. :from previous state (keyword as a string), or nil
  7604. :to new state (keyword as a string), or nil")
  7605. (defun org-todo (&optional arg)
  7606. "Change the TODO state of an item.
  7607. The state of an item is given by a keyword at the start of the heading,
  7608. like
  7609. *** TODO Write paper
  7610. *** DONE Call mom
  7611. The different keywords are specified in the variable `org-todo-keywords'.
  7612. By default the available states are \"TODO\" and \"DONE\".
  7613. So for this example: when the item starts with TODO, it is changed to DONE.
  7614. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7615. DONE are present, add TODO at the beginning of the heading.
  7616. With C-u prefix arg, use completion to determine the new state.
  7617. With numeric prefix arg, switch to that state.
  7618. For calling through lisp, arg is also interpreted in the following way:
  7619. 'none -> empty state
  7620. \"\"(empty string) -> switch to empty state
  7621. 'done -> switch to DONE
  7622. 'nextset -> switch to the next set of keywords
  7623. 'previousset -> switch to the previous set of keywords
  7624. \"WAITING\" -> switch to the specified keyword, but only if it
  7625. really is a member of `org-todo-keywords'."
  7626. (interactive "P")
  7627. (save-excursion
  7628. (catch 'exit
  7629. (org-back-to-heading)
  7630. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7631. (or (looking-at (concat " +" org-todo-regexp " *"))
  7632. (looking-at " *"))
  7633. (let* ((match-data (match-data))
  7634. (startpos (point-at-bol))
  7635. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7636. (org-log-done org-log-done)
  7637. (org-log-repeat org-log-repeat)
  7638. (org-todo-log-states org-todo-log-states)
  7639. (this (match-string 1))
  7640. (hl-pos (match-beginning 0))
  7641. (head (org-get-todo-sequence-head this))
  7642. (ass (assoc head org-todo-kwd-alist))
  7643. (interpret (nth 1 ass))
  7644. (done-word (nth 3 ass))
  7645. (final-done-word (nth 4 ass))
  7646. (last-state (or this ""))
  7647. (completion-ignore-case t)
  7648. (member (member this org-todo-keywords-1))
  7649. (tail (cdr member))
  7650. (state (cond
  7651. ((and org-todo-key-trigger
  7652. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7653. (and (not arg) org-use-fast-todo-selection
  7654. (not (eq org-use-fast-todo-selection 'prefix)))))
  7655. ;; Use fast selection
  7656. (org-fast-todo-selection))
  7657. ((and (equal arg '(4))
  7658. (or (not org-use-fast-todo-selection)
  7659. (not org-todo-key-trigger)))
  7660. ;; Read a state with completion
  7661. (completing-read "State: " (mapcar (lambda(x) (list x))
  7662. org-todo-keywords-1)
  7663. nil t))
  7664. ((eq arg 'right)
  7665. (if this
  7666. (if tail (car tail) nil)
  7667. (car org-todo-keywords-1)))
  7668. ((eq arg 'left)
  7669. (if (equal member org-todo-keywords-1)
  7670. nil
  7671. (if this
  7672. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7673. org-todo-keywords-1)
  7674. (org-last org-todo-keywords-1))))
  7675. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7676. (setq arg nil))) ; hack to fall back to cycling
  7677. (arg
  7678. ;; user or caller requests a specific state
  7679. (cond
  7680. ((equal arg "") nil)
  7681. ((eq arg 'none) nil)
  7682. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7683. ((eq arg 'nextset)
  7684. (or (car (cdr (member head org-todo-heads)))
  7685. (car org-todo-heads)))
  7686. ((eq arg 'previousset)
  7687. (let ((org-todo-heads (reverse org-todo-heads)))
  7688. (or (car (cdr (member head org-todo-heads)))
  7689. (car org-todo-heads))))
  7690. ((car (member arg org-todo-keywords-1)))
  7691. ((nth (1- (prefix-numeric-value arg))
  7692. org-todo-keywords-1))))
  7693. ((null member) (or head (car org-todo-keywords-1)))
  7694. ((equal this final-done-word) nil) ;; -> make empty
  7695. ((null tail) nil) ;; -> first entry
  7696. ((eq interpret 'sequence)
  7697. (car tail))
  7698. ((memq interpret '(type priority))
  7699. (if (eq this-command last-command)
  7700. (car tail)
  7701. (if (> (length tail) 0)
  7702. (or done-word (car org-done-keywords))
  7703. nil)))
  7704. (t nil)))
  7705. (next (if state (concat " " state " ") " "))
  7706. (change-plist (list :type 'todo-state-change :from this :to state
  7707. :position startpos))
  7708. dolog now-done-p)
  7709. (when org-blocker-hook
  7710. (unless (save-excursion
  7711. (save-match-data
  7712. (run-hook-with-args-until-failure
  7713. 'org-blocker-hook change-plist)))
  7714. (if (interactive-p)
  7715. (error "TODO state change from %s to %s blocked" this state)
  7716. ;; fail silently
  7717. (message "TODO state change from %s to %s blocked" this state)
  7718. (throw 'exit nil))))
  7719. (store-match-data match-data)
  7720. (replace-match next t t)
  7721. (unless (pos-visible-in-window-p hl-pos)
  7722. (message "TODO state changed to %s" (org-trim next)))
  7723. (unless head
  7724. (setq head (org-get-todo-sequence-head state)
  7725. ass (assoc head org-todo-kwd-alist)
  7726. interpret (nth 1 ass)
  7727. done-word (nth 3 ass)
  7728. final-done-word (nth 4 ass)))
  7729. (when (memq arg '(nextset previousset))
  7730. (message "Keyword-Set %d/%d: %s"
  7731. (- (length org-todo-sets) -1
  7732. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7733. (length org-todo-sets)
  7734. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7735. (setq org-last-todo-state-is-todo
  7736. (not (member state org-done-keywords)))
  7737. (setq now-done-p (and (member state org-done-keywords)
  7738. (not (member this org-done-keywords))))
  7739. (and logging (org-local-logging logging))
  7740. (when (and (or org-todo-log-states org-log-done)
  7741. (not (memq arg '(nextset previousset))))
  7742. ;; we need to look at recording a time and note
  7743. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7744. (nth 2 (assoc this org-todo-log-states))))
  7745. (when (and state
  7746. (member state org-not-done-keywords)
  7747. (not (member this org-not-done-keywords)))
  7748. ;; This is now a todo state and was not one before
  7749. ;; If there was a CLOSED time stamp, get rid of it.
  7750. (org-add-planning-info nil nil 'closed))
  7751. (when (and now-done-p org-log-done)
  7752. ;; It is now done, and it was not done before
  7753. (org-add-planning-info 'closed (org-current-time))
  7754. (if (and (not dolog) (eq 'note org-log-done))
  7755. (org-add-log-setup 'done state 'findpos 'note)))
  7756. (when (and state dolog)
  7757. ;; This is a non-nil state, and we need to log it
  7758. (org-add-log-setup 'state state 'findpos dolog)))
  7759. ;; Fixup tag positioning
  7760. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7761. (when org-provide-todo-statistics
  7762. (org-update-parent-todo-statistics))
  7763. (run-hooks 'org-after-todo-state-change-hook)
  7764. (if (and arg (not (member state org-done-keywords)))
  7765. (setq head (org-get-todo-sequence-head state)))
  7766. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7767. ;; Do we need to trigger a repeat?
  7768. (when now-done-p (org-auto-repeat-maybe state))
  7769. ;; Fixup cursor location if close to the keyword
  7770. (if (and (outline-on-heading-p)
  7771. (not (bolp))
  7772. (save-excursion (beginning-of-line 1)
  7773. (looking-at org-todo-line-regexp))
  7774. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7775. (progn
  7776. (goto-char (or (match-end 2) (match-end 1)))
  7777. (just-one-space)))
  7778. (when org-trigger-hook
  7779. (save-excursion
  7780. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7781. (defun org-update-parent-todo-statistics ()
  7782. "Update any statistics cookie in the parent of the current headline."
  7783. (interactive)
  7784. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7785. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7786. (catch 'exit
  7787. (save-excursion
  7788. (setq level (org-up-heading-safe))
  7789. (unless (and level
  7790. (re-search-forward box-re (point-at-eol) t))
  7791. (throw 'exit nil))
  7792. (setq is-percent (match-end 2))
  7793. (save-match-data
  7794. (unless (outline-next-heading) (throw 'exit nil))
  7795. (while (looking-at org-todo-line-regexp)
  7796. (setq kwd (match-string 2))
  7797. (and kwd (setq cnt-all (1+ cnt-all)))
  7798. (and (member kwd org-done-keywords)
  7799. (setq cnt-done (1+ cnt-done)))
  7800. (condition-case nil
  7801. (outline-forward-same-level 1)
  7802. (error (end-of-line 1)))))
  7803. (replace-match
  7804. (if is-percent
  7805. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7806. (format "[%d/%d]" cnt-done cnt-all)))
  7807. (run-hook-with-args 'org-after-todo-statistics-hook
  7808. cnt-done (- cnt-all cnt-done))))))
  7809. (defvar org-after-todo-statistics-hook nil
  7810. "Hook that is called after a TODO statistics cookie has been updated.
  7811. Each function is called with two arguments: the number of not-done entries
  7812. and the number of done entries.
  7813. For example, the following function, when added to this hook, will switch
  7814. an entry to DONE when all children are done, and back to TODO when new
  7815. entries are set to a TODO status. Note that this hook is only called
  7816. when there is a statistics cookie in the headline!
  7817. (defun org-summary-todo (n-done n-not-done)
  7818. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7819. (let (org-log-done org-log-states) ; turn off logging
  7820. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7821. ")
  7822. (defun org-local-logging (value)
  7823. "Get logging settings from a property VALUE."
  7824. (let* (words w a)
  7825. ;; directly set the variables, they are already local.
  7826. (setq org-log-done nil
  7827. org-log-repeat nil
  7828. org-todo-log-states nil)
  7829. (setq words (org-split-string value))
  7830. (while (setq w (pop words))
  7831. (cond
  7832. ((setq a (assoc w org-startup-options))
  7833. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7834. (set (nth 1 a) (nth 2 a))))
  7835. ((setq a (org-extract-log-state-settings w))
  7836. (and (member (car a) org-todo-keywords-1)
  7837. (push a org-todo-log-states)))))))
  7838. (defun org-get-todo-sequence-head (kwd)
  7839. "Return the head of the TODO sequence to which KWD belongs.
  7840. If KWD is not set, check if there is a text property remembering the
  7841. right sequence."
  7842. (let (p)
  7843. (cond
  7844. ((not kwd)
  7845. (or (get-text-property (point-at-bol) 'org-todo-head)
  7846. (progn
  7847. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7848. nil (point-at-eol)))
  7849. (get-text-property p 'org-todo-head))))
  7850. ((not (member kwd org-todo-keywords-1))
  7851. (car org-todo-keywords-1))
  7852. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7853. (defun org-fast-todo-selection ()
  7854. "Fast TODO keyword selection with single keys.
  7855. Returns the new TODO keyword, or nil if no state change should occur."
  7856. (let* ((fulltable org-todo-key-alist)
  7857. (done-keywords org-done-keywords) ;; needed for the faces.
  7858. (maxlen (apply 'max (mapcar
  7859. (lambda (x)
  7860. (if (stringp (car x)) (string-width (car x)) 0))
  7861. fulltable)))
  7862. (expert nil)
  7863. (fwidth (+ maxlen 3 1 3))
  7864. (ncol (/ (- (window-width) 4) fwidth))
  7865. tg cnt e c tbl
  7866. groups ingroup)
  7867. (save-window-excursion
  7868. (if expert
  7869. (set-buffer (get-buffer-create " *Org todo*"))
  7870. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7871. (erase-buffer)
  7872. (org-set-local 'org-done-keywords done-keywords)
  7873. (setq tbl fulltable cnt 0)
  7874. (while (setq e (pop tbl))
  7875. (cond
  7876. ((equal e '(:startgroup))
  7877. (push '() groups) (setq ingroup t)
  7878. (when (not (= cnt 0))
  7879. (setq cnt 0)
  7880. (insert "\n"))
  7881. (insert "{ "))
  7882. ((equal e '(:endgroup))
  7883. (setq ingroup nil cnt 0)
  7884. (insert "}\n"))
  7885. (t
  7886. (setq tg (car e) c (cdr e))
  7887. (if ingroup (push tg (car groups)))
  7888. (setq tg (org-add-props tg nil 'face
  7889. (org-get-todo-face tg)))
  7890. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7891. (insert "[" c "] " tg (make-string
  7892. (- fwidth 4 (length tg)) ?\ ))
  7893. (when (= (setq cnt (1+ cnt)) ncol)
  7894. (insert "\n")
  7895. (if ingroup (insert " "))
  7896. (setq cnt 0)))))
  7897. (insert "\n")
  7898. (goto-char (point-min))
  7899. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7900. (fit-window-to-buffer))
  7901. (message "[a-z..]:Set [SPC]:clear")
  7902. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7903. (cond
  7904. ((or (= c ?\C-g)
  7905. (and (= c ?q) (not (rassoc c fulltable))))
  7906. (setq quit-flag t))
  7907. ((= c ?\ ) nil)
  7908. ((setq e (rassoc c fulltable) tg (car e))
  7909. tg)
  7910. (t (setq quit-flag t))))))
  7911. (defun org-entry-is-todo-p ()
  7912. (member (org-get-todo-state) org-not-done-keywords))
  7913. (defun org-entry-is-done-p ()
  7914. (member (org-get-todo-state) org-done-keywords))
  7915. (defun org-get-todo-state ()
  7916. (save-excursion
  7917. (org-back-to-heading t)
  7918. (and (looking-at org-todo-line-regexp)
  7919. (match-end 2)
  7920. (match-string 2))))
  7921. (defun org-at-date-range-p (&optional inactive-ok)
  7922. "Is the cursor inside a date range?"
  7923. (interactive)
  7924. (save-excursion
  7925. (catch 'exit
  7926. (let ((pos (point)))
  7927. (skip-chars-backward "^[<\r\n")
  7928. (skip-chars-backward "<[")
  7929. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7930. (>= (match-end 0) pos)
  7931. (throw 'exit t))
  7932. (skip-chars-backward "^<[\r\n")
  7933. (skip-chars-backward "<[")
  7934. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7935. (>= (match-end 0) pos)
  7936. (throw 'exit t)))
  7937. nil)))
  7938. (defun org-get-repeat ()
  7939. "Check if there is a deadline/schedule with repeater in this entry."
  7940. (save-match-data
  7941. (save-excursion
  7942. (org-back-to-heading t)
  7943. (if (re-search-forward
  7944. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7945. (match-string 1)))))
  7946. (defvar org-last-changed-timestamp)
  7947. (defvar org-last-inserted-timestamp)
  7948. (defvar org-log-post-message)
  7949. (defvar org-log-note-purpose)
  7950. (defvar org-log-note-how)
  7951. (defvar org-log-note-extra)
  7952. (defun org-auto-repeat-maybe (done-word)
  7953. "Check if the current headline contains a repeated deadline/schedule.
  7954. If yes, set TODO state back to what it was and change the base date
  7955. of repeating deadline/scheduled time stamps to new date.
  7956. This function is run automatically after each state change to a DONE state."
  7957. ;; last-state is dynamically scoped into this function
  7958. (let* ((repeat (org-get-repeat))
  7959. (aa (assoc last-state org-todo-kwd-alist))
  7960. (interpret (nth 1 aa))
  7961. (head (nth 2 aa))
  7962. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7963. (msg "Entry repeats: ")
  7964. (org-log-done nil)
  7965. (org-todo-log-states nil)
  7966. (nshiftmax 10) (nshift 0)
  7967. re type n what ts mb0 time)
  7968. (when repeat
  7969. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7970. (org-todo (if (eq interpret 'type) last-state head))
  7971. (when org-log-repeat
  7972. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7973. (memq 'org-add-log-note post-command-hook))
  7974. ;; OK, we are already setup for some record
  7975. (if (eq org-log-repeat 'note)
  7976. ;; make sure we take a note, not only a time stamp
  7977. (setq org-log-note-how 'note))
  7978. ;; Set up for taking a record
  7979. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7980. 'findpos org-log-repeat)))
  7981. (org-back-to-heading t)
  7982. (org-add-planning-info nil nil 'closed)
  7983. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7984. org-deadline-time-regexp "\\)\\|\\("
  7985. org-ts-regexp "\\)"))
  7986. (while (re-search-forward
  7987. re (save-excursion (outline-next-heading) (point)) t)
  7988. (setq type (if (match-end 1) org-scheduled-string
  7989. (if (match-end 3) org-deadline-string "Plain:"))
  7990. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7991. mb0 (match-beginning 0))
  7992. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7993. (setq n (string-to-number (match-string 2 ts))
  7994. what (match-string 3 ts))
  7995. (if (equal what "w") (setq n (* n 7) what "d"))
  7996. ;; Preparation, see if we need to modify the start date for the change
  7997. (when (match-end 1)
  7998. (setq time (save-match-data (org-time-string-to-time ts)))
  7999. (cond
  8000. ((equal (match-string 1 ts) ".")
  8001. ;; Shift starting date to today
  8002. (org-timestamp-change
  8003. (- (time-to-days (current-time)) (time-to-days time))
  8004. 'day))
  8005. ((equal (match-string 1 ts) "+")
  8006. (while (or (= nshift 0)
  8007. (<= (time-to-days time) (time-to-days (current-time))))
  8008. (when (= (incf nshift) nshiftmax)
  8009. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8010. (error "Abort")))
  8011. (org-timestamp-change n (cdr (assoc what whata)))
  8012. (org-at-timestamp-p t)
  8013. (setq ts (match-string 1))
  8014. (setq time (save-match-data (org-time-string-to-time ts))))
  8015. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8016. ;; rematch, so that we have everything in place for the real shift
  8017. (org-at-timestamp-p t)
  8018. (setq ts (match-string 1))
  8019. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8020. (org-timestamp-change n (cdr (assoc what whata)))
  8021. (setq msg (concat msg type org-last-changed-timestamp " "))))
  8022. (setq org-log-post-message msg)
  8023. (message "%s" msg))))
  8024. (defun org-show-todo-tree (arg)
  8025. "Make a compact tree which shows all headlines marked with TODO.
  8026. The tree will show the lines where the regexp matches, and all higher
  8027. headlines above the match.
  8028. With a \\[universal-argument] prefix, also show the DONE entries.
  8029. With a numeric prefix N, construct a sparse tree for the Nth element
  8030. of `org-todo-keywords-1'."
  8031. (interactive "P")
  8032. (let ((case-fold-search nil)
  8033. (kwd-re
  8034. (cond ((null arg) org-not-done-regexp)
  8035. ((equal arg '(4))
  8036. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  8037. (mapcar 'list org-todo-keywords-1))))
  8038. (concat "\\("
  8039. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8040. "\\)\\>")))
  8041. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8042. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8043. org-todo-keywords-1)))
  8044. (t (error "Invalid prefix argument: %s" arg)))))
  8045. (message "%d TODO entries found"
  8046. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8047. (defun org-deadline (&optional remove time)
  8048. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8049. With argument REMOVE, remove any deadline 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-deadline-string)
  8056. (message "Item no longer has a deadline."))
  8057. (if (org-get-repeat)
  8058. (error "Cannot change deadline on task with repeater, please do that by hand")
  8059. (org-add-planning-info 'deadline time 'closed)
  8060. (message "Deadline on %s" org-last-inserted-timestamp))))
  8061. (defun org-schedule (&optional remove time)
  8062. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8063. With argument REMOVE, remove any scheduling date from the item.
  8064. When TIME is set, it should be an internal time specification, and the
  8065. scheduling will use the corresponding date."
  8066. (interactive "P")
  8067. (if remove
  8068. (progn
  8069. (org-remove-timestamp-with-keyword org-scheduled-string)
  8070. (message "Item is no longer scheduled."))
  8071. (if (org-get-repeat)
  8072. (error "Cannot reschedule task with repeater, please do that by hand")
  8073. (org-add-planning-info 'scheduled time 'closed)
  8074. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8075. (defun org-remove-timestamp-with-keyword (keyword)
  8076. "Remove all time stamps with KEYWORD in the current entry."
  8077. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8078. beg)
  8079. (save-excursion
  8080. (org-back-to-heading t)
  8081. (setq beg (point))
  8082. (org-end-of-subtree t t)
  8083. (while (re-search-backward re beg t)
  8084. (replace-match "")
  8085. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8086. (equal (char-before) ?\ ))
  8087. (backward-delete-char 1)
  8088. (if (string-match "^[ \t]*$" (buffer-substring
  8089. (point-at-bol) (point-at-eol)))
  8090. (delete-region (point-at-bol)
  8091. (min (point-max) (1+ (point-at-eol))))))))))
  8092. (defun org-add-planning-info (what &optional time &rest remove)
  8093. "Insert new timestamp with keyword in the line directly after the headline.
  8094. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8095. If non is given, the user is prompted for a date.
  8096. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8097. be removed."
  8098. (interactive)
  8099. (let (org-time-was-given org-end-time-was-given ts
  8100. end default-time default-input)
  8101. (when (and (not time) (memq what '(scheduled deadline)))
  8102. ;; Try to get a default date/time from existing timestamp
  8103. (save-excursion
  8104. (org-back-to-heading t)
  8105. (setq end (save-excursion (outline-next-heading) (point)))
  8106. (when (re-search-forward (if (eq what 'scheduled)
  8107. org-scheduled-time-regexp
  8108. org-deadline-time-regexp)
  8109. end t)
  8110. (setq ts (match-string 1)
  8111. default-time
  8112. (apply 'encode-time (org-parse-time-string ts))
  8113. default-input (and ts (org-get-compact-tod ts))))))
  8114. (when what
  8115. ;; If necessary, get the time from the user
  8116. (setq time (or time (org-read-date nil 'to-time nil nil
  8117. default-time default-input))))
  8118. (when (and org-insert-labeled-timestamps-at-point
  8119. (member what '(scheduled deadline)))
  8120. (insert
  8121. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8122. (org-insert-time-stamp time org-time-was-given
  8123. nil nil nil (list org-end-time-was-given))
  8124. (setq what nil))
  8125. (save-excursion
  8126. (save-restriction
  8127. (let (col list elt ts buffer-invisibility-spec)
  8128. (org-back-to-heading t)
  8129. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8130. (goto-char (match-end 1))
  8131. (setq col (current-column))
  8132. (goto-char (match-end 0))
  8133. (if (eobp) (insert "\n") (forward-char 1))
  8134. (if (and (not (looking-at outline-regexp))
  8135. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8136. "[^\r\n]*"))
  8137. (not (equal (match-string 1) org-clock-string)))
  8138. (narrow-to-region (match-beginning 0) (match-end 0))
  8139. (insert-before-markers "\n")
  8140. (backward-char 1)
  8141. (narrow-to-region (point) (point))
  8142. (and org-adapt-indentation (org-indent-to-column col)))
  8143. ;; Check if we have to remove something.
  8144. (setq list (cons what remove))
  8145. (while list
  8146. (setq elt (pop list))
  8147. (goto-char (point-min))
  8148. (when (or (and (eq elt 'scheduled)
  8149. (re-search-forward org-scheduled-time-regexp nil t))
  8150. (and (eq elt 'deadline)
  8151. (re-search-forward org-deadline-time-regexp nil t))
  8152. (and (eq elt 'closed)
  8153. (re-search-forward org-closed-time-regexp nil t)))
  8154. (replace-match "")
  8155. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8156. (if (looking-at " +") (replace-match ""))))
  8157. (goto-char (point-max))
  8158. (when what
  8159. (insert
  8160. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8161. (cond ((eq what 'scheduled) org-scheduled-string)
  8162. ((eq what 'deadline) org-deadline-string)
  8163. ((eq what 'closed) org-closed-string))
  8164. " ")
  8165. (setq ts (org-insert-time-stamp
  8166. time
  8167. (or org-time-was-given
  8168. (and (eq what 'closed) org-log-done-with-time))
  8169. (eq what 'closed)
  8170. nil nil (list org-end-time-was-given)))
  8171. (end-of-line 1))
  8172. (goto-char (point-min))
  8173. (widen)
  8174. (if (and (looking-at "[ \t]+\n")
  8175. (equal (char-before) ?\n))
  8176. (delete-region (1- (point)) (point-at-eol)))
  8177. ts)))))
  8178. (defvar org-log-note-marker (make-marker))
  8179. (defvar org-log-note-purpose nil)
  8180. (defvar org-log-note-state nil)
  8181. (defvar org-log-note-how nil)
  8182. (defvar org-log-note-extra nil)
  8183. (defvar org-log-note-window-configuration nil)
  8184. (defvar org-log-note-return-to (make-marker))
  8185. (defvar org-log-post-message nil
  8186. "Message to be displayed after a log note has been stored.
  8187. The auto-repeater uses this.")
  8188. (defun org-add-note ()
  8189. "Add a note to the current entry.
  8190. This is done in the same way as adding a state change note."
  8191. (interactive)
  8192. (org-add-log-setup 'note nil t nil))
  8193. (defun org-add-log-setup (&optional purpose state findpos how extra)
  8194. "Set up the post command hook to take a note.
  8195. If this is about to TODO state change, the new state is expected in STATE.
  8196. When FINDPOS is non-nil, find the correct position for the note in
  8197. the current entry. If not, assume that it can be inserted at point.
  8198. HOW is an indicator what kind of note should be created.
  8199. EXTRA is additional text that will be inserted into the notes buffer."
  8200. (save-excursion
  8201. (when findpos
  8202. (org-back-to-heading t)
  8203. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8204. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8205. "[^\r\n]*\\)?"))
  8206. (goto-char (match-end 0))
  8207. (unless org-log-states-order-reversed
  8208. (and (= (char-after) ?\n) (forward-char 1))
  8209. (org-skip-over-state-notes)
  8210. (skip-chars-backward " \t\n\r")))
  8211. (move-marker org-log-note-marker (point))
  8212. (setq org-log-note-purpose purpose
  8213. org-log-note-state state
  8214. org-log-note-how how
  8215. org-log-note-extra extra)
  8216. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8217. (defun org-skip-over-state-notes ()
  8218. "Skip past the list of State notes in an entry."
  8219. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8220. (while (looking-at "[ \t]*- State")
  8221. (condition-case nil
  8222. (org-next-item)
  8223. (error (org-end-of-item)))))
  8224. (defun org-add-log-note (&optional purpose)
  8225. "Pop up a window for taking a note, and add this note later at point."
  8226. (remove-hook 'post-command-hook 'org-add-log-note)
  8227. (setq org-log-note-window-configuration (current-window-configuration))
  8228. (delete-other-windows)
  8229. (move-marker org-log-note-return-to (point))
  8230. (switch-to-buffer (marker-buffer org-log-note-marker))
  8231. (goto-char org-log-note-marker)
  8232. (org-switch-to-buffer-other-window "*Org Note*")
  8233. (erase-buffer)
  8234. (if (memq org-log-note-how '(time state))
  8235. (org-store-log-note)
  8236. (let ((org-inhibit-startup t)) (org-mode))
  8237. (insert (format "# Insert note for %s.
  8238. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8239. (cond
  8240. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8241. ((eq org-log-note-purpose 'done) "closed todo item")
  8242. ((eq org-log-note-purpose 'state)
  8243. (format "state change to \"%s\"" org-log-note-state))
  8244. ((eq org-log-note-purpose 'note)
  8245. "this entry")
  8246. (t (error "This should not happen")))))
  8247. (if org-log-note-extra (insert org-log-note-extra))
  8248. (org-set-local 'org-finish-function 'org-store-log-note)))
  8249. (defvar org-note-abort nil) ; dynamically scoped
  8250. (defun org-store-log-note ()
  8251. "Finish taking a log note, and insert it to where it belongs."
  8252. (let ((txt (buffer-string))
  8253. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8254. lines ind)
  8255. (kill-buffer (current-buffer))
  8256. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8257. (setq txt (replace-match "" t t txt)))
  8258. (if (string-match "\\s-+\\'" txt)
  8259. (setq txt (replace-match "" t t txt)))
  8260. (setq lines (org-split-string txt "\n"))
  8261. (when (and note (string-match "\\S-" note))
  8262. (setq note
  8263. (org-replace-escapes
  8264. note
  8265. (list (cons "%u" (user-login-name))
  8266. (cons "%U" user-full-name)
  8267. (cons "%t" (format-time-string
  8268. (org-time-stamp-format 'long 'inactive)
  8269. (current-time)))
  8270. (cons "%s" (if org-log-note-state
  8271. (concat "\"" org-log-note-state "\"")
  8272. "")))))
  8273. (if lines (setq note (concat note " \\\\")))
  8274. (push note lines))
  8275. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8276. (when lines
  8277. (save-excursion
  8278. (set-buffer (marker-buffer org-log-note-marker))
  8279. (save-excursion
  8280. (goto-char org-log-note-marker)
  8281. (move-marker org-log-note-marker nil)
  8282. (end-of-line 1)
  8283. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8284. (indent-relative nil)
  8285. (insert "- " (pop lines))
  8286. (org-indent-line-function)
  8287. (beginning-of-line 1)
  8288. (looking-at "[ \t]*")
  8289. (setq ind (concat (match-string 0) " "))
  8290. (end-of-line 1)
  8291. (while lines (insert "\n" ind (pop lines)))))))
  8292. (set-window-configuration org-log-note-window-configuration)
  8293. (with-current-buffer (marker-buffer org-log-note-return-to)
  8294. (goto-char org-log-note-return-to))
  8295. (move-marker org-log-note-return-to nil)
  8296. (and org-log-post-message (message "%s" org-log-post-message)))
  8297. (defun org-sparse-tree (&optional arg)
  8298. "Create a sparse tree, prompt for the details.
  8299. This command can create sparse trees. You first need to select the type
  8300. of match used to create the tree:
  8301. t Show entries with a specific TODO keyword.
  8302. T Show entries selected by a tags match.
  8303. p Enter a property name and its value (both with completion on existing
  8304. names/values) and show entries with that property.
  8305. r Show entries matching a regular expression
  8306. d Show deadlines due within `org-deadline-warning-days'."
  8307. (interactive "P")
  8308. (let (ans kwd value)
  8309. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8310. (setq ans (read-char-exclusive))
  8311. (cond
  8312. ((equal ans ?d)
  8313. (call-interactively 'org-check-deadlines))
  8314. ((equal ans ?b)
  8315. (call-interactively 'org-check-before-date))
  8316. ((equal ans ?t)
  8317. (org-show-todo-tree '(4)))
  8318. ((equal ans ?T)
  8319. (call-interactively 'org-tags-sparse-tree))
  8320. ((member ans '(?p ?P))
  8321. (setq kwd (completing-read "Property: "
  8322. (mapcar 'list (org-buffer-property-keys))))
  8323. (setq value (completing-read "Value: "
  8324. (mapcar 'list (org-property-values kwd))))
  8325. (unless (string-match "\\`{.*}\\'" value)
  8326. (setq value (concat "\"" value "\"")))
  8327. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8328. ((member ans '(?r ?R ?/))
  8329. (call-interactively 'org-occur))
  8330. (t (error "No such sparse tree command \"%c\"" ans)))))
  8331. (defvar org-occur-highlights nil
  8332. "List of overlays used for occur matches.")
  8333. (make-variable-buffer-local 'org-occur-highlights)
  8334. (defvar org-occur-parameters nil
  8335. "Parameters of the active org-occur calls.
  8336. This is a list, each call to org-occur pushes as cons cell,
  8337. containing the regular expression and the callback, onto the list.
  8338. The list can contain several entries if `org-occur' has been called
  8339. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8340. will only contain one set of parameters. When the highlights are
  8341. removed (for example with `C-c C-c', or with the next edit (depending
  8342. on `org-remove-highlights-with-change'), this variable is emptied
  8343. as well.")
  8344. (make-variable-buffer-local 'org-occur-parameters)
  8345. (defun org-occur (regexp &optional keep-previous callback)
  8346. "Make a compact tree which shows all matches of REGEXP.
  8347. The tree will show the lines where the regexp matches, and all higher
  8348. headlines above the match. It will also show the heading after the match,
  8349. to make sure editing the matching entry is easy.
  8350. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8351. call to `org-occur' will be kept, to allow stacking of calls to this
  8352. command.
  8353. If CALLBACK is non-nil, it is a function which is called to confirm
  8354. that the match should indeed be shown."
  8355. (interactive "sRegexp: \nP")
  8356. (unless keep-previous
  8357. (org-remove-occur-highlights nil nil t))
  8358. (push (cons regexp callback) org-occur-parameters)
  8359. (let ((cnt 0))
  8360. (save-excursion
  8361. (goto-char (point-min))
  8362. (if (or (not keep-previous) ; do not want to keep
  8363. (not org-occur-highlights)) ; no previous matches
  8364. ;; hide everything
  8365. (org-overview))
  8366. (while (re-search-forward regexp nil t)
  8367. (when (or (not callback)
  8368. (save-match-data (funcall callback)))
  8369. (setq cnt (1+ cnt))
  8370. (when org-highlight-sparse-tree-matches
  8371. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8372. (org-show-context 'occur-tree))))
  8373. (when org-remove-highlights-with-change
  8374. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8375. nil 'local))
  8376. (unless org-sparse-tree-open-archived-trees
  8377. (org-hide-archived-subtrees (point-min) (point-max)))
  8378. (run-hooks 'org-occur-hook)
  8379. (if (interactive-p)
  8380. (message "%d match(es) for regexp %s" cnt regexp))
  8381. cnt))
  8382. (defun org-show-context (&optional key)
  8383. "Make sure point and context and visible.
  8384. How much context is shown depends upon the variables
  8385. `org-show-hierarchy-above', `org-show-following-heading'. and
  8386. `org-show-siblings'."
  8387. (let ((heading-p (org-on-heading-p t))
  8388. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8389. (following-p (org-get-alist-option org-show-following-heading key))
  8390. (entry-p (org-get-alist-option org-show-entry-below key))
  8391. (siblings-p (org-get-alist-option org-show-siblings key)))
  8392. (catch 'exit
  8393. ;; Show heading or entry text
  8394. (if (and heading-p (not entry-p))
  8395. (org-flag-heading nil) ; only show the heading
  8396. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8397. (org-show-hidden-entry))) ; show entire entry
  8398. (when following-p
  8399. ;; Show next sibling, or heading below text
  8400. (save-excursion
  8401. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8402. (org-flag-heading nil))))
  8403. (when siblings-p (org-show-siblings))
  8404. (when hierarchy-p
  8405. ;; show all higher headings, possibly with siblings
  8406. (save-excursion
  8407. (while (and (condition-case nil
  8408. (progn (org-up-heading-all 1) t)
  8409. (error nil))
  8410. (not (bobp)))
  8411. (org-flag-heading nil)
  8412. (when siblings-p (org-show-siblings))))))))
  8413. (defun org-reveal (&optional siblings)
  8414. "Show current entry, hierarchy above it, and the following headline.
  8415. This can be used to show a consistent set of context around locations
  8416. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8417. not t for the search context.
  8418. With optional argument SIBLINGS, on each level of the hierarchy all
  8419. siblings are shown. This repairs the tree structure to what it would
  8420. look like when opened with hierarchical calls to `org-cycle'."
  8421. (interactive "P")
  8422. (let ((org-show-hierarchy-above t)
  8423. (org-show-following-heading t)
  8424. (org-show-siblings (if siblings t org-show-siblings)))
  8425. (org-show-context nil)))
  8426. (defun org-highlight-new-match (beg end)
  8427. "Highlight from BEG to END and mark the highlight is an occur headline."
  8428. (let ((ov (org-make-overlay beg end)))
  8429. (org-overlay-put ov 'face 'secondary-selection)
  8430. (push ov org-occur-highlights)))
  8431. (defun org-remove-occur-highlights (&optional beg end noremove)
  8432. "Remove the occur highlights from the buffer.
  8433. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8434. from the `before-change-functions' in the current buffer."
  8435. (interactive)
  8436. (unless org-inhibit-highlight-removal
  8437. (mapc 'org-delete-overlay org-occur-highlights)
  8438. (setq org-occur-highlights nil)
  8439. (setq org-occur-parameters nil)
  8440. (unless noremove
  8441. (remove-hook 'before-change-functions
  8442. 'org-remove-occur-highlights 'local))))
  8443. ;;;; Priorities
  8444. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8445. "Regular expression matching the priority indicator.")
  8446. (defvar org-remove-priority-next-time nil)
  8447. (defun org-priority-up ()
  8448. "Increase the priority of the current item."
  8449. (interactive)
  8450. (org-priority 'up))
  8451. (defun org-priority-down ()
  8452. "Decrease the priority of the current item."
  8453. (interactive)
  8454. (org-priority 'down))
  8455. (defun org-priority (&optional action)
  8456. "Change the priority of an item by ARG.
  8457. ACTION can be `set', `up', `down', or a character."
  8458. (interactive)
  8459. (setq action (or action 'set))
  8460. (let (current new news have remove)
  8461. (save-excursion
  8462. (org-back-to-heading)
  8463. (if (looking-at org-priority-regexp)
  8464. (setq current (string-to-char (match-string 2))
  8465. have t)
  8466. (setq current org-default-priority))
  8467. (cond
  8468. ((or (eq action 'set)
  8469. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8470. (if (not (eq action 'set))
  8471. (setq new action)
  8472. (message "Priority %c-%c, SPC to remove: "
  8473. org-highest-priority org-lowest-priority)
  8474. (setq new (read-char-exclusive)))
  8475. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8476. (= (upcase org-lowest-priority) org-lowest-priority))
  8477. (setq new (upcase new)))
  8478. (cond ((equal new ?\ ) (setq remove t))
  8479. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8480. (error "Priority must be between `%c' and `%c'"
  8481. org-highest-priority org-lowest-priority))))
  8482. ((eq action 'up)
  8483. (if (and (not have) (eq last-command this-command))
  8484. (setq new org-lowest-priority)
  8485. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8486. org-default-priority (1- current)))))
  8487. ((eq action 'down)
  8488. (if (and (not have) (eq last-command this-command))
  8489. (setq new org-highest-priority)
  8490. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8491. org-default-priority (1+ current)))))
  8492. (t (error "Invalid action")))
  8493. (if (or (< (upcase new) org-highest-priority)
  8494. (> (upcase new) org-lowest-priority))
  8495. (setq remove t))
  8496. (setq news (format "%c" new))
  8497. (if have
  8498. (if remove
  8499. (replace-match "" t t nil 1)
  8500. (replace-match news t t nil 2))
  8501. (if remove
  8502. (error "No priority cookie found in line")
  8503. (looking-at org-todo-line-regexp)
  8504. (if (match-end 2)
  8505. (progn
  8506. (goto-char (match-end 2))
  8507. (insert " [#" news "]"))
  8508. (goto-char (match-beginning 3))
  8509. (insert "[#" news "] ")))))
  8510. (org-preserve-lc (org-set-tags nil 'align))
  8511. (if remove
  8512. (message "Priority removed")
  8513. (message "Priority of current item set to %s" news))))
  8514. (defun org-get-priority (s)
  8515. "Find priority cookie and return priority."
  8516. (save-match-data
  8517. (if (not (string-match org-priority-regexp s))
  8518. (* 1000 (- org-lowest-priority org-default-priority))
  8519. (* 1000 (- org-lowest-priority
  8520. (string-to-char (match-string 2 s)))))))
  8521. ;;;; Tags
  8522. (defvar org-agenda-archives-mode)
  8523. (defun org-scan-tags (action matcher &optional todo-only)
  8524. "Scan headline tags with inheritance and produce output ACTION.
  8525. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8526. or `agenda' to produce an entry list for an agenda view. It can also be
  8527. a Lisp form or a function that should be called at each matched headline, in
  8528. this case the return value is a list of all return values from these calls.
  8529. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8530. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8531. only lines with a TODO keyword are included in the output."
  8532. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8533. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8534. (org-re
  8535. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8536. (props (list 'face 'default
  8537. 'done-face 'org-done
  8538. 'undone-face 'default
  8539. 'mouse-face 'highlight
  8540. 'org-not-done-regexp org-not-done-regexp
  8541. 'org-todo-regexp org-todo-regexp
  8542. 'keymap org-agenda-keymap
  8543. 'help-echo
  8544. (format "mouse-2 or RET jump to org file %s"
  8545. (abbreviate-file-name
  8546. (or (buffer-file-name (buffer-base-buffer))
  8547. (buffer-name (buffer-base-buffer)))))))
  8548. (case-fold-search nil)
  8549. lspos tags tags-list
  8550. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8551. (llast 0) rtn rtn1 level category i txt
  8552. todo marker entry priority)
  8553. (when (not (member action '(agenda sparse-tree)))
  8554. (setq action (list 'lambda nil action)))
  8555. (save-excursion
  8556. (goto-char (point-min))
  8557. (when (eq action 'sparse-tree)
  8558. (org-overview)
  8559. (org-remove-occur-highlights))
  8560. (while (re-search-forward re nil t)
  8561. (catch :skip
  8562. (setq todo (if (match-end 1) (match-string 2))
  8563. tags (if (match-end 4) (match-string 4)))
  8564. (goto-char (setq lspos (1+ (match-beginning 0))))
  8565. (setq level (org-reduced-level (funcall outline-level))
  8566. category (org-get-category))
  8567. (setq i llast llast level)
  8568. ;; remove tag lists from same and sublevels
  8569. (while (>= i level)
  8570. (when (setq entry (assoc i tags-alist))
  8571. (setq tags-alist (delete entry tags-alist)))
  8572. (setq i (1- i)))
  8573. ;; add the next tags
  8574. (when tags
  8575. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8576. tags-alist
  8577. (cons (cons level tags) tags-alist)))
  8578. ;; compile tags for current headline
  8579. (setq tags-list
  8580. (if org-use-tag-inheritance
  8581. (apply 'append (mapcar 'cdr tags-alist))
  8582. tags))
  8583. (when (and tags org-use-tag-inheritance
  8584. (not (eq t org-use-tag-inheritance)))
  8585. ;; selective inheritance, remove uninherited ones
  8586. (setcdr (car tags-alist)
  8587. (org-remove-uniherited-tags (cdar tags-alist))))
  8588. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8589. (eval matcher)
  8590. (or
  8591. (not (member org-archive-tag tags-list))
  8592. ;; we have an archive tag, should we use this anyway?
  8593. (or (not org-agenda-skip-archived-trees)
  8594. (and (eq action 'agenda) org-agenda-archives-mode))))
  8595. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8596. ;; select this headline
  8597. (cond
  8598. ((eq action 'sparse-tree)
  8599. (and org-highlight-sparse-tree-matches
  8600. (org-get-heading) (match-end 0)
  8601. (org-highlight-new-match
  8602. (match-beginning 0) (match-beginning 1)))
  8603. (org-show-context 'tags-tree))
  8604. ((eq action 'agenda)
  8605. (setq txt (org-format-agenda-item
  8606. ""
  8607. (concat
  8608. (if org-tags-match-list-sublevels
  8609. (make-string (1- level) ?.) "")
  8610. (org-get-heading))
  8611. category tags-list)
  8612. priority (org-get-priority txt))
  8613. (goto-char lspos)
  8614. (setq marker (org-agenda-new-marker))
  8615. (org-add-props txt props
  8616. 'org-marker marker 'org-hd-marker marker 'org-category category
  8617. 'priority priority 'type "tagsmatch")
  8618. (push txt rtn))
  8619. ((functionp action)
  8620. (save-excursion
  8621. (setq rtn1 (funcall action))
  8622. (push rtn1 rtn))
  8623. (goto-char (point-at-eol)))
  8624. (t (error "Invalid action")))
  8625. ;; if we are to skip sublevels, jump to end of subtree
  8626. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8627. (when (and (eq action 'sparse-tree)
  8628. (not org-sparse-tree-open-archived-trees))
  8629. (org-hide-archived-subtrees (point-min) (point-max)))
  8630. (nreverse rtn)))
  8631. (defun org-remove-uniherited-tags (tags)
  8632. "Remove all tags that are not inherited from the list TAGS."
  8633. (cond
  8634. ((eq org-use-tag-inheritance t) tags)
  8635. ((not org-use-tag-inheritance) nil)
  8636. ((stringp org-use-tag-inheritance)
  8637. (delq nil (mapcar
  8638. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8639. tags)))
  8640. ((listp org-use-tag-inheritance)
  8641. (org-delete-all org-use-tag-inheritance tags))))
  8642. (defvar todo-only) ;; dynamically scoped
  8643. (defun org-tags-sparse-tree (&optional todo-only match)
  8644. "Create a sparse tree according to tags string MATCH.
  8645. MATCH can contain positive and negative selection of tags, like
  8646. \"+WORK+URGENT-WITHBOSS\".
  8647. If optional argument TODO_ONLY is non-nil, only select lines that are
  8648. also TODO lines."
  8649. (interactive "P")
  8650. (org-prepare-agenda-buffers (list (current-buffer)))
  8651. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8652. (defvar org-cached-props nil)
  8653. (defun org-cached-entry-get (pom property)
  8654. (if (or (eq t org-use-property-inheritance)
  8655. (and (stringp org-use-property-inheritance)
  8656. (string-match org-use-property-inheritance property))
  8657. (and (listp org-use-property-inheritance)
  8658. (member property org-use-property-inheritance)))
  8659. ;; Caching is not possible, check it directly
  8660. (org-entry-get pom property 'inherit)
  8661. ;; Get all properties, so that we can do complicated checks easily
  8662. (cdr (assoc property (or org-cached-props
  8663. (setq org-cached-props
  8664. (org-entry-properties pom)))))))
  8665. (defun org-global-tags-completion-table (&optional files)
  8666. "Return the list of all tags in all agenda buffer/files."
  8667. (save-excursion
  8668. (org-uniquify
  8669. (delq nil
  8670. (apply 'append
  8671. (mapcar
  8672. (lambda (file)
  8673. (set-buffer (find-file-noselect file))
  8674. (append (org-get-buffer-tags)
  8675. (mapcar (lambda (x) (if (stringp (car-safe x))
  8676. (list (car-safe x)) nil))
  8677. org-tag-alist)))
  8678. (if (and files (car files))
  8679. files
  8680. (org-agenda-files))))))))
  8681. (defun org-make-tags-matcher (match)
  8682. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8683. ;; todo-only is scoped dynamically into this function, and the function
  8684. ;; may change it it the matcher asksk for it.
  8685. (unless match
  8686. ;; Get a new match request, with completion
  8687. (let ((org-last-tags-completion-table
  8688. (org-global-tags-completion-table)))
  8689. (setq match (completing-read
  8690. "Match: " 'org-tags-completion-function nil nil nil
  8691. 'org-tags-history))))
  8692. ;; Parse the string and create a lisp form
  8693. (let ((match0 match)
  8694. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8695. minus tag mm
  8696. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8697. orterms term orlist re-p str-p level-p level-op time-p
  8698. prop-p pn pv po cat-p gv)
  8699. (if (string-match "/+" match)
  8700. ;; match contains also a todo-matching request
  8701. (progn
  8702. (setq tagsmatch (substring match 0 (match-beginning 0))
  8703. todomatch (substring match (match-end 0)))
  8704. (if (string-match "^!" todomatch)
  8705. (setq todo-only t todomatch (substring todomatch 1)))
  8706. (if (string-match "^\\s-*$" todomatch)
  8707. (setq todomatch nil)))
  8708. ;; only matching tags
  8709. (setq tagsmatch match todomatch nil))
  8710. ;; Make the tags matcher
  8711. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8712. (setq tagsmatcher t)
  8713. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8714. (while (setq term (pop orterms))
  8715. (while (and (equal (substring term -1) "\\") orterms)
  8716. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8717. (while (string-match re term)
  8718. (setq minus (and (match-end 1)
  8719. (equal (match-string 1 term) "-"))
  8720. tag (match-string 2 term)
  8721. re-p (equal (string-to-char tag) ?{)
  8722. level-p (match-end 4)
  8723. prop-p (match-end 5)
  8724. mm (cond
  8725. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8726. (level-p
  8727. (setq level-op (org-op-to-function (match-string 3 term)))
  8728. `(,level-op level ,(string-to-number
  8729. (match-string 4 term))))
  8730. (prop-p
  8731. (setq pn (match-string 5 term)
  8732. po (match-string 6 term)
  8733. pv (match-string 7 term)
  8734. cat-p (equal pn "CATEGORY")
  8735. re-p (equal (string-to-char pv) ?{)
  8736. str-p (equal (string-to-char pv) ?\")
  8737. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8738. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8739. (if time-p (setq pv (org-matcher-time pv)))
  8740. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8741. (if (equal pn "CATEGORY")
  8742. (setq gv '(get-text-property (point) 'org-category))
  8743. (setq gv `(org-cached-entry-get nil ,pn)))
  8744. (if re-p
  8745. (if (eq po 'org<>)
  8746. `(not (string-match ,pv (or ,gv "")))
  8747. `(string-match ,pv (or ,gv "")))
  8748. (if str-p
  8749. `(,po (or ,gv "") ,pv)
  8750. `(,po (string-to-number (or ,gv ""))
  8751. ,(string-to-number pv) ))))
  8752. (t `(member ,(downcase tag) tags-list)))
  8753. mm (if minus (list 'not mm) mm)
  8754. term (substring term (match-end 0)))
  8755. (push mm tagsmatcher))
  8756. (push (if (> (length tagsmatcher) 1)
  8757. (cons 'and tagsmatcher)
  8758. (car tagsmatcher))
  8759. orlist)
  8760. (setq tagsmatcher nil))
  8761. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8762. (setq tagsmatcher
  8763. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8764. ;; Make the todo matcher
  8765. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8766. (setq todomatcher t)
  8767. (setq orterms (org-split-string todomatch "|") orlist nil)
  8768. (while (setq term (pop orterms))
  8769. (while (string-match re term)
  8770. (setq minus (and (match-end 1)
  8771. (equal (match-string 1 term) "-"))
  8772. kwd (match-string 2 term)
  8773. re-p (equal (string-to-char kwd) ?{)
  8774. term (substring term (match-end 0))
  8775. mm (if re-p
  8776. `(string-match ,(substring kwd 1 -1) todo)
  8777. (list 'equal 'todo kwd))
  8778. mm (if minus (list 'not mm) mm))
  8779. (push mm todomatcher))
  8780. (push (if (> (length todomatcher) 1)
  8781. (cons 'and todomatcher)
  8782. (car todomatcher))
  8783. orlist)
  8784. (setq todomatcher nil))
  8785. (setq todomatcher (if (> (length orlist) 1)
  8786. (cons 'or orlist) (car orlist))))
  8787. ;; Return the string and lisp forms of the matcher
  8788. (setq matcher (if todomatcher
  8789. (list 'and tagsmatcher todomatcher)
  8790. tagsmatcher))
  8791. (cons match0 matcher)))
  8792. (defun org-op-to-function (op &optional stringp)
  8793. "Turn an operator into the appropriate function."
  8794. (setq op
  8795. (cond
  8796. ((equal op "<" ) '(< string< org-time<))
  8797. ((equal op ">" ) '(> org-string> org-time>))
  8798. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8799. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8800. ((member op '("=" "==")) '(= string= org-time=))
  8801. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8802. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8803. (defun org<> (a b) (not (= a b)))
  8804. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8805. (defun org-string>= (a b) (not (string< a b)))
  8806. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8807. (defun org-string<> (a b) (not (string= a b)))
  8808. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8809. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8810. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8811. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8812. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8813. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8814. (defun org-2ft (s)
  8815. "Convert S to a floating point time.
  8816. If S is already a number, just return it. If it is a string, parse
  8817. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8818. (cond
  8819. ((numberp s) s)
  8820. ((stringp s)
  8821. (condition-case nil
  8822. (float-time (apply 'encode-time (org-parse-time-string s)))
  8823. (error 0.)))
  8824. (t 0.)))
  8825. (defun org-matcher-time (s)
  8826. (cond
  8827. ((equal s "<now>") (float-time))
  8828. ((equal s "<today>")
  8829. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8830. (t (org-2ft s))))
  8831. (defun org-match-any-p (re list)
  8832. "Does re match any element of list?"
  8833. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8834. (delq nil list))
  8835. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8836. (defvar org-tags-overlay (org-make-overlay 1 1))
  8837. (org-detach-overlay org-tags-overlay)
  8838. (defun org-get-tags-at (&optional pos)
  8839. "Get a list of all headline tags applicable at POS.
  8840. POS defaults to point. If tags are inherited, the list contains
  8841. the targets in the same sequence as the headlines appear, i.e.
  8842. the tags of the current headline come last."
  8843. (interactive)
  8844. (let (tags ltags lastpos parent)
  8845. (save-excursion
  8846. (save-restriction
  8847. (widen)
  8848. (goto-char (or pos (point)))
  8849. (save-match-data
  8850. (condition-case nil
  8851. (progn
  8852. (org-back-to-heading t)
  8853. (while (not (equal lastpos (point)))
  8854. (setq lastpos (point))
  8855. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8856. (setq ltags (org-split-string
  8857. (org-match-string-no-properties 1) ":"))
  8858. (setq tags (append (org-remove-uniherited-tags ltags)
  8859. tags)))
  8860. (or org-use-tag-inheritance (error ""))
  8861. (org-up-heading-all 1)
  8862. (setq parent t)))
  8863. (error nil))))
  8864. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8865. (defun org-toggle-tag (tag &optional onoff)
  8866. "Toggle the tag TAG for the current line.
  8867. If ONOFF is `on' or `off', don't toggle but set to this state."
  8868. (unless (org-on-heading-p t) (error "Not on headling"))
  8869. (let (res current)
  8870. (save-excursion
  8871. (beginning-of-line)
  8872. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8873. (point-at-eol) t)
  8874. (progn
  8875. (setq current (match-string 1))
  8876. (replace-match ""))
  8877. (setq current ""))
  8878. (setq current (nreverse (org-split-string current ":")))
  8879. (cond
  8880. ((eq onoff 'on)
  8881. (setq res t)
  8882. (or (member tag current) (push tag current)))
  8883. ((eq onoff 'off)
  8884. (or (not (member tag current)) (setq current (delete tag current))))
  8885. (t (if (member tag current)
  8886. (setq current (delete tag current))
  8887. (setq res t)
  8888. (push tag current))))
  8889. (end-of-line 1)
  8890. (if current
  8891. (progn
  8892. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8893. (org-set-tags nil t))
  8894. (delete-horizontal-space))
  8895. (run-hooks 'org-after-tags-change-hook))
  8896. res))
  8897. (defun org-align-tags-here (to-col)
  8898. ;; Assumes that this is a headline
  8899. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8900. (beginning-of-line 1)
  8901. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8902. (< pos (match-beginning 2)))
  8903. (progn
  8904. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8905. (goto-char (match-beginning 1))
  8906. (insert " ")
  8907. (delete-region (point) (1+ (match-beginning 2)))
  8908. (setq ncol (max (1+ (current-column))
  8909. (1+ col)
  8910. (if (> to-col 0)
  8911. to-col
  8912. (- (abs to-col) tags-l))))
  8913. (setq p (point))
  8914. (insert (make-string (- ncol (current-column)) ?\ ))
  8915. (setq ncol (current-column))
  8916. (when indent-tabs-mode (tabify p (point-at-eol)))
  8917. (org-move-to-column (min ncol col) t))
  8918. (goto-char pos))))
  8919. (defun org-set-tags (&optional arg just-align)
  8920. "Set the tags for the current headline.
  8921. With prefix ARG, realign all tags in headings in the current buffer."
  8922. (interactive "P")
  8923. (let* ((re (concat "^" outline-regexp))
  8924. (current (org-get-tags-string))
  8925. (col (current-column))
  8926. (org-setting-tags t)
  8927. table current-tags inherited-tags ; computed below when needed
  8928. tags p0 c0 c1 rpl)
  8929. (if arg
  8930. (save-excursion
  8931. (goto-char (point-min))
  8932. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8933. (while (re-search-forward re nil t)
  8934. (org-set-tags nil t)
  8935. (end-of-line 1)))
  8936. (message "All tags realigned to column %d" org-tags-column))
  8937. (if just-align
  8938. (setq tags current)
  8939. ;; Get a new set of tags from the user
  8940. (save-excursion
  8941. (setq table (or org-tag-alist (org-get-buffer-tags))
  8942. org-last-tags-completion-table table
  8943. current-tags (org-split-string current ":")
  8944. inherited-tags (nreverse
  8945. (nthcdr (length current-tags)
  8946. (nreverse (org-get-tags-at))))
  8947. tags
  8948. (if (or (eq t org-use-fast-tag-selection)
  8949. (and org-use-fast-tag-selection
  8950. (delq nil (mapcar 'cdr table))))
  8951. (org-fast-tag-selection
  8952. current-tags inherited-tags table
  8953. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8954. (let ((org-add-colon-after-tag-completion t))
  8955. (org-trim
  8956. (org-without-partial-completion
  8957. (completing-read "Tags: " 'org-tags-completion-function
  8958. nil nil current 'org-tags-history)))))))
  8959. (while (string-match "[-+&]+" tags)
  8960. ;; No boolean logic, just a list
  8961. (setq tags (replace-match ":" t t tags))))
  8962. (if (string-match "\\`[\t ]*\\'" tags)
  8963. (setq tags "")
  8964. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8965. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8966. ;; Insert new tags at the correct column
  8967. (beginning-of-line 1)
  8968. (cond
  8969. ((and (equal current "") (equal tags "")))
  8970. ((re-search-forward
  8971. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8972. (point-at-eol) t)
  8973. (if (equal tags "")
  8974. (setq rpl "")
  8975. (goto-char (match-beginning 0))
  8976. (setq c0 (current-column) p0 (point)
  8977. c1 (max (1+ c0) (if (> org-tags-column 0)
  8978. org-tags-column
  8979. (- (- org-tags-column) (length tags))))
  8980. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8981. (replace-match rpl t t)
  8982. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8983. tags)
  8984. (t (error "Tags alignment failed")))
  8985. (org-move-to-column col)
  8986. (unless just-align
  8987. (run-hooks 'org-after-tags-change-hook)))))
  8988. (defun org-change-tag-in-region (beg end tag off)
  8989. "Add or remove TAG for each entry in the region.
  8990. This works in the agenda, and also in an org-mode buffer."
  8991. (interactive
  8992. (list (region-beginning) (region-end)
  8993. (let ((org-last-tags-completion-table
  8994. (if (org-mode-p)
  8995. (org-get-buffer-tags)
  8996. (org-global-tags-completion-table))))
  8997. (completing-read
  8998. "Tag: " 'org-tags-completion-function nil nil nil
  8999. 'org-tags-history))
  9000. (progn
  9001. (message "[s]et or [r]emove? ")
  9002. (equal (read-char-exclusive) ?r))))
  9003. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9004. (let ((agendap (equal major-mode 'org-agenda-mode))
  9005. l1 l2 m buf pos newhead (cnt 0))
  9006. (goto-char end)
  9007. (setq l2 (1- (org-current-line)))
  9008. (goto-char beg)
  9009. (setq l1 (org-current-line))
  9010. (loop for l from l1 to l2 do
  9011. (goto-line l)
  9012. (setq m (get-text-property (point) 'org-hd-marker))
  9013. (when (or (and (org-mode-p) (org-on-heading-p))
  9014. (and agendap m))
  9015. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9016. pos (if agendap m (point)))
  9017. (with-current-buffer buf
  9018. (save-excursion
  9019. (save-restriction
  9020. (goto-char pos)
  9021. (setq cnt (1+ cnt))
  9022. (org-toggle-tag tag (if off 'off 'on))
  9023. (setq newhead (org-get-heading)))))
  9024. (and agendap (org-agenda-change-all-lines newhead m))))
  9025. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9026. (defun org-tags-completion-function (string predicate &optional flag)
  9027. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9028. (confirm (lambda (x) (stringp (car x)))))
  9029. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9030. (setq s1 (match-string 1 string)
  9031. s2 (match-string 2 string))
  9032. (setq s1 "" s2 string))
  9033. (cond
  9034. ((eq flag nil)
  9035. ;; try completion
  9036. (setq rtn (try-completion s2 ctable confirm))
  9037. (if (stringp rtn)
  9038. (setq rtn
  9039. (concat s1 s2 (substring rtn (length s2))
  9040. (if (and org-add-colon-after-tag-completion
  9041. (assoc rtn ctable))
  9042. ":" ""))))
  9043. rtn)
  9044. ((eq flag t)
  9045. ;; all-completions
  9046. (all-completions s2 ctable confirm)
  9047. )
  9048. ((eq flag 'lambda)
  9049. ;; exact match?
  9050. (assoc s2 ctable)))
  9051. ))
  9052. (defun org-fast-tag-insert (kwd tags face &optional end)
  9053. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  9054. (insert (format "%-12s" (concat kwd ":"))
  9055. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9056. (or end "")))
  9057. (defun org-fast-tag-show-exit (flag)
  9058. (save-excursion
  9059. (goto-line 3)
  9060. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9061. (replace-match ""))
  9062. (when flag
  9063. (end-of-line 1)
  9064. (org-move-to-column (- (window-width) 19) t)
  9065. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9066. (defun org-set-current-tags-overlay (current prefix)
  9067. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9068. (if (featurep 'xemacs)
  9069. (org-overlay-display org-tags-overlay (concat prefix s)
  9070. 'secondary-selection)
  9071. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9072. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9073. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9074. "Fast tag selection with single keys.
  9075. CURRENT is the current list of tags in the headline, INHERITED is the
  9076. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9077. possibly with grouping information. TODO-TABLE is a similar table with
  9078. TODO keywords, should these have keys assigned to them.
  9079. If the keys are nil, a-z are automatically assigned.
  9080. Returns the new tags string, or nil to not change the current settings."
  9081. (let* ((fulltable (append table todo-table))
  9082. (maxlen (apply 'max (mapcar
  9083. (lambda (x)
  9084. (if (stringp (car x)) (string-width (car x)) 0))
  9085. fulltable)))
  9086. (buf (current-buffer))
  9087. (expert (eq org-fast-tag-selection-single-key 'expert))
  9088. (buffer-tags nil)
  9089. (fwidth (+ maxlen 3 1 3))
  9090. (ncol (/ (- (window-width) 4) fwidth))
  9091. (i-face 'org-done)
  9092. (c-face 'org-todo)
  9093. tg cnt e c char c1 c2 ntable tbl rtn
  9094. ov-start ov-end ov-prefix
  9095. (exit-after-next org-fast-tag-selection-single-key)
  9096. (done-keywords org-done-keywords)
  9097. groups ingroup)
  9098. (save-excursion
  9099. (beginning-of-line 1)
  9100. (if (looking-at
  9101. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9102. (setq ov-start (match-beginning 1)
  9103. ov-end (match-end 1)
  9104. ov-prefix "")
  9105. (setq ov-start (1- (point-at-eol))
  9106. ov-end (1+ ov-start))
  9107. (skip-chars-forward "^\n\r")
  9108. (setq ov-prefix
  9109. (concat
  9110. (buffer-substring (1- (point)) (point))
  9111. (if (> (current-column) org-tags-column)
  9112. " "
  9113. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9114. (org-move-overlay org-tags-overlay ov-start ov-end)
  9115. (save-window-excursion
  9116. (if expert
  9117. (set-buffer (get-buffer-create " *Org tags*"))
  9118. (delete-other-windows)
  9119. (split-window-vertically)
  9120. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9121. (erase-buffer)
  9122. (org-set-local 'org-done-keywords done-keywords)
  9123. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9124. (org-fast-tag-insert "Current" current c-face "\n\n")
  9125. (org-fast-tag-show-exit exit-after-next)
  9126. (org-set-current-tags-overlay current ov-prefix)
  9127. (setq tbl fulltable char ?a cnt 0)
  9128. (while (setq e (pop tbl))
  9129. (cond
  9130. ((equal e '(:startgroup))
  9131. (push '() groups) (setq ingroup t)
  9132. (when (not (= cnt 0))
  9133. (setq cnt 0)
  9134. (insert "\n"))
  9135. (insert "{ "))
  9136. ((equal e '(:endgroup))
  9137. (setq ingroup nil cnt 0)
  9138. (insert "}\n"))
  9139. (t
  9140. (setq tg (car e) c2 nil)
  9141. (if (cdr e)
  9142. (setq c (cdr e))
  9143. ;; automatically assign a character.
  9144. (setq c1 (string-to-char
  9145. (downcase (substring
  9146. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9147. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9148. (while (or (rassoc char ntable) (rassoc char table))
  9149. (setq char (1+ char)))
  9150. (setq c2 c1))
  9151. (setq c (or c2 char)))
  9152. (if ingroup (push tg (car groups)))
  9153. (setq tg (org-add-props tg nil 'face
  9154. (cond
  9155. ((not (assoc tg table))
  9156. (org-get-todo-face tg))
  9157. ((member tg current) c-face)
  9158. ((member tg inherited) i-face)
  9159. (t nil))))
  9160. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9161. (insert "[" c "] " tg (make-string
  9162. (- fwidth 4 (length tg)) ?\ ))
  9163. (push (cons tg c) ntable)
  9164. (when (= (setq cnt (1+ cnt)) ncol)
  9165. (insert "\n")
  9166. (if ingroup (insert " "))
  9167. (setq cnt 0)))))
  9168. (setq ntable (nreverse ntable))
  9169. (insert "\n")
  9170. (goto-char (point-min))
  9171. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  9172. (fit-window-to-buffer))
  9173. (setq rtn
  9174. (catch 'exit
  9175. (while t
  9176. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9177. (if groups " [!] no groups" " [!]groups")
  9178. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9179. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9180. (cond
  9181. ((= c ?\r) (throw 'exit t))
  9182. ((= c ?!)
  9183. (setq groups (not groups))
  9184. (goto-char (point-min))
  9185. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9186. ((= c ?\C-c)
  9187. (if (not expert)
  9188. (org-fast-tag-show-exit
  9189. (setq exit-after-next (not exit-after-next)))
  9190. (setq expert nil)
  9191. (delete-other-windows)
  9192. (split-window-vertically)
  9193. (org-switch-to-buffer-other-window " *Org tags*")
  9194. (and (fboundp 'fit-window-to-buffer)
  9195. (fit-window-to-buffer))))
  9196. ((or (= c ?\C-g)
  9197. (and (= c ?q) (not (rassoc c ntable))))
  9198. (org-detach-overlay org-tags-overlay)
  9199. (setq quit-flag t))
  9200. ((= c ?\ )
  9201. (setq current nil)
  9202. (if exit-after-next (setq exit-after-next 'now)))
  9203. ((= c ?\t)
  9204. (condition-case nil
  9205. (setq tg (completing-read
  9206. "Tag: "
  9207. (or buffer-tags
  9208. (with-current-buffer buf
  9209. (org-get-buffer-tags)))))
  9210. (quit (setq tg "")))
  9211. (when (string-match "\\S-" tg)
  9212. (add-to-list 'buffer-tags (list tg))
  9213. (if (member tg current)
  9214. (setq current (delete tg current))
  9215. (push tg current)))
  9216. (if exit-after-next (setq exit-after-next 'now)))
  9217. ((setq e (rassoc c todo-table) tg (car e))
  9218. (with-current-buffer buf
  9219. (save-excursion (org-todo tg)))
  9220. (if exit-after-next (setq exit-after-next 'now)))
  9221. ((setq e (rassoc c ntable) tg (car e))
  9222. (if (member tg current)
  9223. (setq current (delete tg current))
  9224. (loop for g in groups do
  9225. (if (member tg g)
  9226. (mapc (lambda (x)
  9227. (setq current (delete x current)))
  9228. g)))
  9229. (push tg current))
  9230. (if exit-after-next (setq exit-after-next 'now))))
  9231. ;; Create a sorted list
  9232. (setq current
  9233. (sort current
  9234. (lambda (a b)
  9235. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9236. (if (eq exit-after-next 'now) (throw 'exit t))
  9237. (goto-char (point-min))
  9238. (beginning-of-line 2)
  9239. (delete-region (point) (point-at-eol))
  9240. (org-fast-tag-insert "Current" current c-face)
  9241. (org-set-current-tags-overlay current ov-prefix)
  9242. (while (re-search-forward
  9243. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9244. (setq tg (match-string 1))
  9245. (add-text-properties
  9246. (match-beginning 1) (match-end 1)
  9247. (list 'face
  9248. (cond
  9249. ((member tg current) c-face)
  9250. ((member tg inherited) i-face)
  9251. (t (get-text-property (match-beginning 1) 'face))))))
  9252. (goto-char (point-min)))))
  9253. (org-detach-overlay org-tags-overlay)
  9254. (if rtn
  9255. (mapconcat 'identity current ":")
  9256. nil))))
  9257. (defun org-get-tags-string ()
  9258. "Get the TAGS string in the current headline."
  9259. (unless (org-on-heading-p t)
  9260. (error "Not on a heading"))
  9261. (save-excursion
  9262. (beginning-of-line 1)
  9263. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9264. (org-match-string-no-properties 1)
  9265. "")))
  9266. (defun org-get-tags ()
  9267. "Get the list of tags specified in the current headline."
  9268. (org-split-string (org-get-tags-string) ":"))
  9269. (defun org-get-buffer-tags ()
  9270. "Get a table of all tags used in the buffer, for completion."
  9271. (let (tags)
  9272. (save-excursion
  9273. (goto-char (point-min))
  9274. (while (re-search-forward
  9275. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9276. (when (equal (char-after (point-at-bol 0)) ?*)
  9277. (mapc (lambda (x) (add-to-list 'tags x))
  9278. (org-split-string (org-match-string-no-properties 1) ":")))))
  9279. (mapcar 'list tags)))
  9280. ;;;; The mapping API
  9281. ;;;###autoload
  9282. (defun org-map-entries (func &optional match scope &rest skip)
  9283. "Call FUNC at each headline selected by MATCH in SCOPE.
  9284. FUNC is a function or a lisp form. The function will be called without
  9285. arguments, with the cursor positioned at the beginning of the headline.
  9286. The return values of all calls to the function will be collected and
  9287. returned as a list.
  9288. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9289. Only headlines that are matched by this query will be considered during
  9290. the iteration. When MATCH is nil or t, all headlines will be
  9291. visited by the iteration.
  9292. SCOPE determines the scope of this command. It can be any of:
  9293. nil The current buffer, respecting the restriction if any
  9294. tree The subtree started with the entry at point
  9295. file The current buffer, without restriction
  9296. file-with-archives
  9297. The current buffer, and any archives associated with it
  9298. agenda All agenda files
  9299. agenda-with-archives
  9300. All agenda files with any archive files associated with them
  9301. \(file1 file2 ...)
  9302. If this is a list, all files in the list will be scanned
  9303. The remaining args are treated as settings for the skipping facilities of
  9304. the scanner. The following items can be given here:
  9305. archive skip trees with the archive tag.
  9306. comment skip trees with the COMMENT keyword
  9307. function or Emacs Lisp form:
  9308. will be used as value for `org-agenda-skip-function', so whenever
  9309. the the function returns t, FUNC will not be called for that
  9310. entry and search will continue from the point where the
  9311. function leaves it."
  9312. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9313. (org-agenda-skip-archived-trees (memq 'archive skip))
  9314. (org-agenda-skip-comment-trees (memq 'comment skip))
  9315. (org-agenda-skip-function
  9316. (car (org-delete-all '(comment archive) skip)))
  9317. (org-tags-match-list-sublevels t)
  9318. matcher pos file)
  9319. (cond
  9320. ((eq match t) (setq matcher t))
  9321. ((eq match nil) (setq matcher t))
  9322. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  9323. (when (eq scope 'tree)
  9324. (org-back-to-heading t)
  9325. (org-narrow-to-subtree)
  9326. (setq scope nil))
  9327. (if (not scope)
  9328. (progn
  9329. (org-prepare-agenda-buffers
  9330. (list (buffer-file-name (current-buffer))))
  9331. (org-scan-tags func matcher))
  9332. ;; Get the right scope
  9333. (setq pos (point))
  9334. (cond
  9335. ((and scope (listp scope) (symbolp (car scope)))
  9336. (setq scope (eval scope)))
  9337. ((eq scope 'agenda)
  9338. (setq scope (org-agenda-files t)))
  9339. ((eq scope 'agenda-with-archives)
  9340. (setq scope (org-agenda-files t))
  9341. (setq scope (org-add-archive-files scope)))
  9342. ((eq scope 'file)
  9343. (setq scope (list (buffer-file-name))))
  9344. ((eq scope 'file-with-archives)
  9345. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9346. (org-prepare-agenda-buffers scope)
  9347. (while (setq file (pop scope))
  9348. (with-current-buffer (org-find-base-buffer-visiting file)
  9349. (save-excursion
  9350. (save-restriction
  9351. (widen)
  9352. (goto-char (point-min))
  9353. (org-scan-tags func matcher))))))))
  9354. ;;;; Properties
  9355. ;;; Setting and retrieving properties
  9356. (defconst org-special-properties
  9357. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9358. "TIMESTAMP" "TIMESTAMP_IA")
  9359. "The special properties valid in Org-mode.
  9360. These are properties that are not defined in the property drawer,
  9361. but in some other way.")
  9362. (defconst org-default-properties
  9363. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9364. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9365. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9366. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9367. "Some properties that are used by Org-mode for various purposes.
  9368. Being in this list makes sure that they are offered for completion.")
  9369. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9370. "Regular expression matching the first line of a property drawer.")
  9371. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9372. "Regular expression matching the first line of a property drawer.")
  9373. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9374. "Regular expression matching the first line of a property drawer.")
  9375. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9376. "Regular expression matching the first line of a property drawer.")
  9377. (defconst org-property-drawer-re
  9378. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9379. org-property-end-re "\\)\n?")
  9380. "Matches an entire property drawer.")
  9381. (defconst org-clock-drawer-re
  9382. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9383. org-property-end-re "\\)\n?")
  9384. "Matches an entire clock drawer.")
  9385. (defun org-property-action ()
  9386. "Do an action on properties."
  9387. (interactive)
  9388. (let (c)
  9389. (org-at-property-p)
  9390. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9391. (setq c (read-char-exclusive))
  9392. (cond
  9393. ((equal c ?s)
  9394. (call-interactively 'org-set-property))
  9395. ((equal c ?d)
  9396. (call-interactively 'org-delete-property))
  9397. ((equal c ?D)
  9398. (call-interactively 'org-delete-property-globally))
  9399. ((equal c ?c)
  9400. (call-interactively 'org-compute-property-at-point))
  9401. (t (error "No such property action %c" c)))))
  9402. (defun org-at-property-p ()
  9403. "Is the cursor in a property line?"
  9404. ;; FIXME: Does not check if we are actually in the drawer.
  9405. ;; FIXME: also returns true on any drawers.....
  9406. ;; This is used by C-c C-c for property action.
  9407. (save-excursion
  9408. (beginning-of-line 1)
  9409. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9410. (defun org-get-property-block (&optional beg end force)
  9411. "Return the (beg . end) range of the body of the property drawer.
  9412. BEG and END can be beginning and end of subtree, if not given
  9413. they will be found.
  9414. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9415. (catch 'exit
  9416. (save-excursion
  9417. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9418. (end (or end (progn (outline-next-heading) (point)))))
  9419. (goto-char beg)
  9420. (if (re-search-forward org-property-start-re end t)
  9421. (setq beg (1+ (match-end 0)))
  9422. (if force
  9423. (save-excursion
  9424. (org-insert-property-drawer)
  9425. (setq end (progn (outline-next-heading) (point))))
  9426. (throw 'exit nil))
  9427. (goto-char beg)
  9428. (if (re-search-forward org-property-start-re end t)
  9429. (setq beg (1+ (match-end 0)))))
  9430. (if (re-search-forward org-property-end-re end t)
  9431. (setq end (match-beginning 0))
  9432. (or force (throw 'exit nil))
  9433. (goto-char beg)
  9434. (setq end beg)
  9435. (org-indent-line-function)
  9436. (insert ":END:\n"))
  9437. (cons beg end)))))
  9438. (defun org-entry-properties (&optional pom which)
  9439. "Get all properties of the entry at point-or-marker POM.
  9440. This includes the TODO keyword, the tags, time strings for deadline,
  9441. scheduled, and clocking, and any additional properties defined in the
  9442. entry. The return value is an alist, keys may occur multiple times
  9443. if the property key was used several times.
  9444. POM may also be nil, in which case the current entry is used.
  9445. If WHICH is nil or `all', get all properties. If WHICH is
  9446. `special' or `standard', only get that subclass."
  9447. (setq which (or which 'all))
  9448. (org-with-point-at pom
  9449. (let ((clockstr (substring org-clock-string 0 -1))
  9450. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9451. beg end range props sum-props key value string clocksum)
  9452. (save-excursion
  9453. (when (condition-case nil (org-back-to-heading t) (error nil))
  9454. (setq beg (point))
  9455. (setq sum-props (get-text-property (point) 'org-summaries))
  9456. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9457. (outline-next-heading)
  9458. (setq end (point))
  9459. (when (memq which '(all special))
  9460. ;; Get the special properties, like TODO and tags
  9461. (goto-char beg)
  9462. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9463. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9464. (when (looking-at org-priority-regexp)
  9465. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9466. (when (and (setq value (org-get-tags-string))
  9467. (string-match "\\S-" value))
  9468. (push (cons "TAGS" value) props))
  9469. (when (setq value (org-get-tags-at))
  9470. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9471. props))
  9472. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9473. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9474. string (if (equal key clockstr)
  9475. (org-no-properties
  9476. (org-trim
  9477. (buffer-substring
  9478. (match-beginning 3) (goto-char (point-at-eol)))))
  9479. (substring (org-match-string-no-properties 3) 1 -1)))
  9480. (unless key
  9481. (if (= (char-after (match-beginning 3)) ?\[)
  9482. (setq key "TIMESTAMP_IA")
  9483. (setq key "TIMESTAMP")))
  9484. (when (or (equal key clockstr) (not (assoc key props)))
  9485. (push (cons key string) props)))
  9486. )
  9487. (when (memq which '(all standard))
  9488. ;; Get the standard properties, like :PORP: ...
  9489. (setq range (org-get-property-block beg end))
  9490. (when range
  9491. (goto-char (car range))
  9492. (while (re-search-forward
  9493. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9494. (cdr range) t)
  9495. (setq key (org-match-string-no-properties 1)
  9496. value (org-trim (or (org-match-string-no-properties 2) "")))
  9497. (unless (member key excluded)
  9498. (push (cons key (or value "")) props)))))
  9499. (if clocksum
  9500. (push (cons "CLOCKSUM"
  9501. (org-columns-number-to-string (/ (float clocksum) 60.)
  9502. 'add_times))
  9503. props))
  9504. (append sum-props (nreverse props)))))))
  9505. (defun org-entry-get (pom property &optional inherit)
  9506. "Get value of PROPERTY for entry at point-or-marker POM.
  9507. If INHERIT is non-nil and the entry does not have the property,
  9508. then also check higher levels of the hierarchy.
  9509. If INHERIT is the symbol `selective', use inheritance only if the setting
  9510. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9511. If the property is present but empty, the return value is the empty string.
  9512. If the property is not present at all, nil is returned."
  9513. (org-with-point-at pom
  9514. (if (and inherit (if (eq inherit 'selective)
  9515. (org-property-inherit-p property)
  9516. t))
  9517. (org-entry-get-with-inheritance property)
  9518. (if (member property org-special-properties)
  9519. ;; We need a special property. Use brute force, get all properties.
  9520. (cdr (assoc property (org-entry-properties nil 'special)))
  9521. (let ((range (org-get-property-block)))
  9522. (if (and range
  9523. (goto-char (car range))
  9524. (re-search-forward
  9525. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9526. (cdr range) t))
  9527. ;; Found the property, return it.
  9528. (if (match-end 1)
  9529. (org-match-string-no-properties 1)
  9530. "")))))))
  9531. (defun org-property-or-variable-value (var &optional inherit)
  9532. "Check if there is a property fixing the value of VAR.
  9533. If yes, return this value. If not, return the current value of the variable."
  9534. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9535. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9536. (read prop)
  9537. (symbol-value var))))
  9538. (defun org-entry-delete (pom property)
  9539. "Delete the property PROPERTY from entry at point-or-marker POM."
  9540. (org-with-point-at pom
  9541. (if (member property org-special-properties)
  9542. nil ; cannot delete these properties.
  9543. (let ((range (org-get-property-block)))
  9544. (if (and range
  9545. (goto-char (car range))
  9546. (re-search-forward
  9547. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9548. (cdr range) t))
  9549. (progn
  9550. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9551. t)
  9552. nil)))))
  9553. ;; Multi-values properties are properties that contain multiple values
  9554. ;; These values are assumed to be single words, separated by whitespace.
  9555. (defun org-entry-add-to-multivalued-property (pom property value)
  9556. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9557. (let* ((old (org-entry-get pom property))
  9558. (values (and old (org-split-string old "[ \t]"))))
  9559. (unless (member value values)
  9560. (setq values (cons value values))
  9561. (org-entry-put pom property
  9562. (mapconcat 'identity values " ")))))
  9563. (defun org-entry-remove-from-multivalued-property (pom property value)
  9564. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9565. (let* ((old (org-entry-get pom property))
  9566. (values (and old (org-split-string old "[ \t]"))))
  9567. (when (member value values)
  9568. (setq values (delete value values))
  9569. (org-entry-put pom property
  9570. (mapconcat 'identity values " ")))))
  9571. (defun org-entry-member-in-multivalued-property (pom property value)
  9572. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9573. (let* ((old (org-entry-get pom property))
  9574. (values (and old (org-split-string old "[ \t]"))))
  9575. (member value values)))
  9576. (defvar org-entry-property-inherited-from (make-marker))
  9577. (defun org-entry-get-with-inheritance (property)
  9578. "Get entry property, and search higher levels if not present."
  9579. (let (tmp)
  9580. (save-excursion
  9581. (save-restriction
  9582. (widen)
  9583. (catch 'ex
  9584. (while t
  9585. (when (setq tmp (org-entry-get nil property))
  9586. (org-back-to-heading t)
  9587. (move-marker org-entry-property-inherited-from (point))
  9588. (throw 'ex tmp))
  9589. (or (org-up-heading-safe) (throw 'ex nil)))))
  9590. (or tmp
  9591. (cdr (assoc property org-file-properties))
  9592. (cdr (assoc property org-global-properties))
  9593. (cdr (assoc property org-global-properties-fixed))))))
  9594. (defun org-entry-put (pom property value)
  9595. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9596. (org-with-point-at pom
  9597. (org-back-to-heading t)
  9598. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9599. range)
  9600. (cond
  9601. ((equal property "TODO")
  9602. (when (and (stringp value) (string-match "\\S-" value)
  9603. (not (member value org-todo-keywords-1)))
  9604. (error "\"%s\" is not a valid TODO state" value))
  9605. (if (or (not value)
  9606. (not (string-match "\\S-" value)))
  9607. (setq value 'none))
  9608. (org-todo value)
  9609. (org-set-tags nil 'align))
  9610. ((equal property "PRIORITY")
  9611. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9612. (string-to-char value) ?\ ))
  9613. (org-set-tags nil 'align))
  9614. ((equal property "SCHEDULED")
  9615. (if (re-search-forward org-scheduled-time-regexp end t)
  9616. (cond
  9617. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9618. ((eq value 'later) (org-timestamp-change 1 'day))
  9619. (t (call-interactively 'org-schedule)))
  9620. (call-interactively 'org-schedule)))
  9621. ((equal property "DEADLINE")
  9622. (if (re-search-forward org-deadline-time-regexp end t)
  9623. (cond
  9624. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9625. ((eq value 'later) (org-timestamp-change 1 'day))
  9626. (t (call-interactively 'org-deadline)))
  9627. (call-interactively 'org-deadline)))
  9628. ((member property org-special-properties)
  9629. (error "The %s property can not yet be set with `org-entry-put'"
  9630. property))
  9631. (t ; a non-special property
  9632. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9633. (setq range (org-get-property-block beg end 'force))
  9634. (goto-char (car range))
  9635. (if (re-search-forward
  9636. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9637. (progn
  9638. (delete-region (match-beginning 1) (match-end 1))
  9639. (goto-char (match-beginning 1)))
  9640. (goto-char (cdr range))
  9641. (insert "\n")
  9642. (backward-char 1)
  9643. (org-indent-line-function)
  9644. (insert ":" property ":"))
  9645. (and value (insert " " value))
  9646. (org-indent-line-function)))))))
  9647. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9648. "Get all property keys in the current buffer.
  9649. With INCLUDE-SPECIALS, also list the special properties that relect things
  9650. like tags and TODO state.
  9651. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9652. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9653. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9654. formats in the current buffer."
  9655. (let (rtn range cfmt cols s p)
  9656. (save-excursion
  9657. (save-restriction
  9658. (widen)
  9659. (goto-char (point-min))
  9660. (while (re-search-forward org-property-start-re nil t)
  9661. (setq range (org-get-property-block))
  9662. (goto-char (car range))
  9663. (while (re-search-forward
  9664. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9665. (cdr range) t)
  9666. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9667. (outline-next-heading))))
  9668. (when include-specials
  9669. (setq rtn (append org-special-properties rtn)))
  9670. (when include-defaults
  9671. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9672. (when include-columns
  9673. (save-excursion
  9674. (save-restriction
  9675. (widen)
  9676. (goto-char (point-min))
  9677. (while (re-search-forward
  9678. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9679. nil t)
  9680. (setq cfmt (match-string 2) s 0)
  9681. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9682. cfmt s)
  9683. (setq s (match-end 0)
  9684. p (match-string 1 cfmt))
  9685. (unless (or (equal p "ITEM")
  9686. (member p org-special-properties))
  9687. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9688. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9689. (defun org-property-values (key)
  9690. "Return a list of all values of property KEY."
  9691. (save-excursion
  9692. (save-restriction
  9693. (widen)
  9694. (goto-char (point-min))
  9695. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9696. values)
  9697. (while (re-search-forward re nil t)
  9698. (add-to-list 'values (org-trim (match-string 1))))
  9699. (delete "" values)))))
  9700. (defun org-insert-property-drawer ()
  9701. "Insert a property drawer into the current entry."
  9702. (interactive)
  9703. (org-back-to-heading t)
  9704. (looking-at outline-regexp)
  9705. (let ((indent (- (match-end 0)(match-beginning 0)))
  9706. (beg (point))
  9707. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9708. end hiddenp)
  9709. (outline-next-heading)
  9710. (setq end (point))
  9711. (goto-char beg)
  9712. (while (re-search-forward re end t))
  9713. (setq hiddenp (org-invisible-p))
  9714. (end-of-line 1)
  9715. (and (equal (char-after) ?\n) (forward-char 1))
  9716. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9717. (beginning-of-line 2))
  9718. (org-skip-over-state-notes)
  9719. (skip-chars-backward " \t\n\r")
  9720. (if (eq (char-before) ?*) (forward-char 1))
  9721. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9722. (beginning-of-line 0)
  9723. (org-indent-to-column indent)
  9724. (beginning-of-line 2)
  9725. (org-indent-to-column indent)
  9726. (beginning-of-line 0)
  9727. (if hiddenp
  9728. (save-excursion
  9729. (org-back-to-heading t)
  9730. (hide-entry))
  9731. (org-flag-drawer t))))
  9732. (defun org-set-property (property value)
  9733. "In the current entry, set PROPERTY to VALUE.
  9734. When called interactively, this will prompt for a property name, offering
  9735. completion on existing and default properties. And then it will prompt
  9736. for a value, offering competion either on allowed values (via an inherited
  9737. xxx_ALL property) or on existing values in other instances of this property
  9738. in the current file."
  9739. (interactive
  9740. (let* ((completion-ignore-case t)
  9741. (keys (org-buffer-property-keys nil t t))
  9742. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9743. (prop (if (member prop0 keys)
  9744. prop0
  9745. (or (cdr (assoc (downcase prop0)
  9746. (mapcar (lambda (x) (cons (downcase x) x))
  9747. keys)))
  9748. prop0)))
  9749. (cur (org-entry-get nil prop))
  9750. (allowed (org-property-get-allowed-values nil prop 'table))
  9751. (existing (mapcar 'list (org-property-values prop)))
  9752. (val (if allowed
  9753. (org-completing-read "Value: " allowed nil 'req-match)
  9754. (org-completing-read
  9755. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9756. (concat "[" cur "]") "")
  9757. ": ")
  9758. existing nil nil "" nil cur))))
  9759. (list prop (if (equal val "") cur val))))
  9760. (unless (equal (org-entry-get nil property) value)
  9761. (org-entry-put nil property value)))
  9762. (defun org-delete-property (property)
  9763. "In the current entry, delete PROPERTY."
  9764. (interactive
  9765. (let* ((completion-ignore-case t)
  9766. (prop (completing-read
  9767. "Property: " (org-entry-properties nil 'standard))))
  9768. (list prop)))
  9769. (message "Property %s %s" property
  9770. (if (org-entry-delete nil property)
  9771. "deleted"
  9772. "was not present in the entry")))
  9773. (defun org-delete-property-globally (property)
  9774. "Remove PROPERTY globally, from all entries."
  9775. (interactive
  9776. (let* ((completion-ignore-case t)
  9777. (prop (completing-read
  9778. "Globally remove property: "
  9779. (mapcar 'list (org-buffer-property-keys)))))
  9780. (list prop)))
  9781. (save-excursion
  9782. (save-restriction
  9783. (widen)
  9784. (goto-char (point-min))
  9785. (let ((cnt 0))
  9786. (while (re-search-forward
  9787. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9788. nil t)
  9789. (setq cnt (1+ cnt))
  9790. (replace-match ""))
  9791. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9792. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9793. (defun org-compute-property-at-point ()
  9794. "Compute the property at point.
  9795. This looks for an enclosing column format, extracts the operator and
  9796. then applies it to the proerty in the column format's scope."
  9797. (interactive)
  9798. (unless (org-at-property-p)
  9799. (error "Not at a property"))
  9800. (let ((prop (org-match-string-no-properties 2)))
  9801. (org-columns-get-format-and-top-level)
  9802. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9803. (error "No operator defined for property %s" prop))
  9804. (org-columns-compute prop)))
  9805. (defun org-property-get-allowed-values (pom property &optional table)
  9806. "Get allowed values for the property PROPERTY.
  9807. When TABLE is non-nil, return an alist that can directly be used for
  9808. completion."
  9809. (let (vals)
  9810. (cond
  9811. ((equal property "TODO")
  9812. (setq vals (org-with-point-at pom
  9813. (append org-todo-keywords-1 '("")))))
  9814. ((equal property "PRIORITY")
  9815. (let ((n org-lowest-priority))
  9816. (while (>= n org-highest-priority)
  9817. (push (char-to-string n) vals)
  9818. (setq n (1- n)))))
  9819. ((member property org-special-properties))
  9820. (t
  9821. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9822. (when (and vals (string-match "\\S-" vals))
  9823. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9824. (setq vals (mapcar (lambda (x)
  9825. (cond ((stringp x) x)
  9826. ((numberp x) (number-to-string x))
  9827. ((symbolp x) (symbol-name x))
  9828. (t "???")))
  9829. vals)))))
  9830. (if table (mapcar 'list vals) vals)))
  9831. (defun org-property-previous-allowed-value (&optional previous)
  9832. "Switch to the next allowed value for this property."
  9833. (interactive)
  9834. (org-property-next-allowed-value t))
  9835. (defun org-property-next-allowed-value (&optional previous)
  9836. "Switch to the next allowed value for this property."
  9837. (interactive)
  9838. (unless (org-at-property-p)
  9839. (error "Not at a property"))
  9840. (let* ((key (match-string 2))
  9841. (value (match-string 3))
  9842. (allowed (or (org-property-get-allowed-values (point) key)
  9843. (and (member value '("[ ]" "[-]" "[X]"))
  9844. '("[ ]" "[X]"))))
  9845. nval)
  9846. (unless allowed
  9847. (error "Allowed values for this property have not been defined"))
  9848. (if previous (setq allowed (reverse allowed)))
  9849. (if (member value allowed)
  9850. (setq nval (car (cdr (member value allowed)))))
  9851. (setq nval (or nval (car allowed)))
  9852. (if (equal nval value)
  9853. (error "Only one allowed value for this property"))
  9854. (org-at-property-p)
  9855. (replace-match (concat " :" key ": " nval) t t)
  9856. (org-indent-line-function)
  9857. (beginning-of-line 1)
  9858. (skip-chars-forward " \t")))
  9859. (defun org-find-entry-with-id (ident)
  9860. "Locate the entry that contains the ID property with exact value IDENT.
  9861. IDENT can be a string, a symbol or a number, this function will search for
  9862. the string representation of it.
  9863. Return the position where this entry starts, or nil if there is no such entry."
  9864. (let ((id (cond
  9865. ((stringp ident) ident)
  9866. ((symbol-name ident) (symbol-name ident))
  9867. ((numberp ident) (number-to-string ident))
  9868. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9869. (case-fold-search nil))
  9870. (save-excursion
  9871. (save-restriction
  9872. (widen)
  9873. (goto-char (point-min))
  9874. (when (re-search-forward
  9875. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9876. nil t)
  9877. (org-back-to-heading)
  9878. (point))))))
  9879. ;;;; Timestamps
  9880. (defvar org-last-changed-timestamp nil)
  9881. (defvar org-last-inserted-timestamp nil
  9882. "The last time stamp inserted with `org-insert-time-stamp'.")
  9883. (defvar org-time-was-given) ; dynamically scoped parameter
  9884. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9885. (defvar org-ts-what) ; dynamically scoped parameter
  9886. (defun org-time-stamp (arg)
  9887. "Prompt for a date/time and insert a time stamp.
  9888. If the user specifies a time like HH:MM, or if this command is called
  9889. with a prefix argument, the time stamp will contain date and time.
  9890. Otherwise, only the date will be included. All parts of a date not
  9891. specified by the user will be filled in from the current date/time.
  9892. So if you press just return without typing anything, the time stamp
  9893. will represent the current date/time. If there is already a timestamp
  9894. at the cursor, it will be modified."
  9895. (interactive "P")
  9896. (let* ((ts nil)
  9897. (default-time
  9898. ;; Default time is either today, or, when entering a range,
  9899. ;; the range start.
  9900. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9901. (save-excursion
  9902. (re-search-backward
  9903. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9904. (- (point) 20) t)))
  9905. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9906. (current-time)))
  9907. (default-input (and ts (org-get-compact-tod ts)))
  9908. org-time-was-given org-end-time-was-given time)
  9909. (cond
  9910. ((and (org-at-timestamp-p)
  9911. (eq last-command 'org-time-stamp)
  9912. (eq this-command 'org-time-stamp))
  9913. (insert "--")
  9914. (setq time (let ((this-command this-command))
  9915. (org-read-date arg 'totime nil nil default-time default-input)))
  9916. (org-insert-time-stamp time (or org-time-was-given arg)))
  9917. ((org-at-timestamp-p)
  9918. (setq time (let ((this-command this-command))
  9919. (org-read-date arg 'totime nil nil default-time default-input)))
  9920. (when (org-at-timestamp-p) ; just to get the match data
  9921. (replace-match "")
  9922. (setq org-last-changed-timestamp
  9923. (org-insert-time-stamp
  9924. time (or org-time-was-given arg)
  9925. nil nil nil (list org-end-time-was-given))))
  9926. (message "Timestamp updated"))
  9927. (t
  9928. (setq time (let ((this-command this-command))
  9929. (org-read-date arg 'totime nil nil default-time default-input)))
  9930. (org-insert-time-stamp time (or org-time-was-given arg)
  9931. nil nil nil (list org-end-time-was-given))))))
  9932. ;; FIXME: can we use this for something else, like computing time differences?
  9933. (defun org-get-compact-tod (s)
  9934. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9935. (let* ((t1 (match-string 1 s))
  9936. (h1 (string-to-number (match-string 2 s)))
  9937. (m1 (string-to-number (match-string 3 s)))
  9938. (t2 (and (match-end 4) (match-string 5 s)))
  9939. (h2 (and t2 (string-to-number (match-string 6 s))))
  9940. (m2 (and t2 (string-to-number (match-string 7 s))))
  9941. dh dm)
  9942. (if (not t2)
  9943. t1
  9944. (setq dh (- h2 h1) dm (- m2 m1))
  9945. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9946. (concat t1 "+" (number-to-string dh)
  9947. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9948. (defun org-time-stamp-inactive (&optional arg)
  9949. "Insert an inactive time stamp.
  9950. An inactive time stamp is enclosed in square brackets instead of angle
  9951. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9952. does not link to the calendar and cannot be changed with the S-cursor keys.
  9953. So these are more for recording a certain time/date."
  9954. (interactive "P")
  9955. (let (org-time-was-given org-end-time-was-given time)
  9956. (setq time (org-read-date arg 'totime))
  9957. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9958. nil nil (list org-end-time-was-given))))
  9959. (defvar org-date-ovl (org-make-overlay 1 1))
  9960. (org-overlay-put org-date-ovl 'face 'org-warning)
  9961. (org-detach-overlay org-date-ovl)
  9962. (defvar org-ans1) ; dynamically scoped parameter
  9963. (defvar org-ans2) ; dynamically scoped parameter
  9964. (defvar org-plain-time-of-day-regexp) ; defined below
  9965. (defvar org-overriding-default-time nil) ; dynamically scoped
  9966. (defvar org-read-date-overlay nil)
  9967. (defvar org-dcst nil) ; dynamically scoped
  9968. (defun org-read-date (&optional with-time to-time from-string prompt
  9969. default-time default-input)
  9970. "Read a date, possibly a time, and make things smooth for the user.
  9971. The prompt will suggest to enter an ISO date, but you can also enter anything
  9972. which will at least partially be understood by `parse-time-string'.
  9973. Unrecognized parts of the date will default to the current day, month, year,
  9974. hour and minute. If this command is called to replace a timestamp at point,
  9975. of to enter the second timestamp of a range, the default time is taken from the
  9976. existing stamp. For example,
  9977. 3-2-5 --> 2003-02-05
  9978. feb 15 --> currentyear-02-15
  9979. sep 12 9 --> 2009-09-12
  9980. 12:45 --> today 12:45
  9981. 22 sept 0:34 --> currentyear-09-22 0:34
  9982. 12 --> currentyear-currentmonth-12
  9983. Fri --> nearest Friday (today or later)
  9984. etc.
  9985. Furthermore you can specify a relative date by giving, as the *first* thing
  9986. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9987. change in days weeks, months, years.
  9988. With a single plus or minus, the date is relative to today. With a double
  9989. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9990. +4d --> four days from today
  9991. +4 --> same as above
  9992. +2w --> two weeks from today
  9993. ++5 --> five days from default date
  9994. The function understands only English month and weekday abbreviations,
  9995. but this can be configured with the variables `parse-time-months' and
  9996. `parse-time-weekdays'.
  9997. While prompting, a calendar is popped up - you can also select the
  9998. date with the mouse (button 1). The calendar shows a period of three
  9999. months. To scroll it to other months, use the keys `>' and `<'.
  10000. If you don't like the calendar, turn it off with
  10001. \(setq org-read-date-popup-calendar nil)
  10002. With optional argument TO-TIME, the date will immediately be converted
  10003. to an internal time.
  10004. With an optional argument WITH-TIME, the prompt will suggest to also
  10005. insert a time. Note that when WITH-TIME is not set, you can still
  10006. enter a time, and this function will inform the calling routine about
  10007. this change. The calling routine may then choose to change the format
  10008. used to insert the time stamp into the buffer to include the time.
  10009. With optional argument FROM-STRING, read from this string instead from
  10010. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10011. the time/date that is used for everything that is not specified by the
  10012. user."
  10013. (require 'parse-time)
  10014. (let* ((org-time-stamp-rounding-minutes
  10015. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10016. (org-dcst org-display-custom-times)
  10017. (ct (org-current-time))
  10018. (def (or org-overriding-default-time default-time ct))
  10019. (defdecode (decode-time def))
  10020. (dummy (progn
  10021. (when (< (nth 2 defdecode) org-extend-today-until)
  10022. (setcar (nthcdr 2 defdecode) -1)
  10023. (setcar (nthcdr 1 defdecode) 59)
  10024. (setq def (apply 'encode-time defdecode)
  10025. defdecode (decode-time def)))))
  10026. (calendar-move-hook nil)
  10027. (calendar-view-diary-initially-flag nil)
  10028. (view-diary-entries-initially nil)
  10029. (calendar-view-holidays-initially-flag nil)
  10030. (view-calendar-holidays-initially nil)
  10031. (timestr (format-time-string
  10032. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10033. (prompt (concat (if prompt (concat prompt " ") "")
  10034. (format "Date+time [%s]: " timestr)))
  10035. ans (org-ans0 "") org-ans1 org-ans2 final)
  10036. (cond
  10037. (from-string (setq ans from-string))
  10038. (org-read-date-popup-calendar
  10039. (save-excursion
  10040. (save-window-excursion
  10041. (calendar)
  10042. (calendar-forward-day (- (time-to-days def)
  10043. (calendar-absolute-from-gregorian
  10044. (calendar-current-date))))
  10045. (org-eval-in-calendar nil t)
  10046. (let* ((old-map (current-local-map))
  10047. (map (copy-keymap calendar-mode-map))
  10048. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10049. (org-defkey map (kbd "RET") 'org-calendar-select)
  10050. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10051. 'org-calendar-select-mouse)
  10052. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10053. 'org-calendar-select-mouse)
  10054. (org-defkey minibuffer-local-map [(meta shift left)]
  10055. (lambda () (interactive)
  10056. (org-eval-in-calendar '(calendar-backward-month 1))))
  10057. (org-defkey minibuffer-local-map [(meta shift right)]
  10058. (lambda () (interactive)
  10059. (org-eval-in-calendar '(calendar-forward-month 1))))
  10060. (org-defkey minibuffer-local-map [(meta shift up)]
  10061. (lambda () (interactive)
  10062. (org-eval-in-calendar '(calendar-backward-year 1))))
  10063. (org-defkey minibuffer-local-map [(meta shift down)]
  10064. (lambda () (interactive)
  10065. (org-eval-in-calendar '(calendar-forward-year 1))))
  10066. (org-defkey minibuffer-local-map [(shift up)]
  10067. (lambda () (interactive)
  10068. (org-eval-in-calendar '(calendar-backward-week 1))))
  10069. (org-defkey minibuffer-local-map [(shift down)]
  10070. (lambda () (interactive)
  10071. (org-eval-in-calendar '(calendar-forward-week 1))))
  10072. (org-defkey minibuffer-local-map [(shift left)]
  10073. (lambda () (interactive)
  10074. (org-eval-in-calendar '(calendar-backward-day 1))))
  10075. (org-defkey minibuffer-local-map [(shift right)]
  10076. (lambda () (interactive)
  10077. (org-eval-in-calendar '(calendar-forward-day 1))))
  10078. (org-defkey minibuffer-local-map ">"
  10079. (lambda () (interactive)
  10080. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10081. (org-defkey minibuffer-local-map "<"
  10082. (lambda () (interactive)
  10083. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10084. (unwind-protect
  10085. (progn
  10086. (use-local-map map)
  10087. (add-hook 'post-command-hook 'org-read-date-display)
  10088. (setq org-ans0 (read-string prompt default-input nil nil))
  10089. ;; org-ans0: from prompt
  10090. ;; org-ans1: from mouse click
  10091. ;; org-ans2: from calendar motion
  10092. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10093. (remove-hook 'post-command-hook 'org-read-date-display)
  10094. (use-local-map old-map)
  10095. (when org-read-date-overlay
  10096. (org-delete-overlay org-read-date-overlay)
  10097. (setq org-read-date-overlay nil)))))))
  10098. (t ; Naked prompt only
  10099. (unwind-protect
  10100. (setq ans (read-string prompt default-input nil timestr))
  10101. (when org-read-date-overlay
  10102. (org-delete-overlay org-read-date-overlay)
  10103. (setq org-read-date-overlay nil)))))
  10104. (setq final (org-read-date-analyze ans def defdecode))
  10105. (if to-time
  10106. (apply 'encode-time final)
  10107. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10108. (format "%04d-%02d-%02d %02d:%02d"
  10109. (nth 5 final) (nth 4 final) (nth 3 final)
  10110. (nth 2 final) (nth 1 final))
  10111. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10112. (defvar def)
  10113. (defvar defdecode)
  10114. (defvar with-time)
  10115. (defun org-read-date-display ()
  10116. "Display the currrent date prompt interpretation in the minibuffer."
  10117. (when org-read-date-display-live
  10118. (when org-read-date-overlay
  10119. (org-delete-overlay org-read-date-overlay))
  10120. (let ((p (point)))
  10121. (end-of-line 1)
  10122. (while (not (equal (buffer-substring
  10123. (max (point-min) (- (point) 4)) (point))
  10124. " "))
  10125. (insert " "))
  10126. (goto-char p))
  10127. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10128. " " (or org-ans1 org-ans2)))
  10129. (org-end-time-was-given nil)
  10130. (f (org-read-date-analyze ans def defdecode))
  10131. (fmts (if org-dcst
  10132. org-time-stamp-custom-formats
  10133. org-time-stamp-formats))
  10134. (fmt (if (or with-time
  10135. (and (boundp 'org-time-was-given) org-time-was-given))
  10136. (cdr fmts)
  10137. (car fmts)))
  10138. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10139. (when (and org-end-time-was-given
  10140. (string-match org-plain-time-of-day-regexp txt))
  10141. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10142. org-end-time-was-given
  10143. (substring txt (match-end 0)))))
  10144. (setq org-read-date-overlay
  10145. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10146. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10147. (defun org-read-date-analyze (ans def defdecode)
  10148. "Analyze the combined answer of the date prompt."
  10149. ;; FIXME: cleanup and comment
  10150. (let (delta deltan deltaw deltadef year month day
  10151. hour minute second wday pm h2 m2 tl wday1
  10152. iso-year iso-weekday iso-week iso-year iso-date)
  10153. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10154. (setq ans "+0"))
  10155. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10156. (setq ans (replace-match "" t t ans)
  10157. deltan (car delta)
  10158. deltaw (nth 1 delta)
  10159. deltadef (nth 2 delta)))
  10160. ;; Check if there is an iso week date in there
  10161. ;; If yes, sore the info and ostpone interpreting it until the rest
  10162. ;; of the parsing is done
  10163. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10164. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10165. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10166. iso-week (string-to-number (match-string 2 ans)))
  10167. (setq ans (replace-match "" t t ans)))
  10168. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10169. (when (string-match
  10170. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10171. (setq year (if (match-end 2)
  10172. (string-to-number (match-string 2 ans))
  10173. (string-to-number (format-time-string "%Y")))
  10174. month (string-to-number (match-string 3 ans))
  10175. day (string-to-number (match-string 4 ans)))
  10176. (if (< year 100) (setq year (+ 2000 year)))
  10177. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10178. t nil ans)))
  10179. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10180. ;; If there is a time with am/pm, and *no* time without it, we convert
  10181. ;; so that matching will be successful.
  10182. (loop for i from 1 to 2 do ; twice, for end time as well
  10183. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10184. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10185. (setq hour (string-to-number (match-string 1 ans))
  10186. minute (if (match-end 3)
  10187. (string-to-number (match-string 3 ans))
  10188. 0)
  10189. pm (equal ?p
  10190. (string-to-char (downcase (match-string 4 ans)))))
  10191. (if (and (= hour 12) (not pm))
  10192. (setq hour 0)
  10193. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10194. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10195. t t ans))))
  10196. ;; Check if a time range is given as a duration
  10197. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10198. (setq hour (string-to-number (match-string 1 ans))
  10199. h2 (+ hour (string-to-number (match-string 3 ans)))
  10200. minute (string-to-number (match-string 2 ans))
  10201. m2 (+ minute (if (match-end 5) (string-to-number
  10202. (match-string 5 ans))0)))
  10203. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10204. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10205. t t ans)))
  10206. ;; Check if there is a time range
  10207. (when (boundp 'org-end-time-was-given)
  10208. (setq org-time-was-given nil)
  10209. (when (and (string-match org-plain-time-of-day-regexp ans)
  10210. (match-end 8))
  10211. (setq org-end-time-was-given (match-string 8 ans))
  10212. (setq ans (concat (substring ans 0 (match-beginning 7))
  10213. (substring ans (match-end 7))))))
  10214. (setq tl (parse-time-string ans)
  10215. day (or (nth 3 tl) (nth 3 defdecode))
  10216. month (or (nth 4 tl)
  10217. (if (and org-read-date-prefer-future
  10218. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10219. (1+ (nth 4 defdecode))
  10220. (nth 4 defdecode)))
  10221. year (or (nth 5 tl)
  10222. (if (and org-read-date-prefer-future
  10223. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10224. (1+ (nth 5 defdecode))
  10225. (nth 5 defdecode)))
  10226. hour (or (nth 2 tl) (nth 2 defdecode))
  10227. minute (or (nth 1 tl) (nth 1 defdecode))
  10228. second (or (nth 0 tl) 0)
  10229. wday (nth 6 tl))
  10230. ;; Special date definitions below
  10231. (cond
  10232. (iso-week
  10233. ;; There was an iso week
  10234. (setq year (or iso-year year)
  10235. day (or iso-weekday wday 1)
  10236. wday nil ; to make sure that the trigger below does not match
  10237. iso-date (calendar-gregorian-from-absolute
  10238. (calendar-absolute-from-iso
  10239. (list iso-week day year))))
  10240. ; FIXME: Should we also push ISO weeks into the future?
  10241. ; (when (and org-read-date-prefer-future
  10242. ; (not iso-year)
  10243. ; (< (calendar-absolute-from-gregorian iso-date)
  10244. ; (time-to-days (current-time))))
  10245. ; (setq year (1+ year)
  10246. ; iso-date (calendar-gregorian-from-absolute
  10247. ; (calendar-absolute-from-iso
  10248. ; (list iso-week day year)))))
  10249. (setq month (car iso-date)
  10250. year (nth 2 iso-date)
  10251. day (nth 1 iso-date)))
  10252. (deltan
  10253. (unless deltadef
  10254. (let ((now (decode-time (current-time))))
  10255. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10256. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10257. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10258. ((equal deltaw "m") (setq month (+ month deltan)))
  10259. ((equal deltaw "y") (setq year (+ year deltan)))))
  10260. ((and wday (not (nth 3 tl)))
  10261. ;; Weekday was given, but no day, so pick that day in the week
  10262. ;; on or after the derived date.
  10263. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10264. (unless (equal wday wday1)
  10265. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10266. (if (and (boundp 'org-time-was-given)
  10267. (nth 2 tl))
  10268. (setq org-time-was-given t))
  10269. (if (< year 100) (setq year (+ 2000 year)))
  10270. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10271. (list second minute hour day month year)))
  10272. (defvar parse-time-weekdays)
  10273. (defun org-read-date-get-relative (s today default)
  10274. "Check string S for special relative date string.
  10275. TODAY and DEFAULT are internal times, for today and for a default.
  10276. Return shift list (N what def-flag)
  10277. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10278. N is the number of WHATs to shift.
  10279. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10280. the DEFAULT date rather than TODAY."
  10281. (when (and
  10282. (string-match
  10283. (concat
  10284. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10285. "\\([0-9]+\\)?"
  10286. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10287. "\\([ \t]\\|$\\)") s)
  10288. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10289. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10290. (string-to-char (substring (match-string 1 s) -1))
  10291. ?+))
  10292. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10293. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10294. (what (if (match-end 3) (match-string 3 s) "d"))
  10295. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10296. (date (if rel default today))
  10297. (wday (nth 6 (decode-time date)))
  10298. delta)
  10299. (if wday1
  10300. (progn
  10301. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10302. (if (= dir ?-) (setq delta (- delta 7)))
  10303. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10304. (list delta "d" rel))
  10305. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10306. (defun org-eval-in-calendar (form &optional keepdate)
  10307. "Eval FORM in the calendar window and return to current window.
  10308. Also, store the cursor date in variable org-ans2."
  10309. (let ((sw (selected-window)))
  10310. (select-window (get-buffer-window "*Calendar*"))
  10311. (eval form)
  10312. (when (and (not keepdate) (calendar-cursor-to-date))
  10313. (let* ((date (calendar-cursor-to-date))
  10314. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10315. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10316. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10317. (select-window sw)))
  10318. ; ;; Update the prompt to show new default date
  10319. ; (save-excursion
  10320. ; (goto-char (point-min))
  10321. ; (when (and org-ans2
  10322. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10323. ; (get-text-property (match-end 0) 'field))
  10324. ; (let ((inhibit-read-only t))
  10325. ; (replace-match (concat "[" org-ans2 "]") t t)
  10326. ; (add-text-properties (point-min) (1+ (match-end 0))
  10327. ; (text-properties-at (1+ (point-min)))))))))
  10328. (defun org-calendar-select ()
  10329. "Return to `org-read-date' with the date currently selected.
  10330. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10331. (interactive)
  10332. (when (calendar-cursor-to-date)
  10333. (let* ((date (calendar-cursor-to-date))
  10334. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10335. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10336. (if (active-minibuffer-window) (exit-minibuffer))))
  10337. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10338. "Insert a date stamp for the date given by the internal TIME.
  10339. WITH-HM means, use the stamp format that includes the time of the day.
  10340. INACTIVE means use square brackets instead of angular ones, so that the
  10341. stamp will not contribute to the agenda.
  10342. PRE and POST are optional strings to be inserted before and after the
  10343. stamp.
  10344. The command returns the inserted time stamp."
  10345. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10346. stamp)
  10347. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10348. (insert-before-markers (or pre ""))
  10349. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10350. (when (listp extra)
  10351. (setq extra (car extra))
  10352. (if (and (stringp extra)
  10353. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10354. (setq extra (format "-%02d:%02d"
  10355. (string-to-number (match-string 1 extra))
  10356. (string-to-number (match-string 2 extra))))
  10357. (setq extra nil)))
  10358. (when extra
  10359. (backward-char 1)
  10360. (insert-before-markers extra)
  10361. (forward-char 1))
  10362. (insert-before-markers (or post ""))
  10363. (setq org-last-inserted-timestamp stamp)))
  10364. (defun org-toggle-time-stamp-overlays ()
  10365. "Toggle the use of custom time stamp formats."
  10366. (interactive)
  10367. (setq org-display-custom-times (not org-display-custom-times))
  10368. (unless org-display-custom-times
  10369. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10370. (while (setq p (next-single-property-change p 'display))
  10371. (if (and (get-text-property p 'display)
  10372. (eq (get-text-property p 'face) 'org-date))
  10373. (remove-text-properties
  10374. p (setq p (next-single-property-change p 'display))
  10375. '(display t))))
  10376. (set-buffer-modified-p bmp)))
  10377. (if (featurep 'xemacs)
  10378. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10379. (org-restart-font-lock)
  10380. (setq org-table-may-need-update t)
  10381. (if org-display-custom-times
  10382. (message "Time stamps are overlayed with custom format")
  10383. (message "Time stamp overlays removed")))
  10384. (defun org-display-custom-time (beg end)
  10385. "Overlay modified time stamp format over timestamp between BEG and END."
  10386. (let* ((ts (buffer-substring beg end))
  10387. t1 w1 with-hm tf time str w2 (off 0))
  10388. (save-match-data
  10389. (setq t1 (org-parse-time-string ts t))
  10390. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10391. (setq off (- (match-end 0) (match-beginning 0)))))
  10392. (setq end (- end off))
  10393. (setq w1 (- end beg)
  10394. with-hm (and (nth 1 t1) (nth 2 t1))
  10395. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10396. time (org-fix-decoded-time t1)
  10397. str (org-add-props
  10398. (format-time-string
  10399. (substring tf 1 -1) (apply 'encode-time time))
  10400. nil 'mouse-face 'highlight)
  10401. w2 (length str))
  10402. (if (not (= w2 w1))
  10403. (add-text-properties (1+ beg) (+ 2 beg)
  10404. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10405. (if (featurep 'xemacs)
  10406. (progn
  10407. (put-text-property beg end 'invisible t)
  10408. (put-text-property beg end 'end-glyph (make-glyph str)))
  10409. (put-text-property beg end 'display str))))
  10410. (defun org-translate-time (string)
  10411. "Translate all timestamps in STRING to custom format.
  10412. But do this only if the variable `org-display-custom-times' is set."
  10413. (when org-display-custom-times
  10414. (save-match-data
  10415. (let* ((start 0)
  10416. (re org-ts-regexp-both)
  10417. t1 with-hm inactive tf time str beg end)
  10418. (while (setq start (string-match re string start))
  10419. (setq beg (match-beginning 0)
  10420. end (match-end 0)
  10421. t1 (save-match-data
  10422. (org-parse-time-string (substring string beg end) t))
  10423. with-hm (and (nth 1 t1) (nth 2 t1))
  10424. inactive (equal (substring string beg (1+ beg)) "[")
  10425. tf (funcall (if with-hm 'cdr 'car)
  10426. org-time-stamp-custom-formats)
  10427. time (org-fix-decoded-time t1)
  10428. str (format-time-string
  10429. (concat
  10430. (if inactive "[" "<") (substring tf 1 -1)
  10431. (if inactive "]" ">"))
  10432. (apply 'encode-time time))
  10433. string (replace-match str t t string)
  10434. start (+ start (length str)))))))
  10435. string)
  10436. (defun org-fix-decoded-time (time)
  10437. "Set 0 instead of nil for the first 6 elements of time.
  10438. Don't touch the rest."
  10439. (let ((n 0))
  10440. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10441. (defun org-days-to-time (timestamp-string)
  10442. "Difference between TIMESTAMP-STRING and now in days."
  10443. (- (time-to-days (org-time-string-to-time timestamp-string))
  10444. (time-to-days (current-time))))
  10445. (defun org-deadline-close (timestamp-string &optional ndays)
  10446. "Is the time in TIMESTAMP-STRING close to the current date?"
  10447. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10448. (and (< (org-days-to-time timestamp-string) ndays)
  10449. (not (org-entry-is-done-p))))
  10450. (defun org-get-wdays (ts)
  10451. "Get the deadline lead time appropriate for timestring TS."
  10452. (cond
  10453. ((<= org-deadline-warning-days 0)
  10454. ;; 0 or negative, enforce this value no matter what
  10455. (- org-deadline-warning-days))
  10456. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10457. ;; lead time is specified.
  10458. (floor (* (string-to-number (match-string 1 ts))
  10459. (cdr (assoc (match-string 2 ts)
  10460. '(("d" . 1) ("w" . 7)
  10461. ("m" . 30.4) ("y" . 365.25)))))))
  10462. ;; go for the default.
  10463. (t org-deadline-warning-days)))
  10464. (defun org-calendar-select-mouse (ev)
  10465. "Return to `org-read-date' with the date currently selected.
  10466. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10467. (interactive "e")
  10468. (mouse-set-point ev)
  10469. (when (calendar-cursor-to-date)
  10470. (let* ((date (calendar-cursor-to-date))
  10471. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10472. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10473. (if (active-minibuffer-window) (exit-minibuffer))))
  10474. (defun org-check-deadlines (ndays)
  10475. "Check if there are any deadlines due or past due.
  10476. A deadline is considered due if it happens within `org-deadline-warning-days'
  10477. days from today's date. If the deadline appears in an entry marked DONE,
  10478. it is not shown. The prefix arg NDAYS can be used to test that many
  10479. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10480. (interactive "P")
  10481. (let* ((org-warn-days
  10482. (cond
  10483. ((equal ndays '(4)) 100000)
  10484. (ndays (prefix-numeric-value ndays))
  10485. (t (abs org-deadline-warning-days))))
  10486. (case-fold-search nil)
  10487. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10488. (callback
  10489. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10490. (message "%d deadlines past-due or due within %d days"
  10491. (org-occur regexp nil callback)
  10492. org-warn-days)))
  10493. (defun org-check-before-date (date)
  10494. "Check if there are deadlines or scheduled entries before DATE."
  10495. (interactive (list (org-read-date)))
  10496. (let ((case-fold-search nil)
  10497. (regexp (concat "\\<\\(" org-deadline-string
  10498. "\\|" org-scheduled-string
  10499. "\\) *<\\([^>]+\\)>"))
  10500. (callback
  10501. (lambda () (time-less-p
  10502. (org-time-string-to-time (match-string 2))
  10503. (org-time-string-to-time date)))))
  10504. (message "%d entries before %s"
  10505. (org-occur regexp nil callback) date)))
  10506. (defun org-evaluate-time-range (&optional to-buffer)
  10507. "Evaluate a time range by computing the difference between start and end.
  10508. Normally the result is just printed in the echo area, but with prefix arg
  10509. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10510. If the time range is actually in a table, the result is inserted into the
  10511. next column.
  10512. For time difference computation, a year is assumed to be exactly 365
  10513. days in order to avoid rounding problems."
  10514. (interactive "P")
  10515. (or
  10516. (org-clock-update-time-maybe)
  10517. (save-excursion
  10518. (unless (org-at-date-range-p t)
  10519. (goto-char (point-at-bol))
  10520. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10521. (if (not (org-at-date-range-p t))
  10522. (error "Not at a time-stamp range, and none found in current line")))
  10523. (let* ((ts1 (match-string 1))
  10524. (ts2 (match-string 2))
  10525. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10526. (match-end (match-end 0))
  10527. (time1 (org-time-string-to-time ts1))
  10528. (time2 (org-time-string-to-time ts2))
  10529. (t1 (time-to-seconds time1))
  10530. (t2 (time-to-seconds time2))
  10531. (diff (abs (- t2 t1)))
  10532. (negative (< (- t2 t1) 0))
  10533. ;; (ys (floor (* 365 24 60 60)))
  10534. (ds (* 24 60 60))
  10535. (hs (* 60 60))
  10536. (fy "%dy %dd %02d:%02d")
  10537. (fy1 "%dy %dd")
  10538. (fd "%dd %02d:%02d")
  10539. (fd1 "%dd")
  10540. (fh "%02d:%02d")
  10541. y d h m align)
  10542. (if havetime
  10543. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10544. y 0
  10545. d (floor (/ diff ds)) diff (mod diff ds)
  10546. h (floor (/ diff hs)) diff (mod diff hs)
  10547. m (floor (/ diff 60)))
  10548. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10549. y 0
  10550. d (floor (+ (/ diff ds) 0.5))
  10551. h 0 m 0))
  10552. (if (not to-buffer)
  10553. (message "%s" (org-make-tdiff-string y d h m))
  10554. (if (org-at-table-p)
  10555. (progn
  10556. (goto-char match-end)
  10557. (setq align t)
  10558. (and (looking-at " *|") (goto-char (match-end 0))))
  10559. (goto-char match-end))
  10560. (if (looking-at
  10561. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10562. (replace-match ""))
  10563. (if negative (insert " -"))
  10564. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10565. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10566. (insert " " (format fh h m))))
  10567. (if align (org-table-align))
  10568. (message "Time difference inserted")))))
  10569. (defun org-make-tdiff-string (y d h m)
  10570. (let ((fmt "")
  10571. (l nil))
  10572. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10573. l (push y l)))
  10574. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10575. l (push d l)))
  10576. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10577. l (push h l)))
  10578. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10579. l (push m l)))
  10580. (apply 'format fmt (nreverse l))))
  10581. (defun org-time-string-to-time (s)
  10582. (apply 'encode-time (org-parse-time-string s)))
  10583. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10584. "Convert a time stamp to an absolute day number.
  10585. If there is a specifyer for a cyclic time stamp, get the closest date to
  10586. DAYNR.
  10587. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10588. (cond
  10589. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10590. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10591. daynr
  10592. (+ daynr 1000)))
  10593. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10594. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10595. (time-to-days (current-time))) (match-string 0 s)
  10596. prefer show-all))
  10597. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10598. (defun org-days-to-iso-week (days)
  10599. "Return the iso week number."
  10600. (require 'cal-iso)
  10601. (car (calendar-iso-from-absolute days)))
  10602. (defun org-small-year-to-year (year)
  10603. "Convert 2-digit years into 4-digit years.
  10604. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10605. The year 2000 cannot be abbreviated. Any year lager than 99
  10606. is retrned unchanged."
  10607. (if (< year 38)
  10608. (setq year (+ 2000 year))
  10609. (if (< year 100)
  10610. (setq year (+ 1900 year))))
  10611. year)
  10612. (defun org-time-from-absolute (d)
  10613. "Return the time corresponding to date D.
  10614. D may be an absolute day number, or a calendar-type list (month day year)."
  10615. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10616. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10617. (defun org-calendar-holiday ()
  10618. "List of holidays, for Diary display in Org-mode."
  10619. (require 'holidays)
  10620. (let ((hl (funcall
  10621. (if (fboundp 'calendar-check-holidays)
  10622. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10623. (if hl (mapconcat 'identity hl "; "))))
  10624. (defun org-diary-sexp-entry (sexp entry date)
  10625. "Process a SEXP diary ENTRY for DATE."
  10626. (require 'diary-lib)
  10627. (let ((result (if calendar-debug-sexp
  10628. (let ((stack-trace-on-error t))
  10629. (eval (car (read-from-string sexp))))
  10630. (condition-case nil
  10631. (eval (car (read-from-string sexp)))
  10632. (error
  10633. (beep)
  10634. (message "Bad sexp at line %d in %s: %s"
  10635. (org-current-line)
  10636. (buffer-file-name) sexp)
  10637. (sleep-for 2))))))
  10638. (cond ((stringp result) result)
  10639. ((and (consp result)
  10640. (stringp (cdr result))) (cdr result))
  10641. (result entry)
  10642. (t nil))))
  10643. (defun org-diary-to-ical-string (frombuf)
  10644. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10645. This uses the icalendar.el library."
  10646. (let* ((tmpdir (if (featurep 'xemacs)
  10647. (temp-directory)
  10648. temporary-file-directory))
  10649. (tmpfile (make-temp-name
  10650. (expand-file-name "orgics" tmpdir)))
  10651. buf rtn b e)
  10652. (save-excursion
  10653. (set-buffer frombuf)
  10654. (icalendar-export-region (point-min) (point-max) tmpfile)
  10655. (setq buf (find-buffer-visiting tmpfile))
  10656. (set-buffer buf)
  10657. (goto-char (point-min))
  10658. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10659. (setq b (match-beginning 0)))
  10660. (goto-char (point-max))
  10661. (if (re-search-backward "^END:VEVENT" nil t)
  10662. (setq e (match-end 0)))
  10663. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10664. (kill-buffer buf)
  10665. (delete-file tmpfile)
  10666. rtn))
  10667. (defun org-closest-date (start current change prefer show-all)
  10668. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10669. When PREFER is `past' return a date that is either CURRENT or past.
  10670. When PREFER is `future', return a date that is either CURRENT or future.
  10671. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10672. ;; Make the proper lists from the dates
  10673. (catch 'exit
  10674. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10675. dn dw sday cday n1 n2
  10676. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10677. (setq start (org-date-to-gregorian start)
  10678. current (org-date-to-gregorian
  10679. (if show-all
  10680. current
  10681. (time-to-days (current-time))))
  10682. sday (calendar-absolute-from-gregorian start)
  10683. cday (calendar-absolute-from-gregorian current))
  10684. (if (<= cday sday) (throw 'exit sday))
  10685. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10686. (setq dn (string-to-number (match-string 1 change))
  10687. dw (cdr (assoc (match-string 2 change) a1)))
  10688. (error "Invalid change specifyer: %s" change))
  10689. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10690. (cond
  10691. ((eq dw 'day)
  10692. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10693. n2 (+ n1 dn)))
  10694. ((eq dw 'year)
  10695. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10696. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10697. (setq date1 (list m d y1)
  10698. n1 (calendar-absolute-from-gregorian date1)
  10699. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10700. n2 (calendar-absolute-from-gregorian date2)))
  10701. ((eq dw 'month)
  10702. ;; approx number of month between the two dates
  10703. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10704. ;; How often does dn fit in there?
  10705. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10706. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10707. m (+ m nm)
  10708. ny (floor (/ m 12))
  10709. y (+ y ny)
  10710. m (- m (* ny 12)))
  10711. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10712. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10713. (setq m2 (+ m dn) y2 y)
  10714. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10715. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10716. (while (<= n2 cday)
  10717. (setq n1 n2 m m2 y y2)
  10718. (setq m2 (+ m dn) y2 y)
  10719. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10720. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10721. (if show-all
  10722. (cond
  10723. ((eq prefer 'past) n1)
  10724. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10725. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10726. (cond
  10727. ((eq prefer 'past) n1)
  10728. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10729. (t (if (= cday n1) n1 n2)))))))
  10730. (defun org-date-to-gregorian (date)
  10731. "Turn any specification of DATE into a gregorian date for the calendar."
  10732. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10733. ((and (listp date) (= (length date) 3)) date)
  10734. ((stringp date)
  10735. (setq date (org-parse-time-string date))
  10736. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10737. ((listp date)
  10738. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10739. (defun org-parse-time-string (s &optional nodefault)
  10740. "Parse the standard Org-mode time string.
  10741. This should be a lot faster than the normal `parse-time-string'.
  10742. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10743. hour and minute fields will be nil if not given."
  10744. (if (string-match org-ts-regexp0 s)
  10745. (list 0
  10746. (if (or (match-beginning 8) (not nodefault))
  10747. (string-to-number (or (match-string 8 s) "0")))
  10748. (if (or (match-beginning 7) (not nodefault))
  10749. (string-to-number (or (match-string 7 s) "0")))
  10750. (string-to-number (match-string 4 s))
  10751. (string-to-number (match-string 3 s))
  10752. (string-to-number (match-string 2 s))
  10753. nil nil nil)
  10754. (make-list 9 0)))
  10755. (defun org-timestamp-up (&optional arg)
  10756. "Increase the date item at the cursor by one.
  10757. If the cursor is on the year, change the year. If it is on the month or
  10758. the day, change that.
  10759. With prefix ARG, change by that many units."
  10760. (interactive "p")
  10761. (org-timestamp-change (prefix-numeric-value arg)))
  10762. (defun org-timestamp-down (&optional arg)
  10763. "Decrease the date item at the cursor by one.
  10764. If the cursor is on the year, change the year. If it is on the month or
  10765. the day, change that.
  10766. With prefix ARG, change by that many units."
  10767. (interactive "p")
  10768. (org-timestamp-change (- (prefix-numeric-value arg))))
  10769. (defun org-timestamp-up-day (&optional arg)
  10770. "Increase the date in the time stamp by one day.
  10771. With prefix ARG, change that many days."
  10772. (interactive "p")
  10773. (if (and (not (org-at-timestamp-p t))
  10774. (org-on-heading-p))
  10775. (org-todo 'up)
  10776. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10777. (defun org-timestamp-down-day (&optional arg)
  10778. "Decrease the date in the time stamp by one day.
  10779. With prefix ARG, change that many days."
  10780. (interactive "p")
  10781. (if (and (not (org-at-timestamp-p t))
  10782. (org-on-heading-p))
  10783. (org-todo 'down)
  10784. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10785. (defun org-at-timestamp-p (&optional inactive-ok)
  10786. "Determine if the cursor is in or at a timestamp."
  10787. (interactive)
  10788. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10789. (pos (point))
  10790. (ans (or (looking-at tsr)
  10791. (save-excursion
  10792. (skip-chars-backward "^[<\n\r\t")
  10793. (if (> (point) (point-min)) (backward-char 1))
  10794. (and (looking-at tsr)
  10795. (> (- (match-end 0) pos) -1))))))
  10796. (and ans
  10797. (boundp 'org-ts-what)
  10798. (setq org-ts-what
  10799. (cond
  10800. ((= pos (match-beginning 0)) 'bracket)
  10801. ((= pos (1- (match-end 0))) 'bracket)
  10802. ((org-pos-in-match-range pos 2) 'year)
  10803. ((org-pos-in-match-range pos 3) 'month)
  10804. ((org-pos-in-match-range pos 7) 'hour)
  10805. ((org-pos-in-match-range pos 8) 'minute)
  10806. ((or (org-pos-in-match-range pos 4)
  10807. (org-pos-in-match-range pos 5)) 'day)
  10808. ((and (> pos (or (match-end 8) (match-end 5)))
  10809. (< pos (match-end 0)))
  10810. (- pos (or (match-end 8) (match-end 5))))
  10811. (t 'day))))
  10812. ans))
  10813. (defun org-toggle-timestamp-type ()
  10814. "Toggle the type (<active> or [inactive]) of a time stamp."
  10815. (interactive)
  10816. (when (org-at-timestamp-p t)
  10817. (save-excursion
  10818. (goto-char (match-beginning 0))
  10819. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10820. (goto-char (1- (match-end 0)))
  10821. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10822. (message "Timestamp is now %sactive"
  10823. (if (equal (char-before) ?>) "in" ""))))
  10824. (defun org-timestamp-change (n &optional what)
  10825. "Change the date in the time stamp at point.
  10826. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10827. `year', `minute', `second'. If WHAT is not given, the cursor position
  10828. in the timestamp determines what will be changed."
  10829. (let ((pos (point))
  10830. with-hm inactive
  10831. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10832. org-ts-what
  10833. extra rem
  10834. ts time time0)
  10835. (if (not (org-at-timestamp-p t))
  10836. (error "Not at a timestamp"))
  10837. (if (and (not what) (eq org-ts-what 'bracket))
  10838. (org-toggle-timestamp-type)
  10839. (if (and (not what) (not (eq org-ts-what 'day))
  10840. org-display-custom-times
  10841. (get-text-property (point) 'display)
  10842. (not (get-text-property (1- (point)) 'display)))
  10843. (setq org-ts-what 'day))
  10844. (setq org-ts-what (or what org-ts-what)
  10845. inactive (= (char-after (match-beginning 0)) ?\[)
  10846. ts (match-string 0))
  10847. (replace-match "")
  10848. (if (string-match
  10849. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10850. ts)
  10851. (setq extra (match-string 1 ts)))
  10852. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10853. (setq with-hm t))
  10854. (setq time0 (org-parse-time-string ts))
  10855. (when (and (eq org-ts-what 'minute)
  10856. (eq current-prefix-arg nil))
  10857. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10858. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10859. (setcar (cdr time0) (+ (nth 1 time0)
  10860. (if (> n 0) (- rem) (- dm rem))))))
  10861. (setq time
  10862. (encode-time (or (car time0) 0)
  10863. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10864. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10865. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10866. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10867. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10868. (nthcdr 6 time0)))
  10869. (when (integerp org-ts-what)
  10870. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10871. (if (eq what 'calendar)
  10872. (let ((cal-date (org-get-date-from-calendar)))
  10873. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10874. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10875. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10876. (setcar time0 (or (car time0) 0))
  10877. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10878. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10879. (setq time (apply 'encode-time time0))))
  10880. (setq org-last-changed-timestamp
  10881. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10882. (org-clock-update-time-maybe)
  10883. (goto-char pos)
  10884. ;; Try to recenter the calendar window, if any
  10885. (if (and org-calendar-follow-timestamp-change
  10886. (get-buffer-window "*Calendar*" t)
  10887. (memq org-ts-what '(day month year)))
  10888. (org-recenter-calendar (time-to-days time))))))
  10889. (defun org-modify-ts-extra (s pos n dm)
  10890. "Change the different parts of the lead-time and repeat fields in timestamp."
  10891. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10892. ng h m new rem)
  10893. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10894. (cond
  10895. ((or (org-pos-in-match-range pos 2)
  10896. (org-pos-in-match-range pos 3))
  10897. (setq m (string-to-number (match-string 3 s))
  10898. h (string-to-number (match-string 2 s)))
  10899. (if (org-pos-in-match-range pos 2)
  10900. (setq h (+ h n))
  10901. (setq n (* dm (org-no-warnings (signum n))))
  10902. (when (not (= 0 (setq rem (% m dm))))
  10903. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10904. (setq m (+ m n)))
  10905. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10906. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10907. (setq h (min 24 (max 0 h)))
  10908. (setq ng 1 new (format "-%02d:%02d" h m)))
  10909. ((org-pos-in-match-range pos 6)
  10910. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10911. ((org-pos-in-match-range pos 5)
  10912. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10913. ((org-pos-in-match-range pos 9)
  10914. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10915. ((org-pos-in-match-range pos 8)
  10916. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10917. (when ng
  10918. (setq s (concat
  10919. (substring s 0 (match-beginning ng))
  10920. new
  10921. (substring s (match-end ng))))))
  10922. s))
  10923. (defun org-recenter-calendar (date)
  10924. "If the calendar is visible, recenter it to DATE."
  10925. (let* ((win (selected-window))
  10926. (cwin (get-buffer-window "*Calendar*" t))
  10927. (calendar-move-hook nil))
  10928. (when cwin
  10929. (select-window cwin)
  10930. (calendar-goto-date (if (listp date) date
  10931. (calendar-gregorian-from-absolute date)))
  10932. (select-window win))))
  10933. (defun org-goto-calendar (&optional arg)
  10934. "Go to the Emacs calendar at the current date.
  10935. If there is a time stamp in the current line, go to that date.
  10936. A prefix ARG can be used to force the current date."
  10937. (interactive "P")
  10938. (let ((tsr org-ts-regexp) diff
  10939. (calendar-move-hook nil)
  10940. (calendar-view-holidays-initially-flag nil)
  10941. (view-calendar-holidays-initially nil)
  10942. (calendar-view-diary-initially-flag nil)
  10943. (view-diary-entries-initially nil))
  10944. (if (or (org-at-timestamp-p)
  10945. (save-excursion
  10946. (beginning-of-line 1)
  10947. (looking-at (concat ".*" tsr))))
  10948. (let ((d1 (time-to-days (current-time)))
  10949. (d2 (time-to-days
  10950. (org-time-string-to-time (match-string 1)))))
  10951. (setq diff (- d2 d1))))
  10952. (calendar)
  10953. (calendar-goto-today)
  10954. (if (and diff (not arg)) (calendar-forward-day diff))))
  10955. (defun org-get-date-from-calendar ()
  10956. "Return a list (month day year) of date at point in calendar."
  10957. (with-current-buffer "*Calendar*"
  10958. (save-match-data
  10959. (calendar-cursor-to-date))))
  10960. (defun org-date-from-calendar ()
  10961. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10962. If there is already a time stamp at the cursor position, update it."
  10963. (interactive)
  10964. (if (org-at-timestamp-p t)
  10965. (org-timestamp-change 0 'calendar)
  10966. (let ((cal-date (org-get-date-from-calendar)))
  10967. (org-insert-time-stamp
  10968. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10969. (defun org-minutes-to-hh:mm-string (m)
  10970. "Compute H:MM from a number of minutes."
  10971. (let ((h (/ m 60)))
  10972. (setq m (- m (* 60 h)))
  10973. (format org-time-clocksum-format h m)))
  10974. (defun org-hh:mm-string-to-minutes (s)
  10975. "Convert a string H:MM to a number of minutes."
  10976. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10977. (+ (* (string-to-number (match-string 1 s)) 60)
  10978. (string-to-number (match-string 2 s)))
  10979. 0))
  10980. ;;;; Agenda files
  10981. ;;;###autoload
  10982. (defun org-iswitchb (&optional arg)
  10983. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10984. With a prefix argument, restrict available to files.
  10985. With two prefix arguments, restrict available buffers to agenda files.
  10986. Due to some yet unresolved reason, the global function
  10987. `iswitchb-mode' needs to be active for this function to work."
  10988. (interactive "P")
  10989. (require 'iswitchb)
  10990. (let ((enabled iswitchb-mode) blist)
  10991. (or enabled (iswitchb-mode 1))
  10992. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10993. ((equal arg '(16)) (org-buffer-list 'agenda))
  10994. (t (org-buffer-list))))
  10995. (unwind-protect
  10996. (let ((iswitchb-make-buflist-hook
  10997. (lambda ()
  10998. (setq iswitchb-temp-buflist
  10999. (mapcar 'buffer-name blist)))))
  11000. (switch-to-buffer
  11001. (iswitchb-read-buffer
  11002. "Switch-to: " nil t))
  11003. (or enabled (iswitchb-mode -1))))))
  11004. (defun org-buffer-list (&optional predicate exclude-tmp)
  11005. "Return a list of Org buffers.
  11006. PREDICATE can be `export', `files' or `agenda'.
  11007. export restrict the list to Export buffers.
  11008. files restrict the list to buffers visiting Org files.
  11009. agenda restrict the list to buffers visiting agenda files.
  11010. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11011. (let* ((bfn nil)
  11012. (agenda-files (and (eq predicate 'agenda)
  11013. (mapcar 'file-truename (org-agenda-files t))))
  11014. (filter
  11015. (cond
  11016. ((eq predicate 'files)
  11017. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11018. ((eq predicate 'export)
  11019. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11020. ((eq predicate 'agenda)
  11021. (lambda (b)
  11022. (with-current-buffer b
  11023. (and (eq major-mode 'org-mode)
  11024. (setq bfn (buffer-file-name b))
  11025. (member (file-truename bfn) agenda-files)))))
  11026. (t (lambda (b) (with-current-buffer b
  11027. (or (eq major-mode 'org-mode)
  11028. (string-match "\*Org .*Export"
  11029. (buffer-name b)))))))))
  11030. (delq nil
  11031. (mapcar
  11032. (lambda(b)
  11033. (if (and (funcall filter b)
  11034. (or (not exclude-tmp)
  11035. (not (string-match "tmp" (buffer-name b)))))
  11036. b
  11037. nil))
  11038. (buffer-list)))))
  11039. (defun org-agenda-files (&optional unrestricted archives)
  11040. "Get the list of agenda files.
  11041. Optional UNRESTRICTED means return the full list even if a restriction
  11042. is currently in place.
  11043. When ARCHIVES is t, include all archive files hat are really being
  11044. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11045. `org-agenda-archives-mode' is t."
  11046. (let ((files
  11047. (cond
  11048. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11049. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11050. ((listp org-agenda-files) org-agenda-files)
  11051. (t (error "Invalid value of `org-agenda-files'")))))
  11052. (setq files (apply 'append
  11053. (mapcar (lambda (f)
  11054. (if (file-directory-p f)
  11055. (directory-files
  11056. f t org-agenda-file-regexp)
  11057. (list f)))
  11058. files)))
  11059. (when org-agenda-skip-unavailable-files
  11060. (setq files (delq nil
  11061. (mapcar (function
  11062. (lambda (file)
  11063. (and (file-readable-p file) file)))
  11064. files))))
  11065. (when (or (eq archives t)
  11066. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11067. (setq files (org-add-archive-files files)))
  11068. files))
  11069. (defun org-edit-agenda-file-list ()
  11070. "Edit the list of agenda files.
  11071. Depending on setup, this either uses customize to edit the variable
  11072. `org-agenda-files', or it visits the file that is holding the list. In the
  11073. latter case, the buffer is set up in a way that saving it automatically kills
  11074. the buffer and restores the previous window configuration."
  11075. (interactive)
  11076. (if (stringp org-agenda-files)
  11077. (let ((cw (current-window-configuration)))
  11078. (find-file org-agenda-files)
  11079. (org-set-local 'org-window-configuration cw)
  11080. (org-add-hook 'after-save-hook
  11081. (lambda ()
  11082. (set-window-configuration
  11083. (prog1 org-window-configuration
  11084. (kill-buffer (current-buffer))))
  11085. (org-install-agenda-files-menu)
  11086. (message "New agenda file list installed"))
  11087. nil 'local)
  11088. (message "%s" (substitute-command-keys
  11089. "Edit list and finish with \\[save-buffer]")))
  11090. (customize-variable 'org-agenda-files)))
  11091. (defun org-store-new-agenda-file-list (list)
  11092. "Set new value for the agenda file list and save it correcly."
  11093. (if (stringp org-agenda-files)
  11094. (let ((f org-agenda-files) b)
  11095. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11096. (with-temp-file f
  11097. (insert (mapconcat 'identity list "\n") "\n")))
  11098. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11099. (setq org-agenda-files list)
  11100. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11101. (defun org-read-agenda-file-list ()
  11102. "Read the list of agenda files from a file."
  11103. (when (file-directory-p org-agenda-files)
  11104. (error "`org-agenda-files' cannot be a single directory"))
  11105. (when (stringp org-agenda-files)
  11106. (with-temp-buffer
  11107. (insert-file-contents org-agenda-files)
  11108. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11109. ;;;###autoload
  11110. (defun org-cycle-agenda-files ()
  11111. "Cycle through the files in `org-agenda-files'.
  11112. If the current buffer visits an agenda file, find the next one in the list.
  11113. If the current buffer does not, find the first agenda file."
  11114. (interactive)
  11115. (let* ((fs (org-agenda-files t))
  11116. (files (append fs (list (car fs))))
  11117. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11118. file)
  11119. (unless files (error "No agenda files"))
  11120. (catch 'exit
  11121. (while (setq file (pop files))
  11122. (if (equal (file-truename file) tcf)
  11123. (when (car files)
  11124. (find-file (car files))
  11125. (throw 'exit t))))
  11126. (find-file (car fs)))
  11127. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11128. (defun org-agenda-file-to-front (&optional to-end)
  11129. "Move/add the current file to the top of the agenda file list.
  11130. If the file is not present in the list, it is added to the front. If it is
  11131. present, it is moved there. With optional argument TO-END, add/move to the
  11132. end of the list."
  11133. (interactive "P")
  11134. (let ((org-agenda-skip-unavailable-files nil)
  11135. (file-alist (mapcar (lambda (x)
  11136. (cons (file-truename x) x))
  11137. (org-agenda-files t)))
  11138. (ctf (file-truename buffer-file-name))
  11139. x had)
  11140. (setq x (assoc ctf file-alist) had x)
  11141. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11142. (if to-end
  11143. (setq file-alist (append (delq x file-alist) (list x)))
  11144. (setq file-alist (cons x (delq x file-alist))))
  11145. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11146. (org-install-agenda-files-menu)
  11147. (message "File %s to %s of agenda file list"
  11148. (if had "moved" "added") (if to-end "end" "front"))))
  11149. (defun org-remove-file (&optional file)
  11150. "Remove current file from the list of files in variable `org-agenda-files'.
  11151. These are the files which are being checked for agenda entries.
  11152. Optional argument FILE means, use this file instead of the current."
  11153. (interactive)
  11154. (let* ((org-agenda-skip-unavailable-files nil)
  11155. (file (or file buffer-file-name))
  11156. (true-file (file-truename file))
  11157. (afile (abbreviate-file-name file))
  11158. (files (delq nil (mapcar
  11159. (lambda (x)
  11160. (if (equal true-file
  11161. (file-truename x))
  11162. nil x))
  11163. (org-agenda-files t)))))
  11164. (if (not (= (length files) (length (org-agenda-files t))))
  11165. (progn
  11166. (org-store-new-agenda-file-list files)
  11167. (org-install-agenda-files-menu)
  11168. (message "Removed file: %s" afile))
  11169. (message "File was not in list: %s (not removed)" afile))))
  11170. (defun org-file-menu-entry (file)
  11171. (vector file (list 'find-file file) t))
  11172. (defun org-check-agenda-file (file)
  11173. "Make sure FILE exists. If not, ask user what to do."
  11174. (when (not (file-exists-p file))
  11175. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11176. (abbreviate-file-name file))
  11177. (let ((r (downcase (read-char-exclusive))))
  11178. (cond
  11179. ((equal r ?r)
  11180. (org-remove-file file)
  11181. (throw 'nextfile t))
  11182. (t (error "Abort"))))))
  11183. (defun org-get-agenda-file-buffer (file)
  11184. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11185. it to the list of buffers which might be released later."
  11186. (let ((buf (org-find-base-buffer-visiting file)))
  11187. (if buf
  11188. buf ; just return it
  11189. ;; Make a new buffer and remember it
  11190. (setq buf (find-file-noselect file))
  11191. (if buf (push buf org-agenda-new-buffers))
  11192. buf)))
  11193. (defun org-release-buffers (blist)
  11194. "Release all buffers in list, asking the user for confirmation when needed.
  11195. When a buffer is unmodified, it is just killed. When modified, it is saved
  11196. \(if the user agrees) and then killed."
  11197. (let (buf file)
  11198. (while (setq buf (pop blist))
  11199. (setq file (buffer-file-name buf))
  11200. (when (and (buffer-modified-p buf)
  11201. file
  11202. (y-or-n-p (format "Save file %s? " file)))
  11203. (with-current-buffer buf (save-buffer)))
  11204. (kill-buffer buf))))
  11205. (defun org-prepare-agenda-buffers (files)
  11206. "Create buffers for all agenda files, protect archived trees and comments."
  11207. (interactive)
  11208. (let ((pa '(:org-archived t))
  11209. (pc '(:org-comment t))
  11210. (pall '(:org-archived t :org-comment t))
  11211. (inhibit-read-only t)
  11212. (rea (concat ":" org-archive-tag ":"))
  11213. bmp file re)
  11214. (save-excursion
  11215. (save-restriction
  11216. (while (setq file (pop files))
  11217. (if (bufferp file)
  11218. (set-buffer file)
  11219. (org-check-agenda-file file)
  11220. (set-buffer (org-get-agenda-file-buffer file)))
  11221. (widen)
  11222. (setq bmp (buffer-modified-p))
  11223. (org-refresh-category-properties)
  11224. (setq org-todo-keywords-for-agenda
  11225. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11226. (setq org-done-keywords-for-agenda
  11227. (append org-done-keywords-for-agenda org-done-keywords))
  11228. (setq org-todo-keyword-alist-for-agenda
  11229. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11230. (setq org-tag-alist-for-agenda
  11231. (append org-tag-alist-for-agenda org-tag-alist))
  11232. (save-excursion
  11233. (remove-text-properties (point-min) (point-max) pall)
  11234. (when org-agenda-skip-archived-trees
  11235. (goto-char (point-min))
  11236. (while (re-search-forward rea nil t)
  11237. (if (org-on-heading-p t)
  11238. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11239. (goto-char (point-min))
  11240. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11241. (while (re-search-forward re nil t)
  11242. (add-text-properties
  11243. (match-beginning 0) (org-end-of-subtree t) pc)))
  11244. (set-buffer-modified-p bmp))))
  11245. (setq org-todo-keyword-alist-for-agenda
  11246. (org-uniquify org-todo-keyword-alist-for-agenda)
  11247. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11248. ;;;; Embedded LaTeX
  11249. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11250. "Keymap for the minor `org-cdlatex-mode'.")
  11251. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11252. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11253. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11254. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11255. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11256. (defvar org-cdlatex-texmathp-advice-is-done nil
  11257. "Flag remembering if we have applied the advice to texmathp already.")
  11258. (define-minor-mode org-cdlatex-mode
  11259. "Toggle the minor `org-cdlatex-mode'.
  11260. This mode supports entering LaTeX environment and math in LaTeX fragments
  11261. in Org-mode.
  11262. \\{org-cdlatex-mode-map}"
  11263. nil " OCDL" nil
  11264. (when org-cdlatex-mode (require 'cdlatex))
  11265. (unless org-cdlatex-texmathp-advice-is-done
  11266. (setq org-cdlatex-texmathp-advice-is-done t)
  11267. (defadvice texmathp (around org-math-always-on activate)
  11268. "Always return t in org-mode buffers.
  11269. This is because we want to insert math symbols without dollars even outside
  11270. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11271. en embedded LaTeX fragement, let texmathp do its job.
  11272. \\[org-cdlatex-mode-map]"
  11273. (interactive)
  11274. (let (p)
  11275. (cond
  11276. ((not (org-mode-p)) ad-do-it)
  11277. ((eq this-command 'cdlatex-math-symbol)
  11278. (setq ad-return-value t
  11279. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11280. (t
  11281. (let ((p (org-inside-LaTeX-fragment-p)))
  11282. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11283. (setq ad-return-value t
  11284. texmathp-why '("Org-mode embedded math" . 0))
  11285. (if p ad-do-it)))))))))
  11286. (defun turn-on-org-cdlatex ()
  11287. "Unconditionally turn on `org-cdlatex-mode'."
  11288. (org-cdlatex-mode 1))
  11289. (defun org-inside-LaTeX-fragment-p ()
  11290. "Test if point is inside a LaTeX fragment.
  11291. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11292. sequence appearing also before point.
  11293. Even though the matchers for math are configurable, this function assumes
  11294. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11295. delimiters are skipped when they have been removed by customization.
  11296. The return value is nil, or a cons cell with the delimiter and
  11297. and the position of this delimiter.
  11298. This function does a reasonably good job, but can locally be fooled by
  11299. for example currency specifications. For example it will assume being in
  11300. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11301. fragments that are properly closed, but during editing, we have to live
  11302. with the uncertainty caused by missing closing delimiters. This function
  11303. looks only before point, not after."
  11304. (catch 'exit
  11305. (let ((pos (point))
  11306. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11307. (lim (progn
  11308. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11309. (point)))
  11310. dd-on str (start 0) m re)
  11311. (goto-char pos)
  11312. (when dodollar
  11313. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11314. re (nth 1 (assoc "$" org-latex-regexps)))
  11315. (while (string-match re str start)
  11316. (cond
  11317. ((= (match-end 0) (length str))
  11318. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11319. ((= (match-end 0) (- (length str) 5))
  11320. (throw 'exit nil))
  11321. (t (setq start (match-end 0))))))
  11322. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11323. (goto-char pos)
  11324. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11325. (and (match-beginning 2) (throw 'exit nil))
  11326. ;; count $$
  11327. (while (re-search-backward "\\$\\$" lim t)
  11328. (setq dd-on (not dd-on)))
  11329. (goto-char pos)
  11330. (if dd-on (cons "$$" m))))))
  11331. (defun org-try-cdlatex-tab ()
  11332. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11333. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11334. - inside a LaTeX fragment, or
  11335. - after the first word in a line, where an abbreviation expansion could
  11336. insert a LaTeX environment."
  11337. (when org-cdlatex-mode
  11338. (cond
  11339. ((save-excursion
  11340. (skip-chars-backward "a-zA-Z0-9*")
  11341. (skip-chars-backward " \t")
  11342. (bolp))
  11343. (cdlatex-tab) t)
  11344. ((org-inside-LaTeX-fragment-p)
  11345. (cdlatex-tab) t)
  11346. (t nil))))
  11347. (defun org-cdlatex-underscore-caret (&optional arg)
  11348. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11349. Revert to the normal definition outside of these fragments."
  11350. (interactive "P")
  11351. (if (org-inside-LaTeX-fragment-p)
  11352. (call-interactively 'cdlatex-sub-superscript)
  11353. (let (org-cdlatex-mode)
  11354. (call-interactively (key-binding (vector last-input-event))))))
  11355. (defun org-cdlatex-math-modify (&optional arg)
  11356. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11357. Revert to the normal definition outside of these fragments."
  11358. (interactive "P")
  11359. (if (org-inside-LaTeX-fragment-p)
  11360. (call-interactively 'cdlatex-math-modify)
  11361. (let (org-cdlatex-mode)
  11362. (call-interactively (key-binding (vector last-input-event))))))
  11363. (defvar org-latex-fragment-image-overlays nil
  11364. "List of overlays carrying the images of latex fragments.")
  11365. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11366. (defun org-remove-latex-fragment-image-overlays ()
  11367. "Remove all overlays with LaTeX fragment images in current buffer."
  11368. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11369. (setq org-latex-fragment-image-overlays nil))
  11370. (defun org-preview-latex-fragment (&optional subtree)
  11371. "Preview the LaTeX fragment at point, or all locally or globally.
  11372. If the cursor is in a LaTeX fragment, create the image and overlay
  11373. it over the source code. If there is no fragment at point, display
  11374. all fragments in the current text, from one headline to the next. With
  11375. prefix SUBTREE, display all fragments in the current subtree. With a
  11376. double prefix `C-u C-u', or when the cursor is before the first headline,
  11377. display all fragments in the buffer.
  11378. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11379. (interactive "P")
  11380. (org-remove-latex-fragment-image-overlays)
  11381. (save-excursion
  11382. (save-restriction
  11383. (let (beg end at msg)
  11384. (cond
  11385. ((or (equal subtree '(16))
  11386. (not (save-excursion
  11387. (re-search-backward (concat "^" outline-regexp) nil t))))
  11388. (setq beg (point-min) end (point-max)
  11389. msg "Creating images for buffer...%s"))
  11390. ((equal subtree '(4))
  11391. (org-back-to-heading)
  11392. (setq beg (point) end (org-end-of-subtree t)
  11393. msg "Creating images for subtree...%s"))
  11394. (t
  11395. (if (setq at (org-inside-LaTeX-fragment-p))
  11396. (goto-char (max (point-min) (- (cdr at) 2)))
  11397. (org-back-to-heading))
  11398. (setq beg (point) end (progn (outline-next-heading) (point))
  11399. msg (if at "Creating image...%s"
  11400. "Creating images for entry...%s"))))
  11401. (message msg "")
  11402. (narrow-to-region beg end)
  11403. (goto-char beg)
  11404. (org-format-latex
  11405. (concat "ltxpng/" (file-name-sans-extension
  11406. (file-name-nondirectory
  11407. buffer-file-name)))
  11408. default-directory 'overlays msg at 'forbuffer)
  11409. (message msg "done. Use `C-c C-c' to remove images.")))))
  11410. (defvar org-latex-regexps
  11411. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11412. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11413. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11414. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11415. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11416. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11417. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11418. "Regular expressions for matching embedded LaTeX.")
  11419. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11420. "Replace LaTeX fragments with links to an image, and produce images."
  11421. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11422. (let* ((prefixnodir (file-name-nondirectory prefix))
  11423. (absprefix (expand-file-name prefix dir))
  11424. (todir (file-name-directory absprefix))
  11425. (opt org-format-latex-options)
  11426. (matchers (plist-get opt :matchers))
  11427. (re-list org-latex-regexps)
  11428. (cnt 0) txt link beg end re e checkdir
  11429. m n block linkfile movefile ov)
  11430. ;; Check if there are old images files with this prefix, and remove them
  11431. (when (file-directory-p todir)
  11432. (mapc 'delete-file
  11433. (directory-files
  11434. todir 'full
  11435. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11436. ;; Check the different regular expressions
  11437. (while (setq e (pop re-list))
  11438. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11439. block (if (nth 3 e) "\n\n" ""))
  11440. (when (member m matchers)
  11441. (goto-char (point-min))
  11442. (while (re-search-forward re nil t)
  11443. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11444. (setq txt (match-string n)
  11445. beg (match-beginning n) end (match-end n)
  11446. cnt (1+ cnt)
  11447. linkfile (format "%s_%04d.png" prefix cnt)
  11448. movefile (format "%s_%04d.png" absprefix cnt)
  11449. link (concat block "[[file:" linkfile "]]" block))
  11450. (if msg (message msg cnt))
  11451. (goto-char beg)
  11452. (unless checkdir ; make sure the directory exists
  11453. (setq checkdir t)
  11454. (or (file-directory-p todir) (make-directory todir)))
  11455. (org-create-formula-image
  11456. txt movefile opt forbuffer)
  11457. (if overlays
  11458. (progn
  11459. (setq ov (org-make-overlay beg end))
  11460. (if (featurep 'xemacs)
  11461. (progn
  11462. (org-overlay-put ov 'invisible t)
  11463. (org-overlay-put
  11464. ov 'end-glyph
  11465. (make-glyph (vector 'png :file movefile))))
  11466. (org-overlay-put
  11467. ov 'display
  11468. (list 'image :type 'png :file movefile :ascent 'center)))
  11469. (push ov org-latex-fragment-image-overlays)
  11470. (goto-char end))
  11471. (delete-region beg end)
  11472. (insert link))))))))
  11473. ;; This function borrows from Ganesh Swami's latex2png.el
  11474. (defun org-create-formula-image (string tofile options buffer)
  11475. (let* ((tmpdir (if (featurep 'xemacs)
  11476. (temp-directory)
  11477. temporary-file-directory))
  11478. (texfilebase (make-temp-name
  11479. (expand-file-name "orgtex" tmpdir)))
  11480. (texfile (concat texfilebase ".tex"))
  11481. (dvifile (concat texfilebase ".dvi"))
  11482. (pngfile (concat texfilebase ".png"))
  11483. (fnh (if (featurep 'xemacs)
  11484. (font-height (get-face-font 'default))
  11485. (face-attribute 'default :height nil)))
  11486. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11487. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11488. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11489. "Black"))
  11490. (bg (or (plist-get options (if buffer :background :html-background))
  11491. "Transparent")))
  11492. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11493. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11494. (with-temp-file texfile
  11495. (insert org-format-latex-header
  11496. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11497. (let ((dir default-directory))
  11498. (condition-case nil
  11499. (progn
  11500. (cd tmpdir)
  11501. (call-process "latex" nil nil nil texfile))
  11502. (error nil))
  11503. (cd dir))
  11504. (if (not (file-exists-p dvifile))
  11505. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11506. (condition-case nil
  11507. (call-process "dvipng" nil nil nil
  11508. "-E" "-fg" fg "-bg" bg
  11509. "-D" dpi
  11510. ;;"-x" scale "-y" scale
  11511. "-T" "tight"
  11512. "-o" pngfile
  11513. dvifile)
  11514. (error nil))
  11515. (if (not (file-exists-p pngfile))
  11516. (progn (message "Failed to create png file from %s" texfile) nil)
  11517. ;; Use the requested file name and clean up
  11518. (copy-file pngfile tofile 'replace)
  11519. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11520. (delete-file (concat texfilebase e)))
  11521. pngfile))))
  11522. (defun org-dvipng-color (attr)
  11523. "Return an rgb color specification for dvipng."
  11524. (apply 'format "rgb %s %s %s"
  11525. (mapcar 'org-normalize-color
  11526. (color-values (face-attribute 'default attr nil)))))
  11527. (defun org-normalize-color (value)
  11528. "Return string to be used as color value for an RGB component."
  11529. (format "%g" (/ value 65535.0)))
  11530. ;;;; Key bindings
  11531. ;; Make `C-c C-x' a prefix key
  11532. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11533. ;; TAB key with modifiers
  11534. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11535. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11536. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11537. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11538. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11539. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11540. ;; The following line is necessary under Suse GNU/Linux
  11541. (unless (featurep 'xemacs)
  11542. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11543. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11544. (define-key org-mode-map [backtab] 'org-shifttab)
  11545. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11546. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11547. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11548. ;; Cursor keys with modifiers
  11549. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11550. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11551. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11552. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11553. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11554. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11555. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11556. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11557. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11558. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11559. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11560. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11561. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11562. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11563. ;;; Extra keys for tty access.
  11564. ;; We only set them when really needed because otherwise the
  11565. ;; menus don't show the simple keys
  11566. (when (or org-use-extra-keys
  11567. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11568. (not window-system))
  11569. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11570. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11571. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11572. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11573. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11574. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11575. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11576. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11577. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11578. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11579. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11580. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11581. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11582. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11583. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11584. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11585. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11586. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11587. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11588. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11589. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11590. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11591. ;; All the other keys
  11592. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11593. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11594. (if (boundp 'narrow-map)
  11595. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11596. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11597. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11598. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11599. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11600. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11601. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11602. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11603. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11604. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11605. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11606. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11607. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11608. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11609. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11610. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11611. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11612. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11613. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11614. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11615. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11616. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11617. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11618. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11619. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11620. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11621. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11622. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11623. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11624. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11625. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11626. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11627. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11628. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11629. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11630. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11631. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11632. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11633. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11634. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11635. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11636. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11637. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11638. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11639. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11640. (org-defkey org-mode-map "\C-m" 'org-return)
  11641. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11642. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11643. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11644. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11645. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11646. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11647. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11648. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11649. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11650. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11651. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11652. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11653. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11654. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11655. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11656. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11657. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11658. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11659. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11660. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11661. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11662. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11663. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11664. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11665. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11666. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11667. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11668. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11669. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11670. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11671. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11672. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11673. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11674. (when (featurep 'xemacs)
  11675. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11676. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11677. (defun org-self-insert-command (N)
  11678. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11679. If the cursor is in a table looking at whitespace, the whitespace is
  11680. overwritten, and the table is not marked as requiring realignment."
  11681. (interactive "p")
  11682. (if (and (org-table-p)
  11683. (progn
  11684. ;; check if we blank the field, and if that triggers align
  11685. (and (featurep 'org-table) org-table-auto-blank-field
  11686. (member last-command
  11687. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11688. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11689. ;; got extra space, this field does not determine column width
  11690. (let (org-table-may-need-update) (org-table-blank-field))
  11691. ;; no extra space, this field may determine column width
  11692. (org-table-blank-field)))
  11693. t)
  11694. (eq N 1)
  11695. (looking-at "[^|\n]* |"))
  11696. (let (org-table-may-need-update)
  11697. (goto-char (1- (match-end 0)))
  11698. (delete-backward-char 1)
  11699. (goto-char (match-beginning 0))
  11700. (self-insert-command N))
  11701. (setq org-table-may-need-update t)
  11702. (self-insert-command N)
  11703. (org-fix-tags-on-the-fly)))
  11704. (defun org-fix-tags-on-the-fly ()
  11705. (when (and (equal (char-after (point-at-bol)) ?*)
  11706. (org-on-heading-p))
  11707. (org-align-tags-here org-tags-column)))
  11708. (defun org-delete-backward-char (N)
  11709. "Like `delete-backward-char', insert whitespace at field end in tables.
  11710. When deleting backwards, in tables this function will insert whitespace in
  11711. front of the next \"|\" separator, to keep the table aligned. The table will
  11712. still be marked for re-alignment if the field did fill the entire column,
  11713. because, in this case the deletion might narrow the column."
  11714. (interactive "p")
  11715. (if (and (org-table-p)
  11716. (eq N 1)
  11717. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11718. (looking-at ".*?|"))
  11719. (let ((pos (point))
  11720. (noalign (looking-at "[^|\n\r]* |"))
  11721. (c org-table-may-need-update))
  11722. (backward-delete-char N)
  11723. (skip-chars-forward "^|")
  11724. (insert " ")
  11725. (goto-char (1- pos))
  11726. ;; noalign: if there were two spaces at the end, this field
  11727. ;; does not determine the width of the column.
  11728. (if noalign (setq org-table-may-need-update c)))
  11729. (backward-delete-char N)
  11730. (org-fix-tags-on-the-fly)))
  11731. (defun org-delete-char (N)
  11732. "Like `delete-char', but insert whitespace at field end in tables.
  11733. When deleting characters, in tables this function will insert whitespace in
  11734. front of the next \"|\" separator, to keep the table aligned. The table will
  11735. still be marked for re-alignment if the field did fill the entire column,
  11736. because, in this case the deletion might narrow the column."
  11737. (interactive "p")
  11738. (if (and (org-table-p)
  11739. (not (bolp))
  11740. (not (= (char-after) ?|))
  11741. (eq N 1))
  11742. (if (looking-at ".*?|")
  11743. (let ((pos (point))
  11744. (noalign (looking-at "[^|\n\r]* |"))
  11745. (c org-table-may-need-update))
  11746. (replace-match (concat
  11747. (substring (match-string 0) 1 -1)
  11748. " |"))
  11749. (goto-char pos)
  11750. ;; noalign: if there were two spaces at the end, this field
  11751. ;; does not determine the width of the column.
  11752. (if noalign (setq org-table-may-need-update c)))
  11753. (delete-char N))
  11754. (delete-char N)
  11755. (org-fix-tags-on-the-fly)))
  11756. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11757. (put 'org-self-insert-command 'delete-selection t)
  11758. (put 'orgtbl-self-insert-command 'delete-selection t)
  11759. (put 'org-delete-char 'delete-selection 'supersede)
  11760. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11761. ;; Make `flyspell-mode' delay after some commands
  11762. (put 'org-self-insert-command 'flyspell-delayed t)
  11763. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11764. (put 'org-delete-char 'flyspell-delayed t)
  11765. (put 'org-delete-backward-char 'flyspell-delayed t)
  11766. ;; Make pabbrev-mode expand after org-mode commands
  11767. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11768. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11769. ;; How to do this: Measure non-white length of current string
  11770. ;; If equal to column width, we should realign.
  11771. (defun org-remap (map &rest commands)
  11772. "In MAP, remap the functions given in COMMANDS.
  11773. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11774. (let (new old)
  11775. (while commands
  11776. (setq old (pop commands) new (pop commands))
  11777. (if (fboundp 'command-remapping)
  11778. (org-defkey map (vector 'remap old) new)
  11779. (substitute-key-definition old new map global-map)))))
  11780. (when (eq org-enable-table-editor 'optimized)
  11781. ;; If the user wants maximum table support, we need to hijack
  11782. ;; some standard editing functions
  11783. (org-remap org-mode-map
  11784. 'self-insert-command 'org-self-insert-command
  11785. 'delete-char 'org-delete-char
  11786. 'delete-backward-char 'org-delete-backward-char)
  11787. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11788. (defun org-shiftcursor-error ()
  11789. "Throw an error because Shift-Cursor command was applied in wrong context."
  11790. (error "This command is active in special context like tables, headlines or timestamps"))
  11791. (defun org-shifttab (&optional arg)
  11792. "Global visibility cycling or move to previous table field.
  11793. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11794. on context.
  11795. See the individual commands for more information."
  11796. (interactive "P")
  11797. (cond
  11798. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11799. ((integerp arg)
  11800. (message "Content view to level: %d" arg)
  11801. (org-content (prefix-numeric-value arg))
  11802. (setq org-cycle-global-status 'overview))
  11803. (t (call-interactively 'org-global-cycle))))
  11804. (defun org-shiftmetaleft ()
  11805. "Promote subtree or delete table column.
  11806. Calls `org-promote-subtree', `org-outdent-item',
  11807. or `org-table-delete-column', depending on context.
  11808. See the individual commands for more information."
  11809. (interactive)
  11810. (cond
  11811. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11812. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11813. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11814. (t (org-shiftcursor-error))))
  11815. (defun org-shiftmetaright ()
  11816. "Demote subtree or insert table column.
  11817. Calls `org-demote-subtree', `org-indent-item',
  11818. or `org-table-insert-column', depending on context.
  11819. See the individual commands for more information."
  11820. (interactive)
  11821. (cond
  11822. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11823. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11824. ((org-at-item-p) (call-interactively 'org-indent-item))
  11825. (t (org-shiftcursor-error))))
  11826. (defun org-shiftmetaup (&optional arg)
  11827. "Move subtree up or kill table row.
  11828. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11829. `org-move-item-up' depending on context. See the individual commands
  11830. for more information."
  11831. (interactive "P")
  11832. (cond
  11833. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11834. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11835. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11836. (t (org-shiftcursor-error))))
  11837. (defun org-shiftmetadown (&optional arg)
  11838. "Move subtree down or insert table row.
  11839. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11840. `org-move-item-down', depending on context. See the individual
  11841. commands for more information."
  11842. (interactive "P")
  11843. (cond
  11844. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11845. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11846. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11847. (t (org-shiftcursor-error))))
  11848. (defun org-metaleft (&optional arg)
  11849. "Promote heading or move table column to left.
  11850. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11851. With no specific context, calls the Emacs default `backward-word'.
  11852. See the individual commands for more information."
  11853. (interactive "P")
  11854. (cond
  11855. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11856. ((or (org-on-heading-p) (org-region-active-p))
  11857. (call-interactively 'org-do-promote))
  11858. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11859. (t (call-interactively 'backward-word))))
  11860. (defun org-metaright (&optional arg)
  11861. "Demote subtree or move table column to right.
  11862. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11863. With no specific context, calls the Emacs default `forward-word'.
  11864. See the individual commands for more information."
  11865. (interactive "P")
  11866. (cond
  11867. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11868. ((or (org-on-heading-p) (org-region-active-p))
  11869. (call-interactively 'org-do-demote))
  11870. ((org-at-item-p) (call-interactively 'org-indent-item))
  11871. (t (call-interactively 'forward-word))))
  11872. (defun org-metaup (&optional arg)
  11873. "Move subtree up or move table row up.
  11874. Calls `org-move-subtree-up' or `org-table-move-row' or
  11875. `org-move-item-up', depending on context. See the individual commands
  11876. for more information."
  11877. (interactive "P")
  11878. (cond
  11879. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11880. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11881. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11882. (t (transpose-lines 1) (beginning-of-line -1))))
  11883. (defun org-metadown (&optional arg)
  11884. "Move subtree down or move table row down.
  11885. Calls `org-move-subtree-down' or `org-table-move-row' or
  11886. `org-move-item-down', depending on context. See the individual
  11887. commands for more information."
  11888. (interactive "P")
  11889. (cond
  11890. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11891. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11892. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11893. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11894. (defun org-shiftup (&optional arg)
  11895. "Increase item in timestamp or increase priority of current headline.
  11896. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11897. depending on context. See the individual commands for more information."
  11898. (interactive "P")
  11899. (cond
  11900. ((org-at-timestamp-p t)
  11901. (call-interactively (if org-edit-timestamp-down-means-later
  11902. 'org-timestamp-down 'org-timestamp-up)))
  11903. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11904. ((org-at-item-p) (call-interactively 'org-previous-item))
  11905. ((org-clocktable-try-shift 'up arg))
  11906. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11907. (defun org-shiftdown (&optional arg)
  11908. "Decrease item in timestamp or decrease priority of current headline.
  11909. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11910. depending on context. See the individual commands for more information."
  11911. (interactive "P")
  11912. (cond
  11913. ((org-at-timestamp-p t)
  11914. (call-interactively (if org-edit-timestamp-down-means-later
  11915. 'org-timestamp-up 'org-timestamp-down)))
  11916. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11917. ((org-clocktable-try-shift 'down arg))
  11918. (t (call-interactively 'org-next-item))))
  11919. (defun org-shiftright (&optional arg)
  11920. "Next TODO keyword or timestamp one day later, depending on context."
  11921. (interactive "P")
  11922. (cond
  11923. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11924. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11925. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11926. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11927. ((org-clocktable-try-shift 'right arg))
  11928. (t (org-shiftcursor-error))))
  11929. (defun org-shiftleft (&optional arg)
  11930. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11931. (interactive "P")
  11932. (cond
  11933. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11934. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11935. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11936. ((org-at-property-p)
  11937. (call-interactively 'org-property-previous-allowed-value))
  11938. ((org-clocktable-try-shift 'left arg))
  11939. (t (org-shiftcursor-error))))
  11940. (defun org-shiftcontrolright ()
  11941. "Switch to next TODO set."
  11942. (interactive)
  11943. (cond
  11944. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11945. (t (org-shiftcursor-error))))
  11946. (defun org-shiftcontrolleft ()
  11947. "Switch to previous TODO set."
  11948. (interactive)
  11949. (cond
  11950. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11951. (t (org-shiftcursor-error))))
  11952. (defun org-ctrl-c-ret ()
  11953. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11954. (interactive)
  11955. (cond
  11956. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11957. (t (call-interactively 'org-insert-heading))))
  11958. (defun org-copy-special ()
  11959. "Copy region in table or copy current subtree.
  11960. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11961. See the individual commands for more information."
  11962. (interactive)
  11963. (call-interactively
  11964. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11965. (defun org-cut-special ()
  11966. "Cut region in table or cut current subtree.
  11967. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11968. See the individual commands for more information."
  11969. (interactive)
  11970. (call-interactively
  11971. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11972. (defun org-paste-special (arg)
  11973. "Paste rectangular region into table, or past subtree relative to level.
  11974. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11975. See the individual commands for more information."
  11976. (interactive "P")
  11977. (if (org-at-table-p)
  11978. (org-table-paste-rectangle)
  11979. (org-paste-subtree arg)))
  11980. (defun org-edit-special ()
  11981. "Call a special editor for the stuff at point.
  11982. When at a table, call the formula editor with `org-table-edit-formulas'.
  11983. When at the first line of an src example, call `org-edit-src-code'.
  11984. When in an #+include line, visit the include file. Otherwise call
  11985. `ffap' to visit the file at point."
  11986. (interactive)
  11987. (cond
  11988. ((org-at-table-p)
  11989. (call-interactively 'org-table-edit-formulas))
  11990. ((save-excursion
  11991. (beginning-of-line 1)
  11992. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11993. (find-file (org-trim (match-string 1))))
  11994. ((org-edit-src-code))
  11995. ((org-edit-fixed-width-region))
  11996. (t (call-interactively 'ffap))))
  11997. (defun org-ctrl-c-ctrl-c (&optional arg)
  11998. "Set tags in headline, or update according to changed information at point.
  11999. This command does many different things, depending on context:
  12000. - If the cursor is in a headline, prompt for tags and insert them
  12001. into the current line, aligned to `org-tags-column'. When called
  12002. with prefix arg, realign all tags in the current buffer.
  12003. - If the cursor is in one of the special #+KEYWORD lines, this
  12004. triggers scanning the buffer for these lines and updating the
  12005. information.
  12006. - If the cursor is inside a table, realign the table. This command
  12007. works even if the automatic table editor has been turned off.
  12008. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12009. the entire table.
  12010. - If the cursor is a the beginning of a dynamic block, update it.
  12011. - If the cursor is inside a table created by the table.el package,
  12012. activate that table.
  12013. - If the current buffer is a remember buffer, close note and file it.
  12014. with a prefix argument, file it without further interaction to the default
  12015. location.
  12016. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12017. links in this buffer.
  12018. - If the cursor is on a numbered item in a plain list, renumber the
  12019. ordered list.
  12020. - If the cursor is on a checkbox, toggle it."
  12021. (interactive "P")
  12022. (let ((org-enable-table-editor t))
  12023. (cond
  12024. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12025. org-occur-highlights
  12026. org-latex-fragment-image-overlays)
  12027. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12028. (org-remove-occur-highlights)
  12029. (org-remove-latex-fragment-image-overlays)
  12030. (message "Temporary highlights/overlays removed from current buffer"))
  12031. ((and (local-variable-p 'org-finish-function (current-buffer))
  12032. (fboundp org-finish-function))
  12033. (funcall org-finish-function))
  12034. ((org-at-property-p)
  12035. (call-interactively 'org-property-action))
  12036. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12037. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12038. ((org-at-table.el-p)
  12039. (require 'table)
  12040. (beginning-of-line 1)
  12041. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12042. (call-interactively 'table-recognize-table))
  12043. ((org-at-table-p)
  12044. (org-table-maybe-eval-formula)
  12045. (if arg
  12046. (call-interactively 'org-table-recalculate)
  12047. (org-table-maybe-recalculate-line))
  12048. (call-interactively 'org-table-align))
  12049. ((org-at-item-checkbox-p)
  12050. (call-interactively 'org-toggle-checkbox))
  12051. ((org-at-item-p)
  12052. (call-interactively 'org-maybe-renumber-ordered-list))
  12053. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12054. ;; Dynamic block
  12055. (beginning-of-line 1)
  12056. (save-excursion (org-update-dblock)))
  12057. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12058. (cond
  12059. ((equal (match-string 1) "TBLFM")
  12060. ;; Recalculate the table before this line
  12061. (save-excursion
  12062. (beginning-of-line 1)
  12063. (skip-chars-backward " \r\n\t")
  12064. (if (org-at-table-p)
  12065. (org-call-with-arg 'org-table-recalculate t))))
  12066. (t
  12067. ; (org-set-regexps-and-options)
  12068. ; (org-restart-font-lock)
  12069. (let ((org-inhibit-startup t)) (org-mode-restart))
  12070. (message "Local setup has been refreshed"))))
  12071. (t (error "C-c C-c can do nothing useful at this location.")))))
  12072. (defun org-mode-restart ()
  12073. "Restart Org-mode, to scan again for special lines.
  12074. Also updates the keyword regular expressions."
  12075. (interactive)
  12076. (org-mode)
  12077. (message "Org-mode restarted"))
  12078. (defun org-kill-note-or-show-branches ()
  12079. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12080. (interactive)
  12081. (if (not org-finish-function)
  12082. (call-interactively 'show-branches)
  12083. (let ((org-note-abort t))
  12084. (funcall org-finish-function))))
  12085. (defun org-return (&optional indent)
  12086. "Goto next table row or insert a newline.
  12087. Calls `org-table-next-row' or `newline', depending on context.
  12088. See the individual commands for more information."
  12089. (interactive)
  12090. (cond
  12091. ((bobp) (if indent (newline-and-indent) (newline)))
  12092. ((and (org-at-heading-p)
  12093. (looking-at
  12094. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12095. (org-show-entry)
  12096. (end-of-line 1)
  12097. (newline))
  12098. ((org-at-table-p)
  12099. (org-table-justify-field-maybe)
  12100. (call-interactively 'org-table-next-row))
  12101. (t (if indent (newline-and-indent) (newline)))))
  12102. (defun org-return-indent ()
  12103. "Goto next table row or insert a newline and indent.
  12104. Calls `org-table-next-row' or `newline-and-indent', depending on
  12105. context. See the individual commands for more information."
  12106. (interactive)
  12107. (org-return t))
  12108. (defun org-ctrl-c-star ()
  12109. "Compute table, or change heading status of lines.
  12110. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12111. depending on context. This will also turn a plain list item or a normal
  12112. line into a subheading."
  12113. (interactive)
  12114. (cond
  12115. ((org-at-table-p)
  12116. (call-interactively 'org-table-recalculate))
  12117. ((org-region-active-p)
  12118. ;; Convert all lines in region to list items
  12119. (call-interactively 'org-toggle-region-headings))
  12120. ((org-on-heading-p)
  12121. (org-toggle-region-headings (point-at-bol)
  12122. (min (1+ (point-at-eol)) (point-max))))
  12123. ((org-at-item-p)
  12124. ;; Convert to heading
  12125. (let ((level (save-match-data
  12126. (save-excursion
  12127. (condition-case nil
  12128. (progn
  12129. (org-back-to-heading t)
  12130. (funcall outline-level))
  12131. (error 0))))))
  12132. (replace-match
  12133. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12134. (t (org-toggle-region-headings (point-at-bol)
  12135. (min (1+ (point-at-eol)) (point-max))))))
  12136. (defun org-ctrl-c-minus ()
  12137. "Insert separator line in table or modify bullet status of line.
  12138. Also turns a plain line or a region of lines into list items.
  12139. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12140. `org-cycle-list-bullet', depending on context."
  12141. (interactive)
  12142. (cond
  12143. ((org-at-table-p)
  12144. (call-interactively 'org-table-insert-hline))
  12145. ((org-on-heading-p)
  12146. ;; Convert to item
  12147. (save-excursion
  12148. (beginning-of-line 1)
  12149. (if (looking-at "\\*+ ")
  12150. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12151. ((org-region-active-p)
  12152. ;; Convert all lines in region to list items
  12153. (call-interactively 'org-toggle-region-items))
  12154. ((org-in-item-p)
  12155. (call-interactively 'org-cycle-list-bullet))
  12156. (t (org-toggle-region-items (point-at-bol)
  12157. (min (1+ (point-at-eol)) (point-max))))))
  12158. (defun org-toggle-region-items (beg end)
  12159. "Convert all lines in region to list items.
  12160. If the first line is already an item, convert all list items in the region
  12161. to normal lines."
  12162. (interactive "r")
  12163. (let (l2 l)
  12164. (save-excursion
  12165. (goto-char end)
  12166. (setq l2 (org-current-line))
  12167. (goto-char beg)
  12168. (beginning-of-line 1)
  12169. (setq l (1- (org-current-line)))
  12170. (if (org-at-item-p)
  12171. ;; We already have items, de-itemize
  12172. (while (< (setq l (1+ l)) l2)
  12173. (when (org-at-item-p)
  12174. (goto-char (match-beginning 2))
  12175. (delete-region (match-beginning 2) (match-end 2))
  12176. (and (looking-at "[ \t]+") (replace-match "")))
  12177. (beginning-of-line 2))
  12178. (while (< (setq l (1+ l)) l2)
  12179. (unless (org-at-item-p)
  12180. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12181. (replace-match "\\1- \\2")))
  12182. (beginning-of-line 2))))))
  12183. (defun org-toggle-region-headings (beg end)
  12184. "Convert all lines in region to list items.
  12185. If the first line is already an item, convert all list items in the region
  12186. to normal lines."
  12187. (interactive "r")
  12188. (let (l2 l)
  12189. (save-excursion
  12190. (goto-char end)
  12191. (setq l2 (org-current-line))
  12192. (goto-char beg)
  12193. (beginning-of-line 1)
  12194. (setq l (1- (org-current-line)))
  12195. (if (org-on-heading-p)
  12196. ;; We already have headlines, de-star them
  12197. (while (< (setq l (1+ l)) l2)
  12198. (when (org-on-heading-p t)
  12199. (and (looking-at outline-regexp) (replace-match "")))
  12200. (beginning-of-line 2))
  12201. (let* ((stars (save-excursion
  12202. (re-search-backward org-complex-heading-regexp nil t)
  12203. (or (match-string 1) "*")))
  12204. (add-stars (if org-odd-levels-only "**" "*"))
  12205. (rpl (concat stars add-stars " \\2")))
  12206. (while (< (setq l (1+ l)) l2)
  12207. (unless (org-on-heading-p)
  12208. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12209. (replace-match rpl)))
  12210. (beginning-of-line 2)))))))
  12211. (defun org-meta-return (&optional arg)
  12212. "Insert a new heading or wrap a region in a table.
  12213. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12214. See the individual commands for more information."
  12215. (interactive "P")
  12216. (cond
  12217. ((org-at-table-p)
  12218. (call-interactively 'org-table-wrap-region))
  12219. (t (call-interactively 'org-insert-heading))))
  12220. ;;; Menu entries
  12221. ;; Define the Org-mode menus
  12222. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12223. '("Tbl"
  12224. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12225. ["Next Field" org-cycle (org-at-table-p)]
  12226. ["Previous Field" org-shifttab (org-at-table-p)]
  12227. ["Next Row" org-return (org-at-table-p)]
  12228. "--"
  12229. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12230. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12231. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12232. "--"
  12233. ("Column"
  12234. ["Move Column Left" org-metaleft (org-at-table-p)]
  12235. ["Move Column Right" org-metaright (org-at-table-p)]
  12236. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12237. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12238. ("Row"
  12239. ["Move Row Up" org-metaup (org-at-table-p)]
  12240. ["Move Row Down" org-metadown (org-at-table-p)]
  12241. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12242. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12243. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12244. "--"
  12245. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12246. ("Rectangle"
  12247. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12248. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12249. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12250. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12251. "--"
  12252. ("Calculate"
  12253. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12254. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12255. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12256. "--"
  12257. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12258. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12259. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12260. "--"
  12261. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12262. "--"
  12263. ["Sum Column/Rectangle" org-table-sum
  12264. (or (org-at-table-p) (org-region-active-p))]
  12265. ["Which Column?" org-table-current-column (org-at-table-p)])
  12266. ["Debug Formulas"
  12267. org-table-toggle-formula-debugger
  12268. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12269. ["Show Col/Row Numbers"
  12270. org-table-toggle-coordinate-overlays
  12271. :style toggle
  12272. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12273. "--"
  12274. ["Create" org-table-create (and (not (org-at-table-p))
  12275. org-enable-table-editor)]
  12276. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12277. ["Import from File" org-table-import (not (org-at-table-p))]
  12278. ["Export to File" org-table-export (org-at-table-p)]
  12279. "--"
  12280. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12281. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12282. '("Org"
  12283. ("Show/Hide"
  12284. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12285. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12286. ["Sparse Tree..." org-sparse-tree t]
  12287. ["Reveal Context" org-reveal t]
  12288. ["Show All" show-all t]
  12289. "--"
  12290. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12291. "--"
  12292. ["New Heading" org-insert-heading t]
  12293. ("Navigate Headings"
  12294. ["Up" outline-up-heading t]
  12295. ["Next" outline-next-visible-heading t]
  12296. ["Previous" outline-previous-visible-heading t]
  12297. ["Next Same Level" outline-forward-same-level t]
  12298. ["Previous Same Level" outline-backward-same-level t]
  12299. "--"
  12300. ["Jump" org-goto t])
  12301. ("Edit Structure"
  12302. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12303. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12304. "--"
  12305. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12306. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12307. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12308. "--"
  12309. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12310. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12311. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12312. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12313. "--"
  12314. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12315. "--"
  12316. ["Convert to odd levels" org-convert-to-odd-levels t]
  12317. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12318. ("Editing"
  12319. ["Emphasis..." org-emphasize t]
  12320. ["Edit Source Example" org-edit-special t])
  12321. ("Archive"
  12322. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12323. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12324. ; :active t :keys "C-u C-c C-x C-a"]
  12325. ["Sparse trees open ARCHIVE trees"
  12326. (setq org-sparse-tree-open-archived-trees
  12327. (not org-sparse-tree-open-archived-trees))
  12328. :style toggle :selected org-sparse-tree-open-archived-trees]
  12329. ["Cycling opens ARCHIVE trees"
  12330. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12331. :style toggle :selected org-cycle-open-archived-trees]
  12332. "--"
  12333. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12334. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12335. ; ["Check and Move Children" (org-archive-subtree '(4))
  12336. ; :active t :keys "C-u C-c C-x C-s"]
  12337. )
  12338. "--"
  12339. ("TODO Lists"
  12340. ["TODO/DONE/-" org-todo t]
  12341. ("Select keyword"
  12342. ["Next keyword" org-shiftright (org-on-heading-p)]
  12343. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12344. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12345. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12346. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12347. ["Show TODO Tree" org-show-todo-tree t]
  12348. ["Global TODO list" org-todo-list t]
  12349. "--"
  12350. ["Set Priority" org-priority t]
  12351. ["Priority Up" org-shiftup t]
  12352. ["Priority Down" org-shiftdown t])
  12353. ("TAGS and Properties"
  12354. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12355. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12356. "--"
  12357. ["Set property" 'org-set-property t]
  12358. ["Column view of properties" org-columns t]
  12359. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12360. ("Dates and Scheduling"
  12361. ["Timestamp" org-time-stamp t]
  12362. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12363. ("Change Date"
  12364. ["1 Day Later" org-shiftright t]
  12365. ["1 Day Earlier" org-shiftleft t]
  12366. ["1 ... Later" org-shiftup t]
  12367. ["1 ... Earlier" org-shiftdown t])
  12368. ["Compute Time Range" org-evaluate-time-range t]
  12369. ["Schedule Item" org-schedule t]
  12370. ["Deadline" org-deadline t]
  12371. "--"
  12372. ["Custom time format" org-toggle-time-stamp-overlays
  12373. :style radio :selected org-display-custom-times]
  12374. "--"
  12375. ["Goto Calendar" org-goto-calendar t]
  12376. ["Date from Calendar" org-date-from-calendar t])
  12377. ("Logging work"
  12378. ["Clock in" org-clock-in t]
  12379. ["Clock out" org-clock-out t]
  12380. ["Clock cancel" org-clock-cancel t]
  12381. ["Goto running clock" org-clock-goto t]
  12382. ["Display times" org-clock-display t]
  12383. ["Create clock table" org-clock-report t]
  12384. "--"
  12385. ["Record DONE time"
  12386. (progn (setq org-log-done (not org-log-done))
  12387. (message "Switching to %s will %s record a timestamp"
  12388. (car org-done-keywords)
  12389. (if org-log-done "automatically" "not")))
  12390. :style toggle :selected org-log-done])
  12391. "--"
  12392. ["Agenda Command..." org-agenda t]
  12393. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12394. ("File List for Agenda")
  12395. ("Special views current file"
  12396. ["TODO Tree" org-show-todo-tree t]
  12397. ["Check Deadlines" org-check-deadlines t]
  12398. ["Timeline" org-timeline t]
  12399. ["Tags Tree" org-tags-sparse-tree t])
  12400. "--"
  12401. ("Hyperlinks"
  12402. ["Store Link (Global)" org-store-link t]
  12403. ["Insert Link" org-insert-link t]
  12404. ["Follow Link" org-open-at-point t]
  12405. "--"
  12406. ["Next link" org-next-link t]
  12407. ["Previous link" org-previous-link t]
  12408. "--"
  12409. ["Descriptive Links"
  12410. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12411. :style radio
  12412. :selected (member '(org-link) buffer-invisibility-spec)]
  12413. ["Literal Links"
  12414. (progn
  12415. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12416. :style radio
  12417. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12418. "--"
  12419. ["Export/Publish..." org-export t]
  12420. ("LaTeX"
  12421. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12422. :selected org-cdlatex-mode]
  12423. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12424. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12425. ["Modify math symbol" org-cdlatex-math-modify
  12426. (org-inside-LaTeX-fragment-p)]
  12427. ["Export LaTeX fragments as images"
  12428. (if (featurep 'org-exp)
  12429. (setq org-export-with-LaTeX-fragments
  12430. (not org-export-with-LaTeX-fragments))
  12431. (require 'org-exp))
  12432. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12433. org-export-with-LaTeX-fragments)])
  12434. "--"
  12435. ("Documentation"
  12436. ["Show Version" org-version t]
  12437. ["Info Documentation" org-info t])
  12438. ("Customize"
  12439. ["Browse Org Group" org-customize t]
  12440. "--"
  12441. ["Expand This Menu" org-create-customize-menu
  12442. (fboundp 'customize-menu-create)])
  12443. "--"
  12444. ["Refresh setup" org-mode-restart t]
  12445. ))
  12446. (defun org-info (&optional node)
  12447. "Read documentation for Org-mode in the info system.
  12448. With optional NODE, go directly to that node."
  12449. (interactive)
  12450. (info (format "(org)%s" (or node ""))))
  12451. (defun org-install-agenda-files-menu ()
  12452. (let ((bl (buffer-list)))
  12453. (save-excursion
  12454. (while bl
  12455. (set-buffer (pop bl))
  12456. (if (org-mode-p) (setq bl nil)))
  12457. (when (org-mode-p)
  12458. (easy-menu-change
  12459. '("Org") "File List for Agenda"
  12460. (append
  12461. (list
  12462. ["Edit File List" (org-edit-agenda-file-list) t]
  12463. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12464. ["Remove Current File from List" org-remove-file t]
  12465. ["Cycle through agenda files" org-cycle-agenda-files t]
  12466. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12467. "--")
  12468. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12469. ;;;; Documentation
  12470. ;;;###autoload
  12471. (defun org-require-autoloaded-modules ()
  12472. (interactive)
  12473. (mapc 'require
  12474. '(org-agenda org-archive org-clock org-colview
  12475. org-exp org-id org-export-latex org-publish
  12476. org-remember org-table)))
  12477. ;;;###autoload
  12478. (defun org-customize ()
  12479. "Call the customize function with org as argument."
  12480. (interactive)
  12481. (org-load-modules-maybe)
  12482. (org-require-autoloaded-modules)
  12483. (customize-browse 'org))
  12484. (defun org-create-customize-menu ()
  12485. "Create a full customization menu for Org-mode, insert it into the menu."
  12486. (interactive)
  12487. (org-load-modules-maybe)
  12488. (org-require-autoloaded-modules)
  12489. (if (fboundp 'customize-menu-create)
  12490. (progn
  12491. (easy-menu-change
  12492. '("Org") "Customize"
  12493. `(["Browse Org group" org-customize t]
  12494. "--"
  12495. ,(customize-menu-create 'org)
  12496. ["Set" Custom-set t]
  12497. ["Save" Custom-save t]
  12498. ["Reset to Current" Custom-reset-current t]
  12499. ["Reset to Saved" Custom-reset-saved t]
  12500. ["Reset to Standard Settings" Custom-reset-standard t]))
  12501. (message "\"Org\"-menu now contains full customization menu"))
  12502. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12503. ;;;; Miscellaneous stuff
  12504. ;;; Generally useful functions
  12505. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12506. "Display the given MESSAGE as a warning."
  12507. (if (fboundp 'display-warning)
  12508. (display-warning 'org message
  12509. (if (featurep 'xemacs)
  12510. 'warning
  12511. :warning))
  12512. (let ((buf (get-buffer-create "*Org warnings*")))
  12513. (with-current-buffer buf
  12514. (goto-char (point-max))
  12515. (insert "Warning (Org): " message)
  12516. (unless (bolp)
  12517. (newline)))
  12518. (display-buffer buf)
  12519. (sit-for 0))))
  12520. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12521. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12522. (if (and marker (marker-buffer marker)
  12523. (buffer-live-p (marker-buffer marker)))
  12524. (progn
  12525. (switch-to-buffer (marker-buffer marker))
  12526. (if (or (> marker (point-max)) (< marker (point-min)))
  12527. (widen))
  12528. (goto-char marker))
  12529. (if bookmark
  12530. (bookmark-jump bookmark)
  12531. (error "Cannot find location"))))
  12532. (defun org-quote-csv-field (s)
  12533. "Quote field for inclusion in CSV material."
  12534. (if (string-match "[\",]" s)
  12535. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12536. s))
  12537. (defun org-plist-delete (plist property)
  12538. "Delete PROPERTY from PLIST.
  12539. This is in contrast to merely setting it to 0."
  12540. (let (p)
  12541. (while plist
  12542. (if (not (eq property (car plist)))
  12543. (setq p (plist-put p (car plist) (nth 1 plist))))
  12544. (setq plist (cddr plist)))
  12545. p))
  12546. (defun org-force-self-insert (N)
  12547. "Needed to enforce self-insert under remapping."
  12548. (interactive "p")
  12549. (self-insert-command N))
  12550. (defun org-string-width (s)
  12551. "Compute width of string, ignoring invisible characters.
  12552. This ignores character with invisibility property `org-link', and also
  12553. characters with property `org-cwidth', because these will become invisible
  12554. upon the next fontification round."
  12555. (let (b l)
  12556. (when (or (eq t buffer-invisibility-spec)
  12557. (assq 'org-link buffer-invisibility-spec))
  12558. (while (setq b (text-property-any 0 (length s)
  12559. 'invisible 'org-link s))
  12560. (setq s (concat (substring s 0 b)
  12561. (substring s (or (next-single-property-change
  12562. b 'invisible s) (length s)))))))
  12563. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12564. (setq s (concat (substring s 0 b)
  12565. (substring s (or (next-single-property-change
  12566. b 'org-cwidth s) (length s))))))
  12567. (setq l (string-width s) b -1)
  12568. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12569. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12570. l))
  12571. (defun org-base-buffer (buffer)
  12572. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12573. (if (not buffer)
  12574. buffer
  12575. (or (buffer-base-buffer buffer)
  12576. buffer)))
  12577. (defun org-trim (s)
  12578. "Remove whitespace at beginning and end of string."
  12579. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12580. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12581. s)
  12582. (defun org-wrap (string &optional width lines)
  12583. "Wrap string to either a number of lines, or a width in characters.
  12584. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12585. that costs. If there is a word longer than WIDTH, the text is actually
  12586. wrapped to the length of that word.
  12587. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12588. many lines, whatever width that takes.
  12589. The return value is a list of lines, without newlines at the end."
  12590. (let* ((words (org-split-string string "[ \t\n]+"))
  12591. (maxword (apply 'max (mapcar 'org-string-width words)))
  12592. w ll)
  12593. (cond (width
  12594. (org-do-wrap words (max maxword width)))
  12595. (lines
  12596. (setq w maxword)
  12597. (setq ll (org-do-wrap words maxword))
  12598. (if (<= (length ll) lines)
  12599. ll
  12600. (setq ll words)
  12601. (while (> (length ll) lines)
  12602. (setq w (1+ w))
  12603. (setq ll (org-do-wrap words w)))
  12604. ll))
  12605. (t (error "Cannot wrap this")))))
  12606. (defun org-do-wrap (words width)
  12607. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12608. (let (lines line)
  12609. (while words
  12610. (setq line (pop words))
  12611. (while (and words (< (+ (length line) (length (car words))) width))
  12612. (setq line (concat line " " (pop words))))
  12613. (setq lines (push line lines)))
  12614. (nreverse lines)))
  12615. (defun org-split-string (string &optional separators)
  12616. "Splits STRING into substrings at SEPARATORS.
  12617. No empty strings are returned if there are matches at the beginning
  12618. and end of string."
  12619. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12620. (start 0)
  12621. notfirst
  12622. (list nil))
  12623. (while (and (string-match rexp string
  12624. (if (and notfirst
  12625. (= start (match-beginning 0))
  12626. (< start (length string)))
  12627. (1+ start) start))
  12628. (< (match-beginning 0) (length string)))
  12629. (setq notfirst t)
  12630. (or (eq (match-beginning 0) 0)
  12631. (and (eq (match-beginning 0) (match-end 0))
  12632. (eq (match-beginning 0) start))
  12633. (setq list
  12634. (cons (substring string start (match-beginning 0))
  12635. list)))
  12636. (setq start (match-end 0)))
  12637. (or (eq start (length string))
  12638. (setq list
  12639. (cons (substring string start)
  12640. list)))
  12641. (nreverse list)))
  12642. (defun org-context ()
  12643. "Return a list of contexts of the current cursor position.
  12644. If several contexts apply, all are returned.
  12645. Each context entry is a list with a symbol naming the context, and
  12646. two positions indicating start and end of the context. Possible
  12647. contexts are:
  12648. :headline anywhere in a headline
  12649. :headline-stars on the leading stars in a headline
  12650. :todo-keyword on a TODO keyword (including DONE) in a headline
  12651. :tags on the TAGS in a headline
  12652. :priority on the priority cookie in a headline
  12653. :item on the first line of a plain list item
  12654. :item-bullet on the bullet/number of a plain list item
  12655. :checkbox on the checkbox in a plain list item
  12656. :table in an org-mode table
  12657. :table-special on a special filed in a table
  12658. :table-table in a table.el table
  12659. :link on a hyperlink
  12660. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12661. :target on a <<target>>
  12662. :radio-target on a <<<radio-target>>>
  12663. :latex-fragment on a LaTeX fragment
  12664. :latex-preview on a LaTeX fragment with overlayed preview image
  12665. This function expects the position to be visible because it uses font-lock
  12666. faces as a help to recognize the following contexts: :table-special, :link,
  12667. and :keyword."
  12668. (let* ((f (get-text-property (point) 'face))
  12669. (faces (if (listp f) f (list f)))
  12670. (p (point)) clist o)
  12671. ;; First the large context
  12672. (cond
  12673. ((org-on-heading-p t)
  12674. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12675. (when (progn
  12676. (beginning-of-line 1)
  12677. (looking-at org-todo-line-tags-regexp))
  12678. (push (org-point-in-group p 1 :headline-stars) clist)
  12679. (push (org-point-in-group p 2 :todo-keyword) clist)
  12680. (push (org-point-in-group p 4 :tags) clist))
  12681. (goto-char p)
  12682. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12683. (if (looking-at "\\[#[A-Z0-9]\\]")
  12684. (push (org-point-in-group p 0 :priority) clist)))
  12685. ((org-at-item-p)
  12686. (push (org-point-in-group p 2 :item-bullet) clist)
  12687. (push (list :item (point-at-bol)
  12688. (save-excursion (org-end-of-item) (point)))
  12689. clist)
  12690. (and (org-at-item-checkbox-p)
  12691. (push (org-point-in-group p 0 :checkbox) clist)))
  12692. ((org-at-table-p)
  12693. (push (list :table (org-table-begin) (org-table-end)) clist)
  12694. (if (memq 'org-formula faces)
  12695. (push (list :table-special
  12696. (previous-single-property-change p 'face)
  12697. (next-single-property-change p 'face)) clist)))
  12698. ((org-at-table-p 'any)
  12699. (push (list :table-table) clist)))
  12700. (goto-char p)
  12701. ;; Now the small context
  12702. (cond
  12703. ((org-at-timestamp-p)
  12704. (push (org-point-in-group p 0 :timestamp) clist))
  12705. ((memq 'org-link faces)
  12706. (push (list :link
  12707. (previous-single-property-change p 'face)
  12708. (next-single-property-change p 'face)) clist))
  12709. ((memq 'org-special-keyword faces)
  12710. (push (list :keyword
  12711. (previous-single-property-change p 'face)
  12712. (next-single-property-change p 'face)) clist))
  12713. ((org-on-target-p)
  12714. (push (org-point-in-group p 0 :target) clist)
  12715. (goto-char (1- (match-beginning 0)))
  12716. (if (looking-at org-radio-target-regexp)
  12717. (push (org-point-in-group p 0 :radio-target) clist))
  12718. (goto-char p))
  12719. ((setq o (car (delq nil
  12720. (mapcar
  12721. (lambda (x)
  12722. (if (memq x org-latex-fragment-image-overlays) x))
  12723. (org-overlays-at (point))))))
  12724. (push (list :latex-fragment
  12725. (org-overlay-start o) (org-overlay-end o)) clist)
  12726. (push (list :latex-preview
  12727. (org-overlay-start o) (org-overlay-end o)) clist))
  12728. ((org-inside-LaTeX-fragment-p)
  12729. ;; FIXME: positions wrong.
  12730. (push (list :latex-fragment (point) (point)) clist)))
  12731. (setq clist (nreverse (delq nil clist)))
  12732. clist))
  12733. ;; FIXME: Compare with at-regexp-p Do we need both?
  12734. (defun org-in-regexp (re &optional nlines visually)
  12735. "Check if point is inside a match of regexp.
  12736. Normally only the current line is checked, but you can include NLINES extra
  12737. lines both before and after point into the search.
  12738. If VISUALLY is set, require that the cursor is not after the match but
  12739. really on, so that the block visually is on the match."
  12740. (catch 'exit
  12741. (let ((pos (point))
  12742. (eol (point-at-eol (+ 1 (or nlines 0))))
  12743. (inc (if visually 1 0)))
  12744. (save-excursion
  12745. (beginning-of-line (- 1 (or nlines 0)))
  12746. (while (re-search-forward re eol t)
  12747. (if (and (<= (match-beginning 0) pos)
  12748. (>= (+ inc (match-end 0)) pos))
  12749. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12750. (defun org-at-regexp-p (regexp)
  12751. "Is point inside a match of REGEXP in the current line?"
  12752. (catch 'exit
  12753. (save-excursion
  12754. (let ((pos (point)) (end (point-at-eol)))
  12755. (beginning-of-line 1)
  12756. (while (re-search-forward regexp end t)
  12757. (if (and (<= (match-beginning 0) pos)
  12758. (>= (match-end 0) pos))
  12759. (throw 'exit t)))
  12760. nil))))
  12761. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12762. "Call `multi-occur' with buffers for all agenda files."
  12763. (interactive "sOrg-files matching: \np")
  12764. (let* ((files (org-agenda-files))
  12765. (tnames (mapcar 'file-truename files))
  12766. (extra org-agenda-text-search-extra-files)
  12767. f)
  12768. (when (eq (car extra) 'agenda-archives)
  12769. (setq extra (cdr extra))
  12770. (setq files (org-add-archive-files files)))
  12771. (while (setq f (pop extra))
  12772. (unless (member (file-truename f) tnames)
  12773. (add-to-list 'files f 'append)
  12774. (add-to-list 'tnames (file-truename f) 'append)))
  12775. (multi-occur
  12776. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12777. regexp)))
  12778. (if (boundp 'occur-mode-find-occurrence-hook)
  12779. ;; Emacs 23
  12780. (add-hook 'occur-mode-find-occurrence-hook
  12781. (lambda ()
  12782. (when (org-mode-p)
  12783. (org-reveal))))
  12784. ;; Emacs 22
  12785. (defadvice occur-mode-goto-occurrence
  12786. (after org-occur-reveal activate)
  12787. (and (org-mode-p) (org-reveal)))
  12788. (defadvice occur-mode-goto-occurrence-other-window
  12789. (after org-occur-reveal activate)
  12790. (and (org-mode-p) (org-reveal)))
  12791. (defadvice occur-mode-display-occurrence
  12792. (after org-occur-reveal activate)
  12793. (when (org-mode-p)
  12794. (let ((pos (occur-mode-find-occurrence)))
  12795. (with-current-buffer (marker-buffer pos)
  12796. (save-excursion
  12797. (goto-char pos)
  12798. (org-reveal)))))))
  12799. (defun org-uniquify (list)
  12800. "Remove duplicate elements from LIST."
  12801. (let (res)
  12802. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12803. res))
  12804. (defun org-delete-all (elts list)
  12805. "Remove all elements in ELTS from LIST."
  12806. (while elts
  12807. (setq list (delete (pop elts) list)))
  12808. list)
  12809. (defun org-back-over-empty-lines ()
  12810. "Move backwards over witespace, to the beginning of the first empty line.
  12811. Returns the number of empty lines passed."
  12812. (let ((pos (point)))
  12813. (skip-chars-backward " \t\n\r")
  12814. (beginning-of-line 2)
  12815. (goto-char (min (point) pos))
  12816. (count-lines (point) pos)))
  12817. (defun org-skip-whitespace ()
  12818. (skip-chars-forward " \t\n\r"))
  12819. (defun org-point-in-group (point group &optional context)
  12820. "Check if POINT is in match-group GROUP.
  12821. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12822. match. If the match group does ot exist or point is not inside it,
  12823. return nil."
  12824. (and (match-beginning group)
  12825. (>= point (match-beginning group))
  12826. (<= point (match-end group))
  12827. (if context
  12828. (list context (match-beginning group) (match-end group))
  12829. t)))
  12830. (defun org-switch-to-buffer-other-window (&rest args)
  12831. "Switch to buffer in a second window on the current frame.
  12832. In particular, do not allow pop-up frames."
  12833. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12834. special-display-function)
  12835. (apply 'switch-to-buffer-other-window args)))
  12836. (defun org-combine-plists (&rest plists)
  12837. "Create a single property list from all plists in PLISTS.
  12838. The process starts by copying the first list, and then setting properties
  12839. from the other lists. Settings in the last list are the most significant
  12840. ones and overrule settings in the other lists."
  12841. (let ((rtn (copy-sequence (pop plists)))
  12842. p v ls)
  12843. (while plists
  12844. (setq ls (pop plists))
  12845. (while ls
  12846. (setq p (pop ls) v (pop ls))
  12847. (setq rtn (plist-put rtn p v))))
  12848. rtn))
  12849. (defun org-move-line-down (arg)
  12850. "Move the current line down. With prefix argument, move it past ARG lines."
  12851. (interactive "p")
  12852. (let ((col (current-column))
  12853. beg end pos)
  12854. (beginning-of-line 1) (setq beg (point))
  12855. (beginning-of-line 2) (setq end (point))
  12856. (beginning-of-line (+ 1 arg))
  12857. (setq pos (move-marker (make-marker) (point)))
  12858. (insert (delete-and-extract-region beg end))
  12859. (goto-char pos)
  12860. (org-move-to-column col)))
  12861. (defun org-move-line-up (arg)
  12862. "Move the current line up. With prefix argument, move it past ARG lines."
  12863. (interactive "p")
  12864. (let ((col (current-column))
  12865. beg end pos)
  12866. (beginning-of-line 1) (setq beg (point))
  12867. (beginning-of-line 2) (setq end (point))
  12868. (beginning-of-line (- arg))
  12869. (setq pos (move-marker (make-marker) (point)))
  12870. (insert (delete-and-extract-region beg end))
  12871. (goto-char pos)
  12872. (org-move-to-column col)))
  12873. (defun org-replace-escapes (string table)
  12874. "Replace %-escapes in STRING with values in TABLE.
  12875. TABLE is an association list with keys like \"%a\" and string values.
  12876. The sequences in STRING may contain normal field width and padding information,
  12877. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12878. so values can contain further %-escapes if they are define later in TABLE."
  12879. (let ((case-fold-search nil)
  12880. e re rpl)
  12881. (while (setq e (pop table))
  12882. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12883. (while (string-match re string)
  12884. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12885. (cdr e)))
  12886. (setq string (replace-match rpl t t string))))
  12887. string))
  12888. (defun org-sublist (list start end)
  12889. "Return a section of LIST, from START to END.
  12890. Counting starts at 1."
  12891. (let (rtn (c start))
  12892. (setq list (nthcdr (1- start) list))
  12893. (while (and list (<= c end))
  12894. (push (pop list) rtn)
  12895. (setq c (1+ c)))
  12896. (nreverse rtn)))
  12897. (defun org-find-base-buffer-visiting (file)
  12898. "Like `find-buffer-visiting' but alway return the base buffer and
  12899. not an indirect buffer."
  12900. (let ((buf (find-buffer-visiting file)))
  12901. (if buf
  12902. (or (buffer-base-buffer buf) buf)
  12903. nil)))
  12904. (defun org-image-file-name-regexp ()
  12905. "Return regexp matching the file names of images."
  12906. (if (fboundp 'image-file-name-regexp)
  12907. (image-file-name-regexp)
  12908. (let ((image-file-name-extensions
  12909. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12910. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12911. (concat "\\."
  12912. (regexp-opt (nconc (mapcar 'upcase
  12913. image-file-name-extensions)
  12914. image-file-name-extensions)
  12915. t)
  12916. "\\'"))))
  12917. (defun org-file-image-p (file)
  12918. "Return non-nil if FILE is an image."
  12919. (save-match-data
  12920. (string-match (org-image-file-name-regexp) file)))
  12921. (defun org-get-cursor-date ()
  12922. "Return the date at cursor in as a time.
  12923. This works in the calendar and in the agenda, anywhere else it just
  12924. returns the current time."
  12925. (let (date day defd)
  12926. (cond
  12927. ((eq major-mode 'calendar-mode)
  12928. (setq date (calendar-cursor-to-date)
  12929. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12930. ((eq major-mode 'org-agenda-mode)
  12931. (setq day (get-text-property (point) 'day))
  12932. (if day
  12933. (setq date (calendar-gregorian-from-absolute day)
  12934. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12935. (nth 2 date))))))
  12936. (or defd (current-time))))
  12937. (defvar org-agenda-action-marker (make-marker)
  12938. "Marker pointing to the entry for the next agenda action.")
  12939. (defun org-mark-entry-for-agenda-action ()
  12940. "Mark the current entry as target of an agenda action.
  12941. Agenda actions are actions executed from the agenda with the key `k',
  12942. which make use of the date at the cursor."
  12943. (interactive)
  12944. (move-marker org-agenda-action-marker
  12945. (save-excursion (org-back-to-heading t) (point))
  12946. (current-buffer))
  12947. (message
  12948. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12949. ;;; Paragraph filling stuff.
  12950. ;; We want this to be just right, so use the full arsenal.
  12951. (defun org-indent-line-function ()
  12952. "Indent line like previous, but further if previous was headline or item."
  12953. (interactive)
  12954. (let* ((pos (point))
  12955. (itemp (org-at-item-p))
  12956. column bpos bcol tpos tcol bullet btype bullet-type)
  12957. ;; Find the previous relevant line
  12958. (beginning-of-line 1)
  12959. (cond
  12960. ((looking-at "#") (setq column 0))
  12961. ((looking-at "\\*+ ") (setq column 0))
  12962. (t
  12963. (beginning-of-line 0)
  12964. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12965. (beginning-of-line 0))
  12966. (cond
  12967. ((looking-at "\\*+[ \t]+")
  12968. (if (not org-adapt-indentation)
  12969. (setq column 0)
  12970. (goto-char (match-end 0))
  12971. (setq column (current-column))))
  12972. ((org-in-item-p)
  12973. (org-beginning-of-item)
  12974. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12975. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12976. (setq bpos (match-beginning 1) tpos (match-end 0)
  12977. bcol (progn (goto-char bpos) (current-column))
  12978. tcol (progn (goto-char tpos) (current-column))
  12979. bullet (match-string 1)
  12980. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12981. (if (> tcol (+ bcol org-description-max-indent))
  12982. (setq tcol (+ bcol 5)))
  12983. (if (not itemp)
  12984. (setq column tcol)
  12985. (goto-char pos)
  12986. (beginning-of-line 1)
  12987. (if (looking-at "\\S-")
  12988. (progn
  12989. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12990. (setq bullet (match-string 1)
  12991. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12992. (setq column (if (equal btype bullet-type) bcol tcol)))
  12993. (setq column (org-get-indentation)))))
  12994. (t (setq column (org-get-indentation))))))
  12995. (goto-char pos)
  12996. (if (<= (current-column) (current-indentation))
  12997. (org-indent-line-to column)
  12998. (save-excursion (org-indent-line-to column)))
  12999. (setq column (current-column))
  13000. (beginning-of-line 1)
  13001. (if (looking-at
  13002. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13003. (replace-match (concat "\\1" (format org-property-format
  13004. (match-string 2) (match-string 3)))
  13005. t nil))
  13006. (org-move-to-column column)))
  13007. (defun org-set-autofill-regexps ()
  13008. (interactive)
  13009. ;; In the paragraph separator we include headlines, because filling
  13010. ;; text in a line directly attached to a headline would otherwise
  13011. ;; fill the headline as well.
  13012. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13013. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13014. ;; The paragraph starter includes hand-formatted lists.
  13015. (org-set-local 'paragraph-start
  13016. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13017. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13018. ;; But only if the user has not turned off tables or fixed-width regions
  13019. (org-set-local
  13020. 'auto-fill-inhibit-regexp
  13021. (concat "\\*+ \\|#\\+"
  13022. "\\|[ \t]*" org-keyword-time-regexp
  13023. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13024. (concat
  13025. "\\|[ \t]*["
  13026. (if org-enable-table-editor "|" "")
  13027. (if org-enable-fixed-width-editor ":" "")
  13028. "]"))))
  13029. ;; We use our own fill-paragraph function, to make sure that tables
  13030. ;; and fixed-width regions are not wrapped. That function will pass
  13031. ;; through to `fill-paragraph' when appropriate.
  13032. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13033. ; Adaptive filling: To get full control, first make sure that
  13034. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13035. (org-set-local 'adaptive-fill-regexp "\000")
  13036. (org-set-local 'adaptive-fill-function
  13037. 'org-adaptive-fill-function)
  13038. (org-set-local
  13039. 'align-mode-rules-list
  13040. '((org-in-buffer-settings
  13041. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13042. (modes . '(org-mode))))))
  13043. (defun org-fill-paragraph (&optional justify)
  13044. "Re-align a table, pass through to fill-paragraph if no table."
  13045. (let ((table-p (org-at-table-p))
  13046. (table.el-p (org-at-table.el-p)))
  13047. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13048. (save-excursion (goto-char (point-at-bol))
  13049. (looking-at outline-regexp)))
  13050. t) ; skip headlines
  13051. (table.el-p t) ; skip table.el tables
  13052. (table-p (org-table-align) t) ; align org-mode tables
  13053. (t nil)))) ; call paragraph-fill
  13054. ;; For reference, this is the default value of adaptive-fill-regexp
  13055. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13056. (defun org-adaptive-fill-function ()
  13057. "Return a fill prefix for org-mode files.
  13058. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13059. work correctly."
  13060. (cond ((looking-at "#[ \t]+")
  13061. (match-string 0))
  13062. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13063. (save-excursion
  13064. (if (> (match-end 1) (+ (match-beginning 1)
  13065. org-description-max-indent))
  13066. (goto-char (+ (match-beginning 1) 5))
  13067. (goto-char (match-end 0)))
  13068. (make-string (current-column) ?\ )))
  13069. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13070. (save-excursion
  13071. (goto-char (match-end 0))
  13072. (make-string (current-column) ?\ )))
  13073. (t nil)))
  13074. ;;; Other stuff.
  13075. (defun org-toggle-fixed-width-section (arg)
  13076. "Toggle the fixed-width export.
  13077. If there is no active region, the QUOTE keyword at the current headline is
  13078. inserted or removed. When present, it causes the text between this headline
  13079. and the next to be exported as fixed-width text, and unmodified.
  13080. If there is an active region, this command adds or removes a colon as the
  13081. first character of this line. If the first character of a line is a colon,
  13082. this line is also exported in fixed-width font."
  13083. (interactive "P")
  13084. (let* ((cc 0)
  13085. (regionp (org-region-active-p))
  13086. (beg (if regionp (region-beginning) (point)))
  13087. (end (if regionp (region-end)))
  13088. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13089. (case-fold-search nil)
  13090. (re "[ \t]*\\(:\\)")
  13091. off)
  13092. (if regionp
  13093. (save-excursion
  13094. (goto-char beg)
  13095. (setq cc (current-column))
  13096. (beginning-of-line 1)
  13097. (setq off (looking-at re))
  13098. (while (> nlines 0)
  13099. (setq nlines (1- nlines))
  13100. (beginning-of-line 1)
  13101. (cond
  13102. (arg
  13103. (org-move-to-column cc t)
  13104. (insert ":\n")
  13105. (forward-line -1))
  13106. ((and off (looking-at re))
  13107. (replace-match "" t t nil 1))
  13108. ((not off) (org-move-to-column cc t) (insert ":")))
  13109. (forward-line 1)))
  13110. (save-excursion
  13111. (org-back-to-heading)
  13112. (if (looking-at (concat outline-regexp
  13113. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13114. (replace-match "" t t nil 1)
  13115. (if (looking-at outline-regexp)
  13116. (progn
  13117. (goto-char (match-end 0))
  13118. (insert org-quote-string " "))))))))
  13119. ;;;; Functions extending outline functionality
  13120. (defun org-beginning-of-line (&optional arg)
  13121. "Go to the beginning of the current line. If that is invisible, continue
  13122. to a visible line beginning. This makes the function of C-a more intuitive.
  13123. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13124. first attempt, and only move to after the tags when the cursor is already
  13125. beyond the end of the headline."
  13126. (interactive "P")
  13127. (let ((pos (point)) refpos)
  13128. (beginning-of-line 1)
  13129. (if (bobp)
  13130. nil
  13131. (backward-char 1)
  13132. (if (org-invisible-p)
  13133. (while (and (not (bobp)) (org-invisible-p))
  13134. (backward-char 1)
  13135. (beginning-of-line 1))
  13136. (forward-char 1)))
  13137. (when org-special-ctrl-a/e
  13138. (cond
  13139. ((and (looking-at org-complex-heading-regexp)
  13140. (= (char-after (match-end 1)) ?\ ))
  13141. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13142. (point-at-eol)))
  13143. (goto-char
  13144. (if (eq org-special-ctrl-a/e t)
  13145. (cond ((> pos refpos) refpos)
  13146. ((= pos (point)) refpos)
  13147. (t (point)))
  13148. (cond ((> pos (point)) (point))
  13149. ((not (eq last-command this-command)) (point))
  13150. (t refpos)))))
  13151. ((org-at-item-p)
  13152. (goto-char
  13153. (if (eq org-special-ctrl-a/e t)
  13154. (cond ((> pos (match-end 4)) (match-end 4))
  13155. ((= pos (point)) (match-end 4))
  13156. (t (point)))
  13157. (cond ((> pos (point)) (point))
  13158. ((not (eq last-command this-command)) (point))
  13159. (t (match-end 4))))))))
  13160. (org-no-warnings
  13161. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13162. (defun org-end-of-line (&optional arg)
  13163. "Go to the end of the line.
  13164. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13165. first attempt, and only move to after the tags when the cursor is already
  13166. beyond the end of the headline."
  13167. (interactive "P")
  13168. (if (or (not org-special-ctrl-a/e)
  13169. (not (org-on-heading-p)))
  13170. (end-of-line arg)
  13171. (let ((pos (point)))
  13172. (beginning-of-line 1)
  13173. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13174. (if (eq org-special-ctrl-a/e t)
  13175. (if (or (< pos (match-beginning 1))
  13176. (= pos (match-end 0)))
  13177. (goto-char (match-beginning 1))
  13178. (goto-char (match-end 0)))
  13179. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13180. (goto-char (match-end 0))
  13181. (goto-char (match-beginning 1))))
  13182. (end-of-line arg))))
  13183. (org-no-warnings
  13184. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13185. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13186. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13187. (defun org-kill-line (&optional arg)
  13188. "Kill line, to tags or end of line."
  13189. (interactive "P")
  13190. (cond
  13191. ((or (not org-special-ctrl-k)
  13192. (bolp)
  13193. (not (org-on-heading-p)))
  13194. (call-interactively 'kill-line))
  13195. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13196. (kill-region (point) (match-beginning 1))
  13197. (org-set-tags nil t))
  13198. (t (kill-region (point) (point-at-eol)))))
  13199. (define-key org-mode-map "\C-k" 'org-kill-line)
  13200. (defun org-yank (&optional arg)
  13201. (interactive "*P")
  13202. (let ((fold (get-text-property 0 :org-folded-kill
  13203. (current-kill (cond ((listp arg) 0)
  13204. ((eq arg '-) -2)
  13205. (t (1- arg))))))
  13206. (fold t)
  13207. (yank-excluded-properties
  13208. (cons :org-folded-kill yank-excluded-properties))
  13209. (pos (point)) p1)
  13210. (call-interactively 'yank)
  13211. (setq p1 (point))
  13212. (goto-char pos)
  13213. (when (and (bolp) (looking-at outline-regexp) fold)
  13214. (save-restriction
  13215. (narrow-to-region pos p1)
  13216. (hide-subtree)
  13217. (org-cycle-show-empty-lines 'folded))
  13218. (goto-char p1))))
  13219. (defun org-invisible-p ()
  13220. "Check if point is at a character currently not visible."
  13221. ;; Early versions of noutline don't have `outline-invisible-p'.
  13222. (if (fboundp 'outline-invisible-p)
  13223. (outline-invisible-p)
  13224. (get-char-property (point) 'invisible)))
  13225. (defun org-invisible-p2 ()
  13226. "Check if point is at a character currently not visible."
  13227. (save-excursion
  13228. (if (and (eolp) (not (bobp))) (backward-char 1))
  13229. ;; Early versions of noutline don't have `outline-invisible-p'.
  13230. (if (fboundp 'outline-invisible-p)
  13231. (outline-invisible-p)
  13232. (get-char-property (point) 'invisible))))
  13233. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13234. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13235. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13236. (defun org-at-heading-or-item-p ()
  13237. (or (org-on-heading-p) (org-at-item-p)))
  13238. (defun org-on-target-p ()
  13239. (or (org-in-regexp org-radio-target-regexp)
  13240. (org-in-regexp org-target-regexp)))
  13241. (defun org-up-heading-all (arg)
  13242. "Move to the heading line of which the present line is a subheading.
  13243. This function considers both visible and invisible heading lines.
  13244. With argument, move up ARG levels."
  13245. (if (fboundp 'outline-up-heading-all)
  13246. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13247. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13248. (defun org-up-heading-safe ()
  13249. "Move to the heading line of which the present line is a subheading.
  13250. This version will not throw an error. It will return the level of the
  13251. headline found, or nil if no higher level is found."
  13252. (let ((pos (point)) start-level level
  13253. (re (concat "^" outline-regexp)))
  13254. (catch 'exit
  13255. (outline-back-to-heading t)
  13256. (setq start-level (funcall outline-level))
  13257. (if (equal start-level 1) (throw 'exit nil))
  13258. (while (re-search-backward re nil t)
  13259. (setq level (funcall outline-level))
  13260. (if (< level start-level) (throw 'exit level)))
  13261. nil)))
  13262. (defun org-first-sibling-p ()
  13263. "Is this heading the first child of its parents?"
  13264. (interactive)
  13265. (let ((re (concat "^" outline-regexp))
  13266. level l)
  13267. (unless (org-at-heading-p t)
  13268. (error "Not at a heading"))
  13269. (setq level (funcall outline-level))
  13270. (save-excursion
  13271. (if (not (re-search-backward re nil t))
  13272. t
  13273. (setq l (funcall outline-level))
  13274. (< l level)))))
  13275. (defun org-goto-sibling (&optional previous)
  13276. "Goto the next sibling, even if it is invisible.
  13277. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13278. when a sibling was found. When none is found, return nil and don't
  13279. move point."
  13280. (let ((fun (if previous 're-search-backward 're-search-forward))
  13281. (pos (point))
  13282. (re (concat "^" outline-regexp))
  13283. level l)
  13284. (when (condition-case nil (org-back-to-heading t) (error nil))
  13285. (setq level (funcall outline-level))
  13286. (catch 'exit
  13287. (or previous (forward-char 1))
  13288. (while (funcall fun re nil t)
  13289. (setq l (funcall outline-level))
  13290. (when (< l level) (goto-char pos) (throw 'exit nil))
  13291. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13292. (goto-char pos)
  13293. nil))))
  13294. (defun org-show-siblings ()
  13295. "Show all siblings of the current headline."
  13296. (save-excursion
  13297. (while (org-goto-sibling) (org-flag-heading nil)))
  13298. (save-excursion
  13299. (while (org-goto-sibling 'previous)
  13300. (org-flag-heading nil))))
  13301. (defun org-show-hidden-entry ()
  13302. "Show an entry where even the heading is hidden."
  13303. (save-excursion
  13304. (org-show-entry)))
  13305. (defun org-flag-heading (flag &optional entry)
  13306. "Flag the current heading. FLAG non-nil means make invisible.
  13307. When ENTRY is non-nil, show the entire entry."
  13308. (save-excursion
  13309. (org-back-to-heading t)
  13310. ;; Check if we should show the entire entry
  13311. (if entry
  13312. (progn
  13313. (org-show-entry)
  13314. (save-excursion
  13315. (and (outline-next-heading)
  13316. (org-flag-heading nil))))
  13317. (outline-flag-region (max (point-min) (1- (point)))
  13318. (save-excursion (outline-end-of-heading) (point))
  13319. flag))))
  13320. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13321. ;; This is an exact copy of the original function, but it uses
  13322. ;; `org-back-to-heading', to make it work also in invisible
  13323. ;; trees. And is uses an invisible-OK argument.
  13324. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13325. (org-back-to-heading invisible-OK)
  13326. (let ((first t)
  13327. (level (funcall outline-level)))
  13328. (while (and (not (eobp))
  13329. (or first (> (funcall outline-level) level)))
  13330. (setq first nil)
  13331. (outline-next-heading))
  13332. (unless to-heading
  13333. (if (memq (preceding-char) '(?\n ?\^M))
  13334. (progn
  13335. ;; Go to end of line before heading
  13336. (forward-char -1)
  13337. (if (memq (preceding-char) '(?\n ?\^M))
  13338. ;; leave blank line before heading
  13339. (forward-char -1))))))
  13340. (point))
  13341. (defun org-show-subtree ()
  13342. "Show everything after this heading at deeper levels."
  13343. (outline-flag-region
  13344. (point)
  13345. (save-excursion
  13346. (outline-end-of-subtree) (outline-next-heading) (point))
  13347. nil))
  13348. (defun org-show-entry ()
  13349. "Show the body directly following this heading.
  13350. Show the heading too, if it is currently invisible."
  13351. (interactive)
  13352. (save-excursion
  13353. (condition-case nil
  13354. (progn
  13355. (org-back-to-heading t)
  13356. (outline-flag-region
  13357. (max (point-min) (1- (point)))
  13358. (save-excursion
  13359. (re-search-forward
  13360. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13361. (or (match-beginning 1) (point-max)))
  13362. nil))
  13363. (error nil))))
  13364. (defun org-make-options-regexp (kwds)
  13365. "Make a regular expression for keyword lines."
  13366. (concat
  13367. "^"
  13368. "#?[ \t]*\\+\\("
  13369. (mapconcat 'regexp-quote kwds "\\|")
  13370. "\\):[ \t]*"
  13371. "\\(.+\\)"))
  13372. ;; Make isearch reveal the necessary context
  13373. (defun org-isearch-end ()
  13374. "Reveal context after isearch exits."
  13375. (when isearch-success ; only if search was successful
  13376. (if (featurep 'xemacs)
  13377. ;; Under XEmacs, the hook is run in the correct place,
  13378. ;; we directly show the context.
  13379. (org-show-context 'isearch)
  13380. ;; In Emacs the hook runs *before* restoring the overlays.
  13381. ;; So we have to use a one-time post-command-hook to do this.
  13382. ;; (Emacs 22 has a special variable, see function `org-mode')
  13383. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13384. isearch-mode-end-hook-quit)
  13385. ;; Only when the isearch was not quitted.
  13386. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13387. 'append 'local)))))
  13388. (defun org-isearch-post-command ()
  13389. "Remove self from hook, and show context."
  13390. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13391. (org-show-context 'isearch))
  13392. ;;;; Integration with and fixes for other packages
  13393. ;;; Imenu support
  13394. (defvar org-imenu-markers nil
  13395. "All markers currently used by Imenu.")
  13396. (make-variable-buffer-local 'org-imenu-markers)
  13397. (defun org-imenu-new-marker (&optional pos)
  13398. "Return a new marker for use by Imenu, and remember the marker."
  13399. (let ((m (make-marker)))
  13400. (move-marker m (or pos (point)))
  13401. (push m org-imenu-markers)
  13402. m))
  13403. (defun org-imenu-get-tree ()
  13404. "Produce the index for Imenu."
  13405. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13406. (setq org-imenu-markers nil)
  13407. (let* ((n org-imenu-depth)
  13408. (re (concat "^" outline-regexp))
  13409. (subs (make-vector (1+ n) nil))
  13410. (last-level 0)
  13411. m tree level head)
  13412. (save-excursion
  13413. (save-restriction
  13414. (widen)
  13415. (goto-char (point-max))
  13416. (while (re-search-backward re nil t)
  13417. (setq level (org-reduced-level (funcall outline-level)))
  13418. (when (<= level n)
  13419. (looking-at org-complex-heading-regexp)
  13420. (setq head (org-match-string-no-properties 4)
  13421. m (org-imenu-new-marker))
  13422. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13423. (if (>= level last-level)
  13424. (push (cons head m) (aref subs level))
  13425. (push (cons head (aref subs (1+ level))) (aref subs level))
  13426. (loop for i from (1+ level) to n do (aset subs i nil)))
  13427. (setq last-level level)))))
  13428. (aref subs 1)))
  13429. (eval-after-load "imenu"
  13430. '(progn
  13431. (add-hook 'imenu-after-jump-hook
  13432. (lambda ()
  13433. (if (eq major-mode 'org-mode)
  13434. (org-show-context 'org-goto))))))
  13435. ;; Speedbar support
  13436. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13437. "Overlay marking the agenda restriction line in speedbar.")
  13438. (org-overlay-put org-speedbar-restriction-lock-overlay
  13439. 'face 'org-agenda-restriction-lock)
  13440. (org-overlay-put org-speedbar-restriction-lock-overlay
  13441. 'help-echo "Agendas are currently limited to this item.")
  13442. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13443. (defun org-speedbar-set-agenda-restriction ()
  13444. "Restrict future agenda commands to the location at point in speedbar.
  13445. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13446. (interactive)
  13447. (require 'org-agenda)
  13448. (let (p m tp np dir txt w)
  13449. (cond
  13450. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13451. 'org-imenu t))
  13452. (setq m (get-text-property p 'org-imenu-marker))
  13453. (save-excursion
  13454. (save-restriction
  13455. (set-buffer (marker-buffer m))
  13456. (goto-char m)
  13457. (org-agenda-set-restriction-lock 'subtree))))
  13458. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13459. 'speedbar-function 'speedbar-find-file))
  13460. (setq tp (previous-single-property-change
  13461. (1+ p) 'speedbar-function)
  13462. np (next-single-property-change
  13463. tp 'speedbar-function)
  13464. dir (speedbar-line-directory)
  13465. txt (buffer-substring-no-properties (or tp (point-min))
  13466. (or np (point-max))))
  13467. (save-excursion
  13468. (save-restriction
  13469. (set-buffer (find-file-noselect
  13470. (let ((default-directory dir))
  13471. (expand-file-name txt))))
  13472. (unless (org-mode-p)
  13473. (error "Cannot restrict to non-Org-mode file"))
  13474. (org-agenda-set-restriction-lock 'file))))
  13475. (t (error "Don't know how to restrict Org-mode's agenda")))
  13476. (org-move-overlay org-speedbar-restriction-lock-overlay
  13477. (point-at-bol) (point-at-eol))
  13478. (setq current-prefix-arg nil)
  13479. (org-agenda-maybe-redo)))
  13480. (eval-after-load "speedbar"
  13481. '(progn
  13482. (speedbar-add-supported-extension ".org")
  13483. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13484. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13485. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13486. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13487. (add-hook 'speedbar-visiting-tag-hook
  13488. (lambda () (org-show-context 'org-goto)))))
  13489. ;;; Fixes and Hacks for problems with other packages
  13490. ;; Make flyspell not check words in links, to not mess up our keymap
  13491. (defun org-mode-flyspell-verify ()
  13492. "Don't let flyspell put overlays at active buttons."
  13493. (not (get-text-property (point) 'keymap)))
  13494. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13495. (eval-after-load "bookmark"
  13496. '(if (boundp 'bookmark-after-jump-hook)
  13497. ;; We can use the hook
  13498. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13499. ;; Hook not available, use advice
  13500. (defadvice bookmark-jump (after org-make-visible activate)
  13501. "Make the position visible."
  13502. (org-bookmark-jump-unhide))))
  13503. (defun org-bookmark-jump-unhide ()
  13504. "Unhide the current position, to show the bookmark location."
  13505. (and (org-mode-p)
  13506. (or (org-invisible-p)
  13507. (save-excursion (goto-char (max (point-min) (1- (point))))
  13508. (org-invisible-p)))
  13509. (org-show-context 'bookmark-jump)))
  13510. ;; Make session.el ignore our circular variable
  13511. (eval-after-load "session"
  13512. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13513. ;;;; Experimental code
  13514. (defun org-closed-in-range ()
  13515. "Sparse tree of items closed in a certain time range.
  13516. Still experimental, may disappear in the future."
  13517. (interactive)
  13518. ;; Get the time interval from the user.
  13519. (let* ((time1 (time-to-seconds
  13520. (org-read-date nil 'to-time nil "Starting date: ")))
  13521. (time2 (time-to-seconds
  13522. (org-read-date nil 'to-time nil "End date:")))
  13523. ;; callback function
  13524. (callback (lambda ()
  13525. (let ((time
  13526. (time-to-seconds
  13527. (apply 'encode-time
  13528. (org-parse-time-string
  13529. (match-string 1))))))
  13530. ;; check if time in interval
  13531. (and (>= time time1) (<= time time2))))))
  13532. ;; make tree, check each match with the callback
  13533. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13534. ;;;; Finish up
  13535. (provide 'org)
  13536. (run-hooks 'org-load-hook)
  13537. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13538. ;;; org.el ends here