org.el 635 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035
  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, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.29trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.29trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let* ((org-version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" ))
  98. (version))
  99. (if (file-exists-p (expand-file-name ".git" dir))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (shell-command "git describe --abbrev=4 HEAD")
  103. (save-excursion
  104. (set-buffer "*Shell Command Output*")
  105. (goto-char (point-min))
  106. (re-search-forward "[^\n]+")
  107. (setq git-version (match-string 0))
  108. (subst-char-in-string ?- ?. git-version t)
  109. (shell-command "git diff-index --name-only HEAD --")
  110. (unless (eql 1 (point-max))
  111. (setq git-version (concat git-version ".dirty")))
  112. (setq org-version (concat org-version " (" git-version ")")))
  113. (cd pwd)))
  114. (setq version (format "Org-mode version %s" org-version))
  115. (if here (insert version))
  116. (message version)
  117. version))
  118. ;;; Compatibility constants
  119. ;;; The custom variables
  120. (defgroup org nil
  121. "Outline-based notes management and organizer."
  122. :tag "Org"
  123. :group 'outlines
  124. :group 'hypermedia
  125. :group 'calendar)
  126. (defcustom org-load-hook nil
  127. "Hook that is run after org.el has been loaded."
  128. :group 'org
  129. :type 'hook)
  130. (defvar org-modules) ; defined below
  131. (defvar org-modules-loaded nil
  132. "Have the modules been loaded already?")
  133. (defun org-load-modules-maybe (&optional force)
  134. "Load all extensions listed in `org-modules'."
  135. (when (or force (not org-modules-loaded))
  136. (mapc (lambda (ext)
  137. (condition-case nil (require ext)
  138. (error (message "Problems while trying to load feature `%s'" ext))))
  139. org-modules)
  140. (setq org-modules-loaded t)))
  141. (defun org-set-modules (var value)
  142. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  143. (set var value)
  144. (when (featurep 'org)
  145. (org-load-modules-maybe 'force)))
  146. (when (org-bound-and-true-p org-modules)
  147. (let ((a (member 'org-infojs org-modules)))
  148. (and a (setcar a 'org-jsinfo))))
  149. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  150. "Modules that should always be loaded together with org.el.
  151. If a description starts with <C>, the file is not part of Emacs
  152. and loading it will require that you have downloaded and properly installed
  153. the org-mode distribution.
  154. You can also use this system to load external packages (i.e. neither Org
  155. core modules, not modules from the CONTRIB directory). Just add symbols
  156. to the end of the list. If the package is called org-xyz.el, then you need
  157. to add the symbol `xyz', and the package must have a call to
  158. (provide 'org-xyz)"
  159. :group 'org
  160. :set 'org-set-modules
  161. :type
  162. '(set :greedy t
  163. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  164. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  165. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  166. (const :tag " id: Global IDs for identifying entries" org-id)
  167. (const :tag " info: Links to Info nodes" org-info)
  168. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  169. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  170. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  171. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  172. (const :tag " mew Links to Mew folders/messages" org-mew)
  173. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  174. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  175. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  176. (const :tag " vm: Links to VM folders/messages" org-vm)
  177. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  178. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  179. (const :tag " mouse: Additional mouse support" org-mouse)
  180. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  181. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  182. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  183. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  184. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  185. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  186. (const :tag "C collector: Collect properties into tables" org-collector)
  187. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  188. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  189. (const :tag "C eval: Include command output as text" org-eval)
  190. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  191. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  192. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  193. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  194. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  195. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  196. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  197. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  198. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  199. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  200. (const :tag "C R: Computation using the R language" org-R)
  201. (const :tag "C registry: A registry for Org links" org-registry)
  202. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  203. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  204. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  205. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  206. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  207. (const :tag "C track: Keep up with Org development" org-track)
  208. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  209. (defcustom org-support-shift-select nil
  210. "Non-nil means, make shift-cursor commands select text when possible.
  211. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  212. selecting a region, or enlarge thusly regions started in this way.
  213. In Org-mode, in special contexts, these same keys are used for other
  214. purposes, important enough to compete with shift selection. Org tries
  215. to balance these needs by supporting `shift-select-mode' outside these
  216. special contexts, under control of this variable.
  217. The default of this variable is nil, to avoid confusing behavior. Shifted
  218. cursor keys will then execute Org commands in the following contexts:
  219. - on a headline, changing TODO state (left/right) and priority (up/down)
  220. - on a time stamp, changing the time
  221. - in a plain list item, changing the bullet type
  222. - in a property definition line, switching between allowed values
  223. - in the BEGIN line of a clock table (changing the time block).
  224. Outside these contexts, the commands will throw an error.
  225. When this variable is t and the cursor is not in a special context,
  226. Org-mode will support shift-selection for making and enlarging regions.
  227. To make this more effective, the bullet cycling will no longer happen
  228. anywhere in an item line, but only if the cursor is exactly on the bullet.
  229. If you set this variable to the symbol `always', then the keys
  230. will not be special in headlines, property lines, and item lines, to make
  231. shift selection work there as well. If this is what you want, you can
  232. use the following alternative commands: `C-c C-t' and `C-c ,' to
  233. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  234. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  235. edited by hand or in column view.
  236. However, when the cursor is on a timestamp, shift-cursor commands
  237. will still edit the time stamp - this is just too good to give up.
  238. XEmacs user should have this variable set to nil, because shift-select-mode
  239. is Emacs 23 only."
  240. :group 'org
  241. :type '(choice
  242. (const :tag "Never" nil)
  243. (const :tag "When outside special context" t)
  244. (const :tag "Everywhere except timestamps" always)))
  245. (defgroup org-startup nil
  246. "Options concerning startup of Org-mode."
  247. :tag "Org Startup"
  248. :group 'org)
  249. (defcustom org-startup-folded t
  250. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  251. This can also be configured on a per-file basis by adding one of
  252. the following lines anywhere in the buffer:
  253. #+STARTUP: fold
  254. #+STARTUP: nofold
  255. #+STARTUP: content"
  256. :group 'org-startup
  257. :type '(choice
  258. (const :tag "nofold: show all" nil)
  259. (const :tag "fold: overview" t)
  260. (const :tag "content: all headlines" content)))
  261. (defcustom org-startup-truncated t
  262. "Non-nil means, entering Org-mode will set `truncate-lines'.
  263. This is useful since some lines containing links can be very long and
  264. uninteresting. Also tables look terrible when wrapped."
  265. :group 'org-startup
  266. :type 'boolean)
  267. (defcustom org-startup-indented nil
  268. "Non-nil means, turn on `org-indent-mode' on startup.
  269. This can also be configured on a per-file basis by adding one of
  270. the following lines anywhere in the buffer:
  271. #+STARTUP: indent
  272. #+STARTUP: noindent"
  273. :group 'org-structure
  274. :type '(choice
  275. (const :tag "Not" nil)
  276. (const :tag "Globally (slow on startup in large files)" t)))
  277. (defcustom org-startup-align-all-tables nil
  278. "Non-nil means, align all tables when visiting a file.
  279. This is useful when the column width in tables is forced with <N> cookies
  280. in table fields. Such tables will look correct only after the first re-align.
  281. This can also be configured on a per-file basis by adding one of
  282. the following lines anywhere in the buffer:
  283. #+STARTUP: align
  284. #+STARTUP: noalign"
  285. :group 'org-startup
  286. :type 'boolean)
  287. (defcustom org-insert-mode-line-in-empty-file nil
  288. "Non-nil means insert the first line setting Org-mode in empty files.
  289. When the function `org-mode' is called interactively in an empty file, this
  290. normally means that the file name does not automatically trigger Org-mode.
  291. To ensure that the file will always be in Org-mode in the future, a
  292. line enforcing Org-mode will be inserted into the buffer, if this option
  293. has been set."
  294. :group 'org-startup
  295. :type 'boolean)
  296. (defcustom org-replace-disputed-keys nil
  297. "Non-nil means use alternative key bindings for some keys.
  298. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  299. These keys are also used by other packages like shift-selection-mode'
  300. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  301. If you want to use Org-mode together with one of these other modes,
  302. or more generally if you would like to move some Org-mode commands to
  303. other keys, set this variable and configure the keys with the variable
  304. `org-disputed-keys'.
  305. This option is only relevant at load-time of Org-mode, and must be set
  306. *before* org.el is loaded. Changing it requires a restart of Emacs to
  307. become effective."
  308. :group 'org-startup
  309. :type 'boolean)
  310. (defcustom org-use-extra-keys nil
  311. "Non-nil means use extra key sequence definitions for certain
  312. commands. This happens automatically if you run XEmacs or if
  313. window-system is nil. This variable lets you do the same
  314. manually. You must set it before loading org.
  315. Example: on Carbon Emacs 22 running graphically, with an external
  316. keyboard on a Powerbook, the default way of setting M-left might
  317. not work for either Alt or ESC. Setting this variable will make
  318. it work for ESC."
  319. :group 'org-startup
  320. :type 'boolean)
  321. (if (fboundp 'defvaralias)
  322. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  323. (defcustom org-disputed-keys
  324. '(([(shift up)] . [(meta p)])
  325. ([(shift down)] . [(meta n)])
  326. ([(shift left)] . [(meta -)])
  327. ([(shift right)] . [(meta +)])
  328. ([(control shift right)] . [(meta shift +)])
  329. ([(control shift left)] . [(meta shift -)]))
  330. "Keys for which Org-mode and other modes compete.
  331. This is an alist, cars are the default keys, second element specifies
  332. the alternative to use when `org-replace-disputed-keys' is t.
  333. Keys can be specified in any syntax supported by `define-key'.
  334. The value of this option takes effect only at Org-mode's startup,
  335. therefore you'll have to restart Emacs to apply it after changing."
  336. :group 'org-startup
  337. :type 'alist)
  338. (defun org-key (key)
  339. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  340. Or return the original if not disputed."
  341. (if org-replace-disputed-keys
  342. (let* ((nkey (key-description key))
  343. (x (org-find-if (lambda (x)
  344. (equal (key-description (car x)) nkey))
  345. org-disputed-keys)))
  346. (if x (cdr x) key))
  347. key))
  348. (defun org-find-if (predicate seq)
  349. (catch 'exit
  350. (while seq
  351. (if (funcall predicate (car seq))
  352. (throw 'exit (car seq))
  353. (pop seq)))))
  354. (defun org-defkey (keymap key def)
  355. "Define a key, possibly translated, as returned by `org-key'."
  356. (define-key keymap (org-key key) def))
  357. (defcustom org-ellipsis nil
  358. "The ellipsis to use in the Org-mode outline.
  359. When nil, just use the standard three dots. When a string, use that instead,
  360. When a face, use the standard 3 dots, but with the specified face.
  361. The change affects only Org-mode (which will then use its own display table).
  362. Changing this requires executing `M-x org-mode' in a buffer to become
  363. effective."
  364. :group 'org-startup
  365. :type '(choice (const :tag "Default" nil)
  366. (face :tag "Face" :value org-warning)
  367. (string :tag "String" :value "...#")))
  368. (defvar org-display-table nil
  369. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  370. (defgroup org-keywords nil
  371. "Keywords in Org-mode."
  372. :tag "Org Keywords"
  373. :group 'org)
  374. (defcustom org-deadline-string "DEADLINE:"
  375. "String to mark deadline entries.
  376. A deadline is this string, followed by a time stamp. Should be a word,
  377. terminated by a colon. You can insert a schedule keyword and
  378. a timestamp with \\[org-deadline].
  379. Changes become only effective after restarting Emacs."
  380. :group 'org-keywords
  381. :type 'string)
  382. (defcustom org-scheduled-string "SCHEDULED:"
  383. "String to mark scheduled TODO entries.
  384. A schedule is this string, followed by a time stamp. Should be a word,
  385. terminated by a colon. You can insert a schedule keyword and
  386. a timestamp with \\[org-schedule].
  387. Changes become only effective after restarting Emacs."
  388. :group 'org-keywords
  389. :type 'string)
  390. (defcustom org-closed-string "CLOSED:"
  391. "String used as the prefix for timestamps logging closing a TODO entry."
  392. :group 'org-keywords
  393. :type 'string)
  394. (defcustom org-clock-string "CLOCK:"
  395. "String used as prefix for timestamps clocking work hours on an item."
  396. :group 'org-keywords
  397. :type 'string)
  398. (defcustom org-comment-string "COMMENT"
  399. "Entries starting with this keyword will never be exported.
  400. An entry can be toggled between COMMENT and normal with
  401. \\[org-toggle-comment].
  402. Changes become only effective after restarting Emacs."
  403. :group 'org-keywords
  404. :type 'string)
  405. (defcustom org-quote-string "QUOTE"
  406. "Entries starting with this keyword will be exported in fixed-width font.
  407. Quoting applies only to the text in the entry following the headline, and does
  408. not extend beyond the next headline, even if that is lower level.
  409. An entry can be toggled between QUOTE and normal with
  410. \\[org-toggle-fixed-width-section]."
  411. :group 'org-keywords
  412. :type 'string)
  413. (defconst org-repeat-re
  414. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  415. "Regular expression for specifying repeated events.
  416. After a match, group 1 contains the repeat expression.")
  417. (defgroup org-structure nil
  418. "Options concerning the general structure of Org-mode files."
  419. :tag "Org Structure"
  420. :group 'org)
  421. (defgroup org-reveal-location nil
  422. "Options about how to make context of a location visible."
  423. :tag "Org Reveal Location"
  424. :group 'org-structure)
  425. (defconst org-context-choice
  426. '(choice
  427. (const :tag "Always" t)
  428. (const :tag "Never" nil)
  429. (repeat :greedy t :tag "Individual contexts"
  430. (cons
  431. (choice :tag "Context"
  432. (const agenda)
  433. (const org-goto)
  434. (const occur-tree)
  435. (const tags-tree)
  436. (const link-search)
  437. (const mark-goto)
  438. (const bookmark-jump)
  439. (const isearch)
  440. (const default))
  441. (boolean))))
  442. "Contexts for the reveal options.")
  443. (defcustom org-show-hierarchy-above '((default . t))
  444. "Non-nil means, show full hierarchy when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the hierarchy of headings
  447. above the exposed location is shown.
  448. Turning this off for example for sparse trees makes them very compact.
  449. Instead of t, this can also be an alist specifying this option for different
  450. contexts. Valid contexts are
  451. agenda when exposing an entry from the agenda
  452. org-goto when using the command `org-goto' on key C-c C-j
  453. occur-tree when using the command `org-occur' on key C-c /
  454. tags-tree when constructing a sparse tree based on tags matches
  455. link-search when exposing search matches associated with a link
  456. mark-goto when exposing the jump goal of a mark
  457. bookmark-jump when exposing a bookmark location
  458. isearch when exiting from an incremental search
  459. default default for all contexts not set explicitly"
  460. :group 'org-reveal-location
  461. :type org-context-choice)
  462. (defcustom org-show-following-heading '((default . nil))
  463. "Non-nil means, show following heading when revealing a location.
  464. Org-mode often shows locations in an org-mode file which might have
  465. been invisible before. When this is set, the heading following the
  466. match is shown.
  467. Turning this off for example for sparse trees makes them very compact,
  468. but makes it harder to edit the location of the match. In such a case,
  469. use the command \\[org-reveal] to show more context.
  470. Instead of t, this can also be an alist specifying this option for different
  471. contexts. See `org-show-hierarchy-above' for valid contexts."
  472. :group 'org-reveal-location
  473. :type org-context-choice)
  474. (defcustom org-show-siblings '((default . nil) (isearch t))
  475. "Non-nil means, show all sibling heading when revealing a location.
  476. Org-mode often shows locations in an org-mode file which might have
  477. been invisible before. When this is set, the sibling of the current entry
  478. heading are all made visible. If `org-show-hierarchy-above' is t,
  479. the same happens on each level of the hierarchy above the current entry.
  480. By default this is on for the isearch context, off for all other contexts.
  481. Turning this off for example for sparse trees makes them very compact,
  482. but makes it harder to edit the location of the match. In such a case,
  483. use the command \\[org-reveal] to show more context.
  484. Instead of t, this can also be an alist specifying this option for different
  485. contexts. See `org-show-hierarchy-above' for valid contexts."
  486. :group 'org-reveal-location
  487. :type org-context-choice)
  488. (defcustom org-show-entry-below '((default . nil))
  489. "Non-nil means, show the entry below a headline when revealing a location.
  490. Org-mode often shows locations in an org-mode file which might have
  491. been invisible before. When this is set, the text below the headline that is
  492. exposed is also shown.
  493. By default this is off for all contexts.
  494. Instead of t, this can also be an alist specifying this option for different
  495. contexts. See `org-show-hierarchy-above' for valid contexts."
  496. :group 'org-reveal-location
  497. :type org-context-choice)
  498. (defcustom org-indirect-buffer-display 'other-window
  499. "How should indirect tree buffers be displayed?
  500. This applies to indirect buffers created with the commands
  501. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  502. Valid values are:
  503. current-window Display in the current window
  504. other-window Just display in another window.
  505. dedicated-frame Create one new frame, and re-use it each time.
  506. new-frame Make a new frame each time. Note that in this case
  507. previously-made indirect buffers are kept, and you need to
  508. kill these buffers yourself."
  509. :group 'org-structure
  510. :group 'org-agenda-windows
  511. :type '(choice
  512. (const :tag "In current window" current-window)
  513. (const :tag "In current frame, other window" other-window)
  514. (const :tag "Each time a new frame" new-frame)
  515. (const :tag "One dedicated frame" dedicated-frame)))
  516. (defgroup org-cycle nil
  517. "Options concerning visibility cycling in Org-mode."
  518. :tag "Org Cycle"
  519. :group 'org-structure)
  520. (defcustom org-cycle-skip-children-state-if-no-children t
  521. "Non-nil means, skip CHILDREN state in entries that don't have any."
  522. :group 'org-cycle
  523. :type 'boolean)
  524. (defcustom org-cycle-max-level nil
  525. "Maximum level which should still be subject to visibility cycling.
  526. Levels higher than this will, for cycling, be treated as text, not a headline.
  527. When `org-odd-levels-only' is set, a value of N in this variable actually
  528. means 2N-1 stars as the limiting headline.
  529. When nil, cycle all levels.
  530. Note that the limiting level of cycling is also influenced by
  531. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  532. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  533. than its value."
  534. :group 'org-cycle
  535. :type '(choice
  536. (const :tag "No limit" nil)
  537. (integer :tag "Maximum level")))
  538. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  539. "Names of drawers. Drawers are not opened by cycling on the headline above.
  540. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  541. this:
  542. :DRAWERNAME:
  543. .....
  544. :END:
  545. The drawer \"PROPERTIES\" is special for capturing properties through
  546. the property API.
  547. Drawers can be defined on the per-file basis with a line like:
  548. #+DRAWERS: HIDDEN STATE PROPERTIES"
  549. :group 'org-structure
  550. :group 'org-cycle
  551. :type '(repeat (string :tag "Drawer Name")))
  552. (defcustom org-hide-block-startup nil
  553. "Non-nil means, , entering Org-mode will fold all blocks.
  554. This can also be set in on a per-file basis with
  555. #+STARTUP: hideblocks
  556. #+STARTUP: showblocks"
  557. :group 'org-startup
  558. :group 'org-cycle
  559. :type 'boolean)
  560. (defcustom org-cycle-global-at-bob nil
  561. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  562. This makes it possible to do global cycling without having to use S-TAB or
  563. C-u TAB. For this special case to work, the first line of the buffer
  564. must not be a headline - it may be empty or some other text. When used in
  565. this way, `org-cycle-hook' is disables temporarily, to make sure the
  566. cursor stays at the beginning of the buffer.
  567. When this option is nil, don't do anything special at the beginning
  568. of the buffer."
  569. :group 'org-cycle
  570. :type 'boolean)
  571. (defcustom org-cycle-emulate-tab t
  572. "Where should `org-cycle' emulate TAB.
  573. nil Never
  574. white Only in completely white lines
  575. whitestart Only at the beginning of lines, before the first non-white char
  576. t Everywhere except in headlines
  577. exc-hl-bol Everywhere except at the start of a headline
  578. If TAB is used in a place where it does not emulate TAB, the current subtree
  579. visibility is cycled."
  580. :group 'org-cycle
  581. :type '(choice (const :tag "Never" nil)
  582. (const :tag "Only in completely white lines" white)
  583. (const :tag "Before first char in a line" whitestart)
  584. (const :tag "Everywhere except in headlines" t)
  585. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  586. ))
  587. (defcustom org-cycle-separator-lines 2
  588. "Number of empty lines needed to keep an empty line between collapsed trees.
  589. If you leave an empty line between the end of a subtree and the following
  590. headline, this empty line is hidden when the subtree is folded.
  591. Org-mode will leave (exactly) one empty line visible if the number of
  592. empty lines is equal or larger to the number given in this variable.
  593. So the default 2 means, at least 2 empty lines after the end of a subtree
  594. are needed to produce free space between a collapsed subtree and the
  595. following headline.
  596. Special case: when 0, never leave empty lines in collapsed view."
  597. :group 'org-cycle
  598. :type 'integer)
  599. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  600. (defcustom org-pre-cycle-hook nil
  601. "Hook that is run before visibility cycling is happening.
  602. The function(s) in this hook must accept a single argument which indicates
  603. the new state that will be set right after running this hook. The
  604. argument is a symbol. Before a global state change, it can have the values
  605. `overview', `content', or `all'. Before a local state change, it can have
  606. the values `folded', `children', or `subtree'."
  607. :group 'org-cycle
  608. :type 'hook)
  609. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  610. org-cycle-hide-drawers
  611. org-cycle-show-empty-lines
  612. org-optimize-window-after-visibility-change)
  613. "Hook that is run after `org-cycle' has changed the buffer visibility.
  614. The function(s) in this hook must accept a single argument which indicates
  615. the new state that was set by the most recent `org-cycle' command. The
  616. argument is a symbol. After a global state change, it can have the values
  617. `overview', `content', or `all'. After a local state change, it can have
  618. the values `folded', `children', or `subtree'."
  619. :group 'org-cycle
  620. :type 'hook)
  621. (defgroup org-edit-structure nil
  622. "Options concerning structure editing in Org-mode."
  623. :tag "Org Edit Structure"
  624. :group 'org-structure)
  625. (defcustom org-odd-levels-only nil
  626. "Non-nil means, skip even levels and only use odd levels for the outline.
  627. This has the effect that two stars are being added/taken away in
  628. promotion/demotion commands. It also influences how levels are
  629. handled by the exporters.
  630. Changing it requires restart of `font-lock-mode' to become effective
  631. for fontification also in regions already fontified.
  632. You may also set this on a per-file basis by adding one of the following
  633. lines to the buffer:
  634. #+STARTUP: odd
  635. #+STARTUP: oddeven"
  636. :group 'org-edit-structure
  637. :group 'org-font-lock
  638. :type 'boolean)
  639. (defcustom org-adapt-indentation t
  640. "Non-nil means, adapt indentation to outline node level.
  641. When this variable is set, Org assumes that you write outlines by
  642. indenting text in each node to align with the headline (after the stars).
  643. The following issues are influenced by this variable:
  644. - When this is set and the *entire* text in an entry is indented, the
  645. indentation is increased by one space in a demotion command, and
  646. decreased by one in a promotion command. If any line in the entry
  647. body starts with text at column 0, indentation is not changed at all.
  648. - Property drawers and planning information is inserted indented when
  649. this variable s set. When nil, they will not be indented.
  650. - TAB indents a line relative to context. The lines below a headline
  651. will be indented when this variable is set.
  652. Note that this is all about true indentation, by adding and removing
  653. space characters. See also `org-indent.el' which does level-dependent
  654. indentation in a virtual way, i.e. at display time in Emacs."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defcustom org-special-ctrl-a/e nil
  658. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  659. When t, `C-a' will bring back the cursor to the beginning of the
  660. headline text, i.e. after the stars and after a possible TODO keyword.
  661. In an item, this will be the position after the bullet.
  662. When the cursor is already at that position, another `C-a' will bring
  663. it to the beginning of the line.
  664. `C-e' will jump to the end of the headline, ignoring the presence of tags
  665. in the headline. A second `C-e' will then jump to the true end of the
  666. line, after any tags.
  667. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  668. going to the true line boundary first. Only a directly following, identical
  669. keypress will bring the cursor to the special positions.
  670. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  671. set separately."
  672. :group 'org-edit-structure
  673. :type '(choice
  674. (const :tag "off" nil)
  675. (const :tag "after stars/bullet and before tags first" t)
  676. (const :tag "true line boundary first" reversed)
  677. (cons :tag "Set C-a and C-e separately"
  678. (choice :tag "Special C-a"
  679. (const :tag "off" nil)
  680. (const :tag "after stars/bullet first" t)
  681. (const :tag "before stars/bullet first" reversed))
  682. (choice :tag "Special C-e"
  683. (const :tag "off" nil)
  684. (const :tag "before tags first" t)
  685. (const :tag "after tags first" reversed)))))
  686. (if (fboundp 'defvaralias)
  687. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  688. (defcustom org-special-ctrl-k nil
  689. "Non-nil means `C-k' will behave specially in headlines.
  690. When nil, `C-k' will call the default `kill-line' command.
  691. When t, the following will happen while the cursor is in the headline:
  692. - When the cursor is at the beginning of a headline, kill the entire
  693. line and possible the folded subtree below the line.
  694. - When in the middle of the headline text, kill the headline up to the tags.
  695. - When after the headline text, kill the tags."
  696. :group 'org-edit-structure
  697. :type 'boolean)
  698. (defcustom org-yank-folded-subtrees t
  699. "Non-nil means, when yanking subtrees, fold them.
  700. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  701. it starts with a heading and all other headings in it are either children
  702. or siblings, then fold all the subtrees. However, do this only if no
  703. text after the yank would be swallowed into a folded tree by this action."
  704. :group 'org-edit-structure
  705. :type 'boolean)
  706. (defcustom org-yank-adjusted-subtrees nil
  707. "Non-nil means, when yanking subtrees, adjust the level.
  708. With this setting, `org-paste-subtree' is used to insert the subtree, see
  709. this function for details."
  710. :group 'org-edit-structure
  711. :type 'boolean)
  712. (defcustom org-M-RET-may-split-line '((default . t))
  713. "Non-nil means, M-RET will split the line at the cursor position.
  714. When nil, it will go to the end of the line before making a
  715. new line.
  716. You may also set this option in a different way for different
  717. contexts. Valid contexts are:
  718. headline when creating a new headline
  719. item when creating a new item
  720. table in a table field
  721. default the value to be used for all contexts not explicitly
  722. customized"
  723. :group 'org-structure
  724. :group 'org-table
  725. :type '(choice
  726. (const :tag "Always" t)
  727. (const :tag "Never" nil)
  728. (repeat :greedy t :tag "Individual contexts"
  729. (cons
  730. (choice :tag "Context"
  731. (const headline)
  732. (const item)
  733. (const table)
  734. (const default))
  735. (boolean)))))
  736. (defcustom org-insert-heading-respect-content nil
  737. "Non-nil means, insert new headings after the current subtree.
  738. When nil, the new heading is created directly after the current line.
  739. The commands \\[org-insert-heading-respect-content] and
  740. \\[org-insert-todo-heading-respect-content] turn this variable on
  741. for the duration of the command."
  742. :group 'org-structure
  743. :type 'boolean)
  744. (defcustom org-blank-before-new-entry '((heading . auto)
  745. (plain-list-item . auto))
  746. "Should `org-insert-heading' leave a blank line before new heading/item?
  747. The value is an alist, with `heading' and `plain-list-item' as car,
  748. and a boolean flag as cdr. For plain lists, if the variable
  749. `org-empty-line-terminates-plain-lists' is set, the setting here
  750. is ignored and no empty line is inserted, to keep the list in tact."
  751. :group 'org-edit-structure
  752. :type '(list
  753. (cons (const heading)
  754. (choice (const :tag "Never" nil)
  755. (const :tag "Always" t)
  756. (const :tag "Auto" auto)))
  757. (cons (const plain-list-item)
  758. (choice (const :tag "Never" nil)
  759. (const :tag "Always" t)
  760. (const :tag "Auto" auto)))))
  761. (defcustom org-insert-heading-hook nil
  762. "Hook being run after inserting a new heading."
  763. :group 'org-edit-structure
  764. :type 'hook)
  765. (defcustom org-enable-fixed-width-editor t
  766. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  767. This currently only means, they are never auto-wrapped.
  768. When nil, such lines will be treated like ordinary lines.
  769. See also the QUOTE keyword."
  770. :group 'org-edit-structure
  771. :type 'boolean)
  772. (defcustom org-goto-auto-isearch t
  773. "Non-nil means, typing characters in org-goto starts incremental search."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defgroup org-sparse-trees nil
  777. "Options concerning sparse trees in Org-mode."
  778. :tag "Org Sparse Trees"
  779. :group 'org-structure)
  780. (defcustom org-highlight-sparse-tree-matches t
  781. "Non-nil means, highlight all matches that define a sparse tree.
  782. The highlights will automatically disappear the next time the buffer is
  783. changed by an edit command."
  784. :group 'org-sparse-trees
  785. :type 'boolean)
  786. (defcustom org-remove-highlights-with-change t
  787. "Non-nil means, any change to the buffer will remove temporary highlights.
  788. Such highlights are created by `org-occur' and `org-clock-display'.
  789. When nil, `C-c C-c needs to be used to get rid of the highlights.
  790. The highlights created by `org-preview-latex-fragment' always need
  791. `C-c C-c' to be removed."
  792. :group 'org-sparse-trees
  793. :group 'org-time
  794. :type 'boolean)
  795. (defcustom org-occur-hook '(org-first-headline-recenter)
  796. "Hook that is run after `org-occur' has constructed a sparse tree.
  797. This can be used to recenter the window to show as much of the structure
  798. as possible."
  799. :group 'org-sparse-trees
  800. :type 'hook)
  801. (defgroup org-imenu-and-speedbar nil
  802. "Options concerning imenu and speedbar in Org-mode."
  803. :tag "Org Imenu and Speedbar"
  804. :group 'org-structure)
  805. (defcustom org-imenu-depth 2
  806. "The maximum level for Imenu access to Org-mode headlines.
  807. This also applied for speedbar access."
  808. :group 'org-imenu-and-speedbar
  809. :type 'integer)
  810. (defgroup org-table nil
  811. "Options concerning tables in Org-mode."
  812. :tag "Org Table"
  813. :group 'org)
  814. (defcustom org-enable-table-editor 'optimized
  815. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  816. When nil, such lines will be treated like ordinary lines.
  817. When equal to the symbol `optimized', the table editor will be optimized to
  818. do the following:
  819. - Automatic overwrite mode in front of whitespace in table fields.
  820. This makes the structure of the table stay in tact as long as the edited
  821. field does not exceed the column width.
  822. - Minimize the number of realigns. Normally, the table is aligned each time
  823. TAB or RET are pressed to move to another field. With optimization this
  824. happens only if changes to a field might have changed the column width.
  825. Optimization requires replacing the functions `self-insert-command',
  826. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  827. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  828. very good at guessing when a re-align will be necessary, but you can always
  829. force one with \\[org-ctrl-c-ctrl-c].
  830. If you would like to use the optimized version in Org-mode, but the
  831. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  832. This variable can be used to turn on and off the table editor during a session,
  833. but in order to toggle optimization, a restart is required.
  834. See also the variable `org-table-auto-blank-field'."
  835. :group 'org-table
  836. :type '(choice
  837. (const :tag "off" nil)
  838. (const :tag "on" t)
  839. (const :tag "on, optimized" optimized)))
  840. (defcustom org-self-insert-cluster-for-undo t
  841. "Non-nil means cluster self-insert commands for undo when possible.
  842. If this is set, then, like in the Emacs command loop, 20 consequtive
  843. characters will be undone together.
  844. This is configurable, because there is some impact on typing performance."
  845. :group 'org-table
  846. :type 'boolean)
  847. (defcustom org-table-tab-recognizes-table.el t
  848. "Non-nil means, TAB will automatically notice a table.el table.
  849. When it sees such a table, it moves point into it and - if necessary -
  850. calls `table-recognize-table'."
  851. :group 'org-table-editing
  852. :type 'boolean)
  853. (defgroup org-link nil
  854. "Options concerning links in Org-mode."
  855. :tag "Org Link"
  856. :group 'org)
  857. (defvar org-link-abbrev-alist-local nil
  858. "Buffer-local version of `org-link-abbrev-alist', which see.
  859. The value of this is taken from the #+LINK lines.")
  860. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  861. (defcustom org-link-abbrev-alist nil
  862. "Alist of link abbreviations.
  863. The car of each element is a string, to be replaced at the start of a link.
  864. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  865. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  866. [[linkkey:tag][description]]
  867. The 'linkkey' must be a word word, starting with a letter, followed
  868. by letters, numbers, '-' or '_'.
  869. If REPLACE is a string, the tag will simply be appended to create the link.
  870. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  871. the placeholder \"%h\" will cause a url-encoded version of the tag to
  872. be inserted at that point (see the function `url-hexify-string').
  873. REPLACE may also be a function that will be called with the tag as the
  874. only argument to create the link, which should be returned as a string.
  875. See the manual for examples."
  876. :group 'org-link
  877. :type '(repeat
  878. (cons
  879. (string :tag "Protocol")
  880. (choice
  881. (string :tag "Format")
  882. (function)))))
  883. (defcustom org-descriptive-links t
  884. "Non-nil means, hide link part and only show description of bracket links.
  885. Bracket links are like [[link][description]]. This variable sets the initial
  886. state in new org-mode buffers. The setting can then be toggled on a
  887. per-buffer basis from the Org->Hyperlinks menu."
  888. :group 'org-link
  889. :type 'boolean)
  890. (defcustom org-link-file-path-type 'adaptive
  891. "How the path name in file links should be stored.
  892. Valid values are:
  893. relative Relative to the current directory, i.e. the directory of the file
  894. into which the link is being inserted.
  895. absolute Absolute path, if possible with ~ for home directory.
  896. noabbrev Absolute path, no abbreviation of home directory.
  897. adaptive Use relative path for files in the current directory and sub-
  898. directories of it. For other files, use an absolute path."
  899. :group 'org-link
  900. :type '(choice
  901. (const relative)
  902. (const absolute)
  903. (const noabbrev)
  904. (const adaptive)))
  905. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  906. "Types of links that should be activated in Org-mode files.
  907. This is a list of symbols, each leading to the activation of a certain link
  908. type. In principle, it does not hurt to turn on most link types - there may
  909. be a small gain when turning off unused link types. The types are:
  910. bracket The recommended [[link][description]] or [[link]] links with hiding.
  911. angular Links in angular brackets that may contain whitespace like
  912. <bbdb:Carsten Dominik>.
  913. plain Plain links in normal text, no whitespace, like http://google.com.
  914. radio Text that is matched by a radio target, see manual for details.
  915. tag Tag settings in a headline (link to tag search).
  916. date Time stamps (link to calendar).
  917. footnote Footnote labels.
  918. Changing this variable requires a restart of Emacs to become effective."
  919. :group 'org-link
  920. :type '(set :greedy t
  921. (const :tag "Double bracket links (new style)" bracket)
  922. (const :tag "Angular bracket links (old style)" angular)
  923. (const :tag "Plain text links" plain)
  924. (const :tag "Radio target matches" radio)
  925. (const :tag "Tags" tag)
  926. (const :tag "Timestamps" date)
  927. (const :tag "Footnotes" footnote)))
  928. (defcustom org-make-link-description-function nil
  929. "Function to use to generate link descriptions from links. If
  930. nil the link location will be used. This function must take two
  931. parameters; the first is the link and the second the description
  932. org-insert-link has generated, and should return the description
  933. to use."
  934. :group 'org-link
  935. :type 'function)
  936. (defgroup org-link-store nil
  937. "Options concerning storing links in Org-mode."
  938. :tag "Org Store Link"
  939. :group 'org-link)
  940. (defcustom org-email-link-description-format "Email %c: %.30s"
  941. "Format of the description part of a link to an email or usenet message.
  942. The following %-escapes will be replaced by corresponding information:
  943. %F full \"From\" field
  944. %f name, taken from \"From\" field, address if no name
  945. %T full \"To\" field
  946. %t first name in \"To\" field, address if no name
  947. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  948. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  949. %s subject
  950. %m message-id.
  951. You may use normal field width specification between the % and the letter.
  952. This is for example useful to limit the length of the subject.
  953. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  954. :group 'org-link-store
  955. :type 'string)
  956. (defcustom org-from-is-user-regexp
  957. (let (r1 r2)
  958. (when (and user-mail-address (not (string= user-mail-address "")))
  959. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  960. (when (and user-full-name (not (string= user-full-name "")))
  961. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  962. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  963. "Regexp matched against the \"From:\" header of an email or usenet message.
  964. It should match if the message is from the user him/herself."
  965. :group 'org-link-store
  966. :type 'regexp)
  967. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  968. "Non-nil means, storing a link to an Org file will use entry IDs.
  969. Note that before this variable is even considered, org-id must be loaded,
  970. so please customize `org-modules' and turn it on.
  971. The variable can have the following values:
  972. t Create an ID if needed to make a link to the current entry.
  973. create-if-interactive
  974. If `org-store-link' is called directly (interactively, as a user
  975. command), do create an ID to support the link. But when doing the
  976. job for remember, only use the ID if it already exists. The
  977. purpose of this setting is to avoid proliferation of unwanted
  978. IDs, just because you happen to be in an Org file when you
  979. call `org-remember' that automatically and preemptively
  980. creates a link. If you do want to get an ID link in a remember
  981. template to an entry not having an ID, create it first by
  982. explicitly creating a link to it, using `C-c C-l' first.
  983. create-if-interactive-and-no-custom-id
  984. Like create-if-interactive, but do not create an ID if there is
  985. a CUSTOM_ID property defined in the entry. This is the default.
  986. use-existing
  987. Use existing ID, do not create one.
  988. nil Never use an ID to make a link, instead link using a text search for
  989. the headline text."
  990. :group 'org-link-store
  991. :type '(choice
  992. (const :tag "Create ID to make link" t)
  993. (const :tag "Create if storing link interactively"
  994. create-if-interactive)
  995. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  996. create-if-interactive-and-no-custom-id)
  997. (const :tag "Only use existing" use-existing)
  998. (const :tag "Do not use ID to create link" nil)))
  999. (defcustom org-context-in-file-links t
  1000. "Non-nil means, file links from `org-store-link' contain context.
  1001. A search string will be added to the file name with :: as separator and
  1002. used to find the context when the link is activated by the command
  1003. `org-open-at-point'.
  1004. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1005. negates this setting for the duration of the command."
  1006. :group 'org-link-store
  1007. :type 'boolean)
  1008. (defcustom org-keep-stored-link-after-insertion nil
  1009. "Non-nil means, keep link in list for entire session.
  1010. The command `org-store-link' adds a link pointing to the current
  1011. location to an internal list. These links accumulate during a session.
  1012. The command `org-insert-link' can be used to insert links into any
  1013. Org-mode file (offering completion for all stored links). When this
  1014. option is nil, every link which has been inserted once using \\[org-insert-link]
  1015. will be removed from the list, to make completing the unused links
  1016. more efficient."
  1017. :group 'org-link-store
  1018. :type 'boolean)
  1019. (defgroup org-link-follow nil
  1020. "Options concerning following links in Org-mode."
  1021. :tag "Org Follow Link"
  1022. :group 'org-link)
  1023. (defcustom org-link-translation-function nil
  1024. "Function to translate links with different syntax to Org syntax.
  1025. This can be used to translate links created for example by the Planner
  1026. or emacs-wiki packages to Org syntax.
  1027. The function must accept two parameters, a TYPE containing the link
  1028. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1029. which is everything after the link protocol. It should return a cons
  1030. with possibly modified values of type and path.
  1031. Org contains a function for this, so if you set this variable to
  1032. `org-translate-link-from-planner', you should be able follow many
  1033. links created by planner."
  1034. :group 'org-link-follow
  1035. :type 'function)
  1036. (defcustom org-follow-link-hook nil
  1037. "Hook that is run after a link has been followed."
  1038. :group 'org-link-follow
  1039. :type 'hook)
  1040. (defcustom org-tab-follows-link nil
  1041. "Non-nil means, on links TAB will follow the link.
  1042. Needs to be set before org.el is loaded.
  1043. This really should not be used, it does not make sense, and the
  1044. implementation is bad."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-return-follows-link nil
  1048. "Non-nil means, on links RET will follow the link.
  1049. Needs to be set before org.el is loaded."
  1050. :group 'org-link-follow
  1051. :type 'boolean)
  1052. (defcustom org-mouse-1-follows-link
  1053. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1054. "Non-nil means, mouse-1 on a link will follow the link.
  1055. A longer mouse click will still set point. Does not work on XEmacs.
  1056. Needs to be set before org.el is loaded."
  1057. :group 'org-link-follow
  1058. :type 'boolean)
  1059. (defcustom org-mark-ring-length 4
  1060. "Number of different positions to be recorded in the ring
  1061. Changing this requires a restart of Emacs to work correctly."
  1062. :group 'org-link-follow
  1063. :type 'integer)
  1064. (defcustom org-link-frame-setup
  1065. '((vm . vm-visit-folder-other-frame)
  1066. (gnus . gnus-other-frame)
  1067. (file . find-file-other-window))
  1068. "Setup the frame configuration for following links.
  1069. When following a link with Emacs, it may often be useful to display
  1070. this link in another window or frame. This variable can be used to
  1071. set this up for the different types of links.
  1072. For VM, use any of
  1073. `vm-visit-folder'
  1074. `vm-visit-folder-other-frame'
  1075. For Gnus, use any of
  1076. `gnus'
  1077. `gnus-other-frame'
  1078. `org-gnus-no-new-news'
  1079. For FILE, use any of
  1080. `find-file'
  1081. `find-file-other-window'
  1082. `find-file-other-frame'
  1083. For the calendar, use the variable `calendar-setup'.
  1084. For BBDB, it is currently only possible to display the matches in
  1085. another window."
  1086. :group 'org-link-follow
  1087. :type '(list
  1088. (cons (const vm)
  1089. (choice
  1090. (const vm-visit-folder)
  1091. (const vm-visit-folder-other-window)
  1092. (const vm-visit-folder-other-frame)))
  1093. (cons (const gnus)
  1094. (choice
  1095. (const gnus)
  1096. (const gnus-other-frame)
  1097. (const org-gnus-no-new-news)))
  1098. (cons (const file)
  1099. (choice
  1100. (const find-file)
  1101. (const find-file-other-window)
  1102. (const find-file-other-frame)))))
  1103. (defcustom org-display-internal-link-with-indirect-buffer nil
  1104. "Non-nil means, use indirect buffer to display infile links.
  1105. Activating internal links (from one location in a file to another location
  1106. in the same file) normally just jumps to the location. When the link is
  1107. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1108. another window. When this option is set, the other window actually displays
  1109. an indirect buffer clone of the current buffer, to avoid any visibility
  1110. changes to the current buffer."
  1111. :group 'org-link-follow
  1112. :type 'boolean)
  1113. (defcustom org-open-non-existing-files nil
  1114. "Non-nil means, `org-open-file' will open non-existing files.
  1115. When nil, an error will be generated.
  1116. This variable applies only to external applications because they
  1117. might choke on non-existing files. If the link is to a file that
  1118. will be openend in Emacs, the variable is ignored."
  1119. :group 'org-link-follow
  1120. :type 'boolean)
  1121. (defcustom org-open-directory-means-index-dot-org nil
  1122. "Non-nil means, a link to a directory really means to index.org.
  1123. When nil, following a directory link will run dired or open a finder/explorer
  1124. window on that directory."
  1125. :group 'org-link-follow
  1126. :type 'boolean)
  1127. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1128. "Function and arguments to call for following mailto links.
  1129. This is a list with the first element being a lisp function, and the
  1130. remaining elements being arguments to the function. In string arguments,
  1131. %a will be replaced by the address, and %s will be replaced by the subject
  1132. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1133. :group 'org-link-follow
  1134. :type '(choice
  1135. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1136. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1137. (const :tag "message-mail" (message-mail "%a" "%s"))
  1138. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1139. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1140. "Non-nil means, ask for confirmation before executing shell links.
  1141. Shell links can be dangerous: just think about a link
  1142. [[shell:rm -rf ~/*][Google Search]]
  1143. This link would show up in your Org-mode document as \"Google Search\",
  1144. but really it would remove your entire home directory.
  1145. Therefore we advise against setting this variable to nil.
  1146. Just change it to `y-or-n-p' if you want to confirm with a
  1147. single keystroke rather than having to type \"yes\"."
  1148. :group 'org-link-follow
  1149. :type '(choice
  1150. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1151. (const :tag "with y-or-n (faster)" y-or-n-p)
  1152. (const :tag "no confirmation (dangerous)" nil)))
  1153. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1154. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1155. Elisp links can be dangerous: just think about a link
  1156. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1157. This link would show up in your Org-mode document as \"Google Search\",
  1158. but really it would remove your entire home directory.
  1159. Therefore we advise against setting this variable to nil.
  1160. Just change it to `y-or-n-p' if you want to confirm with a
  1161. single keystroke rather than having to type \"yes\"."
  1162. :group 'org-link-follow
  1163. :type '(choice
  1164. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1165. (const :tag "with y-or-n (faster)" y-or-n-p)
  1166. (const :tag "no confirmation (dangerous)" nil)))
  1167. (defconst org-file-apps-defaults-gnu
  1168. '((remote . emacs)
  1169. (system . mailcap)
  1170. (t . mailcap))
  1171. "Default file applications on a UNIX or GNU/Linux system.
  1172. See `org-file-apps'.")
  1173. (defconst org-file-apps-defaults-macosx
  1174. '((remote . emacs)
  1175. (t . "open %s")
  1176. (system . "open %s")
  1177. ("ps.gz" . "gv %s")
  1178. ("eps.gz" . "gv %s")
  1179. ("dvi" . "xdvi %s")
  1180. ("fig" . "xfig %s"))
  1181. "Default file applications on a MacOS X system.
  1182. The system \"open\" is known as a default, but we use X11 applications
  1183. for some files for which the OS does not have a good default.
  1184. See `org-file-apps'.")
  1185. (defconst org-file-apps-defaults-windowsnt
  1186. (list
  1187. '(remote . emacs)
  1188. (cons t
  1189. (list (if (featurep 'xemacs)
  1190. 'mswindows-shell-execute
  1191. 'w32-shell-execute)
  1192. "open" 'file))
  1193. (cons 'system
  1194. (list (if (featurep 'xemacs)
  1195. 'mswindows-shell-execute
  1196. 'w32-shell-execute)
  1197. "open" 'file)))
  1198. "Default file applications on a Windows NT system.
  1199. The system \"open\" is used for most files.
  1200. See `org-file-apps'.")
  1201. (defcustom org-file-apps
  1202. '(
  1203. (auto-mode . emacs)
  1204. ("\\.x?html?\\'" . default)
  1205. ("\\.pdf\\'" . default)
  1206. )
  1207. "External applications for opening `file:path' items in a document.
  1208. Org-mode uses system defaults for different file types, but
  1209. you can use this variable to set the application for a given file
  1210. extension. The entries in this list are cons cells where the car identifies
  1211. files and the cdr the corresponding command. Possible values for the
  1212. file identifier are
  1213. \"regex\" Regular expression matched against the file name. For backward
  1214. compatibility, this can also be a string with only alphanumeric
  1215. characters, which is then interpreted as an extension.
  1216. `directory' Matches a directory
  1217. `remote' Matches a remote file, accessible through tramp or efs.
  1218. Remote files most likely should be visited through Emacs
  1219. because external applications cannot handle such paths.
  1220. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1221. so all files Emacs knows how to handle. Using this with
  1222. command `emacs' will open most files in Emacs. Beware that this
  1223. will also open html files inside Emacs, unless you add
  1224. (\"html\" . default) to the list as well.
  1225. t Default for files not matched by any of the other options.
  1226. `system' The system command to open files, like `open' on Windows
  1227. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1228. that will be selected if you call `C-c C-o' with a double
  1229. `C-u C-u' prefix.
  1230. Possible values for the command are:
  1231. `emacs' The file will be visited by the current Emacs process.
  1232. `default' Use the default application for this file type, which is the
  1233. association for t in the list, most likely in the system-specific
  1234. part.
  1235. This can be used to overrule an unwanted setting in the
  1236. system-specific variable.
  1237. `system' Use the system command for opening files, like \"open\".
  1238. This command is specified by the entry whose car is `system'.
  1239. Most likely, the system-specific version of this variable
  1240. does define this command, but you can overrule/replace it
  1241. here.
  1242. string A command to be executed by a shell; %s will be replaced
  1243. by the path to the file.
  1244. sexp A Lisp form which will be evaluated. The file path will
  1245. be available in the Lisp variable `file'.
  1246. For more examples, see the system specific constants
  1247. `org-file-apps-defaults-macosx'
  1248. `org-file-apps-defaults-windowsnt'
  1249. `org-file-apps-defaults-gnu'."
  1250. :group 'org-link-follow
  1251. :type '(repeat
  1252. (cons (choice :value ""
  1253. (string :tag "Extension")
  1254. (const :tag "System command to open files" system)
  1255. (const :tag "Default for unrecognized files" t)
  1256. (const :tag "Remote file" remote)
  1257. (const :tag "Links to a directory" directory)
  1258. (const :tag "Any files that have Emacs modes"
  1259. auto-mode))
  1260. (choice :value ""
  1261. (const :tag "Visit with Emacs" emacs)
  1262. (const :tag "Use default" default)
  1263. (const :tag "Use the system command" system)
  1264. (string :tag "Command")
  1265. (sexp :tag "Lisp form")))))
  1266. (defgroup org-refile nil
  1267. "Options concerning refiling entries in Org-mode."
  1268. :tag "Org Refile"
  1269. :group 'org)
  1270. (defcustom org-directory "~/org"
  1271. "Directory with org files.
  1272. This is just a default location to look for Org files. There is no need
  1273. at all to put your files into this directory. It is only used in the
  1274. following situations:
  1275. 1. When a remember template specifies a target file that is not an
  1276. absolute path. The path will then be interpreted relative to
  1277. `org-directory'
  1278. 2. When a remember note is filed away in an interactive way (when exiting the
  1279. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1280. with `org-directory' as the default path."
  1281. :group 'org-refile
  1282. :group 'org-remember
  1283. :type 'directory)
  1284. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1285. "Default target for storing notes.
  1286. Used by the hooks for remember.el. This can be a string, or nil to mean
  1287. the value of `remember-data-file'.
  1288. You can set this on a per-template basis with the variable
  1289. `org-remember-templates'."
  1290. :group 'org-refile
  1291. :group 'org-remember
  1292. :type '(choice
  1293. (const :tag "Default from remember-data-file" nil)
  1294. file))
  1295. (defcustom org-goto-interface 'outline
  1296. "The default interface to be used for `org-goto'.
  1297. Allowed values are:
  1298. outline The interface shows an outline of the relevant file
  1299. and the correct heading is found by moving through
  1300. the outline or by searching with incremental search.
  1301. outline-path-completion Headlines in the current buffer are offered via
  1302. completion. This is the interface also used by
  1303. the refile command."
  1304. :group 'org-refile
  1305. :type '(choice
  1306. (const :tag "Outline" outline)
  1307. (const :tag "Outline-path-completion" outline-path-completion)))
  1308. (defcustom org-goto-max-level 5
  1309. "Maximum level to be considered when running org-goto with refile interface."
  1310. :group 'org-refile
  1311. :type 'integer)
  1312. (defcustom org-reverse-note-order nil
  1313. "Non-nil means, store new notes at the beginning of a file or entry.
  1314. When nil, new notes will be filed to the end of a file or entry.
  1315. This can also be a list with cons cells of regular expressions that
  1316. are matched against file names, and values."
  1317. :group 'org-remember
  1318. :group 'org-refile
  1319. :type '(choice
  1320. (const :tag "Reverse always" t)
  1321. (const :tag "Reverse never" nil)
  1322. (repeat :tag "By file name regexp"
  1323. (cons regexp boolean))))
  1324. (defcustom org-refile-targets nil
  1325. "Targets for refiling entries with \\[org-refile].
  1326. This is list of cons cells. Each cell contains:
  1327. - a specification of the files to be considered, either a list of files,
  1328. or a symbol whose function or variable value will be used to retrieve
  1329. a file name or a list of file names. If you use `org-agenda-files' for
  1330. that, all agenda files will be scanned for targets. Nil means, consider
  1331. headings in the current buffer.
  1332. - A specification of how to find candidate refile targets. This may be
  1333. any of:
  1334. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1335. This tag has to be present in all target headlines, inheritance will
  1336. not be considered.
  1337. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1338. todo keyword.
  1339. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1340. headlines that are refiling targets.
  1341. - a cons cell (:level . N). Any headline of level N is considered a target.
  1342. Note that, when `org-odd-levels-only' is set, level corresponds to
  1343. order in hierarchy, not to the number of stars.
  1344. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1345. Note that, when `org-odd-levels-only' is set, level corresponds to
  1346. order in hierarchy, not to the number of stars.
  1347. You can set the variable `org-refile-target-verify-function' to a function
  1348. to verify each headline found by the simple critery above.
  1349. When this variable is nil, all top-level headlines in the current buffer
  1350. are used, equivalent to the value `((nil . (:level . 1))'."
  1351. :group 'org-refile
  1352. :type '(repeat
  1353. (cons
  1354. (choice :value org-agenda-files
  1355. (const :tag "All agenda files" org-agenda-files)
  1356. (const :tag "Current buffer" nil)
  1357. (function) (variable) (file))
  1358. (choice :tag "Identify target headline by"
  1359. (cons :tag "Specific tag" (const :value :tag) (string))
  1360. (cons :tag "TODO keyword" (const :value :todo) (string))
  1361. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1362. (cons :tag "Level number" (const :value :level) (integer))
  1363. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1364. (defcustom org-refile-target-verify-function nil
  1365. "Function to verify if the headline at point should be a refile target.
  1366. The function will be called without arguments, with point at the
  1367. beginning of the headline. It should return t and leave point
  1368. where it is if the headline is a valid target for refiling.
  1369. If the target should not be selected, the function must return nil.
  1370. In addition to this, it may move point to a place from where the search
  1371. should be continued. For example, the function may decide that the entire
  1372. subtree of the current entry should be excluded and move point to the end
  1373. of the subtree."
  1374. :group 'org-refile
  1375. :type 'function)
  1376. (defcustom org-refile-use-outline-path nil
  1377. "Non-nil means, provide refile targets as paths.
  1378. So a level 3 headline will be available as level1/level2/level3.
  1379. When the value is `file', also include the file name (without directory)
  1380. into the path. In this case, you can also stop the completion after
  1381. the file name, to get entries inserted as top level in the file.
  1382. When `full-file-path', include the full file path."
  1383. :group 'org-refile
  1384. :type '(choice
  1385. (const :tag "Not" nil)
  1386. (const :tag "Yes" t)
  1387. (const :tag "Start with file name" file)
  1388. (const :tag "Start with full file path" full-file-path)))
  1389. (defcustom org-outline-path-complete-in-steps t
  1390. "Non-nil means, complete the outline path in hierarchical steps.
  1391. When Org-mode uses the refile interface to select an outline path
  1392. \(see variable `org-refile-use-outline-path'), the completion of
  1393. the path can be done is a single go, or if can be done in steps down
  1394. the headline hierarchy. Going in steps is probably the best if you
  1395. do not use a special completion package like `ido' or `icicles'.
  1396. However, when using these packages, going in one step can be very
  1397. fast, while still showing the whole path to the entry."
  1398. :group 'org-refile
  1399. :type 'boolean)
  1400. (defcustom org-refile-allow-creating-parent-nodes nil
  1401. "Non-nil means, allow to create new nodes as refile targets.
  1402. New nodes are then created by adding \"/new node name\" to the completion
  1403. of an existing node. When the value of this variable is `confirm',
  1404. new node creation must be confirmed by the user (recommended)
  1405. When nil, the completion must match an existing entry.
  1406. Note that, if the new heading is not seen by the criteria
  1407. listed in `org-refile-targets', multiple instances of the same
  1408. heading would be created by trying again to file under the new
  1409. heading."
  1410. :group 'org-refile
  1411. :type '(choice
  1412. (const :tag "Never" nil)
  1413. (const :tag "Always" t)
  1414. (const :tag "Prompt for confirmation" confirm)))
  1415. (defgroup org-todo nil
  1416. "Options concerning TODO items in Org-mode."
  1417. :tag "Org TODO"
  1418. :group 'org)
  1419. (defgroup org-progress nil
  1420. "Options concerning Progress logging in Org-mode."
  1421. :tag "Org Progress"
  1422. :group 'org-time)
  1423. (defvar org-todo-interpretation-widgets
  1424. '(
  1425. (:tag "Sequence (cycling hits every state)" sequence)
  1426. (:tag "Type (cycling directly to DONE)" type))
  1427. "The available interpretation symbols for customizing
  1428. `org-todo-keywords'.
  1429. Interested libraries should add to this list.")
  1430. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1431. "List of TODO entry keyword sequences and their interpretation.
  1432. \\<org-mode-map>This is a list of sequences.
  1433. Each sequence starts with a symbol, either `sequence' or `type',
  1434. indicating if the keywords should be interpreted as a sequence of
  1435. action steps, or as different types of TODO items. The first
  1436. keywords are states requiring action - these states will select a headline
  1437. for inclusion into the global TODO list Org-mode produces. If one of
  1438. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1439. signify that no further action is necessary. If \"|\" is not found,
  1440. the last keyword is treated as the only DONE state of the sequence.
  1441. The command \\[org-todo] cycles an entry through these states, and one
  1442. additional state where no keyword is present. For details about this
  1443. cycling, see the manual.
  1444. TODO keywords and interpretation can also be set on a per-file basis with
  1445. the special #+SEQ_TODO and #+TYP_TODO lines.
  1446. Each keyword can optionally specify a character for fast state selection
  1447. \(in combination with the variable `org-use-fast-todo-selection')
  1448. and specifiers for state change logging, using the same syntax
  1449. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1450. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1451. indicates to record a time stamp each time this state is selected.
  1452. Each keyword may also specify if a timestamp or a note should be
  1453. recorded when entering or leaving the state, by adding additional
  1454. characters in the parenthesis after the keyword. This looks like this:
  1455. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1456. record only the time of the state change. With X and Y being either
  1457. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1458. Y when leaving the state if and only if the *target* state does not
  1459. define X. You may omit any of the fast-selection key or X or /Y,
  1460. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1461. For backward compatibility, this variable may also be just a list
  1462. of keywords - in this case the interpretation (sequence or type) will be
  1463. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1464. :group 'org-todo
  1465. :group 'org-keywords
  1466. :type '(choice
  1467. (repeat :tag "Old syntax, just keywords"
  1468. (string :tag "Keyword"))
  1469. (repeat :tag "New syntax"
  1470. (cons
  1471. (choice
  1472. :tag "Interpretation"
  1473. ;;Quick and dirty way to see
  1474. ;;`org-todo-interpretations'. This takes the
  1475. ;;place of item arguments
  1476. :convert-widget
  1477. (lambda (widget)
  1478. (widget-put widget
  1479. :args (mapcar
  1480. #'(lambda (x)
  1481. (widget-convert
  1482. (cons 'const x)))
  1483. org-todo-interpretation-widgets))
  1484. widget))
  1485. (repeat
  1486. (string :tag "Keyword"))))))
  1487. (defvar org-todo-keywords-1 nil
  1488. "All TODO and DONE keywords active in a buffer.")
  1489. (make-variable-buffer-local 'org-todo-keywords-1)
  1490. (defvar org-todo-keywords-for-agenda nil)
  1491. (defvar org-done-keywords-for-agenda nil)
  1492. (defvar org-todo-keyword-alist-for-agenda nil)
  1493. (defvar org-tag-alist-for-agenda nil)
  1494. (defvar org-agenda-contributing-files nil)
  1495. (defvar org-not-done-keywords nil)
  1496. (make-variable-buffer-local 'org-not-done-keywords)
  1497. (defvar org-done-keywords nil)
  1498. (make-variable-buffer-local 'org-done-keywords)
  1499. (defvar org-todo-heads nil)
  1500. (make-variable-buffer-local 'org-todo-heads)
  1501. (defvar org-todo-sets nil)
  1502. (make-variable-buffer-local 'org-todo-sets)
  1503. (defvar org-todo-log-states nil)
  1504. (make-variable-buffer-local 'org-todo-log-states)
  1505. (defvar org-todo-kwd-alist nil)
  1506. (make-variable-buffer-local 'org-todo-kwd-alist)
  1507. (defvar org-todo-key-alist nil)
  1508. (make-variable-buffer-local 'org-todo-key-alist)
  1509. (defvar org-todo-key-trigger nil)
  1510. (make-variable-buffer-local 'org-todo-key-trigger)
  1511. (defcustom org-todo-interpretation 'sequence
  1512. "Controls how TODO keywords are interpreted.
  1513. This variable is in principle obsolete and is only used for
  1514. backward compatibility, if the interpretation of todo keywords is
  1515. not given already in `org-todo-keywords'. See that variable for
  1516. more information."
  1517. :group 'org-todo
  1518. :group 'org-keywords
  1519. :type '(choice (const sequence)
  1520. (const type)))
  1521. (defcustom org-use-fast-todo-selection t
  1522. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1523. This variable describes if and under what circumstances the cycling
  1524. mechanism for TODO keywords will be replaced by a single-key, direct
  1525. selection scheme.
  1526. When nil, fast selection is never used.
  1527. When the symbol `prefix', it will be used when `org-todo' is called with
  1528. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1529. in an agenda buffer.
  1530. When t, fast selection is used by default. In this case, the prefix
  1531. argument forces cycling instead.
  1532. In all cases, the special interface is only used if access keys have actually
  1533. been assigned by the user, i.e. if keywords in the configuration are followed
  1534. by a letter in parenthesis, like TODO(t)."
  1535. :group 'org-todo
  1536. :type '(choice
  1537. (const :tag "Never" nil)
  1538. (const :tag "By default" t)
  1539. (const :tag "Only with C-u C-c C-t" prefix)))
  1540. (defcustom org-provide-todo-statistics t
  1541. "Non-nil means, update todo statistics after insert and toggle.
  1542. ALL-HEADLINES means update todo statistics by including headlines
  1543. with no TODO keyword as well, counting them as not done.
  1544. A list of TODO keywords means the same, but skip keywords that are
  1545. not in this list.
  1546. When this is set, todo statistics is updated in the parent of the
  1547. current entry each time a todo state is changed."
  1548. :group 'org-todo
  1549. :type '(choice
  1550. (const :tag "Yes, only for TODO entries" t)
  1551. (const :tag "Yes, including all entries" 'all-headlines)
  1552. (repeat :tag "Yes, for TODOs in this list"
  1553. (string :tag "TODO keyword"))
  1554. (other :tag "No TODO statistics" nil)))
  1555. (defcustom org-hierarchical-todo-statistics t
  1556. "Non-nil means, TODO statistics covers just direct children.
  1557. When nil, all entries in the subtree are considered.
  1558. This has only an effect if `org-provide-todo-statistics' is set.
  1559. To set this to nil for only a single subtree, use a COOKIE_DATA
  1560. property and include the word \"recursive\" into the value."
  1561. :group 'org-todo
  1562. :type 'boolean)
  1563. (defcustom org-after-todo-state-change-hook nil
  1564. "Hook which is run after the state of a TODO item was changed.
  1565. The new state (a string with a TODO keyword, or nil) is available in the
  1566. Lisp variable `state'."
  1567. :group 'org-todo
  1568. :type 'hook)
  1569. (defvar org-blocker-hook nil
  1570. "Hook for functions that are allowed to block a state change.
  1571. Each function gets as its single argument a property list, see
  1572. `org-trigger-hook' for more information about this list.
  1573. If any of the functions in this hook returns nil, the state change
  1574. is blocked.")
  1575. (defvar org-trigger-hook nil
  1576. "Hook for functions that are triggered by a state change.
  1577. Each function gets as its single argument a property list with at least
  1578. the following elements:
  1579. (:type type-of-change :position pos-at-entry-start
  1580. :from old-state :to new-state)
  1581. Depending on the type, more properties may be present.
  1582. This mechanism is currently implemented for:
  1583. TODO state changes
  1584. ------------------
  1585. :type todo-state-change
  1586. :from previous state (keyword as a string), or nil, or a symbol
  1587. 'todo' or 'done', to indicate the general type of state.
  1588. :to new state, like in :from")
  1589. (defcustom org-enforce-todo-dependencies nil
  1590. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1591. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1592. be blocked if any prior sibling is not yet done.
  1593. Finally, if the parent is blocked because of ordered siblings of its own,
  1594. the child will also be blocked.
  1595. This variable needs to be set before org.el is loaded, and you need to
  1596. restart Emacs after a change to make the change effective. The only way
  1597. to change is while Emacs is running is through the customize interface."
  1598. :set (lambda (var val)
  1599. (set var val)
  1600. (if val
  1601. (add-hook 'org-blocker-hook
  1602. 'org-block-todo-from-children-or-siblings-or-parent)
  1603. (remove-hook 'org-blocker-hook
  1604. 'org-block-todo-from-children-or-siblings-or-parent)))
  1605. :group 'org-todo
  1606. :type 'boolean)
  1607. (defcustom org-enforce-todo-checkbox-dependencies nil
  1608. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1609. When this is nil, checkboxes have no influence on switching TODO states.
  1610. When non-nil, you first need to check off all check boxes before the TODO
  1611. entry can be switched to DONE.
  1612. This variable needs to be set before org.el is loaded, and you need to
  1613. restart Emacs after a change to make the change effective. The only way
  1614. to change is while Emacs is running is through the customize interface."
  1615. :set (lambda (var val)
  1616. (set var val)
  1617. (if val
  1618. (add-hook 'org-blocker-hook
  1619. 'org-block-todo-from-checkboxes)
  1620. (remove-hook 'org-blocker-hook
  1621. 'org-block-todo-from-checkboxes)))
  1622. :group 'org-todo
  1623. :type 'boolean)
  1624. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1625. "Non-nil means, inserting a TODO heading is treated as state change.
  1626. So when the command \\[org-insert-todo-heading] is used, state change
  1627. logging will apply if appropriate. When nil, the new TODO item will
  1628. be inserted directly, and no logging will take place."
  1629. :group 'org-todo
  1630. :type 'boolean)
  1631. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1632. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1633. This is the default behavior. However, setting this to nil allows a
  1634. convenient way to select a TODO state and bypass any logging associated
  1635. with that."
  1636. :group 'org-todo
  1637. :type 'boolean)
  1638. (defcustom org-todo-state-tags-triggers nil
  1639. "Tag changes that should be triggered by TODO state changes.
  1640. This is a list. Each entry is
  1641. (state-change (tag . flag) .......)
  1642. State-change can be a string with a state, and empty string to indicate the
  1643. state that has no TODO keyword, or it can be one of the symbols `todo'
  1644. or `done', meaning any not-done or done state, respectively."
  1645. :group 'org-todo
  1646. :group 'org-tags
  1647. :type '(repeat
  1648. (cons (choice :tag "When changing to"
  1649. (const :tag "Not-done state" todo)
  1650. (const :tag "Done state" done)
  1651. (string :tag "State"))
  1652. (repeat
  1653. (cons :tag "Tag action"
  1654. (string :tag "Tag")
  1655. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1656. (defcustom org-log-done nil
  1657. "Information to record when a task moves to the DONE state.
  1658. Possible values are:
  1659. nil Don't add anything, just change the keyword
  1660. time Add a time stamp to the task
  1661. note Prompt a closing note and add it with template `org-log-note-headings'
  1662. This option can also be set with on a per-file-basis with
  1663. #+STARTUP: nologdone
  1664. #+STARTUP: logdone
  1665. #+STARTUP: lognotedone
  1666. You can have local logging settings for a subtree by setting the LOGGING
  1667. property to one or more of these keywords."
  1668. :group 'org-todo
  1669. :group 'org-progress
  1670. :type '(choice
  1671. (const :tag "No logging" nil)
  1672. (const :tag "Record CLOSED timestamp" time)
  1673. (const :tag "Record CLOSED timestamp with closing note." note)))
  1674. ;; Normalize old uses of org-log-done.
  1675. (cond
  1676. ((eq org-log-done t) (setq org-log-done 'time))
  1677. ((and (listp org-log-done) (memq 'done org-log-done))
  1678. (setq org-log-done 'note)))
  1679. (defcustom org-log-note-clock-out nil
  1680. "Non-nil means, record a note when clocking out of an item.
  1681. This can also be configured on a per-file basis by adding one of
  1682. the following lines anywhere in the buffer:
  1683. #+STARTUP: lognoteclock-out
  1684. #+STARTUP: nolognoteclock-out"
  1685. :group 'org-todo
  1686. :group 'org-progress
  1687. :type 'boolean)
  1688. (defcustom org-log-done-with-time t
  1689. "Non-nil means, the CLOSED time stamp will contain date and time.
  1690. When nil, only the date will be recorded."
  1691. :group 'org-progress
  1692. :type 'boolean)
  1693. (defcustom org-log-note-headings
  1694. '((done . "CLOSING NOTE %t")
  1695. (state . "State %-12s from %-12S %t")
  1696. (note . "Note taken on %t")
  1697. (clock-out . ""))
  1698. "Headings for notes added to entries.
  1699. The value is an alist, with the car being a symbol indicating the note
  1700. context, and the cdr is the heading to be used. The heading may also be the
  1701. empty string.
  1702. %t in the heading will be replaced by a time stamp.
  1703. %s will be replaced by the new TODO state, in double quotes.
  1704. %S will be replaced by the old TODO state, in double quotes.
  1705. %u will be replaced by the user name.
  1706. %U will be replaced by the full user name."
  1707. :group 'org-todo
  1708. :group 'org-progress
  1709. :type '(list :greedy t
  1710. (cons (const :tag "Heading when closing an item" done) string)
  1711. (cons (const :tag
  1712. "Heading when changing todo state (todo sequence only)"
  1713. state) string)
  1714. (cons (const :tag "Heading when just taking a note" note) string)
  1715. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1716. (unless (assq 'note org-log-note-headings)
  1717. (push '(note . "%t") org-log-note-headings))
  1718. (defcustom org-log-into-drawer nil
  1719. "Non-nil means, insert state change notes and time stamps into a drawer.
  1720. When nil, state changes notes will be inserted after the headline and
  1721. any scheduling and clock lines, but not inside a drawer.
  1722. The value of this variable should be the name of the drawer to use.
  1723. LOGBOOK is proposed at the default drawer for this purpose, you can
  1724. also set this to a string to define the drawer of your choice.
  1725. A value of t is also allowed, representing \"LOGBOOK\".
  1726. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1727. will be ignored.
  1728. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1729. a subtree."
  1730. :group 'org-todo
  1731. :group 'org-progress
  1732. :type '(choice
  1733. (const :tag "Not into a drawer" nil)
  1734. (const :tag "LOGBOOK" t)
  1735. (string :tag "Other")))
  1736. (if (fboundp 'defvaralias)
  1737. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1738. (defun org-log-into-drawer ()
  1739. "Return the value of `org-log-into-drawer', but let properties overrule.
  1740. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1741. used instead of the default value."
  1742. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1743. (cond
  1744. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1745. ((equal p "t") "LOGBOOK")
  1746. (t p))))
  1747. (defcustom org-log-state-notes-insert-after-drawers nil
  1748. "Non-nil means, insert state change notes after any drawers in entry.
  1749. Only the drawers that *immediately* follow the headline and the
  1750. deadline/scheduled line are skipped.
  1751. When nil, insert notes right after the heading and perhaps the line
  1752. with deadline/scheduling if present.
  1753. This variable will have no effect if `org-log-into-drawer' is
  1754. set."
  1755. :group 'org-todo
  1756. :group 'org-progress
  1757. :type 'boolean)
  1758. (defcustom org-log-states-order-reversed t
  1759. "Non-nil means, the latest state change note will be directly after heading.
  1760. When nil, the notes will be orderer according to time."
  1761. :group 'org-todo
  1762. :group 'org-progress
  1763. :type 'boolean)
  1764. (defcustom org-log-repeat 'time
  1765. "Non-nil means, record moving through the DONE state when triggering repeat.
  1766. An auto-repeating tasks is immediately switched back to TODO when marked
  1767. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1768. the TODO keyword definition, or recording a closing note by setting
  1769. `org-log-done', there will be no record of the task moving through DONE.
  1770. This variable forces taking a note anyway. Possible values are:
  1771. nil Don't force a record
  1772. time Record a time stamp
  1773. note Record a note
  1774. This option can also be set with on a per-file-basis with
  1775. #+STARTUP: logrepeat
  1776. #+STARTUP: lognoterepeat
  1777. #+STARTUP: nologrepeat
  1778. You can have local logging settings for a subtree by setting the LOGGING
  1779. property to one or more of these keywords."
  1780. :group 'org-todo
  1781. :group 'org-progress
  1782. :type '(choice
  1783. (const :tag "Don't force a record" nil)
  1784. (const :tag "Force recording the DONE state" time)
  1785. (const :tag "Force recording a note with the DONE state" note)))
  1786. (defgroup org-priorities nil
  1787. "Priorities in Org-mode."
  1788. :tag "Org Priorities"
  1789. :group 'org-todo)
  1790. (defcustom org-enable-priority-commands t
  1791. "Non-nil means, priority commands are active.
  1792. When nil, these commands will be disabled, so that you never accidentally
  1793. set a priority."
  1794. :group 'org-priorities
  1795. :type 'boolean)
  1796. (defcustom org-highest-priority ?A
  1797. "The highest priority of TODO items. A character like ?A, ?B etc.
  1798. Must have a smaller ASCII number than `org-lowest-priority'."
  1799. :group 'org-priorities
  1800. :type 'character)
  1801. (defcustom org-lowest-priority ?C
  1802. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1803. Must have a larger ASCII number than `org-highest-priority'."
  1804. :group 'org-priorities
  1805. :type 'character)
  1806. (defcustom org-default-priority ?B
  1807. "The default priority of TODO items.
  1808. This is the priority an item get if no explicit priority is given."
  1809. :group 'org-priorities
  1810. :type 'character)
  1811. (defcustom org-priority-start-cycle-with-default t
  1812. "Non-nil means, start with default priority when starting to cycle.
  1813. When this is nil, the first step in the cycle will be (depending on the
  1814. command used) one higher or lower that the default priority."
  1815. :group 'org-priorities
  1816. :type 'boolean)
  1817. (defgroup org-time nil
  1818. "Options concerning time stamps and deadlines in Org-mode."
  1819. :tag "Org Time"
  1820. :group 'org)
  1821. (defcustom org-insert-labeled-timestamps-at-point nil
  1822. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1823. When nil, these labeled time stamps are forces into the second line of an
  1824. entry, just after the headline. When scheduling from the global TODO list,
  1825. the time stamp will always be forced into the second line."
  1826. :group 'org-time
  1827. :type 'boolean)
  1828. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1829. "Formats for `format-time-string' which are used for time stamps.
  1830. It is not recommended to change this constant.")
  1831. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1832. "Number of minutes to round time stamps to.
  1833. These are two values, the first applies when first creating a time stamp.
  1834. The second applies when changing it with the commands `S-up' and `S-down'.
  1835. When changing the time stamp, this means that it will change in steps
  1836. of N minutes, as given by the second value.
  1837. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1838. numbers should be factors of 60, so for example 5, 10, 15.
  1839. When this is larger than 1, you can still force an exact time-stamp by using
  1840. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1841. and by using a prefix arg to `S-up/down' to specify the exact number
  1842. of minutes to shift."
  1843. :group 'org-time
  1844. :get '(lambda (var) ; Make sure all entries have 5 elements
  1845. (if (integerp (default-value var))
  1846. (list (default-value var) 5)
  1847. (default-value var)))
  1848. :type '(list
  1849. (integer :tag "when inserting times")
  1850. (integer :tag "when modifying times")))
  1851. ;; Normalize old customizations of this variable.
  1852. (when (integerp org-time-stamp-rounding-minutes)
  1853. (setq org-time-stamp-rounding-minutes
  1854. (list org-time-stamp-rounding-minutes
  1855. org-time-stamp-rounding-minutes)))
  1856. (defcustom org-display-custom-times nil
  1857. "Non-nil means, overlay custom formats over all time stamps.
  1858. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1859. To turn this on on a per-file basis, insert anywhere in the file:
  1860. #+STARTUP: customtime"
  1861. :group 'org-time
  1862. :set 'set-default
  1863. :type 'sexp)
  1864. (make-variable-buffer-local 'org-display-custom-times)
  1865. (defcustom org-time-stamp-custom-formats
  1866. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1867. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1868. These are overlayed over the default ISO format if the variable
  1869. `org-display-custom-times' is set. Time like %H:%M should be at the
  1870. end of the second format. The custom formats are also honored by export
  1871. commands, if custom time display is turned on at the time of export."
  1872. :group 'org-time
  1873. :type 'sexp)
  1874. (defun org-time-stamp-format (&optional long inactive)
  1875. "Get the right format for a time string."
  1876. (let ((f (if long (cdr org-time-stamp-formats)
  1877. (car org-time-stamp-formats))))
  1878. (if inactive
  1879. (concat "[" (substring f 1 -1) "]")
  1880. f)))
  1881. (defcustom org-time-clocksum-format "%d:%02d"
  1882. "The format string used when creating CLOCKSUM lines, or when
  1883. org-mode generates a time duration."
  1884. :group 'org-time
  1885. :type 'string)
  1886. (defcustom org-deadline-warning-days 14
  1887. "No. of days before expiration during which a deadline becomes active.
  1888. This variable governs the display in sparse trees and in the agenda.
  1889. When 0 or negative, it means use this number (the absolute value of it)
  1890. even if a deadline has a different individual lead time specified.
  1891. Custom commands can set this variable in the options section."
  1892. :group 'org-time
  1893. :group 'org-agenda-daily/weekly
  1894. :type 'integer)
  1895. (defcustom org-read-date-prefer-future t
  1896. "Non-nil means, assume future for incomplete date input from user.
  1897. This affects the following situations:
  1898. 1. The user gives a day, but no month.
  1899. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1900. read this as the third of *next* month. However, if you enter \"17\",
  1901. it will be considered as *this* month.
  1902. 2. The user gives a month but not a year.
  1903. For example, if it is april and you enter \"feb 2\", this will be read
  1904. as feb 2, *next* year. \"May 5\", however, will be this year.
  1905. Currently this does not work for ISO week specifications.
  1906. When this option is nil, the current month and year will always be used
  1907. as defaults."
  1908. :group 'org-time
  1909. :type 'boolean)
  1910. (defcustom org-read-date-display-live t
  1911. "Non-nil means, display current interpretation of date prompt live.
  1912. This display will be in an overlay, in the minibuffer."
  1913. :group 'org-time
  1914. :type 'boolean)
  1915. (defcustom org-read-date-popup-calendar t
  1916. "Non-nil means, pop up a calendar when prompting for a date.
  1917. In the calendar, the date can be selected with mouse-1. However, the
  1918. minibuffer will also be active, and you can simply enter the date as well.
  1919. When nil, only the minibuffer will be available."
  1920. :group 'org-time
  1921. :type 'boolean)
  1922. (if (fboundp 'defvaralias)
  1923. (defvaralias 'org-popup-calendar-for-date-prompt
  1924. 'org-read-date-popup-calendar))
  1925. (defcustom org-read-date-minibuffer-setup-hook nil
  1926. "Hook to be used to set up keys for the date/time interface.
  1927. Add key definitions to `minibuffer-local-map', which will be a temporary
  1928. copy."
  1929. :group 'org-time
  1930. :type 'hook)
  1931. (defcustom org-extend-today-until 0
  1932. "The hour when your day really ends. Must be an integer.
  1933. This has influence for the following applications:
  1934. - When switching the agenda to \"today\". It it is still earlier than
  1935. the time given here, the day recognized as TODAY is actually yesterday.
  1936. - When a date is read from the user and it is still before the time given
  1937. here, the current date and time will be assumed to be yesterday, 23:59.
  1938. Also, timestamps inserted in remember templates follow this rule.
  1939. IMPORTANT: This is a feature whose implementation is and likely will
  1940. remain incomplete. Really, it is only here because past midnight seems to
  1941. be the favorite working time of John Wiegley :-)"
  1942. :group 'org-time
  1943. :type 'integer)
  1944. (defcustom org-edit-timestamp-down-means-later nil
  1945. "Non-nil means, S-down will increase the time in a time stamp.
  1946. When nil, S-up will increase."
  1947. :group 'org-time
  1948. :type 'boolean)
  1949. (defcustom org-calendar-follow-timestamp-change t
  1950. "Non-nil means, make the calendar window follow timestamp changes.
  1951. When a timestamp is modified and the calendar window is visible, it will be
  1952. moved to the new date."
  1953. :group 'org-time
  1954. :type 'boolean)
  1955. (defgroup org-tags nil
  1956. "Options concerning tags in Org-mode."
  1957. :tag "Org Tags"
  1958. :group 'org)
  1959. (defcustom org-tag-alist nil
  1960. "List of tags allowed in Org-mode files.
  1961. When this list is nil, Org-mode will base TAG input on what is already in the
  1962. buffer.
  1963. The value of this variable is an alist, the car of each entry must be a
  1964. keyword as a string, the cdr may be a character that is used to select
  1965. that tag through the fast-tag-selection interface.
  1966. See the manual for details."
  1967. :group 'org-tags
  1968. :type '(repeat
  1969. (choice
  1970. (cons (string :tag "Tag name")
  1971. (character :tag "Access char"))
  1972. (const :tag "Start radio group" (:startgroup))
  1973. (const :tag "End radio group" (:endgroup))
  1974. (const :tag "New line" (:newline)))))
  1975. (defcustom org-tag-persistent-alist nil
  1976. "List of tags that will always appear in all Org-mode files.
  1977. This is in addition to any in buffer settings or customizations
  1978. of `org-tag-alist'.
  1979. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1980. The value of this variable is an alist, the car of each entry must be a
  1981. keyword as a string, the cdr may be a character that is used to select
  1982. that tag through the fast-tag-selection interface.
  1983. See the manual for details.
  1984. To disable these tags on a per-file basis, insert anywhere in the file:
  1985. #+STARTUP: noptag"
  1986. :group 'org-tags
  1987. :type '(repeat
  1988. (choice
  1989. (cons (string :tag "Tag name")
  1990. (character :tag "Access char"))
  1991. (const :tag "Start radio group" (:startgroup))
  1992. (const :tag "End radio group" (:endgroup))
  1993. (const :tag "New line" (:newline)))))
  1994. (defvar org-file-tags nil
  1995. "List of tags that can be inherited by all entries in the file.
  1996. The tags will be inherited if the variable `org-use-tag-inheritance'
  1997. says they should be.
  1998. This variable is populated from #+TAG lines.")
  1999. (defcustom org-use-fast-tag-selection 'auto
  2000. "Non-nil means, use fast tag selection scheme.
  2001. This is a special interface to select and deselect tags with single keys.
  2002. When nil, fast selection is never used.
  2003. When the symbol `auto', fast selection is used if and only if selection
  2004. characters for tags have been configured, either through the variable
  2005. `org-tag-alist' or through a #+TAGS line in the buffer.
  2006. When t, fast selection is always used and selection keys are assigned
  2007. automatically if necessary."
  2008. :group 'org-tags
  2009. :type '(choice
  2010. (const :tag "Always" t)
  2011. (const :tag "Never" nil)
  2012. (const :tag "When selection characters are configured" 'auto)))
  2013. (defcustom org-fast-tag-selection-single-key nil
  2014. "Non-nil means, fast tag selection exits after first change.
  2015. When nil, you have to press RET to exit it.
  2016. During fast tag selection, you can toggle this flag with `C-c'.
  2017. This variable can also have the value `expert'. In this case, the window
  2018. displaying the tags menu is not even shown, until you press C-c again."
  2019. :group 'org-tags
  2020. :type '(choice
  2021. (const :tag "No" nil)
  2022. (const :tag "Yes" t)
  2023. (const :tag "Expert" expert)))
  2024. (defvar org-fast-tag-selection-include-todo nil
  2025. "Non-nil means, fast tags selection interface will also offer TODO states.
  2026. This is an undocumented feature, you should not rely on it.")
  2027. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2028. "The column to which tags should be indented in a headline.
  2029. If this number is positive, it specifies the column. If it is negative,
  2030. it means that the tags should be flushright to that column. For example,
  2031. -80 works well for a normal 80 character screen."
  2032. :group 'org-tags
  2033. :type 'integer)
  2034. (defcustom org-auto-align-tags t
  2035. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2036. These operations change the length of a headline and therefore shift
  2037. the tags around. With this options turned on, after each such operation
  2038. the tags are again aligned to `org-tags-column'."
  2039. :group 'org-tags
  2040. :type 'boolean)
  2041. (defcustom org-use-tag-inheritance t
  2042. "Non-nil means, tags in levels apply also for sublevels.
  2043. When nil, only the tags directly given in a specific line apply there.
  2044. This may also be a list of tags that should be inherited, or a regexp that
  2045. matches tags that should be inherited. Additional control is possible
  2046. with the variable `org-tags-exclude-from-inheritance' which gives an
  2047. explicit list of tags to be excluded from inheritance., even if the value of
  2048. `org-use-tag-inheritance' would select it for inheritance.
  2049. If this option is t, a match early-on in a tree can lead to a large
  2050. number of matches in the subtree when constructing the agenda or creating
  2051. a sparse tree. If you only want to see the first match in a tree during
  2052. a search, check out the variable `org-tags-match-list-sublevels'."
  2053. :group 'org-tags
  2054. :type '(choice
  2055. (const :tag "Not" nil)
  2056. (const :tag "Always" t)
  2057. (repeat :tag "Specific tags" (string :tag "Tag"))
  2058. (regexp :tag "Tags matched by regexp")))
  2059. (defcustom org-tags-exclude-from-inheritance nil
  2060. "List of tags that should never be inherited.
  2061. This is a way to exclude a few tags from inheritance. For way to do
  2062. the opposite, to actively allow inheritance for selected tags,
  2063. see the variable `org-use-tag-inheritance'."
  2064. :group 'org-tags
  2065. :type '(repeat (string :tag "Tag")))
  2066. (defun org-tag-inherit-p (tag)
  2067. "Check if TAG is one that should be inherited."
  2068. (cond
  2069. ((member tag org-tags-exclude-from-inheritance) nil)
  2070. ((eq org-use-tag-inheritance t) t)
  2071. ((not org-use-tag-inheritance) nil)
  2072. ((stringp org-use-tag-inheritance)
  2073. (string-match org-use-tag-inheritance tag))
  2074. ((listp org-use-tag-inheritance)
  2075. (member tag org-use-tag-inheritance))
  2076. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2077. (defcustom org-tags-match-list-sublevels t
  2078. "Non-nil means list also sublevels of headlines matching a search.
  2079. This variable applies to tags/property searches, and also to stuck
  2080. projects because this search is based on a tags match as well.
  2081. When set to the symbol `indented', sublevels are indented with
  2082. leading dots.
  2083. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2084. the sublevels of a headline matching a tag search often also match
  2085. the same search. Listing all of them can create very long lists.
  2086. Setting this variable to nil causes subtrees of a match to be skipped.
  2087. This variable is semi-obsolete and probably should always be true. It
  2088. is better to limit inheritance to certain tags using the variables
  2089. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2090. :group 'org-tags
  2091. :type '(choice
  2092. (const :tag "No, don't list them" nil)
  2093. (const :tag "Yes, do list them" t)
  2094. (const :tag "List them, indented with leading dots" indented)))
  2095. (defcustom org-tags-sort-function nil
  2096. "When set, tags are sorted using this function as a comparator"
  2097. :group 'org-tags
  2098. :type '(choice
  2099. (const :tag "No sorting" nil)
  2100. (const :tag "Alphabetical" string<)
  2101. (const :tag "Reverse alphabetical" string>)
  2102. (function :tag "Custom function" nil)))
  2103. (defvar org-tags-history nil
  2104. "History of minibuffer reads for tags.")
  2105. (defvar org-last-tags-completion-table nil
  2106. "The last used completion table for tags.")
  2107. (defvar org-after-tags-change-hook nil
  2108. "Hook that is run after the tags in a line have changed.")
  2109. (defgroup org-properties nil
  2110. "Options concerning properties in Org-mode."
  2111. :tag "Org Properties"
  2112. :group 'org)
  2113. (defcustom org-property-format "%-10s %s"
  2114. "How property key/value pairs should be formatted by `indent-line'.
  2115. When `indent-line' hits a property definition, it will format the line
  2116. according to this format, mainly to make sure that the values are
  2117. lined-up with respect to each other."
  2118. :group 'org-properties
  2119. :type 'string)
  2120. (defcustom org-use-property-inheritance nil
  2121. "Non-nil means, properties apply also for sublevels.
  2122. This setting is chiefly used during property searches. Turning it on can
  2123. cause significant overhead when doing a search, which is why it is not
  2124. on by default.
  2125. When nil, only the properties directly given in the current entry count.
  2126. When t, every property is inherited. The value may also be a list of
  2127. properties that should have inheritance, or a regular expression matching
  2128. properties that should be inherited.
  2129. However, note that some special properties use inheritance under special
  2130. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2131. and the properties ending in \"_ALL\" when they are used as descriptor
  2132. for valid values of a property.
  2133. Note for programmers:
  2134. When querying an entry with `org-entry-get', you can control if inheritance
  2135. should be used. By default, `org-entry-get' looks only at the local
  2136. properties. You can request inheritance by setting the inherit argument
  2137. to t (to force inheritance) or to `selective' (to respect the setting
  2138. in this variable)."
  2139. :group 'org-properties
  2140. :type '(choice
  2141. (const :tag "Not" nil)
  2142. (const :tag "Always" t)
  2143. (repeat :tag "Specific properties" (string :tag "Property"))
  2144. (regexp :tag "Properties matched by regexp")))
  2145. (defun org-property-inherit-p (property)
  2146. "Check if PROPERTY is one that should be inherited."
  2147. (cond
  2148. ((eq org-use-property-inheritance t) t)
  2149. ((not org-use-property-inheritance) nil)
  2150. ((stringp org-use-property-inheritance)
  2151. (string-match org-use-property-inheritance property))
  2152. ((listp org-use-property-inheritance)
  2153. (member property org-use-property-inheritance))
  2154. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2155. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2156. "The default column format, if no other format has been defined.
  2157. This variable can be set on the per-file basis by inserting a line
  2158. #+COLUMNS: %25ITEM ....."
  2159. :group 'org-properties
  2160. :type 'string)
  2161. (defcustom org-columns-ellipses ".."
  2162. "The ellipses to be used when a field in column view is truncated.
  2163. When this is the empty string, as many characters as possible are shown,
  2164. but then there will be no visual indication that the field has been truncated.
  2165. When this is a string of length N, the last N characters of a truncated
  2166. field are replaced by this string. If the column is narrower than the
  2167. ellipses string, only part of the ellipses string will be shown."
  2168. :group 'org-properties
  2169. :type 'string)
  2170. (defcustom org-columns-modify-value-for-display-function nil
  2171. "Function that modifies values for display in column view.
  2172. For example, it can be used to cut out a certain part from a time stamp.
  2173. The function must take 2 arguments:
  2174. column-title The title of the column (*not* the property name)
  2175. value The value that should be modified.
  2176. The function should return the value that should be displayed,
  2177. or nil if the normal value should be used."
  2178. :group 'org-properties
  2179. :type 'function)
  2180. (defcustom org-effort-property "Effort"
  2181. "The property that is being used to keep track of effort estimates.
  2182. Effort estimates given in this property need to have the format H:MM."
  2183. :group 'org-properties
  2184. :group 'org-progress
  2185. :type '(string :tag "Property"))
  2186. (defconst org-global-properties-fixed
  2187. '(("VISIBILITY_ALL" . "folded children content all")
  2188. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2189. "List of property/value pairs that can be inherited by any entry.
  2190. These are fixed values, for the preset properties. The user variable
  2191. that can be used to add to this list is `org-global-properties'.
  2192. The entries in this list are cons cells where the car is a property
  2193. name and cdr is a string with the value. If the value represents
  2194. multiple items like an \"_ALL\" property, separate the items by
  2195. spaces.")
  2196. (defcustom org-global-properties nil
  2197. "List of property/value pairs that can be inherited by any entry.
  2198. This list will be combined with the constant `org-global-properties-fixed'.
  2199. The entries in this list are cons cells where the car is a property
  2200. name and cdr is a string with the value.
  2201. You can set buffer-local values for the same purpose in the variable
  2202. `org-file-properties' this by adding lines like
  2203. #+PROPERTY: NAME VALUE"
  2204. :group 'org-properties
  2205. :type '(repeat
  2206. (cons (string :tag "Property")
  2207. (string :tag "Value"))))
  2208. (defvar org-file-properties nil
  2209. "List of property/value pairs that can be inherited by any entry.
  2210. Valid for the current buffer.
  2211. This variable is populated from #+PROPERTY lines.")
  2212. (make-variable-buffer-local 'org-file-properties)
  2213. (defgroup org-agenda nil
  2214. "Options concerning agenda views in Org-mode."
  2215. :tag "Org Agenda"
  2216. :group 'org)
  2217. (defvar org-category nil
  2218. "Variable used by org files to set a category for agenda display.
  2219. Such files should use a file variable to set it, for example
  2220. # -*- mode: org; org-category: \"ELisp\"
  2221. or contain a special line
  2222. #+CATEGORY: ELisp
  2223. If the file does not specify a category, then file's base name
  2224. is used instead.")
  2225. (make-variable-buffer-local 'org-category)
  2226. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2227. (defcustom org-agenda-files nil
  2228. "The files to be used for agenda display.
  2229. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2230. \\[org-remove-file]. You can also use customize to edit the list.
  2231. If an entry is a directory, all files in that directory that are matched by
  2232. `org-agenda-file-regexp' will be part of the file list.
  2233. If the value of the variable is not a list but a single file name, then
  2234. the list of agenda files is actually stored and maintained in that file, one
  2235. agenda file per line."
  2236. :group 'org-agenda
  2237. :type '(choice
  2238. (repeat :tag "List of files and directories" file)
  2239. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2240. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2241. "Regular expression to match files for `org-agenda-files'.
  2242. If any element in the list in that variable contains a directory instead
  2243. of a normal file, all files in that directory that are matched by this
  2244. regular expression will be included."
  2245. :group 'org-agenda
  2246. :type 'regexp)
  2247. (defcustom org-agenda-text-search-extra-files nil
  2248. "List of extra files to be searched by text search commands.
  2249. These files will be search in addition to the agenda files by the
  2250. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2251. Note that these files will only be searched for text search commands,
  2252. not for the other agenda views like todo lists, tag searches or the weekly
  2253. agenda. This variable is intended to list notes and possibly archive files
  2254. that should also be searched by these two commands.
  2255. In fact, if the first element in the list is the symbol `agenda-archives',
  2256. than all archive files of all agenda files will be added to the search
  2257. scope."
  2258. :group 'org-agenda
  2259. :type '(set :greedy t
  2260. (const :tag "Agenda Archives" agenda-archives)
  2261. (repeat :inline t (file))))
  2262. (if (fboundp 'defvaralias)
  2263. (defvaralias 'org-agenda-multi-occur-extra-files
  2264. 'org-agenda-text-search-extra-files))
  2265. (defcustom org-agenda-skip-unavailable-files nil
  2266. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2267. A nil value means to remove them, after a query, from the list."
  2268. :group 'org-agenda
  2269. :type 'boolean)
  2270. (defcustom org-calendar-to-agenda-key [?c]
  2271. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2272. The command `org-calendar-goto-agenda' will be bound to this key. The
  2273. default is the character `c' because then `c' can be used to switch back and
  2274. forth between agenda and calendar."
  2275. :group 'org-agenda
  2276. :type 'sexp)
  2277. (defcustom org-calendar-agenda-action-key [?k]
  2278. "The key to be installed in `calendar-mode-map' for agenda-action.
  2279. The command `org-agenda-action' will be bound to this key. The
  2280. default is the character `k' because we use the same key in the agenda."
  2281. :group 'org-agenda
  2282. :type 'sexp)
  2283. (eval-after-load "calendar"
  2284. '(progn
  2285. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2286. 'org-calendar-goto-agenda)
  2287. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2288. 'org-agenda-action)))
  2289. (defgroup org-latex nil
  2290. "Options for embedding LaTeX code into Org-mode."
  2291. :tag "Org LaTeX"
  2292. :group 'org)
  2293. (defcustom org-format-latex-options
  2294. '(:foreground default :background default :scale 1.0
  2295. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2296. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2297. "Options for creating images from LaTeX fragments.
  2298. This is a property list with the following properties:
  2299. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2300. `default' means use the foreground of the default face.
  2301. :background the background color, or \"Transparent\".
  2302. `default' means use the background of the default face.
  2303. :scale a scaling factor for the size of the images.
  2304. :html-foreground, :html-background, :html-scale
  2305. the same numbers for HTML export.
  2306. :matchers a list indicating which matchers should be used to
  2307. find LaTeX fragments. Valid members of this list are:
  2308. \"begin\" find environments
  2309. \"$1\" find single characters surrounded by $.$
  2310. \"$\" find math expressions surrounded by $...$
  2311. \"$$\" find math expressions surrounded by $$....$$
  2312. \"\\(\" find math expressions surrounded by \\(...\\)
  2313. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2314. :group 'org-latex
  2315. :type 'plist)
  2316. (defcustom org-format-latex-header "\\documentclass{article}
  2317. \\usepackage{fullpage} % do not remove
  2318. \\usepackage{amssymb}
  2319. \\usepackage[usenames]{color}
  2320. \\usepackage{amsmath}
  2321. \\usepackage{latexsym}
  2322. \\usepackage[mathscr]{eucal}
  2323. \\pagestyle{empty} % do not remove"
  2324. "The document header used for processing LaTeX fragments."
  2325. :group 'org-latex
  2326. :type 'string)
  2327. (defgroup org-font-lock nil
  2328. "Font-lock settings for highlighting in Org-mode."
  2329. :tag "Org Font Lock"
  2330. :group 'org)
  2331. (defcustom org-level-color-stars-only nil
  2332. "Non-nil means fontify only the stars in each headline.
  2333. When nil, the entire headline is fontified.
  2334. Changing it requires restart of `font-lock-mode' to become effective
  2335. also in regions already fontified."
  2336. :group 'org-font-lock
  2337. :type 'boolean)
  2338. (defcustom org-hide-leading-stars nil
  2339. "Non-nil means, hide the first N-1 stars in a headline.
  2340. This works by using the face `org-hide' for these stars. This
  2341. face is white for a light background, and black for a dark
  2342. background. You may have to customize the face `org-hide' to
  2343. make this work.
  2344. Changing it requires restart of `font-lock-mode' to become effective
  2345. also in regions already fontified.
  2346. You may also set this on a per-file basis by adding one of the following
  2347. lines to the buffer:
  2348. #+STARTUP: hidestars
  2349. #+STARTUP: showstars"
  2350. :group 'org-font-lock
  2351. :type 'boolean)
  2352. (defcustom org-fontify-done-headline nil
  2353. "Non-nil means, change the face of a headline if it is marked DONE.
  2354. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2355. When this is non-nil, the headline after the keyword is set to the
  2356. `org-headline-done' as an additional indication."
  2357. :group 'org-font-lock
  2358. :type 'boolean)
  2359. (defcustom org-fontify-emphasized-text t
  2360. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2361. Changing this variable requires a restart of Emacs to take effect."
  2362. :group 'org-font-lock
  2363. :type 'boolean)
  2364. (defcustom org-fontify-whole-heading-line nil
  2365. "Non-nil means fontify the whole line for headings.
  2366. This is useful when setting a background color for the
  2367. org-leve-* faces."
  2368. :group 'org-font-lock
  2369. :type 'boolean)
  2370. (defcustom org-highlight-latex-fragments-and-specials nil
  2371. "Non-nil means, fontify what is treated specially by the exporters."
  2372. :group 'org-font-lock
  2373. :type 'boolean)
  2374. (defcustom org-hide-emphasis-markers nil
  2375. "Non-nil mean font-lock should hide the emphasis marker characters."
  2376. :group 'org-font-lock
  2377. :type 'boolean)
  2378. (defvar org-emph-re nil
  2379. "Regular expression for matching emphasis.")
  2380. (defvar org-verbatim-re nil
  2381. "Regular expression for matching verbatim text.")
  2382. (defvar org-emphasis-regexp-components) ; defined just below
  2383. (defvar org-emphasis-alist) ; defined just below
  2384. (defun org-set-emph-re (var val)
  2385. "Set variable and compute the emphasis regular expression."
  2386. (set var val)
  2387. (when (and (boundp 'org-emphasis-alist)
  2388. (boundp 'org-emphasis-regexp-components)
  2389. org-emphasis-alist org-emphasis-regexp-components)
  2390. (let* ((e org-emphasis-regexp-components)
  2391. (pre (car e))
  2392. (post (nth 1 e))
  2393. (border (nth 2 e))
  2394. (body (nth 3 e))
  2395. (nl (nth 4 e))
  2396. (body1 (concat body "*?"))
  2397. (markers (mapconcat 'car org-emphasis-alist ""))
  2398. (vmarkers (mapconcat
  2399. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2400. org-emphasis-alist "")))
  2401. ;; make sure special characters appear at the right position in the class
  2402. (if (string-match "\\^" markers)
  2403. (setq markers (concat (replace-match "" t t markers) "^")))
  2404. (if (string-match "-" markers)
  2405. (setq markers (concat (replace-match "" t t markers) "-")))
  2406. (if (string-match "\\^" vmarkers)
  2407. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2408. (if (string-match "-" vmarkers)
  2409. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2410. (if (> nl 0)
  2411. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2412. (int-to-string nl) "\\}")))
  2413. ;; Make the regexp
  2414. (setq org-emph-re
  2415. (concat "\\([" pre "]\\|^\\)"
  2416. "\\("
  2417. "\\([" markers "]\\)"
  2418. "\\("
  2419. "[^" border "]\\|"
  2420. "[^" border "]"
  2421. body1
  2422. "[^" border "]"
  2423. "\\)"
  2424. "\\3\\)"
  2425. "\\([" post "]\\|$\\)"))
  2426. (setq org-verbatim-re
  2427. (concat "\\([" pre "]\\|^\\)"
  2428. "\\("
  2429. "\\([" vmarkers "]\\)"
  2430. "\\("
  2431. "[^" border "]\\|"
  2432. "[^" border "]"
  2433. body1
  2434. "[^" border "]"
  2435. "\\)"
  2436. "\\3\\)"
  2437. "\\([" post "]\\|$\\)")))))
  2438. (defcustom org-emphasis-regexp-components
  2439. '(" \t('`\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2440. "Components used to build the regular expression for emphasis.
  2441. This is a list with 6 entries. Terminology: In an emphasis string
  2442. like \" *strong word* \", we call the initial space PREMATCH, the final
  2443. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2444. and \"trong wor\" is the body. The different components in this variable
  2445. specify what is allowed/forbidden in each part:
  2446. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2447. post Chars allowed as postmatch. End of line will be allowed too.
  2448. border The chars *forbidden* as border characters.
  2449. body-regexp A regexp like \".\" to match a body character. Don't use
  2450. non-shy groups here, and don't allow newline here.
  2451. newline The maximum number of newlines allowed in an emphasis exp.
  2452. Use customize to modify this, or restart Emacs after changing it."
  2453. :group 'org-font-lock
  2454. :set 'org-set-emph-re
  2455. :type '(list
  2456. (sexp :tag "Allowed chars in pre ")
  2457. (sexp :tag "Allowed chars in post ")
  2458. (sexp :tag "Forbidden chars in border ")
  2459. (sexp :tag "Regexp for body ")
  2460. (integer :tag "number of newlines allowed")
  2461. (option (boolean :tag "Please ignore this button"))))
  2462. (defcustom org-emphasis-alist
  2463. `(("*" bold "<b>" "</b>")
  2464. ("/" italic "<i>" "</i>")
  2465. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2466. ("=" org-code "<code>" "</code>" verbatim)
  2467. ("~" org-verbatim "<code>" "</code>" verbatim)
  2468. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2469. "<del>" "</del>")
  2470. )
  2471. "Special syntax for emphasized text.
  2472. Text starting and ending with a special character will be emphasized, for
  2473. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2474. characters, the face to be used by font-lock for highlighting in Org-mode
  2475. Emacs buffers, and the HTML tags to be used for this.
  2476. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2477. Use customize to modify this, or restart Emacs after changing it."
  2478. :group 'org-font-lock
  2479. :set 'org-set-emph-re
  2480. :type '(repeat
  2481. (list
  2482. (string :tag "Marker character")
  2483. (choice
  2484. (face :tag "Font-lock-face")
  2485. (plist :tag "Face property list"))
  2486. (string :tag "HTML start tag")
  2487. (string :tag "HTML end tag")
  2488. (option (const verbatim)))))
  2489. (defvar org-protecting-blocks
  2490. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2491. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2492. This is needed for font-lock setup.")
  2493. ;;; Miscellaneous options
  2494. (defgroup org-completion nil
  2495. "Completion in Org-mode."
  2496. :tag "Org Completion"
  2497. :group 'org)
  2498. (defcustom org-completion-use-ido nil
  2499. "Non-nil means, use ido completion wherever possible.
  2500. Note that `ido-mode' must be active for this variable to be relevant.
  2501. If you decide to turn this variable on, you might well want to turn off
  2502. `org-outline-path-complete-in-steps'.
  2503. See also `org-completion-use-iswitchb'."
  2504. :group 'org-completion
  2505. :type 'boolean)
  2506. (defcustom org-completion-use-iswitchb nil
  2507. "Non-nil means, use iswitchb completion wherever possible.
  2508. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2509. If you decide to turn this variable on, you might well want to turn off
  2510. `org-outline-path-complete-in-steps'.
  2511. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2512. :group 'org-completion
  2513. :type 'boolean)
  2514. (defcustom org-completion-fallback-command 'hippie-expand
  2515. "The expansion command called by \\[org-complete] in normal context.
  2516. Normal means, no org-mode-specific context."
  2517. :group 'org-completion
  2518. :type 'function)
  2519. ;;; Functions and variables from ther packages
  2520. ;; Declared here to avoid compiler warnings
  2521. ;; XEmacs only
  2522. (defvar outline-mode-menu-heading)
  2523. (defvar outline-mode-menu-show)
  2524. (defvar outline-mode-menu-hide)
  2525. (defvar zmacs-regions) ; XEmacs regions
  2526. ;; Emacs only
  2527. (defvar mark-active)
  2528. ;; Various packages
  2529. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2530. (declare-function calendar-forward-day "cal-move" (arg))
  2531. (declare-function calendar-goto-date "cal-move" (date))
  2532. (declare-function calendar-goto-today "cal-move" ())
  2533. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2534. (defvar calc-embedded-close-formula)
  2535. (defvar calc-embedded-open-formula)
  2536. (declare-function cdlatex-tab "ext:cdlatex" ())
  2537. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2538. (defvar font-lock-unfontify-region-function)
  2539. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2540. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2541. (defvar iswitchb-temp-buflist)
  2542. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2543. (defvar org-agenda-tags-todo-honor-ignore-options)
  2544. (declare-function org-agenda-skip "org-agenda" ())
  2545. (declare-function org-format-agenda-item "org-agenda"
  2546. (extra txt &optional category tags dotime noprefix remove-re))
  2547. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2548. (declare-function org-agenda-change-all-lines "org-agenda"
  2549. (newhead hdmarker &optional fixface just-this))
  2550. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2551. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2552. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2553. (beg end))
  2554. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2555. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2556. "org-agenda" (&optional end))
  2557. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2558. (declare-function org-indent-mode "org-indent" (arg))
  2559. (declare-function parse-time-string "parse-time" (string))
  2560. (declare-function remember "remember" (&optional initial))
  2561. (declare-function remember-buffer-desc "remember" ())
  2562. (declare-function remember-finalize "remember" ())
  2563. (defvar remember-save-after-remembering)
  2564. (defvar remember-data-file)
  2565. (defvar remember-register)
  2566. (defvar remember-buffer)
  2567. (defvar remember-handler-functions)
  2568. (defvar remember-annotation-functions)
  2569. (defvar texmathp-why)
  2570. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2571. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2572. (defvar w3m-current-url)
  2573. (defvar w3m-current-title)
  2574. (defvar org-latex-regexps)
  2575. ;;; Autoload and prepare some org modules
  2576. ;; Some table stuff that needs to be defined here, because it is used
  2577. ;; by the functions setting up org-mode or checking for table context.
  2578. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2579. "Detects an org-type or table-type table.")
  2580. (defconst org-table-line-regexp "^[ \t]*|"
  2581. "Detects an org-type table line.")
  2582. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2583. "Detects an org-type table line.")
  2584. (defconst org-table-hline-regexp "^[ \t]*|-"
  2585. "Detects an org-type table hline.")
  2586. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2587. "Detects a table-type table hline.")
  2588. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2589. "Searching from within a table (any type) this finds the first line
  2590. outside the table.")
  2591. ;; Autoload the functions in org-table.el that are needed by functions here.
  2592. (eval-and-compile
  2593. (org-autoload "org-table"
  2594. '(org-table-align org-table-begin org-table-blank-field
  2595. org-table-convert org-table-convert-region org-table-copy-down
  2596. org-table-copy-region org-table-create
  2597. org-table-create-or-convert-from-region
  2598. org-table-create-with-table.el org-table-current-dline
  2599. org-table-cut-region org-table-delete-column org-table-edit-field
  2600. org-table-edit-formulas org-table-end org-table-eval-formula
  2601. org-table-export org-table-field-info
  2602. org-table-get-stored-formulas org-table-goto-column
  2603. org-table-hline-and-move org-table-import org-table-insert-column
  2604. org-table-insert-hline org-table-insert-row org-table-iterate
  2605. org-table-justify-field-maybe org-table-kill-row
  2606. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2607. org-table-move-column org-table-move-column-left
  2608. org-table-move-column-right org-table-move-row
  2609. org-table-move-row-down org-table-move-row-up
  2610. org-table-next-field org-table-next-row org-table-paste-rectangle
  2611. org-table-previous-field org-table-recalculate
  2612. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2613. org-table-toggle-coordinate-overlays
  2614. org-table-toggle-formula-debugger org-table-wrap-region
  2615. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2616. (defun org-at-table-p (&optional table-type)
  2617. "Return t if the cursor is inside an org-type table.
  2618. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2619. (if org-enable-table-editor
  2620. (save-excursion
  2621. (beginning-of-line 1)
  2622. (looking-at (if table-type org-table-any-line-regexp
  2623. org-table-line-regexp)))
  2624. nil))
  2625. (defsubst org-table-p () (org-at-table-p))
  2626. (defun org-at-table.el-p ()
  2627. "Return t if and only if we are at a table.el table."
  2628. (and (org-at-table-p 'any)
  2629. (save-excursion
  2630. (goto-char (org-table-begin 'any))
  2631. (looking-at org-table1-hline-regexp))))
  2632. (defun org-table-recognize-table.el ()
  2633. "If there is a table.el table nearby, recognize it and move into it."
  2634. (if org-table-tab-recognizes-table.el
  2635. (if (org-at-table.el-p)
  2636. (progn
  2637. (beginning-of-line 1)
  2638. (if (looking-at org-table-dataline-regexp)
  2639. nil
  2640. (if (looking-at org-table1-hline-regexp)
  2641. (progn
  2642. (beginning-of-line 2)
  2643. (if (looking-at org-table-any-border-regexp)
  2644. (beginning-of-line -1)))))
  2645. (if (re-search-forward "|" (org-table-end t) t)
  2646. (progn
  2647. (require 'table)
  2648. (if (table--at-cell-p (point))
  2649. t
  2650. (message "recognizing table.el table...")
  2651. (table-recognize-table)
  2652. (message "recognizing table.el table...done")))
  2653. (error "This should not happen..."))
  2654. t)
  2655. nil)
  2656. nil))
  2657. (defun org-at-table-hline-p ()
  2658. "Return t if the cursor is inside a hline in a table."
  2659. (if org-enable-table-editor
  2660. (save-excursion
  2661. (beginning-of-line 1)
  2662. (looking-at org-table-hline-regexp))
  2663. nil))
  2664. (defvar org-table-clean-did-remove-column nil)
  2665. (defun org-table-map-tables (function)
  2666. "Apply FUNCTION to the start of all tables in the buffer."
  2667. (save-excursion
  2668. (save-restriction
  2669. (widen)
  2670. (goto-char (point-min))
  2671. (while (re-search-forward org-table-any-line-regexp nil t)
  2672. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2673. (beginning-of-line 1)
  2674. (when (looking-at org-table-line-regexp)
  2675. (save-excursion (funcall function))
  2676. (or (looking-at org-table-line-regexp)
  2677. (forward-char 1)))
  2678. (re-search-forward org-table-any-border-regexp nil 1))))
  2679. (message "Mapping tables: done"))
  2680. ;; Declare and autoload functions from org-exp.el & Co
  2681. (declare-function org-default-export-plist "org-exp")
  2682. (declare-function org-infile-export-plist "org-exp")
  2683. (declare-function org-get-current-options "org-exp")
  2684. (eval-and-compile
  2685. (org-autoload "org-exp"
  2686. '(org-export org-export-visible
  2687. org-insert-export-options-template
  2688. org-table-clean-before-export))
  2689. (org-autoload "org-ascii"
  2690. '(org-export-as-ascii org-export-ascii-preprocess
  2691. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2692. org-export-region-as-ascii))
  2693. (org-autoload "org-html"
  2694. '(org-export-as-html-and-open
  2695. org-export-as-html-batch org-export-as-html-to-buffer
  2696. org-replace-region-by-html org-export-region-as-html
  2697. org-export-as-html))
  2698. (org-autoload "org-icalendar"
  2699. '(org-export-icalendar-this-file
  2700. org-export-icalendar-all-agenda-files
  2701. org-export-icalendar-combine-agenda-files))
  2702. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2703. ;; Declare and autoload functions from org-agenda.el
  2704. (eval-and-compile
  2705. (org-autoload "org-agenda"
  2706. '(org-agenda org-agenda-list org-search-view
  2707. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2708. org-diary org-agenda-to-appt
  2709. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2710. ;; Autoload org-remember
  2711. (eval-and-compile
  2712. (org-autoload "org-remember"
  2713. '(org-remember-insinuate org-remember-annotation
  2714. org-remember-apply-template org-remember org-remember-handler)))
  2715. ;; Autoload org-clock.el
  2716. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2717. (beg end))
  2718. (declare-function org-clock-update-mode-line "org-clock" ())
  2719. (defvar org-clock-start-time)
  2720. (defvar org-clock-marker (make-marker)
  2721. "Marker recording the last clock-in.")
  2722. (defvar org-clock-hd-marker (make-marker)
  2723. "Marker recording the last clock-in, but the headline position.")
  2724. (defun org-clock-is-active ()
  2725. "Return non-nil if clock is currently running.
  2726. The return value is actually the clock marker."
  2727. (marker-buffer org-clock-marker))
  2728. (eval-and-compile
  2729. (org-autoload
  2730. "org-clock"
  2731. '(org-clock-in org-clock-out org-clock-cancel
  2732. org-clock-goto org-clock-sum org-clock-display
  2733. org-clock-remove-overlays org-clock-report
  2734. org-clocktable-shift org-dblock-write:clocktable
  2735. org-get-clocktable)))
  2736. (defun org-clock-update-time-maybe ()
  2737. "If this is a CLOCK line, update it and return t.
  2738. Otherwise, return nil."
  2739. (interactive)
  2740. (save-excursion
  2741. (beginning-of-line 1)
  2742. (skip-chars-forward " \t")
  2743. (when (looking-at org-clock-string)
  2744. (let ((re (concat "[ \t]*" org-clock-string
  2745. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2746. "\\([ \t]*=>.*\\)?\\)?"))
  2747. ts te h m s neg)
  2748. (cond
  2749. ((not (looking-at re))
  2750. nil)
  2751. ((not (match-end 2))
  2752. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2753. (> org-clock-marker (point))
  2754. (<= org-clock-marker (point-at-eol)))
  2755. ;; The clock is running here
  2756. (setq org-clock-start-time
  2757. (apply 'encode-time
  2758. (org-parse-time-string (match-string 1))))
  2759. (org-clock-update-mode-line)))
  2760. (t
  2761. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2762. (end-of-line 1)
  2763. (setq ts (match-string 1)
  2764. te (match-string 3))
  2765. (setq s (- (time-to-seconds
  2766. (apply 'encode-time (org-parse-time-string te)))
  2767. (time-to-seconds
  2768. (apply 'encode-time (org-parse-time-string ts))))
  2769. neg (< s 0)
  2770. s (abs s)
  2771. h (floor (/ s 3600))
  2772. s (- s (* 3600 h))
  2773. m (floor (/ s 60))
  2774. s (- s (* 60 s)))
  2775. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2776. t))))))
  2777. (defun org-check-running-clock ()
  2778. "Check if the current buffer contains the running clock.
  2779. If yes, offer to stop it and to save the buffer with the changes."
  2780. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2781. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2782. (buffer-name))))
  2783. (org-clock-out)
  2784. (when (y-or-n-p "Save changed buffer?")
  2785. (save-buffer))))
  2786. (defun org-clocktable-try-shift (dir n)
  2787. "Check if this line starts a clock table, if yes, shift the time block."
  2788. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2789. (org-clocktable-shift dir n)))
  2790. ;; Autoload org-timer.el
  2791. (eval-and-compile
  2792. (org-autoload
  2793. "org-timer"
  2794. '(org-timer-start org-timer org-timer-item
  2795. org-timer-change-times-in-region
  2796. org-timer-set-timer
  2797. org-timer-reset-timers
  2798. org-timer-show-remaining-time)))
  2799. ;; Autoload org-feed.el
  2800. (eval-and-compile
  2801. (org-autoload
  2802. "org-feed"
  2803. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2804. ;; Autoload org-indent.el
  2805. (eval-and-compile
  2806. (org-autoload
  2807. "org-indent"
  2808. '(org-indent-mode)))
  2809. ;; Autoload archiving code
  2810. ;; The stuff that is needed for cycling and tags has to be defined here.
  2811. (defgroup org-archive nil
  2812. "Options concerning archiving in Org-mode."
  2813. :tag "Org Archive"
  2814. :group 'org-structure)
  2815. (defcustom org-archive-location "%s_archive::"
  2816. "The location where subtrees should be archived.
  2817. The value of this variable is a string, consisting of two parts,
  2818. separated by a double-colon. The first part is a filename and
  2819. the second part is a headline.
  2820. When the filename is omitted, archiving happens in the same file.
  2821. %s in the filename will be replaced by the current file
  2822. name (without the directory part). Archiving to a different file
  2823. is useful to keep archived entries from contributing to the
  2824. Org-mode Agenda.
  2825. The archived entries will be filed as subtrees of the specified
  2826. headline. When the headline is omitted, the subtrees are simply
  2827. filed away at the end of the file, as top-level entries. Also in
  2828. the heading you can use %s to represent the file name, this can be
  2829. useful when using the same archive for a number of different files.
  2830. Here are a few examples:
  2831. \"%s_archive::\"
  2832. If the current file is Projects.org, archive in file
  2833. Projects.org_archive, as top-level trees. This is the default.
  2834. \"::* Archived Tasks\"
  2835. Archive in the current file, under the top-level headline
  2836. \"* Archived Tasks\".
  2837. \"~/org/archive.org::\"
  2838. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2839. \"~/org/archive.org::From %s\"
  2840. Archive in file ~/org/archive.org (absolute path), und headlines
  2841. \"From FILENAME\" where file name is the current file name.
  2842. \"basement::** Finished Tasks\"
  2843. Archive in file ./basement (relative path), as level 3 trees
  2844. below the level 2 heading \"** Finished Tasks\".
  2845. You may set this option on a per-file basis by adding to the buffer a
  2846. line like
  2847. #+ARCHIVE: basement::** Finished Tasks
  2848. You may also define it locally for a subtree by setting an ARCHIVE property
  2849. in the entry. If such a property is found in an entry, or anywhere up
  2850. the hierarchy, it will be used."
  2851. :group 'org-archive
  2852. :type 'string)
  2853. (defcustom org-archive-tag "ARCHIVE"
  2854. "The tag that marks a subtree as archived.
  2855. An archived subtree does not open during visibility cycling, and does
  2856. not contribute to the agenda listings.
  2857. After changing this, font-lock must be restarted in the relevant buffers to
  2858. get the proper fontification."
  2859. :group 'org-archive
  2860. :group 'org-keywords
  2861. :type 'string)
  2862. (defcustom org-agenda-skip-archived-trees t
  2863. "Non-nil means, the agenda will skip any items located in archived trees.
  2864. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2865. variable is no longer recommended, you should leave it at the value t.
  2866. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2867. :group 'org-archive
  2868. :group 'org-agenda-skip
  2869. :type 'boolean)
  2870. (defcustom org-columns-skip-arrchived-trees t
  2871. "Non-nil means, irgnore archived trees when creating column view."
  2872. :group 'org-archive
  2873. :group 'org-properties
  2874. :type 'boolean)
  2875. (defcustom org-cycle-open-archived-trees nil
  2876. "Non-nil means, `org-cycle' will open archived trees.
  2877. An archived tree is a tree marked with the tag ARCHIVE.
  2878. When nil, archived trees will stay folded. You can still open them with
  2879. normal outline commands like `show-all', but not with the cycling commands."
  2880. :group 'org-archive
  2881. :group 'org-cycle
  2882. :type 'boolean)
  2883. (defcustom org-sparse-tree-open-archived-trees nil
  2884. "Non-nil means sparse tree construction shows matches in archived trees.
  2885. When nil, matches in these trees are highlighted, but the trees are kept in
  2886. collapsed state."
  2887. :group 'org-archive
  2888. :group 'org-sparse-trees
  2889. :type 'boolean)
  2890. (defun org-cycle-hide-archived-subtrees (state)
  2891. "Re-hide all archived subtrees after a visibility state change."
  2892. (when (and (not org-cycle-open-archived-trees)
  2893. (not (memq state '(overview folded))))
  2894. (save-excursion
  2895. (let* ((globalp (memq state '(contents all)))
  2896. (beg (if globalp (point-min) (point)))
  2897. (end (if globalp (point-max) (org-end-of-subtree t))))
  2898. (org-hide-archived-subtrees beg end)
  2899. (goto-char beg)
  2900. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2901. (message "%s" (substitute-command-keys
  2902. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2903. (defun org-force-cycle-archived ()
  2904. "Cycle subtree even if it is archived."
  2905. (interactive)
  2906. (setq this-command 'org-cycle)
  2907. (let ((org-cycle-open-archived-trees t))
  2908. (call-interactively 'org-cycle)))
  2909. (defun org-hide-archived-subtrees (beg end)
  2910. "Re-hide all archived subtrees after a visibility state change."
  2911. (save-excursion
  2912. (let* ((re (concat ":" org-archive-tag ":")))
  2913. (goto-char beg)
  2914. (while (re-search-forward re end t)
  2915. (and (org-on-heading-p) (hide-subtree))
  2916. (org-end-of-subtree t)))))
  2917. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2918. (eval-and-compile
  2919. (org-autoload "org-archive"
  2920. '(org-add-archive-files org-archive-subtree
  2921. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2922. ;; Autoload Column View Code
  2923. (declare-function org-columns-number-to-string "org-colview")
  2924. (declare-function org-columns-get-format-and-top-level "org-colview")
  2925. (declare-function org-columns-compute "org-colview")
  2926. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2927. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2928. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2929. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2930. ;; Autoload ID code
  2931. (declare-function org-id-store-link "org-id")
  2932. (declare-function org-id-locations-load "org-id")
  2933. (declare-function org-id-locations-save "org-id")
  2934. (defvar org-id-track-globally)
  2935. (org-autoload "org-id"
  2936. '(org-id-get-create org-id-new org-id-copy org-id-get
  2937. org-id-get-with-outline-path-completion
  2938. org-id-get-with-outline-drilling
  2939. org-id-goto org-id-find org-id-store-link))
  2940. ;; Autoload Plotting Code
  2941. (org-autoload "org-plot"
  2942. '(org-plot/gnuplot))
  2943. ;;; Variables for pre-computed regular expressions, all buffer local
  2944. (defvar org-drawer-regexp nil
  2945. "Matches first line of a hidden block.")
  2946. (make-variable-buffer-local 'org-drawer-regexp)
  2947. (defvar org-todo-regexp nil
  2948. "Matches any of the TODO state keywords.")
  2949. (make-variable-buffer-local 'org-todo-regexp)
  2950. (defvar org-not-done-regexp nil
  2951. "Matches any of the TODO state keywords except the last one.")
  2952. (make-variable-buffer-local 'org-not-done-regexp)
  2953. (defvar org-not-done-heading-regexp nil
  2954. "Matches a TODO headline that is not done.")
  2955. (make-variable-buffer-local 'org-not-done-regexp)
  2956. (defvar org-todo-line-regexp nil
  2957. "Matches a headline and puts TODO state into group 2 if present.")
  2958. (make-variable-buffer-local 'org-todo-line-regexp)
  2959. (defvar org-complex-heading-regexp nil
  2960. "Matches a headline and puts everything into groups:
  2961. group 1: the stars
  2962. group 2: The todo keyword, maybe
  2963. group 3: Priority cookie
  2964. group 4: True headline
  2965. group 5: Tags")
  2966. (make-variable-buffer-local 'org-complex-heading-regexp)
  2967. (defvar org-todo-line-tags-regexp nil
  2968. "Matches a headline and puts TODO state into group 2 if present.
  2969. Also put tags into group 4 if tags are present.")
  2970. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2971. (defvar org-nl-done-regexp nil
  2972. "Matches newline followed by a headline with the DONE keyword.")
  2973. (make-variable-buffer-local 'org-nl-done-regexp)
  2974. (defvar org-looking-at-done-regexp nil
  2975. "Matches the DONE keyword a point.")
  2976. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2977. (defvar org-ds-keyword-length 12
  2978. "Maximum length of the Deadline and SCHEDULED keywords.")
  2979. (make-variable-buffer-local 'org-ds-keyword-length)
  2980. (defvar org-deadline-regexp nil
  2981. "Matches the DEADLINE keyword.")
  2982. (make-variable-buffer-local 'org-deadline-regexp)
  2983. (defvar org-deadline-time-regexp nil
  2984. "Matches the DEADLINE keyword together with a time stamp.")
  2985. (make-variable-buffer-local 'org-deadline-time-regexp)
  2986. (defvar org-deadline-line-regexp nil
  2987. "Matches the DEADLINE keyword and the rest of the line.")
  2988. (make-variable-buffer-local 'org-deadline-line-regexp)
  2989. (defvar org-scheduled-regexp nil
  2990. "Matches the SCHEDULED keyword.")
  2991. (make-variable-buffer-local 'org-scheduled-regexp)
  2992. (defvar org-scheduled-time-regexp nil
  2993. "Matches the SCHEDULED keyword together with a time stamp.")
  2994. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2995. (defvar org-closed-time-regexp nil
  2996. "Matches the CLOSED keyword together with a time stamp.")
  2997. (make-variable-buffer-local 'org-closed-time-regexp)
  2998. (defvar org-keyword-time-regexp nil
  2999. "Matches any of the 4 keywords, together with the time stamp.")
  3000. (make-variable-buffer-local 'org-keyword-time-regexp)
  3001. (defvar org-keyword-time-not-clock-regexp nil
  3002. "Matches any of the 3 keywords, together with the time stamp.")
  3003. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3004. (defvar org-maybe-keyword-time-regexp nil
  3005. "Matches a timestamp, possibly preceeded by a keyword.")
  3006. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3007. (defvar org-planning-or-clock-line-re nil
  3008. "Matches a line with planning or clock info.")
  3009. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3010. (defconst org-plain-time-of-day-regexp
  3011. (concat
  3012. "\\(\\<[012]?[0-9]"
  3013. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3014. "\\(--?"
  3015. "\\(\\<[012]?[0-9]"
  3016. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3017. "\\)?")
  3018. "Regular expression to match a plain time or time range.
  3019. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3020. groups carry important information:
  3021. 0 the full match
  3022. 1 the first time, range or not
  3023. 8 the second time, if it is a range.")
  3024. (defconst org-plain-time-extension-regexp
  3025. (concat
  3026. "\\(\\<[012]?[0-9]"
  3027. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3028. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3029. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3030. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3031. groups carry important information:
  3032. 0 the full match
  3033. 7 hours of duration
  3034. 9 minutes of duration")
  3035. (defconst org-stamp-time-of-day-regexp
  3036. (concat
  3037. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3038. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3039. "\\(--?"
  3040. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3041. "Regular expression to match a timestamp time or time range.
  3042. After a match, the following groups carry important information:
  3043. 0 the full match
  3044. 1 date plus weekday, for backreferencing to make sure both times on same day
  3045. 2 the first time, range or not
  3046. 4 the second time, if it is a range.")
  3047. (defconst org-startup-options
  3048. '(("fold" org-startup-folded t)
  3049. ("overview" org-startup-folded t)
  3050. ("nofold" org-startup-folded nil)
  3051. ("showall" org-startup-folded nil)
  3052. ("content" org-startup-folded content)
  3053. ("indent" org-startup-indented t)
  3054. ("noindent" org-startup-indented nil)
  3055. ("hidestars" org-hide-leading-stars t)
  3056. ("showstars" org-hide-leading-stars nil)
  3057. ("odd" org-odd-levels-only t)
  3058. ("oddeven" org-odd-levels-only nil)
  3059. ("align" org-startup-align-all-tables t)
  3060. ("noalign" org-startup-align-all-tables nil)
  3061. ("customtime" org-display-custom-times t)
  3062. ("logdone" org-log-done time)
  3063. ("lognotedone" org-log-done note)
  3064. ("nologdone" org-log-done nil)
  3065. ("lognoteclock-out" org-log-note-clock-out t)
  3066. ("nolognoteclock-out" org-log-note-clock-out nil)
  3067. ("logrepeat" org-log-repeat state)
  3068. ("lognoterepeat" org-log-repeat note)
  3069. ("nologrepeat" org-log-repeat nil)
  3070. ("fninline" org-footnote-define-inline t)
  3071. ("nofninline" org-footnote-define-inline nil)
  3072. ("fnlocal" org-footnote-section nil)
  3073. ("fnauto" org-footnote-auto-label t)
  3074. ("fnprompt" org-footnote-auto-label nil)
  3075. ("fnconfirm" org-footnote-auto-label confirm)
  3076. ("fnplain" org-footnote-auto-label plain)
  3077. ("fnadjust" org-footnote-auto-adjust t)
  3078. ("nofnadjust" org-footnote-auto-adjust nil)
  3079. ("constcgs" constants-unit-system cgs)
  3080. ("constSI" constants-unit-system SI)
  3081. ("noptag" org-tag-persistent-alist nil)
  3082. ("hideblocks" org-hide-block-startup t)
  3083. ("nohideblocks" org-hide-block-startup nil))
  3084. "Variable associated with STARTUP options for org-mode.
  3085. Each element is a list of three items: The startup options as written
  3086. in the #+STARTUP line, the corresponding variable, and the value to
  3087. set this variable to if the option is found. An optional forth element PUSH
  3088. means to push this value onto the list in the variable.")
  3089. (defun org-set-regexps-and-options ()
  3090. "Precompute regular expressions for current buffer."
  3091. (when (org-mode-p)
  3092. (org-set-local 'org-todo-kwd-alist nil)
  3093. (org-set-local 'org-todo-key-alist nil)
  3094. (org-set-local 'org-todo-key-trigger nil)
  3095. (org-set-local 'org-todo-keywords-1 nil)
  3096. (org-set-local 'org-done-keywords nil)
  3097. (org-set-local 'org-todo-heads nil)
  3098. (org-set-local 'org-todo-sets nil)
  3099. (org-set-local 'org-todo-log-states nil)
  3100. (org-set-local 'org-file-properties nil)
  3101. (org-set-local 'org-file-tags nil)
  3102. (let ((re (org-make-options-regexp
  3103. '("CATEGORY" "TODO" "COLUMNS"
  3104. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3105. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3106. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3107. (splitre "[ \t]+")
  3108. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3109. tail sep kws1 prio props ftags drawers
  3110. ext-setup-or-nil setup-contents (start 0))
  3111. (save-excursion
  3112. (save-restriction
  3113. (widen)
  3114. (goto-char (point-min))
  3115. (while (or (and ext-setup-or-nil
  3116. (string-match re ext-setup-or-nil start)
  3117. (setq start (match-end 0)))
  3118. (and (setq ext-setup-or-nil nil start 0)
  3119. (re-search-forward re nil t)))
  3120. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3121. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3122. (cond
  3123. ((equal key "CATEGORY")
  3124. (if (string-match "[ \t]+$" value)
  3125. (setq value (replace-match "" t t value)))
  3126. (setq cat value))
  3127. ((member key '("SEQ_TODO" "TODO"))
  3128. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3129. ((equal key "TYP_TODO")
  3130. (push (cons 'type (org-split-string value splitre)) kwds))
  3131. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3132. ;; general TODO-like setup
  3133. (push (cons (intern (downcase (match-string 1 key)))
  3134. (org-split-string value splitre)) kwds))
  3135. ((equal key "TAGS")
  3136. (setq tags (append tags (if tags '("\\n") nil)
  3137. (org-split-string value splitre))))
  3138. ((equal key "COLUMNS")
  3139. (org-set-local 'org-columns-default-format value))
  3140. ((equal key "LINK")
  3141. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3142. (push (cons (match-string 1 value)
  3143. (org-trim (match-string 2 value)))
  3144. links)))
  3145. ((equal key "PRIORITIES")
  3146. (setq prio (org-split-string value " +")))
  3147. ((equal key "PROPERTY")
  3148. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3149. (push (cons (match-string 1 value) (match-string 2 value))
  3150. props)))
  3151. ((equal key "FILETAGS")
  3152. (when (string-match "\\S-" value)
  3153. (setq ftags
  3154. (append
  3155. ftags
  3156. (apply 'append
  3157. (mapcar (lambda (x) (org-split-string x ":"))
  3158. (org-split-string value)))))))
  3159. ((equal key "DRAWERS")
  3160. (setq drawers (org-split-string value splitre)))
  3161. ((equal key "CONSTANTS")
  3162. (setq const (append const (org-split-string value splitre))))
  3163. ((equal key "STARTUP")
  3164. (let ((opts (org-split-string value splitre))
  3165. l var val)
  3166. (while (setq l (pop opts))
  3167. (when (setq l (assoc l org-startup-options))
  3168. (setq var (nth 1 l) val (nth 2 l))
  3169. (if (not (nth 3 l))
  3170. (set (make-local-variable var) val)
  3171. (if (not (listp (symbol-value var)))
  3172. (set (make-local-variable var) nil))
  3173. (set (make-local-variable var) (symbol-value var))
  3174. (add-to-list var val))))))
  3175. ((equal key "ARCHIVE")
  3176. (string-match " *$" value)
  3177. (setq arch (replace-match "" t t value))
  3178. (remove-text-properties 0 (length arch)
  3179. '(face t fontified t) arch))
  3180. ((equal key "SETUPFILE")
  3181. (setq setup-contents (org-file-contents
  3182. (expand-file-name
  3183. (org-remove-double-quotes value))
  3184. 'noerror))
  3185. (if (not ext-setup-or-nil)
  3186. (setq ext-setup-or-nil setup-contents start 0)
  3187. (setq ext-setup-or-nil
  3188. (concat (substring ext-setup-or-nil 0 start)
  3189. "\n" setup-contents "\n"
  3190. (substring ext-setup-or-nil start)))))
  3191. ))))
  3192. (when cat
  3193. (org-set-local 'org-category (intern cat))
  3194. (push (cons "CATEGORY" cat) props))
  3195. (when prio
  3196. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3197. (setq prio (mapcar 'string-to-char prio))
  3198. (org-set-local 'org-highest-priority (nth 0 prio))
  3199. (org-set-local 'org-lowest-priority (nth 1 prio))
  3200. (org-set-local 'org-default-priority (nth 2 prio)))
  3201. (and props (org-set-local 'org-file-properties (nreverse props)))
  3202. (and ftags (org-set-local 'org-file-tags
  3203. (mapcar 'org-add-prop-inherited ftags)))
  3204. (and drawers (org-set-local 'org-drawers drawers))
  3205. (and arch (org-set-local 'org-archive-location arch))
  3206. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3207. ;; Process the TODO keywords
  3208. (unless kwds
  3209. ;; Use the global values as if they had been given locally.
  3210. (setq kwds (default-value 'org-todo-keywords))
  3211. (if (stringp (car kwds))
  3212. (setq kwds (list (cons org-todo-interpretation
  3213. (default-value 'org-todo-keywords)))))
  3214. (setq kwds (reverse kwds)))
  3215. (setq kwds (nreverse kwds))
  3216. (let (inter kws kw)
  3217. (while (setq kws (pop kwds))
  3218. (let ((kws (or
  3219. (run-hook-with-args-until-success
  3220. 'org-todo-setup-filter-hook kws)
  3221. kws)))
  3222. (setq inter (pop kws) sep (member "|" kws)
  3223. kws0 (delete "|" (copy-sequence kws))
  3224. kwsa nil
  3225. kws1 (mapcar
  3226. (lambda (x)
  3227. ;; 1 2
  3228. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3229. (progn
  3230. (setq kw (match-string 1 x)
  3231. key (and (match-end 2) (match-string 2 x))
  3232. log (org-extract-log-state-settings x))
  3233. (push (cons kw (and key (string-to-char key))) kwsa)
  3234. (and log (push log org-todo-log-states))
  3235. kw)
  3236. (error "Invalid TODO keyword %s" x)))
  3237. kws0)
  3238. kwsa (if kwsa (append '((:startgroup))
  3239. (nreverse kwsa)
  3240. '((:endgroup))))
  3241. hw (car kws1)
  3242. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3243. tail (list inter hw (car dws) (org-last dws))))
  3244. (add-to-list 'org-todo-heads hw 'append)
  3245. (push kws1 org-todo-sets)
  3246. (setq org-done-keywords (append org-done-keywords dws nil))
  3247. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3248. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3249. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3250. (setq org-todo-sets (nreverse org-todo-sets)
  3251. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3252. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3253. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3254. ;; Process the constants
  3255. (when const
  3256. (let (e cst)
  3257. (while (setq e (pop const))
  3258. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3259. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3260. (setq org-table-formula-constants-local cst)))
  3261. ;; Process the tags.
  3262. (when tags
  3263. (let (e tgs)
  3264. (while (setq e (pop tags))
  3265. (cond
  3266. ((equal e "{") (push '(:startgroup) tgs))
  3267. ((equal e "}") (push '(:endgroup) tgs))
  3268. ((equal e "\\n") (push '(:newline) tgs))
  3269. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3270. (push (cons (match-string 1 e)
  3271. (string-to-char (match-string 2 e)))
  3272. tgs))
  3273. (t (push (list e) tgs))))
  3274. (org-set-local 'org-tag-alist nil)
  3275. (while (setq e (pop tgs))
  3276. (or (and (stringp (car e))
  3277. (assoc (car e) org-tag-alist))
  3278. (push e org-tag-alist)))))
  3279. ;; Compute the regular expressions and other local variables
  3280. (if (not org-done-keywords)
  3281. (setq org-done-keywords (and org-todo-keywords-1
  3282. (list (org-last org-todo-keywords-1)))))
  3283. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3284. (length org-scheduled-string)
  3285. (length org-clock-string)
  3286. (length org-closed-string)))
  3287. org-drawer-regexp
  3288. (concat "^[ \t]*:\\("
  3289. (mapconcat 'regexp-quote org-drawers "\\|")
  3290. "\\):[ \t]*$")
  3291. org-not-done-keywords
  3292. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3293. org-todo-regexp
  3294. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3295. "\\|") "\\)\\>")
  3296. org-not-done-regexp
  3297. (concat "\\<\\("
  3298. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3299. "\\)\\>")
  3300. org-not-done-heading-regexp
  3301. (concat "^\\(\\*+\\)[ \t]+\\("
  3302. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3303. "\\)\\>")
  3304. org-todo-line-regexp
  3305. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3306. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3307. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3308. org-complex-heading-regexp
  3309. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3310. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3311. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3312. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3313. org-nl-done-regexp
  3314. (concat "\n\\*+[ \t]+"
  3315. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3316. "\\)" "\\>")
  3317. org-todo-line-tags-regexp
  3318. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3319. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3320. (org-re
  3321. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3322. org-looking-at-done-regexp
  3323. (concat "^" "\\(?:"
  3324. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3325. "\\>")
  3326. org-deadline-regexp (concat "\\<" org-deadline-string)
  3327. org-deadline-time-regexp
  3328. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3329. org-deadline-line-regexp
  3330. (concat "\\<\\(" org-deadline-string "\\).*")
  3331. org-scheduled-regexp
  3332. (concat "\\<" org-scheduled-string)
  3333. org-scheduled-time-regexp
  3334. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3335. org-closed-time-regexp
  3336. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3337. org-keyword-time-regexp
  3338. (concat "\\<\\(" org-scheduled-string
  3339. "\\|" org-deadline-string
  3340. "\\|" org-closed-string
  3341. "\\|" org-clock-string "\\)"
  3342. " *[[<]\\([^]>]+\\)[]>]")
  3343. org-keyword-time-not-clock-regexp
  3344. (concat "\\<\\(" org-scheduled-string
  3345. "\\|" org-deadline-string
  3346. "\\|" org-closed-string
  3347. "\\)"
  3348. " *[[<]\\([^]>]+\\)[]>]")
  3349. org-maybe-keyword-time-regexp
  3350. (concat "\\(\\<\\(" org-scheduled-string
  3351. "\\|" org-deadline-string
  3352. "\\|" org-closed-string
  3353. "\\|" org-clock-string "\\)\\)?"
  3354. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3355. org-planning-or-clock-line-re
  3356. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3357. "\\|" org-deadline-string
  3358. "\\|" org-closed-string "\\|" org-clock-string
  3359. "\\)\\>\\)")
  3360. )
  3361. (org-compute-latex-and-specials-regexp)
  3362. (org-set-font-lock-defaults))))
  3363. (defun org-file-contents (file &optional noerror)
  3364. "Return the contents of FILE, as a string."
  3365. (if (or (not file)
  3366. (not (file-readable-p file)))
  3367. (if noerror
  3368. (progn
  3369. (message "Cannot read file %s" file)
  3370. (ding) (sit-for 2)
  3371. "")
  3372. (error "Cannot read file %s" file))
  3373. (with-temp-buffer
  3374. (insert-file-contents file)
  3375. (buffer-string))))
  3376. (defun org-extract-log-state-settings (x)
  3377. "Extract the log state setting from a TODO keyword string.
  3378. This will extract info from a string like \"WAIT(w@/!)\"."
  3379. (let (kw key log1 log2)
  3380. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3381. (setq kw (match-string 1 x)
  3382. key (and (match-end 2) (match-string 2 x))
  3383. log1 (and (match-end 3) (match-string 3 x))
  3384. log2 (and (match-end 4) (match-string 4 x)))
  3385. (and (or log1 log2)
  3386. (list kw
  3387. (and log1 (if (equal log1 "!") 'time 'note))
  3388. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3389. (defun org-remove-keyword-keys (list)
  3390. "Remove a pair of parenthesis at the end of each string in LIST."
  3391. (mapcar (lambda (x)
  3392. (if (string-match "(.*)$" x)
  3393. (substring x 0 (match-beginning 0))
  3394. x))
  3395. list))
  3396. ;; FIXME: this could be done much better, using second characters etc.
  3397. (defun org-assign-fast-keys (alist)
  3398. "Assign fast keys to a keyword-key alist.
  3399. Respect keys that are already there."
  3400. (let (new e k c c1 c2 (char ?a))
  3401. (while (setq e (pop alist))
  3402. (cond
  3403. ((equal e '(:startgroup)) (push e new))
  3404. ((equal e '(:endgroup)) (push e new))
  3405. ((equal e '(:newline)) (push e new))
  3406. (t
  3407. (setq k (car e) c2 nil)
  3408. (if (cdr e)
  3409. (setq c (cdr e))
  3410. ;; automatically assign a character.
  3411. (setq c1 (string-to-char
  3412. (downcase (substring
  3413. k (if (= (string-to-char k) ?@) 1 0)))))
  3414. (if (or (rassoc c1 new) (rassoc c1 alist))
  3415. (while (or (rassoc char new) (rassoc char alist))
  3416. (setq char (1+ char)))
  3417. (setq c2 c1))
  3418. (setq c (or c2 char)))
  3419. (push (cons k c) new))))
  3420. (nreverse new)))
  3421. ;;; Some variables used in various places
  3422. (defvar org-window-configuration nil
  3423. "Used in various places to store a window configuration.")
  3424. (defvar org-finish-function nil
  3425. "Function to be called when `C-c C-c' is used.
  3426. This is for getting out of special buffers like remember.")
  3427. ;; FIXME: Occasionally check by commenting these, to make sure
  3428. ;; no other functions uses these, forgetting to let-bind them.
  3429. (defvar entry)
  3430. (defvar last-state)
  3431. (defvar date)
  3432. ;; Defined somewhere in this file, but used before definition.
  3433. (defvar org-html-entities)
  3434. (defvar org-struct-menu)
  3435. (defvar org-org-menu)
  3436. (defvar org-tbl-menu)
  3437. (defvar org-agenda-keymap)
  3438. ;;;; Define the Org-mode
  3439. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3440. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  3441. ;; We use a before-change function to check if a table might need
  3442. ;; an update.
  3443. (defvar org-table-may-need-update t
  3444. "Indicates that a table might need an update.
  3445. This variable is set by `org-before-change-function'.
  3446. `org-table-align' sets it back to nil.")
  3447. (defun org-before-change-function (beg end)
  3448. "Every change indicates that a table might need an update."
  3449. (setq org-table-may-need-update t))
  3450. (defvar org-mode-map)
  3451. (defvar org-mode-hook nil
  3452. "Mode hook for Org-mode, run after the mode was turned on.")
  3453. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3454. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3455. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3456. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3457. (defvar org-table-buffer-is-an nil)
  3458. (defconst org-outline-regexp "\\*+ ")
  3459. ;;;###autoload
  3460. (define-derived-mode org-mode outline-mode "Org"
  3461. "Outline-based notes management and organizer, alias
  3462. \"Carsten's outline-mode for keeping track of everything.\"
  3463. Org-mode develops organizational tasks around a NOTES file which
  3464. contains information about projects as plain text. Org-mode is
  3465. implemented on top of outline-mode, which is ideal to keep the content
  3466. of large files well structured. It supports ToDo items, deadlines and
  3467. time stamps, which magically appear in the diary listing of the Emacs
  3468. calendar. Tables are easily created with a built-in table editor.
  3469. Plain text URL-like links connect to websites, emails (VM), Usenet
  3470. messages (Gnus), BBDB entries, and any files related to the project.
  3471. For printing and sharing of notes, an Org-mode file (or a part of it)
  3472. can be exported as a structured ASCII or HTML file.
  3473. The following commands are available:
  3474. \\{org-mode-map}"
  3475. ;; Get rid of Outline menus, they are not needed
  3476. ;; Need to do this here because define-derived-mode sets up
  3477. ;; the keymap so late. Still, it is a waste to call this each time
  3478. ;; we switch another buffer into org-mode.
  3479. (if (featurep 'xemacs)
  3480. (when (boundp 'outline-mode-menu-heading)
  3481. ;; Assume this is Greg's port, it used easymenu
  3482. (easy-menu-remove outline-mode-menu-heading)
  3483. (easy-menu-remove outline-mode-menu-show)
  3484. (easy-menu-remove outline-mode-menu-hide))
  3485. (define-key org-mode-map [menu-bar headings] 'undefined)
  3486. (define-key org-mode-map [menu-bar hide] 'undefined)
  3487. (define-key org-mode-map [menu-bar show] 'undefined))
  3488. (org-load-modules-maybe)
  3489. (easy-menu-add org-org-menu)
  3490. (easy-menu-add org-tbl-menu)
  3491. (org-install-agenda-files-menu)
  3492. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3493. (org-add-to-invisibility-spec '(org-cwidth))
  3494. (org-add-to-invisibility-spec '(org-hide-block . t))
  3495. (when (featurep 'xemacs)
  3496. (org-set-local 'line-move-ignore-invisible t))
  3497. (org-set-local 'outline-regexp org-outline-regexp)
  3498. (org-set-local 'outline-level 'org-outline-level)
  3499. (when (and org-ellipsis
  3500. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3501. (fboundp 'make-glyph-code))
  3502. (unless org-display-table
  3503. (setq org-display-table (make-display-table)))
  3504. (set-display-table-slot
  3505. org-display-table 4
  3506. (vconcat (mapcar
  3507. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3508. org-ellipsis)))
  3509. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3510. (setq buffer-display-table org-display-table))
  3511. (org-set-regexps-and-options)
  3512. (when (and org-tag-faces (not org-tags-special-faces-re))
  3513. ;; tag faces set outside customize.... force initialization.
  3514. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3515. ;; Calc embedded
  3516. (org-set-local 'calc-embedded-open-mode "# ")
  3517. (modify-syntax-entry ?# "<")
  3518. (modify-syntax-entry ?@ "w")
  3519. (if org-startup-truncated (setq truncate-lines t))
  3520. (org-set-local 'font-lock-unfontify-region-function
  3521. 'org-unfontify-region)
  3522. ;; Activate before-change-function
  3523. (org-set-local 'org-table-may-need-update t)
  3524. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3525. 'local)
  3526. ;; Check for running clock before killing a buffer
  3527. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3528. ;; Paragraphs and auto-filling
  3529. (org-set-autofill-regexps)
  3530. (setq indent-line-function 'org-indent-line-function)
  3531. (org-update-radio-target-regexp)
  3532. ;; Make sure dependence stuff works reliably, even for users who set it
  3533. ;; too late :-(
  3534. (if org-enforce-todo-dependencies
  3535. (add-hook 'org-blocker-hook
  3536. 'org-block-todo-from-children-or-siblings-or-parent)
  3537. (remove-hook 'org-blocker-hook
  3538. 'org-block-todo-from-children-or-siblings-or-parent))
  3539. (if org-enforce-todo-checkbox-dependencies
  3540. (add-hook 'org-blocker-hook
  3541. 'org-block-todo-from-checkboxes)
  3542. (remove-hook 'org-blocker-hook
  3543. 'org-block-todo-from-checkboxes))
  3544. ;; Comment characters
  3545. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3546. (org-set-local 'comment-padding " ")
  3547. ;; Align options lines
  3548. (org-set-local
  3549. 'align-mode-rules-list
  3550. '((org-in-buffer-settings
  3551. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3552. (modes . '(org-mode)))))
  3553. ;; Imenu
  3554. (org-set-local 'imenu-create-index-function
  3555. 'org-imenu-get-tree)
  3556. ;; Make isearch reveal context
  3557. (if (or (featurep 'xemacs)
  3558. (not (boundp 'outline-isearch-open-invisible-function)))
  3559. ;; Emacs 21 and XEmacs make use of the hook
  3560. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3561. ;; Emacs 22 deals with this through a special variable
  3562. (org-set-local 'outline-isearch-open-invisible-function
  3563. (lambda (&rest ignore) (org-show-context 'isearch))))
  3564. ;; If empty file that did not turn on org-mode automatically, make it to.
  3565. (if (and org-insert-mode-line-in-empty-file
  3566. (interactive-p)
  3567. (= (point-min) (point-max)))
  3568. (insert "# -*- mode: org -*-\n\n"))
  3569. (unless org-inhibit-startup
  3570. (when org-startup-align-all-tables
  3571. (let ((bmp (buffer-modified-p)))
  3572. (org-table-map-tables 'org-table-align)
  3573. (set-buffer-modified-p bmp)))
  3574. (when org-startup-indented
  3575. (require 'org-indent)
  3576. (org-indent-mode 1))
  3577. (org-set-startup-visibility)))
  3578. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3579. (defun org-current-time ()
  3580. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3581. (if (> (car org-time-stamp-rounding-minutes) 1)
  3582. (let ((r (car org-time-stamp-rounding-minutes))
  3583. (time (decode-time)))
  3584. (apply 'encode-time
  3585. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3586. (nthcdr 2 time))))
  3587. (current-time)))
  3588. ;;;; Font-Lock stuff, including the activators
  3589. (defvar org-mouse-map (make-sparse-keymap))
  3590. (org-defkey org-mouse-map
  3591. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3592. (org-defkey org-mouse-map
  3593. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3594. (when org-mouse-1-follows-link
  3595. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3596. (when org-tab-follows-link
  3597. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3598. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3599. (require 'font-lock)
  3600. (defconst org-non-link-chars "]\t\n\r<>")
  3601. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3602. "shell" "elisp"))
  3603. (defvar org-link-types-re nil
  3604. "Matches a link that has a url-like prefix like \"http:\"")
  3605. (defvar org-link-re-with-space nil
  3606. "Matches a link with spaces, optional angular brackets around it.")
  3607. (defvar org-link-re-with-space2 nil
  3608. "Matches a link with spaces, optional angular brackets around it.")
  3609. (defvar org-link-re-with-space3 nil
  3610. "Matches a link with spaces, only for internal part in bracket links.")
  3611. (defvar org-angle-link-re nil
  3612. "Matches link with angular brackets, spaces are allowed.")
  3613. (defvar org-plain-link-re nil
  3614. "Matches plain link, without spaces.")
  3615. (defvar org-bracket-link-regexp nil
  3616. "Matches a link in double brackets.")
  3617. (defvar org-bracket-link-analytic-regexp nil
  3618. "Regular expression used to analyze links.
  3619. Here is what the match groups contain after a match:
  3620. 1: http:
  3621. 2: http
  3622. 3: path
  3623. 4: [desc]
  3624. 5: desc")
  3625. (defvar org-bracket-link-analytic-regexp++ nil
  3626. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3627. (defvar org-any-link-re nil
  3628. "Regular expression matching any link.")
  3629. (defun org-make-link-regexps ()
  3630. "Update the link regular expressions.
  3631. This should be called after the variable `org-link-types' has changed."
  3632. (setq org-link-types-re
  3633. (concat
  3634. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3635. org-link-re-with-space
  3636. (concat
  3637. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3638. "\\([^" org-non-link-chars " ]"
  3639. "[^" org-non-link-chars "]*"
  3640. "[^" org-non-link-chars " ]\\)>?")
  3641. org-link-re-with-space2
  3642. (concat
  3643. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3644. "\\([^" org-non-link-chars " ]"
  3645. "[^\t\n\r]*"
  3646. "[^" org-non-link-chars " ]\\)>?")
  3647. org-link-re-with-space3
  3648. (concat
  3649. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3650. "\\([^" org-non-link-chars " ]"
  3651. "[^\t\n\r]*\\)")
  3652. org-angle-link-re
  3653. (concat
  3654. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3655. "\\([^" org-non-link-chars " ]"
  3656. "[^" org-non-link-chars "]*"
  3657. "\\)>")
  3658. org-plain-link-re
  3659. (concat
  3660. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3661. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3662. org-bracket-link-regexp
  3663. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3664. org-bracket-link-analytic-regexp
  3665. (concat
  3666. "\\[\\["
  3667. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3668. "\\([^]]+\\)"
  3669. "\\]"
  3670. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3671. "\\]")
  3672. org-bracket-link-analytic-regexp++
  3673. (concat
  3674. "\\[\\["
  3675. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3676. "\\([^]]+\\)"
  3677. "\\]"
  3678. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3679. "\\]")
  3680. org-any-link-re
  3681. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3682. org-angle-link-re "\\)\\|\\("
  3683. org-plain-link-re "\\)")))
  3684. (org-make-link-regexps)
  3685. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3686. "Regular expression for fast time stamp matching.")
  3687. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3688. "Regular expression for fast time stamp matching.")
  3689. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3690. "Regular expression matching time strings for analysis.
  3691. This one does not require the space after the date, so it can be used
  3692. on a string that terminates immediately after the date.")
  3693. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3694. "Regular expression matching time strings for analysis.")
  3695. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3696. "Regular expression matching time stamps, with groups.")
  3697. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3698. "Regular expression matching time stamps (also [..]), with groups.")
  3699. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3700. "Regular expression matching a time stamp range.")
  3701. (defconst org-tr-regexp-both
  3702. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3703. "Regular expression matching a time stamp range.")
  3704. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3705. org-ts-regexp "\\)?")
  3706. "Regular expression matching a time stamp or time stamp range.")
  3707. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3708. org-ts-regexp-both "\\)?")
  3709. "Regular expression matching a time stamp or time stamp range.
  3710. The time stamps may be either active or inactive.")
  3711. (defvar org-emph-face nil)
  3712. (defun org-do-emphasis-faces (limit)
  3713. "Run through the buffer and add overlays to links."
  3714. (let (rtn a)
  3715. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3716. (if (not (= (char-after (match-beginning 3))
  3717. (char-after (match-beginning 4))))
  3718. (progn
  3719. (setq rtn t)
  3720. (setq a (assoc (match-string 3) org-emphasis-alist))
  3721. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3722. 'face
  3723. (nth 1 a))
  3724. (and (nth 4 a)
  3725. (org-remove-flyspell-overlays-in
  3726. (match-beginning 0) (match-end 0)))
  3727. (add-text-properties (match-beginning 2) (match-end 2)
  3728. '(font-lock-multiline t))
  3729. (when org-hide-emphasis-markers
  3730. (add-text-properties (match-end 4) (match-beginning 5)
  3731. '(invisible org-link))
  3732. (add-text-properties (match-beginning 3) (match-end 3)
  3733. '(invisible org-link)))))
  3734. (backward-char 1))
  3735. rtn))
  3736. (defun org-emphasize (&optional char)
  3737. "Insert or change an emphasis, i.e. a font like bold or italic.
  3738. If there is an active region, change that region to a new emphasis.
  3739. If there is no region, just insert the marker characters and position
  3740. the cursor between them.
  3741. CHAR should be either the marker character, or the first character of the
  3742. HTML tag associated with that emphasis. If CHAR is a space, the means
  3743. to remove the emphasis of the selected region.
  3744. If char is not given (for example in an interactive call) it
  3745. will be prompted for."
  3746. (interactive)
  3747. (let ((eal org-emphasis-alist) e det
  3748. (erc org-emphasis-regexp-components)
  3749. (prompt "")
  3750. (string "") beg end move tag c s)
  3751. (if (org-region-active-p)
  3752. (setq beg (region-beginning) end (region-end)
  3753. string (buffer-substring beg end))
  3754. (setq move t))
  3755. (while (setq e (pop eal))
  3756. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3757. c (aref tag 0))
  3758. (push (cons c (string-to-char (car e))) det)
  3759. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3760. (substring tag 1)))))
  3761. (setq det (nreverse det))
  3762. (unless char
  3763. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3764. (setq char (read-char-exclusive)))
  3765. (setq char (or (cdr (assoc char det)) char))
  3766. (if (equal char ?\ )
  3767. (setq s "" move nil)
  3768. (unless (assoc (char-to-string char) org-emphasis-alist)
  3769. (error "No such emphasis marker: \"%c\"" char))
  3770. (setq s (char-to-string char)))
  3771. (while (and (> (length string) 1)
  3772. (equal (substring string 0 1) (substring string -1))
  3773. (assoc (substring string 0 1) org-emphasis-alist))
  3774. (setq string (substring string 1 -1)))
  3775. (setq string (concat s string s))
  3776. (if beg (delete-region beg end))
  3777. (unless (or (bolp)
  3778. (string-match (concat "[" (nth 0 erc) "\n]")
  3779. (char-to-string (char-before (point)))))
  3780. (insert " "))
  3781. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3782. (char-to-string (char-after (point))))
  3783. (insert " ") (backward-char 1))
  3784. (insert string)
  3785. (and move (backward-char 1))))
  3786. (defconst org-nonsticky-props
  3787. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3788. (defsubst org-rear-nonsticky-at (pos)
  3789. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3790. (defun org-activate-plain-links (limit)
  3791. "Run through the buffer and add overlays to links."
  3792. (catch 'exit
  3793. (let (f)
  3794. (if (re-search-forward org-plain-link-re limit t)
  3795. (progn
  3796. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3797. (setq f (get-text-property (match-beginning 0) 'face))
  3798. (if (or (eq f 'org-tag)
  3799. (and (listp f) (memq 'org-tag f)))
  3800. nil
  3801. (add-text-properties (match-beginning 0) (match-end 0)
  3802. (list 'mouse-face 'highlight
  3803. 'keymap org-mouse-map))
  3804. (org-rear-nonsticky-at (match-end 0)))
  3805. t)))))
  3806. (defun org-activate-code (limit)
  3807. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3808. (progn
  3809. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3810. (remove-text-properties (match-beginning 0) (match-end 0)
  3811. '(display t invisible t intangible t))
  3812. t)))
  3813. (defun org-fontify-meta-lines-and-blocks (limit)
  3814. "Fontify #+ lines and blocks, in the correct ways."
  3815. (let ((case-fold-search t))
  3816. (if (re-search-forward
  3817. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3818. limit t)
  3819. (let ((beg (match-beginning 0))
  3820. (beg1 (line-beginning-position 2))
  3821. (dc1 (downcase (match-string 2)))
  3822. (dc3 (downcase (match-string 3)))
  3823. end end1 quoting)
  3824. (cond
  3825. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3826. ;; a single line of backend-specific content
  3827. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3828. (remove-text-properties (match-beginning 0) (match-end 0)
  3829. '(display t invisible t intangible t))
  3830. (add-text-properties (match-beginning 1) (match-end 3)
  3831. '(font-lock-fontified t face org-meta-line))
  3832. (add-text-properties (match-beginning 6) (match-end 6)
  3833. '(font-lock-fontified t face org-block))
  3834. t)
  3835. ((and (match-end 4) (equal dc3 "begin"))
  3836. ;; Truely a block
  3837. (setq quoting (member (downcase (match-string 5))
  3838. org-protecting-blocks))
  3839. (when (re-search-forward
  3840. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3841. nil t) ;; on purpose, we look further than LIMIT
  3842. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3843. (when quoting
  3844. (remove-text-properties beg end
  3845. '(display t invisible t intangible t)))
  3846. (add-text-properties
  3847. beg end
  3848. '(font-lock-fontified t font-lock-multiline t))
  3849. (add-text-properties beg beg1 '(face org-meta-line))
  3850. (add-text-properties end1 end '(face org-meta-line))
  3851. (when quoting
  3852. (add-text-properties beg1 end1 '(face org-block)))
  3853. t))
  3854. ((not (member (char-after beg) '(?\ ?\t)))
  3855. ;; just any other in-buffer setting, but not indented
  3856. (add-text-properties
  3857. beg (match-end 0)
  3858. '(font-lock-fontified t face org-meta-line))
  3859. t)
  3860. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3861. (and (match-end 4) (equal dc3 "attr")))
  3862. (add-text-properties
  3863. beg (match-end 0)
  3864. '(font-lock-fontified t face org-meta-line))
  3865. t)
  3866. (t nil))))))
  3867. (defun org-activate-angle-links (limit)
  3868. "Run through the buffer and add overlays to links."
  3869. (if (re-search-forward org-angle-link-re limit t)
  3870. (progn
  3871. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3872. (add-text-properties (match-beginning 0) (match-end 0)
  3873. (list 'mouse-face 'highlight
  3874. 'keymap org-mouse-map))
  3875. (org-rear-nonsticky-at (match-end 0))
  3876. t)))
  3877. (defun org-activate-footnote-links (limit)
  3878. "Run through the buffer and add overlays to links."
  3879. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3880. limit t)
  3881. (progn
  3882. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3883. (add-text-properties (match-beginning 2) (match-end 2)
  3884. (list 'mouse-face 'highlight
  3885. 'keymap org-mouse-map
  3886. 'help-echo
  3887. (if (= (point-at-bol) (match-beginning 2))
  3888. "Footnote definition"
  3889. "Footnote reference")
  3890. ))
  3891. (org-rear-nonsticky-at (match-end 2))
  3892. t)))
  3893. (defun org-activate-bracket-links (limit)
  3894. "Run through the buffer and add overlays to bracketed links."
  3895. (if (re-search-forward org-bracket-link-regexp limit t)
  3896. (let* ((help (concat "LINK: "
  3897. (org-match-string-no-properties 1)))
  3898. ;; FIXME: above we should remove the escapes.
  3899. ;; but that requires another match, protecting match data,
  3900. ;; a lot of overhead for font-lock.
  3901. (ip (org-maybe-intangible
  3902. (list 'invisible 'org-link
  3903. 'keymap org-mouse-map 'mouse-face 'highlight
  3904. 'font-lock-multiline t 'help-echo help)))
  3905. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3906. 'font-lock-multiline t 'help-echo help)))
  3907. ;; We need to remove the invisible property here. Table narrowing
  3908. ;; may have made some of this invisible.
  3909. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3910. (remove-text-properties (match-beginning 0) (match-end 0)
  3911. '(invisible nil))
  3912. (if (match-end 3)
  3913. (progn
  3914. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3915. (org-rear-nonsticky-at (match-beginning 3))
  3916. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3917. (org-rear-nonsticky-at (match-end 3))
  3918. (add-text-properties (match-end 3) (match-end 0) ip)
  3919. (org-rear-nonsticky-at (match-end 0)))
  3920. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3921. (org-rear-nonsticky-at (match-beginning 1))
  3922. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3923. (org-rear-nonsticky-at (match-end 1))
  3924. (add-text-properties (match-end 1) (match-end 0) ip)
  3925. (org-rear-nonsticky-at (match-end 0)))
  3926. t)))
  3927. (defun org-activate-dates (limit)
  3928. "Run through the buffer and add overlays to dates."
  3929. (if (re-search-forward org-tsr-regexp-both limit t)
  3930. (progn
  3931. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3932. (add-text-properties (match-beginning 0) (match-end 0)
  3933. (list 'mouse-face 'highlight
  3934. 'keymap org-mouse-map))
  3935. (org-rear-nonsticky-at (match-end 0))
  3936. (when org-display-custom-times
  3937. (if (match-end 3)
  3938. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3939. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3940. t)))
  3941. (defvar org-target-link-regexp nil
  3942. "Regular expression matching radio targets in plain text.")
  3943. (make-variable-buffer-local 'org-target-link-regexp)
  3944. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3945. "Regular expression matching a link target.")
  3946. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3947. "Regular expression matching a radio target.")
  3948. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3949. "Regular expression matching any target.")
  3950. (defun org-activate-target-links (limit)
  3951. "Run through the buffer and add overlays to target matches."
  3952. (when org-target-link-regexp
  3953. (let ((case-fold-search t))
  3954. (if (re-search-forward org-target-link-regexp limit t)
  3955. (progn
  3956. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3957. (add-text-properties (match-beginning 0) (match-end 0)
  3958. (list 'mouse-face 'highlight
  3959. 'keymap org-mouse-map
  3960. 'help-echo "Radio target link"
  3961. 'org-linked-text t))
  3962. (org-rear-nonsticky-at (match-end 0))
  3963. t)))))
  3964. (defun org-update-radio-target-regexp ()
  3965. "Find all radio targets in this file and update the regular expression."
  3966. (interactive)
  3967. (when (memq 'radio org-activate-links)
  3968. (setq org-target-link-regexp
  3969. (org-make-target-link-regexp (org-all-targets 'radio)))
  3970. (org-restart-font-lock)))
  3971. (defun org-hide-wide-columns (limit)
  3972. (let (s e)
  3973. (setq s (text-property-any (point) (or limit (point-max))
  3974. 'org-cwidth t))
  3975. (when s
  3976. (setq e (next-single-property-change s 'org-cwidth))
  3977. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3978. (goto-char e)
  3979. t)))
  3980. (defvar org-latex-and-specials-regexp nil
  3981. "Regular expression for highlighting export special stuff.")
  3982. (defvar org-match-substring-regexp)
  3983. (defvar org-match-substring-with-braces-regexp)
  3984. ;; This should be with the exporter code, but we also use if for font-locking
  3985. (defconst org-export-html-special-string-regexps
  3986. '(("\\\\-" . "&shy;")
  3987. ("---\\([^-]\\)" . "&mdash;\\1")
  3988. ("--\\([^-]\\)" . "&ndash;\\1")
  3989. ("\\.\\.\\." . "&hellip;"))
  3990. "Regular expressions for special string conversion.")
  3991. (defun org-compute-latex-and-specials-regexp ()
  3992. "Compute regular expression for stuff treated specially by exporters."
  3993. (if (not org-highlight-latex-fragments-and-specials)
  3994. (org-set-local 'org-latex-and-specials-regexp nil)
  3995. (require 'org-exp)
  3996. (let*
  3997. ((matchers (plist-get org-format-latex-options :matchers))
  3998. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3999. org-latex-regexps)))
  4000. (options (org-combine-plists (org-default-export-plist)
  4001. (org-infile-export-plist)))
  4002. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4003. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4004. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4005. (org-export-html-expand (plist-get options :expand-quoted-html))
  4006. (org-export-with-special-strings (plist-get options :special-strings))
  4007. (re-sub
  4008. (cond
  4009. ((equal org-export-with-sub-superscripts '{})
  4010. (list org-match-substring-with-braces-regexp))
  4011. (org-export-with-sub-superscripts
  4012. (list org-match-substring-regexp))
  4013. (t nil)))
  4014. (re-latex
  4015. (if org-export-with-LaTeX-fragments
  4016. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4017. (re-macros
  4018. (if org-export-with-TeX-macros
  4019. (list (concat "\\\\"
  4020. (regexp-opt
  4021. (append (mapcar 'car org-html-entities)
  4022. (if (boundp 'org-latex-entities)
  4023. (mapcar (lambda (x)
  4024. (or (car-safe x) x))
  4025. org-latex-entities)
  4026. nil))
  4027. 'words))) ; FIXME
  4028. ))
  4029. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4030. (re-special (if org-export-with-special-strings
  4031. (mapcar (lambda (x) (car x))
  4032. org-export-html-special-string-regexps)))
  4033. (re-rest
  4034. (delq nil
  4035. (list
  4036. (if org-export-html-expand "@<[^>\n]+>")
  4037. ))))
  4038. (org-set-local
  4039. 'org-latex-and-specials-regexp
  4040. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4041. re-rest) "\\|")))))
  4042. (defun org-do-latex-and-special-faces (limit)
  4043. "Run through the buffer and add overlays to links."
  4044. (when org-latex-and-specials-regexp
  4045. (let (rtn d)
  4046. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4047. limit t))
  4048. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4049. 'face))
  4050. '(org-code org-verbatim underline)))
  4051. (progn
  4052. (setq rtn t
  4053. d (cond ((member (char-after (1+ (match-beginning 0)))
  4054. '(?_ ?^)) 1)
  4055. (t 0)))
  4056. (font-lock-prepend-text-property
  4057. (+ d (match-beginning 0)) (match-end 0)
  4058. 'face 'org-latex-and-export-specials)
  4059. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4060. '(font-lock-multiline t)))))
  4061. rtn)))
  4062. (defun org-restart-font-lock ()
  4063. "Restart font-lock-mode, to force refontification."
  4064. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4065. (font-lock-mode -1)
  4066. (font-lock-mode 1)))
  4067. (defun org-all-targets (&optional radio)
  4068. "Return a list of all targets in this file.
  4069. With optional argument RADIO, only find radio targets."
  4070. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4071. rtn)
  4072. (save-excursion
  4073. (goto-char (point-min))
  4074. (while (re-search-forward re nil t)
  4075. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4076. rtn)))
  4077. (defun org-make-target-link-regexp (targets)
  4078. "Make regular expression matching all strings in TARGETS.
  4079. The regular expression finds the targets also if there is a line break
  4080. between words."
  4081. (and targets
  4082. (concat
  4083. "\\<\\("
  4084. (mapconcat
  4085. (lambda (x)
  4086. (while (string-match " +" x)
  4087. (setq x (replace-match "\\s-+" t t x)))
  4088. x)
  4089. targets
  4090. "\\|")
  4091. "\\)\\>")))
  4092. (defun org-activate-tags (limit)
  4093. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4094. (progn
  4095. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4096. (add-text-properties (match-beginning 1) (match-end 1)
  4097. (list 'mouse-face 'highlight
  4098. 'keymap org-mouse-map))
  4099. (org-rear-nonsticky-at (match-end 1))
  4100. t)))
  4101. (defun org-outline-level ()
  4102. (save-excursion
  4103. (looking-at outline-regexp)
  4104. (if (match-beginning 1)
  4105. (+ (org-get-string-indentation (match-string 1)) 1000)
  4106. (1- (- (match-end 0) (match-beginning 0))))))
  4107. (defvar org-font-lock-keywords nil)
  4108. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4109. "Regular expression matching a property line.")
  4110. (defvar org-font-lock-hook nil
  4111. "Functions to be called for special font lock stuff.")
  4112. (defun org-font-lock-hook (limit)
  4113. (run-hook-with-args 'org-font-lock-hook limit))
  4114. (defun org-set-font-lock-defaults ()
  4115. (let* ((em org-fontify-emphasized-text)
  4116. (lk org-activate-links)
  4117. (org-font-lock-extra-keywords
  4118. (list
  4119. ;; Call the hook
  4120. '(org-font-lock-hook)
  4121. ;; Headlines
  4122. `(,(if org-fontify-whole-heading-line
  4123. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4124. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4125. (1 (org-get-level-face 1))
  4126. (2 (org-get-level-face 2))
  4127. (3 (org-get-level-face 3)))
  4128. ;; Table lines
  4129. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4130. (1 'org-table t))
  4131. ;; Table internals
  4132. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4133. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4134. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4135. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4136. ;; Drawers
  4137. (list org-drawer-regexp '(0 'org-special-keyword t))
  4138. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4139. ;; Properties
  4140. (list org-property-re
  4141. '(1 'org-special-keyword t)
  4142. '(3 'org-property-value t))
  4143. ;; Links
  4144. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4145. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4146. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4147. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4148. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4149. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4150. (if (memq 'footnote lk) '(org-activate-footnote-links
  4151. (2 'org-footnote t)))
  4152. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4153. '(org-hide-wide-columns (0 nil append))
  4154. ;; TODO lines
  4155. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4156. '(1 (org-get-todo-face 1) t))
  4157. ;; DONE
  4158. (if org-fontify-done-headline
  4159. (list (concat "^[*]+ +\\<\\("
  4160. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4161. "\\)\\(.*\\)")
  4162. '(2 'org-headline-done t))
  4163. nil)
  4164. ;; Priorities
  4165. '(org-font-lock-add-priority-faces)
  4166. ;; Tags
  4167. '(org-font-lock-add-tag-faces)
  4168. ;; Special keywords
  4169. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4170. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4171. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4172. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4173. ;; Emphasis
  4174. (if em
  4175. (if (featurep 'xemacs)
  4176. '(org-do-emphasis-faces (0 nil append))
  4177. '(org-do-emphasis-faces)))
  4178. ;; Checkboxes
  4179. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4180. 2 'org-checkbox prepend)
  4181. (if org-provide-checkbox-statistics
  4182. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4183. (0 (org-get-checkbox-statistics-face) t)))
  4184. ;; Description list items
  4185. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4186. 2 'bold prepend)
  4187. ;; ARCHIVEd headings
  4188. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4189. '(1 'org-archived prepend))
  4190. ;; Specials
  4191. '(org-do-latex-and-special-faces)
  4192. ;; Code
  4193. '(org-activate-code (1 'org-code t))
  4194. ;; COMMENT
  4195. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4196. "\\|" org-quote-string "\\)\\>")
  4197. '(1 'org-special-keyword t))
  4198. '("^#.*" (0 'font-lock-comment-face t))
  4199. ;; Blocks and meta lines
  4200. '(org-fontify-meta-lines-and-blocks)
  4201. )))
  4202. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4203. ;; Now set the full font-lock-keywords
  4204. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4205. (org-set-local 'font-lock-defaults
  4206. '(org-font-lock-keywords t nil nil backward-paragraph))
  4207. (kill-local-variable 'font-lock-keywords) nil))
  4208. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4209. "Fontify string S like in Org-mode"
  4210. (with-temp-buffer
  4211. (insert s)
  4212. (let ((org-odd-levels-only odd-levels))
  4213. (org-mode)
  4214. (font-lock-fontify-buffer)
  4215. (buffer-string))))
  4216. (defvar org-m nil)
  4217. (defvar org-l nil)
  4218. (defvar org-f nil)
  4219. (defun org-get-level-face (n)
  4220. "Get the right face for match N in font-lock matching of headlines."
  4221. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4222. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4223. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4224. (cond
  4225. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4226. ((eq n 2) org-f)
  4227. (t (if org-level-color-stars-only nil org-f))))
  4228. (defun org-get-todo-face (kwd)
  4229. "Get the right face for a TODO keyword KWD.
  4230. If KWD is a number, get the corresponding match group."
  4231. (if (numberp kwd) (setq kwd (match-string kwd)))
  4232. (or (cdr (assoc kwd org-todo-keyword-faces))
  4233. (and (member kwd org-done-keywords) 'org-done)
  4234. 'org-todo))
  4235. (defun org-font-lock-add-tag-faces (limit)
  4236. "Add the special tag faces."
  4237. (when (and org-tag-faces org-tags-special-faces-re)
  4238. (while (re-search-forward org-tags-special-faces-re limit t)
  4239. (add-text-properties (match-beginning 1) (match-end 1)
  4240. (list 'face (org-get-tag-face 1)
  4241. 'font-lock-fontified t))
  4242. (backward-char 1))))
  4243. (defun org-font-lock-add-priority-faces (limit)
  4244. "Add the special priority faces."
  4245. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4246. (add-text-properties
  4247. (match-beginning 0) (match-end 0)
  4248. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4249. org-priority-faces))
  4250. 'org-special-keyword)
  4251. 'font-lock-fontified t))))
  4252. (defun org-get-tag-face (kwd)
  4253. "Get the right face for a TODO keyword KWD.
  4254. If KWD is a number, get the corresponding match group."
  4255. (if (numberp kwd) (setq kwd (match-string kwd)))
  4256. (or (cdr (assoc kwd org-tag-faces))
  4257. 'org-tag))
  4258. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4259. "Remove fontification and activation overlays from links."
  4260. (font-lock-default-unfontify-region beg end)
  4261. (let* ((buffer-undo-list t)
  4262. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4263. (inhibit-modification-hooks t)
  4264. deactivate-mark buffer-file-name buffer-file-truename)
  4265. (remove-text-properties beg end
  4266. '(mouse-face t keymap t org-linked-text t
  4267. invisible t intangible t
  4268. line-prefix t wrap-prefix t
  4269. org-no-flyspell t))))
  4270. ;;;; Visibility cycling, including org-goto and indirect buffer
  4271. ;;; Cycling
  4272. (defvar org-cycle-global-status nil)
  4273. (make-variable-buffer-local 'org-cycle-global-status)
  4274. (defvar org-cycle-subtree-status nil)
  4275. (make-variable-buffer-local 'org-cycle-subtree-status)
  4276. ;;;###autoload
  4277. (defvar org-inlinetask-min-level)
  4278. (defun org-cycle (&optional arg)
  4279. "TAB-action and visibility cycling for Org-mode.
  4280. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4281. is outine visibility cycling, but it also invokes other actions
  4282. in special contexts.
  4283. - When this function is called with a prefix argument, rotate the entire
  4284. buffer through 3 states (global cycling)
  4285. 1. OVERVIEW: Show only top-level headlines.
  4286. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4287. 3. SHOW ALL: Show everything.
  4288. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4289. determined by the variable `org-startup-folded', and by any VISIBILITY
  4290. properties in the buffer.
  4291. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4292. including any drawers.
  4293. - When inside a table, re-align the table and move to the next field.
  4294. - When point is at the beginning of a headline, rotate the subtree started
  4295. by this line through 3 different states (local cycling)
  4296. 1. FOLDED: Only the main headline is shown.
  4297. 2. CHILDREN: The main headline and the direct children are shown.
  4298. From this state, you can move to one of the children
  4299. and zoom in further.
  4300. 3. SUBTREE: Show the entire subtree, including body text.
  4301. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4302. - When there is a numeric prefix, go up to a heading with level ARG, do
  4303. a `show-subtree' and return to the previous cursor position. If ARG
  4304. is negative, go up that many levels.
  4305. - When point is not at the beginning of a headline, execute the global
  4306. binding for TAB, which is re-indenting the line. See the option
  4307. `org-cycle-emulate-tab' for details.
  4308. - Special case: if point is at the beginning of the buffer and there is
  4309. no headline in line 1, this function will act as if called with prefix arg.
  4310. But only if also the variable `org-cycle-global-at-bob' is t."
  4311. (interactive "P")
  4312. (org-load-modules-maybe)
  4313. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4314. (let* ((limit-level
  4315. (or org-cycle-max-level
  4316. (and (boundp 'org-inlinetask-min-level)
  4317. org-inlinetask-min-level
  4318. (1- org-inlinetask-min-level))))
  4319. (nstars (and limit-level
  4320. (if org-odd-levels-only
  4321. (and limit-level (1- (* limit-level 2)))
  4322. limit-level)))
  4323. (outline-regexp
  4324. (cond
  4325. ((not (org-mode-p)) outline-regexp)
  4326. ((or (eq org-cycle-include-plain-lists 'integrate)
  4327. (and org-cycle-include-plain-lists (org-at-item-p)))
  4328. (concat "\\(?:\\*"
  4329. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4330. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4331. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4332. (bob-special (and org-cycle-global-at-bob (bobp)
  4333. (not (looking-at outline-regexp))))
  4334. (org-cycle-hook
  4335. (if bob-special
  4336. (delq 'org-optimize-window-after-visibility-change
  4337. (copy-sequence org-cycle-hook))
  4338. org-cycle-hook))
  4339. (pos (point)))
  4340. (if (or bob-special (equal arg '(4)))
  4341. ;; special case: use global cycling
  4342. (setq arg t))
  4343. (cond
  4344. ((equal arg '(16))
  4345. (org-set-startup-visibility)
  4346. (message "Startup visibility, plus VISIBILITY properties"))
  4347. ((equal arg '(64))
  4348. (show-all)
  4349. (message "Entire buffer visible, including drawers"))
  4350. ((org-at-table-p 'any)
  4351. ;; Enter the table or move to the next field in the table
  4352. (or (org-table-recognize-table.el)
  4353. (progn
  4354. (if arg (org-table-edit-field t)
  4355. (org-table-justify-field-maybe)
  4356. (call-interactively 'org-table-next-field)))))
  4357. ((run-hook-with-args-until-success
  4358. 'org-tab-after-check-for-table-hook))
  4359. ((eq arg t) ;; Global cycling
  4360. (org-cycle-internal-global))
  4361. ((and org-drawers org-drawer-regexp
  4362. (save-excursion
  4363. (beginning-of-line 1)
  4364. (looking-at org-drawer-regexp)))
  4365. ;; Toggle block visibility
  4366. (org-flag-drawer
  4367. (not (get-char-property (match-end 0) 'invisible))))
  4368. ((integerp arg)
  4369. ;; Show-subtree, ARG levels up from here.
  4370. (save-excursion
  4371. (org-back-to-heading)
  4372. (outline-up-heading (if (< arg 0) (- arg)
  4373. (- (funcall outline-level) arg)))
  4374. (org-show-subtree)))
  4375. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4376. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4377. (org-cycle-internal-local))
  4378. ;; TAB emulation and template completion
  4379. (buffer-read-only (org-back-to-heading))
  4380. ((run-hook-with-args-until-success
  4381. 'org-tab-after-check-for-cycling-hook))
  4382. ((org-try-structure-completion))
  4383. ((org-try-cdlatex-tab))
  4384. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4385. (or (not (bolp))
  4386. (not (looking-at outline-regexp))))
  4387. (call-interactively (global-key-binding "\t")))
  4388. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4389. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4390. (or (and (eq org-cycle-emulate-tab 'white)
  4391. (= (match-end 0) (point-at-eol)))
  4392. (and (eq org-cycle-emulate-tab 'whitestart)
  4393. (>= (match-end 0) pos))))
  4394. t
  4395. (eq org-cycle-emulate-tab t))
  4396. (call-interactively (global-key-binding "\t")))
  4397. (t (save-excursion
  4398. (org-back-to-heading)
  4399. (org-cycle)))))))
  4400. (defun org-cycle-internal-global ()
  4401. "Do the global cycling action."
  4402. (cond
  4403. ((and (eq last-command this-command)
  4404. (eq org-cycle-global-status 'overview))
  4405. ;; We just created the overview - now do table of contents
  4406. ;; This can be slow in very large buffers, so indicate action
  4407. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4408. (message "CONTENTS...")
  4409. (org-content)
  4410. (message "CONTENTS...done")
  4411. (setq org-cycle-global-status 'contents)
  4412. (run-hook-with-args 'org-cycle-hook 'contents))
  4413. ((and (eq last-command this-command)
  4414. (eq org-cycle-global-status 'contents))
  4415. ;; We just showed the table of contents - now show everything
  4416. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4417. (show-all)
  4418. (message "SHOW ALL")
  4419. (setq org-cycle-global-status 'all)
  4420. (run-hook-with-args 'org-cycle-hook 'all))
  4421. (t
  4422. ;; Default action: go to overview
  4423. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4424. (org-overview)
  4425. (message "OVERVIEW")
  4426. (setq org-cycle-global-status 'overview)
  4427. (run-hook-with-args 'org-cycle-hook 'overview))))
  4428. (defun org-cycle-internal-local ()
  4429. "Do the local cycling action."
  4430. (org-back-to-heading)
  4431. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4432. ;; First, some boundaries
  4433. (save-excursion
  4434. (org-back-to-heading)
  4435. (setq level (funcall outline-level))
  4436. (save-excursion
  4437. (beginning-of-line 2)
  4438. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4439. ; XEmacs does not have `next-single-char-property-change'
  4440. ; I'm not sure about Emacs 21.
  4441. (while (and (not (eobp)) ;; this is like `next-line'
  4442. (get-char-property (1- (point)) 'invisible))
  4443. (beginning-of-line 2))
  4444. (while (and (not (eobp)) ;; this is like `next-line'
  4445. (get-char-property (1- (point)) 'invisible))
  4446. (goto-char (next-single-char-property-change (point) 'invisible))
  4447. ;;;??? (or (bolp) (beginning-of-line 2))))
  4448. (and (eolp) (beginning-of-line 2))))
  4449. (setq eol (point)))
  4450. (outline-end-of-heading) (setq eoh (point))
  4451. (save-excursion
  4452. (outline-next-heading)
  4453. (setq has-children (and (org-at-heading-p t)
  4454. (> (funcall outline-level) level))))
  4455. (org-end-of-subtree t)
  4456. (unless (eobp)
  4457. (skip-chars-forward " \t\n")
  4458. (beginning-of-line 1) ; in case this is an item
  4459. )
  4460. (setq eos (if (eobp) (point) (1- (point)))))
  4461. ;; Find out what to do next and set `this-command'
  4462. (cond
  4463. ((= eos eoh)
  4464. ;; Nothing is hidden behind this heading
  4465. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4466. (message "EMPTY ENTRY")
  4467. (setq org-cycle-subtree-status nil)
  4468. (save-excursion
  4469. (goto-char eos)
  4470. (outline-next-heading)
  4471. (if (org-invisible-p) (org-flag-heading nil))))
  4472. ((and (or (>= eol eos)
  4473. (not (string-match "\\S-" (buffer-substring eol eos))))
  4474. (or has-children
  4475. (not (setq children-skipped
  4476. org-cycle-skip-children-state-if-no-children))))
  4477. ;; Entire subtree is hidden in one line: children view
  4478. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4479. (org-show-entry)
  4480. (show-children)
  4481. (message "CHILDREN")
  4482. (save-excursion
  4483. (goto-char eos)
  4484. (outline-next-heading)
  4485. (if (org-invisible-p) (org-flag-heading nil)))
  4486. (setq org-cycle-subtree-status 'children)
  4487. (run-hook-with-args 'org-cycle-hook 'children))
  4488. ((or children-skipped
  4489. (and (eq last-command this-command)
  4490. (eq org-cycle-subtree-status 'children)))
  4491. ;; We just showed the children, or no children are there,
  4492. ;; now show everything.
  4493. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4494. (org-show-subtree)
  4495. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4496. (setq org-cycle-subtree-status 'subtree)
  4497. (run-hook-with-args 'org-cycle-hook 'subtree))
  4498. (t
  4499. ;; Default action: hide the subtree.
  4500. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4501. (hide-subtree)
  4502. (message "FOLDED")
  4503. (setq org-cycle-subtree-status 'folded)
  4504. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4505. ;;;###autoload
  4506. (defun org-global-cycle (&optional arg)
  4507. "Cycle the global visibility. For details see `org-cycle'.
  4508. With C-u prefix arg, switch to startup visibility.
  4509. With a numeric prefix, show all headlines up to that level."
  4510. (interactive "P")
  4511. (let ((org-cycle-include-plain-lists
  4512. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4513. (cond
  4514. ((integerp arg)
  4515. (show-all)
  4516. (hide-sublevels arg)
  4517. (setq org-cycle-global-status 'contents))
  4518. ((equal arg '(4))
  4519. (org-set-startup-visibility)
  4520. (message "Startup visibility, plus VISIBILITY properties."))
  4521. (t
  4522. (org-cycle '(4))))))
  4523. (defun org-set-startup-visibility ()
  4524. "Set the visibility required by startup options and properties."
  4525. (cond
  4526. ((eq org-startup-folded t)
  4527. (org-cycle '(4)))
  4528. ((eq org-startup-folded 'content)
  4529. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4530. (org-cycle '(4)) (org-cycle '(4)))))
  4531. (if org-hide-block-startup (org-hide-block-all))
  4532. (org-set-visibility-according-to-property 'no-cleanup)
  4533. (org-cycle-hide-archived-subtrees 'all)
  4534. (org-cycle-hide-drawers 'all)
  4535. (org-cycle-show-empty-lines 'all))
  4536. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4537. "Switch subtree visibilities according to :VISIBILITY: property."
  4538. (interactive)
  4539. (let (org-show-entry-below state)
  4540. (save-excursion
  4541. (goto-char (point-min))
  4542. (while (re-search-forward
  4543. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4544. nil t)
  4545. (setq state (match-string 1))
  4546. (save-excursion
  4547. (org-back-to-heading t)
  4548. (hide-subtree)
  4549. (org-reveal)
  4550. (cond
  4551. ((equal state '("fold" "folded"))
  4552. (hide-subtree))
  4553. ((equal state "children")
  4554. (org-show-hidden-entry)
  4555. (show-children))
  4556. ((equal state "content")
  4557. (save-excursion
  4558. (save-restriction
  4559. (org-narrow-to-subtree)
  4560. (org-content))))
  4561. ((member state '("all" "showall"))
  4562. (show-subtree)))))
  4563. (unless no-cleanup
  4564. (org-cycle-hide-archived-subtrees 'all)
  4565. (org-cycle-hide-drawers 'all)
  4566. (org-cycle-show-empty-lines 'all)))))
  4567. (defun org-overview ()
  4568. "Switch to overview mode, showing only top-level headlines.
  4569. Really, this shows all headlines with level equal or greater than the level
  4570. of the first headline in the buffer. This is important, because if the
  4571. first headline is not level one, then (hide-sublevels 1) gives confusing
  4572. results."
  4573. (interactive)
  4574. (let ((level (save-excursion
  4575. (goto-char (point-min))
  4576. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4577. (progn
  4578. (goto-char (match-beginning 0))
  4579. (funcall outline-level))))))
  4580. (and level (hide-sublevels level))))
  4581. (defun org-content (&optional arg)
  4582. "Show all headlines in the buffer, like a table of contents.
  4583. With numerical argument N, show content up to level N."
  4584. (interactive "P")
  4585. (save-excursion
  4586. ;; Visit all headings and show their offspring
  4587. (and (integerp arg) (org-overview))
  4588. (goto-char (point-max))
  4589. (catch 'exit
  4590. (while (and (progn (condition-case nil
  4591. (outline-previous-visible-heading 1)
  4592. (error (goto-char (point-min))))
  4593. t)
  4594. (looking-at outline-regexp))
  4595. (if (integerp arg)
  4596. (show-children (1- arg))
  4597. (show-branches))
  4598. (if (bobp) (throw 'exit nil))))))
  4599. (defun org-optimize-window-after-visibility-change (state)
  4600. "Adjust the window after a change in outline visibility.
  4601. This function is the default value of the hook `org-cycle-hook'."
  4602. (when (get-buffer-window (current-buffer))
  4603. (cond
  4604. ((eq state 'content) nil)
  4605. ((eq state 'all) nil)
  4606. ((eq state 'folded) nil)
  4607. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4608. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4609. ;; FIXME: no longer in use
  4610. (defun org-compact-display-after-subtree-move ()
  4611. "Show a compacter version of the tree of the entry's parent."
  4612. (save-excursion
  4613. (if (org-up-heading-safe)
  4614. (progn
  4615. (hide-subtree)
  4616. (show-entry)
  4617. (show-children)
  4618. (org-cycle-show-empty-lines 'children)
  4619. (org-cycle-hide-drawers 'children))
  4620. (org-overview))))
  4621. (defun org-remove-empty-overlays-at (pos)
  4622. "Remove outline overlays that do not contain non-white stuff."
  4623. (mapc
  4624. (lambda (o)
  4625. (and (eq 'outline (org-overlay-get o 'invisible))
  4626. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4627. (org-overlay-end o))))
  4628. (org-delete-overlay o)))
  4629. (org-overlays-at pos)))
  4630. (defun org-clean-visibility-after-subtree-move ()
  4631. "Fix visibility issues after moving a subtree."
  4632. ;; First, find a reasonable region to look at:
  4633. ;; Start two siblings above, end three below
  4634. (let* ((beg (save-excursion
  4635. (and (outline-get-last-sibling)
  4636. (outline-get-last-sibling))
  4637. (point)))
  4638. (end (save-excursion
  4639. (and (outline-get-next-sibling)
  4640. (outline-get-next-sibling)
  4641. (outline-get-next-sibling))
  4642. (if (org-at-heading-p)
  4643. (point-at-eol)
  4644. (point))))
  4645. (level (looking-at "\\*+"))
  4646. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4647. (save-excursion
  4648. (save-restriction
  4649. (narrow-to-region beg end)
  4650. (when re
  4651. ;; Properly fold already folded siblings
  4652. (goto-char (point-min))
  4653. (while (re-search-forward re nil t)
  4654. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4655. (hide-entry))))
  4656. (org-cycle-show-empty-lines 'overview)
  4657. (org-cycle-hide-drawers 'overview)))))
  4658. (defun org-cycle-show-empty-lines (state)
  4659. "Show empty lines above all visible headlines.
  4660. The region to be covered depends on STATE when called through
  4661. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4662. entire buffer covered. Note that an empty line is only shown if there
  4663. are at least `org-cycle-separator-lines' empty lines before the headline."
  4664. (when (> org-cycle-separator-lines 0)
  4665. (save-excursion
  4666. (let* ((n org-cycle-separator-lines)
  4667. (re (cond
  4668. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4669. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4670. (t (let ((ns (number-to-string (- n 2))))
  4671. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4672. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4673. beg end)
  4674. (cond
  4675. ((memq state '(overview contents t))
  4676. (setq beg (point-min) end (point-max)))
  4677. ((memq state '(children folded))
  4678. (setq beg (point) end (progn (org-end-of-subtree t t)
  4679. (beginning-of-line 2)
  4680. (point)))))
  4681. (when beg
  4682. (goto-char beg)
  4683. (while (re-search-forward re end t)
  4684. (if (not (get-char-property (match-end 1) 'invisible))
  4685. (outline-flag-region
  4686. (match-beginning 1) (match-end 1) nil)))))))
  4687. ;; Never hide empty lines at the end of the file.
  4688. (save-excursion
  4689. (goto-char (point-max))
  4690. (outline-previous-heading)
  4691. (outline-end-of-heading)
  4692. (if (and (looking-at "[ \t\n]+")
  4693. (= (match-end 0) (point-max)))
  4694. (outline-flag-region (point) (match-end 0) nil))))
  4695. (defun org-show-empty-lines-in-parent ()
  4696. "Move to the parent and re-show empty lines before visible headlines."
  4697. (save-excursion
  4698. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4699. (org-cycle-show-empty-lines context))))
  4700. (defun org-cycle-hide-drawers (state)
  4701. "Re-hide all drawers after a visibility state change."
  4702. (when (and (org-mode-p)
  4703. (not (memq state '(overview folded contents))))
  4704. (save-excursion
  4705. (let* ((globalp (memq state '(contents all)))
  4706. (beg (if globalp (point-min) (point)))
  4707. (end (if globalp (point-max)
  4708. (if (eq state 'children)
  4709. (save-excursion (outline-next-heading) (point))
  4710. (org-end-of-subtree t)))))
  4711. (goto-char beg)
  4712. (while (re-search-forward org-drawer-regexp end t)
  4713. (org-flag-drawer t))))))
  4714. (defun org-flag-drawer (flag)
  4715. (save-excursion
  4716. (beginning-of-line 1)
  4717. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4718. (let ((b (match-end 0))
  4719. (outline-regexp org-outline-regexp))
  4720. (if (re-search-forward
  4721. "^[ \t]*:END:"
  4722. (save-excursion (outline-next-heading) (point)) t)
  4723. (outline-flag-region b (point-at-eol) flag)
  4724. (error ":END: line missing at position %s" b))))))
  4725. (defun org-subtree-end-visible-p ()
  4726. "Is the end of the current subtree visible?"
  4727. (pos-visible-in-window-p
  4728. (save-excursion (org-end-of-subtree t) (point))))
  4729. (defun org-first-headline-recenter (&optional N)
  4730. "Move cursor to the first headline and recenter the headline.
  4731. Optional argument N means, put the headline into the Nth line of the window."
  4732. (goto-char (point-min))
  4733. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4734. (beginning-of-line)
  4735. (recenter (prefix-numeric-value N))))
  4736. ;;; Folding of blocks
  4737. (defconst org-block-regexp
  4738. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4739. "Regular expression for hiding blocks.")
  4740. (defvar org-hide-block-overlays nil
  4741. "Overays hiding blocks.")
  4742. (make-variable-buffer-local 'org-hide-block-overlays)
  4743. (defun org-block-map (function &optional start end)
  4744. "Call func at the head of all source blocks in the current
  4745. buffer. Optional arguments START and END can be used to limit
  4746. the range."
  4747. (let ((start (or start (point-min)))
  4748. (end (or end (point-max))))
  4749. (save-excursion
  4750. (goto-char start)
  4751. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4752. (save-excursion
  4753. (save-match-data
  4754. (goto-char (match-beginning 0))
  4755. (funcall function)))))))
  4756. (defun org-hide-block-toggle-all ()
  4757. "Toggle the visibility of all blocks in the current buffer."
  4758. (org-block-map #'org-hide-block-toggle))
  4759. (defun org-hide-block-all ()
  4760. "Fold all blocks in the current buffer."
  4761. (interactive)
  4762. (org-show-block-all)
  4763. (org-block-map #'org-hide-block-toggle-maybe))
  4764. (defun org-show-block-all ()
  4765. "Unfold all blocks in the current buffer."
  4766. (mapc 'org-delete-overlay org-hide-block-overlays)
  4767. (setq org-hide-block-overlays nil))
  4768. (defun org-hide-block-toggle-maybe ()
  4769. "Toggle visibility of block at point."
  4770. (interactive)
  4771. (let ((case-fold-search t))
  4772. (if (save-excursion
  4773. (beginning-of-line 1)
  4774. (looking-at org-block-regexp))
  4775. (progn (org-hide-block-toggle)
  4776. t) ;; to signal that we took action
  4777. nil))) ;; to signal that we did not
  4778. (defun org-hide-block-toggle (&optional force)
  4779. "Toggle the visibility of the current block."
  4780. (interactive)
  4781. (save-excursion
  4782. (beginning-of-line)
  4783. (if (re-search-forward org-block-regexp nil t)
  4784. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4785. (end (match-end 0)) ;; end of entire body
  4786. ov)
  4787. (if (memq t (mapcar (lambda (overlay)
  4788. (eq (org-overlay-get overlay 'invisible)
  4789. 'org-hide-block))
  4790. (org-overlays-at start)))
  4791. (if (or (not force) (eq force 'off))
  4792. (mapc (lambda (ov)
  4793. (when (member ov org-hide-block-overlays)
  4794. (setq org-hide-block-overlays
  4795. (delq ov org-hide-block-overlays)))
  4796. (when (eq (org-overlay-get ov 'invisible)
  4797. 'org-hide-block)
  4798. (org-delete-overlay ov)))
  4799. (org-overlays-at start)))
  4800. (setq ov (org-make-overlay start end))
  4801. (org-overlay-put ov 'invisible 'org-hide-block)
  4802. ;; make the block accessible to isearch
  4803. (org-overlay-put
  4804. ov 'isearch-open-invisible
  4805. (lambda (ov)
  4806. (when (member ov org-hide-block-overlays)
  4807. (setq org-hide-block-overlays
  4808. (delq ov org-hide-block-overlays)))
  4809. (when (eq (org-overlay-get ov 'invisible)
  4810. 'org-hide-block)
  4811. (org-delete-overlay ov))))
  4812. (push ov org-hide-block-overlays)))
  4813. (error "Not looking at a source block"))))
  4814. ;; org-tab-after-check-for-cycling-hook
  4815. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4816. ;; Remove overlays when changing major mode
  4817. (add-hook 'org-mode-hook
  4818. (lambda () (org-add-hook 'change-major-mode-hook
  4819. 'org-show-block-all 'append 'local)))
  4820. ;;; Org-goto
  4821. (defvar org-goto-window-configuration nil)
  4822. (defvar org-goto-marker nil)
  4823. (defvar org-goto-map
  4824. (let ((map (make-sparse-keymap)))
  4825. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4826. (while (setq cmd (pop cmds))
  4827. (substitute-key-definition cmd cmd map global-map)))
  4828. (suppress-keymap map)
  4829. (org-defkey map "\C-m" 'org-goto-ret)
  4830. (org-defkey map [(return)] 'org-goto-ret)
  4831. (org-defkey map [(left)] 'org-goto-left)
  4832. (org-defkey map [(right)] 'org-goto-right)
  4833. (org-defkey map [(control ?g)] 'org-goto-quit)
  4834. (org-defkey map "\C-i" 'org-cycle)
  4835. (org-defkey map [(tab)] 'org-cycle)
  4836. (org-defkey map [(down)] 'outline-next-visible-heading)
  4837. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4838. (if org-goto-auto-isearch
  4839. (if (fboundp 'define-key-after)
  4840. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4841. nil)
  4842. (org-defkey map "q" 'org-goto-quit)
  4843. (org-defkey map "n" 'outline-next-visible-heading)
  4844. (org-defkey map "p" 'outline-previous-visible-heading)
  4845. (org-defkey map "f" 'outline-forward-same-level)
  4846. (org-defkey map "b" 'outline-backward-same-level)
  4847. (org-defkey map "u" 'outline-up-heading))
  4848. (org-defkey map "/" 'org-occur)
  4849. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4850. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4851. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4852. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4853. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4854. map))
  4855. (defconst org-goto-help
  4856. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4857. RET=jump to location [Q]uit and return to previous location
  4858. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4859. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4860. ;; FIXME: Docstring doe not mention both interfaces
  4861. (defun org-goto (&optional alternative-interface)
  4862. "Look up a different location in the current file, keeping current visibility.
  4863. When you want look-up or go to a different location in a document, the
  4864. fastest way is often to fold the entire buffer and then dive into the tree.
  4865. This method has the disadvantage, that the previous location will be folded,
  4866. which may not be what you want.
  4867. This command works around this by showing a copy of the current buffer
  4868. in an indirect buffer, in overview mode. You can dive into the tree in
  4869. that copy, use org-occur and incremental search to find a location.
  4870. When pressing RET or `Q', the command returns to the original buffer in
  4871. which the visibility is still unchanged. After RET is will also jump to
  4872. the location selected in the indirect buffer and expose the
  4873. the headline hierarchy above."
  4874. (interactive "P")
  4875. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4876. (org-refile-use-outline-path t)
  4877. (org-refile-target-verify-function nil)
  4878. (interface
  4879. (if (not alternative-interface)
  4880. org-goto-interface
  4881. (if (eq org-goto-interface 'outline)
  4882. 'outline-path-completion
  4883. 'outline)))
  4884. (org-goto-start-pos (point))
  4885. (selected-point
  4886. (if (eq interface 'outline)
  4887. (car (org-get-location (current-buffer) org-goto-help))
  4888. (nth 3 (org-refile-get-location "Goto: ")))))
  4889. (if selected-point
  4890. (progn
  4891. (org-mark-ring-push org-goto-start-pos)
  4892. (goto-char selected-point)
  4893. (if (or (org-invisible-p) (org-invisible-p2))
  4894. (org-show-context 'org-goto)))
  4895. (message "Quit"))))
  4896. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4897. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4898. (defvar org-goto-local-auto-isearch-map) ; defined below
  4899. (defun org-get-location (buf help)
  4900. "Let the user select a location in the Org-mode buffer BUF.
  4901. This function uses a recursive edit. It returns the selected position
  4902. or nil."
  4903. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4904. (isearch-hide-immediately nil)
  4905. (isearch-search-fun-function
  4906. (lambda () 'org-goto-local-search-headings))
  4907. (org-goto-selected-point org-goto-exit-command))
  4908. (save-excursion
  4909. (save-window-excursion
  4910. (delete-other-windows)
  4911. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4912. (switch-to-buffer
  4913. (condition-case nil
  4914. (make-indirect-buffer (current-buffer) "*org-goto*")
  4915. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4916. (with-output-to-temp-buffer "*Help*"
  4917. (princ help))
  4918. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4919. (setq buffer-read-only nil)
  4920. (let ((org-startup-truncated t)
  4921. (org-startup-folded nil)
  4922. (org-startup-align-all-tables nil))
  4923. (org-mode)
  4924. (org-overview))
  4925. (setq buffer-read-only t)
  4926. (if (and (boundp 'org-goto-start-pos)
  4927. (integer-or-marker-p org-goto-start-pos))
  4928. (let ((org-show-hierarchy-above t)
  4929. (org-show-siblings t)
  4930. (org-show-following-heading t))
  4931. (goto-char org-goto-start-pos)
  4932. (and (org-invisible-p) (org-show-context)))
  4933. (goto-char (point-min)))
  4934. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4935. (message "Select location and press RET")
  4936. (use-local-map org-goto-map)
  4937. (recursive-edit)
  4938. ))
  4939. (kill-buffer "*org-goto*")
  4940. (cons org-goto-selected-point org-goto-exit-command)))
  4941. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4942. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4943. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4944. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4945. (defun org-goto-local-search-headings (string bound noerror)
  4946. "Search and make sure that any matches are in headlines."
  4947. (catch 'return
  4948. (while (if isearch-forward
  4949. (search-forward string bound noerror)
  4950. (search-backward string bound noerror))
  4951. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4952. (and (member :headline context)
  4953. (not (member :tags context))))
  4954. (throw 'return (point))))))
  4955. (defun org-goto-local-auto-isearch ()
  4956. "Start isearch."
  4957. (interactive)
  4958. (goto-char (point-min))
  4959. (let ((keys (this-command-keys)))
  4960. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4961. (isearch-mode t)
  4962. (isearch-process-search-char (string-to-char keys)))))
  4963. (defun org-goto-ret (&optional arg)
  4964. "Finish `org-goto' by going to the new location."
  4965. (interactive "P")
  4966. (setq org-goto-selected-point (point)
  4967. org-goto-exit-command 'return)
  4968. (throw 'exit nil))
  4969. (defun org-goto-left ()
  4970. "Finish `org-goto' by going to the new location."
  4971. (interactive)
  4972. (if (org-on-heading-p)
  4973. (progn
  4974. (beginning-of-line 1)
  4975. (setq org-goto-selected-point (point)
  4976. org-goto-exit-command 'left)
  4977. (throw 'exit nil))
  4978. (error "Not on a heading")))
  4979. (defun org-goto-right ()
  4980. "Finish `org-goto' by going to the new location."
  4981. (interactive)
  4982. (if (org-on-heading-p)
  4983. (progn
  4984. (setq org-goto-selected-point (point)
  4985. org-goto-exit-command 'right)
  4986. (throw 'exit nil))
  4987. (error "Not on a heading")))
  4988. (defun org-goto-quit ()
  4989. "Finish `org-goto' without cursor motion."
  4990. (interactive)
  4991. (setq org-goto-selected-point nil)
  4992. (setq org-goto-exit-command 'quit)
  4993. (throw 'exit nil))
  4994. ;;; Indirect buffer display of subtrees
  4995. (defvar org-indirect-dedicated-frame nil
  4996. "This is the frame being used for indirect tree display.")
  4997. (defvar org-last-indirect-buffer nil)
  4998. (defun org-tree-to-indirect-buffer (&optional arg)
  4999. "Create indirect buffer and narrow it to current subtree.
  5000. With numerical prefix ARG, go up to this level and then take that tree.
  5001. If ARG is negative, go up that many levels.
  5002. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5003. indirect buffer previously made with this command, to avoid proliferation of
  5004. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5005. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5006. is kept so that you can work with several indirect buffers at the same time.
  5007. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5008. requests that a new frame be made for the new buffer, so that the dedicated
  5009. frame is not changed."
  5010. (interactive "P")
  5011. (let ((cbuf (current-buffer))
  5012. (cwin (selected-window))
  5013. (pos (point))
  5014. beg end level heading ibuf)
  5015. (save-excursion
  5016. (org-back-to-heading t)
  5017. (when (numberp arg)
  5018. (setq level (org-outline-level))
  5019. (if (< arg 0) (setq arg (+ level arg)))
  5020. (while (> (setq level (org-outline-level)) arg)
  5021. (outline-up-heading 1 t)))
  5022. (setq beg (point)
  5023. heading (org-get-heading))
  5024. (org-end-of-subtree t) (setq end (point)))
  5025. (if (and (buffer-live-p org-last-indirect-buffer)
  5026. (not (eq org-indirect-buffer-display 'new-frame))
  5027. (not arg))
  5028. (kill-buffer org-last-indirect-buffer))
  5029. (setq ibuf (org-get-indirect-buffer cbuf)
  5030. org-last-indirect-buffer ibuf)
  5031. (cond
  5032. ((or (eq org-indirect-buffer-display 'new-frame)
  5033. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5034. (select-frame (make-frame))
  5035. (delete-other-windows)
  5036. (switch-to-buffer ibuf)
  5037. (org-set-frame-title heading))
  5038. ((eq org-indirect-buffer-display 'dedicated-frame)
  5039. (raise-frame
  5040. (select-frame (or (and org-indirect-dedicated-frame
  5041. (frame-live-p org-indirect-dedicated-frame)
  5042. org-indirect-dedicated-frame)
  5043. (setq org-indirect-dedicated-frame (make-frame)))))
  5044. (delete-other-windows)
  5045. (switch-to-buffer ibuf)
  5046. (org-set-frame-title (concat "Indirect: " heading)))
  5047. ((eq org-indirect-buffer-display 'current-window)
  5048. (switch-to-buffer ibuf))
  5049. ((eq org-indirect-buffer-display 'other-window)
  5050. (pop-to-buffer ibuf))
  5051. (t (error "Invalid value.")))
  5052. (if (featurep 'xemacs)
  5053. (save-excursion (org-mode) (turn-on-font-lock)))
  5054. (narrow-to-region beg end)
  5055. (show-all)
  5056. (goto-char pos)
  5057. (and (window-live-p cwin) (select-window cwin))))
  5058. (defun org-get-indirect-buffer (&optional buffer)
  5059. (setq buffer (or buffer (current-buffer)))
  5060. (let ((n 1) (base (buffer-name buffer)) bname)
  5061. (while (buffer-live-p
  5062. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5063. (setq n (1+ n)))
  5064. (condition-case nil
  5065. (make-indirect-buffer buffer bname 'clone)
  5066. (error (make-indirect-buffer buffer bname)))))
  5067. (defun org-set-frame-title (title)
  5068. "Set the title of the current frame to the string TITLE."
  5069. ;; FIXME: how to name a single frame in XEmacs???
  5070. (unless (featurep 'xemacs)
  5071. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5072. ;;;; Structure editing
  5073. ;;; Inserting headlines
  5074. (defun org-previous-line-empty-p ()
  5075. (save-excursion
  5076. (and (not (bobp))
  5077. (or (beginning-of-line 0) t)
  5078. (save-match-data
  5079. (looking-at "[ \t]*$")))))
  5080. (defun org-insert-heading (&optional force-heading)
  5081. "Insert a new heading or item with same depth at point.
  5082. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5083. If point is at the beginning of a headline, insert a sibling before the
  5084. current headline. If point is not at the beginning, do not split the line,
  5085. but create the new headline after the current line."
  5086. (interactive "P")
  5087. (if (= (buffer-size) 0)
  5088. (insert "\n* ")
  5089. (when (or force-heading (not (org-insert-item)))
  5090. (let* ((empty-line-p nil)
  5091. (head (save-excursion
  5092. (condition-case nil
  5093. (progn
  5094. (org-back-to-heading)
  5095. (setq empty-line-p (org-previous-line-empty-p))
  5096. (match-string 0))
  5097. (error "*"))))
  5098. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5099. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5100. pos hide-previous previous-pos)
  5101. (cond
  5102. ((and (org-on-heading-p) (bolp)
  5103. (or (bobp)
  5104. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5105. ;; insert before the current line
  5106. (open-line (if blank 2 1)))
  5107. ((and (bolp)
  5108. (not org-insert-heading-respect-content)
  5109. (or (bobp)
  5110. (save-excursion
  5111. (backward-char 1) (not (org-invisible-p)))))
  5112. ;; insert right here
  5113. nil)
  5114. (t
  5115. ;; somewhere in the line
  5116. (save-excursion
  5117. (setq previous-pos (point-at-bol))
  5118. (end-of-line)
  5119. (setq hide-previous (org-invisible-p)))
  5120. (and org-insert-heading-respect-content (org-show-subtree))
  5121. (let ((split
  5122. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5123. (save-excursion
  5124. (let ((p (point)))
  5125. (goto-char (point-at-bol))
  5126. (and (looking-at org-complex-heading-regexp)
  5127. (> p (match-beginning 4)))))))
  5128. tags pos)
  5129. (cond
  5130. (org-insert-heading-respect-content
  5131. (org-end-of-subtree nil t)
  5132. (or (bolp) (newline))
  5133. (or (org-previous-line-empty-p)
  5134. (and blank (newline)))
  5135. (open-line 1))
  5136. ((org-on-heading-p)
  5137. (when hide-previous
  5138. (show-children)
  5139. (org-show-entry))
  5140. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5141. (setq tags (and (match-end 2) (match-string 2)))
  5142. (and (match-end 1)
  5143. (delete-region (match-beginning 1) (match-end 1)))
  5144. (setq pos (point-at-bol))
  5145. (or split (end-of-line 1))
  5146. (delete-horizontal-space)
  5147. (newline (if blank 2 1))
  5148. (when tags
  5149. (save-excursion
  5150. (goto-char pos)
  5151. (end-of-line 1)
  5152. (insert " " tags)
  5153. (org-set-tags nil 'align))))
  5154. (t
  5155. (or split (end-of-line 1))
  5156. (newline (if blank 2 1)))))))
  5157. (insert head) (just-one-space)
  5158. (setq pos (point))
  5159. (end-of-line 1)
  5160. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5161. (when (and org-insert-heading-respect-content hide-previous)
  5162. (save-excursion
  5163. (goto-char previous-pos)
  5164. (hide-subtree)))
  5165. (run-hooks 'org-insert-heading-hook)))))
  5166. (defun org-get-heading (&optional no-tags)
  5167. "Return the heading of the current entry, without the stars."
  5168. (save-excursion
  5169. (org-back-to-heading t)
  5170. (if (looking-at
  5171. (if no-tags
  5172. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5173. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5174. (match-string 1) "")))
  5175. (defun org-heading-components ()
  5176. "Return the components of the current heading.
  5177. This is a list with the following elements:
  5178. - the level as an integer
  5179. - the reduced level, different if `org-odd-levels-only' is set.
  5180. - the TODO keyword, or nil
  5181. - the priority character, like ?A, or nil if no priority is given
  5182. - the headline text itself, or the tags string if no headline text
  5183. - the tags string, or nil."
  5184. (save-excursion
  5185. (org-back-to-heading t)
  5186. (if (looking-at org-complex-heading-regexp)
  5187. (list (length (match-string 1))
  5188. (org-reduced-level (length (match-string 1)))
  5189. (org-match-string-no-properties 2)
  5190. (and (match-end 3) (aref (match-string 3) 2))
  5191. (org-match-string-no-properties 4)
  5192. (org-match-string-no-properties 5)))))
  5193. (defun org-get-entry ()
  5194. "Get the entry text, after heading, entire subtree."
  5195. (save-excursion
  5196. (org-back-to-heading t)
  5197. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5198. (defun org-insert-heading-after-current ()
  5199. "Insert a new heading with same level as current, after current subtree."
  5200. (interactive)
  5201. (org-back-to-heading)
  5202. (org-insert-heading)
  5203. (org-move-subtree-down)
  5204. (end-of-line 1))
  5205. (defun org-insert-heading-respect-content ()
  5206. (interactive)
  5207. (let ((org-insert-heading-respect-content t))
  5208. (org-insert-heading t)))
  5209. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5210. (interactive "P")
  5211. (let ((org-insert-heading-respect-content t))
  5212. (org-insert-todo-heading force-state t)))
  5213. (defun org-insert-todo-heading (arg &optional force-heading)
  5214. "Insert a new heading with the same level and TODO state as current heading.
  5215. If the heading has no TODO state, or if the state is DONE, use the first
  5216. state (TODO by default). Also with prefix arg, force first state."
  5217. (interactive "P")
  5218. (when (or force-heading (not (org-insert-item 'checkbox)))
  5219. (org-insert-heading force-heading)
  5220. (save-excursion
  5221. (org-back-to-heading)
  5222. (outline-previous-heading)
  5223. (looking-at org-todo-line-regexp))
  5224. (let*
  5225. ((new-mark-x
  5226. (if (or arg
  5227. (not (match-beginning 2))
  5228. (member (match-string 2) org-done-keywords))
  5229. (car org-todo-keywords-1)
  5230. (match-string 2)))
  5231. (new-mark
  5232. (or
  5233. (run-hook-with-args-until-success
  5234. 'org-todo-get-default-hook new-mark-x nil)
  5235. new-mark-x)))
  5236. (beginning-of-line 1)
  5237. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5238. (if org-treat-insert-todo-heading-as-state-change
  5239. (org-todo new-mark)
  5240. (insert new-mark " "))))
  5241. (when org-provide-todo-statistics
  5242. (org-update-parent-todo-statistics))))
  5243. (defun org-insert-subheading (arg)
  5244. "Insert a new subheading and demote it.
  5245. Works for outline headings and for plain lists alike."
  5246. (interactive "P")
  5247. (org-insert-heading arg)
  5248. (cond
  5249. ((org-on-heading-p) (org-do-demote))
  5250. ((org-at-item-p) (org-indent-item 1))))
  5251. (defun org-insert-todo-subheading (arg)
  5252. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5253. Works for outline headings and for plain lists alike."
  5254. (interactive "P")
  5255. (org-insert-todo-heading arg)
  5256. (cond
  5257. ((org-on-heading-p) (org-do-demote))
  5258. ((org-at-item-p) (org-indent-item 1))))
  5259. ;;; Promotion and Demotion
  5260. (defvar org-after-demote-entry-hook nil
  5261. "Hook run after an entry has been demoted.
  5262. The cursor will be at the beginning of the entry.
  5263. When a subtree is being demoted, the hook will be called for each node.")
  5264. (defvar org-after-promote-entry-hook nil
  5265. "Hook run after an entry has been promoted.
  5266. The cursor will be at the beginning of the entry.
  5267. When a subtree is being promoted, the hook will be called for each node.")
  5268. (defun org-promote-subtree ()
  5269. "Promote the entire subtree.
  5270. See also `org-promote'."
  5271. (interactive)
  5272. (save-excursion
  5273. (org-map-tree 'org-promote))
  5274. (org-fix-position-after-promote))
  5275. (defun org-demote-subtree ()
  5276. "Demote the entire subtree. See `org-demote'.
  5277. See also `org-promote'."
  5278. (interactive)
  5279. (save-excursion
  5280. (org-map-tree 'org-demote))
  5281. (org-fix-position-after-promote))
  5282. (defun org-do-promote ()
  5283. "Promote the current heading higher up the tree.
  5284. If the region is active in `transient-mark-mode', promote all headings
  5285. in the region."
  5286. (interactive)
  5287. (save-excursion
  5288. (if (org-region-active-p)
  5289. (org-map-region 'org-promote (region-beginning) (region-end))
  5290. (org-promote)))
  5291. (org-fix-position-after-promote))
  5292. (defun org-do-demote ()
  5293. "Demote the current heading lower down the tree.
  5294. If the region is active in `transient-mark-mode', demote all headings
  5295. in the region."
  5296. (interactive)
  5297. (save-excursion
  5298. (if (org-region-active-p)
  5299. (org-map-region 'org-demote (region-beginning) (region-end))
  5300. (org-demote)))
  5301. (org-fix-position-after-promote))
  5302. (defun org-fix-position-after-promote ()
  5303. "Make sure that after pro/demotion cursor position is right."
  5304. (let ((pos (point)))
  5305. (when (save-excursion
  5306. (beginning-of-line 1)
  5307. (looking-at org-todo-line-regexp)
  5308. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5309. (cond ((eobp) (insert " "))
  5310. ((eolp) (insert " "))
  5311. ((equal (char-after) ?\ ) (forward-char 1))))))
  5312. (defun org-reduced-level (l)
  5313. "Compute the effective level of a heading.
  5314. This takes into account the setting of `org-odd-levels-only'."
  5315. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5316. (defun org-get-valid-level (level &optional change)
  5317. "Rectify a level change under the influence of `org-odd-levels-only'
  5318. LEVEL is a current level, CHANGE is by how much the level should be
  5319. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5320. even level numbers will become the next higher odd number."
  5321. (if org-odd-levels-only
  5322. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5323. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5324. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5325. (max 1 (+ level (or change 0)))))
  5326. (if (boundp 'define-obsolete-function-alias)
  5327. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5328. (define-obsolete-function-alias 'org-get-legal-level
  5329. 'org-get-valid-level)
  5330. (define-obsolete-function-alias 'org-get-legal-level
  5331. 'org-get-valid-level "23.1")))
  5332. (defun org-promote ()
  5333. "Promote the current heading higher up the tree.
  5334. If the region is active in `transient-mark-mode', promote all headings
  5335. in the region."
  5336. (org-back-to-heading t)
  5337. (let* ((level (save-match-data (funcall outline-level)))
  5338. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5339. (diff (abs (- level (length up-head) -1))))
  5340. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5341. (replace-match up-head nil t)
  5342. ;; Fixup tag positioning
  5343. (and org-auto-align-tags (org-set-tags nil t))
  5344. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5345. (run-hooks 'org-after-promote-entry-hook)))
  5346. (defun org-demote ()
  5347. "Demote the current heading lower down the tree.
  5348. If the region is active in `transient-mark-mode', demote all headings
  5349. in the region."
  5350. (org-back-to-heading t)
  5351. (let* ((level (save-match-data (funcall outline-level)))
  5352. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5353. (diff (abs (- level (length down-head) -1))))
  5354. (replace-match down-head nil t)
  5355. ;; Fixup tag positioning
  5356. (and org-auto-align-tags (org-set-tags nil t))
  5357. (if org-adapt-indentation (org-fixup-indentation diff))
  5358. (run-hooks 'org-after-demote-entry-hook)))
  5359. (defun org-map-tree (fun)
  5360. "Call FUN for every heading underneath the current one."
  5361. (org-back-to-heading)
  5362. (let ((level (funcall outline-level)))
  5363. (save-excursion
  5364. (funcall fun)
  5365. (while (and (progn
  5366. (outline-next-heading)
  5367. (> (funcall outline-level) level))
  5368. (not (eobp)))
  5369. (funcall fun)))))
  5370. (defun org-map-region (fun beg end)
  5371. "Call FUN for every heading between BEG and END."
  5372. (let ((org-ignore-region t))
  5373. (save-excursion
  5374. (setq end (copy-marker end))
  5375. (goto-char beg)
  5376. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5377. (< (point) end))
  5378. (funcall fun))
  5379. (while (and (progn
  5380. (outline-next-heading)
  5381. (< (point) end))
  5382. (not (eobp)))
  5383. (funcall fun)))))
  5384. (defun org-fixup-indentation (diff)
  5385. "Change the indentation in the current entry by DIFF
  5386. However, if any line in the current entry has no indentation, or if it
  5387. would end up with no indentation after the change, nothing at all is done."
  5388. (save-excursion
  5389. (let ((end (save-excursion (outline-next-heading)
  5390. (point-marker)))
  5391. (prohibit (if (> diff 0)
  5392. "^\\S-"
  5393. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5394. col)
  5395. (unless (save-excursion (end-of-line 1)
  5396. (re-search-forward prohibit end t))
  5397. (while (and (< (point) end)
  5398. (re-search-forward "^[ \t]+" end t))
  5399. (goto-char (match-end 0))
  5400. (setq col (current-column))
  5401. (if (< diff 0) (replace-match ""))
  5402. (org-indent-to-column (+ diff col))))
  5403. (move-marker end nil))))
  5404. (defun org-convert-to-odd-levels ()
  5405. "Convert an org-mode file with all levels allowed to one with odd levels.
  5406. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5407. level 5 etc."
  5408. (interactive)
  5409. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5410. (let ((org-odd-levels-only nil) n)
  5411. (save-excursion
  5412. (goto-char (point-min))
  5413. (while (re-search-forward "^\\*\\*+ " nil t)
  5414. (setq n (- (length (match-string 0)) 2))
  5415. (while (>= (setq n (1- n)) 0)
  5416. (org-demote))
  5417. (end-of-line 1))))))
  5418. (defun org-convert-to-oddeven-levels ()
  5419. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5420. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5421. section with an even level, conversion would destroy the structure of the file. An error
  5422. is signaled in this case."
  5423. (interactive)
  5424. (goto-char (point-min))
  5425. ;; First check if there are no even levels
  5426. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5427. (org-show-context t)
  5428. (error "Not all levels are odd in this file. Conversion not possible."))
  5429. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5430. (let ((org-odd-levels-only nil) n)
  5431. (save-excursion
  5432. (goto-char (point-min))
  5433. (while (re-search-forward "^\\*\\*+ " nil t)
  5434. (setq n (/ (1- (length (match-string 0))) 2))
  5435. (while (>= (setq n (1- n)) 0)
  5436. (org-promote))
  5437. (end-of-line 1))))))
  5438. (defun org-tr-level (n)
  5439. "Make N odd if required."
  5440. (if org-odd-levels-only (1+ (/ n 2)) n))
  5441. ;;; Vertical tree motion, cutting and pasting of subtrees
  5442. (defun org-move-subtree-up (&optional arg)
  5443. "Move the current subtree up past ARG headlines of the same level."
  5444. (interactive "p")
  5445. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5446. (defun org-move-subtree-down (&optional arg)
  5447. "Move the current subtree down past ARG headlines of the same level."
  5448. (interactive "p")
  5449. (setq arg (prefix-numeric-value arg))
  5450. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5451. 'outline-get-last-sibling))
  5452. (ins-point (make-marker))
  5453. (cnt (abs arg))
  5454. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5455. ;; Select the tree
  5456. (org-back-to-heading)
  5457. (setq beg0 (point))
  5458. (save-excursion
  5459. (setq ne-beg (org-back-over-empty-lines))
  5460. (setq beg (point)))
  5461. (save-match-data
  5462. (save-excursion (outline-end-of-heading)
  5463. (setq folded (org-invisible-p)))
  5464. (outline-end-of-subtree))
  5465. (outline-next-heading)
  5466. (setq ne-end (org-back-over-empty-lines))
  5467. (setq end (point))
  5468. (goto-char beg0)
  5469. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5470. ;; include less whitespace
  5471. (save-excursion
  5472. (goto-char beg)
  5473. (forward-line (- ne-beg ne-end))
  5474. (setq beg (point))))
  5475. ;; Find insertion point, with error handling
  5476. (while (> cnt 0)
  5477. (or (and (funcall movfunc) (looking-at outline-regexp))
  5478. (progn (goto-char beg0)
  5479. (error "Cannot move past superior level or buffer limit")))
  5480. (setq cnt (1- cnt)))
  5481. (if (> arg 0)
  5482. ;; Moving forward - still need to move over subtree
  5483. (progn (org-end-of-subtree t t)
  5484. (save-excursion
  5485. (org-back-over-empty-lines)
  5486. (or (bolp) (newline)))))
  5487. (setq ne-ins (org-back-over-empty-lines))
  5488. (move-marker ins-point (point))
  5489. (setq txt (buffer-substring beg end))
  5490. (org-save-markers-in-region beg end)
  5491. (delete-region beg end)
  5492. (org-remove-empty-overlays-at beg)
  5493. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5494. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5495. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5496. (let ((bbb (point)))
  5497. (insert-before-markers txt)
  5498. (org-reinstall-markers-in-region bbb)
  5499. (move-marker ins-point bbb))
  5500. (or (bolp) (insert "\n"))
  5501. (setq ins-end (point))
  5502. (goto-char ins-point)
  5503. (org-skip-whitespace)
  5504. (when (and (< arg 0)
  5505. (org-first-sibling-p)
  5506. (> ne-ins ne-beg))
  5507. ;; Move whitespace back to beginning
  5508. (save-excursion
  5509. (goto-char ins-end)
  5510. (let ((kill-whole-line t))
  5511. (kill-line (- ne-ins ne-beg)) (point)))
  5512. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5513. (move-marker ins-point nil)
  5514. (if folded
  5515. (hide-subtree)
  5516. (org-show-entry)
  5517. (show-children)
  5518. (org-cycle-hide-drawers 'children))
  5519. (org-clean-visibility-after-subtree-move)))
  5520. (defvar org-subtree-clip ""
  5521. "Clipboard for cut and paste of subtrees.
  5522. This is actually only a copy of the kill, because we use the normal kill
  5523. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5524. (defvar org-subtree-clip-folded nil
  5525. "Was the last copied subtree folded?
  5526. This is used to fold the tree back after pasting.")
  5527. (defun org-cut-subtree (&optional n)
  5528. "Cut the current subtree into the clipboard.
  5529. With prefix arg N, cut this many sequential subtrees.
  5530. This is a short-hand for marking the subtree and then cutting it."
  5531. (interactive "p")
  5532. (org-copy-subtree n 'cut))
  5533. (defun org-copy-subtree (&optional n cut force-store-markers)
  5534. "Cut the current subtree into the clipboard.
  5535. With prefix arg N, cut this many sequential subtrees.
  5536. This is a short-hand for marking the subtree and then copying it.
  5537. If CUT is non-nil, actually cut the subtree.
  5538. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5539. of some markers in the region, even if CUT is non-nil. This is
  5540. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5541. (interactive "p")
  5542. (let (beg end folded (beg0 (point)))
  5543. (if (interactive-p)
  5544. (org-back-to-heading nil) ; take what looks like a subtree
  5545. (org-back-to-heading t)) ; take what is really there
  5546. (org-back-over-empty-lines)
  5547. (setq beg (point))
  5548. (skip-chars-forward " \t\r\n")
  5549. (save-match-data
  5550. (save-excursion (outline-end-of-heading)
  5551. (setq folded (org-invisible-p)))
  5552. (condition-case nil
  5553. (org-forward-same-level (1- n) t)
  5554. (error nil))
  5555. (org-end-of-subtree t t))
  5556. (org-back-over-empty-lines)
  5557. (setq end (point))
  5558. (goto-char beg0)
  5559. (when (> end beg)
  5560. (setq org-subtree-clip-folded folded)
  5561. (when (or cut force-store-markers)
  5562. (org-save-markers-in-region beg end))
  5563. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5564. (setq org-subtree-clip (current-kill 0))
  5565. (message "%s: Subtree(s) with %d characters"
  5566. (if cut "Cut" "Copied")
  5567. (length org-subtree-clip)))))
  5568. (defun org-paste-subtree (&optional level tree for-yank)
  5569. "Paste the clipboard as a subtree, with modification of headline level.
  5570. The entire subtree is promoted or demoted in order to match a new headline
  5571. level.
  5572. If the cursor is at the beginning of a headline, the same level as
  5573. that headline is used to paste the tree
  5574. If not, the new level is derived from the *visible* headings
  5575. before and after the insertion point, and taken to be the inferior headline
  5576. level of the two. So if the previous visible heading is level 3 and the
  5577. next is level 4 (or vice versa), level 4 will be used for insertion.
  5578. This makes sure that the subtree remains an independent subtree and does
  5579. not swallow low level entries.
  5580. You can also force a different level, either by using a numeric prefix
  5581. argument, or by inserting the heading marker by hand. For example, if the
  5582. cursor is after \"*****\", then the tree will be shifted to level 5.
  5583. If optional TREE is given, use this text instead of the kill ring.
  5584. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5585. move back over whitespace before inserting, and move point to the end of
  5586. the inserted text when done."
  5587. (interactive "P")
  5588. (setq tree (or tree (and kill-ring (current-kill 0))))
  5589. (unless (org-kill-is-subtree-p tree)
  5590. (error "%s"
  5591. (substitute-command-keys
  5592. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5593. (let* ((visp (not (org-invisible-p)))
  5594. (txt tree)
  5595. (^re (concat "^\\(" outline-regexp "\\)"))
  5596. (re (concat "\\(" outline-regexp "\\)"))
  5597. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5598. (old-level (if (string-match ^re txt)
  5599. (- (match-end 0) (match-beginning 0) 1)
  5600. -1))
  5601. (force-level (cond (level (prefix-numeric-value level))
  5602. ((and (looking-at "[ \t]*$")
  5603. (string-match
  5604. ^re_ (buffer-substring
  5605. (point-at-bol) (point))))
  5606. (- (match-end 1) (match-beginning 1)))
  5607. ((and (bolp)
  5608. (looking-at org-outline-regexp))
  5609. (- (match-end 0) (point) 1))
  5610. (t nil)))
  5611. (previous-level (save-excursion
  5612. (condition-case nil
  5613. (progn
  5614. (outline-previous-visible-heading 1)
  5615. (if (looking-at re)
  5616. (- (match-end 0) (match-beginning 0) 1)
  5617. 1))
  5618. (error 1))))
  5619. (next-level (save-excursion
  5620. (condition-case nil
  5621. (progn
  5622. (or (looking-at outline-regexp)
  5623. (outline-next-visible-heading 1))
  5624. (if (looking-at re)
  5625. (- (match-end 0) (match-beginning 0) 1)
  5626. 1))
  5627. (error 1))))
  5628. (new-level (or force-level (max previous-level next-level)))
  5629. (shift (if (or (= old-level -1)
  5630. (= new-level -1)
  5631. (= old-level new-level))
  5632. 0
  5633. (- new-level old-level)))
  5634. (delta (if (> shift 0) -1 1))
  5635. (func (if (> shift 0) 'org-demote 'org-promote))
  5636. (org-odd-levels-only nil)
  5637. beg end newend)
  5638. ;; Remove the forced level indicator
  5639. (if force-level
  5640. (delete-region (point-at-bol) (point)))
  5641. ;; Paste
  5642. (beginning-of-line 1)
  5643. (unless for-yank (org-back-over-empty-lines))
  5644. (setq beg (point))
  5645. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5646. (insert-before-markers txt)
  5647. (unless (string-match "\n\\'" txt) (insert "\n"))
  5648. (setq newend (point))
  5649. (org-reinstall-markers-in-region beg)
  5650. (setq end (point))
  5651. (goto-char beg)
  5652. (skip-chars-forward " \t\n\r")
  5653. (setq beg (point))
  5654. (if (and (org-invisible-p) visp)
  5655. (save-excursion (outline-show-heading)))
  5656. ;; Shift if necessary
  5657. (unless (= shift 0)
  5658. (save-restriction
  5659. (narrow-to-region beg end)
  5660. (while (not (= shift 0))
  5661. (org-map-region func (point-min) (point-max))
  5662. (setq shift (+ delta shift)))
  5663. (goto-char (point-min))
  5664. (setq newend (point-max))))
  5665. (when (or (interactive-p) for-yank)
  5666. (message "Clipboard pasted as level %d subtree" new-level))
  5667. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5668. kill-ring
  5669. (eq org-subtree-clip (current-kill 0))
  5670. org-subtree-clip-folded)
  5671. ;; The tree was folded before it was killed/copied
  5672. (hide-subtree))
  5673. (and for-yank (goto-char newend))))
  5674. (defun org-kill-is-subtree-p (&optional txt)
  5675. "Check if the current kill is an outline subtree, or a set of trees.
  5676. Returns nil if kill does not start with a headline, or if the first
  5677. headline level is not the largest headline level in the tree.
  5678. So this will actually accept several entries of equal levels as well,
  5679. which is OK for `org-paste-subtree'.
  5680. If optional TXT is given, check this string instead of the current kill."
  5681. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5682. (start-level (and kill
  5683. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5684. org-outline-regexp "\\)")
  5685. kill)
  5686. (- (match-end 2) (match-beginning 2) 1)))
  5687. (re (concat "^" org-outline-regexp))
  5688. (start (1+ (or (match-beginning 2) -1))))
  5689. (if (not start-level)
  5690. (progn
  5691. nil) ;; does not even start with a heading
  5692. (catch 'exit
  5693. (while (setq start (string-match re kill (1+ start)))
  5694. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5695. (throw 'exit nil)))
  5696. t))))
  5697. (defvar org-markers-to-move nil
  5698. "Markers that should be moved with a cut-and-paste operation.
  5699. Those markers are stored together with their positions relative to
  5700. the start of the region.")
  5701. (defun org-save-markers-in-region (beg end)
  5702. "Check markers in region.
  5703. If these markers are between BEG and END, record their position relative
  5704. to BEG, so that after moving the block of text, we can put the markers back
  5705. into place.
  5706. This function gets called just before an entry or tree gets cut from the
  5707. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5708. called immediately, to move the markers with the entries."
  5709. (setq org-markers-to-move nil)
  5710. (when (featurep 'org-clock)
  5711. (org-clock-save-markers-for-cut-and-paste beg end))
  5712. (when (featurep 'org-agenda)
  5713. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5714. (defun org-check-and-save-marker (marker beg end)
  5715. "Check if MARKER is between BEG and END.
  5716. If yes, remember the marker and the distance to BEG."
  5717. (when (and (marker-buffer marker)
  5718. (equal (marker-buffer marker) (current-buffer)))
  5719. (if (and (>= marker beg) (< marker end))
  5720. (push (cons marker (- marker beg)) org-markers-to-move))))
  5721. (defun org-reinstall-markers-in-region (beg)
  5722. "Move all remembered markers to their position relative to BEG."
  5723. (mapc (lambda (x)
  5724. (move-marker (car x) (+ beg (cdr x))))
  5725. org-markers-to-move)
  5726. (setq org-markers-to-move nil))
  5727. (defun org-narrow-to-subtree ()
  5728. "Narrow buffer to the current subtree."
  5729. (interactive)
  5730. (save-excursion
  5731. (save-match-data
  5732. (narrow-to-region
  5733. (progn (org-back-to-heading t) (point))
  5734. (progn (org-end-of-subtree t) (point))))))
  5735. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5736. "Clone the task (subtree) at point N times.
  5737. The clones will be inserted as siblings.
  5738. In interactive use, the user will be prompted for the number of clones
  5739. to be produced, and for a time SHIFT, which may be a repeater as used
  5740. in time stamps, for example `+3d'.
  5741. When a valid repeater is given and the entry contains any time stamps,
  5742. the clones will become a sequence in time, with time stamps in the
  5743. subtree shifted for each clone produced. If SHIFT is nil or the
  5744. empty string, time stamps will be left alone.
  5745. If the original subtree did contain time stamps with a repeater,
  5746. the following will happen:
  5747. - the repeater will be removed in each clone
  5748. - an additional clone will be produced, with the current, unshifted
  5749. date(s) in the entry.
  5750. - the original entry will be placed *after* all the clones, with
  5751. repeater intact.
  5752. - the start days in the repeater in the original entry will be shifted
  5753. to past the last clone.
  5754. I this way you can spell out a number of instances of a repeating task,
  5755. and still retain the repeater to cover future instances of the task."
  5756. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5757. (let (beg end template task
  5758. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5759. (if (not (and (integerp n) (> n 0)))
  5760. (error "Invalid number of replications %s" n))
  5761. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5762. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5763. shift)))
  5764. (error "Invalid shift specification %s" shift))
  5765. (when doshift
  5766. (setq shift-n (string-to-number (match-string 1 shift))
  5767. shift-what (cdr (assoc (match-string 2 shift)
  5768. '(("d" . day) ("w" . week)
  5769. ("m" . month) ("y" . year))))))
  5770. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5771. (setq nmin 1 nmax n)
  5772. (org-back-to-heading t)
  5773. (setq beg (point))
  5774. (org-end-of-subtree t t)
  5775. (setq end (point))
  5776. (setq template (buffer-substring beg end))
  5777. (when (and doshift
  5778. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5779. (delete-region beg end)
  5780. (setq end beg)
  5781. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5782. (goto-char end)
  5783. (loop for n from nmin to nmax do
  5784. (if (not doshift)
  5785. (setq task template)
  5786. (with-temp-buffer
  5787. (insert template)
  5788. (org-mode)
  5789. (goto-char (point-min))
  5790. (while (re-search-forward org-ts-regexp-both nil t)
  5791. (org-timestamp-change (* n shift-n) shift-what))
  5792. (unless (= n n-no-remove)
  5793. (goto-char (point-min))
  5794. (while (re-search-forward org-ts-regexp nil t)
  5795. (save-excursion
  5796. (goto-char (match-beginning 0))
  5797. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5798. (delete-region (match-beginning 1) (match-end 1))))))
  5799. (setq task (buffer-string))))
  5800. (insert task))
  5801. (goto-char beg)))
  5802. ;;; Outline Sorting
  5803. (defun org-sort (with-case)
  5804. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5805. Optional argument WITH-CASE means sort case-sensitively.
  5806. With a double prefix argument, also remove duplicate entries."
  5807. (interactive "P")
  5808. (if (org-at-table-p)
  5809. (org-call-with-arg 'org-table-sort-lines with-case)
  5810. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5811. (defun org-sort-remove-invisible (s)
  5812. (remove-text-properties 0 (length s) org-rm-props s)
  5813. (while (string-match org-bracket-link-regexp s)
  5814. (setq s (replace-match (if (match-end 2)
  5815. (match-string 3 s)
  5816. (match-string 1 s)) t t s)))
  5817. s)
  5818. (defvar org-priority-regexp) ; defined later in the file
  5819. (defvar org-after-sorting-entries-or-items-hook nil
  5820. "Hook that is run after a bunch of entries or items have been sorted.
  5821. When children are sorted, the cursor is in the parent line when this
  5822. hook gets called. When a region or a plain list is sorted, the cursor
  5823. will be in the first entry of the sorted region/list.")
  5824. (defun org-sort-entries-or-items
  5825. (&optional with-case sorting-type getkey-func compare-func property)
  5826. "Sort entries on a certain level of an outline tree, or plain list items.
  5827. If there is an active region, the entries in the region are sorted.
  5828. Else, if the cursor is before the first entry, sort the top-level items.
  5829. Else, the children of the entry at point are sorted.
  5830. If the cursor is at the first item in a plain list, the list items will be
  5831. sorted.
  5832. Sorting can be alphabetically, numerically, by date/time as given by
  5833. a time stamp, by a property or by priority.
  5834. The command prompts for the sorting type unless it has been given to the
  5835. function through the SORTING-TYPE argument, which needs to a character,
  5836. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5837. precise meaning of each character:
  5838. n Numerically, by converting the beginning of the entry/item to a number.
  5839. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5840. t By date/time, either the first active time stamp in the entry, or, if
  5841. none exist, by the first inactive one.
  5842. In items, only the first line will be chekced.
  5843. s By the scheduled date/time.
  5844. d By deadline date/time.
  5845. c By creation time, which is assumed to be the first inactive time stamp
  5846. at the beginning of a line.
  5847. p By priority according to the cookie.
  5848. r By the value of a property.
  5849. Capital letters will reverse the sort order.
  5850. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5851. called with point at the beginning of the record. It must return either
  5852. a string or a number that should serve as the sorting key for that record.
  5853. Comparing entries ignores case by default. However, with an optional argument
  5854. WITH-CASE, the sorting considers case as well."
  5855. (interactive "P")
  5856. (let ((case-func (if with-case 'identity 'downcase))
  5857. start beg end stars re re2
  5858. txt what tmp plain-list-p)
  5859. ;; Find beginning and end of region to sort
  5860. (cond
  5861. ((org-region-active-p)
  5862. ;; we will sort the region
  5863. (setq end (region-end)
  5864. what "region")
  5865. (goto-char (region-beginning))
  5866. (if (not (org-on-heading-p)) (outline-next-heading))
  5867. (setq start (point)))
  5868. ((org-at-item-p)
  5869. ;; we will sort this plain list
  5870. (org-beginning-of-item-list) (setq start (point))
  5871. (org-end-of-item-list) (setq end (point))
  5872. (goto-char start)
  5873. (setq plain-list-p t
  5874. what "plain list"))
  5875. ((or (org-on-heading-p)
  5876. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5877. ;; we will sort the children of the current headline
  5878. (org-back-to-heading)
  5879. (setq start (point)
  5880. end (progn (org-end-of-subtree t t)
  5881. (org-back-over-empty-lines)
  5882. (point))
  5883. what "children")
  5884. (goto-char start)
  5885. (show-subtree)
  5886. (outline-next-heading))
  5887. (t
  5888. ;; we will sort the top-level entries in this file
  5889. (goto-char (point-min))
  5890. (or (org-on-heading-p) (outline-next-heading))
  5891. (setq start (point) end (point-max) what "top-level")
  5892. (goto-char start)
  5893. (show-all)))
  5894. (setq beg (point))
  5895. (if (>= beg end) (error "Nothing to sort"))
  5896. (unless plain-list-p
  5897. (looking-at "\\(\\*+\\)")
  5898. (setq stars (match-string 1)
  5899. re (concat "^" (regexp-quote stars) " +")
  5900. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5901. txt (buffer-substring beg end))
  5902. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5903. (if (and (not (equal stars "*")) (string-match re2 txt))
  5904. (error "Region to sort contains a level above the first entry")))
  5905. (unless sorting-type
  5906. (message
  5907. (if plain-list-p
  5908. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5909. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5910. [t]ime [s]cheduled [d]eadline [c]reated
  5911. A/N/T/S/D/C/P/O/F means reversed:")
  5912. what)
  5913. (setq sorting-type (read-char-exclusive))
  5914. (and (= (downcase sorting-type) ?f)
  5915. (setq getkey-func
  5916. (org-icompleting-read "Sort using function: "
  5917. obarray 'fboundp t nil nil))
  5918. (setq getkey-func (intern getkey-func)))
  5919. (and (= (downcase sorting-type) ?r)
  5920. (setq property
  5921. (org-icompleting-read "Property: "
  5922. (mapcar 'list (org-buffer-property-keys t))
  5923. nil t))))
  5924. (message "Sorting entries...")
  5925. (save-restriction
  5926. (narrow-to-region start end)
  5927. (let ((dcst (downcase sorting-type))
  5928. (case-fold-search nil)
  5929. (now (current-time)))
  5930. (sort-subr
  5931. (/= dcst sorting-type)
  5932. ;; This function moves to the beginning character of the "record" to
  5933. ;; be sorted.
  5934. (if plain-list-p
  5935. (lambda nil
  5936. (if (org-at-item-p) t (goto-char (point-max))))
  5937. (lambda nil
  5938. (if (re-search-forward re nil t)
  5939. (goto-char (match-beginning 0))
  5940. (goto-char (point-max)))))
  5941. ;; This function moves to the last character of the "record" being
  5942. ;; sorted.
  5943. (if plain-list-p
  5944. 'org-end-of-item
  5945. (lambda nil
  5946. (save-match-data
  5947. (condition-case nil
  5948. (outline-forward-same-level 1)
  5949. (error
  5950. (goto-char (point-max)))))))
  5951. ;; This function returns the value that gets sorted against.
  5952. (if plain-list-p
  5953. (lambda nil
  5954. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5955. (cond
  5956. ((= dcst ?n)
  5957. (string-to-number (buffer-substring (match-end 0)
  5958. (point-at-eol))))
  5959. ((= dcst ?a)
  5960. (buffer-substring (match-end 0) (point-at-eol)))
  5961. ((= dcst ?t)
  5962. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5963. (re-search-forward org-ts-regexp-both
  5964. (point-at-eol) t))
  5965. (org-time-string-to-seconds (match-string 0))
  5966. (time-to-seconds now)))
  5967. ((= dcst ?f)
  5968. (if getkey-func
  5969. (progn
  5970. (setq tmp (funcall getkey-func))
  5971. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5972. tmp)
  5973. (error "Invalid key function `%s'" getkey-func)))
  5974. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5975. (lambda nil
  5976. (cond
  5977. ((= dcst ?n)
  5978. (if (looking-at org-complex-heading-regexp)
  5979. (string-to-number (match-string 4))
  5980. nil))
  5981. ((= dcst ?a)
  5982. (if (looking-at org-complex-heading-regexp)
  5983. (funcall case-func (match-string 4))
  5984. nil))
  5985. ((= dcst ?t)
  5986. (let ((end (save-excursion (outline-next-heading) (point))))
  5987. (if (or (re-search-forward org-ts-regexp end t)
  5988. (re-search-forward org-ts-regexp-both end t))
  5989. (org-time-string-to-seconds (match-string 0))
  5990. (time-to-seconds now))))
  5991. ((= dcst ?c)
  5992. (let ((end (save-excursion (outline-next-heading) (point))))
  5993. (if (re-search-forward
  5994. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5995. end t)
  5996. (org-time-string-to-seconds (match-string 0))
  5997. (time-to-seconds now))))
  5998. ((= dcst ?s)
  5999. (let ((end (save-excursion (outline-next-heading) (point))))
  6000. (if (re-search-forward org-scheduled-time-regexp end t)
  6001. (org-time-string-to-seconds (match-string 1))
  6002. (time-to-seconds now))))
  6003. ((= dcst ?d)
  6004. (let ((end (save-excursion (outline-next-heading) (point))))
  6005. (if (re-search-forward org-deadline-time-regexp end t)
  6006. (org-time-string-to-seconds (match-string 1))
  6007. (time-to-seconds now))))
  6008. ((= dcst ?p)
  6009. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6010. (string-to-char (match-string 2))
  6011. org-default-priority))
  6012. ((= dcst ?r)
  6013. (or (org-entry-get nil property) ""))
  6014. ((= dcst ?o)
  6015. (if (looking-at org-complex-heading-regexp)
  6016. (- 9999 (length (member (match-string 2)
  6017. org-todo-keywords-1)))))
  6018. ((= dcst ?f)
  6019. (if getkey-func
  6020. (progn
  6021. (setq tmp (funcall getkey-func))
  6022. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6023. tmp)
  6024. (error "Invalid key function `%s'" getkey-func)))
  6025. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6026. nil
  6027. (cond
  6028. ((= dcst ?a) 'string<)
  6029. ((= dcst ?f) compare-func)
  6030. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6031. (t nil)))))
  6032. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6033. (message "Sorting entries...done")))
  6034. (defun org-do-sort (table what &optional with-case sorting-type)
  6035. "Sort TABLE of WHAT according to SORTING-TYPE.
  6036. The user will be prompted for the SORTING-TYPE if the call to this
  6037. function does not specify it. WHAT is only for the prompt, to indicate
  6038. what is being sorted. The sorting key will be extracted from
  6039. the car of the elements of the table.
  6040. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6041. (unless sorting-type
  6042. (message
  6043. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6044. what)
  6045. (setq sorting-type (read-char-exclusive)))
  6046. (let ((dcst (downcase sorting-type))
  6047. extractfun comparefun)
  6048. ;; Define the appropriate functions
  6049. (cond
  6050. ((= dcst ?n)
  6051. (setq extractfun 'string-to-number
  6052. comparefun (if (= dcst sorting-type) '< '>)))
  6053. ((= dcst ?a)
  6054. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6055. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6056. comparefun (if (= dcst sorting-type)
  6057. 'string<
  6058. (lambda (a b) (and (not (string< a b))
  6059. (not (string= a b)))))))
  6060. ((= dcst ?t)
  6061. (setq extractfun
  6062. (lambda (x)
  6063. (if (or (string-match org-ts-regexp x)
  6064. (string-match org-ts-regexp-both x))
  6065. (time-to-seconds
  6066. (org-time-string-to-time (match-string 0 x)))
  6067. 0))
  6068. comparefun (if (= dcst sorting-type) '< '>)))
  6069. (t (error "Invalid sorting type `%c'" sorting-type)))
  6070. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6071. table)
  6072. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6073. ;;; The orgstruct minor mode
  6074. ;; Define a minor mode which can be used in other modes in order to
  6075. ;; integrate the org-mode structure editing commands.
  6076. ;; This is really a hack, because the org-mode structure commands use
  6077. ;; keys which normally belong to the major mode. Here is how it
  6078. ;; works: The minor mode defines all the keys necessary to operate the
  6079. ;; structure commands, but wraps the commands into a function which
  6080. ;; tests if the cursor is currently at a headline or a plain list
  6081. ;; item. If that is the case, the structure command is used,
  6082. ;; temporarily setting many Org-mode variables like regular
  6083. ;; expressions for filling etc. However, when any of those keys is
  6084. ;; used at a different location, function uses `key-binding' to look
  6085. ;; up if the key has an associated command in another currently active
  6086. ;; keymap (minor modes, major mode, global), and executes that
  6087. ;; command. There might be problems if any of the keys is otherwise
  6088. ;; used as a prefix key.
  6089. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6090. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6091. ;; addresses this by checking explicitly for both bindings.
  6092. (defvar orgstruct-mode-map (make-sparse-keymap)
  6093. "Keymap for the minor `orgstruct-mode'.")
  6094. (defvar org-local-vars nil
  6095. "List of local variables, for use by `orgstruct-mode'")
  6096. ;;;###autoload
  6097. (define-minor-mode orgstruct-mode
  6098. "Toggle the minor more `orgstruct-mode'.
  6099. This mode is for using Org-mode structure commands in other modes.
  6100. The following key behave as if Org-mode was active, if the cursor
  6101. is on a headline, or on a plain list item (both in the definition
  6102. of Org-mode).
  6103. M-up Move entry/item up
  6104. M-down Move entry/item down
  6105. M-left Promote
  6106. M-right Demote
  6107. M-S-up Move entry/item up
  6108. M-S-down Move entry/item down
  6109. M-S-left Promote subtree
  6110. M-S-right Demote subtree
  6111. M-q Fill paragraph and items like in Org-mode
  6112. C-c ^ Sort entries
  6113. C-c - Cycle list bullet
  6114. TAB Cycle item visibility
  6115. M-RET Insert new heading/item
  6116. S-M-RET Insert new TODO heading / Checkbox item
  6117. C-c C-c Set tags / toggle checkbox"
  6118. nil " OrgStruct" nil
  6119. (org-load-modules-maybe)
  6120. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6121. ;;;###autoload
  6122. (defun turn-on-orgstruct ()
  6123. "Unconditionally turn on `orgstruct-mode'."
  6124. (orgstruct-mode 1))
  6125. (defun orgstruct++-mode (&optional arg)
  6126. "Toggle `orgstruct-mode', the enhanced version of it.
  6127. In addition to setting orgstruct-mode, this also exports all indentation
  6128. and autofilling variables from org-mode into the buffer. It will also
  6129. recognize item context in multiline items.
  6130. Note that turning off orgstruct-mode will *not* remove the
  6131. indentation/paragraph settings. This can only be done by refreshing the
  6132. major mode, for example with \\[normal-mode]."
  6133. (interactive "P")
  6134. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6135. (if (< arg 1)
  6136. (orgstruct-mode -1)
  6137. (orgstruct-mode 1)
  6138. (let (var val)
  6139. (mapc
  6140. (lambda (x)
  6141. (when (string-match
  6142. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6143. (symbol-name (car x)))
  6144. (setq var (car x) val (nth 1 x))
  6145. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6146. org-local-vars)
  6147. (org-set-local 'orgstruct-is-++ t))))
  6148. (defvar orgstruct-is-++ nil
  6149. "Is orgstruct-mode in ++ version in the current-buffer?")
  6150. (make-variable-buffer-local 'orgstruct-is-++)
  6151. ;;;###autoload
  6152. (defun turn-on-orgstruct++ ()
  6153. "Unconditionally turn on `orgstruct++-mode'."
  6154. (orgstruct++-mode 1))
  6155. (defun orgstruct-error ()
  6156. "Error when there is no default binding for a structure key."
  6157. (interactive)
  6158. (error "This key has no function outside structure elements"))
  6159. (defun orgstruct-setup ()
  6160. "Setup orgstruct keymaps."
  6161. (let ((nfunc 0)
  6162. (bindings
  6163. (list
  6164. '([(meta up)] org-metaup)
  6165. '([(meta down)] org-metadown)
  6166. '([(meta left)] org-metaleft)
  6167. '([(meta right)] org-metaright)
  6168. '([(meta shift up)] org-shiftmetaup)
  6169. '([(meta shift down)] org-shiftmetadown)
  6170. '([(meta shift left)] org-shiftmetaleft)
  6171. '([(meta shift right)] org-shiftmetaright)
  6172. '([?\e (up)] org-metaup)
  6173. '([?\e (down)] org-metadown)
  6174. '([?\e (left)] org-metaleft)
  6175. '([?\e (right)] org-metaright)
  6176. '([?\e (shift up)] org-shiftmetaup)
  6177. '([?\e (shift down)] org-shiftmetadown)
  6178. '([?\e (shift left)] org-shiftmetaleft)
  6179. '([?\e (shift right)] org-shiftmetaright)
  6180. '([(shift up)] org-shiftup)
  6181. '([(shift down)] org-shiftdown)
  6182. '([(shift left)] org-shiftleft)
  6183. '([(shift right)] org-shiftright)
  6184. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6185. '("\M-q" fill-paragraph)
  6186. '("\C-c^" org-sort)
  6187. '("\C-c-" org-cycle-list-bullet)))
  6188. elt key fun cmd)
  6189. (while (setq elt (pop bindings))
  6190. (setq nfunc (1+ nfunc))
  6191. (setq key (org-key (car elt))
  6192. fun (nth 1 elt)
  6193. cmd (orgstruct-make-binding fun nfunc key))
  6194. (org-defkey orgstruct-mode-map key cmd))
  6195. ;; Special treatment needed for TAB and RET
  6196. (org-defkey orgstruct-mode-map [(tab)]
  6197. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6198. (org-defkey orgstruct-mode-map "\C-i"
  6199. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6200. (org-defkey orgstruct-mode-map "\M-\C-m"
  6201. (orgstruct-make-binding 'org-insert-heading 105
  6202. "\M-\C-m" [(meta return)]))
  6203. (org-defkey orgstruct-mode-map [(meta return)]
  6204. (orgstruct-make-binding 'org-insert-heading 106
  6205. [(meta return)] "\M-\C-m"))
  6206. (org-defkey orgstruct-mode-map [(shift meta return)]
  6207. (orgstruct-make-binding 'org-insert-todo-heading 107
  6208. [(meta return)] "\M-\C-m"))
  6209. (org-defkey orgstruct-mode-map "\e\C-m"
  6210. (orgstruct-make-binding 'org-insert-heading 108
  6211. "\e\C-m" [?\e (return)]))
  6212. (org-defkey orgstruct-mode-map [?\e (return)]
  6213. (orgstruct-make-binding 'org-insert-heading 109
  6214. [?\e (return)] "\e\C-m"))
  6215. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6216. (orgstruct-make-binding 'org-insert-todo-heading 110
  6217. [?\e (return)] "\e\C-m"))
  6218. (unless org-local-vars
  6219. (setq org-local-vars (org-get-local-variables)))
  6220. t))
  6221. (defun orgstruct-make-binding (fun n &rest keys)
  6222. "Create a function for binding in the structure minor mode.
  6223. FUN is the command to call inside a table. N is used to create a unique
  6224. command name. KEYS are keys that should be checked in for a command
  6225. to execute outside of tables."
  6226. (eval
  6227. (list 'defun
  6228. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6229. '(arg)
  6230. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6231. "Outside of structure, run the binding of `"
  6232. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6233. "'.")
  6234. '(interactive "p")
  6235. (list 'if
  6236. `(org-context-p 'headline 'item
  6237. (and orgstruct-is-++
  6238. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6239. 'item-body))
  6240. (list 'org-run-like-in-org-mode (list 'quote fun))
  6241. (list 'let '(orgstruct-mode)
  6242. (list 'call-interactively
  6243. (append '(or)
  6244. (mapcar (lambda (k)
  6245. (list 'key-binding k))
  6246. keys)
  6247. '('orgstruct-error))))))))
  6248. (defun org-context-p (&rest contexts)
  6249. "Check if local context is any of CONTEXTS.
  6250. Possible values in the list of contexts are `table', `headline', and `item'."
  6251. (let ((pos (point)))
  6252. (goto-char (point-at-bol))
  6253. (prog1 (or (and (memq 'table contexts)
  6254. (looking-at "[ \t]*|"))
  6255. (and (memq 'headline contexts)
  6256. ;;????????? (looking-at "\\*+"))
  6257. (looking-at outline-regexp))
  6258. (and (memq 'item contexts)
  6259. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6260. (and (memq 'item-body contexts)
  6261. (org-in-item-p)))
  6262. (goto-char pos))))
  6263. (defun org-get-local-variables ()
  6264. "Return a list of all local variables in an org-mode buffer."
  6265. (let (varlist)
  6266. (with-current-buffer (get-buffer-create "*Org tmp*")
  6267. (erase-buffer)
  6268. (org-mode)
  6269. (setq varlist (buffer-local-variables)))
  6270. (kill-buffer "*Org tmp*")
  6271. (delq nil
  6272. (mapcar
  6273. (lambda (x)
  6274. (setq x
  6275. (if (symbolp x)
  6276. (list x)
  6277. (list (car x) (list 'quote (cdr x)))))
  6278. (if (string-match
  6279. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6280. (symbol-name (car x)))
  6281. x nil))
  6282. varlist))))
  6283. ;;;###autoload
  6284. (defun org-run-like-in-org-mode (cmd)
  6285. "Run a command, pretending that the current buffer is in Org-mode.
  6286. This will temporarily bind local variables that are typically bound in
  6287. Org-mode to the values they have in Org-mode, and then interactively
  6288. call CMD."
  6289. (org-load-modules-maybe)
  6290. (unless org-local-vars
  6291. (setq org-local-vars (org-get-local-variables)))
  6292. (eval (list 'let org-local-vars
  6293. (list 'call-interactively (list 'quote cmd)))))
  6294. ;;;; Archiving
  6295. (defun org-get-category (&optional pos)
  6296. "Get the category applying to position POS."
  6297. (get-text-property (or pos (point)) 'org-category))
  6298. (defun org-refresh-category-properties ()
  6299. "Refresh category text properties in the buffer."
  6300. (let ((def-cat (cond
  6301. ((null org-category)
  6302. (if buffer-file-name
  6303. (file-name-sans-extension
  6304. (file-name-nondirectory buffer-file-name))
  6305. "???"))
  6306. ((symbolp org-category) (symbol-name org-category))
  6307. (t org-category)))
  6308. beg end cat pos optionp)
  6309. (org-unmodified
  6310. (save-excursion
  6311. (save-restriction
  6312. (widen)
  6313. (goto-char (point-min))
  6314. (put-text-property (point) (point-max) 'org-category def-cat)
  6315. (while (re-search-forward
  6316. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6317. (setq pos (match-end 0)
  6318. optionp (equal (char-after (match-beginning 0)) ?#)
  6319. cat (org-trim (match-string 2)))
  6320. (if optionp
  6321. (setq beg (point-at-bol) end (point-max))
  6322. (org-back-to-heading t)
  6323. (setq beg (point) end (org-end-of-subtree t t)))
  6324. (put-text-property beg end 'org-category cat)
  6325. (goto-char pos)))))))
  6326. ;;;; Link Stuff
  6327. ;;; Link abbreviations
  6328. (defun org-link-expand-abbrev (link)
  6329. "Apply replacements as defined in `org-link-abbrev-alist."
  6330. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6331. (let* ((key (match-string 1 link))
  6332. (as (or (assoc key org-link-abbrev-alist-local)
  6333. (assoc key org-link-abbrev-alist)))
  6334. (tag (and (match-end 2) (match-string 3 link)))
  6335. rpl)
  6336. (if (not as)
  6337. link
  6338. (setq rpl (cdr as))
  6339. (cond
  6340. ((symbolp rpl) (funcall rpl tag))
  6341. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6342. ((string-match "%h" rpl)
  6343. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6344. (t (concat rpl tag)))))
  6345. link))
  6346. ;;; Storing and inserting links
  6347. (defvar org-insert-link-history nil
  6348. "Minibuffer history for links inserted with `org-insert-link'.")
  6349. (defvar org-stored-links nil
  6350. "Contains the links stored with `org-store-link'.")
  6351. (defvar org-store-link-plist nil
  6352. "Plist with info about the most recently link created with `org-store-link'.")
  6353. (defvar org-link-protocols nil
  6354. "Link protocols added to Org-mode using `org-add-link-type'.")
  6355. (defvar org-store-link-functions nil
  6356. "List of functions that are called to create and store a link.
  6357. Each function will be called in turn until one returns a non-nil
  6358. value. Each function should check if it is responsible for creating
  6359. this link (for example by looking at the major mode).
  6360. If not, it must exit and return nil.
  6361. If yes, it should return a non-nil value after a calling
  6362. `org-store-link-props' with a list of properties and values.
  6363. Special properties are:
  6364. :type The link prefix. like \"http\". This must be given.
  6365. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6366. This is obligatory as well.
  6367. :description Optional default description for the second pair
  6368. of brackets in an Org-mode link. The user can still change
  6369. this when inserting this link into an Org-mode buffer.
  6370. In addition to these, any additional properties can be specified
  6371. and then used in remember templates.")
  6372. (defun org-add-link-type (type &optional follow export)
  6373. "Add TYPE to the list of `org-link-types'.
  6374. Re-compute all regular expressions depending on `org-link-types'
  6375. FOLLOW and EXPORT are two functions.
  6376. FOLLOW should take the link path as the single argument and do whatever
  6377. is necessary to follow the link, for example find a file or display
  6378. a mail message.
  6379. EXPORT should format the link path for export to one of the export formats.
  6380. It should be a function accepting three arguments:
  6381. path the path of the link, the text after the prefix (like \"http:\")
  6382. desc the description of the link, if any, nil if there was no description
  6383. format the export format, a symbol like `html' or `latex'.
  6384. The function may use the FORMAT information to return different values
  6385. depending on the format. The return value will be put literally into
  6386. the exported file.
  6387. Org-mode has a built-in default for exporting links. If you are happy with
  6388. this default, there is no need to define an export function for the link
  6389. type. For a simple example of an export function, see `org-bbdb.el'."
  6390. (add-to-list 'org-link-types type t)
  6391. (org-make-link-regexps)
  6392. (if (assoc type org-link-protocols)
  6393. (setcdr (assoc type org-link-protocols) (list follow export))
  6394. (push (list type follow export) org-link-protocols)))
  6395. ;;;###autoload
  6396. (defun org-store-link (arg)
  6397. "\\<org-mode-map>Store an org-link to the current location.
  6398. This link is added to `org-stored-links' and can later be inserted
  6399. into an org-buffer with \\[org-insert-link].
  6400. For some link types, a prefix arg is interpreted:
  6401. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6402. For file links, arg negates `org-context-in-file-links'."
  6403. (interactive "P")
  6404. (org-load-modules-maybe)
  6405. (setq org-store-link-plist nil) ; reset
  6406. (let ((outline-regexp (org-get-limited-outline-regexp))
  6407. link cpltxt desc description search txt custom-id)
  6408. (cond
  6409. ((run-hook-with-args-until-success 'org-store-link-functions)
  6410. (setq link (plist-get org-store-link-plist :link)
  6411. desc (or (plist-get org-store-link-plist :description) link)))
  6412. ((equal (buffer-name) "*Org Edit Src Example*")
  6413. (let (label gc)
  6414. (while (or (not label)
  6415. (save-excursion
  6416. (save-restriction
  6417. (widen)
  6418. (goto-char (point-min))
  6419. (re-search-forward
  6420. (regexp-quote (format org-coderef-label-format label))
  6421. nil t))))
  6422. (when label (message "Label exists already") (sit-for 2))
  6423. (setq label (read-string "Code line label: " label)))
  6424. (end-of-line 1)
  6425. (setq link (format org-coderef-label-format label))
  6426. (setq gc (- 79 (length link)))
  6427. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6428. (insert link)
  6429. (setq link (concat "(" label ")") desc nil)))
  6430. ((eq major-mode 'calendar-mode)
  6431. (let ((cd (calendar-cursor-to-date)))
  6432. (setq link
  6433. (format-time-string
  6434. (car org-time-stamp-formats)
  6435. (apply 'encode-time
  6436. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6437. nil nil nil))))
  6438. (org-store-link-props :type "calendar" :date cd)))
  6439. ((eq major-mode 'w3-mode)
  6440. (setq cpltxt (if (and (buffer-name)
  6441. (not (string-match "Untitled" (buffer-name))))
  6442. (buffer-name)
  6443. (url-view-url t))
  6444. link (org-make-link (url-view-url t)))
  6445. (org-store-link-props :type "w3" :url (url-view-url t)))
  6446. ((eq major-mode 'w3m-mode)
  6447. (setq cpltxt (or w3m-current-title w3m-current-url)
  6448. link (org-make-link w3m-current-url))
  6449. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6450. ((setq search (run-hook-with-args-until-success
  6451. 'org-create-file-search-functions))
  6452. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6453. "::" search))
  6454. (setq cpltxt (or description link)))
  6455. ((eq major-mode 'image-mode)
  6456. (setq cpltxt (concat "file:"
  6457. (abbreviate-file-name buffer-file-name))
  6458. link (org-make-link cpltxt))
  6459. (org-store-link-props :type "image" :file buffer-file-name))
  6460. ((eq major-mode 'dired-mode)
  6461. ;; link to the file in the current line
  6462. (setq cpltxt (concat "file:"
  6463. (abbreviate-file-name
  6464. (expand-file-name
  6465. (dired-get-filename nil t))))
  6466. link (org-make-link cpltxt)))
  6467. ((and buffer-file-name (org-mode-p))
  6468. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6469. (cond
  6470. ((org-in-regexp "<<\\(.*?\\)>>")
  6471. (setq cpltxt
  6472. (concat "file:"
  6473. (abbreviate-file-name buffer-file-name)
  6474. "::" (match-string 1))
  6475. link (org-make-link cpltxt)))
  6476. ((and (featurep 'org-id)
  6477. (or (eq org-link-to-org-use-id t)
  6478. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6479. (interactive-p))
  6480. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6481. (interactive-p)
  6482. (not custom-id))
  6483. (and org-link-to-org-use-id
  6484. (condition-case nil
  6485. (org-entry-get nil "ID")
  6486. (error nil)))))
  6487. ;; We can make a link using the ID.
  6488. (setq link (condition-case nil
  6489. (prog1 (org-id-store-link)
  6490. (setq desc (plist-get org-store-link-plist
  6491. :description)))
  6492. (error
  6493. ;; probably before first headline, link to file only
  6494. (concat "file:"
  6495. (abbreviate-file-name buffer-file-name))))))
  6496. (t
  6497. ;; Just link to current headline
  6498. (setq cpltxt (concat "file:"
  6499. (abbreviate-file-name buffer-file-name)))
  6500. ;; Add a context search string
  6501. (when (org-xor org-context-in-file-links arg)
  6502. (setq txt (cond
  6503. ((org-on-heading-p) nil)
  6504. ((org-region-active-p)
  6505. (buffer-substring (region-beginning) (region-end)))
  6506. (t nil)))
  6507. (when (or (null txt) (string-match "\\S-" txt))
  6508. (setq cpltxt
  6509. (concat cpltxt "::"
  6510. (condition-case nil
  6511. (org-make-org-heading-search-string txt)
  6512. (error "")))
  6513. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6514. (if (string-match "::\\'" cpltxt)
  6515. (setq cpltxt (substring cpltxt 0 -2)))
  6516. (setq link (org-make-link cpltxt)))))
  6517. ((buffer-file-name (buffer-base-buffer))
  6518. ;; Just link to this file here.
  6519. (setq cpltxt (concat "file:"
  6520. (abbreviate-file-name
  6521. (buffer-file-name (buffer-base-buffer)))))
  6522. ;; Add a context string
  6523. (when (org-xor org-context-in-file-links arg)
  6524. (setq txt (if (org-region-active-p)
  6525. (buffer-substring (region-beginning) (region-end))
  6526. (buffer-substring (point-at-bol) (point-at-eol))))
  6527. ;; Only use search option if there is some text.
  6528. (when (string-match "\\S-" txt)
  6529. (setq cpltxt
  6530. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6531. desc "NONE")))
  6532. (setq link (org-make-link cpltxt)))
  6533. ((interactive-p)
  6534. (error "Cannot link to a buffer which is not visiting a file"))
  6535. (t (setq link nil)))
  6536. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6537. (setq link (or link cpltxt)
  6538. desc (or desc cpltxt))
  6539. (if (equal desc "NONE") (setq desc nil))
  6540. (if (and (or (interactive-p) executing-kbd-macro) link)
  6541. (progn
  6542. (setq org-stored-links
  6543. (cons (list link desc) org-stored-links))
  6544. (message "Stored: %s" (or desc link))
  6545. (when custom-id
  6546. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6547. "::#" custom-id))
  6548. (setq org-stored-links
  6549. (cons (list link desc) org-stored-links))))
  6550. (and link (org-make-link-string link desc)))))
  6551. (defun org-store-link-props (&rest plist)
  6552. "Store link properties, extract names and addresses."
  6553. (let (x adr)
  6554. (when (setq x (plist-get plist :from))
  6555. (setq adr (mail-extract-address-components x))
  6556. (setq plist (plist-put plist :fromname (car adr)))
  6557. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6558. (when (setq x (plist-get plist :to))
  6559. (setq adr (mail-extract-address-components x))
  6560. (setq plist (plist-put plist :toname (car adr)))
  6561. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6562. (let ((from (plist-get plist :from))
  6563. (to (plist-get plist :to)))
  6564. (when (and from to org-from-is-user-regexp)
  6565. (setq plist
  6566. (plist-put plist :fromto
  6567. (if (string-match org-from-is-user-regexp from)
  6568. (concat "to %t")
  6569. (concat "from %f"))))))
  6570. (setq org-store-link-plist plist))
  6571. (defun org-add-link-props (&rest plist)
  6572. "Add these properties to the link property list."
  6573. (let (key value)
  6574. (while plist
  6575. (setq key (pop plist) value (pop plist))
  6576. (setq org-store-link-plist
  6577. (plist-put org-store-link-plist key value)))))
  6578. (defun org-email-link-description (&optional fmt)
  6579. "Return the description part of an email link.
  6580. This takes information from `org-store-link-plist' and formats it
  6581. according to FMT (default from `org-email-link-description-format')."
  6582. (setq fmt (or fmt org-email-link-description-format))
  6583. (let* ((p org-store-link-plist)
  6584. (to (plist-get p :toaddress))
  6585. (from (plist-get p :fromaddress))
  6586. (table
  6587. (list
  6588. (cons "%c" (plist-get p :fromto))
  6589. (cons "%F" (plist-get p :from))
  6590. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6591. (cons "%T" (plist-get p :to))
  6592. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6593. (cons "%s" (plist-get p :subject))
  6594. (cons "%m" (plist-get p :message-id)))))
  6595. (when (string-match "%c" fmt)
  6596. ;; Check if the user wrote this message
  6597. (if (and org-from-is-user-regexp from to
  6598. (save-match-data (string-match org-from-is-user-regexp from)))
  6599. (setq fmt (replace-match "to %t" t t fmt))
  6600. (setq fmt (replace-match "from %f" t t fmt))))
  6601. (org-replace-escapes fmt table)))
  6602. (defun org-make-org-heading-search-string (&optional string heading)
  6603. "Make search string for STRING or current headline."
  6604. (interactive)
  6605. (let ((s (or string (org-get-heading))))
  6606. (unless (and string (not heading))
  6607. ;; We are using a headline, clean up garbage in there.
  6608. (if (string-match org-todo-regexp s)
  6609. (setq s (replace-match "" t t s)))
  6610. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6611. (setq s (replace-match "" t t s)))
  6612. (setq s (org-trim s))
  6613. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6614. org-comment-string "\\)") s)
  6615. (setq s (replace-match "" t t s)))
  6616. (while (string-match org-ts-regexp s)
  6617. (setq s (replace-match "" t t s))))
  6618. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6619. (setq s (replace-match " " t t s)))
  6620. (or string (setq s (concat "*" s))) ; Add * for headlines
  6621. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6622. (defun org-make-link (&rest strings)
  6623. "Concatenate STRINGS."
  6624. (apply 'concat strings))
  6625. (defun org-make-link-string (link &optional description)
  6626. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6627. (unless (string-match "\\S-" link)
  6628. (error "Empty link"))
  6629. (when (stringp description)
  6630. ;; Remove brackets from the description, they are fatal.
  6631. (while (string-match "\\[" description)
  6632. (setq description (replace-match "{" t t description)))
  6633. (while (string-match "\\]" description)
  6634. (setq description (replace-match "}" t t description))))
  6635. (when (equal (org-link-escape link) description)
  6636. ;; No description needed, it is identical
  6637. (setq description nil))
  6638. (when (and (not description)
  6639. (not (equal link (org-link-escape link))))
  6640. (setq description (org-extract-attributes link)))
  6641. (concat "[[" (org-link-escape link) "]"
  6642. (if description (concat "[" description "]") "")
  6643. "]"))
  6644. (defconst org-link-escape-chars
  6645. '((?\ . "%20")
  6646. (?\[ . "%5B")
  6647. (?\] . "%5D")
  6648. (?\340 . "%E0") ; `a
  6649. (?\342 . "%E2") ; ^a
  6650. (?\347 . "%E7") ; ,c
  6651. (?\350 . "%E8") ; `e
  6652. (?\351 . "%E9") ; 'e
  6653. (?\352 . "%EA") ; ^e
  6654. (?\356 . "%EE") ; ^i
  6655. (?\364 . "%F4") ; ^o
  6656. (?\371 . "%F9") ; `u
  6657. (?\373 . "%FB") ; ^u
  6658. (?\; . "%3B")
  6659. (?? . "%3F")
  6660. (?= . "%3D")
  6661. (?+ . "%2B")
  6662. )
  6663. "Association list of escapes for some characters problematic in links.
  6664. This is the list that is used for internal purposes.")
  6665. (defvar org-url-encoding-use-url-hexify nil)
  6666. (defconst org-link-escape-chars-browser
  6667. '((?\ . "%20")) ; 32 for the SPC char
  6668. "Association list of escapes for some characters problematic in links.
  6669. This is the list that is used before handing over to the browser.")
  6670. (defun org-link-escape (text &optional table)
  6671. "Escape characters in TEXT that are problematic for links."
  6672. (if org-url-encoding-use-url-hexify
  6673. (url-hexify-string text)
  6674. (setq table (or table org-link-escape-chars))
  6675. (when text
  6676. (let ((re (mapconcat (lambda (x) (regexp-quote
  6677. (char-to-string (car x))))
  6678. table "\\|")))
  6679. (while (string-match re text)
  6680. (setq text
  6681. (replace-match
  6682. (cdr (assoc (string-to-char (match-string 0 text))
  6683. table))
  6684. t t text)))
  6685. text))))
  6686. (defun org-link-unescape (text &optional table)
  6687. "Reverse the action of `org-link-escape'."
  6688. (if org-url-encoding-use-url-hexify
  6689. (url-unhex-string text)
  6690. (setq table (or table org-link-escape-chars))
  6691. (when text
  6692. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6693. table "\\|")))
  6694. (while (string-match re text)
  6695. (setq text
  6696. (replace-match
  6697. (char-to-string (car (rassoc (match-string 0 text) table)))
  6698. t t text)))
  6699. text))))
  6700. (defun org-xor (a b)
  6701. "Exclusive or."
  6702. (if a (not b) b))
  6703. (defun org-fixup-message-id-for-http (s)
  6704. "Replace special characters in a message id, so it can be used in an http query."
  6705. (while (string-match "<" s)
  6706. (setq s (replace-match "%3C" t t s)))
  6707. (while (string-match ">" s)
  6708. (setq s (replace-match "%3E" t t s)))
  6709. (while (string-match "@" s)
  6710. (setq s (replace-match "%40" t t s)))
  6711. s)
  6712. ;;;###autoload
  6713. (defun org-insert-link-global ()
  6714. "Insert a link like Org-mode does.
  6715. This command can be called in any mode to insert a link in Org-mode syntax."
  6716. (interactive)
  6717. (org-load-modules-maybe)
  6718. (org-run-like-in-org-mode 'org-insert-link))
  6719. (defun org-insert-link (&optional complete-file link-location)
  6720. "Insert a link. At the prompt, enter the link.
  6721. Completion can be used to insert any of the link protocol prefixes like
  6722. http or ftp in use.
  6723. The history can be used to select a link previously stored with
  6724. `org-store-link'. When the empty string is entered (i.e. if you just
  6725. press RET at the prompt), the link defaults to the most recently
  6726. stored link. As SPC triggers completion in the minibuffer, you need to
  6727. use M-SPC or C-q SPC to force the insertion of a space character.
  6728. You will also be prompted for a description, and if one is given, it will
  6729. be displayed in the buffer instead of the link.
  6730. If there is already a link at point, this command will allow you to edit link
  6731. and description parts.
  6732. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6733. be selected using completion. The path to the file will be relative to the
  6734. current directory if the file is in the current directory or a subdirectory.
  6735. Otherwise, the link will be the absolute path as completed in the minibuffer
  6736. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6737. option `org-link-file-path-type'.
  6738. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6739. the current directory or below.
  6740. With three \\[universal-argument] prefixes, negate the meaning of
  6741. `org-keep-stored-link-after-insertion'.
  6742. If `org-make-link-description-function' is non-nil, this function will be
  6743. called with the link target, and the result will be the default
  6744. link description.
  6745. If the LINK-LOCATION parameter is non-nil, this value will be
  6746. used as the link location instead of reading one interactively."
  6747. (interactive "P")
  6748. (let* ((wcf (current-window-configuration))
  6749. (region (if (org-region-active-p)
  6750. (buffer-substring (region-beginning) (region-end))))
  6751. (remove (and region (list (region-beginning) (region-end))))
  6752. (desc region)
  6753. tmphist ; byte-compile incorrectly complains about this
  6754. (link link-location)
  6755. entry file all-prefixes)
  6756. (cond
  6757. (link-location) ; specified by arg, just use it.
  6758. ((org-in-regexp org-bracket-link-regexp 1)
  6759. ;; We do have a link at point, and we are going to edit it.
  6760. (setq remove (list (match-beginning 0) (match-end 0)))
  6761. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6762. (setq link (read-string "Link: "
  6763. (org-link-unescape
  6764. (org-match-string-no-properties 1)))))
  6765. ((or (org-in-regexp org-angle-link-re)
  6766. (org-in-regexp org-plain-link-re))
  6767. ;; Convert to bracket link
  6768. (setq remove (list (match-beginning 0) (match-end 0))
  6769. link (read-string "Link: "
  6770. (org-remove-angle-brackets (match-string 0)))))
  6771. ((member complete-file '((4) (16)))
  6772. ;; Completing read for file names.
  6773. (setq link (org-file-complete-link complete-file)))
  6774. (t
  6775. ;; Read link, with completion for stored links.
  6776. (with-output-to-temp-buffer "*Org Links*"
  6777. (princ "Insert a link.
  6778. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6779. (when org-stored-links
  6780. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6781. (princ (mapconcat
  6782. (lambda (x)
  6783. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6784. (reverse org-stored-links) "\n"))))
  6785. (let ((cw (selected-window)))
  6786. (select-window (get-buffer-window "*Org Links*"))
  6787. (setq truncate-lines t)
  6788. (unless (pos-visible-in-window-p (point-max))
  6789. (org-fit-window-to-buffer))
  6790. (and (window-live-p cw) (select-window cw)))
  6791. ;; Fake a link history, containing the stored links.
  6792. (setq tmphist (append (mapcar 'car org-stored-links)
  6793. org-insert-link-history))
  6794. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6795. (mapcar 'car org-link-abbrev-alist)
  6796. org-link-types))
  6797. (unwind-protect
  6798. (progn
  6799. (setq link
  6800. (let ((org-completion-use-ido nil)
  6801. (org-completion-use-iswitchb nil))
  6802. (org-completing-read
  6803. "Link: "
  6804. (append
  6805. (mapcar (lambda (x) (list (concat x ":")))
  6806. all-prefixes)
  6807. (mapcar 'car org-stored-links))
  6808. nil nil nil
  6809. 'tmphist
  6810. (car (car org-stored-links)))))
  6811. (if (or (member link all-prefixes)
  6812. (and (equal ":" (substring link -1))
  6813. (member (substring link 0 -1) all-prefixes)
  6814. (setq link (substring link 0 -1))))
  6815. (setq link (org-link-try-special-completion link))))
  6816. (set-window-configuration wcf)
  6817. (kill-buffer "*Org Links*"))
  6818. (setq entry (assoc link org-stored-links))
  6819. (or entry (push link org-insert-link-history))
  6820. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6821. (not org-keep-stored-link-after-insertion))
  6822. (setq org-stored-links (delq (assoc link org-stored-links)
  6823. org-stored-links)))
  6824. (setq desc (or desc (nth 1 entry)))))
  6825. (if (string-match org-plain-link-re link)
  6826. ;; URL-like link, normalize the use of angular brackets.
  6827. (setq link (org-make-link (org-remove-angle-brackets link))))
  6828. ;; Check if we are linking to the current file with a search option
  6829. ;; If yes, simplify the link by using only the search option.
  6830. (when (and buffer-file-name
  6831. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6832. (let* ((path (match-string 1 link))
  6833. (case-fold-search nil)
  6834. (search (match-string 2 link)))
  6835. (save-match-data
  6836. (if (equal (file-truename buffer-file-name) (file-truename path))
  6837. ;; We are linking to this same file, with a search option
  6838. (setq link search)))))
  6839. ;; Check if we can/should use a relative path. If yes, simplify the link
  6840. (when (string-match "^file:\\(.*\\)" link)
  6841. (let* ((path (match-string 1 link))
  6842. (origpath path)
  6843. (case-fold-search nil))
  6844. (cond
  6845. ((or (eq org-link-file-path-type 'absolute)
  6846. (equal complete-file '(16)))
  6847. (setq path (abbreviate-file-name (expand-file-name path))))
  6848. ((eq org-link-file-path-type 'noabbrev)
  6849. (setq path (expand-file-name path)))
  6850. ((eq org-link-file-path-type 'relative)
  6851. (setq path (file-relative-name path)))
  6852. (t
  6853. (save-match-data
  6854. (if (string-match (concat "^" (regexp-quote
  6855. (file-name-as-directory
  6856. (expand-file-name "."))))
  6857. (expand-file-name path))
  6858. ;; We are linking a file with relative path name.
  6859. (setq path (substring (expand-file-name path)
  6860. (match-end 0)))
  6861. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6862. (setq link (concat "file:" path))
  6863. (if (equal desc origpath)
  6864. (setq desc path))))
  6865. (if org-make-link-description-function
  6866. (setq desc (funcall org-make-link-description-function link desc)))
  6867. (setq desc (read-string "Description: " desc))
  6868. (unless (string-match "\\S-" desc) (setq desc nil))
  6869. (if remove (apply 'delete-region remove))
  6870. (insert (org-make-link-string link desc))))
  6871. (defun org-link-try-special-completion (type)
  6872. "If there is completion support for link type TYPE, offer it."
  6873. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6874. (if (functionp fun)
  6875. (funcall fun)
  6876. (read-string "Link (no completion support): " (concat type ":")))))
  6877. (defun org-file-complete-link (&optional arg)
  6878. "Create a file link using completion."
  6879. (let (file link)
  6880. (setq file (read-file-name "File: "))
  6881. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6882. (pwd1 (file-name-as-directory (abbreviate-file-name
  6883. (expand-file-name ".")))))
  6884. (cond
  6885. ((equal arg '(16))
  6886. (setq link (org-make-link
  6887. "file:"
  6888. (abbreviate-file-name (expand-file-name file)))))
  6889. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6890. (setq link (org-make-link "file:" (match-string 1 file))))
  6891. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6892. (expand-file-name file))
  6893. (setq link (org-make-link
  6894. "file:" (match-string 1 (expand-file-name file)))))
  6895. (t (setq link (org-make-link "file:" file)))))
  6896. link))
  6897. (defun org-completing-read (&rest args)
  6898. "Completing-read with SPACE being a normal character."
  6899. (let ((minibuffer-local-completion-map
  6900. (copy-keymap minibuffer-local-completion-map)))
  6901. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6902. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6903. (apply 'org-icompleting-read args)))
  6904. (defun org-completing-read-no-i (&rest args)
  6905. (let (org-completion-use-ido org-completion-use-iswitchb)
  6906. (apply 'org-completing-read args)))
  6907. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6908. "Use iswitch as a completing-read replacement to choose from choices.
  6909. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6910. from."
  6911. (let ((iswitchb-make-buflist-hook
  6912. (lambda ()
  6913. (setq iswitchb-temp-buflist choices))))
  6914. (iswitchb-read-buffer prompt)))
  6915. (defun org-icompleting-read (&rest args)
  6916. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6917. (if (and org-completion-use-ido
  6918. (fboundp 'ido-completing-read)
  6919. (boundp 'ido-mode) ido-mode
  6920. (listp (second args)))
  6921. (let ((ido-enter-matching-directory nil))
  6922. (apply 'ido-completing-read (concat (car args))
  6923. (if (consp (car (nth 1 args)))
  6924. (mapcar (lambda (x) (car x)) (nth 1 args))
  6925. (nth 1 args))
  6926. (cddr args)))
  6927. (if (and org-completion-use-iswitchb
  6928. (boundp 'iswitchb-mode) iswitchb-mode
  6929. (listp (second args)))
  6930. (apply 'org-iswitchb-completing-read (concat (car args))
  6931. (mapcar (lambda (x) (car x)) (nth 1 args))
  6932. (cddr args))
  6933. (apply 'completing-read args))))
  6934. (defun org-extract-attributes (s)
  6935. "Extract the attributes cookie from a string and set as text property."
  6936. (let (a attr (start 0) key value)
  6937. (save-match-data
  6938. (when (string-match "{{\\([^}]+\\)}}$" s)
  6939. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6940. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6941. (setq key (match-string 1 a) value (match-string 2 a)
  6942. start (match-end 0)
  6943. attr (plist-put attr (intern key) value))))
  6944. (org-add-props s nil 'org-attr attr))
  6945. s))
  6946. (defun org-extract-attributes-from-string (tag)
  6947. (let (key value attr)
  6948. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6949. (setq key (match-string 1 tag) value (match-string 2 tag)
  6950. tag (replace-match "" t t tag)
  6951. attr (plist-put attr (intern key) value)))
  6952. (cons tag attr)))
  6953. (defun org-attributes-to-string (plist)
  6954. "Format a property list into an HTML attribute list."
  6955. (let ((s "") key value)
  6956. (while plist
  6957. (setq key (pop plist) value (pop plist))
  6958. (and value
  6959. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6960. s))
  6961. ;;; Opening/following a link
  6962. (defvar org-link-search-failed nil)
  6963. (defun org-next-link ()
  6964. "Move forward to the next link.
  6965. If the link is in hidden text, expose it."
  6966. (interactive)
  6967. (when (and org-link-search-failed (eq this-command last-command))
  6968. (goto-char (point-min))
  6969. (message "Link search wrapped back to beginning of buffer"))
  6970. (setq org-link-search-failed nil)
  6971. (let* ((pos (point))
  6972. (ct (org-context))
  6973. (a (assoc :link ct)))
  6974. (if a (goto-char (nth 2 a)))
  6975. (if (re-search-forward org-any-link-re nil t)
  6976. (progn
  6977. (goto-char (match-beginning 0))
  6978. (if (org-invisible-p) (org-show-context)))
  6979. (goto-char pos)
  6980. (setq org-link-search-failed t)
  6981. (error "No further link found"))))
  6982. (defun org-previous-link ()
  6983. "Move backward to the previous link.
  6984. If the link is in hidden text, expose it."
  6985. (interactive)
  6986. (when (and org-link-search-failed (eq this-command last-command))
  6987. (goto-char (point-max))
  6988. (message "Link search wrapped back to end of buffer"))
  6989. (setq org-link-search-failed nil)
  6990. (let* ((pos (point))
  6991. (ct (org-context))
  6992. (a (assoc :link ct)))
  6993. (if a (goto-char (nth 1 a)))
  6994. (if (re-search-backward org-any-link-re nil t)
  6995. (progn
  6996. (goto-char (match-beginning 0))
  6997. (if (org-invisible-p) (org-show-context)))
  6998. (goto-char pos)
  6999. (setq org-link-search-failed t)
  7000. (error "No further link found"))))
  7001. (defun org-translate-link (s)
  7002. "Translate a link string if a translation function has been defined."
  7003. (if (and org-link-translation-function
  7004. (fboundp org-link-translation-function)
  7005. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7006. (progn
  7007. (setq s (funcall org-link-translation-function
  7008. (match-string 1) (match-string 2)))
  7009. (concat (car s) ":" (cdr s)))
  7010. s))
  7011. (defun org-translate-link-from-planner (type path)
  7012. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7013. This is still an experimental function, your mileage may vary."
  7014. (cond
  7015. ((member type '("http" "https" "news" "ftp"))
  7016. ;; standard Internet links are the same.
  7017. nil)
  7018. ((and (equal type "irc") (string-match "^//" path))
  7019. ;; Planner has two / at the beginning of an irc link, we have 1.
  7020. ;; We should have zero, actually....
  7021. (setq path (substring path 1)))
  7022. ((and (equal type "lisp") (string-match "^/" path))
  7023. ;; Planner has a slash, we do not.
  7024. (setq type "elisp" path (substring path 1)))
  7025. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7026. ;; A typical message link. Planner has the id after the fina slash,
  7027. ;; we separate it with a hash mark
  7028. (setq path (concat (match-string 1 path) "#"
  7029. (org-remove-angle-brackets (match-string 2 path)))))
  7030. )
  7031. (cons type path))
  7032. (defun org-find-file-at-mouse (ev)
  7033. "Open file link or URL at mouse."
  7034. (interactive "e")
  7035. (mouse-set-point ev)
  7036. (org-open-at-point 'in-emacs))
  7037. (defun org-open-at-mouse (ev)
  7038. "Open file link or URL at mouse."
  7039. (interactive "e")
  7040. (mouse-set-point ev)
  7041. (if (eq major-mode 'org-agenda-mode)
  7042. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7043. (org-open-at-point))
  7044. (defvar org-window-config-before-follow-link nil
  7045. "The window configuration before following a link.
  7046. This is saved in case the need arises to restore it.")
  7047. (defvar org-open-link-marker (make-marker)
  7048. "Marker pointing to the location where `org-open-at-point; was called.")
  7049. ;;;###autoload
  7050. (defun org-open-at-point-global ()
  7051. "Follow a link like Org-mode does.
  7052. This command can be called in any mode to follow a link that has
  7053. Org-mode syntax."
  7054. (interactive)
  7055. (org-run-like-in-org-mode 'org-open-at-point))
  7056. ;;;###autoload
  7057. (defun org-open-link-from-string (s &optional arg)
  7058. "Open a link in the string S, as if it was in Org-mode."
  7059. (interactive "sLink: \nP")
  7060. (let ((reference-buffer (current-buffer)))
  7061. (with-temp-buffer
  7062. (let ((org-inhibit-startup t))
  7063. (org-mode)
  7064. (insert s)
  7065. (goto-char (point-min))
  7066. (org-open-at-point arg reference-buffer)))))
  7067. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7068. "Open link at or after point.
  7069. If there is no link at point, this function will search forward up to
  7070. the end of the current line.
  7071. Normally, files will be opened by an appropriate application. If the
  7072. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7073. With a double prefix argument, try to open outside of Emacs, in the
  7074. application the system uses for this file type."
  7075. (interactive "P")
  7076. (org-load-modules-maybe)
  7077. (move-marker org-open-link-marker (point))
  7078. (setq org-window-config-before-follow-link (current-window-configuration))
  7079. (org-remove-occur-highlights nil nil t)
  7080. (cond
  7081. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7082. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7083. (org-footnote-action))
  7084. (t
  7085. (let (type path link line search (pos (point)))
  7086. (catch 'match
  7087. (save-excursion
  7088. (skip-chars-forward "^]\n\r")
  7089. (when (org-in-regexp org-bracket-link-regexp)
  7090. (setq link (org-extract-attributes
  7091. (org-link-unescape (org-match-string-no-properties 1))))
  7092. (while (string-match " *\n *" link)
  7093. (setq link (replace-match " " t t link)))
  7094. (setq link (org-link-expand-abbrev link))
  7095. (cond
  7096. ((or (file-name-absolute-p link)
  7097. (string-match "^\\.\\.?/" link))
  7098. (setq type "file" path link))
  7099. ((string-match org-link-re-with-space3 link)
  7100. (setq type (match-string 1 link) path (match-string 2 link)))
  7101. (t (setq type "thisfile" path link)))
  7102. (throw 'match t)))
  7103. (when (get-text-property (point) 'org-linked-text)
  7104. (setq type "thisfile"
  7105. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7106. (1+ (point)) (point))
  7107. path (buffer-substring
  7108. (previous-single-property-change pos 'org-linked-text)
  7109. (next-single-property-change pos 'org-linked-text)))
  7110. (throw 'match t))
  7111. (save-excursion
  7112. (when (or (org-in-regexp org-angle-link-re)
  7113. (org-in-regexp org-plain-link-re))
  7114. (setq type (match-string 1) path (match-string 2))
  7115. (throw 'match t)))
  7116. (save-excursion
  7117. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7118. (setq type "tags"
  7119. path (match-string 1))
  7120. (while (string-match ":" path)
  7121. (setq path (replace-match "+" t t path)))
  7122. (throw 'match t)))
  7123. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7124. (setq type "tree-match"
  7125. path (match-string 1))
  7126. (throw 'match t)))
  7127. (unless path
  7128. (error "No link found"))
  7129. ;; switch back to reference buffer
  7130. ;; needed when if called in a temporary buffer through
  7131. ;; org-open-link-from-string
  7132. (and reference-buffer (switch-to-buffer reference-buffer))
  7133. ;; Remove any trailing spaces in path
  7134. (if (string-match " +\\'" path)
  7135. (setq path (replace-match "" t t path)))
  7136. (if (and org-link-translation-function
  7137. (fboundp org-link-translation-function))
  7138. ;; Check if we need to translate the link
  7139. (let ((tmp (funcall org-link-translation-function type path)))
  7140. (setq type (car tmp) path (cdr tmp))))
  7141. (cond
  7142. ((assoc type org-link-protocols)
  7143. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7144. ((equal type "mailto")
  7145. (let ((cmd (car org-link-mailto-program))
  7146. (args (cdr org-link-mailto-program)) args1
  7147. (address path) (subject "") a)
  7148. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7149. (setq address (match-string 1 path)
  7150. subject (org-link-escape (match-string 2 path))))
  7151. (while args
  7152. (cond
  7153. ((not (stringp (car args))) (push (pop args) args1))
  7154. (t (setq a (pop args))
  7155. (if (string-match "%a" a)
  7156. (setq a (replace-match address t t a)))
  7157. (if (string-match "%s" a)
  7158. (setq a (replace-match subject t t a)))
  7159. (push a args1))))
  7160. (apply cmd (nreverse args1))))
  7161. ((member type '("http" "https" "ftp" "news"))
  7162. (browse-url (concat type ":" (org-link-escape
  7163. path org-link-escape-chars-browser))))
  7164. ((member type '("message"))
  7165. (browse-url (concat type ":" path)))
  7166. ((string= type "tags")
  7167. (org-tags-view in-emacs path))
  7168. ((string= type "thisfile")
  7169. (if in-emacs
  7170. (switch-to-buffer-other-window
  7171. (org-get-buffer-for-internal-link (current-buffer)))
  7172. (org-mark-ring-push))
  7173. (let ((cmd `(org-link-search
  7174. ,path
  7175. ,(cond ((equal in-emacs '(4)) 'occur)
  7176. ((equal in-emacs '(16)) 'org-occur)
  7177. (t nil))
  7178. ,pos)))
  7179. (condition-case nil (eval cmd)
  7180. (error (progn (widen) (eval cmd))))))
  7181. ((string= type "tree-match")
  7182. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7183. ((string= type "file")
  7184. (if (string-match "::\\([0-9]+\\)\\'" path)
  7185. (setq line (string-to-number (match-string 1 path))
  7186. path (substring path 0 (match-beginning 0)))
  7187. (if (string-match "::\\(.+\\)\\'" path)
  7188. (setq search (match-string 1 path)
  7189. path (substring path 0 (match-beginning 0)))))
  7190. (if (string-match "[*?{]" (file-name-nondirectory path))
  7191. (dired path)
  7192. (org-open-file path in-emacs line search)))
  7193. ((string= type "news")
  7194. (require 'org-gnus)
  7195. (org-gnus-follow-link path))
  7196. ((string= type "shell")
  7197. (let ((cmd path))
  7198. (if (or (not org-confirm-shell-link-function)
  7199. (funcall org-confirm-shell-link-function
  7200. (format "Execute \"%s\" in shell? "
  7201. (org-add-props cmd nil
  7202. 'face 'org-warning))))
  7203. (progn
  7204. (message "Executing %s" cmd)
  7205. (shell-command cmd))
  7206. (error "Abort"))))
  7207. ((string= type "elisp")
  7208. (let ((cmd path))
  7209. (if (or (not org-confirm-elisp-link-function)
  7210. (funcall org-confirm-elisp-link-function
  7211. (format "Execute \"%s\" as elisp? "
  7212. (org-add-props cmd nil
  7213. 'face 'org-warning))))
  7214. (message "%s => %s" cmd
  7215. (if (equal (string-to-char cmd) ?\()
  7216. (eval (read cmd))
  7217. (call-interactively (read cmd))))
  7218. (error "Abort"))))
  7219. (t
  7220. (browse-url-at-point))))))
  7221. (move-marker org-open-link-marker nil)
  7222. (run-hook-with-args 'org-follow-link-hook))
  7223. ;;;; Time estimates
  7224. (defun org-get-effort (&optional pom)
  7225. "Get the effort estimate for the current entry."
  7226. (org-entry-get pom org-effort-property))
  7227. ;;; File search
  7228. (defvar org-create-file-search-functions nil
  7229. "List of functions to construct the right search string for a file link.
  7230. These functions are called in turn with point at the location to
  7231. which the link should point.
  7232. A function in the hook should first test if it would like to
  7233. handle this file type, for example by checking the major-mode or
  7234. the file extension. If it decides not to handle this file, it
  7235. should just return nil to give other functions a chance. If it
  7236. does handle the file, it must return the search string to be used
  7237. when following the link. The search string will be part of the
  7238. file link, given after a double colon, and `org-open-at-point'
  7239. will automatically search for it. If special measures must be
  7240. taken to make the search successful, another function should be
  7241. added to the companion hook `org-execute-file-search-functions',
  7242. which see.
  7243. A function in this hook may also use `setq' to set the variable
  7244. `description' to provide a suggestion for the descriptive text to
  7245. be used for this link when it gets inserted into an Org-mode
  7246. buffer with \\[org-insert-link].")
  7247. (defvar org-execute-file-search-functions nil
  7248. "List of functions to execute a file search triggered by a link.
  7249. Functions added to this hook must accept a single argument, the
  7250. search string that was part of the file link, the part after the
  7251. double colon. The function must first check if it would like to
  7252. handle this search, for example by checking the major-mode or the
  7253. file extension. If it decides not to handle this search, it
  7254. should just return nil to give other functions a chance. If it
  7255. does handle the search, it must return a non-nil value to keep
  7256. other functions from trying.
  7257. Each function can access the current prefix argument through the
  7258. variable `current-prefix-argument'. Note that a single prefix is
  7259. used to force opening a link in Emacs, so it may be good to only
  7260. use a numeric or double prefix to guide the search function.
  7261. In case this is needed, a function in this hook can also restore
  7262. the window configuration before `org-open-at-point' was called using:
  7263. (set-window-configuration org-window-config-before-follow-link)")
  7264. (defun org-link-search (s &optional type avoid-pos)
  7265. "Search for a link search option.
  7266. If S is surrounded by forward slashes, it is interpreted as a
  7267. regular expression. In org-mode files, this will create an `org-occur'
  7268. sparse tree. In ordinary files, `occur' will be used to list matches.
  7269. If the current buffer is in `dired-mode', grep will be used to search
  7270. in all files. If AVOID-POS is given, ignore matches near that position."
  7271. (let ((case-fold-search t)
  7272. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7273. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7274. (append '(("") (" ") ("\t") ("\n"))
  7275. org-emphasis-alist)
  7276. "\\|") "\\)"))
  7277. (pos (point))
  7278. (pre nil) (post nil)
  7279. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7280. (cond
  7281. ;; First check if there are any special
  7282. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7283. ;; Now try the builtin stuff
  7284. ((and (equal (string-to-char s0) ?#)
  7285. (> (length s0) 1)
  7286. (save-excursion
  7287. (goto-char (point-min))
  7288. (and
  7289. (re-search-forward
  7290. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7291. (setq type 'dedicated
  7292. pos (match-beginning 0))))
  7293. ;; There is an exact target for this
  7294. (goto-char pos)
  7295. (org-back-to-heading t)))
  7296. ((save-excursion
  7297. (goto-char (point-min))
  7298. (and
  7299. (re-search-forward
  7300. (concat "<<" (regexp-quote s0) ">>") nil t)
  7301. (setq type 'dedicated
  7302. pos (match-beginning 0))))
  7303. ;; There is an exact target for this
  7304. (goto-char pos))
  7305. ((and (string-match "^(\\(.*\\))$" s0)
  7306. (save-excursion
  7307. (goto-char (point-min))
  7308. (and
  7309. (re-search-forward
  7310. (concat "[^[]" (regexp-quote
  7311. (format org-coderef-label-format
  7312. (match-string 1 s0))))
  7313. nil t)
  7314. (setq type 'dedicated
  7315. pos (1+ (match-beginning 0))))))
  7316. ;; There is a coderef target for this
  7317. (goto-char pos))
  7318. ((string-match "^/\\(.*\\)/$" s)
  7319. ;; A regular expression
  7320. (cond
  7321. ((org-mode-p)
  7322. (org-occur (match-string 1 s)))
  7323. ;;((eq major-mode 'dired-mode)
  7324. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7325. (t (org-do-occur (match-string 1 s)))))
  7326. (t
  7327. ;; A normal search strings
  7328. (when (equal (string-to-char s) ?*)
  7329. ;; Anchor on headlines, post may include tags.
  7330. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7331. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7332. s (substring s 1)))
  7333. (remove-text-properties
  7334. 0 (length s)
  7335. '(face nil mouse-face nil keymap nil fontified nil) s)
  7336. ;; Make a series of regular expressions to find a match
  7337. (setq words (org-split-string s "[ \n\r\t]+")
  7338. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7339. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7340. "\\)" markers)
  7341. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7342. re2a (concat "[ \t\r\n]" re2a_)
  7343. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7344. re4 (concat "[^a-zA-Z_]" re4_)
  7345. re1 (concat pre re2 post)
  7346. re3 (concat pre (if pre re4_ re4) post)
  7347. re5 (concat pre ".*" re4)
  7348. re2 (concat pre re2)
  7349. re2a (concat pre (if pre re2a_ re2a))
  7350. re4 (concat pre (if pre re4_ re4))
  7351. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7352. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7353. re5 "\\)"
  7354. ))
  7355. (cond
  7356. ((eq type 'org-occur) (org-occur reall))
  7357. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7358. (t (goto-char (point-min))
  7359. (setq type 'fuzzy)
  7360. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7361. (org-search-not-self 1 re1 nil t)
  7362. (org-search-not-self 1 re2 nil t)
  7363. (org-search-not-self 1 re2a nil t)
  7364. (org-search-not-self 1 re3 nil t)
  7365. (org-search-not-self 1 re4 nil t)
  7366. (org-search-not-self 1 re5 nil t)
  7367. )
  7368. (goto-char (match-beginning 1))
  7369. (goto-char pos)
  7370. (error "No match")))))
  7371. (t
  7372. ;; Normal string-search
  7373. (goto-char (point-min))
  7374. (if (search-forward s nil t)
  7375. (goto-char (match-beginning 0))
  7376. (error "No match"))))
  7377. (and (org-mode-p) (org-show-context 'link-search))
  7378. type))
  7379. (defun org-search-not-self (group &rest args)
  7380. "Execute `re-search-forward', but only accept matches that do not
  7381. enclose the position of `org-open-link-marker'."
  7382. (let ((m org-open-link-marker))
  7383. (catch 'exit
  7384. (while (apply 're-search-forward args)
  7385. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7386. (goto-char (match-end group))
  7387. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7388. (> (match-beginning 0) (marker-position m))
  7389. (< (match-end 0) (marker-position m)))
  7390. (save-match-data
  7391. (or (not (org-in-regexp
  7392. org-bracket-link-analytic-regexp 1))
  7393. (not (match-end 4)) ; no description
  7394. (and (<= (match-beginning 4) (point))
  7395. (>= (match-end 4) (point))))))
  7396. (throw 'exit (point))))))))
  7397. (defun org-get-buffer-for-internal-link (buffer)
  7398. "Return a buffer to be used for displaying the link target of internal links."
  7399. (cond
  7400. ((not org-display-internal-link-with-indirect-buffer)
  7401. buffer)
  7402. ((string-match "(Clone)$" (buffer-name buffer))
  7403. (message "Buffer is already a clone, not making another one")
  7404. ;; we also do not modify visibility in this case
  7405. buffer)
  7406. (t ; make a new indirect buffer for displaying the link
  7407. (let* ((bn (buffer-name buffer))
  7408. (ibn (concat bn "(Clone)"))
  7409. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7410. (with-current-buffer ib (org-overview))
  7411. ib))))
  7412. (defun org-do-occur (regexp &optional cleanup)
  7413. "Call the Emacs command `occur'.
  7414. If CLEANUP is non-nil, remove the printout of the regular expression
  7415. in the *Occur* buffer. This is useful if the regex is long and not useful
  7416. to read."
  7417. (occur regexp)
  7418. (when cleanup
  7419. (let ((cwin (selected-window)) win beg end)
  7420. (when (setq win (get-buffer-window "*Occur*"))
  7421. (select-window win))
  7422. (goto-char (point-min))
  7423. (when (re-search-forward "match[a-z]+" nil t)
  7424. (setq beg (match-end 0))
  7425. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7426. (setq end (1- (match-beginning 0)))))
  7427. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7428. (goto-char (point-min))
  7429. (select-window cwin))))
  7430. ;;; The mark ring for links jumps
  7431. (defvar org-mark-ring nil
  7432. "Mark ring for positions before jumps in Org-mode.")
  7433. (defvar org-mark-ring-last-goto nil
  7434. "Last position in the mark ring used to go back.")
  7435. ;; Fill and close the ring
  7436. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7437. (loop for i from 1 to org-mark-ring-length do
  7438. (push (make-marker) org-mark-ring))
  7439. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7440. org-mark-ring)
  7441. (defun org-mark-ring-push (&optional pos buffer)
  7442. "Put the current position or POS into the mark ring and rotate it."
  7443. (interactive)
  7444. (setq pos (or pos (point)))
  7445. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7446. (move-marker (car org-mark-ring)
  7447. (or pos (point))
  7448. (or buffer (current-buffer)))
  7449. (message "%s"
  7450. (substitute-command-keys
  7451. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7452. (defun org-mark-ring-goto (&optional n)
  7453. "Jump to the previous position in the mark ring.
  7454. With prefix arg N, jump back that many stored positions. When
  7455. called several times in succession, walk through the entire ring.
  7456. Org-mode commands jumping to a different position in the current file,
  7457. or to another Org-mode file, automatically push the old position
  7458. onto the ring."
  7459. (interactive "p")
  7460. (let (p m)
  7461. (if (eq last-command this-command)
  7462. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7463. (setq p org-mark-ring))
  7464. (setq org-mark-ring-last-goto p)
  7465. (setq m (car p))
  7466. (switch-to-buffer (marker-buffer m))
  7467. (goto-char m)
  7468. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7469. (defun org-remove-angle-brackets (s)
  7470. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7471. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7472. s)
  7473. (defun org-add-angle-brackets (s)
  7474. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7475. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7476. s)
  7477. (defun org-remove-double-quotes (s)
  7478. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7479. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7480. s)
  7481. ;;; Following specific links
  7482. (defun org-follow-timestamp-link ()
  7483. (cond
  7484. ((org-at-date-range-p t)
  7485. (let ((org-agenda-start-on-weekday)
  7486. (t1 (match-string 1))
  7487. (t2 (match-string 2)))
  7488. (setq t1 (time-to-days (org-time-string-to-time t1))
  7489. t2 (time-to-days (org-time-string-to-time t2)))
  7490. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7491. ((org-at-timestamp-p t)
  7492. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7493. (substring (match-string 1) 0 10)))
  7494. 1))
  7495. (t (error "This should not happen"))))
  7496. ;;; Following file links
  7497. (defvar org-wait nil)
  7498. (defun org-open-file (path &optional in-emacs line search)
  7499. "Open the file at PATH.
  7500. First, this expands any special file name abbreviations. Then the
  7501. configuration variable `org-file-apps' is checked if it contains an
  7502. entry for this file type, and if yes, the corresponding command is launched.
  7503. If no application is found, Emacs simply visits the file.
  7504. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7505. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7506. and o use an external application to visit the file.
  7507. Optional LINE specifies a line to go to, optional SEARCH a string to
  7508. search for. If LINE or SEARCH is given, the file will always be
  7509. opened in Emacs.
  7510. If the file does not exist, an error is thrown."
  7511. (setq in-emacs (or in-emacs line search))
  7512. (let* ((file (if (equal path "")
  7513. buffer-file-name
  7514. (substitute-in-file-name (expand-file-name path))))
  7515. (apps (append org-file-apps (org-default-apps)))
  7516. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7517. (dirp (if remp nil (file-directory-p file)))
  7518. (file (if (and dirp org-open-directory-means-index-dot-org)
  7519. (concat (file-name-as-directory file) "index.org")
  7520. file))
  7521. (a-m-a-p (assq 'auto-mode apps))
  7522. (dfile (downcase file))
  7523. (old-buffer (current-buffer))
  7524. (old-pos (point))
  7525. (old-mode major-mode)
  7526. ext cmd)
  7527. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7528. (setq ext (match-string 1 dfile))
  7529. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7530. (setq ext (match-string 1 dfile))))
  7531. (cond
  7532. ((equal in-emacs '(16))
  7533. (setq cmd (cdr (assoc 'system apps))))
  7534. (in-emacs (setq cmd 'emacs))
  7535. (t
  7536. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7537. (and dirp (cdr (assoc 'directory apps)))
  7538. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7539. 'string-match)
  7540. (cdr (assoc ext apps))
  7541. (cdr (assoc t apps))))))
  7542. (when (eq cmd 'system)
  7543. (setq cmd (cdr (assoc 'system apps))))
  7544. (when (eq cmd 'default)
  7545. (setq cmd (cdr (assoc t apps))))
  7546. (when (eq cmd 'mailcap)
  7547. (require 'mailcap)
  7548. (mailcap-parse-mailcaps)
  7549. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7550. (command (mailcap-mime-info mime-type)))
  7551. (if (stringp command)
  7552. (setq cmd command)
  7553. (setq cmd 'emacs))))
  7554. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7555. (not (file-exists-p file))
  7556. (not org-open-non-existing-files))
  7557. (error "No such file: %s" file))
  7558. (cond
  7559. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7560. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7561. (while (string-match "['\"]%s['\"]" cmd)
  7562. (setq cmd (replace-match "%s" t t cmd)))
  7563. (while (string-match "%s" cmd)
  7564. (setq cmd (replace-match
  7565. (save-match-data
  7566. (shell-quote-argument
  7567. (convert-standard-filename file)))
  7568. t t cmd)))
  7569. (save-window-excursion
  7570. (start-process-shell-command cmd nil cmd)
  7571. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7572. ))
  7573. ((or (stringp cmd)
  7574. (eq cmd 'emacs))
  7575. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7576. (widen)
  7577. (if line (goto-line line)
  7578. (if search (org-link-search search))))
  7579. ((consp cmd)
  7580. (let ((file (convert-standard-filename file)))
  7581. (eval cmd)))
  7582. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7583. (and (org-mode-p) (eq old-mode 'org-mode)
  7584. (or (not (equal old-buffer (current-buffer)))
  7585. (not (equal old-pos (point))))
  7586. (org-mark-ring-push old-pos old-buffer))))
  7587. (defun org-default-apps ()
  7588. "Return the default applications for this operating system."
  7589. (cond
  7590. ((eq system-type 'darwin)
  7591. org-file-apps-defaults-macosx)
  7592. ((eq system-type 'windows-nt)
  7593. org-file-apps-defaults-windowsnt)
  7594. (t org-file-apps-defaults-gnu)))
  7595. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7596. "Convert extensions to regular expressions in the cars of LIST.
  7597. Also, weed out any non-string entries, because the return value is used
  7598. only for regexp matching.
  7599. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7600. point to the symbol `emacs', indicating that the file should
  7601. be opened in Emacs."
  7602. (append
  7603. (delq nil
  7604. (mapcar (lambda (x)
  7605. (if (not (stringp (car x)))
  7606. nil
  7607. (if (string-match "\\W" (car x))
  7608. x
  7609. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7610. list))
  7611. (if add-auto-mode
  7612. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7613. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7614. (defun org-file-remote-p (file)
  7615. "Test whether FILE specifies a location on a remote system.
  7616. Return non-nil if the location is indeed remote.
  7617. For example, the filename \"/user@host:/foo\" specifies a location
  7618. on the system \"/user@host:\"."
  7619. (cond ((fboundp 'file-remote-p)
  7620. (file-remote-p file))
  7621. ((fboundp 'tramp-handle-file-remote-p)
  7622. (tramp-handle-file-remote-p file))
  7623. ((and (boundp 'ange-ftp-name-format)
  7624. (string-match (car ange-ftp-name-format) file))
  7625. t)
  7626. (t nil)))
  7627. ;;;; Refiling
  7628. (defun org-get-org-file ()
  7629. "Read a filename, with default directory `org-directory'."
  7630. (let ((default (or org-default-notes-file remember-data-file)))
  7631. (read-file-name (format "File name [%s]: " default)
  7632. (file-name-as-directory org-directory)
  7633. default)))
  7634. (defun org-notes-order-reversed-p ()
  7635. "Check if the current file should receive notes in reversed order."
  7636. (cond
  7637. ((not org-reverse-note-order) nil)
  7638. ((eq t org-reverse-note-order) t)
  7639. ((not (listp org-reverse-note-order)) nil)
  7640. (t (catch 'exit
  7641. (let ((all org-reverse-note-order)
  7642. entry)
  7643. (while (setq entry (pop all))
  7644. (if (string-match (car entry) buffer-file-name)
  7645. (throw 'exit (cdr entry))))
  7646. nil)))))
  7647. (defvar org-refile-target-table nil
  7648. "The list of refile targets, created by `org-refile'.")
  7649. (defvar org-agenda-new-buffers nil
  7650. "Buffers created to visit agenda files.")
  7651. (defun org-get-refile-targets (&optional default-buffer)
  7652. "Produce a table with refile targets."
  7653. (let ((case-fold-search nil)
  7654. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7655. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7656. targets txt re files f desc descre fast-path-p level pos0)
  7657. (message "Getting targets...")
  7658. (with-current-buffer (or default-buffer (current-buffer))
  7659. (while (setq entry (pop entries))
  7660. (setq files (car entry) desc (cdr entry))
  7661. (setq fast-path-p nil)
  7662. (cond
  7663. ((null files) (setq files (list (current-buffer))))
  7664. ((eq files 'org-agenda-files)
  7665. (setq files (org-agenda-files 'unrestricted)))
  7666. ((and (symbolp files) (fboundp files))
  7667. (setq files (funcall files)))
  7668. ((and (symbolp files) (boundp files))
  7669. (setq files (symbol-value files))))
  7670. (if (stringp files) (setq files (list files)))
  7671. (cond
  7672. ((eq (car desc) :tag)
  7673. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7674. ((eq (car desc) :todo)
  7675. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7676. ((eq (car desc) :regexp)
  7677. (setq descre (cdr desc)))
  7678. ((eq (car desc) :level)
  7679. (setq descre (concat "^\\*\\{" (number-to-string
  7680. (if org-odd-levels-only
  7681. (1- (* 2 (cdr desc)))
  7682. (cdr desc)))
  7683. "\\}[ \t]")))
  7684. ((eq (car desc) :maxlevel)
  7685. (setq fast-path-p t)
  7686. (setq descre (concat "^\\*\\{1," (number-to-string
  7687. (if org-odd-levels-only
  7688. (1- (* 2 (cdr desc)))
  7689. (cdr desc)))
  7690. "\\}[ \t]")))
  7691. (t (error "Bad refiling target description %s" desc)))
  7692. (while (setq f (pop files))
  7693. (save-excursion
  7694. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7695. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7696. (setq f (expand-file-name f))
  7697. (if (eq org-refile-use-outline-path 'file)
  7698. (push (list (file-name-nondirectory f) f nil nil) targets))
  7699. (save-excursion
  7700. (save-restriction
  7701. (widen)
  7702. (goto-char (point-min))
  7703. (while (re-search-forward descre nil t)
  7704. (goto-char (setq pos0 (point-at-bol)))
  7705. (catch 'next
  7706. (when org-refile-target-verify-function
  7707. (save-match-data
  7708. (or (funcall org-refile-target-verify-function)
  7709. (throw 'next t))))
  7710. (when (looking-at org-complex-heading-regexp)
  7711. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7712. txt (org-link-display-format (match-string 4))
  7713. re (concat "^" (regexp-quote
  7714. (buffer-substring (match-beginning 1)
  7715. (match-end 4)))))
  7716. (if (match-end 5) (setq re (concat re "[ \t]+"
  7717. (regexp-quote
  7718. (match-string 5)))))
  7719. (setq re (concat re "[ \t]*$"))
  7720. (when org-refile-use-outline-path
  7721. (setq txt (mapconcat 'org-protect-slash
  7722. (append
  7723. (if (eq org-refile-use-outline-path 'file)
  7724. (list (file-name-nondirectory
  7725. (buffer-file-name (buffer-base-buffer))))
  7726. (if (eq org-refile-use-outline-path 'full-file-path)
  7727. (list (buffer-file-name (buffer-base-buffer)))))
  7728. (org-get-outline-path fast-path-p level txt)
  7729. (list txt))
  7730. "/")))
  7731. (push (list txt f re (point)) targets)))
  7732. (when (= (point) pos0)
  7733. ;; verification function has not moved point
  7734. (goto-char (point-at-eol))))))))))
  7735. (message "Getting targets...done")
  7736. (nreverse targets)))
  7737. (defun org-protect-slash (s)
  7738. (while (string-match "/" s)
  7739. (setq s (replace-match "\\" t t s)))
  7740. s)
  7741. (defvar org-olpa (make-vector 20 nil))
  7742. (defun org-get-outline-path (&optional fastp level heading)
  7743. "Return the outline path to the current entry, as a list."
  7744. (if fastp
  7745. (progn
  7746. (if (> level 19)
  7747. (error "Outline path failure, more than 19 levels."))
  7748. (loop for i from level upto 19 do
  7749. (aset org-olpa i nil))
  7750. (prog1
  7751. (delq nil (append org-olpa nil))
  7752. (aset org-olpa level heading)))
  7753. (let (rtn)
  7754. (save-excursion
  7755. (while (org-up-heading-safe)
  7756. (when (looking-at org-complex-heading-regexp)
  7757. (push (org-match-string-no-properties 4) rtn)))
  7758. rtn))))
  7759. (defvar org-refile-history nil
  7760. "History for refiling operations.")
  7761. (defvar org-after-refile-insert-hook nil
  7762. "Hook run after `org-refile' has inserted its stuff at the new location.
  7763. Note that this is still *before* the stuff will be removed from
  7764. the *old* location.")
  7765. (defun org-refile (&optional goto default-buffer rfloc)
  7766. "Move the entry at point to another heading.
  7767. The list of target headings is compiled using the information in
  7768. `org-refile-targets', which see. This list is created before each use
  7769. and will therefore always be up-to-date.
  7770. At the target location, the entry is filed as a subitem of the target heading.
  7771. Depending on `org-reverse-note-order', the new subitem will either be the
  7772. first or the last subitem.
  7773. If there is an active region, all entries in that region will be moved.
  7774. However, the region must fulfil the requirement that the first heading
  7775. is the first one sets the top-level of the moved text - at most siblings
  7776. below it are allowed.
  7777. With prefix arg GOTO, the command will only visit the target location,
  7778. not actually move anything.
  7779. With a double prefix `C-u C-u', go to the location where the last refiling
  7780. operation has put the subtree.
  7781. RFLOC can be a refile location obtained in a different way.
  7782. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7783. (interactive "P")
  7784. (let* ((cbuf (current-buffer))
  7785. (regionp (org-region-active-p))
  7786. (region-start (and regionp (region-beginning)))
  7787. (region-end (and regionp (region-end)))
  7788. (region-length (and regionp (- region-end region-start)))
  7789. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7790. pos it nbuf file re level reversed)
  7791. (when regionp
  7792. (goto-char region-start)
  7793. (or (bolp) (goto-char (point-at-bol)))
  7794. (setq region-start (point))
  7795. (unless (org-kill-is-subtree-p
  7796. (buffer-substring region-start region-end))
  7797. (error "The region is not a (sequence of) subtree(s)")))
  7798. (if (equal goto '(16))
  7799. (org-refile-goto-last-stored)
  7800. (when (setq it (or rfloc
  7801. (save-excursion
  7802. (org-refile-get-location
  7803. (if goto "Goto: " "Refile to: ") default-buffer
  7804. org-refile-allow-creating-parent-nodes))))
  7805. (setq file (nth 1 it)
  7806. re (nth 2 it)
  7807. pos (nth 3 it))
  7808. (if (and (not goto)
  7809. pos
  7810. (equal (buffer-file-name) file)
  7811. (if regionp
  7812. (and (>= pos region-start)
  7813. (<= pos region-end))
  7814. (and (>= pos (point))
  7815. (< pos (save-excursion
  7816. (org-end-of-subtree t t))))))
  7817. (error "Cannot refile to position inside the tree or region"))
  7818. (setq nbuf (or (find-buffer-visiting file)
  7819. (find-file-noselect file)))
  7820. (if goto
  7821. (progn
  7822. (switch-to-buffer nbuf)
  7823. (goto-char pos)
  7824. (org-show-context 'org-goto))
  7825. (if regionp
  7826. (progn
  7827. (org-kill-new (buffer-substring region-start region-end))
  7828. (org-save-markers-in-region region-start region-end))
  7829. (org-copy-subtree 1 nil t))
  7830. (save-excursion
  7831. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7832. (find-file-noselect file))))
  7833. (setq reversed (org-notes-order-reversed-p))
  7834. (save-excursion
  7835. (save-restriction
  7836. (widen)
  7837. (if pos
  7838. (progn
  7839. (goto-char pos)
  7840. (looking-at outline-regexp)
  7841. (setq level (org-get-valid-level (funcall outline-level) 1))
  7842. (goto-char
  7843. (if reversed
  7844. (or (outline-next-heading) (point-max))
  7845. (or (save-excursion (outline-get-next-sibling))
  7846. (org-end-of-subtree t t)
  7847. (point-max)))))
  7848. (setq level 1)
  7849. (if (not reversed)
  7850. (goto-char (point-max))
  7851. (goto-char (point-min))
  7852. (or (outline-next-heading) (goto-char (point-max)))))
  7853. (if (not (bolp)) (newline))
  7854. (bookmark-set "org-refile-last-stored")
  7855. (org-paste-subtree level)
  7856. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7857. (run-hooks 'org-after-refile-insert-hook))))
  7858. (if regionp
  7859. (delete-region (point) (+ (point) region-length))
  7860. (org-cut-subtree))
  7861. (when (featurep 'org-inlinetask)
  7862. (org-inlinetask-remove-END-maybe))
  7863. (setq org-markers-to-move nil)
  7864. (message "Refiled to \"%s\"" (car it))))))
  7865. (org-reveal))
  7866. (defun org-refile-goto-last-stored ()
  7867. "Go to the location where the last refile was stored."
  7868. (interactive)
  7869. (bookmark-jump "org-refile-last-stored")
  7870. (message "This is the location of the last refile"))
  7871. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7872. "Prompt the user for a refile location, using PROMPT."
  7873. (let ((org-refile-targets org-refile-targets)
  7874. (org-refile-use-outline-path org-refile-use-outline-path))
  7875. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7876. (unless org-refile-target-table
  7877. (error "No refile targets"))
  7878. (let* ((cbuf (current-buffer))
  7879. (partial-completion-mode nil)
  7880. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7881. (cfunc (if (and org-refile-use-outline-path
  7882. org-outline-path-complete-in-steps)
  7883. 'org-olpath-completing-read
  7884. 'org-icompleting-read))
  7885. (extra (if org-refile-use-outline-path "/" ""))
  7886. (filename (and cfn (expand-file-name cfn)))
  7887. (tbl (mapcar
  7888. (lambda (x)
  7889. (if (and (not (member org-refile-use-outline-path
  7890. '(file full-file-path)))
  7891. (not (equal filename (nth 1 x))))
  7892. (cons (concat (car x) extra " ("
  7893. (file-name-nondirectory (nth 1 x)) ")")
  7894. (cdr x))
  7895. (cons (concat (car x) extra) (cdr x))))
  7896. org-refile-target-table))
  7897. (completion-ignore-case t)
  7898. pa answ parent-target child parent old-hist)
  7899. (setq old-hist org-refile-history)
  7900. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7901. nil 'org-refile-history))
  7902. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7903. (if pa
  7904. (progn
  7905. (when (or (not org-refile-history)
  7906. (not (eq old-hist org-refile-history))
  7907. (not (equal (car pa) (car org-refile-history))))
  7908. (setq org-refile-history
  7909. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  7910. org-refile-history
  7911. (cdr org-refile-history))))
  7912. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  7913. (pop org-refile-history)))
  7914. pa)
  7915. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7916. (setq parent (match-string 1 answ)
  7917. child (match-string 2 answ))
  7918. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7919. (when (and parent-target
  7920. (or (eq new-nodes t)
  7921. (and (eq new-nodes 'confirm)
  7922. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7923. (org-refile-new-child parent-target child))))))
  7924. (defun org-refile-new-child (parent-target child)
  7925. "Use refile target PARENT-TARGET to add new CHILD below it."
  7926. (unless parent-target
  7927. (error "Cannot find parent for new node"))
  7928. (let ((file (nth 1 parent-target))
  7929. (pos (nth 3 parent-target))
  7930. level)
  7931. (with-current-buffer (or (find-buffer-visiting file)
  7932. (find-file-noselect file))
  7933. (save-excursion
  7934. (save-restriction
  7935. (widen)
  7936. (if pos
  7937. (goto-char pos)
  7938. (goto-char (point-max))
  7939. (if (not (bolp)) (newline)))
  7940. (when (looking-at outline-regexp)
  7941. (setq level (funcall outline-level))
  7942. (org-end-of-subtree t t))
  7943. (org-back-over-empty-lines)
  7944. (insert "\n" (make-string
  7945. (if pos (org-get-valid-level level 1) 1) ?*)
  7946. " " child "\n")
  7947. (beginning-of-line 0)
  7948. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7949. (defun org-olpath-completing-read (prompt collection &rest args)
  7950. "Read an outline path like a file name."
  7951. (let ((thetable collection)
  7952. (org-completion-use-ido nil) ; does not work with ido.
  7953. (org-completion-use-iswitchb nil)) ; or iswitchb
  7954. (apply
  7955. 'org-icompleting-read prompt
  7956. (lambda (string predicate &optional flag)
  7957. (let (rtn r f (l (length string)))
  7958. (cond
  7959. ((eq flag nil)
  7960. ;; try completion
  7961. (try-completion string thetable))
  7962. ((eq flag t)
  7963. ;; all-completions
  7964. (setq rtn (all-completions string thetable predicate))
  7965. (mapcar
  7966. (lambda (x)
  7967. (setq r (substring x l))
  7968. (if (string-match " ([^)]*)$" x)
  7969. (setq f (match-string 0 x))
  7970. (setq f ""))
  7971. (if (string-match "/" r)
  7972. (concat string (substring r 0 (match-end 0)) f)
  7973. x))
  7974. rtn))
  7975. ((eq flag 'lambda)
  7976. ;; exact match?
  7977. (assoc string thetable)))
  7978. ))
  7979. args)))
  7980. ;;;; Dynamic blocks
  7981. (defun org-find-dblock (name)
  7982. "Find the first dynamic block with name NAME in the buffer.
  7983. If not found, stay at current position and return nil."
  7984. (let (pos)
  7985. (save-excursion
  7986. (goto-char (point-min))
  7987. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7988. nil t)
  7989. (match-beginning 0))))
  7990. (if pos (goto-char pos))
  7991. pos))
  7992. (defconst org-dblock-start-re
  7993. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7994. "Matches the startline of a dynamic block, with parameters.")
  7995. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7996. "Matches the end of a dynamic block.")
  7997. (defun org-create-dblock (plist)
  7998. "Create a dynamic block section, with parameters taken from PLIST.
  7999. PLIST must contain a :name entry which is used as name of the block."
  8000. (unless (bolp) (newline))
  8001. (let ((name (plist-get plist :name)))
  8002. (insert "#+BEGIN: " name)
  8003. (while plist
  8004. (if (eq (car plist) :name)
  8005. (setq plist (cddr plist))
  8006. (insert " " (prin1-to-string (pop plist)))))
  8007. (insert "\n\n#+END:\n")
  8008. (beginning-of-line -2)))
  8009. (defun org-prepare-dblock ()
  8010. "Prepare dynamic block for refresh.
  8011. This empties the block, puts the cursor at the insert position and returns
  8012. the property list including an extra property :name with the block name."
  8013. (unless (looking-at org-dblock-start-re)
  8014. (error "Not at a dynamic block"))
  8015. (let* ((begdel (1+ (match-end 0)))
  8016. (name (org-no-properties (match-string 1)))
  8017. (params (append (list :name name)
  8018. (read (concat "(" (match-string 3) ")")))))
  8019. (unless (re-search-forward org-dblock-end-re nil t)
  8020. (error "Dynamic block not terminated"))
  8021. (setq params
  8022. (append params
  8023. (list :content (buffer-substring
  8024. begdel (match-beginning 0)))))
  8025. (delete-region begdel (match-beginning 0))
  8026. (goto-char begdel)
  8027. (open-line 1)
  8028. params))
  8029. (defun org-map-dblocks (&optional command)
  8030. "Apply COMMAND to all dynamic blocks in the current buffer.
  8031. If COMMAND is not given, use `org-update-dblock'."
  8032. (let ((cmd (or command 'org-update-dblock))
  8033. pos)
  8034. (save-excursion
  8035. (goto-char (point-min))
  8036. (while (re-search-forward org-dblock-start-re nil t)
  8037. (goto-char (setq pos (match-beginning 0)))
  8038. (condition-case nil
  8039. (funcall cmd)
  8040. (error (message "Error during update of dynamic block")))
  8041. (goto-char pos)
  8042. (unless (re-search-forward org-dblock-end-re nil t)
  8043. (error "Dynamic block not terminated"))))))
  8044. (defun org-dblock-update (&optional arg)
  8045. "User command for updating dynamic blocks.
  8046. Update the dynamic block at point. With prefix ARG, update all dynamic
  8047. blocks in the buffer."
  8048. (interactive "P")
  8049. (if arg
  8050. (org-update-all-dblocks)
  8051. (or (looking-at org-dblock-start-re)
  8052. (org-beginning-of-dblock))
  8053. (org-update-dblock)))
  8054. (defun org-update-dblock ()
  8055. "Update the dynamic block at point
  8056. This means to empty the block, parse for parameters and then call
  8057. the correct writing function."
  8058. (save-window-excursion
  8059. (let* ((pos (point))
  8060. (line (org-current-line))
  8061. (params (org-prepare-dblock))
  8062. (name (plist-get params :name))
  8063. (cmd (intern (concat "org-dblock-write:" name))))
  8064. (message "Updating dynamic block `%s' at line %d..." name line)
  8065. (funcall cmd params)
  8066. (message "Updating dynamic block `%s' at line %d...done" name line)
  8067. (goto-char pos))))
  8068. (defun org-beginning-of-dblock ()
  8069. "Find the beginning of the dynamic block at point.
  8070. Error if there is no such block at point."
  8071. (let ((pos (point))
  8072. beg)
  8073. (end-of-line 1)
  8074. (if (and (re-search-backward org-dblock-start-re nil t)
  8075. (setq beg (match-beginning 0))
  8076. (re-search-forward org-dblock-end-re nil t)
  8077. (> (match-end 0) pos))
  8078. (goto-char beg)
  8079. (goto-char pos)
  8080. (error "Not in a dynamic block"))))
  8081. (defun org-update-all-dblocks ()
  8082. "Update all dynamic blocks in the buffer.
  8083. This function can be used in a hook."
  8084. (when (org-mode-p)
  8085. (org-map-dblocks 'org-update-dblock)))
  8086. ;;;; Completion
  8087. (defconst org-additional-option-like-keywords
  8088. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8089. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8090. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8091. "BEGIN:" "END:"
  8092. "ORGTBL" "TBLFM:" "TBLNAME:"
  8093. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8094. "BEGIN_QUOTE" "END_QUOTE"
  8095. "BEGIN_VERSE" "END_VERSE"
  8096. "BEGIN_CENTER" "END_CENTER"
  8097. "BEGIN_SRC" "END_SRC"
  8098. "CATEGORY" "COLUMNS"
  8099. "CAPTION" "LABEL"
  8100. "BIND"
  8101. "MACRO"))
  8102. (defcustom org-structure-template-alist
  8103. '(
  8104. ("s" "#+begin_src ?\n\n#+end_src"
  8105. "<src lang=\"?\">\n\n</src>")
  8106. ("e" "#+begin_example\n?\n#+end_example"
  8107. "<example>\n?\n</example>")
  8108. ("q" "#+begin_quote\n?\n#+end_quote"
  8109. "<quote>\n?\n</quote>")
  8110. ("v" "#+begin_verse\n?\n#+end_verse"
  8111. "<verse>\n?\n/verse>")
  8112. ("c" "#+begin_center\n?\n#+end_center"
  8113. "<center>\n?\n/center>")
  8114. ("l" "#+begin_latex\n?\n#+end_latex"
  8115. "<literal style=\"latex\">\n?\n</literal>")
  8116. ("L" "#+latex: "
  8117. "<literal style=\"latex\">?</literal>")
  8118. ("h" "#+begin_html\n?\n#+end_html"
  8119. "<literal style=\"html\">\n?\n</literal>")
  8120. ("H" "#+html: "
  8121. "<literal style=\"html\">?</literal>")
  8122. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8123. ("A" "#+ascii: ")
  8124. ("i" "#+include %file ?"
  8125. "<include file=%file markup=\"?\">")
  8126. )
  8127. "Structure completion elements.
  8128. This is a list of abbreviation keys and values. The value gets inserted
  8129. it you type @samp{.} followed by the key and then the completion key,
  8130. usually `M-TAB'. %file will be replaced by a file name after prompting
  8131. for the file using completion.
  8132. There are two templates for each key, the first uses the original Org syntax,
  8133. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8134. the default when the /org-mtags.el/ module has been loaded. See also the
  8135. variable `org-mtags-prefer-muse-templates'.
  8136. This is an experimental feature, it is undecided if it is going to stay in."
  8137. :group 'org-completion
  8138. :type '(repeat
  8139. (string :tag "Key")
  8140. (string :tag "Template")
  8141. (string :tag "Muse Template")))
  8142. (defun org-try-structure-completion ()
  8143. "Try to complete a structure template before point.
  8144. This looks for strings like \"<e\" on an otherwise empty line and
  8145. expands them."
  8146. (let ((l (buffer-substring (point-at-bol) (point)))
  8147. a)
  8148. (when (and (looking-at "[ \t]*$")
  8149. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8150. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8151. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8152. (match-beginning 1)) a)
  8153. t)))
  8154. (defun org-complete-expand-structure-template (start cell)
  8155. "Expand a structure template."
  8156. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8157. (rpl (nth (if musep 2 1) cell))
  8158. (ind ""))
  8159. (delete-region start (point))
  8160. (when (string-match "\\`#\\+" rpl)
  8161. (cond
  8162. ((bolp))
  8163. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8164. (setq ind (buffer-substring (point-at-bol) (point))))
  8165. (t (newline))))
  8166. (setq start (point))
  8167. (if (string-match "%file" rpl)
  8168. (setq rpl (replace-match
  8169. (concat
  8170. "\""
  8171. (save-match-data
  8172. (abbreviate-file-name (read-file-name "Include file: ")))
  8173. "\"")
  8174. t t rpl)))
  8175. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8176. (concat "\n" ind)))
  8177. (insert rpl)
  8178. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8179. (defun org-complete (&optional arg)
  8180. "Perform completion on word at point.
  8181. At the beginning of a headline, this completes TODO keywords as given in
  8182. `org-todo-keywords'.
  8183. If the current word is preceded by a backslash, completes the TeX symbols
  8184. that are supported for HTML support.
  8185. If the current word is preceded by \"#+\", completes special words for
  8186. setting file options.
  8187. In the line after \"#+STARTUP:, complete valid keywords.\"
  8188. At all other locations, this simply calls the value of
  8189. `org-completion-fallback-command'."
  8190. (interactive "P")
  8191. (org-without-partial-completion
  8192. (catch 'exit
  8193. (let* ((a nil)
  8194. (end (point))
  8195. (beg1 (save-excursion
  8196. (skip-chars-backward (org-re "[:alnum:]_@"))
  8197. (point)))
  8198. (beg (save-excursion
  8199. (skip-chars-backward "a-zA-Z0-9_:$")
  8200. (point)))
  8201. (confirm (lambda (x) (stringp (car x))))
  8202. (searchhead (equal (char-before beg) ?*))
  8203. (struct
  8204. (when (and (member (char-before beg1) '(?. ?<))
  8205. (setq a (assoc (buffer-substring beg1 (point))
  8206. org-structure-template-alist)))
  8207. (org-complete-expand-structure-template (1- beg1) a)
  8208. (throw 'exit t)))
  8209. (tag (and (equal (char-before beg1) ?:)
  8210. (equal (char-after (point-at-bol)) ?*)))
  8211. (prop (and (equal (char-before beg1) ?:)
  8212. (not (equal (char-after (point-at-bol)) ?*))))
  8213. (texp (equal (char-before beg) ?\\))
  8214. (link (equal (char-before beg) ?\[))
  8215. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8216. beg)
  8217. "#+"))
  8218. (startup (string-match "^#\\+STARTUP:.*"
  8219. (buffer-substring (point-at-bol) (point))))
  8220. (completion-ignore-case opt)
  8221. (type nil)
  8222. (tbl nil)
  8223. (table (cond
  8224. (opt
  8225. (setq type :opt)
  8226. (require 'org-exp)
  8227. (append
  8228. (delq nil
  8229. (mapcar
  8230. (lambda (x)
  8231. (if (string-match
  8232. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8233. (cons (match-string 2 x)
  8234. (match-string 1 x))))
  8235. (org-split-string (org-get-current-options) "\n")))
  8236. (mapcar 'list org-additional-option-like-keywords)))
  8237. (startup
  8238. (setq type :startup)
  8239. org-startup-options)
  8240. (link (append org-link-abbrev-alist-local
  8241. org-link-abbrev-alist))
  8242. (texp
  8243. (setq type :tex)
  8244. org-html-entities)
  8245. ((string-match "\\`\\*+[ \t]+\\'"
  8246. (buffer-substring (point-at-bol) beg))
  8247. (setq type :todo)
  8248. (mapcar 'list org-todo-keywords-1))
  8249. (searchhead
  8250. (setq type :searchhead)
  8251. (save-excursion
  8252. (goto-char (point-min))
  8253. (while (re-search-forward org-todo-line-regexp nil t)
  8254. (push (list
  8255. (org-make-org-heading-search-string
  8256. (match-string 3) t))
  8257. tbl)))
  8258. tbl)
  8259. (tag (setq type :tag beg beg1)
  8260. (or org-tag-alist (org-get-buffer-tags)))
  8261. (prop (setq type :prop beg beg1)
  8262. (mapcar 'list (org-buffer-property-keys nil t t)))
  8263. (t (progn
  8264. (call-interactively org-completion-fallback-command)
  8265. (throw 'exit nil)))))
  8266. (pattern (buffer-substring-no-properties beg end))
  8267. (completion (try-completion pattern table confirm)))
  8268. (cond ((eq completion t)
  8269. (if (not (assoc (upcase pattern) table))
  8270. (message "Already complete")
  8271. (if (and (equal type :opt)
  8272. (not (member (car (assoc (upcase pattern) table))
  8273. org-additional-option-like-keywords)))
  8274. (insert (substring (cdr (assoc (upcase pattern) table))
  8275. (length pattern)))
  8276. (if (memq type '(:tag :prop)) (insert ":")))))
  8277. ((null completion)
  8278. (message "Can't find completion for \"%s\"" pattern)
  8279. (ding))
  8280. ((not (string= pattern completion))
  8281. (delete-region beg end)
  8282. (if (string-match " +$" completion)
  8283. (setq completion (replace-match "" t t completion)))
  8284. (insert completion)
  8285. (if (get-buffer-window "*Completions*")
  8286. (delete-window (get-buffer-window "*Completions*")))
  8287. (if (assoc completion table)
  8288. (if (eq type :todo) (insert " ")
  8289. (if (memq type '(:tag :prop)) (insert ":"))))
  8290. (if (and (equal type :opt) (assoc completion table))
  8291. (message "%s" (substitute-command-keys
  8292. "Press \\[org-complete] again to insert example settings"))))
  8293. (t
  8294. (message "Making completion list...")
  8295. (let ((list (sort (all-completions pattern table confirm)
  8296. 'string<)))
  8297. (with-output-to-temp-buffer "*Completions*"
  8298. (condition-case nil
  8299. ;; Protection needed for XEmacs and emacs 21
  8300. (display-completion-list list pattern)
  8301. (error (display-completion-list list)))))
  8302. (message "Making completion list...%s" "done")))))))
  8303. ;;;; TODO, DEADLINE, Comments
  8304. (defun org-toggle-comment ()
  8305. "Change the COMMENT state of an entry."
  8306. (interactive)
  8307. (save-excursion
  8308. (org-back-to-heading)
  8309. (let (case-fold-search)
  8310. (if (looking-at (concat outline-regexp
  8311. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8312. (replace-match "" t t nil 1)
  8313. (if (looking-at outline-regexp)
  8314. (progn
  8315. (goto-char (match-end 0))
  8316. (insert org-comment-string " ")))))))
  8317. (defvar org-last-todo-state-is-todo nil
  8318. "This is non-nil when the last TODO state change led to a TODO state.
  8319. If the last change removed the TODO tag or switched to DONE, then
  8320. this is nil.")
  8321. (defvar org-setting-tags nil) ; dynamically skipped
  8322. (defun org-parse-local-options (string var)
  8323. "Parse STRING for startup setting relevant for variable VAR."
  8324. (let ((rtn (symbol-value var))
  8325. e opts)
  8326. (save-match-data
  8327. (if (or (not string) (not (string-match "\\S-" string)))
  8328. rtn
  8329. (setq opts (delq nil (mapcar (lambda (x)
  8330. (setq e (assoc x org-startup-options))
  8331. (if (eq (nth 1 e) var) e nil))
  8332. (org-split-string string "[ \t]+"))))
  8333. (if (not opts)
  8334. rtn
  8335. (setq rtn nil)
  8336. (while (setq e (pop opts))
  8337. (if (not (nth 3 e))
  8338. (setq rtn (nth 2 e))
  8339. (if (not (listp rtn)) (setq rtn nil))
  8340. (push (nth 2 e) rtn)))
  8341. rtn)))))
  8342. (defvar org-todo-setup-filter-hook nil
  8343. "Hook for functions that pre-filter todo specs.
  8344. Each function takes a todo spec and returns either `nil' or the spec
  8345. transformed into canonical form." )
  8346. (defvar org-todo-get-default-hook nil
  8347. "Hook for functions that get a default item for todo.
  8348. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8349. `nil' or a string to be used for the todo mark." )
  8350. (defvar org-agenda-headline-snapshot-before-repeat)
  8351. (defun org-todo (&optional arg)
  8352. "Change the TODO state of an item.
  8353. The state of an item is given by a keyword at the start of the heading,
  8354. like
  8355. *** TODO Write paper
  8356. *** DONE Call mom
  8357. The different keywords are specified in the variable `org-todo-keywords'.
  8358. By default the available states are \"TODO\" and \"DONE\".
  8359. So for this example: when the item starts with TODO, it is changed to DONE.
  8360. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8361. DONE are present, add TODO at the beginning of the heading.
  8362. With C-u prefix arg, use completion to determine the new state.
  8363. With numeric prefix arg, switch to that state.
  8364. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8365. With a tripple C-u prefix, circumvent any state blocking.
  8366. For calling through lisp, arg is also interpreted in the following way:
  8367. 'none -> empty state
  8368. \"\"(empty string) -> switch to empty state
  8369. 'done -> switch to DONE
  8370. 'nextset -> switch to the next set of keywords
  8371. 'previousset -> switch to the previous set of keywords
  8372. \"WAITING\" -> switch to the specified keyword, but only if it
  8373. really is a member of `org-todo-keywords'."
  8374. (interactive "P")
  8375. (if (equal arg '(16)) (setq arg 'nextset))
  8376. (let ((org-blocker-hook org-blocker-hook)
  8377. (case-fold-search nil))
  8378. (when (equal arg '(64))
  8379. (setq arg nil org-blocker-hook nil))
  8380. (when (and org-blocker-hook
  8381. (or org-inhibit-blocking
  8382. (org-entry-get nil "NOBLOCKING")))
  8383. (setq org-blocker-hook nil))
  8384. (save-excursion
  8385. (catch 'exit
  8386. (org-back-to-heading)
  8387. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8388. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8389. (looking-at " *"))
  8390. (let* ((match-data (match-data))
  8391. (startpos (point-at-bol))
  8392. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8393. (org-log-done org-log-done)
  8394. (org-log-repeat org-log-repeat)
  8395. (org-todo-log-states org-todo-log-states)
  8396. (this (match-string 1))
  8397. (hl-pos (match-beginning 0))
  8398. (head (org-get-todo-sequence-head this))
  8399. (ass (assoc head org-todo-kwd-alist))
  8400. (interpret (nth 1 ass))
  8401. (done-word (nth 3 ass))
  8402. (final-done-word (nth 4 ass))
  8403. (last-state (or this ""))
  8404. (completion-ignore-case t)
  8405. (member (member this org-todo-keywords-1))
  8406. (tail (cdr member))
  8407. (state (cond
  8408. ((and org-todo-key-trigger
  8409. (or (and (equal arg '(4))
  8410. (eq org-use-fast-todo-selection 'prefix))
  8411. (and (not arg) org-use-fast-todo-selection
  8412. (not (eq org-use-fast-todo-selection
  8413. 'prefix)))))
  8414. ;; Use fast selection
  8415. (org-fast-todo-selection))
  8416. ((and (equal arg '(4))
  8417. (or (not org-use-fast-todo-selection)
  8418. (not org-todo-key-trigger)))
  8419. ;; Read a state with completion
  8420. (org-icompleting-read
  8421. "State: " (mapcar (lambda(x) (list x))
  8422. org-todo-keywords-1)
  8423. nil t))
  8424. ((eq arg 'right)
  8425. (if this
  8426. (if tail (car tail) nil)
  8427. (car org-todo-keywords-1)))
  8428. ((eq arg 'left)
  8429. (if (equal member org-todo-keywords-1)
  8430. nil
  8431. (if this
  8432. (nth (- (length org-todo-keywords-1)
  8433. (length tail) 2)
  8434. org-todo-keywords-1)
  8435. (org-last org-todo-keywords-1))))
  8436. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8437. (setq arg nil))) ; hack to fall back to cycling
  8438. (arg
  8439. ;; user or caller requests a specific state
  8440. (cond
  8441. ((equal arg "") nil)
  8442. ((eq arg 'none) nil)
  8443. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8444. ((eq arg 'nextset)
  8445. (or (car (cdr (member head org-todo-heads)))
  8446. (car org-todo-heads)))
  8447. ((eq arg 'previousset)
  8448. (let ((org-todo-heads (reverse org-todo-heads)))
  8449. (or (car (cdr (member head org-todo-heads)))
  8450. (car org-todo-heads))))
  8451. ((car (member arg org-todo-keywords-1)))
  8452. ((nth (1- (prefix-numeric-value arg))
  8453. org-todo-keywords-1))))
  8454. ((null member) (or head (car org-todo-keywords-1)))
  8455. ((equal this final-done-word) nil) ;; -> make empty
  8456. ((null tail) nil) ;; -> first entry
  8457. ((memq interpret '(type priority))
  8458. (if (eq this-command last-command)
  8459. (car tail)
  8460. (if (> (length tail) 0)
  8461. (or done-word (car org-done-keywords))
  8462. nil)))
  8463. (t
  8464. (car tail))))
  8465. (state (or
  8466. (run-hook-with-args-until-success
  8467. 'org-todo-get-default-hook state last-state)
  8468. state))
  8469. (next (if state (concat " " state " ") " "))
  8470. (change-plist (list :type 'todo-state-change :from this :to state
  8471. :position startpos))
  8472. dolog now-done-p)
  8473. (when org-blocker-hook
  8474. (setq org-last-todo-state-is-todo
  8475. (not (member this org-done-keywords)))
  8476. (unless (save-excursion
  8477. (save-match-data
  8478. (run-hook-with-args-until-failure
  8479. 'org-blocker-hook change-plist)))
  8480. (if (interactive-p)
  8481. (error "TODO state change from %s to %s blocked" this state)
  8482. ;; fail silently
  8483. (message "TODO state change from %s to %s blocked" this state)
  8484. (throw 'exit nil))))
  8485. (store-match-data match-data)
  8486. (replace-match next t t)
  8487. (unless (pos-visible-in-window-p hl-pos)
  8488. (message "TODO state changed to %s" (org-trim next)))
  8489. (unless head
  8490. (setq head (org-get-todo-sequence-head state)
  8491. ass (assoc head org-todo-kwd-alist)
  8492. interpret (nth 1 ass)
  8493. done-word (nth 3 ass)
  8494. final-done-word (nth 4 ass)))
  8495. (when (memq arg '(nextset previousset))
  8496. (message "Keyword-Set %d/%d: %s"
  8497. (- (length org-todo-sets) -1
  8498. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8499. (length org-todo-sets)
  8500. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8501. (setq org-last-todo-state-is-todo
  8502. (not (member state org-done-keywords)))
  8503. (setq now-done-p (and (member state org-done-keywords)
  8504. (not (member this org-done-keywords))))
  8505. (and logging (org-local-logging logging))
  8506. (when (and (or org-todo-log-states org-log-done)
  8507. (not (eq org-inhibit-logging t))
  8508. (not (memq arg '(nextset previousset))))
  8509. ;; we need to look at recording a time and note
  8510. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8511. (nth 2 (assoc this org-todo-log-states))))
  8512. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8513. (setq dolog 'time))
  8514. (when (and state
  8515. (member state org-not-done-keywords)
  8516. (not (member this org-not-done-keywords)))
  8517. ;; This is now a todo state and was not one before
  8518. ;; If there was a CLOSED time stamp, get rid of it.
  8519. (org-add-planning-info nil nil 'closed))
  8520. (when (and now-done-p org-log-done)
  8521. ;; It is now done, and it was not done before
  8522. (org-add-planning-info 'closed (org-current-time))
  8523. (if (and (not dolog) (eq 'note org-log-done))
  8524. (org-add-log-setup 'done state this 'findpos 'note)))
  8525. (when (and state dolog)
  8526. ;; This is a non-nil state, and we need to log it
  8527. (org-add-log-setup 'state state this 'findpos dolog)))
  8528. ;; Fixup tag positioning
  8529. (org-todo-trigger-tag-changes state)
  8530. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8531. (when org-provide-todo-statistics
  8532. (org-update-parent-todo-statistics))
  8533. (run-hooks 'org-after-todo-state-change-hook)
  8534. (if (and arg (not (member state org-done-keywords)))
  8535. (setq head (org-get-todo-sequence-head state)))
  8536. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8537. ;; Do we need to trigger a repeat?
  8538. (when now-done-p
  8539. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8540. ;; This is for the agenda, take a snapshot of the headline.
  8541. (save-match-data
  8542. (setq org-agenda-headline-snapshot-before-repeat
  8543. (org-get-heading))))
  8544. (org-auto-repeat-maybe state))
  8545. ;; Fixup cursor location if close to the keyword
  8546. (if (and (outline-on-heading-p)
  8547. (not (bolp))
  8548. (save-excursion (beginning-of-line 1)
  8549. (looking-at org-todo-line-regexp))
  8550. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8551. (progn
  8552. (goto-char (or (match-end 2) (match-end 1)))
  8553. (and (looking-at " ") (just-one-space))))
  8554. (when org-trigger-hook
  8555. (save-excursion
  8556. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8557. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8558. "Block turning an entry into a TODO, using the hierarchy.
  8559. This checks whether the current task should be blocked from state
  8560. changes. Such blocking occurs when:
  8561. 1. The task has children which are not all in a completed state.
  8562. 2. A task has a parent with the property :ORDERED:, and there
  8563. are siblings prior to the current task with incomplete
  8564. status.
  8565. 3. The parent of the task is blocked because it has siblings that should
  8566. be done first, or is child of a block grandparent TODO entry."
  8567. (catch 'dont-block
  8568. ;; If this is not a todo state change, or if this entry is already DONE,
  8569. ;; do not block
  8570. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8571. (member (plist-get change-plist :from)
  8572. (cons 'done org-done-keywords))
  8573. (member (plist-get change-plist :to)
  8574. (cons 'todo org-not-done-keywords)))
  8575. (throw 'dont-block t))
  8576. ;; If this task has children, and any are undone, it's blocked
  8577. (save-excursion
  8578. (org-back-to-heading t)
  8579. (let ((this-level (funcall outline-level)))
  8580. (outline-next-heading)
  8581. (let ((child-level (funcall outline-level)))
  8582. (while (and (not (eobp))
  8583. (> child-level this-level))
  8584. ;; this todo has children, check whether they are all
  8585. ;; completed
  8586. (if (and (not (org-entry-is-done-p))
  8587. (org-entry-is-todo-p))
  8588. (throw 'dont-block nil))
  8589. (outline-next-heading)
  8590. (setq child-level (funcall outline-level))))))
  8591. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8592. ;; any previous siblings are undone, it's blocked
  8593. (save-excursion
  8594. (org-back-to-heading t)
  8595. (let* ((pos (point))
  8596. (parent-pos (and (org-up-heading-safe) (point))))
  8597. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8598. (when (and (org-entry-get (point) "ORDERED")
  8599. (forward-line 1)
  8600. (re-search-forward org-not-done-heading-regexp pos t))
  8601. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8602. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8603. (while t
  8604. (goto-char parent-pos)
  8605. (if (not (looking-at org-not-done-heading-regexp))
  8606. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8607. (setq pos (point))
  8608. (setq parent-pos (and (org-up-heading-safe) (point)))
  8609. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8610. (when (and (org-entry-get (point) "ORDERED")
  8611. (forward-line 1)
  8612. (re-search-forward org-not-done-heading-regexp pos t))
  8613. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8614. (defcustom org-track-ordered-property-with-tag nil
  8615. "Should the ORDERED property also be shown as a tag?
  8616. The ORDERED property decides if an entry should require subtasks to be
  8617. completed in sequence. Since a property is not very visible, setting
  8618. this option means that toggling the ORDERED property with the command
  8619. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8620. not relevant for the behavior, but it makes things more visible.
  8621. Note that toggling the tag with tags commands will not change the property
  8622. and therefore not influence behavior!
  8623. This can be t, meaning the tag ORDERED should be used, It can also be a
  8624. string to select a different tag for this task."
  8625. :group 'org-todo
  8626. :type '(choice
  8627. (const :tag "No tracking" nil)
  8628. (const :tag "Track with ORDERED tag" t)
  8629. (string :tag "Use other tag")))
  8630. (defun org-toggle-ordered-property ()
  8631. "Toggle the ORDERED property of the current entry.
  8632. For better visibility, you can track the value of this property with a tag.
  8633. See variable `org-track-ordered-property-with-tag'."
  8634. (interactive)
  8635. (let* ((t1 org-track-ordered-property-with-tag)
  8636. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8637. (save-excursion
  8638. (org-back-to-heading)
  8639. (if (org-entry-get nil "ORDERED")
  8640. (progn
  8641. (org-delete-property "ORDERED")
  8642. (and tag (org-toggle-tag tag 'off))
  8643. (message "Subtasks can be completed in arbitrary order"))
  8644. (org-entry-put nil "ORDERED" "t")
  8645. (and tag (org-toggle-tag tag 'on))
  8646. (message "Subtasks must be completed in sequence")))))
  8647. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8648. (defun org-block-todo-from-checkboxes (change-plist)
  8649. "Block turning an entry into a TODO, using checkboxes.
  8650. This checks whether the current task should be blocked from state
  8651. changes because there are uncheckd boxes in this entry."
  8652. (catch 'dont-block
  8653. ;; If this is not a todo state change, or if this entry is already DONE,
  8654. ;; do not block
  8655. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8656. (member (plist-get change-plist :from)
  8657. (cons 'done org-done-keywords))
  8658. (member (plist-get change-plist :to)
  8659. (cons 'todo org-not-done-keywords)))
  8660. (throw 'dont-block t))
  8661. ;; If this task has checkboxes that are not checked, it's blocked
  8662. (save-excursion
  8663. (org-back-to-heading t)
  8664. (let ((beg (point)) end)
  8665. (outline-next-heading)
  8666. (setq end (point))
  8667. (goto-char beg)
  8668. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8669. end t)
  8670. (progn
  8671. (if (boundp 'org-blocked-by-checkboxes)
  8672. (setq org-blocked-by-checkboxes t))
  8673. (throw 'dont-block nil)))))
  8674. t)) ; do not block
  8675. (defvar org-entry-property-inherited-from) ;; defined below
  8676. (defun org-update-parent-todo-statistics ()
  8677. "Update any statistics cookie in the parent of the current headline.
  8678. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8679. the entire subtree and this will travel up the hierarchy and update
  8680. statistics everywhere."
  8681. (interactive)
  8682. (let* ((lim 0) prop
  8683. (recursive (or (not org-hierarchical-todo-statistics)
  8684. (string-match
  8685. "\\<recursive\\>"
  8686. (or (setq prop (org-entry-get
  8687. nil "COOKIE_DATA" 'inherit)) ""))))
  8688. (lim (or (and prop (marker-position
  8689. org-entry-property-inherited-from))
  8690. lim))
  8691. (first t)
  8692. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8693. level ltoggle l1
  8694. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8695. (catch 'exit
  8696. (save-excursion
  8697. (beginning-of-line 1)
  8698. (if (org-at-heading-p)
  8699. (setq ltoggle (funcall outline-level))
  8700. (error "This should not happen"))
  8701. (while (and (setq level (org-up-heading-safe))
  8702. (or recursive first)
  8703. (>= (point) lim))
  8704. (setq first nil)
  8705. (unless (and level
  8706. (not (string-match
  8707. "\\<checkbox\\>"
  8708. (downcase
  8709. (or (org-entry-get
  8710. nil "COOKIE_DATA")
  8711. "")))))
  8712. (throw 'exit nil))
  8713. (while (re-search-forward box-re (point-at-eol) t)
  8714. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8715. (setq is-percent (match-end 2))
  8716. (save-match-data
  8717. (unless (outline-next-heading) (throw 'exit nil))
  8718. (while (and (looking-at org-complex-heading-regexp)
  8719. (> (setq l1 (length (match-string 1))) level))
  8720. (setq kwd (and (or recursive (= l1 ltoggle))
  8721. (match-string 2)))
  8722. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8723. (and (listp org-provide-todo-statistics)
  8724. (or (member kwd org-provide-todo-statistics)
  8725. (member kwd org-done-keywords))))
  8726. (setq cnt-all (1+ cnt-all))
  8727. (if (eq org-provide-todo-statistics t)
  8728. (and kwd (setq cnt-all (1+ cnt-all)))))
  8729. (and (member kwd org-done-keywords)
  8730. (setq cnt-done (1+ cnt-done)))
  8731. (outline-next-heading)))
  8732. (replace-match
  8733. (if is-percent
  8734. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8735. (format "[%d/%d]" cnt-done cnt-all)))))
  8736. (when cookie-present
  8737. (run-hook-with-args 'org-after-todo-statistics-hook
  8738. cnt-done (- cnt-all cnt-done)))))
  8739. (run-hooks 'org-todo-statistics-hook)))
  8740. (defvar org-after-todo-statistics-hook nil
  8741. "Hook that is called after a TODO statistics cookie has been updated.
  8742. Each function is called with two arguments: the number of not-done entries
  8743. and the number of done entries.
  8744. For example, the following function, when added to this hook, will switch
  8745. an entry to DONE when all children are done, and back to TODO when new
  8746. entries are set to a TODO status. Note that this hook is only called
  8747. when there is a statistics cookie in the headline!
  8748. (defun org-summary-todo (n-done n-not-done)
  8749. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8750. (let (org-log-done org-log-states) ; turn off logging
  8751. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8752. ")
  8753. (defvar org-todo-statistics-hook nil
  8754. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8755. This hook runs even if there is no statisics cookie present, in which case
  8756. `org-after-todo-statistics-hook' would not run.")
  8757. (defun org-todo-trigger-tag-changes (state)
  8758. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8759. (let ((l org-todo-state-tags-triggers)
  8760. changes)
  8761. (when (or (not state) (equal state ""))
  8762. (setq changes (append changes (cdr (assoc "" l)))))
  8763. (when (and (stringp state) (> (length state) 0))
  8764. (setq changes (append changes (cdr (assoc state l)))))
  8765. (when (member state org-not-done-keywords)
  8766. (setq changes (append changes (cdr (assoc 'todo l)))))
  8767. (when (member state org-done-keywords)
  8768. (setq changes (append changes (cdr (assoc 'done l)))))
  8769. (dolist (c changes)
  8770. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8771. (defun org-local-logging (value)
  8772. "Get logging settings from a property VALUE."
  8773. (let* (words w a)
  8774. ;; directly set the variables, they are already local.
  8775. (setq org-log-done nil
  8776. org-log-repeat nil
  8777. org-todo-log-states nil)
  8778. (setq words (org-split-string value))
  8779. (while (setq w (pop words))
  8780. (cond
  8781. ((setq a (assoc w org-startup-options))
  8782. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8783. (set (nth 1 a) (nth 2 a))))
  8784. ((setq a (org-extract-log-state-settings w))
  8785. (and (member (car a) org-todo-keywords-1)
  8786. (push a org-todo-log-states)))))))
  8787. (defun org-get-todo-sequence-head (kwd)
  8788. "Return the head of the TODO sequence to which KWD belongs.
  8789. If KWD is not set, check if there is a text property remembering the
  8790. right sequence."
  8791. (let (p)
  8792. (cond
  8793. ((not kwd)
  8794. (or (get-text-property (point-at-bol) 'org-todo-head)
  8795. (progn
  8796. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8797. nil (point-at-eol)))
  8798. (get-text-property p 'org-todo-head))))
  8799. ((not (member kwd org-todo-keywords-1))
  8800. (car org-todo-keywords-1))
  8801. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8802. (defun org-fast-todo-selection ()
  8803. "Fast TODO keyword selection with single keys.
  8804. Returns the new TODO keyword, or nil if no state change should occur."
  8805. (let* ((fulltable org-todo-key-alist)
  8806. (done-keywords org-done-keywords) ;; needed for the faces.
  8807. (maxlen (apply 'max (mapcar
  8808. (lambda (x)
  8809. (if (stringp (car x)) (string-width (car x)) 0))
  8810. fulltable)))
  8811. (expert nil)
  8812. (fwidth (+ maxlen 3 1 3))
  8813. (ncol (/ (- (window-width) 4) fwidth))
  8814. tg cnt e c tbl
  8815. groups ingroup)
  8816. (save-excursion
  8817. (save-window-excursion
  8818. (if expert
  8819. (set-buffer (get-buffer-create " *Org todo*"))
  8820. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8821. (erase-buffer)
  8822. (org-set-local 'org-done-keywords done-keywords)
  8823. (setq tbl fulltable cnt 0)
  8824. (while (setq e (pop tbl))
  8825. (cond
  8826. ((equal e '(:startgroup))
  8827. (push '() groups) (setq ingroup t)
  8828. (when (not (= cnt 0))
  8829. (setq cnt 0)
  8830. (insert "\n"))
  8831. (insert "{ "))
  8832. ((equal e '(:endgroup))
  8833. (setq ingroup nil cnt 0)
  8834. (insert "}\n"))
  8835. ((equal e '(:newline))
  8836. (when (not (= cnt 0))
  8837. (setq cnt 0)
  8838. (insert "\n")
  8839. (setq e (car tbl))
  8840. (while (equal (car tbl) '(:newline))
  8841. (insert "\n")
  8842. (setq tbl (cdr tbl)))))
  8843. (t
  8844. (setq tg (car e) c (cdr e))
  8845. (if ingroup (push tg (car groups)))
  8846. (setq tg (org-add-props tg nil 'face
  8847. (org-get-todo-face tg)))
  8848. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8849. (insert "[" c "] " tg (make-string
  8850. (- fwidth 4 (length tg)) ?\ ))
  8851. (when (= (setq cnt (1+ cnt)) ncol)
  8852. (insert "\n")
  8853. (if ingroup (insert " "))
  8854. (setq cnt 0)))))
  8855. (insert "\n")
  8856. (goto-char (point-min))
  8857. (if (not expert) (org-fit-window-to-buffer))
  8858. (message "[a-z..]:Set [SPC]:clear")
  8859. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8860. (cond
  8861. ((or (= c ?\C-g)
  8862. (and (= c ?q) (not (rassoc c fulltable))))
  8863. (setq quit-flag t))
  8864. ((= c ?\ ) nil)
  8865. ((setq e (rassoc c fulltable) tg (car e))
  8866. tg)
  8867. (t (setq quit-flag t)))))))
  8868. (defun org-entry-is-todo-p ()
  8869. (member (org-get-todo-state) org-not-done-keywords))
  8870. (defun org-entry-is-done-p ()
  8871. (member (org-get-todo-state) org-done-keywords))
  8872. (defun org-get-todo-state ()
  8873. (save-excursion
  8874. (org-back-to-heading t)
  8875. (and (looking-at org-todo-line-regexp)
  8876. (match-end 2)
  8877. (match-string 2))))
  8878. (defun org-at-date-range-p (&optional inactive-ok)
  8879. "Is the cursor inside a date range?"
  8880. (interactive)
  8881. (save-excursion
  8882. (catch 'exit
  8883. (let ((pos (point)))
  8884. (skip-chars-backward "^[<\r\n")
  8885. (skip-chars-backward "<[")
  8886. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8887. (>= (match-end 0) pos)
  8888. (throw 'exit t))
  8889. (skip-chars-backward "^<[\r\n")
  8890. (skip-chars-backward "<[")
  8891. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8892. (>= (match-end 0) pos)
  8893. (throw 'exit t)))
  8894. nil)))
  8895. (defun org-get-repeat ()
  8896. "Check if there is a deadline/schedule with repeater in this entry."
  8897. (save-match-data
  8898. (save-excursion
  8899. (org-back-to-heading t)
  8900. (if (re-search-forward
  8901. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8902. (match-string 1)))))
  8903. (defvar org-last-changed-timestamp)
  8904. (defvar org-last-inserted-timestamp)
  8905. (defvar org-log-post-message)
  8906. (defvar org-log-note-purpose)
  8907. (defvar org-log-note-how)
  8908. (defvar org-log-note-extra)
  8909. (defun org-auto-repeat-maybe (done-word)
  8910. "Check if the current headline contains a repeated deadline/schedule.
  8911. If yes, set TODO state back to what it was and change the base date
  8912. of repeating deadline/scheduled time stamps to new date.
  8913. This function is run automatically after each state change to a DONE state."
  8914. ;; last-state is dynamically scoped into this function
  8915. (let* ((repeat (org-get-repeat))
  8916. (aa (assoc last-state org-todo-kwd-alist))
  8917. (interpret (nth 1 aa))
  8918. (head (nth 2 aa))
  8919. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8920. (msg "Entry repeats: ")
  8921. (org-log-done nil)
  8922. (org-todo-log-states nil)
  8923. (nshiftmax 10) (nshift 0)
  8924. re type n what ts time)
  8925. (when repeat
  8926. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8927. (org-todo (if (eq interpret 'type) last-state head))
  8928. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8929. (org-time-stamp-format t t)))
  8930. (when org-log-repeat
  8931. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8932. (memq 'org-add-log-note post-command-hook))
  8933. ;; OK, we are already setup for some record
  8934. (if (eq org-log-repeat 'note)
  8935. ;; make sure we take a note, not only a time stamp
  8936. (setq org-log-note-how 'note))
  8937. ;; Set up for taking a record
  8938. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8939. last-state
  8940. 'findpos org-log-repeat)))
  8941. (org-back-to-heading t)
  8942. (org-add-planning-info nil nil 'closed)
  8943. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8944. org-deadline-time-regexp "\\)\\|\\("
  8945. org-ts-regexp "\\)"))
  8946. (while (re-search-forward
  8947. re (save-excursion (outline-next-heading) (point)) t)
  8948. (setq type (if (match-end 1) org-scheduled-string
  8949. (if (match-end 3) org-deadline-string "Plain:"))
  8950. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8951. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8952. (setq n (string-to-number (match-string 2 ts))
  8953. what (match-string 3 ts))
  8954. (if (equal what "w") (setq n (* n 7) what "d"))
  8955. ;; Preparation, see if we need to modify the start date for the change
  8956. (when (match-end 1)
  8957. (setq time (save-match-data (org-time-string-to-time ts)))
  8958. (cond
  8959. ((equal (match-string 1 ts) ".")
  8960. ;; Shift starting date to today
  8961. (org-timestamp-change
  8962. (- (time-to-days (current-time)) (time-to-days time))
  8963. 'day))
  8964. ((equal (match-string 1 ts) "+")
  8965. (while (or (= nshift 0)
  8966. (<= (time-to-days time) (time-to-days (current-time))))
  8967. (when (= (incf nshift) nshiftmax)
  8968. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8969. (error "Abort")))
  8970. (org-timestamp-change n (cdr (assoc what whata)))
  8971. (org-at-timestamp-p t)
  8972. (setq ts (match-string 1))
  8973. (setq time (save-match-data (org-time-string-to-time ts))))
  8974. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8975. ;; rematch, so that we have everything in place for the real shift
  8976. (org-at-timestamp-p t)
  8977. (setq ts (match-string 1))
  8978. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8979. (org-timestamp-change n (cdr (assoc what whata)))
  8980. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8981. (setq org-log-post-message msg)
  8982. (message "%s" msg))))
  8983. (defun org-show-todo-tree (arg)
  8984. "Make a compact tree which shows all headlines marked with TODO.
  8985. The tree will show the lines where the regexp matches, and all higher
  8986. headlines above the match.
  8987. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8988. With a numeric prefix N, construct a sparse tree for the Nth element
  8989. of `org-todo-keywords-1'."
  8990. (interactive "P")
  8991. (let ((case-fold-search nil)
  8992. (kwd-re
  8993. (cond ((null arg) org-not-done-regexp)
  8994. ((equal arg '(4))
  8995. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  8996. (mapcar 'list org-todo-keywords-1))))
  8997. (concat "\\("
  8998. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8999. "\\)\\>")))
  9000. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9001. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9002. org-todo-keywords-1)))
  9003. (t (error "Invalid prefix argument: %s" arg)))))
  9004. (message "%d TODO entries found"
  9005. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9006. (defun org-deadline (&optional remove time)
  9007. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9008. With argument REMOVE, remove any deadline from the item.
  9009. When TIME is set, it should be an internal time specification, and the
  9010. scheduling will use the corresponding date."
  9011. (interactive "P")
  9012. (if remove
  9013. (progn
  9014. (org-remove-timestamp-with-keyword org-deadline-string)
  9015. (message "Item no longer has a deadline."))
  9016. (if (org-get-repeat)
  9017. (error "Cannot change deadline on task with repeater, please do that by hand")
  9018. (org-add-planning-info 'deadline time 'closed)
  9019. (message "Deadline on %s" org-last-inserted-timestamp))))
  9020. (defun org-schedule (&optional remove time)
  9021. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9022. With argument REMOVE, remove any scheduling date from the item.
  9023. When TIME is set, it should be an internal time specification, and the
  9024. scheduling will use the corresponding date."
  9025. (interactive "P")
  9026. (if remove
  9027. (progn
  9028. (org-remove-timestamp-with-keyword org-scheduled-string)
  9029. (message "Item is no longer scheduled."))
  9030. (if (org-get-repeat)
  9031. (error "Cannot reschedule task with repeater, please do that by hand")
  9032. (org-add-planning-info 'scheduled time 'closed)
  9033. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9034. (defun org-get-scheduled-time (pom &optional inherit)
  9035. "Get the scheduled time as a time tuple, of a format suitable
  9036. for calling org-schedule with, or if there is no scheduling,
  9037. returns nil."
  9038. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9039. (when time
  9040. (apply 'encode-time (org-parse-time-string time)))))
  9041. (defun org-get-deadline-time (pom &optional inherit)
  9042. "Get the deadine as a time tuple, of a format suitable for
  9043. calling org-deadlin with, or if there is no scheduling, returns
  9044. nil."
  9045. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9046. (when time
  9047. (apply 'encode-time (org-parse-time-string time)))))
  9048. (defun org-remove-timestamp-with-keyword (keyword)
  9049. "Remove all time stamps with KEYWORD in the current entry."
  9050. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9051. beg)
  9052. (save-excursion
  9053. (org-back-to-heading t)
  9054. (setq beg (point))
  9055. (org-end-of-subtree t t)
  9056. (while (re-search-backward re beg t)
  9057. (replace-match "")
  9058. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9059. (equal (char-before) ?\ ))
  9060. (backward-delete-char 1)
  9061. (if (string-match "^[ \t]*$" (buffer-substring
  9062. (point-at-bol) (point-at-eol)))
  9063. (delete-region (point-at-bol)
  9064. (min (point-max) (1+ (point-at-eol))))))))))
  9065. (defun org-add-planning-info (what &optional time &rest remove)
  9066. "Insert new timestamp with keyword in the line directly after the headline.
  9067. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9068. If non is given, the user is prompted for a date.
  9069. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9070. be removed."
  9071. (interactive)
  9072. (let (org-time-was-given org-end-time-was-given ts
  9073. end default-time default-input)
  9074. (catch 'exit
  9075. (when (and (not time) (memq what '(scheduled deadline)))
  9076. ;; Try to get a default date/time from existing timestamp
  9077. (save-excursion
  9078. (org-back-to-heading t)
  9079. (setq end (save-excursion (outline-next-heading) (point)))
  9080. (when (re-search-forward (if (eq what 'scheduled)
  9081. org-scheduled-time-regexp
  9082. org-deadline-time-regexp)
  9083. end t)
  9084. (setq ts (match-string 1)
  9085. default-time
  9086. (apply 'encode-time (org-parse-time-string ts))
  9087. default-input (and ts (org-get-compact-tod ts))))))
  9088. (when what
  9089. ;; If necessary, get the time from the user
  9090. (setq time (or time (org-read-date nil 'to-time nil nil
  9091. default-time default-input))))
  9092. (when (and org-insert-labeled-timestamps-at-point
  9093. (member what '(scheduled deadline)))
  9094. (insert
  9095. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9096. (org-insert-time-stamp time org-time-was-given
  9097. nil nil nil (list org-end-time-was-given))
  9098. (setq what nil))
  9099. (save-excursion
  9100. (save-restriction
  9101. (let (col list elt ts buffer-invisibility-spec)
  9102. (org-back-to-heading t)
  9103. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9104. (goto-char (match-end 1))
  9105. (setq col (current-column))
  9106. (goto-char (match-end 0))
  9107. (if (eobp) (insert "\n") (forward-char 1))
  9108. (when (and (not what)
  9109. (not (looking-at
  9110. (concat "[ \t]*"
  9111. org-keyword-time-not-clock-regexp))))
  9112. ;; Nothing to add, nothing to remove...... :-)
  9113. (throw 'exit nil))
  9114. (if (and (not (looking-at outline-regexp))
  9115. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9116. "[^\r\n]*"))
  9117. (not (equal (match-string 1) org-clock-string)))
  9118. (narrow-to-region (match-beginning 0) (match-end 0))
  9119. (insert-before-markers "\n")
  9120. (backward-char 1)
  9121. (narrow-to-region (point) (point))
  9122. (and org-adapt-indentation (org-indent-to-column col)))
  9123. ;; Check if we have to remove something.
  9124. (setq list (cons what remove))
  9125. (while list
  9126. (setq elt (pop list))
  9127. (goto-char (point-min))
  9128. (when (or (and (eq elt 'scheduled)
  9129. (re-search-forward org-scheduled-time-regexp nil t))
  9130. (and (eq elt 'deadline)
  9131. (re-search-forward org-deadline-time-regexp nil t))
  9132. (and (eq elt 'closed)
  9133. (re-search-forward org-closed-time-regexp nil t)))
  9134. (replace-match "")
  9135. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9136. (if (looking-at " +") (replace-match ""))))
  9137. (goto-char (point-max))
  9138. (when what
  9139. (insert
  9140. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9141. (cond ((eq what 'scheduled) org-scheduled-string)
  9142. ((eq what 'deadline) org-deadline-string)
  9143. ((eq what 'closed) org-closed-string))
  9144. " ")
  9145. (setq ts (org-insert-time-stamp
  9146. time
  9147. (or org-time-was-given
  9148. (and (eq what 'closed) org-log-done-with-time))
  9149. (eq what 'closed)
  9150. nil nil (list org-end-time-was-given)))
  9151. (end-of-line 1))
  9152. (goto-char (point-min))
  9153. (widen)
  9154. (if (and (looking-at "[ \t]+\n")
  9155. (equal (char-before) ?\n))
  9156. (delete-region (1- (point)) (point-at-eol)))
  9157. ts))))))
  9158. (defvar org-log-note-marker (make-marker))
  9159. (defvar org-log-note-purpose nil)
  9160. (defvar org-log-note-state nil)
  9161. (defvar org-log-note-previous-state nil)
  9162. (defvar org-log-note-how nil)
  9163. (defvar org-log-note-extra nil)
  9164. (defvar org-log-note-window-configuration nil)
  9165. (defvar org-log-note-return-to (make-marker))
  9166. (defvar org-log-post-message nil
  9167. "Message to be displayed after a log note has been stored.
  9168. The auto-repeater uses this.")
  9169. (defun org-add-note ()
  9170. "Add a note to the current entry.
  9171. This is done in the same way as adding a state change note."
  9172. (interactive)
  9173. (org-add-log-setup 'note nil nil 'findpos nil))
  9174. (defvar org-property-end-re)
  9175. (defun org-add-log-setup (&optional purpose state prev-state
  9176. findpos how &optional extra)
  9177. "Set up the post command hook to take a note.
  9178. If this is about to TODO state change, the new state is expected in STATE.
  9179. When FINDPOS is non-nil, find the correct position for the note in
  9180. the current entry. If not, assume that it can be inserted at point.
  9181. HOW is an indicator what kind of note should be created.
  9182. EXTRA is additional text that will be inserted into the notes buffer."
  9183. (let* ((org-log-into-drawer (org-log-into-drawer))
  9184. (drawer (cond ((stringp org-log-into-drawer)
  9185. org-log-into-drawer)
  9186. (org-log-into-drawer "LOGBOOK")
  9187. (t nil))))
  9188. (save-restriction
  9189. (save-excursion
  9190. (when findpos
  9191. (org-back-to-heading t)
  9192. (narrow-to-region (point) (save-excursion
  9193. (outline-next-heading) (point)))
  9194. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9195. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9196. "[^\r\n]*\\)?"))
  9197. (goto-char (match-end 0))
  9198. (cond
  9199. (drawer
  9200. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9201. nil t)
  9202. (progn
  9203. (goto-char (match-end 0))
  9204. (or org-log-states-order-reversed
  9205. (and (re-search-forward org-property-end-re nil t)
  9206. (goto-char (1- (match-beginning 0))))))
  9207. (insert "\n:" drawer ":\n:END:")
  9208. (beginning-of-line 0)
  9209. (org-indent-line-function)
  9210. (beginning-of-line 2)
  9211. (org-indent-line-function)
  9212. (end-of-line 0)))
  9213. ((and org-log-state-notes-insert-after-drawers
  9214. (save-excursion
  9215. (forward-line) (looking-at org-drawer-regexp)))
  9216. (forward-line)
  9217. (while (looking-at org-drawer-regexp)
  9218. (goto-char (match-end 0))
  9219. (re-search-forward org-property-end-re (point-max) t)
  9220. (forward-line))
  9221. (forward-line -1)))
  9222. (unless org-log-states-order-reversed
  9223. (and (= (char-after) ?\n) (forward-char 1))
  9224. (org-skip-over-state-notes)
  9225. (skip-chars-backward " \t\n\r")))
  9226. (move-marker org-log-note-marker (point))
  9227. (setq org-log-note-purpose purpose
  9228. org-log-note-state state
  9229. org-log-note-previous-state prev-state
  9230. org-log-note-how how
  9231. org-log-note-extra extra)
  9232. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9233. (defun org-skip-over-state-notes ()
  9234. "Skip past the list of State notes in an entry."
  9235. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9236. (while (looking-at "[ \t]*- State")
  9237. (condition-case nil
  9238. (org-next-item)
  9239. (error (org-end-of-item)))))
  9240. (defun org-add-log-note (&optional purpose)
  9241. "Pop up a window for taking a note, and add this note later at point."
  9242. (remove-hook 'post-command-hook 'org-add-log-note)
  9243. (setq org-log-note-window-configuration (current-window-configuration))
  9244. (delete-other-windows)
  9245. (move-marker org-log-note-return-to (point))
  9246. (switch-to-buffer (marker-buffer org-log-note-marker))
  9247. (goto-char org-log-note-marker)
  9248. (org-switch-to-buffer-other-window "*Org Note*")
  9249. (erase-buffer)
  9250. (if (memq org-log-note-how '(time state))
  9251. (let (current-prefix-arg) (org-store-log-note))
  9252. (let ((org-inhibit-startup t)) (org-mode))
  9253. (insert (format "# Insert note for %s.
  9254. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9255. (cond
  9256. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9257. ((eq org-log-note-purpose 'done) "closed todo item")
  9258. ((eq org-log-note-purpose 'state)
  9259. (format "state change from \"%s\" to \"%s\""
  9260. (or org-log-note-previous-state "")
  9261. (or org-log-note-state "")))
  9262. ((eq org-log-note-purpose 'note)
  9263. "this entry")
  9264. (t (error "This should not happen")))))
  9265. (if org-log-note-extra (insert org-log-note-extra))
  9266. (org-set-local 'org-finish-function 'org-store-log-note)))
  9267. (defvar org-note-abort nil) ; dynamically scoped
  9268. (defun org-store-log-note ()
  9269. "Finish taking a log note, and insert it to where it belongs."
  9270. (let ((txt (buffer-string))
  9271. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9272. lines ind)
  9273. (kill-buffer (current-buffer))
  9274. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9275. (setq txt (replace-match "" t t txt)))
  9276. (if (string-match "\\s-+\\'" txt)
  9277. (setq txt (replace-match "" t t txt)))
  9278. (setq lines (org-split-string txt "\n"))
  9279. (when (and note (string-match "\\S-" note))
  9280. (setq note
  9281. (org-replace-escapes
  9282. note
  9283. (list (cons "%u" (user-login-name))
  9284. (cons "%U" user-full-name)
  9285. (cons "%t" (format-time-string
  9286. (org-time-stamp-format 'long 'inactive)
  9287. (current-time)))
  9288. (cons "%s" (if org-log-note-state
  9289. (concat "\"" org-log-note-state "\"")
  9290. ""))
  9291. (cons "%S" (if org-log-note-previous-state
  9292. (concat "\"" org-log-note-previous-state "\"")
  9293. "\"\"")))))
  9294. (if lines (setq note (concat note " \\\\")))
  9295. (push note lines))
  9296. (when (or current-prefix-arg org-note-abort)
  9297. (when org-log-into-drawer
  9298. (org-remove-empty-drawer-at
  9299. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9300. org-log-note-marker))
  9301. (setq lines nil))
  9302. (when lines
  9303. (save-excursion
  9304. (set-buffer (marker-buffer org-log-note-marker))
  9305. (save-excursion
  9306. (goto-char org-log-note-marker)
  9307. (move-marker org-log-note-marker nil)
  9308. (end-of-line 1)
  9309. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9310. (insert "- " (pop lines))
  9311. (org-indent-line-function)
  9312. (beginning-of-line 1)
  9313. (looking-at "[ \t]*")
  9314. (setq ind (concat (match-string 0) " "))
  9315. (end-of-line 1)
  9316. (while lines (insert "\n" ind (pop lines)))
  9317. (message "Note stored")
  9318. (org-back-to-heading t)
  9319. (org-cycle-hide-drawers 'children)))))
  9320. (set-window-configuration org-log-note-window-configuration)
  9321. (with-current-buffer (marker-buffer org-log-note-return-to)
  9322. (goto-char org-log-note-return-to))
  9323. (move-marker org-log-note-return-to nil)
  9324. (and org-log-post-message (message "%s" org-log-post-message)))
  9325. (defun org-remove-empty-drawer-at (drawer pos)
  9326. "Remove an emptyr DARWER drawer at position POS.
  9327. POS may also be a marker."
  9328. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9329. (save-excursion
  9330. (save-restriction
  9331. (widen)
  9332. (goto-char pos)
  9333. (if (org-in-regexp
  9334. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9335. (replace-match ""))))))
  9336. (defun org-sparse-tree (&optional arg)
  9337. "Create a sparse tree, prompt for the details.
  9338. This command can create sparse trees. You first need to select the type
  9339. of match used to create the tree:
  9340. t Show entries with a specific TODO keyword.
  9341. m Show entries selected by a tags/property match.
  9342. p Enter a property name and its value (both with completion on existing
  9343. names/values) and show entries with that property.
  9344. r Show entries matching a regular expression.
  9345. d Show deadlines due within `org-deadline-warning-days'.
  9346. b Show deadlines and scheduled items before a date.
  9347. a Show deadlines and scheduled items after a date."
  9348. (interactive "P")
  9349. (let (ans kwd value)
  9350. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9351. (setq ans (read-char-exclusive))
  9352. (cond
  9353. ((equal ans ?d)
  9354. (call-interactively 'org-check-deadlines))
  9355. ((equal ans ?b)
  9356. (call-interactively 'org-check-before-date))
  9357. ((equal ans ?a)
  9358. (call-interactively 'org-check-after-date))
  9359. ((equal ans ?t)
  9360. (org-show-todo-tree '(4)))
  9361. ((member ans '(?T ?m))
  9362. (call-interactively 'org-match-sparse-tree))
  9363. ((member ans '(?p ?P))
  9364. (setq kwd (org-icompleting-read "Property: "
  9365. (mapcar 'list (org-buffer-property-keys))))
  9366. (setq value (org-icompleting-read "Value: "
  9367. (mapcar 'list (org-property-values kwd))))
  9368. (unless (string-match "\\`{.*}\\'" value)
  9369. (setq value (concat "\"" value "\"")))
  9370. (org-match-sparse-tree arg (concat kwd "=" value)))
  9371. ((member ans '(?r ?R ?/))
  9372. (call-interactively 'org-occur))
  9373. (t (error "No such sparse tree command \"%c\"" ans)))))
  9374. (defvar org-occur-highlights nil
  9375. "List of overlays used for occur matches.")
  9376. (make-variable-buffer-local 'org-occur-highlights)
  9377. (defvar org-occur-parameters nil
  9378. "Parameters of the active org-occur calls.
  9379. This is a list, each call to org-occur pushes as cons cell,
  9380. containing the regular expression and the callback, onto the list.
  9381. The list can contain several entries if `org-occur' has been called
  9382. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9383. will only contain one set of parameters. When the highlights are
  9384. removed (for example with `C-c C-c', or with the next edit (depending
  9385. on `org-remove-highlights-with-change'), this variable is emptied
  9386. as well.")
  9387. (make-variable-buffer-local 'org-occur-parameters)
  9388. (defun org-occur (regexp &optional keep-previous callback)
  9389. "Make a compact tree which shows all matches of REGEXP.
  9390. The tree will show the lines where the regexp matches, and all higher
  9391. headlines above the match. It will also show the heading after the match,
  9392. to make sure editing the matching entry is easy.
  9393. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9394. call to `org-occur' will be kept, to allow stacking of calls to this
  9395. command.
  9396. If CALLBACK is non-nil, it is a function which is called to confirm
  9397. that the match should indeed be shown."
  9398. (interactive "sRegexp: \nP")
  9399. (when (equal regexp "")
  9400. (error "Regexp cannot be empty"))
  9401. (unless keep-previous
  9402. (org-remove-occur-highlights nil nil t))
  9403. (push (cons regexp callback) org-occur-parameters)
  9404. (let ((cnt 0))
  9405. (save-excursion
  9406. (goto-char (point-min))
  9407. (if (or (not keep-previous) ; do not want to keep
  9408. (not org-occur-highlights)) ; no previous matches
  9409. ;; hide everything
  9410. (org-overview))
  9411. (while (re-search-forward regexp nil t)
  9412. (when (or (not callback)
  9413. (save-match-data (funcall callback)))
  9414. (setq cnt (1+ cnt))
  9415. (when org-highlight-sparse-tree-matches
  9416. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9417. (org-show-context 'occur-tree))))
  9418. (when org-remove-highlights-with-change
  9419. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9420. nil 'local))
  9421. (unless org-sparse-tree-open-archived-trees
  9422. (org-hide-archived-subtrees (point-min) (point-max)))
  9423. (run-hooks 'org-occur-hook)
  9424. (if (interactive-p)
  9425. (message "%d match(es) for regexp %s" cnt regexp))
  9426. cnt))
  9427. (defun org-show-context (&optional key)
  9428. "Make sure point and context and visible.
  9429. How much context is shown depends upon the variables
  9430. `org-show-hierarchy-above', `org-show-following-heading'. and
  9431. `org-show-siblings'."
  9432. (let ((heading-p (org-on-heading-p t))
  9433. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9434. (following-p (org-get-alist-option org-show-following-heading key))
  9435. (entry-p (org-get-alist-option org-show-entry-below key))
  9436. (siblings-p (org-get-alist-option org-show-siblings key)))
  9437. (catch 'exit
  9438. ;; Show heading or entry text
  9439. (if (and heading-p (not entry-p))
  9440. (org-flag-heading nil) ; only show the heading
  9441. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9442. (org-show-hidden-entry))) ; show entire entry
  9443. (when following-p
  9444. ;; Show next sibling, or heading below text
  9445. (save-excursion
  9446. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9447. (org-flag-heading nil))))
  9448. (when siblings-p (org-show-siblings))
  9449. (when hierarchy-p
  9450. ;; show all higher headings, possibly with siblings
  9451. (save-excursion
  9452. (while (and (condition-case nil
  9453. (progn (org-up-heading-all 1) t)
  9454. (error nil))
  9455. (not (bobp)))
  9456. (org-flag-heading nil)
  9457. (when siblings-p (org-show-siblings))))))))
  9458. (defun org-reveal (&optional siblings)
  9459. "Show current entry, hierarchy above it, and the following headline.
  9460. This can be used to show a consistent set of context around locations
  9461. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9462. not t for the search context.
  9463. With optional argument SIBLINGS, on each level of the hierarchy all
  9464. siblings are shown. This repairs the tree structure to what it would
  9465. look like when opened with hierarchical calls to `org-cycle'."
  9466. (interactive "P")
  9467. (let ((org-show-hierarchy-above t)
  9468. (org-show-following-heading t)
  9469. (org-show-siblings (if siblings t org-show-siblings)))
  9470. (org-show-context nil)))
  9471. (defun org-highlight-new-match (beg end)
  9472. "Highlight from BEG to END and mark the highlight is an occur headline."
  9473. (let ((ov (org-make-overlay beg end)))
  9474. (org-overlay-put ov 'face 'secondary-selection)
  9475. (push ov org-occur-highlights)))
  9476. (defun org-remove-occur-highlights (&optional beg end noremove)
  9477. "Remove the occur highlights from the buffer.
  9478. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9479. from the `before-change-functions' in the current buffer."
  9480. (interactive)
  9481. (unless org-inhibit-highlight-removal
  9482. (mapc 'org-delete-overlay org-occur-highlights)
  9483. (setq org-occur-highlights nil)
  9484. (setq org-occur-parameters nil)
  9485. (unless noremove
  9486. (remove-hook 'before-change-functions
  9487. 'org-remove-occur-highlights 'local))))
  9488. ;;;; Priorities
  9489. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9490. "Regular expression matching the priority indicator.")
  9491. (defvar org-remove-priority-next-time nil)
  9492. (defun org-priority-up ()
  9493. "Increase the priority of the current item."
  9494. (interactive)
  9495. (org-priority 'up))
  9496. (defun org-priority-down ()
  9497. "Decrease the priority of the current item."
  9498. (interactive)
  9499. (org-priority 'down))
  9500. (defun org-priority (&optional action)
  9501. "Change the priority of an item by ARG.
  9502. ACTION can be `set', `up', `down', or a character."
  9503. (interactive)
  9504. (unless org-enable-priority-commands
  9505. (error "Priority commands are disabled"))
  9506. (setq action (or action 'set))
  9507. (let (current new news have remove)
  9508. (save-excursion
  9509. (org-back-to-heading t)
  9510. (if (looking-at org-priority-regexp)
  9511. (setq current (string-to-char (match-string 2))
  9512. have t)
  9513. (setq current org-default-priority))
  9514. (cond
  9515. ((or (eq action 'set)
  9516. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9517. (if (not (eq action 'set))
  9518. (setq new action)
  9519. (message "Priority %c-%c, SPC to remove: "
  9520. org-highest-priority org-lowest-priority)
  9521. (setq new (read-char-exclusive)))
  9522. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9523. (= (upcase org-lowest-priority) org-lowest-priority))
  9524. (setq new (upcase new)))
  9525. (cond ((equal new ?\ ) (setq remove t))
  9526. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9527. (error "Priority must be between `%c' and `%c'"
  9528. org-highest-priority org-lowest-priority))))
  9529. ((eq action 'up)
  9530. (if (and (not have) (eq last-command this-command))
  9531. (setq new org-lowest-priority)
  9532. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9533. org-default-priority (1- current)))))
  9534. ((eq action 'down)
  9535. (if (and (not have) (eq last-command this-command))
  9536. (setq new org-highest-priority)
  9537. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9538. org-default-priority (1+ current)))))
  9539. (t (error "Invalid action")))
  9540. (if (or (< (upcase new) org-highest-priority)
  9541. (> (upcase new) org-lowest-priority))
  9542. (setq remove t))
  9543. (setq news (format "%c" new))
  9544. (if have
  9545. (if remove
  9546. (replace-match "" t t nil 1)
  9547. (replace-match news t t nil 2))
  9548. (if remove
  9549. (error "No priority cookie found in line")
  9550. (let ((case-fold-search nil))
  9551. (looking-at org-todo-line-regexp))
  9552. (if (match-end 2)
  9553. (progn
  9554. (goto-char (match-end 2))
  9555. (insert " [#" news "]"))
  9556. (goto-char (match-beginning 3))
  9557. (insert "[#" news "] "))))
  9558. (org-preserve-lc (org-set-tags nil 'align)))
  9559. (if remove
  9560. (message "Priority removed")
  9561. (message "Priority of current item set to %s" news))))
  9562. (defun org-get-priority (s)
  9563. "Find priority cookie and return priority."
  9564. (save-match-data
  9565. (if (not (string-match org-priority-regexp s))
  9566. (* 1000 (- org-lowest-priority org-default-priority))
  9567. (* 1000 (- org-lowest-priority
  9568. (string-to-char (match-string 2 s)))))))
  9569. ;;;; Tags
  9570. (defvar org-agenda-archives-mode)
  9571. (defvar org-map-continue-from nil
  9572. "Position from where mapping should continue.
  9573. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9574. (defvar org-scanner-tags nil
  9575. "The current tag list while the tags scanner is running.")
  9576. (defvar org-trust-scanner-tags nil
  9577. "Should `org-get-tags-at' use the tags fro the scanner.
  9578. This is for internal dynamical scoping only.
  9579. When this is non-nil, the function `org-get-tags-at' will return the value
  9580. of `org-scanner-tags' instead of building the list by itself. This
  9581. can lead to large speed-ups when the tags scanner is used in a file with
  9582. many entries, and when the list of tags is retrieved, for example to
  9583. obtain a list of properties. Building the tags list for each entry in such
  9584. a file becomes an N^2 operation - but with this variable set, it scales
  9585. as N.")
  9586. (defun org-scan-tags (action matcher &optional todo-only)
  9587. "Scan headline tags with inheritance and produce output ACTION.
  9588. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9589. or `agenda' to produce an entry list for an agenda view. It can also be
  9590. a Lisp form or a function that should be called at each matched headline, in
  9591. this case the return value is a list of all return values from these calls.
  9592. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9593. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9594. only lines with a TODO keyword are included in the output."
  9595. (require 'org-agenda)
  9596. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9597. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9598. (org-re
  9599. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9600. (props (list 'face 'default
  9601. 'done-face 'org-agenda-done
  9602. 'undone-face 'default
  9603. 'mouse-face 'highlight
  9604. 'org-not-done-regexp org-not-done-regexp
  9605. 'org-todo-regexp org-todo-regexp
  9606. 'keymap org-agenda-keymap
  9607. 'help-echo
  9608. (format "mouse-2 or RET jump to org file %s"
  9609. (abbreviate-file-name
  9610. (or (buffer-file-name (buffer-base-buffer))
  9611. (buffer-name (buffer-base-buffer)))))))
  9612. (case-fold-search nil)
  9613. (org-map-continue-from nil)
  9614. lspos tags tags-list
  9615. (tags-alist (list (cons 0 org-file-tags)))
  9616. (llast 0) rtn rtn1 level category i txt
  9617. todo marker entry priority)
  9618. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9619. (setq action (list 'lambda nil action)))
  9620. (save-excursion
  9621. (goto-char (point-min))
  9622. (when (eq action 'sparse-tree)
  9623. (org-overview)
  9624. (org-remove-occur-highlights))
  9625. (while (re-search-forward re nil t)
  9626. (catch :skip
  9627. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9628. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9629. (goto-char (setq lspos (match-beginning 0)))
  9630. (setq level (org-reduced-level (funcall outline-level))
  9631. category (org-get-category))
  9632. (setq i llast llast level)
  9633. ;; remove tag lists from same and sublevels
  9634. (while (>= i level)
  9635. (when (setq entry (assoc i tags-alist))
  9636. (setq tags-alist (delete entry tags-alist)))
  9637. (setq i (1- i)))
  9638. ;; add the next tags
  9639. (when tags
  9640. (setq tags (org-split-string tags ":")
  9641. tags-alist
  9642. (cons (cons level tags) tags-alist)))
  9643. ;; compile tags for current headline
  9644. (setq tags-list
  9645. (if org-use-tag-inheritance
  9646. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9647. tags)
  9648. org-scanner-tags tags-list)
  9649. (when org-use-tag-inheritance
  9650. (setcdr (car tags-alist)
  9651. (mapcar (lambda (x)
  9652. (setq x (copy-sequence x))
  9653. (org-add-prop-inherited x))
  9654. (cdar tags-alist))))
  9655. (when (and tags org-use-tag-inheritance
  9656. (or (not (eq t org-use-tag-inheritance))
  9657. org-tags-exclude-from-inheritance))
  9658. ;; selective inheritance, remove uninherited ones
  9659. (setcdr (car tags-alist)
  9660. (org-remove-uniherited-tags (cdar tags-alist))))
  9661. (when (and (or (not todo-only)
  9662. (and (member todo org-not-done-keywords)
  9663. (or (not org-agenda-tags-todo-honor-ignore-options)
  9664. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9665. (let ((case-fold-search t)) (eval matcher))
  9666. (or
  9667. (not (member org-archive-tag tags-list))
  9668. ;; we have an archive tag, should we use this anyway?
  9669. (or (not org-agenda-skip-archived-trees)
  9670. (and (eq action 'agenda) org-agenda-archives-mode))))
  9671. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9672. ;; select this headline
  9673. (cond
  9674. ((eq action 'sparse-tree)
  9675. (and org-highlight-sparse-tree-matches
  9676. (org-get-heading) (match-end 0)
  9677. (org-highlight-new-match
  9678. (match-beginning 0) (match-beginning 1)))
  9679. (org-show-context 'tags-tree))
  9680. ((eq action 'agenda)
  9681. (setq txt (org-format-agenda-item
  9682. ""
  9683. (concat
  9684. (if (eq org-tags-match-list-sublevels 'indented)
  9685. (make-string (1- level) ?.) "")
  9686. (org-get-heading))
  9687. category
  9688. tags-list
  9689. )
  9690. priority (org-get-priority txt))
  9691. (goto-char lspos)
  9692. (setq marker (org-agenda-new-marker))
  9693. (org-add-props txt props
  9694. 'org-marker marker 'org-hd-marker marker 'org-category category
  9695. 'todo-state todo
  9696. 'priority priority 'type "tagsmatch")
  9697. (push txt rtn))
  9698. ((functionp action)
  9699. (setq org-map-continue-from nil)
  9700. (save-excursion
  9701. (setq rtn1 (funcall action))
  9702. (push rtn1 rtn)))
  9703. (t (error "Invalid action")))
  9704. ;; if we are to skip sublevels, jump to end of subtree
  9705. (unless org-tags-match-list-sublevels
  9706. (org-end-of-subtree t)
  9707. (backward-char 1))))
  9708. ;; Get the correct position from where to continue
  9709. (if org-map-continue-from
  9710. (goto-char org-map-continue-from)
  9711. (and (= (point) lspos) (end-of-line 1)))))
  9712. (when (and (eq action 'sparse-tree)
  9713. (not org-sparse-tree-open-archived-trees))
  9714. (org-hide-archived-subtrees (point-min) (point-max)))
  9715. (nreverse rtn)))
  9716. (defun org-remove-uniherited-tags (tags)
  9717. "Remove all tags that are not inherited from the list TAGS."
  9718. (cond
  9719. ((eq org-use-tag-inheritance t)
  9720. (if org-tags-exclude-from-inheritance
  9721. (org-delete-all org-tags-exclude-from-inheritance tags)
  9722. tags))
  9723. ((not org-use-tag-inheritance) nil)
  9724. ((stringp org-use-tag-inheritance)
  9725. (delq nil (mapcar
  9726. (lambda (x)
  9727. (if (and (string-match org-use-tag-inheritance x)
  9728. (not (member x org-tags-exclude-from-inheritance)))
  9729. x nil))
  9730. tags)))
  9731. ((listp org-use-tag-inheritance)
  9732. (delq nil (mapcar
  9733. (lambda (x)
  9734. (if (member x org-use-tag-inheritance) x nil))
  9735. tags)))))
  9736. (defvar todo-only) ;; dynamically scoped
  9737. (defun org-match-sparse-tree (&optional todo-only match)
  9738. "Create a sparse tree according to tags string MATCH.
  9739. MATCH can contain positive and negative selection of tags, like
  9740. \"+WORK+URGENT-WITHBOSS\".
  9741. If optional argument TODO-ONLY is non-nil, only select lines that are
  9742. also TODO lines."
  9743. (interactive "P")
  9744. (org-prepare-agenda-buffers (list (current-buffer)))
  9745. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9746. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9747. (defvar org-cached-props nil)
  9748. (defun org-cached-entry-get (pom property)
  9749. (if (or (eq t org-use-property-inheritance)
  9750. (and (stringp org-use-property-inheritance)
  9751. (string-match org-use-property-inheritance property))
  9752. (and (listp org-use-property-inheritance)
  9753. (member property org-use-property-inheritance)))
  9754. ;; Caching is not possible, check it directly
  9755. (org-entry-get pom property 'inherit)
  9756. ;; Get all properties, so that we can do complicated checks easily
  9757. (cdr (assoc property (or org-cached-props
  9758. (setq org-cached-props
  9759. (org-entry-properties pom)))))))
  9760. (defun org-global-tags-completion-table (&optional files)
  9761. "Return the list of all tags in all agenda buffer/files."
  9762. (save-excursion
  9763. (org-uniquify
  9764. (delq nil
  9765. (apply 'append
  9766. (mapcar
  9767. (lambda (file)
  9768. (set-buffer (find-file-noselect file))
  9769. (append (org-get-buffer-tags)
  9770. (mapcar (lambda (x) (if (stringp (car-safe x))
  9771. (list (car-safe x)) nil))
  9772. org-tag-alist)))
  9773. (if (and files (car files))
  9774. files
  9775. (org-agenda-files))))))))
  9776. (defun org-make-tags-matcher (match)
  9777. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9778. ;; todo-only is scoped dynamically into this function, and the function
  9779. ;; may change it if the matcher asks for it.
  9780. (unless match
  9781. ;; Get a new match request, with completion
  9782. (let ((org-last-tags-completion-table
  9783. (org-global-tags-completion-table)))
  9784. (setq match (org-completing-read-no-i
  9785. "Match: " 'org-tags-completion-function nil nil nil
  9786. 'org-tags-history))))
  9787. ;; Parse the string and create a lisp form
  9788. (let ((match0 match)
  9789. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9790. minus tag mm
  9791. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9792. orterms term orlist re-p str-p level-p level-op time-p
  9793. prop-p pn pv po cat-p gv rest)
  9794. (if (string-match "/+" match)
  9795. ;; match contains also a todo-matching request
  9796. (progn
  9797. (setq tagsmatch (substring match 0 (match-beginning 0))
  9798. todomatch (substring match (match-end 0)))
  9799. (if (string-match "^!" todomatch)
  9800. (setq todo-only t todomatch (substring todomatch 1)))
  9801. (if (string-match "^\\s-*$" todomatch)
  9802. (setq todomatch nil)))
  9803. ;; only matching tags
  9804. (setq tagsmatch match todomatch nil))
  9805. ;; Make the tags matcher
  9806. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9807. (setq tagsmatcher t)
  9808. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9809. (while (setq term (pop orterms))
  9810. (while (and (equal (substring term -1) "\\") orterms)
  9811. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9812. (while (string-match re term)
  9813. (setq rest (substring term (match-end 0))
  9814. minus (and (match-end 1)
  9815. (equal (match-string 1 term) "-"))
  9816. tag (match-string 2 term)
  9817. re-p (equal (string-to-char tag) ?{)
  9818. level-p (match-end 4)
  9819. prop-p (match-end 5)
  9820. mm (cond
  9821. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9822. (level-p
  9823. (setq level-op (org-op-to-function (match-string 3 term)))
  9824. `(,level-op level ,(string-to-number
  9825. (match-string 4 term))))
  9826. (prop-p
  9827. (setq pn (match-string 5 term)
  9828. po (match-string 6 term)
  9829. pv (match-string 7 term)
  9830. cat-p (equal pn "CATEGORY")
  9831. re-p (equal (string-to-char pv) ?{)
  9832. str-p (equal (string-to-char pv) ?\")
  9833. time-p (save-match-data
  9834. (string-match "^\"[[<].*[]>]\"$" pv))
  9835. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9836. (if time-p (setq pv (org-matcher-time pv)))
  9837. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9838. (cond
  9839. ((equal pn "CATEGORY")
  9840. (setq gv '(get-text-property (point) 'org-category)))
  9841. ((equal pn "TODO")
  9842. (setq gv 'todo))
  9843. (t
  9844. (setq gv `(org-cached-entry-get nil ,pn))))
  9845. (if re-p
  9846. (if (eq po 'org<>)
  9847. `(not (string-match ,pv (or ,gv "")))
  9848. `(string-match ,pv (or ,gv "")))
  9849. (if str-p
  9850. `(,po (or ,gv "") ,pv)
  9851. `(,po (string-to-number (or ,gv ""))
  9852. ,(string-to-number pv) ))))
  9853. (t `(member ,tag tags-list)))
  9854. mm (if minus (list 'not mm) mm)
  9855. term rest)
  9856. (push mm tagsmatcher))
  9857. (push (if (> (length tagsmatcher) 1)
  9858. (cons 'and tagsmatcher)
  9859. (car tagsmatcher))
  9860. orlist)
  9861. (setq tagsmatcher nil))
  9862. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9863. (setq tagsmatcher
  9864. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9865. ;; Make the todo matcher
  9866. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9867. (setq todomatcher t)
  9868. (setq orterms (org-split-string todomatch "|") orlist nil)
  9869. (while (setq term (pop orterms))
  9870. (while (string-match re term)
  9871. (setq minus (and (match-end 1)
  9872. (equal (match-string 1 term) "-"))
  9873. kwd (match-string 2 term)
  9874. re-p (equal (string-to-char kwd) ?{)
  9875. term (substring term (match-end 0))
  9876. mm (if re-p
  9877. `(string-match ,(substring kwd 1 -1) todo)
  9878. (list 'equal 'todo kwd))
  9879. mm (if minus (list 'not mm) mm))
  9880. (push mm todomatcher))
  9881. (push (if (> (length todomatcher) 1)
  9882. (cons 'and todomatcher)
  9883. (car todomatcher))
  9884. orlist)
  9885. (setq todomatcher nil))
  9886. (setq todomatcher (if (> (length orlist) 1)
  9887. (cons 'or orlist) (car orlist))))
  9888. ;; Return the string and lisp forms of the matcher
  9889. (setq matcher (if todomatcher
  9890. (list 'and tagsmatcher todomatcher)
  9891. tagsmatcher))
  9892. (cons match0 matcher)))
  9893. (defun org-op-to-function (op &optional stringp)
  9894. "Turn an operator into the appropriate function."
  9895. (setq op
  9896. (cond
  9897. ((equal op "<" ) '(< string< org-time<))
  9898. ((equal op ">" ) '(> org-string> org-time>))
  9899. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9900. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9901. ((member op '("=" "==")) '(= string= org-time=))
  9902. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9903. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9904. (defun org<> (a b) (not (= a b)))
  9905. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9906. (defun org-string>= (a b) (not (string< a b)))
  9907. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9908. (defun org-string<> (a b) (not (string= a b)))
  9909. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9910. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9911. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9912. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9913. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9914. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9915. (defun org-2ft (s)
  9916. "Convert S to a floating point time.
  9917. If S is already a number, just return it. If it is a string, parse
  9918. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9919. (cond
  9920. ((numberp s) s)
  9921. ((stringp s)
  9922. (condition-case nil
  9923. (float-time (apply 'encode-time (org-parse-time-string s)))
  9924. (error 0.)))
  9925. (t 0.)))
  9926. (defun org-time-today ()
  9927. "Time in seconds today at 0:00.
  9928. Returns the float number of seconds since the beginning of the
  9929. epoch to the beginning of today (00:00)."
  9930. (float-time (apply 'encode-time
  9931. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9932. (defun org-matcher-time (s)
  9933. "Interpret a time comparison value."
  9934. (save-match-data
  9935. (cond
  9936. ((string= s "<now>") (float-time))
  9937. ((string= s "<today>") (org-time-today))
  9938. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9939. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9940. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9941. (+ (org-time-today)
  9942. (* (string-to-number (match-string 1 s))
  9943. (cdr (assoc (match-string 2 s)
  9944. '(("d" . 86400.0) ("w" . 604800.0)
  9945. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9946. (t (org-2ft s)))))
  9947. (defun org-match-any-p (re list)
  9948. "Does re match any element of list?"
  9949. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9950. (delq nil list))
  9951. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9952. (defvar org-tags-overlay (org-make-overlay 1 1))
  9953. (org-detach-overlay org-tags-overlay)
  9954. (defun org-get-local-tags-at (&optional pos)
  9955. "Get a list of tags defined in the current headline."
  9956. (org-get-tags-at pos 'local))
  9957. (defun org-get-local-tags ()
  9958. "Get a list of tags defined in the current headline."
  9959. (org-get-tags-at nil 'local))
  9960. (defun org-get-tags-at (&optional pos local)
  9961. "Get a list of all headline tags applicable at POS.
  9962. POS defaults to point. If tags are inherited, the list contains
  9963. the targets in the same sequence as the headlines appear, i.e.
  9964. the tags of the current headline come last.
  9965. When LOCAL is non-nil, only return tags from the current headline,
  9966. ignore inherited ones."
  9967. (interactive)
  9968. (if (and org-trust-scanner-tags
  9969. (or (not pos) (equal pos (point)))
  9970. (not local))
  9971. org-scanner-tags
  9972. (let (tags ltags lastpos parent)
  9973. (save-excursion
  9974. (save-restriction
  9975. (widen)
  9976. (goto-char (or pos (point)))
  9977. (save-match-data
  9978. (catch 'done
  9979. (condition-case nil
  9980. (progn
  9981. (org-back-to-heading t)
  9982. (while (not (equal lastpos (point)))
  9983. (setq lastpos (point))
  9984. (when (looking-at
  9985. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9986. (setq ltags (org-split-string
  9987. (org-match-string-no-properties 1) ":"))
  9988. (when parent
  9989. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9990. (setq tags (append
  9991. (if parent
  9992. (org-remove-uniherited-tags ltags)
  9993. ltags)
  9994. tags)))
  9995. (or org-use-tag-inheritance (throw 'done t))
  9996. (if local (throw 'done t))
  9997. (or (org-up-heading-safe) (error nil))
  9998. (setq parent t)))
  9999. (error nil)))))
  10000. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10001. (defun org-add-prop-inherited (s)
  10002. (add-text-properties 0 (length s) '(inherited t) s)
  10003. s)
  10004. (defun org-toggle-tag (tag &optional onoff)
  10005. "Toggle the tag TAG for the current line.
  10006. If ONOFF is `on' or `off', don't toggle but set to this state."
  10007. (let (res current)
  10008. (save-excursion
  10009. (org-back-to-heading t)
  10010. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10011. (point-at-eol) t)
  10012. (progn
  10013. (setq current (match-string 1))
  10014. (replace-match ""))
  10015. (setq current ""))
  10016. (setq current (nreverse (org-split-string current ":")))
  10017. (cond
  10018. ((eq onoff 'on)
  10019. (setq res t)
  10020. (or (member tag current) (push tag current)))
  10021. ((eq onoff 'off)
  10022. (or (not (member tag current)) (setq current (delete tag current))))
  10023. (t (if (member tag current)
  10024. (setq current (delete tag current))
  10025. (setq res t)
  10026. (push tag current))))
  10027. (end-of-line 1)
  10028. (if current
  10029. (progn
  10030. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10031. (org-set-tags nil t))
  10032. (delete-horizontal-space))
  10033. (run-hooks 'org-after-tags-change-hook))
  10034. res))
  10035. (defun org-align-tags-here (to-col)
  10036. ;; Assumes that this is a headline
  10037. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10038. (beginning-of-line 1)
  10039. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10040. (< pos (match-beginning 2)))
  10041. (progn
  10042. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10043. (goto-char (match-beginning 1))
  10044. (insert " ")
  10045. (delete-region (point) (1+ (match-beginning 2)))
  10046. (setq ncol (max (1+ (current-column))
  10047. (1+ col)
  10048. (if (> to-col 0)
  10049. to-col
  10050. (- (abs to-col) tags-l))))
  10051. (setq p (point))
  10052. (insert (make-string (- ncol (current-column)) ?\ ))
  10053. (setq ncol (current-column))
  10054. (when indent-tabs-mode (tabify p (point-at-eol)))
  10055. (org-move-to-column (min ncol col) t))
  10056. (goto-char pos))))
  10057. (defun org-set-tags-command (&optional arg just-align)
  10058. "Call the set-tags command for the current entry."
  10059. (interactive "P")
  10060. (if (org-on-heading-p)
  10061. (org-set-tags arg just-align)
  10062. (save-excursion
  10063. (org-back-to-heading t)
  10064. (org-set-tags arg just-align))))
  10065. (defun org-set-tags (&optional arg just-align)
  10066. "Set the tags for the current headline.
  10067. With prefix ARG, realign all tags in headings in the current buffer."
  10068. (interactive "P")
  10069. (let* ((re (concat "^" outline-regexp))
  10070. (current (org-get-tags-string))
  10071. (col (current-column))
  10072. (org-setting-tags t)
  10073. table current-tags inherited-tags ; computed below when needed
  10074. tags p0 c0 c1 rpl)
  10075. (if arg
  10076. (save-excursion
  10077. (goto-char (point-min))
  10078. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10079. (while (re-search-forward re nil t)
  10080. (org-set-tags nil t)
  10081. (end-of-line 1)))
  10082. (message "All tags realigned to column %d" org-tags-column))
  10083. (if just-align
  10084. (setq tags current)
  10085. ;; Get a new set of tags from the user
  10086. (save-excursion
  10087. (setq table (append org-tag-persistent-alist
  10088. (or org-tag-alist (org-get-buffer-tags)))
  10089. org-last-tags-completion-table table
  10090. current-tags (org-split-string current ":")
  10091. inherited-tags (nreverse
  10092. (nthcdr (length current-tags)
  10093. (nreverse (org-get-tags-at))))
  10094. tags
  10095. (if (or (eq t org-use-fast-tag-selection)
  10096. (and org-use-fast-tag-selection
  10097. (delq nil (mapcar 'cdr table))))
  10098. (org-fast-tag-selection
  10099. current-tags inherited-tags table
  10100. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10101. (let ((org-add-colon-after-tag-completion t))
  10102. (org-trim
  10103. (org-without-partial-completion
  10104. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10105. nil nil current 'org-tags-history)))))))
  10106. (while (string-match "[-+&]+" tags)
  10107. ;; No boolean logic, just a list
  10108. (setq tags (replace-match ":" t t tags))))
  10109. (if org-tags-sort-function
  10110. (setq tags (mapconcat 'identity
  10111. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10112. org-tags-sort-function) ":")))
  10113. (if (string-match "\\`[\t ]*\\'" tags)
  10114. (setq tags "")
  10115. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10116. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10117. ;; Insert new tags at the correct column
  10118. (beginning-of-line 1)
  10119. (cond
  10120. ((and (equal current "") (equal tags "")))
  10121. ((re-search-forward
  10122. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10123. (point-at-eol) t)
  10124. (if (equal tags "")
  10125. (setq rpl "")
  10126. (goto-char (match-beginning 0))
  10127. (setq c0 (current-column) p0 (point)
  10128. c1 (max (1+ c0) (if (> org-tags-column 0)
  10129. org-tags-column
  10130. (- (- org-tags-column) (length tags))))
  10131. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10132. (replace-match rpl t t)
  10133. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10134. tags)
  10135. (t (error "Tags alignment failed")))
  10136. (org-move-to-column col)
  10137. (unless just-align
  10138. (run-hooks 'org-after-tags-change-hook)))))
  10139. (defun org-change-tag-in-region (beg end tag off)
  10140. "Add or remove TAG for each entry in the region.
  10141. This works in the agenda, and also in an org-mode buffer."
  10142. (interactive
  10143. (list (region-beginning) (region-end)
  10144. (let ((org-last-tags-completion-table
  10145. (if (org-mode-p)
  10146. (org-get-buffer-tags)
  10147. (org-global-tags-completion-table))))
  10148. (org-icompleting-read
  10149. "Tag: " 'org-tags-completion-function nil nil nil
  10150. 'org-tags-history))
  10151. (progn
  10152. (message "[s]et or [r]emove? ")
  10153. (equal (read-char-exclusive) ?r))))
  10154. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10155. (let ((agendap (equal major-mode 'org-agenda-mode))
  10156. l1 l2 m buf pos newhead (cnt 0))
  10157. (goto-char end)
  10158. (setq l2 (1- (org-current-line)))
  10159. (goto-char beg)
  10160. (setq l1 (org-current-line))
  10161. (loop for l from l1 to l2 do
  10162. (goto-line l)
  10163. (setq m (get-text-property (point) 'org-hd-marker))
  10164. (when (or (and (org-mode-p) (org-on-heading-p))
  10165. (and agendap m))
  10166. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10167. pos (if agendap m (point)))
  10168. (with-current-buffer buf
  10169. (save-excursion
  10170. (save-restriction
  10171. (goto-char pos)
  10172. (setq cnt (1+ cnt))
  10173. (org-toggle-tag tag (if off 'off 'on))
  10174. (setq newhead (org-get-heading)))))
  10175. (and agendap (org-agenda-change-all-lines newhead m))))
  10176. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10177. (defun org-tags-completion-function (string predicate &optional flag)
  10178. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10179. (confirm (lambda (x) (stringp (car x)))))
  10180. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10181. (setq s1 (match-string 1 string)
  10182. s2 (match-string 2 string))
  10183. (setq s1 "" s2 string))
  10184. (cond
  10185. ((eq flag nil)
  10186. ;; try completion
  10187. (setq rtn (try-completion s2 ctable confirm))
  10188. (if (stringp rtn)
  10189. (setq rtn
  10190. (concat s1 s2 (substring rtn (length s2))
  10191. (if (and org-add-colon-after-tag-completion
  10192. (assoc rtn ctable))
  10193. ":" ""))))
  10194. rtn)
  10195. ((eq flag t)
  10196. ;; all-completions
  10197. (all-completions s2 ctable confirm)
  10198. )
  10199. ((eq flag 'lambda)
  10200. ;; exact match?
  10201. (assoc s2 ctable)))
  10202. ))
  10203. (defun org-fast-tag-insert (kwd tags face &optional end)
  10204. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10205. (insert (format "%-12s" (concat kwd ":"))
  10206. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10207. (or end "")))
  10208. (defun org-fast-tag-show-exit (flag)
  10209. (save-excursion
  10210. (goto-line 3)
  10211. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10212. (replace-match ""))
  10213. (when flag
  10214. (end-of-line 1)
  10215. (org-move-to-column (- (window-width) 19) t)
  10216. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10217. (defun org-set-current-tags-overlay (current prefix)
  10218. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10219. (if (featurep 'xemacs)
  10220. (org-overlay-display org-tags-overlay (concat prefix s)
  10221. 'secondary-selection)
  10222. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10223. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10224. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10225. "Fast tag selection with single keys.
  10226. CURRENT is the current list of tags in the headline, INHERITED is the
  10227. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10228. possibly with grouping information. TODO-TABLE is a similar table with
  10229. TODO keywords, should these have keys assigned to them.
  10230. If the keys are nil, a-z are automatically assigned.
  10231. Returns the new tags string, or nil to not change the current settings."
  10232. (let* ((fulltable (append table todo-table))
  10233. (maxlen (apply 'max (mapcar
  10234. (lambda (x)
  10235. (if (stringp (car x)) (string-width (car x)) 0))
  10236. fulltable)))
  10237. (buf (current-buffer))
  10238. (expert (eq org-fast-tag-selection-single-key 'expert))
  10239. (buffer-tags nil)
  10240. (fwidth (+ maxlen 3 1 3))
  10241. (ncol (/ (- (window-width) 4) fwidth))
  10242. (i-face 'org-done)
  10243. (c-face 'org-todo)
  10244. tg cnt e c char c1 c2 ntable tbl rtn
  10245. ov-start ov-end ov-prefix
  10246. (exit-after-next org-fast-tag-selection-single-key)
  10247. (done-keywords org-done-keywords)
  10248. groups ingroup)
  10249. (save-excursion
  10250. (beginning-of-line 1)
  10251. (if (looking-at
  10252. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10253. (setq ov-start (match-beginning 1)
  10254. ov-end (match-end 1)
  10255. ov-prefix "")
  10256. (setq ov-start (1- (point-at-eol))
  10257. ov-end (1+ ov-start))
  10258. (skip-chars-forward "^\n\r")
  10259. (setq ov-prefix
  10260. (concat
  10261. (buffer-substring (1- (point)) (point))
  10262. (if (> (current-column) org-tags-column)
  10263. " "
  10264. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10265. (org-move-overlay org-tags-overlay ov-start ov-end)
  10266. (save-window-excursion
  10267. (if expert
  10268. (set-buffer (get-buffer-create " *Org tags*"))
  10269. (delete-other-windows)
  10270. (split-window-vertically)
  10271. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10272. (erase-buffer)
  10273. (org-set-local 'org-done-keywords done-keywords)
  10274. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10275. (org-fast-tag-insert "Current" current c-face "\n\n")
  10276. (org-fast-tag-show-exit exit-after-next)
  10277. (org-set-current-tags-overlay current ov-prefix)
  10278. (setq tbl fulltable char ?a cnt 0)
  10279. (while (setq e (pop tbl))
  10280. (cond
  10281. ((equal e '(:startgroup))
  10282. (push '() groups) (setq ingroup t)
  10283. (when (not (= cnt 0))
  10284. (setq cnt 0)
  10285. (insert "\n"))
  10286. (insert "{ "))
  10287. ((equal e '(:endgroup))
  10288. (setq ingroup nil cnt 0)
  10289. (insert "}\n"))
  10290. ((equal e '(:newline))
  10291. (when (not (= cnt 0))
  10292. (setq cnt 0)
  10293. (insert "\n")
  10294. (setq e (car tbl))
  10295. (while (equal (car tbl) '(:newline))
  10296. (insert "\n")
  10297. (setq tbl (cdr tbl)))))
  10298. (t
  10299. (setq tg (copy-sequence (car e)) c2 nil)
  10300. (if (cdr e)
  10301. (setq c (cdr e))
  10302. ;; automatically assign a character.
  10303. (setq c1 (string-to-char
  10304. (downcase (substring
  10305. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10306. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10307. (while (or (rassoc char ntable) (rassoc char table))
  10308. (setq char (1+ char)))
  10309. (setq c2 c1))
  10310. (setq c (or c2 char)))
  10311. (if ingroup (push tg (car groups)))
  10312. (setq tg (org-add-props tg nil 'face
  10313. (cond
  10314. ((not (assoc tg table))
  10315. (org-get-todo-face tg))
  10316. ((member tg current) c-face)
  10317. ((member tg inherited) i-face)
  10318. (t nil))))
  10319. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10320. (insert "[" c "] " tg (make-string
  10321. (- fwidth 4 (length tg)) ?\ ))
  10322. (push (cons tg c) ntable)
  10323. (when (= (setq cnt (1+ cnt)) ncol)
  10324. (insert "\n")
  10325. (if ingroup (insert " "))
  10326. (setq cnt 0)))))
  10327. (setq ntable (nreverse ntable))
  10328. (insert "\n")
  10329. (goto-char (point-min))
  10330. (if (not expert) (org-fit-window-to-buffer))
  10331. (setq rtn
  10332. (catch 'exit
  10333. (while t
  10334. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10335. (if groups " [!] no groups" " [!]groups")
  10336. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10337. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10338. (cond
  10339. ((= c ?\r) (throw 'exit t))
  10340. ((= c ?!)
  10341. (setq groups (not groups))
  10342. (goto-char (point-min))
  10343. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10344. ((= c ?\C-c)
  10345. (if (not expert)
  10346. (org-fast-tag-show-exit
  10347. (setq exit-after-next (not exit-after-next)))
  10348. (setq expert nil)
  10349. (delete-other-windows)
  10350. (split-window-vertically)
  10351. (org-switch-to-buffer-other-window " *Org tags*")
  10352. (org-fit-window-to-buffer)))
  10353. ((or (= c ?\C-g)
  10354. (and (= c ?q) (not (rassoc c ntable))))
  10355. (org-detach-overlay org-tags-overlay)
  10356. (setq quit-flag t))
  10357. ((= c ?\ )
  10358. (setq current nil)
  10359. (if exit-after-next (setq exit-after-next 'now)))
  10360. ((= c ?\t)
  10361. (condition-case nil
  10362. (setq tg (org-icompleting-read
  10363. "Tag: "
  10364. (or buffer-tags
  10365. (with-current-buffer buf
  10366. (org-get-buffer-tags)))))
  10367. (quit (setq tg "")))
  10368. (when (string-match "\\S-" tg)
  10369. (add-to-list 'buffer-tags (list tg))
  10370. (if (member tg current)
  10371. (setq current (delete tg current))
  10372. (push tg current)))
  10373. (if exit-after-next (setq exit-after-next 'now)))
  10374. ((setq e (rassoc c todo-table) tg (car e))
  10375. (with-current-buffer buf
  10376. (save-excursion (org-todo tg)))
  10377. (if exit-after-next (setq exit-after-next 'now)))
  10378. ((setq e (rassoc c ntable) tg (car e))
  10379. (if (member tg current)
  10380. (setq current (delete tg current))
  10381. (loop for g in groups do
  10382. (if (member tg g)
  10383. (mapc (lambda (x)
  10384. (setq current (delete x current)))
  10385. g)))
  10386. (push tg current))
  10387. (if exit-after-next (setq exit-after-next 'now))))
  10388. ;; Create a sorted list
  10389. (setq current
  10390. (sort current
  10391. (lambda (a b)
  10392. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10393. (if (eq exit-after-next 'now) (throw 'exit t))
  10394. (goto-char (point-min))
  10395. (beginning-of-line 2)
  10396. (delete-region (point) (point-at-eol))
  10397. (org-fast-tag-insert "Current" current c-face)
  10398. (org-set-current-tags-overlay current ov-prefix)
  10399. (while (re-search-forward
  10400. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10401. (setq tg (match-string 1))
  10402. (add-text-properties
  10403. (match-beginning 1) (match-end 1)
  10404. (list 'face
  10405. (cond
  10406. ((member tg current) c-face)
  10407. ((member tg inherited) i-face)
  10408. (t (get-text-property (match-beginning 1) 'face))))))
  10409. (goto-char (point-min)))))
  10410. (org-detach-overlay org-tags-overlay)
  10411. (if rtn
  10412. (mapconcat 'identity current ":")
  10413. nil))))
  10414. (defun org-get-tags-string ()
  10415. "Get the TAGS string in the current headline."
  10416. (unless (org-on-heading-p t)
  10417. (error "Not on a heading"))
  10418. (save-excursion
  10419. (beginning-of-line 1)
  10420. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10421. (org-match-string-no-properties 1)
  10422. "")))
  10423. (defun org-get-tags ()
  10424. "Get the list of tags specified in the current headline."
  10425. (org-split-string (org-get-tags-string) ":"))
  10426. (defun org-get-buffer-tags ()
  10427. "Get a table of all tags used in the buffer, for completion."
  10428. (let (tags)
  10429. (save-excursion
  10430. (goto-char (point-min))
  10431. (while (re-search-forward
  10432. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10433. (when (equal (char-after (point-at-bol 0)) ?*)
  10434. (mapc (lambda (x) (add-to-list 'tags x))
  10435. (org-split-string (org-match-string-no-properties 1) ":")))))
  10436. (mapcar 'list tags)))
  10437. ;;;; The mapping API
  10438. ;;;###autoload
  10439. (defun org-map-entries (func &optional match scope &rest skip)
  10440. "Call FUNC at each headline selected by MATCH in SCOPE.
  10441. FUNC is a function or a lisp form. The function will be called without
  10442. arguments, with the cursor positioned at the beginning of the headline.
  10443. The return values of all calls to the function will be collected and
  10444. returned as a list.
  10445. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10446. does not need to preserve point. After evaluation, the cursor will be
  10447. moved to the end of the line (presumably of the headline of the
  10448. processed entry) and search continues from there. Under some
  10449. circumstances, this may not produce the wanted results. For example,
  10450. if you have removed (e.g. archived) the current (sub)tree it could
  10451. mean that the next entry will be skipped entirely. In such cases, you
  10452. can specify the position from where search should continue by making
  10453. FUNC set the variable `org-map-continue-from' to the desired buffer
  10454. position.
  10455. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10456. Only headlines that are matched by this query will be considered during
  10457. the iteration. When MATCH is nil or t, all headlines will be
  10458. visited by the iteration.
  10459. SCOPE determines the scope of this command. It can be any of:
  10460. nil The current buffer, respecting the restriction if any
  10461. tree The subtree started with the entry at point
  10462. file The current buffer, without restriction
  10463. file-with-archives
  10464. The current buffer, and any archives associated with it
  10465. agenda All agenda files
  10466. agenda-with-archives
  10467. All agenda files with any archive files associated with them
  10468. \(file1 file2 ...)
  10469. If this is a list, all files in the list will be scanned
  10470. The remaining args are treated as settings for the skipping facilities of
  10471. the scanner. The following items can be given here:
  10472. archive skip trees with the archive tag.
  10473. comment skip trees with the COMMENT keyword
  10474. function or Emacs Lisp form:
  10475. will be used as value for `org-agenda-skip-function', so whenever
  10476. the the function returns t, FUNC will not be called for that
  10477. entry and search will continue from the point where the
  10478. function leaves it.
  10479. If your function needs to retrieve the tags including inherited tags
  10480. at the *current* entry, you can use the value of the variable
  10481. `org-scanner-tags' which will be much faster than getting the value
  10482. with `org-get-tags-at'. If your function gets properties with
  10483. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10484. to t around the call to `org-entry-properties' to get the same speedup.
  10485. Note that if your function moves around to retrieve tags and properties at
  10486. a *different* entry, you cannot use these techniques."
  10487. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10488. (org-agenda-skip-archived-trees (memq 'archive skip))
  10489. (org-agenda-skip-comment-trees (memq 'comment skip))
  10490. (org-agenda-skip-function
  10491. (car (org-delete-all '(comment archive) skip)))
  10492. (org-tags-match-list-sublevels t)
  10493. matcher file res
  10494. org-todo-keywords-for-agenda
  10495. org-done-keywords-for-agenda
  10496. org-todo-keyword-alist-for-agenda
  10497. org-tag-alist-for-agenda)
  10498. (cond
  10499. ((eq match t) (setq matcher t))
  10500. ((eq match nil) (setq matcher t))
  10501. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10502. (save-excursion
  10503. (save-restriction
  10504. (when (eq scope 'tree)
  10505. (org-back-to-heading t)
  10506. (org-narrow-to-subtree)
  10507. (setq scope nil))
  10508. (if (not scope)
  10509. (progn
  10510. (org-prepare-agenda-buffers
  10511. (list (buffer-file-name (current-buffer))))
  10512. (setq res (org-scan-tags func matcher)))
  10513. ;; Get the right scope
  10514. (cond
  10515. ((and scope (listp scope) (symbolp (car scope)))
  10516. (setq scope (eval scope)))
  10517. ((eq scope 'agenda)
  10518. (setq scope (org-agenda-files t)))
  10519. ((eq scope 'agenda-with-archives)
  10520. (setq scope (org-agenda-files t))
  10521. (setq scope (org-add-archive-files scope)))
  10522. ((eq scope 'file)
  10523. (setq scope (list (buffer-file-name))))
  10524. ((eq scope 'file-with-archives)
  10525. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10526. (org-prepare-agenda-buffers scope)
  10527. (while (setq file (pop scope))
  10528. (with-current-buffer (org-find-base-buffer-visiting file)
  10529. (save-excursion
  10530. (save-restriction
  10531. (widen)
  10532. (goto-char (point-min))
  10533. (setq res (append res (org-scan-tags func matcher))))))))))
  10534. res))
  10535. ;;;; Properties
  10536. ;;; Setting and retrieving properties
  10537. (defconst org-special-properties
  10538. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10539. "TIMESTAMP" "TIMESTAMP_IA")
  10540. "The special properties valid in Org-mode.
  10541. These are properties that are not defined in the property drawer,
  10542. but in some other way.")
  10543. (defconst org-default-properties
  10544. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10545. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10546. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10547. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10548. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10549. "CLOCK_MODELINE_TOTAL")
  10550. "Some properties that are used by Org-mode for various purposes.
  10551. Being in this list makes sure that they are offered for completion.")
  10552. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10553. "Regular expression matching the first line of a property drawer.")
  10554. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10555. "Regular expression matching the first line of a property drawer.")
  10556. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10557. "Regular expression matching the first line of a property drawer.")
  10558. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10559. "Regular expression matching the first line of a property drawer.")
  10560. (defconst org-property-drawer-re
  10561. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10562. org-property-end-re "\\)\n?")
  10563. "Matches an entire property drawer.")
  10564. (defconst org-clock-drawer-re
  10565. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10566. org-property-end-re "\\)\n?")
  10567. "Matches an entire clock drawer.")
  10568. (defun org-property-action ()
  10569. "Do an action on properties."
  10570. (interactive)
  10571. (let (c)
  10572. (org-at-property-p)
  10573. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10574. (setq c (read-char-exclusive))
  10575. (cond
  10576. ((equal c ?s)
  10577. (call-interactively 'org-set-property))
  10578. ((equal c ?d)
  10579. (call-interactively 'org-delete-property))
  10580. ((equal c ?D)
  10581. (call-interactively 'org-delete-property-globally))
  10582. ((equal c ?c)
  10583. (call-interactively 'org-compute-property-at-point))
  10584. (t (error "No such property action %c" c)))))
  10585. (defun org-at-property-p ()
  10586. "Is the cursor in a property line?"
  10587. ;; FIXME: Does not check if we are actually in the drawer.
  10588. ;; FIXME: also returns true on any drawers.....
  10589. ;; This is used by C-c C-c for property action.
  10590. (save-excursion
  10591. (beginning-of-line 1)
  10592. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10593. (defun org-get-property-block (&optional beg end force)
  10594. "Return the (beg . end) range of the body of the property drawer.
  10595. BEG and END can be beginning and end of subtree, if not given
  10596. they will be found.
  10597. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10598. (catch 'exit
  10599. (save-excursion
  10600. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10601. (end (or end (progn (outline-next-heading) (point)))))
  10602. (goto-char beg)
  10603. (if (re-search-forward org-property-start-re end t)
  10604. (setq beg (1+ (match-end 0)))
  10605. (if force
  10606. (save-excursion
  10607. (org-insert-property-drawer)
  10608. (setq end (progn (outline-next-heading) (point))))
  10609. (throw 'exit nil))
  10610. (goto-char beg)
  10611. (if (re-search-forward org-property-start-re end t)
  10612. (setq beg (1+ (match-end 0)))))
  10613. (if (re-search-forward org-property-end-re end t)
  10614. (setq end (match-beginning 0))
  10615. (or force (throw 'exit nil))
  10616. (goto-char beg)
  10617. (setq end beg)
  10618. (org-indent-line-function)
  10619. (insert ":END:\n"))
  10620. (cons beg end)))))
  10621. (defun org-entry-properties (&optional pom which)
  10622. "Get all properties of the entry at point-or-marker POM.
  10623. This includes the TODO keyword, the tags, time strings for deadline,
  10624. scheduled, and clocking, and any additional properties defined in the
  10625. entry. The return value is an alist, keys may occur multiple times
  10626. if the property key was used several times.
  10627. POM may also be nil, in which case the current entry is used.
  10628. If WHICH is nil or `all', get all properties. If WHICH is
  10629. `special' or `standard', only get that subclass."
  10630. (setq which (or which 'all))
  10631. (org-with-point-at pom
  10632. (let ((clockstr (substring org-clock-string 0 -1))
  10633. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10634. beg end range props sum-props key value string clocksum)
  10635. (save-excursion
  10636. (when (condition-case nil
  10637. (and (org-mode-p) (org-back-to-heading t))
  10638. (error nil))
  10639. (setq beg (point))
  10640. (setq sum-props (get-text-property (point) 'org-summaries))
  10641. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10642. (outline-next-heading)
  10643. (setq end (point))
  10644. (when (memq which '(all special))
  10645. ;; Get the special properties, like TODO and tags
  10646. (goto-char beg)
  10647. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10648. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10649. (when (looking-at org-priority-regexp)
  10650. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10651. (when (and (setq value (org-get-tags-string))
  10652. (string-match "\\S-" value))
  10653. (push (cons "TAGS" value) props))
  10654. (when (setq value (org-get-tags-at))
  10655. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10656. props))
  10657. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10658. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10659. string (if (equal key clockstr)
  10660. (org-no-properties
  10661. (org-trim
  10662. (buffer-substring
  10663. (match-beginning 3) (goto-char (point-at-eol)))))
  10664. (substring (org-match-string-no-properties 3) 1 -1)))
  10665. (unless key
  10666. (if (= (char-after (match-beginning 3)) ?\[)
  10667. (setq key "TIMESTAMP_IA")
  10668. (setq key "TIMESTAMP")))
  10669. (when (or (equal key clockstr) (not (assoc key props)))
  10670. (push (cons key string) props)))
  10671. )
  10672. (when (memq which '(all standard))
  10673. ;; Get the standard properties, like :PROP: ...
  10674. (setq range (org-get-property-block beg end))
  10675. (when range
  10676. (goto-char (car range))
  10677. (while (re-search-forward
  10678. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10679. (cdr range) t)
  10680. (setq key (org-match-string-no-properties 1)
  10681. value (org-trim (or (org-match-string-no-properties 2) "")))
  10682. (unless (member key excluded)
  10683. (push (cons key (or value "")) props)))))
  10684. (if clocksum
  10685. (push (cons "CLOCKSUM"
  10686. (org-columns-number-to-string (/ (float clocksum) 60.)
  10687. 'add_times))
  10688. props))
  10689. (unless (assoc "CATEGORY" props)
  10690. (setq value (or (org-get-category)
  10691. (progn (org-refresh-category-properties)
  10692. (org-get-category))))
  10693. (push (cons "CATEGORY" value) props))
  10694. (append sum-props (nreverse props)))))))
  10695. (defun org-entry-get (pom property &optional inherit)
  10696. "Get value of PROPERTY for entry at point-or-marker POM.
  10697. If INHERIT is non-nil and the entry does not have the property,
  10698. then also check higher levels of the hierarchy.
  10699. If INHERIT is the symbol `selective', use inheritance only if the setting
  10700. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10701. If the property is present but empty, the return value is the empty string.
  10702. If the property is not present at all, nil is returned."
  10703. (org-with-point-at pom
  10704. (if (and inherit (if (eq inherit 'selective)
  10705. (org-property-inherit-p property)
  10706. t))
  10707. (org-entry-get-with-inheritance property)
  10708. (if (member property org-special-properties)
  10709. ;; We need a special property. Use brute force, get all properties.
  10710. (cdr (assoc property (org-entry-properties nil 'special)))
  10711. (let ((range (org-get-property-block)))
  10712. (if (and range
  10713. (goto-char (car range))
  10714. (re-search-forward
  10715. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10716. (cdr range) t))
  10717. ;; Found the property, return it.
  10718. (if (match-end 1)
  10719. (org-match-string-no-properties 1)
  10720. "")))))))
  10721. (defun org-property-or-variable-value (var &optional inherit)
  10722. "Check if there is a property fixing the value of VAR.
  10723. If yes, return this value. If not, return the current value of the variable."
  10724. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10725. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10726. (read prop)
  10727. (symbol-value var))))
  10728. (defun org-entry-delete (pom property)
  10729. "Delete the property PROPERTY from entry at point-or-marker POM."
  10730. (org-with-point-at pom
  10731. (if (member property org-special-properties)
  10732. nil ; cannot delete these properties.
  10733. (let ((range (org-get-property-block)))
  10734. (if (and range
  10735. (goto-char (car range))
  10736. (re-search-forward
  10737. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10738. (cdr range) t))
  10739. (progn
  10740. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10741. t)
  10742. nil)))))
  10743. ;; Multi-values properties are properties that contain multiple values
  10744. ;; These values are assumed to be single words, separated by whitespace.
  10745. (defun org-entry-add-to-multivalued-property (pom property value)
  10746. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10747. (let* ((old (org-entry-get pom property))
  10748. (values (and old (org-split-string old "[ \t]"))))
  10749. (setq value (org-entry-protect-space value))
  10750. (unless (member value values)
  10751. (setq values (cons value values))
  10752. (org-entry-put pom property
  10753. (mapconcat 'identity values " ")))))
  10754. (defun org-entry-remove-from-multivalued-property (pom property value)
  10755. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10756. (let* ((old (org-entry-get pom property))
  10757. (values (and old (org-split-string old "[ \t]"))))
  10758. (setq value (org-entry-protect-space value))
  10759. (when (member value values)
  10760. (setq values (delete value values))
  10761. (org-entry-put pom property
  10762. (mapconcat 'identity values " ")))))
  10763. (defun org-entry-member-in-multivalued-property (pom property value)
  10764. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10765. (let* ((old (org-entry-get pom property))
  10766. (values (and old (org-split-string old "[ \t]"))))
  10767. (setq value (org-entry-protect-space value))
  10768. (member value values)))
  10769. (defun org-entry-get-multivalued-property (pom property)
  10770. "Return a list of values in a multivalued property."
  10771. (let* ((value (org-entry-get pom property))
  10772. (values (and value (org-split-string value "[ \t]"))))
  10773. (mapcar 'org-entry-restore-space values)))
  10774. (defun org-entry-put-multivalued-property (pom property &rest values)
  10775. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10776. VALUES should be a list of strings. Spaces will be protected."
  10777. (org-entry-put pom property
  10778. (mapconcat 'org-entry-protect-space values " "))
  10779. (let* ((value (org-entry-get pom property))
  10780. (values (and value (org-split-string value "[ \t]"))))
  10781. (mapcar 'org-entry-restore-space values)))
  10782. (defun org-entry-protect-space (s)
  10783. "Protect spaces and newline in string S."
  10784. (while (string-match " " s)
  10785. (setq s (replace-match "%20" t t s)))
  10786. (while (string-match "\n" s)
  10787. (setq s (replace-match "%0A" t t s)))
  10788. s)
  10789. (defun org-entry-restore-space (s)
  10790. "Restore spaces and newline in string S."
  10791. (while (string-match "%20" s)
  10792. (setq s (replace-match " " t t s)))
  10793. (while (string-match "%0A" s)
  10794. (setq s (replace-match "\n" t t s)))
  10795. s)
  10796. (defvar org-entry-property-inherited-from (make-marker)
  10797. "Marker pointing to the entry from where a property was inherited.
  10798. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10799. location of the entry where the inheritance search matched. If there was
  10800. no match, the marker will point nowhere.
  10801. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10802. is set.")
  10803. (defun org-entry-get-with-inheritance (property)
  10804. "Get entry property, and search higher levels if not present."
  10805. (move-marker org-entry-property-inherited-from nil)
  10806. (let (tmp)
  10807. (save-excursion
  10808. (save-restriction
  10809. (widen)
  10810. (catch 'ex
  10811. (while t
  10812. (when (setq tmp (org-entry-get nil property))
  10813. (org-back-to-heading t)
  10814. (move-marker org-entry-property-inherited-from (point))
  10815. (throw 'ex tmp))
  10816. (or (org-up-heading-safe) (throw 'ex nil)))))
  10817. (or tmp
  10818. (cdr (assoc property org-file-properties))
  10819. (cdr (assoc property org-global-properties))
  10820. (cdr (assoc property org-global-properties-fixed))))))
  10821. (defun org-entry-put (pom property value)
  10822. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10823. (org-with-point-at pom
  10824. (org-back-to-heading t)
  10825. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10826. range)
  10827. (cond
  10828. ((equal property "TODO")
  10829. (when (and (stringp value) (string-match "\\S-" value)
  10830. (not (member value org-todo-keywords-1)))
  10831. (error "\"%s\" is not a valid TODO state" value))
  10832. (if (or (not value)
  10833. (not (string-match "\\S-" value)))
  10834. (setq value 'none))
  10835. (org-todo value)
  10836. (org-set-tags nil 'align))
  10837. ((equal property "PRIORITY")
  10838. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10839. (string-to-char value) ?\ ))
  10840. (org-set-tags nil 'align))
  10841. ((equal property "SCHEDULED")
  10842. (if (re-search-forward org-scheduled-time-regexp end t)
  10843. (cond
  10844. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10845. ((eq value 'later) (org-timestamp-change 1 'day))
  10846. (t (call-interactively 'org-schedule)))
  10847. (call-interactively 'org-schedule)))
  10848. ((equal property "DEADLINE")
  10849. (if (re-search-forward org-deadline-time-regexp end t)
  10850. (cond
  10851. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10852. ((eq value 'later) (org-timestamp-change 1 'day))
  10853. (t (call-interactively 'org-deadline)))
  10854. (call-interactively 'org-deadline)))
  10855. ((member property org-special-properties)
  10856. (error "The %s property can not yet be set with `org-entry-put'"
  10857. property))
  10858. (t ; a non-special property
  10859. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10860. (setq range (org-get-property-block beg end 'force))
  10861. (goto-char (car range))
  10862. (if (re-search-forward
  10863. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10864. (progn
  10865. (delete-region (match-beginning 1) (match-end 1))
  10866. (goto-char (match-beginning 1)))
  10867. (goto-char (cdr range))
  10868. (insert "\n")
  10869. (backward-char 1)
  10870. (org-indent-line-function)
  10871. (insert ":" property ":"))
  10872. (and value (insert " " value))
  10873. (org-indent-line-function)))))))
  10874. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10875. "Get all property keys in the current buffer.
  10876. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10877. like tags and TODO state.
  10878. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10879. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10880. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10881. formats in the current buffer."
  10882. (let (rtn range cfmt s p)
  10883. (save-excursion
  10884. (save-restriction
  10885. (widen)
  10886. (goto-char (point-min))
  10887. (while (re-search-forward org-property-start-re nil t)
  10888. (setq range (org-get-property-block))
  10889. (goto-char (car range))
  10890. (while (re-search-forward
  10891. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10892. (cdr range) t)
  10893. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10894. (outline-next-heading))))
  10895. (when include-specials
  10896. (setq rtn (append org-special-properties rtn)))
  10897. (when include-defaults
  10898. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10899. (add-to-list 'rtn org-effort-property))
  10900. (when include-columns
  10901. (save-excursion
  10902. (save-restriction
  10903. (widen)
  10904. (goto-char (point-min))
  10905. (while (re-search-forward
  10906. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10907. nil t)
  10908. (setq cfmt (match-string 2) s 0)
  10909. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10910. cfmt s)
  10911. (setq s (match-end 0)
  10912. p (match-string 1 cfmt))
  10913. (unless (or (equal p "ITEM")
  10914. (member p org-special-properties))
  10915. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10916. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10917. (defun org-property-values (key)
  10918. "Return a list of all values of property KEY."
  10919. (save-excursion
  10920. (save-restriction
  10921. (widen)
  10922. (goto-char (point-min))
  10923. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10924. values)
  10925. (while (re-search-forward re nil t)
  10926. (add-to-list 'values (org-trim (match-string 1))))
  10927. (delete "" values)))))
  10928. (defun org-insert-property-drawer ()
  10929. "Insert a property drawer into the current entry."
  10930. (interactive)
  10931. (org-back-to-heading t)
  10932. (looking-at outline-regexp)
  10933. (let ((indent (if org-adapt-indentation
  10934. (- (match-end 0)(match-beginning 0))
  10935. 0))
  10936. (beg (point))
  10937. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10938. end hiddenp)
  10939. (outline-next-heading)
  10940. (setq end (point))
  10941. (goto-char beg)
  10942. (while (re-search-forward re end t))
  10943. (setq hiddenp (org-invisible-p))
  10944. (end-of-line 1)
  10945. (and (equal (char-after) ?\n) (forward-char 1))
  10946. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10947. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10948. ;; just skip this line
  10949. (beginning-of-line 2)
  10950. ;; Drawer start, find the end
  10951. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10952. (beginning-of-line 1)))
  10953. (org-skip-over-state-notes)
  10954. (skip-chars-backward " \t\n\r")
  10955. (if (eq (char-before) ?*) (forward-char 1))
  10956. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10957. (beginning-of-line 0)
  10958. (org-indent-to-column indent)
  10959. (beginning-of-line 2)
  10960. (org-indent-to-column indent)
  10961. (beginning-of-line 0)
  10962. (if hiddenp
  10963. (save-excursion
  10964. (org-back-to-heading t)
  10965. (hide-entry))
  10966. (org-flag-drawer t))))
  10967. (defun org-set-property (property value)
  10968. "In the current entry, set PROPERTY to VALUE.
  10969. When called interactively, this will prompt for a property name, offering
  10970. completion on existing and default properties. And then it will prompt
  10971. for a value, offering completion either on allowed values (via an inherited
  10972. xxx_ALL property) or on existing values in other instances of this property
  10973. in the current file."
  10974. (interactive
  10975. (let* ((completion-ignore-case t)
  10976. (keys (org-buffer-property-keys nil t t))
  10977. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  10978. (prop (if (member prop0 keys)
  10979. prop0
  10980. (or (cdr (assoc (downcase prop0)
  10981. (mapcar (lambda (x) (cons (downcase x) x))
  10982. keys)))
  10983. prop0)))
  10984. (cur (org-entry-get nil prop))
  10985. (allowed (org-property-get-allowed-values nil prop 'table))
  10986. (existing (mapcar 'list (org-property-values prop)))
  10987. (val (if allowed
  10988. (org-completing-read "Value: " allowed nil 'req-match)
  10989. (let (org-completion-use-ido org-completion-use-iswitchb)
  10990. (org-completing-read
  10991. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10992. (concat "[" cur "]") "")
  10993. ": ")
  10994. existing nil nil "" nil cur)))))
  10995. (list prop (if (equal val "") cur val))))
  10996. (unless (equal (org-entry-get nil property) value)
  10997. (org-entry-put nil property value)))
  10998. (defun org-delete-property (property)
  10999. "In the current entry, delete PROPERTY."
  11000. (interactive
  11001. (let* ((completion-ignore-case t)
  11002. (prop (org-icompleting-read
  11003. "Property: " (org-entry-properties nil 'standard))))
  11004. (list prop)))
  11005. (message "Property %s %s" property
  11006. (if (org-entry-delete nil property)
  11007. "deleted"
  11008. "was not present in the entry")))
  11009. (defun org-delete-property-globally (property)
  11010. "Remove PROPERTY globally, from all entries."
  11011. (interactive
  11012. (let* ((completion-ignore-case t)
  11013. (prop (org-icompleting-read
  11014. "Globally remove property: "
  11015. (mapcar 'list (org-buffer-property-keys)))))
  11016. (list prop)))
  11017. (save-excursion
  11018. (save-restriction
  11019. (widen)
  11020. (goto-char (point-min))
  11021. (let ((cnt 0))
  11022. (while (re-search-forward
  11023. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11024. nil t)
  11025. (setq cnt (1+ cnt))
  11026. (replace-match ""))
  11027. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11028. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11029. (defun org-compute-property-at-point ()
  11030. "Compute the property at point.
  11031. This looks for an enclosing column format, extracts the operator and
  11032. then applies it to the property in the column format's scope."
  11033. (interactive)
  11034. (unless (org-at-property-p)
  11035. (error "Not at a property"))
  11036. (let ((prop (org-match-string-no-properties 2)))
  11037. (org-columns-get-format-and-top-level)
  11038. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11039. (error "No operator defined for property %s" prop))
  11040. (org-columns-compute prop)))
  11041. (defun org-property-get-allowed-values (pom property &optional table)
  11042. "Get allowed values for the property PROPERTY.
  11043. When TABLE is non-nil, return an alist that can directly be used for
  11044. completion."
  11045. (let (vals)
  11046. (cond
  11047. ((equal property "TODO")
  11048. (setq vals (org-with-point-at pom
  11049. (append org-todo-keywords-1 '("")))))
  11050. ((equal property "PRIORITY")
  11051. (let ((n org-lowest-priority))
  11052. (while (>= n org-highest-priority)
  11053. (push (char-to-string n) vals)
  11054. (setq n (1- n)))))
  11055. ((member property org-special-properties))
  11056. (t
  11057. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11058. (when (and vals (string-match "\\S-" vals))
  11059. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11060. (setq vals (mapcar (lambda (x)
  11061. (cond ((stringp x) x)
  11062. ((numberp x) (number-to-string x))
  11063. ((symbolp x) (symbol-name x))
  11064. (t "???")))
  11065. vals)))))
  11066. (if table (mapcar 'list vals) vals)))
  11067. (defun org-property-previous-allowed-value (&optional previous)
  11068. "Switch to the next allowed value for this property."
  11069. (interactive)
  11070. (org-property-next-allowed-value t))
  11071. (defun org-property-next-allowed-value (&optional previous)
  11072. "Switch to the next allowed value for this property."
  11073. (interactive)
  11074. (unless (org-at-property-p)
  11075. (error "Not at a property"))
  11076. (let* ((key (match-string 2))
  11077. (value (match-string 3))
  11078. (allowed (or (org-property-get-allowed-values (point) key)
  11079. (and (member value '("[ ]" "[-]" "[X]"))
  11080. '("[ ]" "[X]"))))
  11081. nval)
  11082. (unless allowed
  11083. (error "Allowed values for this property have not been defined"))
  11084. (if previous (setq allowed (reverse allowed)))
  11085. (if (member value allowed)
  11086. (setq nval (car (cdr (member value allowed)))))
  11087. (setq nval (or nval (car allowed)))
  11088. (if (equal nval value)
  11089. (error "Only one allowed value for this property"))
  11090. (org-at-property-p)
  11091. (replace-match (concat " :" key ": " nval) t t)
  11092. (org-indent-line-function)
  11093. (beginning-of-line 1)
  11094. (skip-chars-forward " \t")))
  11095. (defun org-find-entry-with-id (ident)
  11096. "Locate the entry that contains the ID property with exact value IDENT.
  11097. IDENT can be a string, a symbol or a number, this function will search for
  11098. the string representation of it.
  11099. Return the position where this entry starts, or nil if there is no such entry."
  11100. (interactive "sID: ")
  11101. (let ((id (cond
  11102. ((stringp ident) ident)
  11103. ((symbol-name ident) (symbol-name ident))
  11104. ((numberp ident) (number-to-string ident))
  11105. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11106. (case-fold-search nil))
  11107. (save-excursion
  11108. (save-restriction
  11109. (widen)
  11110. (goto-char (point-min))
  11111. (when (re-search-forward
  11112. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11113. nil t)
  11114. (org-back-to-heading t)
  11115. (point))))))
  11116. ;;;; Timestamps
  11117. (defvar org-last-changed-timestamp nil)
  11118. (defvar org-last-inserted-timestamp nil
  11119. "The last time stamp inserted with `org-insert-time-stamp'.")
  11120. (defvar org-time-was-given) ; dynamically scoped parameter
  11121. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11122. (defvar org-ts-what) ; dynamically scoped parameter
  11123. (defun org-time-stamp (arg &optional inactive)
  11124. "Prompt for a date/time and insert a time stamp.
  11125. If the user specifies a time like HH:MM, or if this command is called
  11126. with a prefix argument, the time stamp will contain date and time.
  11127. Otherwise, only the date will be included. All parts of a date not
  11128. specified by the user will be filled in from the current date/time.
  11129. So if you press just return without typing anything, the time stamp
  11130. will represent the current date/time. If there is already a timestamp
  11131. at the cursor, it will be modified."
  11132. (interactive "P")
  11133. (let* ((ts nil)
  11134. (default-time
  11135. ;; Default time is either today, or, when entering a range,
  11136. ;; the range start.
  11137. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11138. (save-excursion
  11139. (re-search-backward
  11140. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11141. (- (point) 20) t)))
  11142. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11143. (current-time)))
  11144. (default-input (and ts (org-get-compact-tod ts)))
  11145. org-time-was-given org-end-time-was-given time)
  11146. (cond
  11147. ((and (org-at-timestamp-p t)
  11148. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11149. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11150. (insert "--")
  11151. (setq time (let ((this-command this-command))
  11152. (org-read-date arg 'totime nil nil
  11153. default-time default-input)))
  11154. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11155. ((org-at-timestamp-p t)
  11156. (setq time (let ((this-command this-command))
  11157. (org-read-date arg 'totime nil nil default-time default-input)))
  11158. (when (org-at-timestamp-p t) ; just to get the match data
  11159. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11160. (replace-match "")
  11161. (setq org-last-changed-timestamp
  11162. (org-insert-time-stamp
  11163. time (or org-time-was-given arg)
  11164. inactive nil nil (list org-end-time-was-given))))
  11165. (message "Timestamp updated"))
  11166. (t
  11167. (setq time (let ((this-command this-command))
  11168. (org-read-date arg 'totime nil nil default-time default-input)))
  11169. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11170. nil nil (list org-end-time-was-given))))))
  11171. ;; FIXME: can we use this for something else, like computing time differences?
  11172. (defun org-get-compact-tod (s)
  11173. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11174. (let* ((t1 (match-string 1 s))
  11175. (h1 (string-to-number (match-string 2 s)))
  11176. (m1 (string-to-number (match-string 3 s)))
  11177. (t2 (and (match-end 4) (match-string 5 s)))
  11178. (h2 (and t2 (string-to-number (match-string 6 s))))
  11179. (m2 (and t2 (string-to-number (match-string 7 s))))
  11180. dh dm)
  11181. (if (not t2)
  11182. t1
  11183. (setq dh (- h2 h1) dm (- m2 m1))
  11184. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11185. (concat t1 "+" (number-to-string dh)
  11186. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11187. (defun org-time-stamp-inactive (&optional arg)
  11188. "Insert an inactive time stamp.
  11189. An inactive time stamp is enclosed in square brackets instead of angle
  11190. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11191. does not link to the calendar and cannot be changed with the S-cursor keys.
  11192. So these are more for recording a certain time/date."
  11193. (interactive "P")
  11194. (org-time-stamp arg 'inactive))
  11195. (defvar org-date-ovl (org-make-overlay 1 1))
  11196. (org-overlay-put org-date-ovl 'face 'org-warning)
  11197. (org-detach-overlay org-date-ovl)
  11198. (defvar org-ans1) ; dynamically scoped parameter
  11199. (defvar org-ans2) ; dynamically scoped parameter
  11200. (defvar org-plain-time-of-day-regexp) ; defined below
  11201. (defvar org-overriding-default-time nil) ; dynamically scoped
  11202. (defvar org-read-date-overlay nil)
  11203. (defvar org-dcst nil) ; dynamically scoped
  11204. (defvar org-read-date-history nil)
  11205. (defvar org-read-date-final-answer nil)
  11206. (defun org-read-date (&optional with-time to-time from-string prompt
  11207. default-time default-input)
  11208. "Read a date, possibly a time, and make things smooth for the user.
  11209. The prompt will suggest to enter an ISO date, but you can also enter anything
  11210. which will at least partially be understood by `parse-time-string'.
  11211. Unrecognized parts of the date will default to the current day, month, year,
  11212. hour and minute. If this command is called to replace a timestamp at point,
  11213. of to enter the second timestamp of a range, the default time is taken from the
  11214. existing stamp. For example,
  11215. 3-2-5 --> 2003-02-05
  11216. feb 15 --> currentyear-02-15
  11217. sep 12 9 --> 2009-09-12
  11218. 12:45 --> today 12:45
  11219. 22 sept 0:34 --> currentyear-09-22 0:34
  11220. 12 --> currentyear-currentmonth-12
  11221. Fri --> nearest Friday (today or later)
  11222. etc.
  11223. Furthermore you can specify a relative date by giving, as the *first* thing
  11224. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11225. change in days weeks, months, years.
  11226. With a single plus or minus, the date is relative to today. With a double
  11227. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11228. +4d --> four days from today
  11229. +4 --> same as above
  11230. +2w --> two weeks from today
  11231. ++5 --> five days from default date
  11232. The function understands only English month and weekday abbreviations,
  11233. but this can be configured with the variables `parse-time-months' and
  11234. `parse-time-weekdays'.
  11235. While prompting, a calendar is popped up - you can also select the
  11236. date with the mouse (button 1). The calendar shows a period of three
  11237. months. To scroll it to other months, use the keys `>' and `<'.
  11238. If you don't like the calendar, turn it off with
  11239. \(setq org-read-date-popup-calendar nil)
  11240. With optional argument TO-TIME, the date will immediately be converted
  11241. to an internal time.
  11242. With an optional argument WITH-TIME, the prompt will suggest to also
  11243. insert a time. Note that when WITH-TIME is not set, you can still
  11244. enter a time, and this function will inform the calling routine about
  11245. this change. The calling routine may then choose to change the format
  11246. used to insert the time stamp into the buffer to include the time.
  11247. With optional argument FROM-STRING, read from this string instead from
  11248. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11249. the time/date that is used for everything that is not specified by the
  11250. user."
  11251. (require 'parse-time)
  11252. (let* ((org-time-stamp-rounding-minutes
  11253. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11254. (org-dcst org-display-custom-times)
  11255. (ct (org-current-time))
  11256. (def (or org-overriding-default-time default-time ct))
  11257. (defdecode (decode-time def))
  11258. (dummy (progn
  11259. (when (< (nth 2 defdecode) org-extend-today-until)
  11260. (setcar (nthcdr 2 defdecode) -1)
  11261. (setcar (nthcdr 1 defdecode) 59)
  11262. (setq def (apply 'encode-time defdecode)
  11263. defdecode (decode-time def)))))
  11264. (calendar-frame-setup nil)
  11265. (calendar-move-hook nil)
  11266. (calendar-view-diary-initially-flag nil)
  11267. (view-diary-entries-initially nil)
  11268. (calendar-view-holidays-initially-flag nil)
  11269. (view-calendar-holidays-initially nil)
  11270. (timestr (format-time-string
  11271. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11272. (prompt (concat (if prompt (concat prompt " ") "")
  11273. (format "Date+time [%s]: " timestr)))
  11274. ans (org-ans0 "") org-ans1 org-ans2 final)
  11275. (cond
  11276. (from-string (setq ans from-string))
  11277. (org-read-date-popup-calendar
  11278. (save-excursion
  11279. (save-window-excursion
  11280. (calendar)
  11281. (calendar-forward-day (- (time-to-days def)
  11282. (calendar-absolute-from-gregorian
  11283. (calendar-current-date))))
  11284. (org-eval-in-calendar nil t)
  11285. (let* ((old-map (current-local-map))
  11286. (map (copy-keymap calendar-mode-map))
  11287. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11288. (org-defkey map (kbd "RET") 'org-calendar-select)
  11289. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11290. 'org-calendar-select-mouse)
  11291. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11292. 'org-calendar-select-mouse)
  11293. (org-defkey minibuffer-local-map [(meta shift left)]
  11294. (lambda () (interactive)
  11295. (org-eval-in-calendar '(calendar-backward-month 1))))
  11296. (org-defkey minibuffer-local-map [(meta shift right)]
  11297. (lambda () (interactive)
  11298. (org-eval-in-calendar '(calendar-forward-month 1))))
  11299. (org-defkey minibuffer-local-map [(meta shift up)]
  11300. (lambda () (interactive)
  11301. (org-eval-in-calendar '(calendar-backward-year 1))))
  11302. (org-defkey minibuffer-local-map [(meta shift down)]
  11303. (lambda () (interactive)
  11304. (org-eval-in-calendar '(calendar-forward-year 1))))
  11305. (org-defkey minibuffer-local-map [?\e (shift left)]
  11306. (lambda () (interactive)
  11307. (org-eval-in-calendar '(calendar-backward-month 1))))
  11308. (org-defkey minibuffer-local-map [?\e (shift right)]
  11309. (lambda () (interactive)
  11310. (org-eval-in-calendar '(calendar-forward-month 1))))
  11311. (org-defkey minibuffer-local-map [?\e (shift up)]
  11312. (lambda () (interactive)
  11313. (org-eval-in-calendar '(calendar-backward-year 1))))
  11314. (org-defkey minibuffer-local-map [?\e (shift down)]
  11315. (lambda () (interactive)
  11316. (org-eval-in-calendar '(calendar-forward-year 1))))
  11317. (org-defkey minibuffer-local-map [(shift up)]
  11318. (lambda () (interactive)
  11319. (org-eval-in-calendar '(calendar-backward-week 1))))
  11320. (org-defkey minibuffer-local-map [(shift down)]
  11321. (lambda () (interactive)
  11322. (org-eval-in-calendar '(calendar-forward-week 1))))
  11323. (org-defkey minibuffer-local-map [(shift left)]
  11324. (lambda () (interactive)
  11325. (org-eval-in-calendar '(calendar-backward-day 1))))
  11326. (org-defkey minibuffer-local-map [(shift right)]
  11327. (lambda () (interactive)
  11328. (org-eval-in-calendar '(calendar-forward-day 1))))
  11329. (org-defkey minibuffer-local-map ">"
  11330. (lambda () (interactive)
  11331. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11332. (org-defkey minibuffer-local-map "<"
  11333. (lambda () (interactive)
  11334. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11335. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11336. (unwind-protect
  11337. (progn
  11338. (use-local-map map)
  11339. (add-hook 'post-command-hook 'org-read-date-display)
  11340. (setq org-ans0 (read-string prompt default-input
  11341. 'org-read-date-history nil))
  11342. ;; org-ans0: from prompt
  11343. ;; org-ans1: from mouse click
  11344. ;; org-ans2: from calendar motion
  11345. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11346. (remove-hook 'post-command-hook 'org-read-date-display)
  11347. (use-local-map old-map)
  11348. (when org-read-date-overlay
  11349. (org-delete-overlay org-read-date-overlay)
  11350. (setq org-read-date-overlay nil)))))))
  11351. (t ; Naked prompt only
  11352. (unwind-protect
  11353. (setq ans (read-string prompt default-input
  11354. 'org-read-date-history timestr))
  11355. (when org-read-date-overlay
  11356. (org-delete-overlay org-read-date-overlay)
  11357. (setq org-read-date-overlay nil)))))
  11358. (setq final (org-read-date-analyze ans def defdecode))
  11359. (setq org-read-date-final-answer ans)
  11360. (if to-time
  11361. (apply 'encode-time final)
  11362. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11363. (format "%04d-%02d-%02d %02d:%02d"
  11364. (nth 5 final) (nth 4 final) (nth 3 final)
  11365. (nth 2 final) (nth 1 final))
  11366. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11367. (defvar def)
  11368. (defvar defdecode)
  11369. (defvar with-time)
  11370. (defun org-read-date-display ()
  11371. "Display the current date prompt interpretation in the minibuffer."
  11372. (when org-read-date-display-live
  11373. (when org-read-date-overlay
  11374. (org-delete-overlay org-read-date-overlay))
  11375. (let ((p (point)))
  11376. (end-of-line 1)
  11377. (while (not (equal (buffer-substring
  11378. (max (point-min) (- (point) 4)) (point))
  11379. " "))
  11380. (insert " "))
  11381. (goto-char p))
  11382. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11383. " " (or org-ans1 org-ans2)))
  11384. (org-end-time-was-given nil)
  11385. (f (org-read-date-analyze ans def defdecode))
  11386. (fmts (if org-dcst
  11387. org-time-stamp-custom-formats
  11388. org-time-stamp-formats))
  11389. (fmt (if (or with-time
  11390. (and (boundp 'org-time-was-given) org-time-was-given))
  11391. (cdr fmts)
  11392. (car fmts)))
  11393. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11394. (when (and org-end-time-was-given
  11395. (string-match org-plain-time-of-day-regexp txt))
  11396. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11397. org-end-time-was-given
  11398. (substring txt (match-end 0)))))
  11399. (setq org-read-date-overlay
  11400. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11401. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11402. (defun org-read-date-analyze (ans def defdecode)
  11403. "Analyse the combined answer of the date prompt."
  11404. ;; FIXME: cleanup and comment
  11405. (let (delta deltan deltaw deltadef year month day
  11406. hour minute second wday pm h2 m2 tl wday1
  11407. iso-year iso-weekday iso-week iso-year iso-date)
  11408. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11409. (setq ans "+0"))
  11410. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11411. (setq ans (replace-match "" t t ans)
  11412. deltan (car delta)
  11413. deltaw (nth 1 delta)
  11414. deltadef (nth 2 delta)))
  11415. ;; Check if there is an iso week date in there
  11416. ;; If yes, sore the info and postpone interpreting it until the rest
  11417. ;; of the parsing is done
  11418. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11419. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11420. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11421. iso-week (string-to-number (match-string 2 ans)))
  11422. (setq ans (replace-match "" t t ans)))
  11423. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11424. (when (string-match
  11425. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11426. (setq year (if (match-end 2)
  11427. (string-to-number (match-string 2 ans))
  11428. (string-to-number (format-time-string "%Y")))
  11429. month (string-to-number (match-string 3 ans))
  11430. day (string-to-number (match-string 4 ans)))
  11431. (if (< year 100) (setq year (+ 2000 year)))
  11432. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11433. t nil ans)))
  11434. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11435. ;; If there is a time with am/pm, and *no* time without it, we convert
  11436. ;; so that matching will be successful.
  11437. (loop for i from 1 to 2 do ; twice, for end time as well
  11438. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11439. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11440. (setq hour (string-to-number (match-string 1 ans))
  11441. minute (if (match-end 3)
  11442. (string-to-number (match-string 3 ans))
  11443. 0)
  11444. pm (equal ?p
  11445. (string-to-char (downcase (match-string 4 ans)))))
  11446. (if (and (= hour 12) (not pm))
  11447. (setq hour 0)
  11448. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11449. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11450. t t ans))))
  11451. ;; Check if a time range is given as a duration
  11452. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11453. (setq hour (string-to-number (match-string 1 ans))
  11454. h2 (+ hour (string-to-number (match-string 3 ans)))
  11455. minute (string-to-number (match-string 2 ans))
  11456. m2 (+ minute (if (match-end 5) (string-to-number
  11457. (match-string 5 ans))0)))
  11458. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11459. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11460. t t ans)))
  11461. ;; Check if there is a time range
  11462. (when (boundp 'org-end-time-was-given)
  11463. (setq org-time-was-given nil)
  11464. (when (and (string-match org-plain-time-of-day-regexp ans)
  11465. (match-end 8))
  11466. (setq org-end-time-was-given (match-string 8 ans))
  11467. (setq ans (concat (substring ans 0 (match-beginning 7))
  11468. (substring ans (match-end 7))))))
  11469. (setq tl (parse-time-string ans)
  11470. day (or (nth 3 tl) (nth 3 defdecode))
  11471. month (or (nth 4 tl)
  11472. (if (and org-read-date-prefer-future
  11473. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11474. (1+ (nth 4 defdecode))
  11475. (nth 4 defdecode)))
  11476. year (or (nth 5 tl)
  11477. (if (and org-read-date-prefer-future
  11478. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11479. (1+ (nth 5 defdecode))
  11480. (nth 5 defdecode)))
  11481. hour (or (nth 2 tl) (nth 2 defdecode))
  11482. minute (or (nth 1 tl) (nth 1 defdecode))
  11483. second (or (nth 0 tl) 0)
  11484. wday (nth 6 tl))
  11485. ;; Special date definitions below
  11486. (cond
  11487. (iso-week
  11488. ;; There was an iso week
  11489. (setq year (or iso-year year)
  11490. day (or iso-weekday wday 1)
  11491. wday nil ; to make sure that the trigger below does not match
  11492. iso-date (calendar-gregorian-from-absolute
  11493. (calendar-absolute-from-iso
  11494. (list iso-week day year))))
  11495. ; FIXME: Should we also push ISO weeks into the future?
  11496. ; (when (and org-read-date-prefer-future
  11497. ; (not iso-year)
  11498. ; (< (calendar-absolute-from-gregorian iso-date)
  11499. ; (time-to-days (current-time))))
  11500. ; (setq year (1+ year)
  11501. ; iso-date (calendar-gregorian-from-absolute
  11502. ; (calendar-absolute-from-iso
  11503. ; (list iso-week day year)))))
  11504. (setq month (car iso-date)
  11505. year (nth 2 iso-date)
  11506. day (nth 1 iso-date)))
  11507. (deltan
  11508. (unless deltadef
  11509. (let ((now (decode-time (current-time))))
  11510. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11511. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11512. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11513. ((equal deltaw "m") (setq month (+ month deltan)))
  11514. ((equal deltaw "y") (setq year (+ year deltan)))))
  11515. ((and wday (not (nth 3 tl)))
  11516. ;; Weekday was given, but no day, so pick that day in the week
  11517. ;; on or after the derived date.
  11518. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11519. (unless (equal wday wday1)
  11520. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11521. (if (and (boundp 'org-time-was-given)
  11522. (nth 2 tl))
  11523. (setq org-time-was-given t))
  11524. (if (< year 100) (setq year (+ 2000 year)))
  11525. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11526. (list second minute hour day month year)))
  11527. (defvar parse-time-weekdays)
  11528. (defun org-read-date-get-relative (s today default)
  11529. "Check string S for special relative date string.
  11530. TODAY and DEFAULT are internal times, for today and for a default.
  11531. Return shift list (N what def-flag)
  11532. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11533. N is the number of WHATs to shift.
  11534. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11535. the DEFAULT date rather than TODAY."
  11536. (when (and
  11537. (string-match
  11538. (concat
  11539. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11540. "\\([0-9]+\\)?"
  11541. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11542. "\\([ \t]\\|$\\)") s)
  11543. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11544. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11545. (string-to-char (substring (match-string 1 s) -1))
  11546. ?+))
  11547. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11548. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11549. (what (if (match-end 3) (match-string 3 s) "d"))
  11550. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11551. (date (if rel default today))
  11552. (wday (nth 6 (decode-time date)))
  11553. delta)
  11554. (if wday1
  11555. (progn
  11556. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11557. (if (= dir ?-) (setq delta (- delta 7)))
  11558. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11559. (list delta "d" rel))
  11560. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11561. (defun org-eval-in-calendar (form &optional keepdate)
  11562. "Eval FORM in the calendar window and return to current window.
  11563. Also, store the cursor date in variable org-ans2."
  11564. (let ((sf (selected-frame))
  11565. (sw (selected-window)))
  11566. (select-window (get-buffer-window "*Calendar*" t))
  11567. (eval form)
  11568. (when (and (not keepdate) (calendar-cursor-to-date))
  11569. (let* ((date (calendar-cursor-to-date))
  11570. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11571. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11572. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11573. (select-window sw)
  11574. (select-frame-set-input-focus sf)))
  11575. (defun org-calendar-select ()
  11576. "Return to `org-read-date' with the date currently selected.
  11577. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11578. (interactive)
  11579. (when (calendar-cursor-to-date)
  11580. (let* ((date (calendar-cursor-to-date))
  11581. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11582. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11583. (if (active-minibuffer-window) (exit-minibuffer))))
  11584. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11585. "Insert a date stamp for the date given by the internal TIME.
  11586. WITH-HM means, use the stamp format that includes the time of the day.
  11587. INACTIVE means use square brackets instead of angular ones, so that the
  11588. stamp will not contribute to the agenda.
  11589. PRE and POST are optional strings to be inserted before and after the
  11590. stamp.
  11591. The command returns the inserted time stamp."
  11592. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11593. stamp)
  11594. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11595. (insert-before-markers (or pre ""))
  11596. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11597. (when (listp extra)
  11598. (setq extra (car extra))
  11599. (if (and (stringp extra)
  11600. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11601. (setq extra (format "-%02d:%02d"
  11602. (string-to-number (match-string 1 extra))
  11603. (string-to-number (match-string 2 extra))))
  11604. (setq extra nil)))
  11605. (when extra
  11606. (backward-char 1)
  11607. (insert-before-markers extra)
  11608. (forward-char 1))
  11609. (insert-before-markers (or post ""))
  11610. (setq org-last-inserted-timestamp stamp)))
  11611. (defun org-toggle-time-stamp-overlays ()
  11612. "Toggle the use of custom time stamp formats."
  11613. (interactive)
  11614. (setq org-display-custom-times (not org-display-custom-times))
  11615. (unless org-display-custom-times
  11616. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11617. (while (setq p (next-single-property-change p 'display))
  11618. (if (and (get-text-property p 'display)
  11619. (eq (get-text-property p 'face) 'org-date))
  11620. (remove-text-properties
  11621. p (setq p (next-single-property-change p 'display))
  11622. '(display t))))
  11623. (set-buffer-modified-p bmp)))
  11624. (if (featurep 'xemacs)
  11625. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11626. (org-restart-font-lock)
  11627. (setq org-table-may-need-update t)
  11628. (if org-display-custom-times
  11629. (message "Time stamps are overlayed with custom format")
  11630. (message "Time stamp overlays removed")))
  11631. (defun org-display-custom-time (beg end)
  11632. "Overlay modified time stamp format over timestamp between BEG and END."
  11633. (let* ((ts (buffer-substring beg end))
  11634. t1 w1 with-hm tf time str w2 (off 0))
  11635. (save-match-data
  11636. (setq t1 (org-parse-time-string ts t))
  11637. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11638. (setq off (- (match-end 0) (match-beginning 0)))))
  11639. (setq end (- end off))
  11640. (setq w1 (- end beg)
  11641. with-hm (and (nth 1 t1) (nth 2 t1))
  11642. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11643. time (org-fix-decoded-time t1)
  11644. str (org-add-props
  11645. (format-time-string
  11646. (substring tf 1 -1) (apply 'encode-time time))
  11647. nil 'mouse-face 'highlight)
  11648. w2 (length str))
  11649. (if (not (= w2 w1))
  11650. (add-text-properties (1+ beg) (+ 2 beg)
  11651. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11652. (if (featurep 'xemacs)
  11653. (progn
  11654. (put-text-property beg end 'invisible t)
  11655. (put-text-property beg end 'end-glyph (make-glyph str)))
  11656. (put-text-property beg end 'display str))))
  11657. (defun org-translate-time (string)
  11658. "Translate all timestamps in STRING to custom format.
  11659. But do this only if the variable `org-display-custom-times' is set."
  11660. (when org-display-custom-times
  11661. (save-match-data
  11662. (let* ((start 0)
  11663. (re org-ts-regexp-both)
  11664. t1 with-hm inactive tf time str beg end)
  11665. (while (setq start (string-match re string start))
  11666. (setq beg (match-beginning 0)
  11667. end (match-end 0)
  11668. t1 (save-match-data
  11669. (org-parse-time-string (substring string beg end) t))
  11670. with-hm (and (nth 1 t1) (nth 2 t1))
  11671. inactive (equal (substring string beg (1+ beg)) "[")
  11672. tf (funcall (if with-hm 'cdr 'car)
  11673. org-time-stamp-custom-formats)
  11674. time (org-fix-decoded-time t1)
  11675. str (format-time-string
  11676. (concat
  11677. (if inactive "[" "<") (substring tf 1 -1)
  11678. (if inactive "]" ">"))
  11679. (apply 'encode-time time))
  11680. string (replace-match str t t string)
  11681. start (+ start (length str)))))))
  11682. string)
  11683. (defun org-fix-decoded-time (time)
  11684. "Set 0 instead of nil for the first 6 elements of time.
  11685. Don't touch the rest."
  11686. (let ((n 0))
  11687. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11688. (defun org-days-to-time (timestamp-string)
  11689. "Difference between TIMESTAMP-STRING and now in days."
  11690. (- (time-to-days (org-time-string-to-time timestamp-string))
  11691. (time-to-days (current-time))))
  11692. (defun org-deadline-close (timestamp-string &optional ndays)
  11693. "Is the time in TIMESTAMP-STRING close to the current date?"
  11694. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11695. (and (< (org-days-to-time timestamp-string) ndays)
  11696. (not (org-entry-is-done-p))))
  11697. (defun org-get-wdays (ts)
  11698. "Get the deadline lead time appropriate for timestring TS."
  11699. (cond
  11700. ((<= org-deadline-warning-days 0)
  11701. ;; 0 or negative, enforce this value no matter what
  11702. (- org-deadline-warning-days))
  11703. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11704. ;; lead time is specified.
  11705. (floor (* (string-to-number (match-string 1 ts))
  11706. (cdr (assoc (match-string 2 ts)
  11707. '(("d" . 1) ("w" . 7)
  11708. ("m" . 30.4) ("y" . 365.25)))))))
  11709. ;; go for the default.
  11710. (t org-deadline-warning-days)))
  11711. (defun org-calendar-select-mouse (ev)
  11712. "Return to `org-read-date' with the date currently selected.
  11713. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11714. (interactive "e")
  11715. (mouse-set-point ev)
  11716. (when (calendar-cursor-to-date)
  11717. (let* ((date (calendar-cursor-to-date))
  11718. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11719. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11720. (if (active-minibuffer-window) (exit-minibuffer))))
  11721. (defun org-check-deadlines (ndays)
  11722. "Check if there are any deadlines due or past due.
  11723. A deadline is considered due if it happens within `org-deadline-warning-days'
  11724. days from today's date. If the deadline appears in an entry marked DONE,
  11725. it is not shown. The prefix arg NDAYS can be used to test that many
  11726. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11727. (interactive "P")
  11728. (let* ((org-warn-days
  11729. (cond
  11730. ((equal ndays '(4)) 100000)
  11731. (ndays (prefix-numeric-value ndays))
  11732. (t (abs org-deadline-warning-days))))
  11733. (case-fold-search nil)
  11734. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11735. (callback
  11736. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11737. (message "%d deadlines past-due or due within %d days"
  11738. (org-occur regexp nil callback)
  11739. org-warn-days)))
  11740. (defun org-check-before-date (date)
  11741. "Check if there are deadlines or scheduled entries before DATE."
  11742. (interactive (list (org-read-date)))
  11743. (let ((case-fold-search nil)
  11744. (regexp (concat "\\<\\(" org-deadline-string
  11745. "\\|" org-scheduled-string
  11746. "\\) *<\\([^>]+\\)>"))
  11747. (callback
  11748. (lambda () (time-less-p
  11749. (org-time-string-to-time (match-string 2))
  11750. (org-time-string-to-time date)))))
  11751. (message "%d entries before %s"
  11752. (org-occur regexp nil callback) date)))
  11753. (defun org-check-after-date (date)
  11754. "Check if there are deadlines or scheduled entries after DATE."
  11755. (interactive (list (org-read-date)))
  11756. (let ((case-fold-search nil)
  11757. (regexp (concat "\\<\\(" org-deadline-string
  11758. "\\|" org-scheduled-string
  11759. "\\) *<\\([^>]+\\)>"))
  11760. (callback
  11761. (lambda () (not
  11762. (time-less-p
  11763. (org-time-string-to-time (match-string 2))
  11764. (org-time-string-to-time date))))))
  11765. (message "%d entries after %s"
  11766. (org-occur regexp nil callback) date)))
  11767. (defun org-evaluate-time-range (&optional to-buffer)
  11768. "Evaluate a time range by computing the difference between start and end.
  11769. Normally the result is just printed in the echo area, but with prefix arg
  11770. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11771. If the time range is actually in a table, the result is inserted into the
  11772. next column.
  11773. For time difference computation, a year is assumed to be exactly 365
  11774. days in order to avoid rounding problems."
  11775. (interactive "P")
  11776. (or
  11777. (org-clock-update-time-maybe)
  11778. (save-excursion
  11779. (unless (org-at-date-range-p t)
  11780. (goto-char (point-at-bol))
  11781. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11782. (if (not (org-at-date-range-p t))
  11783. (error "Not at a time-stamp range, and none found in current line")))
  11784. (let* ((ts1 (match-string 1))
  11785. (ts2 (match-string 2))
  11786. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11787. (match-end (match-end 0))
  11788. (time1 (org-time-string-to-time ts1))
  11789. (time2 (org-time-string-to-time ts2))
  11790. (t1 (time-to-seconds time1))
  11791. (t2 (time-to-seconds time2))
  11792. (diff (abs (- t2 t1)))
  11793. (negative (< (- t2 t1) 0))
  11794. ;; (ys (floor (* 365 24 60 60)))
  11795. (ds (* 24 60 60))
  11796. (hs (* 60 60))
  11797. (fy "%dy %dd %02d:%02d")
  11798. (fy1 "%dy %dd")
  11799. (fd "%dd %02d:%02d")
  11800. (fd1 "%dd")
  11801. (fh "%02d:%02d")
  11802. y d h m align)
  11803. (if havetime
  11804. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11805. y 0
  11806. d (floor (/ diff ds)) diff (mod diff ds)
  11807. h (floor (/ diff hs)) diff (mod diff hs)
  11808. m (floor (/ diff 60)))
  11809. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11810. y 0
  11811. d (floor (+ (/ diff ds) 0.5))
  11812. h 0 m 0))
  11813. (if (not to-buffer)
  11814. (message "%s" (org-make-tdiff-string y d h m))
  11815. (if (org-at-table-p)
  11816. (progn
  11817. (goto-char match-end)
  11818. (setq align t)
  11819. (and (looking-at " *|") (goto-char (match-end 0))))
  11820. (goto-char match-end))
  11821. (if (looking-at
  11822. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11823. (replace-match ""))
  11824. (if negative (insert " -"))
  11825. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11826. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11827. (insert " " (format fh h m))))
  11828. (if align (org-table-align))
  11829. (message "Time difference inserted")))))
  11830. (defun org-make-tdiff-string (y d h m)
  11831. (let ((fmt "")
  11832. (l nil))
  11833. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11834. l (push y l)))
  11835. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11836. l (push d l)))
  11837. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11838. l (push h l)))
  11839. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11840. l (push m l)))
  11841. (apply 'format fmt (nreverse l))))
  11842. (defun org-time-string-to-time (s)
  11843. (apply 'encode-time (org-parse-time-string s)))
  11844. (defun org-time-string-to-seconds (s)
  11845. (time-to-seconds (org-time-string-to-time s)))
  11846. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11847. "Convert a time stamp to an absolute day number.
  11848. If there is a specifyer for a cyclic time stamp, get the closest date to
  11849. DAYNR.
  11850. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11851. the variable date is bound by the calendar when this is called."
  11852. (cond
  11853. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11854. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11855. daynr
  11856. (+ daynr 1000)))
  11857. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11858. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11859. (time-to-days (current-time))) (match-string 0 s)
  11860. prefer show-all))
  11861. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11862. (defun org-days-to-iso-week (days)
  11863. "Return the iso week number."
  11864. (require 'cal-iso)
  11865. (car (calendar-iso-from-absolute days)))
  11866. (defun org-small-year-to-year (year)
  11867. "Convert 2-digit years into 4-digit years.
  11868. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11869. The year 2000 cannot be abbreviated. Any year larger than 99
  11870. is returned unchanged."
  11871. (if (< year 38)
  11872. (setq year (+ 2000 year))
  11873. (if (< year 100)
  11874. (setq year (+ 1900 year))))
  11875. year)
  11876. (defun org-time-from-absolute (d)
  11877. "Return the time corresponding to date D.
  11878. D may be an absolute day number, or a calendar-type list (month day year)."
  11879. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11880. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11881. (defun org-calendar-holiday ()
  11882. "List of holidays, for Diary display in Org-mode."
  11883. (require 'holidays)
  11884. (let ((hl (funcall
  11885. (if (fboundp 'calendar-check-holidays)
  11886. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11887. (if hl (mapconcat 'identity hl "; "))))
  11888. (defun org-diary-sexp-entry (sexp entry date)
  11889. "Process a SEXP diary ENTRY for DATE."
  11890. (require 'diary-lib)
  11891. (let ((result (if calendar-debug-sexp
  11892. (let ((stack-trace-on-error t))
  11893. (eval (car (read-from-string sexp))))
  11894. (condition-case nil
  11895. (eval (car (read-from-string sexp)))
  11896. (error
  11897. (beep)
  11898. (message "Bad sexp at line %d in %s: %s"
  11899. (org-current-line)
  11900. (buffer-file-name) sexp)
  11901. (sleep-for 2))))))
  11902. (cond ((stringp result) result)
  11903. ((and (consp result)
  11904. (stringp (cdr result))) (cdr result))
  11905. (result entry)
  11906. (t nil))))
  11907. (defun org-diary-to-ical-string (frombuf)
  11908. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11909. This uses the icalendar.el library."
  11910. (let* ((tmpdir (if (featurep 'xemacs)
  11911. (temp-directory)
  11912. temporary-file-directory))
  11913. (tmpfile (make-temp-name
  11914. (expand-file-name "orgics" tmpdir)))
  11915. buf rtn b e)
  11916. (save-excursion
  11917. (set-buffer frombuf)
  11918. (icalendar-export-region (point-min) (point-max) tmpfile)
  11919. (setq buf (find-buffer-visiting tmpfile))
  11920. (set-buffer buf)
  11921. (goto-char (point-min))
  11922. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11923. (setq b (match-beginning 0)))
  11924. (goto-char (point-max))
  11925. (if (re-search-backward "^END:VEVENT" nil t)
  11926. (setq e (match-end 0)))
  11927. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11928. (kill-buffer buf)
  11929. (delete-file tmpfile)
  11930. rtn))
  11931. (defun org-closest-date (start current change prefer show-all)
  11932. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11933. When PREFER is `past' return a date that is either CURRENT or past.
  11934. When PREFER is `future', return a date that is either CURRENT or future.
  11935. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11936. ;; Make the proper lists from the dates
  11937. (catch 'exit
  11938. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11939. dn dw sday cday n1 n2 n0
  11940. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11941. (setq start (org-date-to-gregorian start)
  11942. current (org-date-to-gregorian
  11943. (if show-all
  11944. current
  11945. (time-to-days (current-time))))
  11946. sday (calendar-absolute-from-gregorian start)
  11947. cday (calendar-absolute-from-gregorian current))
  11948. (if (<= cday sday) (throw 'exit sday))
  11949. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11950. (setq dn (string-to-number (match-string 1 change))
  11951. dw (cdr (assoc (match-string 2 change) a1)))
  11952. (error "Invalid change specifyer: %s" change))
  11953. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11954. (cond
  11955. ((eq dw 'day)
  11956. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11957. n2 (+ n1 dn)))
  11958. ((eq dw 'year)
  11959. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11960. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11961. (setq date1 (list m d y1)
  11962. n1 (calendar-absolute-from-gregorian date1)
  11963. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11964. n2 (calendar-absolute-from-gregorian date2)))
  11965. ((eq dw 'month)
  11966. ;; approx number of month between the two dates
  11967. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11968. ;; How often does dn fit in there?
  11969. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11970. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11971. m (+ m nm)
  11972. ny (floor (/ m 12))
  11973. y (+ y ny)
  11974. m (- m (* ny 12)))
  11975. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11976. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11977. (setq m2 (+ m dn) y2 y)
  11978. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11979. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11980. (while (<= n2 cday)
  11981. (setq n1 n2 m m2 y y2)
  11982. (setq m2 (+ m dn) y2 y)
  11983. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11984. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11985. ;; Make sure n1 is the earlier date
  11986. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11987. (if show-all
  11988. (cond
  11989. ((eq prefer 'past) n1)
  11990. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11991. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11992. (cond
  11993. ((eq prefer 'past) n1)
  11994. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11995. (t (if (= cday n1) n1 n2)))))))
  11996. (defun org-date-to-gregorian (date)
  11997. "Turn any specification of DATE into a gregorian date for the calendar."
  11998. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11999. ((and (listp date) (= (length date) 3)) date)
  12000. ((stringp date)
  12001. (setq date (org-parse-time-string date))
  12002. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12003. ((listp date)
  12004. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12005. (defun org-parse-time-string (s &optional nodefault)
  12006. "Parse the standard Org-mode time string.
  12007. This should be a lot faster than the normal `parse-time-string'.
  12008. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12009. hour and minute fields will be nil if not given."
  12010. (if (string-match org-ts-regexp0 s)
  12011. (list 0
  12012. (if (or (match-beginning 8) (not nodefault))
  12013. (string-to-number (or (match-string 8 s) "0")))
  12014. (if (or (match-beginning 7) (not nodefault))
  12015. (string-to-number (or (match-string 7 s) "0")))
  12016. (string-to-number (match-string 4 s))
  12017. (string-to-number (match-string 3 s))
  12018. (string-to-number (match-string 2 s))
  12019. nil nil nil)
  12020. (make-list 9 0)))
  12021. (defun org-timestamp-up (&optional arg)
  12022. "Increase the date item at the cursor by one.
  12023. If the cursor is on the year, change the year. If it is on the month or
  12024. the day, change that.
  12025. With prefix ARG, change by that many units."
  12026. (interactive "p")
  12027. (org-timestamp-change (prefix-numeric-value arg)))
  12028. (defun org-timestamp-down (&optional arg)
  12029. "Decrease the date item at the cursor by one.
  12030. If the cursor is on the year, change the year. If it is on the month or
  12031. the day, change that.
  12032. With prefix ARG, change by that many units."
  12033. (interactive "p")
  12034. (org-timestamp-change (- (prefix-numeric-value arg))))
  12035. (defun org-timestamp-up-day (&optional arg)
  12036. "Increase the date in the time stamp by one day.
  12037. With prefix ARG, change that many days."
  12038. (interactive "p")
  12039. (if (and (not (org-at-timestamp-p t))
  12040. (org-on-heading-p))
  12041. (org-todo 'up)
  12042. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12043. (defun org-timestamp-down-day (&optional arg)
  12044. "Decrease the date in the time stamp by one day.
  12045. With prefix ARG, change that many days."
  12046. (interactive "p")
  12047. (if (and (not (org-at-timestamp-p t))
  12048. (org-on-heading-p))
  12049. (org-todo 'down)
  12050. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12051. (defun org-at-timestamp-p (&optional inactive-ok)
  12052. "Determine if the cursor is in or at a timestamp."
  12053. (interactive)
  12054. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12055. (pos (point))
  12056. (ans (or (looking-at tsr)
  12057. (save-excursion
  12058. (skip-chars-backward "^[<\n\r\t")
  12059. (if (> (point) (point-min)) (backward-char 1))
  12060. (and (looking-at tsr)
  12061. (> (- (match-end 0) pos) -1))))))
  12062. (and ans
  12063. (boundp 'org-ts-what)
  12064. (setq org-ts-what
  12065. (cond
  12066. ((= pos (match-beginning 0)) 'bracket)
  12067. ((= pos (1- (match-end 0))) 'bracket)
  12068. ((org-pos-in-match-range pos 2) 'year)
  12069. ((org-pos-in-match-range pos 3) 'month)
  12070. ((org-pos-in-match-range pos 7) 'hour)
  12071. ((org-pos-in-match-range pos 8) 'minute)
  12072. ((or (org-pos-in-match-range pos 4)
  12073. (org-pos-in-match-range pos 5)) 'day)
  12074. ((and (> pos (or (match-end 8) (match-end 5)))
  12075. (< pos (match-end 0)))
  12076. (- pos (or (match-end 8) (match-end 5))))
  12077. (t 'day))))
  12078. ans))
  12079. (defun org-toggle-timestamp-type ()
  12080. "Toggle the type (<active> or [inactive]) of a time stamp."
  12081. (interactive)
  12082. (when (org-at-timestamp-p t)
  12083. (let ((beg (match-beginning 0)) (end (match-end 0))
  12084. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12085. (save-excursion
  12086. (goto-char beg)
  12087. (while (re-search-forward "[][<>]" end t)
  12088. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12089. t t)))
  12090. (message "Timestamp is now %sactive"
  12091. (if (equal (char-after beg) ?<) "" "in")))))
  12092. (defun org-timestamp-change (n &optional what)
  12093. "Change the date in the time stamp at point.
  12094. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12095. `year', `minute', `second'. If WHAT is not given, the cursor position
  12096. in the timestamp determines what will be changed."
  12097. (let ((pos (point))
  12098. with-hm inactive
  12099. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12100. org-ts-what
  12101. extra rem
  12102. ts time time0)
  12103. (if (not (org-at-timestamp-p t))
  12104. (error "Not at a timestamp"))
  12105. (if (and (not what) (eq org-ts-what 'bracket))
  12106. (org-toggle-timestamp-type)
  12107. (if (and (not what) (not (eq org-ts-what 'day))
  12108. org-display-custom-times
  12109. (get-text-property (point) 'display)
  12110. (not (get-text-property (1- (point)) 'display)))
  12111. (setq org-ts-what 'day))
  12112. (setq org-ts-what (or what org-ts-what)
  12113. inactive (= (char-after (match-beginning 0)) ?\[)
  12114. ts (match-string 0))
  12115. (replace-match "")
  12116. (if (string-match
  12117. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12118. ts)
  12119. (setq extra (match-string 1 ts)))
  12120. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12121. (setq with-hm t))
  12122. (setq time0 (org-parse-time-string ts))
  12123. (when (and (eq org-ts-what 'minute)
  12124. (eq current-prefix-arg nil))
  12125. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12126. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12127. (setcar (cdr time0) (+ (nth 1 time0)
  12128. (if (> n 0) (- rem) (- dm rem))))))
  12129. (setq time
  12130. (encode-time (or (car time0) 0)
  12131. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12132. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12133. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12134. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12135. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12136. (nthcdr 6 time0)))
  12137. (when (and (member org-ts-what '(hour minute))
  12138. extra
  12139. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12140. (setq extra (org-modify-ts-extra
  12141. extra
  12142. (if (eq org-ts-what 'hour) 2 5)
  12143. n dm)))
  12144. (when (integerp org-ts-what)
  12145. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12146. (if (eq what 'calendar)
  12147. (let ((cal-date (org-get-date-from-calendar)))
  12148. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12149. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12150. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12151. (setcar time0 (or (car time0) 0))
  12152. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12153. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12154. (setq time (apply 'encode-time time0))))
  12155. (setq org-last-changed-timestamp
  12156. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12157. (org-clock-update-time-maybe)
  12158. (goto-char pos)
  12159. ;; Try to recenter the calendar window, if any
  12160. (if (and org-calendar-follow-timestamp-change
  12161. (get-buffer-window "*Calendar*" t)
  12162. (memq org-ts-what '(day month year)))
  12163. (org-recenter-calendar (time-to-days time))))))
  12164. (defun org-modify-ts-extra (s pos n dm)
  12165. "Change the different parts of the lead-time and repeat fields in timestamp."
  12166. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12167. ng h m new rem)
  12168. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12169. (cond
  12170. ((or (org-pos-in-match-range pos 2)
  12171. (org-pos-in-match-range pos 3))
  12172. (setq m (string-to-number (match-string 3 s))
  12173. h (string-to-number (match-string 2 s)))
  12174. (if (org-pos-in-match-range pos 2)
  12175. (setq h (+ h n))
  12176. (setq n (* dm (org-no-warnings (signum n))))
  12177. (when (not (= 0 (setq rem (% m dm))))
  12178. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12179. (setq m (+ m n)))
  12180. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12181. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12182. (setq h (min 24 (max 0 h)))
  12183. (setq ng 1 new (format "-%02d:%02d" h m)))
  12184. ((org-pos-in-match-range pos 6)
  12185. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12186. ((org-pos-in-match-range pos 5)
  12187. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12188. ((org-pos-in-match-range pos 9)
  12189. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12190. ((org-pos-in-match-range pos 8)
  12191. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12192. (when ng
  12193. (setq s (concat
  12194. (substring s 0 (match-beginning ng))
  12195. new
  12196. (substring s (match-end ng))))))
  12197. s))
  12198. (defun org-recenter-calendar (date)
  12199. "If the calendar is visible, recenter it to DATE."
  12200. (let* ((win (selected-window))
  12201. (cwin (get-buffer-window "*Calendar*" t))
  12202. (calendar-move-hook nil))
  12203. (when cwin
  12204. (select-window cwin)
  12205. (calendar-goto-date (if (listp date) date
  12206. (calendar-gregorian-from-absolute date)))
  12207. (select-window win))))
  12208. (defun org-goto-calendar (&optional arg)
  12209. "Go to the Emacs calendar at the current date.
  12210. If there is a time stamp in the current line, go to that date.
  12211. A prefix ARG can be used to force the current date."
  12212. (interactive "P")
  12213. (let ((tsr org-ts-regexp) diff
  12214. (calendar-move-hook nil)
  12215. (calendar-view-holidays-initially-flag nil)
  12216. (view-calendar-holidays-initially nil)
  12217. (calendar-view-diary-initially-flag nil)
  12218. (view-diary-entries-initially nil))
  12219. (if (or (org-at-timestamp-p)
  12220. (save-excursion
  12221. (beginning-of-line 1)
  12222. (looking-at (concat ".*" tsr))))
  12223. (let ((d1 (time-to-days (current-time)))
  12224. (d2 (time-to-days
  12225. (org-time-string-to-time (match-string 1)))))
  12226. (setq diff (- d2 d1))))
  12227. (calendar)
  12228. (calendar-goto-today)
  12229. (if (and diff (not arg)) (calendar-forward-day diff))))
  12230. (defun org-get-date-from-calendar ()
  12231. "Return a list (month day year) of date at point in calendar."
  12232. (with-current-buffer "*Calendar*"
  12233. (save-match-data
  12234. (calendar-cursor-to-date))))
  12235. (defun org-date-from-calendar ()
  12236. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12237. If there is already a time stamp at the cursor position, update it."
  12238. (interactive)
  12239. (if (org-at-timestamp-p t)
  12240. (org-timestamp-change 0 'calendar)
  12241. (let ((cal-date (org-get-date-from-calendar)))
  12242. (org-insert-time-stamp
  12243. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12244. (defun org-minutes-to-hh:mm-string (m)
  12245. "Compute H:MM from a number of minutes."
  12246. (let ((h (/ m 60)))
  12247. (setq m (- m (* 60 h)))
  12248. (format org-time-clocksum-format h m)))
  12249. (defun org-hh:mm-string-to-minutes (s)
  12250. "Convert a string H:MM to a number of minutes.
  12251. If the string is just a number, interprete it as minutes.
  12252. In fact, the first hh:mm or number in the string will be taken,
  12253. there can be extra stuff in the string.
  12254. If no number is found, the return value is 0."
  12255. (cond
  12256. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12257. (+ (* (string-to-number (match-string 1 s)) 60)
  12258. (string-to-number (match-string 2 s))))
  12259. ((string-match "\\([0-9]+\\)" s)
  12260. (string-to-number (match-string 1 s)))
  12261. (t 0)))
  12262. ;;;; Files
  12263. (defun org-save-all-org-buffers ()
  12264. "Save all Org-mode buffers without user confirmation."
  12265. (interactive)
  12266. (message "Saving all Org-mode buffers...")
  12267. (save-some-buffers t 'org-mode-p)
  12268. (when (featurep 'org-id) (org-id-locations-save))
  12269. (message "Saving all Org-mode buffers... done"))
  12270. (defun org-revert-all-org-buffers ()
  12271. "Revert all Org-mode buffers.
  12272. Prompt for confirmation when there are unsaved changes.
  12273. Be sure you know what you are doing before letting this function
  12274. overwrite your changes.
  12275. This function is useful in a setup where one tracks org files
  12276. with a version control system, to revert on one machine after pulling
  12277. changes from another. I believe the procedure must be like this:
  12278. 1. M-x org-save-all-org-buffers
  12279. 2. Pull changes from the other machine, resolve conflicts
  12280. 3. M-x org-revert-all-org-buffers"
  12281. (interactive)
  12282. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12283. (error "Abort"))
  12284. (save-excursion
  12285. (save-window-excursion
  12286. (mapc
  12287. (lambda (b)
  12288. (when (and (with-current-buffer b (org-mode-p))
  12289. (with-current-buffer b buffer-file-name))
  12290. (switch-to-buffer b)
  12291. (revert-buffer t 'no-confirm)))
  12292. (buffer-list))
  12293. (when (and (featurep 'org-id) org-id-track-globally)
  12294. (org-id-locations-load)))))
  12295. ;;;; Agenda files
  12296. ;;;###autoload
  12297. (defun org-iswitchb (&optional arg)
  12298. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12299. With a prefix argument, restrict available to files.
  12300. With two prefix arguments, restrict available buffers to agenda files.
  12301. Due to some yet unresolved reason, the global function
  12302. `iswitchb-mode' needs to be active for this function to work."
  12303. (interactive "P")
  12304. (require 'iswitchb)
  12305. (let ((enabled iswitchb-mode) blist)
  12306. (or enabled (iswitchb-mode 1))
  12307. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12308. ((equal arg '(16)) (org-buffer-list 'agenda))
  12309. (t (org-buffer-list))))
  12310. (unwind-protect
  12311. (let ((iswitchb-make-buflist-hook
  12312. (lambda ()
  12313. (setq iswitchb-temp-buflist
  12314. (mapcar 'buffer-name blist)))))
  12315. (switch-to-buffer
  12316. (iswitchb-read-buffer
  12317. "Switch-to: " nil t)))
  12318. (or enabled (iswitchb-mode -1)))))
  12319. ;;;###autoload
  12320. (defun org-iswitchb (&optional arg)
  12321. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12322. With a prefix argument, restrict available to files.
  12323. With two prefix arguments, restrict available buffers to agenda files."
  12324. (interactive "P")
  12325. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12326. ((equal arg '(16)) (org-buffer-list 'agenda))
  12327. (t (org-buffer-list)))))
  12328. (switch-to-buffer
  12329. (org-icompleting-read "Org buffer: "
  12330. (mapcar 'list (mapcar 'buffer-name blist))
  12331. nil t))))
  12332. ;;;###autoload
  12333. (defalias 'org-ido-switchb 'org-iswitchb)
  12334. (defun org-buffer-list (&optional predicate exclude-tmp)
  12335. "Return a list of Org buffers.
  12336. PREDICATE can be `export', `files' or `agenda'.
  12337. export restrict the list to Export buffers.
  12338. files restrict the list to buffers visiting Org files.
  12339. agenda restrict the list to buffers visiting agenda files.
  12340. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12341. (let* ((bfn nil)
  12342. (agenda-files (and (eq predicate 'agenda)
  12343. (mapcar 'file-truename (org-agenda-files t))))
  12344. (filter
  12345. (cond
  12346. ((eq predicate 'files)
  12347. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12348. ((eq predicate 'export)
  12349. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12350. ((eq predicate 'agenda)
  12351. (lambda (b)
  12352. (with-current-buffer b
  12353. (and (eq major-mode 'org-mode)
  12354. (setq bfn (buffer-file-name b))
  12355. (member (file-truename bfn) agenda-files)))))
  12356. (t (lambda (b) (with-current-buffer b
  12357. (or (eq major-mode 'org-mode)
  12358. (string-match "\*Org .*Export"
  12359. (buffer-name b)))))))))
  12360. (delq nil
  12361. (mapcar
  12362. (lambda(b)
  12363. (if (and (funcall filter b)
  12364. (or (not exclude-tmp)
  12365. (not (string-match "tmp" (buffer-name b)))))
  12366. b
  12367. nil))
  12368. (buffer-list)))))
  12369. (defun org-agenda-files (&optional unrestricted archives)
  12370. "Get the list of agenda files.
  12371. Optional UNRESTRICTED means return the full list even if a restriction
  12372. is currently in place.
  12373. When ARCHIVES is t, include all archive files hat are really being
  12374. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12375. `org-agenda-archives-mode' is t."
  12376. (let ((files
  12377. (cond
  12378. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12379. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12380. ((listp org-agenda-files) org-agenda-files)
  12381. (t (error "Invalid value of `org-agenda-files'")))))
  12382. (setq files (apply 'append
  12383. (mapcar (lambda (f)
  12384. (if (file-directory-p f)
  12385. (directory-files
  12386. f t org-agenda-file-regexp)
  12387. (list f)))
  12388. files)))
  12389. (when org-agenda-skip-unavailable-files
  12390. (setq files (delq nil
  12391. (mapcar (function
  12392. (lambda (file)
  12393. (and (file-readable-p file) file)))
  12394. files))))
  12395. (when (or (eq archives t)
  12396. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12397. (setq files (org-add-archive-files files)))
  12398. files))
  12399. (defun org-edit-agenda-file-list ()
  12400. "Edit the list of agenda files.
  12401. Depending on setup, this either uses customize to edit the variable
  12402. `org-agenda-files', or it visits the file that is holding the list. In the
  12403. latter case, the buffer is set up in a way that saving it automatically kills
  12404. the buffer and restores the previous window configuration."
  12405. (interactive)
  12406. (if (stringp org-agenda-files)
  12407. (let ((cw (current-window-configuration)))
  12408. (find-file org-agenda-files)
  12409. (org-set-local 'org-window-configuration cw)
  12410. (org-add-hook 'after-save-hook
  12411. (lambda ()
  12412. (set-window-configuration
  12413. (prog1 org-window-configuration
  12414. (kill-buffer (current-buffer))))
  12415. (org-install-agenda-files-menu)
  12416. (message "New agenda file list installed"))
  12417. nil 'local)
  12418. (message "%s" (substitute-command-keys
  12419. "Edit list and finish with \\[save-buffer]")))
  12420. (customize-variable 'org-agenda-files)))
  12421. (defun org-store-new-agenda-file-list (list)
  12422. "Set new value for the agenda file list and save it correctly."
  12423. (if (stringp org-agenda-files)
  12424. (let ((f org-agenda-files) b)
  12425. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12426. (with-temp-file f
  12427. (insert (mapconcat 'identity list "\n") "\n")))
  12428. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12429. (setq org-agenda-files list)
  12430. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12431. (defun org-read-agenda-file-list ()
  12432. "Read the list of agenda files from a file."
  12433. (when (file-directory-p org-agenda-files)
  12434. (error "`org-agenda-files' cannot be a single directory"))
  12435. (when (stringp org-agenda-files)
  12436. (with-temp-buffer
  12437. (insert-file-contents org-agenda-files)
  12438. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12439. ;;;###autoload
  12440. (defun org-cycle-agenda-files ()
  12441. "Cycle through the files in `org-agenda-files'.
  12442. If the current buffer visits an agenda file, find the next one in the list.
  12443. If the current buffer does not, find the first agenda file."
  12444. (interactive)
  12445. (let* ((fs (org-agenda-files t))
  12446. (files (append fs (list (car fs))))
  12447. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12448. file)
  12449. (unless files (error "No agenda files"))
  12450. (catch 'exit
  12451. (while (setq file (pop files))
  12452. (if (equal (file-truename file) tcf)
  12453. (when (car files)
  12454. (find-file (car files))
  12455. (throw 'exit t))))
  12456. (find-file (car fs)))
  12457. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12458. (defun org-agenda-file-to-front (&optional to-end)
  12459. "Move/add the current file to the top of the agenda file list.
  12460. If the file is not present in the list, it is added to the front. If it is
  12461. present, it is moved there. With optional argument TO-END, add/move to the
  12462. end of the list."
  12463. (interactive "P")
  12464. (let ((org-agenda-skip-unavailable-files nil)
  12465. (file-alist (mapcar (lambda (x)
  12466. (cons (file-truename x) x))
  12467. (org-agenda-files t)))
  12468. (ctf (file-truename buffer-file-name))
  12469. x had)
  12470. (setq x (assoc ctf file-alist) had x)
  12471. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12472. (if to-end
  12473. (setq file-alist (append (delq x file-alist) (list x)))
  12474. (setq file-alist (cons x (delq x file-alist))))
  12475. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12476. (org-install-agenda-files-menu)
  12477. (message "File %s to %s of agenda file list"
  12478. (if had "moved" "added") (if to-end "end" "front"))))
  12479. (defun org-remove-file (&optional file)
  12480. "Remove current file from the list of files in variable `org-agenda-files'.
  12481. These are the files which are being checked for agenda entries.
  12482. Optional argument FILE means, use this file instead of the current."
  12483. (interactive)
  12484. (let* ((org-agenda-skip-unavailable-files nil)
  12485. (file (or file buffer-file-name))
  12486. (true-file (file-truename file))
  12487. (afile (abbreviate-file-name file))
  12488. (files (delq nil (mapcar
  12489. (lambda (x)
  12490. (if (equal true-file
  12491. (file-truename x))
  12492. nil x))
  12493. (org-agenda-files t)))))
  12494. (if (not (= (length files) (length (org-agenda-files t))))
  12495. (progn
  12496. (org-store-new-agenda-file-list files)
  12497. (org-install-agenda-files-menu)
  12498. (message "Removed file: %s" afile))
  12499. (message "File was not in list: %s (not removed)" afile))))
  12500. (defun org-file-menu-entry (file)
  12501. (vector file (list 'find-file file) t))
  12502. (defun org-check-agenda-file (file)
  12503. "Make sure FILE exists. If not, ask user what to do."
  12504. (when (not (file-exists-p file))
  12505. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12506. (abbreviate-file-name file))
  12507. (let ((r (downcase (read-char-exclusive))))
  12508. (cond
  12509. ((equal r ?r)
  12510. (org-remove-file file)
  12511. (throw 'nextfile t))
  12512. (t (error "Abort"))))))
  12513. (defun org-get-agenda-file-buffer (file)
  12514. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12515. it to the list of buffers which might be released later."
  12516. (let ((buf (org-find-base-buffer-visiting file)))
  12517. (if buf
  12518. buf ; just return it
  12519. ;; Make a new buffer and remember it
  12520. (setq buf (find-file-noselect file))
  12521. (if buf (push buf org-agenda-new-buffers))
  12522. buf)))
  12523. (defun org-release-buffers (blist)
  12524. "Release all buffers in list, asking the user for confirmation when needed.
  12525. When a buffer is unmodified, it is just killed. When modified, it is saved
  12526. \(if the user agrees) and then killed."
  12527. (let (buf file)
  12528. (while (setq buf (pop blist))
  12529. (setq file (buffer-file-name buf))
  12530. (when (and (buffer-modified-p buf)
  12531. file
  12532. (y-or-n-p (format "Save file %s? " file)))
  12533. (with-current-buffer buf (save-buffer)))
  12534. (kill-buffer buf))))
  12535. (defun org-prepare-agenda-buffers (files)
  12536. "Create buffers for all agenda files, protect archived trees and comments."
  12537. (interactive)
  12538. (let ((pa '(:org-archived t))
  12539. (pc '(:org-comment t))
  12540. (pall '(:org-archived t :org-comment t))
  12541. (inhibit-read-only t)
  12542. (rea (concat ":" org-archive-tag ":"))
  12543. bmp file re)
  12544. (save-excursion
  12545. (save-restriction
  12546. (while (setq file (pop files))
  12547. (catch 'nextfile
  12548. (if (bufferp file)
  12549. (set-buffer file)
  12550. (org-check-agenda-file file)
  12551. (set-buffer (org-get-agenda-file-buffer file)))
  12552. (widen)
  12553. (setq bmp (buffer-modified-p))
  12554. (org-refresh-category-properties)
  12555. (setq org-todo-keywords-for-agenda
  12556. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12557. (setq org-done-keywords-for-agenda
  12558. (append org-done-keywords-for-agenda org-done-keywords))
  12559. (setq org-todo-keyword-alist-for-agenda
  12560. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12561. (setq org-tag-alist-for-agenda
  12562. (append org-tag-alist-for-agenda org-tag-alist))
  12563. (save-excursion
  12564. (remove-text-properties (point-min) (point-max) pall)
  12565. (when org-agenda-skip-archived-trees
  12566. (goto-char (point-min))
  12567. (while (re-search-forward rea nil t)
  12568. (if (org-on-heading-p t)
  12569. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12570. (goto-char (point-min))
  12571. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12572. (while (re-search-forward re nil t)
  12573. (add-text-properties
  12574. (match-beginning 0) (org-end-of-subtree t) pc)))
  12575. (set-buffer-modified-p bmp)))))
  12576. (setq org-todo-keyword-alist-for-agenda
  12577. (org-uniquify org-todo-keyword-alist-for-agenda)
  12578. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12579. ;;;; Embedded LaTeX
  12580. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12581. "Keymap for the minor `org-cdlatex-mode'.")
  12582. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12583. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12584. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12585. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12586. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12587. (defvar org-cdlatex-texmathp-advice-is-done nil
  12588. "Flag remembering if we have applied the advice to texmathp already.")
  12589. (define-minor-mode org-cdlatex-mode
  12590. "Toggle the minor `org-cdlatex-mode'.
  12591. This mode supports entering LaTeX environment and math in LaTeX fragments
  12592. in Org-mode.
  12593. \\{org-cdlatex-mode-map}"
  12594. nil " OCDL" nil
  12595. (when org-cdlatex-mode (require 'cdlatex))
  12596. (unless org-cdlatex-texmathp-advice-is-done
  12597. (setq org-cdlatex-texmathp-advice-is-done t)
  12598. (defadvice texmathp (around org-math-always-on activate)
  12599. "Always return t in org-mode buffers.
  12600. This is because we want to insert math symbols without dollars even outside
  12601. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12602. an embedded LaTeX fragment, let texmathp do its job.
  12603. \\[org-cdlatex-mode-map]"
  12604. (interactive)
  12605. (let (p)
  12606. (cond
  12607. ((not (org-mode-p)) ad-do-it)
  12608. ((eq this-command 'cdlatex-math-symbol)
  12609. (setq ad-return-value t
  12610. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12611. (t
  12612. (let ((p (org-inside-LaTeX-fragment-p)))
  12613. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12614. (setq ad-return-value t
  12615. texmathp-why '("Org-mode embedded math" . 0))
  12616. (if p ad-do-it)))))))))
  12617. (defun turn-on-org-cdlatex ()
  12618. "Unconditionally turn on `org-cdlatex-mode'."
  12619. (org-cdlatex-mode 1))
  12620. (defun org-inside-LaTeX-fragment-p ()
  12621. "Test if point is inside a LaTeX fragment.
  12622. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12623. sequence appearing also before point.
  12624. Even though the matchers for math are configurable, this function assumes
  12625. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12626. delimiters are skipped when they have been removed by customization.
  12627. The return value is nil, or a cons cell with the delimiter and
  12628. and the position of this delimiter.
  12629. This function does a reasonably good job, but can locally be fooled by
  12630. for example currency specifications. For example it will assume being in
  12631. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12632. fragments that are properly closed, but during editing, we have to live
  12633. with the uncertainty caused by missing closing delimiters. This function
  12634. looks only before point, not after."
  12635. (catch 'exit
  12636. (let ((pos (point))
  12637. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12638. (lim (progn
  12639. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12640. (point)))
  12641. dd-on str (start 0) m re)
  12642. (goto-char pos)
  12643. (when dodollar
  12644. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12645. re (nth 1 (assoc "$" org-latex-regexps)))
  12646. (while (string-match re str start)
  12647. (cond
  12648. ((= (match-end 0) (length str))
  12649. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12650. ((= (match-end 0) (- (length str) 5))
  12651. (throw 'exit nil))
  12652. (t (setq start (match-end 0))))))
  12653. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12654. (goto-char pos)
  12655. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12656. (and (match-beginning 2) (throw 'exit nil))
  12657. ;; count $$
  12658. (while (re-search-backward "\\$\\$" lim t)
  12659. (setq dd-on (not dd-on)))
  12660. (goto-char pos)
  12661. (if dd-on (cons "$$" m))))))
  12662. (defun org-try-cdlatex-tab ()
  12663. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12664. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12665. - inside a LaTeX fragment, or
  12666. - after the first word in a line, where an abbreviation expansion could
  12667. insert a LaTeX environment."
  12668. (when org-cdlatex-mode
  12669. (cond
  12670. ((save-excursion
  12671. (skip-chars-backward "a-zA-Z0-9*")
  12672. (skip-chars-backward " \t")
  12673. (bolp))
  12674. (cdlatex-tab) t)
  12675. ((org-inside-LaTeX-fragment-p)
  12676. (cdlatex-tab) t)
  12677. (t nil))))
  12678. (defun org-cdlatex-underscore-caret (&optional arg)
  12679. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12680. Revert to the normal definition outside of these fragments."
  12681. (interactive "P")
  12682. (if (org-inside-LaTeX-fragment-p)
  12683. (call-interactively 'cdlatex-sub-superscript)
  12684. (let (org-cdlatex-mode)
  12685. (call-interactively (key-binding (vector last-input-event))))))
  12686. (defun org-cdlatex-math-modify (&optional arg)
  12687. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12688. Revert to the normal definition outside of these fragments."
  12689. (interactive "P")
  12690. (if (org-inside-LaTeX-fragment-p)
  12691. (call-interactively 'cdlatex-math-modify)
  12692. (let (org-cdlatex-mode)
  12693. (call-interactively (key-binding (vector last-input-event))))))
  12694. (defvar org-latex-fragment-image-overlays nil
  12695. "List of overlays carrying the images of latex fragments.")
  12696. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12697. (defun org-remove-latex-fragment-image-overlays ()
  12698. "Remove all overlays with LaTeX fragment images in current buffer."
  12699. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12700. (setq org-latex-fragment-image-overlays nil))
  12701. (defun org-preview-latex-fragment (&optional subtree)
  12702. "Preview the LaTeX fragment at point, or all locally or globally.
  12703. If the cursor is in a LaTeX fragment, create the image and overlay
  12704. it over the source code. If there is no fragment at point, display
  12705. all fragments in the current text, from one headline to the next. With
  12706. prefix SUBTREE, display all fragments in the current subtree. With a
  12707. double prefix `C-u C-u', or when the cursor is before the first headline,
  12708. display all fragments in the buffer.
  12709. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12710. (interactive "P")
  12711. (org-remove-latex-fragment-image-overlays)
  12712. (save-excursion
  12713. (save-restriction
  12714. (let (beg end at msg)
  12715. (cond
  12716. ((or (equal subtree '(16))
  12717. (not (save-excursion
  12718. (re-search-backward (concat "^" outline-regexp) nil t))))
  12719. (setq beg (point-min) end (point-max)
  12720. msg "Creating images for buffer...%s"))
  12721. ((equal subtree '(4))
  12722. (org-back-to-heading)
  12723. (setq beg (point) end (org-end-of-subtree t)
  12724. msg "Creating images for subtree...%s"))
  12725. (t
  12726. (if (setq at (org-inside-LaTeX-fragment-p))
  12727. (goto-char (max (point-min) (- (cdr at) 2)))
  12728. (org-back-to-heading))
  12729. (setq beg (point) end (progn (outline-next-heading) (point))
  12730. msg (if at "Creating image...%s"
  12731. "Creating images for entry...%s"))))
  12732. (message msg "")
  12733. (narrow-to-region beg end)
  12734. (goto-char beg)
  12735. (org-format-latex
  12736. (concat "ltxpng/" (file-name-sans-extension
  12737. (file-name-nondirectory
  12738. buffer-file-name)))
  12739. default-directory 'overlays msg at 'forbuffer)
  12740. (message msg "done. Use `C-c C-c' to remove images.")))))
  12741. (defvar org-latex-regexps
  12742. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12743. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12744. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12745. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12746. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12747. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12748. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12749. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12750. "Regular expressions for matching embedded LaTeX.")
  12751. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12752. "Replace LaTeX fragments with links to an image, and produce images."
  12753. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12754. (let* ((prefixnodir (file-name-nondirectory prefix))
  12755. (absprefix (expand-file-name prefix dir))
  12756. (todir (file-name-directory absprefix))
  12757. (opt org-format-latex-options)
  12758. (matchers (plist-get opt :matchers))
  12759. (re-list org-latex-regexps)
  12760. (cnt 0) txt link beg end re e checkdir
  12761. executables-checked
  12762. m n block linkfile movefile ov)
  12763. ;; Check if there are old images files with this prefix, and remove them
  12764. (when (file-directory-p todir)
  12765. (mapc 'delete-file
  12766. (directory-files
  12767. todir 'full
  12768. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12769. ;; Check the different regular expressions
  12770. (while (setq e (pop re-list))
  12771. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12772. block (if (nth 3 e) "\n\n" ""))
  12773. (when (member m matchers)
  12774. (goto-char (point-min))
  12775. (while (re-search-forward re nil t)
  12776. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12777. (not (get-text-property (match-beginning n)
  12778. 'org-protected))
  12779. (or (not overlays)
  12780. (not (eq (get-char-property (match-beginning n)
  12781. 'org-overlay-type)
  12782. 'org-latex-overlay))))
  12783. (setq txt (match-string n)
  12784. beg (match-beginning n) end (match-end n)
  12785. cnt (1+ cnt)
  12786. linkfile (format "%s_%04d.png" prefix cnt)
  12787. movefile (format "%s_%04d.png" absprefix cnt)
  12788. link (concat block "[[file:" linkfile "]]" block))
  12789. (if msg (message msg cnt))
  12790. (goto-char beg)
  12791. (unless checkdir ; make sure the directory exists
  12792. (setq checkdir t)
  12793. (or (file-directory-p todir) (make-directory todir)))
  12794. (unless executables-checked
  12795. (org-check-external-command
  12796. "latex" "needed to convert LaTeX fragments to images")
  12797. (org-check-external-command
  12798. "dvipng" "needed to convert LaTeX fragments to images")
  12799. (setq executables-checked t))
  12800. (org-create-formula-image
  12801. txt movefile opt forbuffer)
  12802. (if overlays
  12803. (progn
  12804. (mapc (lambda (o)
  12805. (if (eq (org-overlay-get o 'org-overlay-type)
  12806. 'org-latex-overlay)
  12807. (org-delete-overlay o)))
  12808. (org-overlays-in beg end))
  12809. (setq ov (org-make-overlay beg end))
  12810. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12811. (if (featurep 'xemacs)
  12812. (progn
  12813. (org-overlay-put ov 'invisible t)
  12814. (org-overlay-put
  12815. ov 'end-glyph
  12816. (make-glyph (vector 'png :file movefile))))
  12817. (org-overlay-put
  12818. ov 'display
  12819. (list 'image :type 'png :file movefile :ascent 'center)))
  12820. (push ov org-latex-fragment-image-overlays)
  12821. (goto-char end))
  12822. (delete-region beg end)
  12823. (insert link))))))))
  12824. ;; This function borrows from Ganesh Swami's latex2png.el
  12825. (defun org-create-formula-image (string tofile options buffer)
  12826. (require 'org-latex)
  12827. (let* ((tmpdir (if (featurep 'xemacs)
  12828. (temp-directory)
  12829. temporary-file-directory))
  12830. (texfilebase (make-temp-name
  12831. (expand-file-name "orgtex" tmpdir)))
  12832. (texfile (concat texfilebase ".tex"))
  12833. (dvifile (concat texfilebase ".dvi"))
  12834. (pngfile (concat texfilebase ".png"))
  12835. (fnh (if (featurep 'xemacs)
  12836. (font-height (get-face-font 'default))
  12837. (face-attribute 'default :height nil)))
  12838. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12839. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12840. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12841. "Black"))
  12842. (bg (or (plist-get options (if buffer :background :html-background))
  12843. "Transparent")))
  12844. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12845. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12846. (with-temp-file texfile
  12847. (insert org-format-latex-header
  12848. (if org-export-latex-packages-alist
  12849. (concat "\n"
  12850. (mapconcat (lambda(p)
  12851. (if (equal "" (car p))
  12852. (format "\\usepackage{%s}" (cadr p))
  12853. (format "\\usepackage[%s]{%s}"
  12854. (car p) (cadr p))))
  12855. org-export-latex-packages-alist "\n"))
  12856. "")
  12857. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12858. (debug)
  12859. (let ((dir default-directory))
  12860. (condition-case nil
  12861. (progn
  12862. (cd tmpdir)
  12863. (call-process "latex" nil nil nil texfile))
  12864. (error nil))
  12865. (cd dir))
  12866. (if (not (file-exists-p dvifile))
  12867. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12868. (condition-case nil
  12869. (call-process "dvipng" nil nil nil
  12870. "-fg" fg "-bg" bg
  12871. "-D" dpi
  12872. ;;"-x" scale "-y" scale
  12873. "-T" "tight"
  12874. "-o" pngfile
  12875. dvifile)
  12876. (error nil))
  12877. (if (not (file-exists-p pngfile))
  12878. (progn (message "Failed to create png file from %s" texfile) nil)
  12879. ;; Use the requested file name and clean up
  12880. (copy-file pngfile tofile 'replace)
  12881. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12882. (delete-file (concat texfilebase e)))
  12883. pngfile))))
  12884. (defun org-dvipng-color (attr)
  12885. "Return an rgb color specification for dvipng."
  12886. (apply 'format "rgb %s %s %s"
  12887. (mapcar 'org-normalize-color
  12888. (color-values (face-attribute 'default attr nil)))))
  12889. (defun org-normalize-color (value)
  12890. "Return string to be used as color value for an RGB component."
  12891. (format "%g" (/ value 65535.0)))
  12892. ;;;; Key bindings
  12893. ;; Make `C-c C-x' a prefix key
  12894. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12895. ;; TAB key with modifiers
  12896. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12897. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12898. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12899. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12900. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12901. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12902. ;; The following line is necessary under Suse GNU/Linux
  12903. (unless (featurep 'xemacs)
  12904. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12905. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12906. (define-key org-mode-map [backtab] 'org-shifttab)
  12907. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12908. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12909. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12910. ;; Cursor keys with modifiers
  12911. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12912. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12913. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12914. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12915. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12916. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12917. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12918. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12919. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12920. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12921. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12922. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12923. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12924. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12925. ;;; Extra keys for tty access.
  12926. ;; We only set them when really needed because otherwise the
  12927. ;; menus don't show the simple keys
  12928. (when (or org-use-extra-keys
  12929. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12930. (not window-system))
  12931. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12932. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12933. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12934. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12935. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12936. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12937. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12938. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12939. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12940. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12941. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12942. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12943. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12944. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12945. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12946. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12947. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12948. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12949. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12950. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12951. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12952. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12953. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12954. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12955. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12956. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12957. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12958. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12959. ;; All the other keys
  12960. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12961. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12962. (if (boundp 'narrow-map)
  12963. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12964. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12965. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12966. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12967. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12968. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12969. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12970. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12971. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12972. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12973. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12974. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12975. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12976. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12977. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12978. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12979. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12980. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12981. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12982. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12983. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12984. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12985. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12986. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12987. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12988. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12989. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12990. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12991. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12992. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12993. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12994. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12995. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12996. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12997. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12998. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12999. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13000. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13001. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13002. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13003. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13004. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13005. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13006. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13007. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13008. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13009. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13010. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13011. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13012. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13013. (org-defkey org-mode-map "\C-m" 'org-return)
  13014. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13015. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13016. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13017. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13018. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13019. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13020. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13021. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13022. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13023. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13024. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13025. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13026. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13027. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13028. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13029. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13030. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13031. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13032. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13033. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13034. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13035. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13036. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13037. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13038. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13039. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13040. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13041. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13042. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13043. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13044. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13045. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13046. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13047. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13048. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13049. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13050. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13051. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13052. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13053. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13054. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13055. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13056. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13057. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13058. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13059. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13060. (when (featurep 'xemacs)
  13061. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13062. (defvar org-self-insert-command-undo-counter 0)
  13063. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13064. (defun org-self-insert-command (N)
  13065. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13066. If the cursor is in a table looking at whitespace, the whitespace is
  13067. overwritten, and the table is not marked as requiring realignment."
  13068. (interactive "p")
  13069. (if (and
  13070. (org-table-p)
  13071. (progn
  13072. ;; check if we blank the field, and if that triggers align
  13073. (and (featurep 'org-table) org-table-auto-blank-field
  13074. (member last-command
  13075. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13076. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13077. ;; got extra space, this field does not determine column width
  13078. (let (org-table-may-need-update) (org-table-blank-field))
  13079. ;; no extra space, this field may determine column width
  13080. (org-table-blank-field)))
  13081. t)
  13082. (eq N 1)
  13083. (looking-at "[^|\n]* |"))
  13084. (let (org-table-may-need-update)
  13085. (goto-char (1- (match-end 0)))
  13086. (delete-backward-char 1)
  13087. (goto-char (match-beginning 0))
  13088. (self-insert-command N))
  13089. (setq org-table-may-need-update t)
  13090. (self-insert-command N)
  13091. (org-fix-tags-on-the-fly)
  13092. (if org-self-insert-cluster-for-undo
  13093. (if (not (eq last-command 'org-self-insert-command))
  13094. (setq org-self-insert-command-undo-counter 1)
  13095. (if (>= org-self-insert-command-undo-counter 20)
  13096. (setq org-self-insert-command-undo-counter 1)
  13097. (and (> org-self-insert-command-undo-counter 0)
  13098. buffer-undo-list
  13099. (not (cadr buffer-undo-list)) ; remove nil entry
  13100. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13101. (setq org-self-insert-command-undo-counter
  13102. (1+ org-self-insert-command-undo-counter)))))))
  13103. (defun org-fix-tags-on-the-fly ()
  13104. (when (and (equal (char-after (point-at-bol)) ?*)
  13105. (org-on-heading-p))
  13106. (org-align-tags-here org-tags-column)))
  13107. (defun org-delete-backward-char (N)
  13108. "Like `delete-backward-char', insert whitespace at field end in tables.
  13109. When deleting backwards, in tables this function will insert whitespace in
  13110. front of the next \"|\" separator, to keep the table aligned. The table will
  13111. still be marked for re-alignment if the field did fill the entire column,
  13112. because, in this case the deletion might narrow the column."
  13113. (interactive "p")
  13114. (if (and (org-table-p)
  13115. (eq N 1)
  13116. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13117. (looking-at ".*?|"))
  13118. (let ((pos (point))
  13119. (noalign (looking-at "[^|\n\r]* |"))
  13120. (c org-table-may-need-update))
  13121. (backward-delete-char N)
  13122. (skip-chars-forward "^|")
  13123. (insert " ")
  13124. (goto-char (1- pos))
  13125. ;; noalign: if there were two spaces at the end, this field
  13126. ;; does not determine the width of the column.
  13127. (if noalign (setq org-table-may-need-update c)))
  13128. (backward-delete-char N)
  13129. (org-fix-tags-on-the-fly)))
  13130. (defun org-delete-char (N)
  13131. "Like `delete-char', but insert whitespace at field end in tables.
  13132. When deleting characters, in tables this function will insert whitespace in
  13133. front of the next \"|\" separator, to keep the table aligned. The table will
  13134. still be marked for re-alignment if the field did fill the entire column,
  13135. because, in this case the deletion might narrow the column."
  13136. (interactive "p")
  13137. (if (and (org-table-p)
  13138. (not (bolp))
  13139. (not (= (char-after) ?|))
  13140. (eq N 1))
  13141. (if (looking-at ".*?|")
  13142. (let ((pos (point))
  13143. (noalign (looking-at "[^|\n\r]* |"))
  13144. (c org-table-may-need-update))
  13145. (replace-match (concat
  13146. (substring (match-string 0) 1 -1)
  13147. " |"))
  13148. (goto-char pos)
  13149. ;; noalign: if there were two spaces at the end, this field
  13150. ;; does not determine the width of the column.
  13151. (if noalign (setq org-table-may-need-update c)))
  13152. (delete-char N))
  13153. (delete-char N)
  13154. (org-fix-tags-on-the-fly)))
  13155. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13156. (put 'org-self-insert-command 'delete-selection t)
  13157. (put 'orgtbl-self-insert-command 'delete-selection t)
  13158. (put 'org-delete-char 'delete-selection 'supersede)
  13159. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13160. (put 'org-yank 'delete-selection 'yank)
  13161. ;; Make `flyspell-mode' delay after some commands
  13162. (put 'org-self-insert-command 'flyspell-delayed t)
  13163. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13164. (put 'org-delete-char 'flyspell-delayed t)
  13165. (put 'org-delete-backward-char 'flyspell-delayed t)
  13166. ;; Make pabbrev-mode expand after org-mode commands
  13167. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13168. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13169. ;; How to do this: Measure non-white length of current string
  13170. ;; If equal to column width, we should realign.
  13171. (defun org-remap (map &rest commands)
  13172. "In MAP, remap the functions given in COMMANDS.
  13173. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13174. (let (new old)
  13175. (while commands
  13176. (setq old (pop commands) new (pop commands))
  13177. (if (fboundp 'command-remapping)
  13178. (org-defkey map (vector 'remap old) new)
  13179. (substitute-key-definition old new map global-map)))))
  13180. (when (eq org-enable-table-editor 'optimized)
  13181. ;; If the user wants maximum table support, we need to hijack
  13182. ;; some standard editing functions
  13183. (org-remap org-mode-map
  13184. 'self-insert-command 'org-self-insert-command
  13185. 'delete-char 'org-delete-char
  13186. 'delete-backward-char 'org-delete-backward-char)
  13187. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13188. (defvar org-ctrl-c-ctrl-c-hook nil
  13189. "Hook for functions attaching themselves to `C-c C-c'.
  13190. This can be used to add additional functionality to the C-c C-c key which
  13191. executes context-dependent commands.
  13192. Each function will be called with no arguments. The function must check
  13193. if the context is appropriate for it to act. If yes, it should do its
  13194. thing and then return a non-nil value. If the context is wrong,
  13195. just do nothing and return nil.")
  13196. (defvar org-tab-first-hook nil
  13197. "Hook for functions to attach themselves to TAB.
  13198. See `org-ctrl-c-ctrl-c-hook' for more information.
  13199. This hook runs as the first action when TAB is pressed, even before
  13200. `org-cycle' messes around with the `outline-regexp' to cater for
  13201. inline tasks and plain list item folding.
  13202. If any function in this hook returns t, not other actions like table
  13203. field motion visibility cycling will be done.")
  13204. (defvar org-tab-after-check-for-table-hook nil
  13205. "Hook for functions to attach themselves to TAB.
  13206. See `org-ctrl-c-ctrl-c-hook' for more information.
  13207. This hook runs after it has been established that the cursor is not in a
  13208. table, but before checking if the cursor is in a headline or if global cycling
  13209. should be done.
  13210. If any function in this hook returns t, not other actions like visibility
  13211. cycling will be done.")
  13212. (defvar org-tab-after-check-for-cycling-hook nil
  13213. "Hook for functions to attach themselves to TAB.
  13214. See `org-ctrl-c-ctrl-c-hook' for more information.
  13215. This hook runs after it has been established that not table field motion and
  13216. not visibility should be done because of current context. This is probably
  13217. the place where a package like yasnippets can hook in.")
  13218. (defvar org-metaleft-hook nil
  13219. "Hook for functions attaching themselves to `M-left'.
  13220. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13221. (defvar org-metaright-hook nil
  13222. "Hook for functions attaching themselves to `M-right'.
  13223. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13224. (defvar org-metaup-hook nil
  13225. "Hook for functions attaching themselves to `M-up'.
  13226. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13227. (defvar org-metadown-hook nil
  13228. "Hook for functions attaching themselves to `M-down'.
  13229. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13230. (defvar org-shiftmetaleft-hook nil
  13231. "Hook for functions attaching themselves to `M-S-left'.
  13232. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13233. (defvar org-shiftmetaright-hook nil
  13234. "Hook for functions attaching themselves to `M-S-right'.
  13235. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13236. (defvar org-shiftmetaup-hook nil
  13237. "Hook for functions attaching themselves to `M-S-up'.
  13238. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13239. (defvar org-shiftmetadown-hook nil
  13240. "Hook for functions attaching themselves to `M-S-down'.
  13241. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13242. (defvar org-metareturn-hook nil
  13243. "Hook for functions attaching themselves to `M-RET'.
  13244. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13245. (defun org-modifier-cursor-error ()
  13246. "Throw an error, a modified cursor command was applied in wrong context."
  13247. (error "This command is active in special context like tables, headlines or items"))
  13248. (defun org-shiftselect-error ()
  13249. "Throw an error because Shift-Cursor command was applied in wrong context."
  13250. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13251. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13252. (error "This command works only in special context like headlines or timestamps.")))
  13253. (defun org-call-for-shift-select (cmd)
  13254. (let ((this-command-keys-shift-translated t))
  13255. (call-interactively cmd)))
  13256. (defun org-shifttab (&optional arg)
  13257. "Global visibility cycling or move to previous table field.
  13258. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13259. on context.
  13260. See the individual commands for more information."
  13261. (interactive "P")
  13262. (cond
  13263. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13264. ((integerp arg)
  13265. (message "Content view to level: %d" arg)
  13266. (org-content (prefix-numeric-value arg))
  13267. (setq org-cycle-global-status 'overview))
  13268. (t (call-interactively 'org-global-cycle))))
  13269. (defun org-shiftmetaleft ()
  13270. "Promote subtree or delete table column.
  13271. Calls `org-promote-subtree', `org-outdent-item',
  13272. or `org-table-delete-column', depending on context.
  13273. See the individual commands for more information."
  13274. (interactive)
  13275. (cond
  13276. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13277. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13278. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13279. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13280. (t (org-modifier-cursor-error))))
  13281. (defun org-shiftmetaright ()
  13282. "Demote subtree or insert table column.
  13283. Calls `org-demote-subtree', `org-indent-item',
  13284. or `org-table-insert-column', depending on context.
  13285. See the individual commands for more information."
  13286. (interactive)
  13287. (cond
  13288. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13289. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13290. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13291. ((org-at-item-p) (call-interactively 'org-indent-item))
  13292. (t (org-modifier-cursor-error))))
  13293. (defun org-shiftmetaup (&optional arg)
  13294. "Move subtree up or kill table row.
  13295. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13296. `org-move-item-up' depending on context. See the individual commands
  13297. for more information."
  13298. (interactive "P")
  13299. (cond
  13300. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13301. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13302. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13303. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13304. (t (org-modifier-cursor-error))))
  13305. (defun org-shiftmetadown (&optional arg)
  13306. "Move subtree down or insert table row.
  13307. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13308. `org-move-item-down', depending on context. See the individual
  13309. commands for more information."
  13310. (interactive "P")
  13311. (cond
  13312. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13313. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13314. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13315. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13316. (t (org-modifier-cursor-error))))
  13317. (defun org-metaleft (&optional arg)
  13318. "Promote heading or move table column to left.
  13319. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13320. With no specific context, calls the Emacs default `backward-word'.
  13321. See the individual commands for more information."
  13322. (interactive "P")
  13323. (cond
  13324. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13325. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13326. ((or (org-on-heading-p)
  13327. (and (org-region-active-p)
  13328. (save-excursion
  13329. (goto-char (region-beginning))
  13330. (org-on-heading-p))))
  13331. (call-interactively 'org-do-promote))
  13332. ((or (org-at-item-p)
  13333. (and (org-region-active-p)
  13334. (save-excursion
  13335. (goto-char (region-beginning))
  13336. (org-at-item-p))))
  13337. (call-interactively 'org-outdent-item))
  13338. (t (call-interactively 'backward-word))))
  13339. (defun org-metaright (&optional arg)
  13340. "Demote subtree or move table column to right.
  13341. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13342. With no specific context, calls the Emacs default `forward-word'.
  13343. See the individual commands for more information."
  13344. (interactive "P")
  13345. (cond
  13346. ((run-hook-with-args-until-success 'org-metaright-hook))
  13347. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13348. ((or (org-on-heading-p)
  13349. (and (org-region-active-p)
  13350. (save-excursion
  13351. (goto-char (region-beginning))
  13352. (org-on-heading-p))))
  13353. (call-interactively 'org-do-demote))
  13354. ((or (org-at-item-p)
  13355. (and (org-region-active-p)
  13356. (save-excursion
  13357. (goto-char (region-beginning))
  13358. (org-at-item-p))))
  13359. (call-interactively 'org-indent-item))
  13360. (t (call-interactively 'forward-word))))
  13361. (defun org-metaup (&optional arg)
  13362. "Move subtree up or move table row up.
  13363. Calls `org-move-subtree-up' or `org-table-move-row' or
  13364. `org-move-item-up', depending on context. See the individual commands
  13365. for more information."
  13366. (interactive "P")
  13367. (cond
  13368. ((run-hook-with-args-until-success 'org-metaup-hook))
  13369. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13370. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13371. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13372. (t (transpose-lines 1) (beginning-of-line -1))))
  13373. (defun org-metadown (&optional arg)
  13374. "Move subtree down or move table row down.
  13375. Calls `org-move-subtree-down' or `org-table-move-row' or
  13376. `org-move-item-down', depending on context. See the individual
  13377. commands for more information."
  13378. (interactive "P")
  13379. (cond
  13380. ((run-hook-with-args-until-success 'org-metadown-hook))
  13381. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13382. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13383. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13384. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13385. (defun org-shiftup (&optional arg)
  13386. "Increase item in timestamp or increase priority of current headline.
  13387. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13388. depending on context. See the individual commands for more information."
  13389. (interactive "P")
  13390. (cond
  13391. ((and org-support-shift-select (org-region-active-p))
  13392. (org-call-for-shift-select 'previous-line))
  13393. ((org-at-timestamp-p t)
  13394. (call-interactively (if org-edit-timestamp-down-means-later
  13395. 'org-timestamp-down 'org-timestamp-up)))
  13396. ((and (not (eq org-support-shift-select 'always))
  13397. org-enable-priority-commands
  13398. (org-on-heading-p))
  13399. (call-interactively 'org-priority-up))
  13400. ((and (not org-support-shift-select) (org-at-item-p))
  13401. (call-interactively 'org-previous-item))
  13402. ((org-clocktable-try-shift 'up arg))
  13403. (org-support-shift-select
  13404. (org-call-for-shift-select 'previous-line))
  13405. (t (org-shiftselect-error))))
  13406. (defun org-shiftdown (&optional arg)
  13407. "Decrease item in timestamp or decrease priority of current headline.
  13408. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13409. depending on context. See the individual commands for more information."
  13410. (interactive "P")
  13411. (cond
  13412. ((and org-support-shift-select (org-region-active-p))
  13413. (org-call-for-shift-select 'next-line))
  13414. ((org-at-timestamp-p t)
  13415. (call-interactively (if org-edit-timestamp-down-means-later
  13416. 'org-timestamp-up 'org-timestamp-down)))
  13417. ((and (not (eq org-support-shift-select 'always))
  13418. org-enable-priority-commands
  13419. (org-on-heading-p))
  13420. (call-interactively 'org-priority-down))
  13421. ((and (not org-support-shift-select) (org-at-item-p))
  13422. (call-interactively 'org-next-item))
  13423. ((org-clocktable-try-shift 'down arg))
  13424. (org-support-shift-select
  13425. (org-call-for-shift-select 'next-line))
  13426. (t (org-shiftselect-error))))
  13427. (defun org-shiftright (&optional arg)
  13428. "Cycle the thing at point or in the current line, depending on context.
  13429. Depending on context, this does one of the following:
  13430. - switch a timestamp at point one day into the future
  13431. - on a headline, switch to the next TODO keyword.
  13432. - on an item, switch entire list to the next bullet type
  13433. - on a property line, switch to the next allowed value
  13434. - on a clocktable definition line, move time block into the future"
  13435. (interactive "P")
  13436. (cond
  13437. ((and org-support-shift-select (org-region-active-p))
  13438. (org-call-for-shift-select 'forward-char))
  13439. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13440. ((and (not (eq org-support-shift-select 'always))
  13441. (org-on-heading-p))
  13442. (let ((org-inhibit-logging
  13443. (not org-treat-S-cursor-todo-selection-as-state-change))
  13444. (org-inhibit-blocking
  13445. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13446. (org-call-with-arg 'org-todo 'right)))
  13447. ((or (and org-support-shift-select
  13448. (not (eq org-support-shift-select 'always))
  13449. (org-at-item-bullet-p))
  13450. (and (not org-support-shift-select) (org-at-item-p)))
  13451. (org-call-with-arg 'org-cycle-list-bullet nil))
  13452. ((and (not (eq org-support-shift-select 'always))
  13453. (org-at-property-p))
  13454. (call-interactively 'org-property-next-allowed-value))
  13455. ((org-clocktable-try-shift 'right arg))
  13456. (org-support-shift-select
  13457. (org-call-for-shift-select 'forward-char))
  13458. (t (org-shiftselect-error))))
  13459. (defun org-shiftleft (&optional arg)
  13460. "Cycle the thing at point or in the current line, depending on context.
  13461. Depending on context, this does one of the following:
  13462. - switch a timestamp at point one day into the past
  13463. - on a headline, switch to the previous TODO keyword.
  13464. - on an item, switch entire list to the previous bullet type
  13465. - on a property line, switch to the previous allowed value
  13466. - on a clocktable definition line, move time block into the past"
  13467. (interactive "P")
  13468. (cond
  13469. ((and org-support-shift-select (org-region-active-p))
  13470. (org-call-for-shift-select 'backward-char))
  13471. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13472. ((and (not (eq org-support-shift-select 'always))
  13473. (org-on-heading-p))
  13474. (let ((org-inhibit-logging
  13475. (not org-treat-S-cursor-todo-selection-as-state-change))
  13476. (org-inhibit-blocking
  13477. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13478. (org-call-with-arg 'org-todo 'left)))
  13479. ((or (and org-support-shift-select
  13480. (not (eq org-support-shift-select 'always))
  13481. (org-at-item-bullet-p))
  13482. (and (not org-support-shift-select) (org-at-item-p)))
  13483. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13484. ((and (not (eq org-support-shift-select 'always))
  13485. (org-at-property-p))
  13486. (call-interactively 'org-property-previous-allowed-value))
  13487. ((org-clocktable-try-shift 'left arg))
  13488. (org-support-shift-select
  13489. (org-call-for-shift-select 'backward-char))
  13490. (t (org-shiftselect-error))))
  13491. (defun org-shiftcontrolright ()
  13492. "Switch to next TODO set."
  13493. (interactive)
  13494. (cond
  13495. ((and org-support-shift-select (org-region-active-p))
  13496. (org-call-for-shift-select 'forward-word))
  13497. ((and (not (eq org-support-shift-select 'always))
  13498. (org-on-heading-p))
  13499. (org-call-with-arg 'org-todo 'nextset))
  13500. (org-support-shift-select
  13501. (org-call-for-shift-select 'forward-word))
  13502. (t (org-shiftselect-error))))
  13503. (defun org-shiftcontrolleft ()
  13504. "Switch to previous TODO set."
  13505. (interactive)
  13506. (cond
  13507. ((and org-support-shift-select (org-region-active-p))
  13508. (org-call-for-shift-select 'backward-word))
  13509. ((and (not (eq org-support-shift-select 'always))
  13510. (org-on-heading-p))
  13511. (org-call-with-arg 'org-todo 'previousset))
  13512. (org-support-shift-select
  13513. (org-call-for-shift-select 'backward-word))
  13514. (t (org-shiftselect-error))))
  13515. (defun org-ctrl-c-ret ()
  13516. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13517. (interactive)
  13518. (cond
  13519. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13520. (t (call-interactively 'org-insert-heading))))
  13521. (defun org-copy-special ()
  13522. "Copy region in table or copy current subtree.
  13523. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13524. See the individual commands for more information."
  13525. (interactive)
  13526. (call-interactively
  13527. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13528. (defun org-cut-special ()
  13529. "Cut region in table or cut current subtree.
  13530. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13531. See the individual commands for more information."
  13532. (interactive)
  13533. (call-interactively
  13534. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13535. (defun org-paste-special (arg)
  13536. "Paste rectangular region into table, or past subtree relative to level.
  13537. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13538. See the individual commands for more information."
  13539. (interactive "P")
  13540. (if (org-at-table-p)
  13541. (org-table-paste-rectangle)
  13542. (org-paste-subtree arg)))
  13543. (defun org-edit-special ()
  13544. "Call a special editor for the stuff at point.
  13545. When at a table, call the formula editor with `org-table-edit-formulas'.
  13546. When at the first line of an src example, call `org-edit-src-code'.
  13547. When in an #+include line, visit the include file. Otherwise call
  13548. `ffap' to visit the file at point."
  13549. (interactive)
  13550. (cond
  13551. ((org-at-table-p)
  13552. (call-interactively 'org-table-edit-formulas))
  13553. ((save-excursion
  13554. (beginning-of-line 1)
  13555. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13556. (find-file (org-trim (match-string 1))))
  13557. ((org-edit-src-code))
  13558. ((org-edit-fixed-width-region))
  13559. (t (call-interactively 'ffap))))
  13560. (defun org-ctrl-c-ctrl-c (&optional arg)
  13561. "Set tags in headline, or update according to changed information at point.
  13562. This command does many different things, depending on context:
  13563. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13564. this is what we do.
  13565. - If the cursor is in a headline, prompt for tags and insert them
  13566. into the current line, aligned to `org-tags-column'. When called
  13567. with prefix arg, realign all tags in the current buffer.
  13568. - If the cursor is in one of the special #+KEYWORD lines, this
  13569. triggers scanning the buffer for these lines and updating the
  13570. information.
  13571. - If the cursor is inside a table, realign the table. This command
  13572. works even if the automatic table editor has been turned off.
  13573. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13574. the entire table.
  13575. - If the cursor is at a footnote reference or definition, jump to
  13576. the corresponding definition or references, respectively.
  13577. - If the cursor is a the beginning of a dynamic block, update it.
  13578. - If the cursor is inside a table created by the table.el package,
  13579. activate that table.
  13580. - If the current buffer is a remember buffer, close note and file
  13581. it. A prefix argument of 1 files to the default location
  13582. without further interaction. A prefix argument of 2 files to
  13583. the currently clocking task.
  13584. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13585. links in this buffer.
  13586. - If the cursor is on a numbered item in a plain list, renumber the
  13587. ordered list.
  13588. - If the cursor is on a checkbox, toggle it."
  13589. (interactive "P")
  13590. (let ((org-enable-table-editor t))
  13591. (cond
  13592. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13593. org-occur-highlights
  13594. org-latex-fragment-image-overlays)
  13595. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13596. (org-remove-occur-highlights)
  13597. (org-remove-latex-fragment-image-overlays)
  13598. (message "Temporary highlights/overlays removed from current buffer"))
  13599. ((and (local-variable-p 'org-finish-function (current-buffer))
  13600. (fboundp org-finish-function))
  13601. (funcall org-finish-function))
  13602. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13603. ((org-at-property-p)
  13604. (call-interactively 'org-property-action))
  13605. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13606. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13607. ((org-at-table.el-p)
  13608. (require 'table)
  13609. (beginning-of-line 1)
  13610. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13611. (call-interactively 'table-recognize-table))
  13612. ((org-at-table-p)
  13613. (org-table-maybe-eval-formula)
  13614. (if arg
  13615. (call-interactively 'org-table-recalculate)
  13616. (org-table-maybe-recalculate-line))
  13617. (call-interactively 'org-table-align))
  13618. ((or (org-footnote-at-reference-p)
  13619. (org-footnote-at-definition-p))
  13620. (call-interactively 'org-footnote-action))
  13621. ((org-at-item-checkbox-p)
  13622. (call-interactively 'org-toggle-checkbox))
  13623. ((org-at-item-p)
  13624. (if arg
  13625. (call-interactively 'org-toggle-checkbox)
  13626. (call-interactively 'org-maybe-renumber-ordered-list)))
  13627. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13628. ;; Dynamic block
  13629. (beginning-of-line 1)
  13630. (save-excursion (org-update-dblock)))
  13631. ((save-excursion
  13632. (beginning-of-line 1)
  13633. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13634. (cond
  13635. ((equal (match-string 1) "TBLFM")
  13636. ;; Recalculate the table before this line
  13637. (save-excursion
  13638. (beginning-of-line 1)
  13639. (skip-chars-backward " \r\n\t")
  13640. (if (org-at-table-p)
  13641. (org-call-with-arg 'org-table-recalculate t))))
  13642. (t
  13643. ; (org-set-regexps-and-options)
  13644. ; (org-restart-font-lock)
  13645. (let ((org-inhibit-startup t)) (org-mode-restart))
  13646. (message "Local setup has been refreshed"))))
  13647. ((org-clock-update-time-maybe))
  13648. (t (error "C-c C-c can do nothing useful at this location.")))))
  13649. (defun org-mode-restart ()
  13650. "Restart Org-mode, to scan again for special lines.
  13651. Also updates the keyword regular expressions."
  13652. (interactive)
  13653. (org-mode)
  13654. (message "Org-mode restarted"))
  13655. (defun org-kill-note-or-show-branches ()
  13656. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13657. (interactive)
  13658. (if (not org-finish-function)
  13659. (call-interactively 'show-branches)
  13660. (let ((org-note-abort t))
  13661. (funcall org-finish-function))))
  13662. (defun org-return (&optional indent)
  13663. "Goto next table row or insert a newline.
  13664. Calls `org-table-next-row' or `newline', depending on context.
  13665. See the individual commands for more information."
  13666. (interactive)
  13667. (cond
  13668. ((bobp) (if indent (newline-and-indent) (newline)))
  13669. ((org-at-table-p)
  13670. (org-table-justify-field-maybe)
  13671. (call-interactively 'org-table-next-row))
  13672. ((and org-return-follows-link
  13673. (eq (get-text-property (point) 'face) 'org-link))
  13674. (call-interactively 'org-open-at-point))
  13675. ((and (org-at-heading-p)
  13676. (looking-at
  13677. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13678. (org-show-entry)
  13679. (end-of-line 1)
  13680. (newline))
  13681. (t (if indent (newline-and-indent) (newline)))))
  13682. (defun org-return-indent ()
  13683. "Goto next table row or insert a newline and indent.
  13684. Calls `org-table-next-row' or `newline-and-indent', depending on
  13685. context. See the individual commands for more information."
  13686. (interactive)
  13687. (org-return t))
  13688. (defun org-ctrl-c-star ()
  13689. "Compute table, or change heading status of lines.
  13690. Calls `org-table-recalculate' or `org-toggle-heading',
  13691. depending on context."
  13692. (interactive)
  13693. (cond
  13694. ((org-at-table-p)
  13695. (call-interactively 'org-table-recalculate))
  13696. (t
  13697. ;; Convert all lines in region to list items
  13698. (call-interactively 'org-toggle-heading))))
  13699. (defun org-ctrl-c-minus ()
  13700. "Insert separator line in table or modify bullet status of line.
  13701. Also turns a plain line or a region of lines into list items.
  13702. Calls `org-table-insert-hline', `org-toggle-item', or
  13703. `org-cycle-list-bullet', depending on context."
  13704. (interactive)
  13705. (cond
  13706. ((org-at-table-p)
  13707. (call-interactively 'org-table-insert-hline))
  13708. ((org-region-active-p)
  13709. (call-interactively 'org-toggle-item))
  13710. ((org-in-item-p)
  13711. (call-interactively 'org-cycle-list-bullet))
  13712. (t
  13713. (call-interactively 'org-toggle-item))))
  13714. (defun org-toggle-item ()
  13715. "Convert headings or normal lines to items, items to normal lines.
  13716. If there is no active region, only the current line is considered.
  13717. If the first line in the region is a headline, convert all headlines to items.
  13718. If the first line in the region is an item, convert all items to normal lines.
  13719. If the first line is normal text, add an item bullet to each line."
  13720. (interactive)
  13721. (let (l2 l beg end)
  13722. (if (org-region-active-p)
  13723. (setq beg (region-beginning) end (region-end))
  13724. (setq beg (point-at-bol)
  13725. end (min (1+ (point-at-eol)) (point-max))))
  13726. (save-excursion
  13727. (goto-char end)
  13728. (setq l2 (org-current-line))
  13729. (goto-char beg)
  13730. (beginning-of-line 1)
  13731. (setq l (1- (org-current-line)))
  13732. (if (org-at-item-p)
  13733. ;; We already have items, de-itemize
  13734. (while (< (setq l (1+ l)) l2)
  13735. (when (org-at-item-p)
  13736. (goto-char (match-beginning 2))
  13737. (delete-region (match-beginning 2) (match-end 2))
  13738. (and (looking-at "[ \t]+") (replace-match "")))
  13739. (beginning-of-line 2))
  13740. (if (org-on-heading-p)
  13741. ;; Headings, convert to items
  13742. (while (< (setq l (1+ l)) l2)
  13743. (if (looking-at org-outline-regexp)
  13744. (replace-match "- " t t))
  13745. (beginning-of-line 2))
  13746. ;; normal lines, turn them into items
  13747. (while (< (setq l (1+ l)) l2)
  13748. (unless (org-at-item-p)
  13749. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13750. (replace-match "\\1- \\2")))
  13751. (beginning-of-line 2)))))))
  13752. (defun org-toggle-heading (&optional nstars)
  13753. "Convert headings to normal text, or items or text to headings.
  13754. If there is no active region, only the current line is considered.
  13755. If the first line is a heading, remove the stars from all headlines
  13756. in the region.
  13757. If the first line is a plain list item, turn all plain list items
  13758. into headings.
  13759. If the first line is a normal line, turn each and every line in the
  13760. region into a heading.
  13761. When converting a line into a heading, the number of stars is chosen
  13762. such that the lines become children of the current entry. However,
  13763. when a prefix argument is given, its value determines the number of
  13764. stars to add."
  13765. (interactive "P")
  13766. (let (l2 l itemp beg end)
  13767. (if (org-region-active-p)
  13768. (setq beg (region-beginning) end (region-end))
  13769. (setq beg (point-at-bol)
  13770. end (min (1+ (point-at-eol)) (point-max))))
  13771. (save-excursion
  13772. (goto-char end)
  13773. (setq l2 (org-current-line))
  13774. (goto-char beg)
  13775. (beginning-of-line 1)
  13776. (setq l (1- (org-current-line)))
  13777. (if (org-on-heading-p)
  13778. ;; We already have headlines, de-star them
  13779. (while (< (setq l (1+ l)) l2)
  13780. (when (org-on-heading-p t)
  13781. (and (looking-at outline-regexp) (replace-match "")))
  13782. (beginning-of-line 2))
  13783. (setq itemp (org-at-item-p))
  13784. (let* ((stars
  13785. (if nstars
  13786. (make-string (prefix-numeric-value current-prefix-arg)
  13787. ?*)
  13788. (save-excursion
  13789. (if (re-search-backward org-complex-heading-regexp nil t)
  13790. (match-string 1) ""))))
  13791. (add-stars (cond (nstars "")
  13792. ((equal stars "") "*")
  13793. (org-odd-levels-only "**")
  13794. (t "*")))
  13795. (rpl (concat stars add-stars " ")))
  13796. (while (< (setq l (1+ l)) l2)
  13797. (if itemp
  13798. (and (org-at-item-p) (replace-match rpl t t))
  13799. (unless (org-on-heading-p)
  13800. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13801. (replace-match (concat rpl (match-string 2))))))
  13802. (beginning-of-line 2)))))))
  13803. (defun org-meta-return (&optional arg)
  13804. "Insert a new heading or wrap a region in a table.
  13805. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13806. See the individual commands for more information."
  13807. (interactive "P")
  13808. (cond
  13809. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13810. ((org-at-table-p)
  13811. (call-interactively 'org-table-wrap-region))
  13812. (t (call-interactively 'org-insert-heading))))
  13813. ;;; Menu entries
  13814. ;; Define the Org-mode menus
  13815. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13816. '("Tbl"
  13817. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13818. ["Next Field" org-cycle (org-at-table-p)]
  13819. ["Previous Field" org-shifttab (org-at-table-p)]
  13820. ["Next Row" org-return (org-at-table-p)]
  13821. "--"
  13822. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13823. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13824. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13825. "--"
  13826. ("Column"
  13827. ["Move Column Left" org-metaleft (org-at-table-p)]
  13828. ["Move Column Right" org-metaright (org-at-table-p)]
  13829. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13830. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13831. ("Row"
  13832. ["Move Row Up" org-metaup (org-at-table-p)]
  13833. ["Move Row Down" org-metadown (org-at-table-p)]
  13834. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13835. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13836. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13837. "--"
  13838. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13839. ("Rectangle"
  13840. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13841. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13842. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13843. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13844. "--"
  13845. ("Calculate"
  13846. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13847. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13848. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13849. "--"
  13850. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13851. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13852. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13853. "--"
  13854. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13855. "--"
  13856. ["Sum Column/Rectangle" org-table-sum
  13857. (or (org-at-table-p) (org-region-active-p))]
  13858. ["Which Column?" org-table-current-column (org-at-table-p)])
  13859. ["Debug Formulas"
  13860. org-table-toggle-formula-debugger
  13861. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13862. ["Show Col/Row Numbers"
  13863. org-table-toggle-coordinate-overlays
  13864. :style toggle
  13865. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13866. "--"
  13867. ["Create" org-table-create (and (not (org-at-table-p))
  13868. org-enable-table-editor)]
  13869. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13870. ["Import from File" org-table-import (not (org-at-table-p))]
  13871. ["Export to File" org-table-export (org-at-table-p)]
  13872. "--"
  13873. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13874. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13875. '("Org"
  13876. ("Show/Hide"
  13877. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13878. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13879. ["Sparse Tree..." org-sparse-tree t]
  13880. ["Reveal Context" org-reveal t]
  13881. ["Show All" show-all t]
  13882. "--"
  13883. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13884. "--"
  13885. ["New Heading" org-insert-heading t]
  13886. ("Navigate Headings"
  13887. ["Up" outline-up-heading t]
  13888. ["Next" outline-next-visible-heading t]
  13889. ["Previous" outline-previous-visible-heading t]
  13890. ["Next Same Level" outline-forward-same-level t]
  13891. ["Previous Same Level" outline-backward-same-level t]
  13892. "--"
  13893. ["Jump" org-goto t])
  13894. ("Edit Structure"
  13895. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13896. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13897. "--"
  13898. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13899. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13900. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13901. "--"
  13902. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13903. "--"
  13904. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13905. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13906. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13907. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13908. "--"
  13909. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13910. "--"
  13911. ["Convert to odd levels" org-convert-to-odd-levels t]
  13912. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13913. ("Editing"
  13914. ["Emphasis..." org-emphasize t]
  13915. ["Edit Source Example" org-edit-special t]
  13916. "--"
  13917. ["Footnote new/jump" org-footnote-action t]
  13918. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13919. ("Archive"
  13920. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13921. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13922. ; :active t :keys "C-u C-c C-x C-a"]
  13923. ["Sparse trees open ARCHIVE trees"
  13924. (setq org-sparse-tree-open-archived-trees
  13925. (not org-sparse-tree-open-archived-trees))
  13926. :style toggle :selected org-sparse-tree-open-archived-trees]
  13927. ["Cycling opens ARCHIVE trees"
  13928. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13929. :style toggle :selected org-cycle-open-archived-trees]
  13930. "--"
  13931. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13932. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13933. ; ["Check and Move Children" (org-archive-subtree '(4))
  13934. ; :active t :keys "C-u C-c C-x C-s"]
  13935. )
  13936. "--"
  13937. ("Hyperlinks"
  13938. ["Store Link (Global)" org-store-link t]
  13939. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13940. ["Insert Link" org-insert-link t]
  13941. ["Follow Link" org-open-at-point t]
  13942. "--"
  13943. ["Next link" org-next-link t]
  13944. ["Previous link" org-previous-link t]
  13945. "--"
  13946. ["Descriptive Links"
  13947. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13948. :style radio
  13949. :selected (member '(org-link) buffer-invisibility-spec)]
  13950. ["Literal Links"
  13951. (progn
  13952. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13953. :style radio
  13954. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13955. "--"
  13956. ("TODO Lists"
  13957. ["TODO/DONE/-" org-todo t]
  13958. ("Select keyword"
  13959. ["Next keyword" org-shiftright (org-on-heading-p)]
  13960. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13961. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13962. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13963. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13964. ["Show TODO Tree" org-show-todo-tree t]
  13965. ["Global TODO list" org-todo-list t]
  13966. "--"
  13967. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13968. :selected org-enforce-todo-dependencies :style toggle :active t]
  13969. "Settings for tree at point"
  13970. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13971. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13972. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13973. ["Do Children parallel" org-toggle-ordered-property :style radio
  13974. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13975. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13976. "--"
  13977. ["Set Priority" org-priority t]
  13978. ["Priority Up" org-shiftup t]
  13979. ["Priority Down" org-shiftdown t]
  13980. "--"
  13981. ["Get news from all feeds" org-feed-update-all t]
  13982. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13983. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13984. ("TAGS and Properties"
  13985. ["Set Tags" org-set-tags-command t]
  13986. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13987. "--"
  13988. ["Set property" org-set-property t]
  13989. ["Column view of properties" org-columns t]
  13990. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13991. ("Dates and Scheduling"
  13992. ["Timestamp" org-time-stamp t]
  13993. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13994. ("Change Date"
  13995. ["1 Day Later" org-shiftright t]
  13996. ["1 Day Earlier" org-shiftleft t]
  13997. ["1 ... Later" org-shiftup t]
  13998. ["1 ... Earlier" org-shiftdown t])
  13999. ["Compute Time Range" org-evaluate-time-range t]
  14000. ["Schedule Item" org-schedule t]
  14001. ["Deadline" org-deadline t]
  14002. "--"
  14003. ["Custom time format" org-toggle-time-stamp-overlays
  14004. :style radio :selected org-display-custom-times]
  14005. "--"
  14006. ["Goto Calendar" org-goto-calendar t]
  14007. ["Date from Calendar" org-date-from-calendar t]
  14008. "--"
  14009. ["Start/Restart Timer" org-timer-start t]
  14010. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14011. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14012. ["Insert Timer String" org-timer t]
  14013. ["Insert Timer Item" org-timer-item t])
  14014. ("Logging work"
  14015. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14016. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14017. ["Clock out" org-clock-out t]
  14018. ["Clock cancel" org-clock-cancel t]
  14019. "--"
  14020. ["Mark as default task" org-clock-mark-default-task t]
  14021. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14022. ["Goto running clock" org-clock-goto t]
  14023. "--"
  14024. ["Display times" org-clock-display t]
  14025. ["Create clock table" org-clock-report t]
  14026. "--"
  14027. ["Record DONE time"
  14028. (progn (setq org-log-done (not org-log-done))
  14029. (message "Switching to %s will %s record a timestamp"
  14030. (car org-done-keywords)
  14031. (if org-log-done "automatically" "not")))
  14032. :style toggle :selected org-log-done])
  14033. "--"
  14034. ["Agenda Command..." org-agenda t]
  14035. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14036. ("File List for Agenda")
  14037. ("Special views current file"
  14038. ["TODO Tree" org-show-todo-tree t]
  14039. ["Check Deadlines" org-check-deadlines t]
  14040. ["Timeline" org-timeline t]
  14041. ["Tags/Property tree" org-match-sparse-tree t])
  14042. "--"
  14043. ["Export/Publish..." org-export t]
  14044. ("LaTeX"
  14045. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14046. :selected org-cdlatex-mode]
  14047. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14048. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14049. ["Modify math symbol" org-cdlatex-math-modify
  14050. (org-inside-LaTeX-fragment-p)]
  14051. ["Insert citation" org-reftex-citation t]
  14052. "--"
  14053. ["Export LaTeX fragments as images"
  14054. (if (featurep 'org-exp)
  14055. (setq org-export-with-LaTeX-fragments
  14056. (not org-export-with-LaTeX-fragments))
  14057. (require 'org-exp))
  14058. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14059. org-export-with-LaTeX-fragments)])
  14060. "--"
  14061. ("Documentation"
  14062. ["Show Version" org-version t]
  14063. ["Info Documentation" org-info t])
  14064. ("Customize"
  14065. ["Browse Org Group" org-customize t]
  14066. "--"
  14067. ["Expand This Menu" org-create-customize-menu
  14068. (fboundp 'customize-menu-create)])
  14069. ["Send bug report" org-submit-bug-report t]
  14070. "--"
  14071. ("Refresh/Reload"
  14072. ["Refresh setup current buffer" org-mode-restart t]
  14073. ["Reload Org (after update)" org-reload t]
  14074. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14075. ))
  14076. (defun org-info (&optional node)
  14077. "Read documentation for Org-mode in the info system.
  14078. With optional NODE, go directly to that node."
  14079. (interactive)
  14080. (info (format "(org)%s" (or node ""))))
  14081. ;;;###autoload
  14082. (defun org-submit-bug-report ()
  14083. "Submit a bug report on Org-mode via mail.
  14084. Don't hesitate to report any problems or inaccurate documentation.
  14085. If you don't have setup sending mail from (X)Emacs, please copy the
  14086. output buffer into your mail program, as it gives us important
  14087. information about your Org-mode version and configuration."
  14088. (interactive)
  14089. (require 'reporter)
  14090. (org-load-modules-maybe)
  14091. (org-require-autoloaded-modules)
  14092. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14093. (reporter-submit-bug-report
  14094. "emacs-orgmode@gnu.org"
  14095. (org-version)
  14096. (let (list)
  14097. (save-window-excursion
  14098. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14099. (delete-other-windows)
  14100. (erase-buffer)
  14101. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14102. We would like to add your full Org-mode and Outline configuration to the
  14103. bug report. This greatly simplifies the work of the maintainer and
  14104. other experts on the mailing list.
  14105. HOWEVER, some variables you have customized may contain private
  14106. information. The names of customers, colleagues, or friends, might
  14107. appear in the form of file names, tags, todo states, or search strings.
  14108. If you answer yes to the prompt, you might want to check and remove
  14109. such private information before sending the email.")
  14110. (add-text-properties (point-min) (point-max) '(face org-warning))
  14111. (when (yes-or-no-p "Include your Org-mode configuration ")
  14112. (mapatoms
  14113. (lambda (v)
  14114. (and (boundp v)
  14115. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14116. (or (and (symbol-value v)
  14117. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14118. (and
  14119. (get v 'custom-type) (get v 'standard-value)
  14120. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14121. (push v list)))))
  14122. (kill-buffer (get-buffer "*Warn about privacy*"))
  14123. list))
  14124. nil nil
  14125. "Remember to cover the basics, that is, what you expected to happen and
  14126. what in fact did happen. You don't know hoe to make a good report? See
  14127. http://orgmode.org/manual/Feedback.html#Feedback
  14128. Your bug report will be posted to the Org-mode mailing list.
  14129. ------------------------------------------------------------------------")))
  14130. (defun org-install-agenda-files-menu ()
  14131. (let ((bl (buffer-list)))
  14132. (save-excursion
  14133. (while bl
  14134. (set-buffer (pop bl))
  14135. (if (org-mode-p) (setq bl nil)))
  14136. (when (org-mode-p)
  14137. (easy-menu-change
  14138. '("Org") "File List for Agenda"
  14139. (append
  14140. (list
  14141. ["Edit File List" (org-edit-agenda-file-list) t]
  14142. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14143. ["Remove Current File from List" org-remove-file t]
  14144. ["Cycle through agenda files" org-cycle-agenda-files t]
  14145. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14146. "--")
  14147. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14148. ;;;; Documentation
  14149. ;;;###autoload
  14150. (defun org-require-autoloaded-modules ()
  14151. (interactive)
  14152. (mapc 'require
  14153. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14154. org-docbook org-exp org-html org-icalendar
  14155. org-id org-latex
  14156. org-publish org-remember org-table
  14157. org-timer org-xoxo)))
  14158. ;;;###autoload
  14159. (defun org-reload (&optional uncompiled)
  14160. "Reload all org lisp files.
  14161. With prefix arg UNCOMPILED, load the uncompiled versions."
  14162. (interactive "P")
  14163. (require 'find-func)
  14164. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14165. (dir-org (file-name-directory (org-find-library-name "org")))
  14166. (dir-org-contrib (ignore-errors
  14167. (file-name-directory
  14168. (org-find-library-name "org-contribdir"))))
  14169. (files
  14170. (append (directory-files dir-org t file-re)
  14171. (and dir-org-contrib
  14172. (directory-files dir-org-contrib t file-re))))
  14173. (remove-re (concat (if (featurep 'xemacs)
  14174. "org-colview" "org-colview-xemacs")
  14175. "\\'")))
  14176. (setq files (mapcar 'file-name-sans-extension files))
  14177. (setq files (mapcar
  14178. (lambda (x) (if (string-match remove-re x) nil x))
  14179. files))
  14180. (setq files (delq nil files))
  14181. (mapc
  14182. (lambda (f)
  14183. (when (featurep (intern (file-name-nondirectory f)))
  14184. (if (and (not uncompiled)
  14185. (file-exists-p (concat f ".elc")))
  14186. (load (concat f ".elc") nil nil t)
  14187. (load (concat f ".el") nil nil t))))
  14188. files))
  14189. (org-version))
  14190. ;;;###autoload
  14191. (defun org-customize ()
  14192. "Call the customize function with org as argument."
  14193. (interactive)
  14194. (org-load-modules-maybe)
  14195. (org-require-autoloaded-modules)
  14196. (customize-browse 'org))
  14197. (defun org-create-customize-menu ()
  14198. "Create a full customization menu for Org-mode, insert it into the menu."
  14199. (interactive)
  14200. (org-load-modules-maybe)
  14201. (org-require-autoloaded-modules)
  14202. (if (fboundp 'customize-menu-create)
  14203. (progn
  14204. (easy-menu-change
  14205. '("Org") "Customize"
  14206. `(["Browse Org group" org-customize t]
  14207. "--"
  14208. ,(customize-menu-create 'org)
  14209. ["Set" Custom-set t]
  14210. ["Save" Custom-save t]
  14211. ["Reset to Current" Custom-reset-current t]
  14212. ["Reset to Saved" Custom-reset-saved t]
  14213. ["Reset to Standard Settings" Custom-reset-standard t]))
  14214. (message "\"Org\"-menu now contains full customization menu"))
  14215. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14216. ;;;; Miscellaneous stuff
  14217. ;;; Generally useful functions
  14218. (defun org-find-text-property-in-string (prop s)
  14219. "Return the first non-nil value of property PROP in string S."
  14220. (or (get-text-property 0 prop s)
  14221. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14222. prop s)))
  14223. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14224. "Display the given MESSAGE as a warning."
  14225. (if (fboundp 'display-warning)
  14226. (display-warning 'org message
  14227. (if (featurep 'xemacs)
  14228. 'warning
  14229. :warning))
  14230. (let ((buf (get-buffer-create "*Org warnings*")))
  14231. (with-current-buffer buf
  14232. (goto-char (point-max))
  14233. (insert "Warning (Org): " message)
  14234. (unless (bolp)
  14235. (newline)))
  14236. (display-buffer buf)
  14237. (sit-for 0))))
  14238. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14239. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14240. (if (and marker (marker-buffer marker)
  14241. (buffer-live-p (marker-buffer marker)))
  14242. (progn
  14243. (switch-to-buffer (marker-buffer marker))
  14244. (if (or (> marker (point-max)) (< marker (point-min)))
  14245. (widen))
  14246. (goto-char marker)
  14247. (org-show-context 'org-goto))
  14248. (if bookmark
  14249. (bookmark-jump bookmark)
  14250. (error "Cannot find location"))))
  14251. (defun org-quote-csv-field (s)
  14252. "Quote field for inclusion in CSV material."
  14253. (if (string-match "[\",]" s)
  14254. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14255. s))
  14256. (defun org-plist-delete (plist property)
  14257. "Delete PROPERTY from PLIST.
  14258. This is in contrast to merely setting it to 0."
  14259. (let (p)
  14260. (while plist
  14261. (if (not (eq property (car plist)))
  14262. (setq p (plist-put p (car plist) (nth 1 plist))))
  14263. (setq plist (cddr plist)))
  14264. p))
  14265. (defun org-force-self-insert (N)
  14266. "Needed to enforce self-insert under remapping."
  14267. (interactive "p")
  14268. (self-insert-command N))
  14269. (defun org-string-width (s)
  14270. "Compute width of string, ignoring invisible characters.
  14271. This ignores character with invisibility property `org-link', and also
  14272. characters with property `org-cwidth', because these will become invisible
  14273. upon the next fontification round."
  14274. (let (b l)
  14275. (when (or (eq t buffer-invisibility-spec)
  14276. (assq 'org-link buffer-invisibility-spec))
  14277. (while (setq b (text-property-any 0 (length s)
  14278. 'invisible 'org-link s))
  14279. (setq s (concat (substring s 0 b)
  14280. (substring s (or (next-single-property-change
  14281. b 'invisible s) (length s)))))))
  14282. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14283. (setq s (concat (substring s 0 b)
  14284. (substring s (or (next-single-property-change
  14285. b 'org-cwidth s) (length s))))))
  14286. (setq l (string-width s) b -1)
  14287. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14288. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14289. l))
  14290. (defun org-get-indentation (&optional line)
  14291. "Get the indentation of the current line, interpreting tabs.
  14292. When LINE is given, assume it represents a line and compute its indentation."
  14293. (if line
  14294. (if (string-match "^ *" (org-remove-tabs line))
  14295. (match-end 0))
  14296. (save-excursion
  14297. (beginning-of-line 1)
  14298. (skip-chars-forward " \t")
  14299. (current-column))))
  14300. (defun org-remove-tabs (s &optional width)
  14301. "Replace tabulators in S with spaces.
  14302. Assumes that s is a single line, starting in column 0."
  14303. (setq width (or width tab-width))
  14304. (while (string-match "\t" s)
  14305. (setq s (replace-match
  14306. (make-string
  14307. (- (* width (/ (+ (match-beginning 0) width) width))
  14308. (match-beginning 0)) ?\ )
  14309. t t s)))
  14310. s)
  14311. (defun org-fix-indentation (line ind)
  14312. "Fix indentation in LINE.
  14313. IND is a cons cell with target and minimum indentation.
  14314. If the current indentation in LINE is smaller than the minimum,
  14315. leave it alone. If it is larger than ind, set it to the target."
  14316. (let* ((l (org-remove-tabs line))
  14317. (i (org-get-indentation l))
  14318. (i1 (car ind)) (i2 (cdr ind)))
  14319. (if (>= i i2) (setq l (substring line i2)))
  14320. (if (> i1 0)
  14321. (concat (make-string i1 ?\ ) l)
  14322. l)))
  14323. (defun org-remove-indentation (code &optional n)
  14324. "Remove the maximum common indentation from the lines in CODE.
  14325. N may optionally be the number of spaces to remove."
  14326. (with-temp-buffer
  14327. (insert code)
  14328. (org-do-remove-indentation n)
  14329. (buffer-string)))
  14330. (defun org-do-remove-indentation (&optional n)
  14331. "Remove the maximum common indentation from the buffer."
  14332. (untabify (point-min) (point-max))
  14333. (let ((min 10000) re)
  14334. (if n
  14335. (setq min n)
  14336. (goto-char (point-min))
  14337. (while (re-search-forward "^ *[^ \n]" nil t)
  14338. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14339. (unless (or (= min 0) (= min 10000))
  14340. (setq re (format "^ \\{%d\\}" min))
  14341. (goto-char (point-min))
  14342. (while (re-search-forward re nil t)
  14343. (replace-match "")
  14344. (end-of-line 1))
  14345. min)))
  14346. (defun org-base-buffer (buffer)
  14347. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14348. (if (not buffer)
  14349. buffer
  14350. (or (buffer-base-buffer buffer)
  14351. buffer)))
  14352. (defun org-trim (s)
  14353. "Remove whitespace at beginning and end of string."
  14354. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14355. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14356. s)
  14357. (defun org-wrap (string &optional width lines)
  14358. "Wrap string to either a number of lines, or a width in characters.
  14359. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14360. that costs. If there is a word longer than WIDTH, the text is actually
  14361. wrapped to the length of that word.
  14362. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14363. many lines, whatever width that takes.
  14364. The return value is a list of lines, without newlines at the end."
  14365. (let* ((words (org-split-string string "[ \t\n]+"))
  14366. (maxword (apply 'max (mapcar 'org-string-width words)))
  14367. w ll)
  14368. (cond (width
  14369. (org-do-wrap words (max maxword width)))
  14370. (lines
  14371. (setq w maxword)
  14372. (setq ll (org-do-wrap words maxword))
  14373. (if (<= (length ll) lines)
  14374. ll
  14375. (setq ll words)
  14376. (while (> (length ll) lines)
  14377. (setq w (1+ w))
  14378. (setq ll (org-do-wrap words w)))
  14379. ll))
  14380. (t (error "Cannot wrap this")))))
  14381. (defun org-do-wrap (words width)
  14382. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14383. (let (lines line)
  14384. (while words
  14385. (setq line (pop words))
  14386. (while (and words (< (+ (length line) (length (car words))) width))
  14387. (setq line (concat line " " (pop words))))
  14388. (setq lines (push line lines)))
  14389. (nreverse lines)))
  14390. (defun org-split-string (string &optional separators)
  14391. "Splits STRING into substrings at SEPARATORS.
  14392. No empty strings are returned if there are matches at the beginning
  14393. and end of string."
  14394. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14395. (start 0)
  14396. notfirst
  14397. (list nil))
  14398. (while (and (string-match rexp string
  14399. (if (and notfirst
  14400. (= start (match-beginning 0))
  14401. (< start (length string)))
  14402. (1+ start) start))
  14403. (< (match-beginning 0) (length string)))
  14404. (setq notfirst t)
  14405. (or (eq (match-beginning 0) 0)
  14406. (and (eq (match-beginning 0) (match-end 0))
  14407. (eq (match-beginning 0) start))
  14408. (setq list
  14409. (cons (substring string start (match-beginning 0))
  14410. list)))
  14411. (setq start (match-end 0)))
  14412. (or (eq start (length string))
  14413. (setq list
  14414. (cons (substring string start)
  14415. list)))
  14416. (nreverse list)))
  14417. (defun org-quote-vert (s)
  14418. "Replace \"|\" with \"\\vert\"."
  14419. (while (string-match "|" s)
  14420. (setq s (replace-match "\\vert" t t s)))
  14421. s)
  14422. (defun org-uuidgen-p (s)
  14423. "Is S an ID created by UUIDGEN?"
  14424. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14425. (defun org-context ()
  14426. "Return a list of contexts of the current cursor position.
  14427. If several contexts apply, all are returned.
  14428. Each context entry is a list with a symbol naming the context, and
  14429. two positions indicating start and end of the context. Possible
  14430. contexts are:
  14431. :headline anywhere in a headline
  14432. :headline-stars on the leading stars in a headline
  14433. :todo-keyword on a TODO keyword (including DONE) in a headline
  14434. :tags on the TAGS in a headline
  14435. :priority on the priority cookie in a headline
  14436. :item on the first line of a plain list item
  14437. :item-bullet on the bullet/number of a plain list item
  14438. :checkbox on the checkbox in a plain list item
  14439. :table in an org-mode table
  14440. :table-special on a special filed in a table
  14441. :table-table in a table.el table
  14442. :link on a hyperlink
  14443. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14444. :target on a <<target>>
  14445. :radio-target on a <<<radio-target>>>
  14446. :latex-fragment on a LaTeX fragment
  14447. :latex-preview on a LaTeX fragment with overlayed preview image
  14448. This function expects the position to be visible because it uses font-lock
  14449. faces as a help to recognize the following contexts: :table-special, :link,
  14450. and :keyword."
  14451. (let* ((f (get-text-property (point) 'face))
  14452. (faces (if (listp f) f (list f)))
  14453. (p (point)) clist o)
  14454. ;; First the large context
  14455. (cond
  14456. ((org-on-heading-p t)
  14457. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14458. (when (progn
  14459. (beginning-of-line 1)
  14460. (looking-at org-todo-line-tags-regexp))
  14461. (push (org-point-in-group p 1 :headline-stars) clist)
  14462. (push (org-point-in-group p 2 :todo-keyword) clist)
  14463. (push (org-point-in-group p 4 :tags) clist))
  14464. (goto-char p)
  14465. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14466. (if (looking-at "\\[#[A-Z0-9]\\]")
  14467. (push (org-point-in-group p 0 :priority) clist)))
  14468. ((org-at-item-p)
  14469. (push (org-point-in-group p 2 :item-bullet) clist)
  14470. (push (list :item (point-at-bol)
  14471. (save-excursion (org-end-of-item) (point)))
  14472. clist)
  14473. (and (org-at-item-checkbox-p)
  14474. (push (org-point-in-group p 0 :checkbox) clist)))
  14475. ((org-at-table-p)
  14476. (push (list :table (org-table-begin) (org-table-end)) clist)
  14477. (if (memq 'org-formula faces)
  14478. (push (list :table-special
  14479. (previous-single-property-change p 'face)
  14480. (next-single-property-change p 'face)) clist)))
  14481. ((org-at-table-p 'any)
  14482. (push (list :table-table) clist)))
  14483. (goto-char p)
  14484. ;; Now the small context
  14485. (cond
  14486. ((org-at-timestamp-p)
  14487. (push (org-point-in-group p 0 :timestamp) clist))
  14488. ((memq 'org-link faces)
  14489. (push (list :link
  14490. (previous-single-property-change p 'face)
  14491. (next-single-property-change p 'face)) clist))
  14492. ((memq 'org-special-keyword faces)
  14493. (push (list :keyword
  14494. (previous-single-property-change p 'face)
  14495. (next-single-property-change p 'face)) clist))
  14496. ((org-on-target-p)
  14497. (push (org-point-in-group p 0 :target) clist)
  14498. (goto-char (1- (match-beginning 0)))
  14499. (if (looking-at org-radio-target-regexp)
  14500. (push (org-point-in-group p 0 :radio-target) clist))
  14501. (goto-char p))
  14502. ((setq o (car (delq nil
  14503. (mapcar
  14504. (lambda (x)
  14505. (if (memq x org-latex-fragment-image-overlays) x))
  14506. (org-overlays-at (point))))))
  14507. (push (list :latex-fragment
  14508. (org-overlay-start o) (org-overlay-end o)) clist)
  14509. (push (list :latex-preview
  14510. (org-overlay-start o) (org-overlay-end o)) clist))
  14511. ((org-inside-LaTeX-fragment-p)
  14512. ;; FIXME: positions wrong.
  14513. (push (list :latex-fragment (point) (point)) clist)))
  14514. (setq clist (nreverse (delq nil clist)))
  14515. clist))
  14516. ;; FIXME: Compare with at-regexp-p Do we need both?
  14517. (defun org-in-regexp (re &optional nlines visually)
  14518. "Check if point is inside a match of regexp.
  14519. Normally only the current line is checked, but you can include NLINES extra
  14520. lines both before and after point into the search.
  14521. If VISUALLY is set, require that the cursor is not after the match but
  14522. really on, so that the block visually is on the match."
  14523. (catch 'exit
  14524. (let ((pos (point))
  14525. (eol (point-at-eol (+ 1 (or nlines 0))))
  14526. (inc (if visually 1 0)))
  14527. (save-excursion
  14528. (beginning-of-line (- 1 (or nlines 0)))
  14529. (while (re-search-forward re eol t)
  14530. (if (and (<= (match-beginning 0) pos)
  14531. (>= (+ inc (match-end 0)) pos))
  14532. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14533. (defun org-at-regexp-p (regexp)
  14534. "Is point inside a match of REGEXP in the current line?"
  14535. (catch 'exit
  14536. (save-excursion
  14537. (let ((pos (point)) (end (point-at-eol)))
  14538. (beginning-of-line 1)
  14539. (while (re-search-forward regexp end t)
  14540. (if (and (<= (match-beginning 0) pos)
  14541. (>= (match-end 0) pos))
  14542. (throw 'exit t)))
  14543. nil))))
  14544. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14545. "Call `multi-occur' with buffers for all agenda files."
  14546. (interactive "sOrg-files matching: \np")
  14547. (let* ((files (org-agenda-files))
  14548. (tnames (mapcar 'file-truename files))
  14549. (extra org-agenda-text-search-extra-files)
  14550. f)
  14551. (when (eq (car extra) 'agenda-archives)
  14552. (setq extra (cdr extra))
  14553. (setq files (org-add-archive-files files)))
  14554. (while (setq f (pop extra))
  14555. (unless (member (file-truename f) tnames)
  14556. (add-to-list 'files f 'append)
  14557. (add-to-list 'tnames (file-truename f) 'append)))
  14558. (multi-occur
  14559. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14560. regexp)))
  14561. (if (boundp 'occur-mode-find-occurrence-hook)
  14562. ;; Emacs 23
  14563. (add-hook 'occur-mode-find-occurrence-hook
  14564. (lambda ()
  14565. (when (org-mode-p)
  14566. (org-reveal))))
  14567. ;; Emacs 22
  14568. (defadvice occur-mode-goto-occurrence
  14569. (after org-occur-reveal activate)
  14570. (and (org-mode-p) (org-reveal)))
  14571. (defadvice occur-mode-goto-occurrence-other-window
  14572. (after org-occur-reveal activate)
  14573. (and (org-mode-p) (org-reveal)))
  14574. (defadvice occur-mode-display-occurrence
  14575. (after org-occur-reveal activate)
  14576. (when (org-mode-p)
  14577. (let ((pos (occur-mode-find-occurrence)))
  14578. (with-current-buffer (marker-buffer pos)
  14579. (save-excursion
  14580. (goto-char pos)
  14581. (org-reveal)))))))
  14582. (defun org-occur-link-in-agenda-files ()
  14583. "Create a link and search for it in the agendas.
  14584. The link is not stored in `org-stored-links', it is just created
  14585. for the search purpose."
  14586. (interactive)
  14587. (let ((link (condition-case nil
  14588. (org-store-link nil)
  14589. (error "Unable to create a link to here"))))
  14590. (org-occur-in-agenda-files (regexp-quote link))))
  14591. (defun org-uniquify (list)
  14592. "Remove duplicate elements from LIST."
  14593. (let (res)
  14594. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14595. res))
  14596. (defun org-delete-all (elts list)
  14597. "Remove all elements in ELTS from LIST."
  14598. (while elts
  14599. (setq list (delete (pop elts) list)))
  14600. list)
  14601. (defun org-back-over-empty-lines ()
  14602. "Move backwards over whitespace, to the beginning of the first empty line.
  14603. Returns the number of empty lines passed."
  14604. (let ((pos (point)))
  14605. (skip-chars-backward " \t\n\r")
  14606. (beginning-of-line 2)
  14607. (goto-char (min (point) pos))
  14608. (count-lines (point) pos)))
  14609. (defun org-skip-whitespace ()
  14610. (skip-chars-forward " \t\n\r"))
  14611. (defun org-point-in-group (point group &optional context)
  14612. "Check if POINT is in match-group GROUP.
  14613. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14614. match. If the match group does ot exist or point is not inside it,
  14615. return nil."
  14616. (and (match-beginning group)
  14617. (>= point (match-beginning group))
  14618. (<= point (match-end group))
  14619. (if context
  14620. (list context (match-beginning group) (match-end group))
  14621. t)))
  14622. (defun org-switch-to-buffer-other-window (&rest args)
  14623. "Switch to buffer in a second window on the current frame.
  14624. In particular, do not allow pop-up frames."
  14625. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14626. special-display-function)
  14627. (apply 'switch-to-buffer-other-window args)))
  14628. (defun org-combine-plists (&rest plists)
  14629. "Create a single property list from all plists in PLISTS.
  14630. The process starts by copying the first list, and then setting properties
  14631. from the other lists. Settings in the last list are the most significant
  14632. ones and overrule settings in the other lists."
  14633. (let ((rtn (copy-sequence (pop plists)))
  14634. p v ls)
  14635. (while plists
  14636. (setq ls (pop plists))
  14637. (while ls
  14638. (setq p (pop ls) v (pop ls))
  14639. (setq rtn (plist-put rtn p v))))
  14640. rtn))
  14641. (defun org-move-line-down (arg)
  14642. "Move the current line down. With prefix argument, move it past ARG lines."
  14643. (interactive "p")
  14644. (let ((col (current-column))
  14645. beg end pos)
  14646. (beginning-of-line 1) (setq beg (point))
  14647. (beginning-of-line 2) (setq end (point))
  14648. (beginning-of-line (+ 1 arg))
  14649. (setq pos (move-marker (make-marker) (point)))
  14650. (insert (delete-and-extract-region beg end))
  14651. (goto-char pos)
  14652. (org-move-to-column col)))
  14653. (defun org-move-line-up (arg)
  14654. "Move the current line up. With prefix argument, move it past ARG lines."
  14655. (interactive "p")
  14656. (let ((col (current-column))
  14657. beg end pos)
  14658. (beginning-of-line 1) (setq beg (point))
  14659. (beginning-of-line 2) (setq end (point))
  14660. (beginning-of-line (- arg))
  14661. (setq pos (move-marker (make-marker) (point)))
  14662. (insert (delete-and-extract-region beg end))
  14663. (goto-char pos)
  14664. (org-move-to-column col)))
  14665. (defun org-replace-escapes (string table)
  14666. "Replace %-escapes in STRING with values in TABLE.
  14667. TABLE is an association list with keys like \"%a\" and string values.
  14668. The sequences in STRING may contain normal field width and padding information,
  14669. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14670. so values can contain further %-escapes if they are define later in TABLE."
  14671. (let ((case-fold-search nil)
  14672. e re rpl)
  14673. (while (setq e (pop table))
  14674. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14675. (while (string-match re string)
  14676. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14677. (cdr e)))
  14678. (setq string (replace-match rpl t t string))))
  14679. string))
  14680. (defun org-sublist (list start end)
  14681. "Return a section of LIST, from START to END.
  14682. Counting starts at 1."
  14683. (let (rtn (c start))
  14684. (setq list (nthcdr (1- start) list))
  14685. (while (and list (<= c end))
  14686. (push (pop list) rtn)
  14687. (setq c (1+ c)))
  14688. (nreverse rtn)))
  14689. (defun org-find-base-buffer-visiting (file)
  14690. "Like `find-buffer-visiting' but always return the base buffer and
  14691. not an indirect buffer."
  14692. (let ((buf (or (get-file-buffer file)
  14693. (find-buffer-visiting file))))
  14694. (if buf
  14695. (or (buffer-base-buffer buf) buf)
  14696. nil)))
  14697. (defun org-image-file-name-regexp (&optional extensions)
  14698. "Return regexp matching the file names of images.
  14699. If EXTENSIONS is given, only match these."
  14700. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14701. (image-file-name-regexp)
  14702. (let ((image-file-name-extensions
  14703. (or extensions
  14704. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14705. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14706. (concat "\\."
  14707. (regexp-opt (nconc (mapcar 'upcase
  14708. image-file-name-extensions)
  14709. image-file-name-extensions)
  14710. t)
  14711. "\\'"))))
  14712. (defun org-file-image-p (file &optional extensions)
  14713. "Return non-nil if FILE is an image."
  14714. (save-match-data
  14715. (string-match (org-image-file-name-regexp extensions) file)))
  14716. (defun org-get-cursor-date ()
  14717. "Return the date at cursor in as a time.
  14718. This works in the calendar and in the agenda, anywhere else it just
  14719. returns the current time."
  14720. (let (date day defd)
  14721. (cond
  14722. ((eq major-mode 'calendar-mode)
  14723. (setq date (calendar-cursor-to-date)
  14724. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14725. ((eq major-mode 'org-agenda-mode)
  14726. (setq day (get-text-property (point) 'day))
  14727. (if day
  14728. (setq date (calendar-gregorian-from-absolute day)
  14729. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14730. (nth 2 date))))))
  14731. (or defd (current-time))))
  14732. (defvar org-agenda-action-marker (make-marker)
  14733. "Marker pointing to the entry for the next agenda action.")
  14734. (defun org-mark-entry-for-agenda-action ()
  14735. "Mark the current entry as target of an agenda action.
  14736. Agenda actions are actions executed from the agenda with the key `k',
  14737. which make use of the date at the cursor."
  14738. (interactive)
  14739. (move-marker org-agenda-action-marker
  14740. (save-excursion (org-back-to-heading t) (point))
  14741. (current-buffer))
  14742. (message
  14743. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14744. ;;; Paragraph filling stuff.
  14745. ;; We want this to be just right, so use the full arsenal.
  14746. (defun org-indent-line-function ()
  14747. "Indent line like previous, but further if previous was headline or item."
  14748. (interactive)
  14749. (let* ((pos (point))
  14750. (itemp (org-at-item-p))
  14751. (case-fold-search t)
  14752. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14753. column bpos bcol tpos tcol bullet btype bullet-type)
  14754. ;; Find the previous relevant line
  14755. (beginning-of-line 1)
  14756. (cond
  14757. ((looking-at "#") (setq column 0))
  14758. ((looking-at "\\*+ ") (setq column 0))
  14759. ((and (looking-at "[ \t]*:END:")
  14760. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14761. (save-excursion
  14762. (goto-char (1- (match-beginning 1)))
  14763. (setq column (current-column))))
  14764. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14765. (save-excursion
  14766. (re-search-backward
  14767. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14768. (setq column (org-get-indentation (match-string 0))))
  14769. (t
  14770. (beginning-of-line 0)
  14771. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14772. (not (looking-at "[ \t]*:END:"))
  14773. (not (looking-at org-drawer-regexp)))
  14774. (beginning-of-line 0))
  14775. (cond
  14776. ((looking-at "\\*+[ \t]+")
  14777. (if (not org-adapt-indentation)
  14778. (setq column 0)
  14779. (goto-char (match-end 0))
  14780. (setq column (current-column))))
  14781. ((looking-at org-drawer-regexp)
  14782. (goto-char (1- (match-beginning 1)))
  14783. (setq column (current-column)))
  14784. ((looking-at "\\([ \t]*\\):END:")
  14785. (goto-char (match-end 1))
  14786. (setq column (current-column)))
  14787. ((org-in-item-p)
  14788. (org-beginning-of-item)
  14789. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14790. (setq bpos (match-beginning 1) tpos (match-end 0)
  14791. bcol (progn (goto-char bpos) (current-column))
  14792. tcol (progn (goto-char tpos) (current-column))
  14793. bullet (match-string 1)
  14794. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14795. (if (> tcol (+ bcol org-description-max-indent))
  14796. (setq tcol (+ bcol 5)))
  14797. (if (not itemp)
  14798. (setq column tcol)
  14799. (goto-char pos)
  14800. (beginning-of-line 1)
  14801. (if (looking-at "\\S-")
  14802. (progn
  14803. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14804. (setq bullet (match-string 1)
  14805. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14806. (setq column (if (equal btype bullet-type) bcol tcol)))
  14807. (setq column (org-get-indentation)))))
  14808. (t (setq column (org-get-indentation))))))
  14809. (goto-char pos)
  14810. (if (<= (current-column) (current-indentation))
  14811. (org-indent-line-to column)
  14812. (save-excursion (org-indent-line-to column)))
  14813. (setq column (current-column))
  14814. (beginning-of-line 1)
  14815. (if (looking-at
  14816. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14817. (replace-match (concat "\\1" (format org-property-format
  14818. (match-string 2) (match-string 3)))
  14819. t nil))
  14820. (org-move-to-column column)))
  14821. (defun org-set-autofill-regexps ()
  14822. (interactive)
  14823. ;; In the paragraph separator we include headlines, because filling
  14824. ;; text in a line directly attached to a headline would otherwise
  14825. ;; fill the headline as well.
  14826. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14827. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14828. ;; The paragraph starter includes hand-formatted lists.
  14829. (org-set-local
  14830. 'paragraph-start
  14831. (concat
  14832. "\f" "\\|"
  14833. "[ ]*$" "\\|"
  14834. "\\*+ " "\\|"
  14835. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14836. "[ \t]*[:|]" "\\|"
  14837. "\\$\\$" "\\|"
  14838. "\\\\\\(begin\\|end\\|[][]\\)"))
  14839. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14840. ;; But only if the user has not turned off tables or fixed-width regions
  14841. (org-set-local
  14842. 'auto-fill-inhibit-regexp
  14843. (concat "\\*+ \\|#\\+"
  14844. "\\|[ \t]*" org-keyword-time-regexp
  14845. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14846. (concat
  14847. "\\|[ \t]*["
  14848. (if org-enable-table-editor "|" "")
  14849. (if org-enable-fixed-width-editor ":" "")
  14850. "]"))))
  14851. ;; We use our own fill-paragraph function, to make sure that tables
  14852. ;; and fixed-width regions are not wrapped. That function will pass
  14853. ;; through to `fill-paragraph' when appropriate.
  14854. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14855. ; Adaptive filling: To get full control, first make sure that
  14856. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14857. (org-set-local 'adaptive-fill-regexp "\000")
  14858. (org-set-local 'adaptive-fill-function
  14859. 'org-adaptive-fill-function)
  14860. (org-set-local
  14861. 'align-mode-rules-list
  14862. '((org-in-buffer-settings
  14863. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14864. (modes . '(org-mode))))))
  14865. (defun org-fill-paragraph (&optional justify)
  14866. "Re-align a table, pass through to fill-paragraph if no table."
  14867. (let ((table-p (org-at-table-p))
  14868. (table.el-p (org-at-table.el-p)))
  14869. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14870. (save-excursion (goto-char (point-at-bol))
  14871. (looking-at outline-regexp)))
  14872. t) ; skip headlines
  14873. (table.el-p t) ; skip table.el tables
  14874. (table-p (org-table-align) t) ; align org-mode tables
  14875. (t nil)))) ; call paragraph-fill
  14876. ;; For reference, this is the default value of adaptive-fill-regexp
  14877. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14878. (defun org-adaptive-fill-function ()
  14879. "Return a fill prefix for org-mode files.
  14880. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14881. work correctly."
  14882. (cond ((looking-at "#[ \t]+")
  14883. (match-string 0))
  14884. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14885. (save-excursion
  14886. (if (> (match-end 1) (+ (match-beginning 1)
  14887. org-description-max-indent))
  14888. (goto-char (+ (match-beginning 1) 5))
  14889. (goto-char (match-end 0)))
  14890. (make-string (current-column) ?\ )))
  14891. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14892. (save-excursion
  14893. (goto-char (match-end 0))
  14894. (make-string (current-column) ?\ )))
  14895. (t nil)))
  14896. ;;; Other stuff.
  14897. (defun org-toggle-fixed-width-section (arg)
  14898. "Toggle the fixed-width export.
  14899. If there is no active region, the QUOTE keyword at the current headline is
  14900. inserted or removed. When present, it causes the text between this headline
  14901. and the next to be exported as fixed-width text, and unmodified.
  14902. If there is an active region, this command adds or removes a colon as the
  14903. first character of this line. If the first character of a line is a colon,
  14904. this line is also exported in fixed-width font."
  14905. (interactive "P")
  14906. (let* ((cc 0)
  14907. (regionp (org-region-active-p))
  14908. (beg (if regionp (region-beginning) (point)))
  14909. (end (if regionp (region-end)))
  14910. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14911. (case-fold-search nil)
  14912. (re "[ \t]*\\(: \\)")
  14913. off)
  14914. (if regionp
  14915. (save-excursion
  14916. (goto-char beg)
  14917. (setq cc (current-column))
  14918. (beginning-of-line 1)
  14919. (setq off (looking-at re))
  14920. (while (> nlines 0)
  14921. (setq nlines (1- nlines))
  14922. (beginning-of-line 1)
  14923. (cond
  14924. (arg
  14925. (org-move-to-column cc t)
  14926. (insert ": \n")
  14927. (forward-line -1))
  14928. ((and off (looking-at re))
  14929. (replace-match "" t t nil 1))
  14930. ((not off) (org-move-to-column cc t) (insert ": ")))
  14931. (forward-line 1)))
  14932. (save-excursion
  14933. (org-back-to-heading)
  14934. (if (looking-at (concat outline-regexp
  14935. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14936. (replace-match "" t t nil 1)
  14937. (if (looking-at outline-regexp)
  14938. (progn
  14939. (goto-char (match-end 0))
  14940. (insert org-quote-string " "))))))))
  14941. (defun org-reftex-citation ()
  14942. "Use reftex-citation to insert a citation into the buffer.
  14943. This looks for a line like
  14944. #+BIBLIOGRAPHY: foo plain option:-d
  14945. and derives from it that foo.bib is the bbliography file relevant
  14946. for this document. It then installs the necessary environment for RefTeX
  14947. to work in this buffer and calls `reftex-citation' to insert a citation
  14948. into the buffer.
  14949. Export of such citations to both LaTeX and HTML is handled by the contributed
  14950. package org-exp-bibtex by Taru Karttunen."
  14951. (interactive)
  14952. (let ((reftex-docstruct-symbol 'rds)
  14953. (reftex-cite-format "\\cite{%l}")
  14954. rds bib)
  14955. (save-excursion
  14956. (save-restriction
  14957. (widen)
  14958. (let ((case-fold-search t)
  14959. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14960. (if (not (save-excursion
  14961. (or (re-search-forward re nil t)
  14962. (re-search-backward re nil t))))
  14963. (error "No bibliography defined in file")
  14964. (setq bib (concat (match-string 1) ".bib")
  14965. rds (list (list 'bib bib)))))))
  14966. (call-interactively 'reftex-citation)))
  14967. ;;;; Functions extending outline functionality
  14968. (defun org-beginning-of-line (&optional arg)
  14969. "Go to the beginning of the current line. If that is invisible, continue
  14970. to a visible line beginning. This makes the function of C-a more intuitive.
  14971. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14972. first attempt, and only move to after the tags when the cursor is already
  14973. beyond the end of the headline."
  14974. (interactive "P")
  14975. (let ((pos (point))
  14976. (special (if (consp org-special-ctrl-a/e)
  14977. (car org-special-ctrl-a/e)
  14978. org-special-ctrl-a/e))
  14979. refpos)
  14980. (if (org-bound-and-true-p line-move-visual)
  14981. (beginning-of-visual-line 1)
  14982. (beginning-of-line 1))
  14983. (if (and arg (fboundp 'move-beginning-of-line))
  14984. (call-interactively 'move-beginning-of-line)
  14985. (if (bobp)
  14986. nil
  14987. (backward-char 1)
  14988. (if (org-invisible-p)
  14989. (while (and (not (bobp)) (org-invisible-p))
  14990. (backward-char 1)
  14991. (beginning-of-line 1))
  14992. (forward-char 1))))
  14993. (when special
  14994. (cond
  14995. ((and (looking-at org-complex-heading-regexp)
  14996. (= (char-after (match-end 1)) ?\ ))
  14997. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14998. (point-at-eol)))
  14999. (goto-char
  15000. (if (eq special t)
  15001. (cond ((> pos refpos) refpos)
  15002. ((= pos (point)) refpos)
  15003. (t (point)))
  15004. (cond ((> pos (point)) (point))
  15005. ((not (eq last-command this-command)) (point))
  15006. (t refpos)))))
  15007. ((org-at-item-p)
  15008. (goto-char
  15009. (if (eq special t)
  15010. (cond ((> pos (match-end 4)) (match-end 4))
  15011. ((= pos (point)) (match-end 4))
  15012. (t (point)))
  15013. (cond ((> pos (point)) (point))
  15014. ((not (eq last-command this-command)) (point))
  15015. (t (match-end 4))))))))
  15016. (org-no-warnings
  15017. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15018. (defun org-end-of-line (&optional arg)
  15019. "Go to the end of the line.
  15020. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15021. first attempt, and only move to after the tags when the cursor is already
  15022. beyond the end of the headline."
  15023. (interactive "P")
  15024. (let ((special (if (consp org-special-ctrl-a/e)
  15025. (cdr org-special-ctrl-a/e)
  15026. org-special-ctrl-a/e)))
  15027. (if (or (not special)
  15028. (not (org-on-heading-p))
  15029. arg)
  15030. (call-interactively
  15031. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15032. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15033. (t 'end-of-line)))
  15034. (let ((pos (point)))
  15035. (beginning-of-line 1)
  15036. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15037. (if (eq special t)
  15038. (if (or (< pos (match-beginning 1))
  15039. (= pos (match-end 0)))
  15040. (goto-char (match-beginning 1))
  15041. (goto-char (match-end 0)))
  15042. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15043. (goto-char (match-end 0))
  15044. (goto-char (match-beginning 1))))
  15045. (call-interactively (if (fboundp 'move-end-of-line)
  15046. 'move-end-of-line
  15047. 'end-of-line)))))
  15048. (org-no-warnings
  15049. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15050. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15051. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15052. (defun org-backward-sentence (&optional arg)
  15053. "Go to beginning of sentence, or beginning of table field.
  15054. This will call `backward-sentence' or `org-table-beginning-of-field',
  15055. depending on context."
  15056. (interactive "P")
  15057. (cond
  15058. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15059. (t (call-interactively 'backward-sentence))))
  15060. (defun org-forward-sentence (&optional arg)
  15061. "Go to end of sentence, or end of table field.
  15062. This will call `forward-sentence' or `org-table-end-of-field',
  15063. depending on context."
  15064. (interactive "P")
  15065. (cond
  15066. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15067. (t (call-interactively 'forward-sentence))))
  15068. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15069. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15070. (defun org-kill-line (&optional arg)
  15071. "Kill line, to tags or end of line."
  15072. (interactive "P")
  15073. (cond
  15074. ((or (not org-special-ctrl-k)
  15075. (bolp)
  15076. (not (org-on-heading-p)))
  15077. (call-interactively 'kill-line))
  15078. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15079. (kill-region (point) (match-beginning 1))
  15080. (org-set-tags nil t))
  15081. (t (kill-region (point) (point-at-eol)))))
  15082. (define-key org-mode-map "\C-k" 'org-kill-line)
  15083. (defun org-yank (&optional arg)
  15084. "Yank. If the kill is a subtree, treat it specially.
  15085. This command will look at the current kill and check if is a single
  15086. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15087. cursor is at the beginning of a line or after the stars of a currently
  15088. empty headline, then the yank is handled specially. How exactly depends
  15089. on the value of the following variables, both set by default.
  15090. org-yank-folded-subtrees
  15091. When set, the subtree(s) will be folded after insertion, but only
  15092. if doing so would now swallow text after the yanked text.
  15093. org-yank-adjusted-subtrees
  15094. When set, the subtree will be promoted or demoted in order to
  15095. fit into the local outline tree structure, which means that the level
  15096. will be adjusted so that it becomes the smaller one of the two
  15097. *visible* surrounding headings.
  15098. Any prefix to this command will cause `yank' to be called directly with
  15099. no special treatment. In particular, a simple `C-u' prefix will just
  15100. plainly yank the text as it is.
  15101. \[1] The test checks if the first non-white line is a heading
  15102. and if there are no other headings with fewer stars."
  15103. (interactive "P")
  15104. (org-yank-generic 'yank arg))
  15105. (defun org-yank-generic (command arg)
  15106. "Perform some yank-like command.
  15107. This function implements the behavior described in the `org-yank'
  15108. documentation. However, it has been generalized to work for any
  15109. interactive command with similar behavior."
  15110. ;; pretend to be command COMMAND
  15111. (setq this-command command)
  15112. (if arg
  15113. (call-interactively command)
  15114. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15115. (and (org-kill-is-subtree-p)
  15116. (or (bolp)
  15117. (and (looking-at "[ \t]*$")
  15118. (string-match
  15119. "\\`\\*+\\'"
  15120. (buffer-substring (point-at-bol) (point)))))))
  15121. swallowp)
  15122. (cond
  15123. ((and subtreep org-yank-folded-subtrees)
  15124. (let ((beg (point))
  15125. end)
  15126. (if (and subtreep org-yank-adjusted-subtrees)
  15127. (org-paste-subtree nil nil 'for-yank)
  15128. (call-interactively command))
  15129. (setq end (point))
  15130. (goto-char beg)
  15131. (when (and (bolp) subtreep
  15132. (not (setq swallowp
  15133. (org-yank-folding-would-swallow-text beg end))))
  15134. (or (looking-at outline-regexp)
  15135. (re-search-forward (concat "^" outline-regexp) end t))
  15136. (while (and (< (point) end) (looking-at outline-regexp))
  15137. (hide-subtree)
  15138. (org-cycle-show-empty-lines 'folded)
  15139. (condition-case nil
  15140. (outline-forward-same-level 1)
  15141. (error (goto-char end)))))
  15142. (when swallowp
  15143. (message
  15144. "Inserted text not folded because that would swallow text"))
  15145. (goto-char end)
  15146. (skip-chars-forward " \t\n\r")
  15147. (beginning-of-line 1)
  15148. (push-mark beg 'nomsg)))
  15149. ((and subtreep org-yank-adjusted-subtrees)
  15150. (let ((beg (point-at-bol)))
  15151. (org-paste-subtree nil nil 'for-yank)
  15152. (push-mark beg 'nomsg)))
  15153. (t
  15154. (call-interactively command))))))
  15155. (defun org-yank-folding-would-swallow-text (beg end)
  15156. "Would hide-subtree at BEG swallow any text after END?"
  15157. (let (level)
  15158. (save-excursion
  15159. (goto-char beg)
  15160. (when (or (looking-at outline-regexp)
  15161. (re-search-forward (concat "^" outline-regexp) end t))
  15162. (setq level (org-outline-level)))
  15163. (goto-char end)
  15164. (skip-chars-forward " \t\r\n\v\f")
  15165. (if (or (eobp)
  15166. (and (bolp) (looking-at org-outline-regexp)
  15167. (<= (org-outline-level) level)))
  15168. nil ; Nothing would be swallowed
  15169. t)))) ; something would swallow
  15170. (define-key org-mode-map "\C-y" 'org-yank)
  15171. (defun org-invisible-p ()
  15172. "Check if point is at a character currently not visible."
  15173. ;; Early versions of noutline don't have `outline-invisible-p'.
  15174. (if (fboundp 'outline-invisible-p)
  15175. (outline-invisible-p)
  15176. (get-char-property (point) 'invisible)))
  15177. (defun org-invisible-p2 ()
  15178. "Check if point is at a character currently not visible."
  15179. (save-excursion
  15180. (if (and (eolp) (not (bobp))) (backward-char 1))
  15181. ;; Early versions of noutline don't have `outline-invisible-p'.
  15182. (if (fboundp 'outline-invisible-p)
  15183. (outline-invisible-p)
  15184. (get-char-property (point) 'invisible))))
  15185. (defun org-back-to-heading (&optional invisible-ok)
  15186. "Call `outline-back-to-heading', but provide a better error message."
  15187. (condition-case nil
  15188. (outline-back-to-heading invisible-ok)
  15189. (error (error "Before first headline at position %d in buffer %s"
  15190. (point) (current-buffer)))))
  15191. (defun org-before-first-heading-p ()
  15192. "Before first heading?"
  15193. (save-excursion
  15194. (null (re-search-backward "^\\*+ " nil t))))
  15195. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15196. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15197. (defun org-at-heading-or-item-p ()
  15198. (or (org-on-heading-p) (org-at-item-p)))
  15199. (defun org-on-target-p ()
  15200. (or (org-in-regexp org-radio-target-regexp)
  15201. (org-in-regexp org-target-regexp)))
  15202. (defun org-up-heading-all (arg)
  15203. "Move to the heading line of which the present line is a subheading.
  15204. This function considers both visible and invisible heading lines.
  15205. With argument, move up ARG levels."
  15206. (if (fboundp 'outline-up-heading-all)
  15207. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15208. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15209. (defun org-up-heading-safe ()
  15210. "Move to the heading line of which the present line is a subheading.
  15211. This version will not throw an error. It will return the level of the
  15212. headline found, or nil if no higher level is found.
  15213. Also, this function will be a lot faster than `outline-up-heading',
  15214. because it relies on stars being the outline starters. This can really
  15215. make a significant difference in outlines with very many siblings."
  15216. (let (start-level re)
  15217. (org-back-to-heading t)
  15218. (setq start-level (funcall outline-level))
  15219. (if (equal start-level 1)
  15220. nil
  15221. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15222. (if (re-search-backward re nil t)
  15223. (funcall outline-level)))))
  15224. (defun org-first-sibling-p ()
  15225. "Is this heading the first child of its parents?"
  15226. (interactive)
  15227. (let ((re (concat "^" outline-regexp))
  15228. level l)
  15229. (unless (org-at-heading-p t)
  15230. (error "Not at a heading"))
  15231. (setq level (funcall outline-level))
  15232. (save-excursion
  15233. (if (not (re-search-backward re nil t))
  15234. t
  15235. (setq l (funcall outline-level))
  15236. (< l level)))))
  15237. (defun org-goto-sibling (&optional previous)
  15238. "Goto the next sibling, even if it is invisible.
  15239. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15240. when a sibling was found. When none is found, return nil and don't
  15241. move point."
  15242. (let ((fun (if previous 're-search-backward 're-search-forward))
  15243. (pos (point))
  15244. (re (concat "^" outline-regexp))
  15245. level l)
  15246. (when (condition-case nil (org-back-to-heading t) (error nil))
  15247. (setq level (funcall outline-level))
  15248. (catch 'exit
  15249. (or previous (forward-char 1))
  15250. (while (funcall fun re nil t)
  15251. (setq l (funcall outline-level))
  15252. (when (< l level) (goto-char pos) (throw 'exit nil))
  15253. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15254. (goto-char pos)
  15255. nil))))
  15256. (defun org-show-siblings ()
  15257. "Show all siblings of the current headline."
  15258. (save-excursion
  15259. (while (org-goto-sibling) (org-flag-heading nil)))
  15260. (save-excursion
  15261. (while (org-goto-sibling 'previous)
  15262. (org-flag-heading nil))))
  15263. (defun org-show-hidden-entry ()
  15264. "Show an entry where even the heading is hidden."
  15265. (save-excursion
  15266. (org-show-entry)))
  15267. (defun org-flag-heading (flag &optional entry)
  15268. "Flag the current heading. FLAG non-nil means make invisible.
  15269. When ENTRY is non-nil, show the entire entry."
  15270. (save-excursion
  15271. (org-back-to-heading t)
  15272. ;; Check if we should show the entire entry
  15273. (if entry
  15274. (progn
  15275. (org-show-entry)
  15276. (save-excursion
  15277. (and (outline-next-heading)
  15278. (org-flag-heading nil))))
  15279. (outline-flag-region (max (point-min) (1- (point)))
  15280. (save-excursion (outline-end-of-heading) (point))
  15281. flag))))
  15282. (defun org-get-next-sibling ()
  15283. "Move to next heading of the same level, and return point.
  15284. If there is no such heading, return nil.
  15285. This is like outline-next-sibling, but invisible headings are ok."
  15286. (let ((level (funcall outline-level)))
  15287. (outline-next-heading)
  15288. (while (and (not (eobp)) (> (funcall outline-level) level))
  15289. (outline-next-heading))
  15290. (if (or (eobp) (< (funcall outline-level) level))
  15291. nil
  15292. (point))))
  15293. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15294. ;; This contains an exact copy of the original function, but it uses
  15295. ;; `org-back-to-heading', to make it work also in invisible
  15296. ;; trees. And is uses an invisible-OK argument.
  15297. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15298. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15299. ;; with many children and grandchildren etc, this can be much faster
  15300. ;; than the outline version.
  15301. (org-back-to-heading invisible-OK)
  15302. (let ((first t)
  15303. (level (funcall outline-level)))
  15304. (if (and (org-mode-p) (< level 1000))
  15305. ;; A true heading (not a plain list item), in Org-mode
  15306. ;; This means we can easily find the end by looking
  15307. ;; only for the right number of stars. Using a regexp to do
  15308. ;; this is so much faster than using a Lisp loop.
  15309. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15310. (forward-char 1)
  15311. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15312. ;; something else, do it the slow way
  15313. (while (and (not (eobp))
  15314. (or first (> (funcall outline-level) level)))
  15315. (setq first nil)
  15316. (outline-next-heading)))
  15317. (unless to-heading
  15318. (if (memq (preceding-char) '(?\n ?\^M))
  15319. (progn
  15320. ;; Go to end of line before heading
  15321. (forward-char -1)
  15322. (if (memq (preceding-char) '(?\n ?\^M))
  15323. ;; leave blank line before heading
  15324. (forward-char -1))))))
  15325. (point))
  15326. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15327. "Use Org version in org-mode, for dramatic speed-up."
  15328. (if (eq major-mode 'org-mode)
  15329. (progn
  15330. (org-end-of-subtree nil t)
  15331. (backward-char 1))
  15332. ad-do-it))
  15333. (defun org-forward-same-level (arg &optional invisible-ok)
  15334. "Move forward to the arg'th subheading at same level as this one.
  15335. Stop at the first and last subheadings of a superior heading."
  15336. (interactive "p")
  15337. (org-back-to-heading invisible-ok)
  15338. (org-on-heading-p)
  15339. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15340. (re (format "^\\*\\{1,%d\\} " level))
  15341. l)
  15342. (forward-char 1)
  15343. (while (> arg 0)
  15344. (while (and (re-search-forward re nil 'move)
  15345. (setq l (- (match-end 0) (match-beginning 0) 1))
  15346. (= l level)
  15347. (not invisible-ok)
  15348. (org-invisible-p))
  15349. (if (< l level) (setq arg 1)))
  15350. (setq arg (1- arg)))
  15351. (beginning-of-line 1)))
  15352. (defun org-backward-same-level (arg &optional invisible-ok)
  15353. "Move backward to the arg'th subheading at same level as this one.
  15354. Stop at the first and last subheadings of a superior heading."
  15355. (interactive "p")
  15356. (org-back-to-heading)
  15357. (org-on-heading-p)
  15358. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15359. (re (format "^\\*\\{1,%d\\} " level))
  15360. l)
  15361. (while (> arg 0)
  15362. (while (and (re-search-backward re nil 'move)
  15363. (setq l (- (match-end 0) (match-beginning 0) 1))
  15364. (= l level)
  15365. (not invisible-ok)
  15366. (org-invisible-p))
  15367. (if (< l level) (setq arg 1)))
  15368. (setq arg (1- arg)))))
  15369. (defun org-show-subtree ()
  15370. "Show everything after this heading at deeper levels."
  15371. (outline-flag-region
  15372. (point)
  15373. (save-excursion
  15374. (outline-end-of-subtree) (outline-next-heading) (point))
  15375. nil))
  15376. (defun org-show-entry ()
  15377. "Show the body directly following this heading.
  15378. Show the heading too, if it is currently invisible."
  15379. (interactive)
  15380. (save-excursion
  15381. (condition-case nil
  15382. (progn
  15383. (org-back-to-heading t)
  15384. (outline-flag-region
  15385. (max (point-min) (1- (point)))
  15386. (save-excursion
  15387. (if (re-search-forward
  15388. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15389. (match-beginning 1)
  15390. (point-max)))
  15391. nil)
  15392. (org-cycle-hide-drawers 'children))
  15393. (error nil))))
  15394. (defun org-make-options-regexp (kwds &optional extra)
  15395. "Make a regular expression for keyword lines."
  15396. (concat
  15397. "^"
  15398. "#?[ \t]*\\+\\("
  15399. (mapconcat 'regexp-quote kwds "\\|")
  15400. (if extra (concat "\\|" extra))
  15401. "\\):[ \t]*"
  15402. "\\(.*\\)"))
  15403. ;; Make isearch reveal the necessary context
  15404. (defun org-isearch-end ()
  15405. "Reveal context after isearch exits."
  15406. (when isearch-success ; only if search was successful
  15407. (if (featurep 'xemacs)
  15408. ;; Under XEmacs, the hook is run in the correct place,
  15409. ;; we directly show the context.
  15410. (org-show-context 'isearch)
  15411. ;; In Emacs the hook runs *before* restoring the overlays.
  15412. ;; So we have to use a one-time post-command-hook to do this.
  15413. ;; (Emacs 22 has a special variable, see function `org-mode')
  15414. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15415. isearch-mode-end-hook-quit)
  15416. ;; Only when the isearch was not quitted.
  15417. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15418. 'append 'local)))))
  15419. (defun org-isearch-post-command ()
  15420. "Remove self from hook, and show context."
  15421. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15422. (org-show-context 'isearch))
  15423. ;;;; Integration with and fixes for other packages
  15424. ;;; Imenu support
  15425. (defvar org-imenu-markers nil
  15426. "All markers currently used by Imenu.")
  15427. (make-variable-buffer-local 'org-imenu-markers)
  15428. (defun org-imenu-new-marker (&optional pos)
  15429. "Return a new marker for use by Imenu, and remember the marker."
  15430. (let ((m (make-marker)))
  15431. (move-marker m (or pos (point)))
  15432. (push m org-imenu-markers)
  15433. m))
  15434. (defun org-imenu-get-tree ()
  15435. "Produce the index for Imenu."
  15436. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15437. (setq org-imenu-markers nil)
  15438. (let* ((n org-imenu-depth)
  15439. (re (concat "^" outline-regexp))
  15440. (subs (make-vector (1+ n) nil))
  15441. (last-level 0)
  15442. m level head)
  15443. (save-excursion
  15444. (save-restriction
  15445. (widen)
  15446. (goto-char (point-max))
  15447. (while (re-search-backward re nil t)
  15448. (setq level (org-reduced-level (funcall outline-level)))
  15449. (when (<= level n)
  15450. (looking-at org-complex-heading-regexp)
  15451. (setq head (org-link-display-format
  15452. (org-match-string-no-properties 4))
  15453. m (org-imenu-new-marker))
  15454. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15455. (if (>= level last-level)
  15456. (push (cons head m) (aref subs level))
  15457. (push (cons head (aref subs (1+ level))) (aref subs level))
  15458. (loop for i from (1+ level) to n do (aset subs i nil)))
  15459. (setq last-level level)))))
  15460. (aref subs 1)))
  15461. (eval-after-load "imenu"
  15462. '(progn
  15463. (add-hook 'imenu-after-jump-hook
  15464. (lambda ()
  15465. (if (eq major-mode 'org-mode)
  15466. (org-show-context 'org-goto))))))
  15467. (defun org-link-display-format (link)
  15468. "Replace a link with either the description, or the link target
  15469. if no description is present"
  15470. (save-match-data
  15471. (if (string-match org-bracket-link-analytic-regexp link)
  15472. (replace-match (or (match-string 5 link)
  15473. (concat (match-string 1 link)
  15474. (match-string 3 link)))
  15475. nil nil link)
  15476. link)))
  15477. ;; Speedbar support
  15478. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15479. "Overlay marking the agenda restriction line in speedbar.")
  15480. (org-overlay-put org-speedbar-restriction-lock-overlay
  15481. 'face 'org-agenda-restriction-lock)
  15482. (org-overlay-put org-speedbar-restriction-lock-overlay
  15483. 'help-echo "Agendas are currently limited to this item.")
  15484. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15485. (defun org-speedbar-set-agenda-restriction ()
  15486. "Restrict future agenda commands to the location at point in speedbar.
  15487. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15488. (interactive)
  15489. (require 'org-agenda)
  15490. (let (p m tp np dir txt)
  15491. (cond
  15492. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15493. 'org-imenu t))
  15494. (setq m (get-text-property p 'org-imenu-marker))
  15495. (save-excursion
  15496. (save-restriction
  15497. (set-buffer (marker-buffer m))
  15498. (goto-char m)
  15499. (org-agenda-set-restriction-lock 'subtree))))
  15500. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15501. 'speedbar-function 'speedbar-find-file))
  15502. (setq tp (previous-single-property-change
  15503. (1+ p) 'speedbar-function)
  15504. np (next-single-property-change
  15505. tp 'speedbar-function)
  15506. dir (speedbar-line-directory)
  15507. txt (buffer-substring-no-properties (or tp (point-min))
  15508. (or np (point-max))))
  15509. (save-excursion
  15510. (save-restriction
  15511. (set-buffer (find-file-noselect
  15512. (let ((default-directory dir))
  15513. (expand-file-name txt))))
  15514. (unless (org-mode-p)
  15515. (error "Cannot restrict to non-Org-mode file"))
  15516. (org-agenda-set-restriction-lock 'file))))
  15517. (t (error "Don't know how to restrict Org-mode's agenda")))
  15518. (org-move-overlay org-speedbar-restriction-lock-overlay
  15519. (point-at-bol) (point-at-eol))
  15520. (setq current-prefix-arg nil)
  15521. (org-agenda-maybe-redo)))
  15522. (eval-after-load "speedbar"
  15523. '(progn
  15524. (speedbar-add-supported-extension ".org")
  15525. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15526. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15527. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15528. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15529. (add-hook 'speedbar-visiting-tag-hook
  15530. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15531. ;;; Fixes and Hacks for problems with other packages
  15532. ;; Make flyspell not check words in links, to not mess up our keymap
  15533. (defun org-mode-flyspell-verify ()
  15534. "Don't let flyspell put overlays at active buttons."
  15535. (and (not (get-text-property (point) 'keymap))
  15536. (not (get-text-property (point) 'org-no-flyspell))))
  15537. (defun org-remove-flyspell-overlays-in (beg end)
  15538. "Remove flyspell overlays in region."
  15539. (and (org-bound-and-true-p flyspell-mode)
  15540. (fboundp 'flyspell-delete-region-overlays)
  15541. (flyspell-delete-region-overlays beg end))
  15542. (add-text-properties beg end '(org-no-flyspell t)))
  15543. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15544. (eval-after-load "bookmark"
  15545. '(if (boundp 'bookmark-after-jump-hook)
  15546. ;; We can use the hook
  15547. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15548. ;; Hook not available, use advice
  15549. (defadvice bookmark-jump (after org-make-visible activate)
  15550. "Make the position visible."
  15551. (org-bookmark-jump-unhide))))
  15552. ;; Make sure saveplace show the location if it was hidden
  15553. (eval-after-load "saveplace"
  15554. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15555. "Make the position visible."
  15556. (org-bookmark-jump-unhide)))
  15557. (defun org-bookmark-jump-unhide ()
  15558. "Unhide the current position, to show the bookmark location."
  15559. (and (org-mode-p)
  15560. (or (org-invisible-p)
  15561. (save-excursion (goto-char (max (point-min) (1- (point))))
  15562. (org-invisible-p)))
  15563. (org-show-context 'bookmark-jump)))
  15564. ;; Make session.el ignore our circular variable
  15565. (eval-after-load "session"
  15566. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15567. ;;;; Experimental code
  15568. (defun org-closed-in-range ()
  15569. "Sparse tree of items closed in a certain time range.
  15570. Still experimental, may disappear in the future."
  15571. (interactive)
  15572. ;; Get the time interval from the user.
  15573. (let* ((time1 (time-to-seconds
  15574. (org-read-date nil 'to-time nil "Starting date: ")))
  15575. (time2 (time-to-seconds
  15576. (org-read-date nil 'to-time nil "End date:")))
  15577. ;; callback function
  15578. (callback (lambda ()
  15579. (let ((time
  15580. (time-to-seconds
  15581. (apply 'encode-time
  15582. (org-parse-time-string
  15583. (match-string 1))))))
  15584. ;; check if time in interval
  15585. (and (>= time time1) (<= time time2))))))
  15586. ;; make tree, check each match with the callback
  15587. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15588. ;;;; Finish up
  15589. (provide 'org)
  15590. (run-hooks 'org-load-hook)
  15591. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15592. ;;; org.el ends here